X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FProfHeap.c;h=19beb9cba633f477c633658b2186691eb4deb75f;hb=8a45d6bb5426552fc6993fc4a1f391d0f3c77b8d;hp=2bd9e59a9900ee9932bdda89bb1e889627af7ba9;hpb=6313b907b9223f66ea31d675892aab231aca3783;p=ghc-hetmet.git diff --git a/ghc/rts/ProfHeap.c b/ghc/rts/ProfHeap.c index 2bd9e59..19beb9c 100644 --- a/ghc/rts/ProfHeap.c +++ b/ghc/rts/ProfHeap.c @@ -1,7 +1,7 @@ /* ----------------------------------------------------------------------------- - * $Id: ProfHeap.c,v 1.29 2001/11/28 15:01:02 simonmar Exp $ + * $Id: ProfHeap.c,v 1.47 2003/08/22 22:24:12 sof Exp $ * - * (c) The GHC Team, 1998-2000 + * (c) The GHC Team, 1998-2003 * * Support for heap profiling * @@ -28,11 +28,10 @@ #include "RetainerProfile.h" #include "LdvProfile.h" #include "Arena.h" - -#ifdef DEBUG_HEAP_PROF #include "Printer.h" -static void fprint_data(FILE *fp); -#endif + +#include +#include /* ----------------------------------------------------------------------------- * era stores the current time period. It is the same as the @@ -41,7 +40,7 @@ static void fprint_data(FILE *fp); * RESTRICTION: * era must be no longer than LDV_SHIFT (15 or 30) bits. * Invariants: - * era is initialized to 0 in initHeapProfiling(). + * era is initialized to 1 in initHeapProfiling(). * * max_era is initialized to 2^LDV_SHIFT in initHeapProfiling(). * When era reaches max_era, the profiling stops because a closure can @@ -97,8 +96,8 @@ typedef struct { int drag_total; } Census; -Census *censuses = NULL; -nat n_censuses = 0; +static Census *censuses = NULL; +static nat n_censuses = 0; #ifdef PROFILING static void aggregateCensusInfo( void ); @@ -129,7 +128,8 @@ static char *type_names[] = { , "THUNK_SELECTOR" , "BCO" - , "AP_UPD" + , "AP_STACK" + , "AP" , "PAP" @@ -148,7 +148,6 @@ static char *type_names[] = { , "UPDATE_FRAME" , "CATCH_FRAME" , "STOP_FRAME" - , "SEQ_FRAME" , "BLACKHOLE" , "BLACKHOLE_BQ" @@ -185,20 +184,27 @@ closureIdentity( StgClosure *p ) #ifdef PROFILING case HEAP_BY_CCS: - return ((StgClosure *)p)->header.prof.ccs; + return p->header.prof.ccs; case HEAP_BY_MOD: - return ((StgClosure *)p)->header.prof.ccs->cc->module; + return p->header.prof.ccs->cc->module; case HEAP_BY_DESCR: - return (get_itbl((StgClosure *)p))->prof.closure_desc; + return get_itbl(p)->prof.closure_desc; case HEAP_BY_TYPE: - return (get_itbl((StgClosure *)p))->prof.closure_type; + return get_itbl(p)->prof.closure_type; case HEAP_BY_RETAINER: - return retainerSetOf((StgClosure *)p); + // AFAIK, the only closures in the heap which might not have a + // valid retainer set are DEAD_WEAK closures. + if (isRetainerSetFieldValid(p)) + return retainerSetOf(p); + else + return NULL; + #else // DEBUG case HEAP_BY_INFOPTR: return (void *)((StgClosure *)p)->header.info; case HEAP_BY_CLOSURE_TYPE: return type_names[get_itbl(p)->type]; + #endif default: barf("closureIdentity"); @@ -324,7 +330,8 @@ nextEra( void ) era++; if (era == max_era) { - barf("maximum number of censuses reached; use +RTS -i to reduce"); + prog_belch("maximum number of censuses reached; use +RTS -i to reduce"); + stg_exit(EXIT_FAILURE); } if (era == n_censuses) { @@ -334,11 +341,13 @@ nextEra( void ) } } #endif // PROFILING - + initEra( &censuses[era] ); } -/* -------------------------------------------------------------------------- */ +/* ----------------------------------------------------------------------------- + * DEBUG heap profiling, by info table + * -------------------------------------------------------------------------- */ #ifdef DEBUG_HEAP_PROF FILE *hp_file; @@ -358,6 +367,9 @@ void endProfiling( void ) } #endif /* DEBUG_HEAP_PROF */ +/* -------------------------------------------------------------------------- + * Initialize the heap profilier + * ----------------------------------------------------------------------- */ nat initHeapProfiling(void) { @@ -365,6 +377,13 @@ initHeapProfiling(void) return 0; } +#ifdef PROFILING + if (doingLDVProfiling() && doingRetainerProfiling()) { + prog_belch("cannot mix -hb and -hr"); + stg_exit(1); + } +#endif + // we only count eras if we're doing LDV profiling. Otherwise era // is fixed at zero. #ifdef PROFILING @@ -388,7 +407,7 @@ initHeapProfiling(void) initEra( &censuses[era] ); - fprintf(hp_file, "JOB \"%s", prog_argv[0]); + fprintf(hp_file, "JOB \"%s", prog_name); #ifdef PROFILING { @@ -413,7 +432,7 @@ initHeapProfiling(void) fprintf(hp_file, "END_SAMPLE 0.00\n"); #ifdef DEBUG_HEAP_PROF - DEBUG_LoadSymbols(prog_argv[0]); + DEBUG_LoadSymbols(prog_name); #endif #ifdef PROFILING @@ -441,17 +460,9 @@ endHeapProfiling(void) #endif #ifdef PROFILING - // Note: - // We do not need to perform a major garbage collection because all the - // closures created since the last census will not affect the profiling - // statistics anyhow. - if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) - LdvCensusKillAll(); -#endif - -#ifdef PROFILING - if (RtsFlags.ProfFlags.bioSelector != NULL) { + if (doingLDVProfiling()) { nat t; + LdvCensusKillAll(); aggregateCensusInfo(); for (t = 1; t < era; t++) { dumpCensus( &censuses[t] ); @@ -468,13 +479,24 @@ endHeapProfiling(void) #ifdef PROFILING +static size_t +buf_append(char *p, const char *q, char *end) +{ + int m; + + for (m = 0; p < end; p++, q++, m++) { + *p = *q; + if (*q == '\0') { break; } + } + return m; +} + static void fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length) { - char buf[max_length+1]; + char buf[max_length+1], *p, *buf_end; nat next_offset = 0; nat written; - char *template; // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN. if (ccs == CCS_MAIN) { @@ -482,6 +504,11 @@ fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length) return; } + fprintf(fp, "(%d)", ccs->ccsID); + + p = buf; + buf_end = buf + max_length + 1; + // keep printing components of the stack until we run out of space // in the buffer. If we run out of space, end with "...". for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) { @@ -489,21 +516,16 @@ fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length) // CAF cost centres print as M.CAF, but we leave the module // name out of all the others to save space. if (!strcmp(ccs->cc->label,"CAF")) { - written = snprintf(buf+next_offset, - (int)max_length-3-(int)next_offset, - "%s.CAF", ccs->cc->module); + p += buf_append(p, ccs->cc->module, buf_end); + p += buf_append(p, ".CAF", buf_end); } else { if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) { - template = "%s/"; - } else { - template = "%s"; + p += buf_append(p, "/", buf_end); } - written = snprintf(buf+next_offset, - (int)max_length-3-(int)next_offset, - template, ccs->cc->label); + p += buf_append(p, ccs->cc->label, buf_end); } - - if (next_offset+written >= max_length-4) { + + if (p >= buf_end) { sprintf(buf+max_length-4, "..."); break; } else { @@ -512,9 +534,10 @@ fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length) } fprintf(fp, "%s", buf); } +#endif // PROFILING -static rtsBool -str_matches_selector( char* str, char* sel ) +rtsBool +strMatchesSelector( char* str, char* sel ) { char* p; // fprintf(stderr, "str_matches_selector %s %s\n", str, sel); @@ -537,48 +560,57 @@ str_matches_selector( char* str, char* sel ) } } -// Figure out whether a closure should be counted in this census, by -// testing against all the specified constraints. +/* ----------------------------------------------------------------------------- + * Figure out whether a closure should be counted in this census, by + * testing against all the specified constraints. + * -------------------------------------------------------------------------- */ rtsBool closureSatisfiesConstraints( StgClosure* p ) { +#ifdef DEBUG_HEAP_PROF + return rtsTrue; +#else rtsBool b; - if (RtsFlags.ProfFlags.modSelector) { - b = str_matches_selector( ((StgClosure *)p)->header.prof.ccs->cc->module, - RtsFlags.ProfFlags.modSelector ); - if (!b) return rtsFalse; + + // The CCS has a selected field to indicate whether this closure is + // deselected by not being mentioned in the module, CC, or CCS + // selectors. + if (!p->header.prof.ccs->selected) { + return rtsFalse; } + if (RtsFlags.ProfFlags.descrSelector) { - b = str_matches_selector( (get_itbl((StgClosure *)p))->prof.closure_desc, + b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_desc, RtsFlags.ProfFlags.descrSelector ); if (!b) return rtsFalse; } if (RtsFlags.ProfFlags.typeSelector) { - b = str_matches_selector( (get_itbl((StgClosure *)p))->prof.closure_type, + b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_type, RtsFlags.ProfFlags.typeSelector ); if (!b) return rtsFalse; } - if (RtsFlags.ProfFlags.ccSelector) { - b = str_matches_selector( ((StgClosure *)p)->header.prof.ccs->cc->label, - RtsFlags.ProfFlags.ccSelector ); - if (!b) return rtsFalse; - } if (RtsFlags.ProfFlags.retainerSelector) { RetainerSet *rs; nat i; - rs = retainerSetOf((StgClosure *)p); - if (rs != NULL) { - for (i = 0; i < rs->num; i++) { - b = str_matches_selector( rs->element[i]->cc->label, - RtsFlags.ProfFlags.retainerSelector ); - if (b) return rtsTrue; + // We must check that the retainer set is valid here. One + // reason it might not be valid is if this closure is a + // a newly deceased weak pointer (i.e. a DEAD_WEAK), since + // these aren't reached by the retainer profiler's traversal. + if (isRetainerSetFieldValid((StgClosure *)p)) { + rs = retainerSetOf((StgClosure *)p); + if (rs != NULL) { + for (i = 0; i < rs->num; i++) { + b = strMatchesSelector( rs->element[i]->cc->label, + RtsFlags.ProfFlags.retainerSelector ); + if (b) return rtsTrue; + } } } return rtsFalse; } return rtsTrue; -} #endif /* PROFILING */ +} /* ----------------------------------------------------------------------------- * Aggregate the heap census info for biographical profiling @@ -606,23 +638,8 @@ aggregateCensusInfo( void ) drag_total += censuses[t].drag_total; censuses[t].void_total = void_total; censuses[t].drag_total = drag_total; - ASSERT( censuses[t].void_total < censuses[t].not_used ); - ASSERT( censuses[t].drag_total < censuses[t].used ); - } - - for (t = 1; t < era; t++) { // note: start at 1, not 0 - fprintf(hp_file, "MARK %f\n", censuses[t].time); - fprintf(hp_file, "BEGIN_SAMPLE %f\n", censuses[t].time); - fprintf(hp_file, "VOID\t%u\n", censuses[t].void_total * sizeof(W_)); - fprintf(hp_file, "LAG\t%u\n", - (censuses[t].not_used - censuses[t].void_total) - * sizeof(W_)); - fprintf(hp_file, "USE\t%u\n", - (censuses[t].used - censuses[t].drag_total) * sizeof(W_)); - fprintf(hp_file, "INHERENT_USE\t%u\n", - censuses[t].prim * sizeof(W_)); - fprintf(hp_file, "DRAG\t%u\n", censuses[t].drag_total * sizeof(W_)); - fprintf(hp_file, "END_SAMPLE %f\n", censuses[t].time); + ASSERT( censuses[t].void_total <= censuses[t].not_used ); + ASSERT( censuses[t].drag_total <= censuses[t].used ); } return; @@ -699,18 +716,34 @@ dumpCensus( Census *census ) fprintf(hp_file, "BEGIN_SAMPLE %0.2f\n", census->time); +#ifdef PROFILING + if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) { + fprintf(hp_file, "VOID\t%u\n", census->void_total * sizeof(W_)); + fprintf(hp_file, "LAG\t%u\n", + (census->not_used - census->void_total) * sizeof(W_)); + fprintf(hp_file, "USE\t%u\n", + (census->used - census->drag_total) * sizeof(W_)); + fprintf(hp_file, "INHERENT_USE\t%u\n", + census->prim * sizeof(W_)); + fprintf(hp_file, "DRAG\t%u\n", census->drag_total * + sizeof(W_)); + fprintf(hp_file, "END_SAMPLE %0.2f\n", census->time); + return; + } +#endif + for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) { #ifdef PROFILING if (RtsFlags.ProfFlags.bioSelector != NULL) { count = 0; - if (str_matches_selector("lag", RtsFlags.ProfFlags.bioSelector)) + if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector)) count += ctr->c.ldv.not_used - ctr->c.ldv.void_total; - if (str_matches_selector("drag", RtsFlags.ProfFlags.bioSelector)) + if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector)) count += ctr->c.ldv.drag_total; - if (str_matches_selector("void", RtsFlags.ProfFlags.bioSelector)) + if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector)) count += ctr->c.ldv.void_total; - if (str_matches_selector("use", RtsFlags.ProfFlags.bioSelector)) + if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector)) count += ctr->c.ldv.used - ctr->c.ldv.drag_total; } else #endif @@ -725,10 +758,10 @@ dumpCensus( Census *census ) #ifdef DEBUG_HEAP_PROF switch (RtsFlags.ProfFlags.doHeapProfile) { case HEAP_BY_INFOPTR: - fprint_data(hp_file); + fprintf(hp_file, "%s", lookupGHCName(ctr->identity)); break; case HEAP_BY_CLOSURE_TYPE: - fprint_closure_types(hp_file); + fprintf(hp_file, "%s", (char *)ctr->identity); break; } #endif @@ -736,7 +769,7 @@ dumpCensus( Census *census ) #ifdef PROFILING switch (RtsFlags.ProfFlags.doHeapProfile) { case HEAP_BY_CCS: - fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, 30); + fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, 25); break; case HEAP_BY_MOD: case HEAP_BY_DESCR: @@ -803,6 +836,7 @@ heapCensusChain( Census *census, bdescr *bd ) case FUN: case THUNK: case IND_PERM: + case IND_OLDGEN: case IND_OLDGEN_PERM: case CAF_BLACKHOLE: case SE_CAF_BLACKHOLE: @@ -828,6 +862,10 @@ heapCensusChain( Census *census, bdescr *bd ) break; case BCO: + prim = rtsTrue; + size = bco_sizeW((StgBCO *)p); + break; + case MVAR: case WEAK: case FOREIGN: @@ -844,10 +882,14 @@ heapCensusChain( Census *census, bdescr *bd ) size = sizeofW(StgHeader) + MIN_UPD_SIZE; break; + case AP: case PAP: - case AP_UPD: size = pap_sizeW((StgPAP *)p); break; + + case AP_STACK: + size = ap_stack_sizeW((StgAP_STACK *)p); + break; case ARR_WORDS: prim = rtsTrue; @@ -862,9 +904,20 @@ heapCensusChain( Census *census, bdescr *bd ) case TSO: prim = rtsTrue; +#ifdef DEBUG_HEAP_PROF size = tso_sizeW((StgTSO *)p); break; - +#else + if (RtsFlags.ProfFlags.includeTSOs) { + size = tso_sizeW((StgTSO *)p); + break; + } else { + // Skip this TSO and move on to the next object + p += tso_sizeW((StgTSO *)p); + continue; + } +#endif + default: barf("heapCensus"); } @@ -955,10 +1008,18 @@ heapCensus( void ) } #endif +#ifdef PROFILING stat_startHeapCensus(); +#endif - // traverse the heap, collecting the census info + // Traverse the heap, collecting the census info + + // First the small_alloc_list: we have to fix the free pointer at + // the end by calling tidyAllocatedLists() first. + tidyAllocateLists(); heapCensusChain( census, small_alloc_list ); + + // Now traverse the heap in each generation/step. if (RtsFlags.GcFlags.generations == 1) { heapCensusChain( census, g0s0->to_blocks ); } else { @@ -967,7 +1028,7 @@ heapCensus( void ) heapCensusChain( census, generations[g].steps[s].blocks ); // Are we interested in large objects? might be // confusing to include the stack in a heap profile. - // heapCensusChain( census, generations[g].steps[s].large_objects ); + heapCensusChain( census, generations[g].steps[s].large_objects ); } } } @@ -998,7 +1059,9 @@ heapCensus( void ) // we're into the next time period now nextEra(); +#ifdef PROFILING stat_endHeapCensus(); +#endif } #endif /* PROFILING || DEBUG_HEAP_PROF */