Merge branch 'master' of ../HEAD
[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 lnat 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 {
245 /* No need for the <prog>.prof file */
246 prof_filename = NULL;
247 prof_file = NULL;
248 }
249 else
250 {
251 /* Initialise the log file name */
252 prof_filename = arenaAlloc(prof_arena, strlen(prog) + 6);
253 sprintf(prof_filename, "%s.prof", prog);
254
255 /* open the log file */
256 if ((prof_file = fopen(prof_filename, "w")) == NULL) {
257 debugBelch("Can't open profiling report file %s\n", prof_filename);
258 RtsFlags.CcFlags.doCostCentres = 0;
259 // The following line was added by Sung; retainer/LDV profiling may need
260 // two output files, i.e., <program>.prof/hp.
261 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER)
262 RtsFlags.ProfFlags.doHeapProfile = 0;
263 return;
264 }
265 }
266
267 if (RtsFlags.ProfFlags.doHeapProfile) {
268 /* Initialise the log file name */
269 hp_filename = arenaAlloc(prof_arena, strlen(prog) + 6);
270 sprintf(hp_filename, "%s.hp", prog);
271
272 /* open the log file */
273 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
274 debugBelch("Can't open profiling report file %s\n",
275 hp_filename);
276 RtsFlags.ProfFlags.doHeapProfile = 0;
277 return;
278 }
279 }
280 }
281
282 void
283 initTimeProfiling(void)
284 {
285 /* Start ticking */
286 startProfTimer();
287 };
288
289 void
290 endProfiling ( void )
291 {
292 if (RtsFlags.CcFlags.doCostCentres) {
293 stopProfTimer();
294 }
295 if (RtsFlags.ProfFlags.doHeapProfile) {
296 endHeapProfiling();
297 }
298 }
299
300 /* -----------------------------------------------------------------------------
301 Set CCCS when entering a function.
302
303 The algorithm is as follows.
304
305 ccs ++> ccsfn = ccs ++ dropCommonPrefix ccs ccsfn
306
307 where
308
309 dropCommonPrefix A B
310 -- returns the suffix of B after removing any prefix common
311 -- to both A and B.
312
313 e.g.
314
315 <a,b,c> ++> <> = <a,b,c>
316 <a,b,c> ++> <d> = <a,b,c,d>
317 <a,b,c> ++> <a,b> = <a,b,c>
318 <a,b> ++> <a,b,c> = <a,b,c>
319 <a,b,c> ++> <a,b,d> = <a,b,c,d>
320
321 -------------------------------------------------------------------------- */
322
323 // implements c1 ++> c2, where c1 and c2 are equal depth
324 //
325 static CostCentreStack *
326 enterFunEqualStacks (CostCentreStack *ccs0,
327 CostCentreStack *ccsapp,
328 CostCentreStack *ccsfn)
329 {
330 ASSERT(ccsapp->depth == ccsfn->depth);
331 if (ccsapp == ccsfn) return ccs0;
332 return pushCostCentre(enterFunEqualStacks(ccs0,
333 ccsapp->prevStack,
334 ccsfn->prevStack),
335 ccsfn->cc);
336 }
337
338 // implements c1 ++> c2, where c2 is deeper than c1.
339 // Drop elements of c2 until we have equal stacks, call
340 // enterFunEqualStacks(), and then push on the elements that we
341 // dropped in reverse order.
342 //
343 static CostCentreStack *
344 enterFunCurShorter (CostCentreStack *ccsapp, CostCentreStack *ccsfn, StgWord n)
345 {
346 if (n == 0) {
347 ASSERT(ccsfn->depth == ccsapp->depth);
348 return enterFunEqualStacks(ccsapp,ccsapp,ccsfn);;
349 } else {
350 ASSERT(ccsfn->depth > ccsapp->depth);
351 return pushCostCentre(enterFunCurShorter(ccsapp, ccsfn->prevStack, n-1),
352 ccsfn->cc);
353 }
354 }
355
356 void enterFunCCS (StgRegTable *reg, CostCentreStack *ccsfn)
357 {
358 CostCentreStack *ccsapp;
359
360 // common case 1: both stacks are the same
361 if (ccsfn == reg->rCCCS) {
362 return;
363 }
364
365 // common case 2: the function stack is empty, or just CAF
366 if (ccsfn->prevStack == CCS_MAIN) {
367 return;
368 }
369
370 ccsapp = reg->rCCCS;
371 reg->rCCCS = CCS_OVERHEAD;
372
373 // common case 3: the stacks are completely different (e.g. one is a
374 // descendent of MAIN and the other of a CAF): we append the whole
375 // of the function stack to the current CCS.
376 if (ccsfn->root != ccsapp->root) {
377 reg->rCCCS = appendCCS(ccsapp,ccsfn);
378 return;
379 }
380
381 // uncommon case 4: ccsapp is deeper than ccsfn
382 if (ccsapp->depth > ccsfn->depth) {
383 nat i, n;
384 CostCentreStack *tmp = ccsapp;
385 n = ccsapp->depth - ccsfn->depth;
386 for (i = 0; i < n; i++) {
387 tmp = tmp->prevStack;
388 }
389 reg->rCCCS = enterFunEqualStacks(ccsapp,tmp,ccsfn);
390 return;
391 }
392
393 // uncommon case 5: ccsfn is deeper than CCCS
394 if (ccsfn->depth > ccsapp->depth) {
395 reg->rCCCS = enterFunCurShorter(ccsapp, ccsfn,
396 ccsfn->depth - ccsapp->depth);
397 return;
398 }
399
400 // uncommon case 6: stacks are equal depth, but different
401 reg->rCCCS = enterFunEqualStacks(ccsapp,ccsapp,ccsfn);
402 }
403
404 /* -----------------------------------------------------------------------------
405 Decide whether closures with this CCS should contribute to the heap
406 profile.
407 -------------------------------------------------------------------------- */
408
409 static void
410 ccsSetSelected (CostCentreStack *ccs)
411 {
412 if (RtsFlags.ProfFlags.modSelector) {
413 if (! strMatchesSelector (ccs->cc->module,
414 RtsFlags.ProfFlags.modSelector) ) {
415 ccs->selected = 0;
416 return;
417 }
418 }
419 if (RtsFlags.ProfFlags.ccSelector) {
420 if (! strMatchesSelector (ccs->cc->label,
421 RtsFlags.ProfFlags.ccSelector) ) {
422 ccs->selected = 0;
423 return;
424 }
425 }
426 if (RtsFlags.ProfFlags.ccsSelector) {
427 CostCentreStack *c;
428 for (c = ccs; c != NULL; c = c->prevStack)
429 {
430 if ( strMatchesSelector (c->cc->label,
431 RtsFlags.ProfFlags.ccsSelector) ) {
432 break;
433 }
434 }
435 if (c == NULL) {
436 ccs->selected = 0;
437 return;
438 }
439 }
440
441 ccs->selected = 1;
442 return;
443 }
444
445 /* -----------------------------------------------------------------------------
446 Cost-centre stack manipulation
447 -------------------------------------------------------------------------- */
448
449 #ifdef DEBUG
450 CostCentreStack * _pushCostCentre ( CostCentreStack *ccs, CostCentre *cc );
451 CostCentreStack *
452 pushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
453 #define pushCostCentre _pushCostCentre
454 {
455 IF_DEBUG(prof,
456 traceBegin("pushing %s on ", cc->label);
457 debugCCS(ccs);
458 traceEnd(););
459
460 return pushCostCentre(ccs,cc);
461 }
462 #endif
463
464 /* Append ccs1 to ccs2 (ignoring any CAF cost centre at the root of ccs1 */
465
466 #ifdef DEBUG
467 CostCentreStack *_appendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
468 CostCentreStack *
469 appendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
470 #define appendCCS _appendCCS
471 {
472 IF_DEBUG(prof,
473 if (ccs1 != ccs2) {
474 debugBelch("Appending ");
475 debugCCS(ccs1);
476 debugBelch(" to ");
477 debugCCS(ccs2);
478 debugBelch("\n");});
479 return appendCCS(ccs1,ccs2);
480 }
481 #endif
482
483 CostCentreStack *
484 appendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
485 {
486 if (ccs1 == ccs2) {
487 return ccs1;
488 }
489
490 if (ccs2 == CCS_MAIN || ccs2->cc->is_caf == CC_IS_CAF) {
491 // stop at a CAF element
492 return ccs1;
493 }
494
495 return pushCostCentre(appendCCS(ccs1, ccs2->prevStack), ccs2->cc);
496 }
497
498 // Pick one:
499 // #define RECURSION_DROPS
500 #define RECURSION_TRUNCATES
501
502 CostCentreStack *
503 pushCostCentre (CostCentreStack *ccs, CostCentre *cc)
504 {
505 CostCentreStack *temp_ccs, *ret;
506 IndexTable *ixtable;
507
508 if (ccs == EMPTY_STACK) {
509 ACQUIRE_LOCK(&ccs_mutex);
510 ret = actualPush(ccs,cc);
511 }
512 else
513 {
514 if (ccs->cc == cc) {
515 return ccs;
516 } else {
517 // check if we've already memoized this stack
518 ixtable = ccs->indexTable;
519 temp_ccs = isInIndexTable(ixtable,cc);
520
521 if (temp_ccs != EMPTY_STACK) {
522 return temp_ccs;
523 } else {
524
525 // not in the IndexTable, now we take the lock:
526 ACQUIRE_LOCK(&ccs_mutex);
527
528 if (ccs->indexTable != ixtable)
529 {
530 // someone modified ccs->indexTable while
531 // we did not hold the lock, so we must
532 // check it again:
533 temp_ccs = isInIndexTable(ixtable,cc);
534 if (temp_ccs != EMPTY_STACK)
535 {
536 RELEASE_LOCK(&ccs_mutex);
537 return temp_ccs;
538 }
539 }
540 temp_ccs = checkLoop(ccs,cc);
541 if (temp_ccs != NULL) {
542 // This CC is already in the stack somewhere.
543 // This could be recursion, or just calling
544 // another function with the same CC.
545 // A number of policies are possible at this
546 // point, we implement two here:
547 // - truncate the stack to the previous instance
548 // of this CC
549 // - ignore this push, return the same stack.
550 //
551 CostCentreStack *new_ccs;
552 #if defined(RECURSION_TRUNCATES)
553 new_ccs = temp_ccs;
554 #else // defined(RECURSION_DROPS)
555 new_ccs = ccs;
556 #endif
557 ccs->indexTable = addToIndexTable (ccs->indexTable,
558 new_ccs, cc, 1);
559 ret = new_ccs;
560 } else {
561 ret = actualPush (ccs,cc);
562 }
563 }
564 }
565 }
566
567 RELEASE_LOCK(&ccs_mutex);
568 return ret;
569 }
570
571 static CostCentreStack *
572 checkLoop (CostCentreStack *ccs, CostCentre *cc)
573 {
574 while (ccs != EMPTY_STACK) {
575 if (ccs->cc == cc)
576 return ccs;
577 ccs = ccs->prevStack;
578 }
579 return NULL;
580 }
581
582 static CostCentreStack *
583 actualPush (CostCentreStack *ccs, CostCentre *cc)
584 {
585 CostCentreStack *new_ccs;
586
587 // allocate space for a new CostCentreStack
588 new_ccs = (CostCentreStack *) arenaAlloc(prof_arena, sizeof(CostCentreStack));
589
590 return actualPush_(ccs, cc, new_ccs);
591 }
592
593 static CostCentreStack *
594 actualPush_ (CostCentreStack *ccs, CostCentre *cc, CostCentreStack *new_ccs)
595 {
596 /* assign values to each member of the structure */
597 new_ccs->ccsID = CCS_ID++;
598 new_ccs->cc = cc;
599 new_ccs->prevStack = ccs;
600 new_ccs->root = ccs->root;
601 new_ccs->depth = ccs->depth + 1;
602
603 new_ccs->indexTable = EMPTY_TABLE;
604
605 /* Initialise the various _scc_ counters to zero
606 */
607 new_ccs->scc_count = 0;
608
609 /* Initialize all other stats here. There should be a quick way
610 * that's easily used elsewhere too
611 */
612 new_ccs->time_ticks = 0;
613 new_ccs->mem_alloc = 0;
614 new_ccs->inherited_ticks = 0;
615 new_ccs->inherited_alloc = 0;
616
617 // Set the selected field.
618 ccsSetSelected(new_ccs);
619
620 /* update the memoization table for the parent stack */
621 if (ccs != EMPTY_STACK) {
622 ccs->indexTable = addToIndexTable(ccs->indexTable, new_ccs, cc,
623 0/*not a back edge*/);
624 }
625
626 /* return a pointer to the new stack */
627 return new_ccs;
628 }
629
630
631 static CostCentreStack *
632 isInIndexTable(IndexTable *it, CostCentre *cc)
633 {
634 while (it!=EMPTY_TABLE)
635 {
636 if (it->cc == cc)
637 return it->ccs;
638 else
639 it = it->next;
640 }
641
642 /* otherwise we never found it so return EMPTY_TABLE */
643 return EMPTY_TABLE;
644 }
645
646
647 static IndexTable *
648 addToIndexTable (IndexTable *it, CostCentreStack *new_ccs,
649 CostCentre *cc, unsigned int back_edge)
650 {
651 IndexTable *new_it;
652
653 new_it = arenaAlloc(prof_arena, sizeof(IndexTable));
654
655 new_it->cc = cc;
656 new_it->ccs = new_ccs;
657 new_it->next = it;
658 new_it->back_edge = back_edge;
659 return new_it;
660 }
661
662 /* -----------------------------------------------------------------------------
663 Generating a time & allocation profiling report.
664 -------------------------------------------------------------------------- */
665
666 /* We omit certain system-related CCs and CCSs from the default
667 * reports, so as not to cause confusion.
668 */
669 static rtsBool
670 ignoreCC (CostCentre *cc)
671 {
672 if (RtsFlags.CcFlags.doCostCentres < COST_CENTRES_ALL &&
673 ( cc == CC_OVERHEAD
674 || cc == CC_DONT_CARE
675 || cc == CC_GC
676 || cc == CC_SYSTEM)) {
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 return rtsTrue;
692 } else {
693 return rtsFalse;
694 }
695 }
696
697 /* -----------------------------------------------------------------------------
698 Generating the aggregated per-cost-centre time/alloc report.
699 -------------------------------------------------------------------------- */
700
701 static CostCentre *sorted_cc_list;
702
703 static void
704 aggregateCCCosts( CostCentreStack *ccs )
705 {
706 IndexTable *i;
707
708 ccs->cc->mem_alloc += ccs->mem_alloc;
709 ccs->cc->time_ticks += ccs->time_ticks;
710
711 for (i = ccs->indexTable; i != 0; i = i->next) {
712 if (!i->back_edge) {
713 aggregateCCCosts(i->ccs);
714 }
715 }
716 }
717
718 static void
719 insertCCInSortedList( CostCentre *new_cc )
720 {
721 CostCentre **prev, *cc;
722
723 prev = &sorted_cc_list;
724 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
725 if (new_cc->time_ticks > cc->time_ticks) {
726 new_cc->link = cc;
727 *prev = new_cc;
728 return;
729 } else {
730 prev = &(cc->link);
731 }
732 }
733 new_cc->link = NULL;
734 *prev = new_cc;
735 }
736
737 static nat
738 strlen_utf8 (char *s)
739 {
740 nat n = 0;
741 unsigned char c;
742
743 for (; *s != '\0'; s++) {
744 c = *s;
745 if (c < 0x80 || c > 0xBF) n++;
746 }
747 return n;
748 }
749
750 static void
751 reportPerCCCosts( void )
752 {
753 CostCentre *cc, *next;
754 nat max_label_len, max_module_len;
755
756 aggregateCCCosts(CCS_MAIN);
757 sorted_cc_list = NULL;
758
759 max_label_len = 11; // no shorter than the "COST CENTRE" header
760 max_module_len = 7; // no shorter than the "MODULE" header
761
762 for (cc = CC_LIST; cc != NULL; cc = next) {
763 next = cc->link;
764 if (cc->time_ticks > total_prof_ticks/100
765 || cc->mem_alloc > total_alloc/100
766 || RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL) {
767 insertCCInSortedList(cc);
768
769 max_label_len = stg_max(strlen_utf8(cc->label), max_label_len);
770 max_module_len = stg_max(strlen_utf8(cc->module), max_module_len);
771 }
772 }
773
774 fprintf(prof_file, "%-*s %-*s", max_label_len, "COST CENTRE", max_module_len, "MODULE");
775 fprintf(prof_file, "%6s %6s", "%time", "%alloc");
776 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
777 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
778 }
779 fprintf(prof_file, "\n\n");
780
781 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
782 if (ignoreCC(cc)) {
783 continue;
784 }
785 fprintf(prof_file, "%s%*s %s%*s",
786 cc->label,
787 max_label_len - strlen_utf8(cc->label), "",
788 cc->module,
789 max_module_len - strlen_utf8(cc->module), "");
790
791 fprintf(prof_file, "%6.1f %6.1f",
792 total_prof_ticks == 0 ? 0.0 : (cc->time_ticks / (StgFloat) total_prof_ticks * 100),
793 total_alloc == 0 ? 0.0 : (cc->mem_alloc / (StgFloat)
794 total_alloc * 100)
795 );
796
797 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
798 fprintf(prof_file, " %5" FMT_Word64 " %9" FMT_Word64,
799 (StgWord64)(cc->time_ticks), cc->mem_alloc*sizeof(W_));
800 }
801 fprintf(prof_file, "\n");
802 }
803
804 fprintf(prof_file,"\n\n");
805 }
806
807 /* -----------------------------------------------------------------------------
808 Generate the cost-centre-stack time/alloc report
809 -------------------------------------------------------------------------- */
810
811 static void
812 fprintHeader( nat max_label_len, nat max_module_len )
813 {
814 fprintf(prof_file, "%-*s %-*s%6s %11s %11s %11s\n", max_label_len, "", max_module_len, "", "", "", "individual", "inherited");
815
816 fprintf(prof_file, "%-*s %-*s", max_label_len, "COST CENTRE", max_module_len, "MODULE");
817 fprintf(prof_file, "%6s %11s %5s %5s %5s %5s", "no.", "entries", "%time", "%alloc", "%time", "%alloc");
818
819 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
820 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
821 }
822
823 fprintf(prof_file, "\n\n");
824 }
825
826 void
827 reportCCSProfiling( void )
828 {
829 nat count;
830 char temp[128]; /* sigh: magic constant */
831
832 stopProfTimer();
833
834 total_prof_ticks = 0;
835 total_alloc = 0;
836 countTickss(CCS_MAIN);
837
838 if (RtsFlags.CcFlags.doCostCentres == 0) return;
839
840 fprintf(prof_file, "\t%s Time and Allocation Profiling Report (%s)\n",
841 time_str(), "Final");
842
843 fprintf(prof_file, "\n\t ");
844 fprintf(prof_file, " %s", prog_name);
845 fprintf(prof_file, " +RTS");
846 for (count = 0; rts_argv[count]; count++)
847 fprintf(prof_file, " %s", rts_argv[count]);
848 fprintf(prof_file, " -RTS");
849 for (count = 1; prog_argv[count]; count++)
850 fprintf(prof_file, " %s", prog_argv[count]);
851 fprintf(prof_file, "\n\n");
852
853 fprintf(prof_file, "\ttotal time = %11.2f secs (%lu ticks @ %d us, %d processor%s)\n",
854 ((double) total_prof_ticks *
855 (double) RtsFlags.MiscFlags.tickInterval) / (TIME_RESOLUTION * n_capabilities),
856 (unsigned long) total_prof_ticks,
857 (int) TimeToUS(RtsFlags.MiscFlags.tickInterval),
858 n_capabilities, n_capabilities > 1 ? "s" : "");
859
860 fprintf(prof_file, "\ttotal alloc = %11s bytes",
861 showStgWord64(total_alloc * sizeof(W_),
862 temp, rtsTrue/*commas*/));
863
864 fprintf(prof_file, " (excludes profiling overheads)\n\n");
865
866 reportPerCCCosts();
867
868 inheritCosts(CCS_MAIN);
869
870 reportCCS(pruneCCSTree(CCS_MAIN));
871 }
872
873 static void
874 findCCSMaxLens(CostCentreStack *ccs, nat indent, nat *max_label_len, nat *max_module_len) {
875 CostCentre *cc;
876 IndexTable *i;
877
878 cc = ccs->cc;
879
880 *max_label_len = stg_max(*max_label_len, indent + strlen_utf8(cc->label));
881 *max_module_len = stg_max(*max_module_len, strlen_utf8(cc->module));
882
883 for (i = ccs->indexTable; i != 0; i = i->next) {
884 if (!i->back_edge) {
885 findCCSMaxLens(i->ccs, indent+1, max_label_len, max_module_len);
886 }
887 }
888 }
889
890 static void
891 logCCS(CostCentreStack *ccs, nat indent, nat max_label_len, nat max_module_len)
892 {
893 CostCentre *cc;
894 IndexTable *i;
895
896 cc = ccs->cc;
897
898 /* Only print cost centres with non 0 data ! */
899
900 if (!ignoreCCS(ccs))
901 /* force printing of *all* cost centres if -Pa */
902 {
903
904 fprintf(prof_file, "%-*s%s%*s %s%*s",
905 indent, "",
906 cc->label,
907 max_label_len-indent - strlen_utf8(cc->label), "",
908 cc->module,
909 max_module_len - strlen_utf8(cc->module), "");
910
911 fprintf(prof_file, "%6ld %11" FMT_Word64 " %5.1f %5.1f %5.1f %5.1f",
912 ccs->ccsID, ccs->scc_count,
913 total_prof_ticks == 0 ? 0.0 : ((double)ccs->time_ticks / (double)total_prof_ticks * 100.0),
914 total_alloc == 0 ? 0.0 : ((double)ccs->mem_alloc / (double)total_alloc * 100.0),
915 total_prof_ticks == 0 ? 0.0 : ((double)ccs->inherited_ticks / (double)total_prof_ticks * 100.0),
916 total_alloc == 0 ? 0.0 : ((double)ccs->inherited_alloc / (double)total_alloc * 100.0)
917 );
918
919 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
920 fprintf(prof_file, " %5" FMT_Word64 " %9" FMT_Word64,
921 (StgWord64)(ccs->time_ticks), ccs->mem_alloc*sizeof(W_));
922 }
923 fprintf(prof_file, "\n");
924 }
925
926 for (i = ccs->indexTable; i != 0; i = i->next) {
927 if (!i->back_edge) {
928 logCCS(i->ccs, indent+1, max_label_len, max_module_len);
929 }
930 }
931 }
932
933 static void
934 reportCCS(CostCentreStack *ccs)
935 {
936 nat max_label_len, max_module_len;
937
938 max_label_len = 11; // no shorter than "COST CENTRE" header
939 max_module_len = 7; // no shorter than "MODULE" header
940
941 findCCSMaxLens(ccs, 0, &max_label_len, &max_module_len);
942
943 fprintHeader(max_label_len, max_module_len);
944 logCCS(ccs, 0, max_label_len, max_module_len);
945 }
946
947
948 /* Traverse the cost centre stack tree and accumulate
949 * ticks/allocations.
950 */
951 static void
952 countTickss(CostCentreStack *ccs)
953 {
954 IndexTable *i;
955
956 if (!ignoreCCS(ccs)) {
957 total_alloc += ccs->mem_alloc;
958 total_prof_ticks += ccs->time_ticks;
959 }
960 for (i = ccs->indexTable; i != NULL; i = i->next)
961 if (!i->back_edge) {
962 countTickss(i->ccs);
963 }
964 }
965
966 /* Traverse the cost centre stack tree and inherit ticks & allocs.
967 */
968 static void
969 inheritCosts(CostCentreStack *ccs)
970 {
971 IndexTable *i;
972
973 if (ignoreCCS(ccs)) { return; }
974
975 ccs->inherited_ticks += ccs->time_ticks;
976 ccs->inherited_alloc += ccs->mem_alloc;
977
978 for (i = ccs->indexTable; i != NULL; i = i->next)
979 if (!i->back_edge) {
980 inheritCosts(i->ccs);
981 ccs->inherited_ticks += i->ccs->inherited_ticks;
982 ccs->inherited_alloc += i->ccs->inherited_alloc;
983 }
984
985 return;
986 }
987
988 //
989 // Prune CCSs with zero entries, zero ticks or zero allocation from
990 // the tree, unless COST_CENTRES_ALL is on.
991 //
992 static CostCentreStack *
993 pruneCCSTree (CostCentreStack *ccs)
994 {
995 CostCentreStack *ccs1;
996 IndexTable *i, **prev;
997
998 prev = &ccs->indexTable;
999 for (i = ccs->indexTable; i != 0; i = i->next) {
1000 if (i->back_edge) { continue; }
1001
1002 ccs1 = pruneCCSTree(i->ccs);
1003 if (ccs1 == NULL) {
1004 *prev = i->next;
1005 } else {
1006 prev = &(i->next);
1007 }
1008 }
1009
1010 if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
1011 /* force printing of *all* cost centres if -P -P */ )
1012
1013 || ( ccs->indexTable != 0 )
1014 || ( ccs->scc_count || ccs->time_ticks || ccs->mem_alloc )
1015 ) {
1016 return ccs;
1017 } else {
1018 return NULL;
1019 }
1020 }
1021
1022 void
1023 fprintCCS( FILE *f, CostCentreStack *ccs )
1024 {
1025 fprintf(f,"<");
1026 for (; ccs && ccs != CCS_MAIN; ccs = ccs->prevStack ) {
1027 fprintf(f,"%s.%s", ccs->cc->module, ccs->cc->label);
1028 if (ccs->prevStack && ccs->prevStack != CCS_MAIN) {
1029 fprintf(f,",");
1030 }
1031 }
1032 fprintf(f,">");
1033 }
1034
1035 // Returns: True if the call stack ended with CAF
1036 static rtsBool fprintCallStack (CostCentreStack *ccs)
1037 {
1038 CostCentreStack *prev;
1039
1040 fprintf(stderr,"%s.%s", ccs->cc->module, ccs->cc->label);
1041 prev = ccs->prevStack;
1042 while (prev && prev != CCS_MAIN) {
1043 ccs = prev;
1044 fprintf(stderr, ",\n called from %s.%s",
1045 ccs->cc->module, ccs->cc->label);
1046 prev = ccs->prevStack;
1047 }
1048 fprintf(stderr, "\n");
1049
1050 return (!strncmp(ccs->cc->label, "CAF", 3));
1051 }
1052
1053 /* For calling from .cmm code, where we can't reliably refer to stderr */
1054 void
1055 fprintCCS_stderr (CostCentreStack *ccs, StgClosure *exception, StgTSO *tso)
1056 {
1057 rtsBool is_caf;
1058 StgPtr frame;
1059 StgStack *stack;
1060 CostCentreStack *prev_ccs;
1061 nat depth = 0;
1062 const nat MAX_DEPTH = 10; // don't print gigantic chains of stacks
1063
1064 {
1065 char *desc;
1066 StgInfoTable *info;
1067 info = get_itbl(exception);
1068 switch (info->type) {
1069 case CONSTR:
1070 case CONSTR_1_0:
1071 case CONSTR_0_1:
1072 case CONSTR_2_0:
1073 case CONSTR_1_1:
1074 case CONSTR_0_2:
1075 case CONSTR_STATIC:
1076 case CONSTR_NOCAF_STATIC:
1077 desc = GET_CON_DESC(itbl_to_con_itbl(info));
1078 default:
1079 desc = closure_type_names[info->type];
1080 }
1081 fprintf(stderr, "*** Exception (reporting due to +RTS -xc): (%s), stack trace: \n ", desc);
1082 }
1083
1084 is_caf = fprintCallStack(ccs);
1085
1086 // traverse the stack down to the enclosing update frame to
1087 // find out where this CCS was evaluated from...
1088
1089 stack = tso->stackobj;
1090 frame = stack->sp;
1091 prev_ccs = ccs;
1092
1093 for (; is_caf && depth < MAX_DEPTH; depth++)
1094 {
1095 switch (get_itbl((StgClosure*)frame)->type)
1096 {
1097 case UPDATE_FRAME:
1098 ccs = ((StgUpdateFrame*)frame)->header.prof.ccs;
1099 frame += sizeofW(StgUpdateFrame);
1100 if (ccs == CCS_MAIN) {
1101 goto done;
1102 }
1103 if (ccs == prev_ccs) {
1104 // ignore if this is the same as the previous stack,
1105 // we're probably in library code and haven't
1106 // accumulated any more interesting stack items
1107 // since the last update frame.
1108 break;
1109 }
1110 prev_ccs = ccs;
1111 fprintf(stderr, " --> evaluated by: ");
1112 is_caf = fprintCallStack(ccs);
1113 break;
1114 case UNDERFLOW_FRAME:
1115 stack = ((StgUnderflowFrame*)frame)->next_chunk;
1116 frame = stack->sp;
1117 break;
1118 case STOP_FRAME:
1119 goto done;
1120 default:
1121 frame += stack_frame_sizeW((StgClosure*)frame);
1122 break;
1123 }
1124 }
1125 done:
1126 return;
1127 }
1128
1129 #ifdef DEBUG
1130 void
1131 debugCCS( CostCentreStack *ccs )
1132 {
1133 debugBelch("<");
1134 for (; ccs && ccs != CCS_MAIN; ccs = ccs->prevStack ) {
1135 debugBelch("%s.%s", ccs->cc->module, ccs->cc->label);
1136 if (ccs->prevStack && ccs->prevStack != CCS_MAIN) {
1137 debugBelch(",");
1138 }
1139 }
1140 debugBelch(">");
1141 }
1142 #endif /* DEBUG */
1143
1144 #endif /* PROFILING */