[2101] | 1 | #include "resusage.h"
|
---|
| 2 |
|
---|
| 3 | #include <sys/types.h>
|
---|
| 4 | #include <sys/time.h>
|
---|
| 5 | #include <sys/resource.h>
|
---|
| 6 | #include <unistd.h>
|
---|
| 7 | #include <time.h>
|
---|
| 8 |
|
---|
| 9 | /*!
|
---|
| 10 | \class SOPHYA::ResourceUsage
|
---|
| 11 | \ingroup SysTools
|
---|
| 12 | This class gives acces to various system resource usage
|
---|
| 13 | (CPU, memory, ...).
|
---|
| 14 | All returned values are in kilobytes for memory consumptions
|
---|
| 15 | and in milli-seconds fo CPU and elapsed times.
|
---|
| 16 | The information should be updated through the call to the
|
---|
| 17 | Update() method.
|
---|
[2212] | 18 | Note: The information is obtained through \c getrusage()
|
---|
| 19 | and \c getrlimit() system calls, and depending on the OS,
|
---|
| 20 | not all of the memory usage values are correctly filled.
|
---|
| 21 | \code
|
---|
| 22 | // How to check resource usage for a given part of the program
|
---|
| 23 | ResourceUsage res;
|
---|
| 24 | // --- Part of the program to be checked : Start
|
---|
| 25 | // ...
|
---|
| 26 | res.Update();
|
---|
| 27 | cout << " Memory size increase (KB):" << res.getDeltaMemorySize() << endl;
|
---|
| 28 | cout << " Resource usage info : \n" << res << endl;
|
---|
| 29 | \endcode
|
---|
[2101] | 30 | */
|
---|
[2212] | 31 | /*!
|
---|
| 32 | Constructor. the \b Update() method is called.
|
---|
| 33 | */
|
---|
[2101] | 34 | ResourceUsage::ResourceUsage()
|
---|
| 35 | {
|
---|
| 36 | struct rlimit rl;
|
---|
| 37 | getrlimit(RLIMIT_DATA, &rl);
|
---|
| 38 | max_datasz = rl.rlim_cur/1024;
|
---|
[2191] | 39 | #if defined(SunOS)
|
---|
| 40 | // Max resident size ne semble pas etre defini sur SunOS
|
---|
| 41 | max_rss = max_datasz;
|
---|
| 42 | #else
|
---|
[2101] | 43 | getrlimit(RLIMIT_RSS, &rl);
|
---|
| 44 | max_rss = rl.rlim_cur/1024;
|
---|
[2191] | 45 | #endif
|
---|
[2101] | 46 | getrlimit(RLIMIT_STACK, &rl);
|
---|
| 47 | max_stack = rl.rlim_cur/1024;
|
---|
| 48 |
|
---|
| 49 | cur_datasz = 0;
|
---|
| 50 | cur_stack = 0;
|
---|
| 51 | cur_rss = 0;
|
---|
| 52 | delta_rss = 0;
|
---|
| 53 | cur_tottm = 0;
|
---|
| 54 | cur_usrtm = 0;
|
---|
| 55 | cur_systm = 0;
|
---|
| 56 | elapsed_time = 0;
|
---|
| 57 | delta_rss = 0;
|
---|
| 58 | delta_tottm = 0;
|
---|
| 59 | delta_elapsed_time = 0;
|
---|
| 60 | Update();
|
---|
| 61 |
|
---|
| 62 | }
|
---|
[2212] | 63 | /*!
|
---|
| 64 | Destructor.
|
---|
| 65 | */
|
---|
[2101] | 66 | ResourceUsage::~ResourceUsage()
|
---|
| 67 | {
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | inline uint_8 s_timeval2msec(struct timeval & tv)
|
---|
| 71 | {
|
---|
| 72 | uint_8 ret = tv.tv_sec; ret *= 1000;
|
---|
| 73 | ret += tv.tv_usec/1000; return(ret);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[2212] | 76 | /*!
|
---|
| 77 | Update the CPU and memory usage information.
|
---|
| 78 | */
|
---|
[2101] | 79 | int ResourceUsage::Update()
|
---|
| 80 | {
|
---|
| 81 | struct rusage rsu;
|
---|
| 82 | int rc = getrusage(RUSAGE_SELF, &rsu);
|
---|
| 83 | delta_tottm = cur_tottm;
|
---|
| 84 | delta_rss = cur_rss;
|
---|
| 85 | cur_usrtm = s_timeval2msec(rsu.ru_utime);
|
---|
| 86 | cur_systm = s_timeval2msec(rsu.ru_stime);
|
---|
| 87 | cur_tottm = cur_usrtm+cur_systm;
|
---|
| 88 | cur_rss = rsu.ru_maxrss;
|
---|
| 89 | cur_datasz = rsu.ru_idrss;
|
---|
| 90 | cur_stack = rsu.ru_isrss;
|
---|
| 91 | delta_tottm = cur_tottm-delta_tottm;
|
---|
| 92 | delta_rss = cur_rss-delta_rss;
|
---|
| 93 |
|
---|
| 94 | delta_elapsed_time = elapsed_time;
|
---|
| 95 | time_t tm = time(NULL);
|
---|
| 96 | if (elapsed_time == 0) t0_time = tm;
|
---|
| 97 | elapsed_time = (tm - t0_time)*1000;
|
---|
| 98 | if (elapsed_time < 1) elapsed_time = 1;
|
---|
| 99 | delta_elapsed_time = elapsed_time-delta_elapsed_time;
|
---|
| 100 | return(rc);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[2212] | 103 | /*!
|
---|
| 104 | Prints the CPU and memory usage information.
|
---|
| 105 | \param os : The output stream
|
---|
| 106 | \param lp : The print level (0 .. 2)
|
---|
| 107 | \param upd : if \c true , the Update method is called.
|
---|
| 108 | */
|
---|
[2101] | 109 | void ResourceUsage::Print(ostream& os, int lp, bool upd)
|
---|
| 110 | {
|
---|
| 111 | if (upd) Update();
|
---|
| 112 | os << " --------------- ResourceUsage::Print(lp=" << lp
|
---|
| 113 | <<" ) --------------- " << endl;
|
---|
| 114 | int load = getAverageCPULoad()*100.;
|
---|
| 115 | os << " CPU-Usage= " << getCPUTime() << " Elapsed= " << getElapsedTime()
|
---|
| 116 | << " msec - Load= " << load << " %" << endl;
|
---|
| 117 | long fracmem = getMemorySize()*100/getMaxMemorySize();
|
---|
| 118 | os << " MemoryUsage= " << getMemorySize() << " /Max= " << getMaxMemorySize()
|
---|
| 119 | << " kbytes (" << fracmem << " %)" << endl;
|
---|
| 120 | if (lp < 1) return;
|
---|
| 121 | os << " CPU-Usage= " << cur_tottm << "ms (usr,sys)=("
|
---|
| 122 | << cur_usrtm << "," << cur_systm << ")" << endl;
|
---|
| 123 | os << " DataSize=" << cur_datasz << " /Max " << max_datasz
|
---|
| 124 | << " kbytes" << endl;
|
---|
| 125 | os << " StackSize=" << cur_stack << " /Max " << max_stack
|
---|
| 126 | << " kbytes" << endl;
|
---|
| 127 | os << " ResSize=" << cur_rss << " /Max " << max_rss
|
---|
| 128 | << " kbytes" << endl;
|
---|
| 129 | }
|
---|