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)
559 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
561 char temp[BIG_STRING_LEN];
564 nat g, total_collections = 0;
566 getProcessTimes( &time, &etime );
567 etime -= ElapsedTimeStart;
569 GC_tot_alloc += alloc;
571 /* Count total garbage collections */
572 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
573 total_collections += generations[g].collections;
575 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
576 if (time == 0.0) time = 1;
577 if (etime == 0.0) etime = 1;
579 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
580 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
581 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
584 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
585 ullong_format_string(GC_tot_alloc*sizeof(W_),
586 temp, rtsTrue/*commas*/);
587 statsPrintf("%16s bytes allocated in the heap\n", temp);
589 ullong_format_string(GC_tot_copied*sizeof(W_),
590 temp, rtsTrue/*commas*/);
591 statsPrintf("%16s bytes copied during GC\n", temp);
593 if ( ResidencySamples > 0 ) {
594 ullong_format_string(MaxResidency*sizeof(W_),
595 temp, rtsTrue/*commas*/);
596 statsPrintf("%16s bytes maximum residency (%ld sample(s))\n",
597 temp, ResidencySamples);
600 ullong_format_string(MaxSlop*sizeof(W_), temp, rtsTrue/*commas*/);
601 statsPrintf("%16s bytes maximum slop\n", temp);
603 statsPrintf("%16ld MB total memory in use\n\n",
604 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
606 /* Print garbage collections in each gen */
607 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
608 statsPrintf(" Generation %d: %5d collections, %5d parallel, %5.2fs, %5.2fs elapsed\n",
609 g, generations[g].collections,
610 generations[g].par_collections,
611 TICK_TO_DBL(GC_coll_times[g]),
612 TICK_TO_DBL(GC_coll_etimes[g]));
615 #if defined(THREADED_RTS)
616 if (RtsFlags.ParFlags.gcThreads > 1) {
617 statsPrintf("\n Parallel GC work balance: %.2f (%ld / %ld, ideal %d)\n",
618 (double)GC_par_avg_copied / (double)GC_par_max_copied,
619 (lnat)GC_par_avg_copied, (lnat)GC_par_max_copied,
620 RtsFlags.ParFlags.gcThreads
627 #if defined(THREADED_RTS)
631 for (i = 0, task = all_tasks;
633 i++, task = task->all_link) {
634 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
635 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
637 (task->tso == NULL) ? "(worker)" : "(bound)",
638 TICK_TO_DBL(task->mut_time),
639 TICK_TO_DBL(task->mut_etime),
640 TICK_TO_DBL(task->gc_time),
641 TICK_TO_DBL(task->gc_etime));
646 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
647 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
648 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
649 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
650 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
651 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
653 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
654 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
655 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
656 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
658 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
659 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
660 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
661 TICK_TO_DBL(time), TICK_TO_DBL(etime));
662 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
663 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
664 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
666 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
667 ullong_format_string(0, temp, rtsTrue/*commas*/);
669 ullong_format_string(
670 (ullong)((GC_tot_alloc*sizeof(W_))/
671 TICK_TO_DBL(time - GC_tot_time -
672 PROF_VAL(RP_tot_time + HC_tot_time))),
673 temp, rtsTrue/*commas*/);
675 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
677 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
678 TICK_TO_DBL(time - GC_tot_time -
679 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
681 TICK_TO_DBL(time - GC_tot_time -
682 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
683 / TICK_TO_DBL(etime));
698 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
699 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
700 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
701 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",
703 ResidencySamples == 0 ? 0 :
704 AvgResidency*sizeof(W_)/ResidencySamples,
705 MaxResidency*sizeof(W_),
707 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
708 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
709 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
710 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
713 #if defined(THREADED_RTS) && defined(PROF_SPIN)
717 statsPrintf("recordMutableGen_sync: %"FMT_Word64"\n", recordMutableGen_sync.spin);
718 statsPrintf("gc_alloc_block_sync: %"FMT_Word64"\n", gc_alloc_block_sync.spin);
719 statsPrintf("whitehole_spin: %"FMT_Word64"\n", whitehole_spin);
720 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
721 for (s = 0; s < generations[g].n_steps; s++) {
722 statsPrintf("gen[%d].steps[%d].sync_todo: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_todo.spin);
723 statsPrintf("gen[%d].steps[%d].sync_large_objects: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_large_objects.spin);
734 stgFree(GC_coll_times);
735 GC_coll_times = NULL;
737 stgFree(GC_coll_etimes);
738 GC_coll_etimes = NULL;
741 /* -----------------------------------------------------------------------------
744 Produce some detailed info on the state of the generational GC.
745 -------------------------------------------------------------------------- */
747 statDescribeGens(void)
751 lnat tot_live, tot_slop;
756 "-----------------------------------------------------------------\n"
757 " Gen Max Mut-list Step Blocks Large Live Slop\n"
758 " Blocks Bytes Objects \n"
759 "-----------------------------------------------------------------\n");
763 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
765 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
766 mut += (bd->free - bd->start) * sizeof(W_);
769 debugBelch("%5d %7d %9d", g, generations[g].max_blocks, mut);
771 for (s = 0; s < generations[g].n_steps; s++) {
772 step = &generations[g].steps[s];
773 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
776 live = step->n_words + countOccupied(step->large_objects);
778 debugBelch("%23s","");
780 slop = (step->n_blocks + step->n_large_blocks) * BLOCK_SIZE_W - live;
781 debugBelch("%6d %8d %8d %8ld %8ld\n", s, step->n_blocks, lge,
782 live*sizeof(W_), slop*sizeof(W_));
787 debugBelch("-----------------------------------------------------------------\n");
788 debugBelch("%48s%8ld %8ld\n","",tot_live*sizeof(W_),tot_slop*sizeof(W_));
789 debugBelch("-----------------------------------------------------------------\n");
793 /* -----------------------------------------------------------------------------
794 Stats available via a programmatic interface, so eg. GHCi can time
795 each compilation and expression evaluation.
796 -------------------------------------------------------------------------- */
798 extern HsInt64 getAllocations( void )
799 { return (HsInt64)total_allocated * sizeof(W_); }
801 /* -----------------------------------------------------------------------------
802 Dumping stuff in the stats file, or via the debug message interface
803 -------------------------------------------------------------------------- */
806 statsPrintf( char *s, ... )
808 FILE *sf = RtsFlags.GcFlags.statsFile;
823 FILE *sf = RtsFlags.GcFlags.statsFile;
832 FILE *sf = RtsFlags.GcFlags.statsFile;