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