1 /* -----------------------------------------------------------------------------
2 * $Id: Profiling.c,v 1.21 2001/07/23 23:37:35 andy Exp $
4 * (c) The GHC Team, 1998-2000
6 * Support for profiling
8 * ---------------------------------------------------------------------------*/
15 #include "Profiling.h"
17 #include "Proftimer.h"
22 * Global variables used to assign unique IDs to cc's, ccs's, and
30 /* Table sizes from old profiling system. Not sure if we'll need
33 nat time_intervals = 0;
34 nat earlier_ticks = 0;
41 /* Are we time-profiling?
43 rtsBool time_profiling = rtsFalse;
45 /* figures for the profiling report.
47 static lnat total_alloc, total_prof_ticks;
49 /* Globals for opening the profiling log file(s)
51 static char *prof_filename; /* prof report file name = <program>.prof */
54 static char *hp_filename; /* heap profile (hp2ps style) log file */
57 /* The Current Cost Centre Stack (for attributing costs)
59 CostCentreStack *CCCS;
61 /* Linked lists to keep track of cc's and ccs's that haven't
62 * been declared in the log file yet
65 CostCentreStack *CCS_LIST;
68 * Built-in cost centres and cost-centre stacks:
70 * MAIN is the root of the cost-centre stack tree. If there are
71 * no _scc_s in the program, all costs will be attributed
74 * SYSTEM is the RTS in general (scheduler, etc.). All costs for
75 * RTS operations apart from garbage collection are attributed
78 * GC is the storage manager / garbage collector.
80 * OVERHEAD gets all costs generated by the profiling system
81 * itself. These are costs that would not be incurred
82 * during non-profiled execution of the program.
84 * SUBSUMED is the one-and-only CCS placed on top-level functions.
85 * It indicates that all costs are to be attributed to the
86 * enclosing cost centre stack. SUBSUMED never accumulates
87 * any costs. The is_caf flag is set on the subsumed cost
90 * DONT_CARE is a placeholder cost-centre we assign to static
91 * constructors. It should *never* accumulate any costs.
94 CC_DECLARE(CC_MAIN, "MAIN", "MAIN", CC_IS_BORING, );
95 CC_DECLARE(CC_SYSTEM, "SYSTEM", "MAIN", CC_IS_BORING, );
96 CC_DECLARE(CC_GC, "GC", "GC", CC_IS_BORING, );
97 CC_DECLARE(CC_OVERHEAD, "OVERHEAD_of", "PROFILING", CC_IS_CAF, );
98 CC_DECLARE(CC_SUBSUMED, "SUBSUMED", "MAIN", CC_IS_CAF, );
99 CC_DECLARE(CC_DONT_CARE, "DONT_CARE", "MAIN", CC_IS_BORING, );
101 CCS_DECLARE(CCS_MAIN, CC_MAIN, );
102 CCS_DECLARE(CCS_SYSTEM, CC_SYSTEM, );
103 CCS_DECLARE(CCS_GC, CC_GC, );
104 CCS_DECLARE(CCS_OVERHEAD, CC_OVERHEAD, );
105 CCS_DECLARE(CCS_SUBSUMED, CC_SUBSUMED, );
106 CCS_DECLARE(CCS_DONT_CARE, CC_DONT_CARE, );
109 * Uniques for the XML log-file format
114 #define HEAP_OBJ_UQ 4
115 #define TIME_UPD_UQ 5
116 #define HEAP_UPD_UQ 6
122 static CostCentreStack * ActualPush_ ( CostCentreStack *ccs, CostCentre *cc,
123 CostCentreStack *new_ccs );
124 static rtsBool ccs_to_ignore ( CostCentreStack *ccs );
125 static void count_ticks ( CostCentreStack *ccs );
126 static void inherit_costs ( CostCentreStack *ccs );
127 static void reportCCS ( CostCentreStack *ccs, nat indent );
128 static void DecCCS ( CostCentreStack *ccs );
129 static void DecBackEdge ( CostCentreStack *ccs,
130 CostCentreStack *oldccs );
131 static CostCentreStack * CheckLoop ( CostCentreStack *ccs, CostCentre *cc );
132 static CostCentreStack * pruneCCSTree ( CostCentreStack *ccs );
133 static CostCentreStack * ActualPush ( CostCentreStack *, CostCentre * );
134 static CostCentreStack * IsInIndexTable ( IndexTable *, CostCentre * );
135 static IndexTable * AddToIndexTable ( IndexTable *, CostCentreStack *,
136 CostCentre *, unsigned int );
141 static void printCCS ( CostCentreStack *ccs );
143 static void initTimeProfiling ( void );
144 static void initProfilingLogFile( void );
146 static void reportCCS_XML ( CostCentreStack *ccs );
148 /* -----------------------------------------------------------------------------
149 Initialise the profiling environment
150 -------------------------------------------------------------------------- */
153 initProfiling1 (void)
155 /* for the benefit of allocate()... */
158 /* Initialize counters for IDs */
163 /* Initialize Declaration lists to NULL */
167 /* Register all the cost centres / stacks in the program
168 * CC_MAIN gets link = 0, all others have non-zero link.
170 REGISTER_CC(CC_MAIN);
171 REGISTER_CC(CC_SYSTEM);
173 REGISTER_CC(CC_OVERHEAD);
174 REGISTER_CC(CC_SUBSUMED);
175 REGISTER_CC(CC_DONT_CARE);
176 REGISTER_CCS(CCS_MAIN);
177 REGISTER_CCS(CCS_SYSTEM);
178 REGISTER_CCS(CCS_GC);
179 REGISTER_CCS(CCS_OVERHEAD);
180 REGISTER_CCS(CCS_SUBSUMED);
181 REGISTER_CCS(CCS_DONT_CARE);
185 /* cost centres are registered by the per-module
186 * initialisation code now...
191 initProfiling2 (void)
193 CostCentreStack *ccs, *next;
197 /* Set up the log file, and dump the header and cost centre
198 * information into it. */
199 initProfilingLogFile();
201 /* find all the "special" cost centre stacks, and make them children
204 ASSERT(CCS_MAIN->prevStack == 0);
205 CCS_MAIN->root = CC_MAIN;
207 for (ccs = CCS_LIST; ccs != CCS_MAIN; ) {
208 next = ccs->prevStack;
210 ActualPush_(CCS_MAIN,ccs->cc,ccs);
215 if (RtsFlags.CcFlags.doCostCentres) {
219 if (RtsFlags.ProfFlags.doHeapProfile) {
225 initProfilingLogFile(void)
227 /* Initialise the log file name */
228 prof_filename = stgMallocBytes(strlen(prog_argv[0]) + 6, "initProfiling");
229 sprintf(prof_filename, "%s.prof", prog_argv[0]);
231 /* open the log file */
232 if ((prof_file = fopen(prof_filename, "w")) == NULL) {
233 fprintf(stderr, "Can't open profiling report file %s\n", prof_filename);
234 RtsFlags.CcFlags.doCostCentres = 0;
238 if (RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
239 /* dump the time, and the profiling interval */
240 fprintf(prof_file, "\"%s\"\n", time_str());
241 fprintf(prof_file, "\"%d ms\"\n", TICK_MILLISECS);
243 /* declare all the cost centres */
246 for (cc = CC_LIST; cc != NULL; cc = cc->link) {
247 fprintf(prof_file, "%d %d \"%s\" \"%s\"\n",
248 CC_UQ, cc->ccID, cc->label, cc->module);
253 if (RtsFlags.ProfFlags.doHeapProfile) {
254 /* Initialise the log file name */
255 hp_filename = stgMallocBytes(strlen(prog_argv[0]) + 6, "initProfiling");
256 sprintf(hp_filename, "%s.hp", prog_argv[0]);
258 /* open the log file */
259 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
260 fprintf(stderr, "Can't open profiling report file %s\n",
262 RtsFlags.ProfFlags.doHeapProfile = 0;
269 initTimeProfiling(void)
271 time_profiling = rtsTrue;
278 endProfiling ( void )
280 if (RtsFlags.CcFlags.doCostCentres) {
283 if (RtsFlags.ProfFlags.doHeapProfile) {
288 /* -----------------------------------------------------------------------------
289 Set cost centre stack when entering a function.
290 -------------------------------------------------------------------------- */
291 rtsBool entering_PAP;
294 EnterFunCCS ( CostCentreStack *cccs, CostCentreStack *ccsfn )
296 /* PAP_entry has already set CCCS for us */
298 entering_PAP = rtsFalse;
302 if (ccsfn->root->is_caf == CC_IS_CAF) {
303 return AppendCCS(cccs,ccsfn);
309 /* -----------------------------------------------------------------------------
310 Cost-centre stack manipulation
311 -------------------------------------------------------------------------- */
314 CostCentreStack * _PushCostCentre ( CostCentreStack *ccs, CostCentre *cc );
316 PushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
317 #define PushCostCentre _PushCostCentre
320 fprintf(stderr,"Pushing %s on ", cc->label);
322 fprintf(stderr,"\n"));
323 return PushCostCentre(ccs,cc);
328 PushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
330 CostCentreStack *temp_ccs;
332 if (ccs == EMPTY_STACK)
333 return ActualPush(ccs,cc);
338 /* check if we've already memoized this stack */
339 temp_ccs = IsInIndexTable(ccs->indexTable,cc);
341 if (temp_ccs != EMPTY_STACK)
344 temp_ccs = CheckLoop(ccs,cc);
345 if (temp_ccs != NULL) {
346 /* we have recursed to an older CCS. Mark this in
347 * the index table, and emit a "back edge" into the
350 ccs->indexTable = AddToIndexTable(ccs->indexTable,temp_ccs,cc,1);
351 DecBackEdge(temp_ccs,ccs);
354 return ActualPush(ccs,cc);
361 static CostCentreStack *
362 CheckLoop ( CostCentreStack *ccs, CostCentre *cc )
364 while (ccs != EMPTY_STACK) {
367 ccs = ccs->prevStack;
372 /* Append ccs1 to ccs2 (ignoring any CAF cost centre at the root of ccs1 */
375 CostCentreStack *_AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
377 AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
378 #define AppendCCS _AppendCCS
382 fprintf(stderr,"Appending ");
384 fprintf(stderr," to ");
386 fprintf(stderr,"\n");});
387 return AppendCCS(ccs1,ccs2);
392 AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
394 CostCentreStack *ccs = NULL;
400 if (ccs2->cc->is_caf == CC_IS_CAF) {
404 if (ccs2->prevStack != NULL) {
405 ccs = AppendCCS(ccs1, ccs2->prevStack);
408 return PushCostCentre(ccs,ccs2->cc);
411 static CostCentreStack *
412 ActualPush ( CostCentreStack *ccs, CostCentre *cc )
414 CostCentreStack *new_ccs;
416 /* allocate space for a new CostCentreStack */
417 new_ccs = (CostCentreStack *) stgMallocBytes(sizeof(CostCentreStack), "Error allocating space for CostCentreStack");
419 return ActualPush_(ccs, cc, new_ccs);
422 static CostCentreStack *
423 ActualPush_ ( CostCentreStack *ccs, CostCentre *cc, CostCentreStack *new_ccs )
425 /* assign values to each member of the structure */
426 ASSIGN_CCS_ID(new_ccs->ccsID);
429 new_ccs->prevStack = ccs;
431 new_ccs->indexTable = EMPTY_TABLE;
433 /* Initialise the various _scc_ counters to zero
435 new_ccs->scc_count = 0;
437 /* Initialize all other stats here. There should be a quick way
438 * that's easily used elsewhere too
440 new_ccs->time_ticks = 0;
441 new_ccs->mem_alloc = 0;
442 new_ccs->inherited_ticks = 0;
443 new_ccs->inherited_alloc = 0;
445 new_ccs->root = ccs->root;
447 /* update the memoization table for the parent stack */
448 if (ccs != EMPTY_STACK)
449 ccs->indexTable = AddToIndexTable(ccs->indexTable, new_ccs, cc,
450 0/*not a back edge*/);
452 /* make sure this CC is declared at the next heap/time sample */
455 /* return a pointer to the new stack */
460 static CostCentreStack *
461 IsInIndexTable(IndexTable *it, CostCentre *cc)
463 while (it!=EMPTY_TABLE)
471 /* otherwise we never found it so return EMPTY_TABLE */
477 AddToIndexTable(IndexTable *it, CostCentreStack *new_ccs,
478 CostCentre *cc, unsigned int back_edge)
482 new_it = stgMallocBytes(sizeof(IndexTable), "AddToIndexTable");
485 new_it->ccs = new_ccs;
487 new_it->back_edge = back_edge;
493 DecCCS(CostCentreStack *ccs)
495 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
496 if (ccs->prevStack == EMPTY_STACK)
497 fprintf(prof_file, "%d %d 1 %d\n", CCS_UQ,
498 ccs->ccsID, ccs->cc->ccID);
500 fprintf(prof_file, "%d %d 2 %d %d\n", CCS_UQ,
501 ccs->ccsID, ccs->cc->ccID, ccs->prevStack->ccsID);
506 DecBackEdge( CostCentreStack *ccs, CostCentreStack *oldccs )
508 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
509 if (ccs->prevStack == EMPTY_STACK)
510 fprintf(prof_file, "%d %d 1 %d\n", CCS_UQ,
511 ccs->ccsID, ccs->cc->ccID);
513 fprintf(prof_file, "%d %d 2 %d %d\n", CCS_UQ,
514 ccs->ccsID, ccs->cc->ccID, oldccs->ccsID);
518 /* -----------------------------------------------------------------------------
519 Generating a time & allocation profiling report.
520 -------------------------------------------------------------------------- */
522 /* -----------------------------------------------------------------------------
523 Generating the aggregated per-cost-centre time/alloc report.
524 -------------------------------------------------------------------------- */
526 static CostCentre *sorted_cc_list;
529 aggregate_cc_costs( CostCentreStack *ccs )
533 ccs->cc->mem_alloc += ccs->mem_alloc;
534 ccs->cc->time_ticks += ccs->time_ticks;
536 for (i = ccs->indexTable; i != 0; i = i->next) {
538 aggregate_cc_costs(i->ccs);
544 insert_cc_in_sorted_list( CostCentre *new_cc )
546 CostCentre **prev, *cc;
548 prev = &sorted_cc_list;
549 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
550 if (new_cc->time_ticks > cc->time_ticks) {
563 report_per_cc_costs( void )
565 CostCentre *cc, *next;
567 aggregate_cc_costs(CCS_MAIN);
568 sorted_cc_list = NULL;
570 for (cc = CC_LIST; cc != NULL; cc = next) {
572 if (cc->time_ticks > total_prof_ticks/100
573 || cc->mem_alloc > total_alloc/100
574 || RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL) {
575 insert_cc_in_sorted_list(cc);
579 fprintf(prof_file, "%-20s %-10s", "COST CENTRE", "MODULE");
580 fprintf(prof_file, "%6s %6s", "%time", "%alloc");
581 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
582 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
584 fprintf(prof_file, "\n\n");
586 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
587 fprintf(prof_file, "%-20s %-10s", cc->label, cc->module);
588 fprintf(prof_file, "%6.1f %6.1f",
589 total_prof_ticks == 0 ? 0.0 : (cc->time_ticks / (StgFloat) total_prof_ticks * 100),
590 total_alloc == 0 ? 0.0 : (cc->mem_alloc / (StgFloat)
594 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
595 fprintf(prof_file, " %5ld %9ld", cc->time_ticks, cc->mem_alloc);
597 fprintf(prof_file, "\n");
600 fprintf(prof_file,"\n\n");
603 /* -----------------------------------------------------------------------------
604 Generate the cost-centre-stack time/alloc report
605 -------------------------------------------------------------------------- */
608 fprint_header( void )
610 fprintf(prof_file, "%-24s %-10s individual inherited\n", "", "");
612 fprintf(prof_file, "%-24s %-10s", "COST CENTRE", "MODULE");
613 fprintf(prof_file, "%8s %5s %5s %5s %5s", "entries", "%time", "%alloc", "%time", "%alloc");
615 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
616 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
617 #if defined(PROFILING_DETAIL_COUNTS)
618 fprintf(prof_file, " %8s %8s %8s %8s %8s %8s %8s",
619 "closures", "thunks", "funcs", "PAPs", "subfuns", "subcafs", "cafssub");
623 fprintf(prof_file, "\n\n");
627 report_ccs_profiling( void )
630 char temp[128]; /* sigh: magic constant */
634 total_prof_ticks = 0;
636 count_ticks(CCS_MAIN);
638 switch (RtsFlags.CcFlags.doCostCentres) {
641 case COST_CENTRES_XML:
647 fprintf(prof_file, "\t%s Time and Allocation Profiling Report (%s)\n",
648 time_str(), "Final");
650 fprintf(prof_file, "\n\t ");
651 fprintf(prof_file, " %s", prog_argv[0]);
652 fprintf(prof_file, " +RTS");
653 for (count = 0; rts_argv[count]; count++)
654 fprintf(prof_file, " %s", rts_argv[count]);
655 fprintf(prof_file, " -RTS");
656 for (count = 1; prog_argv[count]; count++)
657 fprintf(prof_file, " %s", prog_argv[count]);
658 fprintf(prof_file, "\n\n");
660 fprintf(prof_file, "\ttotal time = %11.2f secs (%lu ticks @ %d ms)\n",
661 total_prof_ticks / (StgFloat) TICK_FREQUENCY,
662 total_prof_ticks, TICK_MILLISECS);
664 fprintf(prof_file, "\ttotal alloc = %11s bytes",
665 ullong_format_string((ullong) total_alloc * sizeof(W_),
666 temp, rtsTrue/*commas*/));
667 /* ToDo: 64-bit error! */
669 #if defined(PROFILING_DETAIL_COUNTS)
670 fprintf(prof_file, " (%lu closures)", total_allocs);
672 fprintf(prof_file, " (excludes profiling overheads)\n\n");
674 report_per_cc_costs();
676 inherit_costs(CCS_MAIN);
679 reportCCS(pruneCCSTree(CCS_MAIN), 0);
685 reportCCS(CostCentreStack *ccs, nat indent)
692 /* Only print cost centres with non 0 data ! */
694 if ( RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL ||
695 ! ccs_to_ignore(ccs))
696 /* force printing of *all* cost centres if -P -P */
699 fprintf(prof_file, "%-*s%-*s %-10s",
700 indent, "", 24-indent, cc->label, cc->module);
702 fprintf(prof_file, "%8ld %5.1f %5.1f %5.1f %5.1f",
704 total_prof_ticks == 0 ? 0.0 : (ccs->time_ticks / (StgFloat) total_prof_ticks * 100),
705 total_alloc == 0 ? 0.0 : (ccs->mem_alloc / (StgFloat) total_alloc * 100),
706 total_prof_ticks == 0 ? 0.0 : (ccs->inherited_ticks / (StgFloat) total_prof_ticks * 100),
707 total_alloc == 0 ? 0.0 : (ccs->inherited_alloc / (StgFloat) total_alloc * 100)
710 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
711 fprintf(prof_file, " %5ld %9ld", ccs->time_ticks, ccs->mem_alloc*sizeof(W_));
712 #if defined(PROFILING_DETAIL_COUNTS)
713 fprintf(prof_file, " %8ld %8ld %8ld %8ld %8ld %8ld %8ld",
714 ccs->mem_allocs, ccs->thunk_count,
715 ccs->function_count, ccs->pap_count,
716 ccs->subsumed_fun_count, ccs->subsumed_caf_count,
717 ccs->caffun_subsumed);
720 fprintf(prof_file, "\n");
723 for (i = ccs->indexTable; i != 0; i = i->next) {
725 reportCCS(i->ccs, indent+1);
731 /* Traverse the cost centre stack tree and accumulate
735 count_ticks(CostCentreStack *ccs)
739 if (!ccs_to_ignore(ccs)) {
740 total_alloc += ccs->mem_alloc;
741 total_prof_ticks += ccs->time_ticks;
743 for (i = ccs->indexTable; i != NULL; i = i->next)
749 /* Traverse the cost centre stack tree and inherit ticks & allocs.
752 inherit_costs(CostCentreStack *ccs)
756 if (ccs_to_ignore(ccs)) { return; }
758 ccs->inherited_ticks += ccs->time_ticks;
759 ccs->inherited_alloc += ccs->mem_alloc;
761 for (i = ccs->indexTable; i != NULL; i = i->next)
763 inherit_costs(i->ccs);
764 ccs->inherited_ticks += i->ccs->inherited_ticks;
765 ccs->inherited_alloc += i->ccs->inherited_alloc;
771 /* return rtsTrue if it is one of the ones that
772 * should not be reported normally (because it confuses
776 ccs_to_ignore (CostCentreStack *ccs)
778 if ( ccs == CCS_OVERHEAD
779 || ccs == CCS_DONT_CARE
781 || ccs == CCS_SYSTEM) {
788 static CostCentreStack *
789 pruneCCSTree( CostCentreStack *ccs )
791 CostCentreStack *ccs1;
792 IndexTable *i, **prev;
794 prev = &ccs->indexTable;
795 for (i = ccs->indexTable; i != 0; i = i->next) {
796 if (i->back_edge) { continue; }
798 ccs1 = pruneCCSTree(i->ccs);
806 if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
807 /* force printing of *all* cost centres if -P -P */ )
809 || ( ccs->indexTable != 0 )
810 || ( ccs->scc_count || ccs->time_ticks || ccs->mem_alloc )
818 /* -----------------------------------------------------------------------------
819 Generate the XML time/allocation profile
820 -------------------------------------------------------------------------- */
823 gen_XML_logfile( void )
825 fprintf(prof_file, "%d %lu", TIME_UPD_UQ, total_prof_ticks);
827 reportCCS_XML(pruneCCSTree(CCS_MAIN));
829 fprintf(prof_file, " 0\n");
835 reportCCS_XML(CostCentreStack *ccs)
840 if (ccs_to_ignore(ccs)) { return; }
844 fprintf(prof_file, " 1 %d %lu %lu %lu",
845 ccs->ccsID, ccs->scc_count, ccs->time_ticks, ccs->mem_alloc);
847 for (i = ccs->indexTable; i != 0; i = i->next) {
849 reportCCS_XML(i->ccs);
855 print_ccs (FILE *fp, CostCentreStack *ccs)
858 fprintf(fp, "Cost-Centre Stack: ");
863 print_ccs(fp, ccs->prevStack);
864 fprintf(fp, "->[%s,%s]", ccs->cc->label, ccs->cc->module);
866 fprintf(fp, "[%s,%s]", ccs->cc->label, ccs->cc->module);
877 printCCS ( CostCentreStack *ccs )
880 for (; ccs; ccs = ccs->prevStack ) {
881 fprintf(stderr,ccs->cc->label);
882 if (ccs->prevStack) {
890 #endif /* PROFILING */