Skip to content

Commit b538752

Browse files
Roberto Agostino VitilloIngo Molnar
authored andcommitted
perf tools: Add code to support PERF_SAMPLE_BRANCH_STACK
This patch adds: - ability to parse samples with PERF_SAMPLE_BRANCH_STACK - sort on branches (dso_from, symbol_from, dso_to, symbol_to, mispredict) - build histograms on branches Signed-off-by: Roberto Agostino Vitillo <ravitillo@lbl.gov> Signed-off-by: Stephane Eranian <eranian@google.com> Cc: peterz@infradead.org Cc: acme@redhat.com Cc: robert.richter@amd.com Cc: ming.m.lin@intel.com Cc: andi@firstfloor.org Cc: asharma@fb.com Cc: vweaver1@eecs.utk.edu Cc: khandual@linux.vnet.ibm.com Cc: dsahern@gmail.com Link: http://lkml.kernel.org/r/1328826068-11713-12-git-send-email-eranian@google.com Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent d010b33 commit b538752

10 files changed

Lines changed: 468 additions & 78 deletions

File tree

‎tools/perf/perf.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ struct ip_callchain {
179179
u64 ips[0];
180180
};
181181

182+
struct branch_flags {
183+
u64 mispred:1;
184+
u64 predicted:1;
185+
u64 reserved:62;
186+
};
187+
188+
struct branch_entry {
189+
u64 from;
190+
u64 to;
191+
struct branch_flags flags;
192+
};
193+
194+
struct branch_stack {
195+
u64 nr;
196+
struct branch_entry entries[0];
197+
};
198+
182199
extern bool perf_host, perf_guest;
183200
extern const char perf_version_string[];
184201

‎tools/perf/util/event.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ struct perf_sample {
8181
u32 raw_size;
8282
void *raw_data;
8383
struct ip_callchain *callchain;
84+
struct branch_stack *branch_stack;
8485
};
8586

8687
#define BUILD_ID_SIZE 20

‎tools/perf/util/evsel.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,16 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,
576576
data->raw_data = (void *) pdata;
577577
}
578578

579+
if (type & PERF_SAMPLE_BRANCH_STACK) {
580+
u64 sz;
581+
582+
data->branch_stack = (struct branch_stack *)array;
583+
array++; /* nr */
584+
585+
sz = data->branch_stack->nr * sizeof(struct branch_entry);
586+
sz /= sizeof(u64);
587+
array += sz;
588+
}
579589
return 0;
580590
}
581591

‎tools/perf/util/hist.c‎

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,25 @@ static void hists__reset_col_len(struct hists *hists)
5050
hists__set_col_len(hists, col, 0);
5151
}
5252

53+
static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
54+
{
55+
const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
56+
57+
if (hists__col_len(hists, dso) < unresolved_col_width &&
58+
!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
59+
!symbol_conf.dso_list)
60+
hists__set_col_len(hists, dso, unresolved_col_width);
61+
}
62+
5363
static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
5464
{
65+
const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
5566
u16 len;
5667

5768
if (h->ms.sym)
58-
hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen);
59-
else {
60-
const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
61-
62-
if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width &&
63-
!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
64-
!symbol_conf.dso_list)
65-
hists__set_col_len(hists, HISTC_DSO,
66-
unresolved_col_width);
67-
}
69+
hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
70+
else
71+
hists__set_unres_dso_col_len(hists, HISTC_DSO);
6872

6973
len = thread__comm_len(h->thread);
7074
if (hists__new_col_len(hists, HISTC_COMM, len))
@@ -74,6 +78,37 @@ static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
7478
len = dso__name_len(h->ms.map->dso);
7579
hists__new_col_len(hists, HISTC_DSO, len);
7680
}
81+
82+
if (h->branch_info) {
83+
int symlen;
84+
/*
85+
* +4 accounts for '[x] ' priv level info
86+
* +2 account of 0x prefix on raw addresses
87+
*/
88+
if (h->branch_info->from.sym) {
89+
symlen = (int)h->branch_info->from.sym->namelen + 4;
90+
hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
91+
92+
symlen = dso__name_len(h->branch_info->from.map->dso);
93+
hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
94+
} else {
95+
symlen = unresolved_col_width + 4 + 2;
96+
hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
97+
hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
98+
}
99+
100+
if (h->branch_info->to.sym) {
101+
symlen = (int)h->branch_info->to.sym->namelen + 4;
102+
hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
103+
104+
symlen = dso__name_len(h->branch_info->to.map->dso);
105+
hists__new_col_len(hists, HISTC_DSO_TO, symlen);
106+
} else {
107+
symlen = unresolved_col_width + 4 + 2;
108+
hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
109+
hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
110+
}
111+
}
77112
}
78113

