[project @ 2001-11-22 14:25:11 by simonmar]
[ghc-hetmet.git] / ghc / rts / Stats.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Stats.c,v 1.36 2001/11/22 14:25:12 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Statistics and timing-related functions.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 /* Alas, no.  This source is non-posix.
11    #include "PosixSource.h" 
12 */
13
14 #include "Rts.h"
15 #include "RtsFlags.h"
16 #include "RtsUtils.h"
17 #include "StoragePriv.h"
18 #include "MBlock.h"
19 #include "Schedule.h"
20 #include "Stats.h"
21 #include "ParTicky.h"                       // ToDo: move into Rts.h
22 #include "Profiling.h"
23
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #ifndef mingw32_TARGET_OS
29 # ifdef HAVE_SYS_TIMES_H
30 #  include <sys/times.h>
31 # endif
32 #endif
33
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #endif
37
38 #ifdef __CYGWIN32__
39 # ifdef HAVE_TIME_H
40 #  include <time.h>
41 # endif
42 #endif
43
44 #if ! irix_TARGET_OS && ! defined(mingw32_TARGET_OS)
45 # if defined(HAVE_SYS_RESOURCE_H)
46 #  include <sys/resource.h>
47 # endif
48 #endif
49
50 #ifdef HAVE_SYS_TIMEB_H
51 #include <sys/timeb.h>
52 #endif
53
54 #if HAVE_STDLIB_H
55 #include <stdlib.h>
56 #endif
57
58 #if HAVE_WINDOWS_H
59 #include <windows.h>
60 #endif
61
62 #if defined(PAR) || !(!defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS))
63 #include <sys/resource.h>
64 #endif
65
66 /* huh? */
67 #define BIG_STRING_LEN              512
68
69 /* We're not trying to be terribly accurate here, using the 
70  * basic times() function to get a resolution of about 100ths of a 
71  * second, depending on the OS.  A long int will do fine for holding
72  * these values.
73  */
74 #define TICK_TYPE long int
75 #define TICK_TO_DBL(t) ((double)(t) / TicksPerSecond)
76
77 static int TicksPerSecond = 0;
78
79 static TICK_TYPE ElapsedTimeStart = 0;
80 static TICK_TYPE CurrentElapsedTime = 0;
81 static TICK_TYPE CurrentUserTime    = 0;
82
83 static TICK_TYPE InitUserTime     = 0;
84 static TICK_TYPE InitElapsedTime  = 0;
85 static TICK_TYPE InitElapsedStamp = 0;
86
87 static TICK_TYPE MutUserTime      = 0;
88 static TICK_TYPE MutElapsedTime   = 0;
89 static TICK_TYPE MutElapsedStamp  = 0;
90
91 static TICK_TYPE ExitUserTime     = 0;
92 static TICK_TYPE ExitElapsedTime  = 0;
93
94 static ullong GC_tot_alloc        = 0;
95 static ullong GC_tot_copied       = 0;
96
97 static TICK_TYPE GC_start_time,  GC_tot_time = 0;    // User GC Time
98 static TICK_TYPE GCe_start_time, GCe_tot_time = 0;   // Elapsed GC time
99
100 static TICK_TYPE RP_start_time, RP_tot_time = 0;     // retainer prof user time
101 static TICK_TYPE RPe_start_time, RPe_tot_time = 0;   // retainer prof elap time
102
103 static TICK_TYPE LDV_start_time, LDV_tot_time = 0;   // LDV prof user time
104 static TICK_TYPE LDVe_start_time, LDVe_tot_time = 0; // LDV prof elap time
105
106 lnat MaxResidency = 0;     /* in words; for stats only */
107 lnat AvgResidency = 0;
108 lnat ResidencySamples = 0; /* for stats only */
109
110 static lnat GC_start_faults = 0, GC_end_faults = 0;
111
112 static TICK_TYPE *GC_coll_times;
113
114 static void  getTimes(void);
115 static nat   pageFaults(void);
116
117 /* elapsedtime() -- The current elapsed time in seconds */
118
119 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
120 #define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
121 /* Convert FILETIMEs into secs */
122 #define FT2longlong(ll,ft)    \
123     (ll)=(ft).dwHighDateTime; \
124     (ll) <<= 32;              \
125     (ll) |= (ft).dwLowDateTime; \
126     (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
127 #endif
128
129 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
130 /* cygwin32 or mingw32 version */
131 static void
132 getTimes(void)
133 {
134     static int is_win9x = -1;
135
136     FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
137     long long int kT, uT;
138     
139     if (is_win9x < 0) {
140       /* figure out whether we're on a Win9x box or not. */
141       OSVERSIONINFO oi;
142       BOOL b;
143
144       /* Need to init the size field first.*/
145       oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
146       b = GetVersionEx(&oi);
147       
148       is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
149     }
150  
151     if (is_win9x) {
152       /* On Win9x, just attribute all running time to the user. */
153       SYSTEMTIME st;
154
155       GetSystemTime(&st);
156       SystemTimeToFileTime(&st,&userTime);
157     } else {
158       /* ToDo: pin down elapsed times to just the OS thread(s) that
159          are evaluating/managing Haskell code.
160       */
161       if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
162                           &exitTime, &kernelTime, &userTime)) {
163         /* Probably on a Win95 box..*/
164         CurrentElapsedTime = 0;
165         CurrentUserTime = 0;
166         return;
167       }
168     }
169
170     FT2longlong(kT,kernelTime);
171     FT2longlong(uT,userTime);
172     CurrentElapsedTime = uT + kT;
173     CurrentUserTime = uT;
174
175     if (is_win9x) {
176       /* Adjust for the fact that we're using system time & not
177          process time on Win9x. */
178       CurrentUserTime    -= ElapsedTimeStart;
179       CurrentElapsedTime -= ElapsedTimeStart;
180     }
181 }
182
183 #else /* !win32 */
184
185 static void
186 getTimes(void)
187 {
188
189 #ifndef HAVE_TIMES
190     /* We will #ifdef around the fprintf for machines
191        we *know* are unsupported. (WDP 94/05)
192     */
193     fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
194     return 0.0;
195
196 #else /* not stumped */
197     struct tms t;
198     clock_t r = times(&t);
199
200     CurrentElapsedTime = r;
201     CurrentUserTime = t.tms_utime;
202 #endif
203
204 }
205 #endif /* !win32 */
206
207 /* mut_user_time_during_GC() and mut_user_time()
208  *
209  * The former function can be used to get the current mutator time
210  * *during* a GC, i.e. between stat_startGC and stat_endGC.  This is
211  * used in the heap profiler for accurately time stamping the heap
212  * sample.  
213  *
214  * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being 
215  *            defined in stat_startGC() - to minimise system calls, 
216  *            GC_start_time is, however, only defined when really needed (check
217  *            stat_startGC() for details)
218  */
219 double
220 mut_user_time_during_GC( void )
221 {
222   return TICK_TO_DBL(GC_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
223 }
224
225 double
226 mut_user_time( void )
227 {
228     getTimes();
229     return TICK_TO_DBL(CurrentUserTime - GC_tot_time - RP_tot_time - LDV_tot_time);
230 }
231
232 /*
233   mut_user_time_during_RP() is similar to mut_user_time_during_GC();
234   it returns the MUT time during retainer profiling.
235   The same is for mut_user_time_during_LDV();
236  */
237 double
238 mut_user_time_during_RP( void )
239 {
240   return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
241 }
242
243 double
244 mut_user_time_during_LDV( void )
245 {
246   return TICK_TO_DBL(LDV_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
247 }
248
249 static nat
250 pageFaults(void)
251 {
252   /* ToDo (on NT): better, get this via the performance data
253      that's stored in the registry. */
254 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
255     return 0;
256 # else
257     struct rusage t;
258
259     getrusage(RUSAGE_SELF, &t);
260     return(t.ru_majflt);
261 # endif
262 }
263
264 void
265 initStats(void)
266 {
267     nat i;
268     FILE *sf = RtsFlags.GcFlags.statsFile;
269   
270     if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
271         fprintf(sf, "    Alloc    Collect    Live    GC    GC     TOT     TOT  Page Flts\n");
272         fprintf(sf, "    bytes     bytes     bytes  user  elap    user    elap\n");
273     }
274     GC_coll_times = 
275         (TICK_TYPE *)stgMallocBytes(
276             sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
277             "initStats");
278     for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
279         GC_coll_times[i] = 0;
280     }
281 }    
282
283 /* -----------------------------------------------------------------------------
284    Initialisation time...
285    -------------------------------------------------------------------------- */
286
287 void
288 stat_startInit(void)
289 {
290     /* Determine TicksPerSecond ... */
291 #if defined(CLK_TCK)            /* defined by POSIX */
292     TicksPerSecond = CLK_TCK;
293
294 #elif defined(HAVE_SYSCONF)
295     long ticks;
296
297     ticks = sysconf(_SC_CLK_TCK);
298     if ( ticks == -1 ) {
299         fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
300         stg_exit(EXIT_FAILURE);
301     }
302     TicksPerSecond = ticks;
303
304 /* no "sysconf" or CLK_TCK; had better guess */
305 #elif defined(HZ)
306     TicksPerSecond = HZ;
307
308 #elif defined(CLOCKS_PER_SEC)
309     TicksPerSecond = CLOCKS_PER_SEC;
310
311 #else /* had better guess wildly */
312     /* We will #ifdef around the fprintf for machines
313        we *know* are unsupported. (WDP 94/05)
314     */
315     fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
316     TicksPerSecond = 60;
317 #endif
318
319     getTimes();
320     ElapsedTimeStart = CurrentElapsedTime;
321 }
322
323 void 
324 stat_endInit(void)
325 {
326     getTimes();
327     InitUserTime = CurrentUserTime;
328     InitElapsedStamp = CurrentElapsedTime; 
329     if (ElapsedTimeStart > CurrentElapsedTime) {
330         InitElapsedTime = 0;
331     } else {
332         InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
333     }
334 }
335
336 /* -----------------------------------------------------------------------------
337    stat_startExit and stat_endExit
338    
339    These two measure the time taken in shutdownHaskell().
340    -------------------------------------------------------------------------- */
341
342 void
343 stat_startExit(void)
344 {
345     getTimes();
346     MutElapsedStamp = CurrentElapsedTime;
347     MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
348     if (MutElapsedTime < 0) { MutElapsedTime = 0; }     /* sometimes -0.00 */
349     
350     /* for SMP, we don't know the mutator time yet, we have to inspect
351      * all the running threads to find out, and they haven't stopped
352      * yet.  So we just timestamp MutUserTime at this point so we can
353      * calculate the EXIT time.  The real MutUserTime is calculated
354      * in stat_exit below.
355      */
356 #ifdef SMP
357     MutUserTime = CurrentUserTime;
358 #else
359     MutUserTime = CurrentUserTime - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime;
360     if (MutUserTime < 0) { MutUserTime = 0; }
361 #endif
362 }
363
364 void
365 stat_endExit(void)
366 {
367     getTimes();
368 #ifdef SMP
369     ExitUserTime = CurrentUserTime - MutUserTime;
370 #else
371     ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime;
372 #endif
373     ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
374     if (ExitUserTime < 0) {
375         ExitUserTime = 0;
376     }
377     if (ExitElapsedTime < 0) {
378         ExitElapsedTime = 0;
379     }
380 }
381
382 /* -----------------------------------------------------------------------------
383    Called at the beginning of each GC
384    -------------------------------------------------------------------------- */
385
386 static nat rub_bell = 0;
387
388 /*  initialise global variables needed during GC
389  *
390  *  * GC_start_time is read in mut_user_time_during_GC(), which in turn is 
391  *    needed if either PROFILING or DEBUGing is enabled
392  */
393 void
394 stat_startGC(void)
395 {
396     nat bell = RtsFlags.GcFlags.ringBell;
397
398     if (bell) {
399         if (bell > 1) {
400             fprintf(stderr, " GC ");
401             rub_bell = 1;
402         } else {
403             fprintf(stderr, "\007");
404         }
405     }
406
407 #if defined(PROFILING) || defined(DEBUG)
408     getTimes();
409     GC_start_time = CurrentUserTime;  /* needed in mut_user_time_during_GC() */
410 #endif
411
412     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
413 #if !defined(PROFILING) && !defined(DEBUG)
414         getTimes();
415         GC_start_time = CurrentUserTime;
416 #endif
417         GCe_start_time = CurrentElapsedTime;
418         if (RtsFlags.GcFlags.giveStats) {
419             GC_start_faults = pageFaults();
420         }
421     }
422 }
423
424 /* -----------------------------------------------------------------------------
425    Called at the end of each GC
426    -------------------------------------------------------------------------- */
427
428 void
429 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
430 {
431     FILE *sf = RtsFlags.GcFlags.statsFile;
432
433     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
434         TICK_TYPE time, etime, gc_time, gc_etime;
435         
436         getTimes();
437         time     = CurrentUserTime;
438         etime    = CurrentElapsedTime;
439         gc_time  = time - GC_start_time;
440         gc_etime = etime - GCe_start_time;
441         
442         if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
443             nat faults = pageFaults();
444             
445             fprintf(sf, "%9ld %9ld %9ld",
446                     alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
447             fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld  (Gen: %2ld)\n", 
448                     TICK_TO_DBL(gc_time),
449                     TICK_TO_DBL(gc_etime),
450                     TICK_TO_DBL(time),
451                     TICK_TO_DBL(etime - ElapsedTimeStart),
452                     faults - GC_start_faults,
453                     GC_start_faults - GC_end_faults,
454                     gen);
455
456             GC_end_faults = faults;
457             fflush(sf);
458         }
459
460         GC_coll_times[gen] += gc_time;
461
462         GC_tot_copied += (ullong) copied;
463         GC_tot_alloc  += (ullong) alloc;
464         GC_tot_time   += gc_time;
465         GCe_tot_time  += gc_etime;
466         
467 #ifdef SMP
468         {
469             nat i;
470             pthread_t me = pthread_self();
471
472             for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
473                 if (me == task_ids[i].id) {
474                     task_ids[i].gc_time += gc_time;
475                     task_ids[i].gc_etime += gc_etime;
476                     break;
477                 }
478             }
479         }
480 #endif
481
482         if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
483             if (live > MaxResidency) {
484                 MaxResidency = live;
485             }
486             ResidencySamples++;
487             AvgResidency += live;
488         }
489     }
490
491     if (rub_bell) {
492         fprintf(stderr, "\b\b\b  \b\b\b");
493         rub_bell = 0;
494     }
495 }
496
497 /* -----------------------------------------------------------------------------
498    Called at the beginning of each Retainer Profiliing
499    -------------------------------------------------------------------------- */
500 void stat_startRP(void)
501 {
502   getTimes();
503   RP_start_time = CurrentUserTime;
504   RPe_start_time = CurrentElapsedTime;
505 }
506
507 /* -----------------------------------------------------------------------------
508    Called at the end of each Retainer Profiliing
509    -------------------------------------------------------------------------- */
510 void stat_endRP(
511   nat retainerGeneration,
512 #ifdef DEBUG_RETAINER
513   nat maxCStackSize,
514   int maxStackSize,
515 #endif
516   double averageNumVisit,
517   nat allCost,
518   nat numSet)
519 {
520   getTimes();
521   RP_tot_time += CurrentUserTime - RP_start_time;
522   RPe_tot_time += CurrentElapsedTime - RPe_start_time;
523
524   fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n", 
525     retainerGeneration, mut_user_time_during_RP());
526 #ifdef DEBUG_RETAINER
527   fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
528   fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
529 #endif
530   fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
531   fprintf(prof_file, "\tCurrent total costs in bytes = %u\n", allCost * sizeof(StgWord));
532   fprintf(prof_file, "\tNumber of retainer sets = %u\n\n", numSet);
533 }
534
535 /* -----------------------------------------------------------------------------
536    Called at the beginning of each LDV Profiliing
537    -------------------------------------------------------------------------- */
538 void stat_startLDV(void)
539 {
540   getTimes();
541   LDV_start_time = CurrentUserTime;
542   LDVe_start_time = CurrentElapsedTime;
543 }
544
545 /* -----------------------------------------------------------------------------
546    Called at the end of each LDV Profiliing
547    -------------------------------------------------------------------------- */
548 void stat_endLDV(void) 
549 {
550   getTimes();
551   LDV_tot_time += CurrentUserTime - LDV_start_time;
552   LDVe_tot_time += CurrentElapsedTime - LDVe_start_time;
553 }
554
555 /* -----------------------------------------------------------------------------
556    stat_workerStop
557
558    Called under SMP when a worker thread finishes.  We drop the timing
559    stats for this thread into the task_ids struct for that thread.
560    -------------------------------------------------------------------------- */
561
562 #ifdef SMP
563 void
564 stat_workerStop(void)
565 {
566     nat i;
567     pthread_t me = pthread_self();
568
569     for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
570         if (task_ids[i].id == me) {
571             task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
572             task_ids[i].mut_etime = elapsedtime()
573                 - GCe_tot_time
574                 - task_ids[i].elapsedtimestart;
575             if (task_ids[i].mut_time < 0.0)  { task_ids[i].mut_time = 0.0;  }
576             if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
577         }
578     }
579 }
580 #endif
581
582 /* -----------------------------------------------------------------------------
583    Called at the end of execution
584
585    NOTE: number of allocations is not entirely accurate: it doesn't
586    take into account the few bytes at the end of the heap that
587    were left unused when the heap-check failed.
588    -------------------------------------------------------------------------- */
589
590 void
591 stat_exit(int alloc)
592 {
593     FILE *sf = RtsFlags.GcFlags.statsFile;
594     
595     if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
596
597         char temp[BIG_STRING_LEN];
598         TICK_TYPE time;
599         TICK_TYPE etime;
600         nat g, total_collections = 0;
601
602         getTimes();
603         time = CurrentUserTime;
604         etime = CurrentElapsedTime - ElapsedTimeStart;
605
606         GC_tot_alloc += alloc;
607
608         /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
609         if (time  == 0.0)  time = 1;
610         if (etime == 0.0) etime = 1;
611         
612         /* Count total garbage collections */
613         for (g = 0; g < RtsFlags.GcFlags.generations; g++)
614             total_collections += generations[g].collections;
615
616         /* For SMP, we have to get the user time from each thread
617          * and try to work out the total time.
618          */
619 #ifdef SMP
620         {   nat i;
621             MutUserTime = 0.0;
622             for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
623                 MutUserTime += task_ids[i].mut_time;
624             }
625         }
626         time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
627         if (MutUserTime < 0) { MutUserTime = 0; }
628 #endif
629
630         if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
631             fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
632             fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
633         }
634
635         if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
636             ullong_format_string(GC_tot_alloc*sizeof(W_), 
637                                  temp, rtsTrue/*commas*/);
638             fprintf(sf, "%11s bytes allocated in the heap\n", temp);
639
640             ullong_format_string(GC_tot_copied*sizeof(W_), 
641                                  temp, rtsTrue/*commas*/);
642             fprintf(sf, "%11s bytes copied during GC\n", temp);
643
644             if ( ResidencySamples > 0 ) {
645                 ullong_format_string(MaxResidency*sizeof(W_), 
646                                      temp, rtsTrue/*commas*/);
647                 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
648                         temp, ResidencySamples);
649             }
650             fprintf(sf,"\n");
651
652             /* Print garbage collections in each gen */
653             for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
654                 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n", 
655                         generations[g].collections, g, 
656                         TICK_TO_DBL(GC_coll_times[g]));
657             }
658
659             fprintf(sf,"\n%11ld Mb total memory in use\n\n", 
660                     mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
661
662 #ifdef SMP
663             {
664                 nat i;
665                 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
666                     fprintf(sf, "  Task %2d:  MUT time: %6.2fs  (%6.2fs elapsed)\n"
667                             "            GC  time: %6.2fs  (%6.2fs elapsed)\n\n", 
668                             i, 
669                             TICK_TO_DBL(task_ids[i].mut_time),
670                             TICK_TO_DBL(task_ids[i].mut_etime),
671                             TICK_TO_DBL(task_ids[i].gc_time),
672                             TICK_TO_DBL(task_ids[i].gc_etime));
673                 }
674             }
675 #endif
676
677             fprintf(sf, "  INIT  time  %6.2fs  (%6.2fs elapsed)\n",
678                     TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
679             fprintf(sf, "  MUT   time  %6.2fs  (%6.2fs elapsed)\n",
680                     TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
681             fprintf(sf, "  GC    time  %6.2fs  (%6.2fs elapsed)\n",
682                     TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
683 #ifdef PROFILING
684       if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER)
685               fprintf(sf, "  RP    time  %6.2fs  (%6.2fs elapsed)\n",
686                       TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
687       if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV)
688               fprintf(sf, "  LDV   time  %6.2fs  (%6.2fs elapsed)\n",
689                       TICK_TO_DBL(LDV_tot_time), TICK_TO_DBL(LDVe_tot_time));
690 #endif 
691             fprintf(sf, "  EXIT  time  %6.2fs  (%6.2fs elapsed)\n",
692                     TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
693             fprintf(sf, "  Total time  %6.2fs  (%6.2fs elapsed)\n\n",
694                     TICK_TO_DBL(time), TICK_TO_DBL(etime));
695             fprintf(sf, "  %%GC time     %5.1f%%  (%.1f%% elapsed)\n\n",
696                     TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
697                     TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
698
699             if (time - GC_tot_time - RP_tot_time - LDV_tot_time == 0)
700                 ullong_format_string(0, temp, rtsTrue/*commas*/);
701             else
702                 ullong_format_string(
703                     (ullong)((GC_tot_alloc*sizeof(W_))/
704                              TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time)),
705                     temp, rtsTrue/*commas*/);
706             
707             fprintf(sf, "  Alloc rate    %s bytes per MUT second\n\n", temp);
708         
709             fprintf(sf, "  Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
710                     TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime) * 100 
711                     / TICK_TO_DBL(time), 
712                     TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime) * 100 
713                     / TICK_TO_DBL(etime));
714         }
715
716         if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
717           /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
718           fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
719           fprintf(sf, "%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",
720                     total_collections,
721                     AvgResidency*sizeof(W_)/ResidencySamples, 
722                     MaxResidency*sizeof(W_), 
723                     ResidencySamples,
724                     (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
725                     TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
726                     TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
727                     TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
728         }
729
730         fflush(sf);
731         fclose(sf);
732     }
733 }
734
735 /* -----------------------------------------------------------------------------
736    stat_describe_gens
737
738    Produce some detailed info on the state of the generational GC.
739    -------------------------------------------------------------------------- */
740 void
741 statDescribeGens(void)
742 {
743   nat g, s, mut, mut_once, lge, live;
744   StgMutClosure *m;
745   bdescr *bd;
746   step *step;
747
748   fprintf(stderr, "     Gen    Steps      Max   Mutable  Mut-Once  Step   Blocks     Live    Large\n                    Blocks  Closures  Closures                          Objects\n");
749
750   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
751     for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST; 
752          m = m->mut_link) 
753       mut++;
754     for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST; 
755          m = m->mut_link) 
756       mut_once++;
757     fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
758             generations[g].max_blocks, mut, mut_once);
759
760     for (s = 0; s < generations[g].n_steps; s++) {
761       step = &generations[g].steps[s];
762       for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
763         lge++;
764       live = 0;
765       if (RtsFlags.GcFlags.generations == 1) {
766         bd = step->to_blocks;
767       } else {
768         bd = step->blocks;
769       }
770       for (; bd; bd = bd->link) {
771         live += (bd->free - bd->start) * sizeof(W_);
772       }
773       if (s != 0) {
774         fprintf(stderr,"%46s","");
775       }
776       fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
777               live, lge);
778     }
779   }
780   fprintf(stderr,"\n");
781 }
782
783 /* -----------------------------------------------------------------------------
784    Stats available via a programmatic interface, so eg. GHCi can time
785    each compilation and expression evaluation.
786    -------------------------------------------------------------------------- */
787
788 extern HsInt getAllocations( void ) 
789 { return (HsInt)(total_allocated * sizeof(W_)); }