diff options
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/SystemBackendProc/SystemBackendProc.cpp | 75 | ||||
-rw-r--r-- | src/modules/SystemBackendProc/SystemBackendProc.h | 16 |
2 files changed, 55 insertions, 36 deletions
diff --git a/src/modules/SystemBackendProc/SystemBackendProc.cpp b/src/modules/SystemBackendProc/SystemBackendProc.cpp index d238e07..1888227 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.cpp +++ b/src/modules/SystemBackendProc/SystemBackendProc.cpp @@ -24,15 +24,13 @@ #include <cstdio> #include <cstring> -#include <boost/bind.hpp> - namespace Mad { namespace Modules { -SystemBackendProc *SystemBackendProc::backend = 0; +boost::shared_ptr<SystemBackendProc> SystemBackendProc::backend; -bool SystemBackendProc::getUptimeInfo(const boost::function2<void, unsigned long, unsigned long> &callback) { - unsigned long uptime = 0, idleTime = 0; +bool SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) { + Net::ThreadManager::get()->detach(); uptimeFile.seekg(0, std::ios::beg); @@ -44,19 +42,27 @@ bool SystemBackendProc::getUptimeInfo(const boost::function2<void, unsigned long if(!uptimeFile.good()) return false; - uptime = (unsigned long)f; + if(uptime) + *uptime = (unsigned long)f; uptimeFile >> f; - if(uptimeFile.good()) - idleTime = (unsigned long)f; - - Net::ThreadManager::get()->pushWork(boost::bind(callback, uptime, idleTime)); + if(uptimeFile.good() && idleTime) + *idleTime = (unsigned long)f; return true; } -bool SystemBackendProc::getMemoryInfo(const boost::function4<void, unsigned long, unsigned long, unsigned long, unsigned long> &callback) { - unsigned long totalMem = 0, freeMem = 0, totalSwap = 0, freeSwap = 0; +bool SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) { + Net::ThreadManager::get()->detach(); + + if(totalMem) + *totalMem = 0; + if(freeMem) + *freeMem = 0; + if(totalSwap) + *totalSwap = 0; + if(freeSwap) + *freeSwap = 0; meminfoFile.seekg(0, std::ios::beg); @@ -71,30 +77,30 @@ bool SystemBackendProc::getMemoryInfo(const boost::function4<void, unsigned long unsigned long value; if(std::sscanf(line.c_str(), "%s %lu", name, &value) == 2) { - if(std::strcmp(name, "MemTotal:") == 0) - totalMem = value; - else if(std::strcmp(name, "MemFree:") == 0) - freeMem = value; - else if(std::strcmp(name, "SwapTotal:") == 0) - totalSwap = value; - else if(std::strcmp(name, "SwapFree:") == 0) - freeSwap = value; + if(std::strcmp(name, "MemTotal:") == 0 && totalMem) + *totalMem = value; + else if(std::strcmp(name, "MemFree:") == 0 && freeMem) + *freeMem = value; + else if(std::strcmp(name, "SwapTotal:") == 0 && totalSwap) + *totalSwap = value; + else if(std::strcmp(name, "SwapFree:" ) == 0 && freeSwap) + *freeSwap = value; } delete [] name; - if(totalMem && freeMem && totalSwap && freeSwap) + if((!totalMem || *totalMem) && (!freeMem || *freeMem) && (!totalSwap || *totalSwap) && (!freeSwap || *freeSwap)) break; } - Net::ThreadManager::get()->pushWork(boost::bind(callback, totalMem, freeMem, totalSwap, freeSwap)); - return true; } -bool SystemBackendProc::getLoadInfo(const boost::function5<void, unsigned long, unsigned long, float, float, float> &callback) { - unsigned long currentLoad = 0, nProcesses = 0; - float loadAvg1 = 0, loadAvg5 = 0, loadAvg15 = 0; +bool SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) { + Net::ThreadManager::get()->detach(); + + unsigned long currentLoadValue = 0, nProcessesValue = 0; + float loadAvg1Value = 0, loadAvg5Value = 0, loadAvg15Value = 0; loadFile.seekg(0, std::ios::beg); @@ -107,9 +113,22 @@ bool SystemBackendProc::getLoadInfo(const boost::function5<void, unsigned long, if(line.empty()) return false; - std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1, &loadAvg5, &loadAvg15, ¤tLoad, &nProcesses); + std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1Value, &loadAvg5Value, &loadAvg15Value, ¤tLoadValue, &nProcessesValue); + + if(currentLoad) + *currentLoad = currentLoadValue; + + if(nProcesses) + *nProcesses = nProcessesValue; + + if(loadAvg1) + *loadAvg1 = loadAvg1Value; + + if(loadAvg5) + *loadAvg5 = loadAvg5Value; - Net::ThreadManager::get()->pushWork(boost::bind(callback, currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)); + if(loadAvg15) + *loadAvg15 = loadAvg15Value; return true; } diff --git a/src/modules/SystemBackendProc/SystemBackendProc.h b/src/modules/SystemBackendProc/SystemBackendProc.h index bf19e22..9b36df4 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.h +++ b/src/modules/SystemBackendProc/SystemBackendProc.h @@ -23,13 +23,14 @@ #include <Common/SystemBackend.h> #include <fstream> +#include <boost/noncopyable.hpp> namespace Mad { namespace Modules { -class SystemBackendProc : private Common::SystemBackend { +class SystemBackendProc : public Common::SystemBackend, boost::noncopyable { private: - static SystemBackendProc *backend; + static boost::shared_ptr<SystemBackendProc> backend; std::ifstream uptimeFile; std::ifstream meminfoFile; @@ -38,16 +39,16 @@ class SystemBackendProc : private Common::SystemBackend { SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} protected: - virtual bool getUptimeInfo(const boost::function2<void, unsigned long, unsigned long> &callback); - virtual bool getMemoryInfo(const boost::function4<void, unsigned long, unsigned long, unsigned long, unsigned long> &callback); - virtual bool getLoadInfo(const boost::function5<void, unsigned long, unsigned long, float, float, float> &callback); + virtual bool getUptimeInfo(unsigned long *uptime, unsigned long *idleTime); + virtual bool getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap); + virtual bool getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15); public: static void registerBackend() { if(backend) return; - backend = new SystemBackendProc(); + backend.reset(new SystemBackendProc()); Common::SystemManager::get()->registerBackend(backend); } @@ -56,8 +57,7 @@ class SystemBackendProc : private Common::SystemBackend { return; Common::SystemManager::get()->unregisterBackend(backend); - delete backend; - backend = 0; + backend.reset(); } }; |