Skip to content

Commit

Permalink
report: add rss and use/kernel cpu usage fields
Browse files Browse the repository at this point in the history
PR-URL: #45043
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
theanarkh authored and danielleadams committed Jan 3, 2023
1 parent 20a39a3 commit a880568
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
19 changes: 12 additions & 7 deletions doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ is provided below for reference.
}
},
"resourceUsage": {
"userCpuSeconds": 0.069595,
"kernelCpuSeconds": 0.019163,
"cpuConsumptionPercent": 0.000000,
"maxRss": 18079744,
"rss": 45768704,
"userCpuSeconds": 0.040072,
"kernelCpuSeconds": 0.016029,
"cpuConsumptionPercent": 5.6101,
"userCpuConsumptionPercent": 4.0072,
"kernelCpuConsumptionPercent": 1.6029,
"maxRss": 45768704,
"pageFaults": {
"IORequired": 0,
"IONotRequired": 4610
Expand All @@ -212,9 +215,11 @@ is provided below for reference.
}
},
"uvthreadResourceUsage": {
"userCpuSeconds": 0.068457,
"kernelCpuSeconds": 0.019127,
"cpuConsumptionPercent": 0.000000,
"userCpuSeconds": 0.039843,
"kernelCpuSeconds": 0.015937,
"cpuConsumptionPercent": 5.578,
"userCpuConsumptionPercent": 3.9843,
"kernelCpuConsumptionPercent": 1.5937,
"fsActivity": {
"reads": 0,
"writes": 0
Expand Down
15 changes: 15 additions & 0 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,13 @@ static void PrintResourceUsage(JSONWriter* writer) {
// Process and current thread usage statistics
uv_rusage_t rusage;
writer->json_objectstart("resourceUsage");

size_t rss;
int err = uv_resident_set_memory(&rss);
if (!err) {
writer->json_keyvalue("rss", rss);
}

if (uv_getrusage(&rusage) == 0) {
double user_cpu =
rusage.ru_utime.tv_sec + SEC_PER_MICROS * rusage.ru_utime.tv_usec;
Expand All @@ -636,7 +643,11 @@ static void PrintResourceUsage(JSONWriter* writer) {
writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
double cpu_abs = user_cpu + kernel_cpu;
double cpu_percentage = (cpu_abs / uptime) * 100.0;
double user_cpu_percentage = (user_cpu / uptime) * 100.0;
double kernel_cpu_percentage = (kernel_cpu / uptime) * 100.0;
writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
writer->json_keyvalue("userCpuConsumptionPercent", user_cpu_percentage);
writer->json_keyvalue("kernelCpuConsumptionPercent", kernel_cpu_percentage);
writer->json_keyvalue("maxRss", rusage.ru_maxrss * 1024);
writer->json_objectstart("pageFaults");
writer->json_keyvalue("IORequired", rusage.ru_majflt);
Expand All @@ -660,7 +671,11 @@ static void PrintResourceUsage(JSONWriter* writer) {
writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
double cpu_abs = user_cpu + kernel_cpu;
double cpu_percentage = (cpu_abs / uptime) * 100.0;
double user_cpu_percentage = (user_cpu / uptime) * 100.0;
double kernel_cpu_percentage = (kernel_cpu / uptime) * 100.0;
writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
writer->json_keyvalue("userCpuConsumptionPercent", user_cpu_percentage);
writer->json_keyvalue("kernelCpuConsumptionPercent", kernel_cpu_percentage);
writer->json_objectstart("fsActivity");
writer->json_keyvalue("reads", stats.ru_inblock);
writer->json_keyvalue("writes", stats.ru_oublock);
Expand Down
12 changes: 10 additions & 2 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,16 @@ function _validateContent(report, fields = []) {
// Verify the format of the resourceUsage section.
const usage = report.resourceUsage;
const resourceUsageFields = ['userCpuSeconds', 'kernelCpuSeconds',
'cpuConsumptionPercent', 'maxRss',
'cpuConsumptionPercent', 'userCpuConsumptionPercent',
'kernelCpuConsumptionPercent', 'rss', 'maxRss',
'pageFaults', 'fsActivity'];
checkForUnknownFields(usage, resourceUsageFields);
assert.strictEqual(typeof usage.userCpuSeconds, 'number');
assert.strictEqual(typeof usage.kernelCpuSeconds, 'number');
assert.strictEqual(typeof usage.cpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.userCpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.kernelCpuConsumptionPercent, 'number');
assert(Number.isSafeInteger(usage.rss));
assert(Number.isSafeInteger(usage.maxRss));
assert(typeof usage.pageFaults === 'object' && usage.pageFaults !== null);
checkForUnknownFields(usage.pageFaults, ['IORequired', 'IONotRequired']);
Expand All @@ -254,11 +258,15 @@ function _validateContent(report, fields = []) {
if (report.uvthreadResourceUsage) {
const usage = report.uvthreadResourceUsage;
const threadUsageFields = ['userCpuSeconds', 'kernelCpuSeconds',
'cpuConsumptionPercent', 'fsActivity'];
'cpuConsumptionPercent', 'fsActivity',
'userCpuConsumptionPercent',
'kernelCpuConsumptionPercent'];
checkForUnknownFields(usage, threadUsageFields);
assert.strictEqual(typeof usage.userCpuSeconds, 'number');
assert.strictEqual(typeof usage.kernelCpuSeconds, 'number');
assert.strictEqual(typeof usage.cpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.userCpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.kernelCpuConsumptionPercent, 'number');
assert(typeof usage.fsActivity === 'object' && usage.fsActivity !== null);
checkForUnknownFields(usage.fsActivity, ['reads', 'writes']);
assert(Number.isSafeInteger(usage.fsActivity.reads));
Expand Down

0 comments on commit a880568

Please sign in to comment.