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"
26 #include "RetainerProfile.h"
27 #include "LdvProfile.h"
35 /* -----------------------------------------------------------------------------
36 * era stores the current time period. It is the same as the
37 * number of censuses that have been performed.
40 * era must be no longer than LDV_SHIFT (15 or 30) bits.
42 * era is initialized to 1 in initHeapProfiling().
44 * max_era is initialized to 2^LDV_SHIFT in initHeapProfiling().
45 * When era reaches max_era, the profiling stops because a closure can
46 * store only up to (max_era - 1) as its creation or last use time.
47 * -------------------------------------------------------------------------- */
51 /* -----------------------------------------------------------------------------
54 * For most heap profiles each closure identity gets a simple count
55 * of live words in the heap at each census. However, if we're
56 * selecting by biography, then we have to keep the various
57 * lag/drag/void counters for each identity.
58 * -------------------------------------------------------------------------- */
59 typedef struct _counter {
64 int prim; // total size of 'inherently used' closures
65 int not_used; // total size of 'never used' closures
66 int used; // total size of 'used at least once' closures
67 int void_total; // current total size of 'destroyed without being used' closures
68 int drag_total; // current total size of 'used at least once and waiting to die'
71 struct _counter *next;
75 initLDVCtr( counter *ctr )
78 ctr->c.ldv.not_used = 0;
80 ctr->c.ldv.void_total = 0;
81 ctr->c.ldv.drag_total = 0;
85 double time; // the time in MUT time when the census is made
90 // for LDV profiling, when just displaying by LDV
98 static Census *censuses = NULL;
99 static nat n_censuses = 0;
102 static void aggregateCensusInfo( void );
105 static void dumpCensus( Census *census );
107 /* -----------------------------------------------------------------------------
108 Closure Type Profiling;
110 PROBABLY TOTALLY OUT OF DATE -- ToDo (SDM)
111 -------------------------------------------------------------------------- */
113 #ifdef DEBUG_HEAP_PROF
114 static char *type_names[] = {
118 , "CONSTR_NOCAF_STATIC"
154 , "MUT_ARR_PTRS_CLEAN"
155 , "MUT_ARR_PTRS_DIRTY"
156 , "MUT_ARR_PTRS_FROZEN"
170 #endif /* DEBUG_HEAP_PROF */
172 /* -----------------------------------------------------------------------------
173 * Find the "closure identity", which is a unique pointer reresenting
174 * the band to which this closure's heap space is attributed in the
176 * ------------------------------------------------------------------------- */
178 closureIdentity( StgClosure *p )
180 switch (RtsFlags.ProfFlags.doHeapProfile) {
184 return p->header.prof.ccs;
186 return p->header.prof.ccs->cc->module;
188 return get_itbl(p)->prof.closure_desc;
190 return get_itbl(p)->prof.closure_type;
191 case HEAP_BY_RETAINER:
192 // AFAIK, the only closures in the heap which might not have a
193 // valid retainer set are DEAD_WEAK closures.
194 if (isRetainerSetFieldValid(p))
195 return retainerSetOf(p);
200 case HEAP_BY_INFOPTR:
201 return (void *)((StgClosure *)p)->header.info;
202 case HEAP_BY_CLOSURE_TYPE:
203 return type_names[get_itbl(p)->type];
207 barf("closureIdentity");
211 /* --------------------------------------------------------------------------
212 * Profiling type predicates
213 * ----------------------------------------------------------------------- */
215 STATIC_INLINE rtsBool
216 doingLDVProfiling( void )
218 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
219 || RtsFlags.ProfFlags.bioSelector != NULL);
222 STATIC_INLINE rtsBool
223 doingRetainerProfiling( void )
225 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
226 || RtsFlags.ProfFlags.retainerSelector != NULL);
228 #endif /* PROFILING */
230 // Precesses a closure 'c' being destroyed whose size is 'size'.
231 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
232 // such as TSO; they should not be involved in computing dragNew or voidNew.
234 // Even though era is checked in both LdvCensusForDead() and
235 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
236 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
237 // when a thunk is replaced by an indirection object.
241 LDV_recordDead( StgClosure *c, nat size )
247 if (era > 0 && closureSatisfiesConstraints(c)) {
248 size -= sizeofW(StgProfHeader);
249 ASSERT(LDVW(c) != 0);
250 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
251 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
253 if (RtsFlags.ProfFlags.bioSelector == NULL) {
254 censuses[t].void_total += (int)size;
255 censuses[era].void_total -= (int)size;
256 ASSERT(censuses[t].void_total < censuses[t].not_used);
258 id = closureIdentity(c);
259 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
260 ASSERT( ctr != NULL );
261 ctr->c.ldv.void_total += (int)size;
262 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
264 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
266 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
268 ctr->next = censuses[era].ctrs;
269 censuses[era].ctrs = ctr;
271 ctr->c.ldv.void_total -= (int)size;
275 t = LDVW((c)) & LDV_LAST_MASK;
277 if (RtsFlags.ProfFlags.bioSelector == NULL) {
278 censuses[t+1].drag_total += size;
279 censuses[era].drag_total -= size;
282 id = closureIdentity(c);
283 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
284 ASSERT( ctr != NULL );
285 ctr->c.ldv.drag_total += (int)size;
286 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
288 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
290 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
292 ctr->next = censuses[era].ctrs;
293 censuses[era].ctrs = ctr;
295 ctr->c.ldv.drag_total -= (int)size;
303 /* --------------------------------------------------------------------------
304 * Initialize censuses[era];
305 * ----------------------------------------------------------------------- */
307 initEra(Census *census)
309 census->hash = allocHashTable();
311 census->arena = newArena();
313 census->not_used = 0;
316 census->void_total = 0;
317 census->drag_total = 0;
320 /* --------------------------------------------------------------------------
321 * Increases era by 1 and initialize census[era].
322 * Reallocates gi[] and increases its size if needed.
323 * ----------------------------------------------------------------------- */
328 if (doingLDVProfiling()) {
331 if (era == max_era) {
332 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
333 stg_exit(EXIT_FAILURE);
336 if (era == n_censuses) {
338 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
342 #endif /* PROFILING */
344 initEra( &censuses[era] );
347 /* -----------------------------------------------------------------------------
348 * DEBUG heap profiling, by info table
349 * -------------------------------------------------------------------------- */
351 #ifdef DEBUG_HEAP_PROF
353 static char *hp_filename;
355 void initProfiling1( void )
359 void initProfiling2( void )
361 if (RtsFlags.ProfFlags.doHeapProfile) {
362 /* Initialise the log file name */
363 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
364 sprintf(hp_filename, "%s.hp", prog_name);
366 /* open the log file */
367 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
368 debugBelch("Can't open profiling report file %s\n",
370 RtsFlags.ProfFlags.doHeapProfile = 0;
378 void endProfiling( void )
382 #endif /* DEBUG_HEAP_PROF */
385 printSample(rtsBool beginSample, StgDouble sampleValue)
387 StgDouble fractionalPart, integralPart;
388 fractionalPart = modf(sampleValue, &integralPart);
389 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
390 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
391 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
394 /* --------------------------------------------------------------------------
395 * Initialize the heap profilier
396 * ----------------------------------------------------------------------- */
398 initHeapProfiling(void)
400 if (! RtsFlags.ProfFlags.doHeapProfile) {
405 if (doingLDVProfiling() && doingRetainerProfiling()) {
406 errorBelch("cannot mix -hb and -hr");
407 stg_exit(EXIT_FAILURE);
411 // we only count eras if we're doing LDV profiling. Otherwise era
414 if (doingLDVProfiling()) {
422 { // max_era = 2^LDV_SHIFT
425 for (p = 0; p < LDV_SHIFT; p++)
430 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
432 initEra( &censuses[era] );
434 /* initProfilingLogFile(); */
435 fprintf(hp_file, "JOB \"%s", prog_name);
440 for(count = 1; count < prog_argc; count++)
441 fprintf(hp_file, " %s", prog_argv[count]);
442 fprintf(hp_file, " +RTS");
443 for(count = 0; count < rts_argc; count++)
444 fprintf(hp_file, " %s", rts_argv[count]);
446 #endif /* PROFILING */
448 fprintf(hp_file, "\"\n" );
450 fprintf(hp_file, "DATE \"%s\"\n", time_str());
452 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
453 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
455 printSample(rtsTrue, 0);
456 printSample(rtsFalse, 0);
458 #ifdef DEBUG_HEAP_PROF
459 DEBUG_LoadSymbols(prog_name);
463 if (doingRetainerProfiling()) {
464 initRetainerProfiling();
472 endHeapProfiling(void)
476 if (! RtsFlags.ProfFlags.doHeapProfile) {
481 if (doingRetainerProfiling()) {
482 endRetainerProfiling();
487 if (doingLDVProfiling()) {
490 aggregateCensusInfo();
491 for (t = 1; t < era; t++) {
492 dumpCensus( &censuses[t] );
497 seconds = mut_user_time();
498 printSample(rtsTrue, seconds);
499 printSample(rtsFalse, seconds);
507 buf_append(char *p, const char *q, char *end)
511 for (m = 0; p < end; p++, q++, m++) {
513 if (*q == '\0') { break; }
519 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
521 char buf[max_length+1], *p, *buf_end;
523 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
524 if (ccs == CCS_MAIN) {
529 fprintf(fp, "(%ld)", ccs->ccsID);
532 buf_end = buf + max_length + 1;
534 // keep printing components of the stack until we run out of space
535 // in the buffer. If we run out of space, end with "...".
536 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
538 // CAF cost centres print as M.CAF, but we leave the module
539 // name out of all the others to save space.
540 if (!strcmp(ccs->cc->label,"CAF")) {
541 p += buf_append(p, ccs->cc->module, buf_end);
542 p += buf_append(p, ".CAF", buf_end);
544 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
545 p += buf_append(p, "/", buf_end);
547 p += buf_append(p, ccs->cc->label, buf_end);
551 sprintf(buf+max_length-4, "...");
555 fprintf(fp, "%s", buf);
557 #endif /* PROFILING */
560 strMatchesSelector( char* str, char* sel )
563 // debugBelch("str_matches_selector %s %s\n", str, sel);
565 // Compare str against wherever we've got to in sel.
567 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
570 // Match if all of str used and have reached the end of a sel fragment.
571 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
574 // No match. Advance sel to the start of the next elem.
575 while (*sel != ',' && *sel != '\0') sel++;
576 if (*sel == ',') sel++;
578 /* Run out of sel ?? */
579 if (*sel == '\0') return rtsFalse;
583 /* -----------------------------------------------------------------------------
584 * Figure out whether a closure should be counted in this census, by
585 * testing against all the specified constraints.
586 * -------------------------------------------------------------------------- */
588 closureSatisfiesConstraints( StgClosure* p )
590 #ifdef DEBUG_HEAP_PROF
591 (void)p; /* keep gcc -Wall happy */
596 // The CCS has a selected field to indicate whether this closure is
597 // deselected by not being mentioned in the module, CC, or CCS
599 if (!p->header.prof.ccs->selected) {
603 if (RtsFlags.ProfFlags.descrSelector) {
604 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_desc,
605 RtsFlags.ProfFlags.descrSelector );
606 if (!b) return rtsFalse;
608 if (RtsFlags.ProfFlags.typeSelector) {
609 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_type,
610 RtsFlags.ProfFlags.typeSelector );
611 if (!b) return rtsFalse;
613 if (RtsFlags.ProfFlags.retainerSelector) {
616 // We must check that the retainer set is valid here. One
617 // reason it might not be valid is if this closure is a
618 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
619 // these aren't reached by the retainer profiler's traversal.
620 if (isRetainerSetFieldValid((StgClosure *)p)) {
621 rs = retainerSetOf((StgClosure *)p);
623 for (i = 0; i < rs->num; i++) {
624 b = strMatchesSelector( rs->element[i]->cc->label,
625 RtsFlags.ProfFlags.retainerSelector );
626 if (b) return rtsTrue;
633 #endif /* PROFILING */
636 /* -----------------------------------------------------------------------------
637 * Aggregate the heap census info for biographical profiling
638 * -------------------------------------------------------------------------- */
641 aggregateCensusInfo( void )
645 counter *c, *d, *ctrs;
648 if (!doingLDVProfiling()) return;
650 // Aggregate the LDV counters when displaying by biography.
651 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
652 int void_total, drag_total;
654 // Now we compute void_total and drag_total for each census
655 // After the program has finished, the void_total field of
656 // each census contains the count of words that were *created*
657 // in this era and were eventually void. Conversely, if a
658 // void closure was destroyed in this era, it will be
659 // represented by a negative count of words in void_total.
661 // To get the count of live words that are void at each
662 // census, just propagate the void_total count forwards:
666 for (t = 1; t < era; t++) { // note: start at 1, not 0
667 void_total += censuses[t].void_total;
668 drag_total += censuses[t].drag_total;
669 censuses[t].void_total = void_total;
670 censuses[t].drag_total = drag_total;
672 ASSERT( censuses[t].void_total <= censuses[t].not_used );
673 // should be true because: void_total is the count of
674 // live words that are void at this census, which *must*
675 // be less than the number of live words that have not
678 ASSERT( censuses[t].drag_total <= censuses[t].used );
679 // similar reasoning as above.
685 // otherwise... we're doing a heap profile that is restricted to
686 // some combination of lag, drag, void or use. We've kept all the
687 // census info for all censuses so far, but we still need to
688 // aggregate the counters forwards.
691 acc = allocHashTable();
694 for (t = 1; t < era; t++) {
696 // first look through all the counters we're aggregating
697 for (c = ctrs; c != NULL; c = c->next) {
698 // if one of the totals is non-zero, then this closure
699 // type must be present in the heap at this census time...
700 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
703 // if this closure identity isn't present in the
704 // census for this time period, then our running
705 // totals *must* be zero.
706 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
708 // debugCCS(c->identity);
709 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
710 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
712 d->c.ldv.void_total += c->c.ldv.void_total;
713 d->c.ldv.drag_total += c->c.ldv.drag_total;
714 c->c.ldv.void_total = d->c.ldv.void_total;
715 c->c.ldv.drag_total = d->c.ldv.drag_total;
717 ASSERT( c->c.ldv.void_total >= 0 );
718 ASSERT( c->c.ldv.drag_total >= 0 );
722 // now look through the counters in this census to find new ones
723 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
724 d = lookupHashTable(acc, (StgWord)c->identity);
726 d = arenaAlloc( arena, sizeof(counter) );
728 insertHashTable( acc, (StgWord)c->identity, d );
729 d->identity = c->identity;
732 d->c.ldv.void_total = c->c.ldv.void_total;
733 d->c.ldv.drag_total = c->c.ldv.drag_total;
735 ASSERT( c->c.ldv.void_total >= 0 );
736 ASSERT( c->c.ldv.drag_total >= 0 );
740 freeHashTable(acc, NULL);
745 /* -----------------------------------------------------------------------------
746 * Print out the results of a heap census.
747 * -------------------------------------------------------------------------- */
749 dumpCensus( Census *census )
754 printSample(rtsTrue, census->time);
757 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
758 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
759 fprintf(hp_file, "LAG\t%lu\n",
760 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
761 fprintf(hp_file, "USE\t%lu\n",
762 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
763 fprintf(hp_file, "INHERENT_USE\t%lu\n",
764 (unsigned long)(census->prim) * sizeof(W_));
765 fprintf(hp_file, "DRAG\t%lu\n",
766 (unsigned long)(census->drag_total) * sizeof(W_));
767 printSample(rtsFalse, census->time);
772 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
775 if (RtsFlags.ProfFlags.bioSelector != NULL) {
777 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
778 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
779 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
780 count += ctr->c.ldv.drag_total;
781 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
782 count += ctr->c.ldv.void_total;
783 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
784 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
788 count = ctr->c.resid;
791 ASSERT( count >= 0 );
793 if (count == 0) continue;
795 #ifdef DEBUG_HEAP_PROF
796 switch (RtsFlags.ProfFlags.doHeapProfile) {
797 case HEAP_BY_INFOPTR:
798 fprintf(hp_file, "%s", lookupGHCName(ctr->identity));
800 case HEAP_BY_CLOSURE_TYPE:
801 fprintf(hp_file, "%s", (char *)ctr->identity);
807 switch (RtsFlags.ProfFlags.doHeapProfile) {
809 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, 25);
814 fprintf(hp_file, "%s", (char *)ctr->identity);
816 case HEAP_BY_RETAINER:
818 RetainerSet *rs = (RetainerSet *)ctr->identity;
820 // it might be the distinguished retainer set rs_MANY:
821 if (rs == &rs_MANY) {
822 fprintf(hp_file, "MANY");
826 // Mark this retainer set by negating its id, because it
827 // has appeared in at least one census. We print the
828 // values of all such retainer sets into the log file at
829 // the end. A retainer set may exist but not feature in
830 // any censuses if it arose as the intermediate retainer
831 // set for some closure during retainer set calculation.
835 // report in the unit of bytes: * sizeof(StgWord)
836 printRetainerSetShort(hp_file, rs);
840 barf("dumpCensus; doHeapProfile");
844 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
847 printSample(rtsFalse, census->time);
850 /* -----------------------------------------------------------------------------
851 * Code to perform a heap census.
852 * -------------------------------------------------------------------------- */
854 heapCensusChain( Census *census, bdescr *bd )
864 for (; bd != NULL; bd = bd->link) {
866 // HACK: ignore pinned blocks, because they contain gaps.
867 // It's not clear exactly what we'd like to do here, since we
868 // can't tell which objects in the block are actually alive.
869 // Perhaps the whole block should be counted as SYSTEM memory.
870 if (bd->flags & BF_PINNED) {
875 while (p < bd->free) {
876 info = get_itbl((StgClosure *)p);
879 switch (info->type) {
882 size = thunk_sizeW_fromITBL(info);
888 size = sizeofW(StgThunkHeader) + 2;
894 size = sizeofW(StgThunkHeader) + 1;
901 case IND_OLDGEN_PERM:
903 case SE_CAF_BLACKHOLE:
916 size = sizeW_fromITBL(info);
920 // Special case/Delicate Hack: INDs don't normally
921 // appear, since we're doing this heap census right
922 // after GC. However, GarbageCollect() also does
923 // resurrectThreads(), which can update some
924 // blackholes when it calls raiseAsync() on the
925 // resurrected threads. So we know that any IND will
926 // be the size of a BLACKHOLE.
927 size = BLACKHOLE_sizeW();
932 size = bco_sizeW((StgBCO *)p);
941 size = sizeW_fromITBL(info);
945 size = ap_sizeW((StgAP *)p);
949 size = pap_sizeW((StgPAP *)p);
953 size = ap_stack_sizeW((StgAP_STACK *)p);
958 size = arr_words_sizeW(stgCast(StgArrWords*,p));
961 case MUT_ARR_PTRS_CLEAN:
962 case MUT_ARR_PTRS_DIRTY:
963 case MUT_ARR_PTRS_FROZEN:
964 case MUT_ARR_PTRS_FROZEN0:
966 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
971 #ifdef DEBUG_HEAP_PROF
972 size = tso_sizeW((StgTSO *)p);
975 if (RtsFlags.ProfFlags.includeTSOs) {
976 size = tso_sizeW((StgTSO *)p);
979 // Skip this TSO and move on to the next object
980 p += tso_sizeW((StgTSO *)p);
987 size = sizeofW(StgTRecHeader);
990 case TVAR_WAIT_QUEUE:
992 size = sizeofW(StgTVarWaitQueue);
997 size = sizeofW(StgTVar);
1002 size = sizeofW(StgTRecChunk);
1006 barf("heapCensus, unknown object: %d", info->type);
1011 #ifdef DEBUG_HEAP_PROF
1014 // subtract the profiling overhead
1015 real_size = size - sizeofW(StgProfHeader);
1018 if (closureSatisfiesConstraints((StgClosure*)p)) {
1020 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1022 census->prim += real_size;
1023 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1024 census->not_used += real_size;
1026 census->used += real_size;
1030 identity = closureIdentity((StgClosure *)p);
1032 if (identity != NULL) {
1033 ctr = lookupHashTable( census->hash, (StgWord)identity );
1036 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1038 ctr->c.ldv.prim += real_size;
1039 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1040 ctr->c.ldv.not_used += real_size;
1042 ctr->c.ldv.used += real_size;
1046 ctr->c.resid += real_size;
1049 ctr = arenaAlloc( census->arena, sizeof(counter) );
1051 insertHashTable( census->hash, (StgWord)identity, ctr );
1052 ctr->identity = identity;
1053 ctr->next = census->ctrs;
1057 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1059 ctr->c.ldv.prim = real_size;
1060 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1061 ctr->c.ldv.not_used = real_size;
1063 ctr->c.ldv.used = real_size;
1067 ctr->c.resid = real_size;
1085 census = &censuses[era];
1086 census->time = mut_user_time();
1088 // calculate retainer sets if necessary
1090 if (doingRetainerProfiling()) {
1096 stat_startHeapCensus();
1099 // Traverse the heap, collecting the census info
1101 // First the small_alloc_list: we have to fix the free pointer at
1102 // the end by calling tidyAllocatedLists() first.
1103 tidyAllocateLists();
1104 heapCensusChain( census, small_alloc_list );
1106 // Now traverse the heap in each generation/step.
1107 if (RtsFlags.GcFlags.generations == 1) {
1108 heapCensusChain( census, g0s0->blocks );
1110 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1111 for (s = 0; s < generations[g].n_steps; s++) {
1112 heapCensusChain( census, generations[g].steps[s].blocks );
1113 // Are we interested in large objects? might be
1114 // confusing to include the stack in a heap profile.
1115 heapCensusChain( census, generations[g].steps[s].large_objects );
1120 // dump out the census info
1122 // We can't generate any info for LDV profiling until
1123 // the end of the run...
1124 if (!doingLDVProfiling())
1125 dumpCensus( census );
1127 dumpCensus( census );
1131 // free our storage, unless we're keeping all the census info for
1132 // future restriction by biography.
1134 if (RtsFlags.ProfFlags.bioSelector == NULL)
1137 freeHashTable( census->hash, NULL/* don't free the elements */ );
1138 arenaFree( census->arena );
1139 census->hash = NULL;
1140 census->arena = NULL;
1143 // we're into the next time period now
1147 stat_endHeapCensus();
1151 #endif /* PROFILING || DEBUG_HEAP_PROF */