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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_69/] [or1ksim/] [mprofiler.c] - Blame information for rev 997

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

powered by: WebSVN 2.1.0

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