#807: Removed double fclose of prof_file
[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 #include "RtsUtils.h"
14 #include "RtsFlags.h"
15 #include "Profiling.h"
16 #include "Storage.h"
17 #include "Proftimer.h"
18 #include "Timer.h"
19 #include "ProfHeap.h"
20 #include "Arena.h"
21 #include "RetainerProfile.h"
22 #include "LdvProfile.h"
23
24 #include <string.h>
25
26 /*
27 * Profiling allocation arena.
28 */
29 Arena *prof_arena;
30
31 /*
32 * Global variables used to assign unique IDs to cc's, ccs's, and
33 * closure_cats
34 */
35
36 unsigned int CC_ID;
37 unsigned int CCS_ID;
38 unsigned int HP_ID;
39
40 /* figures for the profiling report.
41 */
42 static ullong total_alloc;
43 static lnat total_prof_ticks;
44
45 /* Globals for opening the profiling log file(s)
46 */
47 static char *prof_filename; /* prof report file name = <program>.prof */
48 FILE *prof_file;
49
50 static char *hp_filename; /* heap profile (hp2ps style) log file */
51 FILE *hp_file;
52
53 /* The Current Cost Centre Stack (for attributing costs)
54 */
55 CostCentreStack *CCCS;
56
57 /* Linked lists to keep track of cc's and ccs's that haven't
58 * been declared in the log file yet
59 */
60 CostCentre *CC_LIST;
61 CostCentreStack *CCS_LIST;
62
63 /*
64 * Built-in cost centres and cost-centre stacks:
65 *
66 * MAIN is the root of the cost-centre stack tree. If there are
67 * no _scc_s in the program, all costs will be attributed
68 * to MAIN.
69 *
70 * SYSTEM is the RTS in general (scheduler, etc.). All costs for
71 * RTS operations apart from garbage collection are attributed
72 * to SYSTEM.
73 *
74 * GC is the storage manager / garbage collector.
75 *
76 * OVERHEAD gets all costs generated by the profiling system
77 * itself. These are costs that would not be incurred
78 * during non-profiled execution of the program.
79 *
80 * SUBSUMED is the one-and-only CCS placed on top-level functions.
81 * It indicates that all costs are to be attributed to the
82 * enclosing cost centre stack. SUBSUMED never accumulates
83 * any costs. The is_caf flag is set on the subsumed cost
84 * centre.
85 *
86 * DONT_CARE is a placeholder cost-centre we assign to static
87 * constructors. It should *never* accumulate any costs.
88 */
89
90 CC_DECLARE(CC_MAIN, "MAIN", "MAIN", CC_IS_BORING, );
91 CC_DECLARE(CC_SYSTEM, "SYSTEM", "MAIN", CC_IS_BORING, );
92 CC_DECLARE(CC_GC, "GC", "GC", CC_IS_BORING, );
93 CC_DECLARE(CC_OVERHEAD, "OVERHEAD_of", "PROFILING", CC_IS_CAF, );
94 CC_DECLARE(CC_SUBSUMED, "SUBSUMED", "MAIN", CC_IS_CAF, );
95 CC_DECLARE(CC_DONT_CARE, "DONT_CARE", "MAIN", CC_IS_BORING, );
96
97 CCS_DECLARE(CCS_MAIN, CC_MAIN, );
98 CCS_DECLARE(CCS_SYSTEM, CC_SYSTEM, );
99 CCS_DECLARE(CCS_GC, CC_GC, );
100 CCS_DECLARE(CCS_OVERHEAD, CC_OVERHEAD, );
101 CCS_DECLARE(CCS_SUBSUMED, CC_SUBSUMED, );
102 CCS_DECLARE(CCS_DONT_CARE, CC_DONT_CARE, );
103
104 /*
105 * Uniques for the XML log-file format
106 */
107 #define CC_UQ 1
108 #define CCS_UQ 2
109 #define TC_UQ 3
110 #define HEAP_OBJ_UQ 4
111 #define TIME_UPD_UQ 5
112 #define HEAP_UPD_UQ 6
113
114 /*
115 * Static Functions
116 */
117
118 static CostCentreStack * ActualPush_ ( CostCentreStack *ccs, CostCentre *cc,
119 CostCentreStack *new_ccs );
120 static rtsBool ccs_to_ignore ( CostCentreStack *ccs );
121 static void count_ticks ( CostCentreStack *ccs );
122 static void inherit_costs ( CostCentreStack *ccs );
123 static void reportCCS ( CostCentreStack *ccs, nat indent );
124 static void DecCCS ( CostCentreStack *ccs );
125 static void DecBackEdge ( CostCentreStack *ccs,
126 CostCentreStack *oldccs );
127 static CostCentreStack * CheckLoop ( CostCentreStack *ccs, 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 static void reportCCS_XML ( CostCentreStack *ccs );
139
140 /* -----------------------------------------------------------------------------
141 Initialise the profiling environment
142 -------------------------------------------------------------------------- */
143
144 void
145 initProfiling1 (void)
146 {
147 // initialise our arena
148 prof_arena = newArena();
149
150 /* for the benefit of allocate()... */
151 CCCS = CCS_SYSTEM;
152
153 /* Initialize counters for IDs */
154 CC_ID = 1;
155 CCS_ID = 1;
156 HP_ID = 1;
157
158 /* Initialize Declaration lists to NULL */
159 CC_LIST = NULL;
160 CCS_LIST = NULL;
161
162 /* Register all the cost centres / stacks in the program
163 * CC_MAIN gets link = 0, all others have non-zero link.
164 */
165 REGISTER_CC(CC_MAIN);
166 REGISTER_CC(CC_SYSTEM);
167 REGISTER_CC(CC_GC);
168 REGISTER_CC(CC_OVERHEAD);
169 REGISTER_CC(CC_SUBSUMED);
170 REGISTER_CC(CC_DONT_CARE);
171 REGISTER_CCS(CCS_MAIN);
172 REGISTER_CCS(CCS_SYSTEM);
173 REGISTER_CCS(CCS_GC);
174 REGISTER_CCS(CCS_OVERHEAD);
175 REGISTER_CCS(CCS_SUBSUMED);
176 REGISTER_CCS(CCS_DONT_CARE);
177
178 CCCS = CCS_OVERHEAD;
179
180 /* cost centres are registered by the per-module
181 * initialisation code now...
182 */
183 }
184
185 void
186 initProfiling2 (void)
187 {
188 CostCentreStack *ccs, *next;
189
190 CCCS = CCS_SYSTEM;
191
192 /* Set up the log file, and dump the header and cost centre
193 * information into it. */
194 initProfilingLogFile();
195
196 /* find all the "special" cost centre stacks, and make them children
197 * of CCS_MAIN.
198 */
199 ASSERT(CCS_MAIN->prevStack == 0);
200 CCS_MAIN->root = CC_MAIN;
201 ccsSetSelected(CCS_MAIN);
202 DecCCS(CCS_MAIN);
203
204 for (ccs = CCS_LIST; ccs != CCS_MAIN; ) {
205 next = ccs->prevStack;
206 ccs->prevStack = 0;
207 ActualPush_(CCS_MAIN,ccs->cc,ccs);
208 ccs->root = ccs->cc;
209 ccs = next;
210 }
211
212 if (RtsFlags.CcFlags.doCostCentres) {
213 initTimeProfiling();
214 }
215
216 if (RtsFlags.ProfFlags.doHeapProfile) {
217 initHeapProfiling();
218 }
219 }
220
221 // Decide whether closures with this CCS should contribute to the heap
222 // profile.
223 static void
224 ccsSetSelected( CostCentreStack *ccs )
225 {
226 if (RtsFlags.ProfFlags.modSelector) {
227 if (! strMatchesSelector( ccs->cc->module,
228 RtsFlags.ProfFlags.modSelector ) ) {
229 ccs->selected = 0;
230 return;
231 }
232 }
233 if (RtsFlags.ProfFlags.ccSelector) {
234 if (! strMatchesSelector( ccs->cc->label,
235 RtsFlags.ProfFlags.ccSelector ) ) {
236 ccs->selected = 0;
237 return;
238 }
239 }
240 if (RtsFlags.ProfFlags.ccsSelector) {
241 CostCentreStack *c;
242 for (c = ccs; c != NULL; c = c->prevStack) {
243 if ( strMatchesSelector( c->cc->label,
244 RtsFlags.ProfFlags.ccsSelector )) {
245 break;
246 }
247 }
248 if (c == NULL) {
249 ccs->selected = 0;
250 return;
251 }
252 }
253
254 ccs->selected = 1;
255 return;
256 }
257
258
259 static void
260 initProfilingLogFile(void)
261 {
262 /* Initialise the log file name */
263 prof_filename = arenaAlloc(prof_arena, strlen(prog_name) + 6);
264 sprintf(prof_filename, "%s.prof", prog_name);
265
266 /* open the log file */
267 if ((prof_file = fopen(prof_filename, "w")) == NULL) {
268 debugBelch("Can't open profiling report file %s\n", prof_filename);
269 RtsFlags.CcFlags.doCostCentres = 0;
270 // The following line was added by Sung; retainer/LDV profiling may need
271 // two output files, i.e., <program>.prof/hp.
272 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER)
273 RtsFlags.ProfFlags.doHeapProfile = 0;
274 return;
275 }
276
277 if (RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
278 /* dump the time, and the profiling interval */
279 fprintf(prof_file, "\"%s\"\n", time_str());
280 fprintf(prof_file, "\"%d ms\"\n", TICK_MILLISECS);
281
282 /* declare all the cost centres */
283 {
284 CostCentre *cc;
285 for (cc = CC_LIST; cc != NULL; cc = cc->link) {
286 fprintf(prof_file, "%d %ld \"%s\" \"%s\"\n",
287 CC_UQ, cc->ccID, cc->label, cc->module);
288 }
289 }
290 }
291
292 if (RtsFlags.ProfFlags.doHeapProfile) {
293 /* Initialise the log file name */
294 hp_filename = arenaAlloc(prof_arena, strlen(prog_name) + 6);
295 sprintf(hp_filename, "%s.hp", prog_name);
296
297 /* open the log file */
298 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
299 debugBelch("Can't open profiling report file %s\n",
300 hp_filename);
301 RtsFlags.ProfFlags.doHeapProfile = 0;
302 return;
303 }
304 }
305 }
306
307 void
308 initTimeProfiling(void)
309 {
310 /* Start ticking */
311 startProfTimer();
312 };
313
314 void
315 endProfiling ( void )
316 {
317 if (RtsFlags.CcFlags.doCostCentres) {
318 stopProfTimer();
319 }
320 if (RtsFlags.ProfFlags.doHeapProfile) {
321 endHeapProfiling();
322 }
323 }
324
325 /* -----------------------------------------------------------------------------
326 Set cost centre stack when entering a function.
327 -------------------------------------------------------------------------- */
328 rtsBool entering_PAP;
329
330 void
331 EnterFunCCS ( CostCentreStack *ccsfn )
332 {
333 /* PAP_entry has already set CCCS for us */
334 if (entering_PAP) {
335 entering_PAP = rtsFalse;
336 return;
337 }
338
339 if (ccsfn->root->is_caf == CC_IS_CAF) {
340 CCCS = AppendCCS(CCCS,ccsfn);
341 } else {
342 CCCS = ccsfn;
343 }
344 }
345
346 /* -----------------------------------------------------------------------------
347 Cost-centre stack manipulation
348 -------------------------------------------------------------------------- */
349
350 #ifdef DEBUG
351 CostCentreStack * _PushCostCentre ( CostCentreStack *ccs, CostCentre *cc );
352 CostCentreStack *
353 PushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
354 #define PushCostCentre _PushCostCentre
355 {
356 IF_DEBUG(prof,
357 traceBegin("pushing %s on ", cc->label);
358 debugCCS(ccs);
359 traceEnd(););
360
361 return PushCostCentre(ccs,cc);
362 }
363 #endif
364
365 CostCentreStack *
366 PushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
367 {
368 CostCentreStack *temp_ccs;
369
370 if (ccs == EMPTY_STACK)
371 return ActualPush(ccs,cc);
372 else {
373 if (ccs->cc == cc)
374 return ccs;
375 else {
376 /* check if we've already memoized this stack */
377 temp_ccs = IsInIndexTable(ccs->indexTable,cc);
378
379 if (temp_ccs != EMPTY_STACK)
380 return temp_ccs;
381 else {
382 temp_ccs = CheckLoop(ccs,cc);
383 if (temp_ccs != NULL) {
384 /* we have recursed to an older CCS. Mark this in
385 * the index table, and emit a "back edge" into the
386 * log file.
387 */
388 ccs->indexTable = AddToIndexTable(ccs->indexTable,temp_ccs,cc,1);
389 DecBackEdge(temp_ccs,ccs);
390 return temp_ccs;
391 } else {
392 return ActualPush(ccs,cc);
393 }
394 }
395 }
396 }
397 }
398
399 static CostCentreStack *
400 CheckLoop ( CostCentreStack *ccs, CostCentre *cc )
401 {
402 while (ccs != EMPTY_STACK) {
403 if (ccs->cc == cc)
404 return ccs;
405 ccs = ccs->prevStack;
406 }
407 return NULL;
408 }
409
410 /* Append ccs1 to ccs2 (ignoring any CAF cost centre at the root of ccs1 */
411
412 #ifdef DEBUG
413 CostCentreStack *_AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
414 CostCentreStack *
415 AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
416 #define AppendCCS _AppendCCS
417 {
418 IF_DEBUG(prof,
419 if (ccs1 != ccs2) {
420 debugBelch("Appending ");
421 debugCCS(ccs1);
422 debugBelch(" to ");
423 debugCCS(ccs2);
424 debugBelch("\n");});
425 return AppendCCS(ccs1,ccs2);
426 }
427 #endif
428
429 CostCentreStack *
430 AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
431 {
432 CostCentreStack *ccs = NULL;
433
434 if (ccs1 == ccs2) {
435 return ccs1;
436 }
437
438 if (ccs2->cc->is_caf == CC_IS_CAF) {
439 return ccs1;
440 }
441
442 if (ccs2->prevStack != NULL) {
443 ccs = AppendCCS(ccs1, ccs2->prevStack);
444 }
445
446 return PushCostCentre(ccs,ccs2->cc);
447 }
448
449 static CostCentreStack *
450 ActualPush ( CostCentreStack *ccs, CostCentre *cc )
451 {
452 CostCentreStack *new_ccs;
453
454 /* allocate space for a new CostCentreStack */
455 new_ccs = (CostCentreStack *) arenaAlloc(prof_arena, sizeof(CostCentreStack));
456
457 return ActualPush_(ccs, cc, new_ccs);
458 }
459
460 static CostCentreStack *
461 ActualPush_ ( CostCentreStack *ccs, CostCentre *cc, CostCentreStack *new_ccs )
462 {
463 /* assign values to each member of the structure */
464 new_ccs->ccsID = CCS_ID++;
465 new_ccs->cc = cc;
466 new_ccs->prevStack = ccs;
467
468 new_ccs->indexTable = EMPTY_TABLE;
469
470 /* Initialise the various _scc_ counters to zero
471 */
472 new_ccs->scc_count = 0;
473
474 /* Initialize all other stats here. There should be a quick way
475 * that's easily used elsewhere too
476 */
477 new_ccs->time_ticks = 0;
478 new_ccs->mem_alloc = 0;
479 new_ccs->inherited_ticks = 0;
480 new_ccs->inherited_alloc = 0;
481
482 new_ccs->root = ccs->root;
483
484 // Set the selected field.
485 ccsSetSelected(new_ccs);
486
487 /* update the memoization table for the parent stack */
488 if (ccs != EMPTY_STACK)
489 ccs->indexTable = AddToIndexTable(ccs->indexTable, new_ccs, cc,
490 0/*not a back edge*/);
491
492 /* make sure this CC is declared at the next heap/time sample */
493 DecCCS(new_ccs);
494
495 /* return a pointer to the new stack */
496 return new_ccs;
497 }
498
499
500 static CostCentreStack *
501 IsInIndexTable(IndexTable *it, CostCentre *cc)
502 {
503 while (it!=EMPTY_TABLE)
504 {
505 if (it->cc==cc)
506 return it->ccs;
507 else
508 it = it->next;
509 }
510
511 /* otherwise we never found it so return EMPTY_TABLE */
512 return EMPTY_TABLE;
513 }
514
515
516 static IndexTable *
517 AddToIndexTable(IndexTable *it, CostCentreStack *new_ccs,
518 CostCentre *cc, unsigned int back_edge)
519 {
520 IndexTable *new_it;
521
522 new_it = arenaAlloc(prof_arena, sizeof(IndexTable));
523
524 new_it->cc = cc;
525 new_it->ccs = new_ccs;
526 new_it->next = it;
527 new_it->back_edge = back_edge;
528 return new_it;
529 }
530
531
532 static void
533 DecCCS(CostCentreStack *ccs)
534 {
535 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
536 if (ccs->prevStack == EMPTY_STACK)
537 fprintf(prof_file, "%d %ld 1 %ld\n", CCS_UQ,
538 ccs->ccsID, ccs->cc->ccID);
539 else
540 fprintf(prof_file, "%d %ld 2 %ld %ld\n", CCS_UQ,
541 ccs->ccsID, ccs->cc->ccID, ccs->prevStack->ccsID);
542 }
543 }
544
545 static void
546 DecBackEdge( CostCentreStack *ccs, CostCentreStack *oldccs )
547 {
548 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
549 if (ccs->prevStack == EMPTY_STACK)
550 fprintf(prof_file, "%d %ld 1 %ld\n", CCS_UQ,
551 ccs->ccsID, ccs->cc->ccID);
552 else
553 fprintf(prof_file, "%d %ld 2 %ld %ld\n", CCS_UQ,
554 ccs->ccsID, ccs->cc->ccID, oldccs->ccsID);
555 }
556 }
557
558 /* -----------------------------------------------------------------------------
559 Generating a time & allocation profiling report.
560 -------------------------------------------------------------------------- */
561
562 /* We omit certain system-related CCs and CCSs from the default
563 * reports, so as not to cause confusion.
564 */
565 static rtsBool
566 cc_to_ignore (CostCentre *cc)
567 {
568 if ( cc == CC_OVERHEAD
569 || cc == CC_DONT_CARE
570 || cc == CC_GC
571 || cc == CC_SYSTEM) {
572 return rtsTrue;
573 } else {
574 return rtsFalse;
575 }
576 }
577
578 static rtsBool
579 ccs_to_ignore (CostCentreStack *ccs)
580 {
581 if ( ccs == CCS_OVERHEAD
582 || ccs == CCS_DONT_CARE
583 || ccs == CCS_GC
584 || ccs == CCS_SYSTEM) {
585 return rtsTrue;
586 } else {
587 return rtsFalse;
588 }
589 }
590
591 /* -----------------------------------------------------------------------------
592 Generating the aggregated per-cost-centre time/alloc report.
593 -------------------------------------------------------------------------- */
594
595 static CostCentre *sorted_cc_list;
596
597 static void
598 aggregate_cc_costs( CostCentreStack *ccs )
599 {
600 IndexTable *i;
601
602 ccs->cc->mem_alloc += ccs->mem_alloc;
603 ccs->cc->time_ticks += ccs->time_ticks;
604
605 for (i = ccs->indexTable; i != 0; i = i->next) {
606 if (!i->back_edge) {
607 aggregate_cc_costs(i->ccs);
608 }
609 }
610 }
611
612 static void
613 insert_cc_in_sorted_list( CostCentre *new_cc )
614 {
615 CostCentre **prev, *cc;
616
617 prev = &sorted_cc_list;
618 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
619 if (new_cc->time_ticks > cc->time_ticks) {
620 new_cc->link = cc;
621 *prev = new_cc;
622 return;
623 } else {
624 prev = &(cc->link);
625 }
626 }
627 new_cc->link = NULL;
628 *prev = new_cc;
629 }
630
631 static void
632 report_per_cc_costs( void )
633 {
634 CostCentre *cc, *next;
635
636 aggregate_cc_costs(CCS_MAIN);
637 sorted_cc_list = NULL;
638
639 for (cc = CC_LIST; cc != NULL; cc = next) {
640 next = cc->link;
641 if (cc->time_ticks > total_prof_ticks/100
642 || cc->mem_alloc > total_alloc/100
643 || RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL) {
644 insert_cc_in_sorted_list(cc);
645 }
646 }
647
648 fprintf(prof_file, "%-30s %-20s", "COST CENTRE", "MODULE");
649 fprintf(prof_file, "%6s %6s", "%time", "%alloc");
650 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
651 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
652 }
653 fprintf(prof_file, "\n\n");
654
655 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
656 if (cc_to_ignore(cc)) {
657 continue;
658 }
659 fprintf(prof_file, "%-30s %-20s", cc->label, cc->module);
660 fprintf(prof_file, "%6.1f %6.1f",
661 total_prof_ticks == 0 ? 0.0 : (cc->time_ticks / (StgFloat) total_prof_ticks * 100),
662 total_alloc == 0 ? 0.0 : (cc->mem_alloc / (StgFloat)
663 total_alloc * 100)
664 );
665
666 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
667 fprintf(prof_file, " %5" FMT_Word64 " %9" FMT_Word64,
668 (StgWord64)(cc->time_ticks), cc->mem_alloc);
669 }
670 fprintf(prof_file, "\n");
671 }
672
673 fprintf(prof_file,"\n\n");
674 }
675
676 /* -----------------------------------------------------------------------------
677 Generate the cost-centre-stack time/alloc report
678 -------------------------------------------------------------------------- */
679
680 static void
681 fprint_header( void )
682 {
683 fprintf(prof_file, "%-24s %-10s individual inherited\n", "", "");
684
685 fprintf(prof_file, "%-24s %-50s", "COST CENTRE", "MODULE");
686 fprintf(prof_file, "%6s %10s %5s %5s %5s %5s", "no.", "entries", "%time", "%alloc", "%time", "%alloc");
687
688 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
689 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
690 #if defined(PROFILING_DETAIL_COUNTS)
691 fprintf(prof_file, " %8s %8s %8s %8s %8s %8s %8s",
692 "closures", "thunks", "funcs", "PAPs", "subfuns", "subcafs", "cafssub");
693 #endif
694 }
695
696 fprintf(prof_file, "\n\n");
697 }
698
699 void
700 reportCCSProfiling( void )
701 {
702 nat count;
703 char temp[128]; /* sigh: magic constant */
704
705 stopProfTimer();
706
707 total_prof_ticks = 0;
708 total_alloc = 0;
709 count_ticks(CCS_MAIN);
710
711 switch (RtsFlags.CcFlags.doCostCentres) {
712 case 0:
713 return;
714 case COST_CENTRES_XML:
715 gen_XML_logfile();
716 return;
717 default:
718 break;
719 }
720
721 fprintf(prof_file, "\t%s Time and Allocation Profiling Report (%s)\n",
722 time_str(), "Final");
723
724 fprintf(prof_file, "\n\t ");
725 fprintf(prof_file, " %s", prog_name);
726 fprintf(prof_file, " +RTS");
727 for (count = 0; rts_argv[count]; count++)
728 fprintf(prof_file, " %s", rts_argv[count]);
729 fprintf(prof_file, " -RTS");
730 for (count = 1; prog_argv[count]; count++)
731 fprintf(prof_file, " %s", prog_argv[count]);
732 fprintf(prof_file, "\n\n");
733
734 fprintf(prof_file, "\ttotal time = %11.2f secs (%lu ticks @ %d ms)\n",
735 total_prof_ticks / (StgFloat) TICK_FREQUENCY,
736 total_prof_ticks, TICK_MILLISECS);
737
738 fprintf(prof_file, "\ttotal alloc = %11s bytes",
739 ullong_format_string(total_alloc * sizeof(W_),
740 temp, rtsTrue/*commas*/));
741
742 #if defined(PROFILING_DETAIL_COUNTS)
743 fprintf(prof_file, " (%lu closures)", total_allocs);
744 #endif
745 fprintf(prof_file, " (excludes profiling overheads)\n\n");
746
747 report_per_cc_costs();
748
749 inherit_costs(CCS_MAIN);
750
751 fprint_header();
752 reportCCS(pruneCCSTree(CCS_MAIN), 0);
753 }
754
755 static void
756 reportCCS(CostCentreStack *ccs, nat indent)
757 {
758 CostCentre *cc;
759 IndexTable *i;
760
761 cc = ccs->cc;
762
763 /* Only print cost centres with non 0 data ! */
764
765 if ( RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL ||
766 ! ccs_to_ignore(ccs))
767 /* force printing of *all* cost centres if -P -P */
768 {
769
770 fprintf(prof_file, "%-*s%-*s %-50s",
771 indent, "", 24-indent, cc->label, cc->module);
772
773 fprintf(prof_file, "%6ld %11.0f %5.1f %5.1f %5.1f %5.1f",
774 ccs->ccsID, (double) ccs->scc_count,
775 total_prof_ticks == 0 ? 0.0 : ((double)ccs->time_ticks / (double)total_prof_ticks * 100.0),
776 total_alloc == 0 ? 0.0 : ((double)ccs->mem_alloc / (double)total_alloc * 100.0),
777 total_prof_ticks == 0 ? 0.0 : ((double)ccs->inherited_ticks / (double)total_prof_ticks * 100.0),
778 total_alloc == 0 ? 0.0 : ((double)ccs->inherited_alloc / (double)total_alloc * 100.0)
779 );
780
781 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
782 fprintf(prof_file, " %5" FMT_Word64 " %9" FMT_Word64,
783 (StgWord64)(ccs->time_ticks), ccs->mem_alloc*sizeof(W_));
784 #if defined(PROFILING_DETAIL_COUNTS)
785 fprintf(prof_file, " %8ld %8ld %8ld %8ld %8ld %8ld %8ld",
786 ccs->mem_allocs, ccs->thunk_count,
787 ccs->function_count, ccs->pap_count,
788 ccs->subsumed_fun_count, ccs->subsumed_caf_count,
789 ccs->caffun_subsumed);
790 #endif
791 }
792 fprintf(prof_file, "\n");
793 }
794
795 for (i = ccs->indexTable; i != 0; i = i->next) {
796 if (!i->back_edge) {
797 reportCCS(i->ccs, indent+1);
798 }
799 }
800 }
801
802
803 /* Traverse the cost centre stack tree and accumulate
804 * ticks/allocations.
805 */
806 static void
807 count_ticks(CostCentreStack *ccs)
808 {
809 IndexTable *i;
810
811 if (!ccs_to_ignore(ccs)) {
812 total_alloc += ccs->mem_alloc;
813 total_prof_ticks += ccs->time_ticks;
814 }
815 for (i = ccs->indexTable; i != NULL; i = i->next)
816 if (!i->back_edge) {
817 count_ticks(i->ccs);
818 }
819 }
820
821 /* Traverse the cost centre stack tree and inherit ticks & allocs.
822 */
823 static void
824 inherit_costs(CostCentreStack *ccs)
825 {
826 IndexTable *i;
827
828 if (ccs_to_ignore(ccs)) { return; }
829
830 ccs->inherited_ticks += ccs->time_ticks;
831 ccs->inherited_alloc += ccs->mem_alloc;
832
833 for (i = ccs->indexTable; i != NULL; i = i->next)
834 if (!i->back_edge) {
835 inherit_costs(i->ccs);
836 ccs->inherited_ticks += i->ccs->inherited_ticks;
837 ccs->inherited_alloc += i->ccs->inherited_alloc;
838 }
839
840 return;
841 }
842
843 static CostCentreStack *
844 pruneCCSTree( CostCentreStack *ccs )
845 {
846 CostCentreStack *ccs1;
847 IndexTable *i, **prev;
848
849 prev = &ccs->indexTable;
850 for (i = ccs->indexTable; i != 0; i = i->next) {
851 if (i->back_edge) { continue; }
852
853 ccs1 = pruneCCSTree(i->ccs);
854 if (ccs1 == NULL) {
855 *prev = i->next;
856 } else {
857 prev = &(i->next);
858 }
859 }
860
861 if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
862 /* force printing of *all* cost centres if -P -P */ )
863
864 || ( ccs->indexTable != 0 )
865 || ( ccs->scc_count || ccs->time_ticks || ccs->mem_alloc )
866 ) {
867 return ccs;
868 } else {
869 return NULL;
870 }
871 }
872
873 /* -----------------------------------------------------------------------------
874 Generate the XML time/allocation profile
875 -------------------------------------------------------------------------- */
876
877 void
878 gen_XML_logfile( void )
879 {
880 fprintf(prof_file, "%d %lu", TIME_UPD_UQ, total_prof_ticks);
881
882 reportCCS_XML(pruneCCSTree(CCS_MAIN));
883
884 fprintf(prof_file, " 0\n");
885 }
886
887 static void
888 reportCCS_XML(CostCentreStack *ccs)
889 {
890 CostCentre *cc;
891 IndexTable *i;
892
893 if (ccs_to_ignore(ccs)) { return; }
894
895 cc = ccs->cc;
896
897 fprintf(prof_file, " 1 %ld %" FMT_Word64 " %" FMT_Word64 " %" FMT_Word64,
898 ccs->ccsID, ccs->scc_count, (StgWord64)(ccs->time_ticks), ccs->mem_alloc);
899
900 for (i = ccs->indexTable; i != 0; i = i->next) {
901 if (!i->back_edge) {
902 reportCCS_XML(i->ccs);
903 }
904 }
905 }
906
907 void
908 fprintCCS( FILE *f, CostCentreStack *ccs )
909 {
910 fprintf(f,"<");
911 for (; ccs && ccs != CCS_MAIN; ccs = ccs->prevStack ) {
912 fprintf(f,"%s.%s", ccs->cc->module, ccs->cc->label);
913 if (ccs->prevStack && ccs->prevStack != CCS_MAIN) {
914 fprintf(f,",");
915 }
916 }
917 fprintf(f,">");
918 }
919
920 /* For calling from .cmm code, where we can't reliably refer to stderr */
921 void
922 fprintCCS_stderr( CostCentreStack *ccs )
923 {
924 fprintCCS(stderr, ccs);
925 }
926
927 #ifdef DEBUG
928 void
929 debugCCS( CostCentreStack *ccs )
930 {
931 debugBelch("<");
932 for (; ccs && ccs != CCS_MAIN; ccs = ccs->prevStack ) {
933 debugBelch("%s.%s", ccs->cc->module, ccs->cc->label);
934 if (ccs->prevStack && ccs->prevStack != CCS_MAIN) {
935 debugBelch(",");
936 }
937 }
938 debugBelch(">");
939 }
940 #endif /* DEBUG */
941
942 #endif /* PROFILING */