diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-14 19:30:42 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-14 19:30:42 +0000 |
commit | 4652f73b9e7a0f41ad4c91353e0ac39ef838f6cb (patch) | |
tree | a8774e8624dd302e9b01652501592b7e4715d820 | |
parent | 1a59b542f90c10799085ad2b2d8aed2a6c378acf (diff) |
CPU Usage percent in stats z! yay!
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@5988 e03df62e-2008-0410-955e-edbf42e46eb7
-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 |