1 /* -----------------------------------------------------------------------------
2 * $Id: Profiling.c,v 1.20 2000/05/12 13:01:04 simonmar 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 );
139 static void printCCS ( CostCentreStack *ccs );
141 static void initTimeProfiling ( void );
142 static void initProfilingLogFile( void );
144 static void reportCCS_XML ( CostCentreStack *ccs );
146 /* -----------------------------------------------------------------------------
147 Initialise the profiling environment
148 -------------------------------------------------------------------------- */
151 initProfiling1 (void)
153 /* for the benefit of allocate()... */
156 /* Initialize counters for IDs */
161 /* Initialize Declaration lists to NULL */
165 /* Register all the cost centres / stacks in the program
166 * CC_MAIN gets link = 0, all others have non-zero link.
168 REGISTER_CC(CC_MAIN);
169 REGISTER_CC(CC_SYSTEM);
171 REGISTER_CC(CC_OVERHEAD);
172 REGISTER_CC(CC_SUBSUMED);
173 REGISTER_CC(CC_DONT_CARE);
174 REGISTER_CCS(CCS_MAIN);
175 REGISTER_CCS(CCS_SYSTEM);
176 REGISTER_CCS(CCS_GC);
177 REGISTER_CCS(CCS_OVERHEAD);
178 REGISTER_CCS(CCS_SUBSUMED);
179 REGISTER_CCS(CCS_DONT_CARE);
183 /* cost centres are registered by the per-module
184 * initialisation code now...
189 initProfiling2 (void)
191 CostCentreStack *ccs, *next;
195 /* Set up the log file, and dump the header and cost centre
196 * information into it. */
197 initProfilingLogFile();
199 /* find all the "special" cost centre stacks, and make them children
202 ASSERT(CCS_MAIN->prevStack == 0);
203 CCS_MAIN->root = CC_MAIN;
205 for (ccs = CCS_LIST; ccs != CCS_MAIN; ) {
206 next = ccs->prevStack;
208 ActualPush_(CCS_MAIN,ccs->cc,ccs);
213 if (RtsFlags.CcFlags.doCostCentres) {
217 if (RtsFlags.ProfFlags.doHeapProfile) {
223 initProfilingLogFile(void)
225 /* Initialise the log file name */
226 prof_filename = stgMallocBytes(strlen(prog_argv[0]) + 6, "initProfiling");
227 sprintf(prof_filename, "%s.prof", prog_argv[0]);
229 /* open the log file */
230 if ((prof_file = fopen(prof_filename, "w")) == NULL) {
231 fprintf(stderr, "Can't open profiling report file %s\n", prof_filename);
232 RtsFlags.CcFlags.doCostCentres = 0;
236 if (RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
237 /* dump the time, and the profiling interval */
238 fprintf(prof_file, "\"%s\"\n", time_str());
239 fprintf(prof_file, "\"%d ms\"\n", TICK_MILLISECS);
241 /* declare all the cost centres */
244 for (cc = CC_LIST; cc != NULL; cc = cc->link) {
245 fprintf(prof_file, "%d %d \"%s\" \"%s\"\n",
246 CC_UQ, cc->ccID, cc->label, cc->module);
251 if (RtsFlags.ProfFlags.doHeapProfile) {
252 /* Initialise the log file name */
253 hp_filename = stgMallocBytes(strlen(prog_argv[0]) + 6, "initProfiling");
254 sprintf(hp_filename, "%s.hp", prog_argv[0]);
256 /* open the log file */
257 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
258 fprintf(stderr, "Can't open profiling report file %s\n",
260 RtsFlags.ProfFlags.doHeapProfile = 0;
267 initTimeProfiling(void)
269 time_profiling = rtsTrue;
276 endProfiling ( void )
278 if (RtsFlags.CcFlags.doCostCentres) {
281 if (RtsFlags.ProfFlags.doHeapProfile) {
286 /* -----------------------------------------------------------------------------
287 Set cost centre stack when entering a function.
288 -------------------------------------------------------------------------- */
289 rtsBool entering_PAP;
292 EnterFunCCS ( CostCentreStack *cccs, CostCentreStack *ccsfn )
294 /* PAP_entry has already set CCCS for us */
296 entering_PAP = rtsFalse;
300 if (ccsfn->root->is_caf == CC_IS_CAF) {
301 return AppendCCS(cccs,ccsfn);
307 /* -----------------------------------------------------------------------------
308 Cost-centre stack manipulation
309 -------------------------------------------------------------------------- */
312 CostCentreStack * _PushCostCentre ( CostCentreStack *ccs, CostCentre *cc );
314 PushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
315 #define PushCostCentre _PushCostCentre
318 fprintf(stderr,"Pushing %s on ", cc->label);
320 fprintf(stderr,"\n"));
321 return PushCostCentre(ccs,cc);
326 PushCostCentre ( CostCentreStack *ccs, CostCentre *cc )
328 CostCentreStack *temp_ccs;
330 if (ccs == EMPTY_STACK)
331 return ActualPush(ccs,cc);
336 /* check if we've already memoized this stack */
337 temp_ccs = IsInIndexTable(ccs->indexTable,cc);
339 if (temp_ccs != EMPTY_STACK)
342 temp_ccs = CheckLoop(ccs,cc);
343 if (temp_ccs != NULL) {
344 /* we have recursed to an older CCS. Mark this in
345 * the index table, and emit a "back edge" into the
348 ccs->indexTable = AddToIndexTable(ccs->indexTable,temp_ccs,cc,1);
349 DecBackEdge(temp_ccs,ccs);
352 return ActualPush(ccs,cc);
359 static CostCentreStack *
360 CheckLoop ( CostCentreStack *ccs, CostCentre *cc )
362 while (ccs != EMPTY_STACK) {
365 ccs = ccs->prevStack;
370 /* Append ccs1 to ccs2 (ignoring any CAF cost centre at the root of ccs1 */
373 CostCentreStack *_AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
375 AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
376 #define AppendCCS _AppendCCS
380 fprintf(stderr,"Appending ");
382 fprintf(stderr," to ");
384 fprintf(stderr,"\n");});
385 return AppendCCS(ccs1,ccs2);
390 AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 )
392 CostCentreStack *ccs = NULL;
398 if (ccs2->cc->is_caf == CC_IS_CAF) {
402 if (ccs2->prevStack != NULL) {
403 ccs = AppendCCS(ccs1, ccs2->prevStack);
406 return PushCostCentre(ccs,ccs2->cc);
409 static CostCentreStack *
410 ActualPush ( CostCentreStack *ccs, CostCentre *cc )
412 CostCentreStack *new_ccs;
414 /* allocate space for a new CostCentreStack */
415 new_ccs = (CostCentreStack *) stgMallocBytes(sizeof(CostCentreStack), "Error allocating space for CostCentreStack");
417 return ActualPush_(ccs, cc, new_ccs);
420 static CostCentreStack *
421 ActualPush_ ( CostCentreStack *ccs, CostCentre *cc, CostCentreStack *new_ccs )
423 /* assign values to each member of the structure */
424 ASSIGN_CCS_ID(new_ccs->ccsID);
427 new_ccs->prevStack = ccs;
429 new_ccs->indexTable = EMPTY_TABLE;
431 /* Initialise the various _scc_ counters to zero
433 new_ccs->scc_count = 0;
435 /* Initialize all other stats here. There should be a quick way
436 * that's easily used elsewhere too
438 new_ccs->time_ticks = 0;
439 new_ccs->mem_alloc = 0;
440 new_ccs->inherited_ticks = 0;
441 new_ccs->inherited_alloc = 0;
443 new_ccs->root = ccs->root;
445 /* update the memoization table for the parent stack */
446 if (ccs != EMPTY_STACK)
447 ccs->indexTable = AddToIndexTable(ccs->indexTable, new_ccs, cc,
448 0/*not a back edge*/);
450 /* make sure this CC is declared at the next heap/time sample */
453 /* return a pointer to the new stack */
458 static CostCentreStack *
459 IsInIndexTable(IndexTable *it, CostCentre *cc)
461 while (it!=EMPTY_TABLE)
469 /* otherwise we never found it so return EMPTY_TABLE */
475 AddToIndexTable(IndexTable *it, CostCentreStack *new_ccs,
476 CostCentre *cc, unsigned int back_edge)
480 new_it = stgMallocBytes(sizeof(IndexTable), "AddToIndexTable");
483 new_it->ccs = new_ccs;
485 new_it->back_edge = back_edge;
491 DecCCS(CostCentreStack *ccs)
493 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
494 if (ccs->prevStack == EMPTY_STACK)
495 fprintf(prof_file, "%d %d 1 %d\n", CCS_UQ,
496 ccs->ccsID, ccs->cc->ccID);
498 fprintf(prof_file, "%d %d 2 %d %d\n", CCS_UQ,
499 ccs->ccsID, ccs->cc->ccID, ccs->prevStack->ccsID);
504 DecBackEdge( CostCentreStack *ccs, CostCentreStack *oldccs )
506 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
507 if (ccs->prevStack == EMPTY_STACK)
508 fprintf(prof_file, "%d %d 1 %d\n", CCS_UQ,
509 ccs->ccsID, ccs->cc->ccID);
511 fprintf(prof_file, "%d %d 2 %d %d\n", CCS_UQ,
512 ccs->ccsID, ccs->cc->ccID, oldccs->ccsID);
516 /* -----------------------------------------------------------------------------
517 Generating a time & allocation profiling report.
518 -------------------------------------------------------------------------- */
520 /* -----------------------------------------------------------------------------
521 Generating the aggregated per-cost-centre time/alloc report.
522 -------------------------------------------------------------------------- */
524 static CostCentre *sorted_cc_list;
527 aggregate_cc_costs( CostCentreStack *ccs )
531 ccs->cc->mem_alloc += ccs->mem_alloc;
532 ccs->cc->time_ticks += ccs->time_ticks;
534 for (i = ccs->indexTable; i != 0; i = i->next) {
536 aggregate_cc_costs(i->ccs);
542 insert_cc_in_sorted_list( CostCentre *new_cc )
544 CostCentre **prev, *cc;
546 prev = &sorted_cc_list;
547 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
548 if (new_cc->time_ticks > cc->time_ticks) {
561 report_per_cc_costs( void )
563 CostCentre *cc, *next;
565 aggregate_cc_costs(CCS_MAIN);
566 sorted_cc_list = NULL;
568 for (cc = CC_LIST; cc != NULL; cc = next) {
570 if (cc->time_ticks > total_prof_ticks/100
571 || cc->mem_alloc > total_alloc/100) {
572 insert_cc_in_sorted_list(cc);
576 fprintf(prof_file, "%-20s %-10s", "COST CENTRE", "MODULE");
577 fprintf(prof_file, "%6s %6s", "%time", "%alloc");
578 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
579 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
581 fprintf(prof_file, "\n\n");
583 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
584 fprintf(prof_file, "%-20s %-10s", cc->label, cc->module);
585 fprintf(prof_file, "%6.1f %6.1f",
586 total_prof_ticks == 0 ? 0.0 : (cc->time_ticks / (StgFloat) total_prof_ticks * 100),
587 total_alloc == 0 ? 0.0 : (cc->mem_alloc / (StgFloat)
591 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
592 fprintf(prof_file, " %5ld %9ld", cc->time_ticks, cc->mem_alloc);
594 fprintf(prof_file, "\n");
597 fprintf(prof_file,"\n\n");
600 /* -----------------------------------------------------------------------------
601 Generate the cost-centre-stack time/alloc report
602 -------------------------------------------------------------------------- */
605 fprint_header( void )
607 fprintf(prof_file, "%-24s %-10s individual inherited\n", "", "");
609 fprintf(prof_file, "%-24s %-10s", "COST CENTRE", "MODULE");
610 fprintf(prof_file, "%8s %5s %5s %5s %5s", "entries", "%time", "%alloc", "%time", "%alloc");
612 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
613 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
614 #if defined(PROFILING_DETAIL_COUNTS)
615 fprintf(prof_file, " %8s %8s %8s %8s %8s %8s %8s",
616 "closures", "thunks", "funcs", "PAPs", "subfuns", "subcafs", "cafssub");
620 fprintf(prof_file, "\n\n");
624 report_ccs_profiling( void )
627 char temp[128]; /* sigh: magic constant */
631 total_prof_ticks = 0;
633 count_ticks(CCS_MAIN);
635 switch (RtsFlags.CcFlags.doCostCentres) {
638 case COST_CENTRES_XML:
644 fprintf(prof_file, "\t%s Time and Allocation Profiling Report (%s)\n",
645 time_str(), "Final");
647 fprintf(prof_file, "\n\t ");
648 fprintf(prof_file, " %s", prog_argv[0]);
649 fprintf(prof_file, " +RTS");
650 for (count = 0; rts_argv[count]; count++)
651 fprintf(prof_file, " %s", rts_argv[count]);
652 fprintf(prof_file, " -RTS");
653 for (count = 1; prog_argv[count]; count++)
654 fprintf(prof_file, " %s", prog_argv[count]);
655 fprintf(prof_file, "\n\n");
657 fprintf(prof_file, "\ttotal time = %11.2f secs (%lu ticks @ %d ms)\n",
658 total_prof_ticks / (StgFloat) TICK_FREQUENCY,
659 total_prof_ticks, TICK_MILLISECS);
661 fprintf(prof_file, "\ttotal alloc = %11s bytes",
662 ullong_format_string((ullong) total_alloc * sizeof(W_),
663 temp, rtsTrue/*commas*/));
664 /* ToDo: 64-bit error! */
666 #if defined(PROFILING_DETAIL_COUNTS)
667 fprintf(prof_file, " (%lu closures)", total_allocs);
669 fprintf(prof_file, " (excludes profiling overheads)\n\n");
671 report_per_cc_costs();
673 inherit_costs(CCS_MAIN);
676 reportCCS(pruneCCSTree(CCS_MAIN), 0);
682 reportCCS(CostCentreStack *ccs, nat indent)
689 /* Only print cost centres with non 0 data ! */
691 if ( RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL ||
692 ! ccs_to_ignore(ccs))
693 /* force printing of *all* cost centres if -P -P */
696 fprintf(prof_file, "%-*s%-*s %-10s",
697 indent, "", 24-indent, cc->label, cc->module);
699 fprintf(prof_file, "%8ld %5.1f %5.1f %5.1f %5.1f",
701 total_prof_ticks == 0 ? 0.0 : (ccs->time_ticks / (StgFloat) total_prof_ticks * 100),
702 total_alloc == 0 ? 0.0 : (ccs->mem_alloc / (StgFloat) total_alloc * 100),
703 total_prof_ticks == 0 ? 0.0 : (ccs->inherited_ticks / (StgFloat) total_prof_ticks * 100),
704 total_alloc == 0 ? 0.0 : (ccs->inherited_alloc / (StgFloat) total_alloc * 100)
707 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
708 fprintf(prof_file, " %5ld %9ld", ccs->time_ticks, ccs->mem_alloc*sizeof(W_));
709 #if defined(PROFILING_DETAIL_COUNTS)
710 fprintf(prof_file, " %8ld %8ld %8ld %8ld %8ld %8ld %8ld",
711 ccs->mem_allocs, ccs->thunk_count,
712 ccs->function_count, ccs->pap_count,
713 ccs->subsumed_fun_count, ccs->subsumed_caf_count,
714 ccs->caffun_subsumed);
717 fprintf(prof_file, "\n");
720 for (i = ccs->indexTable; i != 0; i = i->next) {
722 reportCCS(i->ccs, indent+1);
727 /* Traverse the cost centre stack tree and accumulate
731 count_ticks(CostCentreStack *ccs)
735 if (!ccs_to_ignore(ccs)) {
736 total_alloc += ccs->mem_alloc;
737 total_prof_ticks += ccs->time_ticks;
739 for (i = ccs->indexTable; i != NULL; i = i->next)
745 /* Traverse the cost centre stack tree and inherit ticks & allocs.
748 inherit_costs(CostCentreStack *ccs)
752 if (ccs_to_ignore(ccs)) { return; }
754 ccs->inherited_ticks += ccs->time_ticks;
755 ccs->inherited_alloc += ccs->mem_alloc;
757 for (i = ccs->indexTable; i != NULL; i = i->next)
759 inherit_costs(i->ccs);
760 ccs->inherited_ticks += i->ccs->inherited_ticks;
761 ccs->inherited_alloc += i->ccs->inherited_alloc;
767 /* return rtsTrue if it is one of the ones that
768 * should not be reported normally (because it confuses
772 ccs_to_ignore (CostCentreStack *ccs)
774 if ( ccs == CCS_OVERHEAD
775 || ccs == CCS_DONT_CARE
777 || ccs == CCS_SYSTEM) {
784 static CostCentreStack *
785 pruneCCSTree( CostCentreStack *ccs )
787 CostCentreStack *ccs1;
788 IndexTable *i, **prev;
790 prev = &ccs->indexTable;
791 for (i = ccs->indexTable; i != 0; i = i->next) {
792 if (i->back_edge) { continue; }
794 ccs1 = pruneCCSTree(i->ccs);
802 if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
803 /* force printing of *all* cost centres if -P -P */ )
805 || ( ccs->indexTable != 0 )
806 || ( ccs->scc_count || ccs->time_ticks || ccs->mem_alloc )
814 /* -----------------------------------------------------------------------------
815 Generate the XML time/allocation profile
816 -------------------------------------------------------------------------- */
819 gen_XML_logfile( void )
821 fprintf(prof_file, "%d %lu", TIME_UPD_UQ, total_prof_ticks);
823 reportCCS_XML(pruneCCSTree(CCS_MAIN));
825 fprintf(prof_file, " 0\n");
831 reportCCS_XML(CostCentreStack *ccs)
836 if (ccs_to_ignore(ccs)) { return; }
840 fprintf(prof_file, " 1 %d %lu %lu %lu",
841 ccs->ccsID, ccs->scc_count, ccs->time_ticks, ccs->mem_alloc);
843 for (i = ccs->indexTable; i != 0; i = i->next) {
845 reportCCS_XML(i->ccs);
851 print_ccs (FILE *fp, CostCentreStack *ccs)
854 fprintf(fp, "Cost-Centre Stack: ");
859 print_ccs(fp, ccs->prevStack);
860 fprintf(fp, "->[%s,%s]", ccs->cc->label, ccs->cc->module);
862 fprintf(fp, "[%s,%s]", ccs->cc->label, ccs->cc->module);
873 printCCS ( CostCentreStack *ccs )
876 for (; ccs; ccs = ccs->prevStack ) {
877 fprintf(stderr,ccs->cc->label);
878 if (ccs->prevStack) {
886 #endif /* PROFILING */