File:  [gforth] / gforth / vmgen-ex / profile.c
Revision 1.1: download - view: text, annotated - select for diffs
Sun Apr 29 11:28:24 2001 UTC (18 years, 8 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added vmgen-ex

    1: /* VM profiling support stuff
    2: 
    3:   Copyright (C) 2001 Free Software Foundation, Inc.
    4: 
    5:   This file is part of Gforth.
    6: 
    7:   Gforth is free software; you can redistribute it and/or
    8:   modify it under the terms of the GNU General Public License
    9:   as published by the Free Software Foundation; either version 2
   10:   of the License, or (at your option) any later version.
   11: 
   12:   This program is distributed in the hope that it will be useful,
   13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
   14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15:   GNU General Public License for more details.
   16: 
   17:   You should have received a copy of the GNU General Public License
   18:   along with this program; if not, write to the Free Software
   19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: */
   21: 
   22: #include <stdlib.h>
   23: #include <stdio.h>
   24: #include <assert.h>
   25: #include "mini.h"
   26: 
   27: /* data structure: simple hash table with external chaining */
   28: 
   29: #define HASH_SIZE (1<<20)
   30: 
   31: typedef struct block_count {
   32:   struct block_count *next; /* next in hash table */
   33:   struct block_count *fallthrough; /* the block that this one falls
   34:                                        through to without SUPER_END */
   35:   Inst *ip;
   36:   long long count;
   37:   char **insts;
   38:   size_t ninsts;
   39: } block_count;
   40: 
   41: block_count *blocks[HASH_SIZE];
   42: 
   43: #define hash(p) ((((Cell)(p))/sizeof(Inst))&(HASH_SIZE-1))
   44: 
   45: block_count *block_lookup(Inst *ip)
   46: {
   47:   block_count *b = blocks[hash(ip)];
   48: 
   49:   while (b!=NULL && b->ip!=ip)
   50:     b = b->next;
   51:   return b;
   52: }
   53: 
   54: /* looks up present elements, inserts absent elements */
   55: block_count *block_insert(Inst *ip)
   56: { 
   57:   block_count *b = block_lookup(ip);
   58:   block_count *new;
   59: 
   60:   if (b != NULL)
   61:     return b;
   62:   new = (block_count *)malloc(sizeof(block_count));
   63:   new->next = blocks[hash(ip)];
   64:   new->fallthrough = NULL;
   65:   new->ip = ip;
   66:   new->count = 0LL;
   67:   new->insts = malloc(0);
   68:   assert(new->insts != NULL);
   69:   new->ninsts = 0;
   70:   blocks[hash(ip)] = new;
   71:   return new;
   72: }
   73: 
   74: void add_inst(block_count *b, char *inst)
   75: {
   76:   b->insts = realloc(b->insts, (b->ninsts+1) * sizeof(char *));
   77:   b->insts[b->ninsts++] = inst;
   78: }
   79: 
   80: void vm_count_block(Inst *ip)
   81: {
   82:   block_insert(ip)->count++;
   83: }
   84: 
   85: #define VM_IS_INST(inst, n) ((inst) == vm_prim[n])
   86: 
   87: void postprocess_block(block_count *b)
   88: {
   89:   Inst *ip = b->ip;
   90:   block_count *next_block;
   91: 
   92:   do {
   93: #include "mini-profile.i"
   94:     /* else */
   95:     {
   96:       add_inst(b,"unknown");
   97:       ip++;
   98:     }
   99:   _endif_:
  100:     next_block = block_lookup(ip);
  101:   } while (next_block == NULL);
  102:   /* we fell through, so set fallthrough and update the count */
  103:   b->fallthrough = next_block;
  104:   /* also update the counts of all following fallthrough blocks that
  105:      have already been processed */
  106:   while (next_block != NULL) {
  107:     next_block->count += b->count;
  108:     next_block = next_block->fallthrough;
  109:   }
  110: }
  111: 
  112: /* Deal with block entry by falling through from non-SUPER_END
  113:    instructions.  And fill the insts and ninsts fields. */
  114: void postprocess(void)
  115: {
  116:   size_t i;
  117: 
  118:   for (i=0; i<HASH_SIZE; i++) {
  119:     block_count *b = blocks[i];
  120:     for (; b!=0; b = b->next)
  121:       postprocess_block(b);
  122:    }
  123: }
  124: 
  125: #if 0
  126: void print_block(FILE *file, block_count *b)
  127: {
  128:   size_t i;
  129: 
  130:   fprintf(file,"%14lld\t",b->count);
  131:   for (i=0; i<b->ninsts; i++)
  132:     fprintf(file, "%s ", b->insts[i]);
  133:   putc('\n', file);
  134: }
  135: #endif
  136: 
  137: void print_block(FILE *file, block_count *b)
  138: {
  139:   size_t i,j,k;
  140: 
  141:   for (i=2; i<12; i++)
  142:     for (j=0; i+j<=b->ninsts; j++) {
  143:       fprintf(file,"%14lld\t",b->count);
  144:       for (k=j; k<i+j; k++)
  145: 	fprintf(file, "%s ", b->insts[k]);
  146:       putc('\n', file);
  147:     }
  148: }
  149: 
  150: void vm_print_profile(FILE *file)
  151: {
  152:   size_t i;
  153: 
  154:   postprocess();
  155:   for (i=0; i<HASH_SIZE; i++) {
  156:     block_count *b = blocks[i];
  157:     for (; b!=0; b = b->next)
  158:       print_block(file, b);
  159:    }
  160: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>