1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2003
5 * Support for heap profiling
7 * ---------------------------------------------------------------------------*/
9 #if defined(DEBUG) && !defined(PROFILING)
10 #define DEBUG_HEAP_PROF
12 #undef DEBUG_HEAP_PROF
15 #if defined(PROFILING) || defined(DEBUG_HEAP_PROF)
17 #include "PosixSource.h"
21 #include "Profiling.h"
25 #include "RetainerProfile.h"
26 #include "LdvProfile.h"
34 /* -----------------------------------------------------------------------------
35 * era stores the current time period. It is the same as the
36 * number of censuses that have been performed.
39 * era must be no longer than LDV_SHIFT (15 or 30) bits.
41 * era is initialized to 1 in initHeapProfiling().
43 * max_era is initialized to 2^LDV_SHIFT in initHeapProfiling().
44 * When era reaches max_era, the profiling stops because a closure can
45 * store only up to (max_era - 1) as its creation or last use time.
46 * -------------------------------------------------------------------------- */
50 /* -----------------------------------------------------------------------------
53 * For most heap profiles each closure identity gets a simple count
54 * of live words in the heap at each census. However, if we're
55 * selecting by biography, then we have to keep the various
56 * lag/drag/void counters for each identity.
57 * -------------------------------------------------------------------------- */
58 typedef struct _counter {
63 int prim; // total size of 'inherently used' closures
64 int not_used; // total size of 'never used' closures
65 int used; // total size of 'used at least once' closures
66 int void_total; // current total size of 'destroyed without being used' closures
67 int drag_total; // current total size of 'used at least once and waiting to die'
70 struct _counter *next;
74 initLDVCtr( counter *ctr )
77 ctr->c.ldv.not_used = 0;
79 ctr->c.ldv.void_total = 0;
80 ctr->c.ldv.drag_total = 0;
84 double time; // the time in MUT time when the census is made
89 // for LDV profiling, when just displaying by LDV
97 static Census *censuses = NULL;
98 static nat n_censuses = 0;
101 static void aggregateCensusInfo( void );
104 static void dumpCensus( Census *census );
106 /* -----------------------------------------------------------------------------
107 Closure Type Profiling;
109 PROBABLY TOTALLY OUT OF DATE -- ToDo (SDM)
110 -------------------------------------------------------------------------- */
112 #ifdef DEBUG_HEAP_PROF
113 static char *type_names[] = {
117 , "CONSTR_NOCAF_STATIC"
153 , "MUT_ARR_PTRS_CLEAN"
154 , "MUT_ARR_PTRS_DIRTY"
155 , "MUT_ARR_PTRS_FROZEN"
169 #endif /* DEBUG_HEAP_PROF */
171 /* -----------------------------------------------------------------------------
172 * Find the "closure identity", which is a unique pointer reresenting
173 * the band to which this closure's heap space is attributed in the
175 * ------------------------------------------------------------------------- */
177 closureIdentity( StgClosure *p )
179 switch (RtsFlags.ProfFlags.doHeapProfile) {
183 return p->header.prof.ccs;
185 return p->header.prof.ccs->cc->module;
187 return get_itbl(p)->prof.closure_desc;
189 return get_itbl(p)->prof.closure_type;
190 case HEAP_BY_RETAINER:
191 // AFAIK, the only closures in the heap which might not have a
192 // valid retainer set are DEAD_WEAK closures.
193 if (isRetainerSetFieldValid(p))
194 return retainerSetOf(p);
199 case HEAP_BY_INFOPTR:
200 return (void *)((StgClosure *)p)->header.info;
201 case HEAP_BY_CLOSURE_TYPE:
202 return type_names[get_itbl(p)->type];
206 barf("closureIdentity");
210 /* --------------------------------------------------------------------------
211 * Profiling type predicates
212 * ----------------------------------------------------------------------- */
214 STATIC_INLINE rtsBool
215 doingLDVProfiling( void )
217 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
218 || RtsFlags.ProfFlags.bioSelector != NULL);
221 STATIC_INLINE rtsBool
222 doingRetainerProfiling( void )
224 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
225 || RtsFlags.ProfFlags.retainerSelector != NULL);
227 #endif /* PROFILING */
229 // Precesses a closure 'c' being destroyed whose size is 'size'.
230 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
231 // such as TSO; they should not be involved in computing dragNew or voidNew.
233 // Even though era is checked in both LdvCensusForDead() and
234 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
235 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
236 // when a thunk is replaced by an indirection object.
240 LDV_recordDead( StgClosure *c, nat size )
246 if (era > 0 && closureSatisfiesConstraints(c)) {
247 size -= sizeofW(StgProfHeader);
248 ASSERT(LDVW(c) != 0);
249 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
250 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
252 if (RtsFlags.ProfFlags.bioSelector == NULL) {
253 censuses[t].void_total += (int)size;
254 censuses[era].void_total -= (int)size;
255 ASSERT(censuses[t].void_total < censuses[t].not_used);
257 id = closureIdentity(c);
258 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
259 ASSERT( ctr != NULL );
260 ctr->c.ldv.void_total += (int)size;
261 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
263 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
265 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
267 ctr->next = censuses[era].ctrs;
268 censuses[era].ctrs = ctr;
270 ctr->c.ldv.void_total -= (int)size;
274 t = LDVW((c)) & LDV_LAST_MASK;
276 if (RtsFlags.ProfFlags.bioSelector == NULL) {
277 censuses[t+1].drag_total += size;
278 censuses[era].drag_total -= size;
281 id = closureIdentity(c);
282 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
283 ASSERT( ctr != NULL );
284 ctr->c.ldv.drag_total += (int)size;
285 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
287 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
289 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
291 ctr->next = censuses[era].ctrs;
292 censuses[era].ctrs = ctr;
294 ctr->c.ldv.drag_total -= (int)size;
302 /* --------------------------------------------------------------------------
303 * Initialize censuses[era];
304 * ----------------------------------------------------------------------- */
306 initEra(Census *census)
308 census->hash = allocHashTable();
310 census->arena = newArena();
312 census->not_used = 0;
315 census->void_total = 0;
316 census->drag_total = 0;
319 /* --------------------------------------------------------------------------
320 * Increases era by 1 and initialize census[era].
321 * Reallocates gi[] and increases its size if needed.
322 * ----------------------------------------------------------------------- */
327 if (doingLDVProfiling()) {
330 if (era == max_era) {
331 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
332 stg_exit(EXIT_FAILURE);
335 if (era == n_censuses) {
337 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
341 #endif /* PROFILING */
343 initEra( &censuses[era] );
346 /* -----------------------------------------------------------------------------
347 * DEBUG heap profiling, by info table
348 * -------------------------------------------------------------------------- */
350 #ifdef DEBUG_HEAP_PROF
352 static char *hp_filename;
354 void initProfiling1( void )
358 void initProfiling2( void )
360 if (RtsFlags.ProfFlags.doHeapProfile) {
361 /* Initialise the log file name */
362 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
363 sprintf(hp_filename, "%s.hp", prog_name);
365 /* open the log file */
366 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
367 debugBelch("Can't open profiling report file %s\n",
369 RtsFlags.ProfFlags.doHeapProfile = 0;
377 void endProfiling( void )
381 #endif /* DEBUG_HEAP_PROF */
384 printSample(rtsBool beginSample, StgDouble sampleValue)
386 StgDouble fractionalPart, integralPart;
387 fractionalPart = modf(sampleValue, &integralPart);
388 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
389 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
390 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
393 /* --------------------------------------------------------------------------
394 * Initialize the heap profilier
395 * ----------------------------------------------------------------------- */
397 initHeapProfiling(void)
399 if (! RtsFlags.ProfFlags.doHeapProfile) {
404 if (doingLDVProfiling() && doingRetainerProfiling()) {
405 errorBelch("cannot mix -hb and -hr");
406 stg_exit(EXIT_FAILURE);
410 // we only count eras if we're doing LDV profiling. Otherwise era
413 if (doingLDVProfiling()) {
421 { // max_era = 2^LDV_SHIFT
424 for (p = 0; p < LDV_SHIFT; p++)
429 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
431 initEra( &censuses[era] );
433 /* initProfilingLogFile(); */
434 fprintf(hp_file, "JOB \"%s", prog_name);
439 for(count = 1; count < prog_argc; count++)
440 fprintf(hp_file, " %s", prog_argv[count]);
441 fprintf(hp_file, " +RTS");
442 for(count = 0; count < rts_argc; count++)
443 fprintf(hp_file, " %s", rts_argv[count]);
445 #endif /* PROFILING */
447 fprintf(hp_file, "\"\n" );
449 fprintf(hp_file, "DATE \"%s\"\n", time_str());
451 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
452 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
454 printSample(rtsTrue, 0);
455 printSample(rtsFalse, 0);
457 #ifdef DEBUG_HEAP_PROF
458 DEBUG_LoadSymbols(prog_name);
462 if (doingRetainerProfiling()) {
463 initRetainerProfiling();
471 endHeapProfiling(void)
475 if (! RtsFlags.ProfFlags.doHeapProfile) {
480 if (doingRetainerProfiling()) {
481 endRetainerProfiling();
486 if (doingLDVProfiling()) {
489 aggregateCensusInfo();
490 for (t = 1; t < era; t++) {
491 dumpCensus( &censuses[t] );
496 seconds = mut_user_time();
497 printSample(rtsTrue, seconds);
498 printSample(rtsFalse, seconds);
506 buf_append(char *p, const char *q, char *end)
510 for (m = 0; p < end; p++, q++, m++) {
512 if (*q == '\0') { break; }
518 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
520 char buf[max_length+1], *p, *buf_end;
522 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
523 if (ccs == CCS_MAIN) {
528 fprintf(fp, "(%ld)", ccs->ccsID);
531 buf_end = buf + max_length + 1;
533 // keep printing components of the stack until we run out of space
534 // in the buffer. If we run out of space, end with "...".
535 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
537 // CAF cost centres print as M.CAF, but we leave the module
538 // name out of all the others to save space.
539 if (!strcmp(ccs->cc->label,"CAF")) {
540 p += buf_append(p, ccs->cc->module, buf_end);
541 p += buf_append(p, ".CAF", buf_end);
543 p += buf_append(p, ccs->cc->label, buf_end);
544 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
545 p += buf_append(p, "/", buf_end);
550 sprintf(buf+max_length-4, "...");
554 fprintf(fp, "%s", buf);
556 #endif /* PROFILING */
559 strMatchesSelector( char* str, char* sel )
562 // debugBelch("str_matches_selector %s %s\n", str, sel);
564 // Compare str against wherever we've got to in sel.
566 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
569 // Match if all of str used and have reached the end of a sel fragment.
570 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
573 // No match. Advance sel to the start of the next elem.
574 while (*sel != ',' && *sel != '\0') sel++;
575 if (*sel == ',') sel++;
577 /* Run out of sel ?? */
578 if (*sel == '\0') return rtsFalse;
582 /* -----------------------------------------------------------------------------
583 * Figure out whether a closure should be counted in this census, by
584 * testing against all the specified constraints.
585 * -------------------------------------------------------------------------- */
587 closureSatisfiesConstraints( StgClosure* p )
589 #ifdef DEBUG_HEAP_PROF
590 (void)p; /* keep gcc -Wall happy */
595 // The CCS has a selected field to indicate whether this closure is
596 // deselected by not being mentioned in the module, CC, or CCS
598 if (!p->header.prof.ccs->selected) {
602 if (RtsFlags.ProfFlags.descrSelector) {
603 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_desc,
604 RtsFlags.ProfFlags.descrSelector );
605 if (!b) return rtsFalse;
607 if (RtsFlags.ProfFlags.typeSelector) {
608 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_type,
609 RtsFlags.ProfFlags.typeSelector );
610 if (!b) return rtsFalse;
612 if (RtsFlags.ProfFlags.retainerSelector) {
615 // We must check that the retainer set is valid here. One
616 // reason it might not be valid is if this closure is a
617 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
618 // these aren't reached by the retainer profiler's traversal.
619 if (isRetainerSetFieldValid((StgClosure *)p)) {
620 rs = retainerSetOf((StgClosure *)p);
622 for (i = 0; i < rs->num; i++) {
623 b = strMatchesSelector( rs->element[i]->cc->label,
624 RtsFlags.ProfFlags.retainerSelector );
625 if (b) return rtsTrue;
632 #endif /* PROFILING */
635 /* -----------------------------------------------------------------------------
636 * Aggregate the heap census info for biographical profiling
637 * -------------------------------------------------------------------------- */
640 aggregateCensusInfo( void )
644 counter *c, *d, *ctrs;
647 if (!doingLDVProfiling()) return;
649 // Aggregate the LDV counters when displaying by biography.
650 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
651 int void_total, drag_total;
653 // Now we compute void_total and drag_total for each census
654 // After the program has finished, the void_total field of
655 // each census contains the count of words that were *created*
656 // in this era and were eventually void. Conversely, if a
657 // void closure was destroyed in this era, it will be
658 // represented by a negative count of words in void_total.
660 // To get the count of live words that are void at each
661 // census, just propagate the void_total count forwards:
665 for (t = 1; t < era; t++) { // note: start at 1, not 0
666 void_total += censuses[t].void_total;
667 drag_total += censuses[t].drag_total;
668 censuses[t].void_total = void_total;
669 censuses[t].drag_total = drag_total;
671 ASSERT( censuses[t].void_total <= censuses[t].not_used );
672 // should be true because: void_total is the count of
673 // live words that are void at this census, which *must*
674 // be less than the number of live words that have not
677 ASSERT( censuses[t].drag_total <= censuses[t].used );
678 // similar reasoning as above.
684 // otherwise... we're doing a heap profile that is restricted to
685 // some combination of lag, drag, void or use. We've kept all the
686 // census info for all censuses so far, but we still need to
687 // aggregate the counters forwards.
690 acc = allocHashTable();
693 for (t = 1; t < era; t++) {
695 // first look through all the counters we're aggregating
696 for (c = ctrs; c != NULL; c = c->next) {
697 // if one of the totals is non-zero, then this closure
698 // type must be present in the heap at this census time...
699 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
702 // if this closure identity isn't present in the
703 // census for this time period, then our running
704 // totals *must* be zero.
705 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
707 // debugCCS(c->identity);
708 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
709 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
711 d->c.ldv.void_total += c->c.ldv.void_total;
712 d->c.ldv.drag_total += c->c.ldv.drag_total;
713 c->c.ldv.void_total = d->c.ldv.void_total;
714 c->c.ldv.drag_total = d->c.ldv.drag_total;
716 ASSERT( c->c.ldv.void_total >= 0 );
717 ASSERT( c->c.ldv.drag_total >= 0 );
721 // now look through the counters in this census to find new ones
722 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
723 d = lookupHashTable(acc, (StgWord)c->identity);
725 d = arenaAlloc( arena, sizeof(counter) );
727 insertHashTable( acc, (StgWord)c->identity, d );
728 d->identity = c->identity;
731 d->c.ldv.void_total = c->c.ldv.void_total;
732 d->c.ldv.drag_total = c->c.ldv.drag_total;
734 ASSERT( c->c.ldv.void_total >= 0 );
735 ASSERT( c->c.ldv.drag_total >= 0 );
739 freeHashTable(acc, NULL);
744 /* -----------------------------------------------------------------------------
745 * Print out the results of a heap census.
746 * -------------------------------------------------------------------------- */
748 dumpCensus( Census *census )
753 printSample(rtsTrue, census->time);
756 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
757 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
758 fprintf(hp_file, "LAG\t%lu\n",
759 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
760 fprintf(hp_file, "USE\t%lu\n",
761 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
762 fprintf(hp_file, "INHERENT_USE\t%lu\n",
763 (unsigned long)(census->prim) * sizeof(W_));
764 fprintf(hp_file, "DRAG\t%lu\n",
765 (unsigned long)(census->drag_total) * sizeof(W_));
766 printSample(rtsFalse, census->time);
771 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
774 if (RtsFlags.ProfFlags.bioSelector != NULL) {
776 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
777 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
778 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
779 count += ctr->c.ldv.drag_total;
780 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
781 count += ctr->c.ldv.void_total;
782 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
783 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
787 count = ctr->c.resid;
790 ASSERT( count >= 0 );
792 if (count == 0) continue;
794 #ifdef DEBUG_HEAP_PROF
795 switch (RtsFlags.ProfFlags.doHeapProfile) {
796 case HEAP_BY_INFOPTR:
797 fprintf(hp_file, "%s", lookupGHCName(ctr->identity));
799 case HEAP_BY_CLOSURE_TYPE:
800 fprintf(hp_file, "%s", (char *)ctr->identity);
806 switch (RtsFlags.ProfFlags.doHeapProfile) {
808 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
813 fprintf(hp_file, "%s", (char *)ctr->identity);
815 case HEAP_BY_RETAINER:
817 RetainerSet *rs = (RetainerSet *)ctr->identity;
819 // it might be the distinguished retainer set rs_MANY:
820 if (rs == &rs_MANY) {
821 fprintf(hp_file, "MANY");
825 // Mark this retainer set by negating its id, because it
826 // has appeared in at least one census. We print the
827 // values of all such retainer sets into the log file at
828 // the end. A retainer set may exist but not feature in
829 // any censuses if it arose as the intermediate retainer
830 // set for some closure during retainer set calculation.
834 // report in the unit of bytes: * sizeof(StgWord)
835 printRetainerSetShort(hp_file, rs);
839 barf("dumpCensus; doHeapProfile");
843 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
846 printSample(rtsFalse, census->time);
849 /* -----------------------------------------------------------------------------
850 * Code to perform a heap census.
851 * -------------------------------------------------------------------------- */
853 heapCensusChain( Census *census, bdescr *bd )
863 for (; bd != NULL; bd = bd->link) {
865 // HACK: ignore pinned blocks, because they contain gaps.
866 // It's not clear exactly what we'd like to do here, since we
867 // can't tell which objects in the block are actually alive.
868 // Perhaps the whole block should be counted as SYSTEM memory.
869 if (bd->flags & BF_PINNED) {
874 while (p < bd->free) {
875 info = get_itbl((StgClosure *)p);
878 switch (info->type) {
881 size = thunk_sizeW_fromITBL(info);
887 size = sizeofW(StgThunkHeader) + 2;
893 size = sizeofW(StgThunkHeader) + 1;
900 case IND_OLDGEN_PERM:
902 case SE_CAF_BLACKHOLE:
915 size = sizeW_fromITBL(info);
919 // Special case/Delicate Hack: INDs don't normally
920 // appear, since we're doing this heap census right
921 // after GC. However, GarbageCollect() also does
922 // resurrectThreads(), which can update some
923 // blackholes when it calls raiseAsync() on the
924 // resurrected threads. So we know that any IND will
925 // be the size of a BLACKHOLE.
926 size = BLACKHOLE_sizeW();
931 size = bco_sizeW((StgBCO *)p);
940 size = sizeW_fromITBL(info);
944 size = ap_sizeW((StgAP *)p);
948 size = pap_sizeW((StgPAP *)p);
952 size = ap_stack_sizeW((StgAP_STACK *)p);
957 size = arr_words_sizeW(stgCast(StgArrWords*,p));
960 case MUT_ARR_PTRS_CLEAN:
961 case MUT_ARR_PTRS_DIRTY:
962 case MUT_ARR_PTRS_FROZEN:
963 case MUT_ARR_PTRS_FROZEN0:
965 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
970 #ifdef DEBUG_HEAP_PROF
971 size = tso_sizeW((StgTSO *)p);
974 if (RtsFlags.ProfFlags.includeTSOs) {
975 size = tso_sizeW((StgTSO *)p);
978 // Skip this TSO and move on to the next object
979 p += tso_sizeW((StgTSO *)p);
986 size = sizeofW(StgTRecHeader);
989 case TVAR_WATCH_QUEUE:
991 size = sizeofW(StgTVarWatchQueue);
994 case INVARIANT_CHECK_QUEUE:
996 size = sizeofW(StgInvariantCheckQueue);
999 case ATOMIC_INVARIANT:
1001 size = sizeofW(StgAtomicInvariant);
1006 size = sizeofW(StgTVar);
1011 size = sizeofW(StgTRecChunk);
1015 barf("heapCensus, unknown object: %d", info->type);
1020 #ifdef DEBUG_HEAP_PROF
1023 // subtract the profiling overhead
1024 real_size = size - sizeofW(StgProfHeader);
1027 if (closureSatisfiesConstraints((StgClosure*)p)) {
1029 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1031 census->prim += real_size;
1032 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1033 census->not_used += real_size;
1035 census->used += real_size;
1039 identity = closureIdentity((StgClosure *)p);
1041 if (identity != NULL) {
1042 ctr = lookupHashTable( census->hash, (StgWord)identity );
1045 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1047 ctr->c.ldv.prim += real_size;
1048 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1049 ctr->c.ldv.not_used += real_size;
1051 ctr->c.ldv.used += real_size;
1055 ctr->c.resid += real_size;
1058 ctr = arenaAlloc( census->arena, sizeof(counter) );
1060 insertHashTable( census->hash, (StgWord)identity, ctr );
1061 ctr->identity = identity;
1062 ctr->next = census->ctrs;
1066 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1068 ctr->c.ldv.prim = real_size;
1069 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1070 ctr->c.ldv.not_used = real_size;
1072 ctr->c.ldv.used = real_size;
1076 ctr->c.resid = real_size;
1094 census = &censuses[era];
1095 census->time = mut_user_time();
1097 // calculate retainer sets if necessary
1099 if (doingRetainerProfiling()) {
1105 stat_startHeapCensus();
1108 // Traverse the heap, collecting the census info
1110 // First the small_alloc_list: we have to fix the free pointer at
1111 // the end by calling tidyAllocatedLists() first.
1112 tidyAllocateLists();
1113 heapCensusChain( census, small_alloc_list );
1115 // Now traverse the heap in each generation/step.
1116 if (RtsFlags.GcFlags.generations == 1) {
1117 heapCensusChain( census, g0s0->blocks );
1119 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1120 for (s = 0; s < generations[g].n_steps; s++) {
1121 heapCensusChain( census, generations[g].steps[s].blocks );
1122 // Are we interested in large objects? might be
1123 // confusing to include the stack in a heap profile.
1124 heapCensusChain( census, generations[g].steps[s].large_objects );
1129 // dump out the census info
1131 // We can't generate any info for LDV profiling until
1132 // the end of the run...
1133 if (!doingLDVProfiling())
1134 dumpCensus( census );
1136 dumpCensus( census );
1140 // free our storage, unless we're keeping all the census info for
1141 // future restriction by biography.
1143 if (RtsFlags.ProfFlags.bioSelector == NULL)
1146 freeHashTable( census->hash, NULL/* don't free the elements */ );
1147 arenaFree( census->arena );
1148 census->hash = NULL;
1149 census->arena = NULL;
1152 // we're into the next time period now
1156 stat_endHeapCensus();
1160 #endif /* PROFILING || DEBUG_HEAP_PROF */