Stats.c 23 KB
Newer Older
1
/* -----------------------------------------------------------------------------
sof's avatar
sof committed
2
 * $Id: Stats.c,v 1.44 2002/02/06 01:21:40 sof Exp $
3
4
 *
 * (c) The GHC Team, 1998-1999
5
6
7
8
9
 *
 * Statistics and timing-related functions.
 *
 * ---------------------------------------------------------------------------*/

10
11
12
/* Alas, no.  This source is non-posix.
   #include "PosixSource.h" 
*/
13
14
15
16

#include "Rts.h"
#include "RtsFlags.h"
#include "RtsUtils.h"
17
18
#include "StoragePriv.h"
#include "MBlock.h"
19
#include "Schedule.h"
20
#include "Stats.h"
sof's avatar
sof committed
21
#include "ParTicky.h"                       /* ToDo: move into Rts.h */
22
#include "Profiling.h"
23
24
25
26
27

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

rrt's avatar
rrt committed
28
#ifndef mingw32_TARGET_OS
sof's avatar
sof committed
29
30
31
# ifdef HAVE_SYS_TIMES_H
#  include <sys/times.h>
# endif
32
33
34
35
36
37
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

sof's avatar
sof committed
38
39
40
41
42
43
#ifdef __CYGWIN32__
# ifdef HAVE_TIME_H
#  include <time.h>
# endif
#endif

rrt's avatar
rrt committed
44
#if ! irix_TARGET_OS && ! defined(mingw32_TARGET_OS)
sof's avatar
sof committed
45
46
47
# if defined(HAVE_SYS_RESOURCE_H)
#  include <sys/resource.h>
# endif
48
49
50
51
52
53
54
55
56
57
#endif

#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#endif

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

sof's avatar
sof committed
58
59
60
61
#if HAVE_WINDOWS_H
#include <windows.h>
#endif

rrt's avatar
rrt committed
62
#if defined(PAR) || !(!defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS))
63
64
65
#include <sys/resource.h>
#endif

66
67
68
/* huh? */
#define BIG_STRING_LEN              512

69
70
71
72
73
74
75
76
77
/* We're not trying to be terribly accurate here, using the 
 * basic times() function to get a resolution of about 100ths of a 
 * second, depending on the OS.  A long int will do fine for holding
 * these values.
 */
#define TICK_TYPE long int
#define TICK_TO_DBL(t) ((double)(t) / TicksPerSecond)

static int TicksPerSecond = 0;
78

79
80
81
static TICK_TYPE ElapsedTimeStart = 0;
static TICK_TYPE CurrentElapsedTime = 0;
static TICK_TYPE CurrentUserTime    = 0;
82

83
84
85
static TICK_TYPE InitUserTime     = 0;
static TICK_TYPE InitElapsedTime  = 0;
static TICK_TYPE InitElapsedStamp = 0;
86

87
88
89
static TICK_TYPE MutUserTime      = 0;
static TICK_TYPE MutElapsedTime   = 0;
static TICK_TYPE MutElapsedStamp  = 0;
90

91
92
static TICK_TYPE ExitUserTime     = 0;
static TICK_TYPE ExitElapsedTime  = 0;
93

94
95
96
static ullong GC_tot_alloc        = 0;
static ullong GC_tot_copied       = 0;

sof's avatar
sof committed
97
98
static TICK_TYPE GC_start_time = 0,  GC_tot_time  = 0;  /* User GC Time */
static TICK_TYPE GCe_start_time = 0, GCe_tot_time = 0;  /* Elapsed GC time */
99

sof's avatar
sof committed
100
101
102
103
#ifdef PROFILING
static TICK_TYPE RP_start_time  = 0, RP_tot_time  = 0;  /* retainer prof user time */
static TICK_TYPE RPe_start_time = 0, RPe_tot_time = 0;  /* retainer prof elap time */

104
105
static TICK_TYPE HC_start_time, HC_tot_time = 0;     // heap census prof user time
static TICK_TYPE HCe_start_time, HCe_tot_time = 0;   // heap census prof elap time
sof's avatar
sof committed
106
#endif
107

