Use snwprintf instead of swprintf in rts/Linker.c.
[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 W_ current_residency = 0; // in words; for stats only
61 static W_ max_residency = 0;
62 static W_ cumulative_residency = 0;
63 static W_ residency_samples = 0; // for stats only
64 static W_ current_slop = 0;
65 static W_ max_slop = 0;
66
67 static W_ 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 (Capability *cap, 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
282 // Post EVENT_GC_START with the same timestamp as used for stats
283 // (though converted from Time=StgInt64 to EventTimestamp=StgWord64).
284 // Here, as opposed to other places, the event is emitted on the cap
285 // that initiates the GC and external tools expect it to have the same
286 // timestamp as used in +RTS -s calculcations.
287 traceEventGcStartAtT(cap,
288 TimeToNS(gct->gc_start_elapsed - start_init_elapsed));
289
290 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS)
291 {
292 gct->gc_start_faults = getPageFaults();
293 }
294 }
295
296 /* -----------------------------------------------------------------------------
297 * Calculate the total allocated memory since the start of the
298 * program. Also emits events reporting the per-cap allocation
299 * totals.
300 * -------------------------------------------------------------------------- */
301
302 static StgWord
303 calcTotalAllocated(void)
304 {
305 W_ tot_alloc = 0;
306 W_ n;
307 for (n = 0; n < n_capabilities; n++) {
308 tot_alloc += capabilities[n]->total_allocated;
309 traceEventHeapAllocated(capabilities[n],
310 CAPSET_HEAP_DEFAULT,
311 capabilities[n]->total_allocated * sizeof(W_));
312 }
313
314 return tot_alloc;
315 }
316
317 /* -----------------------------------------------------------------------------
318 Called at the end of each GC
319 -------------------------------------------------------------------------- */
320
321 void
322 stat_endGC (Capability *cap, gc_thread *gct,
323 W_ live, W_ copied, W_ slop, nat gen,
324 nat par_n_threads, W_ par_max_copied, W_ par_tot_copied)
325 {
326 W_ tot_alloc;
327 W_ alloc;
328
329 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS ||
330 RtsFlags.ProfFlags.doHeapProfile)
331 // heap profiling needs GC_tot_time
332 {
333 Time cpu, elapsed, gc_cpu, gc_elapsed;
334
335 // Has to be emitted while all caps stopped for GC, but before GC_END.
336 // See trac.haskell.org/ThreadScope/wiki/RTSsummaryEvents
337 // for a detailed design rationale of the current setup
338 // of GC eventlog events.
339 traceEventGcGlobalSync(cap);
340
341 // Emitted before GC_END on all caps, which simplifies tools code.
342 traceEventGcStats(cap,
343 CAPSET_HEAP_DEFAULT,
344 gen,
345 copied * sizeof(W_),
346 slop * sizeof(W_),
347 /* current loss due to fragmentation */
348 (mblocks_allocated * BLOCKS_PER_MBLOCK - n_alloc_blocks)
349 * BLOCK_SIZE_W * sizeof(W_),
350 par_n_threads,
351 par_max_copied * sizeof(W_),
352 par_tot_copied * sizeof(W_));
353
354 getProcessTimes(&cpu, &elapsed);
355
356 // Post EVENT_GC_END with the same timestamp as used for stats
357 // (though converted from Time=StgInt64 to EventTimestamp=StgWord64).
358 // Here, as opposed to other places, the event is emitted on the cap
359 // that initiates the GC and external tools expect it to have the same
360 // timestamp as used in +RTS -s calculcations.
361 traceEventGcEndAtT(cap, TimeToNS(elapsed - start_init_elapsed));
362
363 gc_elapsed = elapsed - gct->gc_start_elapsed;
364 gc_cpu = cpu - gct->gc_start_cpu;
365
366 /* For the moment we calculate both per-HEC and total allocation.
367 * There is thus redundancy here, but for the moment we will calculate
368 * it both the old and new way and assert they're the same.
369 * When we're sure it's working OK then we can simplify things.
370 */
371 tot_alloc = calcTotalAllocated();
372
373 // allocated since the last GC
374 alloc = tot_alloc - GC_tot_alloc;
375 GC_tot_alloc = tot_alloc;
376
377 if (RtsFlags.GcFlags.giveStats == VERBOSE_GC_STATS) {
378 W_ faults = getPageFaults();
379
380 statsPrintf("%9" FMT_SizeT " %9" FMT_SizeT " %9" FMT_SizeT,
381 alloc*sizeof(W_), copied*sizeof(W_),
382 live*sizeof(W_));
383 statsPrintf(" %6.3f %6.3f %8.3f %8.3f %4" FMT_Word " %4" FMT_Word " (Gen: %2d)\n",
384 TimeToSecondsDbl(gc_cpu),
385 TimeToSecondsDbl(gc_elapsed),
386 TimeToSecondsDbl(cpu),
387 TimeToSecondsDbl(elapsed - start_init_elapsed),
388 faults - gct->gc_start_faults,
389 gct->gc_start_faults - GC_end_faults,
390 gen);
391
392 GC_end_faults = faults;
393 statsFlush();
394 }
395
396 GC_coll_cpu[gen] += gc_cpu;
397 GC_coll_elapsed[gen] += gc_elapsed;
398 if (GC_coll_max_pause[gen] < gc_elapsed) {
399 GC_coll_max_pause[gen] = gc_elapsed;
400 }
401
402 GC_tot_copied += (StgWord64) copied;
403 GC_par_max_copied += (StgWord64) par_max_copied;
404 GC_par_tot_copied += (StgWord64) par_tot_copied;
405 GC_tot_cpu += gc_cpu;
406
407 traceEventHeapSize(cap,
408 CAPSET_HEAP_DEFAULT,
409 mblocks_allocated * MBLOCK_SIZE_W * sizeof(W_));
410
411 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
412 if (live > max_residency) {
413 max_residency = live;
414 }
415 current_residency = live;
416 residency_samples++;
417 cumulative_residency += live;
418 traceEventHeapLive(cap,
419 CAPSET_HEAP_DEFAULT,
420 live * sizeof(W_));
421 }
422
423 if (slop > max_slop) max_slop = slop;
424 }
425
426 if (rub_bell) {
427 debugBelch("\b\b\b \b\b\b");
428 rub_bell = 0;
429 }
430
431 #if USE_PAPI
432 if(papi_is_reporting) {
433 /* Switch to counting mutator events */
434 if (gen == 0) {
435 papi_stop_gc0_count();
436 } else {
437 papi_stop_gc1_count();
438 }
439 papi_start_mutator_count();
440 }
441 #endif
442 }
443
444 /* -----------------------------------------------------------------------------
445 Called at the beginning of each Retainer Profiliing
446 -------------------------------------------------------------------------- */
447 #ifdef PROFILING
448 void
449 stat_startRP(void)
450 {
451 Time user, elapsed;
452 getProcessTimes( &user, &elapsed );
453
454 RP_start_time = user;
455 RPe_start_time = elapsed;
456 }
457 #endif /* PROFILING */
458
459 /* -----------------------------------------------------------------------------
460 Called at the end of each Retainer Profiliing
461 -------------------------------------------------------------------------- */
462
463 #ifdef PROFILING
464 void
465 stat_endRP(
466 nat retainerGeneration,
467 #ifdef DEBUG_RETAINER
468 nat maxCStackSize,
469 int maxStackSize,
470 #endif
471 double averageNumVisit)
472 {
473 Time user, elapsed;
474 getProcessTimes( &user, &elapsed );
475
476 RP_tot_time += user - RP_start_time;
477 RPe_tot_time += elapsed - RPe_start_time;
478
479 fprintf(prof_file, "Retainer Profiling: %d, at %f seconds\n",
480 retainerGeneration, mut_user_time_during_RP());
481 #ifdef DEBUG_RETAINER
482 fprintf(prof_file, "\tMax C stack size = %u\n", maxCStackSize);
483 fprintf(prof_file, "\tMax auxiliary stack size = %u\n", maxStackSize);
484 #endif
485 fprintf(prof_file, "\tAverage number of visits per object = %f\n", averageNumVisit);
486 }
487 #endif /* PROFILING */
488
489 /* -----------------------------------------------------------------------------
490 Called at the beginning of each heap census
491 -------------------------------------------------------------------------- */
492 #ifdef PROFILING
493 void
494 stat_startHeapCensus(void)
495 {
496 Time user, elapsed;
497 getProcessTimes( &user, &elapsed );
498
499 HC_start_time = user;
500 HCe_start_time = elapsed;
501 }
502 #endif /* PROFILING */
503
504 /* -----------------------------------------------------------------------------
505 Called at the end of each heap census
506 -------------------------------------------------------------------------- */
507 #ifdef PROFILING
508 void
509 stat_endHeapCensus(void)
510 {
511 Time user, elapsed;
512 getProcessTimes( &user, &elapsed );
513
514 HC_tot_time += user - HC_start_time;
515 HCe_tot_time += elapsed - HCe_start_time;
516 }
517 #endif /* PROFILING */
518
519 /* -----------------------------------------------------------------------------
520 Called at the end of execution
521
522 NOTE: number of allocations is not entirely accurate: it doesn't
523 take into account the few bytes at the end of the heap that
524 were left unused when the heap-check failed.
525 -------------------------------------------------------------------------- */
526
527 #ifdef DEBUG
528 #define TICK_VAR_INI(arity) \
529 StgInt SLOW_CALLS_##arity = 1; \
530 StgInt RIGHT_ARITY_##arity = 1; \
531 StgInt TAGGED_PTR_##arity = 0;
532
533 TICK_VAR_INI(1)
534 TICK_VAR_INI(2)
535
536 StgInt TOTAL_CALLS=1;
537 #endif
538
539 /* Report the value of a counter */
540 #define REPORT(counter) \
541 { \
542 showStgWord64(counter,temp,rtsTrue/*commas*/); \
543 statsPrintf(" (" #counter ") : %s\n",temp); \
544 }
545
546 /* Report the value of a counter as a percentage of another counter */
547 #define REPORT_PCT(counter,countertot) \
548 statsPrintf(" (" #counter ") %% of (" #countertot ") : %.1f%%\n", \
549 counter*100.0/countertot)
550
551 #define TICK_PRINT(arity) \
552 REPORT(SLOW_CALLS_##arity); \
553 REPORT_PCT(RIGHT_ARITY_##arity,SLOW_CALLS_##arity); \
554 REPORT_PCT(TAGGED_PTR_##arity,RIGHT_ARITY_##arity); \
555 REPORT(RIGHT_ARITY_##arity); \
556 REPORT(TAGGED_PTR_##arity)
557
558 #define TICK_PRINT_TOT(arity) \
559 statsPrintf(" (SLOW_CALLS_" #arity ") %% of (TOTAL_CALLS) : %.1f%%\n", \
560 SLOW_CALLS_##arity * 100.0/TOTAL_CALLS)
561
562 static inline Time get_init_cpu(void) { return end_init_cpu - start_init_cpu; }
563 static inline Time get_init_elapsed(void) { return end_init_elapsed - start_init_elapsed; }
564
565
566 void
567 stat_exit (void)
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 W_ tot_alloc;
579 W_ alloc;
580
581 if (RtsFlags.GcFlags.giveStats != NO_GC_STATS) {
582
583 char temp[BIG_STRING_LEN];
584 Time tot_cpu;
585 Time tot_elapsed;
586 nat i, g, total_collections = 0;
587
588 getProcessTimes( &tot_cpu, &tot_elapsed );
589 tot_elapsed -= start_init_elapsed;
590
591 tot_alloc = calcTotalAllocated();
592
593 // allocated since the last GC
594 alloc = tot_alloc - GC_tot_alloc;
595 GC_tot_alloc = tot_alloc;
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("%9" FMT_SizeT " %9.9s %9.9s", (W_)alloc*sizeof(W_), "", "");
607 statsPrintf(" %6.3f %6.3f\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 (%" FMT_Word " 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("%16" FMT_SizeT " MB total memory in use (%" FMT_SizeT " MB lost due to fragmentation)\n\n",
653 (size_t)(peak_mblocks_allocated * MBLOCK_SIZE_W) / (1024 * 1024 / sizeof(W_)),
654 (size_t)(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 %6.3fs %6.3fs %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: %" FMT_Word " (%" FMT_Word " converted, %" FMT_Word " overflowed, %" FMT_Word " dud, %" FMT_Word " GC'd, %" FMT_Word " 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 %7.3fs (%7.3fs elapsed)\n",
708 TimeToSecondsDbl(init_cpu), TimeToSecondsDbl(init_elapsed));
709
710 statsPrintf(" MUT time %7.3fs (%7.3fs elapsed)\n",
711 TimeToSecondsDbl(mut_cpu), TimeToSecondsDbl(mut_elapsed));
712 statsPrintf(" GC time %7.3fs (%7.3fs elapsed)\n",
713 TimeToSecondsDbl(gc_cpu), TimeToSecondsDbl(gc_elapsed));
714
715 #ifdef PROFILING
716 statsPrintf(" RP time %7.3fs (%7.3fs elapsed)\n",
717 TimeToSecondsDbl(RP_tot_time), TimeToSecondsDbl(RPe_tot_time));
718 statsPrintf(" PROF time %7.3fs (%7.3fs elapsed)\n",
719 TimeToSecondsDbl(HC_tot_time), TimeToSecondsDbl(HCe_tot_time));
720 #endif
721 statsPrintf(" EXIT time %7.3fs (%7.3fs elapsed)\n",
722 TimeToSecondsDbl(exit_cpu), TimeToSecondsDbl(exit_elapsed));
723 statsPrintf(" Total time %7.3fs (%7.3fs 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\", \"%.3f\")\n"
783 " ,(\"init_wall_seconds\", \"%.3f\")\n"
784 " ,(\"mutator_cpu_seconds\", \"%.3f\")\n"
785 " ,(\"mutator_wall_seconds\", \"%.3f\")\n"
786 " ,(\"GC_cpu_seconds\", \"%.3f\")\n"
787 " ,(\"GC_wall_seconds\", \"%.3f\")\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, %.3f INIT (%.3f elapsed), %.3f MUT (%.3f elapsed), %.3f GC (%.3f 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 W_ gen_slop;
836 W_ tot_live, tot_slop;
837 W_ 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 %7" FMT_Word " %9d", g, (W_)gen->max_blocks, mut);
876
877 gen_slop = gen_blocks * BLOCK_SIZE_W - gen_live;
878
879 debugBelch("%8" FMT_Word " %8d %8" FMT_Word " %8" FMT_Word "\n", gen_blocks, lge,
880 gen_live*(W_)sizeof(W_), gen_slop*(W_)sizeof(W_));
881 tot_live += gen_live;
882 tot_slop += gen_slop;
883 }
884 debugBelch("----------------------------------------------------------\n");
885 debugBelch("%41s%8" FMT_SizeT " %8" FMT_SizeT "\n",
886 "",tot_live*sizeof(W_),tot_slop*sizeof(W_));
887 debugBelch("----------------------------------------------------------\n");
888 debugBelch("\n");
889 }
890
891 /* -----------------------------------------------------------------------------
892 Stats available via a programmatic interface, so eg. GHCi can time
893 each compilation and expression evaluation.
894 -------------------------------------------------------------------------- */
895
896 extern HsInt64 getAllocations( void )
897 { return (HsInt64)GC_tot_alloc * sizeof(W_); }
898
899 /* EZY: I'm not convinced I got all the casting right. */
900
901 extern rtsBool getGCStatsEnabled( void )
902 {
903 return RtsFlags.GcFlags.giveStats != NO_GC_STATS;
904 }
905
906 extern void getGCStats( GCStats *s )
907 {
908 nat total_collections = 0;
909 nat g;
910 Time gc_cpu = 0;
911 Time gc_elapsed = 0;
912 Time current_elapsed = 0;
913 Time current_cpu = 0;
914
915 getProcessTimes(&current_cpu, &current_elapsed);
916
917 /* EZY: static inline'ify these */
918 for (g = 0; g < RtsFlags.GcFlags.generations; g++)
919 total_collections += generations[g].collections;
920
921 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
922 gc_cpu += GC_coll_cpu[g];
923 gc_elapsed += GC_coll_elapsed[g];
924 }
925
926 s->bytes_allocated = GC_tot_alloc*(StgWord64)sizeof(W_);
927 s->num_gcs = total_collections;
928 s->num_byte_usage_samples = residency_samples;
929 s->max_bytes_used = max_residency*sizeof(W_);
930 s->cumulative_bytes_used = cumulative_residency*(StgWord64)sizeof(W_);
931 s->peak_megabytes_allocated = (StgWord64)(peak_mblocks_allocated * MBLOCK_SIZE / (1024L * 1024L));
932 s->bytes_copied = GC_tot_copied*(StgWord64)sizeof(W_);
933 s->max_bytes_slop = max_slop*(StgWord64)sizeof(W_);
934 s->current_bytes_used = current_residency*(StgWord64)sizeof(W_);
935 s->current_bytes_slop = current_slop*(StgWord64)sizeof(W_);
936 /*
937 s->init_cpu_seconds = TimeToSecondsDbl(get_init_cpu());
938 s->init_wall_seconds = TimeToSecondsDbl(get_init_elapsed());
939 */
940 s->mutator_cpu_seconds = TimeToSecondsDbl(current_cpu - end_init_cpu - gc_cpu - PROF_VAL(RP_tot_time + HC_tot_time));
941 s->mutator_wall_seconds = TimeToSecondsDbl(current_elapsed- end_init_elapsed - gc_elapsed);
942 s->gc_cpu_seconds = TimeToSecondsDbl(gc_cpu);
943 s->gc_wall_seconds = TimeToSecondsDbl(gc_elapsed);
944 /* EZY: Being consistent with incremental output, but maybe should also discount init */
945 s->cpu_seconds = TimeToSecondsDbl(current_cpu);
946 s->wall_seconds = TimeToSecondsDbl(current_elapsed - end_init_elapsed);
947 s->par_tot_bytes_copied = GC_par_tot_copied*(StgWord64)sizeof(W_);
948 s->par_max_bytes_copied = GC_par_max_copied*(StgWord64)sizeof(W_);
949 }
950 // extern void getTaskStats( TaskStats **s ) {}
951 #if 0
952 extern void getSparkStats( SparkCounters *s ) {
953 nat i;
954 s->created = 0;
955 s->dud = 0;
956 s->overflowed = 0;
957 s->converted = 0;
958 s->gcd = 0;
959 s->fizzled = 0;
960 for (i = 0; i < n_capabilities; i++) {
961 s->created += capabilities[i]->spark_stats.created;
962 s->dud += capabilities[i]->spark_stats.dud;
963 s->overflowed+= capabilities[i]->spark_stats.overflowed;
964 s->converted += capabilities[i]->spark_stats.converted;
965 s->gcd += capabilities[i]->spark_stats.gcd;
966 s->fizzled += capabilities[i]->spark_stats.fizzled;
967 }
968 }
969 #endif
970
971 /* -----------------------------------------------------------------------------
972 Dumping stuff in the stats file, or via the debug message interface
973 -------------------------------------------------------------------------- */
974
975 void
976 statsPrintf( char *s, ... )
977 {
978 FILE *sf = RtsFlags.GcFlags.statsFile;
979 va_list ap;
980
981 va_start(ap,s);
982 if (sf == NULL) {
983 vdebugBelch(s,ap);
984 } else {
985 vfprintf(sf, s, ap);
986 }
987 va_end(ap);
988 }
989
990 static void
991 statsFlush( void )
992 {
993 FILE *sf = RtsFlags.GcFlags.statsFile;
994 if (sf != NULL) {
995 fflush(sf);
996 }
997 }
998
999 static void
1000 statsClose( void )
1001 {
1002 FILE *sf = RtsFlags.GcFlags.statsFile;
1003 if (sf != NULL) {
1004 fclose(sf);
1005 }
1006 }