diff options
-rw-r--r-- | include/inspircd.h | 8 | ||||
-rw-r--r-- | src/cmd_stats.cpp | 11 | ||||
-rw-r--r-- | src/inspircd.cpp | 8 |
3 files changed, 27 insertions, 0 deletions
diff --git a/include/inspircd.h b/include/inspircd.h index 64489aaf6..f2ccc081a 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -119,6 +119,14 @@ class serverstats : public classbase */ unsigned long BoundPortCount; + /** Cpu usage at last sample + */ + timeval LastCPU; + + /** Time last sample was read + */ + timeval LastSampled; + /** The constructor initializes all the counts to zero */ serverstats() diff --git a/src/cmd_stats.cpp b/src/cmd_stats.cpp index 70cbcf2ae..751f9ac68 100644 --- a/src/cmd_stats.cpp +++ b/src/cmd_stats.cpp @@ -184,6 +184,17 @@ void DoStats(InspIRCd* ServerInstance, char statschar, userrec* user, string_lis results.push_back(sn+" 249 "+user->nick+" :Page faults: "+ConvToStr(R.ru_majflt)); results.push_back(sn+" 249 "+user->nick+" :Swaps: "+ConvToStr(R.ru_nswap)); results.push_back(sn+" 249 "+user->nick+" :Context Switches: "+ConvToStr(R.ru_nvcsw+R.ru_nivcsw)); + + timeval tv; + char percent[30]; + gettimeofday(&tv, NULL); + + float n_elapsed = ((tv.tv_sec - ServerInstance->stats->LastSampled.tv_sec) * 1000000 + tv.tv_usec - ServerInstance->stats->LastSampled.tv_usec); + float n_eaten = ((R.ru_utime.tv_sec - ServerInstance->stats->LastCPU.tv_sec) * 1000000 + R.ru_utime.tv_usec - ServerInstance->stats->LastCPU.tv_usec); + float per = (n_eaten / n_elapsed) * 100; + + snprintf(percent, 30, "%03.5f%%", per); + results.push_back(sn+" 249 "+user->nick+" :CPU Usage: "+percent); } } break; diff --git a/src/inspircd.cpp b/src/inspircd.cpp index aef7b09e6..a26b5e4c7 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -716,6 +716,8 @@ bool InspIRCd::LoadModule(const char* filename) void InspIRCd::DoOneIteration(bool process_module_sockets) { + static rusage ru; + /* time() seems to be a pretty expensive syscall, so avoid calling it too much. * Once per loop iteration is pleanty. */ @@ -744,6 +746,12 @@ void InspIRCd::DoOneIteration(bool process_module_sockets) FOREACH_MOD_I(this,I_OnBackgroundTimer,OnBackgroundTimer(TIME)); Timers->TickMissedTimers(TIME); } + + if (!getrusage(0, &ru)) + { + gettimeofday(&this->stats->LastSampled, NULL); + this->stats->LastCPU = ru.ru_utime; + } } /* Call the socket engine to wait on the active |