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

Subversion Repositories or1k_old

[/] [or1k_old/] [branches/] [stable_0_1_x/] [or1ksim/] [mprofiler.c] - Blame information for rev 1782

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

powered by: WebSVN 2.1.0

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