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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_1_x/] [or1ksim/] [profiler.c] - Blame information for rev 1777

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

powered by: WebSVN 2.1.0

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