libGizmod/CPUUsage.hpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
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 // Namespace
00045 
00046 namespace Gizmod {
00047 
00049 // Typedefs / enums
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 // CPUUsageInfo Class Definition
00071 
00076 class CPUUsageInfo {
00077 friend class CPUUsage;
00078 public:
00079         // public member variables
00080         double                          Field[CPUUSAGE_MAX];            
00081         double                          Stat[CPUUSAGE_MAX];             
00082         double                          Usage;                          
00083         double                          Average;                        
00084         
00085         // construction / deconstruction
00086         CPUUsageInfo();                                                 
00087         virtual ~CPUUsageInfo();                                        
00088         
00089 private:
00090         // private member variables
00091         H::Average                      mAverager;                      
00092         
00093 private:
00094         // serialization
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 // CPUUsage Class Definition
00108 
00113 class CPUUsage {
00114 public:
00115         // public functions
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         // construction / deconstruction
00123         CPUUsage();                                                     
00124         virtual ~CPUUsage();                                            
00125 
00126 private:
00127         // private functions
00128         void                            threadProc();                   
00129         void                            updateUsageStats();             
00130         
00131         // private member variables
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 } // Gizmod namespace
00160 
00161 #endif // __CPUUsage_h

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3