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",
151 "MUT_ARR_PTRS_CLEAN",
152 "MUT_ARR_PTRS_DIRTY",
153 "MUT_ARR_PTRS_FROZEN0",
154 "MUT_ARR_PTRS_FROZEN",
167 "INVARIANT_CHECK_QUEUE",
179 /* ----------------------------------------------------------------------------
180 * Find the "closure identity", which is a unique pointer reresenting
181 * the band to which this closure's heap space is attributed in the
183 * ------------------------------------------------------------------------- */
185 closureIdentity( StgClosure *p )
187 switch (RtsFlags.ProfFlags.doHeapProfile) {
191 return p->header.prof.ccs;
193 return p->header.prof.ccs->cc->module;
195 return GET_PROF_DESC(get_itbl(p));
197 return GET_PROF_TYPE(get_itbl(p));
198 case HEAP_BY_RETAINER:
199 // AFAIK, the only closures in the heap which might not have a
200 // valid retainer set are DEAD_WEAK closures.
201 if (isRetainerSetFieldValid(p))
202 return retainerSetOf(p);
207 case HEAP_BY_CLOSURE_TYPE:
211 switch (info->type) {
219 case CONSTR_NOCAF_STATIC:
220 printf("",strlen(GET_CON_DESC(itbl_to_con_itbl(info))));
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)
400 if (RtsFlags.ProfFlags.doHeapProfile) {
401 /* Initialise the log file name */
402 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
403 sprintf(hp_filename, "%s.hp", prog_name);
405 /* open the log file */
406 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
407 debugBelch("Can't open profiling report file %s\n",
409 RtsFlags.ProfFlags.doHeapProfile = 0;
417 void endProfiling( void )
421 #endif /* !PROFILING */
424 printSample(rtsBool beginSample, StgDouble sampleValue)
426 StgDouble fractionalPart, integralPart;
427 fractionalPart = modf(sampleValue, &integralPart);
428 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
429 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
430 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
433 /* --------------------------------------------------------------------------
434 * Initialize the heap profilier
435 * ----------------------------------------------------------------------- */
437 initHeapProfiling(void)
439 if (! RtsFlags.ProfFlags.doHeapProfile) {
444 if (doingLDVProfiling() && doingRetainerProfiling()) {
445 errorBelch("cannot mix -hb and -hr");
446 stg_exit(EXIT_FAILURE);
450 // we only count eras if we're doing LDV profiling. Otherwise era
453 if (doingLDVProfiling()) {
461 { // max_era = 2^LDV_SHIFT
464 for (p = 0; p < LDV_SHIFT; p++)
469 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
471 initEra( &censuses[era] );
473 /* initProfilingLogFile(); */
474 fprintf(hp_file, "JOB \"%s", prog_name);
479 for(count = 1; count < prog_argc; count++)
480 fprintf(hp_file, " %s", prog_argv[count]);
481 fprintf(hp_file, " +RTS");
482 for(count = 0; count < rts_argc; count++)
483 fprintf(hp_file, " %s", rts_argv[count]);
485 #endif /* PROFILING */
487 fprintf(hp_file, "\"\n" );
489 fprintf(hp_file, "DATE \"%s\"\n", time_str());
491 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
492 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
494 printSample(rtsTrue, 0);
495 printSample(rtsFalse, 0);
498 if (doingRetainerProfiling()) {
499 initRetainerProfiling();
507 endHeapProfiling(void)
511 if (! RtsFlags.ProfFlags.doHeapProfile) {
516 if (doingRetainerProfiling()) {
517 endRetainerProfiling();
522 if (doingLDVProfiling()) {
525 aggregateCensusInfo();
526 for (t = 1; t < era; t++) {
527 dumpCensus( &censuses[t] );
533 if (doingLDVProfiling()) {
535 for (t = 1; t <= era; t++) {
536 freeEra( &censuses[t] );
539 freeEra( &censuses[0] );
542 freeEra( &censuses[0] );
547 seconds = mut_user_time();
548 printSample(rtsTrue, seconds);
549 printSample(rtsFalse, seconds);
557 buf_append(char *p, const char *q, char *end)
561 for (m = 0; p < end; p++, q++, m++) {
563 if (*q == '\0') { break; }
569 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
571 char buf[max_length+1], *p, *buf_end;
573 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
574 if (ccs == CCS_MAIN) {
579 fprintf(fp, "(%ld)", ccs->ccsID);
582 buf_end = buf + max_length + 1;
584 // keep printing components of the stack until we run out of space
585 // in the buffer. If we run out of space, end with "...".
586 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
588 // CAF cost centres print as M.CAF, but we leave the module
589 // name out of all the others to save space.
590 if (!strcmp(ccs->cc->label,"CAF")) {
591 p += buf_append(p, ccs->cc->module, buf_end);
592 p += buf_append(p, ".CAF", buf_end);
594 p += buf_append(p, ccs->cc->label, buf_end);
595 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
596 p += buf_append(p, "/", buf_end);
601 sprintf(buf+max_length-4, "...");
605 fprintf(fp, "%s", buf);
607 #endif /* PROFILING */
610 strMatchesSelector( char* str, char* sel )
613 // debugBelch("str_matches_selector %s %s\n", str, sel);
615 // Compare str against wherever we've got to in sel.
617 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
620 // Match if all of str used and have reached the end of a sel fragment.
621 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
624 // No match. Advance sel to the start of the next elem.
625 while (*sel != ',' && *sel != '\0') sel++;
626 if (*sel == ',') sel++;
628 /* Run out of sel ?? */
629 if (*sel == '\0') return rtsFalse;
633 /* -----------------------------------------------------------------------------
634 * Figure out whether a closure should be counted in this census, by
635 * testing against all the specified constraints.
636 * -------------------------------------------------------------------------- */
638 closureSatisfiesConstraints( StgClosure* p )
640 #if !defined(PROFILING)
641 (void)p; /* keep gcc -Wall happy */
646 // The CCS has a selected field to indicate whether this closure is
647 // deselected by not being mentioned in the module, CC, or CCS
649 if (!p->header.prof.ccs->selected) {
653 if (RtsFlags.ProfFlags.descrSelector) {
654 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
655 RtsFlags.ProfFlags.descrSelector );
656 if (!b) return rtsFalse;
658 if (RtsFlags.ProfFlags.typeSelector) {
659 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
660 RtsFlags.ProfFlags.typeSelector );
661 if (!b) return rtsFalse;
663 if (RtsFlags.ProfFlags.retainerSelector) {
666 // We must check that the retainer set is valid here. One
667 // reason it might not be valid is if this closure is a
668 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
669 // these aren't reached by the retainer profiler's traversal.
670 if (isRetainerSetFieldValid((StgClosure *)p)) {
671 rs = retainerSetOf((StgClosure *)p);
673 for (i = 0; i < rs->num; i++) {
674 b = strMatchesSelector( rs->element[i]->cc->label,
675 RtsFlags.ProfFlags.retainerSelector );
676 if (b) return rtsTrue;
683 #endif /* PROFILING */
686 /* -----------------------------------------------------------------------------
687 * Aggregate the heap census info for biographical profiling
688 * -------------------------------------------------------------------------- */
691 aggregateCensusInfo( void )
695 counter *c, *d, *ctrs;
698 if (!doingLDVProfiling()) return;
700 // Aggregate the LDV counters when displaying by biography.
701 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
702 int void_total, drag_total;
704 // Now we compute void_total and drag_total for each census
705 // After the program has finished, the void_total field of
706 // each census contains the count of words that were *created*
707 // in this era and were eventually void. Conversely, if a
708 // void closure was destroyed in this era, it will be
709 // represented by a negative count of words in void_total.
711 // To get the count of live words that are void at each
712 // census, just propagate the void_total count forwards:
716 for (t = 1; t < era; t++) { // note: start at 1, not 0
717 void_total += censuses[t].void_total;
718 drag_total += censuses[t].drag_total;
719 censuses[t].void_total = void_total;
720 censuses[t].drag_total = drag_total;
722 ASSERT( censuses[t].void_total <= censuses[t].not_used );
723 // should be true because: void_total is the count of
724 // live words that are void at this census, which *must*
725 // be less than the number of live words that have not
728 ASSERT( censuses[t].drag_total <= censuses[t].used );
729 // similar reasoning as above.
735 // otherwise... we're doing a heap profile that is restricted to
736 // some combination of lag, drag, void or use. We've kept all the
737 // census info for all censuses so far, but we still need to
738 // aggregate the counters forwards.
741 acc = allocHashTable();
744 for (t = 1; t < era; t++) {
746 // first look through all the counters we're aggregating
747 for (c = ctrs; c != NULL; c = c->next) {
748 // if one of the totals is non-zero, then this closure
749 // type must be present in the heap at this census time...
750 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
753 // if this closure identity isn't present in the
754 // census for this time period, then our running
755 // totals *must* be zero.
756 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
758 // debugCCS(c->identity);
759 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
760 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
762 d->c.ldv.void_total += c->c.ldv.void_total;
763 d->c.ldv.drag_total += c->c.ldv.drag_total;
764 c->c.ldv.void_total = d->c.ldv.void_total;
765 c->c.ldv.drag_total = d->c.ldv.drag_total;
767 ASSERT( c->c.ldv.void_total >= 0 );
768 ASSERT( c->c.ldv.drag_total >= 0 );
772 // now look through the counters in this census to find new ones
773 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
774 d = lookupHashTable(acc, (StgWord)c->identity);
776 d = arenaAlloc( arena, sizeof(counter) );
778 insertHashTable( acc, (StgWord)c->identity, d );
779 d->identity = c->identity;
782 d->c.ldv.void_total = c->c.ldv.void_total;
783 d->c.ldv.drag_total = c->c.ldv.drag_total;
785 ASSERT( c->c.ldv.void_total >= 0 );
786 ASSERT( c->c.ldv.drag_total >= 0 );
790 freeHashTable(acc, NULL);
795 /* -----------------------------------------------------------------------------
796 * Print out the results of a heap census.
797 * -------------------------------------------------------------------------- */
799 dumpCensus( Census *census )
804 printSample(rtsTrue, census->time);
807 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
808 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
809 fprintf(hp_file, "LAG\t%lu\n",
810 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
811 fprintf(hp_file, "USE\t%lu\n",
812 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
813 fprintf(hp_file, "INHERENT_USE\t%lu\n",
814 (unsigned long)(census->prim) * sizeof(W_));
815 fprintf(hp_file, "DRAG\t%lu\n",
816 (unsigned long)(census->drag_total) * sizeof(W_));
817 printSample(rtsFalse, census->time);
822 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
825 if (RtsFlags.ProfFlags.bioSelector != NULL) {
827 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
828 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
829 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
830 count += ctr->c.ldv.drag_total;
831 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
832 count += ctr->c.ldv.void_total;
833 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
834 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
838 count = ctr->c.resid;
841 ASSERT( count >= 0 );
843 if (count == 0) continue;
845 #if !defined(PROFILING)
846 switch (RtsFlags.ProfFlags.doHeapProfile) {
847 case HEAP_BY_CLOSURE_TYPE:
848 fprintf(hp_file, "%s", (char *)ctr->identity);
854 switch (RtsFlags.ProfFlags.doHeapProfile) {
856 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
861 fprintf(hp_file, "%s", (char *)ctr->identity);
863 case HEAP_BY_RETAINER:
865 RetainerSet *rs = (RetainerSet *)ctr->identity;
867 // it might be the distinguished retainer set rs_MANY:
868 if (rs == &rs_MANY) {
869 fprintf(hp_file, "MANY");
873 // Mark this retainer set by negating its id, because it
874 // has appeared in at least one census. We print the
875 // values of all such retainer sets into the log file at
876 // the end. A retainer set may exist but not feature in
877 // any censuses if it arose as the intermediate retainer
878 // set for some closure during retainer set calculation.
882 // report in the unit of bytes: * sizeof(StgWord)
883 printRetainerSetShort(hp_file, rs);
887 barf("dumpCensus; doHeapProfile");
891 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
894 printSample(rtsFalse, census->time);
897 /* -----------------------------------------------------------------------------
898 * Code to perform a heap census.
899 * -------------------------------------------------------------------------- */
901 heapCensusChain( Census *census, bdescr *bd )
911 for (; bd != NULL; bd = bd->link) {
913 // HACK: ignore pinned blocks, because they contain gaps.
914 // It's not clear exactly what we'd like to do here, since we
915 // can't tell which objects in the block are actually alive.
916 // Perhaps the whole block should be counted as SYSTEM memory.
917 if (bd->flags & BF_PINNED) {
922 while (p < bd->free) {
923 info = get_itbl((StgClosure *)p);
926 switch (info->type) {
929 size = thunk_sizeW_fromITBL(info);
935 size = sizeofW(StgThunkHeader) + 2;
941 size = sizeofW(StgThunkHeader) + 1;
948 case IND_OLDGEN_PERM:
950 case SE_CAF_BLACKHOLE:
963 size = sizeW_fromITBL(info);
967 // Special case/Delicate Hack: INDs don't normally
968 // appear, since we're doing this heap census right
969 // after GC. However, GarbageCollect() also does
970 // resurrectThreads(), which can update some
971 // blackholes when it calls raiseAsync() on the
972 // resurrected threads. So we know that any IND will
973 // be the size of a BLACKHOLE.
974 size = BLACKHOLE_sizeW();
979 size = bco_sizeW((StgBCO *)p);
988 size = sizeW_fromITBL(info);
992 size = ap_sizeW((StgAP *)p);
996 size = pap_sizeW((StgPAP *)p);
1000 size = ap_stack_sizeW((StgAP_STACK *)p);
1005 size = arr_words_sizeW(stgCast(StgArrWords*,p));
1008 case MUT_ARR_PTRS_CLEAN:
1009 case MUT_ARR_PTRS_DIRTY:
1010 case MUT_ARR_PTRS_FROZEN:
1011 case MUT_ARR_PTRS_FROZEN0:
1013 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1019 if (RtsFlags.ProfFlags.includeTSOs) {
1020 size = tso_sizeW((StgTSO *)p);
1023 // Skip this TSO and move on to the next object
1024 p += tso_sizeW((StgTSO *)p);
1028 size = tso_sizeW((StgTSO *)p);
1034 size = sizeofW(StgTRecHeader);
1037 case TVAR_WATCH_QUEUE:
1039 size = sizeofW(StgTVarWatchQueue);
1042 case INVARIANT_CHECK_QUEUE:
1044 size = sizeofW(StgInvariantCheckQueue);
1047 case ATOMIC_INVARIANT:
1049 size = sizeofW(StgAtomicInvariant);
1054 size = sizeofW(StgTVar);
1059 size = sizeofW(StgTRecChunk);
1063 barf("heapCensus, unknown object: %d", info->type);
1069 // subtract the profiling overhead
1070 real_size = size - sizeofW(StgProfHeader);
1075 if (closureSatisfiesConstraints((StgClosure*)p)) {
1077 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1079 census->prim += real_size;
1080 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1081 census->not_used += real_size;
1083 census->used += real_size;
1087 identity = closureIdentity((StgClosure *)p);
1089 if (identity != NULL) {
1090 ctr = lookupHashTable( census->hash, (StgWord)identity );
1093 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1095 ctr->c.ldv.prim += real_size;
1096 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1097 ctr->c.ldv.not_used += real_size;
1099 ctr->c.ldv.used += real_size;
1103 ctr->c.resid += real_size;
1106 ctr = arenaAlloc( census->arena, sizeof(counter) );
1108 insertHashTable( census->hash, (StgWord)identity, ctr );
1109 ctr->identity = identity;
1110 ctr->next = census->ctrs;
1114 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1116 ctr->c.ldv.prim = real_size;
1117 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1118 ctr->c.ldv.not_used = real_size;
1120 ctr->c.ldv.used = real_size;
1124 ctr->c.resid = real_size;
1142 census = &censuses[era];
1143 census->time = mut_user_time();
1145 // calculate retainer sets if necessary
1147 if (doingRetainerProfiling()) {
1153 stat_startHeapCensus();
1156 // Traverse the heap, collecting the census info
1158 // First the small_alloc_list: we have to fix the free pointer at
1159 // the end by calling tidyAllocatedLists() first.
1160 tidyAllocateLists();
1161 heapCensusChain( census, small_alloc_list );
1163 // Now traverse the heap in each generation/step.
1164 if (RtsFlags.GcFlags.generations == 1) {
1165 heapCensusChain( census, g0s0->blocks );
1167 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1168 for (s = 0; s < generations[g].n_steps; s++) {
1169 heapCensusChain( census, generations[g].steps[s].blocks );
1170 // Are we interested in large objects? might be
1171 // confusing to include the stack in a heap profile.
1172 heapCensusChain( census, generations[g].steps[s].large_objects );
1177 // dump out the census info
1179 // We can't generate any info for LDV profiling until
1180 // the end of the run...
1181 if (!doingLDVProfiling())
1182 dumpCensus( census );
1184 dumpCensus( census );
1188 // free our storage, unless we're keeping all the census info for
1189 // future restriction by biography.
1191 if (RtsFlags.ProfFlags.bioSelector == NULL)
1193 freeHashTable( census->hash, NULL/* don't free the elements */ );
1194 arenaFree( census->arena );
1195 census->hash = NULL;
1196 census->arena = NULL;
1200 // we're into the next time period now
1204 stat_endHeapCensus();