Skip to content

Commit

Permalink
report: add cpu info to report output
Browse files Browse the repository at this point in the history
The report shows CPU consumption %, but without the number of CPU cores,
a consumer cannot tell if the percent (given across all cores) is
actually problematic. E.g., 100% on one CPU is a problem, but 100% on
four CPUs is not necessarily.

This change adds CPU information (similar to `os.cpus()`) to the report
output. Extra info besides the count is also provided as to avoid future
breaking changes in the eventuality that someone needs it; changing the
datatype of `header.cpus` would be breaking.

PR-URL: #28188
Refs: nodejs/diagnostics#307
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
boneskull authored and targos committed Jun 18, 2019
1 parent 9b248e3 commit 4eaac83
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
20 changes: 20 additions & 0 deletions doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ is provided below for reference.
"osRelease": "3.10.0-862.el7.x86_64",
"osVersion": "#1 SMP Wed Mar 21 18:14:51 EDT 2018",
"osMachine": "x86_64",
"osCpus": [
{
"model": "Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz",
"speed": 2700,
"user": 88902660,
"nice": 0,
"sys": 50902570,
"idle": 241732220,
"irq": 0
},
{
"model": "Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz",
"speed": 2700,
"user": 88902660,
"nice": 0,
"sys": 50902570,
"idle": 241732220,
"irq": 0
}
],
"host": "test_machine"
},
"javascriptStack": {
Expand Down
25 changes: 25 additions & 0 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static void PrintSystemInformation(JSONWriter* writer);
static void PrintLoadedLibraries(JSONWriter* writer);
static void PrintComponentVersions(JSONWriter* writer);
static void PrintRelease(JSONWriter* writer);
static void PrintCpuInfo(JSONWriter* writer);

// External function to trigger a report, writing to file.
// The 'name' parameter is in/out: an input filename is used
Expand Down Expand Up @@ -315,13 +316,37 @@ static void PrintVersionInformation(JSONWriter* writer) {
writer->json_keyvalue("osMachine", os_info.machine);
}

PrintCpuInfo(writer);

char host[UV_MAXHOSTNAMESIZE];
size_t host_size = sizeof(host);

if (uv_os_gethostname(host, &host_size) == 0)
writer->json_keyvalue("host", host);
}

// Report CPU info
static void PrintCpuInfo(JSONWriter* writer) {
uv_cpu_info_t* cpu_info;
int count;
if (uv_cpu_info(&cpu_info, &count) == 0) {
writer->json_arraystart("cpus");
for (int i = 0; i < count; i++) {
writer->json_start();
writer->json_keyvalue("model", cpu_info->model);
writer->json_keyvalue("speed", cpu_info->speed);
writer->json_keyvalue("user", cpu_info->cpu_times.user);
writer->json_keyvalue("nice", cpu_info->cpu_times.nice);
writer->json_keyvalue("sys", cpu_info->cpu_times.sys);
writer->json_keyvalue("idle", cpu_info->cpu_times.idle);
writer->json_keyvalue("irq", cpu_info->cpu_times.irq);
writer->json_end();
}
writer->json_arrayend();
uv_free_cpu_info(cpu_info, count);
}
}

// Report the JavaScript stack.
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
Expand Down
14 changes: 12 additions & 2 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ function _validateContent(data) {
'dumpEventTimeStamp', 'processId', 'commandLine',
'nodejsVersion', 'wordSize', 'arch', 'platform',
'componentVersions', 'release', 'osName', 'osRelease',
'osVersion', 'osMachine', 'host', 'glibcVersionRuntime',
'glibcVersionCompiler', 'cwd'];
'osVersion', 'osMachine', 'cpus', 'host',
'glibcVersionRuntime', 'glibcVersionCompiler', 'cwd'];
checkForUnknownFields(header, headerFields);
assert.strictEqual(typeof header.event, 'string');
assert.strictEqual(typeof header.trigger, 'string');
Expand All @@ -87,6 +87,16 @@ function _validateContent(data) {
assert.strictEqual(header.osRelease, os.release());
assert.strictEqual(typeof header.osVersion, 'string');
assert.strictEqual(typeof header.osMachine, 'string');
assert(Array.isArray(header.cpus));
header.cpus.forEach((cpu) => {
assert.strictEqual(typeof cpu.model, 'string');
assert.strictEqual(typeof cpu.speed, 'number');
assert.strictEqual(typeof cpu.user, 'number');
assert.strictEqual(typeof cpu.nice, 'number');
assert.strictEqual(typeof cpu.sys, 'number');
assert.strictEqual(typeof cpu.idle, 'number');
assert.strictEqual(typeof cpu.irq, 'number');
});
assert.strictEqual(header.host, os.hostname());

// Verify the format of the javascriptStack section.
Expand Down

0 comments on commit 4eaac83

Please sign in to comment.