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