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