Skip to content

Commit 3653246

Browse files
committed
perf top: Ditch private annotation code, share perf annotate's
Next step: Live TUI annotation in perf top, just press enter on a symbol line. Cc: Frederic Weisbecker <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Tom Zanussi <[email protected]> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent f1e2701 commit 3653246

File tree

4 files changed

+106
-173
lines changed

4 files changed

+106
-173
lines changed

tools/perf/builtin-top.c

Lines changed: 31 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "perf.h"
2222

23+
#include "util/annotate.h"
2324
#include "util/cache.h"
2425
#include "util/color.h"
2526
#include "util/evlist.h"
@@ -140,10 +141,7 @@ static int parse_source(struct sym_entry *syme)
140141
struct symbol *sym;
141142
struct sym_entry_source *source;
142143
struct map *map;
143-
FILE *file;
144-
char command[PATH_MAX*2];
145-
const char *path;
146-
u64 len;
144+
int err = -1;
147145

148146
if (!syme)
149147
return -1;
@@ -162,197 +160,80 @@ static int parse_source(struct sym_entry *syme)
162160
if (syme->src == NULL)
163161
return -1;
164162
pthread_mutex_init(&syme->src->lock, NULL);
163+
INIT_LIST_HEAD(&syme->src->head);
165164
}
166165

167166
source = syme->src;
168167

169-
if (source->lines) {
168+
if (symbol__annotation(sym)->histograms != NULL) {
170169
pthread_mutex_lock(&source->lock);
171170
goto out_assign;
172171
}
173-
path = map->dso->long_name;
174-
175-
len = sym->end - sym->start;
176-
177-
sprintf(command,
178-
"objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s",
179-
BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
180-
BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
181-
182-
file = popen(command, "r");
183-
if (!file)
184-
return -1;
185172

186173
pthread_mutex_lock(&source->lock);
187-
source->lines_tail = &source->lines;
188-
while (!feof(file)) {
189-
struct source_line *src;
190-
size_t dummy = 0;
191-
char *c, *sep;
192-
193-
src = malloc(sizeof(struct source_line));
194-
assert(src != NULL);
195-
memset(src, 0, sizeof(struct source_line));
196-
197-
if (getline(&src->line, &dummy, file) < 0)
198-
break;
199-
if (!src->line)
200-
break;
201-
202-
c = strchr(src->line, '\n');
203-
if (c)
204-
*c = 0;
205174

206-
src->next = NULL;
207-
*source->lines_tail = src;
208-
source->lines_tail = &src->next;
209-
210-
src->eip = strtoull(src->line, &sep, 16);
211-
if (*sep == ':')
212-
src->eip = map__objdump_2ip(map, src->eip);
213-
else /* this line has no ip info (e.g. source line) */
214-
src->eip = 0;
175+
if (symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
176+
pr_err("Not enough memory for annotating '%s' symbol!\n",
177+
sym->name);
178+
goto out_unlock;
215179
}
216-
pclose(file);
180+
181+
err = symbol__annotate(sym, syme->map, &source->head, 0);
182+
if (err == 0) {
217183
out_assign:
218184
sym_filter_entry = syme;
185+
}
186+
out_unlock:
219187
pthread_mutex_unlock(&source->lock);
220-
return 0;
188+
return err;
221189
}
222190

223191
static void __zero_source_counters(struct sym_entry *syme)
224192
{
225-
int i;
226-
struct source_line *line;
227-
228-
line = syme->src->lines;
229-
while (line) {
230-
for (i = 0; i < top.evlist->nr_entries; i++)
231-
line->count[i] = 0;
232-
line = line->next;
233-
}
193+
struct symbol *sym = sym_entry__symbol(syme);
194+
symbol__annotate_zero_histograms(sym);
234195
}
235196

236197
static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
237198
{
238-
struct source_line *line;
239-
240199
if (syme != sym_filter_entry)
241200
return;
242201

243202
if (pthread_mutex_trylock(&syme->src->lock))
244203
return;
245204

246-
if (syme->src == NULL || syme->src->source == NULL)
247-
goto out_unlock;
248-
249-
for (line = syme->src->lines; line; line = line->next) {
250-
/* skip lines without IP info */
251-
if (line->eip == 0)
252-
continue;
253-
if (line->eip == ip) {
254-
line->count[counter]++;
255-
break;
256-
}
257-
if (line->eip > ip)
258-
break;
259-
}
260-
out_unlock:
261-
pthread_mutex_unlock(&syme->src->lock);
262-
}
263-
264-
#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
205+
ip = syme->map->map_ip(syme->map, ip);
206+
symbol__inc_addr_samples(sym_entry__symbol(syme), syme->map, counter, ip);
265207

266-
static void lookup_sym_source(struct sym_entry *syme)
267-
{
268-
struct symbol *symbol = sym_entry__symbol(syme);
269-
struct source_line *line;
270-
char pattern[PATTERN_LEN + 1];
271-
272-
sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4,
273-
map__rip_2objdump(syme->map, symbol->start));
274-
275-
pthread_mutex_lock(&syme->src->lock);
276-
for (line = syme->src->lines; line; line = line->next) {
277-
if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
278-
syme->src->source = line;
279-
break;
280-
}
281-
}
282208
pthread_mutex_unlock(&syme->src->lock);
283209
}
284210

