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

Commit

Permalink
report: add average CPU consumption
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bidipyne committed Mar 28, 2017
1 parent c4f1da5 commit 67a335e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(&current_time);
TIME_TYPE tm_struct;
#ifdef _WIN32
GetLocalTime(&tm_struct);
Expand Down Expand Up @@ -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 devision by zero.
out << "\n================================================================================";
out << "\n==== Resource Usage ============================================================\n";

Expand All @@ -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) "
Expand All @@ -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";
}
Expand Down

0 comments on commit 67a335e

Please sign in to comment.