ProfHeap.c 8.88 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: ProfHeap.c,v 1.7 2000/03/23 13:13:29 simonmar Exp $
3
 *
4
 * (c) The GHC Team, 1998-2000
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * Support for heap profiling
 *
 * ---------------------------------------------------------------------------*/

#if defined(DEBUG) && !defined(PROFILING)
#define DEBUG_HEAP_PROF
#else
#undef DEBUG_HEAP_PROF
#endif

#if defined(PROFILING) || defined(DEBUG_HEAP_PROF)

#include "Rts.h"
#include "RtsUtils.h"
#include "RtsFlags.h"
#include "ProfRts.h"
#include "Storage.h"
#include "ProfHeap.h"
#include "Stats.h"
#include "ProfRts.h"
#ifdef DEBUG_HEAP_PROF
#include "Printer.h"
static void initSymbolHash(void);
static void clear_table_data(void);
static void fprint_data(FILE *fp);
#endif

char prof_filename[128];	/* urk */

35
#ifdef DEBUG_HEAP_PROF
36
37
FILE *prof_file;

38
39
40
41
42
void initProfiling1( void )
{
}

void initProfiling2( void )
43
44
45
46
47
48
49
50
51
52
{
  initHeapProfiling();
}

void endProfiling( void )
{
  endHeapProfiling();
}
#endif /* DEBUG_HEAP_PROF */

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
nat
initHeapProfiling(void)
{
    if (! RtsFlags.ProfFlags.doHeapProfile) {
        return 0;
    }

    fprintf(prof_file, "JOB \"%s\"\n", prog_argv[0]);
    fprintf(prof_file, "DATE \"%s\"\n", time_str());

    fprintf(prof_file, "SAMPLE_UNIT \"seconds\"\n");
    fprintf(prof_file, "VALUE_UNIT \"bytes\"\n");

    fprintf(prof_file, "BEGIN_SAMPLE 0.00\n");
    fprintf(prof_file, "END_SAMPLE 0.00\n");

#ifdef DEBUG_HEAP_PROF
    DEBUG_LoadSymbols(prog_argv[0]);
    initSymbolHash();
#endif

    return 0;
}

void
endHeapProfiling(void)
{
    StgDouble seconds;

    if (! RtsFlags.ProfFlags.doHeapProfile) {
        return;
    }

    seconds = mut_user_time();
    fprintf(prof_file, "BEGIN_SAMPLE %0.2f\n", seconds);
    fprintf(prof_file, "END_SAMPLE %0.2f\n", seconds);
    fclose(prof_file);
}

#ifdef DEBUG_HEAP_PROF
/* -----------------------------------------------------------------------------
   Hash table for symbols.
   -------------------------------------------------------------------------- */

typedef struct {
    const char *name;
    void *ptr;
    nat data;
} symbol_info;

#define SYMBOL_HASH_SIZE 0x3fff

symbol_info symbol_hash[SYMBOL_HASH_SIZE];

static inline nat
hash(void *ptr)
{
    return ((W_)ptr)>>4 & 0x3fff;
}

static void
initSymbolHash(void)
{
    nat i;

    for (i=0; i < SYMBOL_HASH_SIZE; i++) {
	symbol_hash[i].ptr = NULL;
    }
}

static nat
lookup_symbol(void *addr)
{
    nat orig_bucket = hash(addr);
    nat bucket;

    bucket = orig_bucket;
    while (bucket < SYMBOL_HASH_SIZE && symbol_hash[bucket].ptr != NULL) {
	if (symbol_hash[bucket].ptr == addr) {
	    return bucket;
	}
	bucket++;
    }
    if (bucket == SYMBOL_HASH_SIZE) {
	bucket = 0;
	while (bucket < orig_bucket && symbol_hash[bucket].ptr != NULL) {
	    if (symbol_hash[bucket].ptr == addr) {
		return bucket;
	    }
	    bucket++;
	}
	if (bucket == orig_bucket) {
	    barf("out of symbol table space");
	}
    }
    
    symbol_hash[bucket].ptr  = addr;
    lookupGHCName(addr,&symbol_hash[bucket].name);
    symbol_hash[bucket].data = 0;
    return bucket;
}

