From e0e308448240260c207958dfc3dd9245d903af85 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 23 Mar 2019 07:39:52 +0800 Subject: [PATCH] inspector: implement --cpu-prof[-path] This patch introduces a CLI flag --cpu-prof that starts the V8 CPU profiler on start up, and ends the profiler then writes the CPU profile before the Node.js instance (on the main thread or the worker thread) exits. By default the profile is written to `${cwd}/CPU.${yyyymmdd}.${hhmmss}.${pid}.${tid}.${seq}.cpuprofile`. The patch also introduces a --cpu-prof-path flag for the user to specify the path the profile will be written to. Refs: /~https://github.com/nodejs/node/issues/26878 PR-URL: /~https://github.com/nodejs/node/pull/27147 Reviewed-By: Anna Henningsen --- doc/api/cli.md | 31 ++++ doc/node.1 | 12 ++ src/env-inl.h | 19 ++ src/env.h | 10 ++ src/inspector_profiler.cc | 146 ++++++++++++++- src/inspector_profiler.h | 18 ++ src/node.cc | 6 + src/node_internals.h | 1 + src/node_options.cc | 12 ++ src/node_options.h | 4 + test/fixtures/workload/fibonacci-exit.js | 7 + test/fixtures/workload/fibonacci-sigint.js | 7 + test/fixtures/workload/fibonacci-worker.js | 7 + test/fixtures/workload/fibonacci.js | 8 + test/sequential/test-cpu-prof.js | 195 +++++++++++++++++++++ 15 files changed, 480 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/workload/fibonacci-exit.js create mode 100644 test/fixtures/workload/fibonacci-sigint.js create mode 100644 test/fixtures/workload/fibonacci-worker.js create mode 100644 test/fixtures/workload/fibonacci.js create mode 100644 test/sequential/test-cpu-prof.js diff --git a/doc/api/cli.md b/doc/api/cli.md index a2c92b13341df0..c26177444c7894 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -75,6 +75,37 @@ $ node --completion-bash > node_bash_completion $ source node_bash_completion ``` +### `--cpu-prof` + + +> Stability: 1 - Experimental + +Starts the V8 CPU profiler on start up, and writes the CPU profile to disk +before exit. If `--cpu-prof-path` is not specified, the profile will be +written to `${cwd}/CPU.${yyyymmdd}.${hhmmss}.${pid}.${tid}.${seq}.cpuprofile`. + +```console +$ node --cpu-prof index.js +$ ls *.cpuprofile +CPU.20190409.202950.15293.0.0.cpuprofile +``` + +### `--cpu-prof-path` + + +> Stability: 1 - Experimental + +Location where the CPU profile generated by `--cpu-prof` +should be written to. When used alone, it implies `--cpu-prof`. + +```console +$ node --cpu-prof-path /tmp/test.cpuprofile index.js +``` + ### `--diagnostic-report-directory=directory`