11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 41cb0ef41Sopenharmony_ciconst fs = require('fs'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ciconst util = require('util'); 71cb0ef41Sopenharmony_ciconst { join } = require('path'); 81cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci// Test that --prof also tracks Worker threads. 111cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/24016 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') { 141cb0ef41Sopenharmony_ci const fs = require('fs'); 151cb0ef41Sopenharmony_ci let files = fs.readdirSync(tmpdir.path); 161cb0ef41Sopenharmony_ci const plog = files.filter((name) => /\.log$/.test(name))[0]; 171cb0ef41Sopenharmony_ci if (plog === undefined) { 181cb0ef41Sopenharmony_ci console.error('`--prof` did not produce a profile log for parent thread!'); 191cb0ef41Sopenharmony_ci process.exit(1); 201cb0ef41Sopenharmony_ci } 211cb0ef41Sopenharmony_ci const pingpong = ` 221cb0ef41Sopenharmony_ci let counter = 0; 231cb0ef41Sopenharmony_ci const fs = require('fs'); 241cb0ef41Sopenharmony_ci const { Worker, parentPort } = require('worker_threads'); 251cb0ef41Sopenharmony_ci parentPort.on('message', (m) => { 261cb0ef41Sopenharmony_ci if (counter++ === 1024) 271cb0ef41Sopenharmony_ci process.exit(0); 281cb0ef41Sopenharmony_ci parentPort.postMessage( 291cb0ef41Sopenharmony_ci fs.readFileSync(m.toString()).slice(0, 1024 * 1024)); 301cb0ef41Sopenharmony_ci }); 311cb0ef41Sopenharmony_ci `; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci const { Worker } = require('worker_threads'); 341cb0ef41Sopenharmony_ci const w = new Worker(pingpong, { eval: true }); 351cb0ef41Sopenharmony_ci w.on('message', (m) => { 361cb0ef41Sopenharmony_ci w.postMessage(__filename); 371cb0ef41Sopenharmony_ci }); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci w.on('exit', common.mustCall(() => { 401cb0ef41Sopenharmony_ci files = fs.readdirSync(tmpdir.path); 411cb0ef41Sopenharmony_ci const wlog = files.filter((name) => /\.log$/.test(name) && name !== plog)[0]; 421cb0ef41Sopenharmony_ci if (wlog === undefined) { 431cb0ef41Sopenharmony_ci console.error('`--prof` did not produce a profile log' + 441cb0ef41Sopenharmony_ci ' for worker thread!'); 451cb0ef41Sopenharmony_ci process.exit(1); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci process.exit(0); 481cb0ef41Sopenharmony_ci })); 491cb0ef41Sopenharmony_ci w.postMessage(__filename); 501cb0ef41Sopenharmony_ci} else { 511cb0ef41Sopenharmony_ci tmpdir.refresh(); 521cb0ef41Sopenharmony_ci const timeout = common.platformTimeout(30_000); 531cb0ef41Sopenharmony_ci const spawnResult = spawnSync( 541cb0ef41Sopenharmony_ci process.execPath, ['--prof', __filename, 'child'], 551cb0ef41Sopenharmony_ci { cwd: tmpdir.path, encoding: 'utf8', timeout }); 561cb0ef41Sopenharmony_ci assert.strictEqual(spawnResult.stderr.toString(), '', 571cb0ef41Sopenharmony_ci `child exited with an error: \ 581cb0ef41Sopenharmony_ci ${util.inspect(spawnResult)}`); 591cb0ef41Sopenharmony_ci assert.strictEqual(spawnResult.signal, null, 601cb0ef41Sopenharmony_ci `child exited with signal: ${util.inspect(spawnResult)}`); 611cb0ef41Sopenharmony_ci assert.strictEqual(spawnResult.status, 0, 621cb0ef41Sopenharmony_ci `child exited with non-zero status: \ 631cb0ef41Sopenharmony_ci ${util.inspect(spawnResult)}`); 641cb0ef41Sopenharmony_ci const files = fs.readdirSync(tmpdir.path); 651cb0ef41Sopenharmony_ci const logfiles = files.filter((name) => /\.log$/.test(name)); 661cb0ef41Sopenharmony_ci assert.strictEqual(logfiles.length, 2); // Parent thread + child thread. 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci for (const logfile of logfiles) { 691cb0ef41Sopenharmony_ci const lines = fs.readFileSync( 701cb0ef41Sopenharmony_ci join(tmpdir.path, logfile), 'utf8').split('\n'); 711cb0ef41Sopenharmony_ci const ticks = lines.filter((line) => /^tick,/.test(line)).length; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci // Test that at least 15 ticks have been recorded for both parent and child 741cb0ef41Sopenharmony_ci // threads. When not tracking Worker threads, only 1 or 2 ticks would 751cb0ef41Sopenharmony_ci // have been recorded. 761cb0ef41Sopenharmony_ci // When running locally, this number is usually around 200 771cb0ef41Sopenharmony_ci // for both threads, so 15 seems like a very safe threshold. 781cb0ef41Sopenharmony_ci assert(ticks >= 15, `${ticks} >= 15`); 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci} 81