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