285-
static void show_lines(struct source_line *queue, int count, int total)
286-
{
287-
int i;
288-
struct source_line *line;
289-
290-
line = queue;
291-
for (i = 0; i < count; i++) {
292-
float pcnt = 100.0*(float)line->count[top.sym_counter]/(float)total;
293-
294-
printf("%8li %4.1f%%\t%s\n", line->count[top.sym_counter], pcnt, line->line);
295-
line = line->next;
296-
}
297-
}
298-
299-
#define TRACE_COUNT 3
300-
301211
static void show_details(struct sym_entry *syme)
302212
{
303213
struct symbol *symbol;
304-
struct source_line *line;
305-
struct source_line *line_queue = NULL;
306-
int displayed = 0;
307-
int line_queue_count = 0, total = 0, more = 0;
214+
int more;
308215

309216
if (!syme)
310217
return;
311218

312-
if (!syme->src->source)
313-
lookup_sym_source(syme);
314-
315-
if (!syme->src->source)
219+
symbol = sym_entry__symbol(syme);
220+
if (!syme->src || symbol__annotation(symbol)->histograms == NULL)
316221
return;
317222

318-
symbol = sym_entry__symbol(syme);
319223
printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
320224
printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
321225

322226
pthread_mutex_lock(&syme->src->lock);
323-
line = syme->src->source;
324-
while (line) {
325-
total += line->count[top.sym_counter];
326-
line = line->next;
327-
}
328-
329-
line = syme->src->source;
330-
while (line) {
331-
float pcnt = 0.0;
332-
333-
if (!line_queue_count)
334-
line_queue = line;
335-
line_queue_count++;
336-
337-
if (line->count[top.sym_counter])
338-
pcnt = 100.0 * line->count[top.sym_counter] / (float)total;
339-
if (pcnt >= (float)sym_pcnt_filter) {
340-
if (displayed <= top.print_entries)
341-
show_lines(line_queue, line_queue_count, total);
342-
else more++;
343-
displayed += line_queue_count;
344-
line_queue_count = 0;
345-
line_queue = NULL;
346-
} else if (line_queue_count > TRACE_COUNT) {
347-
line_queue = line_queue->next;
348-
line_queue_count--;
349-
}
350-
351-
line->count[top.sym_counter] = top.zero ? 0 : line->count[top.sym_counter] * 7 / 8;
352-
line = line->next;
353-
}
227+
more = symbol__annotate_printf(symbol, syme->map, &syme->src->head,
228+
top.sym_evsel->idx, 0, sym_pcnt_filter,
229+
top.print_entries);
230+
if (top.zero)
231+
symbol__annotate_zero_histogram(symbol, top.sym_evsel->idx);
232+
else
233+
symbol__annotate_decay_histogram(symbol, &syme->src->head,
234+
top.sym_evsel->idx);
354235
pthread_mutex_unlock(&syme->src->lock);
355-
if (more)
236+
if (more != 0)
356237
printf("%d lines not displayed, maybe increase display entries [e]\n", more);
357238
}
358239

@@ -1172,7 +1053,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
11721053

11731054
top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
11741055

1175-
symbol_conf.priv_size = (sizeof(struct sym_entry) +
1056+
symbol_conf.priv_size = (sizeof(struct sym_entry) + sizeof(struct annotation) +
11761057
(top.evlist->nr_entries + 1) * sizeof(unsigned long));
11771058

11781059
symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);

