1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Statistics and timing-related functions.
7 * ---------------------------------------------------------------------------*/
9 /* Alas, no. This source is non-posix.
10 #include "PosixSource.h"
19 #include "ParTicky.h" /* ToDo: move into Rts.h */
20 #include "Profiling.h"
27 #ifndef mingw32_HOST_OS
28 # ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
33 #ifdef HAVE_SYS_TIME_H
43 #if ! irix_HOST_OS && ! defined(mingw32_HOST_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_HOST_OS || defined(mingw32_HOST_OS) || defined(cygwin32_HOST_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 = 0, GC_tot_time = 0; /* User GC Time */
97 static TICK_TYPE GCe_start_time = 0, GCe_tot_time = 0; /* Elapsed GC time */
100 static TICK_TYPE RP_start_time = 0, RP_tot_time = 0; /* retainer prof user time */
101 static TICK_TYPE RPe_start_time = 0, RPe_tot_time = 0; /* retainer prof elap time */
103 static TICK_TYPE HC_start_time, HC_tot_time = 0; // heap census prof user time
104 static TICK_TYPE HCe_start_time, HCe_tot_time = 0; // heap census prof elap time
108 #define PROF_VAL(x) (x)
110 #define PROF_VAL(x) 0
113 static lnat MaxResidency = 0; // in words; for stats only
114 static lnat AvgResidency = 0;
115 static lnat ResidencySamples = 0; // for stats only
117 static lnat GC_start_faults = 0, GC_end_faults = 0;
119 static TICK_TYPE *GC_coll_times;
121 static void getTimes(void);
122 static nat pageFaults(void);
124 static void statsPrintf( char *s, ... )
125 GNUC3_ATTRIBUTE(format (printf, 1, 2));
127 static void statsFlush( void );
128 static void statsClose( void );
130 /* elapsedtime() -- The current elapsed time in seconds */
132 #if defined(mingw32_HOST_OS) || defined(cygwin32_HOST_OS)
133 #define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
134 /* Convert FILETIMEs into secs */
135 #define FT2longlong(ll,ft) \
136 (ll)=(ft).dwHighDateTime; \
138 (ll) |= (ft).dwLowDateTime; \
139 (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
142 #if defined(mingw32_HOST_OS) || defined(cygwin32_HOST_OS)
143 /* cygwin32 or mingw32 version */
147 static int is_win9x = -1;
149 FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
150 long long int kT, uT;
153 /* figure out whether we're on a Win9x box or not. */
157 /* Need to init the size field first.*/
158 oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
159 b = GetVersionEx(&oi);
161 is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
165 /* On Win9x, just attribute all running time to the user. */
169 SystemTimeToFileTime(&st,&userTime);
171 /* ToDo: pin down elapsed times to just the OS thread(s) that
172 are evaluating/managing Haskell code.
174 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
175 &exitTime, &kernelTime, &userTime)) {
176 /* Probably on a Win95 box..*/
177 CurrentElapsedTime = 0;
183 FT2longlong(kT,kernelTime);
184 FT2longlong(uT,userTime);
185 CurrentElapsedTime = uT + kT;
186 CurrentUserTime = uT;
189 /* Adjust for the fact that we're using system time & not
190 process time on Win9x. */
191 CurrentUserTime -= ElapsedTimeStart;
192 CurrentElapsedTime -= ElapsedTimeStart;
203 /* We will #ifdef around the fprintf for machines
204 we *know* are unsupported. (WDP 94/05)
206 debugBelch("NOTE: `getTimes' does nothing!\n");
209 #else /* not stumped */
211 clock_t r = times(&t);
213 CurrentElapsedTime = r;
214 CurrentUserTime = t.tms_utime;
220 /* mut_user_time_during_GC() and mut_user_time()
222 * The former function can be used to get the current mutator time
223 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
224 * used in the heap profiler for accurately time stamping the heap
227 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
228 * defined in stat_startGC() - to minimise system calls,
229 * GC_start_time is, however, only defined when really needed (check
230 * stat_startGC() for details)
233 mut_user_time_during_GC( void )
235 return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
239 mut_user_time( void )
242 return TICK_TO_DBL(CurrentUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
247 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
248 it returns the MUT time during retainer profiling.
249 The same is for mut_user_time_during_HC();
252 mut_user_time_during_RP( void )
254 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
258 mut_user_time_during_heap_census( void )
260 return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
262 #endif /* PROFILING */
267 /* ToDo (on NT): better, get this via the performance data
268 that's stored in the registry. */
269 # if !defined(HAVE_GETRUSAGE) || irix_HOST_OS || defined(mingw32_HOST_OS) || defined(cygwin32_HOST_OS)
274 getrusage(RUSAGE_SELF, &t);
284 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
285 statsPrintf(" Alloc Collect Live GC GC TOT TOT Page Flts\n");
286 statsPrintf(" bytes bytes bytes user elap user elap\n");
289 (TICK_TYPE *)stgMallocBytes(
290 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
292 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
293 GC_coll_times[i] = 0;
297 /* -----------------------------------------------------------------------------
298 Initialisation time...
299 -------------------------------------------------------------------------- */
304 /* Determine TicksPerSecond ... */
305 #if defined(CLK_TCK) /* defined by POSIX */
306 TicksPerSecond = CLK_TCK;
308 #elif defined(HAVE_SYSCONF)
311 ticks = sysconf(_SC_CLK_TCK);
313 debugBelch("stat_init: bad call to 'sysconf'!\n");
314 stg_exit(EXIT_FAILURE);
316 TicksPerSecond = ticks;
318 /* no "sysconf" or CLK_TCK; had better guess */
322 #elif defined(CLOCKS_PER_SEC)
323 TicksPerSecond = CLOCKS_PER_SEC;
325 #else /* had better guess wildly */
326 /* We will #ifdef around the fprintf for machines
327 we *know* are unsupported. (WDP 94/05)
329 debugBelch("NOTE: Guessing `TicksPerSecond = 60'!\n");
334 ElapsedTimeStart = CurrentElapsedTime;
341 InitUserTime = CurrentUserTime;
342 InitElapsedStamp = CurrentElapsedTime;
343 if (ElapsedTimeStart > CurrentElapsedTime) {
346 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
350 /* -----------------------------------------------------------------------------
351 stat_startExit and stat_endExit
353 These two measure the time taken in shutdownHaskell().
354 -------------------------------------------------------------------------- */
360 MutElapsedStamp = CurrentElapsedTime;
361 MutElapsedTime = CurrentElapsedTime - GCe_tot_time -
362 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
363 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
365 /* for SMP, we don't know the mutator time yet, we have to inspect
366 * all the running threads to find out, and they haven't stopped
367 * yet. So we just timestamp MutUserTime at this point so we can
368 * calculate the EXIT time. The real MutUserTime is calculated
369 * in stat_exit below.
372 MutUserTime = CurrentUserTime;
374 MutUserTime = CurrentUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
375 if (MutUserTime < 0) { MutUserTime = 0; }
384 ExitUserTime = CurrentUserTime - MutUserTime;
386 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
388 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
389 if (ExitUserTime < 0) {
392 if (ExitElapsedTime < 0) {
397 /* -----------------------------------------------------------------------------
398 Called at the beginning of each GC
399 -------------------------------------------------------------------------- */
401 static nat rub_bell = 0;
403 /* initialise global variables needed during GC
405 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
406 * needed if either PROFILING or DEBUGing is enabled
411 nat bell = RtsFlags.GcFlags.ringBell;
422 #if defined(PROFILING) || defined(DEBUG)
424 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
427 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
428 #if !defined(PROFILING) && !defined(DEBUG)
430 GC_start_time = CurrentUserTime;
432 GCe_start_time = CurrentElapsedTime;
433 if (RtsFlags.GcFlags.giveStats) {
434 GC_start_faults = pageFaults();
439 /* -----------------------------------------------------------------------------
440 Called at the end of each GC
441 -------------------------------------------------------------------------- */
444 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
446 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
447 TICK_TYPE time, etime, gc_time, gc_etime;
450 time = CurrentUserTime;
451 etime = CurrentElapsedTime;
452 gc_time = time - GC_start_time;
453 gc_etime = etime - GCe_start_time;
455 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
456 nat faults = pageFaults();
458 statsPrintf("%9ld %9ld %9ld",
459 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
460 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
461 TICK_TO_DBL(gc_time),
462 TICK_TO_DBL(gc_etime),
464 TICK_TO_DBL(etime - ElapsedTimeStart),
465 faults - GC_start_faults,
466 GC_start_faults - GC_end_faults,
469 GC_end_faults = faults;
473 GC_coll_times[gen] += gc_time;
475 GC_tot_copied += (ullong) copied;
476 GC_tot_alloc += (ullong) alloc;
477 GC_tot_time += gc_time;
478 GCe_tot_time += gc_etime;
483 pthread_t me = pthread_self();
485 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
486 if (me == task_ids[i].id) {
487 task_ids[i].gc_time += gc_time;
488 task_ids[i].gc_etime += gc_etime;
495 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
496 if (live > MaxResidency) {
500 AvgResidency += live;
505 debugBelch("\b\b\b \b\b\b");
510 /* -----------------------------------------------------------------------------
511 Called at the beginning of each Retainer Profiliing
512 -------------------------------------------------------------------------- */
518 RP_start_time = CurrentUserTime;
519 RPe_start_time = CurrentElapsedTime;
521 #endif /* PROFILING */
523 /* -----------------------------------------------------------------------------
524 Called at the end of each Retainer Profiliing
525 -------------------------------------------------------------------------- */
530 nat retainerGeneration,
531 #ifdef DEBUG_RETAINER
535 double averageNumVisit)
538 RP_tot_time += CurrentUserTime - RP_start_time;
539 RPe_tot_time += CurrentElapsedTime - RPe_start_time;
541 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
542 retainerGeneration, mut_user_time_during_RP());
543 #ifdef DEBUG_RETAINER
544 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
545 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
547 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
549 #endif /* PROFILING */
551 /* -----------------------------------------------------------------------------
552 Called at the beginning of each heap census
553 -------------------------------------------------------------------------- */
556 stat_startHeapCensus(void)
559 HC_start_time = CurrentUserTime;
560 HCe_start_time = CurrentElapsedTime;
562 #endif /* PROFILING */
564 /* -----------------------------------------------------------------------------
565 Called at the end of each heap census
566 -------------------------------------------------------------------------- */
569 stat_endHeapCensus(void)
572 HC_tot_time += CurrentUserTime - HC_start_time;
573 HCe_tot_time += CurrentElapsedTime - HCe_start_time;
575 #endif /* PROFILING */
577 /* -----------------------------------------------------------------------------
580 Called under SMP when a worker thread finishes. We drop the timing
581 stats for this thread into the task_ids struct for that thread.
582 -------------------------------------------------------------------------- */
586 stat_workerStop(void)
589 pthread_t me = pthread_self();
593 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
594 if (task_ids[i].id == me) {
595 task_ids[i].mut_time = CurrentUserTime - task_ids[i].gc_time;
596 task_ids[i].mut_etime = CurrentElapsedTime
598 - task_ids[i].elapsedtimestart;
599 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
600 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
607 long int stat_getElapsedTime ()
610 return CurrentElapsedTime;
614 /* -----------------------------------------------------------------------------
615 Called at the end of execution
617 NOTE: number of allocations is not entirely accurate: it doesn't
618 take into account the few bytes at the end of the heap that
619 were left unused when the heap-check failed.
620 -------------------------------------------------------------------------- */
625 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
627 char temp[BIG_STRING_LEN];
630 nat g, total_collections = 0;
633 time = CurrentUserTime;
634 etime = CurrentElapsedTime - ElapsedTimeStart;
636 GC_tot_alloc += alloc;
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;
642 /* Count total garbage collections */
643 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
644 total_collections += generations[g].collections;
646 /* For SMP, we have to get the user time from each thread
647 * and try to work out the total time.
652 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
653 MutUserTime += task_ids[i].mut_time;
656 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
657 if (MutUserTime < 0) { MutUserTime = 0; }
660 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
661 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
662 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
665 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
666 ullong_format_string(GC_tot_alloc*sizeof(W_),
667 temp, rtsTrue/*commas*/);
668 statsPrintf("%11s bytes allocated in the heap\n", temp);
670 ullong_format_string(GC_tot_copied*sizeof(W_),
671 temp, rtsTrue/*commas*/);
672 statsPrintf("%11s bytes copied during GC\n", temp);
674 if ( ResidencySamples > 0 ) {
675 ullong_format_string(MaxResidency*sizeof(W_),
676 temp, rtsTrue/*commas*/);
677 statsPrintf("%11s bytes maximum residency (%ld sample(s))\n",
678 temp, ResidencySamples);
682 /* Print garbage collections in each gen */
683 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
684 statsPrintf("%11d collections in generation %d (%6.2fs)\n",
685 generations[g].collections, g,
686 TICK_TO_DBL(GC_coll_times[g]));
689 statsPrintf("\n%11ld Mb total memory in use\n\n",
690 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
695 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
696 statsPrintf(" Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
697 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
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));
707 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
708 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
709 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
710 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
711 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
712 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
714 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
715 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
716 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
717 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
719 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
720 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
721 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
722 TICK_TO_DBL(time), TICK_TO_DBL(etime));
723 statsPrintf(" %%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));
727 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
728 ullong_format_string(0, temp, rtsTrue/*commas*/);
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*/);
736 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
738 statsPrintf(" 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
742 TICK_TO_DBL(time - GC_tot_time -
743 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
744 / TICK_TO_DBL(etime));
747 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
748 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
749 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
750 statsPrintf("%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",
752 ResidencySamples == 0 ? 0 :
753 AvgResidency*sizeof(W_)/ResidencySamples,
754 MaxResidency*sizeof(W_),
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));
767 /* -----------------------------------------------------------------------------
770 Produce some detailed info on the state of the generational GC.
771 -------------------------------------------------------------------------- */
774 statDescribeGens(void)
776 nat g, s, mut, lge, live;
780 debugBelch(" Gen Steps Max Mutable Step Blocks Live Large\n Blocks Closures Closures Objects\n");
783 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
784 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
785 mut += bd->free - bd->start;
788 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
789 generations[g].max_blocks, mut);
791 for (s = 0; s < generations[g].n_steps; s++) {
792 step = &generations[g].steps[s];
793 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
796 if (RtsFlags.GcFlags.generations == 1) {
797 bd = step->to_blocks;
801 for (; bd; bd = bd->link) {
802 live += (bd->free - bd->start) * sizeof(W_);
805 debugBelch("%46s","");
807 debugBelch("%6d %8d %8d %8d\n", s, step->n_blocks,
815 /* -----------------------------------------------------------------------------
816 Stats available via a programmatic interface, so eg. GHCi can time
817 each compilation and expression evaluation.
818 -------------------------------------------------------------------------- */
820 extern HsInt64 getAllocations( void )
821 { return (HsInt64)total_allocated * sizeof(W_); }
823 /* -----------------------------------------------------------------------------
824 Dumping stuff in the stats file, or via the debug message interface
825 -------------------------------------------------------------------------- */
828 statsPrintf( char *s, ... )
830 FILE *sf = RtsFlags.GcFlags.statsFile;
845 FILE *sf = RtsFlags.GcFlags.statsFile;
854 FILE *sf = RtsFlags.GcFlags.statsFile;