1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.25 2001/03/22 03:51:10 hwloidl 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(_WIN32))
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 */
111 #define NS_PER_SEC 10000000LL
112 /* Convert FILETIMEs into secs since the Epoch (Jan1-1970) */
113 #define FT2longlong(ll,ft) \
114 (ll)=(ft).dwHighDateTime; \
116 (ll) |= (ft).dwLowDateTime; \
117 (ll) /= (unsigned long long) (NS_PER_SEC / CLOCKS_PER_SEC)
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..*/
137 FT2longlong(kT,kernelTime);
138 FT2longlong(uT,userTime);
139 CurrentElapsedTime = uT + kT;
140 CurrentUserTime = uT;
149 # if !defined(HAVE_TIMES)
150 /* We will #ifdef around the fprintf for machines
151 we *know* are unsupported. (WDP 94/05)
153 fprintf(stderr, "NOTE: `getTimes' does nothing!\n");
156 # else /* not stumped */
158 clock_t r = times(&t);
160 CurrentElapsedTime = r;
161 CurrentUserTime = t.tms_utime;
167 /* mut_user_time_during_GC() and mut_user_time()
169 * The former function can be used to get the current mutator time
170 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
171 * used in the heap profiler for accurately time stamping the heap
174 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
175 * defined in stat_startGC() - to minimise system calls,
176 * GC_start_time is, however, only defined when really needed (check
177 * stat_startGC() for details)
180 mut_user_time_during_GC(void)
182 return TICK_TO_DBL(GC_start_time - GC_tot_time);
189 return TICK_TO_DBL(CurrentUserTime - GC_tot_time);
195 /* ToDo (on NT): better, get this via the performance data
196 that's stored in the registry. */
197 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(_WIN32)
202 getrusage(RUSAGE_SELF, &t);
211 FILE *sf = RtsFlags.GcFlags.statsFile;
213 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
214 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
215 fprintf(sf, " bytes bytes bytes user elap user elap\n");
218 (TICK_TYPE *)stgMallocBytes(
219 sizeof(TICK_TYPE)*RtsFlags.GcFlags.generations,
221 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
222 GC_coll_times[i] = 0;
226 /* -----------------------------------------------------------------------------
227 Initialisation time...
228 -------------------------------------------------------------------------- */
233 /* Determine TicksPerSecond ... */
234 #if defined(CLK_TCK) /* defined by POSIX */
235 TicksPerSecond = CLK_TCK;
237 #elif defined(HAVE_SYSCONF)
240 ticks = sysconf(_SC_CLK_TCK);
242 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
243 stg_exit(EXIT_FAILURE);
245 TicksPerSecond = (double) ticks;
247 /* no "sysconf" or CLK_TCK; had better guess */
249 TicksPerSecond = (StgDouble) (HZ);
251 #elif defined(CLOCKS_PER_SEC)
252 TicksPerSecond = (StgDouble) (CLOCKS_PER_SEC);
253 #else /* had better guess wildly */
254 /* We will #ifdef around the fprintf for machines
255 we *know* are unsupported. (WDP 94/05)
257 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
258 TicksPerSecond = 60.0;
262 ElapsedTimeStart = CurrentElapsedTime;
269 InitUserTime = CurrentUserTime;
270 InitElapsedStamp = CurrentElapsedTime;
271 if (ElapsedTimeStart > CurrentElapsedTime) {
274 InitElapsedTime = CurrentElapsedTime - ElapsedTimeStart;
278 /* -----------------------------------------------------------------------------
279 stat_startExit and stat_endExit
281 These two measure the time taken in shutdownHaskell().
282 -------------------------------------------------------------------------- */
288 MutElapsedStamp = CurrentElapsedTime;
289 MutElapsedTime = CurrentElapsedTime - GCe_tot_time - InitElapsedStamp;
290 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
292 /* for SMP, we don't know the mutator time yet, we have to inspect
293 * all the running threads to find out, and they haven't stopped
294 * yet. So we just timestamp MutUserTime at this point so we can
295 * calculate the EXIT time. The real MutUserTime is calculated
296 * in stat_exit below.
299 MutUserTime = CurrentUserTime;
301 MutUserTime = CurrentUserTime - GC_tot_time - InitUserTime;
302 if (MutUserTime < 0) { MutUserTime = 0; }
311 ExitUserTime = CurrentUserTime - MutUserTime;
313 ExitUserTime = CurrentUserTime - MutUserTime - GC_tot_time - InitUserTime;
315 ExitElapsedTime = CurrentElapsedTime - MutElapsedStamp;
316 if (ExitUserTime < 0) {
319 if (ExitElapsedTime < 0) {
324 /* -----------------------------------------------------------------------------
325 Called at the beginning of each GC
326 -------------------------------------------------------------------------- */
328 static nat rub_bell = 0;
330 /* initialise global variables needed during GC
332 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
333 * needed if either PROFILING or DEBUGing is enabled
338 nat bell = RtsFlags.GcFlags.ringBell;
342 fprintf(stderr, " GC ");
345 fprintf(stderr, "\007");
349 #if defined(PROFILING) || defined(DEBUG)
351 GC_start_time = CurrentUserTime; /* needed in mut_user_time_during_GC() */
354 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
355 #if !defined(PROFILING) && !defined(DEBUG)
357 GC_start_time = CurrentUserTime;
359 GCe_start_time = CurrentElapsedTime;
360 if (RtsFlags.GcFlags.giveStats) {
361 GC_start_faults = pageFaults();
366 /* -----------------------------------------------------------------------------
367 Called at the end of each GC
368 -------------------------------------------------------------------------- */
371 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
373 FILE *sf = RtsFlags.GcFlags.statsFile;
375 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
376 TICK_TYPE time, etime, gc_time, gc_etime;
379 time = CurrentUserTime;
380 etime = CurrentElapsedTime;
381 gc_time = time - GC_start_time;
382 gc_etime = etime - GCe_start_time;
384 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS && sf != NULL) {
385 nat faults = pageFaults();
387 fprintf(sf, "%9ld %9ld %9ld",
388 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
389 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
390 TICK_TO_DBL(gc_time),
391 TICK_TO_DBL(gc_etime),
393 TICK_TO_DBL(etime - ElapsedTimeStart),
394 faults - GC_start_faults,
395 GC_start_faults - GC_end_faults,
398 GC_end_faults = faults;
402 GC_coll_times[gen] += gc_time;
404 GC_tot_copied += (ullong) copied;
405 GC_tot_alloc += (ullong) alloc;
406 GC_tot_time += gc_time;
407 GCe_tot_time += gc_etime;
412 pthread_t me = pthread_self();
414 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
415 if (me == task_ids[i].id) {
416 task_ids[i].gc_time += gc_time;
417 task_ids[i].gc_etime += gc_etime;
424 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
425 if (live > MaxResidency) {
429 AvgResidency += live;
434 fprintf(stderr, "\b\b\b \b\b\b");
439 /* -----------------------------------------------------------------------------
442 Called under SMP when a worker thread finishes. We drop the timing
443 stats for this thread into the task_ids struct for that thread.
444 -------------------------------------------------------------------------- */
448 stat_workerStop(void)
451 pthread_t me = pthread_self();
453 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
454 if (task_ids[i].id == me) {
455 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
456 task_ids[i].mut_etime = elapsedtime()
458 - task_ids[i].elapsedtimestart;
459 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
460 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
466 /* -----------------------------------------------------------------------------
467 Called at the end of execution
469 NOTE: number of allocations is not entirely accurate: it doesn't
470 take into account the few bytes at the end of the heap that
471 were left unused when the heap-check failed.
472 -------------------------------------------------------------------------- */
477 FILE *sf = RtsFlags.GcFlags.statsFile;
479 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
481 char temp[BIG_STRING_LEN];
484 nat g, total_collections = 0;
487 time = CurrentUserTime;
488 etime = CurrentElapsedTime - ElapsedTimeStart;
490 GC_tot_alloc += alloc;
492 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
493 if (time == 0.0) time = 1;
494 if (etime == 0.0) etime = 1;
496 /* Count total garbage collections */
497 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
498 total_collections += generations[g].collections;
500 /* For SMP, we have to get the user time from each thread
501 * and try to work out the total time.
506 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
507 MutUserTime += task_ids[i].mut_time;
510 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
511 if (MutUserTime < 0) { MutUserTime = 0; }
514 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS && sf != NULL) {
515 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
516 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
519 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS && sf != NULL) {
520 ullong_format_string(GC_tot_alloc*sizeof(W_),
521 temp, rtsTrue/*commas*/);
522 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
524 ullong_format_string(GC_tot_copied*sizeof(W_),
525 temp, rtsTrue/*commas*/);
526 fprintf(sf, "%11s bytes copied during GC\n", temp);
528 if ( ResidencySamples > 0 ) {
529 ullong_format_string(MaxResidency*sizeof(W_),
530 temp, rtsTrue/*commas*/);
531 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
532 temp, ResidencySamples);
536 /* Print garbage collections in each gen */
537 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
538 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
539 generations[g].collections, g,
540 TICK_TO_DBL(GC_coll_times[g]));
543 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
544 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
549 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
550 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
551 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
553 TICK_TO_DBL(task_ids[i].mut_time),
554 TICK_TO_DBL(task_ids[i].mut_etime),
555 TICK_TO_DBL(task_ids[i].gc_time),
556 TICK_TO_DBL(task_ids[i].gc_etime));
561 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
562 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
563 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
564 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
565 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
566 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
567 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
568 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
569 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
570 TICK_TO_DBL(time), TICK_TO_DBL(etime));
571 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
572 TICK_TO_DBL(GC_tot_time)*100/time,
573 TICK_TO_DBL(GCe_tot_time)*100/etime);
575 if (time - GC_tot_time == 0)
576 ullong_format_string(0, temp, rtsTrue/*commas*/);
578 ullong_format_string(
579 (ullong)((GC_tot_alloc*sizeof(W_))/
580 TICK_TO_DBL(time - GC_tot_time)),
581 temp, rtsTrue/*commas*/);
583 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
585 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
586 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
588 TICK_TO_DBL(time - GC_tot_time - InitUserTime) * 100
589 / TICK_TO_DBL(etime));
592 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS && sf != NULL) {
593 fprintf(sf, "<<ghc: %lld bytes, %d GCs, %ld/%ld avg/max bytes residency (%ld samples), %ldM in use, %.2f INIT (%.2f elapsed), %.2f MUT (%.2f elapsed), %.2f GC (%.2f elapsed) :ghc>>\n",
594 GC_tot_alloc*sizeof(W_), total_collections,
595 AvgResidency*sizeof(W_)/ResidencySamples,
596 MaxResidency*sizeof(W_),
598 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024),
599 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
600 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
601 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
609 /* -----------------------------------------------------------------------------
612 Produce some detailed info on the state of the generational GC.
613 -------------------------------------------------------------------------- */
615 stat_describe_gens(void)
617 nat g, s, mut, mut_once, lge, live;
622 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
624 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
625 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
628 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
631 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
632 generations[g].max_blocks, mut, mut_once);
634 for (s = 0; s < generations[g].n_steps; s++) {
635 step = &generations[g].steps[s];
636 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
639 if (RtsFlags.GcFlags.generations == 1) {
644 for (; bd; bd = bd->link) {
645 live += (bd->free - bd->start) * sizeof(W_);
648 fprintf(stderr,"%46s","");
650 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
654 fprintf(stderr,"\n");
657 /* -----------------------------------------------------------------------------
658 Stats available via a programmatic interface, so eg. GHCi can time
659 each compilation and expression evaluation.
660 -------------------------------------------------------------------------- */
662 extern HsInt getAllocations( void )
663 { return (HsInt)(total_allocated * sizeof(W_)); }