Skip to content
Newer
Older
100644 1140 lines (985 sloc) 29.6 KB
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
1 /* ----------------------------------------------------------------------------
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
2 *
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
3 * (c) The GHC Team, 1998-2003
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
4 *
5 * Support for heap profiling
6 *
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
7 * --------------------------------------------------------------------------*/
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
8
bc5c802 [project @ 2001-08-14 13:40:07 by sewardj]
sewardj authored
9 #include "PosixSource.h"
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
10 #include "Rts.h"
a2a67cd @simonmar RTS tidyup sweep, first phase
simonmar authored
11
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
12 #include "RtsUtils.h"
b9d8ddb [project @ 2000-04-03 15:54:49 by simonmar]
simonmar authored
13 #include "Profiling.h"
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
14 #include "ProfHeap.h"
15 #include "Stats.h"
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
16 #include "Hash.h"
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
17 #include "RetainerProfile.h"
45da48d @simonmar profiling build fix
simonmar authored
18 #include "LdvProfile.h"
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
19 #include "Arena.h"
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
20 #include "Printer.h"
18197a0 @simonmar heapCensus: we need to traverse the heap fragments stored in the
simonmar authored
21 #include "sm/GCThread.h"
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
22
c41b6b7 [project @ 2002-07-18 09:12:34 by simonmar]
simonmar authored
23 #include <string.h>
24
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
25 /* -----------------------------------------------------------------------------
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
26 * era stores the current time period. It is the same as the
27 * number of censuses that have been performed.
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
28 *
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
29 * RESTRICTION:
30 * era must be no longer than LDV_SHIFT (15 or 30) bits.
31 * Invariants:
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
32 * era is initialized to 1 in initHeapProfiling().
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
33 *
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
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.
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
37 * -------------------------------------------------------------------------- */
3160a65 [project @ 2004-11-10 02:13:12 by wolfgang]
wolfgang authored
38 unsigned int era;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
39 static nat max_era;
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
40
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
41 /* -----------------------------------------------------------------------------
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
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 * -------------------------------------------------------------------------- */
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
49 typedef struct _counter {
50 void *identity;
51 union {
52 nat resid;
53 struct {
54 int prim; // total size of 'inherently used' closures
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
55 int not_used; // total size of 'never used' closures
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
56 int used; // total size of 'used at least once' closures
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
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'
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
59 } ldv;
60 } c;
61 struct _counter *next;
62 } counter;
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
63
20593d1 [project @ 2003-11-12 17:49:05 by sof]
sof authored
64 STATIC_INLINE void
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
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
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
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 int prim;
82 int not_used;
83 int used;
84 int void_total;
85 int drag_total;
86 } Census;
87
95371b0 [project @ 2002-08-16 13:29:05 by simonmar]
simonmar authored
88 static Census *censuses = NULL;
89 static nat n_censuses = 0;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
90
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
91 #ifdef PROFILING
92 static void aggregateCensusInfo( void );
93 #endif
94
95 static void dumpCensus( Census *census );
96
a2a67cd @simonmar RTS tidyup sweep, first phase
simonmar authored
97 static rtsBool closureSatisfiesConstraints( StgClosure* p );
98
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
99 /* ----------------------------------------------------------------------------
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
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
102 * heap profile.
103 * ------------------------------------------------------------------------- */
24ad9cf @simonmar Fix up inlines for gcc 4.3
simonmar authored
104 static void *
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
105 closureIdentity( StgClosure *p )
106 {
107 switch (RtsFlags.ProfFlags.doHeapProfile) {
108
109 #ifdef PROFILING
110 case HEAP_BY_CCS:
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
111 return p->header.prof.ccs;
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
112 case HEAP_BY_MOD:
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
113 return p->header.prof.ccs->cc->module;
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
114 case HEAP_BY_DESCR:
23e5985 FIX #1418 (partially)
Simon Marlow authored
115 return GET_PROF_DESC(get_itbl(p));
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
116 case HEAP_BY_TYPE:
23e5985 FIX #1418 (partially)
Simon Marlow authored
117 return GET_PROF_TYPE(get_itbl(p));
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
118 case HEAP_BY_RETAINER:
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
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
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
126 #else
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
127 case HEAP_BY_CLOSURE_TYPE:
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
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:
9a00308 @simonmar Common up two closure type -> string tables
simonmar authored
142 return closure_type_names[info->type];
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
143 }
144 }
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
145
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
146 #endif
147 default:
148 barf("closureIdentity");
149 }
150 }
151
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
152 /* --------------------------------------------------------------------------
153 * Profiling type predicates
154 * ----------------------------------------------------------------------- */
155 #ifdef PROFILING
20593d1 [project @ 2003-11-12 17:49:05 by sof]
sof authored
156 STATIC_INLINE rtsBool
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
157 doingLDVProfiling( void )
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
158 {
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
159 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
160 || RtsFlags.ProfFlags.bioSelector != NULL);
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
161 }
162
20593d1 [project @ 2003-11-12 17:49:05 by sof]
sof authored
163 STATIC_INLINE rtsBool
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
164 doingRetainerProfiling( void )
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
165 {
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
166 return (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER
167 || RtsFlags.ProfFlags.retainerSelector != NULL);
168 }
03dc2dd [project @ 2005-03-27 13:41:13 by panne]
panne authored
169 #endif /* PROFILING */
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
170
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
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.
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
179
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
180 #ifdef PROFILING
181 void
182 LDV_recordDead( StgClosure *c, nat size )
183 {
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
184 void *id;
185 nat t;
186 counter *ctr;
187
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
188 if (era > 0 && closureSatisfiesConstraints(c)) {
189 size -= sizeofW(StgProfHeader);
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
190 ASSERT(LDVW(c) != 0);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
191 if ((LDVW((c)) & LDV_STATE_MASK) == LDV_STATE_CREATE) {
192 t = (LDVW((c)) & LDV_CREATE_MASK) >> LDV_SHIFT;
193 if (t < era) {
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
194 if (RtsFlags.ProfFlags.bioSelector == NULL) {
195 censuses[t].void_total += (int)size;
196 censuses[era].void_total -= (int)size;
60905d4 lag/drag/void: add an extra assertion, and some commentary
Simon Marlow authored
197 ASSERT(censuses[t].void_total < censuses[t].not_used);
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
198 } else {
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);
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 -= (int)size;
213 }
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
214 }
215 } else {
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
216 t = LDVW((c)) & LDV_LAST_MASK;
217 if (t + 1 < era) {
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
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 += (int)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 -= (int)size;
237 }
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
238 }
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
239 }
240 }
241 }
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
242 #endif
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
243
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
244 /* --------------------------------------------------------------------------
245 * Initialize censuses[era];
246 * ----------------------------------------------------------------------- */
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
247
20593d1 [project @ 2003-11-12 17:49:05 by sof]
sof authored
248 STATIC_INLINE void
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
249 initEra(Census *census)
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
250 {
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
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;
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
260 }
261
ed12b70 @igfoo Free more things that we allocate
igfoo authored
262 STATIC_INLINE void
263 freeEra(Census *census)
264 {
61af839 another fix for -hb: we appear to be freeing the hash table and arena…
Simon Marlow authored
265 if (RtsFlags.ProfFlags.bioSelector != NULL)
266 // when bioSelector==NULL, these are freed in heapCensus()
267 {
268 arenaFree(census->arena);
269 freeHashTable(census->hash, NULL);
270 }
ed12b70 @igfoo Free more things that we allocate
igfoo authored
271 }
272
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
273 /* --------------------------------------------------------------------------
274 * Increases era by 1 and initialize census[era].
275 * Reallocates gi[] and increases its size if needed.
276 * ----------------------------------------------------------------------- */
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
277
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
278 static void
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
279 nextEra( void )
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
280 {
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
281 #ifdef PROFILING
282 if (doingLDVProfiling()) {
283 era++;
284
285 if (era == max_era) {
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
286 errorBelch("maximum number of censuses reached; use +RTS -i to reduce");
0bffc41 [project @ 2002-12-11 15:36:20 by simonmar]
simonmar authored
287 stg_exit(EXIT_FAILURE);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
288 }
289
290 if (era == n_censuses) {
291 n_censuses *= 2;
292 censuses = stgReallocBytes(censuses, sizeof(Census) * n_censuses,
293 "nextEra");
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
294 }
295 }
03dc2dd [project @ 2005-03-27 13:41:13 by panne]
panne authored
296 #endif /* PROFILING */
0bffc41 [project @ 2002-12-11 15:36:20 by simonmar]
simonmar authored
297
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
298 initEra( &censuses[era] );
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
299 }
300
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
301 /* ----------------------------------------------------------------------------
302 * Heap profiling by info table
303 * ------------------------------------------------------------------------- */
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
304
a7f3dd0 FIX profiling :-(
Simon Marlow authored
305 #if !defined(PROFILING)
066dbe7 [project @ 2000-04-19 12:42:48 by simonmar]
simonmar authored
306 FILE *hp_file;
8def875 [project @ 2003-10-24 14:45:38 by stolz]
stolz authored
307 static char *hp_filename;
ba3011c [project @ 2000-03-07 12:03:01 by simonmar]
simonmar authored
308
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
309 void initProfiling1 (void)
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
310 {
311 }
312
a52ff76 @simonmar Change the way module initialisation is done (#3252, #4417)
simonmar authored
313 void freeProfiling (void)
ed12b70 @igfoo Free more things that we allocate
igfoo authored
314 {
315 }
316
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
317 void initProfiling2 (void)
cadc82f [project @ 1999-09-16 12:29:55 by simonmar]
simonmar authored
318 {
8ef97aa @simonmar Fix #1955 for heap profiles generated by +RTS -hT
simonmar authored
319 char *prog;
320
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
325 {
326 char *suff;
327 suff = strrchr(prog,'.');
328 if (suff != NULL && !strcmp(suff,".exe")) {
329 *suff = '\0';
330 }
331 }
332 #endif
333
8def875 [project @ 2003-10-24 14:45:38 by stolz]
stolz authored
334 if (RtsFlags.ProfFlags.doHeapProfile) {
335 /* Initialise the log file name */
8ef97aa @simonmar Fix #1955 for heap profiles generated by +RTS -hT
simonmar authored
336 hp_filename = stgMallocBytes(strlen(prog) + 6, "hpFileName");
337 sprintf(hp_filename, "%s.hp", prog);
8def875 [project @ 2003-10-24 14:45:38 by stolz]
stolz authored
338
339 /* open the log file */
340 if ((hp_file = fopen(hp_filename, "w")) == NULL) {
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
341 debugBelch("Can't open profiling report file %s\n",
8def875 [project @ 2003-10-24 14:45:38 by stolz]
stolz authored
342 hp_filename);
343 RtsFlags.ProfFlags.doHeapProfile = 0;
344 return;
345 }
346 }
347
8ef97aa @simonmar Fix #1955 for heap profiles generated by +RTS -hT
simonmar authored
348 stgFree(prog);
349
cadc82f [project @ 1999-09-16 12:29:55 by simonmar]
simonmar authored
350 initHeapProfiling();
351 }
352
353 void endProfiling( void )
354 {
355 endHeapProfiling();
356 }
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
357 #endif /* !PROFILING */
cadc82f [project @ 1999-09-16 12:29:55 by simonmar]
simonmar authored
358
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
359 static void
360 printSample(rtsBool beginSample, StgDouble sampleValue)
361 {
362 StgDouble fractionalPart, integralPart;
363 fractionalPart = modf(sampleValue, &integralPart);
186a80c @igfoo Fix ~2000 second profiling time wrapping bug
igfoo authored
364 fprintf(hp_file, "%s %" FMT_Word64 ".%02" FMT_Word64 "\n",
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
365 (beginSample ? "BEGIN_SAMPLE" : "END_SAMPLE"),
186a80c @igfoo Fix ~2000 second profiling time wrapping bug
igfoo authored
366 (StgWord64)integralPart, (StgWord64)(fractionalPart * 100));
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
367 }
368
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
369 /* --------------------------------------------------------------------------
370 * Initialize the heap profilier
371 * ----------------------------------------------------------------------- */
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
372 nat
373 initHeapProfiling(void)
374 {
375 if (! RtsFlags.ProfFlags.doHeapProfile) {
376 return 0;
377 }
378
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
379 #ifdef PROFILING
380 if (doingLDVProfiling() && doingRetainerProfiling()) {
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
381 errorBelch("cannot mix -hb and -hr");
03a9ff0 [project @ 2005-10-21 14:02:17 by simonmar]
simonmar authored
382 stg_exit(EXIT_FAILURE);
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
383 }
384 #endif
385
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
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
50935f1 @igfoo Compute max_era more simply
igfoo authored
397 // max_era = 2^LDV_SHIFT
398 max_era = 1 << LDV_SHIFT;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
399
400 n_censuses = 32;
401 censuses = stgMallocBytes(sizeof(Census) * n_censuses, "initHeapProfiling");
402
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
403 initEra( &censuses[era] );
404
8def875 [project @ 2003-10-24 14:45:38 by stolz]
stolz authored
405 /* initProfilingLogFile(); */
cffcb52 [project @ 2003-08-22 22:24:12 by sof]
sof authored
406 fprintf(hp_file, "JOB \"%s", prog_name);
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
407
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
408 #ifdef PROFILING
409 {
410 int count;
411 for(count = 1; count < prog_argc; count++)
412 fprintf(hp_file, " %s", prog_argv[count]);
9b5e088 [project @ 2004-03-19 23:20:20 by panne]
panne authored
413 fprintf(hp_file, " +RTS");
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
414 for(count = 0; count < rts_argc; count++)
9b5e088 [project @ 2004-03-19 23:20:20 by panne]
panne authored
415 fprintf(hp_file, " %s", rts_argv[count]);
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
416 }
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
417 #endif /* PROFILING */
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
418
419 fprintf(hp_file, "\"\n" );
420
066dbe7 [project @ 2000-04-19 12:42:48 by simonmar]
simonmar authored
421 fprintf(hp_file, "DATE \"%s\"\n", time_str());
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
422
066dbe7 [project @ 2000-04-19 12:42:48 by simonmar]
simonmar authored
423 fprintf(hp_file, "SAMPLE_UNIT \"seconds\"\n");
424 fprintf(hp_file, "VALUE_UNIT \"bytes\"\n");
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
425
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
426 printSample(rtsTrue, 0);
427 printSample(rtsFalse, 0);
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
428
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
429 #ifdef PROFILING
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
430 if (doingRetainerProfiling()) {
431 initRetainerProfiling();
432 }
90515a1 [project @ 2000-04-05 15:32:08 by simonmar]
simonmar authored
433 #endif
434
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
435 return 0;
436 }
437
438 void
439 endHeapProfiling(void)
440 {
441 StgDouble seconds;
442
443 if (! RtsFlags.ProfFlags.doHeapProfile) {
444 return;
445 }
446
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
447 #ifdef PROFILING
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
448 if (doingRetainerProfiling()) {
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
449 endRetainerProfiling();
450 }
451 #endif
452
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
453 #ifdef PROFILING
2097713 [project @ 2001-11-28 17:45:13 by simonmar]
simonmar authored
454 if (doingLDVProfiling()) {
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
455 nat t;
b560e2a [project @ 2001-11-29 16:38:13 by simonmar]
simonmar authored
456 LdvCensusKillAll();
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
457 aggregateCensusInfo();
458 for (t = 1; t < era; t++) {
459 dumpCensus( &censuses[t] );
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
460 }
461 }
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
462 #endif
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
463
a60af39 FIX BUILD (non-profiling RTS was broken in previous patch)
Simon Marlow authored
464 #ifdef PROFILING
7f6c039 FIX crash on exit with biographical profiling
Simon Marlow authored
465 if (doingLDVProfiling()) {
ed12b70 @igfoo Free more things that we allocate
igfoo authored
466 nat t;
7f6c039 FIX crash on exit with biographical profiling
Simon Marlow authored
467 for (t = 1; t <= era; t++) {
ed12b70 @igfoo Free more things that we allocate
igfoo authored
468 freeEra( &censuses[t] );
469 }
7f6c039 FIX crash on exit with biographical profiling
Simon Marlow authored
470 } else {
471 freeEra( &censuses[0] );
ed12b70 @igfoo Free more things that we allocate
igfoo authored
472 }
a60af39 FIX BUILD (non-profiling RTS was broken in previous patch)
Simon Marlow authored
473 #else
474 freeEra( &censuses[0] );
475 #endif
476
ed12b70 @igfoo Free more things that we allocate
igfoo authored
477 stgFree(censuses);
478
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
479 seconds = mut_user_time();
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
480 printSample(rtsTrue, seconds);
481 printSample(rtsFalse, seconds);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
482 fclose(hp_file);
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
483 }
484
485
486
487 #ifdef PROFILING
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
488 static size_t
489 buf_append(char *p, const char *q, char *end)
490 {
491 int m;
492
493 for (m = 0; p < end; p++, q++, m++) {
494 *p = *q;
495 if (*q == '\0') { break; }
496 }
497 return m;
498 }
499
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
500 static void
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
501 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat max_length)
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
502 {
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
503 char buf[max_length+1], *p, *buf_end;
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
504
505 // MAIN on its own gets printed as "MAIN", otherwise we ignore MAIN.
506 if (ccs == CCS_MAIN) {
507 fprintf(fp, "MAIN");
508 return;
509 }
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
510
b0ac0f9 [project @ 2005-04-05 09:30:11 by simonmar]
simonmar authored
511 fprintf(fp, "(%ld)", ccs->ccsID);
7738ad9 [project @ 2001-12-12 15:59:33 by simonmar]
simonmar authored
512
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
513 p = buf;
514 buf_end = buf + max_length + 1;
515
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
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) {
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
519
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
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")) {
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
523 p += buf_append(p, ccs->cc->module, buf_end);
524 p += buf_append(p, ".CAF", buf_end);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
525 } else {
3cdb0ad hp_slash_fix
Ravi Nanavati authored
526 p += buf_append(p, ccs->cc->label, buf_end);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
527 if (ccs->prevStack != NULL && ccs->prevStack != CCS_MAIN) {
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
528 p += buf_append(p, "/", buf_end);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
529 }
530 }
428c025 [project @ 2003-03-18 14:36:56 by simonmar]
simonmar authored
531
532 if (p >= buf_end) {
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
533 sprintf(buf+max_length-4, "...");
534 break;
535 }
536 }
537 fprintf(fp, "%s", buf);
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
538 }
539
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
540 rtsBool
541 strMatchesSelector( char* str, char* sel )
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
542 {
543 char* p;
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
544 // debugBelch("str_matches_selector %s %s\n", str, sel);
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
545 while (1) {
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
546 // Compare str against wherever we've got to in sel.
547 p = str;
548 while (*p != '\0' && *sel != ',' && *sel != '\0' && *p == *sel) {
549 p++; sel++;
550 }
551 // Match if all of str used and have reached the end of a sel fragment.
552 if (*p == '\0' && (*sel == ',' || *sel == '\0'))
553 return rtsTrue;
554
555 // No match. Advance sel to the start of the next elem.
556 while (*sel != ',' && *sel != '\0') sel++;
557 if (*sel == ',') sel++;
558
559 /* Run out of sel ?? */
560 if (*sel == '\0') return rtsFalse;
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
561 }
562 }
563
a2a67cd @simonmar RTS tidyup sweep, first phase
simonmar authored
564 #endif /* PROFILING */
565
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
566 /* -----------------------------------------------------------------------------
567 * Figure out whether a closure should be counted in this census, by
568 * testing against all the specified constraints.
569 * -------------------------------------------------------------------------- */
a2a67cd @simonmar RTS tidyup sweep, first phase
simonmar authored
570 static rtsBool
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
571 closureSatisfiesConstraints( StgClosure* p )
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
572 {
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
573 #if !defined(PROFILING)
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
574 (void)p; /* keep gcc -Wall happy */
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
575 return rtsTrue;
576 #else
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
577 rtsBool b;
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
578
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
581 // selectors.
582 if (!p->header.prof.ccs->selected) {
583 return rtsFalse;
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
584 }
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
585
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
586 if (RtsFlags.ProfFlags.descrSelector) {
23e5985 FIX #1418 (partially)
Simon Marlow authored
587 b = strMatchesSelector( (GET_PROF_DESC(get_itbl((StgClosure *)p))),
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
588 RtsFlags.ProfFlags.descrSelector );
589 if (!b) return rtsFalse;
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
590 }
591 if (RtsFlags.ProfFlags.typeSelector) {
23e5985 FIX #1418 (partially)
Simon Marlow authored
592 b = strMatchesSelector( (GET_PROF_TYPE(get_itbl((StgClosure *)p))),
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
593 RtsFlags.ProfFlags.typeSelector );
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
594 if (!b) return rtsFalse;
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
595 }
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
596 if (RtsFlags.ProfFlags.retainerSelector) {
597 RetainerSet *rs;
598 nat i;
51a1c2a [project @ 2003-02-20 15:39:59 by simonmar]
simonmar authored
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);
605 if (rs != NULL) {
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;
610 }
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
611 }
612 }
613 return rtsFalse;
614 }
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
615 return rtsTrue;
616 #endif /* PROFILING */
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
617 }
bc210f7 [project @ 2001-03-14 11:18:18 by sewardj]
sewardj authored
618
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
619 /* -----------------------------------------------------------------------------
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
620 * Aggregate the heap census info for biographical profiling
621 * -------------------------------------------------------------------------- */
622 #ifdef PROFILING
623 static void
624 aggregateCensusInfo( void )
625 {
626 HashTable *acc;
627 nat t;
628 counter *c, *d, *ctrs;
629 Arena *arena;
630
631 if (!doingLDVProfiling()) return;
632
633 // Aggregate the LDV counters when displaying by biography.
634 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
635 int void_total, drag_total;
636
637 // Now we compute void_total and drag_total for each census
60905d4 lag/drag/void: add an extra assertion, and some commentary
Simon Marlow authored
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.
643 //
644 // To get the count of live words that are void at each
645 // census, just propagate the void_total count forwards:
646
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
647 void_total = 0;
648 drag_total = 0;
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;
60905d4 lag/drag/void: add an extra assertion, and some commentary
Simon Marlow authored
654
8098d68 [project @ 2001-12-12 15:01:25 by simonmar]
simonmar authored
655 ASSERT( censuses[t].void_total <= censuses[t].not_used );
60905d4 lag/drag/void: add an extra assertion, and some commentary
Simon Marlow authored
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
659 // been used yet.
660
8098d68 [project @ 2001-12-12 15:01:25 by simonmar]
simonmar authored
661 ASSERT( censuses[t].drag_total <= censuses[t].used );
60905d4 lag/drag/void: add an extra assertion, and some commentary
Simon Marlow authored
662 // similar reasoning as above.
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
663 }
664
665 return;
666 }
667
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.
672
673 arena = newArena();
674 acc = allocHashTable();
675 ctrs = NULL;
676
677 for (t = 1; t < era; t++) {
678
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);
684
685 if (d == NULL) {
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);
690
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
691 // debugCCS(c->identity);
692 // debugBelch(" census=%d void_total=%d drag_total=%d\n",
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
693 // t, c->c.ldv.void_total, c->c.ldv.drag_total);
694 } else {
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;
699
700 ASSERT( c->c.ldv.void_total >= 0 );
701 ASSERT( c->c.ldv.drag_total >= 0 );
702 }
703 }
704
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);
708 if (d == NULL) {
709 d = arenaAlloc( arena, sizeof(counter) );
710 initLDVCtr(d);
711 insertHashTable( acc, (StgWord)c->identity, d );
712 d->identity = c->identity;
713 d->next = ctrs;
714 ctrs = d;
715 d->c.ldv.void_total = c->c.ldv.void_total;
716 d->c.ldv.drag_total = c->c.ldv.drag_total;
717 }
718 ASSERT( c->c.ldv.void_total >= 0 );
719 ASSERT( c->c.ldv.drag_total >= 0 );
720 }
721 }
722
723 freeHashTable(acc, NULL);
724 arenaFree(arena);
725 }
726 #endif
727
728 /* -----------------------------------------------------------------------------
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
729 * Print out the results of a heap census.
730 * -------------------------------------------------------------------------- */
731 static void
732 dumpCensus( Census *census )
733 {
734 counter *ctr;
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
735 int count;
736
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
737 printSample(rtsTrue, census->time);
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
738
2097713 [project @ 2001-11-28 17:45:13 by simonmar]
simonmar authored
739 #ifdef PROFILING
740 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
178c142 [project @ 2005-05-21 16:09:18 by panne]
panne authored
741 fprintf(hp_file, "VOID\t%lu\n", (unsigned long)(census->void_total) * sizeof(W_));
b0ac0f9 [project @ 2005-04-05 09:30:11 by simonmar]
simonmar authored
742 fprintf(hp_file, "LAG\t%lu\n",
178c142 [project @ 2005-05-21 16:09:18 by panne]
panne authored
743 (unsigned long)(census->not_used - census->void_total) * sizeof(W_));
b0ac0f9 [project @ 2005-04-05 09:30:11 by simonmar]
simonmar authored
744 fprintf(hp_file, "USE\t%lu\n",
178c142 [project @ 2005-05-21 16:09:18 by panne]
panne authored
745 (unsigned long)(census->used - census->drag_total) * sizeof(W_));
b0ac0f9 [project @ 2005-04-05 09:30:11 by simonmar]
simonmar authored
746 fprintf(hp_file, "INHERENT_USE\t%lu\n",
178c142 [project @ 2005-05-21 16:09:18 by panne]
panne authored
747 (unsigned long)(census->prim) * sizeof(W_));
748 fprintf(hp_file, "DRAG\t%lu\n",
749 (unsigned long)(census->drag_total) * sizeof(W_));
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
750 printSample(rtsFalse, census->time);
2097713 [project @ 2001-11-28 17:45:13 by simonmar]
simonmar authored
751 return;
752 }
753 #endif
754
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
755 for (ctr = census->ctrs; ctr != NULL; ctr = ctr->next) {
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
756
757 #ifdef PROFILING
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
758 if (RtsFlags.ProfFlags.bioSelector != NULL) {
759 count = 0;
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
760 if (strMatchesSelector("lag", RtsFlags.ProfFlags.bioSelector))
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
761 count += ctr->c.ldv.not_used - ctr->c.ldv.void_total;
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
762 if (strMatchesSelector("drag", RtsFlags.ProfFlags.bioSelector))
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
763 count += ctr->c.ldv.drag_total;
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
764 if (strMatchesSelector("void", RtsFlags.ProfFlags.bioSelector))
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
765 count += ctr->c.ldv.void_total;
55c8be5 [project @ 2001-12-12 14:31:42 by simonmar]
simonmar authored
766 if (strMatchesSelector("use", RtsFlags.ProfFlags.bioSelector))
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
767 count += ctr->c.ldv.used - ctr->c.ldv.drag_total;
768 } else
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
769 #endif
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
770 {
771 count = ctr->c.resid;
772 }
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
773
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
774 ASSERT( count >= 0 );
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
775
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
776 if (count == 0) continue;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
777
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
778 #if !defined(PROFILING)
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
779 switch (RtsFlags.ProfFlags.doHeapProfile) {
780 case HEAP_BY_CLOSURE_TYPE:
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
781 fprintf(hp_file, "%s", (char *)ctr->identity);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
782 break;
783 }
784 #endif
785
786 #ifdef PROFILING
787 switch (RtsFlags.ProfFlags.doHeapProfile) {
788 case HEAP_BY_CCS:
1687148 rts_ccs_length
Ravi Nanavati authored
789 fprint_ccs(hp_file, (CostCentreStack *)ctr->identity, RtsFlags.ProfFlags.ccsLength);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
790 break;
791 case HEAP_BY_MOD:
792 case HEAP_BY_DESCR:
793 case HEAP_BY_TYPE:
794 fprintf(hp_file, "%s", (char *)ctr->identity);
795 break;
796 case HEAP_BY_RETAINER:
797 {
798 RetainerSet *rs = (RetainerSet *)ctr->identity;
799
cbb4515 [project @ 2001-11-27 15:30:06 by simonmar]
simonmar authored
800 // it might be the distinguished retainer set rs_MANY:
801 if (rs == &rs_MANY) {
802 fprintf(hp_file, "MANY");
803 break;
804 }
805
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
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.
812 if (rs->id > 0)
813 rs->id = -(rs->id);
814
815 // report in the unit of bytes: * sizeof(StgWord)
d5adcc5 @igfoo Make retainer profiling use the label length (-L) flag)
igfoo authored
816 printRetainerSetShort(hp_file, rs, RtsFlags.ProfFlags.ccsLength);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
817 break;
818 }
819 default:
820 barf("dumpCensus; doHeapProfile");
821 }
822 #endif
823
178c142 [project @ 2005-05-21 16:09:18 by panne]
panne authored
824 fprintf(hp_file, "\t%lu\n", (unsigned long)count * sizeof(W_));
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
825 }
826
2db3d8c [project @ 2004-05-11 18:36:10 by panne]
panne authored
827 printSample(rtsFalse, census->time);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
828 }
829
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
830
831 static void heapProfObject(Census *census, StgClosure *p, nat size,
832 rtsBool prim
833 #ifndef PROFILING
834 STG_UNUSED
835 #endif
836 )
837 {
838 void *identity;
839 nat real_size;
840 counter *ctr;
841
842 identity = NULL;
843
844 #ifdef PROFILING
845 // subtract the profiling overhead
846 real_size = size - sizeofW(StgProfHeader);
847 #else
848 real_size = size;
849 #endif
850
851 if (closureSatisfiesConstraints((StgClosure*)p)) {
852 #ifdef PROFILING
853 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV) {
854 if (prim)
855 census->prim += real_size;
856 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
857 census->not_used += real_size;
858 else
859 census->used += real_size;
860 } else
861 #endif
862 {
863 identity = closureIdentity((StgClosure *)p);
864
865 if (identity != NULL) {
866 ctr = lookupHashTable( census->hash, (StgWord)identity );
867 if (ctr != NULL) {
868 #ifdef PROFILING
869 if (RtsFlags.ProfFlags.bioSelector != NULL) {
870 if (prim)
871 ctr->c.ldv.prim += real_size;
872 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
873 ctr->c.ldv.not_used += real_size;
874 else
875 ctr->c.ldv.used += real_size;
876 } else
877 #endif
878 {
879 ctr->c.resid += real_size;
880 }
881 } else {
882 ctr = arenaAlloc( census->arena, sizeof(counter) );
883 initLDVCtr(ctr);
884 insertHashTable( census->hash, (StgWord)identity, ctr );
885 ctr->identity = identity;
886 ctr->next = census->ctrs;
887 census->ctrs = ctr;
888
889 #ifdef PROFILING
890 if (RtsFlags.ProfFlags.bioSelector != NULL) {
891 if (prim)
892 ctr->c.ldv.prim = real_size;
893 else if ((LDVW(p) & LDV_STATE_MASK) == LDV_STATE_CREATE)
894 ctr->c.ldv.not_used = real_size;
895 else
896 ctr->c.ldv.used = real_size;
897 } else
898 #endif
899 {
900 ctr->c.resid = real_size;
901 }
902 }
903 }
904 }
905 }
906 }
907
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
908 /* -----------------------------------------------------------------------------
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
909 * Code to perform a heap census.
910 * -------------------------------------------------------------------------- */
911 static void
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
912 heapCensusChain( Census *census, bdescr *bd )
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
913 {
914 StgPtr p;
915 StgInfoTable *info;
916 nat size;
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
917 rtsBool prim;
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
918
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
919 for (; bd != NULL; bd = bd->link) {
76ebf3d [project @ 2003-09-23 15:38:35 by simonmar]
simonmar authored
920
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
921 // HACK: pretend a pinned block is just one big ARR_WORDS
922 // owned by CCS_SYSTEM. These blocks can be full of holes due
923 // to alignment constraints so we can't traverse the memory
924 // and do a proper census.
925 if (bd->flags & BF_PINNED) {
926 StgClosure arr;
927 SET_HDR(&arr, &stg_ARR_WORDS_info, CCS_SYSTEM);
928 heapProfObject(census, &arr, bd->blocks * BLOCK_SIZE_W, rtsTrue);
929 continue;
930 }
76ebf3d [project @ 2003-09-23 15:38:35 by simonmar]
simonmar authored
931
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
932 p = bd->start;
933 while (p < bd->free) {
934 info = get_itbl((StgClosure *)p);
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
935 prim = rtsFalse;
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
936
937 switch (info->type) {
938
0f3205e [project @ 2005-04-22 09:32:39 by simonmar]
simonmar authored
939 case THUNK:
940 size = thunk_sizeW_fromITBL(info);
941 break;
942
943 case THUNK_1_1:
944 case THUNK_0_2:
945 case THUNK_2_0:
beb5737 make the smp way RTS-only, normal libraries now work with -smp
Simon Marlow authored
946 size = sizeofW(StgThunkHeader) + 2;
0f3205e [project @ 2005-04-22 09:32:39 by simonmar]
simonmar authored
947 break;
948
949 case THUNK_1_0:
950 case THUNK_0_1:
951 case THUNK_SELECTOR:
beb5737 make the smp way RTS-only, normal libraries now work with -smp
Simon Marlow authored
952 size = sizeofW(StgThunkHeader) + 1;
0f3205e [project @ 2005-04-22 09:32:39 by simonmar]
simonmar authored
953 break;
954
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
955 case CONSTR:
956 case FUN:
957 case IND_PERM:
958 case BLACKHOLE:
5d52d9b @simonmar New implementation of BLACKHOLEs
simonmar authored
959 case BLOCKING_QUEUE:
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
960 case FUN_1_0:
961 case FUN_0_1:
962 case FUN_1_1:
963 case FUN_0_2:
964 case FUN_2_0:
965 case CONSTR_1_0:
966 case CONSTR_0_1:
967 case CONSTR_1_1:
968 case CONSTR_0_2:
969 case CONSTR_2_0:
970 size = sizeW_fromITBL(info);
971 break;
beb5737 make the smp way RTS-only, normal libraries now work with -smp
Simon Marlow authored
972
9aae4de [project @ 2005-09-16 09:59:26 by simonmar]
simonmar authored
973 case IND:
974 // Special case/Delicate Hack: INDs don't normally
975 // appear, since we're doing this heap census right
976 // after GC. However, GarbageCollect() also does
977 // resurrectThreads(), which can update some
978 // blackholes when it calls raiseAsync() on the
979 // resurrected threads. So we know that any IND will
980 // be the size of a BLACKHOLE.
981 size = BLACKHOLE_sizeW();
982 break;
983
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
984 case BCO:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
985 prim = rtsTrue;
b3f5308 [project @ 2003-03-24 14:46:53 by simonmar]
simonmar authored
986 size = bco_sizeW((StgBCO *)p);
987 break;
988
1ed01a8 Add a proper write barrier for MVars
Simon Marlow authored
989 case MVAR_CLEAN:
990 case MVAR_DIRTY:
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
991 case WEAK:
7408b39 @simonmar Use message-passing to implement throwTo in the RTS
simonmar authored
992 case PRIM:
993 case MUT_PRIM:
91b0721 [project @ 2006-01-17 16:13:18 by simonmar]
simonmar authored
994 case MUT_VAR_CLEAN:
995 case MUT_VAR_DIRTY:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
996 prim = rtsTrue;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
997 size = sizeW_fromITBL(info);
998 break;
999
0f3205e [project @ 2005-04-22 09:32:39 by simonmar]
simonmar authored
1000 case AP:
1001 size = ap_sizeW((StgAP *)p);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1002 break;
1003
1004 case PAP:
1005 size = pap_sizeW((StgPAP *)p);
1006 break;
0bffc41 [project @ 2002-12-11 15:36:20 by simonmar]
simonmar authored
1007
1008 case AP_STACK:
1009 size = ap_stack_sizeW((StgAP_STACK *)p);
1010 break;
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1011
1012 case ARR_WORDS:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
1013 prim = rtsTrue;
a2a67cd @simonmar RTS tidyup sweep, first phase
simonmar authored
1014 size = arr_words_sizeW((StgArrWords*)p);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1015 break;
1016
da69fa9 [project @ 2006-01-17 16:03:47 by simonmar]
simonmar authored
1017 case MUT_ARR_PTRS_CLEAN:
1018 case MUT_ARR_PTRS_DIRTY:
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1019 case MUT_ARR_PTRS_FROZEN:
37ee58b [project @ 2005-07-26 14:42:53 by simonmar]
simonmar authored
1020 case MUT_ARR_PTRS_FROZEN0:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
1021 prim = rtsTrue;
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1022 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
1023 break;
1024
1025 case TSO:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
1026 prim = rtsTrue;
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
1027 #ifdef PROFILING
70c1ca0 [project @ 2003-01-23 12:13:10 by simonmar]
simonmar authored
1028 if (RtsFlags.ProfFlags.includeTSOs) {
f30d527 @simonmar Implement stack chunks and separate TSO/STACK objects
simonmar authored
1029 size = sizeofW(StgTSO);
70c1ca0 [project @ 2003-01-23 12:13:10 by simonmar]
simonmar authored
1030 break;
1031 } else {
1032 // Skip this TSO and move on to the next object
f30d527 @simonmar Implement stack chunks and separate TSO/STACK objects
simonmar authored
1033 p += sizeofW(StgTSO);
70c1ca0 [project @ 2003-01-23 12:13:10 by simonmar]
simonmar authored
1034 continue;
1035 }
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
1036 #else
f30d527 @simonmar Implement stack chunks and separate TSO/STACK objects
simonmar authored
1037 size = sizeofW(StgTSO);
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
1038 break;
e989066 [project @ 2003-01-23 16:39:30 by simonmar]
simonmar authored
1039 #endif
1040
f30d527 @simonmar Implement stack chunks and separate TSO/STACK objects
simonmar authored
1041 case STACK:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
1042 prim = rtsTrue;
f30d527 @simonmar Implement stack chunks and separate TSO/STACK objects
simonmar authored
1043 #ifdef PROFILING
1044 if (RtsFlags.ProfFlags.includeTSOs) {
1045 size = stack_sizeW((StgStack*)p);
1046 break;
1047 } else {
1048 // Skip this TSO and move on to the next object
1049 p += stack_sizeW((StgStack*)p);
1050 continue;
1051 }
1052 #else
1053 size = stack_sizeW((StgStack*)p);
1054 break;
1055 #endif
1056
1057 case TREC_CHUNK:
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
1058 prim = rtsTrue;
9aae4de [project @ 2005-09-16 09:59:26 by simonmar]
simonmar authored
1059 size = sizeofW(StgTRecChunk);
1060 break;
1061
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1062 default:
9aae4de [project @ 2005-09-16 09:59:26 by simonmar]
simonmar authored
1063 barf("heapCensus, unknown object: %d", info->type);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1064 }
1065
d56cbac @simonmar Better heap profiling for pinned objects (e.g. ByteStrings).
simonmar authored
1066 heapProfObject(census,(StgClosure*)p,size,prim);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1067
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
1068 p += size;
1069 }
1070 }
1071 }
c95b2db [project @ 2001-07-19 07:28:00 by andy]
andy authored
1072
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1073 void
4f8cfaf @igfoo Fix heap profiling times
igfoo authored
1074 heapCensus( Ticks t )
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1075 {
18197a0 @simonmar heapCensus: we need to traverse the heap fragments stored in the
simonmar authored
1076 nat g, n;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1077 Census *census;
18197a0 @simonmar heapCensus: we need to traverse the heap fragments stored in the
simonmar authored
1078 gen_workspace *ws;
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1079
1080 census = &censuses[era];
4f8cfaf @igfoo Fix heap profiling times
igfoo authored
1081 census->time = mut_user_time_until(t);
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1082
1083 // calculate retainer sets if necessary
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1084 #ifdef PROFILING
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1085 if (doingRetainerProfiling()) {
1086 retainerProfile();
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1087 }
1088 #endif
1089
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
1090 #ifdef PROFILING
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
1091 stat_startHeapCensus();
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
1092 #endif
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
1093
6226441 [project @ 2002-11-01 11:05:46 by simonmar]
simonmar authored
1094 // Traverse the heap, collecting the census info
214b366 @simonmar GC refactoring, remove "steps"
simonmar authored
1095 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1096 heapCensusChain( census, generations[g].blocks );
1097 // Are we interested in large objects? might be
1098 // confusing to include the stack in a heap profile.
1099 heapCensusChain( census, generations[g].large_objects );
18197a0 @simonmar heapCensus: we need to traverse the heap fragments stored in the
simonmar authored
1100
1101 for (n = 0; n < n_capabilities; n++) {
1102 ws = &gc_threads[n]->gens[g];
1103 heapCensusChain(census, ws->todo_bd);
1104 heapCensusChain(census, ws->part_list);
1105 heapCensusChain(census, ws->scavd_list);
1106 }
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1107 }
1108
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1109 // dump out the census info
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
1110 #ifdef PROFILING
1111 // We can't generate any info for LDV profiling until
1112 // the end of the run...
1113 if (!doingLDVProfiling())
1114 dumpCensus( census );
1115 #else
1116 dumpCensus( census );
1117 #endif
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1118
6313b90 [project @ 2001-11-28 15:01:02 by simonmar]
simonmar authored
1119
1120 // free our storage, unless we're keeping all the census info for
1121 // future restriction by biography.
1122 #ifdef PROFILING
1123 if (RtsFlags.ProfFlags.bioSelector == NULL)
1124 {
1125 freeHashTable( census->hash, NULL/* don't free the elements */ );
1126 arenaFree( census->arena );
1127 census->hash = NULL;
1128 census->arena = NULL;
1129 }
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
1130 #endif
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1131
1132 // we're into the next time period now
1133 nextEra();
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1134
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
1135 #ifdef PROFILING
dbef766 [project @ 2001-11-26 16:54:21 by simonmar]
simonmar authored
1136 stat_endHeapCensus();
f182992 [project @ 2001-11-28 15:43:23 by simonmar]
simonmar authored
1137 #endif
fd4ef87 [project @ 1999-09-15 13:46:28 by simonmar]
simonmar authored
1138 }
1139
Something went wrong with that request. Please try again.