remove redundant condition checking in profiling RTS code
[ghc.git] / rts / Profiling.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2000
4 *
5 * Support for profiling
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifdef PROFILING
10
11 #include "PosixSource.h"
12 #include "Rts.h"
13
14 #include "RtsUtils.h"
15 #include "Profiling.h"
16 #include "Proftimer.h"
17 #include "ProfHeap.h"
18 #include "Arena.h"
19 #include "RetainerProfile.h"
20 #include "Printer.h"
21 #include "Capability.h"
22
23 #include <string.h>
24
25 #ifdef DEBUG
26 #include "Trace.h"
27 #endif
28
29 /*
30 * Profiling allocation arena.
31 */
32 Arena *prof_arena;
33
34 /*
35 * Global variables used to assign unique IDs to cc's, ccs's, and
36 * closure_cats
37 */
38
39 unsigned int CC_ID = 1;
40 unsigned int CCS_ID = 1;
41
42 /* figures for the profiling report.
43 */
44 static StgWord64 total_alloc;
45 static W_ total_prof_ticks;
46
47 /* Globals for opening the profiling log file(s)
48 */
49 static char *prof_filename; /* prof report file name = <program>.prof */
50 FILE *prof_file;
51
52 static char *hp_filename; /* heap profile (hp2ps style) log file */
53 FILE *hp_file;
54
55 /* Linked lists to keep track of CCs and CCSs that haven't
56 * been declared in the log file yet
57 */
58 CostCentre *CC_LIST = NULL;
59 CostCentreStack *CCS_LIST = NULL;
60
61 #ifdef THREADED_RTS
62 Mutex ccs_mutex;
63 #endif
64
65 /*
66 * Built-in cost centres and cost-centre stacks:
67 *
68 * MAIN is the root of the cost-centre stack tree. If there are
69 * no {-# SCC #-}s in the program, all costs will be attributed
70 * to MAIN.
71 *
72 * SYSTEM is the RTS in general (scheduler, etc.). All costs for
73 * RTS operations apart from garbage collection are attributed
74 * to SYSTEM.
75 *
76 * GC is the storage manager / garbage collector.
77 *
78 * OVERHEAD gets all costs generated by the profiling system
79 * itself. These are costs that would not be incurred
80 * during non-profiled execution of the program.
81 *
82 * DONT_CARE is a placeholder cost-centre we assign to static
83 * constructors. It should *never* accumulate any costs.
84 *
85 * PINNED accumulates memory allocated to pinned objects, which
86 * cannot be profiled separately because we cannot reliably
87 * traverse pinned memory.
88 */
89
90 CC_DECLARE(CC_MAIN, "MAIN", "MAIN", "<built-in>", CC_NOT_CAF, );
91 CC_DECLARE(CC_SYSTEM, "SYSTEM", "SYSTEM", "<built-in>", CC_NOT_CAF, );
92 CC_DECLARE(CC_GC, "GC", "GC", "<built-in>", CC_NOT_CAF, );
93 CC_DECLARE(CC_OVERHEAD, "OVERHEAD_of", "PROFILING", "<built-in>", CC_NOT_CAF, );
94 CC_DECLARE(CC_DONT_CARE, "DONT_CARE", "MAIN", "<built-in>", CC_NOT_CAF, );
95 CC_DECLARE(CC_PINNED, "PINNED", "SYSTEM", "<built-in>", CC_NOT_CAF, );
96 CC_DECLARE(CC_IDLE, "IDLE", "IDLE", "<built-in>", CC_NOT_CAF, );
97
98 CCS_DECLARE(CCS_MAIN, CC_MAIN, );
99 CCS_DECLARE(CCS_SYSTEM, CC_SYSTEM, );
100 CCS_DECLARE(CCS_GC, CC_GC, );
101 CCS_DECLARE(CCS_OVERHEAD, CC_OVERHEAD, );
102 CCS_DECLARE(CCS_DONT_CARE, CC_DONT_CARE, );
103 CCS_DECLARE(CCS_PINNED, CC_PINNED, );
104 CCS_DECLARE(CCS_IDLE, CC_IDLE, );
105
106 /*
107 * Static Functions
108 */
109
110 static CostCentreStack * appendCCS ( CostCentreStack *ccs1,
111 CostCentreStack *ccs2 );
112 static CostCentreStack * actualPush_ ( CostCentreStack *ccs, CostCentre *cc,
113 CostCentreStack *new_ccs );
114 static rtsBool ignoreCCS ( CostCentreStack *ccs );
115 static void countTickss ( CostCentreStack *ccs );
116 static void inheritCosts ( CostCentreStack *ccs );
117 static void findCCSMaxLens ( CostCentreStack *ccs,
118 nat indent,
119 nat *max_label_len,
120 nat *max_module_len );
121 static void logCCS ( CostCentreStack *ccs,
122 nat indent,
123 nat max_label_len,
124 nat max_module_len );
125 static void reportCCS ( CostCentreStack *ccs );
126 static CostCentreStack * checkLoop ( CostCentreStack *ccs,
127 CostCentre *cc );
128 static CostCentreStack * pruneCCSTree ( CostCentreStack *ccs );
129 static CostCentreStack * actualPush ( CostCentreStack *, CostCentre * );
130 static CostCentreStack * isInIndexTable ( IndexTable *, CostCentre * );
131 static IndexTable * addToIndexTable ( IndexTable *, CostCentreStack *,
132 CostCentre *, unsigned int );
133 static void ccsSetSelected ( CostCentreStack *ccs );
134
135 static void initTimeProfiling ( void );
136 static void initProfilingLogFile ( void );
137
138 /* -----------------------------------------------------------------------------
139 Initialise the profiling environment
140 -------------------------------------------------------------------------- */
141
142 void
143 initProfiling1 (void)
144 {
145 // initialise our arena
146 prof_arena = newArena();
147
148 /* for the benefit of allocate()... */
149 {
150 nat n;
151 for (n=0; n < n_capabilities; n++) {
152 capabilities[n]->r.rCCCS = CCS_SYSTEM;
153 }
154 }
155
156 #ifdef THREADED_RTS
157 initMutex(&ccs_mutex);
158 #endif
159 }
160
161 void
162 freeProfiling (void)
163 {
164 arenaFree(prof_arena);
165 }
166
167 void
168 initProfiling2 (void)
169 {
170 CostCentreStack *ccs, *next;
171
172 /* Set up the log file, and dump the header and cost centre
173 * information into it.
174 */
175 initProfilingLogFile();
176
177 /* Register all the cost centres / stacks in the program
178 * CC_MAIN gets link = 0, all others have non-zero link.
179 */
180 REGISTER_CC(CC_MAIN);
181 REGISTER_CC(CC_SYSTEM);
182 REGISTER_CC(CC_GC);
183 REGISTER_CC(CC_OVERHEAD);
184 REGISTER_CC(CC_DONT_CARE);
185 REGISTER_CC(CC_PINNED);
186 REGISTER_CC(CC_IDLE);
187
188 REGISTER_CCS(CCS_SYSTEM);
189 REGISTER_CCS(CCS_GC);
190 REGISTER_CCS(CCS_OVERHEAD);
191 REGISTER_CCS(CCS_DONT_CARE);
192 REGISTER_CCS(CCS_PINNED);
193 REGISTER_CCS(CCS_IDLE);
194 REGISTER_CCS(CCS_MAIN);
195
196 /* find all the registered cost centre stacks, and make them
197 * children of CCS_MAIN.
198 */
199 ASSERT(CCS_LIST == CCS_MAIN);
200 CCS_LIST = CCS_LIST->prevStack;
201 CCS_MAIN->prevStack = NULL;
202 CCS_MAIN->root = CCS_MAIN;
203 ccsSetSelected(CCS_MAIN);
204
205 // make CCS_MAIN the parent of all the pre-defined CCSs.
206 for (ccs = CCS_LIST; ccs != NULL; ) {
207 next = ccs->prevStack;
208 ccs->prevStack = NULL;
209 actualPush_(CCS_MAIN,ccs->cc,ccs);
210 ccs->root = ccs;
211 ccs = next;
212 }
213
214 if (RtsFlags.CcFlags.doCostCentres) {
215 initTimeProfiling();
216 }
217
218 if (RtsFlags.ProfFlags.doHeapProfile) {
219 initHeapProfiling();
220 }
221 }
222
223
224 static void
225 initProfilingLogFile(void)
226 {
227 char *prog;
228
229 prog = arenaAlloc(prof_arena, strlen(prog_name) + 1);
230 strcpy(prog, prog_name);
231 #ifdef mingw32_HOST_OS
232 // on Windows, drop the .exe suffix if there is one
233 {
234 char *suff;
235 suff = strrchr(prog,'.');
236 if (suff != NULL && !strcmp(suff,".exe")) {
237 *suff = '\0';
238 }
239 }
240 #endif
241
242 if (RtsFlags.CcFlags.doCostCentres == 0 &&
243 RtsFlags.ProfFlags.doHeapProfile != HEAP_BY_RETAINER &&
244 RtsFlags.ProfFlags.retainerSelector == NULL)
245 {
246 /* No need for the <prog>.prof file */
247 prof_filename = NULL;
248 prof_file = NULL;
249 }
250 else
251 {
252 /* Initialise the log file name */
253 prof_filename = arenaAlloc(prof_arena, strlen(prog) + 6);
254 sprintf(prof_filename, "%s.prof", prog);
255
256 /* open the log file */
257 if ((prof_file = fopen(prof_filename, "w")) == NULL) {
258 debugBelch("Can't open profiling report file %s\n", prof_filename);
259 RtsFlags.CcFlags.doCostCentres = 0;
260 // The following line was added by Sung; retainer/LDV profiling may need
261 // two output files, i.e., <program>.prof/hp.
262 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER)
263 RtsFlags.ProfFlags.doHeapProfile = 0;
264 return;
265 }
266 }
267
268 if (RtsFlags.ProfFlags.doHeapProfile) {
269 /* Initialise the log file name */
270 hp_filename = arenaAlloc(prof_arena, strlen(prog) + 6);
271 sprintf(hp_filename, "%s.hp", prog);
272
273 /* open the log file */
274 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
275 debugBelch("Can't open profiling report file %s\n",
276 hp_filename);
277 RtsFlags.ProfFlags.doHeapProfile = 0;
278 return;
279 }
280 }
281 }
282
283 void
284 initTimeProfiling(void)
285 {
286 /* Start ticking */
287 startProfTimer();
288 };
289
290 void
291 endProfiling ( void )
292 {
293 if (RtsFlags.CcFlags.doCostCentres) {
294 stopProfTimer();
295 }
296 if (RtsFlags.ProfFlags.doHeapProfile) {
297 endHeapProfiling();
298 }
299 }
300
301 /* -----------------------------------------------------------------------------
302 Set CCCS when entering a function.
303
304 The algorithm is as follows.
305
306 ccs ++> ccsfn = ccs ++ dropCommonPrefix ccs ccsfn
307
308 where
309
310 dropCommonPrefix A B
311 -- returns the suffix of B after removing any prefix common
312 -- to both A and B.
313
314 e.g.
315
316 <a,b,c> ++> <> = <a,b,c>
317 <a,b,c> ++> <d> = <a,b,c,d>
318 <a,b,c> ++> <a,b> = <a,b,c>
319 <a,b> ++> <a,b,c> = <a,b,c>
320 <a,b,c> ++> <a,b,d> = <a,b,c,d>
321
322 -------------------------------------------------------------------------- */
323
324 // implements c1 ++> c2, where c1 and c2 are equal depth
325 //
326 static CostCentreStack *
327 enterFunEqualStacks (CostCentreStack *ccs0,
328 CostCentreStack *ccsapp,
329 CostCentreStack *ccsfn)
330 {
331 ASSERT(ccsapp->depth == ccsfn->depth);
332 if (ccsapp == ccsfn) return ccs0;
333 return pushCostCentre(enterFunEqualStacks(ccs0,
334 ccsapp->prevStack,
335 ccsfn->prevStack),
336 ccsfn->cc);
337 }
338
339 // implements c1 ++> c2, where c2 is deeper than c1.
340 // Drop elements of c2 until we have equal stacks, call
341 // enterFunEqualStacks(), and then push on the elements that we
342 // dropped in reverse order.
343 //
344 static CostCentreStack *
345 enterFunCurShorter (CostCentreStack *ccsapp, CostCentreStack *ccsfn, StgWord n)
346 {
347 if (n == 0) {
348 ASSERT(ccsfn->depth == ccsapp->depth);
349 return enterFunEqualStacks(ccsapp,ccsapp,ccsfn);;
350 } else {
351 ASSERT(ccsfn->depth > ccsapp->depth);
352 return pushCostCentre(enterFunCurShorter(ccsapp, ccsfn->prevStack, n-1),
353 ccsfn->cc);
354 }
355 }
356
357 void enterFunCCS (StgRegTable *reg, CostCentreStack *ccsfn)
358 {
359 CostCentreStack *ccsapp;
360
361 // common case 1: both stacks are the same
362 if (ccsfn == reg->rCCCS) {
363 return;
364 }
365
366 // common case 2: the function stack is empty, or just CAF
367 if (ccsfn->prevStack == CCS_MAIN) {
368 return;
369 }
370
371 ccsapp = reg->rCCCS;
372 reg->rCCCS = CCS_OVERHEAD;
373
374 // common case 3: the stacks are completely different (e.g. one is a
375 // descendent of MAIN and the other of a CAF): we append the whole
376 // of the function stack to the current CCS.
377 if (ccsfn->root != ccsapp->root) {
378 reg->rCCCS = appendCCS(ccsapp,ccsfn);
379 return;
380 }
381
382 // uncommon case 4: ccsapp is deeper than ccsfn
383 if (ccsapp->depth > ccsfn->depth) {
384 nat i, n;
385 CostCentreStack *tmp = ccsapp;
386 n = ccsapp->depth - ccsfn->depth;
387 for (i = 0; i < n; i++) {
388 tmp = tmp->prevStack;
389 }
390 reg->rCCCS = enterFunEqualStacks(ccsapp,tmp,ccsfn);
391 return;
392 }
393
394 // uncommon case 5: ccsfn is deeper than CCCS
395 if (ccsfn->depth > ccsapp->depth) {
396 reg->rCCCS = enterFunCurShorter(ccsapp, ccsfn,
397 ccsfn->depth - ccsapp->depth);
398 return;
399 }
400
401 // uncommon case 6: stacks are equal depth, but different
402 reg->rCCCS = enterFunEqualStacks(ccsapp,ccsapp,ccsfn);
403 }
404
405 /* -----------------------------------------------------------------------------
406 Decide whether closures with this CCS should contribute to the heap
407 profile.
408 -------------------------------------------------------------------------- */
409
410 static void
411 ccsSetSelected (CostCentreStack *ccs)
412 {
413 if (RtsFlags.ProfFlags.modSelector) {
414 if (! strMatchesSelector (ccs->cc->module,
415 RtsFlags.ProfFlags.modSelector) ) {
416 ccs->selected = 0;
417 return;
418 }
419 }
420 if (RtsFlags.ProfFlags.ccSelector) {
421 if (! strMatchesSelector (ccs->cc->label,
422 RtsFlags.ProfFlags.ccSelector) ) {
423 ccs->selected = 0;
424 return;
425 }
426 }
427 if (RtsFlags.ProfFlags.ccsSelector) {
428 CostCentreStack *c;
429 for (c = ccs; c != NULL; c = c->prevStack)
430 {
431 if ( strMatchesSelector (c->cc->label,
432 RtsFlags.ProfFlags.ccsSelector) ) {
433 break;
434 }
435 }
436 if (c == NULL) {
437 ccs->selected = 0;
438 return;
439 }
440 }
441
442 ccs->selected = 1;
443 return;
444 }
445
446 /* -----------------------------------------------------------------------------
447 Cost-centre stack manipulation
448 -------------------------------------------------------------------------- */
449
450 #ifdef DEBUG
451 CostCentreStack * _pushCostCentre ( CostCentreStack *ccs, CostCentre *cc );
452 CostCentreStack *
453 pushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
454 #define pushCostCentre _pushCostCentre
455 {
456 IF_DEBUG(prof,
457 traceBegin("pushing %s on ", cc->label);
458 debugCCS(ccs);
459 traceEnd(););
460
461 return pushCostCentre(ccs,cc);
462 }
463 #endif
464
465 /* Append ccs1 to ccs2 (ignoring any CAF cost centre at the root of ccs1 */
466
467 #ifdef DEBUG
468 CostCentreStack *_appendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
469 CostCentreStack *
470 appendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
471 #define appendCCS _appendCCS
472 {
473 IF_DEBUG(prof,
474 if (ccs1 != ccs2) {
475 debugBelch("Appending ");
476 debugCCS(ccs1);
477 debugBelch(" to ");
478 debugCCS(ccs2);
479 debugBelch("\n");});
480 return appendCCS(ccs1,ccs2);
481 }
482 #endif
483
484 CostCentreStack *
485 appendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
486 {
487 if (ccs1 == ccs2) {
488 return ccs1;
489 }
490
491 if (ccs2 == CCS_MAIN || ccs2->cc->is_caf == CC_IS_CAF) {
492 // stop at a CAF element
493 return ccs1;
494 }
495
496 return pushCostCentre(appendCCS(ccs1, ccs2->prevStack), ccs2->cc);
497 }
498
499 // Pick one:
500 // #define RECURSION_DROPS
501 #define RECURSION_TRUNCATES
502
503 CostCentreStack *
504 pushCostCentre (CostCentreStack *ccs, CostCentre *cc)
505 {
506 CostCentreStack *temp_ccs, *ret;
507 IndexTable *ixtable;
508
509 if (ccs == EMPTY_STACK) {
510 ACQUIRE_LOCK(&ccs_mutex);
511 ret = actualPush(ccs,cc);
512 }
513 else
514 {
515 if (ccs->cc == cc) {
516 return ccs;
517 } else {
518 // check if we've already memoized this stack
519 ixtable = ccs->indexTable;
520 temp_ccs = isInIndexTable(ixtable,cc);
521
522 if (temp_ccs != EMPTY_STACK) {
523 return temp_ccs;
524 } else {
525
526 // not in the IndexTable, now we take the lock:
527 ACQUIRE_LOCK(&ccs_mutex);
528
529 if (ccs->indexTable != ixtable)
530 {
531 // someone modified ccs->indexTable while
532 // we did not hold the lock, so we must
533 // check it again:
534 temp_ccs = isInIndexTable(ixtable,cc);
535 if (temp_ccs != EMPTY_STACK)
536 {
537 RELEASE_LOCK(&ccs_mutex);
538 return temp_ccs;
539 }
540 }
541 temp_ccs = checkLoop(ccs,cc);
542 if (temp_ccs != NULL) {
543 // This CC is already in the stack somewhere.
544 // This could be recursion, or just calling
545 // another function with the same CC.
546 // A number of policies are possible at this
547 // point, we implement two here:
548 // - truncate the stack to the previous instance
549 // of this CC
550 // - ignore this push, return the same stack.
551 //
552 CostCentreStack *new_ccs;
553 #if defined(RECURSION_TRUNCATES)
554 new_ccs = temp_ccs;
555 #else // defined(RECURSION_DROPS)
556 new_ccs = ccs;
557 #endif
558 ccs->indexTable = addToIndexTable (ccs->indexTable,
559 new_ccs, cc, 1);
560 ret = new_ccs;
561 } else {
562 ret = actualPush (ccs,cc);
563 }
564 }
565 }
566 }
567
568 RELEASE_LOCK(&ccs_mutex);
569 return ret;
570 }
571
572 static CostCentreStack *
573 checkLoop (CostCentreStack *ccs, CostCentre *cc)
574 {
575 while (ccs != EMPTY_STACK) {
576 if (ccs->cc == cc)
577 return ccs;
578 ccs = ccs->prevStack;
579 }
580 return NULL;
581 }
582
583 static CostCentreStack *
584 actualPush (CostCentreStack *ccs, CostCentre *cc)
585 {
586 CostCentreStack *new_ccs;
587
588 // allocate space for a new CostCentreStack
589 new_ccs = (CostCentreStack *) arenaAlloc(prof_arena, sizeof(CostCentreStack));
590
591 return actualPush_(ccs, cc, new_ccs);
592 }
593
594 static CostCentreStack *
595 actualPush_ (CostCentreStack *ccs, CostCentre *cc, CostCentreStack *new_ccs)
596 {
597 /* assign values to each member of the structure */
598 new_ccs->ccsID = CCS_ID++;
599 new_ccs->cc = cc;
600 new_ccs->prevStack = ccs;
601 new_ccs->root = ccs->root;
602 new_ccs->depth = ccs->depth + 1;
603
604 new_ccs->indexTable = EMPTY_TABLE;
605
606 /* Initialise the various _scc_ counters to zero
607 */
608 new_ccs->scc_count = 0;
609
610 /* Initialize all other stats here. There should be a quick way
611 * that's easily used elsewhere too
612 */
613 new_ccs->time_ticks = 0;
614 new_ccs->mem_alloc = 0;
615 new_ccs->inherited_ticks = 0;
616 new_ccs->inherited_alloc = 0;
617
618 // Set the selected field.
619 ccsSetSelected(new_ccs);
620
621 /* update the memoization table for the parent stack */
622 ccs->indexTable = addToIndexTable(ccs->indexTable, new_ccs, cc,
623 0/*not a back edge*/);
624
625 /* return a pointer to the new stack */
626 return new_ccs;
627 }
628
629
630 static CostCentreStack *
631 isInIndexTable(IndexTable *it, CostCentre *cc)
632 {
633 while (it!=EMPTY_TABLE)
634 {
635 if (it->cc == cc)
636 return it->ccs;
637 else
638 it = it->next;
639 }
640
641 /* otherwise we never found it so return EMPTY_TABLE */
642 return EMPTY_TABLE;
643 }
644
645
646 static IndexTable *
647 addToIndexTable (IndexTable *it, CostCentreStack *new_ccs,
648 CostCentre *cc, unsigned int back_edge)
649 {
650 IndexTable *new_it;
651
652 new_it = arenaAlloc(prof_arena, sizeof(IndexTable));
653
654 new_it->cc = cc;
655 new_it->ccs = new_ccs;
656 new_it->next = it;
657 new_it->back_edge = back_edge;
658 return new_it;
659 }
660
661 /* -----------------------------------------------------------------------------
662 Generating a time & allocation profiling report.
663 -------------------------------------------------------------------------- */
664
665 /* We omit certain system-related CCs and CCSs from the default
666 * reports, so as not to cause confusion.
667 */
668 static rtsBool
669 ignoreCC (CostCentre *cc)
670 {
671 if (RtsFlags.CcFlags.doCostCentres < COST_CENTRES_ALL &&
672 ( cc == CC_OVERHEAD
673 || cc == CC_DONT_CARE
674 || cc == CC_GC
675 || cc == CC_SYSTEM
676 || cc == CC_IDLE)) {
677 return rtsTrue;
678 } else {
679 return rtsFalse;
680 }
681 }
682
683 static rtsBool
684 ignoreCCS (CostCentreStack *ccs)
685 {
686 if (RtsFlags.CcFlags.doCostCentres < COST_CENTRES_ALL &&
687 ( ccs == CCS_OVERHEAD
688 || ccs == CCS_DONT_CARE
689 || ccs == CCS_GC
690 || ccs == CCS_SYSTEM
691 || ccs == CCS_IDLE)) {
692 return rtsTrue;
693 } else {
694 return rtsFalse;
695 }
696 }
697
698 /* -----------------------------------------------------------------------------
699 Generating the aggregated per-cost-centre time/alloc report.
700 -------------------------------------------------------------------------- */
701
702 static CostCentre *sorted_cc_list;
703
704 static void
705 aggregateCCCosts( CostCentreStack *ccs )
706 {
707 IndexTable *i;
708
709 ccs->cc->mem_alloc += ccs->mem_alloc;
710 ccs->cc->time_ticks += ccs->time_ticks;
711
712 for (i = ccs->indexTable; i != 0; i = i->next) {
713 if (!i->back_edge) {
714 aggregateCCCosts(i->ccs);
715 }
716 }
717 }
718
719 static void
720 insertCCInSortedList( CostCentre *new_cc )
721 {
722 CostCentre **prev, *cc;
723
724 prev = &sorted_cc_list;
725 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
726 if (new_cc->time_ticks > cc->time_ticks) {
727 new_cc->link = cc;
728 *prev = new_cc;
729 return;
730 } else {
731 prev = &(cc->link);
732 }
733 }
734 new_cc->link = NULL;
735 *prev = new_cc;
736 }
737
738 static nat
739 strlen_utf8 (char *s)
740 {
741 nat n = 0;
742 unsigned char c;
743
744 for (; *s != '\0'; s++) {
745 c = *s;
746 if (c < 0x80 || c > 0xBF) n++;
747 }
748 return n;
749 }
750
751 static void
752 reportPerCCCosts( void )
753 {
754 CostCentre *cc, *next;
755 nat max_label_len, max_module_len;
756
757 aggregateCCCosts(CCS_MAIN);
758 sorted_cc_list = NULL;
759
760 max_label_len = 11; // no shorter than the "COST CENTRE" header
761 max_module_len = 7; // no shorter than the "MODULE" header
762
763 for (cc = CC_LIST; cc != NULL; cc = next) {
764 next = cc->link;
765 if (cc->time_ticks > total_prof_ticks/100
766 || cc->mem_alloc > total_alloc/100
767 || RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL) {
768 insertCCInSortedList(cc);
769
770 max_label_len = stg_max(strlen_utf8(cc->label), max_label_len);
771 max_module_len = stg_max(strlen_utf8(cc->module), max_module_len);
772 }
773 }
774
775 fprintf(prof_file, "%-*s %-*s", max_label_len, "COST CENTRE", max_module_len, "MODULE");
776 fprintf(prof_file, "%6s %6s", "%time", "%alloc");
777 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
778 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
779 }
780 fprintf(prof_file, "\n\n");
781
782 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
783 if (ignoreCC(cc)) {
784 continue;
785 }
786 fprintf(prof_file, "%s%*s %s%*s",
787 cc->label,
788 max_label_len - strlen_utf8(cc->label), "",
789 cc->module,
790 max_module_len - strlen_utf8(cc->module), "");
791
792 fprintf(prof_file, "%6.1f %6.1f",
793 total_prof_ticks == 0 ? 0.0 : (cc->time_ticks / (StgFloat) total_prof_ticks * 100),
794 total_alloc == 0 ? 0.0 : (cc->mem_alloc / (StgFloat)
795 total_alloc * 100)
796 );
797
798 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
799 fprintf(prof_file, " %5" FMT_Word64 " %9" FMT_Word64,
800 (StgWord64)(cc->time_ticks), cc->mem_alloc*sizeof(W_));
801 }
802 fprintf(prof_file, "\n");
803 }
804
805 fprintf(prof_file,"\n\n");
806 }
807
808 /* -----------------------------------------------------------------------------
809 Generate the cost-centre-stack time/alloc report
810 -------------------------------------------------------------------------- */
811
812 static void
813 fprintHeader( nat max_label_len, nat max_module_len )
814 {
815 fprintf(prof_file, "%-*s %-*s%6s %11s %11s %11s\n", max_label_len, "", max_module_len, "", "", "", "individual", "inherited");
816
817 fprintf(prof_file, "%-*s %-*s", max_label_len, "COST CENTRE", max_module_len, "MODULE");
818 fprintf(prof_file, "%6s %11s %5s %5s %5s %5s", "no.", "entries", "%time", "%alloc", "%time", "%alloc");
819
820 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
821 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
822 }
823
824 fprintf(prof_file, "\n\n");
825 }
826
827 void
828 reportCCSProfiling( void )
829 {
830 nat count;
831 char temp[128]; /* sigh: magic constant */
832
833 stopProfTimer();
834
835 total_prof_ticks = 0;
836 total_alloc = 0;
837 countTickss(CCS_MAIN);
838
839 if (RtsFlags.CcFlags.doCostCentres == 0) return;
840
841 fprintf(prof_file, "\t%s Time and Allocation Profiling Report (%s)\n",
842 time_str(), "Final");
843
844 fprintf(prof_file, "\n\t ");
845 fprintf(prof_file, " %s", prog_name);
846 fprintf(prof_file, " +RTS");
847 for (count = 0; rts_argv[count]; count++)
848 fprintf(prof_file, " %s", rts_argv[count]);
849 fprintf(prof_file, " -RTS");
850 for (count = 1; prog_argv[count]; count++)
851 fprintf(prof_file, " %s", prog_argv[count]);
852 fprintf(prof_file, "\n\n");
853
854 fprintf(prof_file, "\ttotal time = %11.2f secs (%lu ticks @ %d us, %d processor%s)\n",
855 ((double) total_prof_ticks *
856 (double) RtsFlags.MiscFlags.tickInterval) / (TIME_RESOLUTION * n_capabilities),
857 (unsigned long) total_prof_ticks,
858 (int) TimeToUS(RtsFlags.MiscFlags.tickInterval),
859 n_capabilities, n_capabilities > 1 ? "s" : "");
860
861 fprintf(prof_file, "\ttotal alloc = %11s bytes",
862 showStgWord64(total_alloc * sizeof(W_),
863 temp, rtsTrue/*commas*/));
864
865 fprintf(prof_file, " (excludes profiling overheads)\n\n");
866
867 reportPerCCCosts();
868
869 inheritCosts(CCS_MAIN);
870
871 reportCCS(pruneCCSTree(CCS_MAIN));
872 }
873
874 static void
875 findCCSMaxLens(CostCentreStack *ccs, nat indent, nat *max_label_len, nat *max_module_len) {
876 CostCentre *cc;
877 IndexTable *i;
878
879 cc = ccs->cc;
880
881 *max_label_len = stg_max(*max_label_len, indent + strlen_utf8(cc->label));
882 *max_module_len = stg_max(*max_module_len, strlen_utf8(cc->module));
883
884 for (i = ccs->indexTable; i != 0; i = i->next) {
885 if (!i->back_edge) {
886 findCCSMaxLens(i->ccs, indent+1, max_label_len, max_module_len);
887 }
888 }
889 }
890
891 static void
892 logCCS(CostCentreStack *ccs, nat indent, nat max_label_len, nat max_module_len)
893 {
894 CostCentre *cc;
895 IndexTable *i;
896
897 cc = ccs->cc;
898
899 /* Only print cost centres with non 0 data ! */
900
901 if (!ignoreCCS(ccs))
902 /* force printing of *all* cost centres if -Pa */
903 {
904
905 fprintf(prof_file, "%-*s%s%*s %s%*s",
906 indent, "",
907 cc->label,
908 max_label_len-indent - strlen_utf8(cc->label), "",
909 cc->module,
910 max_module_len - strlen_utf8(cc->module), "");
911
912 fprintf(prof_file, "%6ld %11" FMT_Word64 " %5.1f %5.1f %5.1f %5.1f",
913 ccs->ccsID, ccs->scc_count,
914 total_prof_ticks == 0 ? 0.0 : ((double)ccs->time_ticks / (double)total_prof_ticks * 100.0),
915 total_alloc == 0 ? 0.0 : ((double)ccs->mem_alloc / (double)total_alloc * 100.0),
916 total_prof_ticks == 0 ? 0.0 : ((double)ccs->inherited_ticks / (double)total_prof_ticks * 100.0),
917 total_alloc == 0 ? 0.0 : ((double)ccs->inherited_alloc / (double)total_alloc * 100.0)
918 );
919
920 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
921 fprintf(prof_file, " %5" FMT_Word64 " %9" FMT_Word64,
922 (StgWord64)(ccs->time_ticks), ccs->mem_alloc*sizeof(W_));
923 }
924 fprintf(prof_file, "\n");
925 }
926
927 for (i = ccs->indexTable; i != 0; i = i->next) {
928 if (!i->back_edge) {
929 logCCS(i->ccs, indent+1, max_label_len, max_module_len);
930 }
931 }
932 }
933
934 static void
935 reportCCS(CostCentreStack *ccs)
936 {
937 nat max_label_len, max_module_len;
938
939 max_label_len = 11; // no shorter than "COST CENTRE" header
940 max_module_len = 7; // no shorter than "MODULE" header
941
942 findCCSMaxLens(ccs, 0, &max_label_len, &max_module_len);
943
944 fprintHeader(max_label_len, max_module_len);
945 logCCS(ccs, 0, max_label_len, max_module_len);
946 }
947
948
949 /* Traverse the cost centre stack tree and accumulate
950 * ticks/allocations.
951 */
952 static void
953 countTickss(CostCentreStack *ccs)
954 {
955 IndexTable *i;
956
957 if (!ignoreCCS(ccs)) {
958 total_alloc += ccs->mem_alloc;
959 total_prof_ticks += ccs->time_ticks;
960 }
961 for (i = ccs->indexTable; i != NULL; i = i->next)
962 if (!i->back_edge) {
963 countTickss(i->ccs);
964 }
965 }
966
967 /* Traverse the cost centre stack tree and inherit ticks & allocs.
968 */
969 static void
970 inheritCosts(CostCentreStack *ccs)
971 {
972 IndexTable *i;
973
974 if (ignoreCCS(ccs)) { return; }
975
976 ccs->inherited_ticks += ccs->time_ticks;
977 ccs->inherited_alloc += ccs->mem_alloc;
978
979 for (i = ccs->indexTable; i != NULL; i = i->next)
980 if (!i->back_edge) {
981 inheritCosts(i->ccs);
982 ccs->inherited_ticks += i->ccs->inherited_ticks;
983 ccs->inherited_alloc += i->ccs->inherited_alloc;
984 }
985
986 return;
987 }
988
989 //
990 // Prune CCSs with zero entries, zero ticks or zero allocation from
991 // the tree, unless COST_CENTRES_ALL is on.
992 //
993 static CostCentreStack *
994 pruneCCSTree (CostCentreStack *ccs)
995 {
996 CostCentreStack *ccs1;
997 IndexTable *i, **prev;
998
999 prev = &ccs->indexTable;
1000 for (i = ccs->indexTable; i != 0; i = i->next) {
1001 if (i->back_edge) { continue; }
1002
1003 ccs1 = pruneCCSTree(i->ccs);
1004 if (ccs1 == NULL) {
1005 *prev = i->next;
1006 } else {
1007 prev = &(i->next);
1008 }
1009 }
1010
1011 if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
1012 /* force printing of *all* cost centres if -P -P */ )
1013
1014 || ( ccs->indexTable != 0 )
1015 || ( ccs->scc_count || ccs->time_ticks || ccs->mem_alloc )
1016 ) {
1017 return ccs;
1018 } else {
1019 return NULL;
1020 }
1021 }
1022
1023 void
1024 fprintCCS( FILE *f, CostCentreStack *ccs )
1025 {
1026 fprintf(f,"<");
1027 for (; ccs && ccs != CCS_MAIN; ccs = ccs->prevStack ) {
1028 fprintf(f,"%s.%s", ccs->cc->module, ccs->cc->label);
1029 if (ccs->prevStack && ccs->prevStack != CCS_MAIN) {
1030 fprintf(f,",");
1031 }
1032 }
1033 fprintf(f,">");
1034 }
1035
1036 // Returns: True if the call stack ended with CAF
1037 static rtsBool fprintCallStack (CostCentreStack *ccs)
1038 {
1039 CostCentreStack *prev;
1040
1041 fprintf(stderr,"%s.%s", ccs->cc->module, ccs->cc->label);
1042 prev = ccs->prevStack;
1043 while (prev && prev != CCS_MAIN) {
1044 ccs = prev;
1045 fprintf(stderr, ",\n called from %s.%s",
1046 ccs->cc->module, ccs->cc->label);
1047 prev = ccs->prevStack;
1048 }
1049 fprintf(stderr, "\n");
1050
1051 return (!strncmp(ccs->cc->label, "CAF", 3));
1052 }
1053
1054 /* For calling from .cmm code, where we can't reliably refer to stderr */
1055 void
1056 fprintCCS_stderr (CostCentreStack *ccs, StgClosure *exception, StgTSO *tso)
1057 {
1058 rtsBool is_caf;
1059 StgPtr frame;
1060 StgStack *stack;
1061 CostCentreStack *prev_ccs;
1062 nat depth = 0;
1063 const nat MAX_DEPTH = 10; // don't print gigantic chains of stacks
1064
1065 {
1066 char *desc;
1067 StgInfoTable *info;
1068 info = get_itbl(UNTAG_CLOSURE(exception));
1069 switch (info->type) {
1070 case CONSTR:
1071 case CONSTR_1_0:
1072 case CONSTR_0_1:
1073 case CONSTR_2_0:
1074 case CONSTR_1_1:
1075 case CONSTR_0_2:
1076 case CONSTR_STATIC:
1077 case CONSTR_NOCAF_STATIC:
1078 desc = GET_CON_DESC(itbl_to_con_itbl(info));
1079 break;
1080 default:
1081 desc = closure_type_names[info->type];
1082 break;
1083 }
1084 fprintf(stderr, "*** Exception (reporting due to +RTS -xc): (%s), stack trace: \n ", desc);
1085 }
1086
1087 is_caf = fprintCallStack(ccs);
1088
1089 // traverse the stack down to the enclosing update frame to
1090 // find out where this CCS was evaluated from...
1091
1092 stack = tso->stackobj;
1093 frame = stack->sp;
1094 prev_ccs = ccs;
1095
1096 for (; is_caf && depth < MAX_DEPTH; depth++)
1097 {
1098 switch (get_itbl((StgClosure*)frame)->type)
1099 {
1100 case UPDATE_FRAME:
1101 ccs = ((StgUpdateFrame*)frame)->header.prof.ccs;
1102 frame += sizeofW(StgUpdateFrame);
1103 if (ccs == CCS_MAIN) {
1104 goto done;
1105 }
1106 if (ccs == prev_ccs) {
1107 // ignore if this is the same as the previous stack,
1108 // we're probably in library code and haven't
1109 // accumulated any more interesting stack items
1110 // since the last update frame.
1111 break;
1112 }
1113 prev_ccs = ccs;
1114 fprintf(stderr, " --> evaluated by: ");
1115 is_caf = fprintCallStack(ccs);
1116 break;
1117 case UNDERFLOW_FRAME:
1118 stack = ((StgUnderflowFrame*)frame)->next_chunk;
1119 frame = stack->sp;
1120 break;
1121 case STOP_FRAME:
1122 goto done;
1123 default:
1124 frame += stack_frame_sizeW((StgClosure*)frame);
1125 break;
1126 }
1127 }
1128 done:
1129 return;
1130 }
1131
1132 #ifdef DEBUG
1133 void
1134 debugCCS( CostCentreStack *ccs )
1135 {
1136 debugBelch("<");
1137 for (; ccs && ccs != CCS_MAIN; ccs = ccs->prevStack ) {
1138 debugBelch("%s.%s", ccs->cc->module, ccs->cc->label);
1139 if (ccs->prevStack && ccs->prevStack != CCS_MAIN) {
1140 debugBelch(",");
1141 }
1142 }
1143 debugBelch(">");
1144 }
1145 #endif /* DEBUG */
1146
1147 #endif /* PROFILING */