00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __CPUUsage_h
00030 #define __CPUUsage_h
00031
00032 #if HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035
00036 #include "../libH/Average.hpp"
00037 #include <vector>
00038 #include <boost/shared_ptr.hpp>
00039 #include <boost/archive/text_oarchive.hpp>
00040 #include <boost/archive/text_iarchive.hpp>
00041
00043
00045
00046 namespace Gizmod {
00047
00049
00051
00056 typedef enum {
00057 CPUUSAGE_USER = 0,
00058 CPUUSAGE_NICE = 1,
00059 CPUUSAGE_SYSTEM = 2,
00060 CPUUSAGE_IDLE = 3,
00061 CPUUSAGE_IOWAIT = 4,
00062 CPUUSAGE_IRQ = 5,
00063 CPUUSAGE_SOFTIRQ = 6,
00064 CPUUSAGE_RESERVED = 7,
00065 CPUUSAGE_MAX
00066 } ProcStatField;
00067
00069
00071
00076 class CPUUsageInfo {
00077 friend class CPUUsage;
00078 public:
00079
00080 double Field[CPUUSAGE_MAX];
00081 double Stat[CPUUSAGE_MAX];
00082 double Usage;
00083 double Average;
00084
00085
00086 CPUUsageInfo();
00087 virtual ~CPUUsageInfo();
00088
00089 private:
00090
00091 H::Average mAverager;
00092
00093 private:
00094
00095 friend class boost::serialization::access;
00096 template<class Archive>
00097 void serialize(Archive & ar, const unsigned int version) {
00098 ar & Field;
00099 ar & Stat;
00100 ar & Usage;
00101 ar & Average;
00102 }
00103 };
00104
00106
00108
00113 class CPUUsage {
00114 public:
00115
00116 size_t getNumCPUs();
00117 void init();
00118 virtual void onCPUUsage(std::vector< boost::shared_ptr<CPUUsageInfo> > const & Event);
00119 void setTimeBetweenUpdates(float Seconds);
00120 void shutdown();
00121
00122
00123 CPUUsage();
00124 virtual ~CPUUsage();
00125
00126 private:
00127
00128 void threadProc();
00129 void updateUsageStats();
00130
00131
00132 float mSecsBetweenUpdates;
00133 bool mWatching;
00134 std::vector< boost::shared_ptr<CPUUsageInfo> > mCPUUsage;
00135
00139 struct CPUUsageThreadProc {
00140 CPUUsageThreadProc(CPUUsage * pCPUUsage) : mpCPUUsage(pCPUUsage) {
00141 mpCPUUsage->mThreading = false;
00142 };
00143
00145 void operator()() {
00146 mpCPUUsage->mThreading = true;
00147 mpCPUUsage->threadProc();
00148 mpCPUUsage->mThreading = false;
00149 }
00150
00151 CPUUsage * mpCPUUsage;
00152 };
00153 bool mThreading;
00154 CPUUsageThreadProc mThreadProc;
00155 };
00156
00158
00159 }
00160
00161 #endif // __CPUUsage_h