1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.30 2001/07/08 17:04:04 sof Exp $
4 * (c) The GHC Team, 1998-1999
6 * Statistics and timing-related functions.
8 * ---------------------------------------------------------------------------*/
10 #define NON_POSIX_SOURCE
15 #include "StoragePriv.h"
19 #include "ParTicky.h" // ToDo: move into Rts.h
25 #ifndef mingw32_TARGET_OS
26 # ifdef HAVE_SYS_TIMES_H
27 # include <sys/times.h>
31 #ifdef HAVE_SYS_TIME_H
41 #if ! irix_TARGET_OS && ! defined(mingw32_TARGET_OS)
42 # if defined(HAVE_SYS_RESOURCE_H)
43 # include <sys/resource.h>
47 #ifdef HAVE_SYS_TIMEB_H
48 #include <sys/timeb.h>
59 #if defined(PAR) || !(!defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS))
60 #include <sys/resource.h>
64 #define BIG_STRING_LEN 512
66 /* We're not trying to be terribly accurate here, using the
67 * basic times() function to get a resolution of about 100ths of a
68 * second, depending on the OS. A long int will do fine for holding
71 #define TICK_TYPE long int
72 #define TICK_TO_DBL(t) ((double)(t) / TicksPerSecond)
74 static int TicksPerSecond = 0;
76 static TICK_TYPE ElapsedTimeStart = 0;
77 static TICK_TYPE CurrentElapsedTime = 0;
78 static TICK_TYPE CurrentUserTime = 0;
80 static TICK_TYPE InitUserTime = 0;
81 static TICK_TYPE InitElapsedTime = 0;
82 static TICK_TYPE InitElapsedStamp = 0;
84 static TICK_TYPE MutUserTime = 0;
85 static TICK_TYPE MutElapsedTime = 0;
86 static TICK_TYPE MutElapsedStamp = 0;
88 static TICK_TYPE ExitUserTime = 0;
89 static TICK_TYPE ExitElapsedTime = 0;
91 static ullong GC_tot_alloc = 0;
92 static ullong GC_tot_copied = 0;
94 static TICK_TYPE GC_start_time, GC_tot_time = 0; /* User GC Time */
95 static TICK_TYPE GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
97 lnat MaxResidency = 0; /* in words; for stats only */
98 lnat AvgResidency = 0;
99 lnat ResidencySamples = 0; /* for stats only */
101 static lnat GC_start_faults = 0, GC_end_faults = 0;
103 static TICK_TYPE *GC_coll_times;
105 static void getTimes(void);
106 static nat pageFaults(void);
108 /* elapsedtime() -- The current elapsed time in seconds */
110 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
111 #define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
112 /* Convert FILETIMEs into secs */
113 #define FT2longlong(ll,ft) \
114 (ll)=(ft).dwHighDateTime; \
116 (ll) |= (ft).dwLowDateTime; \
117 (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
120 #if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
121 /* cygwin32 or mingw32 version */
125 FILETIME creationTime, exitTime, kernelTime, userTime;
126 long long int kT, uT;
128 /* ToDo: pin down elapsed times to just the OS thread(s) that
129 are evaluating/managing Haskell code.
131 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
132 &exitTime, &kernelTime, &userTime)) {
133 /* Probably on a Win95 box..*/
134 CurrentElapsedTime = 0;
139 FT2longlong(kT,kernelTime);
140 FT2longlong(uT,userTime);
141 CurrentElapsedTime = uT + kT;
142 CurrentUserTime = uT;
152 /* We will #ifdef around the fprintf for machines
153 we *know* are unsupported. (WDP 94/05)
155 fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
158 #else /* not stumped */
160 clock_t r = times(&t);
162 CurrentElapsedTime = r;
163 CurrentUserTime = t.tms_utime;
169 /* mut_user_time_during_GC() and mut_user_time()
171 * The former function can be used to get the current mutator time
172 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
173 * used in the heap profiler for accurately time stamping the heap
176 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
177 * defined in stat_startGC() - to minimise system calls,
178 * GC_start_time is, however, only defined when really needed (check
179 * stat_startGC() for details)
182 mut_user_time_during_GC(void)
184 return TICK_TO_DBL(GC_start_time - GC_tot_time);
191 return TICK_TO_DBL(CurrentUserTime - GC_tot_time);
197 /* ToDo (on NT): better, get this via the performance data
198 that's stored in the registry. */
199 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
204 getrusage(RUSAGE_SELF, &t);
213 FILE *sf = RtsFlags.GcFlags.statsFile;
215 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
216 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
217 fprintf(sf, " bytes bytes bytes user elap user elap\n");
220 (TICK_TYPE *)stgMallocBytes(
221 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
223 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
224 GC_coll_times[i] = 0;
228 /* -----------------------------------------------------------------------------
229 Initialisation time...
230 -------------------------------------------------------------------------- */
235 /* Determine TicksPerSecond ... */
236 #if defined(CLK_TCK) /* defined by POSIX */
237 TicksPerSecond = CLK_TCK;
239 #elif defined(HAVE_SYSCONF)
242 ticks = sysconf(_SC_CLK_TCK);
244 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
245 stg_exit(EXIT_FAILURE);
247 TicksPerSecond = ticks;
249 /* no "sysconf" or CLK_TCK; had better guess */
253 #elif defined(CLOCKS_PER_SEC)
254 TicksPerSecond = CLOCKS_PER_SEC;
256 #else /* had better guess wildly */
257 /* We will #ifdef around the fprintf for machines
258 we *know* are unsupported. (WDP 94/05)
260 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
265 ElapsedTimeStart = CurrentElapsedTime;
272 InitUserTime = CurrentUserTime;
273 InitElapsedStamp = CurrentElapsedTime;
274 if (ElapsedTimeStart > CurrentElapsedTime) {
277 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
281 /* -----------------------------------------------------------------------------
282 stat_startExit and stat_endExit
284 These two measure the time taken in shutdownHaskell().
285 -------------------------------------------------------------------------- */
291 MutElapsedStamp = CurrentElapsedTime;
292 MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
293 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
295 /* for SMP, we don't know the mutator time yet, we have to inspect
296 * all the running threads to find out, and they haven't stopped
297 * yet. So we just timestamp MutUserTime at this point so we can
298 * calculate the EXIT time. The real MutUserTime is calculated
299 * in stat_exit below.
302 MutUserTime = CurrentUserTime;
304 MutUserTime = CurrentUserTime - GC_tot_time - InitUserTime;
305 if (MutUserTime < 0) { MutUserTime = 0; }
314 ExitUserTime = CurrentUserTime - MutUserTime;
316 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - InitUserTime;
318 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
319 if (ExitUserTime < 0) {
322 if (ExitElapsedTime < 0) {
327 /* -----------------------------------------------------------------------------
328 Called at the beginning of each GC
329 -------------------------------------------------------------------------- */
331 static nat rub_bell = 0;
333 /* initialise global variables needed during GC
335 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
336 * needed if either PROFILING or DEBUGing is enabled
341 nat bell = RtsFlags.GcFlags.ringBell;
345 fprintf(stderr, " GC ");
348 fprintf(stderr, "\007");
352 #if defined(PROFILING) || defined(DEBUG)
354 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
357 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
358 #if !defined(PROFILING) && !defined(DEBUG)
360 GC_start_time = CurrentUserTime;
362 GCe_start_time = CurrentElapsedTime;
363 if (RtsFlags.GcFlags.giveStats) {
364 GC_start_faults = pageFaults();
369 /* -----------------------------------------------------------------------------
370 Called at the end of each GC
371 -------------------------------------------------------------------------- */
374 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
376 FILE *sf = RtsFlags.GcFlags.statsFile;
378 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
379 TICK_TYPE time, etime, gc_time, gc_etime;
382 time = CurrentUserTime;
383 etime = CurrentElapsedTime;
384 gc_time = time - GC_start_time;
385 gc_etime = etime - GCe_start_time;
387 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
388 nat faults = pageFaults();
390 fprintf(sf, "%9ld %9ld %9ld",
391 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
392 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
393 TICK_TO_DBL(gc_time),
394 TICK_TO_DBL(gc_etime),
396 TICK_TO_DBL(etime - ElapsedTimeStart),
397 faults - GC_start_faults,
398 GC_start_faults - GC_end_faults,
401 GC_end_faults = faults;
405 GC_coll_times[gen] += gc_time;
407 GC_tot_copied += (ullong) copied;
408 GC_tot_alloc += (ullong) alloc;
409 GC_tot_time += gc_time;
410 GCe_tot_time += gc_etime;
415 pthread_t me = pthread_self();
417 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
418 if (me == task_ids[i].id) {
419 task_ids[i].gc_time += gc_time;
420 task_ids[i].gc_etime += gc_etime;
427 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
428 if (live > MaxResidency) {
432 AvgResidency += live;
437 fprintf(stderr, "\b\b\b \b\b\b");
442 /* -----------------------------------------------------------------------------
445 Called under SMP when a worker thread finishes. We drop the timing
446 stats for this thread into the task_ids struct for that thread.
447 -------------------------------------------------------------------------- */
451 stat_workerStop(void)
454 pthread_t me = pthread_self();
456 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
457 if (task_ids[i].id == me) {
458 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
459 task_ids[i].mut_etime = elapsedtime()
461 - task_ids[i].elapsedtimestart;
462 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
463 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
469 /* -----------------------------------------------------------------------------
470 Called at the end of execution
472 NOTE: number of allocations is not entirely accurate: it doesn't
473 take into account the few bytes at the end of the heap that
474 were left unused when the heap-check failed.
475 -------------------------------------------------------------------------- */
480 FILE *sf = RtsFlags.GcFlags.statsFile;
482 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
484 char temp[BIG_STRING_LEN];
487 nat g, total_collections = 0;
490 time = CurrentUserTime;
491 etime = CurrentElapsedTime - ElapsedTimeStart;
493 GC_tot_alloc += alloc;
495 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
496 if (time == 0.0) time = 1;
497 if (etime == 0.0) etime = 1;
499 /* Count total garbage collections */
500 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
501 total_collections += generations[g].collections;
503 /* For SMP, we have to get the user time from each thread
504 * and try to work out the total time.
509 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
510 MutUserTime += task_ids[i].mut_time;
513 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
514 if (MutUserTime < 0) { MutUserTime = 0; }
517 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
518 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
519 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
522 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
523 ullong_format_string(GC_tot_alloc*sizeof(W_),
524 temp, rtsTrue/*commas*/);
525 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
527 ullong_format_string(GC_tot_copied*sizeof(W_),
528 temp, rtsTrue/*commas*/);
529 fprintf(sf, "%11s bytes copied during GC\n", temp);
531 if ( ResidencySamples > 0 ) {
532 ullong_format_string(MaxResidency*sizeof(W_),
533 temp, rtsTrue/*commas*/);
534 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
535 temp, ResidencySamples);
539 /* Print garbage collections in each gen */
540 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
541 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
542 generations[g].collections, g,
543 TICK_TO_DBL(GC_coll_times[g]));
546 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
547 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
552 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
553 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
554 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
556 TICK_TO_DBL(task_ids[i].mut_time),
557 TICK_TO_DBL(task_ids[i].mut_etime),
558 TICK_TO_DBL(task_ids[i].gc_time),
559 TICK_TO_DBL(task_ids[i].gc_etime));
564 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
565 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
566 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
567 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
568 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
569 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
570 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
571 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
572 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
573 TICK_TO_DBL(time), TICK_TO_DBL(etime));
574 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
575 TICK_TO_DBL(GC_tot_time)*100/time,
576 TICK_TO_DBL(GCe_tot_time)*100/etime);
578 if (time - GC_tot_time == 0)
579 ullong_format_string(0, temp, rtsTrue/*commas*/);
581 ullong_format_string(
582 (ullong)((GC_tot_alloc*sizeof(W_))/
583 TICK_TO_DBL(time - GC_tot_time)),
584 temp, rtsTrue/*commas*/);
586 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
588 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
589 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
591 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
592 / TICK_TO_DBL(etime));
595 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
596 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
597 fprintf(sf, "<<ghc: %llu bytes, ", GC_tot_alloc*sizeof(W_));
598 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",
600 AvgResidency*sizeof(W_)/ResidencySamples,
601 MaxResidency*sizeof(W_),
603 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
604 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
605 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
606 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
614 /* -----------------------------------------------------------------------------
617 Produce some detailed info on the state of the generational GC.
618 -------------------------------------------------------------------------- */
620 stat_describe_gens(void)
622 nat g, s, mut, mut_once, lge, live;
627 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
629 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
630 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
633 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
636 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
637 generations[g].max_blocks, mut, mut_once);
639 for (s = 0; s < generations[g].n_steps; s++) {
640 step = &generations[g].steps[s];
641 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
644 if (RtsFlags.GcFlags.generations == 1) {
649 for (; bd; bd = bd->link) {
650 live += (bd->free - bd->start) * sizeof(W_);
653 fprintf(stderr,"%46s","");
655 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
659 fprintf(stderr,"\n");
662 /* -----------------------------------------------------------------------------
663 Stats available via a programmatic interface, so eg. GHCi can time
664 each compilation and expression evaluation.
665 -------------------------------------------------------------------------- */
667 extern HsInt getAllocations( void )
668 { return (HsInt)(total_allocated * sizeof(W_)); }