1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2005
5 * Statistics and timing-related functions.
7 * ---------------------------------------------------------------------------*/
16 #include "ParTicky.h" /* ToDo: move into Rts.h */
17 #include "Profiling.h"
28 #define BIG_STRING_LEN 512
30 #define TICK_TO_DBL(t) ((double)(t) / TICKS_PER_SECOND)
32 static Ticks ElapsedTimeStart = 0;
34 static Ticks InitUserTime = 0;
35 static Ticks InitElapsedTime = 0;
36 static Ticks InitElapsedStamp = 0;
38 static Ticks MutUserTime = 0;
39 static Ticks MutElapsedTime = 0;
40 static Ticks MutElapsedStamp = 0;
42 static Ticks ExitUserTime = 0;
43 static Ticks ExitElapsedTime = 0;
45 static ullong GC_tot_alloc = 0;
46 static ullong GC_tot_copied = 0;
48 static ullong GC_par_max_copied = 0;
49 static ullong GC_par_avg_copied = 0;
51 static Ticks GC_start_time = 0, GC_tot_time = 0; /* User GC Time */
52 static Ticks GCe_start_time = 0, GCe_tot_time = 0; /* Elapsed GC time */
55 static Ticks RP_start_time = 0, RP_tot_time = 0; /* retainer prof user time */
56 static Ticks RPe_start_time = 0, RPe_tot_time = 0; /* retainer prof elap time */
58 static Ticks HC_start_time, HC_tot_time = 0; // heap census prof user time
59 static Ticks HCe_start_time, HCe_tot_time = 0; // heap census prof elap time
63 #define PROF_VAL(x) (x)
68 static lnat MaxResidency = 0; // in words; for stats only
69 static lnat AvgResidency = 0;
70 static lnat ResidencySamples = 0; // for stats only
71 static lnat MaxSlop = 0;
73 static lnat GC_start_faults = 0, GC_end_faults = 0;
75 static Ticks *GC_coll_times = NULL;
76 static Ticks *GC_coll_etimes = NULL;
78 static void statsFlush( void );
79 static void statsClose( void );
81 Ticks stat_getElapsedGCTime(void)
86 Ticks stat_getElapsedTime(void)
88 return getProcessElapsedTime() - ElapsedTimeStart;
91 /* mut_user_time_during_GC() and mut_user_time()
93 * The former function can be used to get the current mutator time
94 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
95 * used in the heap profiler for accurately time stamping the heap
98 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
99 * defined in stat_startGC() - to minimise system calls,
100 * GC_start_time is, however, only defined when really needed (check
101 * stat_startGC() for details)
104 mut_user_time_during_GC( void )
106 return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
110 mut_user_time( void )
113 user = getProcessCPUTime();
114 return TICK_TO_DBL(user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
119 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
120 it returns the MUT time during retainer profiling.
121 The same is for mut_user_time_during_HC();
124 mut_user_time_during_RP( void )
126 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
130 mut_user_time_during_heap_census( void )
132 return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
134 #endif /* PROFILING */
136 // initStats0() has no dependencies, it can be called right at the beginning
140 ElapsedTimeStart = 0;
144 InitElapsedStamp = 0;
155 GC_par_max_copied = 0;
156 GC_par_avg_copied = 0;
176 ResidencySamples = 0;
183 // initStats1() can be called after setupRtsFlags()
189 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
190 statsPrintf(" Alloc Copied Live GC GC TOT TOT Page Flts\n");
191 statsPrintf(" bytes bytes bytes user elap user elap\n");
194 (Ticks *)stgMallocBytes(
195 sizeof(Ticks)*RtsFlags.GcFlags.generations,
198 (Ticks *)stgMallocBytes(
199 sizeof(Ticks)*RtsFlags.GcFlags.generations,
201 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
202 GC_coll_times[i] = 0;
203 GC_coll_etimes[i] = 0;
207 /* -----------------------------------------------------------------------------
208 Initialisation time...
209 -------------------------------------------------------------------------- */
216 elapsed = getProcessElapsedTime();
217 ElapsedTimeStart = elapsed;
225 getProcessTimes(&user, &elapsed);
228 InitElapsedStamp = elapsed;
229 if (ElapsedTimeStart > elapsed) {
232 InitElapsedTime = elapsed - ElapsedTimeStart;
235 /* We start counting events for the mutator
236 * when garbage collection starts
237 * we switch to the GC event set. */
238 papi_start_mutator_count();
240 /* This flag is needed to avoid counting the last GC */
241 papi_is_reporting = 1;
246 /* -----------------------------------------------------------------------------
247 stat_startExit and stat_endExit
249 These two measure the time taken in shutdownHaskell().
250 -------------------------------------------------------------------------- */
257 getProcessTimes(&user, &elapsed);
259 MutElapsedStamp = elapsed;
260 MutElapsedTime = elapsed - GCe_tot_time -
261 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
262 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
264 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
265 if (MutUserTime < 0) { MutUserTime = 0; }
268 /* We stop counting mutator events
269 * GC events are not being counted at this point */
270 papi_stop_mutator_count();
272 /* This flag is needed, because GC is run once more after this function */
273 papi_is_reporting = 0;
283 getProcessTimes(&user, &elapsed);
285 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
286 ExitElapsedTime = elapsed - MutElapsedStamp;
287 if (ExitUserTime < 0) {
290 if (ExitElapsedTime < 0) {
295 /* -----------------------------------------------------------------------------
296 Called at the beginning of each GC
297 -------------------------------------------------------------------------- */
299 static nat rub_bell = 0;
301 /* initialise global variables needed during GC
303 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
304 * needed if either PROFILING or DEBUGing is enabled
309 nat bell = RtsFlags.GcFlags.ringBell;
320 #if defined(PROFILING) || defined(DEBUG)
321 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
324 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
325 #if !defined(PROFILING) && !defined(DEBUG)
326 GC_start_time = getProcessCPUTime();
328 GCe_start_time = getProcessElapsedTime();
329 if (RtsFlags.GcFlags.giveStats) {
330 GC_start_faults = getPageFaults();
335 if(papi_is_reporting) {
336 /* Switch to counting GC events */
337 papi_stop_mutator_count();
338 papi_start_gc_count();
344 /* -----------------------------------------------------------------------------
345 Called at the end of each GC
346 -------------------------------------------------------------------------- */
349 stat_endGC (lnat alloc, lnat live, lnat copied, lnat gen,
350 lnat max_copied, lnat avg_copied, lnat slop)
352 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
353 Ticks time, etime, gc_time, gc_etime;
355 getProcessTimes(&time, &etime);
356 gc_time = time - GC_start_time;
357 gc_etime = etime - GCe_start_time;
359 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
360 nat faults = getPageFaults();
362 statsPrintf("%9ld %9ld %9ld",
363 alloc*sizeof(W_), copied*sizeof(W_),
365 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
366 TICK_TO_DBL(gc_time),
367 TICK_TO_DBL(gc_etime),
369 TICK_TO_DBL(etime - ElapsedTimeStart),
370 faults - GC_start_faults,
371 GC_start_faults - GC_end_faults,
374 GC_end_faults = faults;
378 GC_coll_times[gen] += gc_time;
379 GC_coll_etimes[gen] += gc_etime;
381 GC_tot_copied += (ullong) copied;
382 GC_tot_alloc += (ullong) alloc;
383 GC_par_max_copied += (ullong) max_copied;
384 GC_par_avg_copied += (ullong) avg_copied;
385 GC_tot_time += gc_time;
386 GCe_tot_time += gc_etime;
388 #if defined(THREADED_RTS)
391 if ((task = myTask()) != NULL) {
392 task->gc_time += gc_time;
393 task->gc_etime += gc_etime;
398 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
399 if (live > MaxResidency) {
403 AvgResidency += live;
406 if (slop > MaxSlop) MaxSlop = slop;
410 debugBelch("\b\b\b \b\b\b");
415 if(papi_is_reporting) {
416 /* Switch to counting mutator events */
418 papi_stop_gc0_count();
420 papi_stop_gc1_count();
422 papi_start_mutator_count();
427 /* -----------------------------------------------------------------------------
428 Called at the beginning of each Retainer Profiliing
429 -------------------------------------------------------------------------- */
435 getProcessTimes( &user, &elapsed );
437 RP_start_time = user;
438 RPe_start_time = elapsed;
440 #endif /* PROFILING */
442 /* -----------------------------------------------------------------------------
443 Called at the end of each Retainer Profiliing
444 -------------------------------------------------------------------------- */
449 nat retainerGeneration,
450 #ifdef DEBUG_RETAINER
454 double averageNumVisit)
457 getProcessTimes( &user, &elapsed );
459 RP_tot_time += user - RP_start_time;
460 RPe_tot_time += elapsed - RPe_start_time;
462 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
463 retainerGeneration, mut_user_time_during_RP());
464 #ifdef DEBUG_RETAINER
465 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
466 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
468 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
470 #endif /* PROFILING */
472 /* -----------------------------------------------------------------------------
473 Called at the beginning of each heap census
474 -------------------------------------------------------------------------- */
477 stat_startHeapCensus(void)
480 getProcessTimes( &user, &elapsed );
482 HC_start_time = user;
483 HCe_start_time = elapsed;
485 #endif /* PROFILING */
487 /* -----------------------------------------------------------------------------
488 Called at the end of each heap census
489 -------------------------------------------------------------------------- */
492 stat_endHeapCensus(void)
495 getProcessTimes( &user, &elapsed );
497 HC_tot_time += user - HC_start_time;
498 HCe_tot_time += elapsed - HCe_start_time;
500 #endif /* PROFILING */
502 /* -----------------------------------------------------------------------------
503 Called at the end of execution
505 NOTE: number of allocations is not entirely accurate: it doesn't
506 take into account the few bytes at the end of the heap that
507 were left unused when the heap-check failed.
508 -------------------------------------------------------------------------- */
511 #define TICK_VAR(arity) \
512 extern StgInt SLOW_CALLS_##arity; \
513 extern StgInt RIGHT_ARITY_##arity; \
514 extern StgInt TAGGED_PTR_##arity;
516 #define TICK_VAR_INI(arity) \
517 StgInt SLOW_CALLS_##arity = 1; \
518 StgInt RIGHT_ARITY_##arity = 1; \
519 StgInt TAGGED_PTR_##arity = 0;
521 extern StgInt TOTAL_CALLS;
529 StgInt TOTAL_CALLS=1;
532 /* Report the value of a counter */
533 #define REPORT(counter) \
535 ullong_format_string(counter,temp,rtsTrue/*commas*/); \
536 statsPrintf(" (" #counter ") : %s\n",temp); \
539 /* Report the value of a counter as a percentage of another counter */
540 #define REPORT_PCT(counter,countertot) \
541 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
542 counter*100.0/countertot)
544 #define TICK_PRINT(arity) \
545 REPORT(SLOW_CALLS_##arity); \
546 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
547 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
548 REPORT(RIGHT_ARITY_##arity); \
549 REPORT(TAGGED_PTR_##arity)
551 #define TICK_PRINT_TOT(arity) \
552 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
553 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
555 extern lnat hw_alloc_blocks;
560 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
562 char temp[BIG_STRING_LEN];
565 nat g, total_collections = 0;
567 getProcessTimes( &time, &etime );
568 etime -= ElapsedTimeStart;
570 GC_tot_alloc += alloc;
572 /* Count total garbage collections */
573 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
574 total_collections += generations[g].collections;
576 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
577 if (time == 0.0) time = 1;
578 if (etime == 0.0) etime = 1;
580 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
581 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
582 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
585 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
586 ullong_format_string(GC_tot_alloc*sizeof(W_),
587 temp, rtsTrue/*commas*/);
588 statsPrintf("%16s bytes allocated in the heap\n", temp);
590 ullong_format_string(GC_tot_copied*sizeof(W_),
591 temp, rtsTrue/*commas*/);
592 statsPrintf("%16s bytes copied during GC\n", temp);
594 if ( ResidencySamples > 0 ) {
595 ullong_format_string(MaxResidency*sizeof(W_),
596 temp, rtsTrue/*commas*/);
597 statsPrintf("%16s bytes maximum residency (%ld sample(s))\n",
598 temp, ResidencySamples);
601 ullong_format_string(MaxSlop*sizeof(W_), temp, rtsTrue/*commas*/);
602 statsPrintf("%16s bytes maximum slop\n", temp);
604 statsPrintf("%16ld MB total memory in use (%ld MB lost due to fragmentation)\n\n",
605 mblocks_allocated * MBLOCK_SIZE_W / (1024 * 1024 / sizeof(W_)),
606 (mblocks_allocated * MBLOCK_SIZE_W - hw_alloc_blocks * BLOCK_SIZE_W) / (1024 * 1024 / sizeof(W_)));
608 /* Print garbage collections in each gen */
609 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
610 statsPrintf(" Generation %d: %5d collections, %5d parallel, %5.2fs, %5.2fs elapsed\n",
611 g, generations[g].collections,
612 generations[g].par_collections,
613 TICK_TO_DBL(GC_coll_times[g]),
614 TICK_TO_DBL(GC_coll_etimes[g]));
617 #if defined(THREADED_RTS)
618 if (RtsFlags.ParFlags.gcThreads > 1) {
619 statsPrintf("\n Parallel GC work balance: %.2f (%ld / %ld, ideal %d)\n",
620 (double)GC_par_avg_copied / (double)GC_par_max_copied,
621 (lnat)GC_par_avg_copied, (lnat)GC_par_max_copied,
622 RtsFlags.ParFlags.gcThreads
629 #if defined(THREADED_RTS)
633 for (i = 0, task = all_tasks;
635 i++, task = task->all_link) {
636 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
637 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
639 (task->tso == NULL) ? "(worker)" : "(bound)",
640 TICK_TO_DBL(task->mut_time),
641 TICK_TO_DBL(task->mut_etime),
642 TICK_TO_DBL(task->gc_time),
643 TICK_TO_DBL(task->gc_etime));
648 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
649 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
650 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
651 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
652 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
653 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
655 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
656 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
657 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
658 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
660 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
661 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
662 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
663 TICK_TO_DBL(time), TICK_TO_DBL(etime));
664 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
665 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
666 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
668 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
669 ullong_format_string(0, temp, rtsTrue/*commas*/);
671 ullong_format_string(
672 (ullong)((GC_tot_alloc*sizeof(W_))/
673 TICK_TO_DBL(time - GC_tot_time -
674 PROF_VAL(RP_tot_time + HC_tot_time))),
675 temp, rtsTrue/*commas*/);
677 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
679 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
680 TICK_TO_DBL(time - GC_tot_time -
681 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
683 TICK_TO_DBL(time - GC_tot_time -
684 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
685 / TICK_TO_DBL(etime));
700 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
701 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
702 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
703 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",
705 ResidencySamples == 0 ? 0 :
706 AvgResidency*sizeof(W_)/ResidencySamples,
707 MaxResidency*sizeof(W_),
709 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
710 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
711 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
712 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
715 #if defined(THREADED_RTS) && defined(PROF_SPIN)
719 statsPrintf("recordMutableGen_sync: %"FMT_Word64"\n", recordMutableGen_sync.spin);
720 statsPrintf("gc_alloc_block_sync: %"FMT_Word64"\n", gc_alloc_block_sync.spin);
721 statsPrintf("whitehole_spin: %"FMT_Word64"\n", whitehole_spin);
722 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
723 for (s = 0; s < generations[g].n_steps; s++) {
724 statsPrintf("gen[%d].steps[%d].sync_todo: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_todo.spin);
725 statsPrintf("gen[%d].steps[%d].sync_large_objects: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_large_objects.spin);
736 stgFree(GC_coll_times);
737 GC_coll_times = NULL;
739 stgFree(GC_coll_etimes);
740 GC_coll_etimes = NULL;
743 /* -----------------------------------------------------------------------------
746 Produce some detailed info on the state of the generational GC.
747 -------------------------------------------------------------------------- */
749 statDescribeGens(void)
753 lnat tot_live, tot_slop;
758 "-----------------------------------------------------------------\n"
759 " Gen Max Mut-list Step Blocks Large Live Slop\n"
760 " Blocks Bytes Objects \n"
761 "-----------------------------------------------------------------\n");
765 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
767 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
768 mut += (bd->free - bd->start) * sizeof(W_);
771 debugBelch("%5d %7d %9d", g, generations[g].max_blocks, mut);
773 for (s = 0; s < generations[g].n_steps; s++) {
774 step = &generations[g].steps[s];
775 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
778 live = step->n_words + countOccupied(step->large_objects);
780 debugBelch("%23s","");
782 slop = (step->n_blocks + step->n_large_blocks) * BLOCK_SIZE_W - live;
783 debugBelch("%6d %8d %8d %8ld %8ld\n", s, step->n_blocks, lge,
784 live*sizeof(W_), slop*sizeof(W_));
789 debugBelch("-----------------------------------------------------------------\n");
790 debugBelch("%48s%8ld %8ld\n","",tot_live*sizeof(W_),tot_slop*sizeof(W_));
791 debugBelch("-----------------------------------------------------------------\n");
795 /* -----------------------------------------------------------------------------
796 Stats available via a programmatic interface, so eg. GHCi can time
797 each compilation and expression evaluation.
798 -------------------------------------------------------------------------- */
800 extern HsInt64 getAllocations( void )
801 { return (HsInt64)total_allocated * sizeof(W_); }
803 /* -----------------------------------------------------------------------------
804 Dumping stuff in the stats file, or via the debug message interface
805 -------------------------------------------------------------------------- */
808 statsPrintf( char *s, ... )
810 FILE *sf = RtsFlags.GcFlags.statsFile;
825 FILE *sf = RtsFlags.GcFlags.statsFile;
834 FILE *sf = RtsFlags.GcFlags.statsFile;