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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [profiler.c] - Blame information for rev 1778

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 547 markom
/* profiler.c -- profiling utility
2
   Copyright (C) 2001 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 173 markom
/* Command line utility, that displays profiling information, generated
21 547 markom
   by or1ksim. (use --profile option at command line, when running or1ksim.  */
22 173 markom
 
23
#include <stdio.h>
24 1308 phoenix
#include <string.h>
25
 
26 1358 nogj
#include "config.h"
27
 
28
#ifdef HAVE_INTTYPES_H
29
#include <inttypes.h>
30
#endif
31
 
32
#include "port.h"
33
#include "arch.h"
34 632 ivang
#include "profiler.h"
35 940 markom
#include "sim-config.h"
36 173 markom
 
37 879 markom
static struct stack_struct stack[MAX_STACK];
38 173 markom
 
39 879 markom
struct func_struct prof_func[MAX_FUNCS];
40 173 markom
 
41 533 markom
/* Total number of functions */
42 879 markom
int prof_nfuncs = 0;
43 533 markom
 
44
/* Current depth */
45 879 markom
static int nstack = 0;
46 173 markom
 
47 533 markom
/* Max depth */
48 879 markom
static int maxstack = 0;
49 173 markom
 
50 533 markom
/* Number of total calls */
51 879 markom
static int ntotcalls = 0;
52 533 markom
 
53
/* Number of covered calls */
54 879 markom
static int nfunccalls = 0;
55 533 markom
 
56
/* Current cycles */
57 879 markom
int prof_cycles = 0;
58 533 markom
 
59
/* Whether we are in cumulative mode */
60
static int cumulative = 0;
61
 
62
/* Whether we should not report warnings */
63
static int quiet = 0;
64
 
65 879 markom
/* File to read from */
66
static FILE *fprof = 0;
67
 
68
/* Print out command line help */
69 847 markom
void prof_help ()
70
{
71 997 markom
  PRINTF ("profiler [-c] [-q] -g [profile_file_name]\n");
72
  PRINTF ("\t-c\t--cumulative\t\tcumulative sum of cycles in functions\n");
73
  PRINTF ("\t-q\t--quiet\t\t\tsuppress messages\n");
74
  PRINTF ("\t-g\t--generate [profile_file_name]\n");
75
  PRINTF ("\t\t\t\t\toutput profiling results to\n");
76
  PRINTF ("\t\t\t\t\tstdout/profile_file_name\n");
77 847 markom
}
78
 
79 879 markom
/* Acquire data from profiler file */
80
int prof_acquire (char *fprofname)
81
{
82 173 markom
  int line = 0;
83 940 markom
  int reopened = 0;
84
 
85
  if (runtime.sim.fprof) {
86
    fprof = runtime.sim.fprof;
87
    reopened = 1;
88
    rewind (fprof);
89
  } else fprof = fopen (fprofname, "rt");
90
 
91 173 markom
  if (!fprof) {
92 847 markom
    fprintf (stderr, "Cannot open profile file: %s\n", fprofname);
93
    return 1;
94 173 markom
  }
95
 
96
  while (1) {
97
    char dir = fgetc (fprof);
98
    line++;
99
    if (dir == '+') {
100
      if (fscanf (fprof, "%08X %08X %08X %s\n", &stack[nstack].cycles, &stack[nstack].raddr,
101 239 markom
                  &stack[nstack].addr, &stack[nstack].name[0]) != 4)
102
        fprintf (stderr, "Error reading line #%i\n", line);
103 173 markom
      else {
104 879 markom
        prof_cycles = stack[nstack].cycles;
105 533 markom
        nstack++;
106
        if (nstack > maxstack)
107
          maxstack = nstack;
108
      }
109
      ntotcalls++;
110
    } else if (dir == '-') {
111 173 markom
      struct stack_struct s;
112
      if (fscanf (fprof, "%08X %08X\n", &s.cycles, &s.raddr) != 2)
113 239 markom
        fprintf (stderr, "Error reading line #%i\n", line);
114 173 markom
      else {
115 239 markom
        int i;
116 879 markom
        prof_cycles = s.cycles;
117 239 markom
        for (i = nstack - 1; i >= 0; i--)
118
          if (stack[i].raddr == s.raddr) break;
119
        if (i >= 0) {
120 533 markom
          /* pop everything above current from stack,
121 239 markom
             if more than one, something went wrong */
122 533 markom
          while (nstack > i) {
123
            int j;
124
            long time;
125 239 markom
            nstack--;
126 533 markom
            time = s.cycles - stack[nstack].cycles;
127
            if (!quiet && time < 0) {
128
              fprintf (stderr, "WARNING: Negative time at %s (return addr = %08X).\n", stack[i].name, stack[i].raddr);
129
              time = 0;
130
            }
131
 
132
            /* Whether in normal mode, we must substract called function from execution time.  */
133
            if (!cumulative)
134
              for (j = 0; j < nstack; j++)
135
                stack[j].cycles += time;
136
 
137
            if (!quiet && i != nstack)
138 239 markom
              fprintf (stderr, "WARNING: Missaligned return call for %s (%08X) (found %s @ %08X), closing.\n", stack[nstack].name, stack[nstack].raddr, stack[i].name, stack[i].raddr);
139 533 markom
 
140 879 markom
            for (j = 0; j < prof_nfuncs; j++)
141
              if (stack[nstack].addr == prof_func[j].addr) { /* function exists, append. */
142
                prof_func[j].cum_cycles += time;
143
                prof_func[j].calls++;
144 533 markom
                nfunccalls++;
145 239 markom
                break;
146
              }
147 879 markom
            if (j >= prof_nfuncs) { /* function does not yet exist, create new. */
148
              prof_func[prof_nfuncs].cum_cycles = time;
149
              prof_func[prof_nfuncs].calls = 1;
150 533 markom
              nfunccalls++;
151 879 markom
              prof_func[prof_nfuncs].addr = stack[nstack].addr;
152
              strcpy (prof_func[prof_nfuncs].name, stack[nstack].name);
153
              prof_nfuncs++;
154 239 markom
            }
155
          }
156 533 markom
        } else if (!quiet) fprintf (stderr, "WARNING: Cannot find return call for (%08X), ignoring.\n", s.raddr);
157 173 markom
      }
158
    } else
159
      break;
160
  }
161 940 markom
 
162
  /* If we have reopened the file, we need to add end of "[outside functions]" */
163
  if (reopened) {
164
    prof_cycles = runtime.sim.cycles;
165
    /* pop everything above current from stack,
166
       if more than one, something went wrong */
167
    while (nstack > 0) {
168
      int j;
169
      long time;
170
      nstack--;
171
      time = runtime.sim.cycles - stack[nstack].cycles;
172
      /* Whether in normal mode, we must substract called function from execution time.  */
173
      if (!cumulative)
174
        for (j = 0; j < nstack; j++) stack[j].cycles += time;
175
 
176
      for (j = 0; j < prof_nfuncs; j++)
177
        if (stack[nstack].addr == prof_func[j].addr) { /* function exists, append. */
178
          prof_func[j].cum_cycles += time;
179
          prof_func[j].calls++;
180
          nfunccalls++;
181
          break;
182
        }
183
      if (j >= prof_nfuncs) { /* function does not yet exist, create new. */
184
        prof_func[prof_nfuncs].cum_cycles = time;
185
        prof_func[prof_nfuncs].calls = 1;
186
        nfunccalls++;
187
        prof_func[prof_nfuncs].addr = stack[nstack].addr;
188
        strcpy (prof_func[prof_nfuncs].name, stack[nstack].name);
189
        prof_nfuncs++;
190
      }
191
    }
192
  } else fclose(fprof);
193
  return 0;
194 879 markom
}
195 173 markom
 
