1 *********************************************************************
3 Stuff for printing out GC statistics
5 usertime() -- The current user time in seconds
6 elapsedtime() -- The current elapsed time in seconds
13 *********************************************************************
16 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
18 /* how is this to work given we have not read platform.h yet? */
20 #define _INCLUDE_HPUX_SOURCE
24 #include "SMinternal.h"
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
35 #ifdef HAVE_SYS_TIMES_H
36 #include <sys/times.h>
39 #ifdef HAVE_SYS_TIME_H
43 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
44 #include <sys/resource.h>
47 #ifdef HAVE_SYS_TIMEB_H
48 #include <sys/timeb.h>
52 #include <sys/syscall.h>
53 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
54 #define HAVE_GETRUSAGE
57 static StgDouble GC_start_time, GC_tot_time = 0; /* User GC Time */
58 static StgDouble GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
60 #if defined(GCap) || defined(GCgn)
61 static StgDouble GC_min_time = 0;
62 static StgDouble GCe_min_time = 0;
64 static I_ GC_min_no = 0;
65 static I_ GC_min_since_maj = 0;
66 static I_ GC_live_maj = 0; /* Heap live at last major collection */
67 static I_ GC_alloc_since_maj = 0; /* Heap alloc since collection major */
70 static I_ GC_maj_no = 0;
71 static ullong GC_tot_alloc = 0; /* Total heap allocated -- 64 bits? */
73 static I_ GC_start_faults = 0, GC_end_faults = 0;
76 ullong_format_string(ullong x, char *s, rtsBool with_commas)
79 sprintf(s, "%ld", (I_)x);
80 else if (x < (ullong)1000000)
81 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
82 (I_)((x)/(ullong)1000),
83 (I_)((x)%(ullong)1000));
84 else if (x < (ullong)1000000000)
85 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
86 (I_)((x)/(ullong)1000000),
87 (I_)((x)/(ullong)1000%(ullong)1000),
88 (I_)((x)%(ullong)1000));
90 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
91 (I_)((x)/(ullong)1000000000),
92 (I_)((x)/(ullong)1000000%(ullong)1000),
93 (I_)((x)/(ullong)1000%(ullong)1000),
94 (I_)((x)%(ullong)1000));
98 /* "constants" for "usertime" and "elapsedtime" */
100 static StgDouble ElapsedTimeStart = 0.0; /* setup when beginning things */
101 static StgDouble TicksPerSecond = 0.0; /* ditto */
103 /* usertime() -- The current user time in seconds */
108 #if ! (defined(HAVE_GETRUSAGE) || defined(HAVE_TIMES))
109 /* We will #ifdef around the fprintf for machines
110 we *know* are unsupported. (WDP 94/05)
112 fprintf(stderr, "NOTE: `usertime' does nothing!\n");
115 #else /* not stumped */
117 /* "times" is the more standard, but we prefer "getrusage"
118 (because we are old worn-out BSD hackers)
120 # if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
123 getrusage(RUSAGE_SELF, &t);
124 return(t.ru_utime.tv_sec + 1e-6*t.ru_utime.tv_usec);
126 # else /* HAVE_TIMES */
130 return(((StgDouble)(t.tms_utime))/TicksPerSecond);
132 # endif /* HAVE_TIMES */
133 #endif /* not stumped */
137 /* elapsedtime() -- The current elapsed time in seconds */
142 #if ! (defined(HAVE_TIMES) || defined(HAVE_FTIME))
143 /* We will #ifdef around the fprintf for machines
144 we *know* are unsupported. (WDP 94/05)
146 fprintf(stderr, "NOTE: `elapsedtime' does nothing!\n");
149 #else /* not stumped */
151 /* "ftime" may be nicer, but "times" is more standard;
152 but, on a Sun, if you do not get the SysV one, you are *hosed*...
155 # if defined(HAVE_TIMES) && ! sunos4_TARGET_OS
158 return (((StgDouble) times(&t))/TicksPerSecond - ElapsedTimeStart);
160 # else /* HAVE_FTIME */
164 return t.time + 1e-3*t.millitm - ElapsedTimeStart;
166 # endif /* HAVE_FTIME */
167 #endif /* not stumped */
171 start_time(STG_NO_ARGS)
175 /* Determine TicksPerSecond ... */
177 ticks = sysconf(_SC_CLK_TCK);
179 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
182 TicksPerSecond = (StgDouble) ticks;
184 #else /* no "sysconf"; had better guess */
186 TicksPerSecond = (StgDouble) (HZ);
188 # else /* had better guess wildly */
189 /* We will #ifdef around the fprintf for machines
190 we *know* are unsupported. (WDP 94/05)
192 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
193 TicksPerSecond = 60.0;
197 ElapsedTimeStart = elapsedtime();
200 static StgDouble InitUserTime = 0.0; /* user time taken for initialization */
201 static StgDouble InitElapsedTime = 0.0; /* elapsed time taken for initialization */
203 void end_init(STG_NO_ARGS)
205 InitUserTime = usertime();
206 InitElapsedTime = elapsedtime();
210 pagefaults(STG_NO_ARGS)
212 #if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS
217 getrusage(RUSAGE_SELF, &t);
222 /* Called at the beginning of execution of the program */
223 /* Writes the command line and inits stats header */
226 stat_init(char *collector, char *comment1, char *comment2)
228 FILE *sf = RTSflags.GcFlags.statsFile;
231 char temp[BIG_STRING_LEN];
232 ullong_format_string( (ullong)RTSflags.GcFlags.heapSize*sizeof(W_), temp, rtsTrue/*commas*/);
233 fprintf(sf, "\nCollector: %s HeapSize: %s (bytes)\n\n", collector, temp);
234 if (RTSflags.GcFlags.giveStats) {
235 #if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS
236 fprintf(sf, "NOTE: `pagefaults' does nothing!\n");
239 /*######## ####### ####### ##.# ##.## ##.## ####.## ####.## #### ####*/
240 " Alloc Collect Live Resid GC GC TOT TOT Page Flts %s\n",
243 " bytes bytes bytes ency user elap user elap GC MUT %s\n",
247 #if defined(GCap) || defined(GCgn)
250 /*######## ####### ##.# ####### ##.# ### ##.## ##.## ##.## ##.## ####.## ####.## #### ####*/
251 " Alloc Promote Promo Live Resid Minor Minor Minor Major Major TOT TOT Page Flts\n");
253 " bytes bytes ted bytes ency No user elap user elap user elap MUT Major\n");
255 #endif /* generational */
261 /* Called at the beginning of each GC */
262 static I_ rub_bell = 0;
265 stat_startGC(I_ alloc)
267 FILE *sf = RTSflags.GcFlags.statsFile;
269 #if defined(GCap) || defined(GCgn)
270 I_ bell = alloc == 0 ? RTSflags.GcFlags.ringBell : 0;
271 #else /* ! generational */
272 I_ bell = RTSflags.GcFlags.ringBell;
273 #endif /* ! generational */
277 fprintf(stderr, " GC ");
280 fprintf(stderr, "\007");
285 GC_start_time = usertime();
286 GCe_start_time = elapsedtime();
288 #if defined(GCap) || defined(GCgn)
289 if (RTSflags.GcFlags.giveStats || alloc == 0) {
290 GC_start_faults = pagefaults();
292 #else /* ! generational */
293 if (RTSflags.GcFlags.giveStats) {
294 GC_start_faults = pagefaults();
296 #endif /* ! generational */
301 /* Called at the end of each GC */
304 stat_endGC(I_ alloc, I_ collect, I_ live, char *comment)
306 FILE *sf = RTSflags.GcFlags.statsFile;
309 StgDouble time = usertime();
310 StgDouble etime = elapsedtime();
312 if (RTSflags.GcFlags.giveStats) {
313 I_ faults = pagefaults();
315 fprintf(sf, "%8ld %7ld %7ld %5.1f%%",
316 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_), collect == 0 ? 0.0 : (live / (StgDouble) collect * 100));
317 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld %s\n",
318 (time-GC_start_time),
319 (etime-GCe_start_time),
322 faults - GC_start_faults,
323 GC_start_faults - GC_end_faults,
326 GC_end_faults = faults;
330 #if defined(GCap) || defined(GCgn)
331 else if(alloc == 0 && collect != 0) {
332 I_ faults = pagefaults();
334 fprintf(sf, "%8ld %7ld %5.1f%% %7ld %5.1f%%",
335 GC_alloc_since_maj*sizeof(W_), (collect - GC_live_maj)*sizeof(W_),
336 (collect - GC_live_maj) / (StgDouble) GC_alloc_since_maj * 100,
337 live*sizeof(W_), live / (StgDouble) RTSflags.GcFlags.heapSize * 100);
338 fprintf(sf, " %3ld %5.2f %5.2f %5.2f %5.2f %7.2f %7.2f %4ld %4ld\n",
339 GC_min_since_maj, GC_min_time, GCe_min_time,
340 (time-GC_start_time),
341 (etime-GCe_start_time),
344 faults - GC_start_faults,
345 GC_start_faults - GC_end_faults
348 GC_end_faults = faults;
351 #endif /* generational */
353 #if defined(GCap) || defined(GCgn)
354 if (alloc == 0 && collect != 0) {
357 GC_min_no += GC_min_since_maj;
358 GC_min_since_maj = 0;
359 GC_tot_alloc += (ullong) GC_alloc_since_maj;
360 GC_alloc_since_maj = 0;
361 GC_tot_time += time-GC_start_time + GC_min_time;
363 GCe_tot_time += etime-GCe_start_time + GCe_min_time;
367 GC_alloc_since_maj += alloc;
368 GC_min_time += time-GC_start_time;
369 GCe_min_time += etime-GCe_start_time;
371 #else /* ! generational */
373 GC_tot_alloc += (ullong) alloc;
374 GC_tot_time += time-GC_start_time;
375 GCe_tot_time += etime-GCe_start_time;
376 #endif /* ! generational */
381 fprintf(stderr, "\b\b\b \b\b\b");
386 /* Called at the end of execution -- to print a summary of statistics */
391 FILE *sf = RTSflags.GcFlags.statsFile;
394 char temp[BIG_STRING_LEN];
395 StgDouble time = usertime();
396 StgDouble etime = elapsedtime();
398 if (RTSflags.GcFlags.giveStats) {
399 fprintf(sf, "%8ld\n\n", alloc*sizeof(W_));
402 #if defined(GCap) || defined (GCgn)
404 fprintf(sf, "%8ld %7.7s %6.6s %7.7s %6.6s",
405 (GC_alloc_since_maj + alloc)*sizeof(W_), "", "", "", "");
406 fprintf(sf, " %3ld %5.2f %5.2f\n\n",
407 GC_min_since_maj, GC_min_time, GCe_min_time);
409 GC_min_no += GC_min_since_maj;
410 GC_tot_time += GC_min_time;
411 GCe_tot_time += GCe_min_time;
412 GC_tot_alloc += (ullong) (GC_alloc_since_maj + alloc);
413 ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
414 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
415 if ( ResidencySamples > 0 ) {
416 ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
417 fprintf(sf, "%11s bytes maximum residency (%.1f%%, %ld sample(s))\n",
419 MaxResidency / (StgDouble) RTSflags.GcFlags.heapSize * 100,
422 fprintf(sf, "%11ld garbage collections performed (%ld major, %ld minor)\n\n",
423 GC_maj_no + GC_min_no, GC_maj_no, GC_min_no);
425 #else /* ! generational */
427 GC_tot_alloc += (ullong) alloc;
428 ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
429 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
430 if ( ResidencySamples > 0 ) {
431 ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
432 fprintf(sf, "%11s bytes maximum residency (%.1f%%, %ld sample(s))\n",
434 MaxResidency / (StgDouble) RTSflags.GcFlags.heapSize * 100,
437 fprintf(sf, "%11ld garbage collections performed\n\n", GC_maj_no);
439 #endif /* ! generational */
441 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
442 InitUserTime, InitElapsedTime);
443 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
444 time - GC_tot_time - InitUserTime,
445 etime - GCe_tot_time - InitElapsedTime);
446 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
447 GC_tot_time, GCe_tot_time);
448 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
451 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
452 GC_tot_time*100./time, GCe_tot_time*100./etime);
454 ullong_format_string((ullong)(GC_tot_alloc*sizeof(W_)/(time - GC_tot_time)), temp, rtsTrue/*commas*/);
455 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
457 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
458 (time - GC_tot_time - InitUserTime) * 100. / time,
459 (time - GC_tot_time - InitUserTime) * 100. / etime);