1 /* -----------------------------------------------------------------------------
2 * $Id: ProfHeap.c,v 1.7 2000/03/23 13:13:29 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
6 * Support for heap profiling
8 * ---------------------------------------------------------------------------*/
10 #if defined(DEBUG) && !defined(PROFILING)
11 #define DEBUG_HEAP_PROF
13 #undef DEBUG_HEAP_PROF
16 #if defined(PROFILING) || defined(DEBUG_HEAP_PROF)
26 #ifdef DEBUG_HEAP_PROF
28 static void initSymbolHash(void);
29 static void clear_table_data(void);
30 static void fprint_data(FILE *fp);
33 char prof_filename[128]; /* urk */
35 #ifdef DEBUG_HEAP_PROF
38 void initProfiling1( void )
42 void initProfiling2( void )
47 void endProfiling( void )
51 #endif /* DEBUG_HEAP_PROF */
54 initHeapProfiling(void)
56 if (! RtsFlags.ProfFlags.doHeapProfile) {
60 fprintf(prof_file, "JOB \"%s\"\n", prog_argv[0]);
61 fprintf(prof_file, "DATE \"%s\"\n", time_str());
63 fprintf(prof_file, "SAMPLE_UNIT \"seconds\"\n");
64 fprintf(prof_file, "VALUE_UNIT \"bytes\"\n");
66 fprintf(prof_file, "BEGIN_SAMPLE 0.00\n");
67 fprintf(prof_file, "END_SAMPLE 0.00\n");
69 #ifdef DEBUG_HEAP_PROF
70 DEBUG_LoadSymbols(prog_argv[0]);
78 endHeapProfiling(void)
82 if (! RtsFlags.ProfFlags.doHeapProfile) {
86 seconds = mut_user_time();
87 fprintf(prof_file, "BEGIN_SAMPLE %0.2f\n", seconds);
88 fprintf(prof_file, "END_SAMPLE %0.2f\n", seconds);
92 #ifdef DEBUG_HEAP_PROF
93 /* -----------------------------------------------------------------------------
94 Hash table for symbols.
95 -------------------------------------------------------------------------- */
103 #define SYMBOL_HASH_SIZE 0x3fff
105 symbol_info symbol_hash[SYMBOL_HASH_SIZE];
110 return ((W_)ptr)>>4 & 0x3fff;
118 for (i=0; i < SYMBOL_HASH_SIZE; i++) {
119 symbol_hash[i].ptr = NULL;
124 lookup_symbol(void *addr)
126 nat orig_bucket = hash(addr);
129 bucket = orig_bucket;
130 while (bucket < SYMBOL_HASH_SIZE && symbol_hash[bucket].ptr != NULL) {
131 if (symbol_hash[bucket].ptr == addr) {
136 if (bucket == SYMBOL_HASH_SIZE) {
138 while (bucket < orig_bucket && symbol_hash[bucket].ptr != NULL) {
139 if (symbol_hash[bucket].ptr == addr) {
144 if (bucket == orig_bucket) {
145 barf("out of symbol table space");
149 symbol_hash[bucket].ptr = addr;
150 lookupGHCName(addr,&symbol_hash[bucket].name);
151 symbol_hash[bucket].data = 0;
156 clear_table_data(void)
160 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
161 symbol_hash[i].data = 0;
166 fprint_data(FILE *fp)
170 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
171 if (symbol_hash[i].data) {
172 fprintf(fp, " %s %d\n", symbol_hash[i].name, symbol_hash[i].data);
178 add_data(void *addr, nat data)
180 symbol_hash[lookup_symbol(addr)].data += data;
183 /* -----------------------------------------------------------------------------
184 Closure Type Profiling;
186 PROBABLY TOTALLY OUT OF DATE -- ToDo (SDM)
187 -------------------------------------------------------------------------- */
189 static nat closure_types[N_CLOSURE_TYPES];
191 static char *type_names[] = {
197 , "CONSTR_NOCAF_STATIC"
235 , "MUT_ARR_PTRS_FROZEN"
250 fprint_closure_types(FILE *fp)
254 for (i = 0; i < N_CLOSURE_TYPES; i++) {
255 if (closure_types[i]) {
256 fprintf(fp, " %s %d\n", type_names[i], closure_types[i]);
261 #endif /* DEBUG_HEAP_PROF */
266 clearCCSResid(CostCentreStack *ccs)
272 for (i = ccs->indexTable; i != 0; i = i->next) {
274 clearCCSResid(i->ccs);
280 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat components)
283 CostCentreStack *prev;
286 prev = ccs->prevStack;
289 || prev->cc->is_subsumed != CC_IS_BORING
290 || components == 1) {
291 fprintf(fp,"%s",cc->label);
295 fprint_ccs(fp, ccs->prevStack,components-1);
296 fprintf(fp,"/%s",cc->label);
301 reportCCSResid(FILE *fp, CostCentreStack *ccs)
305 if (ccs->mem_resid != 0) {
307 fprint_ccs(fp,ccs,2/*print 2 components only*/);
308 fprintf(fp," %ld\n", ccs->mem_resid * sizeof(W_));
311 for (i = ccs->indexTable; i != 0; i = i->next) {
313 reportCCSResid(fp,i->ccs);
323 const StgInfoTable *info;
328 #ifdef DEBUG_HEAP_PROF
329 switch (RtsFlags.ProfFlags.doHeapProfile) {
330 case HEAP_BY_INFOPTR:
333 case HEAP_BY_CLOSURE_TYPE:
334 memset(closure_types, 0, N_CLOSURE_TYPES * sizeof(nat));
342 switch (RtsFlags.ProfFlags.doHeapProfile) {
343 case NO_HEAP_PROFILING:
348 barf("heapCensus; doHeapProfile");
350 /* zero all the residency counters */
351 clearCCSResid(CCS_MAIN);
354 /* Only do heap profiling in a two-space heap */
355 ASSERT(RtsFlags.GcFlags.generations == 1);
358 time = mut_user_time_during_GC();
359 fprintf(prof_file, "BEGIN_SAMPLE %0.2f\n", time);
363 while (p < bd->free) {
364 info = get_itbl((StgClosure *)p);
366 switch (info->type) {
368 size = bco_sizeW((StgBCO *)p);
372 if (((StgClosure *)p)->header.info == &DEAD_WEAK_info) {
373 size = sizeofW(StgWeak);
376 /* else, fall through... */
381 case IND_OLDGEN_PERM:
383 case SE_CAF_BLACKHOLE:
393 case CONSTR_CHARLIKE:
409 size = sizeW_fromITBL(info);
413 size = sizeofW(StgHeader) + MIN_UPD_SIZE;
416 case AP_UPD: /* we can treat this as being the same as a PAP */
418 size = pap_sizeW((StgPAP *)p);
422 size = arr_words_sizeW(stgCast(StgArrWords*,p));
426 case MUT_ARR_PTRS_FROZEN:
427 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
431 size = tso_sizeW((StgTSO *)p);
438 #ifdef DEBUG_HEAP_PROF
439 switch (RtsFlags.ProfFlags.doHeapProfile) {
440 case HEAP_BY_INFOPTR:
441 add_data((void *)(*p), size * sizeof(W_));
443 case HEAP_BY_CLOSURE_TYPE:
444 closure_types[info->type] += size * sizeof(W_);
450 ((StgClosure *)p)->header.prof.ccs->mem_resid += size;
457 #ifdef DEBUG_HEAP_PROF
458 switch (RtsFlags.ProfFlags.doHeapProfile) {
459 case HEAP_BY_INFOPTR:
460 fprint_data(prof_file);
462 case HEAP_BY_CLOSURE_TYPE:
463 fprint_closure_types(prof_file);
469 reportCCSResid(prof_file,CCS_MAIN);
472 fprintf(prof_file, "END_SAMPLE %0.2f\n", time);
475 #endif /* PROFILING || DEBUG_HEAP_PROF */