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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_70/] [or1ksim/] [profiler.c] - Blame information for rev 879

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

powered by: WebSVN 2.1.0

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