1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2005
5 * Statistics and timing-related functions.
7 * ---------------------------------------------------------------------------*/
15 #include "ParTicky.h" /* ToDo: move into Rts.h */
16 #include "Profiling.h"
21 #define BIG_STRING_LEN 512
23 #define TICK_TO_DBL(t) ((double)(t) / TICKS_PER_SECOND)
25 static Ticks ElapsedTimeStart = 0;
27 static Ticks InitUserTime = 0;
28 static Ticks InitElapsedTime = 0;
29 static Ticks InitElapsedStamp = 0;
31 static Ticks MutUserTime = 0;
32 static Ticks MutElapsedTime = 0;
33 static Ticks MutElapsedStamp = 0;
35 static Ticks ExitUserTime = 0;
36 static Ticks ExitElapsedTime = 0;
38 static ullong GC_tot_alloc = 0;
39 static ullong GC_tot_copied = 0;
40 static ullong GC_tot_scavd_copied = 0;
42 static Ticks GC_start_time = 0, GC_tot_time = 0; /* User GC Time */
43 static Ticks GCe_start_time = 0, GCe_tot_time = 0; /* Elapsed GC time */
46 static Ticks RP_start_time = 0, RP_tot_time = 0; /* retainer prof user time */
47 static Ticks RPe_start_time = 0, RPe_tot_time = 0; /* retainer prof elap time */
49 static Ticks HC_start_time, HC_tot_time = 0; // heap census prof user time
50 static Ticks HCe_start_time, HCe_tot_time = 0; // heap census prof elap time
54 #define PROF_VAL(x) (x)
59 static lnat MaxResidency = 0; // in words; for stats only
60 static lnat AvgResidency = 0;
61 static lnat ResidencySamples = 0; // for stats only
63 static lnat GC_start_faults = 0, GC_end_faults = 0;
65 static Ticks *GC_coll_times;
67 static void statsPrintf( char *s, ... )
68 GNUC3_ATTRIBUTE(format (printf, 1, 2));
70 static void statsFlush( void );
71 static void statsClose( void );
73 Ticks stat_getElapsedGCTime(void)
78 /* mut_user_time_during_GC() and mut_user_time()
80 * The former function can be used to get the current mutator time
81 * *during* a GC, i.e. between stat_startGC and stat_endGC. This is
82 * used in the heap profiler for accurately time stamping the heap
85 * ATTENTION: mut_user_time_during_GC() relies on GC_start_time being
86 * defined in stat_startGC() - to minimise system calls,
87 * GC_start_time is, however, only defined when really needed (check
88 * stat_startGC() for details)
91 mut_user_time_during_GC( void )
93 return TICK_TO_DBL(GC_start_time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
100 user = getProcessCPUTime();
101 return TICK_TO_DBL(user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time));
106 mut_user_time_during_RP() is similar to mut_user_time_during_GC();
107 it returns the MUT time during retainer profiling.
108 The same is for mut_user_time_during_HC();
111 mut_user_time_during_RP( void )
113 return TICK_TO_DBL(RP_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
117 mut_user_time_during_heap_census( void )
119 return TICK_TO_DBL(HC_start_time - GC_tot_time - RP_tot_time - HC_tot_time);
121 #endif /* PROFILING */
128 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
129 statsPrintf(" Alloc Copied Live GC GC TOT TOT Page Flts\n");
130 statsPrintf(" bytes bytes bytes user elap user elap\n");
133 (Ticks *)stgMallocBytes(
134 sizeof(Ticks)*RtsFlags.GcFlags.generations,
136 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
137 GC_coll_times[i] = 0;
141 /* -----------------------------------------------------------------------------
142 Initialisation time...
143 -------------------------------------------------------------------------- */
150 elapsed = getProcessElapsedTime();
151 ElapsedTimeStart = elapsed;
159 getProcessTimes(&user, &elapsed);
162 InitElapsedStamp = elapsed;
163 if (ElapsedTimeStart > elapsed) {
166 InitElapsedTime = elapsed - ElapsedTimeStart;
170 /* -----------------------------------------------------------------------------
171 stat_startExit and stat_endExit
173 These two measure the time taken in shutdownHaskell().
174 -------------------------------------------------------------------------- */
181 getProcessTimes(&user, &elapsed);
183 MutElapsedStamp = elapsed;
184 MutElapsedTime = elapsed - GCe_tot_time -
185 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
186 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
188 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
189 if (MutUserTime < 0) { MutUserTime = 0; }
197 getProcessTimes(&user, &elapsed);
199 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
200 ExitElapsedTime = elapsed - MutElapsedStamp;
201 if (ExitUserTime < 0) {
204 if (ExitElapsedTime < 0) {
209 /* -----------------------------------------------------------------------------
210 Called at the beginning of each GC
211 -------------------------------------------------------------------------- */
213 static nat rub_bell = 0;
215 /* initialise global variables needed during GC
217 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
218 * needed if either PROFILING or DEBUGing is enabled
223 nat bell = RtsFlags.GcFlags.ringBell;
234 #if defined(PROFILING) || defined(DEBUG)
235 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
238 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
239 #if !defined(PROFILING) && !defined(DEBUG)
240 GC_start_time = getProcessCPUTime();
242 GCe_start_time = getProcessElapsedTime();
243 if (RtsFlags.GcFlags.giveStats) {
244 GC_start_faults = getPageFaults();
249 /* -----------------------------------------------------------------------------
250 Called at the end of each GC
251 -------------------------------------------------------------------------- */
254 stat_endGC (lnat alloc, lnat live, lnat copied,
255 lnat scavd_copied, lnat gen)
257 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
258 Ticks time, etime, gc_time, gc_etime;
260 getProcessTimes(&time, &etime);
261 gc_time = time - GC_start_time;
262 gc_etime = etime - GCe_start_time;
264 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
265 nat faults = getPageFaults();
267 statsPrintf("%9ld %9ld %9ld",
268 alloc*sizeof(W_), (copied+scavd_copied)*sizeof(W_),
270 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
271 TICK_TO_DBL(gc_time),
272 TICK_TO_DBL(gc_etime),
274 TICK_TO_DBL(etime - ElapsedTimeStart),
275 faults - GC_start_faults,
276 GC_start_faults - GC_end_faults,
279 GC_end_faults = faults;
283 GC_coll_times[gen] += gc_time;
285 GC_tot_copied += (ullong) copied;
286 GC_tot_scavd_copied += (ullong) scavd_copied;
287 GC_tot_alloc += (ullong) alloc;
288 GC_tot_time += gc_time;
289 GCe_tot_time += gc_etime;
291 #if defined(THREADED_RTS)
294 if ((task = myTask()) != NULL) {
295 task->gc_time += gc_time;
296 task->gc_etime += gc_etime;
301 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
302 if (live > MaxResidency) {
306 AvgResidency += live;
311 debugBelch("\b\b\b \b\b\b");
316 /* -----------------------------------------------------------------------------
317 Called at the beginning of each Retainer Profiliing
318 -------------------------------------------------------------------------- */
324 getProcessTimes( &user, &elapsed );
326 RP_start_time = user;
327 RPe_start_time = elapsed;
329 #endif /* PROFILING */
331 /* -----------------------------------------------------------------------------
332 Called at the end of each Retainer Profiliing
333 -------------------------------------------------------------------------- */
338 nat retainerGeneration,
339 #ifdef DEBUG_RETAINER
343 double averageNumVisit)
346 getProcessTimes( &user, &elapsed );
348 RP_tot_time += user - RP_start_time;
349 RPe_tot_time += elapsed - RPe_start_time;
351 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
352 retainerGeneration, mut_user_time_during_RP());
353 #ifdef DEBUG_RETAINER
354 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
355 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
357 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
359 #endif /* PROFILING */
361 /* -----------------------------------------------------------------------------
362 Called at the beginning of each heap census
363 -------------------------------------------------------------------------- */
366 stat_startHeapCensus(void)
369 getProcessTimes( &user, &elapsed );
371 HC_start_time = user;
372 HCe_start_time = elapsed;
374 #endif /* PROFILING */
376 /* -----------------------------------------------------------------------------
377 Called at the end of each heap census
378 -------------------------------------------------------------------------- */
381 stat_endHeapCensus(void)
384 getProcessTimes( &user, &elapsed );
386 HC_tot_time += user - HC_start_time;
387 HCe_tot_time += elapsed - HCe_start_time;
389 #endif /* PROFILING */
391 /* -----------------------------------------------------------------------------
392 Called at the end of execution
394 NOTE: number of allocations is not entirely accurate: it doesn't
395 take into account the few bytes at the end of the heap that
396 were left unused when the heap-check failed.
397 -------------------------------------------------------------------------- */
402 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
404 char temp[BIG_STRING_LEN];
407 nat g, total_collections = 0;
409 getProcessTimes( &time, &etime );
410 etime -= ElapsedTimeStart;
412 GC_tot_alloc += alloc;
414 /* Count total garbage collections */
415 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
416 total_collections += generations[g].collections;
418 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
419 if (time == 0.0) time = 1;
420 if (etime == 0.0) etime = 1;
422 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
423 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
424 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
427 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
428 ullong_format_string(GC_tot_alloc*sizeof(W_),
429 temp, rtsTrue/*commas*/);
430 statsPrintf("%11s bytes allocated in the heap\n", temp);
432 ullong_format_string(GC_tot_copied*sizeof(W_),
433 temp, rtsTrue/*commas*/);
434 statsPrintf("%11s bytes copied during GC (scavenged)\n", temp);
436 ullong_format_string(GC_tot_scavd_copied*sizeof(W_),
437 temp, rtsTrue/*commas*/);
438 statsPrintf("%11s bytes copied during GC (not scavenged)\n", temp);
440 if ( ResidencySamples > 0 ) {
441 ullong_format_string(MaxResidency*sizeof(W_),
442 temp, rtsTrue/*commas*/);
443 statsPrintf("%11s bytes maximum residency (%ld sample(s))\n",
444 temp, ResidencySamples);
448 /* Print garbage collections in each gen */
449 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
450 statsPrintf("%11d collections in generation %d (%6.2fs)\n",
451 generations[g].collections, g,
452 TICK_TO_DBL(GC_coll_times[g]));
455 statsPrintf("\n%11ld Mb total memory in use\n\n",
456 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
458 #if defined(THREADED_RTS)
462 for (i = 0, task = all_tasks;
464 i++, task = task->all_link) {
465 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
466 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
468 (task->tso == NULL) ? "(worker)" : "(bound)",
469 TICK_TO_DBL(task->mut_time),
470 TICK_TO_DBL(task->mut_etime),
471 TICK_TO_DBL(task->gc_time),
472 TICK_TO_DBL(task->gc_etime));
477 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
478 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
479 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
480 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
481 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
482 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
484 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
485 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
486 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
487 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
489 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
490 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
491 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
492 TICK_TO_DBL(time), TICK_TO_DBL(etime));
493 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
494 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
495 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
497 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
498 ullong_format_string(0, temp, rtsTrue/*commas*/);
500 ullong_format_string(
501 (ullong)((GC_tot_alloc*sizeof(W_))/
502 TICK_TO_DBL(time - GC_tot_time -
503 PROF_VAL(RP_tot_time + HC_tot_time))),
504 temp, rtsTrue/*commas*/);
506 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
508 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
509 TICK_TO_DBL(time - GC_tot_time -
510 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
512 TICK_TO_DBL(time - GC_tot_time -
513 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
514 / TICK_TO_DBL(etime));
517 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
518 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
519 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
520 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",
522 ResidencySamples == 0 ? 0 :
523 AvgResidency*sizeof(W_)/ResidencySamples,
524 MaxResidency*sizeof(W_),
526 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
527 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
528 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
529 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
537 /* -----------------------------------------------------------------------------
540 Produce some detailed info on the state of the generational GC.
541 -------------------------------------------------------------------------- */
544 statDescribeGens(void)
552 " Gen Steps Max Mutable Step Blocks Live Large\n"
553 " Blocks Closures Objects\n");
556 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
557 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
558 mut += bd->free - bd->start;
561 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
562 generations[g].max_blocks, mut);
564 for (s = 0; s < generations[g].n_steps; s++) {
565 step = &generations[g].steps[s];
567 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
570 live = step->n_large_blocks * BLOCK_SIZE;
572 // This live figure will be slightly less that the "live" figure
573 // given by +RTS -Sstderr, because we take don't count the
574 // slop at the end of each block.
575 for (; bd; bd = bd->link) {
576 live += (bd->free - bd->start) * sizeof(W_);
579 debugBelch("%36s","");
581 debugBelch("%6d %8d %8d %8d\n", s, step->n_blocks,
589 /* -----------------------------------------------------------------------------
590 Stats available via a programmatic interface, so eg. GHCi can time
591 each compilation and expression evaluation.
592 -------------------------------------------------------------------------- */
594 extern HsInt64 getAllocations( void )
595 { return (HsInt64)total_allocated * sizeof(W_); }
597 /* -----------------------------------------------------------------------------
598 Dumping stuff in the stats file, or via the debug message interface
599 -------------------------------------------------------------------------- */
602 statsPrintf( char *s, ... )
604 FILE *sf = RtsFlags.GcFlags.statsFile;
619 FILE *sf = RtsFlags.GcFlags.statsFile;
628 FILE *sf = RtsFlags.GcFlags.statsFile;