From 6c4a11cd787a5e2c07ed344c1e306bfac047981b Mon Sep 17 00:00:00 2001 From: Bidisha Pyne Date: Mon, 27 Mar 2017 11:09:12 -0400 Subject: [PATCH] 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. --- src/node_report.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/node_report.cc b/src/node_report.cc index bd6285a..a2113a0 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -106,6 +106,8 @@ static char report_directory[NR_MAXPATH + 1] = ""; // defaults to current workin std::string version_string = UNKNOWN_NODEVERSION_STRING; std::string commandline_string = ""; static TIME_TYPE loadtime_tm_struct; // module load time +static time_t load_time; // module load time absolute +static time_t current_time; // current time absolute /******************************************************************************* * Functions to process node-report configuration options: @@ -294,6 +296,7 @@ void SetVersionString(Isolate* isolate) { * Function to save the node-report module load time value *******************************************************************************/ void SetLoadTime() { + time(&load_time); #ifdef _WIN32 GetLocalTime(&loadtime_tm_struct); #else // UNIX, OSX @@ -383,6 +386,7 @@ void TriggerNodeReport(Isolate* isolate, DumpEvent event, const char* message, c report_active = true; // Obtain the current time and the pid (platform dependent) + time(¤t_time); TIME_TYPE tm_struct; #ifdef _WIN32 GetLocalTime(&tm_struct); @@ -1020,6 +1024,11 @@ static void PrintGCStatistics(std::ostream& out, Isolate* isolate) { ******************************************************************************/ static void PrintResourceUsage(std::ostream& out) { char buf[64]; + double cpu_abs; + double cpu_percentage; + long int diff_time = current_time - load_time; + if (diff_time == 0) + diff_time = 1; // avoid division by zero. out << "\n================================================================================"; out << "\n==== Resource Usage ============================================================\n"; @@ -1030,14 +1039,20 @@ static void PrintResourceUsage(std::ostream& out) { #if defined(__APPLE__) || defined(_AIX) snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec); out << "\n User mode CPU: " << buf << " secs"; + cpu_abs = std::stod(buf); snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec); out << "\n Kernel mode CPU: " << buf << " secs"; + cpu_abs += std::stod(buf); #else snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec); out << "\n User mode CPU: " << buf << " secs"; + cpu_abs = std::stod(buf); snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec); out << "\n Kernel mode CPU: " << buf << " secs"; + cpu_abs += std::stod(buf); #endif + cpu_percentage = (cpu_abs / diff_time) * 100; + out << "\n Average Consumption : "<< cpu_percentage << "%"; out << "\n Maximum resident set size: "; WriteInteger(out, stats.ru_maxrss * 1024); out << " bytes\n Page faults: " << stats.ru_majflt << " (I/O required) " @@ -1051,14 +1066,20 @@ static void PrintResourceUsage(std::ostream& out) { #if defined(__APPLE__) || defined(_AIX) snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec); out << "\n User mode CPU: " << buf << " secs"; + cpu_abs = std::stod(buf); snprintf( buf, sizeof(buf), "%ld.%06d", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec); out << "\n Kernel mode CPU: " << buf << " secs"; + cpu_abs += std::stod(buf); #else snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_utime.tv_sec, stats.ru_utime.tv_usec); out << "\n User mode CPU: " << buf << " secs"; + cpu_abs = std::stod(buf); snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec); out << "\n Kernel mode CPU: " << buf << " secs"; + cpu_abs += std::stod(buf); #endif + cpu_percentage = (cpu_abs / diff_time) * 100; + out << "\n Average Consumption : " << cpu_percentage << "%"; out << "\n Filesystem activity: " << stats.ru_inblock << " reads " << stats.ru_oublock << " writes"; }