static void
clear_table_data(void)
{
    nat i;

    for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
	symbol_hash[i].data = 0;
    }
}

static void
fprint_data(FILE *fp)
{
    nat i;
    
    for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
	if (symbol_hash[i].data) {
	    fprintf(fp, "   %s %d\n", symbol_hash[i].name, symbol_hash[i].data);
	}
    }
}

static inline void
add_data(void *addr, nat data)
{
    symbol_hash[lookup_symbol(addr)].data += data;
}

/* -----------------------------------------------------------------------------
   Closure Type Profiling;

   PROBABLY TOTALLY OUT OF DATE -- ToDo (SDM)
   -------------------------------------------------------------------------- */

static nat closure_types[N_CLOSURE_TYPES];

static char *type_names[] = {
      "INVALID_OBJECT"
    , "CONSTR"
    , "CONSTR_INTLIKE"
    , "CONSTR_CHARLIKE"
    , "CONSTR_STATIC"
    , "CONSTR_NOCAF_STATIC"

    , "FUN"
    , "FUN_STATIC"

    , "THUNK"
    , "THUNK_STATIC"
    , "THUNK_SELECTOR"

    , "BCO"
    , "AP_UPD"

    , "PAP"

    , "IND"
    , "IND_OLDGEN"
    , "IND_PERM"
    , "IND_OLDGEN_PERM"
    , "IND_STATIC"

    , "RET_BCO"
    , "RET_SMALL"
    , "RET_VEC_SMALL"
    , "RET_BIG"
    , "RET_VEC_BIG"
    , "RET_DYN"
    , "UPDATE_FRAME"
    , "CATCH_FRAME"
    , "STOP_FRAME"
    , "SEQ_FRAME"

    , "BLACKHOLE"
    , "BLACKHOLE_BQ"
    , "MVAR"

    , "ARR_WORDS"

    , "MUT_ARR_PTRS"
    , "MUT_ARR_PTRS_FROZEN"
    , "MUT_VAR"

    , "WEAK"
    , "FOREIGN"
  
    , "TSO"

    , "BLOCKED_FETCH"
    , "FETCH_ME"

    , "EVACUATED"
};

static void 
fprint_closure_types(FILE *fp)
{
  nat i;

  for (i = 0; i < N_CLOSURE_TYPES; i++) {
    if (closure_types[i]) {
      fprintf(fp, "   %s %d\n", type_names[i], closure_types[i]);
    }
  }
}

#endif /* DEBUG_HEAP_PROF */


#ifdef PROFILING
static void
clearCCSResid(CostCentreStack *ccs)
{
  IndexTable *i;

  ccs->mem_resid = 0;

  for (i = ccs->indexTable; i != 0; i = i->next) {
273
274
275
    if (!i->back_edge) {
      clearCCSResid(i->ccs);
    }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  }
}

static void
fprint_ccs(FILE *fp, CostCentreStack *ccs, nat components)
{
  CostCentre *cc;
  CostCentreStack *prev;

  cc = ccs->cc;
  prev = ccs->prevStack;

  if (prev == NULL
      || prev->cc->is_subsumed != CC_IS_BORING
      || components == 1) { 
    fprintf(fp,"%s",cc->label);
    return; 

  } else {
    fprint_ccs(fp, ccs->prevStack,components-1);
    fprintf(fp,"/%s",cc->label);
  }
}

static void
reportCCSResid(FILE *fp, CostCentreStack *ccs)
{
  IndexTable *i;

  if (ccs->mem_resid != 0) {
    fprintf(fp,"   ");
    fprint_ccs(fp,ccs,2/*print 2 components only*/);
    fprintf(fp," %ld\n", ccs->mem_resid * sizeof(W_));
  }

  for (i = ccs->indexTable; i != 0; i = i->next) {
312
313
314
    if (!i->back_edge) {
      reportCCSResid(fp,i->ccs);
    }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  }
}
#endif

