faster block allocator, by dividing the free list into buckets
[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 extern lnat hw_alloc_blocks;
556
557 void
558 stat_exit(int alloc)
559 {
560     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
561
562         char temp[BIG_STRING_LEN];
563         Ticks time;
564         Ticks etime;
565         nat g, total_collections = 0;
566
567         getProcessTimes( &time, &etime );
568         etime -= ElapsedTimeStart;
569
570         GC_tot_alloc += alloc;
571
572         /* Count total garbage collections */
573         for (g = 0; g < RtsFlags.GcFlags.generations; g++)
574             total_collections += generations[g].collections;
575
576         /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
577         if (time  == 0.0)  time = 1;
578         if (etime == 0.0) etime = 1;
579         
580         if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
581             statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
582             statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
583         }
584
585         if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
586             ullong_format_string(GC_tot_alloc*sizeof(W_), 
587                                  temp, rtsTrue/*commas*/);
588             statsPrintf("%16s bytes allocated in the heap\n", temp);
589
590             ullong_format_string(GC_tot_copied*sizeof(W_), 
591                                  temp, rtsTrue/*commas*/);
592             statsPrintf("%16s bytes copied during GC\n", temp);
593
594             if ( ResidencySamples > 0 ) {
595                 ullong_format_string(MaxResidency*sizeof(W_), 
596                                      temp, rtsTrue/*commas*/);
597                 statsPrintf("%16s bytes maximum residency (%ld sample(s))\n",
598                         temp, ResidencySamples);
599             }
600
601             ullong_format_string(MaxSlop*sizeof(W_), temp, rtsTrue/*commas*/);
602             statsPrintf("%16s bytes maximum slop\n", temp);
603
604             statsPrintf("%16ld MB total memory in use (%ld MB lost due to fragmentation)\n\n", 
605                         mblocks_allocated * MBLOCK_SIZE_W / (1024 * 1024 / sizeof(W_)),
606                         (mblocks_allocated * MBLOCK_SIZE_W - hw_alloc_blocks * BLOCK_SIZE_W) / (1024 * 1024 / sizeof(W_)));
607
608             /* Print garbage collections in each gen */
609             for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
610                 statsPrintf("  Generation %d: %5d collections, %5d parallel, %5.2fs, %5.2fs elapsed\n", 
611                             g, generations[g].collections, 
612                             generations[g].par_collections,
613                         TICK_TO_DBL(GC_coll_times[g]),
614                         TICK_TO_DBL(GC_coll_etimes[g]));
615             }
616
617 #if defined(THREADED_RTS)
618             if (RtsFlags.ParFlags.gcThreads > 1) {
619                 statsPrintf("\n  Parallel GC work balance: %.2f (%ld / %ld, ideal %d)\n", 
620                             (double)GC_par_avg_copied / (double)GC_par_max_copied,
621                             (lnat)GC_par_avg_copied, (lnat)GC_par_max_copied,
622                             RtsFlags.ParFlags.gcThreads
623                     );
624             }
625 #endif
626
627             statsPrintf("\n");
628
629 #if defined(THREADED_RTS)
630             {
631                 nat i;
632                 Task *task;
633                 for (i = 0, task = all_tasks; 
634                      task != NULL; 
635                      i++, task = task->all_link) {
636                     statsPrintf("  Task %2d %-8s :  MUT time: %6.2fs  (%6.2fs elapsed)\n"
637                             "                      GC  time: %6.2fs  (%6.2fs elapsed)\n\n", 
638                                 i,
639                                 (task->tso == NULL) ? "(worker)" : "(bound)",
640                                 TICK_TO_DBL(task->mut_time),
641                                 TICK_TO_DBL(task->mut_etime),
642                                 TICK_TO_DBL(task->gc_time),
643                                 TICK_TO_DBL(task->gc_etime));
644                 }
645             }
646 #endif
647
648             statsPrintf("  INIT  time  %6.2fs  (%6.2fs elapsed)\n",
649                     TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
650             statsPrintf("  MUT   time  %6.2fs  (%6.2fs elapsed)\n",
651                     TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
652             statsPrintf("  GC    time  %6.2fs  (%6.2fs elapsed)\n",
653                     TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
654 #ifdef PROFILING
655             statsPrintf("  RP    time  %6.2fs  (%6.2fs elapsed)\n",
656                     TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
657             statsPrintf("  PROF  time  %6.2fs  (%6.2fs elapsed)\n",
658                     TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
659 #endif 
660             statsPrintf("  EXIT  time  %6.2fs  (%6.2fs elapsed)\n",
661                     TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
662             statsPrintf("  Total time  %6.2fs  (%6.2fs elapsed)\n\n",
663                     TICK_TO_DBL(time), TICK_TO_DBL(etime));
664             statsPrintf("  %%GC time     %5.1f%%  (%.1f%% elapsed)\n\n",
665                     TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
666                     TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
667
668             if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
669                 ullong_format_string(0, temp, rtsTrue/*commas*/);
670             else
671                 ullong_format_string(
672                     (ullong)((GC_tot_alloc*sizeof(W_))/
673                              TICK_TO_DBL(time - GC_tot_time - 
674                                          PROF_VAL(RP_tot_time + HC_tot_time))),
675                     temp, rtsTrue/*commas*/);
676             
677             statsPrintf("  Alloc rate    %s bytes per MUT second\n\n", temp);
678         
679             statsPrintf("  Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
680                     TICK_TO_DBL(time - GC_tot_time - 
681                                 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100 
682                     / TICK_TO_DBL(time), 
683                     TICK_TO_DBL(time - GC_tot_time - 
684                                 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100 
685                     / TICK_TO_DBL(etime));
686
687             /*
688             TICK_PRINT(1);
689             TICK_PRINT(2);
690             REPORT(TOTAL_CALLS);
691             TICK_PRINT_TOT(1);
692             TICK_PRINT_TOT(2);
693             */
694
695 #if USE_PAPI
696             papi_stats_report();
697 #endif
698         }
699
700         if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
701           /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
702           statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
703           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",
704                     total_collections,
705                     ResidencySamples == 0 ? 0 : 
706                         AvgResidency*sizeof(W_)/ResidencySamples, 
707                     MaxResidency*sizeof(W_), 
708                     ResidencySamples,
709                     (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
710                     TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
711                     TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
712                     TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
713         }
714
715 #if defined(THREADED_RTS) && defined(PROF_SPIN)
716         {
717             nat g, s;
718             
719             statsPrintf("recordMutableGen_sync: %"FMT_Word64"\n", recordMutableGen_sync.spin);
720             statsPrintf("gc_alloc_block_sync: %"FMT_Word64"\n", gc_alloc_block_sync.spin);
721             statsPrintf("whitehole_spin: %"FMT_Word64"\n", whitehole_spin);
722             for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
723                 for (s = 0; s < generations[g].n_steps; s++) {
724                     statsPrintf("gen[%d].steps[%d].sync_todo: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_todo.spin);
725                     statsPrintf("gen[%d].steps[%d].sync_large_objects: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_large_objects.spin);
726                 }
727             }
728         }
729 #endif
730
731         statsFlush();
732         statsClose();
733     }
734
735     if (GC_coll_times)
736       stgFree(GC_coll_times);
737     GC_coll_times = NULL;
738     if (GC_coll_etimes)
739       stgFree(GC_coll_etimes);
740     GC_coll_etimes = NULL;
741 }
742
743 /* -----------------------------------------------------------------------------
744    stat_describe_gens
745
746    Produce some detailed info on the state of the generational GC.
747    -------------------------------------------------------------------------- */
748 void
749 statDescribeGens(void)
750 {
751   nat g, s, mut, lge;
752   lnat live, slop;
753   lnat tot_live, tot_slop;
754   bdescr *bd;
755   step *step;
756
757   debugBelch(
758 "-----------------------------------------------------------------\n"
759 "  Gen     Max  Mut-list  Step   Blocks    Large     Live     Slop\n"
760 "       Blocks     Bytes                 Objects                  \n"
761 "-----------------------------------------------------------------\n");
762
763   tot_live = 0;
764   tot_slop = 0;
765   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
766       mut = 0;
767       for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
768           mut += (bd->free - bd->start) * sizeof(W_);
769       }
770
771     debugBelch("%5d %7d %9d", g, generations[g].max_blocks, mut);
772
773     for (s = 0; s < generations[g].n_steps; s++) {
774       step = &generations[g].steps[s];
775       for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
776         lge++;
777       }
778       live = step->n_words + countOccupied(step->large_objects);
779       if (s != 0) {
780         debugBelch("%23s","");
781       }
782       slop = (step->n_blocks + step->n_large_blocks) * BLOCK_SIZE_W - live;
783       debugBelch("%6d %8d %8d %8ld %8ld\n", s, step->n_blocks, lge,
784                  live*sizeof(W_), slop*sizeof(W_));
785       tot_live += live;
786       tot_slop += slop;
787     }
788   }
789   debugBelch("-----------------------------------------------------------------\n");
790   debugBelch("%48s%8ld %8ld\n","",tot_live*sizeof(W_),tot_slop*sizeof(W_));
791   debugBelch("-----------------------------------------------------------------\n");
792   debugBelch("\n");
793 }
794
795 /* -----------------------------------------------------------------------------
796    Stats available via a programmatic interface, so eg. GHCi can time
797    each compilation and expression evaluation.
798    -------------------------------------------------------------------------- */
799
800 extern HsInt64 getAllocations( void ) 
801 { return (HsInt64)total_allocated * sizeof(W_); }
802
803 /* -----------------------------------------------------------------------------
804    Dumping stuff in the stats file, or via the debug message interface
805    -------------------------------------------------------------------------- */
806
807 void
808 statsPrintf( char *s, ... )
809 {
810     FILE *sf = RtsFlags.GcFlags.statsFile;
811     va_list ap;
812     
813     va_start(ap,s);
814     if (sf == NULL) {
815         vdebugBelch(s,ap);
816     } else {
817         vfprintf(sf, s, ap);
818     }
819     va_end(ap);
820 }
821
822 static void
823 statsFlush( void )
824 {
825     FILE *sf = RtsFlags.GcFlags.statsFile;
826     if (sf != NULL) {
827         fflush(sf);
828     }
829 }
830
831 static void
832 statsClose( void )
833 {
834     FILE *sf = RtsFlags.GcFlags.statsFile;
835     if (sf != NULL) {
836         fclose(sf);
837     }
838 }