Skip to content

Commit 4eaac83

Browse files
boneskulltargos
authored andcommitted
report: add cpu info to report output
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 <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9b248e3 commit 4eaac83

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

doc/api/report.md

+20
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ is provided below for reference.
6262
"osRelease": "3.10.0-862.el7.x86_64",
6363
"osVersion": "#1 SMP Wed Mar 21 18:14:51 EDT 2018",
6464
"osMachine": "x86_64",
65+
"osCpus": [
66+
{
67+
"model": "Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz",
68+
"speed": 2700,
69+
"user": 88902660,
70+
"nice": 0,
71+
"sys": 50902570,
72+
"idle": 241732220,
73+
"irq": 0
74+
},
75+
{
76+
"model": "Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz",
77+
"speed": 2700,
78+
"user": 88902660,
79+
"nice": 0,
80+
"sys": 50902570,
81+
"idle": 241732220,
82+
"irq": 0
83+
}
84+
],
6585
"host": "test_machine"
6686
},
6787
"javascriptStack": {

src/node_report.cc

+25
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static void PrintSystemInformation(JSONWriter* writer);
6565
static void PrintLoadedLibraries(JSONWriter* writer);
6666
static void PrintComponentVersions(JSONWriter* writer);
6767
static void PrintRelease(JSONWriter* writer);
68+
static void PrintCpuInfo(JSONWriter* writer);
6869

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

319+
PrintCpuInfo(writer);
320+
318321
char host[UV_MAXHOSTNAMESIZE];
319322
size_t host_size = sizeof(host);
320323

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

328+
// Report CPU info
329+
static void PrintCpuInfo(JSONWriter* writer) {
330+
uv_cpu_info_t* cpu_info;
331+
int count;
332+
if (uv_cpu_info(&cpu_info, &count) == 0) {
333+
writer->json_arraystart("cpus");
334+
for (int i = 0; i < count; i++) {
335+
writer->json_start();
336+
writer->json_keyvalue("model", cpu_info->model);
337+
writer->json_keyvalue("speed", cpu_info->speed);
338+
writer->json_keyvalue("user", cpu_info->cpu_times.user);
339+
writer->json_keyvalue("nice", cpu_info->cpu_times.nice);
340+
writer->json_keyvalue("sys", cpu_info->cpu_times.sys);
341+
writer->json_keyvalue("idle", cpu_info->cpu_times.idle);
342+
writer->json_keyvalue("irq", cpu_info->cpu_times.irq);
343+
writer->json_end();
344+
}
345+
writer->json_arrayend();
346+
uv_free_cpu_info(cpu_info, count);
347+
}
348+
}
349+
325350
// Report the JavaScript stack.
326351
static void PrintJavaScriptStack(JSONWriter* writer,
327352
Isolate* isolate,

test/common/report.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ function _validateContent(data) {
6262
'dumpEventTimeStamp', 'processId', 'commandLine',
6363
'nodejsVersion', 'wordSize', 'arch', 'platform',
6464
'componentVersions', 'release', 'osName', 'osRelease',
65-
'osVersion', 'osMachine', 'host', 'glibcVersionRuntime',
66-
'glibcVersionCompiler', 'cwd'];
65+
'osVersion', 'osMachine', 'cpus', 'host',
66+
'glibcVersionRuntime', 'glibcVersionCompiler', 'cwd'];
6767
checkForUnknownFields(header, headerFields);
6868
assert.strictEqual(typeof header.event, 'string');
6969
assert.strictEqual(typeof header.trigger, 'string');
@@ -87,6 +87,16 @@ function _validateContent(data) {
8787
assert.strictEqual(header.osRelease, os.release());
8888
assert.strictEqual(typeof header.osVersion, 'string');
8989
assert.strictEqual(typeof header.osMachine, 'string');
90+
assert(Array.isArray(header.cpus));
91+
header.cpus.forEach((cpu) => {
92+
assert.strictEqual(typeof cpu.model, 'string');
93+
assert.strictEqual(typeof cpu.speed, 'number');
94+
assert.strictEqual(typeof cpu.user, 'number');
95+
assert.strictEqual(typeof cpu.nice, 'number');
96+
assert.strictEqual(typeof cpu.sys, 'number');
97+
assert.strictEqual(typeof cpu.idle, 'number');
98+
assert.strictEqual(typeof cpu.irq, 'number');
99+
});
90100
assert.strictEqual(header.host, os.hostname());
91101

92102
// Verify the format of the javascriptStack section.

0 commit comments

Comments
 (0)