1 /* -----------------------------------------------------------------------------
2 * $Id: Profiling.c,v 1.19 2000/04/19 12:42:48 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 );
125 static rtsBool ccs_to_ignore ( CostCentreStack *ccs );
126 static void count_ticks ( CostCentreStack *ccs );
127 static void reportCCS ( CostCentreStack *ccs, nat indent );
128 static void DecCCS ( CostCentreStack *ccs );
129 static void DecBackEdge ( CostCentreStack *ccs, CostCentreStack *oldccs );
130 static CostCentreStack *CheckLoop ( CostCentreStack *ccs, CostCentre *cc );
131 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;
434 new_ccs->sub_scc_count = 0;
435 new_ccs->sub_cafcc_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;
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 print_ccs (FILE *fp, CostCentreStack *ccs)
494 fprintf(fp, "Cost-Centre Stack: ");
499 print_ccs(fp, ccs->prevStack);
500 fprintf(fp, "->[%s,%s]", ccs->cc->label, ccs->cc->module);
502 fprintf(fp, "[%s,%s]", ccs->cc->label, ccs->cc->module);
512 DecCCS(CostCentreStack *ccs)
514 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
515 if (ccs->prevStack == EMPTY_STACK)
516 fprintf(prof_file, "%d %d 1 %d\n", CCS_UQ,
517 ccs->ccsID, ccs->cc->ccID);
519 fprintf(prof_file, "%d %d 2 %d %d\n", CCS_UQ,
520 ccs->ccsID, ccs->cc->ccID, ccs->prevStack->ccsID);
525 DecBackEdge( CostCentreStack *ccs, CostCentreStack *oldccs )
527 if (prof_file && RtsFlags.CcFlags.doCostCentres == COST_CENTRES_XML) {
528 if (ccs->prevStack == EMPTY_STACK)
529 fprintf(prof_file, "%d %d 1 %d\n", CCS_UQ,
530 ccs->ccsID, ccs->cc->ccID);
532 fprintf(prof_file, "%d %d 2 %d %d\n", CCS_UQ,
533 ccs->ccsID, ccs->cc->ccID, oldccs->ccsID);
537 /* -----------------------------------------------------------------------------
538 Generating a time & allocation profiling report.
539 -------------------------------------------------------------------------- */
541 /* -----------------------------------------------------------------------------
542 Generating the aggregated per-cost-centre time/alloc report.
543 -------------------------------------------------------------------------- */
545 static CostCentre *sorted_cc_list;
548 aggregate_cc_costs( CostCentreStack *ccs )
552 ccs->cc->mem_alloc += ccs->mem_alloc;
553 ccs->cc->time_ticks += ccs->time_ticks;
555 for (i = ccs->indexTable; i != 0; i = i->next) {
557 aggregate_cc_costs(i->ccs);
563 insert_cc_in_sorted_list( CostCentre *new_cc )
565 CostCentre **prev, *cc;
567 prev = &sorted_cc_list;
568 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
569 if (new_cc->time_ticks > cc->time_ticks) {
582 report_per_cc_costs( void )
584 CostCentre *cc, *next;
586 aggregate_cc_costs(CCS_MAIN);
587 sorted_cc_list = NULL;
589 for (cc = CC_LIST; cc != NULL; cc = next) {
591 if (cc->time_ticks > total_prof_ticks/100
592 || cc->mem_alloc > total_alloc/100) {
593 insert_cc_in_sorted_list(cc);
597 fprintf(prof_file, "%-20s %-10s", "COST CENTRE", "MODULE");
598 fprintf(prof_file, "%6s %6s", "%time", "%alloc");
599 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
600 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
602 fprintf(prof_file, "\n\n");
604 for (cc = sorted_cc_list; cc != NULL; cc = cc->link) {
605 fprintf(prof_file, "%-20s %-10s", cc->label, cc->module);
606 fprintf(prof_file, "%6.1f %6.1f",
607 total_prof_ticks == 0 ? 0.0 : (cc->time_ticks / (StgFloat) total_prof_ticks * 100),
608 total_alloc == 0 ? 0.0 : (cc->mem_alloc / (StgFloat)
612 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
613 fprintf(prof_file, " %5ld %9ld", cc->time_ticks, cc->mem_alloc);
615 fprintf(prof_file, "\n");
618 fprintf(prof_file,"\n\n");
621 /* -----------------------------------------------------------------------------
622 Generate the cost-centre-stack time/alloc report
623 -------------------------------------------------------------------------- */
626 fprint_header( void )
628 fprintf(prof_file, "%-24s %-10s", "COST CENTRE", "MODULE");
630 fprintf(prof_file, "%8s %5s %5s %8s %5s", "scc", "%time", "%alloc", "inner", "cafs");
632 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
633 fprintf(prof_file, " %5s %9s", "ticks", "bytes");
634 #if defined(PROFILING_DETAIL_COUNTS)
635 fprintf(prof_file, " %8s %8s %8s %8s %8s %8s %8s",
636 "closures", "thunks", "funcs", "PAPs", "subfuns", "subcafs", "cafssub");
640 fprintf(prof_file, "\n\n");
644 report_ccs_profiling( void )
647 char temp[128]; /* sigh: magic constant */
651 total_prof_ticks = 0;
653 count_ticks(CCS_MAIN);
655 switch (RtsFlags.CcFlags.doCostCentres) {
658 case COST_CENTRES_XML:
664 fprintf(prof_file, "\t%s Time and Allocation Profiling Report (%s)\n",
665 time_str(), "Final");
667 fprintf(prof_file, "\n\t ");
668 fprintf(prof_file, " %s", prog_argv[0]);
669 fprintf(prof_file, " +RTS");
670 for (count = 0; rts_argv[count]; count++)
671 fprintf(prof_file, " %s", rts_argv[count]);
672 fprintf(prof_file, " -RTS");
673 for (count = 1; prog_argv[count]; count++)
674 fprintf(prof_file, " %s", prog_argv[count]);
675 fprintf(prof_file, "\n\n");
677 fprintf(prof_file, "\ttotal time = %11.2f secs (%lu ticks @ %d ms)\n",
678 total_prof_ticks / (StgFloat) TICK_FREQUENCY,
679 total_prof_ticks, TICK_MILLISECS);
681 fprintf(prof_file, "\ttotal alloc = %11s bytes",
682 ullong_format_string((ullong) total_alloc * sizeof(W_),
683 temp, rtsTrue/*commas*/));
684 /* ToDo: 64-bit error! */
686 #if defined(PROFILING_DETAIL_COUNTS)
687 fprintf(prof_file, " (%lu closures)", total_allocs);
689 fprintf(prof_file, " (excludes profiling overheads)\n\n");
691 report_per_cc_costs();
694 reportCCS(pruneCCSTree(CCS_MAIN), 0);
700 reportCCS(CostCentreStack *ccs, nat indent)
707 /* Only print cost centres with non 0 data ! */
709 if ( RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL ||
710 ! ccs_to_ignore(ccs))
711 /* force printing of *all* cost centres if -P -P */
714 fprintf(prof_file, "%-*s%-*s %-10s",
715 indent, "", 24-indent, cc->label, cc->module);
717 fprintf(prof_file, "%8ld %5.1f %5.1f %8ld %5ld",
719 total_prof_ticks == 0 ? 0.0 : (ccs->time_ticks / (StgFloat) total_prof_ticks * 100),
720 total_alloc == 0 ? 0.0 : (ccs->mem_alloc / (StgFloat) total_alloc * 100),
721 ccs->sub_scc_count, ccs->sub_cafcc_count);
723 if (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE) {
724 fprintf(prof_file, " %5ld %9ld", ccs->time_ticks, ccs->mem_alloc*sizeof(W_));
725 #if defined(PROFILING_DETAIL_COUNTS)
726 fprintf(prof_file, " %8ld %8ld %8ld %8ld %8ld %8ld %8ld",
727 ccs->mem_allocs, ccs->thunk_count,
728 ccs->function_count, ccs->pap_count,
729 ccs->subsumed_fun_count, ccs->subsumed_caf_count,
730 ccs->caffun_subsumed);
733 fprintf(prof_file, "\n");
736 for (i = ccs->indexTable; i != 0; i = i->next) {
738 reportCCS(i->ccs, indent+1);
743 /* Traverse the cost centre stack tree and accumulate
747 count_ticks(CostCentreStack *ccs)
751 if (!ccs_to_ignore(ccs)) {
752 total_alloc += ccs->mem_alloc;
753 total_prof_ticks += ccs->time_ticks;
755 for (i = ccs->indexTable; i != NULL; i = i->next)
761 /* return rtsTrue if it is one of the ones that
762 * should not be reported normally (because it confuses
766 ccs_to_ignore (CostCentreStack *ccs)
768 if ( ccs == CCS_OVERHEAD
769 || ccs == CCS_DONT_CARE
771 || ccs == CCS_SYSTEM) {
778 static CostCentreStack *
779 pruneCCSTree( CostCentreStack *ccs )
781 CostCentreStack *ccs1;
782 IndexTable *i, **prev;
784 prev = &ccs->indexTable;
785 for (i = ccs->indexTable; i != 0; i = i->next) {
786 if (i->back_edge) { continue; }
788 ccs1 = pruneCCSTree(i->ccs);
796 if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
797 /* force printing of *all* cost centres if -P -P */ )
799 || ( ccs->indexTable != 0 )
800 || ( (ccs->scc_count || ccs->sub_scc_count ||
801 ccs->time_ticks || ccs->mem_alloc
802 || (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_VERBOSE
803 && (ccs->sub_cafcc_count
804 #if defined(PROFILING_DETAIL_COUNTS)
805 || cc->thunk_count || cc->function_count || cc->pap_count
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);
852 printCCS ( CostCentreStack *ccs )
855 for (; ccs; ccs = ccs->prevStack ) {
856 fprintf(stderr,ccs->cc->label);
857 if (ccs->prevStack) {
865 #endif /* PROFILING */