Skip to content

Commit da5d818

Browse files
committed
trace_events: adds a new trace_events api
Removes the requirement to use `--trace-events-enabled` to enable trace events. Tracing is enabled automatically if there are any enabled categories. Adds a new `trace_events` module with an API for enabling/disabling trace events at runtime without a command line flag. ```js const trace_events = require('trace_events'); const categories = [ 'node.perf', 'node.async_hooks' ]; const tracing = trace_events.createTracing({ categories }); tracing.enable(); // do stuff tracing.disable(); ``` Multiple `Tracing` objects may exist and be enabled at any point in time. The enabled trace event categories is the union of all enabled `Tracing` objects and the `--trace-event-categories` flag. PR-URL: #19803 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent 54a2e93 commit da5d818

25 files changed

+677
-87
lines changed

doc/api/_toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* [String Decoder](string_decoder.html)
4747
* [Timers](timers.html)
4848
* [TLS/SSL](tls.html)
49-
* [Tracing](tracing.html)
49+
* [Trace Events](tracing.html)
5050
* [TTY](tty.html)
5151
* [UDP/Datagram](dgram.html)
5252
* [URL](url.html)

doc/api/errors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,18 @@ socket, which is only valid from a client.
15501550

15511551
An attempt was made to renegotiate TLS on a socket instance with TLS disabled.
15521552

1553+
<a id="ERR_TRACE_EVENTS_CATEGORY_REQUIRED"></a>
1554+
### ERR_TRACE_EVENTS_CATEGORY_REQUIRED
1555+
1556+
The `trace_events.createTracing()` method requires at least one trace event
1557+
category.
1558+
1559+
<a id="ERR_TRACE_EVENTS_UNAVAILABLE"></a>
1560+
### ERR_TRACE_EVENTS_UNAVAILABLE
1561+
1562+
The `trace_events` module could not be loaded because Node.js was compiled with
1563+
the `--without-v8-platform` flag.
1564+
15531565
<a id="ERR_TRANSFORM_ALREADY_TRANSFORMING"></a>
15541566
### ERR_TRANSFORM_ALREADY_TRANSFORMING
15551567

doc/api/tracing.md

Lines changed: 153 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# Tracing
1+
# Trace Events
22

33
<!--introduced_in=v7.7.0-->
44

5+
> Stability: 1 - Experimental
6+
57
Trace Event provides a mechanism to centralize tracing information generated by
68
V8, Node.js core, and userspace code.
79

8-
Tracing can be enabled by passing the `--trace-events-enabled` flag when
9-
starting a Node.js application.
10-
11-
The set of categories for which traces are recorded can be specified using the
12-
`--trace-event-categories` flag followed by a list of comma separated category
13-
names.
10+
Tracing can be enabled with the `--trace-event-categories` command-line flag
11+
or by using the trace_events module. The `--trace-event-categories` flag accepts
12+
a list of comma-separated category names.
1413

1514
The available categories are:
1615

@@ -27,7 +26,32 @@ The available categories are:
2726
By default the `node`, `node.async_hooks`, and `v8` categories are enabled.
2827

2928
```txt
30-
node --trace-events-enabled --trace-event-categories v8,node,node.async_hooks server.js
29+
node --trace-event-categories v8,node,node.async_hooks server.js
30+
```
31+
32+
Prior versions of Node.js required the use of the `--trace-events-enabled`
33+
flag to enable trace events. This requirement has been removed. However, the
34+
`--trace-events-enabled` flag *may* still be used and will enable the
35+
`node`, `node.async_hooks`, and `v8` trace event categories by default.
36+
37+
```txt
38+
node --trace-events-enabled
39+
40+
// is equivalent to
41+
42+
node --trace-event-categories v8,node,node.async_hooks
43+
```
44+
45+
Alternatively, trace events may be enabled using the `trace_events` module:
46+
47+
```js
48+
const trace_events = require('trace_events');
49+
const tracing = trace_events.createTracing({ categories: ['node.perf'] });
50+
tracing.enable(); // Enable trace event capture for the 'node.perf' category
51+
52+
// do work
53+
54+
tracing.disable(); // Disable trace event capture for the 'node.perf' category
3155
```
3256

3357
Running Node.js with tracing enabled will produce log files that can be opened
@@ -40,12 +64,132 @@ be specified with `--trace-event-file-pattern` that accepts a template
4064
string that supports `${rotation}` and `${pid}`. For example:
4165

4266
```txt
43-
node --trace-events-enabled --trace-event-file-pattern '${pid}-${rotation}.log' server.js
67+
node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js
4468
```
4569

4670
Starting with Node.js 10.0.0, the tracing system uses the same time source
4771
as the one used by `process.hrtime()`
4872
however the trace-event timestamps are expressed in microseconds,
4973
unlike `process.hrtime()` which returns nanoseconds.
5074

