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 /* We start counting events for the mutator
175 * when garbage collection starts
176 * we switch to the GC event set. */
177 papi_start_mutator_count();
179 /* This flag is needed to avoid counting the last GC */
180 papi_is_reporting = 1;
185 /* -----------------------------------------------------------------------------
186 stat_startExit and stat_endExit
188 These two measure the time taken in shutdownHaskell().
189 -------------------------------------------------------------------------- */
196 getProcessTimes(&user, &elapsed);
198 MutElapsedStamp = elapsed;
199 MutElapsedTime = elapsed - GCe_tot_time -
200 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
201 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
203 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
204 if (MutUserTime < 0) { MutUserTime = 0; }
207 /* We stop counting mutator events
208 * GC events are not being counted at this point */
209 papi_stop_mutator_count();
211 /* This flag is needed, because GC is run once more after this function */
212 papi_is_reporting = 0;
222 getProcessTimes(&user, &elapsed);
224 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
225 ExitElapsedTime = elapsed - MutElapsedStamp;
226 if (ExitUserTime < 0) {
229 if (ExitElapsedTime < 0) {
234 /* -----------------------------------------------------------------------------
235 Called at the beginning of each GC
236 -------------------------------------------------------------------------- */
238 static nat rub_bell = 0;
240 /* initialise global variables needed during GC
242 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
243 * needed if either PROFILING or DEBUGing is enabled
248 nat bell = RtsFlags.GcFlags.ringBell;
259 #if defined(PROFILING) || defined(DEBUG)
260 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
263 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
264 #if !defined(PROFILING) && !defined(DEBUG)
265 GC_start_time = getProcessCPUTime();
267 GCe_start_time = getProcessElapsedTime();
268 if (RtsFlags.GcFlags.giveStats) {
269 GC_start_faults = getPageFaults();
274 if(papi_is_reporting) {
275 /* Switch to counting GC events */
276 papi_stop_mutator_count();
277 papi_start_gc_count();
283 /* -----------------------------------------------------------------------------
284 Called at the end of each GC
285 -------------------------------------------------------------------------- */
288 stat_endGC (lnat alloc, lnat live, lnat copied, lnat gen)
290 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
291 Ticks time, etime, gc_time, gc_etime;
293 getProcessTimes(&time, &etime);
294 gc_time = time - GC_start_time;
295 gc_etime = etime - GCe_start_time;
297 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
298 nat faults = getPageFaults();
300 statsPrintf("%9ld %9ld %9ld",
301 alloc*sizeof(W_), copied*sizeof(W_),
303 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
304 TICK_TO_DBL(gc_time),
305 TICK_TO_DBL(gc_etime),
307 TICK_TO_DBL(etime - ElapsedTimeStart),
308 faults - GC_start_faults,
309 GC_start_faults - GC_end_faults,
312 GC_end_faults = faults;
316 GC_coll_times[gen] += gc_time;
318 GC_tot_copied += (ullong) copied;
319 GC_tot_alloc += (ullong) alloc;
320 GC_tot_time += gc_time;
321 GCe_tot_time += gc_etime;
323 #if defined(THREADED_RTS)
326 if ((task = myTask()) != NULL) {
327 task->gc_time += gc_time;
328 task->gc_etime += gc_etime;
333 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
334 if (live > MaxResidency) {
338 AvgResidency += live;
343 debugBelch("\b\b\b \b\b\b");
348 if(papi_is_reporting) {
349 /* Switch to counting mutator events */
350 papi_stop_gc_count();
351 papi_start_mutator_count();
356 /* -----------------------------------------------------------------------------
357 Called at the beginning of each Retainer Profiliing
358 -------------------------------------------------------------------------- */
364 getProcessTimes( &user, &elapsed );
366 RP_start_time = user;
367 RPe_start_time = elapsed;
369 #endif /* PROFILING */
371 /* -----------------------------------------------------------------------------
372 Called at the end of each Retainer Profiliing
373 -------------------------------------------------------------------------- */
378 nat retainerGeneration,
379 #ifdef DEBUG_RETAINER
383 double averageNumVisit)
386 getProcessTimes( &user, &elapsed );
388 RP_tot_time += user - RP_start_time;
389 RPe_tot_time += elapsed - RPe_start_time;
391 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
392 retainerGeneration, mut_user_time_during_RP());
393 #ifdef DEBUG_RETAINER
394 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
395 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
397 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
399 #endif /* PROFILING */
401 /* -----------------------------------------------------------------------------
402 Called at the beginning of each heap census
403 -------------------------------------------------------------------------- */
406 stat_startHeapCensus(void)
409 getProcessTimes( &user, &elapsed );
411 HC_start_time = user;
412 HCe_start_time = elapsed;
414 #endif /* PROFILING */
416 /* -----------------------------------------------------------------------------
417 Called at the end of each heap census
418 -------------------------------------------------------------------------- */
421 stat_endHeapCensus(void)
424 getProcessTimes( &user, &elapsed );
426 HC_tot_time += user - HC_start_time;
427 HCe_tot_time += elapsed - HCe_start_time;
429 #endif /* PROFILING */
431 /* -----------------------------------------------------------------------------
432 Called at the end of execution
434 NOTE: number of allocations is not entirely accurate: it doesn't
435 take into account the few bytes at the end of the heap that
436 were left unused when the heap-check failed.
437 -------------------------------------------------------------------------- */
440 #define TICK_VAR(arity) \
441 extern StgInt SLOW_CALLS_##arity; \
442 extern StgInt RIGHT_ARITY_##arity; \
443 extern StgInt TAGGED_PTR_##arity;
445 #define TICK_VAR_INI(arity) \
446 StgInt SLOW_CALLS_##arity = 1; \
447 StgInt RIGHT_ARITY_##arity = 1; \
448 StgInt TAGGED_PTR_##arity = 0;
450 extern StgInt TOTAL_CALLS;
458 StgInt TOTAL_CALLS=1;
461 /* Report the value of a counter */
462 #define REPORT(counter) \
464 ullong_format_string(counter,temp,rtsTrue/*commas*/); \
465 statsPrintf(" (" #counter ") : %s\n",temp); \
468 /* Report the value of a counter as a percentage of another counter */
469 #define REPORT_PCT(counter,countertot) \
470 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
471 counter*100.0/countertot)
473 #define TICK_PRINT(arity) \
474 REPORT(SLOW_CALLS_##arity); \
475 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
476 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
477 REPORT(RIGHT_ARITY_##arity); \
478 REPORT(TAGGED_PTR_##arity)
480 #define TICK_PRINT_TOT(arity) \
481 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
482 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
488 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
490 char temp[BIG_STRING_LEN];
493 nat g, total_collections = 0;
495 getProcessTimes( &time, &etime );
496 etime -= ElapsedTimeStart;
498 GC_tot_alloc += alloc;
500 /* Count total garbage collections */
501 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
502 total_collections += generations[g].collections;
504 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
505 if (time == 0.0) time = 1;
506 if (etime == 0.0) etime = 1;
508 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
509 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
510 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
513 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
514 ullong_format_string(GC_tot_alloc*sizeof(W_),
515 temp, rtsTrue/*commas*/);
516 statsPrintf("%11s bytes allocated in the heap\n", temp);
518 ullong_format_string(GC_tot_copied*sizeof(W_),
519 temp, rtsTrue/*commas*/);
520 statsPrintf("%11s bytes copied during GC\n", temp);
522 if ( ResidencySamples > 0 ) {
523 ullong_format_string(MaxResidency*sizeof(W_),
524 temp, rtsTrue/*commas*/);
525 statsPrintf("%11s bytes maximum residency (%ld sample(s))\n",
526 temp, ResidencySamples);
530 /* Print garbage collections in each gen */
531 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
532 statsPrintf("%11d collections in generation %d (%6.2fs)\n",
533 generations[g].collections, g,
534 TICK_TO_DBL(GC_coll_times[g]));
537 statsPrintf("\n%11ld Mb total memory in use\n\n",
538 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
540 #if defined(THREADED_RTS)
544 for (i = 0, task = all_tasks;
546 i++, task = task->all_link) {
547 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
548 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
550 (task->tso == NULL) ? "(worker)" : "(bound)",
551 TICK_TO_DBL(task->mut_time),
552 TICK_TO_DBL(task->mut_etime),
553 TICK_TO_DBL(task->gc_time),
554 TICK_TO_DBL(task->gc_etime));
559 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
560 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
561 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
562 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
563 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
564 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
566 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
567 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
568 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
569 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
571 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
572 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
573 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
574 TICK_TO_DBL(time), TICK_TO_DBL(etime));
575 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
576 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
577 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
579 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
580 ullong_format_string(0, temp, rtsTrue/*commas*/);
582 ullong_format_string(
583 (ullong)((GC_tot_alloc*sizeof(W_))/
584 TICK_TO_DBL(time - GC_tot_time -
585 PROF_VAL(RP_tot_time + HC_tot_time))),
586 temp, rtsTrue/*commas*/);
588 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
590 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
591 TICK_TO_DBL(time - GC_tot_time -
592 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
594 TICK_TO_DBL(time - GC_tot_time -
595 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
596 / TICK_TO_DBL(etime));
611 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
612 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
613 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
614 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",
616 ResidencySamples == 0 ? 0 :
617 AvgResidency*sizeof(W_)/ResidencySamples,
618 MaxResidency*sizeof(W_),
620 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
621 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
622 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
623 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
630 stgFree(GC_coll_times);
631 GC_coll_times = NULL;
634 /* -----------------------------------------------------------------------------
637 Produce some detailed info on the state of the generational GC.
638 -------------------------------------------------------------------------- */
641 statDescribeGens(void)
649 " Gen Steps Max Mut-list Step Blocks Live Large\n"
650 " Blocks Bytes Objects\n");
653 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
654 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
655 mut += (bd->free - bd->start) * sizeof(W_);
658 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
659 generations[g].max_blocks, mut);
661 for (s = 0; s < generations[g].n_steps; s++) {
662 step = &generations[g].steps[s];
664 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
667 live = step->n_large_blocks * BLOCK_SIZE;
669 // This live figure will be slightly less that the "live" figure
670 // given by +RTS -Sstderr, because we take don't count the
671 // slop at the end of each block.
672 for (; bd; bd = bd->link) {
673 live += (bd->free - bd->start) * sizeof(W_);
676 debugBelch("%36s","");
678 debugBelch("%6d %8d %8ld %8d\n", s, step->n_blocks,
686 /* -----------------------------------------------------------------------------
687 Stats available via a programmatic interface, so eg. GHCi can time
688 each compilation and expression evaluation.
689 -------------------------------------------------------------------------- */
691 extern HsInt64 getAllocations( void )
692 { return (HsInt64)total_allocated * sizeof(W_); }
694 /* -----------------------------------------------------------------------------
695 Dumping stuff in the stats file, or via the debug message interface
696 -------------------------------------------------------------------------- */
699 statsPrintf( char *s, ... )
701 FILE *sf = RtsFlags.GcFlags.statsFile;
716 FILE *sf = RtsFlags.GcFlags.statsFile;
725 FILE *sf = RtsFlags.GcFlags.statsFile;