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
72 static lnat GC_start_faults = 0, GC_end_faults = 0;
74 static Ticks *GC_coll_times = NULL;
75 static Ticks *GC_coll_etimes = NULL;
77 static void statsFlush( void );
78 static void statsClose( void );
80 Ticks stat_getElapsedGCTime(void)
85 Ticks stat_getElapsedTime(void)
87 return getProcessElapsedTime() - ElapsedTimeStart;
90 /* mut_user_time_during_GC() and mut_user_time()
92 * The former function can be used to get the current mutator time
93 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
94 * used in the heap profiler for accurately time stamping the heap
97 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
98 * defined in stat_startGC() - to minimise system calls,
99 * GC_start_time is, however, only defined when really needed (check
100 * stat_startGC() for details)
103 mut_user_time_during_GC( void )
105 return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
109 mut_user_time( void )
112 user = getProcessCPUTime();
113 return TICK_TO_DBL(user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
118 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
119 it returns the MUT time during retainer profiling.
120 The same is for mut_user_time_during_HC();
123 mut_user_time_during_RP( void )
125 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
129 mut_user_time_during_heap_census( void )
131 return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
133 #endif /* PROFILING */
140 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
141 statsPrintf(" Alloc Copied Live GC GC TOT TOT Page Flts\n");
142 statsPrintf(" bytes bytes bytes user elap user elap\n");
145 (Ticks *)stgMallocBytes(
146 sizeof(Ticks)*RtsFlags.GcFlags.generations,
149 (Ticks *)stgMallocBytes(
150 sizeof(Ticks)*RtsFlags.GcFlags.generations,
152 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
153 GC_coll_times[i] = 0;
154 GC_coll_etimes[i] = 0;
158 /* -----------------------------------------------------------------------------
159 Initialisation time...
160 -------------------------------------------------------------------------- */
167 elapsed = getProcessElapsedTime();
168 ElapsedTimeStart = elapsed;
176 getProcessTimes(&user, &elapsed);
179 InitElapsedStamp = elapsed;
180 if (ElapsedTimeStart > elapsed) {
183 InitElapsedTime = elapsed - ElapsedTimeStart;
186 /* We start counting events for the mutator
187 * when garbage collection starts
188 * we switch to the GC event set. */
189 papi_start_mutator_count();
191 /* This flag is needed to avoid counting the last GC */
192 papi_is_reporting = 1;
197 /* -----------------------------------------------------------------------------
198 stat_startExit and stat_endExit
200 These two measure the time taken in shutdownHaskell().
201 -------------------------------------------------------------------------- */
208 getProcessTimes(&user, &elapsed);
210 MutElapsedStamp = elapsed;
211 MutElapsedTime = elapsed - GCe_tot_time -
212 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
213 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
215 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
216 if (MutUserTime < 0) { MutUserTime = 0; }
219 /* We stop counting mutator events
220 * GC events are not being counted at this point */
221 papi_stop_mutator_count();
223 /* This flag is needed, because GC is run once more after this function */
224 papi_is_reporting = 0;
234 getProcessTimes(&user, &elapsed);
236 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
237 ExitElapsedTime = elapsed - MutElapsedStamp;
238 if (ExitUserTime < 0) {
241 if (ExitElapsedTime < 0) {
246 /* -----------------------------------------------------------------------------
247 Called at the beginning of each GC
248 -------------------------------------------------------------------------- */
250 static nat rub_bell = 0;
252 /* initialise global variables needed during GC
254 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
255 * needed if either PROFILING or DEBUGing is enabled
260 nat bell = RtsFlags.GcFlags.ringBell;
271 #if defined(PROFILING) || defined(DEBUG)
272 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
275 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
276 #if !defined(PROFILING) && !defined(DEBUG)
277 GC_start_time = getProcessCPUTime();
279 GCe_start_time = getProcessElapsedTime();
280 if (RtsFlags.GcFlags.giveStats) {
281 GC_start_faults = getPageFaults();
286 if(papi_is_reporting) {
287 /* Switch to counting GC events */
288 papi_stop_mutator_count();
289 papi_start_gc_count();
295 /* -----------------------------------------------------------------------------
296 Called at the end of each GC
297 -------------------------------------------------------------------------- */
300 stat_endGC (lnat alloc, lnat live, lnat copied, lnat gen,
301 lnat max_copied, lnat avg_copied)
303 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
304 Ticks time, etime, gc_time, gc_etime;
306 getProcessTimes(&time, &etime);
307 gc_time = time - GC_start_time;
308 gc_etime = etime - GCe_start_time;
310 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
311 nat faults = getPageFaults();
313 statsPrintf("%9ld %9ld %9ld",
314 alloc*sizeof(W_), copied*sizeof(W_),
316 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
317 TICK_TO_DBL(gc_time),
318 TICK_TO_DBL(gc_etime),
320 TICK_TO_DBL(etime - ElapsedTimeStart),
321 faults - GC_start_faults,
322 GC_start_faults - GC_end_faults,
325 GC_end_faults = faults;
329 GC_coll_times[gen] += gc_time;
330 GC_coll_etimes[gen] += gc_etime;
332 GC_tot_copied += (ullong) copied;
333 GC_tot_alloc += (ullong) alloc;
334 GC_par_max_copied += (ullong) max_copied;
335 GC_par_avg_copied += (ullong) avg_copied;
336 GC_tot_time += gc_time;
337 GCe_tot_time += gc_etime;
339 #if defined(THREADED_RTS)
342 if ((task = myTask()) != NULL) {
343 task->gc_time += gc_time;
344 task->gc_etime += gc_etime;
349 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
350 if (live > MaxResidency) {
354 AvgResidency += live;
359 debugBelch("\b\b\b \b\b\b");
364 if(papi_is_reporting) {
365 /* Switch to counting mutator events */
367 papi_stop_gc0_count();
369 papi_stop_gc1_count();
371 papi_start_mutator_count();
376 /* -----------------------------------------------------------------------------
377 Called at the beginning of each Retainer Profiliing
378 -------------------------------------------------------------------------- */
384 getProcessTimes( &user, &elapsed );
386 RP_start_time = user;
387 RPe_start_time = elapsed;
389 #endif /* PROFILING */
391 /* -----------------------------------------------------------------------------
392 Called at the end of each Retainer Profiliing
393 -------------------------------------------------------------------------- */
398 nat retainerGeneration,
399 #ifdef DEBUG_RETAINER
403 double averageNumVisit)
406 getProcessTimes( &user, &elapsed );
408 RP_tot_time += user - RP_start_time;
409 RPe_tot_time += elapsed - RPe_start_time;
411 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
412 retainerGeneration, mut_user_time_during_RP());
413 #ifdef DEBUG_RETAINER
414 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
415 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
417 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
419 #endif /* PROFILING */
421 /* -----------------------------------------------------------------------------
422 Called at the beginning of each heap census
423 -------------------------------------------------------------------------- */
426 stat_startHeapCensus(void)
429 getProcessTimes( &user, &elapsed );
431 HC_start_time = user;
432 HCe_start_time = elapsed;
434 #endif /* PROFILING */
436 /* -----------------------------------------------------------------------------
437 Called at the end of each heap census
438 -------------------------------------------------------------------------- */
441 stat_endHeapCensus(void)
444 getProcessTimes( &user, &elapsed );
446 HC_tot_time += user - HC_start_time;
447 HCe_tot_time += elapsed - HCe_start_time;
449 #endif /* PROFILING */
451 /* -----------------------------------------------------------------------------
452 Called at the end of execution
454 NOTE: number of allocations is not entirely accurate: it doesn't
455 take into account the few bytes at the end of the heap that
456 were left unused when the heap-check failed.
457 -------------------------------------------------------------------------- */
460 #define TICK_VAR(arity) \
461 extern StgInt SLOW_CALLS_##arity; \
462 extern StgInt RIGHT_ARITY_##arity; \
463 extern StgInt TAGGED_PTR_##arity;
465 #define TICK_VAR_INI(arity) \
466 StgInt SLOW_CALLS_##arity = 1; \
467 StgInt RIGHT_ARITY_##arity = 1; \
468 StgInt TAGGED_PTR_##arity = 0;
470 extern StgInt TOTAL_CALLS;
478 StgInt TOTAL_CALLS=1;
481 /* Report the value of a counter */
482 #define REPORT(counter) \
484 ullong_format_string(counter,temp,rtsTrue/*commas*/); \
485 statsPrintf(" (" #counter ") : %s\n",temp); \
488 /* Report the value of a counter as a percentage of another counter */
489 #define REPORT_PCT(counter,countertot) \
490 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
491 counter*100.0/countertot)
493 #define TICK_PRINT(arity) \
494 REPORT(SLOW_CALLS_##arity); \
495 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
496 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
497 REPORT(RIGHT_ARITY_##arity); \
498 REPORT(TAGGED_PTR_##arity)
500 #define TICK_PRINT_TOT(arity) \
501 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
502 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
508 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
510 char temp[BIG_STRING_LEN];
513 nat g, total_collections = 0;
515 getProcessTimes( &time, &etime );
516 etime -= ElapsedTimeStart;
518 GC_tot_alloc += alloc;
520 /* Count total garbage collections */
521 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
522 total_collections += generations[g].collections;
524 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
525 if (time == 0.0) time = 1;
526 if (etime == 0.0) etime = 1;
528 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
529 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
530 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
533 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
534 ullong_format_string(GC_tot_alloc*sizeof(W_),
535 temp, rtsTrue/*commas*/);
536 statsPrintf("%16s bytes allocated in the heap\n", temp);
538 ullong_format_string(GC_tot_copied*sizeof(W_),
539 temp, rtsTrue/*commas*/);
540 statsPrintf("%16s bytes copied during GC\n", temp);
542 if ( ResidencySamples > 0 ) {
543 ullong_format_string(MaxResidency*sizeof(W_),
544 temp, rtsTrue/*commas*/);
545 statsPrintf("%16s bytes maximum residency (%ld sample(s))\n",
546 temp, ResidencySamples);
548 statsPrintf("%16ld MB total memory in use\n\n",
549 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
551 /* Print garbage collections in each gen */
552 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
553 statsPrintf(" Generation %d: %5d collections, %5d parallel, %5.2fs, %5.2fs elapsed\n",
554 g, generations[g].collections,
555 generations[g].par_collections,
556 TICK_TO_DBL(GC_coll_times[g]),
557 TICK_TO_DBL(GC_coll_etimes[g]));
560 #if defined(THREADED_RTS)
561 if (RtsFlags.ParFlags.gcThreads > 1) {
562 statsPrintf("\n Parallel GC work balance: %.2f (%ld / %ld, ideal %d)\n",
563 (double)GC_par_avg_copied / (double)GC_par_max_copied,
564 (lnat)GC_par_avg_copied, (lnat)GC_par_max_copied,
565 RtsFlags.ParFlags.gcThreads
572 #if defined(THREADED_RTS)
576 for (i = 0, task = all_tasks;
578 i++, task = task->all_link) {
579 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
580 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
582 (task->tso == NULL) ? "(worker)" : "(bound)",
583 TICK_TO_DBL(task->mut_time),
584 TICK_TO_DBL(task->mut_etime),
585 TICK_TO_DBL(task->gc_time),
586 TICK_TO_DBL(task->gc_etime));
591 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
592 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
593 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
594 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
595 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
596 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
598 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
599 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
600 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
601 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
603 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
604 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
605 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
606 TICK_TO_DBL(time), TICK_TO_DBL(etime));
607 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
608 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
609 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
611 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
612 ullong_format_string(0, temp, rtsTrue/*commas*/);
614 ullong_format_string(
615 (ullong)((GC_tot_alloc*sizeof(W_))/
616 TICK_TO_DBL(time - GC_tot_time -
617 PROF_VAL(RP_tot_time + HC_tot_time))),
618 temp, rtsTrue/*commas*/);
620 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
622 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
623 TICK_TO_DBL(time - GC_tot_time -
624 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
626 TICK_TO_DBL(time - GC_tot_time -
627 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
628 / TICK_TO_DBL(etime));
643 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
644 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
645 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
646 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",
648 ResidencySamples == 0 ? 0 :
649 AvgResidency*sizeof(W_)/ResidencySamples,
650 MaxResidency*sizeof(W_),
652 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
653 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
654 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
655 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
658 #if defined(THREADED_RTS) && defined(PROF_SPIN)
662 statsPrintf("recordMutableGen_sync: %"FMT_Word64"\n", recordMutableGen_sync.spin);
663 statsPrintf("gc_alloc_block_sync: %"FMT_Word64"\n", gc_alloc_block_sync.spin);
664 statsPrintf("whitehole_spin: %"FMT_Word64"\n", whitehole_spin);
665 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
666 for (s = 0; s < generations[g].n_steps; s++) {
667 statsPrintf("gen[%d].steps[%d].sync_todo: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_todo.spin);
668 statsPrintf("gen[%d].steps[%d].sync_large_objects: %"FMT_Word64"\n", g, s, generations[g].steps[s].sync_large_objects.spin);
679 stgFree(GC_coll_times);
680 GC_coll_times = NULL;
682 stgFree(GC_coll_etimes);
683 GC_coll_etimes = NULL;
686 /* -----------------------------------------------------------------------------
689 Produce some detailed info on the state of the generational GC.
690 -------------------------------------------------------------------------- */
692 statDescribeGens(void)
696 lnat tot_live, tot_slop;
701 "-----------------------------------------------------------------\n"
702 " Gen Max Mut-list Step Blocks Large Live Slop\n"
703 " Blocks Bytes Objects \n"
704 "-----------------------------------------------------------------\n");
708 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
710 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
711 mut += (bd->free - bd->start) * sizeof(W_);
714 debugBelch("%5d %7d %9d", g, generations[g].max_blocks, mut);
716 for (s = 0; s < generations[g].n_steps; s++) {
717 step = &generations[g].steps[s];
718 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
721 live = step->n_words + countOccupied(step->large_objects);
723 debugBelch("%23s","");
725 slop = (step->n_blocks + step->n_large_blocks) * BLOCK_SIZE_W - live;
726 debugBelch("%6d %8d %8d %8ld %8ld\n", s, step->n_blocks, lge,
727 live*sizeof(W_), slop*sizeof(W_));
732 debugBelch("-----------------------------------------------------------------\n");
733 debugBelch("%48s%8ld %8ld\n","",tot_live*sizeof(W_),tot_slop*sizeof(W_));
734 debugBelch("-----------------------------------------------------------------\n");
738 /* -----------------------------------------------------------------------------
739 Stats available via a programmatic interface, so eg. GHCi can time
740 each compilation and expression evaluation.
741 -------------------------------------------------------------------------- */
743 extern HsInt64 getAllocations( void )
744 { return (HsInt64)total_allocated * sizeof(W_); }
746 /* -----------------------------------------------------------------------------
747 Dumping stuff in the stats file, or via the debug message interface
748 -------------------------------------------------------------------------- */
751 statsPrintf( char *s, ... )
753 FILE *sf = RtsFlags.GcFlags.statsFile;
768 FILE *sf = RtsFlags.GcFlags.statsFile;
777 FILE *sf = RtsFlags.GcFlags.statsFile;