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"
21 #include "sm/GCThread.h"
25 /* -----------------------------------------------------------------------------
26 * era stores the current time period. It is the same as the
27 * number of censuses that have been performed.
30 * era must be no longer than LDV_SHIFT (15 or 30) bits.
32 * era is initialized to 1 in initHeapProfiling().
34 * max_era is initialized to 2^LDV_SHIFT in initHeapProfiling().
35 * When era reaches max_era, the profiling stops because a closure can
36 * store only up to (max_era - 1) as its creation or last use time.
37 * -------------------------------------------------------------------------- */
41 /* -----------------------------------------------------------------------------
44 * For most heap profiles each closure identity gets a simple count
45 * of live words in the heap at each census. However, if we're
46 * selecting by biography, then we have to keep the various
47 * lag/drag/void counters for each identity.
48 * -------------------------------------------------------------------------- */
49 typedef struct _counter {
54 int prim; // total size of 'inherently used' closures
55 int not_used; // total size of 'never used' closures
56 int used; // total size of 'used at least once' closures
57 int void_total; // current total size of 'destroyed without being used' closures
58 int drag_total; // current total size of 'used at least once and waiting to die'
61 struct _counter *next;
65 initLDVCtr( counter *ctr )
68 ctr->c.ldv.not_used = 0;
70 ctr->c.ldv.void_total = 0;
71 ctr->c.ldv.drag_total = 0;
75 double time; // the time in MUT time when the census is made
80 // for LDV profiling, when just displaying by LDV
88 static Census *censuses = NULL;
89 static nat n_censuses = 0;
92 static void aggregateCensusInfo( void );
95 static void dumpCensus( Census *census );
97 static rtsBool closureSatisfiesConstraints( StgClosure* p );
99 /* ----------------------------------------------------------------------------
100 * Find the "closure identity", which is a unique pointer reresenting
101 * the band to which this closure's heap space is attributed in the
103 * ------------------------------------------------------------------------- */
105 closureIdentity( StgClosure *p )
107 switch (RtsFlags.ProfFlags.doHeapProfile) {
111 return p->header.prof.ccs;
113 return p->header.prof.ccs->cc->module;
115 return GET_PROF_DESC(get_itbl(p));
117 return GET_PROF_TYPE(get_itbl(p));
118 case HEAP_BY_RETAINER:
119 // AFAIK, the only closures in the heap which might not have a
120 // valid retainer set are DEAD_WEAK closures.
121 if (isRetainerSetFieldValid(p))
122 return retainerSetOf(p);
127 case HEAP_BY_CLOSURE_TYPE:
131 switch (info->type) {
139 case CONSTR_NOCAF_STATIC:
140 return GET_CON_DESC(itbl_to_con_itbl(info));
142 return closure_type_names[info->type];
148 barf("closureIdentity");
152 /* --------------------------------------------------------------------------
153 * Profiling type predicates
154 * ----------------------------------------------------------------------- */
156 STATIC_INLINE rtsBool
157 doingLDVProfiling( void )
159 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
160 || RtsFlags.ProfFlags.bioSelector != NULL);
163 STATIC_INLINE rtsBool
164 doingRetainerProfiling( void )
166 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
167 || RtsFlags.ProfFlags.retainerSelector != NULL);
169 #endif /* PROFILING */
171 // Precesses a closure 'c' being destroyed whose size is 'size'.
172 // Make sure that LDV_recordDead() is not invoked on 'inherently used' closures
173 // such as TSO; they should not be involved in computing dragNew or voidNew.
175 // Even though era is checked in both LdvCensusForDead() and
176 // LdvCensusKillAll(), we still need to make sure that era is > 0 because
177 // LDV_recordDead() may be called from elsewhere in the runtime system. E.g.,
178 // when a thunk is replaced by an indirection object.
182 LDV_recordDead( StgClosure *c, nat size )
188 if (era > 0 && closureSatisfiesConstraints(c)) {
189 size -= sizeofW(StgProfHeader);
190 ASSERT(LDVW(c) != 0);
191 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
192 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
194 if (RtsFlags.ProfFlags.bioSelector == NULL) {
195 censuses[t].void_total += (int)size;
196 censuses[era].void_total -= (int)size;
197 ASSERT(censuses[t].void_total < censuses[t].not_used);
199 id = closureIdentity(c);
200 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
201 ASSERT( ctr != NULL );
202 ctr->c.ldv.void_total += (int)size;
203 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
205 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
207 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
209 ctr->next = censuses[era].ctrs;
210 censuses[era].ctrs = ctr;
212 ctr->c.ldv.void_total -= (int)size;
216 t = LDVW((c)) & LDV_LAST_MASK;
218 if (RtsFlags.ProfFlags.bioSelector == NULL) {
219 censuses[t+1].drag_total += size;
220 censuses[era].drag_total -= size;
223 id = closureIdentity(c);
224 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
225 ASSERT( ctr != NULL );
226 ctr->c.ldv.drag_total += (int)size;
227 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
229 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
231 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
233 ctr->next = censuses[era].ctrs;
234 censuses[era].ctrs = ctr;
236 ctr->c.ldv.drag_total -= (int)size;
244 /* --------------------------------------------------------------------------
245 * Initialize censuses[era];
246 * ----------------------------------------------------------------------- */
249 initEra(Census *census)
251 census->hash = allocHashTable();
253 census->arena = newArena();
255 census->not_used = 0;
258 census->void_total = 0;
259 census->drag_total = 0;
263 freeEra(Census *census)
265 if (RtsFlags.ProfFlags.bioSelector != NULL)
266 // when bioSelector==NULL, these are freed in heapCensus()
268 arenaFree(census->arena);
269 freeHashTable(census->hash, NULL);
273 /* --------------------------------------------------------------------------
274 * Increases era by 1 and initialize census[era].
275 * Reallocates gi[] and increases its size if needed.
276 * ----------------------------------------------------------------------- */
282 if (doingLDVProfiling()) {
285 if (era == max_era) {
286 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
287 stg_exit(EXIT_FAILURE);
290 if (era == n_censuses) {
292 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
296 #endif /* PROFILING */
298 initEra( &censuses[era] );
301 /* ----------------------------------------------------------------------------
302 * Heap profiling by info table
303 * ------------------------------------------------------------------------- */
305 #if !defined(PROFILING)
307 static char *hp_filename;
309 void initProfiling1 (void)
313 void freeProfiling (void)
317 void initProfiling2 (void)
321 prog = stgMallocBytes(strlen(prog_name) + 1, "initProfiling2");
322 strcpy(prog, prog_name);
323 #ifdef mingw32_HOST_OS
324 // on Windows, drop the .exe suffix if there is one
327 suff = strrchr(prog,'.');
328 if (suff != NULL && !strcmp(suff,".exe")) {
334 if (RtsFlags.ProfFlags.doHeapProfile) {
335 /* Initialise the log file name */
336 hp_filename = stgMallocBytes(strlen(prog) + 6, "hpFileName");
337 sprintf(hp_filename, "%s.hp", prog);
339 /* open the log file */
340 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
341 debugBelch("Can't open profiling report file %s\n",
343 RtsFlags.ProfFlags.doHeapProfile = 0;
353 void endProfiling( void )
357 #endif /* !PROFILING */
360 printSample(rtsBool beginSample, StgDouble sampleValue)
362 StgDouble fractionalPart, integralPart;
363 fractionalPart = modf(sampleValue, &integralPart);
364 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
365 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
366 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
369 /* --------------------------------------------------------------------------
370 * Initialize the heap profilier
371 * ----------------------------------------------------------------------- */
373 initHeapProfiling(void)
375 if (! RtsFlags.ProfFlags.doHeapProfile) {
380 if (doingLDVProfiling() && doingRetainerProfiling()) {
381 errorBelch("cannot mix -hb and -hr");
382 stg_exit(EXIT_FAILURE);
386 // we only count eras if we're doing LDV profiling. Otherwise era
389 if (doingLDVProfiling()) {
397 // max_era = 2^LDV_SHIFT
398 max_era = 1 << LDV_SHIFT;
401 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
403 initEra( &censuses[era] );
405 /* initProfilingLogFile(); */
406 fprintf(hp_file, "JOB \"%s", prog_name);
411 for(count = 1; count < prog_argc; count++)
412 fprintf(hp_file, " %s", prog_argv[count]);
413 fprintf(hp_file, " +RTS");
414 for(count = 0; count < rts_argc; count++)
415 fprintf(hp_file, " %s", rts_argv[count]);
417 #endif /* PROFILING */
419 fprintf(hp_file, "\"\n" );
421 fprintf(hp_file, "DATE \"%s\"\n", time_str());
423 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
424 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
426 printSample(rtsTrue, 0);
427 printSample(rtsFalse, 0);
430 if (doingRetainerProfiling()) {
431 initRetainerProfiling();
439 endHeapProfiling(void)
443 if (! RtsFlags.ProfFlags.doHeapProfile) {
448 if (doingRetainerProfiling()) {
449 endRetainerProfiling();
454 if (doingLDVProfiling()) {
457 aggregateCensusInfo();
458 for (t = 1; t < era; t++) {
459 dumpCensus( &censuses[t] );
465 if (doingLDVProfiling()) {
467 for (t = 1; t <= era; t++) {
468 freeEra( &censuses[t] );
471 freeEra( &censuses[0] );
474 freeEra( &censuses[0] );
479 seconds = mut_user_time();
480 printSample(rtsTrue, seconds);
481 printSample(rtsFalse, seconds);
489 buf_append(char *p, const char *q, char *end)
493 for (m = 0; p < end; p++, q++, m++) {
495 if (*q == '\0') { break; }
501 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
503 char buf[max_length+1], *p, *buf_end;
505 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
506 if (ccs == CCS_MAIN) {
511 fprintf(fp, "(%ld)", ccs->ccsID);
514 buf_end = buf + max_length + 1;
516 // keep printing components of the stack until we run out of space
517 // in the buffer. If we run out of space, end with "...".
518 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
520 // CAF cost centres print as M.CAF, but we leave the module
521 // name out of all the others to save space.
522 if (!strcmp(ccs->cc->label,"CAF")) {
523 p += buf_append(p, ccs->cc->module, buf_end);
524 p += buf_append(p, ".CAF", buf_end);
526 p += buf_append(p, ccs->cc->label, buf_end);
527 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
528 p += buf_append(p, "/", buf_end);
533 sprintf(buf+max_length-4, "...");
537 fprintf(fp, "%s", buf);
541 strMatchesSelector( char* str, char* sel )
544 // debugBelch("str_matches_selector %s %s\n", str, sel);
546 // Compare str against wherever we've got to in sel.
548 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
551 // Match if all of str used and have reached the end of a sel fragment.
552 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
555 // No match. Advance sel to the start of the next elem.
556 while (*sel != ',' && *sel != '\0') sel++;
557 if (*sel == ',') sel++;
559 /* Run out of sel ?? */
560 if (*sel == '\0') return rtsFalse;
564 #endif /* PROFILING */
566 /* -----------------------------------------------------------------------------
567 * Figure out whether a closure should be counted in this census, by
568 * testing against all the specified constraints.
569 * -------------------------------------------------------------------------- */
571 closureSatisfiesConstraints( StgClosure* p )
573 #if !defined(PROFILING)
574 (void)p; /* keep gcc -Wall happy */
579 // The CCS has a selected field to indicate whether this closure is
580 // deselected by not being mentioned in the module, CC, or CCS
582 if (!p->header.prof.ccs->selected) {
586 if (RtsFlags.ProfFlags.descrSelector) {
587 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
588 RtsFlags.ProfFlags.descrSelector );
589 if (!b) return rtsFalse;
591 if (RtsFlags.ProfFlags.typeSelector) {
592 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
593 RtsFlags.ProfFlags.typeSelector );
594 if (!b) return rtsFalse;
596 if (RtsFlags.ProfFlags.retainerSelector) {
599 // We must check that the retainer set is valid here. One
600 // reason it might not be valid is if this closure is a
601 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
602 // these aren't reached by the retainer profiler's traversal.
603 if (isRetainerSetFieldValid((StgClosure *)p)) {
604 rs = retainerSetOf((StgClosure *)p);
606 for (i = 0; i < rs->num; i++) {
607 b = strMatchesSelector( rs->element[i]->cc->label,
608 RtsFlags.ProfFlags.retainerSelector );
609 if (b) return rtsTrue;
616 #endif /* PROFILING */
619 /* -----------------------------------------------------------------------------
620 * Aggregate the heap census info for biographical profiling
621 * -------------------------------------------------------------------------- */
624 aggregateCensusInfo( void )
628 counter *c, *d, *ctrs;
631 if (!doingLDVProfiling()) return;
633 // Aggregate the LDV counters when displaying by biography.
634 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
635 int void_total, drag_total;
637 // Now we compute void_total and drag_total for each census
638 // After the program has finished, the void_total field of
639 // each census contains the count of words that were *created*
640 // in this era and were eventually void. Conversely, if a
641 // void closure was destroyed in this era, it will be
642 // represented by a negative count of words in void_total.
644 // To get the count of live words that are void at each
645 // census, just propagate the void_total count forwards:
649 for (t = 1; t < era; t++) { // note: start at 1, not 0
650 void_total += censuses[t].void_total;
651 drag_total += censuses[t].drag_total;
652 censuses[t].void_total = void_total;
653 censuses[t].drag_total = drag_total;
655 ASSERT( censuses[t].void_total <= censuses[t].not_used );
656 // should be true because: void_total is the count of
657 // live words that are void at this census, which *must*
658 // be less than the number of live words that have not
661 ASSERT( censuses[t].drag_total <= censuses[t].used );
662 // similar reasoning as above.
668 // otherwise... we're doing a heap profile that is restricted to
669 // some combination of lag, drag, void or use. We've kept all the
670 // census info for all censuses so far, but we still need to
671 // aggregate the counters forwards.
674 acc = allocHashTable();
677 for (t = 1; t < era; t++) {
679 // first look through all the counters we're aggregating
680 for (c = ctrs; c != NULL; c = c->next) {
681 // if one of the totals is non-zero, then this closure
682 // type must be present in the heap at this census time...
683 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
686 // if this closure identity isn't present in the
687 // census for this time period, then our running
688 // totals *must* be zero.
689 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
691 // debugCCS(c->identity);
692 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
693 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
695 d->c.ldv.void_total += c->c.ldv.void_total;
696 d->c.ldv.drag_total += c->c.ldv.drag_total;
697 c->c.ldv.void_total = d->c.ldv.void_total;
698 c->c.ldv.drag_total = d->c.ldv.drag_total;
700 ASSERT( c->c.ldv.void_total >= 0 );
701 ASSERT( c->c.ldv.drag_total >= 0 );
705 // now look through the counters in this census to find new ones
706 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
707 d = lookupHashTable(acc, (StgWord)c->identity);
709 d = arenaAlloc( arena, sizeof(counter) );
711 insertHashTable( acc, (StgWord)c->identity, d );
712 d->identity = c->identity;
715 d->c.ldv.void_total = c->c.ldv.void_total;
716 d->c.ldv.drag_total = c->c.ldv.drag_total;
718 ASSERT( c->c.ldv.void_total >= 0 );
719 ASSERT( c->c.ldv.drag_total >= 0 );
723 freeHashTable(acc, NULL);
728 /* -----------------------------------------------------------------------------
729 * Print out the results of a heap census.
730 * -------------------------------------------------------------------------- */
732 dumpCensus( Census *census )
737 printSample(rtsTrue, census->time);
740 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
741 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
742 fprintf(hp_file, "LAG\t%lu\n",
743 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
744 fprintf(hp_file, "USE\t%lu\n",
745 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
746 fprintf(hp_file, "INHERENT_USE\t%lu\n",
747 (unsigned long)(census->prim) * sizeof(W_));
748 fprintf(hp_file, "DRAG\t%lu\n",
749 (unsigned long)(census->drag_total) * sizeof(W_));
750 printSample(rtsFalse, census->time);
755 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
758 if (RtsFlags.ProfFlags.bioSelector != NULL) {
760 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
761 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
762 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
763 count += ctr->c.ldv.drag_total;
764 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
765 count += ctr->c.ldv.void_total;
766 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
767 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
771 count = ctr->c.resid;
774 ASSERT( count >= 0 );
776 if (count == 0) continue;
778 #if !defined(PROFILING)
779 switch (RtsFlags.ProfFlags.doHeapProfile) {
780 case HEAP_BY_CLOSURE_TYPE:
781 fprintf(hp_file, "%s", (char *)ctr->identity);
787 switch (RtsFlags.ProfFlags.doHeapProfile) {
789 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
794 fprintf(hp_file, "%s", (char *)ctr->identity);
796 case HEAP_BY_RETAINER:
798 RetainerSet *rs = (RetainerSet *)ctr->identity;
800 // it might be the distinguished retainer set rs_MANY:
801 if (rs == &rs_MANY) {
802 fprintf(hp_file, "MANY");
806 // Mark this retainer set by negating its id, because it
807 // has appeared in at least one census. We print the
808 // values of all such retainer sets into the log file at
809 // the end. A retainer set may exist but not feature in
810 // any censuses if it arose as the intermediate retainer
811 // set for some closure during retainer set calculation.
815 // report in the unit of bytes: * sizeof(StgWord)
816 printRetainerSetShort(hp_file, rs, RtsFlags.ProfFlags.ccsLength);
820 barf("dumpCensus; doHeapProfile");
824 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
827 printSample(rtsFalse, census->time);
830 /* -----------------------------------------------------------------------------
831 * Code to perform a heap census.
832 * -------------------------------------------------------------------------- */
834 heapCensusChain( Census *census, bdescr *bd )
844 for (; bd != NULL; bd = bd->link) {
846 // HACK: ignore pinned blocks, because they contain gaps.
847 // It's not clear exactly what we'd like to do here, since we
848 // can't tell which objects in the block are actually alive.
849 // Perhaps the whole block should be counted as SYSTEM memory.
850 if (bd->flags & BF_PINNED) {
855 while (p < bd->free) {
856 info = get_itbl((StgClosure *)p);
859 switch (info->type) {
862 size = thunk_sizeW_fromITBL(info);
868 size = sizeofW(StgThunkHeader) + 2;
874 size = sizeofW(StgThunkHeader) + 1;
892 size = sizeW_fromITBL(info);
896 // Special case/Delicate Hack: INDs don't normally
897 // appear, since we're doing this heap census right
898 // after GC. However, GarbageCollect() also does
899 // resurrectThreads(), which can update some
900 // blackholes when it calls raiseAsync() on the
901 // resurrected threads. So we know that any IND will
902 // be the size of a BLACKHOLE.
903 size = BLACKHOLE_sizeW();
908 size = bco_sizeW((StgBCO *)p);
919 size = sizeW_fromITBL(info);
923 size = ap_sizeW((StgAP *)p);
927 size = pap_sizeW((StgPAP *)p);
931 size = ap_stack_sizeW((StgAP_STACK *)p);
936 size = arr_words_sizeW((StgArrWords*)p);
939 case MUT_ARR_PTRS_CLEAN:
940 case MUT_ARR_PTRS_DIRTY:
941 case MUT_ARR_PTRS_FROZEN:
942 case MUT_ARR_PTRS_FROZEN0:
944 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
950 if (RtsFlags.ProfFlags.includeTSOs) {
951 size = sizeofW(StgTSO);
954 // Skip this TSO and move on to the next object
955 p += sizeofW(StgTSO);
959 size = sizeofW(StgTSO);
966 if (RtsFlags.ProfFlags.includeTSOs) {
967 size = stack_sizeW((StgStack*)p);
970 // Skip this TSO and move on to the next object
971 p += stack_sizeW((StgStack*)p);
975 size = stack_sizeW((StgStack*)p);
981 size = sizeofW(StgTRecChunk);
985 barf("heapCensus, unknown object: %d", info->type);
991 // subtract the profiling overhead
992 real_size = size - sizeofW(StgProfHeader);
997 if (closureSatisfiesConstraints((StgClosure*)p)) {
999 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
1001 census->prim += real_size;
1002 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1003 census->not_used += real_size;
1005 census->used += real_size;
1009 identity = closureIdentity((StgClosure *)p);
1011 if (identity != NULL) {
1012 ctr = lookupHashTable( census->hash, (StgWord)identity );
1015 if (RtsFlags.ProfFlags.bioSelector != NULL) {
1017 ctr->c.ldv.prim += real_size;
1018 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
1019 ctr->c.ldv.not_used += real_size;
1021 ctr->c.ldv.used += real_size;
1025 ctr->c.resid += real_size;
1028 ctr = arenaAlloc( census->arena, sizeof(counter) );
1030 insertHashTable( census->hash, (StgWord)identity, ctr );
1031 ctr->identity = identity;
1032 ctr->next = census->ctrs;
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;
1065 census = &censuses[era];
1066 census->time = mut_user_time();
1068 // calculate retainer sets if necessary
1070 if (doingRetainerProfiling()) {
1076 stat_startHeapCensus();
1079 // Traverse the heap, collecting the census info
1080 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1081 heapCensusChain( census, generations[g].blocks );
1082 // Are we interested in large objects? might be
1083 // confusing to include the stack in a heap profile.
1084 heapCensusChain( census, generations[g].large_objects );
1086 for (n = 0; n < n_capabilities; n++) {
1087 ws = &gc_threads[n]->gens[g];
1088 heapCensusChain(census, ws->todo_bd);
1089 heapCensusChain(census, ws->part_list);
1090 heapCensusChain(census, ws->scavd_list);
1094 // dump out the census info
1096 // We can't generate any info for LDV profiling until
1097 // the end of the run...
1098 if (!doingLDVProfiling())
1099 dumpCensus( census );
1101 dumpCensus( census );
1105 // free our storage, unless we're keeping all the census info for
1106 // future restriction by biography.
1108 if (RtsFlags.ProfFlags.bioSelector == NULL)
1110 freeHashTable( census->hash, NULL/* don't free the elements */ );
1111 arenaFree( census->arena );
1112 census->hash = NULL;
1113 census->arena = NULL;
1117 // we're into the next time period now
1121 stat_endHeapCensus();