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;
320 freeEra(Census *census)
322 arenaFree(census->arena);
323 freeHashTable(census->hash, NULL);
326 /* --------------------------------------------------------------------------
327 * Increases era by 1 and initialize census[era].
328 * Reallocates gi[] and increases its size if needed.
329 * ----------------------------------------------------------------------- */
334 if (doingLDVProfiling()) {
337 if (era == max_era) {
338 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
339 stg_exit(EXIT_FAILURE);
342 if (era == n_censuses) {
344 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
348 #endif /* PROFILING */
350 initEra( &censuses[era] );
353 /* -----------------------------------------------------------------------------
354 * DEBUG heap profiling, by info table
355 * -------------------------------------------------------------------------- */
357 #ifdef DEBUG_HEAP_PROF
359 static char *hp_filename;
361 void initProfiling1( void )
365 void freeProfiling1( void )
369 void initProfiling2( void )
371 if (RtsFlags.ProfFlags.doHeapProfile) {
372 /* Initialise the log file name */
373 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
374 sprintf(hp_filename, "%s.hp", prog_name);
376 /* open the log file */
377 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
378 debugBelch("Can't open profiling report file %s\n",
380 RtsFlags.ProfFlags.doHeapProfile = 0;
388 void endProfiling( void )
392 #endif /* DEBUG_HEAP_PROF */
395 printSample(rtsBool beginSample, StgDouble sampleValue)
397 StgDouble fractionalPart, integralPart;
398 fractionalPart = modf(sampleValue, &integralPart);
399 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
400 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
401 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
404 /* --------------------------------------------------------------------------
405 * Initialize the heap profilier
406 * ----------------------------------------------------------------------- */
408 initHeapProfiling(void)
410 if (! RtsFlags.ProfFlags.doHeapProfile) {
415 if (doingLDVProfiling() && doingRetainerProfiling()) {
416 errorBelch("cannot mix -hb and -hr");
417 stg_exit(EXIT_FAILURE);
421 // we only count eras if we're doing LDV profiling. Otherwise era
424 if (doingLDVProfiling()) {
432 { // max_era = 2^LDV_SHIFT
435 for (p = 0; p < LDV_SHIFT; p++)
440 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
442 initEra( &censuses[era] );
444 /* initProfilingLogFile(); */
445 fprintf(hp_file, "JOB \"%s", prog_name);
450 for(count = 1; count < prog_argc; count++)
451 fprintf(hp_file, " %s", prog_argv[count]);
452 fprintf(hp_file, " +RTS");
453 for(count = 0; count < rts_argc; count++)
454 fprintf(hp_file, " %s", rts_argv[count]);
456 #endif /* PROFILING */
458 fprintf(hp_file, "\"\n" );
460 fprintf(hp_file, "DATE \"%s\"\n", time_str());
462 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
463 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
465 printSample(rtsTrue, 0);
466 printSample(rtsFalse, 0);
468 #ifdef DEBUG_HEAP_PROF
469 DEBUG_LoadSymbols(prog_name);
473 if (doingRetainerProfiling()) {
474 initRetainerProfiling();
482 endHeapProfiling(void)
486 if (! RtsFlags.ProfFlags.doHeapProfile) {
491 if (doingRetainerProfiling()) {
492 endRetainerProfiling();
497 if (doingLDVProfiling()) {
500 aggregateCensusInfo();
501 for (t = 1; t < era; t++) {
502 dumpCensus( &censuses[t] );
509 for (t = 0; t <= era; t++) {
510 freeEra( &censuses[t] );
515 seconds = mut_user_time();
516 printSample(rtsTrue, seconds);
517 printSample(rtsFalse, seconds);
525 buf_append(char *p, const char *q, char *end)
529 for (m = 0; p < end; p++, q++, m++) {
531 if (*q == '\0') { break; }
537 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
539 char buf[max_length+1], *p, *buf_end;
541 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
542 if (ccs == CCS_MAIN) {
547 fprintf(fp, "(%ld)", ccs->ccsID);
550 buf_end = buf + max_length + 1;
552 // keep printing components of the stack until we run out of space
553 // in the buffer. If we run out of space, end with "...".
554 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
556 // CAF cost centres print as M.CAF, but we leave the module
557 // name out of all the others to save space.
558 if (!strcmp(ccs->cc->label,"CAF")) {
559 p += buf_append(p, ccs->cc->module, buf_end);
560 p += buf_append(p, ".CAF", buf_end);
562 p += buf_append(p, ccs->cc->label, buf_end);
563 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
564 p += buf_append(p, "/", buf_end);
569 sprintf(buf+max_length-4, "...");
573 fprintf(fp, "%s", buf);
575 #endif /* PROFILING */
578 strMatchesSelector( char* str, char* sel )
581 // debugBelch("str_matches_selector %s %s\n", str, sel);
583 // Compare str against wherever we've got to in sel.
585 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
588 // Match if all of str used and have reached the end of a sel fragment.
589 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
592 // No match. Advance sel to the start of the next elem.
593 while (*sel != ',' && *sel != '\0') sel++;
594 if (*sel == ',') sel++;
596 /* Run out of sel ?? */
597 if (*sel == '\0') return rtsFalse;
601 /* -----------------------------------------------------------------------------
602 * Figure out whether a closure should be counted in this census, by
603 * testing against all the specified constraints.
604 * -------------------------------------------------------------------------- */
606 closureSatisfiesConstraints( StgClosure* p )
608 #ifdef DEBUG_HEAP_PROF
609 (void)p; /* keep gcc -Wall happy */
614 // The CCS has a selected field to indicate whether this closure is
615 // deselected by not being mentioned in the module, CC, or CCS
617 if (!p->header.prof.ccs->selected) {
621 if (RtsFlags.ProfFlags.descrSelector) {
622 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_desc,
623 RtsFlags.ProfFlags.descrSelector );
624 if (!b) return rtsFalse;
626 if (RtsFlags.ProfFlags.typeSelector) {
627 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_type,
628 RtsFlags.ProfFlags.typeSelector );
629 if (!b) return rtsFalse;
631 if (RtsFlags.ProfFlags.retainerSelector) {
634 // We must check that the retainer set is valid here. One
635 // reason it might not be valid is if this closure is a
636 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
637 // these aren't reached by the retainer profiler's traversal.
638 if (isRetainerSetFieldValid((StgClosure *)p)) {
639 rs = retainerSetOf((StgClosure *)p);
641 for (i = 0; i < rs->num; i++) {
642 b = strMatchesSelector( rs->element[i]->cc->label,
643 RtsFlags.ProfFlags.retainerSelector );
644 if (b) return rtsTrue;
651 #endif /* PROFILING */
654 /* -----------------------------------------------------------------------------
655 * Aggregate the heap census info for biographical profiling
656 * -------------------------------------------------------------------------- */
659 aggregateCensusInfo( void )
663 counter *c, *d, *ctrs;
666 if (!doingLDVProfiling()) return;
668 // Aggregate the LDV counters when displaying by biography.
669 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
670 int void_total, drag_total;
672 // Now we compute void_total and drag_total for each census
673 // After the program has finished, the void_total field of
674 // each census contains the count of words that were *created*
675 // in this era and were eventually void. Conversely, if a
676 // void closure was destroyed in this era, it will be
677 // represented by a negative count of words in void_total.
679 // To get the count of live words that are void at each
680 // census, just propagate the void_total count forwards:
684 for (t = 1; t < era; t++) { // note: start at 1, not 0
685 void_total += censuses[t].void_total;
686 drag_total += censuses[t].drag_total;
687 censuses[t].void_total = void_total;
688 censuses[t].drag_total = drag_total;
690 ASSERT( censuses[t].void_total <= censuses[t].not_used );
691 // should be true because: void_total is the count of
692 // live words that are void at this census, which *must*
693 // be less than the number of live words that have not
696 ASSERT( censuses[t].drag_total <= censuses[t].used );
697 // similar reasoning as above.
703 // otherwise... we're doing a heap profile that is restricted to
704 // some combination of lag, drag, void or use. We've kept all the
705 // census info for all censuses so far, but we still need to
706 // aggregate the counters forwards.
709 acc = allocHashTable();
712 for (t = 1; t < era; t++) {
714 // first look through all the counters we're aggregating
715 for (c = ctrs; c != NULL; c = c->next) {
716 // if one of the totals is non-zero, then this closure
717 // type must be present in the heap at this census time...
718 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
721 // if this closure identity isn't present in the
722 // census for this time period, then our running
723 // totals *must* be zero.
724 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
726 // debugCCS(c->identity);
727 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
728 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
730 d->c.ldv.void_total += c->c.ldv.void_total;
731 d->c.ldv.drag_total += c->c.ldv.drag_total;
732 c->c.ldv.void_total = d->c.ldv.void_total;
733 c->c.ldv.drag_total = d->c.ldv.drag_total;
735 ASSERT( c->c.ldv.void_total >= 0 );
736 ASSERT( c->c.ldv.drag_total >= 0 );
740 // now look through the counters in this census to find new ones
741 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
742 d = lookupHashTable(acc, (StgWord)c->identity);
744 d = arenaAlloc( arena, sizeof(counter) );
746 insertHashTable( acc, (StgWord)c->identity, d );
747 d->identity = c->identity;
750 d->c.ldv.void_total = c->c.ldv.void_total;
751 d->c.ldv.drag_total = c->c.ldv.drag_total;
753 ASSERT( c->c.ldv.void_total >= 0 );
754 ASSERT( c->c.ldv.drag_total >= 0 );
758 freeHashTable(acc, NULL);
763 /* -----------------------------------------------------------------------------
764 * Print out the results of a heap census.
765 * -------------------------------------------------------------------------- */
767 dumpCensus( Census *census )
772 printSample(rtsTrue, census->time);
775 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
776 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
777 fprintf(hp_file, "LAG\t%lu\n",
778 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
779 fprintf(hp_file, "USE\t%lu\n",
780 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
781 fprintf(hp_file, "INHERENT_USE\t%lu\n",
782 (unsigned long)(census->prim) * sizeof(W_));
783 fprintf(hp_file, "DRAG\t%lu\n",
784 (unsigned long)(census->drag_total) * sizeof(W_));
785 printSample(rtsFalse, census->time);
790 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
793 if (RtsFlags.ProfFlags.bioSelector != NULL) {
795 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
796 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
797 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
798 count += ctr->c.ldv.drag_total;
799 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
800 count += ctr->c.ldv.void_total;
801 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
802 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
806 count = ctr->c.resid;
809 ASSERT( count >= 0 );
811 if (count == 0) continue;
813 #ifdef DEBUG_HEAP_PROF
814 switch (RtsFlags.ProfFlags.doHeapProfile) {
815 case HEAP_BY_INFOPTR:
816 fprintf(hp_file, "%s", lookupGHCName(ctr->identity));
818 case HEAP_BY_CLOSURE_TYPE:
819 fprintf(hp_file, "%s", (char *)ctr->identity);
825 switch (RtsFlags.ProfFlags.doHeapProfile) {
827 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
832 fprintf(hp_file, "%s", (char *)ctr->identity);
834 case HEAP_BY_RETAINER:
836 RetainerSet *rs = (RetainerSet *)ctr->identity;
838 // it might be the distinguished retainer set rs_MANY:
839 if (rs == &rs_MANY) {
840 fprintf(hp_file, "MANY");
844 // Mark this retainer set by negating its id, because it
845 // has appeared in at least one census. We print the
846 // values of all such retainer sets into the log file at
847 // the end. A retainer set may exist but not feature in
848 // any censuses if it arose as the intermediate retainer
849 // set for some closure during retainer set calculation.
853 // report in the unit of bytes: * sizeof(StgWord)
854 printRetainerSetShort(hp_file, rs);
858 barf("dumpCensus; doHeapProfile");
862 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
865 printSample(rtsFalse, census->time);
868 /* -----------------------------------------------------------------------------
869 * Code to perform a heap census.
870 * -------------------------------------------------------------------------- */
872 heapCensusChain( Census *census, bdescr *bd )
882 for (; bd != NULL; bd = bd->link) {
884 // HACK: ignore pinned blocks, because they contain gaps.
885 // It's not clear exactly what we'd like to do here, since we
886 // can't tell which objects in the block are actually alive.
887 // Perhaps the whole block should be counted as SYSTEM memory.
888 if (bd->flags & BF_PINNED) {
893 while (p < bd->free) {
894 info = get_itbl((StgClosure *)p);
897 switch (info->type) {
900 size = thunk_sizeW_fromITBL(info);
906 size = sizeofW(StgThunkHeader) + 2;
912 size = sizeofW(StgThunkHeader) + 1;
919 case IND_OLDGEN_PERM:
921 case SE_CAF_BLACKHOLE:
934 size = sizeW_fromITBL(info);
938 // Special case/Delicate Hack: INDs don't normally
939 // appear, since we're doing this heap census right
940 // after GC. However, GarbageCollect() also does
941 // resurrectThreads(), which can update some
942 // blackholes when it calls raiseAsync() on the
943 // resurrected threads. So we know that any IND will
944 // be the size of a BLACKHOLE.
945 size = BLACKHOLE_sizeW();
950 size = bco_sizeW((StgBCO *)p);
959 size = sizeW_fromITBL(info);
963 size = ap_sizeW((StgAP *)p);
967 size = pap_sizeW((StgPAP *)p);
971 size = ap_stack_sizeW((StgAP_STACK *)p);
976 size = arr_words_sizeW(stgCast(StgArrWords*,p));
979 case MUT_ARR_PTRS_CLEAN:
980 case MUT_ARR_PTRS_DIRTY:
981 case MUT_ARR_PTRS_FROZEN:
982 case MUT_ARR_PTRS_FROZEN0:
984 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
989 #ifdef DEBUG_HEAP_PROF
990 size = tso_sizeW((StgTSO *)p);
993 if (RtsFlags.ProfFlags.includeTSOs) {
994 size = tso_sizeW((StgTSO *)p);
997 // Skip this TSO and move on to the next object
998 p += tso_sizeW((StgTSO *)p);
1005 size = sizeofW(StgTRecHeader);
1008 case TVAR_WATCH_QUEUE:
1010 size = sizeofW(StgTVarWatchQueue);
1013 case INVARIANT_CHECK_QUEUE:
1015 size = sizeofW(StgInvariantCheckQueue);
1018 case ATOMIC_INVARIANT:
1020 size = sizeofW(StgAtomicInvariant);
1025 size = sizeofW(StgTVar);
1030 size = sizeofW(StgTRecChunk);
1034 barf("heapCensus, unknown object: %d", info->type);
1039 #ifdef DEBUG_HEAP_PROF
1042 // subtract the profiling overhead
1043 real_size = size - sizeofW(StgProfHeader);
1046 if (closureSatisfiesConstraints((StgClosure*)p)) {
1048 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1050 census->prim += real_size;
1051 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1052 census->not_used += real_size;
1054 census->used += real_size;
1058 identity = closureIdentity((StgClosure *)p);
1060 if (identity != NULL) {
1061 ctr = lookupHashTable( census->hash, (StgWord)identity );
1064 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1066 ctr->c.ldv.prim += real_size;
1067 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1068 ctr->c.ldv.not_used += real_size;
1070 ctr->c.ldv.used += real_size;
1074 ctr->c.resid += real_size;
1077 ctr = arenaAlloc( census->arena, sizeof(counter) );
1079 insertHashTable( census->hash, (StgWord)identity, ctr );
1080 ctr->identity = identity;
1081 ctr->next = census->ctrs;
1085 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1087 ctr->c.ldv.prim = real_size;
1088 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1089 ctr->c.ldv.not_used = real_size;
1091 ctr->c.ldv.used = real_size;
1095 ctr->c.resid = real_size;
1113 census = &censuses[era];
1114 census->time = mut_user_time();
1116 // calculate retainer sets if necessary
1118 if (doingRetainerProfiling()) {
1124 stat_startHeapCensus();
1127 // Traverse the heap, collecting the census info
1129 // First the small_alloc_list: we have to fix the free pointer at
1130 // the end by calling tidyAllocatedLists() first.
1131 tidyAllocateLists();
1132 heapCensusChain( census, small_alloc_list );
1134 // Now traverse the heap in each generation/step.
1135 if (RtsFlags.GcFlags.generations == 1) {
1136 heapCensusChain( census, g0s0->blocks );
1138 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1139 for (s = 0; s < generations[g].n_steps; s++) {
1140 heapCensusChain( census, generations[g].steps[s].blocks );
1141 // Are we interested in large objects? might be
1142 // confusing to include the stack in a heap profile.
1143 heapCensusChain( census, generations[g].steps[s].large_objects );
1148 // dump out the census info
1150 // We can't generate any info for LDV profiling until
1151 // the end of the run...
1152 if (!doingLDVProfiling())
1153 dumpCensus( census );
1155 dumpCensus( census );
1159 // free our storage, unless we're keeping all the census info for
1160 // future restriction by biography.
1162 if (RtsFlags.ProfFlags.bioSelector == NULL)
1165 freeHashTable( census->hash, NULL/* don't free the elements */ );
1166 arenaFree( census->arena );
1167 census->hash = NULL;
1168 census->arena = NULL;
1171 // we're into the next time period now
1175 stat_endHeapCensus();
1179 #endif /* PROFILING || DEBUG_HEAP_PROF */