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 632

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 173 markom
 
26
struct stack_struct {
27 533 markom
  /* Function address */
28
  unsigned int addr;
29
 
30
  /* Cycles of function start; cycles of subfunctions are added later */
31 173 markom
  unsigned int cycles;
32 533 markom
 
33
  /* Return address */
34 173 markom
  unsigned int raddr;
35 533 markom
 
36
  /* Name of the function */
37 173 markom
  char name[33];
38
} stack[MAX_STACK];
39
 
40
struct func_struct {
41 533 markom
  /* Start address of function */
42 173 markom
  unsigned int addr;
43 533 markom
 
44
  /* Name of the function */
45 173 markom
  char name[33];
46 533 markom
 
47
  /* Total cycles spent in function */
48 173 markom
  long cum_cycles;
49 533 markom
 
50
  /* Calls to this function */
51 173 markom
  long calls;
52
} func[MAX_FUNCS];
53
 
54 533 markom
/* Total number of functions */
55 173 markom
int nfuncs = 0;
56 533 markom
 
57
/* Current depth */
58 173 markom
int nstack = 0;
59
 
60 533 markom
/* Max depth */
61
int maxstack = 0;
62 173 markom
 
63 533 markom
/* Number of total calls */
64
int ntotcalls = 0;
65
 
66
/* Number of covered calls */
67
int nfunccalls = 0;
68
 
69
/* Current cycles */
70
static int cycles = 0;
71
 
72
/* Whether we are in cumulative mode */
73
static int cumulative = 0;
74
 
75
/* Whether we should not report warnings */
76
static int quiet = 0;
77
 
78
/* File to read from */
79
static FILE *fprof = 0;
80
 
