5f5fa58ee8658db35eddca69168d949f7fd3d186
[ghc.git] / rts / Stats.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2005
4 *
5 * Statistics and timing-related functions.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11
12 #include "RtsFlags.h"
13 #include "RtsUtils.h"
14 #include "Schedule.h"
15 #include "Stats.h"
16 #include "Profiling.h"
17 #include "GetTime.h"
18 #include "sm/Storage.h"
19 #include "sm/GC.h" // gc_alloc_block_sync, whitehole_spin
20 #include "sm/GCThread.h"
21 #include "sm/BlockAlloc.h"
22
23 #define TimeToSecondsDbl(t) ((double)(t) / TIME_RESOLUTION)
24
25 static Time
26 start_init_cpu, start_init_elapsed,
27 end_init_cpu, end_init_elapsed,
28 start_exit_cpu, start_exit_elapsed,
29 start_exit_gc_elapsed, start_exit_gc_cpu,
30 end_exit_cpu, end_exit_elapsed;
31
32 #ifdef PROFILING
33 static Time RP_start_time = 0, RP_tot_time = 0; // retainer prof user time
34 static Time RPe_start_time = 0, RPe_tot_time = 0; // retainer prof elap time
35
36 static Time HC_start_time, HC_tot_time = 0; // heap census prof user time
37 static Time HCe_start_time, HCe_tot_time = 0; // heap census prof elap time
38 #endif
39
40 #ifdef PROFILING
41 #define PROF_VAL(x) (x)
42 #else
43 #define PROF_VAL(x) 0
44 #endif
45
46 //
47 // All the stats!
48 //
49 // This is where we accumulate all the stats during execution, and it's also
50 // in a convenient form that we can copy over to a caller of getRTSStats().
51 //
52 static RTSStats stats;
53
54 static W_ GC_end_faults = 0;
55
56 static Time *GC_coll_cpu = NULL;
57 static Time *GC_coll_elapsed = NULL;
58 static Time *GC_coll_max_pause = NULL;
59
60 static void statsPrintf( char *s, ... ) GNUC3_ATTRIBUTE(format (PRINTF, 1, 2));
61 static void statsFlush( void );
62 static void statsClose( void );
63
64 /* -----------------------------------------------------------------------------
65 Current elapsed time
66 ------------------------------------------------------------------------- */
67
68 Time stat_getElapsedTime(void)
69 {
70 return getProcessElapsedTime() - start_init_elapsed;
71 }
72
73 /* ---------------------------------------------------------------------------
74 Measure the current MUT time, for profiling
75 ------------------------------------------------------------------------ */
76
77 double
78 mut_user_time_until( Time t )
79 {
80 return TimeToSecondsDbl(t - stats.gc_cpu_ns);
81 // heapCensus() time is included in GC_tot_cpu, so we don't need
82 // to subtract it here.
83 }
84
85 double
86 mut_user_time( void )
87 {
88 Time cpu;
89 cpu = getProcessCPUTime();
90 return mut_user_time_until(cpu);
91 }
92
93 #ifdef PROFILING
94 /*
95 mut_user_time_during_RP() returns the MUT time during retainer profiling.
96 The same is for mut_user_time_during_HC();
97 */
98 static double
99 mut_user_time_during_RP( void )
100 {
101 return TimeToSecondsDbl(RP_start_time - stats.gc_cpu_ns - RP_tot_time);
102 }
103
104 #endif /* PROFILING */
105
106 /* ---------------------------------------------------------------------------
107 initStats0() has no dependencies, it can be called right at the beginning
108 ------------------------------------------------------------------------ */
109
110 void
111 initStats0(void)
112 {
113 start_init_cpu = 0;
114 start_init_elapsed = 0;
115 end_init_cpu = 0;
116 end_init_elapsed = 0;
117
118 start_exit_cpu = 0;
119 start_exit_elapsed = 0;
120 start_exit_gc_cpu = 0;
121 start_exit_gc_elapsed = 0;
122 end_exit_cpu = 0;
123 end_exit_elapsed = 0;
124
125 #ifdef PROFILING
126 RP_start_time = 0;
127 RP_tot_time = 0;
128 RPe_start_time = 0;
129 RPe_tot_time = 0;
130
131 HC_start_time = 0;
132 HC_tot_time = 0;
133 HCe_start_time = 0;
134 HCe_tot_time = 0;
135 #endif
136
137 GC_end_faults = 0;
138
139 stats = (RTSStats) {
140 .gcs = 0,
141 .major_gcs = 0,
142 .allocated_bytes = 0,
143 .max_live_bytes = 0,
144 .max_large_objects_bytes = 0,
145 .max_compact_bytes = 0,
146 .max_slop_bytes = 0,
147 .max_mem_in_use_bytes = 0,
148 .cumulative_live_bytes = 0,
149 .copied_bytes = 0,
150 .par_copied_bytes = 0,
151 .cumulative_par_max_copied_bytes = 0,
152 .mutator_cpu_ns = 0,
153 .mutator_elapsed_ns = 0,
154 .gc_cpu_ns = 0,
155 .gc_elapsed_ns = 0,
156 .cpu_ns = 0,
157 .elapsed_ns = 0,
158 .gc = {
159 .gen = 0,
160 .threads = 0,
161 .allocated_bytes = 0,
162 .live_bytes = 0,
163 .large_objects_bytes = 0,
164 .compact_bytes = 0,
165 .slop_bytes = 0,
166 .mem_in_use_bytes = 0,
167 .copied_bytes = 0,
168 .par_max_copied_bytes = 0,
169 .sync_elapsed_ns = 0,
170 .cpu_ns = 0,
171 .elapsed_ns = 0
172 }
173 };
174 }
175
176 /* ---------------------------------------------------------------------------
177 initStats1() can be called after setupRtsFlags()
178 ------------------------------------------------------------------------ */
179
180 void
181 initStats1 (void)
182 {
183 uint32_t i;
184
185 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
186 statsPrintf(" Alloc Copied Live GC GC TOT TOT Page Flts\n");
187 statsPrintf(" bytes bytes bytes user elap user elap\n");
188 }
189 GC_coll_cpu =
190 (Time *)stgMallocBytes(
191 sizeof(Time)*RtsFlags.GcFlags.generations,
192 "initStats");
193 GC_coll_elapsed =
194 (Time *)stgMallocBytes(
195 sizeof(Time)*RtsFlags.GcFlags.generations,
196 "initStats");
197 GC_coll_max_pause =
198 (Time *)stgMallocBytes(
199 sizeof(Time)*RtsFlags.GcFlags.generations,
200 "initStats");
201 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
202 GC_coll_cpu[i] = 0;
203 GC_coll_elapsed[i] = 0;
204 GC_coll_max_pause[i] = 0;
205 }
206 }
207
208 /* -----------------------------------------------------------------------------
209 Initialisation time...
210 -------------------------------------------------------------------------- */
211
212 void
213 stat_startInit(void)
214 {
215 getProcessTimes(&start_init_cpu, &start_init_elapsed);
216 }
217
218 void
219 stat_endInit(void)
220 {
221 getProcessTimes(&end_init_cpu, &end_init_elapsed);
222 }
223
224 /* -----------------------------------------------------------------------------
225 stat_startExit and stat_endExit
226
227 These two measure the time taken in shutdownHaskell().
228 -------------------------------------------------------------------------- */
229
230 void
231 stat_startExit(void)
232 {
233 getProcessTimes(&start_exit_cpu, &start_exit_elapsed);
234 start_exit_gc_elapsed = stats.gc_elapsed_ns;
235 start_exit_gc_cpu = stats.gc_cpu_ns;
236 }
237
238 void
239 stat_endExit(void)
240 {
241 getProcessTimes(&end_exit_cpu, &end_exit_elapsed);
242 }
243
244 void
245 stat_startGCSync (gc_thread *gct)
246 {
247 gct->gc_sync_start_elapsed = getProcessElapsedTime();
248 }
249
250 /* -----------------------------------------------------------------------------
251 Called at the beginning of each GC
252 -------------------------------------------------------------------------- */
253
254 void
255 stat_startGC (Capability *cap, gc_thread *gct)
256 {
257 if (RtsFlags.GcFlags.ringBell) {
258 debugBelch("\007");
259 }
260
261 getProcessTimes(&gct->gc_start_cpu, &gct->gc_start_elapsed);
262
263 // Post EVENT_GC_START with the same timestamp as used for stats
264 // (though converted from Time=StgInt64 to EventTimestamp=StgWord64).
265 // Here, as opposed to other places, the event is emitted on the cap
266 // that initiates the GC and external tools expect it to have the same
267 // timestamp as used in +RTS -s calculcations.
268 traceEventGcStartAtT(cap,
269 TimeToNS(gct->gc_start_elapsed - start_init_elapsed));
270
271 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS)
272 {
273 gct->gc_start_faults = getPageFaults();
274 }
275
276 updateNurseriesStats();
277 }
278
279 /* -----------------------------------------------------------------------------
280 Called at the end of each GC
281 -------------------------------------------------------------------------- */
282
283 void
284 stat_endGC (Capability *cap, gc_thread *gct,
285 W_ live, W_ copied, W_ slop, uint32_t gen,
286 uint32_t par_n_threads, W_ par_max_copied)
287 {
288 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS ||
289 rtsConfig.gcDoneHook != NULL ||
290 RtsFlags.ProfFlags.doHeapProfile) // heap profiling needs GC_tot_time
291 {
292 // -------------------------------------------------
293 // Collect all the stats about this GC in stats.gc
294
295 stats.gc.gen = gen;
296 stats.gc.threads = par_n_threads;
297
298 uint64_t tot_alloc_bytes = calcTotalAllocated() * sizeof(W_);
299
300 // allocated since the last GC
301 stats.gc.allocated_bytes = tot_alloc_bytes - stats.allocated_bytes;
302
303 stats.gc.live_bytes = live * sizeof(W_);
304 stats.gc.large_objects_bytes = calcTotalLargeObjectsW() * sizeof(W_);
305 stats.gc.compact_bytes = calcTotalCompactW() * sizeof(W_);
306 stats.gc.slop_bytes = slop * sizeof(W_);
307 stats.gc.mem_in_use_bytes = mblocks_allocated * MBLOCK_SIZE;
308 stats.gc.copied_bytes = copied * sizeof(W_);
309 stats.gc.par_max_copied_bytes = par_max_copied * sizeof(W_);
310
311 Time current_cpu, current_elapsed;
312 getProcessTimes(&current_cpu, &current_elapsed);
313 stats.cpu_ns = current_cpu - start_init_cpu;
314 stats.elapsed_ns = current_elapsed - start_init_elapsed;
315
316 stats.gc.sync_elapsed_ns =
317 gct->gc_start_elapsed - gct->gc_sync_start_elapsed;
318 stats.gc.elapsed_ns = current_elapsed - gct->gc_start_elapsed;
319 stats.gc.cpu_ns = current_cpu - gct->gc_start_cpu;
320
321 // -------------------------------------------------
322 // Update the cumulative stats
323
324 stats.gcs++;
325 stats.allocated_bytes = tot_alloc_bytes;
326 stats.max_mem_in_use_bytes = peak_mblocks_allocated * MBLOCK_SIZE;
327
328 GC_coll_cpu[gen] += stats.gc.cpu_ns;
329 GC_coll_elapsed[gen] += stats.gc.elapsed_ns;
330 if (GC_coll_max_pause[gen] < stats.gc.elapsed_ns) {
331 GC_coll_max_pause[gen] = stats.gc.elapsed_ns;
332 }
333
334 stats.copied_bytes += stats.gc.copied_bytes;
335 if (par_n_threads > 1) {
336 stats.par_copied_bytes += stats.gc.copied_bytes;
337 stats.cumulative_par_max_copied_bytes +=
338 stats.gc.par_max_copied_bytes;
339 }
340 stats.gc_cpu_ns += stats.gc.cpu_ns;
341 stats.gc_elapsed_ns += stats.gc.elapsed_ns;
342
343 if (gen == RtsFlags.GcFlags.generations-1) { // major GC?
344 stats.major_gcs++;
345 if (stats.gc.live_bytes > stats.max_live_bytes) {
346 stats.max_live_bytes = stats.gc.live_bytes;
347 }
348 if (stats.gc.large_objects_bytes > stats.max_large_objects_bytes) {
349 stats.max_large_objects_bytes = stats.gc.large_objects_bytes;
350 }
351 if (stats.gc.compact_bytes > stats.max_compact_bytes) {
352 stats.max_compact_bytes = stats.gc.compact_bytes;
353 }
354 if (stats.gc.slop_bytes > stats.max_slop_bytes) {
355 stats.max_slop_bytes = stats.gc.slop_bytes;
356 }
357 stats.cumulative_live_bytes += stats.gc.live_bytes;
358 }
359
360 // -------------------------------------------------
361 // Emit events to the event log
362
363 // Has to be emitted while all caps stopped for GC, but before GC_END.
364 // See trac.haskell.org/ThreadScope/wiki/RTSsummaryEvents
365 // for a detailed design rationale of the current setup
366 // of GC eventlog events.
367 traceEventGcGlobalSync(cap);
368
369 // Emitted before GC_END on all caps, which simplifies tools code.
370 traceEventGcStats(cap,
371 CAPSET_HEAP_DEFAULT,
372 stats.gc.gen,
373 stats.gc.copied_bytes,
374 stats.gc.slop_bytes,
375 /* current loss due to fragmentation */
376 (mblocks_allocated * BLOCKS_PER_MBLOCK - n_alloc_blocks)
377 * BLOCK_SIZE,
378 par_n_threads,
379 stats.gc.par_max_copied_bytes,
380 stats.gc.copied_bytes);
381
382 // Post EVENT_GC_END with the same timestamp as used for stats
383 // (though converted from Time=StgInt64 to EventTimestamp=StgWord64).
384 // Here, as opposed to other places, the event is emitted on the cap
385 // that initiates the GC and external tools expect it to have the same
386 // timestamp as used in +RTS -s calculcations.
387 traceEventGcEndAtT(cap, TimeToNS(stats.elapsed_ns));
388
389 if (gen == RtsFlags.GcFlags.generations-1) { // major GC?
390 traceEventHeapLive(cap,
391 CAPSET_HEAP_DEFAULT,
392 stats.gc.live_bytes);
393 }
394
395 // -------------------------------------------------
396 // Print GC stats to stdout or a file (+RTS -S/-s)
397
398 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
399 W_ faults = getPageFaults();
400
401 statsPrintf("%9" FMT_Word64 " %9" FMT_Word64 " %9" FMT_Word64,
402 stats.gc.allocated_bytes, stats.gc.copied_bytes,
403 stats.gc.live_bytes);
404
405 statsPrintf(" %6.3f %6.3f %8.3f %8.3f %4"
406 FMT_Word " %4" FMT_Word " (Gen: %2d)\n",
407 TimeToSecondsDbl(stats.gc.cpu_ns),
408 TimeToSecondsDbl(stats.gc.elapsed_ns),
409 TimeToSecondsDbl(stats.cpu_ns),
410 TimeToSecondsDbl(stats.elapsed_ns),
411 faults - gct->gc_start_faults,
412 gct->gc_start_faults - GC_end_faults,
413 gen);
414
415 GC_end_faults = faults;
416 statsFlush();
417 }
418
419
420 if (rtsConfig.gcDoneHook != NULL) {
421 rtsConfig.gcDoneHook(&stats.gc);
422 }
423
424 traceEventHeapSize(cap,
425 CAPSET_HEAP_DEFAULT,
426 mblocks_allocated * MBLOCK_SIZE);
427 }
428 }
429
430 /* -----------------------------------------------------------------------------
431 Called at the beginning of each Retainer Profiliing
432 -------------------------------------------------------------------------- */
433 #ifdef PROFILING
434 void
435 stat_startRP(void)
436 {
437 Time user, elapsed;
438 getProcessTimes( &user, &elapsed );
439
440 RP_start_time = user;
441 RPe_start_time = elapsed;
442 }
443 #endif /* PROFILING */
444
445 /* -----------------------------------------------------------------------------
446 Called at the end of each Retainer Profiliing
447 -------------------------------------------------------------------------- */
448
449 #ifdef PROFILING
450 void
451 stat_endRP(
452 uint32_t retainerGeneration,
453 #ifdef DEBUG_RETAINER
454 uint32_t maxCStackSize,
455 int maxStackSize,
456 #endif
457 double averageNumVisit)
458 {
459 Time user, elapsed;
460 getProcessTimes( &user, &elapsed );
461
462 RP_tot_time += user - RP_start_time;
463 RPe_tot_time += elapsed - RPe_start_time;
464
465 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
466 retainerGeneration, mut_user_time_during_RP());
467 #ifdef DEBUG_RETAINER
468 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
469 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
470 #endif
471 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
472 }
473 #endif /* PROFILING */
474
475 /* -----------------------------------------------------------------------------
476 Called at the beginning of each heap census
477 -------------------------------------------------------------------------- */
478 #ifdef PROFILING
479 void
480 stat_startHeapCensus(void)
481 {
482 Time user, elapsed;
483 getProcessTimes( &user, &elapsed );
484
485 HC_start_time = user;
486 HCe_start_time = elapsed;
487 }
488 #endif /* PROFILING */
489
490 /* -----------------------------------------------------------------------------
491 Called at the end of each heap census
492 -------------------------------------------------------------------------- */
493 #ifdef PROFILING
494 void
495 stat_endHeapCensus(void)
496 {
497 Time user, elapsed;
498 getProcessTimes( &user, &elapsed );
499
500 HC_tot_time += user - HC_start_time;
501 HCe_tot_time += elapsed - HCe_start_time;
502 }
503 #endif /* PROFILING */
504
505 /* -----------------------------------------------------------------------------
506 Called at the end of execution
507
508 NOTE: number of allocations is not entirely accurate: it doesn't
509 take into account the few bytes at the end of the heap that
510 were left unused when the heap-check failed.
511 -------------------------------------------------------------------------- */
512
513 #ifdef DEBUG
514 #define TICK_VAR_INI(arity) \
515 StgInt SLOW_CALLS_##arity = 1; \
516 StgInt RIGHT_ARITY_##arity = 1; \
517 StgInt TAGGED_PTR_##arity = 0;
518
519 TICK_VAR_INI(1)
520 TICK_VAR_INI(2)
521
522 StgInt TOTAL_CALLS=1;
523 #endif
524
525 /* Report the value of a counter */
526 #define REPORT(counter) \
527 { \
528 showStgWord64(counter,temp,true/*commas*/); \
529 statsPrintf(" (" #counter ") : %s\n",temp); \
530 }
531
532 /* Report the value of a counter as a percentage of another counter */
533 #define REPORT_PCT(counter,countertot) \
534 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
535 counter*100.0/countertot)
536
537 #define TICK_PRINT(arity) \
538 REPORT(SLOW_CALLS_##arity); \
539 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
540 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
541 REPORT(RIGHT_ARITY_##arity); \
542 REPORT(TAGGED_PTR_##arity)
543
544 #define TICK_PRINT_TOT(arity) \
545 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
546 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
547
548 STATIC_INLINE Time get_init_cpu(void) {
549 return end_init_cpu - start_init_cpu;
550 }
551
552 STATIC_INLINE Time get_init_elapsed(void) {
553 return end_init_elapsed - start_init_elapsed;
554 }
555
556
557 void
558 stat_exit (void)
559 {
560 generation *gen;
561 Time gc_cpu = 0;
562 Time gc_elapsed = 0;
563 Time init_cpu = 0;
564 Time init_elapsed = 0;
565 Time mut_cpu = 0;
566 Time mut_elapsed = 0;
567 Time exit_cpu = 0;
568 Time exit_elapsed = 0;
569 Time exit_gc_cpu = 0;
570 Time exit_gc_elapsed = 0;
571
572 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
573
574 char temp[512];
575 Time tot_cpu;
576 Time tot_elapsed;
577 uint32_t g;
578
579 getProcessTimes( &tot_cpu, &tot_elapsed );
580 tot_cpu -= start_init_cpu;
581 tot_elapsed -= start_init_elapsed;
582
583 uint64_t tot_alloc_bytes = calcTotalAllocated() * sizeof(W_);
584
585 // allocated since the last GC
586 stats.gc.allocated_bytes = tot_alloc_bytes - stats.allocated_bytes;
587 stats.allocated_bytes = tot_alloc_bytes;
588
589 /* avoid divide by zero if tot_cpu is measured as 0.00 seconds -- SDM */
590 if (tot_cpu <= 0) tot_cpu = 1;
591 if (tot_elapsed <= 0) tot_elapsed = 1;
592
593 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
594 statsPrintf("%9" FMT_Word " %9.9s %9.9s",
595 (W_)stats.gc.allocated_bytes, "", "");
596 statsPrintf(" %6.3f %6.3f\n\n", 0.0, 0.0);
597 }
598
599 // heapCensus() is called by the GC, so RP and HC time are
600 // included in the GC stats. We therefore subtract them to
601 // obtain the actual GC cpu time.
602 gc_cpu = stats.gc_cpu_ns - PROF_VAL(RP_tot_time + HC_tot_time);
603 gc_elapsed = stats.gc_elapsed_ns - PROF_VAL(RPe_tot_time + HCe_tot_time);
604
605 init_cpu = get_init_cpu();
606 init_elapsed = get_init_elapsed();
607
608 // We do a GC during the EXIT phase. We'll attribute the cost of that
609 // to GC instead of EXIT, so carefully subtract it from the EXIT time.
610 exit_gc_cpu = stats.gc_cpu_ns - start_exit_gc_cpu;
611 exit_gc_elapsed = stats.gc_elapsed_ns - start_exit_gc_elapsed;
612 exit_cpu = end_exit_cpu - start_exit_cpu - exit_gc_cpu;
613 exit_elapsed = end_exit_elapsed - start_exit_elapsed - exit_gc_elapsed;
614
615 mut_elapsed = start_exit_elapsed - end_init_elapsed -
616 (gc_elapsed - exit_gc_elapsed);
617
618 mut_cpu = start_exit_cpu - end_init_cpu - (gc_cpu - exit_gc_cpu)
619 - PROF_VAL(RP_tot_time + HC_tot_time);
620 if (mut_cpu < 0) { mut_cpu = 0; }
621
622 // The subdivision of runtime into INIT/EXIT/GC/MUT is just adding and
623 // subtracting, so the parts should add up to the total exactly. Note
624 // that tot_elapsed is captured a tiny bit later than end_exit_elapsed,
625 // so we don't use it here.
626 ASSERT(init_elapsed + mut_elapsed + gc_elapsed + exit_elapsed
627 == end_exit_elapsed - start_init_elapsed);
628
629
630 if (RtsFlags.GcFlags.giveStats >= SUMMARY_GC_STATS) {
631 showStgWord64(stats.allocated_bytes, temp, true/*commas*/);
632 statsPrintf("%16s bytes allocated in the heap\n", temp);
633
634 showStgWord64(stats.copied_bytes, temp, true/*commas*/);
635 statsPrintf("%16s bytes copied during GC\n", temp);
636
637 if ( stats.major_gcs > 0 ) {
638 showStgWord64(stats.max_live_bytes, temp, true/*commas*/);
639 statsPrintf("%16s bytes maximum residency (%" FMT_Word32
640 " sample(s))\n",
641 temp, stats.major_gcs);
642 }
643
644 showStgWord64(stats.max_slop_bytes, temp, true/*commas*/);
645 statsPrintf("%16s bytes maximum slop\n", temp);
646
647 statsPrintf("%16" FMT_SizeT " MB total memory in use (%"
648 FMT_SizeT " MB lost due to fragmentation)\n\n",
649 (size_t)(peak_mblocks_allocated * MBLOCK_SIZE_W) / (1024 * 1024 / sizeof(W_)),
650 (size_t)(peak_mblocks_allocated * BLOCKS_PER_MBLOCK * BLOCK_SIZE_W - hw_alloc_blocks * BLOCK_SIZE_W) / (1024 * 1024 / sizeof(W_)));
651
652 /* Print garbage collections in each gen */
653 statsPrintf(" Tot time (elapsed) Avg pause Max pause\n");
654 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
655 gen = &generations[g];
656 statsPrintf(" Gen %2d %5d colls, %5d par %6.3fs %6.3fs %3.4fs %3.4fs\n",
657 gen->no,
658 gen->collections,
659 gen->par_collections,
660 TimeToSecondsDbl(GC_coll_cpu[g]),
661 TimeToSecondsDbl(GC_coll_elapsed[g]),
662 gen->collections == 0 ? 0 : TimeToSecondsDbl(GC_coll_elapsed[g] / gen->collections),
663 TimeToSecondsDbl(GC_coll_max_pause[g]));
664 }
665
666 #if defined(THREADED_RTS)
667 if (RtsFlags.ParFlags.parGcEnabled && n_capabilities > 1) {
668 statsPrintf("\n Parallel GC work balance: %.2f%% (serial 0%%, perfect 100%%)\n",
669 100 * (((double)stats.par_copied_bytes / (double)stats.cumulative_par_max_copied_bytes) - 1)
670 / (n_capabilities - 1)
671 );
672 }
673 #endif
674 statsPrintf("\n");
675
676 #if defined(THREADED_RTS)
677 statsPrintf(" TASKS: %d (%d bound, %d peak workers (%d total), using -N%d)\n",
678 taskCount, taskCount - workerCount,
679 peakWorkerCount, workerCount,
680 n_capabilities);
681
682 statsPrintf("\n");
683
684 {
685 uint32_t i;
686 SparkCounters sparks = { 0, 0, 0, 0, 0, 0};
687 for (i = 0; i < n_capabilities; i++) {
688 sparks.created += capabilities[i]->spark_stats.created;
689 sparks.dud += capabilities[i]->spark_stats.dud;
690 sparks.overflowed+= capabilities[i]->spark_stats.overflowed;
691 sparks.converted += capabilities[i]->spark_stats.converted;
692 sparks.gcd += capabilities[i]->spark_stats.gcd;
693 sparks.fizzled += capabilities[i]->spark_stats.fizzled;
694 }
695
696 statsPrintf(" SPARKS: %" FMT_Word " (%" FMT_Word " converted, %" FMT_Word " overflowed, %" FMT_Word " dud, %" FMT_Word " GC'd, %" FMT_Word " fizzled)\n\n",
697 sparks.created + sparks.dud + sparks.overflowed,
698 sparks.converted, sparks.overflowed, sparks.dud,
699 sparks.gcd, sparks.fizzled);
700 }
701 #endif
702
703 statsPrintf(" INIT time %7.3fs (%7.3fs elapsed)\n",
704 TimeToSecondsDbl(init_cpu), TimeToSecondsDbl(init_elapsed));
705
706 statsPrintf(" MUT time %7.3fs (%7.3fs elapsed)\n",
707 TimeToSecondsDbl(mut_cpu), TimeToSecondsDbl(mut_elapsed));
708 statsPrintf(" GC time %7.3fs (%7.3fs elapsed)\n",
709 TimeToSecondsDbl(gc_cpu), TimeToSecondsDbl(gc_elapsed));
710
711 #ifdef PROFILING
712 statsPrintf(" RP time %7.3fs (%7.3fs elapsed)\n",
713 TimeToSecondsDbl(RP_tot_time), TimeToSecondsDbl(RPe_tot_time));
714 statsPrintf(" PROF time %7.3fs (%7.3fs elapsed)\n",
715 TimeToSecondsDbl(HC_tot_time), TimeToSecondsDbl(HCe_tot_time));
716 #endif
717 statsPrintf(" EXIT time %7.3fs (%7.3fs elapsed)\n",
718 TimeToSecondsDbl(exit_cpu), TimeToSecondsDbl(exit_elapsed));
719 statsPrintf(" Total time %7.3fs (%7.3fs elapsed)\n\n",
720 TimeToSecondsDbl(tot_cpu), TimeToSecondsDbl(tot_elapsed));
721 #ifndef THREADED_RTS
722 statsPrintf(" %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
723 TimeToSecondsDbl(gc_cpu)*100/TimeToSecondsDbl(tot_cpu),
724 TimeToSecondsDbl(gc_elapsed)*100/TimeToSecondsDbl(tot_elapsed));
725 #endif
726
727 if (mut_cpu == 0) {
728 showStgWord64(0, temp, true/*commas*/);
729 } else {
730 showStgWord64(
731 (StgWord64)((double)stats.allocated_bytes /
732 TimeToSecondsDbl(mut_cpu)),
733 temp, true/*commas*/);
734 }
735
736 statsPrintf(" Alloc rate %s bytes per MUT second\n\n", temp);
737
738 statsPrintf(" Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
739 TimeToSecondsDbl(tot_cpu - gc_cpu -
740 PROF_VAL(RP_tot_time + HC_tot_time) - init_cpu) * 100
741 / TimeToSecondsDbl(tot_cpu),
742 TimeToSecondsDbl(tot_elapsed - gc_elapsed -
743 PROF_VAL(RPe_tot_time + HCe_tot_time) - init_elapsed) * 100
744 / TimeToSecondsDbl(tot_elapsed));
745
746 #if defined(THREADED_RTS) && defined(PROF_SPIN)
747 {
748 uint32_t g;
749
750 statsPrintf("gc_alloc_block_sync: %"FMT_Word64"\n", gc_alloc_block_sync.spin);
751 statsPrintf("whitehole_spin: %"FMT_Word64"\n", whitehole_spin);
752 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
753 statsPrintf("gen[%d].sync: %"FMT_Word64"\n", g, generations[g].sync.spin);
754 }
755 }
756 #endif
757 }
758
759 if (RtsFlags.GcFlags.giveStats == ONELINE_GC_STATS) {
760 char *fmt;
761 if (RtsFlags.MiscFlags.machineReadable) {
762 fmt =
763 " [(\"bytes allocated\", \"%" FMT_Word64 "\")\n"
764 " ,(\"num_GCs\", \"%" FMT_Word32 "\")\n"
765 " ,(\"average_bytes_used\", \"%" FMT_Word64 "\")\n"
766 " ,(\"max_bytes_used\", \"%" FMT_Word64 "\")\n"
767 " ,(\"num_byte_usage_samples\", \"%" FMT_Word32 "\")\n"
768 " ,(\"peak_megabytes_allocated\", \"%" FMT_Word64 "\")\n"
769 " ,(\"init_cpu_seconds\", \"%.3f\")\n"
770 " ,(\"init_wall_seconds\", \"%.3f\")\n"
771 " ,(\"mutator_cpu_seconds\", \"%.3f\")\n"
772 " ,(\"mutator_wall_seconds\", \"%.3f\")\n"
773 " ,(\"GC_cpu_seconds\", \"%.3f\")\n"
774 " ,(\"GC_wall_seconds\", \"%.3f\")\n"
775 " ]\n";
776 }
777 else {
778 fmt =
779 "<<ghc: %" FMT_Word64 " bytes, "
780 "%" FMT_Word32 " GCs, "
781 "%" FMT_Word64 "/%" FMT_Word64 " avg/max bytes residency (%" FMT_Word32 " samples), "
782 "%" FMT_Word64 "M in use, "
783 "%.3f INIT (%.3f elapsed), "
784 "%.3f MUT (%.3f elapsed), "
785 "%.3f GC (%.3f elapsed) :ghc>>\n";
786 }
787 /* print the long long separately to avoid bugginess on mingwin (2001-07-02, mingw-0.5) */
788 statsPrintf(fmt,
789 stats.allocated_bytes,
790 stats.gcs,
791 (uint64_t)
792 (stats.major_gcs == 0 ? 0 :
793 stats.cumulative_live_bytes/stats.major_gcs),
794 stats.max_live_bytes,
795 stats.major_gcs,
796 (uint64_t) (peak_mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L)),
797 TimeToSecondsDbl(init_cpu), TimeToSecondsDbl(init_elapsed),
798 TimeToSecondsDbl(mut_cpu), TimeToSecondsDbl(mut_elapsed),
799 TimeToSecondsDbl(gc_cpu), TimeToSecondsDbl(gc_elapsed));
800 }
801
802 statsFlush();
803 statsClose();
804 }
805
806 if (GC_coll_cpu) {
807 stgFree(GC_coll_cpu);
808 GC_coll_cpu = NULL;
809 }
810 if (GC_coll_elapsed) {
811 stgFree(GC_coll_elapsed);
812 GC_coll_elapsed = NULL;
813 }
814 if (GC_coll_max_pause) {
815 stgFree(GC_coll_max_pause);
816 GC_coll_max_pause = NULL;
817 }
818 }
819
820 /* -----------------------------------------------------------------------------
821 stat_describe_gens
822
823 Produce some detailed info on the state of the generational GC.
824 -------------------------------------------------------------------------- */
825 void
826 statDescribeGens(void)
827 {
828 uint32_t g, mut, lge, compacts, i;
829 W_ gen_slop;
830 W_ tot_live, tot_slop;
831 W_ gen_live, gen_blocks;
832 bdescr *bd;
833 generation *gen;
834
835 debugBelch(
836 "----------------------------------------------------------------------\n"
837 " Gen Max Mut-list Blocks Large Compacts Live Slop\n"
838 " Blocks Bytes Objects \n"
839 "----------------------------------------------------------------------\n");
840
841 tot_live = 0;
842 tot_slop = 0;
843
844 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
845 gen = &generations[g];
846
847 for (bd = gen->large_objects, lge = 0; bd; bd = bd->link) {
848 lge++;
849 }
850
851 for (bd = gen->compact_objects, compacts = 0; bd; bd = bd->link) {
852 compacts++;
853 }
854
855 gen_live = genLiveWords(gen);
856 gen_blocks = genLiveBlocks(gen);
857
858 mut = 0;
859 for (i = 0; i < n_capabilities; i++) {
860 mut += countOccupied(capabilities[i]->mut_lists[g]);
861
862 // Add the pinned object block.
863 bd = capabilities[i]->pinned_object_block;
864 if (bd != NULL) {
865 gen_live += bd->free - bd->start;
866 gen_blocks += bd->blocks;
867 }
868
869 gen_live += gcThreadLiveWords(i,g);
870 gen_blocks += gcThreadLiveBlocks(i,g);
871 }
872
873 debugBelch("%5d %7" FMT_Word " %9d", g, (W_)gen->max_blocks, mut);
874
875 gen_slop = gen_blocks * BLOCK_SIZE_W - gen_live;
876
877 debugBelch("%8" FMT_Word " %8d %8d %9" FMT_Word " %9" FMT_Word "\n", gen_blocks, lge, compacts,
878 gen_live*(W_)sizeof(W_), gen_slop*(W_)sizeof(W_));
879 tot_live += gen_live;
880 tot_slop += gen_slop;
881 }
882 debugBelch("----------------------------------------------------------------------\n");
883 debugBelch("%51s%9" FMT_Word " %9" FMT_Word "\n",
884 "",tot_live*sizeof(W_),tot_slop*sizeof(W_));
885 debugBelch("----------------------------------------------------------------------\n");
886 debugBelch("\n");
887 }
888
889 /* -----------------------------------------------------------------------------
890 Stats available via a programmatic interface, so eg. GHCi can time
891 each compilation and expression evaluation.
892 -------------------------------------------------------------------------- */
893
894 uint64_t getAllocations( void )
895 {
896 return stats.allocated_bytes;
897 }
898
899 int getRTSStatsEnabled( void )
900 {
901 return RtsFlags.GcFlags.giveStats != NO_GC_STATS;
902 }
903
904 void getRTSStats( RTSStats *s )
905 {
906 Time current_elapsed = 0;
907 Time current_cpu = 0;
908
909 *s = stats;
910
911 getProcessTimes(&current_cpu, &current_elapsed);
912 s->cpu_ns = current_cpu - end_init_cpu;
913 s->elapsed_ns = current_elapsed - end_init_elapsed;
914
915 s->mutator_cpu_ns = current_cpu - end_init_cpu - stats.gc_cpu_ns -
916 PROF_VAL(RP_tot_time + HC_tot_time);
917 s->mutator_elapsed_ns = current_elapsed - end_init_elapsed -
918 stats.gc_elapsed_ns;
919 }
920
921 /* -----------------------------------------------------------------------------
922 Dumping stuff in the stats file, or via the debug message interface
923 -------------------------------------------------------------------------- */
924
925 void
926 statsPrintf( char *s, ... )
927 {
928 FILE *sf = RtsFlags.GcFlags.statsFile;
929 va_list ap;
930
931 va_start(ap,s);
932 if (sf == NULL) {
933 vdebugBelch(s,ap);
934 } else {
935 vfprintf(sf, s, ap);
936 }
937 va_end(ap);
938 }
939
940 static void
941 statsFlush( void )
942 {
943 FILE *sf = RtsFlags.GcFlags.statsFile;
944 if (sf != NULL) {
945 fflush(sf);
946 }
947 }
948
949 static void
950 statsClose( void )
951 {
952 FILE *sf = RtsFlags.GcFlags.statsFile;
953 if (sf != NULL) {
954 fclose(sf);
955 }
956 }