diff --git a/src/api/environment.cc b/src/api/environment.cc index b50325f092..8126749cc2 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -6,6 +6,7 @@ #include "node.h" #include "node_builtins.h" #include "node_context_data.h" +#include "node_debug.h" #include "node_errors.h" #include "node_exit_code.h" #include "node_internals.h" @@ -112,10 +113,13 @@ MaybeLocal PrepareStackTraceCallback(Local context, void* NodeArrayBufferAllocator::Allocate(size_t size) { 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); - else + } else { + COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.Uninitialized"); ret = allocator_->AllocateUninitialized(size); + } if (ret != nullptr) [[likely]] { 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) { + COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.Uninitialized"); void* ret = allocator_->AllocateUninitialized(size); if (ret != nullptr) [[likely]] { total_mem_usage_.fetch_add(size, std::memory_order_relaxed); diff --git a/src/node_debug.cc b/src/node_debug.cc index bb0e11747e..8dbc79004f 100644 --- a/src/node_debug.cc +++ b/src/node_debug.cc @@ -8,6 +8,7 @@ #include "v8-fast-api-calls.h" #include "v8.h" +#include #include #include #endif // DEBUG @@ -23,9 +24,20 @@ using v8::Number; using v8::Object; using v8::Value; +thread_local std::unordered_map generic_usage_counters; thread_local std::unordered_map 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) { v8_fast_api_call_counts[key]++; } @@ -34,6 +46,17 @@ int GetV8FastApiCallCount(FastStringKey key) { return v8_fast_api_call_counts[key]; } +void GetGenericUsageCount(const FunctionCallbackInfo& 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& args) { Environment* env = Environment::GetCurrent(args); if (!args[0]->IsString()) { @@ -89,6 +112,7 @@ void Initialize(Local target, Local context, void* priv) { SetMethod(context, target, "getV8FastApiCallCount", GetV8FastApiCallCount); + SetMethod(context, target, "getGenericUsageCount", GetGenericUsageCount); SetFastMethod(context, target, "isEven", SlowIsEven, &fast_is_even); SetFastMethod(context, target, "isOdd", SlowIsOdd, &fast_is_odd); } diff --git a/src/node_debug.h b/src/node_debug.h index 32fbf21c81..6d9e03a672 100644 --- a/src/node_debug.h +++ b/src/node_debug.h @@ -13,10 +13,14 @@ namespace debug { void TrackV8FastApiCall(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) \ node::debug::TrackV8FastApiCall(FastStringKey(key)) #else // !DEBUG #define TRACK_V8_FAST_API_CALL(key) +#define COUNT_GENERIC_USAGE(name) #endif // DEBUG } // namespace debug