Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.

Commit 6c4a11c

Browse files
committed
report: add average CPU consumption
node-report currently produces absolute CPU consumption from the time since the process started. By making use of the load time and the current time, convert this into an average consumption rate which is easily consumable.
1 parent c4f1da5 commit 6c4a11c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/node_report.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ static char report_directory[NR_MAXPATH + 1] = ""; // defaults to current workin
106106
std::string version_string = UNKNOWN_NODEVERSION_STRING;
107107
std::string commandline_string = "";
108108
static TIME_TYPE loadtime_tm_struct; // module load time
109+
static time_t load_time; // module load time absolute
110+
static time_t current_time; // current time absolute
109111

110112
/*******************************************************************************
111113
* Functions to process node-report configuration options:
@@ -294,6 +296,7 @@ void SetVersionString(Isolate* isolate) {
294296
* Function to save the node-report module load time value
295297
*******************************************************************************/
296298
void SetLoadTime() {
299+
time(&load_time);
297300
#ifdef _WIN32
298301
GetLocalTime(&loadtime_tm_struct);
299302
#else // UNIX, OSX
@@ -383,6 +386,7 @@ void TriggerNodeReport(Isolate* isolate, DumpEvent event, const char* message, c
383386
report_active = true;
384387

385388
// Obtain the current time and the pid (platform dependent)
389+
time(&current_time);
386390
TIME_TYPE tm_struct;
387391
#ifdef _WIN32
388392
GetLocalTime(&tm_struct);
@@ -1020,6 +1024,11 @@ static void PrintGCStatistics(std::ostream& out, Isolate* isolate) {
10201024
******************************************************************************/
10211025
static void PrintResourceUsage(std::ostream& out) {
10221026
char buf[64];
1027+
double cpu_abs;
1028+
double cpu_percentage;
1029+
long int diff_time = current_time - load_time;
1030+
if (diff_time == 0)
1031+
diff_time = 1; // avoid division by zero.
10231032
out << "\n================================================================================";
10241033
out << "\n==== Resource Usage ============================================================\n";
10251034

@@ -1030,14 +1039,20 @@ static void PrintResourceUsage(std::ostream& out) {
10301039
#if defined(__APPLE__) || defined(_AIX)
10311040
snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec);
10321041
out << "\n User mode CPU: " << buf << " secs";
1042+
cpu_abs = std::stod(buf);
10331043
snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
10341044
out << "\n Kernel mode CPU: " << buf << " secs";
1045+
cpu_abs += std::stod(buf);
10351046
#else
10361047
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec);
10371048
out << "\n User mode CPU: " << buf << " secs";
1049+
cpu_abs = std::stod(buf);
10381050
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
10391051
out << "\n Kernel mode CPU: " << buf << " secs";
1052+
cpu_abs += std::stod(buf);
10401053
#endif
1054+
cpu_percentage = (cpu_abs / diff_time) * 100;
1055+
out << "\n Average Consumption : "<< cpu_percentage << "%";
10411056
out << "\n Maximum resident set size: ";
10421057
WriteInteger(out, stats.ru_maxrss * 1024);
10431058
out << " bytes\n Page faults: " << stats.ru_majflt << " (I/O required) "
@@ -1051,14 +1066,20 @@ static void PrintResourceUsage(std::ostream& out) {
10511066
#if defined(__APPLE__) || defined(_AIX)
10521067
snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec);
10531068
out << "\n User mode CPU: " << buf << " secs";
1069+
cpu_abs = std::stod(buf);
10541070
snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
10551071
out << "\n Kernel mode CPU: " << buf << " secs";
1072+
cpu_abs += std::stod(buf);
10561073
#else
10571074
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec);
10581075
out << "\n User mode CPU: " << buf << " secs";
1076+
cpu_abs = std::stod(buf);
10591077
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
10601078
out << "\n Kernel mode CPU: " << buf << " secs";
1079+
cpu_abs += std::stod(buf);
10611080
#endif
1081+
cpu_percentage = (cpu_abs / diff_time) * 100;
1082+
out << "\n Average Consumption : " << cpu_percentage << "%";
10621083
out << "\n Filesystem activity: " << stats.ru_inblock << " reads "
10631084
<< stats.ru_oublock << " writes";
10641085
}

0 commit comments

Comments
 (0)