| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | // permet d'obtenir des informations sur les ressources utilisees | 
|---|
| 3 | //                                R. Ansari - Juillet 2002 | 
|---|
| 4 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| 5 |  | 
|---|
| 6 | #ifndef RESUSAGE_H_SEEN | 
|---|
| 7 | #define RESUSAGE_H_SEEN | 
|---|
| 8 |  | 
|---|
| 9 | #include "machdefs.h" | 
|---|
| 10 | #include <iostream> | 
|---|
| 11 |  | 
|---|
| 12 | namespace SOPHYA { | 
|---|
| 13 |  | 
|---|
| 14 | //! Acces to information about resource usage (memory, CPU, ...) | 
|---|
| 15 |  | 
|---|
| 16 | class ResourceUsage | 
|---|
| 17 | { | 
|---|
| 18 | public: | 
|---|
| 19 | /*! | 
|---|
| 20 | - RU_Self : Resource usage for the process itself | 
|---|
| 21 | - RU_Children : Resource usage for the child processes | 
|---|
| 22 | - RU_All : resource usage for the process itself and its child processes | 
|---|
| 23 | */ | 
|---|
| 24 | enum RU_ProcGrp {RU_Self=0, RU_Children=1, RU_All=2}; | 
|---|
| 25 |  | 
|---|
| 26 | ResourceUsage(RU_ProcGrp pg=RU_Self); | 
|---|
| 27 | ~ResourceUsage(); | 
|---|
| 28 |  | 
|---|
| 29 | int Update(); | 
|---|
| 30 |  | 
|---|
| 31 | // Process Id | 
|---|
| 32 | //! Returns the process id. | 
|---|
| 33 | inline uint_8 getProcessId() { return cur_pid; } | 
|---|
| 34 |  | 
|---|
| 35 | // Memory usage in kilo-bytes | 
|---|
| 36 | // NOTE: getDataSize() getStackSize() retournent des valeurs bizarres | 
|---|
| 37 | //! Returns the current memory (Resident) size (in kilo-bytes) | 
|---|
| 38 | inline uint_8 getMemorySize() { return cur_rss; } | 
|---|
| 39 | //! Returns the maximum allowed memory size: Min( \c MaxResidentSize , \c MaxDataSize )) | 
|---|
| 40 | inline uint_8 getMaxMemorySize() | 
|---|
| 41 | { return((getMaxResidentSize()<getMaxDataSize()) ? | 
|---|
| 42 | getMaxResidentSize() :getMaxDataSize() ); } | 
|---|
| 43 | //! Returns the increase in memory usage since the previous call to \b Update() | 
|---|
| 44 | inline uint_8 getDeltaMemorySize() { return delta_rss; } | 
|---|
| 45 |  | 
|---|
| 46 | //! Returns the maximum allowd data segment size (in kilo-bytes) | 
|---|
| 47 | inline uint_8 getMaxDataSize() { return((max_datasz>0)?max_datasz:1024); } | 
|---|
| 48 | //! Returns the current data segment size (in kilo-bytes) | 
|---|
| 49 | inline uint_8 getDataSize() { return cur_datasz; } | 
|---|
| 50 | //! Returns the maximum allowed resident size (in kilo-bytes) | 
|---|
| 51 | inline uint_8 getMaxResidentSize() { return((max_rss>0)?max_rss:1024); } | 
|---|
| 52 | //! Returns the current resident memory size (in kilo-bytes) | 
|---|
| 53 | inline uint_8 getResidentSize() { return cur_rss; } | 
|---|
| 54 | //! Returns the maximum allowed stack size (in kilo-bytes) | 
|---|
| 55 | inline uint_8 getMaxStackSize() { return max_stack; } | 
|---|
| 56 | //! Returns the current stack size (in kilo-bytes) | 
|---|
| 57 | inline uint_8 getStackSize() { return cur_stack; } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | // Time in milli-second | 
|---|
| 61 | //! Returns the total CPU time used (in milli-second) | 
|---|
| 62 | inline uint_8 getCPUTime() { return cur_tottm; } | 
|---|
| 63 | //! Returns the total elapsed time (in milli-second) | 
|---|
| 64 | inline uint_8 getElapsedTime() { return elapsed_time; } | 
|---|
| 65 | //! Returns the average load ( CPU time / elapsed time) | 
|---|
| 66 | inline double  getAverageCPULoad() | 
|---|
| 67 | { return ((elapsed_time>1) ? (double)cur_tottm/(double)elapsed_time: 1.);} | 
|---|
| 68 |  | 
|---|
| 69 | //! Returns the CPU time used since the previous call to \b Update() (in milli-second) | 
|---|
| 70 | inline uint_8 getDeltaCPUTime() { return delta_tottm; } | 
|---|
| 71 | //! Returns the elapsed time since the previous call to \b Update() (in milli-second) | 
|---|
| 72 | inline uint_8 getDeltaElapsedTime() { return delta_elapsed_time; } | 
|---|
| 73 | //! Returns the CPU load since the previous call to \b Update() | 
|---|
| 74 | inline double  getCPULoad() | 
|---|
| 75 | { return ((delta_elapsed_time>1) ? (double)delta_tottm/(double)delta_elapsed_time: 1.);} | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | //! Returns the total CPU time (in milli-second) | 
|---|
| 79 | inline uint_8 getTotalCPUTime() { return cur_tottm; } | 
|---|
| 80 | //! Returns the CPU time in user mode (in milli-second) | 
|---|
| 81 | inline uint_8 getUserCPUTime() { return cur_usrtm; } | 
|---|
| 82 | //! Returns the CPU time in system mode (in milli-second) | 
|---|
| 83 | inline uint_8 getSysCPUTime() { return cur_systm; } | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 | void  Print(ostream& os, int lp=0, bool upd=true); | 
|---|
| 87 | //! Alias for the \b Print() method. | 
|---|
| 88 | inline void print(ostream& os, int lp=0, bool upd=true){ Print(os,lp,upd); } | 
|---|
| 89 |  | 
|---|
| 90 | void ReadLinuxMem();    // methode de lecture des infos memoire ss Linux - OPerdereau | 
|---|
| 91 | void ReadLinuxTotMem(); // methode de lecture des infos memoire ss Linux - OPerdereau | 
|---|
| 92 |  | 
|---|
| 93 | protected: | 
|---|
| 94 | RU_ProcGrp procgrp; // Process group: self, child, all | 
|---|
| 95 | uint_8 max_datasz;  // Max data segment size (KBytes) | 
|---|
| 96 | uint_8 max_rss;     // Max resident size | 
|---|
| 97 | uint_8 max_stack;   // Max stack size | 
|---|
| 98 |  | 
|---|
| 99 | uint_8 cur_datasz;  // Current data segment size | 
|---|
| 100 | uint_8 cur_rss;     // Current resident size | 
|---|
| 101 | uint_8 cur_stack;   // Current stack size | 
|---|
| 102 |  | 
|---|
| 103 | uint_8 cur_tottm;   // Current total CPU-Time - in milli-second | 
|---|
| 104 | uint_8 cur_usrtm;   // Current user CPU-Time - in milli-second | 
|---|
| 105 | uint_8 cur_systm;   // Current system CPU-Time - in milli-second | 
|---|
| 106 | uint_8 elapsed_time; // Elapsed time - in milli-second | 
|---|
| 107 | uint_8 t0_time;      // time T0 in seconds | 
|---|
| 108 |  | 
|---|
| 109 | // Delta_values : difference since last call to Update() | 
|---|
| 110 | uint_8 delta_rss;     // Max resident size | 
|---|
| 111 | uint_8 delta_tottm;   // Delta total CPU-Time - in milli-second | 
|---|
| 112 |  | 
|---|
| 113 | uint_8 delta_elapsed_time; // Elapsed time - in milli-second | 
|---|
| 114 | uint_8 cur_pid ;          // process PID | 
|---|
| 115 | }; | 
|---|
| 116 |  | 
|---|
| 117 | //! Prints the resource usage information on the output stream | 
|---|
| 118 | inline ostream& operator << (ostream& os, ResourceUsage& ru) | 
|---|
| 119 | { ru.Print(os,0,true); return(os); } | 
|---|
| 120 |  | 
|---|
| 121 | } // namespace SOPHYA | 
|---|
| 122 |  | 
|---|
| 123 | #endif | 
|---|
| 124 |  | 
|---|