Skip to content

Commit 2b662d9

Browse files
theanarkhaduh95
authored andcommitted
process: trace sync api
1 parent c4d0229 commit 2b662d9

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

doc/api/tracing.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ The available categories are:
4040
measures and marks.
4141
* `node.perf.timerify`: Enables capture of only Performance API timerify
4242
measurements.
43+
* `node.process.sync`: Enables capture of trace data for the sync APIs of
44+
process module.
4345
* `node.promises.rejections`: Enables capture of trace data tracking the number
4446
of unhandled Promise rejections and handled-after-rejections.
4547
* `node.vm.script`: Enables capture of trace data for the `node:vm` module's

src/inspector/tracing_agent.cc

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ DispatchResponse TracingAgent::getCategories(
190190
categories_list->emplace_back("node.perf");
191191
categories_list->emplace_back("node.perf.timerify");
192192
categories_list->emplace_back("node.perf.usertiming");
193+
categories_list->emplace_back("node.process.sync");
193194
categories_list->emplace_back("node.promises.rejections");
194195
categories_list->emplace_back("node.threadpoolwork.async");
195196
categories_list->emplace_back("node.threadpoolwork.sync");

src/spawn_sync.cc

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "node_external_reference.h"
2626
#include "node_internals.h"
2727
#include "string_bytes.h"
28+
#include "tracing/trace_event.h"
2829
#include "util-inl.h"
2930

3031
#include <cstring>
@@ -519,8 +520,21 @@ Maybe<bool> SyncProcessRunner::TryInitializeAndRunLoop(Local<Value> options) {
519520
}
520521
}
521522
}
523+
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
524+
TRACING_CATEGORY_NODE2(process, sync)) != 0) {
525+
TRACE_EVENT_BEGIN1(TRACING_CATEGORY_NODE2(process, sync),
526+
"spawnSync",
527+
"file",
528+
TRACE_STR_COPY(uv_process_options_.file));
529+
}
522530

523531
r = uv_run(uv_loop_, UV_RUN_DEFAULT);
532+
533+
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
534+
TRACING_CATEGORY_NODE2(process, sync)) != 0) {
535+
TRACE_EVENT_END0(TRACING_CATEGORY_NODE2(process, sync), "spawnSync");
536+
}
537+
524538
if (r < 0)
525539
// We can't handle uv_run failure.
526540
ABORT();

test/parallel/test-inspector-tracing-domain.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ async function test() {
5858
'node.perf',
5959
'node.perf.timerify',
6060
'node.perf.usertiming',
61+
'node.process.sync',
6162
'node.promises.rejections',
6263
'node.threadpoolwork.async',
6364
'node.threadpoolwork.sync',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const cp = require('child_process');
5+
const fs = require('fs');
6+
const path = require('path');
7+
const tmpdir = require('../common/tmpdir');
8+
9+
if (process.env.isChild === '1') {
10+
cp.execFileSync('node', ['-e', '']);
11+
cp.execSync('node', ['-e', '']);
12+
cp.spawnSync('node', ['-e', '']);
13+
return;
14+
}
15+
16+
tmpdir.refresh();
17+
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
18+
19+
cp.spawnSync(process.execPath,
20+
[
21+
'--trace-events-enabled',
22+
'--trace-event-categories', 'node.process.sync',
23+
__filename,
24+
],
25+
{
26+
cwd: tmpdir.path,
27+
env: {
28+
...process.env,
29+
isChild: '1',
30+
},
31+
});
32+
33+
assert(fs.existsSync(FILE_NAME));
34+
const data = fs.readFileSync(FILE_NAME);
35+
const traces = JSON.parse(data.toString()).traceEvents;
36+
assert(traces.length > 0);
37+
const count = traces.filter((item) => item.name === 'spawnSync').length;
38+
// Two begin, Two end
39+
assert.strictEqual(count === 3 * 2, true);

0 commit comments

Comments
 (0)