Skip to content

Commit 6f35dbf

Browse files
committed
src: trace threadpool event
1 parent ab89024 commit 6f35dbf

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

doc/api/tracing.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ The available categories are:
2222
* `node.bootstrap`: Enables capture of Node.js bootstrap milestones.
2323
* `node.console`: Enables capture of `console.time()` and `console.count()`
2424
output.
25+
* `node.threadpool`: Enables capture of Node.js threadpool,
26+
except asynchronous file IO and DNS.
2527
* `node.dns.native`: Enables capture of trace data for DNS queries.
2628
* `node.net.native`: Enables capture of trace data for network.
2729
* `node.environment`: Enables capture of Node.js Environment milestones.

src/threadpoolwork-inl.h

+9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626

2727
#include "util-inl.h"
2828
#include "node_internals.h"
29+
#include "tracing/trace_event.h"
2930

3031
namespace node {
3132

3233
void ThreadPoolWork::ScheduleWork() {
3334
env_->IncreaseWaitingRequestCounter();
35+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(TRACING_CATEGORY_NODE1(threadpool),
36+
"",
37+
this);
3438
int status = uv_queue_work(
3539
env_->event_loop(),
3640
&work_req_,
@@ -41,6 +45,11 @@ void ThreadPoolWork::ScheduleWork() {
4145
[](uv_work_t* req, int status) {
4246
ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
4347
self->env_->DecreaseWaitingRequestCounter();
48+
TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE1(threadpool),
49+
"",
50+
self,
51+
"result",
52+
status);
4453
self->AfterThreadPoolWork(status);
4554
});
4655
CHECK_EQ(status, 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
const common = 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 (!common.hasCrypto)
10+
common.skip('missing crypto');
11+
12+
const { hkdf } = require('crypto');
13+
const { deflate } = require('zlib');
14+
const { Blob } = require('buffer');
15+
16+
17+
if (process.env.isChild === '1') {
18+
hkdf('sha512', 'key', 'salt', 'info', 64, () => {});
19+
deflate('hello', () => {});
20+
// Make async call
21+
const blob = new Blob(['h'.repeat(4096 * 2)]);
22+
blob.arrayBuffer();
23+
return;
24+
}
25+
26+
tmpdir.refresh();
27+
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
28+
29+
cp.spawnSync(process.execPath,
30+
[
31+
'--trace-events-enabled',
32+
'--trace-event-categories', 'node.threadpool',
33+
__filename,
34+
],
35+
{
36+
cwd: tmpdir.path,
37+
env: {
38+
...process.env,
39+
isChild: '1',
40+
},
41+
});
42+
43+
assert(fs.existsSync(FILE_NAME));
44+
const data = fs.readFileSync(FILE_NAME);
45+
const traces = JSON.parse(data.toString()).traceEvents;
46+
assert(traces.length > 0);
47+
const count = traces.filter((trace) => trace.cat === 'node,node.threadpool').length;
48+
// There are three types, each type has two events at least
49+
assert.strictEqual(count > 3 * 2, true);

0 commit comments

Comments
 (0)