1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.32 2001/08/14 13:40:09 sewardj 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 FILETIME creationTime, exitTime, kernelTime, userTime;
128 long long int kT, uT;
130 /* ToDo: pin down elapsed times to just the OS thread(s) that
131 are evaluating/managing Haskell code.
133 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
134 &exitTime, &kernelTime, &userTime)) {
135 /* Probably on a Win95 box..*/
136 CurrentElapsedTime = 0;
141 FT2longlong(kT,kernelTime);
142 FT2longlong(uT,userTime);
143 CurrentElapsedTime = uT + kT;
144 CurrentUserTime = uT;
154 /* We will #ifdef around the fprintf for machines
155 we *know* are unsupported. (WDP 94/05)
157 fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
160 #else /* not stumped */
162 clock_t r = times(&t);
164 CurrentElapsedTime = r;
165 CurrentUserTime = t.tms_utime;
171 /* mut_user_time_during_GC() and mut_user_time()
173 * The former function can be used to get the current mutator time
174 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
175 * used in the heap profiler for accurately time stamping the heap
178 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
179 * defined in stat_startGC() - to minimise system calls,
180 * GC_start_time is, however, only defined when really needed (check
181 * stat_startGC() for details)
184 mut_user_time_during_GC(void)
186 return TICK_TO_DBL(GC_start_time - GC_tot_time);
193 return TICK_TO_DBL(CurrentUserTime - GC_tot_time);
199 /* ToDo (on NT): better, get this via the performance data
200 that's stored in the registry. */
201 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
206 getrusage(RUSAGE_SELF, &t);
215 FILE *sf = RtsFlags.GcFlags.statsFile;
217 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
218 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
219 fprintf(sf, " bytes bytes bytes user elap user elap\n");
222 (TICK_TYPE *)stgMallocBytes(
223 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
225 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
226 GC_coll_times[i] = 0;
230 /* -----------------------------------------------------------------------------
231 Initialisation time...
232 -------------------------------------------------------------------------- */
237 /* Determine TicksPerSecond ... */
238 #if defined(CLK_TCK) /* defined by POSIX */
239 TicksPerSecond = CLK_TCK;
241 #elif defined(HAVE_SYSCONF)
244 ticks = sysconf(_SC_CLK_TCK);
246 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
247 stg_exit(EXIT_FAILURE);
249 TicksPerSecond = ticks;
251 /* no "sysconf" or CLK_TCK; had better guess */
255 #elif defined(CLOCKS_PER_SEC)
256 TicksPerSecond = CLOCKS_PER_SEC;
258 #else /* had better guess wildly */
259 /* We will #ifdef around the fprintf for machines
260 we *know* are unsupported. (WDP 94/05)
262 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
267 ElapsedTimeStart = CurrentElapsedTime;
274 InitUserTime = CurrentUserTime;
275 InitElapsedStamp = CurrentElapsedTime;
276 if (ElapsedTimeStart > CurrentElapsedTime) {
279 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
283 /* -----------------------------------------------------------------------------
284 stat_startExit and stat_endExit
286 These two measure the time taken in shutdownHaskell().
287 -------------------------------------------------------------------------- */
293 MutElapsedStamp = CurrentElapsedTime;
294 MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
295 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
297 /* for SMP, we don't know the mutator time yet, we have to inspect
298 * all the running threads to find out, and they haven't stopped
299 * yet. So we just timestamp MutUserTime at this point so we can
300 * calculate the EXIT time. The real MutUserTime is calculated
301 * in stat_exit below.
304 MutUserTime = CurrentUserTime;
306 MutUserTime = CurrentUserTime - GC_tot_time - InitUserTime;
307 if (MutUserTime < 0) { MutUserTime = 0; }
316 ExitUserTime = CurrentUserTime - MutUserTime;
318 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - InitUserTime;
320 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
321 if (ExitUserTime < 0) {
324 if (ExitElapsedTime < 0) {
329 /* -----------------------------------------------------------------------------
330 Called at the beginning of each GC
331 -------------------------------------------------------------------------- */
333 static nat rub_bell = 0;
335 /* initialise global variables needed during GC
337 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
338 * needed if either PROFILING or DEBUGing is enabled
343 nat bell = RtsFlags.GcFlags.ringBell;
347 fprintf(stderr, " GC ");
350 fprintf(stderr, "\007");
354 #if defined(PROFILING) || defined(DEBUG)
356 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
359 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
360 #if !defined(PROFILING) && !defined(DEBUG)
362 GC_start_time = CurrentUserTime;
364 GCe_start_time = CurrentElapsedTime;
365 if (RtsFlags.GcFlags.giveStats) {
366 GC_start_faults = pageFaults();
371 /* -----------------------------------------------------------------------------
372 Called at the end of each GC
373 -------------------------------------------------------------------------- */
376 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
378 FILE *sf = RtsFlags.GcFlags.statsFile;
380 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
381 TICK_TYPE time, etime, gc_time, gc_etime;
384 time = CurrentUserTime;
385 etime = CurrentElapsedTime;
386 gc_time = time - GC_start_time;
387 gc_etime = etime - GCe_start_time;
389 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
390 nat faults = pageFaults();
392 fprintf(sf, "%9ld %9ld %9ld",
393 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
394 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
395 TICK_TO_DBL(gc_time),
396 TICK_TO_DBL(gc_etime),
398 TICK_TO_DBL(etime - ElapsedTimeStart),
399 faults - GC_start_faults,
400 GC_start_faults - GC_end_faults,
403 GC_end_faults = faults;
407 GC_coll_times[gen] += gc_time;
409 GC_tot_copied += (ullong) copied;
410 GC_tot_alloc += (ullong) alloc;
411 GC_tot_time += gc_time;
412 GCe_tot_time += gc_etime;
417 pthread_t me = pthread_self();
419 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
420 if (me == task_ids[i].id) {
421 task_ids[i].gc_time += gc_time;
422 task_ids[i].gc_etime += gc_etime;
429 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
430 if (live > MaxResidency) {
434 AvgResidency += live;
439 fprintf(stderr, "\b\b\b \b\b\b");
444 /* -----------------------------------------------------------------------------
447 Called under SMP when a worker thread finishes. We drop the timing
448 stats for this thread into the task_ids struct for that thread.
449 -------------------------------------------------------------------------- */
453 stat_workerStop(void)
456 pthread_t me = pthread_self();
458 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
459 if (task_ids[i].id == me) {
460 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
461 task_ids[i].mut_etime = elapsedtime()
463 - task_ids[i].elapsedtimestart;
464 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
465 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
471 /* -----------------------------------------------------------------------------
472 Called at the end of execution
474 NOTE: number of allocations is not entirely accurate: it doesn't
475 take into account the few bytes at the end of the heap that
476 were left unused when the heap-check failed.
477 -------------------------------------------------------------------------- */
482 FILE *sf = RtsFlags.GcFlags.statsFile;
484 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
486 char temp[BIG_STRING_LEN];
489 nat g, total_collections = 0;
492 time = CurrentUserTime;
493 etime = CurrentElapsedTime - ElapsedTimeStart;
495 GC_tot_alloc += alloc;
497 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
498 if (time == 0.0) time = 1;
499 if (etime == 0.0) etime = 1;
501 /* Count total garbage collections */
502 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
503 total_collections += generations[g].collections;
505 /* For SMP, we have to get the user time from each thread
506 * and try to work out the total time.
511 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
512 MutUserTime += task_ids[i].mut_time;
515 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
516 if (MutUserTime < 0) { MutUserTime = 0; }
519 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
520 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
521 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
524 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
525 ullong_format_string(GC_tot_alloc*sizeof(W_),
526 temp, rtsTrue/*commas*/);
527 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
529 ullong_format_string(GC_tot_copied*sizeof(W_),
530 temp, rtsTrue/*commas*/);
531 fprintf(sf, "%11s bytes copied during GC\n", temp);
533 if ( ResidencySamples > 0 ) {
534 ullong_format_string(MaxResidency*sizeof(W_),
535 temp, rtsTrue/*commas*/);
536 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
537 temp, ResidencySamples);
541 /* Print garbage collections in each gen */
542 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
543 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
544 generations[g].collections, g,
545 TICK_TO_DBL(GC_coll_times[g]));
548 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
549 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
554 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
555 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
556 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
558 TICK_TO_DBL(task_ids[i].mut_time),
559 TICK_TO_DBL(task_ids[i].mut_etime),
560 TICK_TO_DBL(task_ids[i].gc_time),
561 TICK_TO_DBL(task_ids[i].gc_etime));
566 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
567 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
568 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
569 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
570 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
571 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
572 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
573 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
574 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
575 TICK_TO_DBL(time), TICK_TO_DBL(etime));
576 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
577 TICK_TO_DBL(GC_tot_time)*100/time,
578 TICK_TO_DBL(GCe_tot_time)*100/etime);
580 if (time - GC_tot_time == 0)
581 ullong_format_string(0, temp, rtsTrue/*commas*/);
583 ullong_format_string(
584 (ullong)((GC_tot_alloc*sizeof(W_))/
585 TICK_TO_DBL(time - GC_tot_time)),
586 temp, rtsTrue/*commas*/);
588 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
590 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
591 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
593 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
594 / TICK_TO_DBL(etime));
597 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
598 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
599 fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
600 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",
602 AvgResidency*sizeof(W_)/ResidencySamples,
603 MaxResidency*sizeof(W_),
605 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
606 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
607 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
608 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
616 /* -----------------------------------------------------------------------------
619 Produce some detailed info on the state of the generational GC.
620 -------------------------------------------------------------------------- */
622 statDescribeGens(void)
624 nat g, s, mut, mut_once, lge, live;
629 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
631 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
632 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
635 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
638 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
639 generations[g].max_blocks, mut, mut_once);
641 for (s = 0; s < generations[g].n_steps; s++) {
642 step = &generations[g].steps[s];
643 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
646 if (RtsFlags.GcFlags.generations == 1) {
647 bd = step->to_blocks;
651 for (; bd; bd = bd->link) {
652 live += (bd->free - bd->start) * sizeof(W_);
655 fprintf(stderr,"%46s","");
657 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
661 fprintf(stderr,"\n");
664 /* -----------------------------------------------------------------------------
665 Stats available via a programmatic interface, so eg. GHCi can time
666 each compilation and expression evaluation.
667 -------------------------------------------------------------------------- */
669 extern HsInt getAllocations( void )
670 { return (HsInt)(total_allocated * sizeof(W_)); }