Skip to content

Commit 267e1b3

Browse files
joyeecheungaduh95
authored andcommitted
src: add COUNT_GENERIC_USAGE utility for tests
PR-URL: #60434 Fixes: #60423 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 0ecbb80 commit 267e1b3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/api/environment.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "node.h"
77
#include "node_builtins.h"
88
#include "node_context_data.h"
9+
#include "node_debug.h"
910
#include "node_errors.h"
1011
#include "node_exit_code.h"
1112
#include "node_internals.h"
@@ -112,17 +113,21 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
112113

113114
void* NodeArrayBufferAllocator::Allocate(size_t size) {
114115
void* ret;
115-
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
116+
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) {
117+
COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.ZeroFilled");
116118
ret = allocator_->Allocate(size);
117-
else
119+
} else {
120+
COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.Uninitialized");
118121
ret = allocator_->AllocateUninitialized(size);
122+
}
119123
if (ret != nullptr) [[likely]] {
120124
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
121125
}
122126
return ret;
123127
}
124128

125129
void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) {
130+
COUNT_GENERIC_USAGE("NodeArrayBufferAllocator.Allocate.Uninitialized");
126131
void* ret = allocator_->AllocateUninitialized(size);
127132
if (ret != nullptr) [[likely]] {
128133
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);

src/node_debug.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "v8-fast-api-calls.h"
99
#include "v8.h"
1010

11+
#include <string>
1112
#include <string_view>
1213
#include <unordered_map>
1314
#endif // DEBUG
@@ -23,9 +24,20 @@ using v8::Number;
2324
using v8::Object;
2425
using v8::Value;
2526

27+
thread_local std::unordered_map<std::string, int> generic_usage_counters;
2628
thread_local std::unordered_map<FastStringKey, int, FastStringKey::Hash>
2729
v8_fast_api_call_counts;
2830

31+
void CountGenericUsage(const char* counter_name) {
32+
if (generic_usage_counters.find(counter_name) == generic_usage_counters.end())
33+
generic_usage_counters[counter_name] = 0;
34+
generic_usage_counters[counter_name]++;
35+
}
36+
37+
int GetGenericUsageCount(const char* counter_name) {
38+
return generic_usage_counters[counter_name];
39+
}
40+
2941
void TrackV8FastApiCall(FastStringKey key) {
3042
v8_fast_api_call_counts[key]++;
3143
}
@@ -34,6 +46,17 @@ int GetV8FastApiCallCount(FastStringKey key) {
3446
return v8_fast_api_call_counts[key];
3547
}
3648

49+
void GetGenericUsageCount(const FunctionCallbackInfo<Value>& args) {
50+
Environment* env = Environment::GetCurrent(args);
51+
if (!args[0]->IsString()) {
52+
env->ThrowError("getGenericUsageCount must be called with a string");
53+
return;
54+
}
55+
Utf8Value utf8_key(env->isolate(), args[0]);
56+
args.GetReturnValue().Set(
57+
GetGenericUsageCount(utf8_key.ToStringView().data()));
58+
}
59+
3760
void GetV8FastApiCallCount(const FunctionCallbackInfo<Value>& args) {
3861
Environment* env = Environment::GetCurrent(args);
3962
if (!args[0]->IsString()) {
@@ -89,6 +112,7 @@ void Initialize(Local<Object> target,
89112
Local<Context> context,
90113
void* priv) {
91114
SetMethod(context, target, "getV8FastApiCallCount", GetV8FastApiCallCount);
115+
SetMethod(context, target, "getGenericUsageCount", GetGenericUsageCount);
92116
SetFastMethod(context, target, "isEven", SlowIsEven, &fast_is_even);
93117
SetFastMethod(context, target, "isOdd", SlowIsOdd, &fast_is_odd);
94118
}

src/node_debug.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ namespace debug {
1313
void TrackV8FastApiCall(FastStringKey key);
1414
int GetV8FastApiCallCount(FastStringKey key);
1515

16+
void CountGenericUsage(const char* counter_name);
17+
#define COUNT_GENERIC_USAGE(name) node::debug::CountGenericUsage(name)
18+
1619
#define TRACK_V8_FAST_API_CALL(key) \
1720
node::debug::TrackV8FastApiCall(FastStringKey(key))
1821
#else // !DEBUG
1922
#define TRACK_V8_FAST_API_CALL(key)
23+
#define COUNT_GENERIC_USAGE(name)
2024
#endif // DEBUG
2125

2226
} // namespace debug

0 commit comments

Comments
 (0)