|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// --- About this test suite |
| 4 | +// |
| 5 | +// JIT support for perf(1) was added in 2009 (see https://lkml.org/lkml/2009/6/8/499). |
| 6 | +// It works by looking for a perf map file in /tmp/perf-<pid>.map, where <pid> is the |
| 7 | +// PID of the target process. |
| 8 | +// |
| 9 | +// The structure of this file is stable. Perf expects each line to specify a symbol |
| 10 | +// in the form: |
| 11 | +// |
| 12 | +// <start> <length> <name> |
| 13 | +// |
| 14 | +// where <start> is the hex representation of the instruction pointer for the beginning |
| 15 | +// of the function, <length> is the byte length of the function, and <name> is the |
| 16 | +// readable JIT name used for reporting. |
| 17 | +// |
| 18 | +// This file asserts that a node script run with the appropriate flags will produce |
| 19 | +// a compliant perf map. |
| 20 | +// |
| 21 | +// NOTE: This test runs only on linux, as that is the only platform supported by perf, and |
| 22 | +// accordingly the only platform where `perf-basic-prof*` v8 flags are available. |
| 23 | + |
| 24 | + |
| 25 | +const common = require('../common'); |
| 26 | +if (!common.isLinux) { |
| 27 | + common.skip('--perf-basic-prof* is statically defined as linux-only'); |
| 28 | +} |
| 29 | + |
| 30 | +const assert = require('assert'); |
| 31 | +const { spawnSync } = require('child_process'); |
| 32 | +const { readFileSync } = require('fs'); |
| 33 | + |
| 34 | +const fixtures = require('../common/fixtures'); |
| 35 | +const tmpdir = require('../common/tmpdir'); |
| 36 | +tmpdir.refresh(); |
| 37 | + |
| 38 | +const testCases = [ |
| 39 | + { |
| 40 | + title: '--perf-basic-prof interpreted', |
| 41 | + nodeFlags: ['--perf-basic-prof', '--no-turbo-inlining', '--no-opt'], |
| 42 | + matches: [ |
| 43 | + '~functionOne .+/linux-perf-logger.js', |
| 44 | + '~functionTwo .+/linux-perf-logger.js', |
| 45 | + 'test-regex', |
| 46 | + ], |
| 47 | + noMatches: ['\\*functionOne', '\\*functionTwo'], |
| 48 | + }, |
| 49 | + { |
| 50 | + title: '--perf-basic-prof compiled', |
| 51 | + nodeFlags: ['--perf-basic-prof', '--no-turbo-inlining', '--always-opt'], |
| 52 | + matches: [ |
| 53 | + 'test-regex', |
| 54 | + '~functionOne .+/linux-perf-logger.js', |
| 55 | + '~functionTwo .+/linux-perf-logger.js', |
| 56 | + '\\*functionOne .+/linux-perf-logger.js', |
| 57 | + '\\*functionTwo .+/linux-perf-logger.js', |
| 58 | + ], |
| 59 | + noMatches: [], |
| 60 | + }, |
| 61 | + { |
| 62 | + title: '--perf-basic-prof-only-functions interpreted', |
| 63 | + nodeFlags: ['--perf-basic-prof-only-functions', '--no-turbo-inlining', '--no-opt'], |
| 64 | + matches: ['~functionOne .+/linux-perf-logger.js', '~functionTwo .+/linux-perf-logger.js'], |
| 65 | + noMatches: ['\\*functionOne', '\\*functionTwo', 'test-regex'], |
| 66 | + }, |
| 67 | + { |
| 68 | + title: '--perf-basic-prof-only-functions compiled', |
| 69 | + nodeFlags: ['--perf-basic-prof-only-functions', '--no-turbo-inlining', '--always-opt'], |
| 70 | + matches: [ |
| 71 | + '~functionOne .+/linux-perf-logger.js', |
| 72 | + '~functionTwo .+/linux-perf-logger.js', |
| 73 | + '\\*functionOne .+/linux-perf-logger.js', |
| 74 | + '\\*functionTwo .+/linux-perf-logger.js', |
| 75 | + ], |
| 76 | + noMatches: ['test-regex'], |
| 77 | + }, |
| 78 | +]; |
| 79 | + |
| 80 | +function runTest(test) { |
| 81 | + const report = { |
| 82 | + title: test.title, |
| 83 | + perfMap: '[uninitialized]', |
| 84 | + errors: [], |
| 85 | + }; |
| 86 | + |
| 87 | + const args = test.nodeFlags.concat(fixtures.path('linux-perf-logger.js')); |
| 88 | + const run = spawnSync(process.execPath, args, { cwd: tmpdir.path, encoding: 'utf8' }); |
| 89 | + if (run.error) { |
| 90 | + report.errors.push(run.error.stack); |
| 91 | + return report; |
| 92 | + } |
| 93 | + if (run.status !== 0) { |
| 94 | + report.errors.push(`running script:\n${run.stderr}`); |
| 95 | + return report; |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + report.perfMap = readFileSync(`/tmp/perf-${run.pid}.map`, 'utf8'); |
| 100 | + } catch (err) { |
| 101 | + report.errors.push(`reading perf map: ${err.stack}`); |
| 102 | + return report; |
| 103 | + } |
| 104 | + |
| 105 | + const hexRegex = '[a-fA-F0-9]+'; |
| 106 | + for (const testRegex of test.matches) { |
| 107 | + const lineRegex = new RegExp(`${hexRegex} ${hexRegex}.*:${testRegex}`); |
| 108 | + if (!lineRegex.test(report.perfMap)) { |
| 109 | + report.errors.push(`Expected to match ${lineRegex}`); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for (const regex of test.noMatches) { |
| 114 | + const noMatch = new RegExp(regex); |
| 115 | + if (noMatch.test(report.perfMap)) { |
| 116 | + report.errors.push(`Expected not to match ${noMatch}`); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return report; |
| 121 | +} |
| 122 | + |
| 123 | +function serializeError(report, index) { |
| 124 | + return `[ERROR ${index + 1}] ${report.title} |
| 125 | +Errors: |
| 126 | +${report.errors.map((err, i) => `${i + 1}. ${err}`).join('\n')} |
| 127 | +Perf map content: |
| 128 | +${report.perfMap} |
| 129 | +</end perf map content> |
| 130 | +`; |
| 131 | +} |
| 132 | + |
| 133 | +function runSuite() { |
| 134 | + const failures = []; |
| 135 | + |
| 136 | + for (const tc of testCases) { |
| 137 | + const report = runTest(tc); |
| 138 | + if (report.errors.length > 0) { |
| 139 | + failures.push(report); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + const errorsToReport = failures.map(serializeError).join('\n--------\n'); |
| 144 | + |
| 145 | + assert.strictEqual(failures.length, 0, `${failures.length} tests failed\n\n${errorsToReport}`); |
| 146 | +} |
| 147 | + |
| 148 | +runSuite(); |
0 commit comments