@@ -797,6 +797,7 @@ static void PrintResourceUsage(std::ostream& out) {
797797 }
798798#ifdef RUSAGE_THREAD
799799 out << " \n\n Event loop thread resource usage:" ;
800+ memset (&stats, 0 , sizeof (stats));
800801 if (getrusage (RUSAGE_THREAD, &stats) == 0 ) {
801802#if defined(__APPLE__) || defined(_AIX)
802803 snprintf ( buf, sizeof (buf), " %ld.%06d" , stats.ru_utime .tv_sec , stats.ru_utime .tv_usec );
@@ -809,13 +810,32 @@ static void PrintResourceUsage(std::ostream& out) {
809810 snprintf ( buf, sizeof (buf), " %ld.%06ld" , stats.ru_stime .tv_sec , stats.ru_stime .tv_usec );
810811 out << " \n Kernel mode CPU: " << buf << " secs" ;
811812#endif
812- 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 ;
813+ 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 ;
813814 cpu_percentage = (cpu_abs / uptime) * 100.0 ;
814815 out << " \n Average CPU Consumption : " << cpu_percentage << " %" ;
815816 out << " \n Filesystem activity: " << stats.ru_inblock << " reads "
816817 << stats.ru_oublock << " writes" ;
817818 }
818- #endif
819+ #elif defined(__APPLE__)
820+ // Currently RUSAGE_THREAD is not currently supported on Mac.
821+ mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
822+ mach_port_t thread = pthread_mach_thread_np (pthread_self ());
823+ thread_basic_info thr_info;
824+
825+ kern_return_t rc = thread_info (thread, THREAD_BASIC_INFO, (thread_info_t ) &thr_info, &count);
826+
827+ if (rc == KERN_SUCCESS) {
828+ out << " \n\n Event loop thread resource usage:" ;
829+ snprintf ( buf, sizeof (buf), " %d.%06d" , thr_info.user_time .seconds , thr_info.user_time .microseconds );
830+ out << " \n User mode CPU: " << buf << " secs" ;
831+ snprintf ( buf, sizeof (buf), " %d.%06d" , thr_info.system_time .seconds , thr_info.system_time .microseconds );
832+ out << " \n Kernel mode CPU: " << buf << " secs" ;
833+ cpu_abs = thr_info.user_time .seconds + 0.000001 * thr_info.user_time .microseconds
834+ + thr_info.system_time .seconds + 0.000001 * thr_info.system_time .microseconds ;
835+ cpu_percentage = (cpu_abs / uptime) * 100.0 ;
836+ out << " \n Average CPU Consumption : " << cpu_percentage << " %" ;
837+ }
838+ #endif // RUSAGE_THREAD
819839 out << std::endl;
820840}
821841#endif
0 commit comments