00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00066 #ifndef LOAD_MONITOR_INCLUDED
00067 #define LOAD_MONITOR_INCLUDED
00068
00069 #include <fstream>
00070 #include <iostream>
00071 #include <windows.h>
00072 #include <wbemidl.h>
00073 #include <comdef.h>
00074 #include "../common/String.h"
00075 #include "../common/LoadSample.h"
00076 #include "../common/HardwareDescription.h"
00077
00078
00079
00080
00081
00082
00083
00084
00085 #define SERIALIZE_TSC
00086
00087
00088
00089
00090 #define LOG_ENABLED 1
00091
00092
00097 namespace lm
00098 {
00099
00105 class LoadMonitor
00106 {
00107 public:
00108
00110 LoadMonitor(void);
00111
00113 ~LoadMonitor(void);
00114
00116 bool Initialize(void);
00117
00119 bool Terminate(void);
00120
00122 LoadSample TakeSample(void);
00123
00125 HardwareDescription GetHardwareDescription(void);
00126
00127 private:
00128
00130 LoadMonitor& operator =(LoadMonitor &) { return *this; }
00131
00133 IWbemServices *services;
00134
00136 IWbemRefresher *refresher;
00137
00143 class PerformanceCounterEnumCache
00144 {
00145 public:
00148 PerformanceCounterEnumCache(void) :
00149 is_cached(false)
00150 {
00151 }
00152
00157 virtual ~PerformanceCounterEnumCache(void)
00158 {
00159 }
00160
00168 virtual bool AddToRefresher(IWbemConfigureRefresher *configure, IWbemServices *services) = 0;
00169
00174 virtual void Release(void) = 0;
00175
00185 virtual bool QueryData(LoadSample &target) = 0;
00186
00188 HRESULT GetEnumObjects(IWbemHiPerfEnum *wbem_enum, size_t *object_count, IWbemObjectAccess ***access_objects);
00189
00191 void ClearEnumObjects(IWbemObjectAccess **access_objects, size_t object_count);
00192
00193 protected:
00194
00196 bool is_cached;
00197
00207 bool Cache(IWbemObjectAccess *access_object)
00208 {
00209 if (!access_object)
00210 {
00211 return false;
00212 }
00213
00214 if (!is_cached)
00215 {
00216 is_cached = CacheImpl(access_object);
00217 }
00218
00219 return is_cached;
00220 }
00221
00223 virtual bool CacheImpl(IWbemObjectAccess *access_object) = 0;
00224 };
00225
00228 class ProcessorEnumCache : public PerformanceCounterEnumCache
00229 {
00230 public:
00232 ProcessorEnumCache(void);
00233
00235 virtual ~ProcessorEnumCache(void);
00236
00238 virtual bool AddToRefresher(IWbemConfigureRefresher *configure, IWbemServices *services);
00239
00241 virtual void Release(void);
00242
00244 virtual bool QueryData(LoadSample &target);
00245
00247 size_t GetCount();
00248
00249 private:
00251 IWbemHiPerfEnum *enum_processor_usage;
00252
00254 long enum_processor_usage_id;
00255
00257 long id_processor_time;
00258
00260 long id_processor_name;
00261
00262 protected:
00264 virtual bool CacheImpl(IWbemObjectAccess *access_object);
00265 };
00266
00269 class NetworkEnumCache : public PerformanceCounterEnumCache
00270 {
00271 public:
00273 NetworkEnumCache(void);
00274
00276 virtual ~NetworkEnumCache(void);
00277
00279 virtual bool AddToRefresher(IWbemConfigureRefresher *configure, IWbemServices *services);
00280
00282 virtual void Release(void);
00283
00285 virtual bool QueryData(LoadSample &target);
00286
00288 bool QueryNames(HardwareDescription &target);
00289
00290 private:
00292 IWbemHiPerfEnum *enum_network;
00293
00295 long enum_network_id;
00296
00298 long id_bytes_received;
00299
00301 long id_bytes_sent;
00302
00304 long id_name;
00305
00306 protected:
00308 virtual bool CacheImpl(IWbemObjectAccess *access_object);
00309 };
00310
00313 class MemoryEnumCache : public PerformanceCounterEnumCache
00314 {
00315 public:
00317 MemoryEnumCache(void);
00318
00320 virtual ~MemoryEnumCache(void);
00321
00323 virtual bool AddToRefresher(IWbemConfigureRefresher *configure, IWbemServices *services);
00324
00326 virtual void Release(void);
00327
00329 virtual bool QueryData(LoadSample &target);
00330
00332 unsigned long long GetSize();
00333
00334 private:
00336 IWbemHiPerfEnum *enum_memory_usage;
00337
00339 long enum_memory_usage_id;
00340
00342 long id_available;
00343
00344 protected:
00346 virtual bool CacheImpl(IWbemObjectAccess *access_object);
00347 };
00348
00351 class SystemEnumCache : public PerformanceCounterEnumCache
00352 {
00353 public:
00355 SystemEnumCache(void);
00356
00358 virtual ~SystemEnumCache(void);
00359
00361 virtual bool AddToRefresher(IWbemConfigureRefresher *configure, IWbemServices *services);
00362
00364 virtual void Release(void);
00365
00367 virtual bool QueryData(LoadSample &target);
00368
00369 private:
00371 IWbemHiPerfEnum *enum_system;
00372
00374 long enum_system_id;
00375
00377 long id_processes;
00378
00380 long id_processor_queue;
00381
00382 protected:
00384 virtual bool CacheImpl(IWbemObjectAccess *access_object);
00385 };
00386
00389 class DriveEnumCache : public PerformanceCounterEnumCache
00390 {
00391 public:
00393 DriveEnumCache(void);
00394
00396 virtual ~DriveEnumCache(void);
00397
00399 virtual bool AddToRefresher(IWbemConfigureRefresher *configure, IWbemServices *services);
00400
00402 virtual void Release(void);
00403
00405 virtual bool QueryData(LoadSample &target);
00406
00408 bool QueryNames(HardwareDescription &target);
00409
00410 private:
00412 IWbemHiPerfEnum *enum_drives;
00413
00415 long enum_drives_id;
00416
00418 long id_disk_write_bytes;
00419
00421 long id_disk_read_bytes;
00422
00424 long id_disk_name;
00425
00426 protected:
00428 virtual bool CacheImpl(IWbemObjectAccess *access_object);
00429 };
00430
00432 ProcessorEnumCache processor;
00433
00435 MemoryEnumCache memory;
00436
00438 NetworkEnumCache network;
00439
00441 DriveEnumCache drive;
00442
00444 SystemEnumCache system;
00445
00447 LoadMonitor(const LoadMonitor &) { }
00448
00450 void LogInfo(const char *message) const;
00451
00453 void LogError(const char *message) const;
00454
00456 unsigned long long GetSampleTime() const;
00457
00462 unsigned long long GetTSC(void) const
00463 {
00464 __asm
00465 {
00466 #ifdef SERIALIZE_TSC
00467 mov eax, 0
00468 cpuid
00469 #endif
00470 rdtsc
00471 }
00472 }
00473 };
00474 }
00475
00476 #endif