[2101] | 1 | #include "resusage.h"
|
---|
| 2 |
|
---|
[2255] | 3 | #include <typeinfo>
|
---|
[2101] | 4 | #include <sys/types.h>
|
---|
| 5 | #include <sys/time.h>
|
---|
[2255] | 6 | #include <sys/stat.h>
|
---|
[2101] | 7 | #include <sys/resource.h>
|
---|
| 8 | #include <unistd.h>
|
---|
| 9 | #include <time.h>
|
---|
[2255] | 10 | #include <fcntl.h>
|
---|
[2322] | 11 | #include <fstream>
|
---|
[2255] | 12 | #include <stdio.h>
|
---|
| 13 | #include <string>
|
---|
[2101] | 14 | /*!
|
---|
| 15 | \class SOPHYA::ResourceUsage
|
---|
| 16 | \ingroup SysTools
|
---|
| 17 | This class gives acces to various system resource usage
|
---|
| 18 | (CPU, memory, ...).
|
---|
| 19 | All returned values are in kilobytes for memory consumptions
|
---|
| 20 | and in milli-seconds fo CPU and elapsed times.
|
---|
| 21 | The information should be updated through the call to the
|
---|
| 22 | Update() method.
|
---|
[2212] | 23 | Note: The information is obtained through \c getrusage()
|
---|
| 24 | and \c getrlimit() system calls, and depending on the OS,
|
---|
| 25 | not all of the memory usage values are correctly filled.
|
---|
| 26 | \code
|
---|
| 27 | // How to check resource usage for a given part of the program
|
---|
| 28 | ResourceUsage res;
|
---|
| 29 | // --- Part of the program to be checked : Start
|
---|
| 30 | // ...
|
---|
| 31 | res.Update();
|
---|
| 32 | cout << " Memory size increase (KB):" << res.getDeltaMemorySize() << endl;
|
---|
| 33 | cout << " Resource usage info : \n" << res << endl;
|
---|
| 34 | \endcode
|
---|
[2101] | 35 | */
|
---|
[2212] | 36 | /*!
|
---|
| 37 | Constructor. the \b Update() method is called.
|
---|
[2487] | 38 | \param pg: Process group RU_Self or RU_Children or RU_All
|
---|
[2212] | 39 | */
|
---|
[2487] | 40 | ResourceUsage::ResourceUsage(RU_ProcGrp pg)
|
---|
[2101] | 41 | {
|
---|
[2487] | 42 | procgrp = pg;
|
---|
[2101] | 43 | struct rlimit rl;
|
---|
| 44 | getrlimit(RLIMIT_DATA, &rl);
|
---|
| 45 | max_datasz = rl.rlim_cur/1024;
|
---|
[2191] | 46 | #if defined(SunOS)
|
---|
| 47 | // Max resident size ne semble pas etre defini sur SunOS
|
---|
| 48 | max_rss = max_datasz;
|
---|
| 49 | #else
|
---|
[2101] | 50 | getrlimit(RLIMIT_RSS, &rl);
|
---|
| 51 | max_rss = rl.rlim_cur/1024;
|
---|
[2191] | 52 | #endif
|
---|
[2101] | 53 | getrlimit(RLIMIT_STACK, &rl);
|
---|
| 54 | max_stack = rl.rlim_cur/1024;
|
---|
[2255] | 55 | #if defined(Linux)
|
---|
| 56 | // recuperation des limites sous Linux
|
---|
| 57 | // car je ne comprends pas ce que renc=voie getrlimit
|
---|
| 58 | // OPerdereau LAL Orsay 11/2002
|
---|
| 59 | ReadLinuxTotMem();
|
---|
| 60 | #endif
|
---|
[2101] | 61 |
|
---|
| 62 | cur_datasz = 0;
|
---|
| 63 | cur_stack = 0;
|
---|
| 64 | cur_rss = 0;
|
---|
| 65 | delta_rss = 0;
|
---|
| 66 | cur_tottm = 0;
|
---|
| 67 | cur_usrtm = 0;
|
---|
| 68 | cur_systm = 0;
|
---|
| 69 | elapsed_time = 0;
|
---|
| 70 | delta_rss = 0;
|
---|
| 71 | delta_tottm = 0;
|
---|
| 72 | delta_elapsed_time = 0;
|
---|
[2255] | 73 | cur_pid = getpid();
|
---|
[2101] | 74 | Update();
|
---|
| 75 |
|
---|
| 76 | }
|
---|
[2212] | 77 | /*!
|
---|
| 78 | Destructor.
|
---|
| 79 | */
|
---|
[2101] | 80 | ResourceUsage::~ResourceUsage()
|
---|
| 81 | {
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | inline uint_8 s_timeval2msec(struct timeval & tv)
|
---|
| 85 | {
|
---|
| 86 | uint_8 ret = tv.tv_sec; ret *= 1000;
|
---|
| 87 | ret += tv.tv_usec/1000; return(ret);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[2212] | 90 | /*!
|
---|
| 91 | Update the CPU and memory usage information.
|
---|
| 92 | */
|
---|
[2101] | 93 | int ResourceUsage::Update()
|
---|
| 94 | {
|
---|
| 95 | struct rusage rsu;
|
---|
[2487] | 96 | int rc;
|
---|
| 97 | if ((procgrp == RU_Self) || (procgrp == RU_All))
|
---|
| 98 | rc = getrusage(RUSAGE_SELF, &rsu);
|
---|
| 99 | else
|
---|
| 100 | rc = getrusage(RUSAGE_CHILDREN, &rsu);
|
---|
| 101 |
|
---|
[2101] | 102 | delta_tottm = cur_tottm;
|
---|
| 103 | delta_rss = cur_rss;
|
---|
| 104 | cur_usrtm = s_timeval2msec(rsu.ru_utime);
|
---|
| 105 | cur_systm = s_timeval2msec(rsu.ru_stime);
|
---|
[2487] | 106 | if (procgrp == RU_All) {
|
---|
| 107 | struct rusage rsuch;
|
---|
| 108 | rc = getrusage(RUSAGE_CHILDREN, &rsuch);
|
---|
| 109 | cur_usrtm += s_timeval2msec(rsuch.ru_utime);
|
---|
| 110 | cur_systm += s_timeval2msec(rsuch.ru_stime);
|
---|
| 111 | }
|
---|
[2101] | 112 | cur_tottm = cur_usrtm+cur_systm;
|
---|
[2255] | 113 | delta_tottm = cur_tottm-delta_tottm;
|
---|
| 114 |
|
---|
| 115 | #if defined(Linux)
|
---|
| 116 | // Recuperation de la place en memoire ss Linux
|
---|
| 117 | ReadLinuxMem();
|
---|
| 118 | #else
|
---|
[2101] | 119 | cur_rss = rsu.ru_maxrss;
|
---|
| 120 | cur_datasz = rsu.ru_idrss;
|
---|
| 121 | cur_stack = rsu.ru_isrss;
|
---|
[2255] | 122 | #endif
|
---|
| 123 |
|
---|
[2101] | 124 | delta_rss = cur_rss-delta_rss;
|
---|
| 125 | delta_elapsed_time = elapsed_time;
|
---|
| 126 | time_t tm = time(NULL);
|
---|
| 127 | if (elapsed_time == 0) t0_time = tm;
|
---|
| 128 | elapsed_time = (tm - t0_time)*1000;
|
---|
| 129 | if (elapsed_time < 1) elapsed_time = 1;
|
---|
| 130 | delta_elapsed_time = elapsed_time-delta_elapsed_time;
|
---|
| 131 | return(rc);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[2212] | 134 | /*!
|
---|
| 135 | Prints the CPU and memory usage information.
|
---|
| 136 | \param os : The output stream
|
---|
| 137 | \param lp : The print level (0 .. 2)
|
---|
| 138 | \param upd : if \c true , the Update method is called.
|
---|
| 139 | */
|
---|
[2101] | 140 | void ResourceUsage::Print(ostream& os, int lp, bool upd)
|
---|
| 141 | {
|
---|
| 142 | if (upd) Update();
|
---|
| 143 | os << " --------------- ResourceUsage::Print(lp=" << lp
|
---|
| 144 | <<" ) --------------- " << endl;
|
---|
[2487] | 145 | int load = (int)(getAverageCPULoad()*100.);
|
---|
[2101] | 146 | os << " CPU-Usage= " << getCPUTime() << " Elapsed= " << getElapsedTime()
|
---|
| 147 | << " msec - Load= " << load << " %" << endl;
|
---|
| 148 | long fracmem = getMemorySize()*100/getMaxMemorySize();
|
---|
| 149 | os << " MemoryUsage= " << getMemorySize() << " /Max= " << getMaxMemorySize()
|
---|
| 150 | << " kbytes (" << fracmem << " %)" << endl;
|
---|
| 151 | if (lp < 1) return;
|
---|
| 152 | os << " CPU-Usage= " << cur_tottm << "ms (usr,sys)=("
|
---|
| 153 | << cur_usrtm << "," << cur_systm << ")" << endl;
|
---|
| 154 | os << " DataSize=" << cur_datasz << " /Max " << max_datasz
|
---|
| 155 | << " kbytes" << endl;
|
---|
| 156 | os << " StackSize=" << cur_stack << " /Max " << max_stack
|
---|
| 157 | << " kbytes" << endl;
|
---|
| 158 | os << " ResSize=" << cur_rss << " /Max " << max_rss
|
---|
| 159 | << " kbytes" << endl;
|
---|
| 160 | }
|
---|
[2255] | 161 | /*!
|
---|
| 162 | Recuperation de la place en memoire ss Linux
|
---|
| 163 | ecrit d'apres les sources du code de top
|
---|
| 164 | O. Perdereau LAL Orsay 11/2002
|
---|
| 165 |
|
---|
| 166 | */
|
---|
| 167 | void ResourceUsage::ReadLinuxMem(){
|
---|
| 168 |
|
---|
| 169 | #if defined(Linux)
|
---|
| 170 |
|
---|
| 171 | char flnm[120];
|
---|
| 172 |
|
---|
| 173 | sprintf(flnm,"/proc/%d/statm",(int)cur_pid);
|
---|
| 174 | ifstream fich(flnm) ;
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | long lia,lib,lic,lid,lie,lif,lig;
|
---|
| 178 |
|
---|
| 179 | fich >> lia >> lib >> lic >> lid >> lie >> lif >> lig ;
|
---|
| 180 |
|
---|
| 181 | cur_rss = lia*4; // les valeurs sont en pages de 4 k
|
---|
| 182 | cur_datasz = lib*4;
|
---|
| 183 | cur_stack = lic*4;
|
---|
| 184 |
|
---|
| 185 | #endif
|
---|
| 186 |
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | /*!
|
---|
| 191 | Recuperation de la memoire dispo ss Linux
|
---|
| 192 | ecrit d'apres les sources du code de top
|
---|
| 193 | O. Perdereau LAL Orsay 11/2002
|
---|
| 194 | */
|
---|
| 195 | void ResourceUsage::ReadLinuxTotMem(){
|
---|
| 196 | #if defined(Linux)
|
---|
| 197 |
|
---|
| 198 | char flnm[]="/proc/meminfo";
|
---|
| 199 | char buff[512];
|
---|
| 200 | ifstream fich(flnm) ;
|
---|
| 201 |
|
---|
| 202 | fich.getline(buff,200); // on saute une ligne
|
---|
| 203 |
|
---|
| 204 | string tst;
|
---|
| 205 | long lia,lib,lic,lid,lie,lif;
|
---|
| 206 | fich >> tst >> lia >> lib >> lic >> lid >> lie >> lif;
|
---|
| 207 |
|
---|
| 208 | max_rss = lia/1024; // conversion en kbytes
|
---|
| 209 |
|
---|
| 210 | #endif
|
---|
| 211 |
|
---|
| 212 | }
|
---|