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 |
|
|
char fprofname[50] = "sim-profile";
|
33 |
|
|
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 |
|
|
argv++; argc--;
|
51 |
|
|
}
|
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 |
|
|
&stack[nstack].addr, &stack[nstack].name[0]) != 4)
|
68 |
|
|
fprintf (stderr, "Error reading line #%i\n", line);
|
69 |
|
|
else {
|
70 |
|
|
cycles = stack[nstack].cycles;
|
71 |
|
|
nstack++;
|
72 |
|
|
if (nstack > maxstack)
|
73 |
|
|
maxstack = nstack;
|
74 |
|
|
}
|
75 |
|
|
} else if (dir == '-') {
|
76 |
|
|
struct stack_struct s;
|
77 |
|
|
if (fscanf (fprof, "%08X %08X\n", &s.cycles, &s.raddr) != 2)
|
78 |
|
|
fprintf (stderr, "Error reading line #%i\n", line);
|
79 |
|
|
else {
|
80 |
|
|
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 |
|
|
|
88 |
|
|
/* 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 |
|
|
|
93 |
|
|
/* 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 |
|
|
}
|
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 |
|
|
if (func[i].cum_cycles > bestcyc) {
|
132 |
|
|
bestcyc = func[i].cum_cycles;
|
133 |
|
|
besti = i;
|
134 |
|
|
}
|
135 |
|
|
i = besti;
|
136 |
|
|
printf ("| %-24s|%08X|%8i|%12.1f|%11i,%3.0f%%|\n",
|
137 |
|
|
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 |
|
|
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 |
|
|
}
|