From 11a8559a30e937b06d5a8b1cd598f315970378e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Wed, 26 Jun 2024 18:42:06 -0300 Subject: [PATCH 1/6] src,lib: expose getCategoryEnabledBuffer to use on node.http Instead call the C++ code every time we need to check for a trace category, now we get the C++ pointer to the flag that holds the info if the trace is enabled and return this pointer inside a buffer that we can use to call/check if the value is enabled. With this change, no C++ call is made and the access to the info happens in JS side, which has no perf penalty. --- lib/internal/http.js | 6 ++-- src/node_trace_events.cc | 31 ++++++++++++++++ ...ace-events-get-category-enabled-buffer.mjs | 35 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-trace-events-get-category-enabled-buffer.mjs diff --git a/lib/internal/http.js b/lib/internal/http.js index da1e8d3332de0e..1eaf0e70f4f8ac 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -8,7 +8,7 @@ const { } = primordials; const { setUnrefTimeout } = require('internal/timers'); -const { trace, isTraceCategoryEnabled } = internalBinding('trace_events'); +const { getCategoryEnabledBuffer, trace } = internalBinding('trace_events'); const { CHAR_LOWERCASE_B, CHAR_LOWERCASE_E, @@ -37,8 +37,10 @@ function getNextTraceEventId() { return ++traceEventId; } +const httpEnabled = getCategoryEnabledBuffer('node.http'); + function isTraceHTTPEnabled() { - return isTraceCategoryEnabled('node.http'); + return httpEnabled[0] > 0; } const traceEventCategory = 'node,node.http'; diff --git a/src/node_trace_events.cc b/src/node_trace_events.cc index a0f8f4de67dabb..4a783138075c5f 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -16,6 +16,8 @@ namespace node { class ExternalReferenceRegistry; using v8::Array; +using v8::ArrayBuffer; +using v8::BackingStore; using v8::Context; using v8::Function; using v8::FunctionCallbackInfo; @@ -25,6 +27,7 @@ using v8::Local; using v8::NewStringType; using v8::Object; using v8::String; +using v8::Uint8Array; using v8::Value; class NodeCategorySet : public BaseObject { @@ -120,6 +123,29 @@ static void SetTraceCategoryStateUpdateHandler( env->set_trace_category_state_function(args[0].As()); } +static void GetCategoryEnabledBuffer( + const FunctionCallbackInfo& args) { + CHECK(args[0]->IsString()); + + Isolate* isolate = args.GetIsolate(); + node::Utf8Value category_name(isolate, args[0]); + + const uint8_t* enabled_pointer = TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( + category_name.out()); + uint8_t* enabled_pointer_cast = const_cast( + enabled_pointer); + + std::unique_ptr bs = ArrayBuffer::NewBackingStore( + enabled_pointer_cast, + sizeof(*enabled_pointer_cast), + [](void*, size_t, void*) {}, + nullptr); + auto ab = ArrayBuffer::New(isolate, std::move(bs)); + v8::Local u8 = v8::Uint8Array::New(ab, 0, 1); + + args.GetReturnValue().Set(u8); +} + void NodeCategorySet::Initialize(Local target, Local unused, Local context, @@ -132,6 +158,10 @@ void NodeCategorySet::Initialize(Local target, target, "setTraceCategoryStateUpdateHandler", SetTraceCategoryStateUpdateHandler); + SetMethod(context, + target, + "getCategoryEnabledBuffer", + GetCategoryEnabledBuffer); Local category_set = NewFunctionTemplate(isolate, NodeCategorySet::New); @@ -160,6 +190,7 @@ void NodeCategorySet::RegisterExternalReferences( ExternalReferenceRegistry* registry) { registry->Register(GetEnabledCategories); registry->Register(SetTraceCategoryStateUpdateHandler); + registry->Register(GetCategoryEnabledBuffer); registry->Register(NodeCategorySet::New); registry->Register(NodeCategorySet::Enable); registry->Register(NodeCategorySet::Disable); diff --git a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs b/test/parallel/test-trace-events-get-category-enabled-buffer.mjs new file mode 100644 index 00000000000000..d44dd8f7d64aa9 --- /dev/null +++ b/test/parallel/test-trace-events-get-category-enabled-buffer.mjs @@ -0,0 +1,35 @@ +// Flags: --expose-internals + +import '../common/index.mjs'; +import { it } from 'node:test'; +import { createTracing, getEnabledCategories } from 'node:trace_events'; +import assert from 'node:assert'; + +import binding from 'internal/test/binding'; +const getCategoryEnabledBuffer = binding.internalBinding('trace_events').getCategoryEnabledBuffer; + +it('should track enabled/disabled categories', () => { + const random = Math.random().toString().slice(2); + const category = `node.${random}`; + + const buffer = getCategoryEnabledBuffer(category); + + const tracing = createTracing({ + categories: [category], + }); + + assert.ok(buffer[0] === 0, `the buffer[0] should start with value 0, got: ${buffer[0]}`); + + tracing.enable(); + + let currentCategories = getEnabledCategories(); + + assert.ok(currentCategories.includes(category), `the getEnabledCategories should include ${category}, got: ${currentCategories}`); + assert.ok(buffer[0] > 0, `the buffer[0] should be greater than 0, got: ${buffer[0]}`); + + tracing.disable(); + + currentCategories = getEnabledCategories(); + assert.ok(currentCategories === undefined, `the getEnabledCategories should return undefined, got: ${currentCategories}`); + assert.ok(buffer[0] === 0, `the buffer[0] should be 0, got: ${buffer[0]}`); +}); From 2288aaadd3592070f248dab2ee77a3239cd405e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Wed, 26 Jun 2024 22:08:30 -0300 Subject: [PATCH 2/6] fixup! src,lib: expose getCategoryEnabledBuffer to use on node.http --- src/node_trace_events.cc | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/node_trace_events.cc b/src/node_trace_events.cc index 4a783138075c5f..9787b14352753c 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -123,23 +123,21 @@ static void SetTraceCategoryStateUpdateHandler( env->set_trace_category_state_function(args[0].As()); } -static void GetCategoryEnabledBuffer( - const FunctionCallbackInfo& args) { +static void GetCategoryEnabledBuffer(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); Isolate* isolate = args.GetIsolate(); node::Utf8Value category_name(isolate, args[0]); - const uint8_t* enabled_pointer = TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( - category_name.out()); - uint8_t* enabled_pointer_cast = const_cast( - enabled_pointer); + const uint8_t* enabled_pointer = + TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_name.out()); + uint8_t* enabled_pointer_cast = const_cast(enabled_pointer); std::unique_ptr bs = ArrayBuffer::NewBackingStore( - enabled_pointer_cast, - sizeof(*enabled_pointer_cast), - [](void*, size_t, void*) {}, - nullptr); + enabled_pointer_cast, + sizeof(*enabled_pointer_cast), + [](void*, size_t, void*) {}, + nullptr); auto ab = ArrayBuffer::New(isolate, std::move(bs)); v8::Local u8 = v8::Uint8Array::New(ab, 0, 1); @@ -158,10 +156,8 @@ void NodeCategorySet::Initialize(Local target, target, "setTraceCategoryStateUpdateHandler", SetTraceCategoryStateUpdateHandler); - SetMethod(context, - target, - "getCategoryEnabledBuffer", - GetCategoryEnabledBuffer); + SetMethod( + context, target, "getCategoryEnabledBuffer", GetCategoryEnabledBuffer); Local category_set = NewFunctionTemplate(isolate, NodeCategorySet::New); From dcaf9e8c7c3e7eb6358dbab7bfbc53fe06c36394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Thu, 11 Jul 2024 19:41:27 -0300 Subject: [PATCH 3/6] fix issue when trace events is missing --- .../test-trace-events-get-category-enabled-buffer.mjs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs b/test/parallel/test-trace-events-get-category-enabled-buffer.mjs index d44dd8f7d64aa9..036d65fc4b4111 100644 --- a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs +++ b/test/parallel/test-trace-events-get-category-enabled-buffer.mjs @@ -2,6 +2,13 @@ import '../common/index.mjs'; import { it } from 'node:test'; + +try { + await import('node:trace_events'); +} catch { + common.skip('missing trace events'); +} + import { createTracing, getEnabledCategories } from 'node:trace_events'; import assert from 'node:assert'; From 60f47e984fadaa4fd732b1aebb9dbc44d53b95f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Thu, 11 Jul 2024 19:47:04 -0300 Subject: [PATCH 4/6] fix missing common --- test/parallel/test-trace-events-get-category-enabled-buffer.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs b/test/parallel/test-trace-events-get-category-enabled-buffer.mjs index 036d65fc4b4111..183b66fb5c64d5 100644 --- a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs +++ b/test/parallel/test-trace-events-get-category-enabled-buffer.mjs @@ -1,6 +1,6 @@ // Flags: --expose-internals -import '../common/index.mjs'; +import * as common from '../common/index.mjs'; import { it } from 'node:test'; try { From 0cc45a4f7d96ebe611268672e5c818f640fd9e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Mon, 15 Jul 2024 17:35:39 -0300 Subject: [PATCH 5/6] change to cjs to fix issue with trace events on unsupported platforms --- ...test-trace-events-get-category-enabled-buffer.js} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename test/parallel/{test-trace-events-get-category-enabled-buffer.mjs => test-trace-events-get-category-enabled-buffer.js} (80%) diff --git a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs b/test/parallel/test-trace-events-get-category-enabled-buffer.js similarity index 80% rename from test/parallel/test-trace-events-get-category-enabled-buffer.mjs rename to test/parallel/test-trace-events-get-category-enabled-buffer.js index 183b66fb5c64d5..be6baf757596f5 100644 --- a/test/parallel/test-trace-events-get-category-enabled-buffer.mjs +++ b/test/parallel/test-trace-events-get-category-enabled-buffer.js @@ -1,18 +1,18 @@ // Flags: --expose-internals -import * as common from '../common/index.mjs'; -import { it } from 'node:test'; +const common = require('../common/index'); +const { it } = require('node:test'); try { - await import('node:trace_events'); + require('trace_events'); } catch { common.skip('missing trace events'); } -import { createTracing, getEnabledCategories } from 'node:trace_events'; -import assert from 'node:assert'; +const { createTracing, getEnabledCategories } = require('trace_events'); +const assert = require('assert'); -import binding from 'internal/test/binding'; +const binding = require('internal/test/binding'); const getCategoryEnabledBuffer = binding.internalBinding('trace_events').getCategoryEnabledBuffer; it('should track enabled/disabled categories', () => { From 3206bd78f95e298b40ce02ef464e9260fbaa9ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Mon, 15 Jul 2024 18:34:49 -0300 Subject: [PATCH 6/6] fix lint --- test/parallel/test-trace-events-get-category-enabled-buffer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-trace-events-get-category-enabled-buffer.js b/test/parallel/test-trace-events-get-category-enabled-buffer.js index be6baf757596f5..3017b8e6dc87d7 100644 --- a/test/parallel/test-trace-events-get-category-enabled-buffer.js +++ b/test/parallel/test-trace-events-get-category-enabled-buffer.js @@ -1,6 +1,7 @@ +'use strict'; // Flags: --expose-internals -const common = require('../common/index'); +const common = require('../common'); const { it } = require('node:test'); try {