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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [mprofiler.c] - Blame information for rev 632

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

powered by: WebSVN 2.1.0

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