81 632 ivang
int main_profile (int mode, char *fname) {
82 264 markom
  char fprofname[50] = "sim.profile";
83 173 markom
  int line = 0;
84 632 ivang
 
85
  /*
86 173 markom
  if (argc > 4 || argc < 2) {
87 533 markom
    fprintf (stderr, "USAGE: profiler [--cumulative|-c] [--quiet|-q] --generate|-g [profile_file_name]\n");
88 173 markom
    exit(1);
89
  }
90 632 ivang
  */
91
 
92
  if (mode && PROF_CUMULATIVE) cumulative = 1;
93
  if (mode && PROF_QUIET)      quiet = 1;
94
  if (!fname)                  fname = fprofname;
95
 
96
  /*
97 173 markom
  argv++; argc--;
98
  while (argc > 0) {
99 533 markom
    if (!strcmp(argv[0], "-q") || !strcmp(argv[0], "--quiet")) {
100
      quiet = 1;
101
      argv++; argc--;
102
    } else if (!strcmp(argv[0], "-c") || !strcmp(argv[0], "--cumulative")) {
103 173 markom
      cumulative = 1;
104
      argv++; argc--;
105
    } else if (strcmp(argv[0], "-g") && strcmp(argv[0], "--generate")) {
106
      fprintf (stderr, "USAGE: profiler [--cumulative|-c] [--generate|-g] [profile_file_name]\n");
107
      exit(1);
108
    } else {
109
      argv++; argc--;
110
      if (argv[0] && argv[0][0] != '-') {
111
        strcpy (&fprofname[0], argv[0]);
112 239 markom
        argv++; argc--;
113 173 markom
      }
114
    }
115
  }
116 632 ivang
  */
117 173 markom
 
118 632 ivang
  fprof = fopen (fname, "rt");
119 173 markom
 
120
  if (!fprof) {
121 632 ivang
    fprintf (stderr, "Cannot open profile file: %s\n", fname);
122 173 markom
    exit(1);
123
  }
124
 
125
  while (1) {
126
    char dir = fgetc (fprof);
127
    line++;
128
    if (dir == '+') {
129
      if (fscanf (fprof, "%08X %08X %08X %s\n", &stack[nstack].cycles, &stack[nstack].raddr,
130 239 markom
                  &stack[nstack].addr, &stack[nstack].name[0]) != 4)
131
        fprintf (stderr, "Error reading line #%i\n", line);
132 173 markom
      else {
133 533 markom
        cycles = stack[nstack].cycles;
134
        nstack++;
135
        if (nstack > maxstack)
136
          maxstack = nstack;
137
      }
138
      ntotcalls++;
139
    } else if (dir == '-') {
140 173 markom
      struct stack_struct s;
141
      if (fscanf (fprof, "%08X %08X\n", &s.cycles, &s.raddr) != 2)
142 239 markom
        fprintf (stderr, "Error reading line #%i\n", line);
143 173 markom
      else {
144 239 markom
        int i;
145
        cycles = s.cycles;
146
        for (i = nstack - 1; i >= 0; i--)
147
          if (stack[i].raddr == s.raddr) break;
148
        if (i >= 0) {
149 533 markom
          /* pop everything above current from stack,
150 239 markom
             if more than one, something went wrong */
151 533 markom
          while (nstack > i) {
152
            int j;
153
            long time;
154 239 markom
            nstack--;
155 533 markom
            time = s.cycles - stack[nstack].cycles;
156
            if (!quiet && time < 0) {
157
              fprintf (stderr, "WARNING: Negative time at %s (return addr = %08X).\n", stack[i].name, stack[i].raddr);
158
              time = 0;
159
            }
160
 
161
            /* Whether in normal mode, we must substract called function from execution time.  */
162
            if (!cumulative)
163
              for (j = 0; j < nstack; j++)
164
                stack[j].cycles += time;
165
 
166
            if (!quiet && i != nstack)
167 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);
168 533 markom
 
169 239 markom
            for (j = 0; j < nfuncs; j++)
170
              if (stack[nstack].addr == func[j].addr) { /* function exists, append. */
171 533 markom
                func[j].cum_cycles += time;
172 239 markom
                func[j].calls++;
173 533 markom
                nfunccalls++;
174 239 markom
                break;
175
              }
176
            if (j >= nfuncs) { /* function does not yet exist, create new. */
177 533 markom
              func[nfuncs].cum_cycles = time;
178 239 markom
              func[nfuncs].calls = 1;
179 533 markom
              nfunccalls++;
180 239 markom
              func[nfuncs].addr = stack[nstack].addr;
181
              strcpy (func[nfuncs].name, stack[nstack].name);
182
              nfuncs++;
183
            }
184
          }
185 533 markom
        } else if (!quiet) fprintf (stderr, "WARNING: Cannot find return call for (%08X), ignoring.\n", s.raddr);
186 173 markom
      }
187
    } else
188
      break;
189
  }
190
  fclose(fprof);
191
 
192
  /* Now we have all data acquired. Print out. */
193
  {
194
    int i, j;
195
    if (cumulative)
196
      printf ("CUMULATIVE TIMES\n");
197
    printf ("---------------------------------------------------------------------------\n");
198
    printf ("|function name            |addr    |# calls |avg cycles  |total cyles     |\n");
199
    printf ("|-------------------------+--------+--------+------------+----------------|\n");
200
    for (j = 0; j < nfuncs; j++) {
201
      int bestcyc = 0, besti = 0;
202
      for (i = 0; i < nfuncs; i++)
203 239 markom
        if (func[i].cum_cycles > bestcyc) {
204
          bestcyc = func[i].cum_cycles;
205
          besti = i;
206
        }
207 173 markom
      i = besti;
208
      printf ("| %-24s|%08X|%8i|%12.1f|%11i,%3.0f%%|\n",
209 239 markom
              func[i].name, func[i].addr, func[i].calls, ((double)func[i].cum_cycles / func[i].calls), func[i].cum_cycles, (100. * func[i].cum_cycles / cycles));
210 173 markom
      func[i].cum_cycles = -1;
211
    }
212
    printf ("---------------------------------------------------------------------------\n");
213
  }
214 533 markom
  printf ("Total %i functions, %i cycles.\n", nfuncs, cycles);
215
  printf ("Total function calls %i/%i (max depth %i).\n", nfunccalls, ntotcalls, maxstack);
216
  return 0;
217 173 markom
}

powered by: WebSVN 2.1.0

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