sof's avatar
sof committed
108
#ifdef PROFILING
109
#define PROF_VAL(x)   (x)
sof's avatar
sof committed
110
111
112
#else
#define PROF_VAL(x)   0
#endif
113
114

lnat MaxResidency = 0;     /* in words; for stats only */
115
lnat AvgResidency = 0;
116
117
118
119
lnat ResidencySamples = 0; /* for stats only */

static lnat GC_start_faults = 0, GC_end_faults = 0;

120
static TICK_TYPE *GC_coll_times;
121

122
123
static void  getTimes(void);
static nat   pageFaults(void);
124
125
126

/* elapsedtime() -- The current elapsed time in seconds */

rrt's avatar
rrt committed
127
#if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
rrt's avatar
rrt committed
128
129
#define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
/* Convert FILETIMEs into secs */
sof's avatar
sof committed
130
131
132
133
#define FT2longlong(ll,ft)    \
    (ll)=(ft).dwHighDateTime; \
    (ll) <<= 32;              \
    (ll) |= (ft).dwLowDateTime; \
rrt's avatar
rrt committed
134
    (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
sof's avatar
sof committed
135
136
#endif

rrt's avatar
rrt committed
137
#if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
sof's avatar
sof committed
138
/* cygwin32 or mingw32 version */
139
140
static void
getTimes(void)
sof's avatar
sof committed
141
{
sof's avatar
sof committed
142
143
144
    static int is_win9x = -1;

    FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
sof's avatar
sof committed
145
    long long int kT, uT;
sof's avatar
sof committed
146
147
148
149
150
151
152
153
154
155
156
157
    
    if (is_win9x < 0) {
      /* figure out whether we're on a Win9x box or not. */
      OSVERSIONINFO oi;
      BOOL b;

      /* Need to init the size field first.*/
      oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
      b = GetVersionEx(&oi);
      
      is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
    }
sof's avatar
sof committed
158
 
sof's avatar
sof committed
159
160
161
162
163
164
165
166
167
168
169
    if (is_win9x) {
      /* On Win9x, just attribute all running time to the user. */
      SYSTEMTIME st;

      GetSystemTime(&st);
      SystemTimeToFileTime(&st,&userTime);
    } else {
      /* ToDo: pin down elapsed times to just the OS thread(s) that
	 are evaluating/managing Haskell code.
      */
      if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
sof's avatar
sof committed
170
171
		          &exitTime, &kernelTime, &userTime)) {
	/* Probably on a Win95 box..*/
sof's avatar
sof committed
172
173
	CurrentElapsedTime = 0;
	CurrentUserTime = 0;
rrt's avatar
rrt committed
174
	return;
sof's avatar
sof committed
175
      }
sof's avatar
sof committed
176
177
178
179
    }

    FT2longlong(kT,kernelTime);
    FT2longlong(uT,userTime);
180
181
    CurrentElapsedTime = uT + kT;
    CurrentUserTime = uT;
sof's avatar
sof committed
182
183
184
185
186
187
188

    if (is_win9x) {
      /* Adjust for the fact that we're using system time & not
	 process time on Win9x. */
      CurrentUserTime    -= ElapsedTimeStart;
      CurrentElapsedTime -= ElapsedTimeStart;
    }
sof's avatar
sof committed
189
190
}

rrt's avatar
rrt committed
191
#else /* !win32 */
sof's avatar
sof committed
192

193
194
static void
getTimes(void)
195
{
196

rrt's avatar
rrt committed
197
#ifndef HAVE_TIMES
198
199
200
    /* We will #ifdef around the fprintf for machines
       we *know* are unsupported. (WDP 94/05)
    */
201
    fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
202
203
    return 0.0;

rrt's avatar
rrt committed
204
#else /* not stumped */
205
206
207
    struct tms t;
    clock_t r = times(&t);

208
209
210
    CurrentElapsedTime = r;
    CurrentUserTime = t.tms_utime;
#endif
211
212

}
rrt's avatar
rrt committed
213
#endif /* !win32 */
sof's avatar
sof committed
214