75+
## The `trace_events` module
76+
<!-- YAML
77+
added: REPLACEME
78+
-->
79+
80+
### `Tracing` object
81+
<!-- YAML
82+
added: REPLACEME
83+
-->
84+
85+
The `Tracing` object is used to enable or disable tracing for sets of
86+
categories. Instances are created using the `trace_events.createTracing()`
87+
method.
88+
89+
When created, the `Tracing` object is disabled. Calling the
90+
`tracing.enable()` method adds the categories to the set of enabled trace event
91+
categories. Calling `tracing.disable()` will remove the categories from the
92+
set of enabled trace event categories.
93+
94+
#### `tracing.categories`
95+
<!-- YAML
96+
added: REPLACEME
97+
-->
98+
99+
* {string}
100+
101+
A comma-separated list of the trace event categories covered by this
102+
`Tracing` object.
103+
104+
#### `tracing.disable()`
105+
<!-- YAML
106+
added: REPLACEME
107+
-->
108+
109+
Disables this `Tracing` object.
110+
111+
Only trace event categories *not* covered by other enabled `Tracing` objects
112+
and *not* specified by the `--trace-event-categories` flag will be disabled.
113+
114+
```js
115+
const trace_events = require('trace_events');
116+
const t1 = trace_events.createTracing({ categories: ['node', 'v8'] });
117+
const t2 = trace_events.createTracing({ categories: ['node.perf', 'node'] });
118+
t1.enable();
119+
t2.enable();
120+
121+
// Prints 'node,node.perf,v8'
122+
console.log(trace_events.getEnabledCategories());
123+
124+
t2.disable(); // will only disable emission of the 'node.perf' category
125+
126+
// Prints 'node,v8'
127+
console.log(trace_events.getEnabledCategories());
128+
```
129+
130+
#### `tracing.enable()`
131+
<!-- YAML
132+
added: REPLACEME
133+
-->
134+
135+
Enables this `Tracing` object for the set of categories covered by the
136+
`Tracing` object.
137+
138+
#### `tracing.enabled`
139+
<!-- YAML
140+
added: REPLACEME
141+
-->
142+
143+
* {boolean} `true` only if the `Tracing` object has been enabled.
144+
145+
### `trace_events.createTracing(options)`
146+
<!-- YAML
147+
added: REPLACEME
148+
-->
149+
150+
* `options` {Object}
151+
* `categories` {string[]} An array of trace category names. Values included
152+
in the array are coerced to a string when possible. An error will be
153+
thrown if the value cannot be coerced.
154+
* Returns: {Tracing}.
155+
156+
Creates and returns a `Tracing` object for the given set of `categories`.
157+
158+
```js
159+
const trace_events = require('trace_events');
160+
const categories = ['node.perf', 'node.async_hooks'];
161+
const tracing = trace_events.createTracing({ categories });
162+
tracing.enable();
163+
// do stuff
164+
tracing.disable();
165+
```
166+
167+
### `trace_events.getEnabledCategories()`
168+
<!-- YAML
169+
added: REPLACEME
170+
-->
171+
172+
* Returns: {string}
173+
174+
Returns a comma-separated list of all currently-enabled trace event
175+
categories. The current set of enabled trace event categories is determined
176+
by the *union* of all currently-enabled `Tracing` objects and any categories
177+
enabled using the `--trace-event-categories` flag.
178+
179+
Given the file `test.js` below, the command
180+
`node --trace-event-categories node.perf test.js` will print
181+
`'node.async_hooks,node.perf'` to the console.
182+
183+
```js
184+
const trace_events = require('trace_events');
185+
const t1 = trace_events.createTracing({ categories: ['node.async_hooks'] });
186+
const t2 = trace_events.createTracing({ categories: ['node.perf'] });
187+
const t3 = trace_events.createTracing({ categories: ['v8'] });
188+
189+
t1.enable();
190+
t2.enable();
191+
192+
console.log(trace_events.getEnabledCategories());
193+
```
194+
51195
[Performance API]: perf_hooks.html

lib/internal/errors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ E('ERR_TLS_REQUIRED_SERVER_NAME',
984984
E('ERR_TLS_SESSION_ATTACK', 'TLS session renegotiation attack detected', Error);
985985
E('ERR_TLS_SNI_FROM_SERVER',
986986
'Cannot issue SNI from a TLS server-side socket', Error);
987+
E('ERR_TRACE_EVENTS_CATEGORY_REQUIRED',
988+
'At least one category is required', TypeError);
989+
E('ERR_TRACE_EVENTS_UNAVAILABLE', 'Trace events are unavailable', Error);
987990
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
988991
'Calling transform done when still transforming', Error);
989992