void
heapCensus(void)
{
  bdescr *bd;
  const StgInfoTable *info;
  StgDouble time;
  nat size;
  StgPtr p;
  
#ifdef DEBUG_HEAP_PROF
  switch (RtsFlags.ProfFlags.doHeapProfile) {
  case HEAP_BY_INFOPTR:
    clear_table_data();
    break;
  case HEAP_BY_CLOSURE_TYPE:
    memset(closure_types, 0, N_CLOSURE_TYPES * sizeof(nat));
    break;
  default:
    return;
  }
#endif

#ifdef PROFILING
  switch (RtsFlags.ProfFlags.doHeapProfile) {
  case NO_HEAP_PROFILING:
    return;
  case HEAP_BY_CCS:
    break;
  default:
    barf("heapCensus; doHeapProfile");
  }
  /* zero all the residency counters */
  clearCCSResid(CCS_MAIN);
#endif

  /* Only do heap profiling in a two-space heap */
  ASSERT(RtsFlags.GcFlags.generations == 1);
  bd = g0s0->to_space;

  time = mut_user_time_during_GC();
  fprintf(prof_file, "BEGIN_SAMPLE %0.2f\n", time);
  
  while (bd != NULL) {
    p = bd->start;
    while (p < bd->free) {
      info = get_itbl((StgClosure *)p);

      switch (info->type) {
      case BCO:
	size = bco_sizeW((StgBCO *)p);
	break;
	
      case CONSTR:
	if (((StgClosure *)p)->header.info == &DEAD_WEAK_info) {
	  size = sizeofW(StgWeak);
	  break;
	}
	/* else, fall through... */

      case FUN:
      case THUNK:
      case IND_PERM:
      case IND_OLDGEN_PERM:
      case CAF_BLACKHOLE:
      case SE_CAF_BLACKHOLE:
      case SE_BLACKHOLE:
      case BLACKHOLE:
      case BLACKHOLE_BQ:
      case WEAK:
      case FOREIGN:
      case STABLE_NAME:
      case MVAR:
      case MUT_VAR:
      case CONSTR_INTLIKE:
      case CONSTR_CHARLIKE:
      case FUN_1_0:
      case FUN_0_1:
      case FUN_1_1:
      case FUN_0_2:
      case FUN_2_0:
      case THUNK_1_0:
      case THUNK_0_1:
      case THUNK_1_1:
      case THUNK_0_2:
      case THUNK_2_0:
      case CONSTR_1_0:
      case CONSTR_0_1:
      case CONSTR_1_1:
      case CONSTR_0_2:
      case CONSTR_2_0:
	size = sizeW_fromITBL(info);
	break;
	
      case THUNK_SELECTOR:
	size = sizeofW(StgHeader) + MIN_UPD_SIZE;
	break;
	
      case AP_UPD: /* we can treat this as being the same as a PAP */
      case PAP:
	size = pap_sizeW((StgPAP *)p);
	break;
	
      case ARR_WORDS:
	size = arr_words_sizeW(stgCast(StgArrWords*,p));
	break;
	
      case MUT_ARR_PTRS:
      case MUT_ARR_PTRS_FROZEN:
	size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
	break;
	
      case TSO:
	size = tso_sizeW((StgTSO *)p);
	break;
	
      default:
	barf("heapCensus");
      }

#ifdef DEBUG_HEAP_PROF
      switch (RtsFlags.ProfFlags.doHeapProfile) {
      case HEAP_BY_INFOPTR:
	add_data((void *)(*p), size * sizeof(W_));
	break;
      case HEAP_BY_CLOSURE_TYPE:
	closure_types[info->type] += size * sizeof(W_);
	break;
      }
#endif

#ifdef PROFILING      
      ((StgClosure *)p)->header.prof.ccs->mem_resid += size;
#endif
      p += size;
    }
    bd = bd->link;
  }

#ifdef DEBUG_HEAP_PROF
  switch (RtsFlags.ProfFlags.doHeapProfile) {
  case HEAP_BY_INFOPTR:
    fprint_data(prof_file);
    break;
  case HEAP_BY_CLOSURE_TYPE:
    fprint_closure_types(prof_file);
    break;
  }
#endif
    
#ifdef PROFILING
  reportCCSResid(prof_file,CCS_MAIN);
#endif

  fprintf(prof_file, "END_SAMPLE %0.2f\n", time);
}    

#endif /* PROFILING || DEBUG_HEAP_PROF */