52 #include <sys/resource.h> 56 #pragma GCC diagnostic ignored "-Wold-style-cast" 57 #pragma GCC diagnostic ignored "-Wmissing-field-initializers" 62 return STR(
"unavailable");
63 #elif defined(__APPLE__) 64 return STR(
"unavailable");
66 extern int end, etext, edata;
67 long vm_init_data, vm_uninit_data, vm_sbrk_data;
70 temp = (long)(&edata) - (long)(&etext);
71 vm_init_data = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
72 temp = (long)(&end) - (long)(&edata);
73 vm_uninit_data = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
74 temp = (long)sbrk(0) - (long)(&end);
75 vm_sbrk_data = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
76 return STR((vm_init_data + vm_uninit_data + vm_sbrk_data) / 1024) +
"MB";
85 FILETIME creationTime, exitTime, kernelTime, userTime;
87 MEMORYSTATUSEX statex;
89 PROCESS_MEMORY_COUNTERS pmc;
90 size_t peak_working_set;
94 WSAStartup(MAKEWORD(2, 2), &wsaData);
95 (void)gethostname(hostname,
sizeof(hostname));
96 hostname[
sizeof(hostname) - 1] =
'\0';
100 if(GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime))
102 ULARGE_INTEGER integerSystemTime, integerUserTime;
103 integerUserTime.u.LowPart = userTime.dwLowDateTime;
104 integerUserTime.u.HighPart = userTime.dwHighDateTime;
105 user = (double)integerUserTime.QuadPart * 1e-7;
106 integerSystemTime.u.LowPart = kernelTime.dwLowDateTime;
107 integerSystemTime.u.HighPart = kernelTime.dwHighDateTime;
108 system = (
double)integerSystemTime.QuadPart * 1e-7;
114 statex.dwLength =
sizeof(statex);
115 if(GlobalMemoryStatusEx(&statex))
117 vm_limit = (
size_t)(statex.ullTotalVirtual / 1024.0 + 0.5);
123 if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
sizeof(pmc)))
125 peak_working_set = (
size_t)(pmc.PeakWorkingSetSize / 1024.0 + 0.5);
126 page_faults = (long)pmc.PageFaultCount;
130 peak_working_set = 0;
133 os <<
"Runtime Statistics\n";
134 os <<
"------------------\n";
135 os <<
"Machine name: " << hostname << std::endl;
136 os <<
"User time " << user <<
" seconds\n";
137 os <<
"System time " << system <<
" seconds\n\n";
138 os <<
"Maximum resident size = ";
139 if(peak_working_set == 0)
140 os <<
"unavailable\n";
142 os << peak_working_set <<
"\n";
143 os <<
"Virtual memory limit = ";
145 os <<
"unavailable\n";
147 os << vm_limit <<
"\n";
148 os <<
"Page faults = " << page_faults <<
"\n";
149 #elif defined(__APPLE__) 152 extern int end, etext, edata;
159 int text, data,
stack;
160 rlim_t vm_limit, vm_soft_limit;
161 long double user, system, scale;
164 long vm_text, vm_init_data, vm_uninit_data, vm_sbrk_data;
167 (void)gethostname(hostname, 256);
168 hostname[256] =
'\0';
171 temp = (long)(&etext);
172 vm_text = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
173 temp = (long)(&edata) - (long)(&etext);
174 vm_init_data = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
175 temp = (long)(&end) - (long)(&edata);
176 vm_uninit_data = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
177 temp = (long)sbrk(0) - (long)(&end);
178 vm_sbrk_data = temp / 1024 + (((temp % 1024) > 512) ? 1 : 0);
181 (void)getrlimit(RLIMIT_DATA, &rlp);
182 vm_limit = rlp.rlim_max / 1024 + (((rlp.rlim_max % 1024) > 512) ? 1 : 0);
183 vm_soft_limit = rlp.rlim_cur / 1024 + (((rlp.rlim_cur % 1024) > 512) ? 1 : 0);
186 (void)getrusage(RUSAGE_SELF, &rusage);
187 user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec / 1000000.L;
188 system = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec / 1000000.L;
189 scale = (user + system) * 100.0
L;
195 os <<
"Runtime Statistics\n";
196 os <<
"------------------\n";
197 os <<
"Machine name: " << hostname << std::endl;
198 os <<
"User time " << user <<
" seconds\n";
199 os <<
"System time " << system <<
" seconds\n\n";
201 text = (int)(rusage.ru_ixrss / scale + 0.5L);
202 data = (int)((rusage.ru_idrss) / scale + 0.5L);
203 stack = (int)((rusage.ru_isrss) / scale + 0.5L);
204 os <<
"Average resident text size = " << text <<
"K\n";
205 os <<
"Average resident data size = " << data <<
"K\n";
206 os <<
"Average resident stack size = " << stack <<
"K\n";
207 os <<
"Maximum resident size = " << rusage.ru_maxrss / 2 <<
"K\n\n";
208 os <<
"Virtual text size = " << vm_text <<
"K\n";
209 os <<
"Virtual data size = " << vm_init_data + vm_uninit_data + vm_sbrk_data <<
"K\n";
210 os <<
" data size initialized = " << vm_init_data <<
"K\n";
211 os <<
" data size uninitialized = " << vm_uninit_data <<
"K\n";
212 os <<
" data size sbrk = " << vm_sbrk_data <<
"K\n";
213 os <<
"Virtual memory limit = ";
214 if(rlp.rlim_cur == RLIM_INFINITY)
220 os << vm_soft_limit <<
"K";
223 if(rlp.rlim_max == RLIM_INFINITY)
229 os << vm_limit <<
"K";
233 os <<
"Major page faults = " << rusage.ru_majflt <<
"\n";
234 os <<
"Minor page faults = " << rusage.ru_minflt <<
"\n";
235 os <<
"Swaps = " << rusage.ru_nswap <<
"\n";
236 os <<
"Input blocks = " << rusage.ru_inblock <<
"\n";
237 os <<
"Output blocks = " << rusage.ru_oublock <<
"\n";
238 os <<
"Context switch (voluntary) = " << rusage.ru_nvcsw <<
"\n";
239 os <<
"Context switch (involuntary) = " << rusage.ru_nivcsw <<
"\n";
#define STR(s)
Macro which performs a lexical_cast to a string.
Auxiliary methods for manipulating string.
std::string PrintVirtualDataMemoryUsage()
void util_print_cpu_stats(std::ostream &os)
Utility managing CPU statistics.