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 *********************************************************************
17 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
20 /* how is this to work given we have not read platform.h yet? */
22 #define _INCLUDE_HPUX_SOURCE
26 #include "SMinternal.h"
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
37 #ifdef HAVE_SYS_TIMES_H
38 #include <sys/times.h>
41 #ifdef HAVE_SYS_TIME_H
45 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
46 #include <sys/resource.h>
49 #ifdef HAVE_SYS_TIMEB_H
50 #include <sys/timeb.h>
54 #include <sys/syscall.h>
55 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
56 #define HAVE_GETRUSAGE
59 static StgDouble GC_start_time, GC_tot_time = 0; /* User GC Time */
60 static StgDouble GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
62 #if defined(GCap) || defined(GCgn)
63 static StgDouble GC_min_time = 0;
64 static StgDouble GCe_min_time = 0;
66 static I_ GC_min_no = 0;
67 static I_ GC_min_since_maj = 0;
68 static I_ GC_live_maj = 0; /* Heap live at last major collection */
69 static I_ GC_alloc_since_maj = 0; /* Heap alloc since collection major */
72 static I_ GC_maj_no = 0;
73 static ullong GC_tot_alloc = 0; /* Total heap allocated -- 64 bits? */
75 static I_ GC_start_faults = 0, GC_end_faults = 0;
78 ullong_format_string(ullong x, char *s, rtsBool with_commas)
81 sprintf(s, "%ld", (I_)x);
82 else if (x < (ullong)1000000)
83 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
84 (I_)((x)/(ullong)1000),
85 (I_)((x)%(ullong)1000));
86 else if (x < (ullong)1000000000)
87 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
88 (I_)((x)/(ullong)1000000),
89 (I_)((x)/(ullong)1000%(ullong)1000),
90 (I_)((x)%(ullong)1000));
92 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
93 (I_)((x)/(ullong)1000000000),
94 (I_)((x)/(ullong)1000000%(ullong)1000),
95 (I_)((x)/(ullong)1000%(ullong)1000),
96 (I_)((x)%(ullong)1000));
100 /* "constants" for "usertime" and "elapsedtime" */
102 static StgDouble ElapsedTimeStart = 0.0; /* setup when beginning things */
103 static StgDouble TicksPerSecond = 0.0; /* ditto */
105 /* usertime() -- The current user time in seconds */
110 #if ! (defined(HAVE_GETRUSAGE) || defined(HAVE_TIMES))
111 /* We will #ifdef around the fprintf for machines
112 we *know* are unsupported. (WDP 94/05)
114 fprintf(stderr, "NOTE: `usertime' does nothing!\n");
117 #else /* not stumped */
119 /* "times" is the more standard, but we prefer "getrusage"
120 (because we are old worn-out BSD hackers)
122 # if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
125 getrusage(RUSAGE_SELF, &t);
126 return(t.ru_utime.tv_sec + 1e-6*t.ru_utime.tv_usec);
128 # else /* HAVE_TIMES */
132 return(((StgDouble)(t.tms_utime))/TicksPerSecond);
134 # endif /* HAVE_TIMES */
135 #endif /* not stumped */
139 /* elapsedtime() -- The current elapsed time in seconds */
144 #if ! (defined(HAVE_TIMES) || defined(HAVE_FTIME))
145 /* We will #ifdef around the fprintf for machines
146 we *know* are unsupported. (WDP 94/05)
148 fprintf(stderr, "NOTE: `elapsedtime' does nothing!\n");
151 #else /* not stumped */
153 /* "ftime" may be nicer, but "times" is more standard;
154 but, on a Sun, if you do not get the SysV one, you are *hosed*...
157 # if defined(HAVE_TIMES) && ! sunos4_TARGET_OS
160 return (((StgDouble) times(&t))/TicksPerSecond - ElapsedTimeStart);
162 # else /* HAVE_FTIME */
166 return t.time + 1e-3*t.millitm - ElapsedTimeStart;
168 # endif /* HAVE_FTIME */
169 #endif /* not stumped */
173 start_time(STG_NO_ARGS)
177 /* Determine TicksPerSecond ... */
179 ticks = sysconf(_SC_CLK_TCK);
181 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
184 TicksPerSecond = (StgDouble) ticks;
186 #else /* no "sysconf"; had better guess */
188 TicksPerSecond = (StgDouble) (HZ);
190 # else /* had better guess wildly */
191 /* We will #ifdef around the fprintf for machines
192 we *know* are unsupported. (WDP 94/05)
194 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
195 TicksPerSecond = 60.0;
199 ElapsedTimeStart = elapsedtime();
202 static StgDouble InitUserTime = 0.0; /* user time taken for initialization */
203 static StgDouble InitElapsedTime = 0.0; /* elapsed time taken for initialization */
205 void end_init(STG_NO_ARGS)
207 InitUserTime = usertime();
208 InitElapsedTime = elapsedtime();
212 pagefaults(STG_NO_ARGS)
214 #if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || cygwin32_TARGET_OS
219 getrusage(RUSAGE_SELF, &t);
224 /* Called at the beginning of execution of the program */
225 /* Writes the command line and inits stats header */
228 stat_init(char *collector, char *comment1, char *comment2)
230 FILE *sf = RTSflags.GcFlags.statsFile;
233 char temp[BIG_STRING_LEN];
234 ullong_format_string( (ullong)RTSflags.GcFlags.heapSize*sizeof(W_), temp, rtsTrue/*commas*/);
235 fprintf(sf, "\nCollector: %s HeapSize: %s (bytes)\n\n", collector, temp);
236 if (RTSflags.GcFlags.giveStats) {
237 #if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS
238 fprintf(sf, "NOTE: `pagefaults' does nothing!\n");
241 /*######## ####### ####### ##.# ##.## ##.## ####.## ####.## #### ####*/
242 " Alloc Collect Live Resid GC GC TOT TOT Page Flts %s\n",
245 " bytes bytes bytes ency user elap user elap GC MUT %s\n",
249 #if defined(GCap) || defined(GCgn)
252 /*######## ####### ##.# ####### ##.# ### ##.## ##.## ##.## ##.## ####.## ####.## #### ####*/
253 " Alloc Promote Promo Live Resid Minor Minor Minor Major Major TOT TOT Page Flts\n");
255 " bytes bytes ted bytes ency No user elap user elap user elap MUT Major\n");
257 #endif /* generational */
263 /* Called at the beginning of each GC */
264 static I_ rub_bell = 0;
267 stat_startGC(I_ alloc)
269 FILE *sf = RTSflags.GcFlags.statsFile;
271 #if defined(GCap) || defined(GCgn)
272 I_ bell = alloc == 0 ? RTSflags.GcFlags.ringBell : 0;
273 #else /* ! generational */
274 I_ bell = RTSflags.GcFlags.ringBell;
275 #endif /* ! generational */
279 fprintf(stderr, " GC ");
282 fprintf(stderr, "\007");
287 GC_start_time = usertime();
288 GCe_start_time = elapsedtime();
290 #if defined(GCap) || defined(GCgn)
291 if (RTSflags.GcFlags.giveStats || alloc == 0) {
292 GC_start_faults = pagefaults();
294 #else /* ! generational */
295 if (RTSflags.GcFlags.giveStats) {
296 GC_start_faults = pagefaults();
298 #endif /* ! generational */
303 /* Called at the end of each GC */
306 stat_endGC(I_ alloc, I_ collect, I_ live, char *comment)
308 FILE *sf = RTSflags.GcFlags.statsFile;
311 StgDouble time = usertime();
312 StgDouble etime = elapsedtime();
314 if (RTSflags.GcFlags.giveStats) {
315 I_ faults = pagefaults();
317 fprintf(sf, "%8ld %7ld %7ld %5.1f%%",
318 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_), collect == 0 ? 0.0 : (live / (StgDouble) collect * 100));
319 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld %s\n",
320 (time-GC_start_time),
321 (etime-GCe_start_time),
324 faults - GC_start_faults,
325 GC_start_faults - GC_end_faults,
328 GC_end_faults = faults;
332 #if defined(GCap) || defined(GCgn)
333 else if(alloc == 0 && collect != 0) {
334 I_ faults = pagefaults();
336 fprintf(sf, "%8ld %7ld %5.1f%% %7ld %5.1f%%",
337 GC_alloc_since_maj*sizeof(W_), (collect - GC_live_maj)*sizeof(W_),
338 (collect - GC_live_maj) / (StgDouble) GC_alloc_since_maj * 100,
339 live*sizeof(W_), live / (StgDouble) RTSflags.GcFlags.heapSize * 100);
340 fprintf(sf, " %3ld %5.2f %5.2f %5.2f %5.2f %7.2f %7.2f %4ld %4ld\n",
341 GC_min_since_maj, GC_min_time, GCe_min_time,
342 (time-GC_start_time),
343 (etime-GCe_start_time),
346 faults - GC_start_faults,
347 GC_start_faults - GC_end_faults
350 GC_end_faults = faults;
353 #endif /* generational */
355 #if defined(GCap) || defined(GCgn)
356 if (alloc == 0 && collect != 0) {
359 GC_min_no += GC_min_since_maj;
360 GC_min_since_maj = 0;
361 GC_tot_alloc += (ullong) GC_alloc_since_maj;
362 GC_alloc_since_maj = 0;
363 GC_tot_time += time-GC_start_time + GC_min_time;
365 GCe_tot_time += etime-GCe_start_time + GCe_min_time;
369 GC_alloc_since_maj += alloc;
370 GC_min_time += time-GC_start_time;
371 GCe_min_time += etime-GCe_start_time;
373 #else /* ! generational */
375 GC_tot_alloc += (ullong) alloc;
376 GC_tot_time += time-GC_start_time;
377 GCe_tot_time += etime-GCe_start_time;
378 #endif /* ! generational */
383 fprintf(stderr, "\b\b\b \b\b\b");
388 /* Called at the end of execution -- to print a summary of statistics */
393 FILE *sf = RTSflags.GcFlags.statsFile;
396 char temp[BIG_STRING_LEN];
397 StgDouble time = usertime();
398 StgDouble etime = elapsedtime();
400 if (RTSflags.GcFlags.giveStats) {
401 fprintf(sf, "%8ld\n\n", alloc*sizeof(W_));
404 #if defined(GCap) || defined (GCgn)
406 fprintf(sf, "%8ld %7.7s %6.6s %7.7s %6.6s",
407 (GC_alloc_since_maj + alloc)*sizeof(W_), "", "", "", "");
408 fprintf(sf, " %3ld %5.2f %5.2f\n\n",
409 GC_min_since_maj, GC_min_time, GCe_min_time);
411 GC_min_no += GC_min_since_maj;
412 GC_tot_time += GC_min_time;
413 GCe_tot_time += GCe_min_time;
414 GC_tot_alloc += (ullong) (GC_alloc_since_maj + alloc);
415 ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
416 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
417 if ( ResidencySamples > 0 ) {
418 ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
419 fprintf(sf, "%11s bytes maximum residency (%.1f%%, %ld sample(s))\n",
421 MaxResidency / (StgDouble) RTSflags.GcFlags.heapSize * 100,
424 fprintf(sf, "%11ld garbage collections performed (%ld major, %ld minor)\n\n",
425 GC_maj_no + GC_min_no, GC_maj_no, GC_min_no);
427 #else /* ! generational */
429 GC_tot_alloc += (ullong) alloc;
430 ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
431 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
432 if ( ResidencySamples > 0 ) {
433 ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
434 fprintf(sf, "%11s bytes maximum residency (%.1f%%, %ld sample(s))\n",
436 MaxResidency / (StgDouble) RTSflags.GcFlags.heapSize * 100,
439 fprintf(sf, "%11ld garbage collections performed\n\n", GC_maj_no);
441 #endif /* ! generational */
443 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
444 InitUserTime, InitElapsedTime);
445 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
446 time - GC_tot_time - InitUserTime,
447 etime - GCe_tot_time - InitElapsedTime);
448 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
449 GC_tot_time, GCe_tot_time);
450 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
453 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
454 GC_tot_time*100./time, GCe_tot_time*100./etime);
456 ullong_format_string((ullong)(GC_tot_alloc*sizeof(W_)/(time - GC_tot_time)), temp, rtsTrue/*commas*/);
457 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
459 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
460 (time - GC_tot_time - InitUserTime) * 100. / time,
461 (time - GC_tot_time - InitUserTime) * 100. / etime);