src: add COUNT_GENERIC_USAGE utility for tests

PR-URL: https://github.com/nodejs/node/pull/60434
Fixes: https://github.com/nodejs/node/issues/60423
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Richard Lau <richard.lau@ibm.com>
This commit is contained in:
Joyee Cheung 2025-10-27 13:42:30 +01:00 committed by Node.js GitHub Bot
parent 51a57f2b36
commit cfbfc1b050
3 changed files with 35 additions and 2 deletions

View File

@ -6,6 +6,7 @@
#include "node.h" #include "node.h"
#include "node_builtins.h" #include "node_builtins.h"
#include "node_context_data.h" #include "node_context_data.h"
#include "node_debug.h"
#include "node_errors.h" #include "node_errors.h"
#include "node_exit_code.h" #include "node_exit_code.h"
#include "node_internals.h" #include "node_internals.h"
@ -112,10 +113,13 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
void* NodeArrayBufferAllocator::Allocate(size_t size) { void* NodeArrayBufferAllocator::Allocate(size_t size) {
void* ret; void* ret;
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) {
COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.ZeroFilled");
ret = allocator_->Allocate(size); ret = allocator_->Allocate(size);
else } else {
COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.Uninitialized");
ret = allocator_->AllocateUninitialized(size); ret = allocator_->AllocateUninitialized(size);
}
if (ret != nullptr) [[likely]] { if (ret != nullptr) [[likely]] {
total_mem_usage_.fetch_add(size, std::memory_order_relaxed); total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
} }
@ -123,6 +127,7 @@ void* NodeArrayBufferAllocator::Allocate(size_t size) {
} }
void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) { void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) {
COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.Uninitialized");
void* ret = allocator_->AllocateUninitialized(size); void* ret = allocator_->AllocateUninitialized(size);
if (ret != nullptr) [[likely]] { if (ret != nullptr) [[likely]] {
total_mem_usage_.fetch_add(size, std::memory_order_relaxed); total_mem_usage_.fetch_add(size, std::memory_order_relaxed);

View File

@ -8,6 +8,7 @@
#include "v8-fast-api-calls.h" #include "v8-fast-api-calls.h"
#include "v8.h" #include "v8.h"
#include <string>
#include <string_view> #include <string_view>
#include <unordered_map> #include <unordered_map>
#endif // DEBUG #endif // DEBUG
@ -23,9 +24,20 @@ using v8::Number;
using v8::Object; using v8::Object;
using v8::Value; using v8::Value;
thread_local std::unordered_map<std::string, int> generic_usage_counters;
thread_local std::unordered_map<FastStringKey, int, FastStringKey::Hash> thread_local std::unordered_map<FastStringKey, int, FastStringKey::Hash>
v8_fast_api_call_counts; v8_fast_api_call_counts;
void CountGenericUsage(const char* counter_name) {
if (generic_usage_counters.find(counter_name) == generic_usage_counters.end())
generic_usage_counters[counter_name] = 0;
generic_usage_counters[counter_name]++;
}
int GetGenericUsageCount(const char* counter_name) {
return generic_usage_counters[counter_name];
}
void TrackV8FastApiCall(FastStringKey key) { void TrackV8FastApiCall(FastStringKey key) {
v8_fast_api_call_counts[key]++; v8_fast_api_call_counts[key]++;
} }
@ -34,6 +46,17 @@ int GetV8FastApiCallCount(FastStringKey key) {
return v8_fast_api_call_counts[key]; return v8_fast_api_call_counts[key];
} }
void GetGenericUsageCount(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsString()) {
env->ThrowError("getGenericUsageCount must be called with a string");
return;
}
Utf8Value utf8_key(env->isolate(), args[0]);
args.GetReturnValue().Set(
GetGenericUsageCount(utf8_key.ToStringView().data()));
}
void GetV8FastApiCallCount(const FunctionCallbackInfo<Value>& args) { void GetV8FastApiCallCount(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsString()) { if (!args[0]->IsString()) {
@ -89,6 +112,7 @@ void Initialize(Local<Object> target,
Local<Context> context, Local<Context> context,
void* priv) { void* priv) {
SetMethod(context, target, "getV8FastApiCallCount", GetV8FastApiCallCount); SetMethod(context, target, "getV8FastApiCallCount", GetV8FastApiCallCount);
SetMethod(context, target, "getGenericUsageCount", GetGenericUsageCount);
SetFastMethod(context, target, "isEven", SlowIsEven, &fast_is_even); SetFastMethod(context, target, "isEven", SlowIsEven, &fast_is_even);
SetFastMethod(context, target, "isOdd", SlowIsOdd, &fast_is_odd); SetFastMethod(context, target, "isOdd", SlowIsOdd, &fast_is_odd);
} }

View File

@ -13,10 +13,14 @@ namespace debug {
void TrackV8FastApiCall(FastStringKey key); void TrackV8FastApiCall(FastStringKey key);
int GetV8FastApiCallCount(FastStringKey key); int GetV8FastApiCallCount(FastStringKey key);
void CountGenericUsage(const char* counter_name);
#define COUNT_GENERIC_USAGE(name) node::debug::CountGenericUsage(name)
#define TRACK_V8_FAST_API_CALL(key) \ #define TRACK_V8_FAST_API_CALL(key) \
node::debug::TrackV8FastApiCall(FastStringKey(key)) node::debug::TrackV8FastApiCall(FastStringKey(key))
#else // !DEBUG #else // !DEBUG
#define TRACK_V8_FAST_API_CALL(key) #define TRACK_V8_FAST_API_CALL(key)
#define COUNT_GENERIC_USAGE(name)
#endif // DEBUG #endif // DEBUG
} // namespace debug } // namespace debug