1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.35 2001/11/20 21:39:12 sof Exp $
4 * (c) The GHC Team, 1998-1999
6 * Statistics and timing-related functions.
8 * ---------------------------------------------------------------------------*/
10 /* Alas, no. This source is non-posix.
11 #include "PosixSource.h"
17 #include "StoragePriv.h"
21 #include "ParTicky.h" // ToDo: move into Rts.h
27 #ifndef mingw32_TARGET_OS
28 # ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
33 #ifdef HAVE_SYS_TIME_H
43 #if ! irix_TARGET_OS && ! defined(mingw32_TARGET_OS)
44 # if defined(HAVE_SYS_RESOURCE_H)
45 # include <sys/resource.h>
49 #ifdef HAVE_SYS_TIMEB_H
50 #include <sys/timeb.h>
61 #if defined(PAR) || !(!defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS))
62 #include <sys/resource.h>
66 #define BIG_STRING_LEN 512
68 /* We're not trying to be terribly accurate here, using the
69 * basic times() function to get a resolution of about 100ths of a
70 * second, depending on the OS. A long int will do fine for holding
73 #define TICK_TYPE long int
74 #define TICK_TO_DBL(t) ((double)(t) / TicksPerSecond)
76 static int TicksPerSecond = 0;
78 static TICK_TYPE ElapsedTimeStart = 0;
79 static TICK_TYPE CurrentElapsedTime = 0;
80 static TICK_TYPE CurrentUserTime = 0;
82 static TICK_TYPE InitUserTime = 0;
83 static TICK_TYPE InitElapsedTime = 0;
84 static TICK_TYPE InitElapsedStamp = 0;
86 static TICK_TYPE MutUserTime = 0;
87 static TICK_TYPE MutElapsedTime = 0;
88 static TICK_TYPE MutElapsedStamp = 0;
90 static TICK_TYPE ExitUserTime = 0;
91 static TICK_TYPE ExitElapsedTime = 0;
93 static ullong GC_tot_alloc = 0;
94 static ullong GC_tot_copied = 0;
96 static TICK_TYPE GC_start_time, GC_tot_time = 0; /* User GC Time */
97 static TICK_TYPE GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
99 lnat MaxResidency = 0; /* in words; for stats only */
100 lnat AvgResidency = 0;
101 lnat ResidencySamples = 0; /* for stats only */
103 static lnat GC_start_faults = 0, GC_end_faults = 0;
105 static TICK_TYPE *GC_coll_times;
107 static void getTimes(void);
108 static nat pageFaults(void);
110 /* elapsedtime() -- The current elapsed time in seconds */
112 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
113 #define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
114 /* Convert FILETIMEs into secs */
115 #define FT2longlong(ll,ft) \
116 (ll)=(ft).dwHighDateTime; \
118 (ll) |= (ft).dwLowDateTime; \
119 (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
122 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
123 /* cygwin32 or mingw32 version */
127 static int is_win9x = -1;
129 FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
130 long long int kT, uT;
133 /* figure out whether we're on a Win9x box or not. */
137 /* Need to init the size field first.*/
138 oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
139 b = GetVersionEx(&oi);
141 is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
145 /* On Win9x, just attribute all running time to the user. */
149 SystemTimeToFileTime(&st,&userTime);
151 /* ToDo: pin down elapsed times to just the OS thread(s) that
152 are evaluating/managing Haskell code.
154 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
155 &exitTime, &kernelTime, &userTime)) {
156 /* Probably on a Win95 box..*/
157 CurrentElapsedTime = 0;
163 FT2longlong(kT,kernelTime);
164 FT2longlong(uT,userTime);
165 CurrentElapsedTime = uT + kT;
166 CurrentUserTime = uT;
169 /* Adjust for the fact that we're using system time & not
170 process time on Win9x. */
171 CurrentUserTime -= ElapsedTimeStart;
172 CurrentElapsedTime -= ElapsedTimeStart;
183 /* We will #ifdef around the fprintf for machines
184 we *know* are unsupported. (WDP 94/05)
186 fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
189 #else /* not stumped */
191 clock_t r = times(&t);
193 CurrentElapsedTime = r;
194 CurrentUserTime = t.tms_utime;
200 /* mut_user_time_during_GC() and mut_user_time()
202 * The former function can be used to get the current mutator time
203 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
204 * used in the heap profiler for accurately time stamping the heap
207 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
208 * defined in stat_startGC() - to minimise system calls,
209 * GC_start_time is, however, only defined when really needed (check
210 * stat_startGC() for details)
213 mut_user_time_during_GC(void)
215 return TICK_TO_DBL(GC_start_time - GC_tot_time);
222 return TICK_TO_DBL(CurrentUserTime - GC_tot_time);
228 /* ToDo (on NT): better, get this via the performance data
229 that's stored in the registry. */
230 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
235 getrusage(RUSAGE_SELF, &t);
244 FILE *sf = RtsFlags.GcFlags.statsFile;
246 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
247 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
248 fprintf(sf, " bytes bytes bytes user elap user elap\n");
251 (TICK_TYPE *)stgMallocBytes(
252 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
254 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
255 GC_coll_times[i] = 0;
259 /* -----------------------------------------------------------------------------
260 Initialisation time...
261 -------------------------------------------------------------------------- */
266 /* Determine TicksPerSecond ... */
267 #if defined(CLK_TCK) /* defined by POSIX */
268 TicksPerSecond = CLK_TCK;
270 #elif defined(HAVE_SYSCONF)
273 ticks = sysconf(_SC_CLK_TCK);
275 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
276 stg_exit(EXIT_FAILURE);
278 TicksPerSecond = ticks;
280 /* no "sysconf" or CLK_TCK; had better guess */
284 #elif defined(CLOCKS_PER_SEC)
285 TicksPerSecond = CLOCKS_PER_SEC;
287 #else /* had better guess wildly */
288 /* We will #ifdef around the fprintf for machines
289 we *know* are unsupported. (WDP 94/05)
291 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
296 ElapsedTimeStart = CurrentElapsedTime;
303 InitUserTime = CurrentUserTime;
304 InitElapsedStamp = CurrentElapsedTime;
305 if (ElapsedTimeStart > CurrentElapsedTime) {
308 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
312 /* -----------------------------------------------------------------------------
313 stat_startExit and stat_endExit
315 These two measure the time taken in shutdownHaskell().
316 -------------------------------------------------------------------------- */
322 MutElapsedStamp = CurrentElapsedTime;
323 MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
324 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
326 /* for SMP, we don't know the mutator time yet, we have to inspect
327 * all the running threads to find out, and they haven't stopped
328 * yet. So we just timestamp MutUserTime at this point so we can
329 * calculate the EXIT time. The real MutUserTime is calculated
330 * in stat_exit below.
333 MutUserTime = CurrentUserTime;
335 MutUserTime = CurrentUserTime - GC_tot_time - InitUserTime;
336 if (MutUserTime < 0) { MutUserTime = 0; }
345 ExitUserTime = CurrentUserTime - MutUserTime;
347 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - InitUserTime;
349 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
350 if (ExitUserTime < 0) {
353 if (ExitElapsedTime < 0) {
358 /* -----------------------------------------------------------------------------
359 Called at the beginning of each GC
360 -------------------------------------------------------------------------- */
362 static nat rub_bell = 0;
364 /* initialise global variables needed during GC
366 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
367 * needed if either PROFILING or DEBUGing is enabled
372 nat bell = RtsFlags.GcFlags.ringBell;
376 fprintf(stderr, " GC ");
379 fprintf(stderr, "\007");
383 #if defined(PROFILING) || defined(DEBUG)
385 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
388 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
389 #if !defined(PROFILING) && !defined(DEBUG)
391 GC_start_time = CurrentUserTime;
393 GCe_start_time = CurrentElapsedTime;
394 if (RtsFlags.GcFlags.giveStats) {
395 GC_start_faults = pageFaults();
400 /* -----------------------------------------------------------------------------
401 Called at the end of each GC
402 -------------------------------------------------------------------------- */
405 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
407 FILE *sf = RtsFlags.GcFlags.statsFile;
409 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
410 TICK_TYPE time, etime, gc_time, gc_etime;
413 time = CurrentUserTime;
414 etime = CurrentElapsedTime;
415 gc_time = time - GC_start_time;
416 gc_etime = etime - GCe_start_time;
418 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
419 nat faults = pageFaults();
421 fprintf(sf, "%9ld %9ld %9ld",
422 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
423 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
424 TICK_TO_DBL(gc_time),
425 TICK_TO_DBL(gc_etime),
427 TICK_TO_DBL(etime - ElapsedTimeStart),
428 faults - GC_start_faults,
429 GC_start_faults - GC_end_faults,
432 GC_end_faults = faults;
436 GC_coll_times[gen] += gc_time;
438 GC_tot_copied += (ullong) copied;
439 GC_tot_alloc += (ullong) alloc;
440 GC_tot_time += gc_time;
441 GCe_tot_time += gc_etime;
446 pthread_t me = pthread_self();
448 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
449 if (me == task_ids[i].id) {
450 task_ids[i].gc_time += gc_time;
451 task_ids[i].gc_etime += gc_etime;
458 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
459 if (live > MaxResidency) {
463 AvgResidency += live;
468 fprintf(stderr, "\b\b\b \b\b\b");
473 /* -----------------------------------------------------------------------------
476 Called under SMP when a worker thread finishes. We drop the timing
477 stats for this thread into the task_ids struct for that thread.
478 -------------------------------------------------------------------------- */
482 stat_workerStop(void)
485 pthread_t me = pthread_self();
487 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
488 if (task_ids[i].id == me) {
489 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
490 task_ids[i].mut_etime = elapsedtime()
492 - task_ids[i].elapsedtimestart;
493 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
494 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
500 /* -----------------------------------------------------------------------------
501 Called at the end of execution
503 NOTE: number of allocations is not entirely accurate: it doesn't
504 take into account the few bytes at the end of the heap that
505 were left unused when the heap-check failed.
506 -------------------------------------------------------------------------- */
511 FILE *sf = RtsFlags.GcFlags.statsFile;
513 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
515 char temp[BIG_STRING_LEN];
518 nat g, total_collections = 0;
521 time = CurrentUserTime;
522 etime = CurrentElapsedTime - ElapsedTimeStart;
524 GC_tot_alloc += alloc;
526 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
527 if (time == 0.0) time = 1;
528 if (etime == 0.0) etime = 1;
530 /* Count total garbage collections */
531 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
532 total_collections += generations[g].collections;
534 /* For SMP, we have to get the user time from each thread
535 * and try to work out the total time.
540 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
541 MutUserTime += task_ids[i].mut_time;
544 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
545 if (MutUserTime < 0) { MutUserTime = 0; }
548 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
549 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
550 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
553 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
554 ullong_format_string(GC_tot_alloc*sizeof(W_),
555 temp, rtsTrue/*commas*/);
556 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
558 ullong_format_string(GC_tot_copied*sizeof(W_),
559 temp, rtsTrue/*commas*/);
560 fprintf(sf, "%11s bytes copied during GC\n", temp);
562 if ( ResidencySamples > 0 ) {
563 ullong_format_string(MaxResidency*sizeof(W_),
564 temp, rtsTrue/*commas*/);
565 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
566 temp, ResidencySamples);
570 /* Print garbage collections in each gen */
571 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
572 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
573 generations[g].collections, g,
574 TICK_TO_DBL(GC_coll_times[g]));
577 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
578 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
583 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
584 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
585 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
587 TICK_TO_DBL(task_ids[i].mut_time),
588 TICK_TO_DBL(task_ids[i].mut_etime),
589 TICK_TO_DBL(task_ids[i].gc_time),
590 TICK_TO_DBL(task_ids[i].gc_etime));
595 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
596 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
597 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
598 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
599 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
600 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
601 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
602 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
603 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
604 TICK_TO_DBL(time), TICK_TO_DBL(etime));
605 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
606 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
607 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
609 if (time - GC_tot_time == 0)
610 ullong_format_string(0, temp, rtsTrue/*commas*/);
612 ullong_format_string(
613 (ullong)((GC_tot_alloc*sizeof(W_))/
614 TICK_TO_DBL(time - GC_tot_time)),
615 temp, rtsTrue/*commas*/);
617 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
619 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
620 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
622 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
623 / TICK_TO_DBL(etime));
626 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
627 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
628 fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
629 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",
631 AvgResidency*sizeof(W_)/ResidencySamples,
632 MaxResidency*sizeof(W_),
634 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
635 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
636 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
637 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
645 /* -----------------------------------------------------------------------------
648 Produce some detailed info on the state of the generational GC.
649 -------------------------------------------------------------------------- */
651 statDescribeGens(void)
653 nat g, s, mut, mut_once, lge, live;
658 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
660 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
661 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
664 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
667 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
668 generations[g].max_blocks, mut, mut_once);
670 for (s = 0; s < generations[g].n_steps; s++) {
671 step = &generations[g].steps[s];
672 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
675 if (RtsFlags.GcFlags.generations == 1) {
676 bd = step->to_blocks;
680 for (; bd; bd = bd->link) {
681 live += (bd->free - bd->start) * sizeof(W_);
684 fprintf(stderr,"%46s","");
686 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
690 fprintf(stderr,"\n");
693 /* -----------------------------------------------------------------------------
694 Stats available via a programmatic interface, so eg. GHCi can time
695 each compilation and expression evaluation.
696 -------------------------------------------------------------------------- */
698 extern HsInt getAllocations( void )
699 { return (HsInt)(total_allocated * sizeof(W_)); }