79114
static void hist_entry__add_cpumode_period(struct hist_entry *he,
@@ -195,26 +230,14 @@ static u8 symbol__parent_filter(const struct symbol *parent)
195230
return 0;
196231
}
197232

198-
struct hist_entry *__hists__add_entry(struct hists *hists,
233+
static struct hist_entry *add_hist_entry(struct hists *hists,
234+
struct hist_entry *entry,
199235
struct addr_location *al,
200-
struct symbol *sym_parent, u64 period)
236+
u64 period)
201237
{
202238
struct rb_node **p;
203239
struct rb_node *parent = NULL;
204240
struct hist_entry *he;
205-
struct hist_entry entry = {
206-
.thread = al->thread,
207-
.ms = {
208-
.map = al->map,
209-
.sym = al->sym,
210-
},
211-
.cpu = al->cpu,
212-
.ip = al->addr,
213-
.level = al->level,
214-
.period = period,
215-
.parent = sym_parent,
216-
.filtered = symbol__parent_filter(sym_parent),
217-
};
218241
int cmp;
219242

220243
pthread_mutex_lock(&hists->lock);
@@ -225,7 +248,7 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
225248
parent = *p;
226249
he = rb_entry(parent, struct hist_entry, rb_node_in);
227250

228-
cmp = hist_entry__cmp(&entry, he);
251+
cmp = hist_entry__cmp(entry, he);
229252

230253
if (!cmp) {
231254
he->period += period;
@@ -239,7 +262,7 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
239262
p = &(*p)->rb_right;
240263
}
241264

242-
he = hist_entry__new(&entry);
265+
he = hist_entry__new(entry);
243266
if (!he)
244267
goto out_unlock;
245268

@@ -252,6 +275,51 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
252275
return he;
253276
}
254277

278+
struct hist_entry *__hists__add_branch_entry(struct hists *self,
279+
struct addr_location *al,
280+
struct symbol *sym_parent,
281+
struct branch_info *bi,
282+
u64 period)
283+
{
284+
struct hist_entry entry = {
285+
.thread = al->thread,
286+
.ms = {
287+
.map = bi->to.map,
288+
.sym = bi->to.sym,
289+
},
290+
.cpu = al->cpu,
291+
.ip = bi->to.addr,
292+
.level = al->level,
293+
.period = period,
294+
.parent = sym_parent,
295+
.filtered = symbol__parent_filter(sym_parent),
296+
.branch_info = bi,
297+
};
298+
299+
return add_hist_entry(self, &entry, al, period);
300+
}
301+
302+
struct hist_entry *__hists__add_entry(struct hists *self,
303+
struct addr_location *al,
304+
struct symbol *sym_parent, u64 period)
305+
{
306+
struct hist_entry entry = {
307+
.thread = al->thread,
308+
.ms = {
309+
.map = al->map,
310+
.sym = al->sym,
311+
},
312+
.cpu = al->cpu,
313+
.ip = al->addr,
314+
.level = al->level,
315+
.period = period,
316+
.parent = sym_parent,
317+
.filtered = symbol__parent_filter(sym_parent),
318+
};
319+
320+
return add_hist_entry(self, &entry, al, period);
321+
}
322+
255323
int64_t
256324
hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
257325
{

‎tools/perf/util/hist.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ enum hist_column {
4242
HISTC_COMM,
4343
HISTC_PARENT,
4444
HISTC_CPU,
45+
HISTC_MISPREDICT,
46+
HISTC_SYMBOL_FROM,
47+
HISTC_SYMBOL_TO,
48+
HISTC_DSO_FROM,
49+
HISTC_DSO_TO,
4550
HISTC_NR_COLS, /* Last entry */
4651
};
4752

@@ -74,6 +79,12 @@ int hist_entry__snprintf(struct hist_entry *self, char *bf, size_t size,
7479
struct hists *hists);
7580
void hist_entry__free(struct hist_entry *);
7681

82+
struct hist_entry *__hists__add_branch_entry(struct hists *self,
83+
struct addr_location *al,
84+
struct symbol *sym_parent,
85+
struct branch_info *bi,
86+
u64 period);
87+
7788
void hists__output_resort(struct hists *self);
7889
void hists__output_resort_threaded(struct hists *hists);
7990
void hists__collapse_resort(struct hists *self);

‎tools/perf/util/session.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,63 @@ static bool symbol__match_parent_regex(struct symbol *sym)
229229
return 0;
230230
}
231231

