1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2003
5 * Support for heap profiling
7 * --------------------------------------------------------------------------*/
9 #include "PosixSource.h"
13 #include "Profiling.h"
17 #include "RetainerProfile.h"
18 #include "LdvProfile.h"
26 /* -----------------------------------------------------------------------------
27 * era stores the current time period. It is the same as the
28 * number of censuses that have been performed.
31 * era must be no longer than LDV_SHIFT (15 or 30) bits.
33 * era is initialized to 1 in initHeapProfiling().
35 * max_era is initialized to 2^LDV_SHIFT in initHeapProfiling().
36 * When era reaches max_era, the profiling stops because a closure can
37 * store only up to (max_era - 1) as its creation or last use time.
38 * -------------------------------------------------------------------------- */
42 /* -----------------------------------------------------------------------------
45 * For most heap profiles each closure identity gets a simple count
46 * of live words in the heap at each census. However, if we're
47 * selecting by biography, then we have to keep the various
48 * lag/drag/void counters for each identity.
49 * -------------------------------------------------------------------------- */
50 typedef struct _counter {
55 int prim; // total size of 'inherently used' closures
56 int not_used; // total size of 'never used' closures
57 int used; // total size of 'used at least once' closures
58 int void_total; // current total size of 'destroyed without being used' closures
59 int drag_total; // current total size of 'used at least once and waiting to die'
62 struct _counter *next;
66 initLDVCtr( counter *ctr )
69 ctr->c.ldv.not_used = 0;
71 ctr->c.ldv.void_total = 0;
72 ctr->c.ldv.drag_total = 0;
76 double time; // the time in MUT time when the census is made
81 // for LDV profiling, when just displaying by LDV
89 static Census *censuses = NULL;
90 static nat n_censuses = 0;
93 static void aggregateCensusInfo( void );
96 static void dumpCensus( Census *census );
98 /* ----------------------------------------------------------------------------
99 Closure Type Profiling;
100 ------------------------------------------------------------------------- */
103 static char *type_names[] = {
112 "CONSTR_NOCAF_STATIC",
152 "MUT_ARR_PTRS_CLEAN",
153 "MUT_ARR_PTRS_DIRTY",
154 "MUT_ARR_PTRS_FROZEN0",
155 "MUT_ARR_PTRS_FROZEN",
168 "INVARIANT_CHECK_QUEUE",
180 /* ----------------------------------------------------------------------------
181 * Find the "closure identity", which is a unique pointer reresenting
182 * the band to which this closure's heap space is attributed in the
184 * ------------------------------------------------------------------------- */
186 closureIdentity( StgClosure *p )
188 switch (RtsFlags.ProfFlags.doHeapProfile) {
192 return p->header.prof.ccs;
194 return p->header.prof.ccs->cc->module;
196 return GET_PROF_DESC(get_itbl(p));
198 return GET_PROF_TYPE(get_itbl(p));
199 case HEAP_BY_RETAINER:
200 // AFAIK, the only closures in the heap which might not have a
201 // valid retainer set are DEAD_WEAK closures.
202 if (isRetainerSetFieldValid(p))
203 return retainerSetOf(p);
208 case HEAP_BY_CLOSURE_TYPE:
212 switch (info->type) {
220 case CONSTR_NOCAF_STATIC:
221 return GET_CON_DESC(itbl_to_con_itbl(info));
223 return type_names[info->type];
229 barf("closureIdentity");
233 /* --------------------------------------------------------------------------
234 * Profiling type predicates
235 * ----------------------------------------------------------------------- */
237 STATIC_INLINE rtsBool
238 doingLDVProfiling( void )
240 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
241 || RtsFlags.ProfFlags.bioSelector != NULL);
244 STATIC_INLINE rtsBool
245 doingRetainerProfiling( void )
247 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
248 || RtsFlags.ProfFlags.retainerSelector != NULL);
250 #endif /* PROFILING */
252 // Precesses a closure 'c' being destroyed whose size is 'size'.
253 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
254 // such as TSO; they should not be involved in computing dragNew or voidNew.
256 // Even though era is checked in both LdvCensusForDead() and
257 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
258 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
259 // when a thunk is replaced by an indirection object.
263 LDV_recordDead( StgClosure *c, nat size )
269 if (era > 0 && closureSatisfiesConstraints(c)) {
270 size -= sizeofW(StgProfHeader);
271 ASSERT(LDVW(c) != 0);
272 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
273 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
275 if (RtsFlags.ProfFlags.bioSelector == NULL) {
276 censuses[t].void_total += (int)size;
277 censuses[era].void_total -= (int)size;
278 ASSERT(censuses[t].void_total < censuses[t].not_used);
280 id = closureIdentity(c);
281 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
282 ASSERT( ctr != NULL );
283 ctr->c.ldv.void_total += (int)size;
284 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
286 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
288 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
290 ctr->next = censuses[era].ctrs;
291 censuses[era].ctrs = ctr;
293 ctr->c.ldv.void_total -= (int)size;
297 t = LDVW((c)) & LDV_LAST_MASK;
299 if (RtsFlags.ProfFlags.bioSelector == NULL) {
300 censuses[t+1].drag_total += size;
301 censuses[era].drag_total -= size;
304 id = closureIdentity(c);
305 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
306 ASSERT( ctr != NULL );
307 ctr->c.ldv.drag_total += (int)size;
308 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
310 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
312 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
314 ctr->next = censuses[era].ctrs;
315 censuses[era].ctrs = ctr;
317 ctr->c.ldv.drag_total -= (int)size;
325 /* --------------------------------------------------------------------------
326 * Initialize censuses[era];
327 * ----------------------------------------------------------------------- */
330 initEra(Census *census)
332 census->hash = allocHashTable();
334 census->arena = newArena();
336 census->not_used = 0;
339 census->void_total = 0;
340 census->drag_total = 0;
344 freeEra(Census *census)
346 if (RtsFlags.ProfFlags.bioSelector != NULL)
347 // when bioSelector==NULL, these are freed in heapCensus()
349 arenaFree(census->arena);
350 freeHashTable(census->hash, NULL);
354 /* --------------------------------------------------------------------------
355 * Increases era by 1 and initialize census[era].
356 * Reallocates gi[] and increases its size if needed.
357 * ----------------------------------------------------------------------- */
363 if (doingLDVProfiling()) {
366 if (era == max_era) {
367 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
368 stg_exit(EXIT_FAILURE);
371 if (era == n_censuses) {
373 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
377 #endif /* PROFILING */
379 initEra( &censuses[era] );
382 /* ----------------------------------------------------------------------------
383 * Heap profiling by info table
384 * ------------------------------------------------------------------------- */
386 #if !defined(PROFILING)
388 static char *hp_filename;
390 void initProfiling1 (void)
394 void freeProfiling1 (void)
398 void initProfiling2 (void)
402 prog = stgMallocBytes(strlen(prog_name) + 1, "initProfiling2");
403 strcpy(prog, prog_name);
404 #ifdef mingw32_HOST_OS
405 // on Windows, drop the .exe suffix if there is one
408 suff = strrchr(prog,'.');
409 if (suff != NULL && !strcmp(suff,".exe")) {
415 if (RtsFlags.ProfFlags.doHeapProfile) {
416 /* Initialise the log file name */
417 hp_filename = stgMallocBytes(strlen(prog) + 6, "hpFileName");
418 sprintf(hp_filename, "%s.hp", prog);
420 /* open the log file */
421 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
422 debugBelch("Can't open profiling report file %s\n",
424 RtsFlags.ProfFlags.doHeapProfile = 0;
434 void endProfiling( void )
438 #endif /* !PROFILING */
441 printSample(rtsBool beginSample, StgDouble sampleValue)
443 StgDouble fractionalPart, integralPart;
444 fractionalPart = modf(sampleValue, &integralPart);
445 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
446 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
447 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
450 /* --------------------------------------------------------------------------
451 * Initialize the heap profilier
452 * ----------------------------------------------------------------------- */
454 initHeapProfiling(void)
456 if (! RtsFlags.ProfFlags.doHeapProfile) {
461 if (doingLDVProfiling() && doingRetainerProfiling()) {
462 errorBelch("cannot mix -hb and -hr");
463 stg_exit(EXIT_FAILURE);
467 // we only count eras if we're doing LDV profiling. Otherwise era
470 if (doingLDVProfiling()) {
478 // max_era = 2^LDV_SHIFT
479 max_era = 1 << LDV_SHIFT;
482 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
484 initEra( &censuses[era] );
486 /* initProfilingLogFile(); */
487 fprintf(hp_file, "JOB \"%s", prog_name);
492 for(count = 1; count < prog_argc; count++)
493 fprintf(hp_file, " %s", prog_argv[count]);
494 fprintf(hp_file, " +RTS");
495 for(count = 0; count < rts_argc; count++)
496 fprintf(hp_file, " %s", rts_argv[count]);
498 #endif /* PROFILING */
500 fprintf(hp_file, "\"\n" );
502 fprintf(hp_file, "DATE \"%s\"\n", time_str());
504 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
505 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
507 printSample(rtsTrue, 0);
508 printSample(rtsFalse, 0);
511 if (doingRetainerProfiling()) {
512 initRetainerProfiling();
520 endHeapProfiling(void)
524 if (! RtsFlags.ProfFlags.doHeapProfile) {
529 if (doingRetainerProfiling()) {
530 endRetainerProfiling();
535 if (doingLDVProfiling()) {
538 aggregateCensusInfo();
539 for (t = 1; t < era; t++) {
540 dumpCensus( &censuses[t] );
546 if (doingLDVProfiling()) {
548 for (t = 1; t <= era; t++) {
549 freeEra( &censuses[t] );
552 freeEra( &censuses[0] );
555 freeEra( &censuses[0] );
560 seconds = mut_user_time();
561 printSample(rtsTrue, seconds);
562 printSample(rtsFalse, seconds);
570 buf_append(char *p, const char *q, char *end)
574 for (m = 0; p < end; p++, q++, m++) {
576 if (*q == '\0') { break; }
582 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
584 char buf[max_length+1], *p, *buf_end;
586 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
587 if (ccs == CCS_MAIN) {
592 fprintf(fp, "(%ld)", ccs->ccsID);
595 buf_end = buf + max_length + 1;
597 // keep printing components of the stack until we run out of space
598 // in the buffer. If we run out of space, end with "...".
599 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
601 // CAF cost centres print as M.CAF, but we leave the module
602 // name out of all the others to save space.
603 if (!strcmp(ccs->cc->label,"CAF")) {
604 p += buf_append(p, ccs->cc->module, buf_end);
605 p += buf_append(p, ".CAF", buf_end);
607 p += buf_append(p, ccs->cc->label, buf_end);
608 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
609 p += buf_append(p, "/", buf_end);
614 sprintf(buf+max_length-4, "...");
618 fprintf(fp, "%s", buf);
620 #endif /* PROFILING */
623 strMatchesSelector( char* str, char* sel )
626 // debugBelch("str_matches_selector %s %s\n", str, sel);
628 // Compare str against wherever we've got to in sel.
630 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
633 // Match if all of str used and have reached the end of a sel fragment.
634 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
637 // No match. Advance sel to the start of the next elem.
638 while (*sel != ',' && *sel != '\0') sel++;
639 if (*sel == ',') sel++;
641 /* Run out of sel ?? */
642 if (*sel == '\0') return rtsFalse;
646 /* -----------------------------------------------------------------------------
647 * Figure out whether a closure should be counted in this census, by
648 * testing against all the specified constraints.
649 * -------------------------------------------------------------------------- */
651 closureSatisfiesConstraints( StgClosure* p )
653 #if !defined(PROFILING)
654 (void)p; /* keep gcc -Wall happy */
659 // The CCS has a selected field to indicate whether this closure is
660 // deselected by not being mentioned in the module, CC, or CCS
662 if (!p->header.prof.ccs->selected) {
666 if (RtsFlags.ProfFlags.descrSelector) {
667 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
668 RtsFlags.ProfFlags.descrSelector );
669 if (!b) return rtsFalse;
671 if (RtsFlags.ProfFlags.typeSelector) {
672 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
673 RtsFlags.ProfFlags.typeSelector );
674 if (!b) return rtsFalse;
676 if (RtsFlags.ProfFlags.retainerSelector) {
679 // We must check that the retainer set is valid here. One
680 // reason it might not be valid is if this closure is a
681 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
682 // these aren't reached by the retainer profiler's traversal.
683 if (isRetainerSetFieldValid((StgClosure *)p)) {
684 rs = retainerSetOf((StgClosure *)p);
686 for (i = 0; i < rs->num; i++) {
687 b = strMatchesSelector( rs->element[i]->cc->label,
688 RtsFlags.ProfFlags.retainerSelector );
689 if (b) return rtsTrue;
696 #endif /* PROFILING */
699 /* -----------------------------------------------------------------------------
700 * Aggregate the heap census info for biographical profiling
701 * -------------------------------------------------------------------------- */
704 aggregateCensusInfo( void )
708 counter *c, *d, *ctrs;
711 if (!doingLDVProfiling()) return;
713 // Aggregate the LDV counters when displaying by biography.
714 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
715 int void_total, drag_total;
717 // Now we compute void_total and drag_total for each census
718 // After the program has finished, the void_total field of
719 // each census contains the count of words that were *created*
720 // in this era and were eventually void. Conversely, if a
721 // void closure was destroyed in this era, it will be
722 // represented by a negative count of words in void_total.
724 // To get the count of live words that are void at each
725 // census, just propagate the void_total count forwards:
729 for (t = 1; t < era; t++) { // note: start at 1, not 0
730 void_total += censuses[t].void_total;
731 drag_total += censuses[t].drag_total;
732 censuses[t].void_total = void_total;
733 censuses[t].drag_total = drag_total;
735 ASSERT( censuses[t].void_total <= censuses[t].not_used );
736 // should be true because: void_total is the count of
737 // live words that are void at this census, which *must*
738 // be less than the number of live words that have not
741 ASSERT( censuses[t].drag_total <= censuses[t].used );
742 // similar reasoning as above.
748 // otherwise... we're doing a heap profile that is restricted to
749 // some combination of lag, drag, void or use. We've kept all the
750 // census info for all censuses so far, but we still need to
751 // aggregate the counters forwards.
754 acc = allocHashTable();
757 for (t = 1; t < era; t++) {
759 // first look through all the counters we're aggregating
760 for (c = ctrs; c != NULL; c = c->next) {
761 // if one of the totals is non-zero, then this closure
762 // type must be present in the heap at this census time...
763 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
766 // if this closure identity isn't present in the
767 // census for this time period, then our running
768 // totals *must* be zero.
769 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
771 // debugCCS(c->identity);
772 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
773 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
775 d->c.ldv.void_total += c->c.ldv.void_total;
776 d->c.ldv.drag_total += c->c.ldv.drag_total;
777 c->c.ldv.void_total = d->c.ldv.void_total;
778 c->c.ldv.drag_total = d->c.ldv.drag_total;
780 ASSERT( c->c.ldv.void_total >= 0 );
781 ASSERT( c->c.ldv.drag_total >= 0 );
785 // now look through the counters in this census to find new ones
786 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
787 d = lookupHashTable(acc, (StgWord)c->identity);
789 d = arenaAlloc( arena, sizeof(counter) );
791 insertHashTable( acc, (StgWord)c->identity, d );
792 d->identity = c->identity;
795 d->c.ldv.void_total = c->c.ldv.void_total;
796 d->c.ldv.drag_total = c->c.ldv.drag_total;
798 ASSERT( c->c.ldv.void_total >= 0 );
799 ASSERT( c->c.ldv.drag_total >= 0 );
803 freeHashTable(acc, NULL);
808 /* -----------------------------------------------------------------------------
809 * Print out the results of a heap census.
810 * -------------------------------------------------------------------------- */
812 dumpCensus( Census *census )
817 printSample(rtsTrue, census->time);
820 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
821 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
822 fprintf(hp_file, "LAG\t%lu\n",
823 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
824 fprintf(hp_file, "USE\t%lu\n",
825 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
826 fprintf(hp_file, "INHERENT_USE\t%lu\n",
827 (unsigned long)(census->prim) * sizeof(W_));
828 fprintf(hp_file, "DRAG\t%lu\n",
829 (unsigned long)(census->drag_total) * sizeof(W_));
830 printSample(rtsFalse, census->time);
835 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
838 if (RtsFlags.ProfFlags.bioSelector != NULL) {
840 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
841 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
842 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
843 count += ctr->c.ldv.drag_total;
844 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
845 count += ctr->c.ldv.void_total;
846 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
847 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
851 count = ctr->c.resid;
854 ASSERT( count >= 0 );
856 if (count == 0) continue;
858 #if !defined(PROFILING)
859 switch (RtsFlags.ProfFlags.doHeapProfile) {
860 case HEAP_BY_CLOSURE_TYPE:
861 fprintf(hp_file, "%s", (char *)ctr->identity);
867 switch (RtsFlags.ProfFlags.doHeapProfile) {
869 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
874 fprintf(hp_file, "%s", (char *)ctr->identity);
876 case HEAP_BY_RETAINER:
878 RetainerSet *rs = (RetainerSet *)ctr->identity;
880 // it might be the distinguished retainer set rs_MANY:
881 if (rs == &rs_MANY) {
882 fprintf(hp_file, "MANY");
886 // Mark this retainer set by negating its id, because it
887 // has appeared in at least one census. We print the
888 // values of all such retainer sets into the log file at
889 // the end. A retainer set may exist but not feature in
890 // any censuses if it arose as the intermediate retainer
891 // set for some closure during retainer set calculation.
895 // report in the unit of bytes: * sizeof(StgWord)
896 printRetainerSetShort(hp_file, rs);
900 barf("dumpCensus; doHeapProfile");
904 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
907 printSample(rtsFalse, census->time);
910 /* -----------------------------------------------------------------------------
911 * Code to perform a heap census.
912 * -------------------------------------------------------------------------- */
914 heapCensusChain( Census *census, bdescr *bd )
924 for (; bd != NULL; bd = bd->link) {
926 // HACK: ignore pinned blocks, because they contain gaps.
927 // It's not clear exactly what we'd like to do here, since we
928 // can't tell which objects in the block are actually alive.
929 // Perhaps the whole block should be counted as SYSTEM memory.
930 if (bd->flags & BF_PINNED) {
935 while (p < bd->free) {
936 info = get_itbl((StgClosure *)p);
939 switch (info->type) {
942 size = thunk_sizeW_fromITBL(info);
948 size = sizeofW(StgThunkHeader) + 2;
954 size = sizeofW(StgThunkHeader) + 1;
961 case IND_OLDGEN_PERM:
963 case SE_CAF_BLACKHOLE:
976 size = sizeW_fromITBL(info);
980 // Special case/Delicate Hack: INDs don't normally
981 // appear, since we're doing this heap census right
982 // after GC. However, GarbageCollect() also does
983 // resurrectThreads(), which can update some
984 // blackholes when it calls raiseAsync() on the
985 // resurrected threads. So we know that any IND will
986 // be the size of a BLACKHOLE.
987 size = BLACKHOLE_sizeW();
992 size = bco_sizeW((StgBCO *)p);
1002 size = sizeW_fromITBL(info);
1006 size = ap_sizeW((StgAP *)p);
1010 size = pap_sizeW((StgPAP *)p);
1014 size = ap_stack_sizeW((StgAP_STACK *)p);
1019 size = arr_words_sizeW(stgCast(StgArrWords*,p));
1022 case MUT_ARR_PTRS_CLEAN:
1023 case MUT_ARR_PTRS_DIRTY:
1024 case MUT_ARR_PTRS_FROZEN:
1025 case MUT_ARR_PTRS_FROZEN0:
1027 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1033 if (RtsFlags.ProfFlags.includeTSOs) {
1034 size = tso_sizeW((StgTSO *)p);
1037 // Skip this TSO and move on to the next object
1038 p += tso_sizeW((StgTSO *)p);
1042 size = tso_sizeW((StgTSO *)p);
1048 size = sizeofW(StgTRecHeader);
1051 case TVAR_WATCH_QUEUE:
1053 size = sizeofW(StgTVarWatchQueue);
1056 case INVARIANT_CHECK_QUEUE:
1058 size = sizeofW(StgInvariantCheckQueue);
1061 case ATOMIC_INVARIANT:
1063 size = sizeofW(StgAtomicInvariant);
1068 size = sizeofW(StgTVar);
1073 size = sizeofW(StgTRecChunk);
1077 barf("heapCensus, unknown object: %d", info->type);
1083 // subtract the profiling overhead
1084 real_size = size - sizeofW(StgProfHeader);
1089 if (closureSatisfiesConstraints((StgClosure*)p)) {
1091 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1093 census->prim += real_size;
1094 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1095 census->not_used += real_size;
1097 census->used += real_size;
1101 identity = closureIdentity((StgClosure *)p);
1103 if (identity != NULL) {
1104 ctr = lookupHashTable( census->hash, (StgWord)identity );
1107 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1109 ctr->c.ldv.prim += real_size;
1110 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1111 ctr->c.ldv.not_used += real_size;
1113 ctr->c.ldv.used += real_size;
1117 ctr->c.resid += real_size;
1120 ctr = arenaAlloc( census->arena, sizeof(counter) );
1122 insertHashTable( census->hash, (StgWord)identity, ctr );
1123 ctr->identity = identity;
1124 ctr->next = census->ctrs;
1128 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1130 ctr->c.ldv.prim = real_size;
1131 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1132 ctr->c.ldv.not_used = real_size;
1134 ctr->c.ldv.used = real_size;
1138 ctr->c.resid = real_size;
1156 census = &censuses[era];
1157 census->time = mut_user_time();
1159 // calculate retainer sets if necessary
1161 if (doingRetainerProfiling()) {
1167 stat_startHeapCensus();
1170 // Traverse the heap, collecting the census info
1171 if (RtsFlags.GcFlags.generations == 1) {
1172 heapCensusChain( census, g0s0->blocks );
1174 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1175 for (s = 0; s < generations[g].n_steps; s++) {
1176 heapCensusChain( census, generations[g].steps[s].blocks );
1177 // Are we interested in large objects? might be
1178 // confusing to include the stack in a heap profile.
1179 heapCensusChain( census, generations[g].steps[s].large_objects );
1184 // dump out the census info
1186 // We can't generate any info for LDV profiling until
1187 // the end of the run...
1188 if (!doingLDVProfiling())
1189 dumpCensus( census );
1191 dumpCensus( census );
1195 // free our storage, unless we're keeping all the census info for
1196 // future restriction by biography.
1198 if (RtsFlags.ProfFlags.bioSelector == NULL)
1200 freeHashTable( census->hash, NULL/* don't free the elements */ );
1201 arenaFree( census->arena );
1202 census->hash = NULL;
1203 census->arena = NULL;
1207 // we're into the next time period now
1211 stat_endHeapCensus();