OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mprofiler.c] - Blame information for rev 548

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 547 markom
/* mprofiler.c -- memory profiling utility
2
   Copyright (C) 2002 Marko Mlinar, markom@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Command line utility, that displays profiling information, generated
21
   by or1ksim. (use --mprofile option at command line, when running or1ksim.  */
22
 
23
#include <stdio.h>
24
#include <malloc.h>
25
#include "support/profile.h"
26
 
27
/* output modes */
28
#define MODE_DETAIL     0
29
#define MODE_PRETTY     1
30
#define MODE_ACCESS     2
31
#define MODE_WIDTH      3
32
 
33
/* Input buffer size */
34
#define BUF_SIZE        100
35
 
36
/* HASH */
37
#define HASH_SIZE       0x10000
38
#define HASH_FUNC(x)    ((x) & 0xffff)
39
struct memory_hash {
40
  struct memory_hash *next;
41
  unsigned long addr;
42
  unsigned long cnt[3];    /* Various counters */
43
} *hash[HASH_SIZE];
44
 
45 548 markom
int cnt[64] = {0};
46
 
47 547 markom
/* Groups size -- how much addresses should be joined together */
48
int group_bits = 2;
49
 
50
/* Start address */
51
int start_addr = 0;
52
 
53
/* End address */
54
int end_addr = 0xffffffff;
55
 
56
/* File to read from */
57
static FILE *fprof = 0;
58
 
59
void help ()
60
{
61
  printf ("USAGE: mprofiler <-d|-p|-a|-w> [-f filename] [-g group] from to\n");
62
  printf ("\t-d\t--detail\t\tdetailed output\n");
63
  printf ("\t-p\t--pretty\t\tpretty output\n");
64
  printf ("\t-a\t--access\t\toutput accesses only\n");
65
  printf ("\t-w\t--width\t\t\toutput by width\n");
66
  printf ("\t-f\t--filename filename\tspecify mprofile file [sim.mprofile]\n");
67
  printf ("\t-g\t--group bits\t\tgroup 2^bits successive\n");
68
  printf ("\t\t\t\t\taddresses together [2]\n");
69
  printf ("\t-h\t--help\t\t\toutput this screen\n");
70
}
71
 
72
void hash_add (unsigned long addr, int index)
73
{
74
  struct memory_hash *h = hash[HASH_FUNC(addr)];
75
  while (h && h->addr != addr) h = h->next;
76
 
77
  if (!h) {
78
    h = (struct memory_hash *)malloc (sizeof (struct memory_hash));
79
    h->next = hash[HASH_FUNC(addr)];
80
    hash[HASH_FUNC(addr)] = h;
81
    h->addr = addr;
82
    h->cnt[0] = h->cnt[1] = h->cnt[2] = 0;
83
  }
84
  h->cnt[index]++;
85
}
86
 
87
unsigned long hash_get (unsigned long addr, int index)
88
{
89
  struct memory_hash *h = hash[HASH_FUNC(addr)];
90
  while (h && h->addr != addr) h = h->next;
91
 
92
  if (!h) return 0;
93
  return h->cnt[index];
94
}
95
 
96
void init ()
97
{
98
  int i;
99
  for (i = 0; i < HASH_SIZE; i++)
100
    hash[i] = NULL;
101
}
102
 
103
void read_file (FILE *f, int mode)
104
{
105
  struct mprofentry_struct buf[BUF_SIZE];
106
  int num_read;
107
  do {
108
    int i;
109
    num_read = fread (buf, sizeof (struct mprofentry_struct), BUF_SIZE, f);
110
    for (i = 0; i < num_read; i++) if (buf[i].addr >= start_addr && buf[i].addr <= end_addr) {
111
      int index;
112 548 markom
      unsigned t = buf[i].type;
113
      if (t > 64) {
114
        printf ("!");
115
        t = 0;
116
      }
117
      cnt[t]++;
118
      if (mode == MODE_WIDTH) t >>= 3;
119
      else t &= 0x7;
120
 
121
      switch (t) {
122 547 markom
        case 1: index = 0; break;
123
        case 2: index = 1; break;
124
        case 4: index = 2; break;
125 548 markom
        default:
126
          index = 0;
127
          printf ("!!!!");
128
          break;
129 547 markom
      }
130
      hash_add (buf[i].addr >> group_bits, index);
131
    }
132
  } while (num_read > 0);
133 548 markom
  {int i;
134
  for (i = 0; i < 64; i++)
135
    printf ("%i:%i\t", i, cnt[i]);
136
  printf ("\n");
137
  }
138 547 markom
}
139
 
