@@ -106,6 +106,8 @@ static char report_directory[NR_MAXPATH + 1] = ""; // defaults to current workin
106
106
std::string version_string = UNKNOWN_NODEVERSION_STRING;
107
107
std::string commandline_string = " " ;
108
108
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
109
111
110
112
/* ******************************************************************************
111
113
* Functions to process node-report configuration options:
@@ -294,6 +296,7 @@ void SetVersionString(Isolate* isolate) {
294
296
* Function to save the node-report module load time value
295
297
*******************************************************************************/
296
298
void SetLoadTime () {
299
+ time (&load_time);
297
300
#ifdef _WIN32
298
301
GetLocalTime (&loadtime_tm_struct);
299
302
#else // UNIX, OSX
@@ -383,6 +386,7 @@ void TriggerNodeReport(Isolate* isolate, DumpEvent event, const char* message, c
383
386
report_active = true ;
384
387
385
388
// Obtain the current time and the pid (platform dependent)
389
+ time (¤t_time);
386
390
TIME_TYPE tm_struct;
387
391
#ifdef _WIN32
388
392
GetLocalTime (&tm_struct);
@@ -1020,6 +1024,11 @@ static void PrintGCStatistics(std::ostream& out, Isolate* isolate) {
1020
1024
******************************************************************************/
1021
1025
static void PrintResourceUsage (std::ostream& out) {
1022
1026
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.
1023
1032
out << " \n ================================================================================" ;
1024
1033
out << " \n ==== Resource Usage ============================================================\n " ;
1025
1034
@@ -1030,14 +1039,20 @@ static void PrintResourceUsage(std::ostream& out) {
1030
1039
#if defined(__APPLE__) || defined(_AIX)
1031
1040
snprintf ( buf, sizeof (buf), " %ld.%06d" , stats.ru_utime .tv_sec , stats.ru_utime .tv_usec );
1032
1041
out << " \n User mode CPU: " << buf << " secs" ;
1042
+ cpu_abs = std::stod (buf);
1033
1043
snprintf ( buf, sizeof (buf), " %ld.%06d" , stats.ru_stime .tv_sec , stats.ru_stime .tv_usec );
1034
1044
out << " \n Kernel mode CPU: " << buf << " secs" ;
1045
+ cpu_abs += std::stod (buf);
1035
1046
#else
1036
1047
snprintf ( buf, sizeof (buf), " %ld.%06ld" , stats.ru_utime .tv_sec , stats.ru_utime .tv_usec );
1037
1048
out << " \n User mode CPU: " << buf << " secs" ;
1049
+ cpu_abs = std::stod (buf);
1038
1050
snprintf ( buf, sizeof (buf), " %ld.%06ld" , stats.ru_stime .tv_sec , stats.ru_stime .tv_usec );
1039
1051
out << " \n Kernel mode CPU: " << buf << " secs" ;
1052
+ cpu_abs += std::stod (buf);
1040
1053
#endif
1054
+ cpu_percentage = (cpu_abs / diff_time) * 100 ;
1055
+ out << " \n Average Consumption : " << cpu_percentage << " %" ;
1041
1056
out << " \n Maximum resident set size: " ;
1042
1057
WriteInteger (out, stats.ru_maxrss * 1024 );
1043
1058
out << " bytes\n Page faults: " << stats.ru_majflt << " (I/O required) "
@@ -1051,14 +1066,20 @@ static void PrintResourceUsage(std::ostream& out) {
1051
1066
#if defined(__APPLE__) || defined(_AIX)
1052
1067
snprintf ( buf, sizeof (buf), " %ld.%06d" , stats.ru_utime .tv_sec , stats.ru_utime .tv_usec );
1053
1068
out << " \n User mode CPU: " << buf << " secs" ;
1069
+ cpu_abs = std::stod (buf);
1054
1070
snprintf ( buf, sizeof (buf), " %ld.%06d" , stats.ru_stime .tv_sec , stats.ru_stime .tv_usec );
1055
1071
out << " \n Kernel mode CPU: " << buf << " secs" ;
1072
+ cpu_abs += std::stod (buf);
1056
1073
#else
1057
1074
snprintf ( buf, sizeof (buf), " %ld.%06ld" , stats.ru_utime .tv_sec , stats.ru_utime .tv_usec );
1058
1075
out << " \n User mode CPU: " << buf << " secs" ;
1076
+ cpu_abs = std::stod (buf);
1059
1077
snprintf ( buf, sizeof (buf), " %ld.%06ld" , stats.ru_stime .tv_sec , stats.ru_stime .tv_usec );
1060
1078
out << " \n Kernel mode CPU: " << buf << " secs" ;
1079
+ cpu_abs += std::stod (buf);
1061
1080
#endif
1081
+ cpu_percentage = (cpu_abs / diff_time) * 100 ;
1082
+ out << " \n Average Consumption : " << cpu_percentage << " %" ;
1062
1083
out << " \n Filesystem activity: " << stats.ru_inblock << " reads "
1063
1084
<< stats.ru_oublock << " writes" ;
1064
1085
}
0 commit comments