Skip to content

Commit b854604

Browse files
committed
src: remove old process.binding('trace_events').emit
Remove the older emit and categoryGroupEnabled bindings in favor of the new intrinsics PR-URL: #22127 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
1 parent c85933c commit b854604

File tree

5 files changed

+22
-167
lines changed

5 files changed

+22
-167
lines changed

benchmark/misc/trace.js

+2-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const common = require('../common.js');
44

55
const bench = common.createBenchmark(main, {
66
n: [100000],
7-
method: ['trace', 'emit', 'isTraceCategoryEnabled', 'categoryGroupEnabled']
7+
method: ['trace', 'isTraceCategoryEnabled']
88
}, {
99
flags: ['--expose-internals', '--trace-event-categories', 'foo']
1010
});
@@ -13,14 +13,6 @@ const {
1313
TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN: kBeforeEvent
1414
} = process.binding('constants').trace;
1515

16-
function doEmit(n, emit) {
17-
bench.start();
18-
for (var i = 0; i < n; i++) {
19-
emit(kBeforeEvent, 'foo', 'test', 0, 'arg1', 1);
20-
}
21-
bench.end(n);
22-
}
23-
2416
function doTrace(n, trace) {
2517
bench.start();
2618
for (var i = 0; i < n; i++) {
@@ -38,39 +30,22 @@ function doIsTraceCategoryEnabled(n, isTraceCategoryEnabled) {
3830
bench.end(n);
3931
}
4032

41-
function doCategoryGroupEnabled(n, categoryGroupEnabled) {
42-
bench.start();
43-
for (var i = 0; i < n; i++) {
44-
categoryGroupEnabled('foo');
45-
categoryGroupEnabled('bar');
46-
}
47-
bench.end(n);
48-
}
49-
5033
function main({ n, method }) {
5134
const { internalBinding } = require('internal/test/binding');
5235

5336
const {
5437
trace,
55-
isTraceCategoryEnabled,
56-
emit,
57-
categoryGroupEnabled
38+
isTraceCategoryEnabled
5839
} = internalBinding('trace_events');
5940

6041
switch (method) {
6142
case '':
6243
case 'trace':
6344
doTrace(n, trace);
6445
break;
65-
case 'emit':
66-
doEmit(n, emit);
67-
break;
6846
case 'isTraceCategoryEnabled':
6947
doIsTraceCategoryEnabled(n, isTraceCategoryEnabled);
7048
break;
71-
case 'categoryGroupEnabled':
72-
doCategoryGroupEnabled(n, categoryGroupEnabled);
73-
break;
7449
default:
7550
throw new Error(`Unexpected method "${method}"`);
7651
}

lib/internal/bootstrap/node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
const traceEvents = internalBinding('trace_events');
104104
const traceEventCategory = 'node,node.async_hooks';
105105

106-
if (traceEvents.categoryGroupEnabled(traceEventCategory)) {
106+
if (traceEvents.isTraceCategoryEnabled(traceEventCategory)) {
107107
NativeModule.require('internal/trace_events_async_hooks')
108108
.setup(traceEvents, traceEventCategory);
109109
}

src/node_trace_events.cc

-126
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ using v8::Array;
1313
using v8::Context;
1414
using v8::FunctionCallbackInfo;
1515
using v8::FunctionTemplate;
16-
using v8::Int32;
1716
using v8::Local;
1817
using v8::Object;
1918
using v8::String;
@@ -99,137 +98,12 @@ void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
9998
}
10099
}
101100

102-
// The tracing APIs require category groups to be pointers to long-lived
103-
// strings. Those strings are stored here.
104-
static std::unordered_set<std::string> category_groups;
105-
static Mutex category_groups_mutex;
106-
107-
// Gets a pointer to the category-enabled flags for a tracing category group,
108-
// if tracing is enabled for it.
109-
static const uint8_t* GetCategoryGroupEnabled(const char* category_group) {
110-
CHECK_NOT_NULL(category_group);
111-
return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
112-
}
113-
114-
static const char* GetCategoryGroup(Environment* env,
115-
const Local<Value> category_value) {
116-
CHECK(category_value->IsString());
117-
118-
Utf8Value category(env->isolate(), category_value);
119-
Mutex::ScopedLock lock(category_groups_mutex);
120-
// If the value already exists in the set, insertion.first will point
121-
// to the existing value. Thus, this will maintain a long lived pointer
122-
// to the category c-string.
123-
auto insertion = category_groups.insert(category.out());
124-
125-
// The returned insertion is a pair whose first item is the object that was
126-
// inserted or that blocked the insertion and second item is a boolean
127-
// indicating whether it was inserted.
128-
return insertion.first->c_str();
129-
}
130-
131-
static void Emit(const FunctionCallbackInfo<Value>& args) {
132-
Environment* env = Environment::GetCurrent(args);
133-
Local<Context> context = env->context();
134-
135-
// Args: [type, category, name, id, argName, argValue]
136-
CHECK_GE(args.Length(), 3);
137-
138-
// Check the category group first, to avoid doing more work if it's not
139-
// enabled.
140-
const char* category_group = GetCategoryGroup(env, args[1]);
141-
const uint8_t* category_group_enabled =
142-
GetCategoryGroupEnabled(category_group);
143-
if (*category_group_enabled == 0) return;
144-
145-
// get trace_event phase
146-
CHECK(args[0]->IsNumber());
147-
char phase = static_cast<char>(args[0]->Int32Value(context).ToChecked());
148-
149-
// get trace_event name
150-
CHECK(args[2]->IsString());
151-
Utf8Value name_value(env->isolate(), args[2]);
152-
const char* name = name_value.out();
153-
154-
// get trace_event id
155-
int64_t id = 0;
156-
bool has_id = false;
157-
if (args.Length() >= 4 && !args[3]->IsUndefined() && !args[3]->IsNull()) {
158-
has_id = true;
159-
CHECK(args[3]->IsNumber());
160-
id = args[3]->IntegerValue(context).ToChecked();
161-
}
162-
163-
// TODO(AndreasMadsen): String values are not supported.
164-
int32_t num_args = 0;
165-
const char* arg_names[2];
166-
uint8_t arg_types[2];
167-
uint64_t arg_values[2];
168-
169-
// Create Utf8Value in the function scope to prevent deallocation.
170-
// The c-string will be copied by TRACE_EVENT_API_ADD_TRACE_EVENT at the end.
171-
Utf8Value arg1NameValue(env->isolate(), args[4]);
172-
Utf8Value arg2NameValue(env->isolate(), args[6]);
173-
174-
if (args.Length() >= 6 &&
175-
(!args[4]->IsUndefined() || !args[5]->IsUndefined())) {
176-
num_args = 1;
177-
arg_types[0] = TRACE_VALUE_TYPE_INT;
178-
179-
CHECK(args[4]->IsString());
180-
arg_names[0] = arg1NameValue.out();
181-
182-
CHECK(args[5]->IsNumber());
183-
arg_values[0] = args[5]->IntegerValue(context).ToChecked();
184-
}
185-
186-
if (args.Length() >= 8 &&
187-
(!args[6]->IsUndefined() || !args[7]->IsUndefined())) {
188-
num_args = 2;
189-
arg_types[1] = TRACE_VALUE_TYPE_INT;
190-
191-
CHECK(args[6]->IsString());
192-
arg_names[1] = arg2NameValue.out();
193-
194-
CHECK(args[7]->IsNumber());
195-
arg_values[1] = args[7]->IntegerValue(context).ToChecked();
196-
}
197-
198-
// The TRACE_EVENT_FLAG_COPY flag indicates that the name and argument
199-
// name should be copied thus they don't need to long-lived pointers.
200-
// The category name still won't be copied and thus need to be a long-lived
201-
// pointer.
202-
uint32_t flags = TRACE_EVENT_FLAG_COPY;
203-
if (has_id) {
204-
flags |= TRACE_EVENT_FLAG_HAS_ID;
205-
}
206-
207-
const char* scope = node::tracing::kGlobalScope;
208-
uint64_t bind_id = node::tracing::kNoId;
209-
210-
TRACE_EVENT_API_ADD_TRACE_EVENT(
211-
phase, category_group_enabled, name, scope, id, bind_id,
212-
num_args, arg_names, arg_types, arg_values,
213-
flags);
214-
}
215-
216-
static void CategoryGroupEnabled(const FunctionCallbackInfo<Value>& args) {
217-
Environment* env = Environment::GetCurrent(args);
218-
219-
const char* category_group = GetCategoryGroup(env, args[0]);
220-
const uint8_t* category_group_enabled =
221-
GetCategoryGroupEnabled(category_group);
222-
args.GetReturnValue().Set(*category_group_enabled > 0);
223-
}
224-
225101
void Initialize(Local<Object> target,
226102
Local<Value> unused,
227103
Local<Context> context,
228104
void* priv) {
229105
Environment* env = Environment::GetCurrent(context);
230106

231-
env->SetMethod(target, "emit", Emit);
232-
env->SetMethod(target, "categoryGroupEnabled", CategoryGroupEnabled);
233107
env->SetMethod(target, "getEnabledCategories", GetEnabledCategories);
234108

235109
Local<FunctionTemplate> category_set =

test/parallel/test-trace-events-binding.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ if (!common.isMainThread)
99

1010
const CODE = `
1111
const { internalBinding } = require('internal/test/binding');
12-
const { emit } = internalBinding('trace_events');
13-
emit('b'.charCodeAt(0), 'custom',
14-
'type-value', 10, 'extra-value', 20);
15-
emit('b'.charCodeAt(0), 'custom',
16-
'type-value', 20, 'first-value', 20, 'second-value', 30);
17-
emit('b'.charCodeAt(0), 'custom',
18-
'type-value', 30);
19-
emit('b'.charCodeAt(0), 'missing',
20-
'type-value', 10, 'extra-value', 20);
12+
const { trace } = internalBinding('trace_events');
13+
trace('b'.charCodeAt(0), 'custom',
14+
'type-value', 10, {'extra-value': 20 });
15+
trace('b'.charCodeAt(0), 'custom',
16+
'type-value', 20, {'first-value': 20, 'second-value': 30 });
17+
trace('b'.charCodeAt(0), 'custom', 'type-value', 30);
18+
trace('b'.charCodeAt(0), 'missing',
19+
'type-value', 10, {'extra-value': 20 });
2120
`;
2221
const FILE_NAME = 'node_trace.1.log';
2322

@@ -27,6 +26,7 @@ process.chdir(tmpdir.path);
2726

2827
const proc = cp.spawn(process.execPath,
2928
[ '--trace-event-categories', 'custom',
29+
'--no-warnings',
3030
'--expose-internals',
3131
'-e', CODE ]);
3232

@@ -42,14 +42,14 @@ proc.once('exit', common.mustCall(() => {
4242
assert.strictEqual(traces[0].cat, 'custom');
4343
assert.strictEqual(traces[0].name, 'type-value');
4444
assert.strictEqual(traces[0].id, '0xa');
45-
assert.deepStrictEqual(traces[0].args, { 'extra-value': 20 });
45+
assert.deepStrictEqual(traces[0].args.data, { 'extra-value': 20 });
4646

4747
assert.strictEqual(traces[1].pid, proc.pid);
4848
assert.strictEqual(traces[1].ph, 'b');
4949
assert.strictEqual(traces[1].cat, 'custom');
5050
assert.strictEqual(traces[1].name, 'type-value');
5151
assert.strictEqual(traces[1].id, '0x14');
52-
assert.deepStrictEqual(traces[1].args, {
52+
assert.deepStrictEqual(traces[1].args.data, {
5353
'first-value': 20,
5454
'second-value': 30
5555
});

test/parallel/test-trace-events-category-used.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ if (!common.isMainThread)
88

99
const CODE = `
1010
const { internalBinding } = require('internal/test/binding');
11-
const { categoryGroupEnabled } = internalBinding('trace_events');
11+
const { isTraceCategoryEnabled } = internalBinding('trace_events');
1212
console.log(
13-
categoryGroupEnabled("custom")
13+
isTraceCategoryEnabled("custom")
1414
);
1515
`;
1616

@@ -21,6 +21,9 @@ process.chdir(tmpdir.path);
2121
const procEnabled = cp.spawn(
2222
process.execPath,
2323
[ '--trace-event-categories', 'custom',
24+
// make test less noisy since internal/test/binding
25+
// emits a warning.
26+
'--no-warnings',
2427
'--expose-internals',
2528
'-e', CODE ]
2629
);
@@ -35,6 +38,9 @@ procEnabled.once('exit', common.mustCall(() => {
3538
const procDisabled = cp.spawn(
3639
process.execPath,
3740
[ '--trace-event-categories', 'other',
41+
// make test less noisy since internal/test/binding
42+
// emits a warning.
43+
'--no-warnings',
3844
'--expose-internals',
3945
'-e', CODE ]
4046
);

0 commit comments

Comments
 (0)