1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "trace_reader.h"
#include "parse_options.h"
#include "opcode.h"
const int kMillion = 1000000;
const int kMHz = 200 * kMillion;
struct symbol {
int numCalls; // number of times this function is called
};
typedef TraceReader<symbol> TraceReaderType;
#include "parse_options-inl.h"
#include "callstack.h"
class MyFrame : public StackFrame<symbol_type> {
public:
void push(int stackLevel, uint64_t time, CallStackBase *base) {
function->numCalls += 1;
}
void pop(int stackLevel, uint64_t time, CallStackBase *base) {
}
};
typedef CallStack<MyFrame> CallStackType;
static const int kNumStackFrames = 500;
static const int kMaxThreads = (32 * 1024);
CallStackType *stacks[kMaxThreads];
// This comparison function is called from qsort() to sort symbols
// into decreasing number of calls.
int cmp_sym_calls(const void *a, const void *b) {
const symbol_type *syma, *symb;
uint64_t calls1, calls2;
syma = static_cast<symbol_type const *>(a);
symb = static_cast<symbol_type const *>(b);
calls1 = syma->numCalls;
calls2 = symb->numCalls;
if (calls1 < calls2)
return 1;
if (calls1 == calls2) {
int cmp = strcmp(syma->name, symb->name);
if (cmp == 0)
cmp = strcmp(syma->region->path, symb->region->path);
return cmp;
}
return -1;
}
// This comparison function is called from qsort() to sort symbols
// into alphabetical order.
int cmp_sym_names(const void *a, const void *b) {
const symbol_type *syma, *symb;
syma = static_cast<symbol_type const *>(a);
symb = static_cast<symbol_type const *>(b);
int cmp = strcmp(syma->region->path, symb->region->path);
if (cmp == 0)
cmp = strcmp(syma->name, symb->name);
return cmp;
}
void Usage(const char *program)
{
fprintf(stderr, "Usage: %s [options] trace_file elf_file\n", program);
OptionsUsage();
}
int main(int argc, char **argv)
{
ParseOptions(argc, argv);
if (argc - optind != 2) {
Usage(argv[0]);
exit(1);
}
char *trace_filename = argv[optind++];
char *elf_file = argv[optind++];
TraceReader<symbol> *trace = new TraceReader<symbol>;
trace->Open(trace_filename);
trace->SetDemangle(demangle);
trace->ReadKernelSymbols(elf_file);
trace->SetRoot(root);
BBEvent event;
while (1) {
BBEvent ignored;
symbol_type *function;
if (GetNextValidEvent(trace, &event, &ignored, &function))
break;
if (event.bb_num == 0)
break;
// Get the stack for the current thread
CallStackType *pStack = stacks[event.pid];
// If the stack does not exist, then allocate a new one.
if (pStack == NULL) {
pStack = new CallStackType(event.pid, kNumStackFrames, trace);
stacks[event.pid] = pStack;
}
// Update the stack
pStack->updateStack(&event, function);
}
for (int ii = 0; ii < kMaxThreads; ++ii) {
if (stacks[ii])
stacks[ii]->popAll(event.time);
}
int nsyms;
symbol_type *syms = trace->GetSymbols(&nsyms);
// Sort the symbols into decreasing number of calls
qsort(syms, nsyms, sizeof(symbol_type), cmp_sym_names);
symbol_type *psym = syms;
for (int ii = 0; ii < nsyms; ++ii, ++psym) {
// Ignore functions with non-zero calls
if (psym->numCalls)
continue;
// Ignore some symbols
if (strcmp(psym->name, "(end)") == 0)
continue;
if (strcmp(psym->name, "(unknown)") == 0)
continue;
if (strcmp(psym->name, ".plt") == 0)
continue;
char *ksym = " ";
if (psym->region->flags & region_type::kIsKernelRegion)
ksym = "k";
printf("%s %s %s\n", ksym, psym->name, psym->region->path);
#if 0
printf("#%d %5d %s %s %s\n", ii + 1, psym->numCalls, ksym, psym->name,
psym->region->path);
#endif
}
delete[] syms;
delete trace;
return 0;
}
|