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"
151 , "MUT_ARR_PTRS_CLEAN"
152 , "MUT_ARR_PTRS_DIRTY"
153 , "MUT_ARR_PTRS_FROZEN"
167 #endif /* DEBUG_HEAP_PROF */
169 /* -----------------------------------------------------------------------------
170 * Find the "closure identity", which is a unique pointer reresenting
171 * the band to which this closure's heap space is attributed in the
173 * ------------------------------------------------------------------------- */
175 closureIdentity( StgClosure *p )
177 switch (RtsFlags.ProfFlags.doHeapProfile) {
181 return p->header.prof.ccs;
183 return p->header.prof.ccs->cc->module;
185 return get_itbl(p)->prof.closure_desc;
187 return get_itbl(p)->prof.closure_type;
188 case HEAP_BY_RETAINER:
189 // AFAIK, the only closures in the heap which might not have a
190 // valid retainer set are DEAD_WEAK closures.
191 if (isRetainerSetFieldValid(p))
192 return retainerSetOf(p);
197 case HEAP_BY_INFOPTR:
198 return (void *)((StgClosure *)p)->header.info;
199 case HEAP_BY_CLOSURE_TYPE:
200 return type_names[get_itbl(p)->type];
204 barf("closureIdentity");
208 /* --------------------------------------------------------------------------
209 * Profiling type predicates
210 * ----------------------------------------------------------------------- */
212 STATIC_INLINE rtsBool
213 doingLDVProfiling( void )
215 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
216 || RtsFlags.ProfFlags.bioSelector != NULL);
219 STATIC_INLINE rtsBool
220 doingRetainerProfiling( void )
222 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
223 || RtsFlags.ProfFlags.retainerSelector != NULL);
225 #endif /* PROFILING */
227 // Precesses a closure 'c' being destroyed whose size is 'size'.
228 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
229 // such as TSO; they should not be involved in computing dragNew or voidNew.
231 // Even though era is checked in both LdvCensusForDead() and
232 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
233 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
234 // when a thunk is replaced by an indirection object.
238 LDV_recordDead( StgClosure *c, nat size )
244 if (era > 0 && closureSatisfiesConstraints(c)) {
245 size -= sizeofW(StgProfHeader);
246 ASSERT(LDVW(c) != 0);
247 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
248 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
250 if (RtsFlags.ProfFlags.bioSelector == NULL) {
251 censuses[t].void_total += (int)size;
252 censuses[era].void_total -= (int)size;
253 ASSERT(censuses[t].void_total < censuses[t].not_used);
255 id = closureIdentity(c);
256 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
257 ASSERT( ctr != NULL );
258 ctr->c.ldv.void_total += (int)size;
259 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
261 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
263 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
265 ctr->next = censuses[era].ctrs;
266 censuses[era].ctrs = ctr;
268 ctr->c.ldv.void_total -= (int)size;
272 t = LDVW((c)) & LDV_LAST_MASK;
274 if (RtsFlags.ProfFlags.bioSelector == NULL) {
275 censuses[t+1].drag_total += size;
276 censuses[era].drag_total -= size;
279 id = closureIdentity(c);
280 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
281 ASSERT( ctr != NULL );
282 ctr->c.ldv.drag_total += (int)size;
283 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
285 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
287 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
289 ctr->next = censuses[era].ctrs;
290 censuses[era].ctrs = ctr;
292 ctr->c.ldv.drag_total -= (int)size;
300 /* --------------------------------------------------------------------------
301 * Initialize censuses[era];
302 * ----------------------------------------------------------------------- */
304 initEra(Census *census)
306 census->hash = allocHashTable();
308 census->arena = newArena();
310 census->not_used = 0;
313 census->void_total = 0;
314 census->drag_total = 0;
318 freeEra(Census *census)
320 arenaFree(census->arena);
321 freeHashTable(census->hash, NULL);
324 /* --------------------------------------------------------------------------
325 * Increases era by 1 and initialize census[era].
326 * Reallocates gi[] and increases its size if needed.
327 * ----------------------------------------------------------------------- */
332 if (doingLDVProfiling()) {
335 if (era == max_era) {
336 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
337 stg_exit(EXIT_FAILURE);
340 if (era == n_censuses) {
342 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
346 #endif /* PROFILING */
348 initEra( &censuses[era] );
351 /* -----------------------------------------------------------------------------
352 * DEBUG heap profiling, by info table
353 * -------------------------------------------------------------------------- */
355 #ifdef DEBUG_HEAP_PROF
357 static char *hp_filename;
359 void initProfiling1( void )
363 void freeProfiling1( void )
367 void initProfiling2( void )
369 if (RtsFlags.ProfFlags.doHeapProfile) {
370 /* Initialise the log file name */
371 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
372 sprintf(hp_filename, "%s.hp", prog_name);
374 /* open the log file */
375 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
376 debugBelch("Can't open profiling report file %s\n",
378 RtsFlags.ProfFlags.doHeapProfile = 0;
386 void endProfiling( void )
390 #endif /* DEBUG_HEAP_PROF */
393 printSample(rtsBool beginSample, StgDouble sampleValue)
395 StgDouble fractionalPart, integralPart;
396 fractionalPart = modf(sampleValue, &integralPart);
397 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
398 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
399 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
402 /* --------------------------------------------------------------------------
403 * Initialize the heap profilier
404 * ----------------------------------------------------------------------- */
406 initHeapProfiling(void)
408 if (! RtsFlags.ProfFlags.doHeapProfile) {
413 if (doingLDVProfiling() && doingRetainerProfiling()) {
414 errorBelch("cannot mix -hb and -hr");
415 stg_exit(EXIT_FAILURE);
419 // we only count eras if we're doing LDV profiling. Otherwise era
422 if (doingLDVProfiling()) {
430 { // max_era = 2^LDV_SHIFT
433 for (p = 0; p < LDV_SHIFT; p++)
438 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
440 initEra( &censuses[era] );
442 /* initProfilingLogFile(); */
443 fprintf(hp_file, "JOB \"%s", prog_name);
448 for(count = 1; count < prog_argc; count++)
449 fprintf(hp_file, " %s", prog_argv[count]);
450 fprintf(hp_file, " +RTS");
451 for(count = 0; count < rts_argc; count++)
452 fprintf(hp_file, " %s", rts_argv[count]);
454 #endif /* PROFILING */
456 fprintf(hp_file, "\"\n" );
458 fprintf(hp_file, "DATE \"%s\"\n", time_str());
460 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
461 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
463 printSample(rtsTrue, 0);
464 printSample(rtsFalse, 0);
466 #ifdef DEBUG_HEAP_PROF
467 DEBUG_LoadSymbols(prog_name);
471 if (doingRetainerProfiling()) {
472 initRetainerProfiling();
480 endHeapProfiling(void)
484 if (! RtsFlags.ProfFlags.doHeapProfile) {
489 if (doingRetainerProfiling()) {
490 endRetainerProfiling();
495 if (doingLDVProfiling()) {
498 aggregateCensusInfo();
499 for (t = 1; t < era; t++) {
500 dumpCensus( &censuses[t] );
507 for (t = 0; t <= era; t++) {
508 freeEra( &censuses[t] );
513 seconds = mut_user_time();
514 printSample(rtsTrue, seconds);
515 printSample(rtsFalse, seconds);
523 buf_append(char *p, const char *q, char *end)
527 for (m = 0; p < end; p++, q++, m++) {
529 if (*q == '\0') { break; }
535 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
537 char buf[max_length+1], *p, *buf_end;
539 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
540 if (ccs == CCS_MAIN) {
545 fprintf(fp, "(%ld)", ccs->ccsID);
548 buf_end = buf + max_length + 1;
550 // keep printing components of the stack until we run out of space
551 // in the buffer. If we run out of space, end with "...".
552 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
554 // CAF cost centres print as M.CAF, but we leave the module
555 // name out of all the others to save space.
556 if (!strcmp(ccs->cc->label,"CAF")) {
557 p += buf_append(p, ccs->cc->module, buf_end);
558 p += buf_append(p, ".CAF", buf_end);
560 p += buf_append(p, ccs->cc->label, buf_end);
561 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
562 p += buf_append(p, "/", buf_end);
567 sprintf(buf+max_length-4, "...");
571 fprintf(fp, "%s", buf);
573 #endif /* PROFILING */
576 strMatchesSelector( char* str, char* sel )
579 // debugBelch("str_matches_selector %s %s\n", str, sel);
581 // Compare str against wherever we've got to in sel.
583 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
586 // Match if all of str used and have reached the end of a sel fragment.
587 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
590 // No match. Advance sel to the start of the next elem.
591 while (*sel != ',' && *sel != '\0') sel++;
592 if (*sel == ',') sel++;
594 /* Run out of sel ?? */
595 if (*sel == '\0') return rtsFalse;
599 /* -----------------------------------------------------------------------------
600 * Figure out whether a closure should be counted in this census, by
601 * testing against all the specified constraints.
602 * -------------------------------------------------------------------------- */
604 closureSatisfiesConstraints( StgClosure* p )
606 #ifdef DEBUG_HEAP_PROF
607 (void)p; /* keep gcc -Wall happy */
612 // The CCS has a selected field to indicate whether this closure is
613 // deselected by not being mentioned in the module, CC, or CCS
615 if (!p->header.prof.ccs->selected) {
619 if (RtsFlags.ProfFlags.descrSelector) {
620 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_desc,
621 RtsFlags.ProfFlags.descrSelector );
622 if (!b) return rtsFalse;
624 if (RtsFlags.ProfFlags.typeSelector) {
625 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_type,
626 RtsFlags.ProfFlags.typeSelector );
627 if (!b) return rtsFalse;
629 if (RtsFlags.ProfFlags.retainerSelector) {
632 // We must check that the retainer set is valid here. One
633 // reason it might not be valid is if this closure is a
634 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
635 // these aren't reached by the retainer profiler's traversal.
636 if (isRetainerSetFieldValid((StgClosure *)p)) {
637 rs = retainerSetOf((StgClosure *)p);
639 for (i = 0; i < rs->num; i++) {
640 b = strMatchesSelector( rs->element[i]->cc->label,
641 RtsFlags.ProfFlags.retainerSelector );
642 if (b) return rtsTrue;
649 #endif /* PROFILING */
652 /* -----------------------------------------------------------------------------
653 * Aggregate the heap census info for biographical profiling
654 * -------------------------------------------------------------------------- */
657 aggregateCensusInfo( void )
661 counter *c, *d, *ctrs;
664 if (!doingLDVProfiling()) return;
666 // Aggregate the LDV counters when displaying by biography.
667 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
668 int void_total, drag_total;
670 // Now we compute void_total and drag_total for each census
671 // After the program has finished, the void_total field of
672 // each census contains the count of words that were *created*
673 // in this era and were eventually void. Conversely, if a
674 // void closure was destroyed in this era, it will be
675 // represented by a negative count of words in void_total.
677 // To get the count of live words that are void at each
678 // census, just propagate the void_total count forwards:
682 for (t = 1; t < era; t++) { // note: start at 1, not 0
683 void_total += censuses[t].void_total;
684 drag_total += censuses[t].drag_total;
685 censuses[t].void_total = void_total;
686 censuses[t].drag_total = drag_total;
688 ASSERT( censuses[t].void_total <= censuses[t].not_used );
689 // should be true because: void_total is the count of
690 // live words that are void at this census, which *must*
691 // be less than the number of live words that have not
694 ASSERT( censuses[t].drag_total <= censuses[t].used );
695 // similar reasoning as above.
701 // otherwise... we're doing a heap profile that is restricted to
702 // some combination of lag, drag, void or use. We've kept all the
703 // census info for all censuses so far, but we still need to
704 // aggregate the counters forwards.
707 acc = allocHashTable();
710 for (t = 1; t < era; t++) {
712 // first look through all the counters we're aggregating
713 for (c = ctrs; c != NULL; c = c->next) {
714 // if one of the totals is non-zero, then this closure
715 // type must be present in the heap at this census time...
716 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
719 // if this closure identity isn't present in the
720 // census for this time period, then our running
721 // totals *must* be zero.
722 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
724 // debugCCS(c->identity);
725 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
726 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
728 d->c.ldv.void_total += c->c.ldv.void_total;
729 d->c.ldv.drag_total += c->c.ldv.drag_total;
730 c->c.ldv.void_total = d->c.ldv.void_total;
731 c->c.ldv.drag_total = d->c.ldv.drag_total;
733 ASSERT( c->c.ldv.void_total >= 0 );
734 ASSERT( c->c.ldv.drag_total >= 0 );
738 // now look through the counters in this census to find new ones
739 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
740 d = lookupHashTable(acc, (StgWord)c->identity);
742 d = arenaAlloc( arena, sizeof(counter) );
744 insertHashTable( acc, (StgWord)c->identity, d );
745 d->identity = c->identity;
748 d->c.ldv.void_total = c->c.ldv.void_total;
749 d->c.ldv.drag_total = c->c.ldv.drag_total;
751 ASSERT( c->c.ldv.void_total >= 0 );
752 ASSERT( c->c.ldv.drag_total >= 0 );
756 freeHashTable(acc, NULL);
761 /* -----------------------------------------------------------------------------
762 * Print out the results of a heap census.
763 * -------------------------------------------------------------------------- */
765 dumpCensus( Census *census )
770 printSample(rtsTrue, census->time);
773 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
774 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
775 fprintf(hp_file, "LAG\t%lu\n",
776 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
777 fprintf(hp_file, "USE\t%lu\n",
778 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
779 fprintf(hp_file, "INHERENT_USE\t%lu\n",
780 (unsigned long)(census->prim) * sizeof(W_));
781 fprintf(hp_file, "DRAG\t%lu\n",
782 (unsigned long)(census->drag_total) * sizeof(W_));
783 printSample(rtsFalse, census->time);
788 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
791 if (RtsFlags.ProfFlags.bioSelector != NULL) {
793 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
794 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
795 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
796 count += ctr->c.ldv.drag_total;
797 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
798 count += ctr->c.ldv.void_total;
799 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
800 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
804 count = ctr->c.resid;
807 ASSERT( count >= 0 );
809 if (count == 0) continue;
811 #ifdef DEBUG_HEAP_PROF
812 switch (RtsFlags.ProfFlags.doHeapProfile) {
813 case HEAP_BY_INFOPTR:
814 fprintf(hp_file, "%s", lookupGHCName(ctr->identity));
816 case HEAP_BY_CLOSURE_TYPE:
817 fprintf(hp_file, "%s", (char *)ctr->identity);
823 switch (RtsFlags.ProfFlags.doHeapProfile) {
825 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
830 fprintf(hp_file, "%s", (char *)ctr->identity);
832 case HEAP_BY_RETAINER:
834 RetainerSet *rs = (RetainerSet *)ctr->identity;
836 // it might be the distinguished retainer set rs_MANY:
837 if (rs == &rs_MANY) {
838 fprintf(hp_file, "MANY");
842 // Mark this retainer set by negating its id, because it
843 // has appeared in at least one census. We print the
844 // values of all such retainer sets into the log file at
845 // the end. A retainer set may exist but not feature in
846 // any censuses if it arose as the intermediate retainer
847 // set for some closure during retainer set calculation.
851 // report in the unit of bytes: * sizeof(StgWord)
852 printRetainerSetShort(hp_file, rs);
856 barf("dumpCensus; doHeapProfile");
860 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
863 printSample(rtsFalse, census->time);
866 /* -----------------------------------------------------------------------------
867 * Code to perform a heap census.
868 * -------------------------------------------------------------------------- */
870 heapCensusChain( Census *census, bdescr *bd )
880 for (; bd != NULL; bd = bd->link) {
882 // HACK: ignore pinned blocks, because they contain gaps.
883 // It's not clear exactly what we'd like to do here, since we
884 // can't tell which objects in the block are actually alive.
885 // Perhaps the whole block should be counted as SYSTEM memory.
886 if (bd->flags & BF_PINNED) {
891 while (p < bd->free) {
892 info = get_itbl((StgClosure *)p);
895 switch (info->type) {
898 size = thunk_sizeW_fromITBL(info);
904 size = sizeofW(StgThunkHeader) + 2;
910 size = sizeofW(StgThunkHeader) + 1;
917 case IND_OLDGEN_PERM:
919 case SE_CAF_BLACKHOLE:
932 size = sizeW_fromITBL(info);
936 // Special case/Delicate Hack: INDs don't normally
937 // appear, since we're doing this heap census right
938 // after GC. However, GarbageCollect() also does
939 // resurrectThreads(), which can update some
940 // blackholes when it calls raiseAsync() on the
941 // resurrected threads. So we know that any IND will
942 // be the size of a BLACKHOLE.
943 size = BLACKHOLE_sizeW();
948 size = bco_sizeW((StgBCO *)p);
957 size = sizeW_fromITBL(info);
961 size = ap_sizeW((StgAP *)p);
965 size = pap_sizeW((StgPAP *)p);
969 size = ap_stack_sizeW((StgAP_STACK *)p);
974 size = arr_words_sizeW(stgCast(StgArrWords*,p));
977 case MUT_ARR_PTRS_CLEAN:
978 case MUT_ARR_PTRS_DIRTY:
979 case MUT_ARR_PTRS_FROZEN:
980 case MUT_ARR_PTRS_FROZEN0:
982 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
987 #ifdef DEBUG_HEAP_PROF
988 size = tso_sizeW((StgTSO *)p);
991 if (RtsFlags.ProfFlags.includeTSOs) {
992 size = tso_sizeW((StgTSO *)p);
995 // Skip this TSO and move on to the next object
996 p += tso_sizeW((StgTSO *)p);
1003 size = sizeofW(StgTRecHeader);
1006 case TVAR_WATCH_QUEUE:
1008 size = sizeofW(StgTVarWatchQueue);
1011 case INVARIANT_CHECK_QUEUE:
1013 size = sizeofW(StgInvariantCheckQueue);
1016 case ATOMIC_INVARIANT:
1018 size = sizeofW(StgAtomicInvariant);
1023 size = sizeofW(StgTVar);
1028 size = sizeofW(StgTRecChunk);
1032 barf("heapCensus, unknown object: %d", info->type);
1037 #ifdef DEBUG_HEAP_PROF
1040 // subtract the profiling overhead
1041 real_size = size - sizeofW(StgProfHeader);
1044 if (closureSatisfiesConstraints((StgClosure*)p)) {
1046 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1048 census->prim += real_size;
1049 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1050 census->not_used += real_size;
1052 census->used += real_size;
1056 identity = closureIdentity((StgClosure *)p);
1058 if (identity != NULL) {
1059 ctr = lookupHashTable( census->hash, (StgWord)identity );
1062 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1064 ctr->c.ldv.prim += real_size;
1065 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1066 ctr->c.ldv.not_used += real_size;
1068 ctr->c.ldv.used += real_size;
1072 ctr->c.resid += real_size;
1075 ctr = arenaAlloc( census->arena, sizeof(counter) );
1077 insertHashTable( census->hash, (StgWord)identity, ctr );
1078 ctr->identity = identity;
1079 ctr->next = census->ctrs;
1083 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1085 ctr->c.ldv.prim = real_size;
1086 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1087 ctr->c.ldv.not_used = real_size;
1089 ctr->c.ldv.used = real_size;
1093 ctr->c.resid = real_size;
1111 census = &censuses[era];
1112 census->time = mut_user_time();
1114 // calculate retainer sets if necessary
1116 if (doingRetainerProfiling()) {
1122 stat_startHeapCensus();
1125 // Traverse the heap, collecting the census info
1127 // First the small_alloc_list: we have to fix the free pointer at
1128 // the end by calling tidyAllocatedLists() first.
1129 tidyAllocateLists();
1130 heapCensusChain( census, small_alloc_list );
1132 // Now traverse the heap in each generation/step.
1133 if (RtsFlags.GcFlags.generations == 1) {
1134 heapCensusChain( census, g0s0->blocks );
1136 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1137 for (s = 0; s < generations[g].n_steps; s++) {
1138 heapCensusChain( census, generations[g].steps[s].blocks );
1139 // Are we interested in large objects? might be
1140 // confusing to include the stack in a heap profile.
1141 heapCensusChain( census, generations[g].steps[s].large_objects );
1146 // dump out the census info
1148 // We can't generate any info for LDV profiling until
1149 // the end of the run...
1150 if (!doingLDVProfiling())
1151 dumpCensus( census );
1153 dumpCensus( census );
1157 // free our storage, unless we're keeping all the census info for
1158 // future restriction by biography.
1160 if (RtsFlags.ProfFlags.bioSelector == NULL)
1163 freeHashTable( census->hash, NULL/* don't free the elements */ );
1164 arenaFree( census->arena );
1165 census->hash = NULL;
1166 census->arena = NULL;
1169 // we're into the next time period now
1173 stat_endHeapCensus();
1177 #endif /* PROFILING || DEBUG_HEAP_PROF */