215
216
/* mut_user_time_during_GC() and mut_user_time()
 *
chak's avatar
chak committed
217
218
219
220
221
222
223
224
225
 * The former function can be used to get the current mutator time
 * *during* a GC, i.e. between stat_startGC and stat_endGC.  This is
 * used in the heap profiler for accurately time stamping the heap
 * sample.  
 *
 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being 
 *	      defined in stat_startGC() - to minimise system calls, 
 *	      GC_start_time is, however, only defined when really needed (check
 *	      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
243
/*
  mut_user_time_during_RP() is similar to mut_user_time_during_GC();
  it returns the MUT time during retainer profiling.
244
  The same is for mut_user_time_during_HC();
245
246
247
248
 */
double
mut_user_time_during_RP( void )
{
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
}
sof's avatar
sof committed
257
#endif /* PROFILING */
258

259
static nat
260
pageFaults(void)
261
{
sof's avatar
sof committed
262
263
  /* ToDo (on NT): better, get this via the performance data
     that's stored in the registry. */
rrt's avatar
rrt committed
264
# if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
265
266
267
268
269
270
271
272
273
    return 0;
# else
    struct rusage t;

    getrusage(RUSAGE_SELF, &t);
    return(t.ru_majflt);
# endif
}

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
void
initStats(void)
{
    nat i;
    FILE *sf = RtsFlags.GcFlags.statsFile;
  
    if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
	fprintf(sf, "    Alloc    Collect    Live    GC    GC     TOT     TOT  Page Flts\n");
	fprintf(sf, "    bytes     bytes     bytes  user  elap    user    elap\n");
    }
    GC_coll_times = 
	(TICK_TYPE *)stgMallocBytes(
	    sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
	    "initStats");
    for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
	GC_coll_times[i] = 0;
    }
}    

/* -----------------------------------------------------------------------------
   Initialisation time...
   -------------------------------------------------------------------------- */
296
297

void
298
stat_startInit(void)
299
300
{
    /* Determine TicksPerSecond ... */
301
302
303
304
305
#if defined(CLK_TCK)		/* defined by POSIX */
    TicksPerSecond = CLK_TCK;

#elif defined(HAVE_SYSCONF)
    long ticks;
sof's avatar
sof committed
306

307
308
309
310
311
    ticks = sysconf(_SC_CLK_TCK);
    if ( ticks == -1 ) {
	fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
    	stg_exit(EXIT_FAILURE);
    }
rrt's avatar
rrt committed
312
    TicksPerSecond = ticks;
313

314
/* no "sysconf" or CLK_TCK; had better guess */
sof's avatar
sof committed
315
#elif defined(HZ)
rrt's avatar
rrt committed
316
    TicksPerSecond = HZ;
317

sof's avatar
sof committed
318
#elif defined(CLOCKS_PER_SEC)
rrt's avatar
rrt committed
319
320
    TicksPerSecond = CLOCKS_PER_SEC;

sof's avatar
sof committed
321
#else /* had better guess wildly */
322
323
324
325
    /* We will #ifdef around the fprintf for machines
       we *know* are unsupported. (WDP 94/05)
    */
    fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
rrt's avatar
rrt committed
326
    TicksPerSecond = 60;
327
328
#endif

329
330
    getTimes();
    ElapsedTimeStart = CurrentElapsedTime;
331
332
333
}

void 
334
stat_endInit(void)
335
{
336
337
338
339
340
341
342
343
    getTimes();
    InitUserTime = CurrentUserTime;
    InitElapsedStamp = CurrentElapsedTime; 
    if (ElapsedTimeStart > CurrentElapsedTime) {
	InitElapsedTime = 0;
    } else {
	InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
    }
344
345
}

346
347
348
349
350
351
/* -----------------------------------------------------------------------------
   stat_startExit and stat_endExit
   
   These two measure the time taken in shutdownHaskell().
   -------------------------------------------------------------------------- */

