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

Commit c9c1359

Browse files
bidipynernchamberlain
authored andcommitted
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. PR-URL: #79 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Howard Hellyer <[email protected]>
1 parent ff6462a commit c9c1359

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/node_report.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ 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
109110

110111
/*******************************************************************************
111112
* Functions to process node-report configuration options:
@@ -301,6 +302,7 @@ void SetLoadTime() {
301302
gettimeofday(&time_val, nullptr);
302303
localtime_r(&time_val.tv_sec, &loadtime_tm_struct);
303304
#endif
305+
load_time = mktime(&loadtime_tm_struct);
304306
}
305307

306308
/*******************************************************************************
@@ -1025,6 +1027,13 @@ static void PrintGCStatistics(std::ostream& out, Isolate* isolate) {
10251027
******************************************************************************/
10261028
static void PrintResourceUsage(std::ostream& out) {
10271029
char buf[64];
1030+
double cpu_abs;
1031+
double cpu_percentage;
1032+
time_t current_time; // current time absolute
1033+
time(&current_time);
1034+
auto uptime = difftime(current_time, load_time);
1035+
if (uptime == 0)
1036+
uptime = 1; // avoid division by zero.
10281037
out << "\n================================================================================";
10291038
out << "\n==== Resource Usage ============================================================\n";
10301039

@@ -1043,6 +1052,9 @@ static void PrintResourceUsage(std::ostream& out) {
10431052
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
10441053
out << "\n Kernel mode CPU: " << buf << " secs";
10451054
#endif
1055+
cpu_abs = stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec + stats.ru_stime.tv_sec + 0.000001 * stats.ru_stime.tv_usec;
1056+
cpu_percentage = (cpu_abs / uptime) * 100.0;
1057+
out << "\n Average CPU Consumption : "<< cpu_percentage << "%";
10461058
out << "\n Maximum resident set size: ";
10471059
WriteInteger(out, stats.ru_maxrss * 1024);
10481060
out << " bytes\n Page faults: " << stats.ru_majflt << " (I/O required) "
@@ -1064,6 +1076,9 @@ static void PrintResourceUsage(std::ostream& out) {
10641076
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
10651077
out << "\n Kernel mode CPU: " << buf << " secs";
10661078
#endif
1079+
cpu_abs = stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec + stats.ru_stime.tv_sec + 0.000001 * stats.ru_stime.tv_usec;
1080+
cpu_percentage = (cpu_abs / uptime) * 100.0;
1081+
out << "\n Average CPU Consumption : " << cpu_percentage << "%";
10671082
out << "\n Filesystem activity: " << stats.ru_inblock << " reads "
10681083
<< stats.ru_oublock << " writes";
10691084
}

0 commit comments

Comments
 (0)