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 848

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

powered by: WebSVN 2.1.0

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