352
353
354
void
stat_startExit(void)
{
355
356
    getTimes();
    MutElapsedStamp = CurrentElapsedTime;
357
358
    MutElapsedTime = CurrentElapsedTime - GCe_tot_time -
	PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
359
    if (MutElapsedTime < 0) { MutElapsedTime = 0; }	/* sometimes -0.00 */
360

361
362
363
364
365
366
    /* for SMP, we don't know the mutator time yet, we have to inspect
     * all the running threads to find out, and they haven't stopped
     * yet.  So we just timestamp MutUserTime at this point so we can
     * calculate the EXIT time.  The real MutUserTime is calculated
     * in stat_exit below.
     */
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
373
374
375
376
377
#endif
}

void
stat_endExit(void)
{
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
385
386
387
388
389
390
    ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
    if (ExitUserTime < 0) {
	ExitUserTime = 0;
    }
    if (ExitElapsedTime < 0) {
	ExitElapsedTime = 0;
    }
391
392
}

393
394
395
396
397
398
/* -----------------------------------------------------------------------------
   Called at the beginning of each GC
   -------------------------------------------------------------------------- */

static nat rub_bell = 0;

chak's avatar
chak committed
399
400
401
402
403
/*  initialise global variables needed during GC
 *
 *  * GC_start_time is read in mut_user_time_during_GC(), which in turn is 
 *    needed if either PROFILING or DEBUGing is enabled
 */
404
405
406
407
408
409
410
411
412
413
414
415
416
417
void
stat_startGC(void)
{
    nat bell = RtsFlags.GcFlags.ringBell;

    if (bell) {
	if (bell > 1) {
	    fprintf(stderr, " GC ");
	    rub_bell = 1;
	} else {
	    fprintf(stderr, "\007");
	}
    }

chak's avatar
chak committed
418
#if defined(PROFILING) || defined(DEBUG)
419
420
    getTimes();
    GC_start_time = CurrentUserTime;  /* needed in mut_user_time_during_GC() */
chak's avatar
chak committed
421
422
#endif

423
    if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
chak's avatar
chak committed
424
#if !defined(PROFILING) && !defined(DEBUG)
425
426
	getTimes();
        GC_start_time = CurrentUserTime;
chak's avatar
chak committed
427
#endif
428
	GCe_start_time = CurrentElapsedTime;
429
	if (RtsFlags.GcFlags.giveStats) {
430
	    GC_start_faults = pageFaults();
431
432
433
434
435
436
437
438
439
	}
    }
}

/* -----------------------------------------------------------------------------
   Called at the end of each GC
   -------------------------------------------------------------------------- */

void
440
stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
441
442
443
{
    FILE *sf = RtsFlags.GcFlags.statsFile;

444
445
446
447
448
449
450
451
452
453
454
455
    if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
	TICK_TYPE time, etime, gc_time, gc_etime;
	
	getTimes();
	time     = CurrentUserTime;
	etime    = CurrentElapsedTime;
	gc_time  = time - GC_start_time;
	gc_etime = etime - GCe_start_time;
	
	if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
	    nat faults = pageFaults();
	    
456
457
458
	    fprintf(sf, "%9ld %9ld %9ld",
		    alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
	    fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld  (Gen: %2ld)\n", 
459
460
461
462
		    TICK_TO_DBL(gc_time),
		    TICK_TO_DBL(gc_etime),
		    TICK_TO_DBL(time),
		    TICK_TO_DBL(etime - ElapsedTimeStart),
463
464
		    faults - GC_start_faults,
		    GC_start_faults - GC_end_faults,
465
		    gen);
466
467
468
469
470

	    GC_end_faults = faults;
	    fflush(sf);
	}

chak's avatar
chak committed
471
	GC_coll_times[gen] += gc_time;
472

473
474
	GC_tot_copied += (ullong) copied;
	GC_tot_alloc  += (ullong) alloc;
475
476
	GC_tot_time   += gc_time;
	GCe_tot_time  += gc_etime;
477
	
