Skip to content

Commit 855bb8d

Browse files
AndreasMadsenMylesBorins
authored andcommitted
trace_events: add executionAsyncId to init events
async_hooks emits trace_events. This adds the executionAsyncId to the init events. In theory this could be inferred from the before and after events but this is much simpler and doesn't require knowledge of all events. PR-URL: #17196 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent b833a59 commit 855bb8d

5 files changed

+51
-14
lines changed

lib/internal/trace_events_async_hooks.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const hook = async_hooks.createHook({
2828

2929
typeMemory.set(asyncId, type);
3030
trace_events.emit(BEFORE_EVENT, 'node.async_hooks',
31-
type, asyncId, 'triggerAsyncId', triggerAsyncId);
31+
type, asyncId,
32+
'triggerAsyncId', triggerAsyncId,
33+
'executionAsyncId', async_hooks.executionAsyncId());
3234
},
3335

3436
before(asyncId) {

src/async_wrap.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -708,9 +708,12 @@ void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
708708
switch (provider_type()) {
709709
#define V(PROVIDER) \
710710
case PROVIDER_ ## PROVIDER: \
711-
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("node.async_hooks", \
711+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2("node.async_hooks", \
712712
#PROVIDER, static_cast<int64_t>(get_async_id()), \
713-
"triggerAsyncId", static_cast<int64_t>(get_trigger_async_id())); \
713+
"executionAsyncId", \
714+
static_cast<int64_t>(env()->execution_async_id()), \
715+
"triggerAsyncId", \
716+
static_cast<int64_t>(get_trigger_async_id())); \
714717
break;
715718
NODE_ASYNC_PROVIDER_TYPES(V)
716719
#undef V

src/node_trace_events.cc

+18-7
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,19 @@ static void Emit(const FunctionCallbackInfo<Value>& args) {
7070
id = args[3]->IntegerValue(context).ToChecked();
7171
}
7272

73-
// TODO(AndreasMadsen): Two extra arguments are not supported.
7473
// TODO(AndreasMadsen): String values are not supported.
7574
int32_t num_args = 0;
76-
const char* arg_names[1];
77-
uint8_t arg_types[1];
78-
uint64_t arg_values[1];
75+
const char* arg_names[2];
76+
uint8_t arg_types[2];
77+
uint64_t arg_values[2];
7978

8079
// Create Utf8Value in the function scope to prevent deallocation.
8180
// The c-string will be copied by TRACE_EVENT_API_ADD_TRACE_EVENT at the end.
8281
Utf8Value arg1NameValue(env->isolate(), args[4]);
82+
Utf8Value arg2NameValue(env->isolate(), args[6]);
8383

84-
if (args.Length() < 6 || (args[4]->IsUndefined() && args[5]->IsUndefined())) {
85-
num_args = 0;
86-
} else {
84+
if (args.Length() >= 6 &&
85+
(!args[4]->IsUndefined() || !args[5]->IsUndefined())) {
8786
num_args = 1;
8887
arg_types[0] = TRACE_VALUE_TYPE_INT;
8988

@@ -94,6 +93,18 @@ static void Emit(const FunctionCallbackInfo<Value>& args) {
9493
arg_values[0] = args[5]->IntegerValue(context).ToChecked();
9594
}
9695

96+
if (args.Length() >= 8 &&
97+
(!args[6]->IsUndefined() || !args[7]->IsUndefined())) {
98+
num_args = 2;
99+
arg_types[1] = TRACE_VALUE_TYPE_INT;
100+
101+
CHECK(args[6]->IsString());
102+
arg_names[1] = arg2NameValue.out();
103+
104+
CHECK(args[7]->IsNumber());
105+
arg_values[1] = args[7]->IntegerValue(context).ToChecked();
106+
}
107+
97108
// The TRACE_EVENT_FLAG_COPY flag indicates that the name and argument
98109
// name should be copied thus they don't need to long-lived pointers.
99110
// The category name still won't be copied and thus need to be a long-lived

test/parallel/test-trace-events-async-hooks.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ proc.once('exit', common.mustCall(() => {
4343
return true;
4444
}));
4545

46-
4746
// JavaScript async_hooks trace events should be generated.
4847
assert(traces.some((trace) => {
4948
if (trace.pid !== proc.pid)
@@ -54,5 +53,14 @@ proc.once('exit', common.mustCall(() => {
5453
return false;
5554
return true;
5655
}));
56+
57+
// Check args in init events
58+
const initEvents = traces.filter((trace) => {
59+
return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
60+
});
61+
assert(initEvents.every((trace) => {
62+
return (trace.args.executionAsyncId > 0 &&
63+
trace.args.triggerAsyncId > 0);
64+
}));
5765
}));
5866
}));

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const CODE = `
1010
'type-value', 10, 'extra-value', 20);
1111
process.binding("trace_events").emit(
1212
'b'.charCodeAt(0), 'custom',
13-
'type-value', 20);
13+
'type-value', 20, 'first-value', 20, 'second-value', 30);
14+
process.binding("trace_events").emit(
15+
'b'.charCodeAt(0), 'custom',
16+
'type-value', 30);
1417
process.binding("trace_events").emit(
1518
'b'.charCodeAt(0), 'missing',
1619
'type-value', 10, 'extra-value', 20);
@@ -29,7 +32,7 @@ proc.once('exit', common.mustCall(() => {
2932
assert(common.fileExists(FILE_NAME));
3033
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
3134
const traces = JSON.parse(data.toString()).traceEvents;
32-
assert.strictEqual(traces.length, 2);
35+
assert.strictEqual(traces.length, 3);
3336

3437
assert.strictEqual(traces[0].pid, proc.pid);
3538
assert.strictEqual(traces[0].ph, 'b');
@@ -43,6 +46,16 @@ proc.once('exit', common.mustCall(() => {
4346
assert.strictEqual(traces[1].cat, 'custom');
4447
assert.strictEqual(traces[1].name, 'type-value');
4548
assert.strictEqual(traces[1].id, '0x14');
46-
assert.deepStrictEqual(traces[1].args, { });
49+
assert.deepStrictEqual(traces[1].args, {
50+
'first-value': 20,
51+
'second-value': 30
52+
});
53+
54+
assert.strictEqual(traces[2].pid, proc.pid);
55+
assert.strictEqual(traces[2].ph, 'b');
56+
assert.strictEqual(traces[2].cat, 'custom');
57+
assert.strictEqual(traces[2].name, 'type-value');
58+
assert.strictEqual(traces[2].id, '0x1e');
59+
assert.deepStrictEqual(traces[2].args, { });
4760
}));
4861
}));

0 commit comments

Comments
 (0)