Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31fdb88

Browse files
H4admarco-ippolito
authored andcommitted
src,lib: expose getCategoryEnabledBuffer to use on node.http
Instead call the C++ code every time we need to check for atrace category, now we get the C++ pointer to the flag thatholds the info if the trace is enabled and return this pointerinside a buffer that we can use to call/check if the value isenabled. With this change, no C++ call is made and the accessto the info happens in JS side, which has no perf penalty.PR-URL:#53602Reviewed-By: James M Snell <jasnell@gmail.com>Reviewed-By: Matteo Collina <matteo.collina@gmail.com>Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent977af25 commit31fdb88

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

‎lib/internal/http.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
}=primordials;
99

1010
const{ setUnrefTimeout}=require('internal/timers');
11-
const{trace, isTraceCategoryEnabled}=internalBinding('trace_events');
11+
const{getCategoryEnabledBuffer, trace}=internalBinding('trace_events');
1212
const{
1313
CHAR_LOWERCASE_B,
1414
CHAR_LOWERCASE_E,
@@ -37,8 +37,10 @@ function getNextTraceEventId() {
3737
return++traceEventId;
3838
}
3939

40+
consthttpEnabled=getCategoryEnabledBuffer('node.http');
41+
4042
functionisTraceHTTPEnabled(){
41-
returnisTraceCategoryEnabled('node.http');
43+
returnhttpEnabled[0]>0;
4244
}
4345

4446
consttraceEventCategory='node,node.http';

‎src/node_trace_events.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace node {
1616
classExternalReferenceRegistry;
1717

1818
using v8::Array;
19+
using v8::ArrayBuffer;
20+
using v8::BackingStore;
1921
using v8::Context;
2022
using v8::Function;
2123
using v8::FunctionCallbackInfo;
@@ -25,6 +27,7 @@ using v8::Local;
2527
using v8::NewStringType;
2628
using v8::Object;
2729
using v8::String;
30+
using v8::Uint8Array;
2831
using v8::Value;
2932

3033
classNodeCategorySet :publicBaseObject {
@@ -120,6 +123,27 @@ static void SetTraceCategoryStateUpdateHandler(
120123
env->set_trace_category_state_function(args[0].As<Function>());
121124
}
122125

126+
staticvoidGetCategoryEnabledBuffer(const FunctionCallbackInfo<Value>& args) {
127+
CHECK(args[0]->IsString());
128+
129+
Isolate* isolate = args.GetIsolate();
130+
node::Utf8Valuecategory_name(isolate, args[0]);
131+
132+
constuint8_t* enabled_pointer =
133+
TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_name.out());
134+
uint8_t* enabled_pointer_cast =const_cast<uint8_t*>(enabled_pointer);
135+
136+
std::unique_ptr<BackingStore> bs =ArrayBuffer::NewBackingStore(
137+
enabled_pointer_cast,
138+
sizeof(*enabled_pointer_cast),
139+
[](void*,size_t,void*) {},
140+
nullptr);
141+
auto ab =ArrayBuffer::New(isolate,std::move(bs));
142+
v8::Local<Uint8Array>u8 =v8::Uint8Array::New(ab,0,1);
143+
144+
args.GetReturnValue().Set(u8);
145+
}
146+
123147
voidNodeCategorySet::Initialize(Local<Object> target,
124148
Local<Value> unused,
125149
Local<Context> context,
@@ -132,6 +156,8 @@ void NodeCategorySet::Initialize(Local<Object> target,
132156
target,
133157
"setTraceCategoryStateUpdateHandler",
134158
SetTraceCategoryStateUpdateHandler);
159+
SetMethod(
160+
context, target,"getCategoryEnabledBuffer", GetCategoryEnabledBuffer);
135161

136162
Local<FunctionTemplate> category_set =
137163
NewFunctionTemplate(isolate, NodeCategorySet::New);
@@ -160,6 +186,7 @@ void NodeCategorySet::RegisterExternalReferences(
160186
ExternalReferenceRegistry* registry) {
161187
registry->Register(GetEnabledCategories);
162188
registry->Register(SetTraceCategoryStateUpdateHandler);
189+
registry->Register(GetCategoryEnabledBuffer);
163190
registry->Register(NodeCategorySet::New);
164191
registry->Register(NodeCategorySet::Enable);
165192
registry->Register(NodeCategorySet::Disable);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
// Flags: --expose-internals
3+
4+
constcommon=require('../common');
5+
const{ it}=require('node:test');
6+
7+
try{
8+
require('trace_events');
9+
}catch{
10+
common.skip('missing trace events');
11+
}
12+
13+
const{ createTracing, getEnabledCategories}=require('trace_events');
14+
constassert=require('assert');
15+
16+
constbinding=require('internal/test/binding');
17+
constgetCategoryEnabledBuffer=binding.internalBinding('trace_events').getCategoryEnabledBuffer;
18+
19+
it('should track enabled/disabled categories',()=>{
20+
constrandom=Math.random().toString().slice(2);
21+
constcategory=`node.${random}`;
22+
23+
constbuffer=getCategoryEnabledBuffer(category);
24+
25+
consttracing=createTracing({
26+
categories:[category],
27+
});
28+
29+
assert.ok(buffer[0]===0,`the buffer[0] should start with value 0, got:${buffer[0]}`);
30+
31+
tracing.enable();
32+
33+
letcurrentCategories=getEnabledCategories();
34+
35+
assert.ok(currentCategories.includes(category),`the getEnabledCategories should include${category}, got:${currentCategories}`);
36+
assert.ok(buffer[0]>0,`the buffer[0] should be greater than 0, got:${buffer[0]}`);
37+
38+
tracing.disable();
39+
40+
currentCategories=getEnabledCategories();
41+
assert.ok(currentCategories===undefined,`the getEnabledCategories should return undefined, got:${currentCategories}`);
42+
assert.ok(buffer[0]===0,`the buffer[0] should be 0, got:${buffer[0]}`);
43+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp