Skip to content

Commit 351ef18

Browse files
lukealbaorichardlau
authored andcommittedMay 16, 2024··
test: v8: Add test-linux-perf-logger test suite
Cherry-picked from 9c714d8 PR-URL: #50352 Backport-PR-URL: #52925 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent 1147fee commit 351ef18

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
 

‎test/fixtures/linux-perf-logger.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
process.stdout.write(`${process.pid}`);
4+
5+
const testRegex = /test-regex/gi;
6+
7+
function functionOne() {
8+
for (let i = 0; i < 100; i++) {
9+
const match = testRegex.exec(Math.random().toString());
10+
}
11+
}
12+
13+
function functionTwo() {
14+
functionOne();
15+
}
16+
17+
functionTwo();
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
// MAINTAINERS' NOTE: As of early 2024, the most common failure mode for this test suite
25+
// is for v8 options to change from version to version. If this suite fails, look there first.
26+
// We use options to forcibly require certain test cases to JIT code, and the nodeFlags to do
27+
// so can change.
28+
29+
const common = require('../common');
30+
if (!common.isLinux) {
31+
common.skip('--perf-basic-prof* is statically defined as linux-only');
32+
}
33+
34+
const assert = require('assert');
35+
const { spawnSync } = require('child_process');
36+
const { readFileSync } = require('fs');
37+
38+
const fixtures = require('../common/fixtures');
39+
const tmpdir = require('../common/tmpdir');
40+
tmpdir.refresh();
41+
42+
const testCases = [
43+
{
44+
title: '--perf-basic-prof interpreted',
45+
nodeFlags: ['--perf-basic-prof', '--no-turbo-inlining', '--no-opt'],
46+
matches: [
47+
'~functionOne .+/linux-perf-logger.js',
48+
'~functionTwo .+/linux-perf-logger.js',
49+
'test-regex',
50+
],
51+
noMatches: ['\\*functionOne', '\\*functionTwo'],
52+
},
53+
{
54+
title: '--perf-basic-prof compiled',
55+
nodeFlags: ['--perf-basic-prof', '--no-turbo-inlining', '--always-opt'],
56+
matches: [
57+
'test-regex',
58+
'~functionOne .+/linux-perf-logger.js',
59+
'~functionTwo .+/linux-perf-logger.js',
60+
'\\*functionOne .+/linux-perf-logger.js',
61+
'\\*functionTwo .+/linux-perf-logger.js',
62+
],
63+
noMatches: [],
64+
},
65+
{
66+
title: '--perf-basic-prof-only-functions interpreted',
67+
nodeFlags: ['--perf-basic-prof-only-functions', '--no-turbo-inlining', '--no-opt'],
68+
matches: ['~functionOne .+/linux-perf-logger.js', '~functionTwo .+/linux-perf-logger.js'],
69+
noMatches: ['\\*functionOne', '\\*functionTwo', 'test-regex'],
70+
},
71+
{
72+
title: '--perf-basic-prof-only-functions compiled',
73+
nodeFlags: ['--perf-basic-prof-only-functions', '--no-turbo-inlining', '--always-opt'],
74+
matches: [
75+
'~functionOne .+/linux-perf-logger.js',
76+
'~functionTwo .+/linux-perf-logger.js',
77+
'\\*functionOne .+/linux-perf-logger.js',
78+
'\\*functionTwo .+/linux-perf-logger.js',
79+
],
80+
noMatches: ['test-regex'],
81+
},
82+
];
83+
84+
function runTest(test) {
85+
const report = {
86+
title: test.title,
87+
perfMap: '[uninitialized]',
88+
errors: [],
89+
};
90+
91+
const args = test.nodeFlags.concat(fixtures.path('linux-perf-logger.js'));
92+
const run = spawnSync(process.execPath, args, { cwd: tmpdir.path, encoding: 'utf8' });
93+
if (run.error) {
94+
report.errors.push(run.error.stack);
95+
return report;
96+
}
97+
if (run.status !== 0) {
98+
report.errors.push(`running script:\n${run.stderr}`);
99+
return report;
100+
}
101+
102+
try {
103+
report.perfMap = readFileSync(`/tmp/perf-${run.pid}.map`, 'utf8');
104+
} catch (err) {
105+
report.errors.push(`reading perf map: ${err.stack}`);
106+
return report;
107+
}
108+
109+
const hexRegex = '[a-fA-F0-9]+';
110+
for (const testRegex of test.matches) {
111+
const lineRegex = new RegExp(`${hexRegex} ${hexRegex}.*:${testRegex}`);
112+
if (!lineRegex.test(report.perfMap)) {
113+
report.errors.push(`Expected to match ${lineRegex}`);
114+
}
115+
}
116+
117+
for (const regex of test.noMatches) {
118+
const noMatch = new RegExp(regex);
119+
if (noMatch.test(report.perfMap)) {
120+
report.errors.push(`Expected not to match ${noMatch}`);
121+
}
122+
}
123+
124+
return report;
125+
}
126+
127+
function serializeError(report, index) {
128+
return `[ERROR ${index + 1}] ${report.title}
129+
Errors:
130+
${report.errors.map((err, i) => `${i + 1}. ${err}`).join('\n')}
131+
Perf map content:
132+
${report.perfMap}
133+
</end perf map content>
134+
`;
135+
}
136+
137+
function runSuite() {
138+
const failures = [];
139+
140+
for (const tc of testCases) {
141+
const report = runTest(tc);
142+
if (report.errors.length > 0) {
143+
failures.push(report);
144+
}
145+
}
146+
147+
const errorsToReport = failures.map(serializeError).join('\n--------\n');
148+
149+
assert.strictEqual(failures.length, 0, `${failures.length} tests failed\n\n${errorsToReport}`);
150+
}
151+
152+
runSuite();

0 commit comments

Comments
 (0)
Please sign in to comment.