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"
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 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;
175 /* -----------------------------------------------------------------------------
176 stat_startExit and stat_endExit
178 These two measure the time taken in shutdownHaskell().
179 -------------------------------------------------------------------------- */
186 getProcessTimes(&user, &elapsed);
188 MutElapsedStamp = elapsed;
189 MutElapsedTime = elapsed - GCe_tot_time -
190 PROF_VAL(RPe_tot_time + HCe_tot_time) - InitElapsedStamp;
191 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
193 MutUserTime = user - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
194 if (MutUserTime < 0) { MutUserTime = 0; }
202 getProcessTimes(&user, &elapsed);
204 ExitUserTime = user - MutUserTime - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime;
205 ExitElapsedTime = elapsed - MutElapsedStamp;
206 if (ExitUserTime < 0) {
209 if (ExitElapsedTime < 0) {
214 /* -----------------------------------------------------------------------------
215 Called at the beginning of each GC
216 -------------------------------------------------------------------------- */
218 static nat rub_bell = 0;
220 /* initialise global variables needed during GC
222 * * GC_start_time is read in mut_user_time_during_GC(), which in turn is
223 * needed if either PROFILING or DEBUGing is enabled
228 nat bell = RtsFlags.GcFlags.ringBell;
239 #if defined(PROFILING) || defined(DEBUG)
240 GC_start_time = getProcessCPUTime(); // needed in mut_user_time_during_GC()
243 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
244 #if !defined(PROFILING) && !defined(DEBUG)
245 GC_start_time = getProcessCPUTime();
247 GCe_start_time = getProcessElapsedTime();
248 if (RtsFlags.GcFlags.giveStats) {
249 GC_start_faults = getPageFaults();
254 /* -----------------------------------------------------------------------------
255 Called at the end of each GC
256 -------------------------------------------------------------------------- */
259 stat_endGC (lnat alloc, lnat live, lnat copied,
260 lnat scavd_copied, lnat gen)
262 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
263 Ticks time, etime, gc_time, gc_etime;
265 getProcessTimes(&time, &etime);
266 gc_time = time - GC_start_time;
267 gc_etime = etime - GCe_start_time;
269 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
270 nat faults = getPageFaults();
272 statsPrintf("%9ld %9ld %9ld",
273 alloc*sizeof(W_), (copied+scavd_copied)*sizeof(W_),
275 statsPrintf(" %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
276 TICK_TO_DBL(gc_time),
277 TICK_TO_DBL(gc_etime),
279 TICK_TO_DBL(etime - ElapsedTimeStart),
280 faults - GC_start_faults,
281 GC_start_faults - GC_end_faults,
284 GC_end_faults = faults;
288 GC_coll_times[gen] += gc_time;
290 GC_tot_copied += (ullong) copied;
291 GC_tot_scavd_copied += (ullong) scavd_copied;
292 GC_tot_alloc += (ullong) alloc;
293 GC_tot_time += gc_time;
294 GCe_tot_time += gc_etime;
296 #if defined(THREADED_RTS)
299 if ((task = myTask()) != NULL) {
300 task->gc_time += gc_time;
301 task->gc_etime += gc_etime;
306 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
307 if (live > MaxResidency) {
311 AvgResidency += live;
316 debugBelch("\b\b\b \b\b\b");
321 /* -----------------------------------------------------------------------------
322 Called at the beginning of each Retainer Profiliing
323 -------------------------------------------------------------------------- */
329 getProcessTimes( &user, &elapsed );
331 RP_start_time = user;
332 RPe_start_time = elapsed;
334 #endif /* PROFILING */
336 /* -----------------------------------------------------------------------------
337 Called at the end of each Retainer Profiliing
338 -------------------------------------------------------------------------- */
343 nat retainerGeneration,
344 #ifdef DEBUG_RETAINER
348 double averageNumVisit)
351 getProcessTimes( &user, &elapsed );
353 RP_tot_time += user - RP_start_time;
354 RPe_tot_time += elapsed - RPe_start_time;
356 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
357 retainerGeneration, mut_user_time_during_RP());
358 #ifdef DEBUG_RETAINER
359 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
360 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
362 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
364 #endif /* PROFILING */
366 /* -----------------------------------------------------------------------------
367 Called at the beginning of each heap census
368 -------------------------------------------------------------------------- */
371 stat_startHeapCensus(void)
374 getProcessTimes( &user, &elapsed );
376 HC_start_time = user;
377 HCe_start_time = elapsed;
379 #endif /* PROFILING */
381 /* -----------------------------------------------------------------------------
382 Called at the end of each heap census
383 -------------------------------------------------------------------------- */
386 stat_endHeapCensus(void)
389 getProcessTimes( &user, &elapsed );
391 HC_tot_time += user - HC_start_time;
392 HCe_tot_time += elapsed - HCe_start_time;
394 #endif /* PROFILING */
396 /* -----------------------------------------------------------------------------
397 Called at the end of execution
399 NOTE: number of allocations is not entirely accurate: it doesn't
400 take into account the few bytes at the end of the heap that
401 were left unused when the heap-check failed.
402 -------------------------------------------------------------------------- */
407 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
409 char temp[BIG_STRING_LEN];
412 nat g, total_collections = 0;
414 getProcessTimes( &time, &etime );
415 etime -= ElapsedTimeStart;
417 GC_tot_alloc += alloc;
419 /* Count total garbage collections */
420 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
421 total_collections += generations[g].collections;
423 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
424 if (time == 0.0) time = 1;
425 if (etime == 0.0) etime = 1;
427 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
428 statsPrintf("%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
429 statsPrintf(" %5.2f %5.2f\n\n", 0.0, 0.0);
432 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
433 ullong_format_string(GC_tot_alloc*sizeof(W_),
434 temp, rtsTrue/*commas*/);
435 statsPrintf("%11s bytes allocated in the heap\n", temp);
437 ullong_format_string(GC_tot_copied*sizeof(W_),
438 temp, rtsTrue/*commas*/);
439 statsPrintf("%11s bytes copied during GC (scavenged)\n", temp);
441 ullong_format_string(GC_tot_scavd_copied*sizeof(W_),
442 temp, rtsTrue/*commas*/);
443 statsPrintf("%11s bytes copied during GC (not scavenged)\n", temp);
445 if ( ResidencySamples > 0 ) {
446 ullong_format_string(MaxResidency*sizeof(W_),
447 temp, rtsTrue/*commas*/);
448 statsPrintf("%11s bytes maximum residency (%ld sample(s))\n",
449 temp, ResidencySamples);
453 /* Print garbage collections in each gen */
454 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
455 statsPrintf("%11d collections in generation %d (%6.2fs)\n",
456 generations[g].collections, g,
457 TICK_TO_DBL(GC_coll_times[g]));
460 statsPrintf("\n%11ld Mb total memory in use\n\n",
461 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
463 #if defined(THREADED_RTS)
467 for (i = 0, task = all_tasks;
469 i++, task = task->all_link) {
470 statsPrintf(" Task %2d %-8s : MUT time: %6.2fs (%6.2fs elapsed)\n"
471 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
473 (task->tso == NULL) ? "(worker)" : "(bound)",
474 TICK_TO_DBL(task->mut_time),
475 TICK_TO_DBL(task->mut_etime),
476 TICK_TO_DBL(task->gc_time),
477 TICK_TO_DBL(task->gc_etime));
482 statsPrintf(" INIT time %6.2fs (%6.2fs elapsed)\n",
483 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime));
484 statsPrintf(" MUT time %6.2fs (%6.2fs elapsed)\n",
485 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime));
486 statsPrintf(" GC time %6.2fs (%6.2fs elapsed)\n",
487 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
489 statsPrintf(" RP time %6.2fs (%6.2fs elapsed)\n",
490 TICK_TO_DBL(RP_tot_time), TICK_TO_DBL(RPe_tot_time));
491 statsPrintf(" PROF time %6.2fs (%6.2fs elapsed)\n",
492 TICK_TO_DBL(HC_tot_time), TICK_TO_DBL(HCe_tot_time));
494 statsPrintf(" EXIT time %6.2fs (%6.2fs elapsed)\n",
495 TICK_TO_DBL(ExitUserTime), TICK_TO_DBL(ExitElapsedTime));
496 statsPrintf(" Total time %6.2fs (%6.2fs elapsed)\n\n",
497 TICK_TO_DBL(time), TICK_TO_DBL(etime));
498 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
499 TICK_TO_DBL(GC_tot_time)*100/TICK_TO_DBL(time),
500 TICK_TO_DBL(GCe_tot_time)*100/TICK_TO_DBL(etime));
502 if (time - GC_tot_time - PROF_VAL(RP_tot_time + HC_tot_time) == 0)
503 ullong_format_string(0, temp, rtsTrue/*commas*/);
505 ullong_format_string(
506 (ullong)((GC_tot_alloc*sizeof(W_))/
507 TICK_TO_DBL(time - GC_tot_time -
508 PROF_VAL(RP_tot_time + HC_tot_time))),
509 temp, rtsTrue/*commas*/);
511 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
513 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
514 TICK_TO_DBL(time - GC_tot_time -
515 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
517 TICK_TO_DBL(time - GC_tot_time -
518 PROF_VAL(RP_tot_time + HC_tot_time) - InitUserTime) * 100
519 / TICK_TO_DBL(etime));
522 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
523 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
524 statsPrintf("<<ghc: %llu bytes, ", GC_tot_alloc*(ullong)sizeof(W_));
525 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",
527 ResidencySamples == 0 ? 0 :
528 AvgResidency*sizeof(W_)/ResidencySamples,
529 MaxResidency*sizeof(W_),
531 (unsigned long)(mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
532 TICK_TO_DBL(InitUserTime), TICK_TO_DBL(InitElapsedTime),
533 TICK_TO_DBL(MutUserTime), TICK_TO_DBL(MutElapsedTime),
534 TICK_TO_DBL(GC_tot_time), TICK_TO_DBL(GCe_tot_time));
541 stgFree(GC_coll_times);
542 GC_coll_times = NULL;
545 /* -----------------------------------------------------------------------------
548 Produce some detailed info on the state of the generational GC.
549 -------------------------------------------------------------------------- */
552 statDescribeGens(void)
560 " Gen Steps Max Mut-list Step Blocks Live Large\n"
561 " Blocks Bytes Objects\n");
564 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
565 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
566 mut += (bd->free - bd->start) * sizeof(W_);
569 debugBelch("%8d %8d %8d %9d", g, generations[g].n_steps,
570 generations[g].max_blocks, mut);
572 for (s = 0; s < generations[g].n_steps; s++) {
573 step = &generations[g].steps[s];
575 for (bd = step->large_objects, lge = 0; bd; bd = bd->link) {
578 live = step->n_large_blocks * BLOCK_SIZE;
580 // This live figure will be slightly less that the "live" figure
581 // given by +RTS -Sstderr, because we take don't count the
582 // slop at the end of each block.
583 for (; bd; bd = bd->link) {
584 live += (bd->free - bd->start) * sizeof(W_);
587 debugBelch("%36s","");
589 debugBelch("%6d %8d %8ld %8d\n", s, step->n_blocks,
597 /* -----------------------------------------------------------------------------
598 Stats available via a programmatic interface, so eg. GHCi can time
599 each compilation and expression evaluation.
600 -------------------------------------------------------------------------- */
602 extern HsInt64 getAllocations( void )
603 { return (HsInt64)total_allocated * sizeof(W_); }
605 /* -----------------------------------------------------------------------------
606 Dumping stuff in the stats file, or via the debug message interface
607 -------------------------------------------------------------------------- */
610 statsPrintf( char *s, ... )
612 FILE *sf = RtsFlags.GcFlags.statsFile;
627 FILE *sf = RtsFlags.GcFlags.statsFile;
636 FILE *sf = RtsFlags.GcFlags.statsFile;