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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_62/] [or1ksim/] [mprofiler.c] - Blame information for rev 547

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
/* Groups size -- how much addresses should be joined together */
46
int group_bits = 2;
47
 
48
/* Start address */
49
int start_addr = 0;
50
 
51
/* End address */
52
int end_addr = 0xffffffff;
53
 
54
/* File to read from */
55
static FILE *fprof = 0;
56
 
57
void help ()
58
{
59
  printf ("USAGE: mprofiler <-d|-p|-a|-w> [-f filename] [-g group] from to\n");
60
  printf ("\t-d\t--detail\t\tdetailed output\n");
61
  printf ("\t-p\t--pretty\t\tpretty output\n");
62
  printf ("\t-a\t--access\t\toutput accesses only\n");
63
  printf ("\t-w\t--width\t\t\toutput by width\n");
64
  printf ("\t-f\t--filename filename\tspecify mprofile file [sim.mprofile]\n");
65
  printf ("\t-g\t--group bits\t\tgroup 2^bits successive\n");
66
  printf ("\t\t\t\t\taddresses together [2]\n");
67
  printf ("\t-h\t--help\t\t\toutput this screen\n");
68
}
69
 
70
void hash_add (unsigned long addr, int index)
71
{
72
  struct memory_hash *h = hash[HASH_FUNC(addr)];
73
  while (h && h->addr != addr) h = h->next;
74
 
75
  if (!h) {
76
    h = (struct memory_hash *)malloc (sizeof (struct memory_hash));
77
    h->next = hash[HASH_FUNC(addr)];
78
    hash[HASH_FUNC(addr)] = h;
79
    h->addr = addr;
80
    h->cnt[0] = h->cnt[1] = h->cnt[2] = 0;
81
  }
82
  h->cnt[index]++;
83
}
84
 
85
unsigned long hash_get (unsigned long addr, int index)
86
{
87
  struct memory_hash *h = hash[HASH_FUNC(addr)];
88
  while (h && h->addr != addr) h = h->next;
89
 
90
  if (!h) return 0;
91
  return h->cnt[index];
92
}
93
 
94
void init ()
95
{
96
  int i;
97
  for (i = 0; i < HASH_SIZE; i++)
98
    hash[i] = NULL;
99
}
100
 
101
void read_file (FILE *f, int mode)
102
{
103
  struct mprofentry_struct buf[BUF_SIZE];
104
  int num_read;
105
  do {
106
    int i;
107
    num_read = fread (buf, sizeof (struct mprofentry_struct), BUF_SIZE, f);
108
    for (i = 0; i < num_read; i++) if (buf[i].addr >= start_addr && buf[i].addr <= end_addr) {
109
      int index;
110
      if (mode != MODE_WIDTH) buf[i].type >>= 3;
111
      else buf[i].type &= 0x7;
112
 
113
      switch (buf[i].type) {
114
        case 1: index = 0; break;
115
        case 2: index = 1; break;
116
        case 4: index = 2; break;
117
      }
118
      hash_add (buf[i].addr >> group_bits, index);
119
    }
120
  } while (num_read > 0);
121
}
122
 
123
static inline int nbits (unsigned long a)
124
{
125
  int cnt = 0;
126
  int b = a;
127
  if (!a) return 0;
128
 
129
  while (a) a >>= 1, cnt++;
130
  if (cnt > 1 && ((b >> (cnt - 2)) & 1))
131
    cnt = cnt * 2 + 1;
132
  else
133
    cnt *= 2;
134
 
135
  return cnt - 1;
136
}
137
 
138
void printout (int mode)
139
{
140
  unsigned long addr = start_addr & ~((1 << group_bits) - 1);
141
  printf ("%08x %08x %08x %08x %i\n", start_addr, end_addr, addr, group_bits, mode);
142
  for (; addr <= end_addr; addr += (1 << group_bits)) {
143
    int i;
144
    unsigned long a = hash_get (addr >> group_bits, 0);
145
    unsigned long b = hash_get (addr >> group_bits, 1);
146
    unsigned long c = hash_get (addr >> group_bits, 2);
147
    printf ("%08x:", addr);
148
    switch (mode) {
149
      case MODE_DETAIL:
150
        if (a) printf (" %10i R", a);
151
        else printf ("            R");
152
        if (b) printf (" %10i W", b);
153
        else printf ("            W");
154
        if (c) printf (" %10i F", c);
155
        else printf ("            F");
156
        break;
157
      case MODE_ACCESS:
158
        printf (" %10i", a + b + c);
159
        break;
160
      case MODE_PRETTY:
161
        printf (" %10i ", a + b + c);
162
        for (i = 0; i < nbits (a + b + c); i++)
163
          printf ("#");
164
#if 0
165
        for (; i < 64; i++)
166
          printf (".");
167
#endif
168
        break;
169
      case MODE_WIDTH:
170
        if (a) printf (" %10i B", a);
171
        else printf ("            B");
172
        if (b) printf (" %10i H", b);
173
        else printf ("            H");
174
        if (c) printf (" %10i W", c);
175
        else printf ("            W");
176
        break;
177
    }
178
    printf ("\n");
179
  }
180
}
181
 
182
int main (int argc, char *argv[])
183
{
184
  char fmprofname[50] = "sim.mprofile";
185
  int param = 0;
186
  int mode = MODE_DETAIL;
187
 
188
  argv++; argc--;
189
  while (argc > 0) {
190
    if (!strcmp(argv[0], "-d") || !strcmp(argv[0], "--detail")) {
191
      mode = MODE_DETAIL;
192
      argv++; argc--;
193
    } else if (!strcmp(argv[0], "-p") || !strcmp(argv[0], "--pretty")) {
194
      mode = MODE_PRETTY;
195
      argv++; argc--;
196
    } else if (!strcmp(argv[0], "-a") || !strcmp(argv[0], "--access")) {
197
      mode = MODE_ACCESS;
198
      argv++; argc--;
199
    } else if (!strcmp(argv[0], "-w") || !strcmp(argv[0], "--width")) {
200
      mode = MODE_WIDTH;
201
      argv++; argc--;
202
    } else if (!strcmp(argv[0], "-g") || !strcmp(argv[0], "--group")) {
203
      argv++; argc--;
204
      group_bits = strtoul (argv[0], NULL, 0);
205
      argv++; argc--;
206
    } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
207
      help ();
208
      exit (0);
209
    } else if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--filename")) {
210
      argv++; argc--;
211
      strcpy (&fmprofname[0], argv[0]);
212
      argv++; argc--;
213
    } else {
214
      printf (": %s", argv[0]);
215
      switch (param) {
216
        case 0:
217
          start_addr = strtoul (argv[0], NULL, 0);
218
          break;
219
        case 1:
220
          end_addr = strtoul (argv[0], NULL, 0);
221
          break;
222
        default:
223
          fprintf (stderr, "Invalid number of parameters.\n");
224
          exit (-1);
225
      }
226
      argv++; argc--; param++;
227
    }
228
  }
229
 
230
  fprof = fopen (&fmprofname[0], "rm");
231
 
232
  if (!fprof) {
233
    fprintf (stderr, "Cannot open profile file: %s\n", &fmprofname[0]);
234
    exit(1);
235
  }
236
 
237
  init ();
238
  read_file (fprof, mode);
239
  fclose (fprof);
240
  printout (mode);
241
  return 0;
242
}

powered by: WebSVN 2.1.0

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