From 5310034af2efedaec94cbf82c2c254a6c74a330f Mon Sep 17 00:00:00 2001 From: Michael Dokolin Date: Mon, 27 Jan 2025 21:30:22 +0100 Subject: [PATCH] [CLI] Add logging messages to simplify CPU profiling (#207885) Resolves #206052. --- src/cli/profiler/profiler.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cli/profiler/profiler.js b/src/cli/profiler/profiler.js index ee06c0a377568..7c6b5e5b408db 100644 --- a/src/cli/profiler/profiler.js +++ b/src/cli/profiler/profiler.js @@ -13,16 +13,19 @@ import { join } from 'path'; import { Session } from 'node:inspector'; import { threadId } from 'node:worker_threads'; import { promisify } from 'util'; +import { Logger } from '../logger'; class Profiler { #counter = 0; + #logger; #path; #session; - constructor() { + constructor(logger) { const execOpts = getopts(process.execArgv); const envOpts = getopts(process.env.NODE_OPTIONS ? process.env.NODE_OPTIONS.split(/\s+/) : []); this.#path = execOpts['diagnostic-dir'] || envOpts['diagnostic-dir'] || process.cwd(); + this.#logger = logger; } #getPath() { @@ -51,13 +54,17 @@ class Profiler { await promisify(this.#session.post)('Profiler.enable'); await promisify(this.#session.post)('Profiler.start'); + this.#logger.log(`CPU profiling is started for process '${process.pid}'.`); } async #stop() { try { const { profile } = await promisify(this.#session.post)('Profiler.stop'); + this.#logger.log(`CPU profiling is stopped for process '${process.pid}'.`); + const path = this.#getPath(); await promisify(writeFile)(path, JSON.stringify(profile)); + this.#logger.log(`Saved CPU profile to '${path}'.`); } finally { this.#session.disconnect(); this.#session = undefined; @@ -81,8 +88,10 @@ export default function (program) { return; } - const profiler = new Profiler(); + const logger = new Logger(); + const profiler = new Profiler(logger); process.removeAllListeners(signal); process.on(signal, profiler.toggle.bind(profiler)); + logger.log(`CPU profiling is enabled on '${signal}' for process '${process.pid}'.`); }); }