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;
69 static Ticks *GC_coll_etimes;
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,
143 (Ticks *)stgMallocBytes(
144 sizeof(Ticks)*RtsFlags.GcFlags.generations,
146 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
147 GC_coll_times[i] = 0;
148 GC_coll_etimes[i] = 0;
152 /* -----------------------------------------------------------------------------
153 Initialisation time...
154 -------------------------------------------------------------------------- */
161 elapsed = getProcessElapsedTime();
162 ElapsedTimeStart = elapsed;
170 getProcessTimes(&user, &elapsed);
173 InitElapsedStamp = elapsed;
174 if (ElapsedTimeStart > elapsed) {
177 InitElapsedTime = elapsed - ElapsedTimeStart;
180 /* We start counting events for the mutator
181 * when garbage collection starts
182 * we switch to the GC event set. */
183 papi_start_mutator_count();
185 /* This flag is needed to avoid counting the last GC */
186 papi_is_reporting = 1;
191 /* -----------------------------------------------------------------------------
192 stat_startExit and stat_endExit
194 These two measure the time taken in shutdownHaskell().
195 -------------------------------------------------------------------------- */
202 getProcessTimes(&user, &elapsed);
204 MutElapsedStamp = elapsed;
205 MutElapsedTime = elapsed - GCe_tot_time -
206 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
207 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
209 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
210 if (MutUserTime < 0) { MutUserTime = 0; }
213 /* We stop counting mutator events
214 * GC events are not being counted at this point */
215 papi_stop_mutator_count();
217 /* This flag is needed, because GC is run once more after this function */
218 papi_is_reporting = 0;
228 getProcessTimes(&user, &elapsed);
230 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
231 ExitElapsedTime = elapsed - MutElapsedStamp;
232 if (ExitUserTime < 0) {
235 if (ExitElapsedTime < 0) {
240 /* -----------------------------------------------------------------------------
241 Called at the beginning of each GC
242 -------------------------------------------------------------------------- */
244 static nat rub_bell = 0;
246 /* initialise global variables needed during GC
248 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
249 * needed if either PROFILING or DEBUGing is enabled
254 nat bell = RtsFlags.GcFlags.ringBell;
265 #if defined(PROFILING) || defined(DEBUG)
266 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
269 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
270 #if !defined(PROFILING) && !defined(DEBUG)
271 GC_start_time = getProcessCPUTime();
273 GCe_start_time = getProcessElapsedTime();
274 if (RtsFlags.GcFlags.giveStats) {
275 GC_start_faults = getPageFaults();
280 if(papi_is_reporting) {
281 /* Switch to counting GC events */
282 papi_stop_mutator_count();
283 papi_start_gc_count();
289 /* -----------------------------------------------------------------------------
290 Called at the end of each GC
291 -------------------------------------------------------------------------- */
294 stat_endGC (lnat alloc, lnat live, lnat copied, lnat gen)
296 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
297 Ticks time, etime, gc_time, gc_etime;
299 getProcessTimes(&time, &etime);
300 gc_time = time - GC_start_time;
301 gc_etime = etime - GCe_start_time;
303 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
304 nat faults = getPageFaults();
306 statsPrintf("%9ld %9ld %9ld",
307 alloc*sizeof(W_), copied*sizeof(W_),
309 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
310 TICK_TO_DBL(gc_time),
311 TICK_TO_DBL(gc_etime),
313 TICK_TO_DBL(etime - ElapsedTimeStart),
314 faults - GC_start_faults,
315 GC_start_faults - GC_end_faults,
318 GC_end_faults = faults;
322 GC_coll_times[gen] += gc_time;
323 GC_coll_etimes[gen] += gc_etime;
325 GC_tot_copied += (ullong) copied;
326 GC_tot_alloc += (ullong) alloc;
327 GC_tot_time += gc_time;
328 GCe_tot_time += gc_etime;
330 #if defined(THREADED_RTS)
333 if ((task = myTask()) != NULL) {
334 task->gc_time += gc_time;
335 task->gc_etime += gc_etime;
340 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
341 if (live > MaxResidency) {
345 AvgResidency += live;
350 debugBelch("\b\b\b \b\b\b");
355 if(papi_is_reporting) {
356 /* Switch to counting mutator events */
358 papi_stop_gc0_count();
360 papi_stop_gc1_count();
362 papi_start_mutator_count();
367 /* -----------------------------------------------------------------------------
368 Called at the beginning of each Retainer Profiliing
369 -------------------------------------------------------------------------- */
375 getProcessTimes( &user, &elapsed );
377 RP_start_time = user;
378 RPe_start_time = elapsed;
380 #endif /* PROFILING */
382 /* -----------------------------------------------------------------------------
383 Called at the end of each Retainer Profiliing
384 -------------------------------------------------------------------------- */
389 nat retainerGeneration,
390 #ifdef DEBUG_RETAINER
394 double averageNumVisit)
397 getProcessTimes( &user, &elapsed );
399 RP_tot_time += user - RP_start_time;
400 RPe_tot_time += elapsed - RPe_start_time;
402 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
403 retainerGeneration, mut_user_time_during_RP());
404 #ifdef DEBUG_RETAINER
405 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
406 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
408 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
410 #endif /* PROFILING */
412 /* -----------------------------------------------------------------------------
413 Called at the beginning of each heap census
414 -------------------------------------------------------------------------- */
417 stat_startHeapCensus(void)
420 getProcessTimes( &user, &elapsed );
422 HC_start_time = user;
423 HCe_start_time = elapsed;
425 #endif /* PROFILING */
427 /* -----------------------------------------------------------------------------
428 Called at the end of each heap census
429 -------------------------------------------------------------------------- */
432 stat_endHeapCensus(void)
435 getProcessTimes( &user, &elapsed );
437 HC_tot_time += user - HC_start_time;
438 HCe_tot_time += elapsed - HCe_start_time;
440 #endif /* PROFILING */
442 /* -----------------------------------------------------------------------------
443 Called at the end of execution
445 NOTE: number of allocations is not entirely accurate: it doesn't
446 take into account the few bytes at the end of the heap that
447 were left unused when the heap-check failed.
448 -------------------------------------------------------------------------- */
451 #define TICK_VAR(arity) \
452 extern StgInt SLOW_CALLS_##arity; \
453 extern StgInt RIGHT_ARITY_##arity; \
454 extern StgInt TAGGED_PTR_##arity;
456 #define TICK_VAR_INI(arity) \
457 StgInt SLOW_CALLS_##arity = 1; \
458 StgInt RIGHT_ARITY_##arity = 1; \
459 StgInt TAGGED_PTR_##arity = 0;
461 extern StgInt TOTAL_CALLS;
469 StgInt TOTAL_CALLS=1;
472 /* Report the value of a counter */
473 #define REPORT(counter) \
475 ullong_format_string(counter,temp,rtsTrue/*commas*/); \
476 statsPrintf(" (" #counter ") : %s\n",temp); \
479 /* Report the value of a counter as a percentage of another counter */
480 #define REPORT_PCT(counter,countertot) \
481 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
482 counter*100.0/countertot)
484 #define TICK_PRINT(arity) \
485 REPORT(SLOW_CALLS_##arity); \
486 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
487 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
488 REPORT(RIGHT_ARITY_##arity); \
489 REPORT(TAGGED_PTR_##arity)
491 #define TICK_PRINT_TOT(arity) \
492 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
493 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
499 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
501 char temp[BIG_STRING_LEN];
504 nat g, total_collections = 0;
506 getProcessTimes( &time, &etime );
507 etime -= ElapsedTimeStart;
509 GC_tot_alloc += alloc;
511 /* Count total garbage collections */
512 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
513 total_collections += generations[g].collections;
515 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
516 if (time == 0.0) time = 1;
517 if (etime == 0.0) etime = 1;
519 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
520 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
521 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
524 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
525 ullong_format_string(GC_tot_alloc*sizeof(W_),
526 temp, rtsTrue/*commas*/);
527 statsPrintf("%11s bytes allocated in the heap\n", temp);
529 ullong_format_string(GC_tot_copied*sizeof(W_),
530 temp, rtsTrue/*commas*/);
531 statsPrintf("%11s bytes copied during GC\n", temp);
533 if ( ResidencySamples > 0 ) {
534 ullong_format_string(MaxResidency*sizeof(W_),
535 temp, rtsTrue/*commas*/);
536 statsPrintf("%11s bytes maximum residency (%ld sample(s))\n",
537 temp, ResidencySamples);
541 /* Print garbage collections in each gen */
542 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
543 statsPrintf("%11d collections in generation %d, %6.2fs, %6.2fs elapsed\n",
544 generations[g].collections, g,
545 TICK_TO_DBL(GC_coll_times[g]),
546 TICK_TO_DBL(GC_coll_etimes[g]));
549 statsPrintf("\n%11ld MB total memory in use\n\n",
550 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
552 #if defined(THREADED_RTS)
556 for (i = 0, task = all_tasks;
558 i++, task = task->all_link) {
559 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
560 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
562 (task->tso == NULL) ? "(worker)" : "(bound)",
563 TICK_TO_DBL(task->mut_time),
564 TICK_TO_DBL(task->mut_etime),
565 TICK_TO_DBL(task->gc_time),
566 TICK_TO_DBL(task->gc_etime));
571 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
572 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
573 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
574 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
575 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
576 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
578 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
579 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
580 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
581 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
583 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
584 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
585 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
586 TICK_TO_DBL(time), TICK_TO_DBL(etime));
587 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
588 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
589 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
591 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
592 ullong_format_string(0, temp, rtsTrue/*commas*/);
594 ullong_format_string(
595 (ullong)((GC_tot_alloc*sizeof(W_))/
596 TICK_TO_DBL(time - GC_tot_time -
597 PROF_VAL(RP_tot_time + HC_tot_time))),
598 temp, rtsTrue/*commas*/);
600 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
602 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
603 TICK_TO_DBL(time - GC_tot_time -
604 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
606 TICK_TO_DBL(time - GC_tot_time -
607 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
608 / TICK_TO_DBL(etime));
623 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
624 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
625 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
626 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",
628 ResidencySamples == 0 ? 0 :
629 AvgResidency*sizeof(W_)/ResidencySamples,
630 MaxResidency*sizeof(W_),
632 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
633 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
634 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
635 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
642 stgFree(GC_coll_times);
643 GC_coll_times = NULL;
646 /* -----------------------------------------------------------------------------
649 Produce some detailed info on the state of the generational GC.
650 -------------------------------------------------------------------------- */
653 statDescribeGens(void)
661 " Gen Steps Max Mut-list Step Blocks Live Large\n"
662 " Blocks Bytes Objects\n");
665 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
666 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
667 mut += (bd->free - bd->start) * sizeof(W_);
670 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
671 generations[g].max_blocks, mut);
673 for (s = 0; s < generations[g].n_steps; s++) {
674 step = &generations[g].steps[s];
676 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
679 // This live figure will be slightly less that the "live" figure
680 // given by +RTS -Sstderr, because we take don't count the
681 // slop at the end of each block.
682 live += countOccupied(step->blocks) + countOccupied(step->large_objects);
684 debugBelch("%36s","");
686 debugBelch("%6d %8d %8ld %8d\n", s, step->n_blocks,
694 /* -----------------------------------------------------------------------------
695 Stats available via a programmatic interface, so eg. GHCi can time
696 each compilation and expression evaluation.
697 -------------------------------------------------------------------------- */
699 extern HsInt64 getAllocations( void )
700 { return (HsInt64)total_allocated * sizeof(W_); }
702 /* -----------------------------------------------------------------------------
703 Dumping stuff in the stats file, or via the debug message interface
704 -------------------------------------------------------------------------- */
707 statsPrintf( char *s, ... )
709 FILE *sf = RtsFlags.GcFlags.statsFile;
724 FILE *sf = RtsFlags.GcFlags.statsFile;
733 FILE *sf = RtsFlags.GcFlags.statsFile;