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 ------------------------------------------------------------------------- */
102 static char *type_names[] = {
111 "CONSTR_NOCAF_STATIC",
150 "MUT_ARR_PTRS_CLEAN",
151 "MUT_ARR_PTRS_DIRTY",
152 "MUT_ARR_PTRS_FROZEN0",
153 "MUT_ARR_PTRS_FROZEN",
166 "INVARIANT_CHECK_QUEUE",
177 /* ----------------------------------------------------------------------------
178 * Find the "closure identity", which is a unique pointer reresenting
179 * the band to which this closure's heap space is attributed in the
181 * ------------------------------------------------------------------------- */
183 closureIdentity( StgClosure *p )
185 switch (RtsFlags.ProfFlags.doHeapProfile) {
189 return p->header.prof.ccs;
191 return p->header.prof.ccs->cc->module;
193 return get_itbl(p)->prof.closure_desc;
195 return get_itbl(p)->prof.closure_type;
196 case HEAP_BY_RETAINER:
197 // AFAIK, the only closures in the heap which might not have a
198 // valid retainer set are DEAD_WEAK closures.
199 if (isRetainerSetFieldValid(p))
200 return retainerSetOf(p);
205 case HEAP_BY_CLOSURE_TYPE:
209 switch (info->type) {
217 case CONSTR_NOCAF_STATIC:
218 printf("",strlen(GET_CON_DESC(itbl_to_con_itbl(info))));
219 return GET_CON_DESC(itbl_to_con_itbl(info));
221 return type_names[info->type];
227 barf("closureIdentity");
231 /* --------------------------------------------------------------------------
232 * Profiling type predicates
233 * ----------------------------------------------------------------------- */
235 STATIC_INLINE rtsBool
236 doingLDVProfiling( void )
238 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
239 || RtsFlags.ProfFlags.bioSelector != NULL);
242 STATIC_INLINE rtsBool
243 doingRetainerProfiling( void )
245 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
246 || RtsFlags.ProfFlags.retainerSelector != NULL);
248 #endif /* PROFILING */
250 // Precesses a closure 'c' being destroyed whose size is 'size'.
251 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
252 // such as TSO; they should not be involved in computing dragNew or voidNew.
254 // Even though era is checked in both LdvCensusForDead() and
255 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
256 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
257 // when a thunk is replaced by an indirection object.
261 LDV_recordDead( StgClosure *c, nat size )
267 if (era > 0 && closureSatisfiesConstraints(c)) {
268 size -= sizeofW(StgProfHeader);
269 ASSERT(LDVW(c) != 0);
270 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
271 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
273 if (RtsFlags.ProfFlags.bioSelector == NULL) {
274 censuses[t].void_total += (int)size;
275 censuses[era].void_total -= (int)size;
276 ASSERT(censuses[t].void_total < censuses[t].not_used);
278 id = closureIdentity(c);
279 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
280 ASSERT( ctr != NULL );
281 ctr->c.ldv.void_total += (int)size;
282 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
284 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
286 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
288 ctr->next = censuses[era].ctrs;
289 censuses[era].ctrs = ctr;
291 ctr->c.ldv.void_total -= (int)size;
295 t = LDVW((c)) & LDV_LAST_MASK;
297 if (RtsFlags.ProfFlags.bioSelector == NULL) {
298 censuses[t+1].drag_total += size;
299 censuses[era].drag_total -= size;
302 id = closureIdentity(c);
303 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
304 ASSERT( ctr != NULL );
305 ctr->c.ldv.drag_total += (int)size;
306 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
308 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
310 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
312 ctr->next = censuses[era].ctrs;
313 censuses[era].ctrs = ctr;
315 ctr->c.ldv.drag_total -= (int)size;
323 /* --------------------------------------------------------------------------
324 * Initialize censuses[era];
325 * ----------------------------------------------------------------------- */
328 initEra(Census *census)
330 census->hash = allocHashTable();
332 census->arena = newArena();
334 census->not_used = 0;
337 census->void_total = 0;
338 census->drag_total = 0;
342 freeEra(Census *census)
344 arenaFree(census->arena);
345 freeHashTable(census->hash, NULL);
348 /* --------------------------------------------------------------------------
349 * Increases era by 1 and initialize census[era].
350 * Reallocates gi[] and increases its size if needed.
351 * ----------------------------------------------------------------------- */
357 if (doingLDVProfiling()) {
360 if (era == max_era) {
361 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
362 stg_exit(EXIT_FAILURE);
365 if (era == n_censuses) {
367 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
371 #endif /* PROFILING */
373 initEra( &censuses[era] );
376 /* ----------------------------------------------------------------------------
377 * Heap profiling by info table
378 * ------------------------------------------------------------------------- */
380 #if !defined(PROFILING)
382 static char *hp_filename;
384 void initProfiling1 (void)
388 void freeProfiling1 (void)
392 void initProfiling2 (void)
394 if (RtsFlags.ProfFlags.doHeapProfile) {
395 /* Initialise the log file name */
396 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
397 sprintf(hp_filename, "%s.hp", prog_name);
399 /* open the log file */
400 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
401 debugBelch("Can't open profiling report file %s\n",
403 RtsFlags.ProfFlags.doHeapProfile = 0;
411 void endProfiling( void )
415 #endif /* !PROFILING */
418 printSample(rtsBool beginSample, StgDouble sampleValue)
420 StgDouble fractionalPart, integralPart;
421 fractionalPart = modf(sampleValue, &integralPart);
422 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
423 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
424 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
427 /* --------------------------------------------------------------------------
428 * Initialize the heap profilier
429 * ----------------------------------------------------------------------- */
431 initHeapProfiling(void)
433 if (! RtsFlags.ProfFlags.doHeapProfile) {
438 if (doingLDVProfiling() && doingRetainerProfiling()) {
439 errorBelch("cannot mix -hb and -hr");
440 stg_exit(EXIT_FAILURE);
444 // we only count eras if we're doing LDV profiling. Otherwise era
447 if (doingLDVProfiling()) {
455 { // max_era = 2^LDV_SHIFT
458 for (p = 0; p < LDV_SHIFT; p++)
463 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
465 initEra( &censuses[era] );
467 /* initProfilingLogFile(); */
468 fprintf(hp_file, "JOB \"%s", prog_name);
473 for(count = 1; count < prog_argc; count++)
474 fprintf(hp_file, " %s", prog_argv[count]);
475 fprintf(hp_file, " +RTS");
476 for(count = 0; count < rts_argc; count++)
477 fprintf(hp_file, " %s", rts_argv[count]);
479 #endif /* PROFILING */
481 fprintf(hp_file, "\"\n" );
483 fprintf(hp_file, "DATE \"%s\"\n", time_str());
485 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
486 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
488 printSample(rtsTrue, 0);
489 printSample(rtsFalse, 0);
492 if (doingRetainerProfiling()) {
493 initRetainerProfiling();
501 endHeapProfiling(void)
505 if (! RtsFlags.ProfFlags.doHeapProfile) {
510 if (doingRetainerProfiling()) {
511 endRetainerProfiling();
516 if (doingLDVProfiling()) {
519 aggregateCensusInfo();
520 for (t = 1; t < era; t++) {
521 dumpCensus( &censuses[t] );
526 if (doingLDVProfiling()) {
528 for (t = 1; t <= era; t++) {
529 freeEra( &censuses[t] );
532 freeEra( &censuses[0] );
536 seconds = mut_user_time();
537 printSample(rtsTrue, seconds);
538 printSample(rtsFalse, seconds);
546 buf_append(char *p, const char *q, char *end)
550 for (m = 0; p < end; p++, q++, m++) {
552 if (*q == '\0') { break; }
558 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
560 char buf[max_length+1], *p, *buf_end;
562 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
563 if (ccs == CCS_MAIN) {
568 fprintf(fp, "(%ld)", ccs->ccsID);
571 buf_end = buf + max_length + 1;
573 // keep printing components of the stack until we run out of space
574 // in the buffer. If we run out of space, end with "...".
575 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
577 // CAF cost centres print as M.CAF, but we leave the module
578 // name out of all the others to save space.
579 if (!strcmp(ccs->cc->label,"CAF")) {
580 p += buf_append(p, ccs->cc->module, buf_end);
581 p += buf_append(p, ".CAF", buf_end);
583 p += buf_append(p, ccs->cc->label, buf_end);
584 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
585 p += buf_append(p, "/", buf_end);
590 sprintf(buf+max_length-4, "...");
594 fprintf(fp, "%s", buf);
596 #endif /* PROFILING */
599 strMatchesSelector( char* str, char* sel )
602 // debugBelch("str_matches_selector %s %s\n", str, sel);
604 // Compare str against wherever we've got to in sel.
606 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
609 // Match if all of str used and have reached the end of a sel fragment.
610 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
613 // No match. Advance sel to the start of the next elem.
614 while (*sel != ',' && *sel != '\0') sel++;
615 if (*sel == ',') sel++;
617 /* Run out of sel ?? */
618 if (*sel == '\0') return rtsFalse;
622 /* -----------------------------------------------------------------------------
623 * Figure out whether a closure should be counted in this census, by
624 * testing against all the specified constraints.
625 * -------------------------------------------------------------------------- */
627 closureSatisfiesConstraints( StgClosure* p )
629 #if !defined(PROFILING)
630 (void)p; /* keep gcc -Wall happy */
635 // The CCS has a selected field to indicate whether this closure is
636 // deselected by not being mentioned in the module, CC, or CCS
638 if (!p->header.prof.ccs->selected) {
642 if (RtsFlags.ProfFlags.descrSelector) {
643 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_desc,
644 RtsFlags.ProfFlags.descrSelector );
645 if (!b) return rtsFalse;
647 if (RtsFlags.ProfFlags.typeSelector) {
648 b = strMatchesSelector( (get_itbl((StgClosure *)p))->prof.closure_type,
649 RtsFlags.ProfFlags.typeSelector );
650 if (!b) return rtsFalse;
652 if (RtsFlags.ProfFlags.retainerSelector) {
655 // We must check that the retainer set is valid here. One
656 // reason it might not be valid is if this closure is a
657 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
658 // these aren't reached by the retainer profiler's traversal.
659 if (isRetainerSetFieldValid((StgClosure *)p)) {
660 rs = retainerSetOf((StgClosure *)p);
662 for (i = 0; i < rs->num; i++) {
663 b = strMatchesSelector( rs->element[i]->cc->label,
664 RtsFlags.ProfFlags.retainerSelector );
665 if (b) return rtsTrue;
672 #endif /* PROFILING */
675 /* -----------------------------------------------------------------------------
676 * Aggregate the heap census info for biographical profiling
677 * -------------------------------------------------------------------------- */
680 aggregateCensusInfo( void )
684 counter *c, *d, *ctrs;
687 if (!doingLDVProfiling()) return;
689 // Aggregate the LDV counters when displaying by biography.
690 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
691 int void_total, drag_total;
693 // Now we compute void_total and drag_total for each census
694 // After the program has finished, the void_total field of
695 // each census contains the count of words that were *created*
696 // in this era and were eventually void. Conversely, if a
697 // void closure was destroyed in this era, it will be
698 // represented by a negative count of words in void_total.
700 // To get the count of live words that are void at each
701 // census, just propagate the void_total count forwards:
705 for (t = 1; t < era; t++) { // note: start at 1, not 0
706 void_total += censuses[t].void_total;
707 drag_total += censuses[t].drag_total;
708 censuses[t].void_total = void_total;
709 censuses[t].drag_total = drag_total;
711 ASSERT( censuses[t].void_total <= censuses[t].not_used );
712 // should be true because: void_total is the count of
713 // live words that are void at this census, which *must*
714 // be less than the number of live words that have not
717 ASSERT( censuses[t].drag_total <= censuses[t].used );
718 // similar reasoning as above.
724 // otherwise... we're doing a heap profile that is restricted to
725 // some combination of lag, drag, void or use. We've kept all the
726 // census info for all censuses so far, but we still need to
727 // aggregate the counters forwards.
730 acc = allocHashTable();
733 for (t = 1; t < era; t++) {
735 // first look through all the counters we're aggregating
736 for (c = ctrs; c != NULL; c = c->next) {
737 // if one of the totals is non-zero, then this closure
738 // type must be present in the heap at this census time...
739 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
742 // if this closure identity isn't present in the
743 // census for this time period, then our running
744 // totals *must* be zero.
745 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
747 // debugCCS(c->identity);
748 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
749 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
751 d->c.ldv.void_total += c->c.ldv.void_total;
752 d->c.ldv.drag_total += c->c.ldv.drag_total;
753 c->c.ldv.void_total = d->c.ldv.void_total;
754 c->c.ldv.drag_total = d->c.ldv.drag_total;
756 ASSERT( c->c.ldv.void_total >= 0 );
757 ASSERT( c->c.ldv.drag_total >= 0 );
761 // now look through the counters in this census to find new ones
762 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
763 d = lookupHashTable(acc, (StgWord)c->identity);
765 d = arenaAlloc( arena, sizeof(counter) );
767 insertHashTable( acc, (StgWord)c->identity, d );
768 d->identity = c->identity;
771 d->c.ldv.void_total = c->c.ldv.void_total;
772 d->c.ldv.drag_total = c->c.ldv.drag_total;
774 ASSERT( c->c.ldv.void_total >= 0 );
775 ASSERT( c->c.ldv.drag_total >= 0 );
779 freeHashTable(acc, NULL);
784 /* -----------------------------------------------------------------------------
785 * Print out the results of a heap census.
786 * -------------------------------------------------------------------------- */
788 dumpCensus( Census *census )
793 printSample(rtsTrue, census->time);
796 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
797 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
798 fprintf(hp_file, "LAG\t%lu\n",
799 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
800 fprintf(hp_file, "USE\t%lu\n",
801 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
802 fprintf(hp_file, "INHERENT_USE\t%lu\n",
803 (unsigned long)(census->prim) * sizeof(W_));
804 fprintf(hp_file, "DRAG\t%lu\n",
805 (unsigned long)(census->drag_total) * sizeof(W_));
806 printSample(rtsFalse, census->time);
811 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
814 if (RtsFlags.ProfFlags.bioSelector != NULL) {
816 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
817 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
818 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
819 count += ctr->c.ldv.drag_total;
820 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
821 count += ctr->c.ldv.void_total;
822 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
823 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
827 count = ctr->c.resid;
830 ASSERT( count >= 0 );
832 if (count == 0) continue;
834 #if !defined(PROFILING)
835 switch (RtsFlags.ProfFlags.doHeapProfile) {
836 case HEAP_BY_CLOSURE_TYPE:
837 fprintf(hp_file, "%s", (char *)ctr->identity);
843 switch (RtsFlags.ProfFlags.doHeapProfile) {
845 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
850 fprintf(hp_file, "%s", (char *)ctr->identity);
852 case HEAP_BY_RETAINER:
854 RetainerSet *rs = (RetainerSet *)ctr->identity;
856 // it might be the distinguished retainer set rs_MANY:
857 if (rs == &rs_MANY) {
858 fprintf(hp_file, "MANY");
862 // Mark this retainer set by negating its id, because it
863 // has appeared in at least one census. We print the
864 // values of all such retainer sets into the log file at
865 // the end. A retainer set may exist but not feature in
866 // any censuses if it arose as the intermediate retainer
867 // set for some closure during retainer set calculation.
871 // report in the unit of bytes: * sizeof(StgWord)
872 printRetainerSetShort(hp_file, rs);
876 barf("dumpCensus; doHeapProfile");
880 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
883 printSample(rtsFalse, census->time);
886 /* -----------------------------------------------------------------------------
887 * Code to perform a heap census.
888 * -------------------------------------------------------------------------- */
890 heapCensusChain( Census *census, bdescr *bd )
900 for (; bd != NULL; bd = bd->link) {
902 // HACK: ignore pinned blocks, because they contain gaps.
903 // It's not clear exactly what we'd like to do here, since we
904 // can't tell which objects in the block are actually alive.
905 // Perhaps the whole block should be counted as SYSTEM memory.
906 if (bd->flags & BF_PINNED) {
911 while (p < bd->free) {
912 info = get_itbl((StgClosure *)p);
915 switch (info->type) {
918 size = thunk_sizeW_fromITBL(info);
924 size = sizeofW(StgThunkHeader) + 2;
930 size = sizeofW(StgThunkHeader) + 1;
937 case IND_OLDGEN_PERM:
939 case SE_CAF_BLACKHOLE:
952 size = sizeW_fromITBL(info);
956 // Special case/Delicate Hack: INDs don't normally
957 // appear, since we're doing this heap census right
958 // after GC. However, GarbageCollect() also does
959 // resurrectThreads(), which can update some
960 // blackholes when it calls raiseAsync() on the
961 // resurrected threads. So we know that any IND will
962 // be the size of a BLACKHOLE.
963 size = BLACKHOLE_sizeW();
968 size = bco_sizeW((StgBCO *)p);
977 size = sizeW_fromITBL(info);
981 size = ap_sizeW((StgAP *)p);
985 size = pap_sizeW((StgPAP *)p);
989 size = ap_stack_sizeW((StgAP_STACK *)p);
994 size = arr_words_sizeW(stgCast(StgArrWords*,p));
997 case MUT_ARR_PTRS_CLEAN:
998 case MUT_ARR_PTRS_DIRTY:
999 case MUT_ARR_PTRS_FROZEN:
1000 case MUT_ARR_PTRS_FROZEN0:
1002 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1008 if (RtsFlags.ProfFlags.includeTSOs) {
1009 size = tso_sizeW((StgTSO *)p);
1012 // Skip this TSO and move on to the next object
1013 p += tso_sizeW((StgTSO *)p);
1017 size = tso_sizeW((StgTSO *)p);
1023 size = sizeofW(StgTRecHeader);
1026 case TVAR_WATCH_QUEUE:
1028 size = sizeofW(StgTVarWatchQueue);
1031 case INVARIANT_CHECK_QUEUE:
1033 size = sizeofW(StgInvariantCheckQueue);
1036 case ATOMIC_INVARIANT:
1038 size = sizeofW(StgAtomicInvariant);
1043 size = sizeofW(StgTVar);
1048 size = sizeofW(StgTRecChunk);
1052 barf("heapCensus, unknown object: %d", info->type);
1058 // subtract the profiling overhead
1059 real_size = size - sizeofW(StgProfHeader);
1064 if (closureSatisfiesConstraints((StgClosure*)p)) {
1066 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1068 census->prim += real_size;
1069 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1070 census->not_used += real_size;
1072 census->used += real_size;
1076 identity = closureIdentity((StgClosure *)p);
1078 if (identity != NULL) {
1079 ctr = lookupHashTable( census->hash, (StgWord)identity );
1082 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1084 ctr->c.ldv.prim += real_size;
1085 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1086 ctr->c.ldv.not_used += real_size;
1088 ctr->c.ldv.used += real_size;
1092 ctr->c.resid += real_size;
1095 ctr = arenaAlloc( census->arena, sizeof(counter) );
1097 insertHashTable( census->hash, (StgWord)identity, ctr );
1098 ctr->identity = identity;
1099 ctr->next = census->ctrs;
1103 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1105 ctr->c.ldv.prim = real_size;
1106 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1107 ctr->c.ldv.not_used = real_size;
1109 ctr->c.ldv.used = real_size;
1113 ctr->c.resid = real_size;
1131 census = &censuses[era];
1132 census->time = mut_user_time();
1134 // calculate retainer sets if necessary
1136 if (doingRetainerProfiling()) {
1142 stat_startHeapCensus();
1145 // Traverse the heap, collecting the census info
1147 // First the small_alloc_list: we have to fix the free pointer at
1148 // the end by calling tidyAllocatedLists() first.
1149 tidyAllocateLists();
1150 heapCensusChain( census, small_alloc_list );
1152 // Now traverse the heap in each generation/step.
1153 if (RtsFlags.GcFlags.generations == 1) {
1154 heapCensusChain( census, g0s0->blocks );
1156 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1157 for (s = 0; s < generations[g].n_steps; s++) {
1158 heapCensusChain( census, generations[g].steps[s].blocks );
1159 // Are we interested in large objects? might be
1160 // confusing to include the stack in a heap profile.
1161 heapCensusChain( census, generations[g].steps[s].large_objects );
1166 // dump out the census info
1168 // We can't generate any info for LDV profiling until
1169 // the end of the run...
1170 if (!doingLDVProfiling())
1171 dumpCensus( census );
1173 dumpCensus( census );
1177 // free our storage, unless we're keeping all the census info for
1178 // future restriction by biography.
1180 if (RtsFlags.ProfFlags.bioSelector == NULL)
1182 freeHashTable( census->hash, NULL/* don't free the elements */ );
1183 arenaFree( census->arena );
1184 census->hash = NULL;
1185 census->arena = NULL;
1189 // we're into the next time period now
1193 stat_endHeapCensus();