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