Skip to content

Commit 4b64c84

Browse files
ofrobotsMylesBorins
authored andcommitted
src: trace_events: support for metadata events
Add support for metadata events. At this point they are added to the main buffer. Emit a metadata event for the main thread. PR-URL: #20757 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent b664a84 commit 4b64c84

9 files changed

+85
-6
lines changed

src/node.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4559,6 +4559,9 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
45594559
Environment env(isolate_data, context, v8_platform.GetTracingAgent());
45604560
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
45614561

4562+
TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
4563+
"JavaScriptMainThread");
4564+
45624565
const char* path = argc > 1 ? argv[1] : nullptr;
45634566
StartInspector(&env, path, debug_options);
45644567

src/tracing/trace_event.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ enum CategoryGroupEnabledFlags {
118118
node::tracing::TraceEventHelper::GetTracingController() \
119119
->UpdateTraceEventDuration
120120

121+
// Adds a metadata event to the trace log. The |AppendValueAsTraceFormat| method
122+
// on the convertable value will be called at flush time.
123+
// TRACE_EVENT_API_ADD_METADATA_EVENT(
124+
// const unsigned char* category_group_enabled,
125+
// const char* event_name,
126+
// const char* arg_name,
127+
// std::unique_ptr<ConvertableToTraceFormat> arg_value)
128+
#define TRACE_EVENT_API_ADD_METADATA_EVENT node::tracing::AddMetadataEvent
129+
121130
// Defines atomic operations used internally by the tracing system.
122131
#define TRACE_EVENT_API_ATOMIC_WORD intptr_t
123132
#define TRACE_EVENT_API_ATOMIC_LOAD(var) (var)
@@ -259,6 +268,16 @@ enum CategoryGroupEnabledFlags {
259268
} \
260269
} while (0)
261270

271+
#define INTERNAL_TRACE_EVENT_METADATA_ADD(category_group, name, ...) \
272+
do { \
273+
INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
274+
if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
275+
TRACE_EVENT_API_ADD_METADATA_EVENT( \
276+
INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
277+
##__VA_ARGS__); \
278+
} \
279+
} while(0)
280+
262281
// Enter and leave a context based on the current scope.
263282
#define INTERNAL_TRACE_EVENT_SCOPED_CONTEXT(category_group, name, context) \
264283
struct INTERNAL_TRACE_EVENT_UID(ScopedContext) { \
@@ -612,6 +631,26 @@ static V8_INLINE uint64_t AddTraceEventWithTimestamp(
612631
arg_names, arg_types, arg_values, flags, timestamp);
613632
}
614633

634+
template <class ARG1_TYPE>
635+
static V8_INLINE uint64_t AddMetadataEvent(
636+
const uint8_t* category_group_enabled, const char* name,
637+
const char* arg1_name, ARG1_TYPE&& arg1_val) {
638+
const int num_args = 1;
639+
uint8_t arg_type;
640+
uint64_t arg_value;
641+
SetTraceValue(std::forward<ARG1_TYPE>(arg1_val), &arg_type, &arg_value);
642+
// TODO(ofrobots): It would be good to add metadata events to a separate
643+
// buffer so that they can be periodically reemitted. For now, we have a
644+
// single buffer, so we just add them to the main buffer.
645+
return TRACE_EVENT_API_ADD_TRACE_EVENT(
646+
TRACE_EVENT_PHASE_METADATA,
647+
category_group_enabled, name,
648+
node::tracing::kGlobalScope, // scope
649+
node::tracing::kNoId, // id
650+
node::tracing::kNoId, // bind_id
651+
num_args, &arg1_name, &arg_type, &arg_value, TRACE_EVENT_FLAG_NONE);
652+
}
653+
615654
// Used by TRACE_EVENTx macros. Do not use directly.
616655
class ScopedTracer {
617656
public:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ if (isChild) {
130130

131131
assert(common.fileExists(file));
132132
fs.readFile(file, common.mustCall((err, data) => {
133-
const traces = JSON.parse(data.toString()).traceEvents;
133+
const traces = JSON.parse(data.toString()).traceEvents
134+
.filter((trace) => trace.cat !== '__metadata');
134135
assert.strictEqual(traces.length,
135136
expectedMarks.length +
136137
expectedBegins.length +

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const proc = cp.spawn(process.execPath,
3131
proc.once('exit', common.mustCall(() => {
3232
assert(common.fileExists(FILE_NAME));
3333
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
34-
const traces = JSON.parse(data.toString()).traceEvents;
34+
const traces = JSON.parse(data.toString()).traceEvents
35+
.filter((trace) => trace.cat !== '__metadata');
3536
assert.strictEqual(traces.length, 3);
3637

3738
assert.strictEqual(traces[0].pid, proc.pid);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ if (process.argv[2] === 'child') {
4242

4343
assert(common.fileExists(file));
4444
fs.readFile(file, common.mustCall((err, data) => {
45-
const traces = JSON.parse(data.toString()).traceEvents;
45+
const traces = JSON.parse(data.toString()).traceEvents
46+
.filter((trace) => trace.cat !== '__metadata');
4647
traces.forEach((trace) => {
4748
assert.strictEqual(trace.pid, proc.pid);
4849
assert(names.includes(trace.name));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
7+
const CODE =
8+
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';
9+
const FILE_NAME = 'node_trace.1.log';
10+
11+
const tmpdir = require('../common/tmpdir');
12+
tmpdir.refresh();
13+
process.chdir(tmpdir.path);
14+
15+
const proc = cp.spawn(process.execPath,
16+
[ '--trace-events-enabled', '-e', CODE ]);
17+
proc.once('exit', common.mustCall(() => {
18+
assert(common.fileExists(FILE_NAME));
19+
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
20+
const traces = JSON.parse(data.toString()).traceEvents;
21+
assert(traces.length > 0);
22+
assert(traces.some((trace) =>
23+
trace.cat === '__metadata' && trace.name === 'thread_name' &&
24+
trace.args.name === 'JavaScriptMainThread'));
25+
}));
26+
}));

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const cp = require('child_process');
5+
const fs = require('fs');
56

67
const CODE =
78
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';
@@ -17,5 +18,10 @@ const proc_no_categories = cp.spawn(
1718
);
1819

1920
proc_no_categories.once('exit', common.mustCall(() => {
20-
assert(!common.fileExists(FILE_NAME));
21+
assert(common.fileExists(FILE_NAME));
22+
// Only __metadata categories should have been emitted.
23+
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
24+
assert.ok(JSON.parse(data.toString()).traceEvents.every(
25+
(trace) => trace.cat === '__metadata'));
26+
}));
2127
}));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ if (process.argv[2] === 'child') {
4949

5050
assert(common.fileExists(file));
5151
fs.readFile(file, common.mustCall((err, data) => {
52-
const traces = JSON.parse(data.toString()).traceEvents;
52+
const traces = JSON.parse(data.toString()).traceEvents
53+
.filter((trace) => trace.cat !== '__metadata');
5354
assert.strictEqual(traces.length,
5455
expectedMarks.length +
5556
expectedBegins.length +

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ if (process.argv[2] === 'child') {
3232

3333
assert(common.fileExists(file));
3434
fs.readFile(file, common.mustCall((err, data) => {
35-
const traces = JSON.parse(data.toString()).traceEvents;
35+
const traces = JSON.parse(data.toString()).traceEvents
36+
.filter((trace) => trace.cat !== '__metadata');
3637
traces.forEach((trace) => {
3738
assert.strictEqual(trace.pid, proc.pid);
3839
assert(names.includes(trace.name));

0 commit comments

Comments
 (0)