lib/internal/modules/cjs/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ const builtinLibs = [
101101
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
102102
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
103103
'os', 'path', 'perf_hooks', 'punycode', 'querystring', 'readline', 'repl',
104-
'stream', 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'
104+
'stream', 'string_decoder', 'tls', 'trace_events', 'tty', 'url', 'util',
105+
'v8', 'vm', 'zlib'
105106
];
106107

107108
if (typeof process.binding('inspector').open === 'function') {

lib/trace_events.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
const { hasTracing } = process.binding('config');
4+
const kHandle = Symbol('handle');
5+
const kEnabled = Symbol('enabled');
6+
const kCategories = Symbol('categories');
7+
8+
const kMaxTracingCount = 10;
9+
10+
const {
11+
ERR_TRACE_EVENTS_CATEGORY_REQUIRED,
12+
ERR_TRACE_EVENTS_UNAVAILABLE,
13+
ERR_INVALID_ARG_TYPE
14+
} = require('internal/errors').codes;
15+
16+
if (!hasTracing)
17+
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
18+
19+
const { CategorySet, getEnabledCategories } = process.binding('trace_events');
20+
const { customInspectSymbol } = require('internal/util');
21+
const { format } = require('util');
22+
23+
const enabledTracingObjects = new Set();
24+
25+
class Tracing {
26+
constructor(categories) {
27+
this[kHandle] = new CategorySet(categories);
28+
this[kCategories] = categories;
29+
this[kEnabled] = false;
30+
}
31+
32+
enable() {
33+
if (!this[kEnabled]) {
34+
this[kEnabled] = true;
35+
this[kHandle].enable();
36+
enabledTracingObjects.add(this);
37+
if (enabledTracingObjects.size > kMaxTracingCount) {
38+
process.emitWarning(
39+
'Possible trace_events memory leak detected. There are more than ' +
40+
`${kMaxTracingCount} enabled Tracing objects.`
41+
);
42+
}
43+
}
44+
}
45+
46+
disable() {
47+
if (this[kEnabled]) {
48+
this[kEnabled] = false;
49+
this[kHandle].disable();
50+
enabledTracingObjects.delete(this);
51+
}
52+
}
53+
54+
get enabled() {
55+
return this[kEnabled];
56+
}
57+
58+
get categories() {
59+
return this[kCategories].join(',');
60+
}
61+
62+
[customInspectSymbol](depth, opts) {
63+
const obj = {
64+
enabled: this.enabled,
65+
categories: this.categories
66+
};
67+
return `Tracing ${format(obj)}`;
68+
}
69+
}
70+
71+
function createTracing(options) {
72+
if (typeof options !== 'object' || options == null)
73+
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
74+
75+
if (!Array.isArray(options.categories)) {
76+
throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]',
77+
options.categories);
78+
}
79+
80+
if (options.categories.length <= 0)
81+
throw new ERR_TRACE_EVENTS_CATEGORY_REQUIRED();
82+
83+
return new Tracing(options.categories);
84+
}
85+
86+
module.exports = {
87+
createTracing,
88+
getEnabledCategories
89+
};

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
'lib/tls.js',
7474
'lib/_tls_common.js',
7575
'lib/_tls_wrap.js',
76+
'lib/trace_events.js',
7677
'lib/tty.js',
7778
'lib/url.js',
7879
'lib/util.js',

src/env-inl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "v8.h"
3333
#include "node_perf_common.h"
3434
#include "node_context_data.h"
35+
#include "tracing/agent.h"
3536

3637
#include <stddef.h>
3738
#include <stdint.h>
@@ -325,6 +326,10 @@ inline v8::Isolate* Environment::isolate() const {
325326
return isolate_;
326327
}
327328

329+
inline tracing::Agent* Environment::tracing_agent() const {
330+
return tracing_agent_;
331+
}
332+
328333
inline Environment* Environment::from_immediate_check_handle(
329334
uv_check_t* handle) {
330335
return ContainerOf(&Environment::immediate_check_handle_, handle);

src/env.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "node_buffer.h"
44
#include "node_platform.h"
55
#include "node_file.h"
6+
#include "tracing/agent.h"
67

78
#include <stdio.h>
89
#include <algorithm>
@@ -87,9 +88,11 @@ void InitThreadLocalOnce() {
8788
}
8889

8990
Environment::Environment(IsolateData* isolate_data,
90-
Local<Context> context)
91+
Local<Context> context,
92+
tracing::Agent* tracing_agent)
9193
: isolate_(context->GetIsolate()),
9294
isolate_data_(isolate_data),
95+
tracing_agent_(tracing_agent),
9396
immediate_info_(context->GetIsolate()),
9497
tick_info_(context->GetIsolate()),
9598
timer_base_(uv_now(isolate_data->event_loop())),

0 commit comments

Comments
 (0)