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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_40/] [or1ksim/] [mprofiler.c] - Blame information for rev 1308

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

powered by: WebSVN 2.1.0

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