232+
static const u8 cpumodes[] = {
233+
PERF_RECORD_MISC_USER,
234+
PERF_RECORD_MISC_KERNEL,
235+
PERF_RECORD_MISC_GUEST_USER,
236+
PERF_RECORD_MISC_GUEST_KERNEL
237+
};
238+
#define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
239+
240+
static void ip__resolve_ams(struct machine *self, struct thread *thread,
241+
struct addr_map_symbol *ams,
242+
u64 ip)
243+
{
244+
struct addr_location al;
245+
size_t i;
246+
u8 m;
247+
248+
memset(&al, 0, sizeof(al));
249+
250+
for (i = 0; i < NCPUMODES; i++) {
251+
m = cpumodes[i];
252+
/*
253+
* We cannot use the header.misc hint to determine whether a
254+
* branch stack address is user, kernel, guest, hypervisor.
255+
* Branches may straddle the kernel/user/hypervisor boundaries.
256+
* Thus, we have to try consecutively until we find a match
257+
* or else, the symbol is unknown
258+
*/
259+
thread__find_addr_location(thread, self, m, MAP__FUNCTION,
260+
ip, &al, NULL);
261+
if (al.sym)
262+
goto found;
263+
}
264+
found:
265+
ams->addr = ip;
266+
ams->sym = al.sym;
267+
ams->map = al.map;
268+
}
269+
270+
struct branch_info *machine__resolve_bstack(struct machine *self,
271+
struct thread *thr,
272+
struct branch_stack *bs)
273+
{
274+
struct branch_info *bi;
275+
unsigned int i;
276+
277+
bi = calloc(bs->nr, sizeof(struct branch_info));
278+
if (!bi)
279+
return NULL;
280+
281+
for (i = 0; i < bs->nr; i++) {
282+
ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to);
283+
ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from);
284+
bi[i].flags = bs->entries[i].flags;
285+
}
286+
return bi;
287+
}
288+
232289
int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
233290
struct thread *thread,
234291
struct ip_callchain *chain,
@@ -697,6 +754,18 @@ static void callchain__printf(struct perf_sample *sample)
697754
i, sample->callchain->ips[i]);
698755
}
699756

757+
static void branch_stack__printf(struct perf_sample *sample)
758+
{
759+
uint64_t i;
760+
761+
printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
762+
763+
for (i = 0; i < sample->branch_stack->nr; i++)
764+
printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
765+
i, sample->branch_stack->entries[i].from,
766+
sample->branch_stack->entries[i].to);
767+
}
768+
700769
static void perf_session__print_tstamp(struct perf_session *session,
701770
union perf_event *event,
702771
struct perf_sample *sample)
@@ -744,6 +813,9 @@ static void dump_sample(struct perf_session *session, union perf_event *event,
744813

745814
if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
746815
callchain__printf(sample);
816+
817+
if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
818+
branch_stack__printf(sample);
747819
}
748820

749821
static struct machine *

‎tools/perf/util/session.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ int perf_session__resolve_callchain(struct perf_session *self, struct perf_evsel
7373
struct ip_callchain *chain,
7474
struct symbol **parent);
7575

76+
struct branch_info *machine__resolve_bstack(struct machine *self,
77+
struct thread *thread,
78+
struct branch_stack *bs);
79+
7680
bool perf_session__has_traces(struct perf_session *self, const char *msg);
7781

7882
void mem_bswap_64(void *src, int byte_size);

0 commit comments

Comments
 (0)