140
static inline int nbits (unsigned long a)
141
{
142
  int cnt = 0;
143
  int b = a;
144
  if (!a) return 0;
145
 
146
  while (a) a >>= 1, cnt++;
147
  if (cnt > 1 && ((b >> (cnt - 2)) & 1))
148
    cnt = cnt * 2 + 1;
149
  else
150
    cnt *= 2;
151
 
152
  return cnt - 1;
153
}
154
 
155
void printout (int mode)
156
{
157
  unsigned long addr = start_addr & ~((1 << group_bits) - 1);
158
  printf ("%08x %08x %08x %08x %i\n", start_addr, end_addr, addr, group_bits, mode);
159
  for (; addr <= end_addr; addr += (1 << group_bits)) {
160
    int i;
161
    unsigned long a = hash_get (addr >> group_bits, 0);
162
    unsigned long b = hash_get (addr >> group_bits, 1);
163
    unsigned long c = hash_get (addr >> group_bits, 2);
164
    printf ("%08x:", addr);
165
    switch (mode) {
166
      case MODE_DETAIL:
167
        if (a) printf (" %10i R", a);
168
        else printf ("            R");
169
        if (b) printf (" %10i W", b);
170
        else printf ("            W");
171
        if (c) printf (" %10i F", c);
172
        else printf ("            F");
173
        break;
174
      case MODE_ACCESS:
175
        printf (" %10i", a + b + c);
176
        break;
177
      case MODE_PRETTY:
178
        printf (" %10i ", a + b + c);
179
        for (i = 0; i < nbits (a + b + c); i++)
180
          printf ("#");
181
#if 0
182
        for (; i < 64; i++)
183
          printf (".");
184
#endif
185
        break;
186
      case MODE_WIDTH:
187
        if (a) printf (" %10i B", a);
188
        else printf ("            B");
189
        if (b) printf (" %10i H", b);
190
        else printf ("            H");
191
        if (c) printf (" %10i W", c);
192
        else printf ("            W");
193
        break;
194
    }
195
    printf ("\n");
196
  }
197
}
198
 
199
int main (int argc, char *argv[])
200
{
201
  char fmprofname[50] = "sim.mprofile";
202
  int param = 0;
203
  int mode = MODE_DETAIL;
204
 
205
  argv++; argc--;
206
  while (argc > 0) {
207
    if (!strcmp(argv[0], "-d") || !strcmp(argv[0], "--detail")) {
208
      mode = MODE_DETAIL;
209
      argv++; argc--;
210
    } else if (!strcmp(argv[0], "-p") || !strcmp(argv[0], "--pretty")) {
211
      mode = MODE_PRETTY;
212
      argv++; argc--;
213
    } else if (!strcmp(argv[0], "-a") || !strcmp(argv[0], "--access")) {
214
      mode = MODE_ACCESS;
215
      argv++; argc--;
216
    } else if (!strcmp(argv[0], "-w") || !strcmp(argv[0], "--width")) {
217
      mode = MODE_WIDTH;
218
      argv++; argc--;
219
    } else if (!strcmp(argv[0], "-g") || !strcmp(argv[0], "--group")) {
220
      argv++; argc--;
221
      group_bits = strtoul (argv[0], NULL, 0);
222
      argv++; argc--;
223
    } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
224
      help ();
225
      exit (0);
226
    } else if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--filename")) {
227
      argv++; argc--;
228
      strcpy (&fmprofname[0], argv[0]);
229
      argv++; argc--;
230
    } else {
231
      switch (param) {
232
        case 0:
233
          start_addr = strtoul (argv[0], NULL, 0);
234
          break;
235
        case 1:
236
          end_addr = strtoul (argv[0], NULL, 0);
237
          break;
238
        default:
239
          fprintf (stderr, "Invalid number of parameters.\n");
240
          exit (-1);
241
      }
242
      argv++; argc--; param++;
243
    }
244
  }
245
 
246
  fprof = fopen (&fmprofname[0], "rm");
247
 
248
  if (!fprof) {
249
    fprintf (stderr, "Cannot open profile file: %s\n", &fmprofname[0]);
250
    exit(1);
251
  }
252
 
253
  init ();
254
  read_file (fprof, mode);
255
  fclose (fprof);
256
  printout (mode);
257
  return 0;
258
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.