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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_40/] [or1ksim/] [profiler.c] - Blame information for rev 547

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

powered by: WebSVN 2.1.0

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