Add purgeObj() to remove the symbol table entries for an object
[ghc.git] / rts / ProfHeap.c
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2003
4 *
5 * Support for heap profiling
6 *
7 * --------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11
12 #include "RtsUtils.h"
13 #include "Profiling.h"
14 #include "ProfHeap.h"
15 #include "Stats.h"
16 #include "Hash.h"
17 #include "RetainerProfile.h"
18 #include "LdvProfile.h"
19 #include "Arena.h"
20 #include "Printer.h"
21 #include "sm/GCThread.h"
22
23 #include <string.h>
24
25 /* -----------------------------------------------------------------------------
26 * era stores the current time period. It is the same as the
27 * number of censuses that have been performed.
28 *
29 * RESTRICTION:
30 * era must be no longer than LDV_SHIFT (15 or 30) bits.
31 * Invariants:
32 * era is initialized to 1 in initHeapProfiling().
33 *
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 * -------------------------------------------------------------------------- */
38 unsigned int era;
39 static nat max_era;
40
41 /* -----------------------------------------------------------------------------
42 * Counters
43 *
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 {
50 void *identity;
51 union {
52 nat resid;
53 struct {
54 long prim; // total size of 'inherently used' closures
55 long not_used; // total size of 'never used' closures
56 long used; // total size of 'used at least once' closures
57 long void_total; // current total size of 'destroyed without being used' closures
58 long drag_total; // current total size of 'used at least once and waiting to die'
59 } ldv;
60 } c;
61 struct _counter *next;
62 } counter;
63
64 STATIC_INLINE void
65 initLDVCtr( counter *ctr )
66 {
67 ctr->c.ldv.prim = 0;
68 ctr->c.ldv.not_used = 0;
69 ctr->c.ldv.used = 0;
70 ctr->c.ldv.void_total = 0;
71 ctr->c.ldv.drag_total = 0;
72 }
73
74 typedef struct {
75 double time; // the time in MUT time when the census is made
76 HashTable * hash;
77 counter * ctrs;
78 Arena * arena;
79
80 // for LDV profiling, when just displaying by LDV
81 long prim;
82 long not_used;
83 long used;
84 long void_total;
85 long drag_total;
86 } Census;
87
88 static Census *censuses = NULL;
89 static nat n_censuses = 0;
90
91 #ifdef PROFILING
92 static void aggregateCensusInfo( void );
93 #endif
94
95 static void dumpCensus( Census *census );
96
97 static rtsBool closureSatisfiesConstraints( StgClosure* p );
98
99 /* ----------------------------------------------------------------------------
100 * Find the "closure identity", which is a unique pointer representing
101 * the band to which this closure's heap space is attributed in the
102 * heap profile.
103 * ------------------------------------------------------------------------- */
104 static void *
105 closureIdentity( StgClosure *p )
106 {
107 switch (RtsFlags.ProfFlags.doHeapProfile) {
108
109 #ifdef PROFILING
110 case HEAP_BY_CCS:
111 return p->header.prof.ccs;
112 case HEAP_BY_MOD:
113 return p->header.prof.ccs->cc->module;
114 case HEAP_BY_DESCR:
115 return GET_PROF_DESC(get_itbl(p));
116 case HEAP_BY_TYPE:
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);
123 else
124 return NULL;
125
126 #else
127 case HEAP_BY_CLOSURE_TYPE:
128 {
129 StgInfoTable *info;
130 info = get_itbl(p);
131 switch (info->type) {
132 case CONSTR:
133 case CONSTR_1_0:
134 case CONSTR_0_1:
135 case CONSTR_2_0:
136 case CONSTR_1_1:
137 case CONSTR_0_2:
138 case CONSTR_STATIC:
139 case CONSTR_NOCAF_STATIC:
140 return GET_CON_DESC(itbl_to_con_itbl(info));
141 default:
142 return closure_type_names[info->type];
143 }
144 }
145
146 #endif
147 default:
148 barf("closureIdentity");
149 }
150 }
151
152 /* --------------------------------------------------------------------------
153 * Profiling type predicates
154 * ----------------------------------------------------------------------- */
155 #ifdef PROFILING
156 STATIC_INLINE rtsBool
157 doingLDVProfiling( void )
158 {
159 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
160 || RtsFlags.ProfFlags.bioSelector != NULL);
161 }
162
163 STATIC_INLINE rtsBool
164 doingRetainerProfiling( void )
165 {
166 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
167 || RtsFlags.ProfFlags.retainerSelector != NULL);
168 }
169 #endif /* PROFILING */
170
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.
174 //
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.
179
180 #ifdef PROFILING
181 void
182 LDV_recordDead( StgClosure *c, nat size )
183 {
184 void *id;
185 nat t;
186 counter *ctr;
187
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;
193 if (t < era) {
194 if (RtsFlags.ProfFlags.bioSelector == NULL) {
195 censuses[t].void_total += (long)size;
196 censuses[era].void_total -= (long)size;
197 ASSERT(censuses[t].void_total < censuses[t].not_used);
198 } else {
199 id = closureIdentity(c);
200 ctr = lookupHashTable(censuses[t].hash, (StgWord)id);
201 ASSERT( ctr != NULL );
202 ctr->c.ldv.void_total += (long)size;
203 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
204 if (ctr == NULL) {
205 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
206 initLDVCtr(ctr);
207 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
208 ctr->identity = id;
209 ctr->next = censuses[era].ctrs;
210 censuses[era].ctrs = ctr;
211 }
212 ctr->c.ldv.void_total -= (long)size;
213 }
214 }
215 } else {
216 t = LDVW((c)) & LDV_LAST_MASK;
217 if (t + 1 < era) {
218 if (RtsFlags.ProfFlags.bioSelector == NULL) {
219 censuses[t+1].drag_total += size;
220 censuses[era].drag_total -= size;
221 } else {
222 void *id;
223 id = closureIdentity(c);
224 ctr = lookupHashTable(censuses[t+1].hash, (StgWord)id);
225 ASSERT( ctr != NULL );
226 ctr->c.ldv.drag_total += (long)size;
227 ctr = lookupHashTable(censuses[era].hash, (StgWord)id);
228 if (ctr == NULL) {
229 ctr = arenaAlloc(censuses[era].arena, sizeof(counter));
230 initLDVCtr(ctr);
231 insertHashTable(censuses[era].hash, (StgWord)id, ctr);
232 ctr->identity = id;
233 ctr->next = censuses[era].ctrs;
234 censuses[era].ctrs = ctr;
235 }
236 ctr->c.ldv.drag_total -= (long)size;
237 }
238 }
239 }
240 }
241 }
242 #endif
243
244 /* --------------------------------------------------------------------------
245 * Initialize censuses[era];
246 * ----------------------------------------------------------------------- */
247
248 STATIC_INLINE void
249 initEra(Census *census)
250 {
251 census->hash = allocHashTable();
252 census->ctrs = NULL;
253 census->arena = newArena();
254
255 census->not_used = 0;
256 census->used = 0;
257 census->prim = 0;
258 census->void_total = 0;
259 census->drag_total = 0;
260 }
261
262 STATIC_INLINE void
263 freeEra(Census *census)
264 {
265 arenaFree(census->arena);
266 freeHashTable(census->hash, NULL);
267 }
268
269 /* --------------------------------------------------------------------------
270 * Increases era by 1 and initialize census[era].
271 * Reallocates gi[] and increases its size if needed.
272 * ----------------------------------------------------------------------- */
273
274 static void
275 nextEra( void )
276 {
277 #ifdef PROFILING
278 if (doingLDVProfiling()) {
279 era++;
280
281 if (era == max_era) {
282 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
283 stg_exit(EXIT_FAILURE);
284 }
285
286 if (era == n_censuses) {
287 n_censuses *= 2;
288 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
289 "nextEra");
290 }
291 }
292 #endif /* PROFILING */
293
294 initEra( &censuses[era] );
295 }
296
297 /* ----------------------------------------------------------------------------
298 * Heap profiling by info table
299 * ------------------------------------------------------------------------- */
300
301 #if !defined(PROFILING)
302 FILE *hp_file;
303 static char *hp_filename;
304
305 void initProfiling1 (void)
306 {
307 }
308
309 void freeProfiling (void)
310 {
311 }
312
313 void initProfiling2 (void)
314 {
315 char *prog;
316
317 prog = stgMallocBytes(strlen(prog_name) + 1, "initProfiling2");
318 strcpy(prog, prog_name);
319 #ifdef mingw32_HOST_OS
320 // on Windows, drop the .exe suffix if there is one
321 {
322 char *suff;
323 suff = strrchr(prog,'.');
324 if (suff != NULL && !strcmp(suff,".exe")) {
325 *suff = '\0';
326 }
327 }
328 #endif
329
330 if (RtsFlags.ProfFlags.doHeapProfile) {
331 /* Initialise the log file name */
332 hp_filename = stgMallocBytes(strlen(prog) + 6, "hpFileName");
333 sprintf(hp_filename, "%s.hp", prog);
334
335 /* open the log file */
336 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
337 debugBelch("Can't open profiling report file %s\n",
338 hp_filename);
339 RtsFlags.ProfFlags.doHeapProfile = 0;
340 stgFree(prog);
341 return;
342 }
343 }
344
345 stgFree(prog);
346
347 initHeapProfiling();
348 }
349
350 void endProfiling( void )
351 {
352 endHeapProfiling();
353 }
354 #endif /* !PROFILING */
355
356 static void
357 printSample(rtsBool beginSample, StgDouble sampleValue)
358 {
359 StgDouble fractionalPart, integralPart;
360 fractionalPart = modf(sampleValue, &integralPart);
361 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
362 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
363 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
364 if (!beginSample) {
365 fflush(hp_file);
366 }
367 }
368
369 /* --------------------------------------------------------------------------
370 * Initialize the heap profilier
371 * ----------------------------------------------------------------------- */
372 nat
373 initHeapProfiling(void)
374 {
375 if (! RtsFlags.ProfFlags.doHeapProfile) {
376 return 0;
377 }
378
379 #ifdef PROFILING
380 if (doingLDVProfiling() && doingRetainerProfiling()) {
381 errorBelch("cannot mix -hb and -hr");
382 stg_exit(EXIT_FAILURE);
383 }
384 #endif
385
386 // we only count eras if we're doing LDV profiling. Otherwise era
387 // is fixed at zero.
388 #ifdef PROFILING
389 if (doingLDVProfiling()) {
390 era = 1;
391 } else
392 #endif
393 {
394 era = 0;
395 }
396
397 // max_era = 2^LDV_SHIFT
398 max_era = 1 << LDV_SHIFT;
399
400 n_censuses = 32;
401 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
402
403 initEra( &censuses[era] );
404
405 /* initProfilingLogFile(); */
406 fprintf(hp_file, "JOB \"%s", prog_name);
407
408 #ifdef PROFILING
409 {
410 int count;
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]);
416 }
417 #endif /* PROFILING */
418
419 fprintf(hp_file, "\"\n" );
420
421 fprintf(hp_file, "DATE \"%s\"\n", time_str());
422
423 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
424 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
425
426 printSample(rtsTrue, 0);
427 printSample(rtsFalse, 0);
428
429 #ifdef PROFILING
430 if (doingRetainerProfiling()) {
431 initRetainerProfiling();
432 }
433 #endif
434
435 return 0;
436 }
437
438 void
439 endHeapProfiling(void)
440 {
441 StgDouble seconds;
442
443 if (! RtsFlags.ProfFlags.doHeapProfile) {
444 return;
445 }
446
447 #ifdef PROFILING
448 if (doingRetainerProfiling()) {
449 endRetainerProfiling();
450 }
451 #endif
452
453 #ifdef PROFILING
454 if (doingLDVProfiling()) {
455 nat t;
456 LdvCensusKillAll();
457 aggregateCensusInfo();
458 for (t = 1; t < era; t++) {
459 dumpCensus( &censuses[t] );
460 }
461 }
462 #endif
463
464 #ifdef PROFILING
465 if (doingLDVProfiling()) {
466 nat t;
467 if (RtsFlags.ProfFlags.bioSelector != NULL) {
468 for (t = 1; t <= era; t++) {
469 freeEra( &censuses[t] );
470 }
471 } else {
472 freeEra( &censuses[era] );
473 }
474 } else {
475 freeEra( &censuses[0] );
476 }
477 #else
478 freeEra( &censuses[0] );
479 #endif
480
481 stgFree(censuses);
482
483 seconds = mut_user_time();
484 printSample(rtsTrue, seconds);
485 printSample(rtsFalse, seconds);
486 fclose(hp_file);
487 }
488
489
490
491 #ifdef PROFILING
492 static size_t
493 buf_append(char *p, const char *q, char *end)
494 {
495 int m;
496
497 for (m = 0; p < end; p++, q++, m++) {
498 *p = *q;
499 if (*q == '\0') { break; }
500 }
501 return m;
502 }
503
504 static void
505 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
506 {
507 char buf[max_length+1], *p, *buf_end;
508
509 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
510 if (ccs == CCS_MAIN) {
511 fprintf(fp, "MAIN");
512 return;
513 }
514
515 fprintf(fp, "(%ld)", ccs->ccsID);
516
517 p = buf;
518 buf_end = buf + max_length + 1;
519
520 // keep printing components of the stack until we run out of space
521 // in the buffer. If we run out of space, end with "...".
522 for (; ccs != NULL && ccs != CCS_MAIN; ccs = ccs->prevStack) {
523
524 // CAF cost centres print as M.CAF, but we leave the module
525 // name out of all the others to save space.
526 if (!strcmp(ccs->cc->label,"CAF")) {
527 p += buf_append(p, ccs->cc->module, buf_end);
528 p += buf_append(p, ".CAF", buf_end);
529 } else {
530 p += buf_append(p, ccs->cc->label, buf_end);
531 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
532 p += buf_append(p, "/", buf_end);
533 }
534 }
535
536 if (p >= buf_end) {
537 sprintf(buf+max_length-4, "...");
538 break;
539 }
540 }
541 fprintf(fp, "%s", buf);
542 }
543
544 rtsBool
545 strMatchesSelector( char* str, char* sel )
546 {
547 char* p;
548 // debugBelch("str_matches_selector %s %s\n", str, sel);
549 while (1) {
550 // Compare str against wherever we've got to in sel.
551 p = str;
552 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
553 p++; sel++;
554 }
555 // Match if all of str used and have reached the end of a sel fragment.
556 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
557 return rtsTrue;
558
559 // No match. Advance sel to the start of the next elem.
560 while (*sel != ',' && *sel != '\0') sel++;
561 if (*sel == ',') sel++;
562
563 /* Run out of sel ?? */
564 if (*sel == '\0') return rtsFalse;
565 }
566 }
567
568 #endif /* PROFILING */
569
570 /* -----------------------------------------------------------------------------
571 * Figure out whether a closure should be counted in this census, by
572 * testing against all the specified constraints.
573 * -------------------------------------------------------------------------- */
574 static rtsBool
575 closureSatisfiesConstraints( StgClosure* p )
576 {
577 #if !defined(PROFILING)
578 (void)p; /* keep gcc -Wall happy */
579 return rtsTrue;
580 #else
581 rtsBool b;
582
583 // The CCS has a selected field to indicate whether this closure is
584 // deselected by not being mentioned in the module, CC, or CCS
585 // selectors.
586 if (!p->header.prof.ccs->selected) {
587 return rtsFalse;
588 }
589
590 if (RtsFlags.ProfFlags.descrSelector) {
591 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
592 RtsFlags.ProfFlags.descrSelector );
593 if (!b) return rtsFalse;
594 }
595 if (RtsFlags.ProfFlags.typeSelector) {
596 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
597 RtsFlags.ProfFlags.typeSelector );
598 if (!b) return rtsFalse;
599 }
600 if (RtsFlags.ProfFlags.retainerSelector) {
601 RetainerSet *rs;
602 nat i;
603 // We must check that the retainer set is valid here. One
604 // reason it might not be valid is if this closure is a
605 // a newly deceased weak pointer (i.e. a DEAD_WEAK), since
606 // these aren't reached by the retainer profiler's traversal.
607 if (isRetainerSetFieldValid((StgClosure *)p)) {
608 rs = retainerSetOf((StgClosure *)p);
609 if (rs != NULL) {
610 for (i = 0; i < rs->num; i++) {
611 b = strMatchesSelector( rs->element[i]->cc->label,
612 RtsFlags.ProfFlags.retainerSelector );
613 if (b) return rtsTrue;
614 }
615 }
616 }
617 return rtsFalse;
618 }
619 return rtsTrue;
620 #endif /* PROFILING */
621 }
622
623 /* -----------------------------------------------------------------------------
624 * Aggregate the heap census info for biographical profiling
625 * -------------------------------------------------------------------------- */
626 #ifdef PROFILING
627 static void
628 aggregateCensusInfo( void )
629 {
630 HashTable *acc;
631 nat t;
632 counter *c, *d, *ctrs;
633 Arena *arena;
634
635 if (!doingLDVProfiling()) return;
636
637 // Aggregate the LDV counters when displaying by biography.
638 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
639 long void_total, drag_total;
640
641 // Now we compute void_total and drag_total for each census
642 // After the program has finished, the void_total field of
643 // each census contains the count of words that were *created*
644 // in this era and were eventually void. Conversely, if a
645 // void closure was destroyed in this era, it will be
646 // represented by a negative count of words in void_total.
647 //
648 // To get the count of live words that are void at each
649 // census, just propagate the void_total count forwards:
650
651 void_total = 0;
652 drag_total = 0;
653 for (t = 1; t < era; t++) { // note: start at 1, not 0
654 void_total += censuses[t].void_total;
655 drag_total += censuses[t].drag_total;
656 censuses[t].void_total = void_total;
657 censuses[t].drag_total = drag_total;
658
659 ASSERT( censuses[t].void_total <= censuses[t].not_used );
660 // should be true because: void_total is the count of
661 // live words that are void at this census, which *must*
662 // be less than the number of live words that have not
663 // been used yet.
664
665 ASSERT( censuses[t].drag_total <= censuses[t].used );
666 // similar reasoning as above.
667 }
668
669 return;
670 }
671
672 // otherwise... we're doing a heap profile that is restricted to
673 // some combination of lag, drag, void or use. We've kept all the
674 // census info for all censuses so far, but we still need to
675 // aggregate the counters forwards.
676
677 arena = newArena();
678 acc = allocHashTable();
679 ctrs = NULL;
680
681 for (t = 1; t < era; t++) {
682
683 // first look through all the counters we're aggregating
684 for (c = ctrs; c != NULL; c = c->next) {
685 // if one of the totals is non-zero, then this closure
686 // type must be present in the heap at this census time...
687 d = lookupHashTable(censuses[t].hash, (StgWord)c->identity);
688
689 if (d == NULL) {
690 // if this closure identity isn't present in the
691 // census for this time period, then our running
692 // totals *must* be zero.
693 ASSERT(c->c.ldv.void_total == 0 && c->c.ldv.drag_total == 0);
694
695 // debugCCS(c->identity);
696 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
697 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
698 } else {
699 d->c.ldv.void_total += c->c.ldv.void_total;
700 d->c.ldv.drag_total += c->c.ldv.drag_total;
701 c->c.ldv.void_total = d->c.ldv.void_total;
702 c->c.ldv.drag_total = d->c.ldv.drag_total;
703
704 ASSERT( c->c.ldv.void_total >= 0 );
705 ASSERT( c->c.ldv.drag_total >= 0 );
706 }
707 }
708
709 // now look through the counters in this census to find new ones
710 for (c = censuses[t].ctrs; c != NULL; c = c->next) {
711 d = lookupHashTable(acc, (StgWord)c->identity);
712 if (d == NULL) {
713 d = arenaAlloc( arena, sizeof(counter) );
714 initLDVCtr(d);
715 insertHashTable( acc, (StgWord)c->identity, d );
716 d->identity = c->identity;
717 d->next = ctrs;
718 ctrs = d;
719 d->c.ldv.void_total = c->c.ldv.void_total;
720 d->c.ldv.drag_total = c->c.ldv.drag_total;
721 }
722 ASSERT( c->c.ldv.void_total >= 0 );
723 ASSERT( c->c.ldv.drag_total >= 0 );
724 }
725 }
726
727 freeHashTable(acc, NULL);
728 arenaFree(arena);
729 }
730 #endif
731
732 /* -----------------------------------------------------------------------------
733 * Print out the results of a heap census.
734 * -------------------------------------------------------------------------- */
735 static void
736 dumpCensus( Census *census )
737 {
738 counter *ctr;
739 long count;
740
741 printSample(rtsTrue, census->time);
742
743 #ifdef PROFILING
744 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
745 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
746 fprintf(hp_file, "LAG\t%lu\n",
747 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
748 fprintf(hp_file, "USE\t%lu\n",
749 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
750 fprintf(hp_file, "INHERENT_USE\t%lu\n",
751 (unsigned long)(census->prim) * sizeof(W_));
752 fprintf(hp_file, "DRAG\t%lu\n",
753 (unsigned long)(census->drag_total) * sizeof(W_));
754 printSample(rtsFalse, census->time);
755 return;
756 }
757 #endif
758
759 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
760
761 #ifdef PROFILING
762 if (RtsFlags.ProfFlags.bioSelector != NULL) {
763 count = 0;
764 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
765 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
766 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
767 count += ctr->c.ldv.drag_total;
768 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
769 count += ctr->c.ldv.void_total;
770 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
771 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
772 } else
773 #endif
774 {
775 count = ctr->c.resid;
776 }
777
778 ASSERT( count >= 0 );
779
780 if (count == 0) continue;
781
782 #if !defined(PROFILING)
783 switch (RtsFlags.ProfFlags.doHeapProfile) {
784 case HEAP_BY_CLOSURE_TYPE:
785 fprintf(hp_file, "%s", (char *)ctr->identity);
786 break;
787 }
788 #endif
789
790 #ifdef PROFILING
791 switch (RtsFlags.ProfFlags.doHeapProfile) {
792 case HEAP_BY_CCS:
793 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
794 break;
795 case HEAP_BY_MOD:
796 case HEAP_BY_DESCR:
797 case HEAP_BY_TYPE:
798 fprintf(hp_file, "%s", (char *)ctr->identity);
799 break;
800 case HEAP_BY_RETAINER:
801 {
802 RetainerSet *rs = (RetainerSet *)ctr->identity;
803
804 // it might be the distinguished retainer set rs_MANY:
805 if (rs == &rs_MANY) {
806 fprintf(hp_file, "MANY");
807 break;
808 }
809
810 // Mark this retainer set by negating its id, because it
811 // has appeared in at least one census. We print the
812 // values of all such retainer sets into the log file at
813 // the end. A retainer set may exist but not feature in
814 // any censuses if it arose as the intermediate retainer
815 // set for some closure during retainer set calculation.
816 if (rs->id > 0)
817 rs->id = -(rs->id);
818
819 // report in the unit of bytes: * sizeof(StgWord)
820 printRetainerSetShort(hp_file, rs, RtsFlags.ProfFlags.ccsLength);
821 break;
822 }
823 default:
824 barf("dumpCensus; doHeapProfile");
825 }
826 #endif
827
828 fprintf(hp_file, "\t%" FMT_SizeT "\n", (W_)count * sizeof(W_));
829 }
830
831 printSample(rtsFalse, census->time);
832 }
833
834
835 static void heapProfObject(Census *census, StgClosure *p, nat size,
836 rtsBool prim
837 #ifndef PROFILING
838 STG_UNUSED
839 #endif
840 )
841 {
842 void *identity;
843 nat real_size;
844 counter *ctr;
845
846 identity = NULL;
847
848 #ifdef PROFILING
849 // subtract the profiling overhead
850 real_size = size - sizeofW(StgProfHeader);
851 #else
852 real_size = size;
853 #endif
854
855 if (closureSatisfiesConstraints((StgClosure*)p)) {
856 #ifdef PROFILING
857 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
858 if (prim)
859 census->prim += real_size;
860 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
861 census->not_used += real_size;
862 else
863 census->used += real_size;
864 } else
865 #endif
866 {
867 identity = closureIdentity((StgClosure *)p);
868
869 if (identity != NULL) {
870 ctr = lookupHashTable( census->hash, (StgWord)identity );
871 if (ctr != NULL) {
872 #ifdef PROFILING
873 if (RtsFlags.ProfFlags.bioSelector != NULL) {
874 if (prim)
875 ctr->c.ldv.prim += real_size;
876 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
877 ctr->c.ldv.not_used += real_size;
878 else
879 ctr->c.ldv.used += real_size;
880 } else
881 #endif
882 {
883 ctr->c.resid += real_size;
884 }
885 } else {
886 ctr = arenaAlloc( census->arena, sizeof(counter) );
887 initLDVCtr(ctr);
888 insertHashTable( census->hash, (StgWord)identity, ctr );
889 ctr->identity = identity;
890 ctr->next = census->ctrs;
891 census->ctrs = ctr;
892
893 #ifdef PROFILING
894 if (RtsFlags.ProfFlags.bioSelector != NULL) {
895 if (prim)
896 ctr->c.ldv.prim = real_size;
897 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
898 ctr->c.ldv.not_used = real_size;
899 else
900 ctr->c.ldv.used = real_size;
901 } else
902 #endif
903 {
904 ctr->c.resid = real_size;
905 }
906 }
907 }
908 }
909 }
910 }
911
912 /* -----------------------------------------------------------------------------
913 * Code to perform a heap census.
914 * -------------------------------------------------------------------------- */
915 static void
916 heapCensusChain( Census *census, bdescr *bd )
917 {
918 StgPtr p;
919 StgInfoTable *info;
920 nat size;
921 rtsBool prim;
922
923 for (; bd != NULL; bd = bd->link) {
924
925 // HACK: pretend a pinned block is just one big ARR_WORDS
926 // owned by CCS_PINNED. These blocks can be full of holes due
927 // to alignment constraints so we can't traverse the memory
928 // and do a proper census.
929 if (bd->flags & BF_PINNED) {
930 StgClosure arr;
931 SET_HDR(&arr, &stg_ARR_WORDS_info, CCS_PINNED);
932 heapProfObject(census, &arr, bd->blocks * BLOCK_SIZE_W, rtsTrue);
933 continue;
934 }
935
936 p = bd->start;
937 while (p < bd->free) {
938 info = get_itbl((StgClosure *)p);
939 prim = rtsFalse;
940
941 switch (info->type) {
942
943 case THUNK:
944 size = thunk_sizeW_fromITBL(info);
945 break;
946
947 case THUNK_1_1:
948 case THUNK_0_2:
949 case THUNK_2_0:
950 size = sizeofW(StgThunkHeader) + 2;
951 break;
952
953 case THUNK_1_0:
954 case THUNK_0_1:
955 case THUNK_SELECTOR:
956 size = sizeofW(StgThunkHeader) + 1;
957 break;
958
959 case CONSTR:
960 case FUN:
961 case IND_PERM:
962 case BLACKHOLE:
963 case BLOCKING_QUEUE:
964 case FUN_1_0:
965 case FUN_0_1:
966 case FUN_1_1:
967 case FUN_0_2:
968 case FUN_2_0:
969 case CONSTR_1_0:
970 case CONSTR_0_1:
971 case CONSTR_1_1:
972 case CONSTR_0_2:
973 case CONSTR_2_0:
974 size = sizeW_fromITBL(info);
975 break;
976
977 case IND:
978 // Special case/Delicate Hack: INDs don't normally
979 // appear, since we're doing this heap census right
980 // after GC. However, GarbageCollect() also does
981 // resurrectThreads(), which can update some
982 // blackholes when it calls raiseAsync() on the
983 // resurrected threads. So we know that any IND will
984 // be the size of a BLACKHOLE.
985 size = BLACKHOLE_sizeW();
986 break;
987
988 case BCO:
989 prim = rtsTrue;
990 size = bco_sizeW((StgBCO *)p);
991 break;
992
993 case MVAR_CLEAN:
994 case MVAR_DIRTY:
995 case TVAR:
996 case WEAK:
997 case PRIM:
998 case MUT_PRIM:
999 case MUT_VAR_CLEAN:
1000 case MUT_VAR_DIRTY:
1001 prim = rtsTrue;
1002 size = sizeW_fromITBL(info);
1003 break;
1004
1005 case AP:
1006 size = ap_sizeW((StgAP *)p);
1007 break;
1008
1009 case PAP:
1010 size = pap_sizeW((StgPAP *)p);
1011 break;
1012
1013 case AP_STACK:
1014 size = ap_stack_sizeW((StgAP_STACK *)p);
1015 break;
1016
1017 case ARR_WORDS:
1018 prim = rtsTrue;
1019 size = arr_words_sizeW((StgArrWords*)p);
1020 break;
1021
1022 case MUT_ARR_PTRS_CLEAN:
1023 case MUT_ARR_PTRS_DIRTY:
1024 case MUT_ARR_PTRS_FROZEN:
1025 case MUT_ARR_PTRS_FROZEN0:
1026 prim = rtsTrue;
1027 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1028 break;
1029
1030 case SMALL_MUT_ARR_PTRS_CLEAN:
1031 case SMALL_MUT_ARR_PTRS_DIRTY:
1032 case SMALL_MUT_ARR_PTRS_FROZEN:
1033 case SMALL_MUT_ARR_PTRS_FROZEN0:
1034 prim = rtsTrue;
1035 size = small_mut_arr_ptrs_sizeW((StgSmallMutArrPtrs *)p);
1036 break;
1037
1038 case TSO:
1039 prim = rtsTrue;
1040 #ifdef PROFILING
1041 if (RtsFlags.ProfFlags.includeTSOs) {
1042 size = sizeofW(StgTSO);
1043 break;
1044 } else {
1045 // Skip this TSO and move on to the next object
1046 p += sizeofW(StgTSO);
1047 continue;
1048 }
1049 #else
1050 size = sizeofW(StgTSO);
1051 break;
1052 #endif
1053
1054 case STACK:
1055 prim = rtsTrue;
1056 #ifdef PROFILING
1057 if (RtsFlags.ProfFlags.includeTSOs) {
1058 size = stack_sizeW((StgStack*)p);
1059 break;
1060 } else {
1061 // Skip this TSO and move on to the next object
1062 p += stack_sizeW((StgStack*)p);
1063 continue;
1064 }
1065 #else
1066 size = stack_sizeW((StgStack*)p);
1067 break;
1068 #endif
1069
1070 case TREC_CHUNK:
1071 prim = rtsTrue;
1072 size = sizeofW(StgTRecChunk);
1073 break;
1074
1075 default:
1076 barf("heapCensus, unknown object: %d", info->type);
1077 }
1078
1079 heapProfObject(census,(StgClosure*)p,size,prim);
1080
1081 p += size;
1082 }
1083 }
1084 }
1085
1086 void heapCensus (Time t)
1087 {
1088 nat g, n;
1089 Census *census;
1090 gen_workspace *ws;
1091
1092 census = &censuses[era];
1093 census->time = mut_user_time_until(t);
1094
1095 // calculate retainer sets if necessary
1096 #ifdef PROFILING
1097 if (doingRetainerProfiling()) {
1098 retainerProfile();
1099 }
1100 #endif
1101
1102 #ifdef PROFILING
1103 stat_startHeapCensus();
1104 #endif
1105
1106 // Traverse the heap, collecting the census info
1107 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1108 heapCensusChain( census, generations[g].blocks );
1109 // Are we interested in large objects? might be
1110 // confusing to include the stack in a heap profile.
1111 heapCensusChain( census, generations[g].large_objects );
1112
1113 for (n = 0; n < n_capabilities; n++) {
1114 ws = &gc_threads[n]->gens[g];
1115 heapCensusChain(census, ws->todo_bd);
1116 heapCensusChain(census, ws->part_list);
1117 heapCensusChain(census, ws->scavd_list);
1118 }
1119 }
1120
1121 // dump out the census info
1122 #ifdef PROFILING
1123 // We can't generate any info for LDV profiling until
1124 // the end of the run...
1125 if (!doingLDVProfiling())
1126 dumpCensus( census );
1127 #else
1128 dumpCensus( census );
1129 #endif
1130
1131
1132 // free our storage, unless we're keeping all the census info for
1133 // future restriction by biography.
1134 #ifdef PROFILING
1135 if (RtsFlags.ProfFlags.bioSelector == NULL)
1136 {
1137 freeEra(census);
1138 census->hash = NULL;
1139 census->arena = NULL;
1140 }
1141 #endif
1142
1143 // we're into the next time period now
1144 nextEra();
1145
1146 #ifdef PROFILING
1147 stat_endHeapCensus();
1148 #endif
1149 }