1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.36 2001/11/22 14:25:12 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);
233 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
234 it returns the MUT time during retainer profiling.
235 The same is for mut_user_time_during_LDV();
238 mut_user_time_during_RP( void )
240 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
244 mut_user_time_during_LDV( void )
246 return TICK_TO_DBL(LDV_start_time - GC_tot_time - RP_tot_time - LDV_tot_time);
252 /* ToDo (on NT): better, get this via the performance data
253 that's stored in the registry. */
254 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
259 getrusage(RUSAGE_SELF, &t);
268 FILE *sf = RtsFlags.GcFlags.statsFile;
270 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
271 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
272 fprintf(sf, " bytes bytes bytes user elap user elap\n");
275 (TICK_TYPE *)stgMallocBytes(
276 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
278 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
279 GC_coll_times[i] = 0;
283 /* -----------------------------------------------------------------------------
284 Initialisation time...
285 -------------------------------------------------------------------------- */
290 /* Determine TicksPerSecond ... */
291 #if defined(CLK_TCK) /* defined by POSIX */
292 TicksPerSecond = CLK_TCK;
294 #elif defined(HAVE_SYSCONF)
297 ticks = sysconf(_SC_CLK_TCK);
299 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
300 stg_exit(EXIT_FAILURE);
302 TicksPerSecond = ticks;
304 /* no "sysconf" or CLK_TCK; had better guess */
308 #elif defined(CLOCKS_PER_SEC)
309 TicksPerSecond = CLOCKS_PER_SEC;
311 #else /* had better guess wildly */
312 /* We will #ifdef around the fprintf for machines
313 we *know* are unsupported. (WDP 94/05)
315 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
320 ElapsedTimeStart = CurrentElapsedTime;
327 InitUserTime = CurrentUserTime;
328 InitElapsedStamp = CurrentElapsedTime;
329 if (ElapsedTimeStart > CurrentElapsedTime) {
332 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
336 /* -----------------------------------------------------------------------------
337 stat_startExit and stat_endExit
339 These two measure the time taken in shutdownHaskell().
340 -------------------------------------------------------------------------- */
346 MutElapsedStamp = CurrentElapsedTime;
347 MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
348 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
350 /* for SMP, we don't know the mutator time yet, we have to inspect
351 * all the running threads to find out, and they haven't stopped
352 * yet. So we just timestamp MutUserTime at this point so we can
353 * calculate the EXIT time. The real MutUserTime is calculated
354 * in stat_exit below.
357 MutUserTime = CurrentUserTime;
359 MutUserTime = CurrentUserTime - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime;
360 if (MutUserTime < 0) { MutUserTime = 0; }
369 ExitUserTime = CurrentUserTime - MutUserTime;
371 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime;
373 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
374 if (ExitUserTime < 0) {
377 if (ExitElapsedTime < 0) {
382 /* -----------------------------------------------------------------------------
383 Called at the beginning of each GC
384 -------------------------------------------------------------------------- */
386 static nat rub_bell = 0;
388 /* initialise global variables needed during GC
390 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
391 * needed if either PROFILING or DEBUGing is enabled
396 nat bell = RtsFlags.GcFlags.ringBell;
400 fprintf(stderr, " GC ");
403 fprintf(stderr, "\007");
407 #if defined(PROFILING) || defined(DEBUG)
409 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
412 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
413 #if !defined(PROFILING) && !defined(DEBUG)
415 GC_start_time = CurrentUserTime;
417 GCe_start_time = CurrentElapsedTime;
418 if (RtsFlags.GcFlags.giveStats) {
419 GC_start_faults = pageFaults();
424 /* -----------------------------------------------------------------------------
425 Called at the end of each GC
426 -------------------------------------------------------------------------- */
429 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
431 FILE *sf = RtsFlags.GcFlags.statsFile;
433 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
434 TICK_TYPE time, etime, gc_time, gc_etime;
437 time = CurrentUserTime;
438 etime = CurrentElapsedTime;
439 gc_time = time - GC_start_time;
440 gc_etime = etime - GCe_start_time;
442 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
443 nat faults = pageFaults();
445 fprintf(sf, "%9ld %9ld %9ld",
446 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
447 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
448 TICK_TO_DBL(gc_time),
449 TICK_TO_DBL(gc_etime),
451 TICK_TO_DBL(etime - ElapsedTimeStart),
452 faults - GC_start_faults,
453 GC_start_faults - GC_end_faults,
456 GC_end_faults = faults;
460 GC_coll_times[gen] += gc_time;
462 GC_tot_copied += (ullong) copied;
463 GC_tot_alloc += (ullong) alloc;
464 GC_tot_time += gc_time;
465 GCe_tot_time += gc_etime;
470 pthread_t me = pthread_self();
472 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
473 if (me == task_ids[i].id) {
474 task_ids[i].gc_time += gc_time;
475 task_ids[i].gc_etime += gc_etime;
482 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
483 if (live > MaxResidency) {
487 AvgResidency += live;
492 fprintf(stderr, "\b\b\b \b\b\b");
497 /* -----------------------------------------------------------------------------
498 Called at the beginning of each Retainer Profiliing
499 -------------------------------------------------------------------------- */
500 void stat_startRP(void)
503 RP_start_time = CurrentUserTime;
504 RPe_start_time = CurrentElapsedTime;
507 /* -----------------------------------------------------------------------------
508 Called at the end of each Retainer Profiliing
509 -------------------------------------------------------------------------- */
511 nat retainerGeneration,
512 #ifdef DEBUG_RETAINER
516 double averageNumVisit,
521 RP_tot_time += CurrentUserTime - RP_start_time;
522 RPe_tot_time += CurrentElapsedTime - RPe_start_time;
524 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
525 retainerGeneration, mut_user_time_during_RP());
526 #ifdef DEBUG_RETAINER
527 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
528 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
530 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
531 fprintf(prof_file, "\tCurrent total costs in bytes = %u\n", allCost * sizeof(StgWord));
532 fprintf(prof_file, "\tNumber of retainer sets = %u\n\n", numSet);
535 /* -----------------------------------------------------------------------------
536 Called at the beginning of each LDV Profiliing
537 -------------------------------------------------------------------------- */
538 void stat_startLDV(void)
541 LDV_start_time = CurrentUserTime;
542 LDVe_start_time = CurrentElapsedTime;
545 /* -----------------------------------------------------------------------------
546 Called at the end of each LDV Profiliing
547 -------------------------------------------------------------------------- */
548 void stat_endLDV(void)
551 LDV_tot_time += CurrentUserTime - LDV_start_time;
552 LDVe_tot_time += CurrentElapsedTime - LDVe_start_time;
555 /* -----------------------------------------------------------------------------
558 Called under SMP when a worker thread finishes. We drop the timing
559 stats for this thread into the task_ids struct for that thread.
560 -------------------------------------------------------------------------- */
564 stat_workerStop(void)
567 pthread_t me = pthread_self();
569 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
570 if (task_ids[i].id == me) {
571 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
572 task_ids[i].mut_etime = elapsedtime()
574 - task_ids[i].elapsedtimestart;
575 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
576 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
582 /* -----------------------------------------------------------------------------
583 Called at the end of execution
585 NOTE: number of allocations is not entirely accurate: it doesn't
586 take into account the few bytes at the end of the heap that
587 were left unused when the heap-check failed.
588 -------------------------------------------------------------------------- */
593 FILE *sf = RtsFlags.GcFlags.statsFile;
595 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
597 char temp[BIG_STRING_LEN];
600 nat g, total_collections = 0;
603 time = CurrentUserTime;
604 etime = CurrentElapsedTime - ElapsedTimeStart;
606 GC_tot_alloc += alloc;
608 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
609 if (time == 0.0) time = 1;
610 if (etime == 0.0) etime = 1;
612 /* Count total garbage collections */
613 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
614 total_collections += generations[g].collections;
616 /* For SMP, we have to get the user time from each thread
617 * and try to work out the total time.
622 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
623 MutUserTime += task_ids[i].mut_time;
626 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
627 if (MutUserTime < 0) { MutUserTime = 0; }
630 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
631 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
632 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
635 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
636 ullong_format_string(GC_tot_alloc*sizeof(W_),
637 temp, rtsTrue/*commas*/);
638 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
640 ullong_format_string(GC_tot_copied*sizeof(W_),
641 temp, rtsTrue/*commas*/);
642 fprintf(sf, "%11s bytes copied during GC\n", temp);
644 if ( ResidencySamples > 0 ) {
645 ullong_format_string(MaxResidency*sizeof(W_),
646 temp, rtsTrue/*commas*/);
647 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
648 temp, ResidencySamples);
652 /* Print garbage collections in each gen */
653 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
654 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
655 generations[g].collections, g,
656 TICK_TO_DBL(GC_coll_times[g]));
659 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
660 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
665 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
666 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
667 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
669 TICK_TO_DBL(task_ids[i].mut_time),
670 TICK_TO_DBL(task_ids[i].mut_etime),
671 TICK_TO_DBL(task_ids[i].gc_time),
672 TICK_TO_DBL(task_ids[i].gc_etime));
677 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
678 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
679 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
680 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
681 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
682 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
684 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER)
685 fprintf(sf, " RP time %6.2fs (%6.2fs elapsed)\n",
686 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
687 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV)
688 fprintf(sf, " LDV time %6.2fs (%6.2fs elapsed)\n",
689 TICK_TO_DBL(LDV_tot_time), TICK_TO_DBL(LDVe_tot_time));
691 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
692 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
693 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
694 TICK_TO_DBL(time), TICK_TO_DBL(etime));
695 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
696 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
697 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
699 if (time - GC_tot_time - RP_tot_time - LDV_tot_time == 0)
700 ullong_format_string(0, temp, rtsTrue/*commas*/);
702 ullong_format_string(
703 (ullong)((GC_tot_alloc*sizeof(W_))/
704 TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time)),
705 temp, rtsTrue/*commas*/);
707 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
709 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
710 TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime) * 100
712 TICK_TO_DBL(time - GC_tot_time - RP_tot_time - LDV_tot_time - InitUserTime) * 100
713 / TICK_TO_DBL(etime));
716 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
717 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
718 fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
719 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",
721 AvgResidency*sizeof(W_)/ResidencySamples,
722 MaxResidency*sizeof(W_),
724 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
725 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
726 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
727 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
735 /* -----------------------------------------------------------------------------
738 Produce some detailed info on the state of the generational GC.
739 -------------------------------------------------------------------------- */
741 statDescribeGens(void)
743 nat g, s, mut, mut_once, lge, live;
748 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
750 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
751 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
754 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
757 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
758 generations[g].max_blocks, mut, mut_once);
760 for (s = 0; s < generations[g].n_steps; s++) {
761 step = &generations[g].steps[s];
762 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
765 if (RtsFlags.GcFlags.generations == 1) {
766 bd = step->to_blocks;
770 for (; bd; bd = bd->link) {
771 live += (bd->free - bd->start) * sizeof(W_);
774 fprintf(stderr,"%46s","");
776 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
780 fprintf(stderr,"\n");
783 /* -----------------------------------------------------------------------------
784 Stats available via a programmatic interface, so eg. GHCi can time
785 each compilation and expression evaluation.
786 -------------------------------------------------------------------------- */
788 extern HsInt getAllocations( void )
789 { return (HsInt)(total_allocated * sizeof(W_)); }