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 997

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

powered by: WebSVN 2.1.0

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