1 /* -----------------------------------------------------------------------------
2 * $Id: ProfHeap.c,v 1.5 2000/03/07 12:03:01 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 initProfiling( void )
43 void endProfiling( void )
47 #endif /* DEBUG_HEAP_PROF */
50 initHeapProfiling(void)
52 if (! RtsFlags.ProfFlags.doHeapProfile) {
56 fprintf(prof_file, "JOB \"%s\"\n", prog_argv[0]);
57 fprintf(prof_file, "DATE \"%s\"\n", time_str());
59 fprintf(prof_file, "SAMPLE_UNIT \"seconds\"\n");
60 fprintf(prof_file, "VALUE_UNIT \"bytes\"\n");
62 fprintf(prof_file, "BEGIN_SAMPLE 0.00\n");
63 fprintf(prof_file, "END_SAMPLE 0.00\n");
65 #ifdef DEBUG_HEAP_PROF
66 DEBUG_LoadSymbols(prog_argv[0]);
74 endHeapProfiling(void)
78 if (! RtsFlags.ProfFlags.doHeapProfile) {
82 seconds = mut_user_time();
83 fprintf(prof_file, "BEGIN_SAMPLE %0.2f\n", seconds);
84 fprintf(prof_file, "END_SAMPLE %0.2f\n", seconds);
88 #ifdef DEBUG_HEAP_PROF
89 /* -----------------------------------------------------------------------------
90 Hash table for symbols.
91 -------------------------------------------------------------------------- */
99 #define SYMBOL_HASH_SIZE 0x3fff
101 symbol_info symbol_hash[SYMBOL_HASH_SIZE];
106 return ((W_)ptr)>>4 & 0x3fff;
114 for (i=0; i < SYMBOL_HASH_SIZE; i++) {
115 symbol_hash[i].ptr = NULL;
120 lookup_symbol(void *addr)
122 nat orig_bucket = hash(addr);
125 bucket = orig_bucket;
126 while (bucket < SYMBOL_HASH_SIZE && symbol_hash[bucket].ptr != NULL) {
127 if (symbol_hash[bucket].ptr == addr) {
132 if (bucket == SYMBOL_HASH_SIZE) {
134 while (bucket < orig_bucket && symbol_hash[bucket].ptr != NULL) {
135 if (symbol_hash[bucket].ptr == addr) {
140 if (bucket == orig_bucket) {
141 barf("out of symbol table space");
145 symbol_hash[bucket].ptr = addr;
146 lookupGHCName(addr,&symbol_hash[bucket].name);
147 symbol_hash[bucket].data = 0;
152 clear_table_data(void)
156 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
157 symbol_hash[i].data = 0;
162 fprint_data(FILE *fp)
166 for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
167 if (symbol_hash[i].data) {
168 fprintf(fp, " %s %d\n", symbol_hash[i].name, symbol_hash[i].data);
174 add_data(void *addr, nat data)
176 symbol_hash[lookup_symbol(addr)].data += data;
179 /* -----------------------------------------------------------------------------
180 Closure Type Profiling;
182 PROBABLY TOTALLY OUT OF DATE -- ToDo (SDM)
183 -------------------------------------------------------------------------- */
185 static nat closure_types[N_CLOSURE_TYPES];
187 static char *type_names[] = {
193 , "CONSTR_NOCAF_STATIC"
231 , "MUT_ARR_PTRS_FROZEN"
246 fprint_closure_types(FILE *fp)
250 for (i = 0; i < N_CLOSURE_TYPES; i++) {
251 if (closure_types[i]) {
252 fprintf(fp, " %s %d\n", type_names[i], closure_types[i]);
257 #endif /* DEBUG_HEAP_PROF */
262 clearCCSResid(CostCentreStack *ccs)
268 for (i = ccs->indexTable; i != 0; i = i->next) {
269 clearCCSResid(i->ccs);
274 fprint_ccs(FILE *fp, CostCentreStack *ccs, nat components)
277 CostCentreStack *prev;
280 prev = ccs->prevStack;
283 || prev->cc->is_subsumed != CC_IS_BORING
284 || components == 1) {
285 fprintf(fp,"%s",cc->label);
289 fprint_ccs(fp, ccs->prevStack,components-1);
290 fprintf(fp,"/%s",cc->label);
295 reportCCSResid(FILE *fp, CostCentreStack *ccs)
299 if (ccs->mem_resid != 0) {
301 fprint_ccs(fp,ccs,2/*print 2 components only*/);
302 fprintf(fp," %ld\n", ccs->mem_resid * sizeof(W_));
305 for (i = ccs->indexTable; i != 0; i = i->next) {
306 reportCCSResid(fp,i->ccs);
315 const StgInfoTable *info;
320 #ifdef DEBUG_HEAP_PROF
321 switch (RtsFlags.ProfFlags.doHeapProfile) {
322 case HEAP_BY_INFOPTR:
325 case HEAP_BY_CLOSURE_TYPE:
326 memset(closure_types, 0, N_CLOSURE_TYPES * sizeof(nat));
334 switch (RtsFlags.ProfFlags.doHeapProfile) {
335 case NO_HEAP_PROFILING:
340 barf("heapCensus; doHeapProfile");
342 /* zero all the residency counters */
343 clearCCSResid(CCS_MAIN);
346 /* Only do heap profiling in a two-space heap */
347 ASSERT(RtsFlags.GcFlags.generations == 1);
350 time = mut_user_time_during_GC();
351 fprintf(prof_file, "BEGIN_SAMPLE %0.2f\n", time);
355 while (p < bd->free) {
356 info = get_itbl((StgClosure *)p);
358 switch (info->type) {
360 size = bco_sizeW((StgBCO *)p);
364 if (((StgClosure *)p)->header.info == &DEAD_WEAK_info) {
365 size = sizeofW(StgWeak);
368 /* else, fall through... */
373 case IND_OLDGEN_PERM:
375 case SE_CAF_BLACKHOLE:
385 case CONSTR_CHARLIKE:
401 size = sizeW_fromITBL(info);
405 size = sizeofW(StgHeader) + MIN_UPD_SIZE;
408 case AP_UPD: /* we can treat this as being the same as a PAP */
410 size = pap_sizeW((StgPAP *)p);
414 size = arr_words_sizeW(stgCast(StgArrWords*,p));
418 case MUT_ARR_PTRS_FROZEN:
419 size = mut_arr_ptrs_sizeW((StgMutArrPtrs *)p);
423 size = tso_sizeW((StgTSO *)p);
430 #ifdef DEBUG_HEAP_PROF
431 switch (RtsFlags.ProfFlags.doHeapProfile) {
432 case HEAP_BY_INFOPTR:
433 add_data((void *)(*p), size * sizeof(W_));
435 case HEAP_BY_CLOSURE_TYPE:
436 closure_types[info->type] += size * sizeof(W_);
442 ((StgClosure *)p)->header.prof.ccs->mem_resid += size;
449 #ifdef DEBUG_HEAP_PROF
450 switch (RtsFlags.ProfFlags.doHeapProfile) {
451 case HEAP_BY_INFOPTR:
452 fprint_data(prof_file);
454 case HEAP_BY_CLOSURE_TYPE:
455 fprint_closure_types(prof_file);
461 reportCCSResid(prof_file,CCS_MAIN);
464 fprintf(prof_file, "END_SAMPLE %0.2f\n", time);
467 #endif /* PROFILING || DEBUG_HEAP_PROF */