1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.37 2001/11/23 10:27:58 simonmar 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
22 #include "Profiling.h"
28 #ifndef mingw32_TARGET_OS
29 # ifdef HAVE_SYS_TIMES_H
30 # include <sys/times.h>
34 #ifdef HAVE_SYS_TIME_H
44 #if ! irix_TARGET_OS && ! defined(mingw32_TARGET_OS)
45 # if defined(HAVE_SYS_RESOURCE_H)
46 # include <sys/resource.h>
50 #ifdef HAVE_SYS_TIMEB_H
51 #include <sys/timeb.h>
62 #if defined(PAR) || !(!defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS))
63 #include <sys/resource.h>
67 #define BIG_STRING_LEN 512
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
74 #define TICK_TYPE long int
75 #define TICK_TO_DBL(t) ((double)(t) / TicksPerSecond)
77 static int TicksPerSecond = 0;
79 static TICK_TYPE ElapsedTimeStart = 0;
80 static TICK_TYPE CurrentElapsedTime = 0;
81 static TICK_TYPE CurrentUserTime = 0;
83 static TICK_TYPE InitUserTime = 0;
84 static TICK_TYPE InitElapsedTime = 0;
85 static TICK_TYPE InitElapsedStamp = 0;
87 static TICK_TYPE MutUserTime = 0;
88 static TICK_TYPE MutElapsedTime = 0;
89 static TICK_TYPE MutElapsedStamp = 0;
91 static TICK_TYPE ExitUserTime = 0;
92 static TICK_TYPE ExitElapsedTime = 0;
94 static ullong GC_tot_alloc = 0;
95 static ullong GC_tot_copied = 0;
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
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
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
106 lnat MaxResidency = 0; /* in words; for stats only */
107 lnat AvgResidency = 0;
108 lnat ResidencySamples = 0; /* for stats only */
110 static lnat GC_start_faults = 0, GC_end_faults = 0;
112 static TICK_TYPE *GC_coll_times;
114 static void getTimes(void);
115 static nat pageFaults(void);
117 /* elapsedtime() -- The current elapsed time in seconds */
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; \
125 (ll) |= (ft).dwLowDateTime; \
126 (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
129 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
130 /* cygwin32 or mingw32 version */
134 static int is_win9x = -1;
136 FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
137 long long int kT, uT;
140 /* figure out whether we're on a Win9x box or not. */
144 /* Need to init the size field first.*/
145 oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
146 b = GetVersionEx(&oi);
148 is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
152 /* On Win9x, just attribute all running time to the user. */
156 SystemTimeToFileTime(&st,&userTime);
158 /* ToDo: pin down elapsed times to just the OS thread(s) that
159 are evaluating/managing Haskell code.
161 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
162 &exitTime, &kernelTime, &userTime)) {
163 /* Probably on a Win95 box..*/
164 CurrentElapsedTime = 0;
170 FT2longlong(kT,kernelTime);
171 FT2longlong(uT,userTime);
172 CurrentElapsedTime = uT + kT;
173 CurrentUserTime = uT;
176 /* Adjust for the fact that we're using system time & not
177 process time on Win9x. */
178 CurrentUserTime -= ElapsedTimeStart;
179 CurrentElapsedTime -= ElapsedTimeStart;
190 /* We will #ifdef around the fprintf for machines
191 we *know* are unsupported. (WDP 94/05)
193 fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
196 #else /* not stumped */
198 clock_t r = times(&t);
200 CurrentElapsedTime = r;
201 CurrentUserTime = t.tms_utime;
207 /* mut_user_time_during_GC() and mut_user_time()
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
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)
220 mut_user_time_during_GC( void )
222 return TICK_TO_DBL(GC_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
226 mut_user_time( void )
229 return TICK_TO_DBL(CurrentUserTime - GC_tot_time - RP_tot_time - LDV_tot_time);
234 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
235 it returns the MUT time during retainer profiling.
236 The same is for mut_user_time_during_LDV();
239 mut_user_time_during_RP( void )
241 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
245 mut_user_time_during_LDV( void )
247 return TICK_TO_DBL(LDV_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
254 /* ToDo (on NT): better, get this via the performance data
255 that's stored in the registry. */
256 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
261 getrusage(RUSAGE_SELF, &t);
270 FILE *sf = RtsFlags.GcFlags.statsFile;
272 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
273 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
274 fprintf(sf, " bytes bytes bytes user elap user elap\n");
277 (TICK_TYPE *)stgMallocBytes(
278 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
280 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
281 GC_coll_times[i] = 0;
285 /* -----------------------------------------------------------------------------
286 Initialisation time...
287 -------------------------------------------------------------------------- */
292 /* Determine TicksPerSecond ... */
293 #if defined(CLK_TCK) /* defined by POSIX */
294 TicksPerSecond = CLK_TCK;
296 #elif defined(HAVE_SYSCONF)
299 ticks = sysconf(_SC_CLK_TCK);
301 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
302 stg_exit(EXIT_FAILURE);
304 TicksPerSecond = ticks;
306 /* no "sysconf" or CLK_TCK; had better guess */
310 #elif defined(CLOCKS_PER_SEC)
311 TicksPerSecond = CLOCKS_PER_SEC;
313 #else /* had better guess wildly */
314 /* We will #ifdef around the fprintf for machines
315 we *know* are unsupported. (WDP 94/05)
317 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
322 ElapsedTimeStart = CurrentElapsedTime;
329 InitUserTime = CurrentUserTime;
330 InitElapsedStamp = CurrentElapsedTime;
331 if (ElapsedTimeStart > CurrentElapsedTime) {
334 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
338 /* -----------------------------------------------------------------------------
339 stat_startExit and stat_endExit
341 These two measure the time taken in shutdownHaskell().
342 -------------------------------------------------------------------------- */
348 MutElapsedStamp = CurrentElapsedTime;
349 MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
350 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
352 /* for SMP, we don't know the mutator time yet, we have to inspect
353 * all the running threads to find out, and they haven't stopped
354 * yet. So we just timestamp MutUserTime at this point so we can
355 * calculate the EXIT time. The real MutUserTime is calculated
356 * in stat_exit below.
359 MutUserTime = CurrentUserTime;
361 MutUserTime = CurrentUserTime - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime;
362 if (MutUserTime < 0) { MutUserTime = 0; }
371 ExitUserTime = CurrentUserTime - MutUserTime;
373 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime;
375 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
376 if (ExitUserTime < 0) {
379 if (ExitElapsedTime < 0) {
384 /* -----------------------------------------------------------------------------
385 Called at the beginning of each GC
386 -------------------------------------------------------------------------- */
388 static nat rub_bell = 0;
390 /* initialise global variables needed during GC
392 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
393 * needed if either PROFILING or DEBUGing is enabled
398 nat bell = RtsFlags.GcFlags.ringBell;
402 fprintf(stderr, " GC ");
405 fprintf(stderr, "\007");
409 #if defined(PROFILING) || defined(DEBUG)
411 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
414 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
415 #if !defined(PROFILING) && !defined(DEBUG)
417 GC_start_time = CurrentUserTime;
419 GCe_start_time = CurrentElapsedTime;
420 if (RtsFlags.GcFlags.giveStats) {
421 GC_start_faults = pageFaults();
426 /* -----------------------------------------------------------------------------
427 Called at the end of each GC
428 -------------------------------------------------------------------------- */
431 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
433 FILE *sf = RtsFlags.GcFlags.statsFile;
435 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
436 TICK_TYPE time, etime, gc_time, gc_etime;
439 time = CurrentUserTime;
440 etime = CurrentElapsedTime;
441 gc_time = time - GC_start_time;
442 gc_etime = etime - GCe_start_time;
444 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
445 nat faults = pageFaults();
447 fprintf(sf, "%9ld %9ld %9ld",
448 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
449 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
450 TICK_TO_DBL(gc_time),
451 TICK_TO_DBL(gc_etime),
453 TICK_TO_DBL(etime - ElapsedTimeStart),
454 faults - GC_start_faults,
455 GC_start_faults - GC_end_faults,
458 GC_end_faults = faults;
462 GC_coll_times[gen] += gc_time;
464 GC_tot_copied += (ullong) copied;
465 GC_tot_alloc += (ullong) alloc;
466 GC_tot_time += gc_time;
467 GCe_tot_time += gc_etime;
472 pthread_t me = pthread_self();
474 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
475 if (me == task_ids[i].id) {
476 task_ids[i].gc_time += gc_time;
477 task_ids[i].gc_etime += gc_etime;
484 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
485 if (live > MaxResidency) {
489 AvgResidency += live;
494 fprintf(stderr, "\b\b\b \b\b\b");
499 /* -----------------------------------------------------------------------------
500 Called at the beginning of each Retainer Profiliing
501 -------------------------------------------------------------------------- */
507 RP_start_time = CurrentUserTime;
508 RPe_start_time = CurrentElapsedTime;
512 /* -----------------------------------------------------------------------------
513 Called at the end of each Retainer Profiliing
514 -------------------------------------------------------------------------- */
519 nat retainerGeneration,
520 #ifdef DEBUG_RETAINER
524 double averageNumVisit,
529 RP_tot_time += CurrentUserTime - RP_start_time;
530 RPe_tot_time += CurrentElapsedTime - RPe_start_time;
532 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
533 retainerGeneration, mut_user_time_during_RP());
534 #ifdef DEBUG_RETAINER
535 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
536 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
538 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
539 fprintf(prof_file, "\tCurrent total costs in bytes = %u\n", allCost * sizeof(StgWord));
540 fprintf(prof_file, "\tNumber of retainer sets = %u\n\n", numSet);
544 /* -----------------------------------------------------------------------------
545 Called at the beginning of each LDV Profiliing
546 -------------------------------------------------------------------------- */
552 LDV_start_time = CurrentUserTime;
553 LDVe_start_time = CurrentElapsedTime;
557 /* -----------------------------------------------------------------------------
558 Called at the end of each LDV Profiliing
559 -------------------------------------------------------------------------- */
565 LDV_tot_time += CurrentUserTime - LDV_start_time;
566 LDVe_tot_time += CurrentElapsedTime - LDVe_start_time;
570 /* -----------------------------------------------------------------------------
573 Called under SMP when a worker thread finishes. We drop the timing
574 stats for this thread into the task_ids struct for that thread.
575 -------------------------------------------------------------------------- */
579 stat_workerStop(void)
582 pthread_t me = pthread_self();
584 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
585 if (task_ids[i].id == me) {
586 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
587 task_ids[i].mut_etime = elapsedtime()
589 - task_ids[i].elapsedtimestart;
590 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
591 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
597 /* -----------------------------------------------------------------------------
598 Called at the end of execution
600 NOTE: number of allocations is not entirely accurate: it doesn't
601 take into account the few bytes at the end of the heap that
602 were left unused when the heap-check failed.
603 -------------------------------------------------------------------------- */
608 FILE *sf = RtsFlags.GcFlags.statsFile;
610 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
612 char temp[BIG_STRING_LEN];
615 nat g, total_collections = 0;
618 time = CurrentUserTime;
619 etime = CurrentElapsedTime - ElapsedTimeStart;
621 GC_tot_alloc += alloc;
623 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
624 if (time == 0.0) time = 1;
625 if (etime == 0.0) etime = 1;
627 /* Count total garbage collections */
628 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
629 total_collections += generations[g].collections;
631 /* For SMP, we have to get the user time from each thread
632 * and try to work out the total time.
637 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
638 MutUserTime += task_ids[i].mut_time;
641 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
642 if (MutUserTime < 0) { MutUserTime = 0; }
645 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
646 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
647 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
650 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
651 ullong_format_string(GC_tot_alloc*sizeof(W_),
652 temp, rtsTrue/*commas*/);
653 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
655 ullong_format_string(GC_tot_copied*sizeof(W_),
656 temp, rtsTrue/*commas*/);
657 fprintf(sf, "%11s bytes copied during GC\n", temp);
659 if ( ResidencySamples > 0 ) {
660 ullong_format_string(MaxResidency*sizeof(W_),
661 temp, rtsTrue/*commas*/);
662 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
663 temp, ResidencySamples);
667 /* Print garbage collections in each gen */
668 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
669 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
670 generations[g].collections, g,
671 TICK_TO_DBL(GC_coll_times[g]));
674 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
675 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
680 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
681 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
682 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
684 TICK_TO_DBL(task_ids[i].mut_time),
685 TICK_TO_DBL(task_ids[i].mut_etime),
686 TICK_TO_DBL(task_ids[i].gc_time),
687 TICK_TO_DBL(task_ids[i].gc_etime));
692 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
693 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
694 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
695 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
696 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
697 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
699 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER)
700 fprintf(sf, " RP time %6.2fs (%6.2fs elapsed)\n",
701 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
702 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV)
703 fprintf(sf, " LDV time %6.2fs (%6.2fs elapsed)\n",
704 TICK_TO_DBL(LDV_tot_time), TICK_TO_DBL(LDVe_tot_time));
706 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
707 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
708 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
709 TICK_TO_DBL(time), TICK_TO_DBL(etime));
710 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
711 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
712 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
714 if (time - GC_tot_time - RP_tot_time - LDV_tot_time == 0)
715 ullong_format_string(0, temp, rtsTrue/*commas*/);
717 ullong_format_string(
718 (ullong)((GC_tot_alloc*sizeof(W_))/
719 TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time)),
720 temp, rtsTrue/*commas*/);
722 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
724 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
725 TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime) * 100
727 TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime) * 100
728 / TICK_TO_DBL(etime));
731 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
732 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
733 fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
734 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",
736 AvgResidency*sizeof(W_)/ResidencySamples,
737 MaxResidency*sizeof(W_),
739 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
740 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
741 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
742 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
750 /* -----------------------------------------------------------------------------
753 Produce some detailed info on the state of the generational GC.
754 -------------------------------------------------------------------------- */
757 statDescribeGens(void)
759 nat g, s, mut, mut_once, lge, live;
764 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
766 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
767 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
770 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
773 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
774 generations[g].max_blocks, mut, mut_once);
776 for (s = 0; s < generations[g].n_steps; s++) {
777 step = &generations[g].steps[s];
778 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
781 if (RtsFlags.GcFlags.generations == 1) {
782 bd = step->to_blocks;
786 for (; bd; bd = bd->link) {
787 live += (bd->free - bd->start) * sizeof(W_);
790 fprintf(stderr,"%46s","");
792 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
796 fprintf(stderr,"\n");
800 /* -----------------------------------------------------------------------------
801 Stats available via a programmatic interface, so eg. GHCi can time
802 each compilation and expression evaluation.
803 -------------------------------------------------------------------------- */
805 extern HsInt getAllocations( void )
806 { return (HsInt)(total_allocated * sizeof(W_)); }