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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [profiler.c] - Blame information for rev 264

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 173 markom
/* Command line utility, that displays profiling information, generated
2
   by or1ksim. (use -profile option at command line, when running or1ksim.
3
   GNU License (C) Marko Mlinar 2001.  */
4
 
5
#include <stdio.h>
6
 
7
#define MAX_STACK 1024
8
#define MAX_FUNCS 1024
9
 
10
struct stack_struct {
11
  unsigned int cycles;
12
  unsigned int raddr;
13
  unsigned int addr;
14
  char name[33];
15
} stack[MAX_STACK];
16
 
17
struct func_struct {
18
  unsigned int addr;
19
  char name[33];
20
  long cum_cycles;
21
  long calls;
22
} func[MAX_FUNCS];
23
 
24
int nfuncs = 0;
25
int nstack = 0;
26
int maxstack = 0, cycles = 0;
27
int cumulative = 0;
28
 
29
FILE *fprof = 0;
30
 
31
int main (int argc, char *argv[]) {
32 264 markom
  char fprofname[50] = "sim.profile";
33 173 markom
  int line = 0;
34
  if (argc > 4 || argc < 2) {
35
    fprintf (stderr, "USAGE: profiler [--cumulative|-c] --generate|-g [profile_file_name]\n");
36
    exit(1);
37
  }
38
  argv++; argc--;
39
  while (argc > 0) {
40
    if (!strcmp(argv[0], "-c") || !strcmp(argv[0], "--cumulative")) {
41
      cumulative = 1;
42
      argv++; argc--;
43
    } else if (strcmp(argv[0], "-g") && strcmp(argv[0], "--generate")) {
44
      fprintf (stderr, "USAGE: profiler [--cumulative|-c] [--generate|-g] [profile_file_name]\n");
45
      exit(1);
46
    } else {
47
      argv++; argc--;
48
      if (argv[0] && argv[0][0] != '-') {
49
        strcpy (&fprofname[0], argv[0]);
50 239 markom
        argv++; argc--;
51 173 markom
      }
52
    }
53
  }
54
 
55
  fprof = fopen (&fprofname[0], "rt");
56
 
57
  if (!fprof) {
58
    fprintf (stderr, "Cannot open profile file: %s\n", &fprofname[0]);
59
    exit(1);
60
  }
61
 
62
  while (1) {
63
    char dir = fgetc (fprof);
64
    line++;
65
    if (dir == '+') {
66
      if (fscanf (fprof, "%08X %08X %08X %s\n", &stack[nstack].cycles, &stack[nstack].raddr,
67 239 markom
                  &stack[nstack].addr, &stack[nstack].name[0]) != 4)
68
        fprintf (stderr, "Error reading line #%i\n", line);
69 173 markom
      else {
70 239 markom
              cycles = stack[nstack].cycles;
71
              nstack++;
72
              if (nstack > maxstack)
73
                maxstack = nstack;
74
        }
75
      } else if (dir == '-') {
76 173 markom
      struct stack_struct s;
77
      if (fscanf (fprof, "%08X %08X\n", &s.cycles, &s.raddr) != 2)
78 239 markom
        fprintf (stderr, "Error reading line #%i\n", line);
79 173 markom
      else {
80 239 markom
        int i;
81
        cycles = s.cycles;
82
        for (i = nstack - 1; i >= 0; i--)
83
          if (stack[i].raddr == s.raddr) break;
84
        if (i >= 0) {
85
          int j;
86
          long time = s.cycles - stack[nstack - 1].cycles;
87 173 markom
 
88 239 markom
          /* Whether in normal mode, we must substract called function from execution time.  */
89
          if (!cumulative)
90
            for (j = 0; j < nstack - 1; j++)
91
              stack[j].cycles += time;
92 173 markom
 
93 239 markom
          /* pop everything above from current from stack,
94
             if more than one, something went wrong */
95
          while (i < nstack) {
96
            nstack--;
97
            if (i != nstack)
98
              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);
99
            for (j = 0; j < nfuncs; j++)
100
              if (stack[nstack].addr == func[j].addr) { /* function exists, append. */
101
                func[j].cum_cycles += s.cycles - stack[nstack].cycles;
102
                func[j].calls++;
103
                break;
104
              }
105
            if (j >= nfuncs) { /* function does not yet exist, create new. */
106
              func[nfuncs].cum_cycles = s.cycles - stack[nstack].cycles;
107
              func[nfuncs].calls = 1;
108
              func[nfuncs].addr = stack[nstack].addr;
109
              strcpy (func[nfuncs].name, stack[nstack].name);
110
              nfuncs++;
111
            }
112
          }
113
        } else fprintf (stderr, "WARNING: Cannot find return call for (%08X), ignoring.\n", s.raddr);
114 173 markom
      }
115
    } else
116
      break;
117
  }
118
  fclose(fprof);
119
 
120
  /* Now we have all data acquired. Print out. */
121
  {
122
    int i, j;
123
    if (cumulative)
124
      printf ("CUMULATIVE TIMES\n");
125
    printf ("---------------------------------------------------------------------------\n");
126
    printf ("|function name            |addr    |# calls |avg cycles  |total cyles     |\n");
127
    printf ("|-------------------------+--------+--------+------------+----------------|\n");
128
    for (j = 0; j < nfuncs; j++) {
129
      int bestcyc = 0, besti = 0;
130
      for (i = 0; i < nfuncs; i++)
131 239 markom
        if (func[i].cum_cycles > bestcyc) {
132
          bestcyc = func[i].cum_cycles;
133
          besti = i;
134
        }
135 173 markom
      i = besti;
136
      printf ("| %-24s|%08X|%8i|%12.1f|%11i,%3.0f%%|\n",
137 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));
138 173 markom
      func[i].cum_cycles = -1;
139
    }
140
    printf ("---------------------------------------------------------------------------\n");
141
  }
142
  printf("Total %i functions (max depth %i), %i cycles.\n", nfuncs, maxstack, cycles);
143
}

powered by: WebSVN 2.1.0

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