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 return GET_CON_DESC(itbl_to_con_itbl(info));
222 return type_names[info->type];
228 barf("closureIdentity");
232 /* --------------------------------------------------------------------------
233 * Profiling type predicates
234 * ----------------------------------------------------------------------- */
236 STATIC_INLINE rtsBool
237 doingLDVProfiling( void )
239 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
240 || RtsFlags.ProfFlags.bioSelector != NULL);
243 STATIC_INLINE rtsBool
244 doingRetainerProfiling( void )
246 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
247 || RtsFlags.ProfFlags.retainerSelector != NULL);
249 #endif /* PROFILING */
251 // Precesses a closure 'c' being destroyed whose size is 'size'.
252 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
253 // such as TSO; they should not be involved in computing dragNew or voidNew.
255 // Even though era is checked in both LdvCensusForDead() and
256 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
257 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
258 // when a thunk is replaced by an indirection object.
262 LDV_recordDead( StgClosure *c, nat size )
268 if (era > 0 && closureSatisfiesConstraints(c)) {
269 size -= sizeofW(StgProfHeader);
270 ASSERT(LDVW(c) != 0);
271 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
272 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
274 if (RtsFlags.ProfFlags.bioSelector == NULL) {
275 censuses[t].void_total += (int)size;
276 censuses[era].void_total -= (int)size;
277 ASSERT(censuses[t].void_total < censuses[t].not_used);
279 id = closureIdentity(c);
280 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
281 ASSERT( ctr != NULL );
282 ctr->c.ldv.void_total += (int)size;
283 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
285 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
287 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
289 ctr->next = censuses[era].ctrs;
290 censuses[era].ctrs = ctr;
292 ctr->c.ldv.void_total -= (int)size;
296 t = LDVW((c)) & LDV_LAST_MASK;
298 if (RtsFlags.ProfFlags.bioSelector == NULL) {
299 censuses[t+1].drag_total += size;
300 censuses[era].drag_total -= size;
303 id = closureIdentity(c);
304 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
305 ASSERT( ctr != NULL );
306 ctr->c.ldv.drag_total += (int)size;
307 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
309 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
311 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
313 ctr->next = censuses[era].ctrs;
314 censuses[era].ctrs = ctr;
316 ctr->c.ldv.drag_total -= (int)size;
324 /* --------------------------------------------------------------------------
325 * Initialize censuses[era];
326 * ----------------------------------------------------------------------- */
329 initEra(Census *census)
331 census->hash = allocHashTable();
333 census->arena = newArena();
335 census->not_used = 0;
338 census->void_total = 0;
339 census->drag_total = 0;
343 freeEra(Census *census)
345 if (RtsFlags.ProfFlags.bioSelector != NULL)
346 // when bioSelector==NULL, these are freed in heapCensus()
348 arenaFree(census->arena);
349 freeHashTable(census->hash, NULL);
353 /* --------------------------------------------------------------------------
354 * Increases era by 1 and initialize census[era].
355 * Reallocates gi[] and increases its size if needed.
356 * ----------------------------------------------------------------------- */
362 if (doingLDVProfiling()) {
365 if (era == max_era) {
366 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
367 stg_exit(EXIT_FAILURE);
370 if (era == n_censuses) {
372 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
376 #endif /* PROFILING */
378 initEra( &censuses[era] );
381 /* ----------------------------------------------------------------------------
382 * Heap profiling by info table
383 * ------------------------------------------------------------------------- */
385 #if !defined(PROFILING)
387 static char *hp_filename;
389 void initProfiling1 (void)
393 void freeProfiling1 (void)
397 void initProfiling2 (void)
399 if (RtsFlags.ProfFlags.doHeapProfile) {
400 /* Initialise the log file name */
401 hp_filename = stgMallocBytes(strlen(prog_name) + 6, "hpFileName");
402 sprintf(hp_filename, "%s.hp", prog_name);
404 /* open the log file */
405 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
406 debugBelch("Can't open profiling report file %s\n",
408 RtsFlags.ProfFlags.doHeapProfile = 0;
416 void endProfiling( void )
420 #endif /* !PROFILING */
423 printSample(rtsBool beginSample, StgDouble sampleValue)
425 StgDouble fractionalPart, integralPart;
426 fractionalPart = modf(sampleValue, &integralPart);
427 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
428 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
429 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
432 /* --------------------------------------------------------------------------
433 * Initialize the heap profilier
434 * ----------------------------------------------------------------------- */
436 initHeapProfiling(void)
438 if (! RtsFlags.ProfFlags.doHeapProfile) {
443 if (doingLDVProfiling() && doingRetainerProfiling()) {
444 errorBelch("cannot mix -hb and -hr");
445 stg_exit(EXIT_FAILURE);
449 // we only count eras if we're doing LDV profiling. Otherwise era
452 if (doingLDVProfiling()) {
460 { // max_era = 2^LDV_SHIFT
463 for (p = 0; p < LDV_SHIFT; p++)
468 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
470 initEra( &censuses[era] );
472 /* initProfilingLogFile(); */
473 fprintf(hp_file, "JOB \"%s", prog_name);
478 for(count = 1; count < prog_argc; count++)
479 fprintf(hp_file, " %s", prog_argv[count]);
480 fprintf(hp_file, " +RTS");
481 for(count = 0; count < rts_argc; count++)
482 fprintf(hp_file, " %s", rts_argv[count]);
484 #endif /* PROFILING */
486 fprintf(hp_file, "\"\n" );
488 fprintf(hp_file, "DATE \"%s\"\n", time_str());
490 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
491 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
493 printSample(rtsTrue, 0);
494 printSample(rtsFalse, 0);
497 if (doingRetainerProfiling()) {
498 initRetainerProfiling();
506 endHeapProfiling(void)
510 if (! RtsFlags.ProfFlags.doHeapProfile) {
515 if (doingRetainerProfiling()) {
516 endRetainerProfiling();
521 if (doingLDVProfiling()) {
524 aggregateCensusInfo();
525 for (t = 1; t < era; t++) {
526 dumpCensus( &censuses[t] );
532 if (doingLDVProfiling()) {
534 for (t = 1; t <= era; t++) {
535 freeEra( &censuses[t] );
538 freeEra( &censuses[0] );
541 freeEra( &censuses[0] );
546 seconds = mut_user_time();
547 printSample(rtsTrue, seconds);
548 printSample(rtsFalse, seconds);
556 buf_append(char *p, const char *q, char *end)
560 for (m = 0; p < end; p++, q++, m++) {
562 if (*q == '\0') { break; }
568 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
570 char buf[max_length+1], *p, *buf_end;
572 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
573 if (ccs == CCS_MAIN) {
578 fprintf(fp, "(%ld)", ccs->ccsID);
581 buf_end = buf + max_length + 1;
583 // keep printing components of the stack until we run out of space
584 // in the buffer. If we run out of space, end with "...".
585 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
587 // CAF cost centres print as M.CAF, but we leave the module
588 // name out of all the others to save space.
589 if (!strcmp(ccs->cc->label,"CAF")) {
590 p += buf_append(p, ccs->cc->module, buf_end);
591 p += buf_append(p, ".CAF", buf_end);
593 p += buf_append(p, ccs->cc->label, buf_end);
594 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
595 p += buf_append(p, "/", buf_end);
600 sprintf(buf+max_length-4, "...");
604 fprintf(fp, "%s", buf);
606 #endif /* PROFILING */
609 strMatchesSelector( char* str, char* sel )
612 // debugBelch("str_matches_selector %s %s\n", str, sel);
614 // Compare str against wherever we've got to in sel.
616 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
619 // Match if all of str used and have reached the end of a sel fragment.
620 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
623 // No match. Advance sel to the start of the next elem.
624 while (*sel != ',' && *sel != '\0') sel++;
625 if (*sel == ',') sel++;
627 /* Run out of sel ?? */
628 if (*sel == '\0') return rtsFalse;
632 /* -----------------------------------------------------------------------------
633 * Figure out whether a closure should be counted in this census, by
634 * testing against all the specified constraints.
635 * -------------------------------------------------------------------------- */
637 closureSatisfiesConstraints( StgClosure* p )
639 #if !defined(PROFILING)
640 (void)p; /* keep gcc -Wall happy */
645 // The CCS has a selected field to indicate whether this closure is
646 // deselected by not being mentioned in the module, CC, or CCS
648 if (!p->header.prof.ccs->selected) {
652 if (RtsFlags.ProfFlags.descrSelector) {
653 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
654 RtsFlags.ProfFlags.descrSelector );
655 if (!b) return rtsFalse;
657 if (RtsFlags.ProfFlags.typeSelector) {
658 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
659 RtsFlags.ProfFlags.typeSelector );
660 if (!b) return rtsFalse;
662 if (RtsFlags.ProfFlags.retainerSelector) {
665 // We must check that the retainer set is valid here. One
666 // reason it might not be valid is if this closure is a
667 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
668 // these aren't reached by the retainer profiler's traversal.
669 if (isRetainerSetFieldValid((StgClosure *)p)) {
670 rs = retainerSetOf((StgClosure *)p);
672 for (i = 0; i < rs->num; i++) {
673 b = strMatchesSelector( rs->element[i]->cc->label,
674 RtsFlags.ProfFlags.retainerSelector );
675 if (b) return rtsTrue;
682 #endif /* PROFILING */
685 /* -----------------------------------------------------------------------------
686 * Aggregate the heap census info for biographical profiling
687 * -------------------------------------------------------------------------- */
690 aggregateCensusInfo( void )
694 counter *c, *d, *ctrs;
697 if (!doingLDVProfiling()) return;
699 // Aggregate the LDV counters when displaying by biography.
700 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
701 int void_total, drag_total;
703 // Now we compute void_total and drag_total for each census
704 // After the program has finished, the void_total field of
705 // each census contains the count of words that were *created*
706 // in this era and were eventually void. Conversely, if a
707 // void closure was destroyed in this era, it will be
708 // represented by a negative count of words in void_total.
710 // To get the count of live words that are void at each
711 // census, just propagate the void_total count forwards:
715 for (t = 1; t < era; t++) { // note: start at 1, not 0
716 void_total += censuses[t].void_total;
717 drag_total += censuses[t].drag_total;
718 censuses[t].void_total = void_total;
719 censuses[t].drag_total = drag_total;
721 ASSERT( censuses[t].void_total <= censuses[t].not_used );
722 // should be true because: void_total is the count of
723 // live words that are void at this census, which *must*
724 // be less than the number of live words that have not
727 ASSERT( censuses[t].drag_total <= censuses[t].used );
728 // similar reasoning as above.
734 // otherwise... we're doing a heap profile that is restricted to
735 // some combination of lag, drag, void or use. We've kept all the
736 // census info for all censuses so far, but we still need to
737 // aggregate the counters forwards.
740 acc = allocHashTable();
743 for (t = 1; t < era; t++) {
745 // first look through all the counters we're aggregating
746 for (c = ctrs; c != NULL; c = c->next) {
747 // if one of the totals is non-zero, then this closure
748 // type must be present in the heap at this census time...
749 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
752 // if this closure identity isn't present in the
753 // census for this time period, then our running
754 // totals *must* be zero.
755 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
757 // debugCCS(c->identity);
758 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
759 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
761 d->c.ldv.void_total += c->c.ldv.void_total;
762 d->c.ldv.drag_total += c->c.ldv.drag_total;
763 c->c.ldv.void_total = d->c.ldv.void_total;
764 c->c.ldv.drag_total = d->c.ldv.drag_total;
766 ASSERT( c->c.ldv.void_total >= 0 );
767 ASSERT( c->c.ldv.drag_total >= 0 );
771 // now look through the counters in this census to find new ones
772 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
773 d = lookupHashTable(acc, (StgWord)c->identity);
775 d = arenaAlloc( arena, sizeof(counter) );
777 insertHashTable( acc, (StgWord)c->identity, d );
778 d->identity = c->identity;
781 d->c.ldv.void_total = c->c.ldv.void_total;
782 d->c.ldv.drag_total = c->c.ldv.drag_total;
784 ASSERT( c->c.ldv.void_total >= 0 );
785 ASSERT( c->c.ldv.drag_total >= 0 );
789 freeHashTable(acc, NULL);
794 /* -----------------------------------------------------------------------------
795 * Print out the results of a heap census.
796 * -------------------------------------------------------------------------- */
798 dumpCensus( Census *census )
803 printSample(rtsTrue, census->time);
806 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
807 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
808 fprintf(hp_file, "LAG\t%lu\n",
809 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
810 fprintf(hp_file, "USE\t%lu\n",
811 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
812 fprintf(hp_file, "INHERENT_USE\t%lu\n",
813 (unsigned long)(census->prim) * sizeof(W_));
814 fprintf(hp_file, "DRAG\t%lu\n",
815 (unsigned long)(census->drag_total) * sizeof(W_));
816 printSample(rtsFalse, census->time);
821 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
824 if (RtsFlags.ProfFlags.bioSelector != NULL) {
826 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
827 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
828 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
829 count += ctr->c.ldv.drag_total;
830 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
831 count += ctr->c.ldv.void_total;
832 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
833 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
837 count = ctr->c.resid;
840 ASSERT( count >= 0 );
842 if (count == 0) continue;
844 #if !defined(PROFILING)
845 switch (RtsFlags.ProfFlags.doHeapProfile) {
846 case HEAP_BY_CLOSURE_TYPE:
847 fprintf(hp_file, "%s", (char *)ctr->identity);
853 switch (RtsFlags.ProfFlags.doHeapProfile) {
855 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
860 fprintf(hp_file, "%s", (char *)ctr->identity);
862 case HEAP_BY_RETAINER:
864 RetainerSet *rs = (RetainerSet *)ctr->identity;
866 // it might be the distinguished retainer set rs_MANY:
867 if (rs == &rs_MANY) {
868 fprintf(hp_file, "MANY");
872 // Mark this retainer set by negating its id, because it
873 // has appeared in at least one census. We print the
874 // values of all such retainer sets into the log file at
875 // the end. A retainer set may exist but not feature in
876 // any censuses if it arose as the intermediate retainer
877 // set for some closure during retainer set calculation.
881 // report in the unit of bytes: * sizeof(StgWord)
882 printRetainerSetShort(hp_file, rs);
886 barf("dumpCensus; doHeapProfile");
890 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
893 printSample(rtsFalse, census->time);
896 /* -----------------------------------------------------------------------------
897 * Code to perform a heap census.
898 * -------------------------------------------------------------------------- */
900 heapCensusChain( Census *census, bdescr *bd )
910 for (; bd != NULL; bd = bd->link) {
912 // HACK: ignore pinned blocks, because they contain gaps.
913 // It's not clear exactly what we'd like to do here, since we
914 // can't tell which objects in the block are actually alive.
915 // Perhaps the whole block should be counted as SYSTEM memory.
916 if (bd->flags & BF_PINNED) {
921 while (p < bd->free) {
922 info = get_itbl((StgClosure *)p);
925 switch (info->type) {
928 size = thunk_sizeW_fromITBL(info);
934 size = sizeofW(StgThunkHeader) + 2;
940 size = sizeofW(StgThunkHeader) + 1;
947 case IND_OLDGEN_PERM:
949 case SE_CAF_BLACKHOLE:
962 size = sizeW_fromITBL(info);
966 // Special case/Delicate Hack: INDs don't normally
967 // appear, since we're doing this heap census right
968 // after GC. However, GarbageCollect() also does
969 // resurrectThreads(), which can update some
970 // blackholes when it calls raiseAsync() on the
971 // resurrected threads. So we know that any IND will
972 // be the size of a BLACKHOLE.
973 size = BLACKHOLE_sizeW();
978 size = bco_sizeW((StgBCO *)p);
987 size = sizeW_fromITBL(info);
991 size = ap_sizeW((StgAP *)p);
995 size = pap_sizeW((StgPAP *)p);
999 size = ap_stack_sizeW((StgAP_STACK *)p);
1004 size = arr_words_sizeW(stgCast(StgArrWords*,p));
1007 case MUT_ARR_PTRS_CLEAN:
1008 case MUT_ARR_PTRS_DIRTY:
1009 case MUT_ARR_PTRS_FROZEN:
1010 case MUT_ARR_PTRS_FROZEN0:
1012 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1018 if (RtsFlags.ProfFlags.includeTSOs) {
1019 size = tso_sizeW((StgTSO *)p);
1022 // Skip this TSO and move on to the next object
1023 p += tso_sizeW((StgTSO *)p);
1027 size = tso_sizeW((StgTSO *)p);
1033 size = sizeofW(StgTRecHeader);
1036 case TVAR_WATCH_QUEUE:
1038 size = sizeofW(StgTVarWatchQueue);
1041 case INVARIANT_CHECK_QUEUE:
1043 size = sizeofW(StgInvariantCheckQueue);
1046 case ATOMIC_INVARIANT:
1048 size = sizeofW(StgAtomicInvariant);
1053 size = sizeofW(StgTVar);
1058 size = sizeofW(StgTRecChunk);
1062 barf("heapCensus, unknown object: %d", info->type);
1068 // subtract the profiling overhead
1069 real_size = size - sizeofW(StgProfHeader);
1074 if (closureSatisfiesConstraints((StgClosure*)p)) {
1076 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1078 census->prim += real_size;
1079 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1080 census->not_used += real_size;
1082 census->used += real_size;
1086 identity = closureIdentity((StgClosure *)p);
1088 if (identity != NULL) {
1089 ctr = lookupHashTable( census->hash, (StgWord)identity );
1092 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1094 ctr->c.ldv.prim += real_size;
1095 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1096 ctr->c.ldv.not_used += real_size;
1098 ctr->c.ldv.used += real_size;
1102 ctr->c.resid += real_size;
1105 ctr = arenaAlloc( census->arena, sizeof(counter) );
1107 insertHashTable( census->hash, (StgWord)identity, ctr );
1108 ctr->identity = identity;
1109 ctr->next = census->ctrs;
1113 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1115 ctr->c.ldv.prim = real_size;
1116 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1117 ctr->c.ldv.not_used = real_size;
1119 ctr->c.ldv.used = real_size;
1123 ctr->c.resid = real_size;
1141 census = &censuses[era];
1142 census->time = mut_user_time();
1144 // calculate retainer sets if necessary
1146 if (doingRetainerProfiling()) {
1152 stat_startHeapCensus();
1155 // Traverse the heap, collecting the census info
1157 // First the small_alloc_list: we have to fix the free pointer at
1158 // the end by calling tidyAllocatedLists() first.
1159 tidyAllocateLists();
1160 heapCensusChain( census, small_alloc_list );
1162 // Now traverse the heap in each generation/step.
1163 if (RtsFlags.GcFlags.generations == 1) {
1164 heapCensusChain( census, g0s0->blocks );
1166 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1167 for (s = 0; s < generations[g].n_steps; s++) {
1168 heapCensusChain( census, generations[g].steps[s].blocks );
1169 // Are we interested in large objects? might be
1170 // confusing to include the stack in a heap profile.
1171 heapCensusChain( census, generations[g].steps[s].large_objects );
1176 // dump out the census info
1178 // We can't generate any info for LDV profiling until
1179 // the end of the run...
1180 if (!doingLDVProfiling())
1181 dumpCensus( census );
1183 dumpCensus( census );
1187 // free our storage, unless we're keeping all the census info for
1188 // future restriction by biography.
1190 if (RtsFlags.ProfFlags.bioSelector == NULL)
1192 freeHashTable( census->hash, NULL/* don't free the elements */ );
1193 arenaFree( census->arena );
1194 census->hash = NULL;
1195 census->arena = NULL;
1199 // we're into the next time period now
1203 stat_endHeapCensus();