source: Sophya/trunk/SophyaLib/SysTools/resusage.cc@ 2598

Last change on this file since 2598 was 2598, checked in by ansari, 21 years ago

Documentation (ajoutee ou completee) pour les classes du module SysTools - Reza 11 Aout 2004

File size: 5.5 KB
RevLine 
[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
[2598]39 - pg = RU_Self : Resource usage for the process itself
40 - pg = RU_Children : Resource usage for the child processes
41 - pg = RU_All : resource usage for the process itself and its child processes
[2212]42*/
[2487]43ResourceUsage::ResourceUsage(RU_ProcGrp pg)
[2101]44{
[2487]45 procgrp = pg;
[2101]46 struct rlimit rl;
47 getrlimit(RLIMIT_DATA, &rl);
48 max_datasz = rl.rlim_cur/1024;
[2191]49#if defined(SunOS)
50 // Max resident size ne semble pas etre defini sur SunOS
51 max_rss = max_datasz;
52#else
[2101]53 getrlimit(RLIMIT_RSS, &rl);
54 max_rss = rl.rlim_cur/1024;
[2191]55#endif
[2101]56 getrlimit(RLIMIT_STACK, &rl);
57 max_stack = rl.rlim_cur/1024;
[2255]58#if defined(Linux)
59 // recuperation des limites sous Linux
60 // car je ne comprends pas ce que renc=voie getrlimit
61 // OPerdereau LAL Orsay 11/2002
62 ReadLinuxTotMem();
63#endif
[2101]64
65 cur_datasz = 0;
66 cur_stack = 0;
67 cur_rss = 0;
68 delta_rss = 0;
69 cur_tottm = 0;
70 cur_usrtm = 0;
71 cur_systm = 0;
72 elapsed_time = 0;
73 delta_rss = 0;
74 delta_tottm = 0;
75 delta_elapsed_time = 0;
[2255]76 cur_pid = getpid();
[2101]77 Update();
78
79}
[2212]80/*!
81 Destructor.
82*/
[2101]83ResourceUsage::~ResourceUsage()
84{
85}
86
87inline uint_8 s_timeval2msec(struct timeval & tv)
88{
89 uint_8 ret = tv.tv_sec; ret *= 1000;
90 ret += tv.tv_usec/1000; return(ret);
91}
92
[2212]93/*!
94 Update the CPU and memory usage information.
95*/
[2101]96int ResourceUsage::Update()
97{
98 struct rusage rsu;
[2487]99 int rc;
100 if ((procgrp == RU_Self) || (procgrp == RU_All))
101 rc = getrusage(RUSAGE_SELF, &rsu);
102 else
103 rc = getrusage(RUSAGE_CHILDREN, &rsu);
104
[2101]105 delta_tottm = cur_tottm;
106 delta_rss = cur_rss;
107 cur_usrtm = s_timeval2msec(rsu.ru_utime);
108 cur_systm = s_timeval2msec(rsu.ru_stime);
[2487]109 if (procgrp == RU_All) {
110 struct rusage rsuch;
111 rc = getrusage(RUSAGE_CHILDREN, &rsuch);
112 cur_usrtm += s_timeval2msec(rsuch.ru_utime);
113 cur_systm += s_timeval2msec(rsuch.ru_stime);
114 }
[2101]115 cur_tottm = cur_usrtm+cur_systm;
[2255]116 delta_tottm = cur_tottm-delta_tottm;
117
118#if defined(Linux)
119 // Recuperation de la place en memoire ss Linux
120 ReadLinuxMem();
121#else
[2101]122 cur_rss = rsu.ru_maxrss;
123 cur_datasz = rsu.ru_idrss;
124 cur_stack = rsu.ru_isrss;
[2255]125#endif
126
[2101]127 delta_rss = cur_rss-delta_rss;
128 delta_elapsed_time = elapsed_time;
129 time_t tm = time(NULL);
130 if (elapsed_time == 0) t0_time = tm;
131 elapsed_time = (tm - t0_time)*1000;
132 if (elapsed_time < 1) elapsed_time = 1;
133 delta_elapsed_time = elapsed_time-delta_elapsed_time;
134 return(rc);
135}
136
[2212]137/*!
138 Prints the CPU and memory usage information.
139 \param os : The output stream
140 \param lp : The print level (0 .. 2)
141 \param upd : if \c true , the Update method is called.
142*/
[2101]143void ResourceUsage::Print(ostream& os, int lp, bool upd)
144{
145 if (upd) Update();
146 os << " --------------- ResourceUsage::Print(lp=" << lp
147 <<" ) --------------- " << endl;
[2487]148 int load = (int)(getAverageCPULoad()*100.);
[2101]149 os << " CPU-Usage= " << getCPUTime() << " Elapsed= " << getElapsedTime()
150 << " msec - Load= " << load << " %" << endl;
151 long fracmem = getMemorySize()*100/getMaxMemorySize();
152 os << " MemoryUsage= " << getMemorySize() << " /Max= " << getMaxMemorySize()
153 << " kbytes (" << fracmem << " %)" << endl;
154 if (lp < 1) return;
155 os << " CPU-Usage= " << cur_tottm << "ms (usr,sys)=("
156 << cur_usrtm << "," << cur_systm << ")" << endl;
157 os << " DataSize=" << cur_datasz << " /Max " << max_datasz
158 << " kbytes" << endl;
159 os << " StackSize=" << cur_stack << " /Max " << max_stack
160 << " kbytes" << endl;
161 os << " ResSize=" << cur_rss << " /Max " << max_rss
162 << " kbytes" << endl;
163}
[2255]164/*!
165 Recuperation de la place en memoire ss Linux
166 ecrit d'apres les sources du code de top
167 O. Perdereau LAL Orsay 11/2002
168
169*/
170void ResourceUsage::ReadLinuxMem(){
171
172#if defined(Linux)
173
174 char flnm[120];
175
176 sprintf(flnm,"/proc/%d/statm",(int)cur_pid);
177 ifstream fich(flnm) ;
178
179
180 long lia,lib,lic,lid,lie,lif,lig;
181
182 fich >> lia >> lib >> lic >> lid >> lie >> lif >> lig ;
183
184 cur_rss = lia*4; // les valeurs sont en pages de 4 k
185 cur_datasz = lib*4;
186 cur_stack = lic*4;
187
188#endif
189
190}
191
192
193/*!
194 Recuperation de la memoire dispo ss Linux
195 ecrit d'apres les sources du code de top
196 O. Perdereau LAL Orsay 11/2002
197*/
198void ResourceUsage::ReadLinuxTotMem(){
199#if defined(Linux)
200
201 char flnm[]="/proc/meminfo";
202 char buff[512];
203 ifstream fich(flnm) ;
204
205 fich.getline(buff,200); // on saute une ligne
206
207 string tst;
208 long lia,lib,lic,lid,lie,lif;
209 fich >> tst >> lia >> lib >> lic >> lid >> lie >> lif;
210
211 max_rss = lia/1024; // conversion en kbytes
212
213#endif
214
215}
Note: See TracBrowser for help on using the repository browser.