478
479
#ifdef SMP
	{
480
481
482
483
484
485
486
487
488
	    nat i;
	    pthread_t me = pthread_self();

	    for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
		if (me == task_ids[i].id) {
		    task_ids[i].gc_time += gc_time;
		    task_ids[i].gc_etime += gc_etime;
		    break;
		}
489
490
491
492
	    }
	}
#endif

493
	if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
494
495
496
497
498
	    if (live > MaxResidency) {
		MaxResidency = live;
	    }
	    ResidencySamples++;
	    AvgResidency += live;
499
	}
500
501
502
503
504
505
506
507
    }

    if (rub_bell) {
	fprintf(stderr, "\b\b\b  \b\b\b");
	rub_bell = 0;
    }
}

508
509
510
/* -----------------------------------------------------------------------------
   Called at the beginning of each Retainer Profiliing
   -------------------------------------------------------------------------- */
511
512
513
#ifdef PROFILING
void
stat_startRP(void)
514
515
516
517
518
{
  getTimes();
  RP_start_time = CurrentUserTime;
  RPe_start_time = CurrentElapsedTime;
}
sof's avatar
sof committed
519
#endif /* PROFILING */
520
521
522
523

/* -----------------------------------------------------------------------------
   Called at the end of each Retainer Profiliing
   -------------------------------------------------------------------------- */
524
525
526
527

#ifdef PROFILING
void
stat_endRP(
528
529
530
531
532
  nat retainerGeneration,
#ifdef DEBUG_RETAINER
  nat maxCStackSize,
  int maxStackSize,
#endif
533
  double averageNumVisit)
534
535
536
537
538
539
540
541
542
543
544
545
546
{
  getTimes();
  RP_tot_time += CurrentUserTime - RP_start_time;
  RPe_tot_time += CurrentElapsedTime - RPe_start_time;

  fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n", 
    retainerGeneration, mut_user_time_during_RP());
#ifdef DEBUG_RETAINER
  fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
  fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
#endif
  fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
}
sof's avatar
sof committed
547
#endif /* PROFILING */
548
549

/* -----------------------------------------------------------------------------
550
   Called at the beginning of each heap census
551
   -------------------------------------------------------------------------- */
552
553
#ifdef PROFILING
void
554
stat_startHeapCensus(void)
555
556
{
  getTimes();
557
558
  HC_start_time = CurrentUserTime;
  HCe_start_time = CurrentElapsedTime;
559
}
sof's avatar
sof committed
560
#endif /* PROFILING */
561
562

/* -----------------------------------------------------------------------------
563
   Called at the end of each heap census
564
   -------------------------------------------------------------------------- */
565
566
#ifdef PROFILING
void
567
stat_endHeapCensus(void) 
568
569
{
  getTimes();
570
571
  HC_tot_time += CurrentUserTime - HC_start_time;
  HCe_tot_time += CurrentElapsedTime - HCe_start_time;
572
}
sof's avatar
sof committed
573
#endif /* PROFILING */
574

575
576
577
578
579
580
581
/* -----------------------------------------------------------------------------
   stat_workerStop

   Called under SMP when a worker thread finishes.  We drop the timing
   stats for this thread into the task_ids struct for that thread.
   -------------------------------------------------------------------------- */

sof's avatar
sof committed
582
#if defined(SMP)
583
584
585
void
stat_workerStop(void)
{
586
587
588
    nat i;
    pthread_t me = pthread_self();

sof's avatar
sof committed
589
590
    getTimes();

591
592
    for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
	if (task_ids[i].id == me) {
sof's avatar
sof committed
593
594
	    task_ids[i].mut_time = CurrentUserTime - task_ids[i].gc_time;
	    task_ids[i].mut_etime = CurrentElapsedTime
595
596
597
598
599
		- GCe_tot_time
		- task_ids[i].elapsedtimestart;
	    if (task_ids[i].mut_time < 0.0)  { task_ids[i].mut_time = 0.0;  }
	    if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
	}
600
601
    }
}
sof's avatar
sof committed
602
#endif
sof's avatar
sof committed
603

