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
00067 #ifndef LOAD_MONITOR_INCLUDED
00068 #define LOAD_MONITOR_INCLUDED
00069
00070 #include <string>
00071 #include <utility>
00072 #include "../common/LoadSample.h"
00073 #include "../common/HardwareDescription.h"
00074
00075
00076
00077
00078 #define LOG_ENABLED 1
00079
00080 #include <string>
00081 #include <vector>
00082 #include "../common/LoadSample.h"
00083 #include "../common/HardwareDescription.h"
00084
00089 namespace lm
00090 {
00097 class LoadMonitor
00098 {
00099 public:
00101 LoadMonitor(void);
00102
00104 ~LoadMonitor(void);
00105
00107 bool Initialize(void);
00108
00110 bool Terminate(void);
00111
00113 LoadSample TakeSample(void);
00114
00116 HardwareDescription GetHardwareDescription(void);
00117
00118 private:
00119
00121 LoadMonitor& operator =(LoadMonitor &) { return *this; }
00123 LoadMonitor(const LoadMonitor &) { }
00124
00127 class StatsProvider
00128 {
00129 public:
00131 StatsProvider(void);
00132
00134 virtual ~StatsProvider(void);
00135
00142 virtual bool QueryData(LoadSample &target) = 0;
00143
00150 virtual bool QueryData(HardwareDescription &target) = 0;
00151
00152 protected:
00154 bool ReadLines(const char *file_name, std::vector< std::string > &target) const;
00155
00157 template< typename T >
00158 bool GetFieldValue(const std::string &line, const size_t index,
00159 T &result, const size_t start_at = 0, const char *delim = " \t") const;
00160
00162 std::pair< std::string, size_t > GetFieldName(const std::string &line,
00163 const std::string &end = ":", const size_t start_at = 0) const;
00164
00166 bool BeginsWith(const std::string &str, const std::string &what) const;
00167
00169 static int GetKernelVersionCode(void);
00170
00171 private:
00173 StatsProvider(const StatsProvider &) { }
00174 };
00175
00178 class MemoryStats : public StatsProvider
00179 {
00180 public:
00182 MemoryStats(void);
00183
00185 virtual ~MemoryStats(void);
00186
00188 virtual bool QueryData(LoadSample &target);
00189
00191 virtual bool QueryData(HardwareDescription &target);
00192 };
00193
00196 class ProcessorStats : public StatsProvider
00197 {
00198 public:
00200 ProcessorStats(void);
00201
00203 virtual ~ProcessorStats(void);
00204
00206 virtual bool QueryData(LoadSample &target);
00207
00209 virtual bool QueryData(HardwareDescription &target);
00210
00211 private:
00212
00230 class OneCpuStats
00231 {
00232 public:
00234 OneCpuStats(void) { }
00235
00237 OneCpuStats(const std::string &line);
00238
00240 OneCpuStats operator -(const OneCpuStats &b) const;
00241
00243 long long Sum(void) const;
00244
00246 static const size_t CPU_IDLE_COLUMN = 3;
00247
00249 std::vector< long long > stat_cache;
00250 };
00251
00253 std::vector< OneCpuStats > cpus;
00254 };
00255
00258 class NetworkStats : public StatsProvider
00259 {
00260 public:
00262 NetworkStats(void);
00263
00265 virtual ~NetworkStats(void);
00266
00268 virtual bool QueryData(LoadSample &target);
00269
00271 virtual bool QueryData(HardwareDescription &target);
00272
00273 private:
00278 class OneInterfaceStats
00279 {
00280 public:
00282 OneInterfaceStats(const std::string &line);
00283
00285 static const size_t RECV_FIELDS = 8;
00286
00288 static const size_t SEND_FIELDS = 8;
00289
00291 static const size_t INTERFACE_NAME_LENGTH = 16;
00292
00294 char name[INTERFACE_NAME_LENGTH];
00295
00297 long long recv[RECV_FIELDS];
00298
00300 long long send[SEND_FIELDS];
00301
00303 static const size_t RECV_BYTES = 0;
00304
00306 static const size_t RECV_PACKETS = 1;
00307
00309 static const size_t RECV_ERRORS = 2;
00310
00312 static const size_t RECV_DROPS = 3;
00313
00315 static const size_t RECV_FIFO = 4;
00316
00318 static const size_t RECV_FRAME = 5;
00319
00321 static const size_t RECV_COMPRESSED = 6;
00322
00324 static const size_t RECV_MULTICAST = 7;
00325
00327 static const size_t SEND_BYTES = 0;
00328
00330 static const size_t SEND_PACKETS = 1;
00331
00333 static const size_t SEND_ERRORS = 2;
00334
00336 static const size_t SEND_DROPS = 3;
00337
00339 static const size_t SEND_FIFO = 4;
00340
00342 static const size_t SEND_COLLISION = 5;
00343
00345 static const size_t SEND_CARRIER = 6;
00346
00348 static const size_t SEND_COMPRESSED = 7;
00349 };
00350
00352 unsigned long long last_time;
00353
00355 std::vector< std::pair< long long, long long > > data;
00356 };
00357
00360 class DriveStats : public StatsProvider
00361 {
00362 public:
00364 DriveStats(void);
00365
00367 virtual ~DriveStats(void);
00368
00370 virtual bool QueryData(LoadSample &target);
00371
00373 virtual bool QueryData(HardwareDescription &target);
00374
00375 private:
00383 class OneDriveStats
00384 {
00385 public:
00386 OneDriveStats(const std::string &line);
00387
00389 bool IsDriveDevice(void) const;
00390
00392 bool IsIDE(void) const;
00393
00395 bool IsSCSI(void) const;
00396
00398 int major;
00399
00401 int minor;
00402
00404 static const size_t DRIVE_NAME_LENGTH = 16;
00405
00407 char name[DRIVE_NAME_LENGTH];
00408
00410 static const size_t COLUMN_COUNT = 11;
00411
00413 unsigned long stat_fields[COLUMN_COUNT];
00414
00416 static const long SECTOR_SIZE = 512;
00417
00419 static const size_t READS_ISSUED_COUNT = 0;
00420
00422 static const size_t READS_MERGED = 1;
00423
00425 static const size_t READ_SECTORS = 2;
00426
00428 static const size_t READ_MILLISECONDS = 3;
00429
00431 static const size_t WRITES_COMPLETED = 4;
00432
00434 static const size_t WRITES_MERGED = 5;
00435
00437 static const size_t WRITE_SECTORS = 6;
00438
00440 static const size_t WRITE_MILLISECONDS = 7;
00441
00443 static const size_t IOS_IN_PROGRESS = 8;
00444
00446 static const size_t IO_MILLISECONDS = 9;
00447
00449 static const size_t IO_WEIGHTED_MILLISECONDS = 10;
00450 };
00451
00453 int kernel_version;
00454
00460 std::vector< std::pair< long, long > > data;
00461
00463 unsigned long long last_time;
00464 };
00465
00468 class SystemStats : public StatsProvider
00469 {
00470 public:
00472 SystemStats(void);
00473
00475 virtual ~SystemStats(void);
00476
00478 virtual bool QueryData(LoadSample &target);
00479
00481 virtual bool QueryData(HardwareDescription &);
00482 };
00483
00485 MemoryStats memory;
00486
00488 ProcessorStats processor;
00489
00491 NetworkStats network;
00492
00494 SystemStats system;
00495
00497 DriveStats drive;
00498
00500 void LogInfo(const char *message) const;
00501
00503 void LogError(const char *message) const;
00504
00506 static unsigned long long GetSampleTime();
00507
00512 unsigned long long GetTSC(void) const
00513 {
00514 unsigned long long result = 0;
00515
00516 asm volatile
00517 (
00518 "rdtsc"
00519 : "=A"(result)
00520 );
00521
00522 return result;
00523 }
00524 };
00525
00526 }
00527
00528 #endif