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"
25 #define BIG_STRING_LEN 512
27 #define TICK_TO_DBL(t) ((double)(t) / TICKS_PER_SECOND)
29 static Ticks ElapsedTimeStart = 0;
31 static Ticks InitUserTime = 0;
32 static Ticks InitElapsedTime = 0;
33 static Ticks InitElapsedStamp = 0;
35 static Ticks MutUserTime = 0;
36 static Ticks MutElapsedTime = 0;
37 static Ticks MutElapsedStamp = 0;
39 static Ticks ExitUserTime = 0;
40 static Ticks ExitElapsedTime = 0;
42 static ullong GC_tot_alloc = 0;
43 static ullong GC_tot_copied = 0;
44 static ullong GC_tot_scavd_copied = 0;
46 static Ticks GC_start_time = 0, GC_tot_time = 0; /* User GC Time */
47 static Ticks GCe_start_time = 0, GCe_tot_time = 0; /* Elapsed GC time */
50 static Ticks RP_start_time = 0, RP_tot_time = 0; /* retainer prof user time */
51 static Ticks RPe_start_time = 0, RPe_tot_time = 0; /* retainer prof elap time */
53 static Ticks HC_start_time, HC_tot_time = 0; // heap census prof user time
54 static Ticks HCe_start_time, HCe_tot_time = 0; // heap census prof elap time
58 #define PROF_VAL(x) (x)
63 static lnat MaxResidency = 0; // in words; for stats only
64 static lnat AvgResidency = 0;
65 static lnat ResidencySamples = 0; // for stats only
67 static lnat GC_start_faults = 0, GC_end_faults = 0;
69 static Ticks *GC_coll_times;
71 static void statsFlush( void );
72 static void statsClose( void );
74 Ticks stat_getElapsedGCTime(void)
79 Ticks stat_getElapsedTime(void)
81 return getProcessElapsedTime() - ElapsedTimeStart;
84 /* mut_user_time_during_GC() and mut_user_time()
86 * The former function can be used to get the current mutator time
87 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
88 * used in the heap profiler for accurately time stamping the heap
91 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
92 * defined in stat_startGC() - to minimise system calls,
93 * GC_start_time is, however, only defined when really needed (check
94 * stat_startGC() for details)
97 mut_user_time_during_GC( void )
99 return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
103 mut_user_time( void )
106 user = getProcessCPUTime();
107 return TICK_TO_DBL(user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
112 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
113 it returns the MUT time during retainer profiling.
114 The same is for mut_user_time_during_HC();
117 mut_user_time_during_RP( void )
119 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
123 mut_user_time_during_heap_census( void )
125 return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
127 #endif /* PROFILING */
134 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
135 statsPrintf(" Alloc Copied Live GC GC TOT TOT Page Flts\n");
136 statsPrintf(" bytes bytes bytes user elap user elap\n");
139 (Ticks *)stgMallocBytes(
140 sizeof(Ticks)*RtsFlags.GcFlags.generations,
142 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
143 GC_coll_times[i] = 0;
147 /* -----------------------------------------------------------------------------
148 Initialisation time...
149 -------------------------------------------------------------------------- */
156 elapsed = getProcessElapsedTime();
157 ElapsedTimeStart = elapsed;
165 getProcessTimes(&user, &elapsed);
168 InitElapsedStamp = elapsed;
169 if (ElapsedTimeStart > elapsed) {
172 InitElapsedTime = elapsed - ElapsedTimeStart;
175 papi_init_eventsets();
177 /* We start counting events for the mutator
178 * when garbage collection starts
179 * we switch to the GC event set. */
180 papi_start_mutator_count();
182 /* This flag is needed to avoid counting the last GC */
183 papi_is_reporting = 1;
188 /* -----------------------------------------------------------------------------
189 stat_startExit and stat_endExit
191 These two measure the time taken in shutdownHaskell().
192 -------------------------------------------------------------------------- */
199 getProcessTimes(&user, &elapsed);
201 MutElapsedStamp = elapsed;
202 MutElapsedTime = elapsed - GCe_tot_time -
203 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
204 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
206 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
207 if (MutUserTime < 0) { MutUserTime = 0; }
210 /* We stop counting mutator events
211 * GC events are not being counted at this point */
212 papi_stop_mutator_count();
214 /* This flag is needed, because GC is run once more after this function */
215 papi_is_reporting = 0;
225 getProcessTimes(&user, &elapsed);
227 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
228 ExitElapsedTime = elapsed - MutElapsedStamp;
229 if (ExitUserTime < 0) {
232 if (ExitElapsedTime < 0) {
237 /* -----------------------------------------------------------------------------
238 Called at the beginning of each GC
239 -------------------------------------------------------------------------- */
241 static nat rub_bell = 0;
243 /* initialise global variables needed during GC
245 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
246 * needed if either PROFILING or DEBUGing is enabled
251 nat bell = RtsFlags.GcFlags.ringBell;
262 #if defined(PROFILING) || defined(DEBUG)
263 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
266 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
267 #if !defined(PROFILING) && !defined(DEBUG)
268 GC_start_time = getProcessCPUTime();
270 GCe_start_time = getProcessElapsedTime();
271 if (RtsFlags.GcFlags.giveStats) {
272 GC_start_faults = getPageFaults();
277 if(papi_is_reporting) {
278 /* Switch to counting GC events */
279 papi_stop_mutator_count();
280 papi_start_gc_count();
286 /* -----------------------------------------------------------------------------
287 Called at the end of each GC
288 -------------------------------------------------------------------------- */
291 stat_endGC (lnat alloc, lnat live, lnat copied,
292 lnat scavd_copied, lnat gen)
294 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
295 Ticks time, etime, gc_time, gc_etime;
297 getProcessTimes(&time, &etime);
298 gc_time = time - GC_start_time;
299 gc_etime = etime - GCe_start_time;
301 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
302 nat faults = getPageFaults();
304 statsPrintf("%9ld %9ld %9ld",
305 alloc*sizeof(W_), (copied+scavd_copied)*sizeof(W_),
307 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
308 TICK_TO_DBL(gc_time),
309 TICK_TO_DBL(gc_etime),
311 TICK_TO_DBL(etime - ElapsedTimeStart),
312 faults - GC_start_faults,
313 GC_start_faults - GC_end_faults,
316 GC_end_faults = faults;
320 GC_coll_times[gen] += gc_time;
322 GC_tot_copied += (ullong) copied;
323 GC_tot_scavd_copied += (ullong) scavd_copied;
324 GC_tot_alloc += (ullong) alloc;
325 GC_tot_time += gc_time;
326 GCe_tot_time += gc_etime;
328 #if defined(THREADED_RTS)
331 if ((task = myTask()) != NULL) {
332 task->gc_time += gc_time;
333 task->gc_etime += gc_etime;
338 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
339 if (live > MaxResidency) {
343 AvgResidency += live;
348 debugBelch("\b\b\b \b\b\b");
353 if(papi_is_reporting) {
354 /* Switch to counting mutator events */
355 papi_stop_gc_count();
356 papi_start_mutator_count();
361 /* -----------------------------------------------------------------------------
362 Called at the beginning of each Retainer Profiliing
363 -------------------------------------------------------------------------- */
369 getProcessTimes( &user, &elapsed );
371 RP_start_time = user;
372 RPe_start_time = elapsed;
374 #endif /* PROFILING */
376 /* -----------------------------------------------------------------------------
377 Called at the end of each Retainer Profiliing
378 -------------------------------------------------------------------------- */
383 nat retainerGeneration,
384 #ifdef DEBUG_RETAINER
388 double averageNumVisit)
391 getProcessTimes( &user, &elapsed );
393 RP_tot_time += user - RP_start_time;
394 RPe_tot_time += elapsed - RPe_start_time;
396 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
397 retainerGeneration, mut_user_time_during_RP());
398 #ifdef DEBUG_RETAINER
399 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
400 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
402 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
404 #endif /* PROFILING */
406 /* -----------------------------------------------------------------------------
407 Called at the beginning of each heap census
408 -------------------------------------------------------------------------- */
411 stat_startHeapCensus(void)
414 getProcessTimes( &user, &elapsed );
416 HC_start_time = user;
417 HCe_start_time = elapsed;
419 #endif /* PROFILING */
421 /* -----------------------------------------------------------------------------
422 Called at the end of each heap census
423 -------------------------------------------------------------------------- */
426 stat_endHeapCensus(void)
429 getProcessTimes( &user, &elapsed );
431 HC_tot_time += user - HC_start_time;
432 HCe_tot_time += elapsed - HCe_start_time;
434 #endif /* PROFILING */
436 /* -----------------------------------------------------------------------------
437 Called at the end of execution
439 NOTE: number of allocations is not entirely accurate: it doesn't
440 take into account the few bytes at the end of the heap that
441 were left unused when the heap-check failed.
442 -------------------------------------------------------------------------- */
445 #define TICK_VAR(arity) \
446 extern StgInt SLOW_CALLS_##arity; \
447 extern StgInt RIGHT_ARITY_##arity; \
448 extern StgInt TAGGED_PTR_##arity;
450 #define TICK_VAR_INI(arity) \
451 StgInt SLOW_CALLS_##arity = 1; \
452 StgInt RIGHT_ARITY_##arity = 1; \
453 StgInt TAGGED_PTR_##arity = 0;
455 extern StgInt TOTAL_CALLS;
463 StgInt TOTAL_CALLS=1;
466 /* Report the value of a counter */
467 #define REPORT(counter) \
469 ullong_format_string(counter,temp,rtsTrue/*commas*/); \
470 statsPrintf(" (" #counter ") : %s\n",temp); \
473 /* Report the value of a counter as a percentage of another counter */
474 #define REPORT_PCT(counter,countertot) \
475 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
476 counter*100.0/countertot)
478 #define TICK_PRINT(arity) \
479 REPORT(SLOW_CALLS_##arity); \
480 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
481 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
482 REPORT(RIGHT_ARITY_##arity); \
483 REPORT(TAGGED_PTR_##arity)
485 #define TICK_PRINT_TOT(arity) \
486 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
487 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
493 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
495 char temp[BIG_STRING_LEN];
498 nat g, total_collections = 0;
500 getProcessTimes( &time, &etime );
501 etime -= ElapsedTimeStart;
503 GC_tot_alloc += alloc;
505 /* Count total garbage collections */
506 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
507 total_collections += generations[g].collections;
509 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
510 if (time == 0.0) time = 1;
511 if (etime == 0.0) etime = 1;
513 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
514 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
515 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
518 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
519 ullong_format_string(GC_tot_alloc*sizeof(W_),
520 temp, rtsTrue/*commas*/);
521 statsPrintf("%11s bytes allocated in the heap\n", temp);
523 ullong_format_string(GC_tot_copied*sizeof(W_),
524 temp, rtsTrue/*commas*/);
525 statsPrintf("%11s bytes copied during GC (scavenged)\n", temp);
527 ullong_format_string(GC_tot_scavd_copied*sizeof(W_),
528 temp, rtsTrue/*commas*/);
529 statsPrintf("%11s bytes copied during GC (not scavenged)\n", temp);
531 if ( ResidencySamples > 0 ) {
532 ullong_format_string(MaxResidency*sizeof(W_),
533 temp, rtsTrue/*commas*/);
534 statsPrintf("%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 statsPrintf("%11d collections in generation %d (%6.2fs)\n",
542 generations[g].collections, g,
543 TICK_TO_DBL(GC_coll_times[g]));
546 statsPrintf("\n%11ld Mb total memory in use\n\n",
547 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
549 #if defined(THREADED_RTS)
553 for (i = 0, task = all_tasks;
555 i++, task = task->all_link) {
556 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
557 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
559 (task->tso == NULL) ? "(worker)" : "(bound)",
560 TICK_TO_DBL(task->mut_time),
561 TICK_TO_DBL(task->mut_etime),
562 TICK_TO_DBL(task->gc_time),
563 TICK_TO_DBL(task->gc_etime));
568 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
569 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
570 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
571 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
572 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
573 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
575 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
576 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
577 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
578 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
580 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
581 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
582 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
583 TICK_TO_DBL(time), TICK_TO_DBL(etime));
584 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
585 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
586 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
588 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
589 ullong_format_string(0, temp, rtsTrue/*commas*/);
591 ullong_format_string(
592 (ullong)((GC_tot_alloc*sizeof(W_))/
593 TICK_TO_DBL(time - GC_tot_time -
594 PROF_VAL(RP_tot_time + HC_tot_time))),
595 temp, rtsTrue/*commas*/);
597 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
599 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
600 TICK_TO_DBL(time - GC_tot_time -
601 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
603 TICK_TO_DBL(time - GC_tot_time -
604 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
605 / TICK_TO_DBL(etime));
616 /* PAPI reporting, should put somewhere else?
617 * Note that the cycles are counted _after_ the initialization of the RTS -- AR */
619 statsPrintf(" -- CPU Mutator counters --\n");
621 papi_report(MutatorCounters);
623 statsPrintf("\n -- CPU GC counters --\n");
625 papi_report(GCCounters);
629 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
630 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
631 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
632 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",
634 ResidencySamples == 0 ? 0 :
635 AvgResidency*sizeof(W_)/ResidencySamples,
636 MaxResidency*sizeof(W_),
638 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
639 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
640 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
641 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
648 stgFree(GC_coll_times);
649 GC_coll_times = NULL;
652 /* -----------------------------------------------------------------------------
655 Produce some detailed info on the state of the generational GC.
656 -------------------------------------------------------------------------- */
659 statDescribeGens(void)
667 " Gen Steps Max Mut-list Step Blocks Live Large\n"
668 " Blocks Bytes Objects\n");
671 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
672 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
673 mut += (bd->free - bd->start) * sizeof(W_);
676 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
677 generations[g].max_blocks, mut);
679 for (s = 0; s < generations[g].n_steps; s++) {
680 step = &generations[g].steps[s];
682 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
685 live = step->n_large_blocks * BLOCK_SIZE;
687 // This live figure will be slightly less that the "live" figure
688 // given by +RTS -Sstderr, because we take don't count the
689 // slop at the end of each block.
690 for (; bd; bd = bd->link) {
691 live += (bd->free - bd->start) * sizeof(W_);
694 debugBelch("%36s","");
696 debugBelch("%6d %8d %8ld %8d\n", s, step->n_blocks,
704 /* -----------------------------------------------------------------------------
705 Stats available via a programmatic interface, so eg. GHCi can time
706 each compilation and expression evaluation.
707 -------------------------------------------------------------------------- */
709 extern HsInt64 getAllocations( void )
710 { return (HsInt64)total_allocated * sizeof(W_); }
712 /* -----------------------------------------------------------------------------
713 Dumping stuff in the stats file, or via the debug message interface
714 -------------------------------------------------------------------------- */
717 statsPrintf( char *s, ... )
719 FILE *sf = RtsFlags.GcFlags.statsFile;
734 FILE *sf = RtsFlags.GcFlags.statsFile;
743 FILE *sf = RtsFlags.GcFlags.statsFile;