sof's avatar
sof committed
604
#if defined(SMP)
sof's avatar
sof committed
605
606
607
608
609
long int stat_getElapsedTime ()
{
  getTimes();
  return CurrentElapsedTime;
}
610
611
#endif

612
613
614
615
616
617
618
619
620
621
622
623
/* -----------------------------------------------------------------------------
   Called at the end of execution

   NOTE: number of allocations is not entirely accurate: it doesn't
   take into account the few bytes at the end of the heap that
   were left unused when the heap-check failed.
   -------------------------------------------------------------------------- */

void
stat_exit(int alloc)
{
    FILE *sf = RtsFlags.GcFlags.statsFile;
624
625
    
    if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
626
627

	char temp[BIG_STRING_LEN];
628
629
630
	TICK_TYPE time;
	TICK_TYPE etime;
	nat g, total_collections = 0;
631

632
633
634
	getTimes();
	time = CurrentUserTime;
	etime = CurrentElapsedTime - ElapsedTimeStart;
635
636

	GC_tot_alloc += alloc;
637

638
639
640
641
642
643
644
	/* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
	if (time  == 0.0)  time = 1;
	if (etime == 0.0) etime = 1;
	
	/* Count total garbage collections */
	for (g = 0; g < RtsFlags.GcFlags.generations; g++)
	    total_collections += generations[g].collections;
645

646
647
648
	/* For SMP, we have to get the user time from each thread
	 * and try to work out the total time.
	 */
649
#ifdef SMP
650
651
652
653
654
	{   nat i;
	    MutUserTime = 0.0;
	    for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
		MutUserTime += task_ids[i].mut_time;
	    }
655
	}
656
657
	time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
	if (MutUserTime < 0) { MutUserTime = 0; }
658
659
#endif

660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
	if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
	    fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
	    fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
	}

	if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
	    ullong_format_string(GC_tot_alloc*sizeof(W_), 
				 temp, rtsTrue/*commas*/);
	    fprintf(sf, "%11s bytes allocated in the heap\n", temp);

	    ullong_format_string(GC_tot_copied*sizeof(W_), 
				 temp, rtsTrue/*commas*/);
	    fprintf(sf, "%11s bytes copied during GC\n", temp);

	    if ( ResidencySamples > 0 ) {
		ullong_format_string(MaxResidency*sizeof(W_), 
676
				     temp, rtsTrue/*commas*/);
677
678
679
680
681
682
683
684
685
686
687
688
689
690
		fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
			temp, ResidencySamples);
	    }
	    fprintf(sf,"\n");

	    /* Print garbage collections in each gen */
	    for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
		fprintf(sf, "%11d collections in generation %d (%6.2fs)\n", 
			generations[g].collections, g, 
			TICK_TO_DBL(GC_coll_times[g]));
	    }

	    fprintf(sf,"\n%11ld Mb total memory in use\n\n", 
		    mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
691

692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#ifdef SMP
	    {
		nat i;
		for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
		    fprintf(sf, "  Task %2d:  MUT time: %6.2fs  (%6.2fs elapsed)\n"
			    "            GC  time: %6.2fs  (%6.2fs elapsed)\n\n", 
			    i, 
			    TICK_TO_DBL(task_ids[i].mut_time),
			    TICK_TO_DBL(task_ids[i].mut_etime),
			    TICK_TO_DBL(task_ids[i].gc_time),
			    TICK_TO_DBL(task_ids[i].gc_etime));
		}
	    }
