calculate and report slop (wasted space at the end of blocks)
[ghc-hetmet.git] / rts / Stats.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2005
4  *
5  * Statistics and timing-related functions.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #include "Rts.h"
10 #include "RtsFlags.h"
11 #include "RtsUtils.h"
12 #include "MBlock.h"
13 #include "Storage.h"
14 #include "Schedule.h"
15 #include "Stats.h"
16 #include "ParTicky.h"                       /* ToDo: move into Rts.h */
17 #include "Profiling.h"
18 #include "GetTime.h"
19 #include "GC.h"
20 #include "GCUtils.h"
21 #include "Evac.h"
22
23 #if USE_PAPI
24 #include "Papi.h"
25 #endif
26
27 /* huh? */
28 #define BIG_STRING_LEN              512
29
30 #define TICK_TO_DBL(t) ((double)(t) / TICKS_PER_SECOND)
31
32 static Ticks ElapsedTimeStart = 0;
33
34 static Ticks InitUserTime     = 0;
35 static Ticks InitElapsedTime  = 0;
36 static Ticks InitElapsedStamp = 0;
37
38 static Ticks MutUserTime      = 0;
39 static Ticks MutElapsedTime   = 0;
40 static Ticks MutElapsedStamp  = 0;
41
42 static Ticks ExitUserTime     = 0;
43 static Ticks ExitElapsedTime  = 0;
44
45 static ullong GC_tot_alloc        = 0;
46 static ullong GC_tot_copied       = 0;
47
48 static ullong GC_par_max_copied = 0;
49 static ullong GC_par_avg_copied = 0;
50
51 static Ticks GC_start_time = 0,  GC_tot_time  = 0;  /* User GC Time */
52 static Ticks GCe_start_time = 0, GCe_tot_time = 0;  /* Elapsed GC time */
53
54 #ifdef PROFILING
55 static Ticks RP_start_time  = 0, RP_tot_time  = 0;  /* retainer prof user time */
56 static Ticks RPe_start_time = 0, RPe_tot_time = 0;  /* retainer prof elap time */
57
58 static Ticks HC_start_time, HC_tot_time = 0;     // heap census prof user time
59 static Ticks HCe_start_time, HCe_tot_time = 0;   // heap census prof elap time
60 #endif
61
62 #ifdef PROFILING
63 #define PROF_VAL(x)   (x)
64 #else
65 #define PROF_VAL(x)   0
66 #endif
67
68 static lnat MaxResidency = 0;     // in words; for stats only
69 static lnat AvgResidency = 0;
70 static lnat ResidencySamples = 0; // for stats only
71 static lnat MaxSlop = 0;
72
73 static lnat GC_start_faults = 0, GC_end_faults = 0;
74
75 static Ticks *GC_coll_times = NULL;
76 static Ticks *GC_coll_etimes = NULL;
77
78 static void statsFlush( void );
79 static void statsClose( void );
80
81 Ticks stat_getElapsedGCTime(void)
82 {
83     return GCe_tot_time;
84 }
85
86 Ticks stat_getElapsedTime(void)
87 {
88     return getProcessElapsedTime() - ElapsedTimeStart;
89 }
90
91 /* mut_user_time_during_GC() and mut_user_time()
92  *
93  * The former function can be used to get the current mutator time
94  * *during* a GC, i.e. between stat_startGC and stat_endGC.  This is
95  * used in the heap profiler for accurately time stamping the heap
96  * sample.  
97  *
98  * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being 
99  *            defined in stat_startGC() - to minimise system calls, 
100  *            GC_start_time is, however, only defined when really needed (check
101  *            stat_startGC() for details)
102  */
103 double
104 mut_user_time_during_GC( void )
105 {
106   return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
107 }
108
109 double
110 mut_user_time( void )
111 {
112     Ticks user;
113     user = getProcessCPUTime();
114     return TICK_TO_DBL(user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
115 }
116
117 #ifdef PROFILING
118 /*
119   mut_user_time_during_RP() is similar to mut_user_time_during_GC();
120   it returns the MUT time during retainer profiling.
121   The same is for mut_user_time_during_HC();
122  */
123 double
124 mut_user_time_during_RP( void )
125 {
126   return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
127 }
128
129 double
130 mut_user_time_during_heap_census( void )
131 {
132   return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
133 }
134 #endif /* PROFILING */
135
136 // initStats0() has no dependencies, it can be called right at the beginning
137 void
138 initStats0(void)
139 {
140     ElapsedTimeStart = 0;
141
142     InitUserTime     = 0;
143     InitElapsedTime  = 0;
144     InitElapsedStamp = 0;
145
146     MutUserTime      = 0;
147     MutElapsedTime   = 0;
148     MutElapsedStamp  = 0;
149
150     ExitUserTime     = 0;
151     ExitElapsedTime  = 0;
152
153     GC_tot_alloc     = 0;
154     GC_tot_copied    = 0;
155     GC_par_max_copied = 0;
156     GC_par_avg_copied = 0;
157     GC_start_time = 0;
158     GC_tot_time  = 0;
159     GCe_start_time = 0;
160     GCe_tot_time = 0;
161
162 #ifdef PROFILING
163     RP_start_time  = 0;
164     RP_tot_time  = 0;
165     RPe_start_time = 0;
166     RPe_tot_time = 0;
167
168     HC_start_time = 0;
169     HC_tot_time = 0;
170     HCe_start_time = 0;
171     HCe_tot_time = 0;
172 #endif
173
174     MaxResidency = 0;
175     AvgResidency = 0;
176     ResidencySamples = 0;
177     MaxSlop = 0;
178
179     GC_start_faults = 0;
180     GC_end_faults = 0;
181 }    
182
183 // initStats1() can be called after setupRtsFlags()
184 void
185 initStats1 (void)
186 {
187     nat i;
188   
189     if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
190         statsPrintf("    Alloc    Copied     Live    GC    GC     TOT     TOT  Page Flts\n");
191         statsPrintf("    bytes     bytes     bytes  user  elap    user    elap\n");
192     }
193     GC_coll_times = 
194         (Ticks *)stgMallocBytes(
195             sizeof(Ticks)*RtsFlags.GcFlags.generations,
196             "initStats");
197     GC_coll_etimes = 
198         (Ticks *)stgMallocBytes(
199             sizeof(Ticks)*RtsFlags.GcFlags.generations,
200             "initStats");
201     for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
202         GC_coll_times[i] = 0;
203         GC_coll_etimes[i] = 0;
204     }
205 }
206
207 /* -----------------------------------------------------------------------------
208    Initialisation time...
209    -------------------------------------------------------------------------- */
210
211 void
212 stat_startInit(void)
213 {
214     Ticks elapsed;
215
216     elapsed = getProcessElapsedTime();
217     ElapsedTimeStart = elapsed;
218 }
219
220 void 
221 stat_endInit(void)
222 {
223     Ticks user, elapsed;
224
225     getProcessTimes(&user, &elapsed);
226
227     InitUserTime = user;
228     InitElapsedStamp = elapsed; 
229     if (ElapsedTimeStart > elapsed) {
230         InitElapsedTime = 0;
231     } else {
232         InitElapsedTime = elapsed - ElapsedTimeStart;
233     }
234 #if USE_PAPI
235     /* We start counting events for the mutator
236      * when garbage collection starts
237      * we switch to the GC event set. */
238     papi_start_mutator_count();
239
240     /* This flag is needed to avoid counting the last GC */
241     papi_is_reporting = 1;
242
243 #endif
244 }
245
246 /* -----------------------------------------------------------------------------
247    stat_startExit and stat_endExit
248    
249    These two measure the time taken in shutdownHaskell().
250    -------------------------------------------------------------------------- */
251
252 void
253 stat_startExit(void)
254 {
255     Ticks user, elapsed;
256
257     getProcessTimes(&user, &elapsed);
258
259     MutElapsedStamp = elapsed;
260     MutElapsedTime = elapsed - GCe_tot_time -
261         PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
262     if (MutElapsedTime < 0) { MutElapsedTime = 0; }     /* sometimes -0.00 */
263
264     MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
265     if (MutUserTime < 0) { MutUserTime = 0; }
266
267 #if USE_PAPI
268     /* We stop counting mutator events
269      * GC events are not being counted at this point */
270     papi_stop_mutator_count();
271
272     /* This flag is needed, because GC is run once more after this function */
273     papi_is_reporting = 0;
274
275 #endif
276 }
277
278 void
279 stat_endExit(void)
280 {
281     Ticks user, elapsed;
282
283     getProcessTimes(&user, &elapsed);
284
285     ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
286     ExitElapsedTime = elapsed - MutElapsedStamp;
287     if (ExitUserTime < 0) {
288         ExitUserTime = 0;
289     }
290     if (ExitElapsedTime < 0) {
291         ExitElapsedTime = 0;
292     }
293 }
294
295 /* -----------------------------------------------------------------------------
296    Called at the beginning of each GC
297    -------------------------------------------------------------------------- */
298
299 static nat rub_bell = 0;
300
301 /*  initialise global variables needed during GC
302  *
303  *  * GC_start_time is read in mut_user_time_during_GC(), which in turn is 
304  *    needed if either PROFILING or DEBUGing is enabled
305  */
306 void
307 stat_startGC(void)
308 {
309     nat bell = RtsFlags.GcFlags.ringBell;
310
311     if (bell) {
312         if (bell > 1) {
313             debugBelch(" GC ");
314             rub_bell = 1;
315         } else {
316             debugBelch("\007");
317         }
318     }
319
320 #if defined(PROFILING) || defined(DEBUG)
321     GC_start_time = getProcessCPUTime();  // needed in mut_user_time_during_GC()
322 #endif
323
324     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
325 #if !defined(PROFILING) && !defined(DEBUG)
326         GC_start_time = getProcessCPUTime();
327 #endif
328         GCe_start_time = getProcessElapsedTime();
329         if (RtsFlags.GcFlags.giveStats) {
330             GC_start_faults = getPageFaults();
331         }
332     }
333
334 #if USE_PAPI
335     if(papi_is_reporting) {
336       /* Switch to counting GC events */
337       papi_stop_mutator_count();
338       papi_start_gc_count();
339     }
340 #endif
341
342 }
343
344 /* -----------------------------------------------------------------------------
345    Called at the end of each GC
346    -------------------------------------------------------------------------- */
347
348 void
349 stat_endGC (lnat alloc, lnat live, lnat copied, lnat gen,
350             lnat max_copied, lnat avg_copied, lnat slop)
351 {
352     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
353         Ticks time, etime, gc_time, gc_etime;
354         
355         getProcessTimes(&time, &etime);
356         gc_time  = time - GC_start_time;
357         gc_etime = etime - GCe_start_time;
358         
359         if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
360             nat faults = getPageFaults();
361             
362             statsPrintf("%9ld %9ld %9ld",
363                     alloc*sizeof(W_), copied*sizeof(W_), 
364                         live*sizeof(W_));
365             statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld  (Gen: %2ld)\n", 
366                     TICK_TO_DBL(gc_time),
367                     TICK_TO_DBL(gc_etime),
368                     TICK_TO_DBL(time),
369                     TICK_TO_DBL(etime - ElapsedTimeStart),
370                     faults - GC_start_faults,
371                     GC_start_faults - GC_end_faults,
372                     gen);
373
374             GC_end_faults = faults;
375             statsFlush();
376         }
377
378         GC_coll_times[gen] += gc_time;
379         GC_coll_etimes[gen] += gc_etime;
380
381         GC_tot_copied += (ullong) copied;
382         GC_tot_alloc  += (ullong) alloc;
383         GC_par_max_copied += (ullong) max_copied;
384         GC_par_avg_copied += (ullong) avg_copied;
385         GC_tot_time   += gc_time;
386         GCe_tot_time  += gc_etime;
387         
388 #if defined(THREADED_RTS)
389         {
390             Task *task;
391             if ((task = myTask()) != NULL) {
392                 task->gc_time += gc_time;
393                 task->gc_etime += gc_etime;
394             }
395         }
396 #endif
397
398         if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
399             if (live > MaxResidency) {
400                 MaxResidency = live;
401             }
402             ResidencySamples++;
403             AvgResidency += live;
404         }
405
406         if (slop > MaxSlop) MaxSlop = slop;
407     }
408
409     if (rub_bell) {
410         debugBelch("\b\b\b  \b\b\b");
411         rub_bell = 0;
412     }
413
414 #if USE_PAPI
415     if(papi_is_reporting) {
416       /* Switch to counting mutator events */
417       if (gen == 0) {
418           papi_stop_gc0_count();
419       } else {
420           papi_stop_gc1_count();
421       }
422       papi_start_mutator_count();
423     }
424 #endif
425 }
426
427 /* -----------------------------------------------------------------------------
428    Called at the beginning of each Retainer Profiliing
429    -------------------------------------------------------------------------- */
430 #ifdef PROFILING
431 void
432 stat_startRP(void)
433 {
434     Ticks user, elapsed;
435     getProcessTimes( &user, &elapsed );
436
437     RP_start_time = user;
438     RPe_start_time = elapsed;
439 }
440 #endif /* PROFILING */
441
442 /* -----------------------------------------------------------------------------
443    Called at the end of each Retainer Profiliing
444    -------------------------------------------------------------------------- */
445
446 #ifdef PROFILING
447 void
448 stat_endRP(
449   nat retainerGeneration,
450 #ifdef DEBUG_RETAINER
451   nat maxCStackSize,
452   int maxStackSize,
453 #endif
454   double averageNumVisit)
455 {
456     Ticks user, elapsed;
457     getProcessTimes( &user, &elapsed );
458
459     RP_tot_time += user - RP_start_time;
460     RPe_tot_time += elapsed - RPe_start_time;
461
462   fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n", 
463     retainerGeneration, mut_user_time_during_RP());
464 #ifdef DEBUG_RETAINER
465   fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
466   fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
467 #endif
468   fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
469 }
470 #endif /* PROFILING */
471
472 /* -----------------------------------------------------------------------------
473    Called at the beginning of each heap census
474    -------------------------------------------------------------------------- */
475 #ifdef PROFILING
476 void
477 stat_startHeapCensus(void)
478 {
479     Ticks user, elapsed;
480     getProcessTimes( &user, &elapsed );
481
482     HC_start_time = user;
483     HCe_start_time = elapsed;
484 }
485 #endif /* PROFILING */
486
487 /* -----------------------------------------------------------------------------
488    Called at the end of each heap census
489    -------------------------------------------------------------------------- */
490 #ifdef PROFILING
491 void
492 stat_endHeapCensus(void) 
493 {
494     Ticks user, elapsed;
495     getProcessTimes( &user, &elapsed );
496
497     HC_tot_time += user - HC_start_time;
498     HCe_tot_time += elapsed - HCe_start_time;
499 }
500 #endif /* PROFILING */
501
502 /* -----------------------------------------------------------------------------
503    Called at the end of execution
504
505    NOTE: number of allocations is not entirely accurate: it doesn't
506    take into account the few bytes at the end of the heap that
507    were left unused when the heap-check failed.
508    -------------------------------------------------------------------------- */
509
510 #ifdef DEBUG
511 #define TICK_VAR(arity) \
512   extern StgInt SLOW_CALLS_##arity; \
513   extern StgInt RIGHT_ARITY_##arity; \
514   extern StgInt TAGGED_PTR_##arity;
515
516 #define TICK_VAR_INI(arity) \
517   StgInt SLOW_CALLS_##arity = 1; \
518   StgInt RIGHT_ARITY_##arity = 1; \
519   StgInt TAGGED_PTR_##arity = 0;
520
521 extern StgInt TOTAL_CALLS;
522
523 TICK_VAR(1)
524 TICK_VAR(2)
525
526 TICK_VAR_INI(1)
527 TICK_VAR_INI(2)
528
529 StgInt TOTAL_CALLS=1;
530 #endif
531
532 /* Report the value of a counter */
533 #define REPORT(counter) \
534   { \
535     ullong_format_string(counter,temp,rtsTrue/*commas*/); \
536     statsPrintf("  (" #counter ")  : %s\n",temp);                               \
537   }
538
539 /* Report the value of a counter as a percentage of another counter */
540 #define REPORT_PCT(counter,countertot) \
541   statsPrintf("  (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
542               counter*100.0/countertot)
543
544 #define TICK_PRINT(arity) \
545   REPORT(SLOW_CALLS_##arity); \
546   REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
547   REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
548   REPORT(RIGHT_ARITY_##arity); \
549   REPORT(TAGGED_PTR_##arity)
550
551 #define TICK_PRINT_TOT(arity) \
552   statsPrintf("  (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
553               SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
554
555
556 void
557 stat_exit(int alloc)
558 {
559     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
560
561         char temp[BIG_STRING_LEN];
562         Ticks time;
563         Ticks etime;
564         nat g, total_collections = 0;
565
566         getProcessTimes( &time, &etime );
567         etime -= ElapsedTimeStart;
568
569         GC_tot_alloc += alloc;
570
571         /* Count total garbage collections */
572         for (g = 0; g < RtsFlags.GcFlags.generations; g++)
573             total_collections += generations[g].collections;
574
575         /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
576         if (time  == 0.0)  time = 1;
577         if (etime == 0.0) etime = 1;
578         
579         if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
580             statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
581             statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
582         }
583
584         if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
585             ullong_format_string(GC_tot_alloc*sizeof(W_), 
586                                  temp, rtsTrue/*commas*/);
587             statsPrintf("%16s bytes allocated in the heap\n", temp);
588
589             ullong_format_string(GC_tot_copied*sizeof(W_), 
590                                  temp, rtsTrue/*commas*/);
591             statsPrintf("%16s bytes copied during GC\n", temp);
592
593             if ( ResidencySamples > 0 ) {
594                 ullong_format_string(MaxResidency*sizeof(W_), 
595                                      temp, rtsTrue/*commas*/);
596                 statsPrintf("%16s bytes maximum residency (%ld sample(s))\n",
597                         temp, ResidencySamples);
598             }
599
600             ullong_format_string(MaxSlop*sizeof(W_), temp, rtsTrue/*commas*/);
601             statsPrintf("%16s bytes maximum slop\n", temp);
602
603             statsPrintf("%16ld MB total memory in use\n\n", 
604                     mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
605
606             /* Print garbage collections in each gen */
607             for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
608                 statsPrintf("  Generation %d: %5d collections, %5d parallel, %5.2fs, %5.2fs elapsed\n", 
609                             g, generations[g].collections, 
610                             generations[g].par_collections,
611                         TICK_TO_DBL(GC_coll_times[g]),
612                         TICK_TO_DBL(GC_coll_etimes[g]));
613             }
614
615 #if defined(THREADED_RTS)
616             if (RtsFlags.ParFlags.gcThreads > 1) {
617                 statsPrintf("\n  Parallel GC work balance: %.2f (%ld / %ld, ideal %d)\n", 
618                             (double)GC_par_avg_copied / (double)GC_par_max_copied,
619                             (lnat)GC_par_avg_copied, (lnat)GC_par_max_copied,
620                             RtsFlags.ParFlags.gcThreads
621                     );
622             }
623 #endif
624
625             statsPrintf("\n");
626
627 #if defined(THREADED_RTS)
628             {
629                 nat i;
630                 Task *task;
631                 for (i = 0, task = all_tasks; 
632                      task != NULL; 
633                      i++, task = task->all_link) {
634                     statsPrintf("  Task %2d %-8s :  MUT time: %6.2fs  (%6.2fs elapsed)\n"
635                             "                      GC  time: %6.2fs  (%6.2fs elapsed)\n\n", 
636                                 i,
637                                 (task->tso == NULL) ? "(worker)" : "(bound)",
638                                 TICK_TO_DBL(task->mut_time),
639                                 TICK_TO_DBL(task->mut_etime),
640                                 TICK_TO_DBL(task->gc_time),
641                                 TICK_TO_DBL(task->gc_etime));
642                 }
643             }
644 #endif
645
646             statsPrintf("  INIT  time  %6.2fs  (%6.2fs elapsed)\n",
647                     TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
648             statsPrintf("  MUT   time  %6.2fs  (%6.2fs elapsed)\n",
649                     TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
650             statsPrintf("  GC    time  %6.2fs  (%6.2fs elapsed)\n",
651                     TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
652 #ifdef PROFILING
653             statsPrintf("  RP    time  %6.2fs  (%6.2fs elapsed)\n",
654                     TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
655             statsPrintf("  PROF  time  %6.2fs  (%6.2fs elapsed)\n",
656                     TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
657 #endif 
658             statsPrintf("  EXIT  time  %6.2fs  (%6.2fs elapsed)\n",
659                     TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
660             statsPrintf("  Total time  %6.2fs  (%6.2fs elapsed)\n\n",
661                     TICK_TO_DBL(time), TICK_TO_DBL(etime));
662             statsPrintf("  %%GC time     %5.1f%%  (%.1f%% elapsed)\n\n",
663                     TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
664                     TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
665
666             if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
667                 ullong_format_string(0, temp, rtsTrue/*commas*/);
668             else
669                 ullong_format_string(
670                     (ullong)((GC_tot_alloc*sizeof(W_))/
671                              TICK_TO_DBL(time - GC_tot_time - 
672                                          PROF_VAL(RP_tot_time + HC_tot_time))),
673                     temp, rtsTrue/*commas*/);
674             
675             statsPrintf("  Alloc rate    %s bytes per MUT second\n\n", temp);
676         
677             statsPrintf("  Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
678                     TICK_TO_DBL(time - GC_tot_time - 
679                                 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100 
680                     / TICK_TO_DBL(time), 
681                     TICK_TO_DBL(time - GC_tot_time - 
682                                 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100 
683                     / TICK_TO_DBL(etime));
684
685             /*
686             TICK_PRINT(1);
687             TICK_PRINT(2);
688             REPORT(TOTAL_CALLS);
689             TICK_PRINT_TOT(1);
690             TICK_PRINT_TOT(2);
691             */
692
693 #if USE_PAPI
694             papi_stats_report();
695 #endif
696         }
697
698         if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
699           /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
700           statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
701           statsPrintf("%d GCs, %ld/%ld avg/max bytes residency (%ld samples), %luM in use, %.2f INIT (%.2f elapsed), %.2f MUT (%.2f elapsed), %.2f GC (%.2f elapsed) :ghc>>\n",
702                     total_collections,
703                     ResidencySamples == 0 ? 0 : 
704                         AvgResidency*sizeof(W_)/ResidencySamples, 
705                     MaxResidency*sizeof(W_), 
706                     ResidencySamples,
707                     (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
708                     TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
709                     TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
710                     TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
711         }
712
713 #if defined(THREADED_RTS) && defined(PROF_SPIN)
714         {
715             nat g, s;
716             
717             statsPrintf("recordMutableGen_sync: %"FMT_Word64"\n", recordMutableGen_sync.spin);
718             statsPrintf("gc_alloc_block_sync: %"FMT_Word64"\n", gc_alloc_block_sync.spin);
719             statsPrintf("whitehole_spin: %"FMT_Word64"\n", whitehole_spin);
720             for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
721                 for (s = 0; s < generations[g].n_steps; s++) {
722                     statsPrintf("gen[%d].steps[%d].sync_todo: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_todo.spin);
723                     statsPrintf("gen[%d].steps[%d].sync_large_objects: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_large_objects.spin);
724                 }
725             }
726         }
727 #endif
728
729         statsFlush();
730         statsClose();
731     }
732
733     if (GC_coll_times)
734       stgFree(GC_coll_times);
735     GC_coll_times = NULL;
736     if (GC_coll_etimes)
737       stgFree(GC_coll_etimes);
738     GC_coll_etimes = NULL;
739 }
740
741 /* -----------------------------------------------------------------------------
742    stat_describe_gens
743
744    Produce some detailed info on the state of the generational GC.
745    -------------------------------------------------------------------------- */
746 void
747 statDescribeGens(void)
748 {
749   nat g, s, mut, lge;
750   lnat live, slop;
751   lnat tot_live, tot_slop;
752   bdescr *bd;
753   step *step;
754
755   debugBelch(
756 "-----------------------------------------------------------------\n"
757 "  Gen     Max  Mut-list  Step   Blocks    Large     Live     Slop\n"
758 "       Blocks     Bytes                 Objects                  \n"
759 "-----------------------------------------------------------------\n");
760
761   tot_live = 0;
762   tot_slop = 0;
763   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
764       mut = 0;
765       for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
766           mut += (bd->free - bd->start) * sizeof(W_);
767       }
768
769     debugBelch("%5d %7d %9d", g, generations[g].max_blocks, mut);
770
771     for (s = 0; s < generations[g].n_steps; s++) {
772       step = &generations[g].steps[s];
773       for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
774         lge++;
775       }
776       live = step->n_words + countOccupied(step->large_objects);
777       if (s != 0) {
778         debugBelch("%23s","");
779       }
780       slop = (step->n_blocks + step->n_large_blocks) * BLOCK_SIZE_W - live;
781       debugBelch("%6d %8d %8d %8ld %8ld\n", s, step->n_blocks, lge,
782                  live*sizeof(W_), slop*sizeof(W_));
783       tot_live += live;
784       tot_slop += slop;
785     }
786   }
787   debugBelch("-----------------------------------------------------------------\n");
788   debugBelch("%48s%8ld %8ld\n","",tot_live*sizeof(W_),tot_slop*sizeof(W_));
789   debugBelch("-----------------------------------------------------------------\n");
790   debugBelch("\n");
791 }
792
793 /* -----------------------------------------------------------------------------
794    Stats available via a programmatic interface, so eg. GHCi can time
795    each compilation and expression evaluation.
796    -------------------------------------------------------------------------- */
797
798 extern HsInt64 getAllocations( void ) 
799 { return (HsInt64)total_allocated * sizeof(W_); }
800
801 /* -----------------------------------------------------------------------------
802    Dumping stuff in the stats file, or via the debug message interface
803    -------------------------------------------------------------------------- */
804
805 void
806 statsPrintf( char *s, ... )
807 {
808     FILE *sf = RtsFlags.GcFlags.statsFile;
809     va_list ap;
810     
811     va_start(ap,s);
812     if (sf == NULL) {
813         vdebugBelch(s,ap);
814     } else {
815         vfprintf(sf, s, ap);
816     }
817     va_end(ap);
818 }
819
820 static void
821 statsFlush( void )
822 {
823     FILE *sf = RtsFlags.GcFlags.statsFile;
824     if (sf != NULL) {
825         fflush(sf);
826     }
827 }
828
829 static void
830 statsClose( void )
831 {
832     FILE *sf = RtsFlags.GcFlags.statsFile;
833     if (sf != NULL) {
834         fclose(sf);
835     }
836 }