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;
45 static Ticks GC_start_time = 0, GC_tot_time = 0; /* User GC Time */
46 static Ticks GCe_start_time = 0, GCe_tot_time = 0; /* Elapsed GC time */
49 static Ticks RP_start_time = 0, RP_tot_time = 0; /* retainer prof user time */
50 static Ticks RPe_start_time = 0, RPe_tot_time = 0; /* retainer prof elap time */
52 static Ticks HC_start_time, HC_tot_time = 0; // heap census prof user time
53 static Ticks HCe_start_time, HCe_tot_time = 0; // heap census prof elap time
57 #define PROF_VAL(x) (x)
62 static lnat MaxResidency = 0; // in words; for stats only
63 static lnat AvgResidency = 0;
64 static lnat ResidencySamples = 0; // for stats only
66 static lnat GC_start_faults = 0, GC_end_faults = 0;
68 static Ticks *GC_coll_times;
70 static void statsFlush( void );
71 static void statsClose( void );
73 Ticks stat_getElapsedGCTime(void)
78 Ticks stat_getElapsedTime(void)
80 return getProcessElapsedTime() - ElapsedTimeStart;
83 /* mut_user_time_during_GC() and mut_user_time()
85 * The former function can be used to get the current mutator time
86 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
87 * used in the heap profiler for accurately time stamping the heap
90 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
91 * defined in stat_startGC() - to minimise system calls,
92 * GC_start_time is, however, only defined when really needed (check
93 * stat_startGC() for details)
96 mut_user_time_during_GC( void )
98 return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
102 mut_user_time( void )
105 user = getProcessCPUTime();
106 return TICK_TO_DBL(user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
111 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
112 it returns the MUT time during retainer profiling.
113 The same is for mut_user_time_during_HC();
116 mut_user_time_during_RP( void )
118 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
122 mut_user_time_during_heap_census( void )
124 return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
126 #endif /* PROFILING */
133 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
134 statsPrintf(" Alloc Copied Live GC GC TOT TOT Page Flts\n");
135 statsPrintf(" bytes bytes bytes user elap user elap\n");
138 (Ticks *)stgMallocBytes(
139 sizeof(Ticks)*RtsFlags.GcFlags.generations,
141 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
142 GC_coll_times[i] = 0;
146 /* -----------------------------------------------------------------------------
147 Initialisation time...
148 -------------------------------------------------------------------------- */
155 elapsed = getProcessElapsedTime();
156 ElapsedTimeStart = elapsed;
164 getProcessTimes(&user, &elapsed);
167 InitElapsedStamp = elapsed;
168 if (ElapsedTimeStart > elapsed) {
171 InitElapsedTime = elapsed - ElapsedTimeStart;
174 papi_init_eventsets();
176 /* We start counting events for the mutator
177 * when garbage collection starts
178 * we switch to the GC event set. */
179 papi_start_mutator_count();
181 /* This flag is needed to avoid counting the last GC */
182 papi_is_reporting = 1;
187 /* -----------------------------------------------------------------------------
188 stat_startExit and stat_endExit
190 These two measure the time taken in shutdownHaskell().
191 -------------------------------------------------------------------------- */
198 getProcessTimes(&user, &elapsed);
200 MutElapsedStamp = elapsed;
201 MutElapsedTime = elapsed - GCe_tot_time -
202 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
203 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
205 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
206 if (MutUserTime < 0) { MutUserTime = 0; }
209 /* We stop counting mutator events
210 * GC events are not being counted at this point */
211 papi_stop_mutator_count();
213 /* This flag is needed, because GC is run once more after this function */
214 papi_is_reporting = 0;
224 getProcessTimes(&user, &elapsed);
226 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
227 ExitElapsedTime = elapsed - MutElapsedStamp;
228 if (ExitUserTime < 0) {
231 if (ExitElapsedTime < 0) {
236 /* -----------------------------------------------------------------------------
237 Called at the beginning of each GC
238 -------------------------------------------------------------------------- */
240 static nat rub_bell = 0;
242 /* initialise global variables needed during GC
244 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
245 * needed if either PROFILING or DEBUGing is enabled
250 nat bell = RtsFlags.GcFlags.ringBell;
261 #if defined(PROFILING) || defined(DEBUG)
262 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
265 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
266 #if !defined(PROFILING) && !defined(DEBUG)
267 GC_start_time = getProcessCPUTime();
269 GCe_start_time = getProcessElapsedTime();
270 if (RtsFlags.GcFlags.giveStats) {
271 GC_start_faults = getPageFaults();
276 if(papi_is_reporting) {
277 /* Switch to counting GC events */
278 papi_stop_mutator_count();
279 papi_start_gc_count();
285 /* -----------------------------------------------------------------------------
286 Called at the end of each GC
287 -------------------------------------------------------------------------- */
290 stat_endGC (lnat alloc, lnat live, lnat copied, lnat gen)
292 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
293 Ticks time, etime, gc_time, gc_etime;
295 getProcessTimes(&time, &etime);
296 gc_time = time - GC_start_time;
297 gc_etime = etime - GCe_start_time;
299 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
300 nat faults = getPageFaults();
302 statsPrintf("%9ld %9ld %9ld",
303 alloc*sizeof(W_), copied*sizeof(W_),
305 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
306 TICK_TO_DBL(gc_time),
307 TICK_TO_DBL(gc_etime),
309 TICK_TO_DBL(etime - ElapsedTimeStart),
310 faults - GC_start_faults,
311 GC_start_faults - GC_end_faults,
314 GC_end_faults = faults;
318 GC_coll_times[gen] += gc_time;
320 GC_tot_copied += (ullong) copied;
321 GC_tot_alloc += (ullong) alloc;
322 GC_tot_time += gc_time;
323 GCe_tot_time += gc_etime;
325 #if defined(THREADED_RTS)
328 if ((task = myTask()) != NULL) {
329 task->gc_time += gc_time;
330 task->gc_etime += gc_etime;
335 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
336 if (live > MaxResidency) {
340 AvgResidency += live;
345 debugBelch("\b\b\b \b\b\b");
350 if(papi_is_reporting) {
351 /* Switch to counting mutator events */
352 papi_stop_gc_count();
353 papi_start_mutator_count();
358 /* -----------------------------------------------------------------------------
359 Called at the beginning of each Retainer Profiliing
360 -------------------------------------------------------------------------- */
366 getProcessTimes( &user, &elapsed );
368 RP_start_time = user;
369 RPe_start_time = elapsed;
371 #endif /* PROFILING */
373 /* -----------------------------------------------------------------------------
374 Called at the end of each Retainer Profiliing
375 -------------------------------------------------------------------------- */
380 nat retainerGeneration,
381 #ifdef DEBUG_RETAINER
385 double averageNumVisit)
388 getProcessTimes( &user, &elapsed );
390 RP_tot_time += user - RP_start_time;
391 RPe_tot_time += elapsed - RPe_start_time;
393 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
394 retainerGeneration, mut_user_time_during_RP());
395 #ifdef DEBUG_RETAINER
396 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
397 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
399 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
401 #endif /* PROFILING */
403 /* -----------------------------------------------------------------------------
404 Called at the beginning of each heap census
405 -------------------------------------------------------------------------- */
408 stat_startHeapCensus(void)
411 getProcessTimes( &user, &elapsed );
413 HC_start_time = user;
414 HCe_start_time = elapsed;
416 #endif /* PROFILING */
418 /* -----------------------------------------------------------------------------
419 Called at the end of each heap census
420 -------------------------------------------------------------------------- */
423 stat_endHeapCensus(void)
426 getProcessTimes( &user, &elapsed );
428 HC_tot_time += user - HC_start_time;
429 HCe_tot_time += elapsed - HCe_start_time;
431 #endif /* PROFILING */
433 /* -----------------------------------------------------------------------------
434 Called at the end of execution
436 NOTE: number of allocations is not entirely accurate: it doesn't
437 take into account the few bytes at the end of the heap that
438 were left unused when the heap-check failed.
439 -------------------------------------------------------------------------- */
442 #define TICK_VAR(arity) \
443 extern StgInt SLOW_CALLS_##arity; \
444 extern StgInt RIGHT_ARITY_##arity; \
445 extern StgInt TAGGED_PTR_##arity;
447 #define TICK_VAR_INI(arity) \
448 StgInt SLOW_CALLS_##arity = 1; \
449 StgInt RIGHT_ARITY_##arity = 1; \
450 StgInt TAGGED_PTR_##arity = 0;
452 extern StgInt TOTAL_CALLS;
460 StgInt TOTAL_CALLS=1;
463 /* Report the value of a counter */
464 #define REPORT(counter) \
466 ullong_format_string(counter,temp,rtsTrue/*commas*/); \
467 statsPrintf(" (" #counter ") : %s\n",temp); \
470 /* Report the value of a counter as a percentage of another counter */
471 #define REPORT_PCT(counter,countertot) \
472 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
473 counter*100.0/countertot)
475 #define TICK_PRINT(arity) \
476 REPORT(SLOW_CALLS_##arity); \
477 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
478 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
479 REPORT(RIGHT_ARITY_##arity); \
480 REPORT(TAGGED_PTR_##arity)
482 #define TICK_PRINT_TOT(arity) \
483 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
484 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
490 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
492 char temp[BIG_STRING_LEN];
495 nat g, total_collections = 0;
497 getProcessTimes( &time, &etime );
498 etime -= ElapsedTimeStart;
500 GC_tot_alloc += alloc;
502 /* Count total garbage collections */
503 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
504 total_collections += generations[g].collections;
506 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
507 if (time == 0.0) time = 1;
508 if (etime == 0.0) etime = 1;
510 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
511 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
512 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
515 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
516 ullong_format_string(GC_tot_alloc*sizeof(W_),
517 temp, rtsTrue/*commas*/);
518 statsPrintf("%11s bytes allocated in the heap\n", temp);
520 ullong_format_string(GC_tot_copied*sizeof(W_),
521 temp, rtsTrue/*commas*/);
522 statsPrintf("%11s bytes copied during GC\n", temp);
524 if ( ResidencySamples > 0 ) {
525 ullong_format_string(MaxResidency*sizeof(W_),
526 temp, rtsTrue/*commas*/);
527 statsPrintf("%11s bytes maximum residency (%ld sample(s))\n",
528 temp, ResidencySamples);
532 /* Print garbage collections in each gen */
533 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
534 statsPrintf("%11d collections in generation %d (%6.2fs)\n",
535 generations[g].collections, g,
536 TICK_TO_DBL(GC_coll_times[g]));
539 statsPrintf("\n%11ld Mb total memory in use\n\n",
540 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
542 #if defined(THREADED_RTS)
546 for (i = 0, task = all_tasks;
548 i++, task = task->all_link) {
549 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
550 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
552 (task->tso == NULL) ? "(worker)" : "(bound)",
553 TICK_TO_DBL(task->mut_time),
554 TICK_TO_DBL(task->mut_etime),
555 TICK_TO_DBL(task->gc_time),
556 TICK_TO_DBL(task->gc_etime));
561 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
562 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
563 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
564 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
565 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
566 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
568 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
569 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
570 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
571 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
573 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
574 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
575 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
576 TICK_TO_DBL(time), TICK_TO_DBL(etime));
577 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
578 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
579 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
581 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
582 ullong_format_string(0, temp, rtsTrue/*commas*/);
584 ullong_format_string(
585 (ullong)((GC_tot_alloc*sizeof(W_))/
586 TICK_TO_DBL(time - GC_tot_time -
587 PROF_VAL(RP_tot_time + HC_tot_time))),
588 temp, rtsTrue/*commas*/);
590 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
592 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
593 TICK_TO_DBL(time - GC_tot_time -
594 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
596 TICK_TO_DBL(time - GC_tot_time -
597 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
598 / TICK_TO_DBL(etime));
609 /* PAPI reporting, should put somewhere else?
610 * Note that the cycles are counted _after_ the initialization of the RTS -- AR */
612 statsPrintf(" -- CPU Mutator counters --\n");
614 papi_report(MutatorCounters);
616 statsPrintf("\n -- CPU GC counters --\n");
618 papi_report(GCCounters);
622 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
623 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
624 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
625 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",
627 ResidencySamples == 0 ? 0 :
628 AvgResidency*sizeof(W_)/ResidencySamples,
629 MaxResidency*sizeof(W_),
631 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
632 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
633 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
634 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
641 stgFree(GC_coll_times);
642 GC_coll_times = NULL;
645 /* -----------------------------------------------------------------------------
648 Produce some detailed info on the state of the generational GC.
649 -------------------------------------------------------------------------- */
652 statDescribeGens(void)
660 " Gen Steps Max Mut-list Step Blocks Live Large\n"
661 " Blocks Bytes Objects\n");
664 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
665 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
666 mut += (bd->free - bd->start) * sizeof(W_);
669 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
670 generations[g].max_blocks, mut);
672 for (s = 0; s < generations[g].n_steps; s++) {
673 step = &generations[g].steps[s];
675 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
678 live = step->n_large_blocks * BLOCK_SIZE;
680 // This live figure will be slightly less that the "live" figure
681 // given by +RTS -Sstderr, because we take don't count the
682 // slop at the end of each block.
683 for (; bd; bd = bd->link) {
684 live += (bd->free - bd->start) * sizeof(W_);
687 debugBelch("%36s","");
689 debugBelch("%6d %8d %8ld %8d\n", s, step->n_blocks,
697 /* -----------------------------------------------------------------------------
698 Stats available via a programmatic interface, so eg. GHCi can time
699 each compilation and expression evaluation.
700 -------------------------------------------------------------------------- */
702 extern HsInt64 getAllocations( void )
703 { return (HsInt64)total_allocated * sizeof(W_); }
705 /* -----------------------------------------------------------------------------
706 Dumping stuff in the stats file, or via the debug message interface
707 -------------------------------------------------------------------------- */
710 statsPrintf( char *s, ... )
712 FILE *sf = RtsFlags.GcFlags.statsFile;
727 FILE *sf = RtsFlags.GcFlags.statsFile;
736 FILE *sf = RtsFlags.GcFlags.statsFile;