File: profile.c

package info (click to toggle)
gforth 0.7.0+ds2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,808 kB
  • sloc: ansic: 8,506; sh: 3,660; lisp: 1,783; makefile: 993; yacc: 186; sed: 141; lex: 102; awk: 21
file content (163 lines) | stat: -rw-r--r-- 3,772 bytes parent folder | download | duplicates (5)
1
2
3
4
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
/* VM profiling support stuff

  Copyright (C) 2001,2003,2007 Free Software Foundation, Inc.

  This file is part of Gforth.

  Gforth is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation, either version 3
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, see http://www.gnu.org/licenses/.
*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "mini.h"

/* data structure: simple hash table with external chaining */

#define HASH_SIZE (1<<20)
#define hash(p) ((((Cell)(p))/sizeof(Inst))&(HASH_SIZE-1))

#ifdef __GNUC__
typedef long long long_long;
#else
typedef long long_long;
#endif

typedef struct block_count {
  struct block_count *next; /* next in hash table */
  struct block_count *fallthrough; /* the block that this one falls
                                       through to without SUPER_END */
  Inst *ip;
  long_long count;
  char **insts;
  size_t ninsts;
} block_count;

block_count *blocks[HASH_SIZE];
Inst *vmcode_end;

block_count *block_lookup(Inst *ip)
{
  block_count *b = blocks[hash(ip)];

  while (b!=NULL && b->ip!=ip)
    b = b->next;
  return b;
}

/* looks up present elements, inserts absent elements */
block_count *block_insert(Inst *ip)
{ 
  block_count *b = block_lookup(ip);
  block_count *new;

  if (b != NULL)
    return b;
  new = (block_count *)malloc(sizeof(block_count));
  new->next = blocks[hash(ip)];
  new->fallthrough = NULL;
  new->ip = ip;
  new->count = (long_long)0;
  new->insts = malloc(1);
  assert(new->insts != NULL);
  new->ninsts = 0;
  blocks[hash(ip)] = new;
  return new;
}

void add_inst(block_count *b, char *inst)
{
  b->insts = realloc(b->insts, (b->ninsts+1) * sizeof(char *));
  b->insts[b->ninsts++] = inst;
}

void vm_count_block(Inst *ip)
{
  block_insert(ip)->count++;
}

void postprocess_block(block_count *b)
{
  Inst *ip = b->ip;
  block_count *next_block=NULL;

  while (next_block == NULL && ip<vmcode_end) {
#include "mini-profile.i"
    /* else */
    {
      add_inst(b,"unknown");
      ip++;
    }
  _endif_:
    next_block = block_lookup(ip);
  }
  /* we fell through, so set fallthrough and update the count */
  b->fallthrough = next_block;
  /* also update the counts of all following fallthrough blocks that
     have already been processed */
  while (next_block != NULL) {
    next_block->count += b->count;
    next_block = next_block->fallthrough;
  }
}

/* Deal with block entry by falling through from non-SUPER_END
   instructions.  And fill the insts and ninsts fields. */
void postprocess(void)
{
  size_t i;

  for (i=0; i<HASH_SIZE; i++) {
    block_count *b = blocks[i];
    for (; b!=0; b = b->next)
      postprocess_block(b);
   }
}

#if 0
void print_block(FILE *file, block_count *b)
{
  size_t i;

  fprintf(file,"%14lld\t",b->count);
  for (i=0; i<b->ninsts; i++)
    fprintf(file, "%s ", b->insts[i]);
  putc('\n', file);
}
#endif

void print_block(FILE *file, block_count *b)
{
  size_t i,j,k;

  for (i=2; i<12; i++)
    for (j=0; i+j<=b->ninsts; j++) {
      fprintf(file,"%14lld\t",b->count);
      for (k=j; k<i+j; k++)
	fprintf(file, "%s ", b->insts[k]);
      putc('\n', file);
    }
}

void vm_print_profile(FILE *file)
{
  size_t i;

  postprocess();
  for (i=0; i<HASH_SIZE; i++) {
    block_count *b = blocks[i];
    for (; b!=0; b = b->next)
      print_block(file, b);
   }
}