|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Reads back the trace file a Node.js process wrote, whichever tracing backend |
| 4 | +// produced it. Legacy builds write the Chrome JSON trace format directly. |
| 5 | +// Perfetto builds write a binary protobuf trace, which perfetto's |
| 6 | +// `trace_processor_shell` converts to the same format. Run `make trace-processor` |
| 7 | +// to download it. |
| 8 | + |
| 9 | +const assert = require('assert'); |
| 10 | +const { spawnSync } = require('child_process'); |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | +const common = require('./'); |
| 14 | + |
| 15 | +const traceProcessor = path.resolve( |
| 16 | + __dirname, '..', '..', 'tools', 'perfetto', 'trace_processor_shell'); |
| 17 | + |
| 18 | +// The JSON form of a trace runs about three times the size of the trace it was |
| 19 | +// converted from, and the traces these tests produce are a few hundred KiB at |
| 20 | +// most. This is an assumed MAX size of a JSON conversion size limit for tests. |
| 21 | +const kMaxTraceJsonBytes = 64 * 1024 * 1024; |
| 22 | + |
| 23 | +// Perfetto builds default the file pattern to a .pftrace extension. |
| 24 | +const defaultTraceFileName = common.hasPerfetto ? 'node_trace.1.pftrace' : |
| 25 | + 'node_trace.1.log'; |
| 26 | + |
| 27 | +// Only perfetto traces need converting, so a missing trace_processor_shell |
| 28 | +// does not stop anything on a legacy build. |
| 29 | +function skipIfTraceReaderMissing() { |
| 30 | + if (common.hasPerfetto && !fs.existsSync(traceProcessor)) { |
| 31 | + common.skip('trace_processor_shell is missing, ' + |
| 32 | + 'run `make trace-processor` to download it'); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function readTraceEvents(file) { |
| 37 | + if (!common.hasPerfetto) { |
| 38 | + return JSON.parse(fs.readFileSync(file, 'utf8')).traceEvents; |
| 39 | + } |
| 40 | + |
| 41 | + const converted = spawnSync(traceProcessor, ['convert', 'json', file], |
| 42 | + { maxBuffer: kMaxTraceJsonBytes }); |
| 43 | + assert.ifError(converted.error); |
| 44 | + assert.strictEqual( |
| 45 | + converted.status, 0, |
| 46 | + `trace_processor_shell failed: ${converted.stderr}`); |
| 47 | + return JSON.parse(converted.stdout.toString()).traceEvents; |
| 48 | +} |
| 49 | + |
| 50 | +// A perfetto trace event carries the single category it was emitted with. The |
| 51 | +// legacy backend instead groups it with every ancestor category, so |
| 52 | +// `node.net.native` is recorded as `node,node.net,node.net.native`. |
| 53 | +function traceCategory(name) { |
| 54 | + if (common.hasPerfetto) { |
| 55 | + return name; |
| 56 | + } |
| 57 | + const parts = name.split('.'); |
| 58 | + return parts.map((_, i) => parts.slice(0, i + 1).join('.')).join(','); |
| 59 | +} |
| 60 | + |
| 61 | +module.exports = { |
| 62 | + defaultTraceFileName, |
| 63 | + readTraceEvents, |
| 64 | + skipIfTraceReaderMissing, |
| 65 | + traceCategory, |
| 66 | +}; |
0 commit comments