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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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