1 | /* $Id: timings.c,v 1.1.1.2 2004-10-03 04:59:48 ghudson Exp $ */ |
---|
2 | |
---|
3 | /* Copyright (C) 1998-99 Martin Baulig |
---|
4 | This file is part of LibGTop 1.0. |
---|
5 | |
---|
6 | Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. |
---|
7 | |
---|
8 | LibGTop is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, |
---|
11 | or (at your option) any later version. |
---|
12 | |
---|
13 | LibGTop is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
16 | for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with LibGTop; see the file COPYING. If not, write to the |
---|
20 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
21 | Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <locale.h> |
---|
25 | #include <stdio.h> |
---|
26 | #include <libintl.h> |
---|
27 | |
---|
28 | #include <glibtop.h> |
---|
29 | #include <glibtop/open.h> |
---|
30 | #include <glibtop/close.h> |
---|
31 | |
---|
32 | #include <glibtop/parameter.h> |
---|
33 | |
---|
34 | #include <glibtop/union.h> |
---|
35 | #include <glibtop/sysdeps.h> |
---|
36 | |
---|
37 | #include <sys/times.h> |
---|
38 | #include <sys/resource.h> |
---|
39 | |
---|
40 | #ifndef PROFILE_COUNT |
---|
41 | #define PROFILE_COUNT 100000L |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef PROFILE_COUNT_EXPENSIVE |
---|
45 | #define PROFILE_COUNT_EXPENSIVE 10000L |
---|
46 | #endif |
---|
47 | |
---|
48 | #define ELAPSED_UTIME ((unsigned long) elapsed_utime.tv_sec * 1000000 + (unsigned long) elapsed_utime.tv_usec) |
---|
49 | #define ELAPSED_STIME ((unsigned long) elapsed_stime.tv_sec * 1000000 + (unsigned long) elapsed_stime.tv_usec) |
---|
50 | |
---|
51 | #define libgtop_timeradd(tvp, uvp, vvp) \ |
---|
52 | do { \ |
---|
53 | (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ |
---|
54 | (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ |
---|
55 | if ((vvp)->tv_usec >= 1000000) { \ |
---|
56 | (vvp)->tv_sec++; \ |
---|
57 | (vvp)->tv_usec -= 1000000; \ |
---|
58 | } \ |
---|
59 | } while (0) |
---|
60 | #define libgtop_timersub(tvp, uvp, vvp) \ |
---|
61 | do { \ |
---|
62 | (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ |
---|
63 | (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ |
---|
64 | if ((vvp)->tv_usec < 0) { \ |
---|
65 | (vvp)->tv_sec--; \ |
---|
66 | (vvp)->tv_usec += 1000000; \ |
---|
67 | } \ |
---|
68 | } while (0) |
---|
69 | |
---|
70 | int |
---|
71 | main (int argc, char *argv []) |
---|
72 | { |
---|
73 | glibtop_union data; |
---|
74 | unsigned c, count, *ptr; |
---|
75 | struct rusage total_start, total_end; |
---|
76 | struct rusage rusage_start, rusage_end; |
---|
77 | struct timeval elapsed_utime, elapsed_stime; |
---|
78 | pid_t pid; |
---|
79 | |
---|
80 | count = PROFILE_COUNT; |
---|
81 | |
---|
82 | setlocale (LC_ALL, ""); |
---|
83 | bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR); |
---|
84 | textdomain (GETTEXT_PACKAGE); |
---|
85 | |
---|
86 | printf ("%-12s (%-10s): %7s - %9s - %9s\n", |
---|
87 | "Feature", "Flags", "Count", "utime", "stime"); |
---|
88 | printf ("-------------------------------------------" |
---|
89 | "---------------\n"); |
---|
90 | |
---|
91 | glibtop_init_r (&glibtop_global_server, 0, 0); |
---|
92 | |
---|
93 | getrusage (RUSAGE_SELF, &total_start); |
---|
94 | |
---|
95 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
96 | |
---|
97 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
98 | glibtop_get_cpu (&data.cpu); |
---|
99 | |
---|
100 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
101 | |
---|
102 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
103 | &elapsed_utime); |
---|
104 | |
---|
105 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
106 | &elapsed_stime); |
---|
107 | |
---|
108 | printf ("CPU (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
109 | (unsigned long) data.cpu.flags, PROFILE_COUNT, |
---|
110 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
111 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
112 | |
---|
113 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
114 | |
---|
115 | for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) |
---|
116 | glibtop_get_mem (&data.mem); |
---|
117 | |
---|
118 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
119 | |
---|
120 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
121 | &elapsed_utime); |
---|
122 | |
---|
123 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
124 | &elapsed_stime); |
---|
125 | |
---|
126 | printf ("Memory (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
127 | (unsigned long) data.mem.flags, PROFILE_COUNT_EXPENSIVE, |
---|
128 | (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, |
---|
129 | (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); |
---|
130 | |
---|
131 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
132 | |
---|
133 | for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) |
---|
134 | glibtop_get_swap (&data.swap); |
---|
135 | |
---|
136 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
137 | |
---|
138 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
139 | &elapsed_utime); |
---|
140 | |
---|
141 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
142 | &elapsed_stime); |
---|
143 | |
---|
144 | printf ("Swap (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
145 | (unsigned long) data.swap.flags, PROFILE_COUNT_EXPENSIVE, |
---|
146 | (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, |
---|
147 | (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); |
---|
148 | |
---|
149 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
150 | |
---|
151 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
152 | glibtop_get_uptime (&data.uptime); |
---|
153 | |
---|
154 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
155 | |
---|
156 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
157 | &elapsed_utime); |
---|
158 | |
---|
159 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
160 | &elapsed_stime); |
---|
161 | |
---|
162 | printf ("Uptime (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
163 | (unsigned long) data.uptime.flags, PROFILE_COUNT, |
---|
164 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
165 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
166 | |
---|
167 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
168 | |
---|
169 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
170 | glibtop_get_loadavg (&data.loadavg); |
---|
171 | |
---|
172 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
173 | |
---|
174 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
175 | &elapsed_utime); |
---|
176 | |
---|
177 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
178 | &elapsed_stime); |
---|
179 | |
---|
180 | printf ("Loadavg (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
181 | (unsigned long) data.loadavg.flags, PROFILE_COUNT, |
---|
182 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
183 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
184 | |
---|
185 | printf ("\n"); |
---|
186 | |
---|
187 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
188 | |
---|
189 | for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) { |
---|
190 | ptr = glibtop_get_proclist (&data.proclist, 0, 0); |
---|
191 | g_free (ptr); |
---|
192 | } |
---|
193 | |
---|
194 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
195 | |
---|
196 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
197 | &elapsed_utime); |
---|
198 | |
---|
199 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
200 | &elapsed_stime); |
---|
201 | |
---|
202 | printf ("Proclist (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
203 | (unsigned long) data.proclist.flags, |
---|
204 | PROFILE_COUNT_EXPENSIVE, |
---|
205 | (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, |
---|
206 | (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); |
---|
207 | |
---|
208 | pid = getpid (); |
---|
209 | |
---|
210 | printf ("\n"); |
---|
211 | |
---|
212 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
213 | |
---|
214 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
215 | glibtop_get_proc_state (&data.proc_state, pid); |
---|
216 | |
---|
217 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
218 | |
---|
219 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
220 | &elapsed_utime); |
---|
221 | |
---|
222 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
223 | &elapsed_stime); |
---|
224 | |
---|
225 | printf ("Proc_State (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
226 | (unsigned long) data.proc_state.flags, PROFILE_COUNT, |
---|
227 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
228 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
229 | |
---|
230 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
231 | |
---|
232 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
233 | glibtop_get_proc_uid (&data.proc_uid, pid); |
---|
234 | |
---|
235 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
236 | |
---|
237 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
238 | &elapsed_utime); |
---|
239 | |
---|
240 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
241 | &elapsed_stime); |
---|
242 | |
---|
243 | printf ("Proc_Uid (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
244 | (unsigned long) data.proc_uid.flags, PROFILE_COUNT, |
---|
245 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
246 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
247 | |
---|
248 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
249 | |
---|
250 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
251 | glibtop_get_proc_mem (&data.proc_mem, pid); |
---|
252 | |
---|
253 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
254 | |
---|
255 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
256 | &elapsed_utime); |
---|
257 | |
---|
258 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
259 | &elapsed_stime); |
---|
260 | |
---|
261 | printf ("Proc_Mem (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
262 | (unsigned long) data.proc_mem.flags, PROFILE_COUNT, |
---|
263 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
264 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
265 | |
---|
266 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
267 | |
---|
268 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
269 | glibtop_get_proc_segment (&data.proc_segment, pid); |
---|
270 | |
---|
271 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
272 | |
---|
273 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
274 | &elapsed_utime); |
---|
275 | |
---|
276 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
277 | &elapsed_stime); |
---|
278 | |
---|
279 | printf ("Proc_Segment (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
280 | (unsigned long) data.proc_segment.flags, PROFILE_COUNT, |
---|
281 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
282 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
283 | |
---|
284 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
285 | |
---|
286 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
287 | glibtop_get_proc_time (&data.proc_time, pid); |
---|
288 | |
---|
289 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
290 | |
---|
291 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
292 | &elapsed_utime); |
---|
293 | |
---|
294 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
295 | &elapsed_stime); |
---|
296 | |
---|
297 | printf ("Proc_Time (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
298 | (unsigned long) data.proc_time.flags, PROFILE_COUNT, |
---|
299 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
300 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
301 | |
---|
302 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
303 | |
---|
304 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
305 | glibtop_get_proc_signal (&data.proc_signal, pid); |
---|
306 | |
---|
307 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
308 | |
---|
309 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
310 | &elapsed_utime); |
---|
311 | |
---|
312 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
313 | &elapsed_stime); |
---|
314 | |
---|
315 | printf ("Proc_Signal (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
316 | (unsigned long) data.proc_signal.flags, PROFILE_COUNT, |
---|
317 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
318 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
319 | |
---|
320 | getrusage (RUSAGE_SELF, &rusage_start); |
---|
321 | |
---|
322 | for (c = 0; c < PROFILE_COUNT; c++) |
---|
323 | glibtop_get_proc_kernel (&data.proc_kernel, pid); |
---|
324 | |
---|
325 | getrusage (RUSAGE_SELF, &rusage_end); |
---|
326 | |
---|
327 | libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, |
---|
328 | &elapsed_utime); |
---|
329 | |
---|
330 | libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, |
---|
331 | &elapsed_stime); |
---|
332 | |
---|
333 | printf ("Proc_Kernel (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", |
---|
334 | (unsigned long) data.proc_kernel.flags, PROFILE_COUNT, |
---|
335 | (long double) ELAPSED_UTIME / PROFILE_COUNT, |
---|
336 | (long double) ELAPSED_STIME / PROFILE_COUNT); |
---|
337 | |
---|
338 | getrusage (RUSAGE_SELF, &total_end); |
---|
339 | |
---|
340 | libgtop_timersub (&total_end.ru_utime, &total_start.ru_utime, |
---|
341 | &elapsed_utime); |
---|
342 | |
---|
343 | libgtop_timersub (&total_end.ru_stime, &total_start.ru_stime, |
---|
344 | &elapsed_stime); |
---|
345 | |
---|
346 | printf ("-------------------------------------------" |
---|
347 | "---------------\n"); |
---|
348 | |
---|
349 | printf ("%-36s %9lu - %9lu\n\n", "TOTAL", |
---|
350 | ELAPSED_UTIME, ELAPSED_STIME); |
---|
351 | |
---|
352 | printf ("All timings are in clock ticks " |
---|
353 | "(1000000 ticks per second).\n\n"); |
---|
354 | |
---|
355 | glibtop_close (); |
---|
356 | |
---|
357 | exit (0); |
---|
358 | } |
---|