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"
23 /* -----------------------------------------------------------------------------
24 * era stores the current time period. It is the same as the
25 * number of censuses that have been performed.
28 * era must be no longer than LDV_SHIFT (15 or 30) bits.
30 * era is initialized to 1 in initHeapProfiling().
32 * max_era is initialized to 2^LDV_SHIFT in initHeapProfiling().
33 * When era reaches max_era, the profiling stops because a closure can
34 * store only up to (max_era - 1) as its creation or last use time.
35 * -------------------------------------------------------------------------- */
39 /* -----------------------------------------------------------------------------
42 * For most heap profiles each closure identity gets a simple count
43 * of live words in the heap at each census. However, if we're
44 * selecting by biography, then we have to keep the various
45 * lag/drag/void counters for each identity.
46 * -------------------------------------------------------------------------- */
47 typedef struct _counter {
52 int prim; // total size of 'inherently used' closures
53 int not_used; // total size of 'never used' closures
54 int used; // total size of 'used at least once' closures
55 int void_total; // current total size of 'destroyed without being used' closures
56 int drag_total; // current total size of 'used at least once and waiting to die'
59 struct _counter *next;
63 initLDVCtr( counter *ctr )
66 ctr->c.ldv.not_used = 0;
68 ctr->c.ldv.void_total = 0;
69 ctr->c.ldv.drag_total = 0;
73 double time; // the time in MUT time when the census is made
78 // for LDV profiling, when just displaying by LDV
86 static Census *censuses = NULL;
87 static nat n_censuses = 0;
90 static void aggregateCensusInfo( void );
93 static void dumpCensus( Census *census );
95 static rtsBool closureSatisfiesConstraints( StgClosure* p );
97 /* ----------------------------------------------------------------------------
98 Closure Type Profiling;
99 ------------------------------------------------------------------------- */
102 static char *type_names[] = {
111 "CONSTR_NOCAF_STATIC",
149 "MUT_ARR_PTRS_CLEAN",
150 "MUT_ARR_PTRS_DIRTY",
151 "MUT_ARR_PTRS_FROZEN0",
152 "MUT_ARR_PTRS_FROZEN",
164 "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_PROF_DESC(get_itbl(p));
195 return GET_PROF_TYPE(get_itbl(p));
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 return GET_CON_DESC(itbl_to_con_itbl(info));
220 return type_names[info->type];
226 barf("closureIdentity");
230 /* --------------------------------------------------------------------------
231 * Profiling type predicates
232 * ----------------------------------------------------------------------- */
234 STATIC_INLINE rtsBool
235 doingLDVProfiling( void )
237 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
238 || RtsFlags.ProfFlags.bioSelector != NULL);
241 STATIC_INLINE rtsBool
242 doingRetainerProfiling( void )
244 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
245 || RtsFlags.ProfFlags.retainerSelector != NULL);
247 #endif /* PROFILING */
249 // Precesses a closure 'c' being destroyed whose size is 'size'.
250 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
251 // such as TSO; they should not be involved in computing dragNew or voidNew.
253 // Even though era is checked in both LdvCensusForDead() and
254 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
255 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
256 // when a thunk is replaced by an indirection object.
260 LDV_recordDead( StgClosure *c, nat size )
266 if (era > 0 && closureSatisfiesConstraints(c)) {
267 size -= sizeofW(StgProfHeader);
268 ASSERT(LDVW(c) != 0);
269 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
270 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
272 if (RtsFlags.ProfFlags.bioSelector == NULL) {
273 censuses[t].void_total += (int)size;
274 censuses[era].void_total -= (int)size;
275 ASSERT(censuses[t].void_total < censuses[t].not_used);
277 id = closureIdentity(c);
278 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
279 ASSERT( ctr != NULL );
280 ctr->c.ldv.void_total += (int)size;
281 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
283 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
285 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
287 ctr->next = censuses[era].ctrs;
288 censuses[era].ctrs = ctr;
290 ctr->c.ldv.void_total -= (int)size;
294 t = LDVW((c)) & LDV_LAST_MASK;
296 if (RtsFlags.ProfFlags.bioSelector == NULL) {
297 censuses[t+1].drag_total += size;
298 censuses[era].drag_total -= size;
301 id = closureIdentity(c);
302 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
303 ASSERT( ctr != NULL );
304 ctr->c.ldv.drag_total += (int)size;
305 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
307 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
309 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
311 ctr->next = censuses[era].ctrs;
312 censuses[era].ctrs = ctr;
314 ctr->c.ldv.drag_total -= (int)size;
322 /* --------------------------------------------------------------------------
323 * Initialize censuses[era];
324 * ----------------------------------------------------------------------- */
327 initEra(Census *census)
329 census->hash = allocHashTable();
331 census->arena = newArena();
333 census->not_used = 0;
336 census->void_total = 0;
337 census->drag_total = 0;
341 freeEra(Census *census)
343 if (RtsFlags.ProfFlags.bioSelector != NULL)
344 // when bioSelector==NULL, these are freed in heapCensus()
346 arenaFree(census->arena);
347 freeHashTable(census->hash, NULL);
351 /* --------------------------------------------------------------------------
352 * Increases era by 1 and initialize census[era].
353 * Reallocates gi[] and increases its size if needed.
354 * ----------------------------------------------------------------------- */
360 if (doingLDVProfiling()) {
363 if (era == max_era) {
364 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
365 stg_exit(EXIT_FAILURE);
368 if (era == n_censuses) {
370 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
374 #endif /* PROFILING */
376 initEra( &censuses[era] );
379 /* ----------------------------------------------------------------------------
380 * Heap profiling by info table
381 * ------------------------------------------------------------------------- */
383 #if !defined(PROFILING)
385 static char *hp_filename;
387 void initProfiling1 (void)
391 void freeProfiling1 (void)
395 void initProfiling2 (void)
399 prog = stgMallocBytes(strlen(prog_name) + 1, "initProfiling2");
400 strcpy(prog, prog_name);
401 #ifdef mingw32_HOST_OS
402 // on Windows, drop the .exe suffix if there is one
405 suff = strrchr(prog,'.');
406 if (suff != NULL && !strcmp(suff,".exe")) {
412 if (RtsFlags.ProfFlags.doHeapProfile) {
413 /* Initialise the log file name */
414 hp_filename = stgMallocBytes(strlen(prog) + 6, "hpFileName");
415 sprintf(hp_filename, "%s.hp", prog);
417 /* open the log file */
418 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
419 debugBelch("Can't open profiling report file %s\n",
421 RtsFlags.ProfFlags.doHeapProfile = 0;
431 void endProfiling( void )
435 #endif /* !PROFILING */
438 printSample(rtsBool beginSample, StgDouble sampleValue)
440 StgDouble fractionalPart, integralPart;
441 fractionalPart = modf(sampleValue, &integralPart);
442 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
443 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
444 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
447 /* --------------------------------------------------------------------------
448 * Initialize the heap profilier
449 * ----------------------------------------------------------------------- */
451 initHeapProfiling(void)
453 if (! RtsFlags.ProfFlags.doHeapProfile) {
458 if (doingLDVProfiling() && doingRetainerProfiling()) {
459 errorBelch("cannot mix -hb and -hr");
460 stg_exit(EXIT_FAILURE);
464 // we only count eras if we're doing LDV profiling. Otherwise era
467 if (doingLDVProfiling()) {
475 // max_era = 2^LDV_SHIFT
476 max_era = 1 << LDV_SHIFT;
479 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
481 initEra( &censuses[era] );
483 /* initProfilingLogFile(); */
484 fprintf(hp_file, "JOB \"%s", prog_name);
489 for(count = 1; count < prog_argc; count++)
490 fprintf(hp_file, " %s", prog_argv[count]);
491 fprintf(hp_file, " +RTS");
492 for(count = 0; count < rts_argc; count++)
493 fprintf(hp_file, " %s", rts_argv[count]);
495 #endif /* PROFILING */
497 fprintf(hp_file, "\"\n" );
499 fprintf(hp_file, "DATE \"%s\"\n", time_str());
501 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
502 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
504 printSample(rtsTrue, 0);
505 printSample(rtsFalse, 0);
508 if (doingRetainerProfiling()) {
509 initRetainerProfiling();
517 endHeapProfiling(void)
521 if (! RtsFlags.ProfFlags.doHeapProfile) {
526 if (doingRetainerProfiling()) {
527 endRetainerProfiling();
532 if (doingLDVProfiling()) {
535 aggregateCensusInfo();
536 for (t = 1; t < era; t++) {
537 dumpCensus( &censuses[t] );
543 if (doingLDVProfiling()) {
545 for (t = 1; t <= era; t++) {
546 freeEra( &censuses[t] );
549 freeEra( &censuses[0] );
552 freeEra( &censuses[0] );
557 seconds = mut_user_time();
558 printSample(rtsTrue, seconds);
559 printSample(rtsFalse, seconds);
567 buf_append(char *p, const char *q, char *end)
571 for (m = 0; p < end; p++, q++, m++) {
573 if (*q == '\0') { break; }
579 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
581 char buf[max_length+1], *p, *buf_end;
583 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
584 if (ccs == CCS_MAIN) {
589 fprintf(fp, "(%ld)", ccs->ccsID);
592 buf_end = buf + max_length + 1;
594 // keep printing components of the stack until we run out of space
595 // in the buffer. If we run out of space, end with "...".
596 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
598 // CAF cost centres print as M.CAF, but we leave the module
599 // name out of all the others to save space.
600 if (!strcmp(ccs->cc->label,"CAF")) {
601 p += buf_append(p, ccs->cc->module, buf_end);
602 p += buf_append(p, ".CAF", buf_end);
604 p += buf_append(p, ccs->cc->label, buf_end);
605 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
606 p += buf_append(p, "/", buf_end);
611 sprintf(buf+max_length-4, "...");
615 fprintf(fp, "%s", buf);
619 strMatchesSelector( char* str, char* sel )
622 // debugBelch("str_matches_selector %s %s\n", str, sel);
624 // Compare str against wherever we've got to in sel.
626 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
629 // Match if all of str used and have reached the end of a sel fragment.
630 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
633 // No match. Advance sel to the start of the next elem.
634 while (*sel != ',' && *sel != '\0') sel++;
635 if (*sel == ',') sel++;
637 /* Run out of sel ?? */
638 if (*sel == '\0') return rtsFalse;
642 #endif /* PROFILING */
644 /* -----------------------------------------------------------------------------
645 * Figure out whether a closure should be counted in this census, by
646 * testing against all the specified constraints.
647 * -------------------------------------------------------------------------- */
649 closureSatisfiesConstraints( StgClosure* p )
651 #if !defined(PROFILING)
652 (void)p; /* keep gcc -Wall happy */
657 // The CCS has a selected field to indicate whether this closure is
658 // deselected by not being mentioned in the module, CC, or CCS
660 if (!p->header.prof.ccs->selected) {
664 if (RtsFlags.ProfFlags.descrSelector) {
665 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
666 RtsFlags.ProfFlags.descrSelector );
667 if (!b) return rtsFalse;
669 if (RtsFlags.ProfFlags.typeSelector) {
670 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
671 RtsFlags.ProfFlags.typeSelector );
672 if (!b) return rtsFalse;
674 if (RtsFlags.ProfFlags.retainerSelector) {
677 // We must check that the retainer set is valid here. One
678 // reason it might not be valid is if this closure is a
679 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
680 // these aren't reached by the retainer profiler's traversal.
681 if (isRetainerSetFieldValid((StgClosure *)p)) {
682 rs = retainerSetOf((StgClosure *)p);
684 for (i = 0; i < rs->num; i++) {
685 b = strMatchesSelector( rs->element[i]->cc->label,
686 RtsFlags.ProfFlags.retainerSelector );
687 if (b) return rtsTrue;
694 #endif /* PROFILING */
697 /* -----------------------------------------------------------------------------
698 * Aggregate the heap census info for biographical profiling
699 * -------------------------------------------------------------------------- */
702 aggregateCensusInfo( void )
706 counter *c, *d, *ctrs;
709 if (!doingLDVProfiling()) return;
711 // Aggregate the LDV counters when displaying by biography.
712 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
713 int void_total, drag_total;
715 // Now we compute void_total and drag_total for each census
716 // After the program has finished, the void_total field of
717 // each census contains the count of words that were *created*
718 // in this era and were eventually void. Conversely, if a
719 // void closure was destroyed in this era, it will be
720 // represented by a negative count of words in void_total.
722 // To get the count of live words that are void at each
723 // census, just propagate the void_total count forwards:
727 for (t = 1; t < era; t++) { // note: start at 1, not 0
728 void_total += censuses[t].void_total;
729 drag_total += censuses[t].drag_total;
730 censuses[t].void_total = void_total;
731 censuses[t].drag_total = drag_total;
733 ASSERT( censuses[t].void_total <= censuses[t].not_used );
734 // should be true because: void_total is the count of
735 // live words that are void at this census, which *must*
736 // be less than the number of live words that have not
739 ASSERT( censuses[t].drag_total <= censuses[t].used );
740 // similar reasoning as above.
746 // otherwise... we're doing a heap profile that is restricted to
747 // some combination of lag, drag, void or use. We've kept all the
748 // census info for all censuses so far, but we still need to
749 // aggregate the counters forwards.
752 acc = allocHashTable();
755 for (t = 1; t < era; t++) {
757 // first look through all the counters we're aggregating
758 for (c = ctrs; c != NULL; c = c->next) {
759 // if one of the totals is non-zero, then this closure
760 // type must be present in the heap at this census time...
761 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
764 // if this closure identity isn't present in the
765 // census for this time period, then our running
766 // totals *must* be zero.
767 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
769 // debugCCS(c->identity);
770 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
771 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
773 d->c.ldv.void_total += c->c.ldv.void_total;
774 d->c.ldv.drag_total += c->c.ldv.drag_total;
775 c->c.ldv.void_total = d->c.ldv.void_total;
776 c->c.ldv.drag_total = d->c.ldv.drag_total;
778 ASSERT( c->c.ldv.void_total >= 0 );
779 ASSERT( c->c.ldv.drag_total >= 0 );
783 // now look through the counters in this census to find new ones
784 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
785 d = lookupHashTable(acc, (StgWord)c->identity);
787 d = arenaAlloc( arena, sizeof(counter) );
789 insertHashTable( acc, (StgWord)c->identity, d );
790 d->identity = c->identity;
793 d->c.ldv.void_total = c->c.ldv.void_total;
794 d->c.ldv.drag_total = c->c.ldv.drag_total;
796 ASSERT( c->c.ldv.void_total >= 0 );
797 ASSERT( c->c.ldv.drag_total >= 0 );
801 freeHashTable(acc, NULL);
806 /* -----------------------------------------------------------------------------
807 * Print out the results of a heap census.
808 * -------------------------------------------------------------------------- */
810 dumpCensus( Census *census )
815 printSample(rtsTrue, census->time);
818 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
819 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
820 fprintf(hp_file, "LAG\t%lu\n",
821 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
822 fprintf(hp_file, "USE\t%lu\n",
823 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
824 fprintf(hp_file, "INHERENT_USE\t%lu\n",
825 (unsigned long)(census->prim) * sizeof(W_));
826 fprintf(hp_file, "DRAG\t%lu\n",
827 (unsigned long)(census->drag_total) * sizeof(W_));
828 printSample(rtsFalse, census->time);
833 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
836 if (RtsFlags.ProfFlags.bioSelector != NULL) {
838 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
839 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
840 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
841 count += ctr->c.ldv.drag_total;
842 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
843 count += ctr->c.ldv.void_total;
844 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
845 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
849 count = ctr->c.resid;
852 ASSERT( count >= 0 );
854 if (count == 0) continue;
856 #if !defined(PROFILING)
857 switch (RtsFlags.ProfFlags.doHeapProfile) {
858 case HEAP_BY_CLOSURE_TYPE:
859 fprintf(hp_file, "%s", (char *)ctr->identity);
865 switch (RtsFlags.ProfFlags.doHeapProfile) {
867 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
872 fprintf(hp_file, "%s", (char *)ctr->identity);
874 case HEAP_BY_RETAINER:
876 RetainerSet *rs = (RetainerSet *)ctr->identity;
878 // it might be the distinguished retainer set rs_MANY:
879 if (rs == &rs_MANY) {
880 fprintf(hp_file, "MANY");
884 // Mark this retainer set by negating its id, because it
885 // has appeared in at least one census. We print the
886 // values of all such retainer sets into the log file at
887 // the end. A retainer set may exist but not feature in
888 // any censuses if it arose as the intermediate retainer
889 // set for some closure during retainer set calculation.
893 // report in the unit of bytes: * sizeof(StgWord)
894 printRetainerSetShort(hp_file, rs);
898 barf("dumpCensus; doHeapProfile");
902 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
905 printSample(rtsFalse, census->time);
908 /* -----------------------------------------------------------------------------
909 * Code to perform a heap census.
910 * -------------------------------------------------------------------------- */
912 heapCensusChain( Census *census, bdescr *bd )
922 for (; bd != NULL; bd = bd->link) {
924 // HACK: ignore pinned blocks, because they contain gaps.
925 // It's not clear exactly what we'd like to do here, since we
926 // can't tell which objects in the block are actually alive.
927 // Perhaps the whole block should be counted as SYSTEM memory.
928 if (bd->flags & BF_PINNED) {
933 while (p < bd->free) {
934 info = get_itbl((StgClosure *)p);
937 switch (info->type) {
940 size = thunk_sizeW_fromITBL(info);
946 size = sizeofW(StgThunkHeader) + 2;
952 size = sizeofW(StgThunkHeader) + 1;
959 case IND_OLDGEN_PERM:
972 size = sizeW_fromITBL(info);
976 // Special case/Delicate Hack: INDs don't normally
977 // appear, since we're doing this heap census right
978 // after GC. However, GarbageCollect() also does
979 // resurrectThreads(), which can update some
980 // blackholes when it calls raiseAsync() on the
981 // resurrected threads. So we know that any IND will
982 // be the size of a BLACKHOLE.
983 size = BLACKHOLE_sizeW();
988 size = bco_sizeW((StgBCO *)p);
998 size = sizeW_fromITBL(info);
1002 size = ap_sizeW((StgAP *)p);
1006 size = pap_sizeW((StgPAP *)p);
1010 size = ap_stack_sizeW((StgAP_STACK *)p);
1015 size = arr_words_sizeW((StgArrWords*)p);
1018 case MUT_ARR_PTRS_CLEAN:
1019 case MUT_ARR_PTRS_DIRTY:
1020 case MUT_ARR_PTRS_FROZEN:
1021 case MUT_ARR_PTRS_FROZEN0:
1023 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1029 if (RtsFlags.ProfFlags.includeTSOs) {
1030 size = tso_sizeW((StgTSO *)p);
1033 // Skip this TSO and move on to the next object
1034 p += tso_sizeW((StgTSO *)p);
1038 size = tso_sizeW((StgTSO *)p);
1044 size = sizeofW(StgTRecHeader);
1047 case TVAR_WATCH_QUEUE:
1049 size = sizeofW(StgTVarWatchQueue);
1052 case INVARIANT_CHECK_QUEUE:
1054 size = sizeofW(StgInvariantCheckQueue);
1057 case ATOMIC_INVARIANT:
1059 size = sizeofW(StgAtomicInvariant);
1064 size = sizeofW(StgTVar);
1069 size = sizeofW(StgTRecChunk);
1073 barf("heapCensus, unknown object: %d", info->type);
1079 // subtract the profiling overhead
1080 real_size = size - sizeofW(StgProfHeader);
1085 if (closureSatisfiesConstraints((StgClosure*)p)) {
1087 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1089 census->prim += real_size;
1090 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1091 census->not_used += real_size;
1093 census->used += real_size;
1097 identity = closureIdentity((StgClosure *)p);
1099 if (identity != NULL) {
1100 ctr = lookupHashTable( census->hash, (StgWord)identity );
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;
1116 ctr = arenaAlloc( census->arena, sizeof(counter) );
1118 insertHashTable( census->hash, (StgWord)identity, ctr );
1119 ctr->identity = identity;
1120 ctr->next = census->ctrs;
1124 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1126 ctr->c.ldv.prim = real_size;
1127 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1128 ctr->c.ldv.not_used = real_size;
1130 ctr->c.ldv.used = real_size;
1134 ctr->c.resid = real_size;
1152 census = &censuses[era];
1153 census->time = mut_user_time();
1155 // calculate retainer sets if necessary
1157 if (doingRetainerProfiling()) {
1163 stat_startHeapCensus();
1166 // Traverse the heap, collecting the census info
1167 if (RtsFlags.GcFlags.generations == 1) {
1168 heapCensusChain( census, g0s0->blocks );
1170 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1171 for (s = 0; s < generations[g].n_steps; s++) {
1172 heapCensusChain( census, generations[g].steps[s].blocks );
1173 // Are we interested in large objects? might be
1174 // confusing to include the stack in a heap profile.
1175 heapCensusChain( census, generations[g].steps[s].large_objects );
1180 // dump out the census info
1182 // We can't generate any info for LDV profiling until
1183 // the end of the run...
1184 if (!doingLDVProfiling())
1185 dumpCensus( census );
1187 dumpCensus( census );
1191 // free our storage, unless we're keeping all the census info for
1192 // future restriction by biography.
1194 if (RtsFlags.ProfFlags.bioSelector == NULL)
1196 freeHashTable( census->hash, NULL/* don't free the elements */ );
1197 arenaFree( census->arena );
1198 census->hash = NULL;
1199 census->arena = NULL;
1203 // we're into the next time period now
1207 stat_endHeapCensus();