tools/perf/util/annotate.c

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ int symbol__alloc_hist(struct symbol *sym, int nevents)
2222
notes->sizeof_sym_hist = (sizeof(*notes->histograms) +
2323
(sym->end - sym->start) * sizeof(u64));
2424
notes->histograms = calloc(nevents, notes->sizeof_sym_hist);
25+
notes->nr_histograms = nevents;
2526
return notes->histograms == NULL ? -1 : 0;
2627
}
2728

29+
void symbol__annotate_zero_histograms(struct symbol *sym)
30+
{
31+
struct annotation *notes = symbol__annotation(sym);
32+
33+
if (notes->histograms != NULL)
34+
memset(notes->histograms, 0,
35+
notes->nr_histograms * notes->sizeof_sym_hist);
36+
}
37+
2838
int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
2939
int evidx, u64 addr)
3040
{
@@ -85,9 +95,10 @@ struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
8595
return NULL;
8696
}
8797

88-
static void objdump_line__print(struct objdump_line *oline,
89-
struct list_head *head, struct symbol *sym,
90-
int evidx, u64 len, int min_pcnt)
98+
static int objdump_line__print(struct objdump_line *oline,
99+
struct list_head *head, struct symbol *sym,
100+
int evidx, u64 len, int min_pcnt,
101+
int printed, int max_lines)
91102
{
92103
static const char *prev_line;
93104
static const char *prev_color;
@@ -119,7 +130,10 @@ static void objdump_line__print(struct objdump_line *oline,
119130
percent = 100.0 * hits / h->sum;
120131

121132
if (percent < min_pcnt)
122-
return;
133+
return -1;
134+
135+
if (printed >= max_lines)
136+
return 1;
123137

124138
color = get_percent_color(percent);
125139

@@ -140,12 +154,16 @@ static void objdump_line__print(struct objdump_line *oline,
140154
color_fprintf(stdout, color, " %7.2f", percent);
141155
printf(" : ");
142156
color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
143-
} else {
157+
} else if (printed >= max_lines)
158+
return 1;
159+
else {
144160
if (!*oline->line)
145161
printf(" :\n");
146162
else
147163
printf(" : %s\n", oline->line);
148164
}
165+
166+
return 0;
149167
}
150168

151169
static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file,
@@ -421,14 +439,15 @@ static void symbol__annotate_hits(struct symbol *sym, int evidx)
421439
printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
422440
}
423441

424-
void symbol__annotate_printf(struct symbol *sym, struct map *map,
425-
struct list_head *head, int evidx, bool full_paths,
426-
int min_pcnt, int max_lines)
442+
int symbol__annotate_printf(struct symbol *sym, struct map *map,
443+
struct list_head *head, int evidx, bool full_paths,
444+
int min_pcnt, int max_lines)
427445
{
428446
struct dso *dso = map->dso;
429447
const char *filename = dso->long_name, *d_filename;
430448
struct objdump_line *pos;
431449
int printed = 2;
450+
int more = 0;
432451
u64 len;
433452

434453
if (full_paths)
@@ -445,10 +464,47 @@ void symbol__annotate_printf(struct symbol *sym, struct map *map,
445464
symbol__annotate_hits(sym, evidx);
446465

447466
list_for_each_entry(pos, head, node) {
448-
objdump_line__print(pos, head, sym, evidx, len, min_pcnt);
449-
if (max_lines && ++printed >= max_lines)
467+
switch (objdump_line__print(pos, head, sym, evidx, len, min_pcnt,
468+
printed, max_lines)) {
469+
case 0:
470+
++printed;
471+
break;
472+
case 1:
473+
/* filtered by max_lines */
474+
++more;
450475
break;
476+
case -1:
477+
default:
478+
/* filtered by min_pcnt */
479+
break;
480+
}
481+
}
482+
483+
return more;
484+
}
451485

486+
void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
487+
{
488+
struct annotation *notes = symbol__annotation(sym);
489+
struct sym_hist *h = annotation__histogram(notes, evidx);
490+
491+
memset(h, 0, notes->sizeof_sym_hist);
492+
}
493+
494+
void symbol__annotate_decay_histogram(struct symbol *sym,
495+
struct list_head *head, int evidx)
496+
{
497+
struct annotation *notes = symbol__annotation(sym);
498+
struct sym_hist *h = annotation__histogram(notes, evidx);
499+
struct objdump_line *pos;
500+
501+
h->sum = 0;
502+
503+
list_for_each_entry(pos, head, node) {
504+
if (pos->offset != -1) {
505+
h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
506+
h->sum += h->addr[pos->offset];
507+
}
452508
}
453509
}
454510

0 commit comments

Comments
 (0)