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

report: add average CPU consumption #79

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ 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

/*******************************************************************************
* Functions to process node-report configuration options:
Expand Down Expand Up @@ -301,6 +302,7 @@ void SetLoadTime() {
gettimeofday(&time_val, nullptr);
localtime_r(&time_val.tv_sec, &loadtime_tm_struct);
#endif
load_time = mktime(&loadtime_tm_struct);
}

/*******************************************************************************
Expand Down Expand Up @@ -1020,6 +1022,13 @@ static void PrintGCStatistics(std::ostream& out, Isolate* isolate) {
******************************************************************************/
static void PrintResourceUsage(std::ostream& out) {
char buf[64];
double cpu_abs;
double cpu_percentage;
time_t current_time; // current time absolute
time(&current_time);
auto uptime = difftime(current_time, load_time);
if (uptime == 0)
uptime = 1; // avoid division by zero.
out << "\n================================================================================";
out << "\n==== Resource Usage ============================================================\n";

Expand All @@ -1038,6 +1047,9 @@ static void PrintResourceUsage(std::ostream& out) {
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
out << "\n Kernel mode CPU: " << buf << " secs";
#endif
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;
cpu_percentage = (cpu_abs / uptime) * 100.0;
out << "\n Average CPU 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 @@ -1059,6 +1071,9 @@ static void PrintResourceUsage(std::ostream& out) {
snprintf( buf, sizeof(buf), "%ld.%06ld", stats.ru_stime.tv_sec, stats.ru_stime.tv_usec);
out << "\n Kernel mode CPU: " << buf << " secs";
#endif
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;
cpu_percentage = (cpu_abs / uptime) * 100.0;
out << "\n Average CPU Consumption : " << cpu_percentage << "%";
out << "\n Filesystem activity: " << stats.ru_inblock << " reads "
<< stats.ru_oublock << " writes";
}
Expand Down