#endif

	    fprintf(sf, "  INIT  time  %6.2fs  (%6.2fs elapsed)\n",
		    TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
	    fprintf(sf, "  MUT   time  %6.2fs  (%6.2fs elapsed)\n",
		    TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
	    fprintf(sf, "  GC    time  %6.2fs  (%6.2fs elapsed)\n",
		    TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
713
#ifdef PROFILING
714
715
716
717
	    fprintf(sf, "  RP    time  %6.2fs  (%6.2fs elapsed)\n",
		    TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
	    fprintf(sf, "  PROF  time  %6.2fs  (%6.2fs elapsed)\n",
		    TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
718
#endif 
719
720
721
722
723
	    fprintf(sf, "  EXIT  time  %6.2fs  (%6.2fs elapsed)\n",
		    TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
	    fprintf(sf, "  Total time  %6.2fs  (%6.2fs elapsed)\n\n",
		    TICK_TO_DBL(time), TICK_TO_DBL(etime));
	    fprintf(sf, "  %%GC time     %5.1f%%  (%.1f%% elapsed)\n\n",
724
		    TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
sof's avatar
sof committed
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
729
730
731
		ullong_format_string(0, temp, rtsTrue/*commas*/);
	    else
		ullong_format_string(
		    (ullong)((GC_tot_alloc*sizeof(W_))/
sof's avatar
sof committed
732
			     TICK_TO_DBL(time - GC_tot_time - 
733
					 PROF_VAL(RP_tot_time + HC_tot_time))),
734
735
736
737
738
		    temp, rtsTrue/*commas*/);
	    
	    fprintf(sf, "  Alloc rate    %s bytes per MUT second\n\n", temp);
	
	    fprintf(sf, "  Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
sof's avatar
sof committed
739
		    TICK_TO_DBL(time - GC_tot_time - 
740
				PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100 
741
		    / TICK_TO_DBL(time), 
sof's avatar
sof committed
742
		    TICK_TO_DBL(time - GC_tot_time - 
743
				PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100 
744
745
746
747
		    / TICK_TO_DBL(etime));
	}

	if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
rrt's avatar
rrt committed
748
749
750
751
	  /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
	  fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
	  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",
		    total_collections,
752
753
		    AvgResidency*sizeof(W_)/ResidencySamples, 
		    MaxResidency*sizeof(W_), 
rrt's avatar
rrt committed
754
755
		    ResidencySamples,
		    (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
756
757
758
759
		    TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
		    TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
		    TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
	}
760
761

	fflush(sf);
762
763
764
	if (sf != stderr) {
	    fclose(sf);
	}
765
766
    }
}
767
768
769
770
771
772

/* -----------------------------------------------------------------------------
   stat_describe_gens

   Produce some detailed info on the state of the generational GC.
   -------------------------------------------------------------------------- */
773
#ifdef DEBUG
774
void
775
statDescribeGens(void)
776
{
777
  nat g, s, mut, mut_once, lge, live;
778
779
780
781
  StgMutClosure *m;
  bdescr *bd;
  step *step;

782
  fprintf(stderr, "     Gen    Steps      Max   Mutable  Mut-Once  Step   Blocks     Live    Large\n                    Blocks  Closures  Closures                          Objects\n");
783
784
785
786
787

  for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
    for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST; 
	 m = m->mut_link) 
      mut++;
788
789
790
    for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST; 
	 m = m->mut_link) 
      mut_once++;
791
    fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
792
	    generations[g].max_blocks, mut, mut_once);
793
794
795
796
797
798

    for (s = 0; s < generations[g].n_steps; s++) {
      step = &generations[g].steps[s];
      for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
	lge++;
      live = 0;
799
      if (RtsFlags.GcFlags.generations == 1) {
800
	bd = step->to_blocks;
801
802
803
804
      } else {
	bd = step->blocks;
      }
      for (; bd; bd = bd->link) {
805
806
807
	live += (bd->free - bd->start) * sizeof(W_);
      }
      if (s != 0) {
808
	fprintf(stderr,"%46s","");
809
810
811
812
813
814
815
      }
      fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
	      live, lge);
    }
  }
  fprintf(stderr,"\n");
}
816
#endif
817
818
819
820
821
822
823
824

/* -----------------------------------------------------------------------------
   Stats available via a programmatic interface, so eg. GHCi can time
   each compilation and expression evaluation.
   -------------------------------------------------------------------------- */

extern HsInt getAllocations( void ) 
{ return (HsInt)(total_allocated * sizeof(W_)); }