196 879 markom
/* Print out profiling data */
197
void prof_print ()
198
{
199
  int i, j;
200
  if (cumulative)
201 997 markom
    PRINTF ("CUMULATIVE TIMES\n");
202
  PRINTF ("---------------------------------------------------------------------------\n");
203
  PRINTF ("|function name            |addr    |# calls |avg cycles  |total cyles     |\n");
204
  PRINTF ("|-------------------------+--------+--------+------------+----------------|\n");
205 879 markom
  for (j = 0; j < prof_nfuncs; j++) {
206
    int bestcyc = 0, besti = 0;
207
    for (i = 0; i < prof_nfuncs; i++)
208
      if (prof_func[i].cum_cycles > bestcyc) {
209
        bestcyc = prof_func[i].cum_cycles;
210
        besti = i;
211
      }
212
    i = besti;
213 1308 phoenix
    PRINTF ("| %-24s|%08X|%8li|%12.1f|%11li,%3.0f%%|\n",
214 879 markom
            prof_func[i].name, prof_func[i].addr, prof_func[i].calls, ((double)prof_func[i].cum_cycles / prof_func[i].calls), prof_func[i].cum_cycles, (100. * prof_func[i].cum_cycles / prof_cycles));
215
    prof_func[i].cum_cycles = -1;
216
  }
217 997 markom
  PRINTF ("---------------------------------------------------------------------------\n");
218
  PRINTF ("Total %i functions, %i cycles.\n", prof_nfuncs, prof_cycles);
219
  PRINTF ("Total function calls %i/%i (max depth %i).\n", nfunccalls, ntotcalls, maxstack);
220 879 markom
}
221
 
222
/* Set options */
223
void prof_set (int _quiet, int _cumulative)
224
{
225
  quiet = _quiet;
226
  cumulative = _cumulative;
227
}
228
 
229
int main_profiler (int argc, char *argv[]) {
230
  char fprofname[50] = "sim.profile";
231
 
232
  if (argc > 4 || argc < 2) {
233
    prof_help ();
234
    return 1;
235
  }
236
 
237
  argv++; argc--;
238
  while (argc > 0) {
239
    if (!strcmp(argv[0], "-q") || !strcmp(argv[0], "--quiet")) {
240
      quiet = 1;
241
      argv++; argc--;
242
    } else if (!strcmp(argv[0], "-c") || !strcmp(argv[0], "--cumulative")) {
243
      cumulative = 1;
244
      argv++; argc--;
245
    } else if (strcmp(argv[0], "-g") && strcmp(argv[0], "--generate")) {
246
      prof_help ();
247
      return -1;
248
    } else {
249
      argv++; argc--;
250
      if (argv[0] && argv[0][0] != '-') {
251
        strcpy (&fprofname[0], argv[0]);
252
        argv++; argc--;
253
      }
254 173 markom
    }
255
  }
256 879 markom
 
257
  prof_acquire (fprofname);
258
 
259
  /* Now we have all data acquired. Print out. */
260
  prof_print ();
261 533 markom
  return 0;
262 173 markom
}

powered by: WebSVN 2.1.0

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