Commit | Line | Data |
---|---|---|
9b32ba71 | 1 | #include <sys/mman.h> |
dd68ada2 | 2 | #include "sort.h" |
8a6c5b26 | 3 | #include "hist.h" |
4dfced35 | 4 | #include "comm.h" |
08e71542 | 5 | #include "symbol.h" |
8b536999 | 6 | #include "evsel.h" |
40184c46 NK |
7 | #include "evlist.h" |
8 | #include <traceevent/event-parse.h> | |
0c877d75 | 9 | #include "mem-events.h" |
dd68ada2 JK |
10 | |
11 | regex_t parent_regex; | |
edb7c60e ACM |
12 | const char default_parent_pattern[] = "^sys_|^do_page_fault"; |
13 | const char *parent_pattern = default_parent_pattern; | |
14 | const char default_sort_order[] = "comm,dso,symbol"; | |
40997d6c | 15 | const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles"; |
512ae1bd NK |
16 | const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked"; |
17 | const char default_top_sort_order[] = "dso,symbol"; | |
18 | const char default_diff_sort_order[] = "dso,symbol"; | |
d49dadea | 19 | const char default_tracepoint_sort_order[] = "trace"; |
512ae1bd | 20 | const char *sort_order; |
a7d945bc | 21 | const char *field_order; |
b21484f1 GP |
22 | regex_t ignore_callees_regex; |
23 | int have_ignore_callees = 0; | |
cfd92dad | 24 | int sort__has_thread = 0; |
078b8d4a | 25 | int sort__has_comm = 0; |
55369fc1 | 26 | enum sort_mode sort__mode = SORT_MODE__NORMAL; |
a4fb581b | 27 | |
37d9bb58 ACM |
28 | /* |
29 | * Replaces all occurrences of a char used with the: | |
30 | * | |
31 | * -t, --field-separator | |
32 | * | |
33 | * option, that uses a special separator character and don't pad with spaces, | |
34 | * replacing all occurances of this separator in symbol names (and other | |
35 | * output) with a '.' character, that thus it's the only non valid separator. | |
36 | */ | |
a4e3b956 | 37 | static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...) |
dd68ada2 JK |
38 | { |
39 | int n; | |
40 | va_list ap; | |
41 | ||
42 | va_start(ap, fmt); | |
a4e3b956 | 43 | n = vsnprintf(bf, size, fmt, ap); |
0ca0c130 | 44 | if (symbol_conf.field_sep && n > 0) { |
a4e3b956 ACM |
45 | char *sep = bf; |
46 | ||
47 | while (1) { | |
0ca0c130 | 48 | sep = strchr(sep, *symbol_conf.field_sep); |
a4e3b956 ACM |
49 | if (sep == NULL) |
50 | break; | |
51 | *sep = '.'; | |
dd68ada2 | 52 | } |
dd68ada2 JK |
53 | } |
54 | va_end(ap); | |
b832796c AB |
55 | |
56 | if (n >= (int)size) | |
57 | return size - 1; | |
dd68ada2 JK |
58 | return n; |
59 | } | |
60 | ||
b9c5143a | 61 | static int64_t cmp_null(const void *l, const void *r) |
872a878f FW |
62 | { |
63 | if (!l && !r) | |
64 | return 0; | |
65 | else if (!l) | |
66 | return -1; | |
67 | else | |
68 | return 1; | |
69 | } | |
70 | ||
71 | /* --sort pid */ | |
72 | ||
73 | static int64_t | |
74 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | |
75 | { | |
38051234 | 76 | return right->thread->tid - left->thread->tid; |
872a878f FW |
77 | } |
78 | ||
c824c433 | 79 | static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf, |
a4e3b956 | 80 | size_t size, unsigned int width) |
dd68ada2 | 81 | { |
b9c5143a | 82 | const char *comm = thread__comm_str(he->thread); |
5b591669 NK |
83 | |
84 | width = max(7U, width) - 6; | |
85 | return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid, | |
86 | width, width, comm ?: ""); | |
dd68ada2 JK |
87 | } |
88 | ||
54430101 NK |
89 | static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg) |
90 | { | |
91 | const struct thread *th = arg; | |
92 | ||
93 | if (type != HIST_FILTER__THREAD) | |
94 | return -1; | |
95 | ||
96 | return th && he->thread != th; | |
97 | } | |
98 | ||
872a878f | 99 | struct sort_entry sort_thread = { |
8246de88 | 100 | .se_header = " Pid:Command", |
872a878f FW |
101 | .se_cmp = sort__thread_cmp, |
102 | .se_snprintf = hist_entry__thread_snprintf, | |
54430101 | 103 | .se_filter = hist_entry__thread_filter, |
872a878f FW |
104 | .se_width_idx = HISTC_THREAD, |
105 | }; | |
106 | ||
107 | /* --sort comm */ | |
108 | ||
109 | static int64_t | |
110 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | |
111 | { | |
fedd63d3 | 112 | /* Compare the addr that should be unique among comm */ |
2f15bd8c | 113 | return strcmp(comm__str(right->comm), comm__str(left->comm)); |
872a878f FW |
114 | } |
115 | ||
116 | static int64_t | |
117 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) | |
118 | { | |
4dfced35 | 119 | /* Compare the addr that should be unique among comm */ |
2f15bd8c | 120 | return strcmp(comm__str(right->comm), comm__str(left->comm)); |
872a878f FW |
121 | } |
122 | ||
202e7a6d NK |
123 | static int64_t |
124 | sort__comm_sort(struct hist_entry *left, struct hist_entry *right) | |
125 | { | |
126 | return strcmp(comm__str(right->comm), comm__str(left->comm)); | |
127 | } | |
128 | ||
c824c433 | 129 | static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf, |
a4e3b956 | 130 | size_t size, unsigned int width) |
dd68ada2 | 131 | { |
5b591669 | 132 | return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm)); |
dd68ada2 JK |
133 | } |
134 | ||
14d1ac74 NK |
135 | struct sort_entry sort_comm = { |
136 | .se_header = "Command", | |
137 | .se_cmp = sort__comm_cmp, | |
138 | .se_collapse = sort__comm_collapse, | |
202e7a6d | 139 | .se_sort = sort__comm_sort, |
14d1ac74 | 140 | .se_snprintf = hist_entry__comm_snprintf, |
54430101 | 141 | .se_filter = hist_entry__thread_filter, |
14d1ac74 NK |
142 | .se_width_idx = HISTC_COMM, |
143 | }; | |
144 | ||
145 | /* --sort dso */ | |
146 | ||
b5387528 RAV |
147 | static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r) |
148 | { | |
149 | struct dso *dso_l = map_l ? map_l->dso : NULL; | |
150 | struct dso *dso_r = map_r ? map_r->dso : NULL; | |
151 | const char *dso_name_l, *dso_name_r; | |
152 | ||
153 | if (!dso_l || !dso_r) | |
202e7a6d | 154 | return cmp_null(dso_r, dso_l); |
b5387528 RAV |
155 | |
156 | if (verbose) { | |
157 | dso_name_l = dso_l->long_name; | |
158 | dso_name_r = dso_r->long_name; | |
159 | } else { | |
160 | dso_name_l = dso_l->short_name; | |
161 | dso_name_r = dso_r->short_name; | |
162 | } | |
163 | ||
164 | return strcmp(dso_name_l, dso_name_r); | |
165 | } | |
166 | ||
872a878f | 167 | static int64_t |
dd68ada2 JK |
168 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
169 | { | |
202e7a6d | 170 | return _sort__dso_cmp(right->ms.map, left->ms.map); |
b5387528 | 171 | } |
dd68ada2 | 172 | |
14d1ac74 NK |
173 | static int _hist_entry__dso_snprintf(struct map *map, char *bf, |
174 | size_t size, unsigned int width) | |
175 | { | |
176 | if (map && map->dso) { | |
177 | const char *dso_name = !verbose ? map->dso->short_name : | |
178 | map->dso->long_name; | |
5b591669 | 179 | return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name); |
14d1ac74 NK |
180 | } |
181 | ||
5b591669 | 182 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]"); |
14d1ac74 NK |
183 | } |
184 | ||
c824c433 | 185 | static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf, |
14d1ac74 NK |
186 | size_t size, unsigned int width) |
187 | { | |
c824c433 | 188 | return _hist_entry__dso_snprintf(he->ms.map, bf, size, width); |
14d1ac74 NK |
189 | } |
190 | ||
54430101 NK |
191 | static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg) |
192 | { | |
193 | const struct dso *dso = arg; | |
194 | ||
195 | if (type != HIST_FILTER__DSO) | |
196 | return -1; | |
197 | ||
198 | return dso && (!he->ms.map || he->ms.map->dso != dso); | |
199 | } | |
200 | ||
14d1ac74 NK |
201 | struct sort_entry sort_dso = { |
202 | .se_header = "Shared Object", | |
203 | .se_cmp = sort__dso_cmp, | |
204 | .se_snprintf = hist_entry__dso_snprintf, | |
54430101 | 205 | .se_filter = hist_entry__dso_filter, |
14d1ac74 NK |
206 | .se_width_idx = HISTC_DSO, |
207 | }; | |
208 | ||
209 | /* --sort symbol */ | |
dd68ada2 | 210 | |
2037be53 NK |
211 | static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip) |
212 | { | |
213 | return (int64_t)(right_ip - left_ip); | |
214 | } | |
215 | ||
51f27d14 | 216 | static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r) |
b5387528 RAV |
217 | { |
218 | if (!sym_l || !sym_r) | |
219 | return cmp_null(sym_l, sym_r); | |
220 | ||
221 | if (sym_l == sym_r) | |
222 | return 0; | |
223 | ||
c05676c0 YB |
224 | if (sym_l->start != sym_r->start) |
225 | return (int64_t)(sym_r->start - sym_l->start); | |
b5387528 | 226 | |
c05676c0 | 227 | return (int64_t)(sym_r->end - sym_l->end); |
b5387528 RAV |
228 | } |
229 | ||
14d1ac74 NK |
230 | static int64_t |
231 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | |
b5387528 | 232 | { |
09600e0f NK |
233 | int64_t ret; |
234 | ||
14d1ac74 | 235 | if (!left->ms.sym && !right->ms.sym) |
2037be53 | 236 | return _sort__addr_cmp(left->ip, right->ip); |
dd68ada2 | 237 | |
09600e0f NK |
238 | /* |
239 | * comparing symbol address alone is not enough since it's a | |
240 | * relative address within a dso. | |
241 | */ | |
69849fc5 | 242 | if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) { |
68f6d022 NK |
243 | ret = sort__dso_cmp(left, right); |
244 | if (ret != 0) | |
245 | return ret; | |
246 | } | |
09600e0f | 247 | |
51f27d14 | 248 | return _sort__sym_cmp(left->ms.sym, right->ms.sym); |
b5387528 RAV |
249 | } |
250 | ||
202e7a6d NK |
251 | static int64_t |
252 | sort__sym_sort(struct hist_entry *left, struct hist_entry *right) | |
253 | { | |
254 | if (!left->ms.sym || !right->ms.sym) | |
255 | return cmp_null(left->ms.sym, right->ms.sym); | |
256 | ||
257 | return strcmp(right->ms.sym->name, left->ms.sym->name); | |
258 | } | |
259 | ||
b5387528 RAV |
260 | static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym, |
261 | u64 ip, char level, char *bf, size_t size, | |
43355522 | 262 | unsigned int width) |
b5387528 RAV |
263 | { |
264 | size_t ret = 0; | |
265 | ||
266 | if (verbose) { | |
267 | char o = map ? dso__symtab_origin(map->dso) : '!'; | |
268 | ret += repsep_snprintf(bf, size, "%-#*llx %c ", | |
ded19d57 | 269 | BITS_PER_LONG / 4 + 2, ip, o); |
439d473b | 270 | } |
dd68ada2 | 271 | |
b5387528 | 272 | ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level); |
98a3b32c SE |
273 | if (sym && map) { |
274 | if (map->type == MAP__VARIABLE) { | |
275 | ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name); | |
276 | ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx", | |
62667746 | 277 | ip - map->unmap_ip(map, sym->start)); |
98a3b32c | 278 | } else { |
89fee709 | 279 | ret += repsep_snprintf(bf + ret, size - ret, "%.*s", |
98a3b32c SE |
280 | width - ret, |
281 | sym->name); | |
282 | } | |
283 | } else { | |
b5387528 RAV |
284 | size_t len = BITS_PER_LONG / 4; |
285 | ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", | |
286 | len, ip); | |
b5387528 RAV |
287 | } |
288 | ||
89fee709 | 289 | return ret; |
dd68ada2 JK |
290 | } |
291 | ||
c824c433 | 292 | static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, |
43355522 | 293 | size_t size, unsigned int width) |
b5387528 | 294 | { |
c824c433 ACM |
295 | return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip, |
296 | he->level, bf, size, width); | |
b5387528 | 297 | } |
dd68ada2 | 298 | |
54430101 NK |
299 | static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg) |
300 | { | |
301 | const char *sym = arg; | |
302 | ||
303 | if (type != HIST_FILTER__SYMBOL) | |
304 | return -1; | |
305 | ||
306 | return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym)); | |
307 | } | |
308 | ||
872a878f FW |
309 | struct sort_entry sort_sym = { |
310 | .se_header = "Symbol", | |
311 | .se_cmp = sort__sym_cmp, | |
202e7a6d | 312 | .se_sort = sort__sym_sort, |
872a878f | 313 | .se_snprintf = hist_entry__sym_snprintf, |
54430101 | 314 | .se_filter = hist_entry__sym_filter, |
872a878f FW |
315 | .se_width_idx = HISTC_SYMBOL, |
316 | }; | |
dd68ada2 | 317 | |
409a8be6 ACM |
318 | /* --sort srcline */ |
319 | ||
cecaec63 NK |
320 | static char *hist_entry__get_srcline(struct hist_entry *he) |
321 | { | |
322 | struct map *map = he->ms.map; | |
323 | ||
324 | if (!map) | |
325 | return SRCLINE_UNKNOWN; | |
326 | ||
327 | return get_srcline(map->dso, map__rip_2objdump(map, he->ip), | |
328 | he->ms.sym, true); | |
329 | } | |
330 | ||
409a8be6 ACM |
331 | static int64_t |
332 | sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right) | |
333 | { | |
cecaec63 NK |
334 | if (!left->srcline) |
335 | left->srcline = hist_entry__get_srcline(left); | |
336 | if (!right->srcline) | |
337 | right->srcline = hist_entry__get_srcline(right); | |
338 | ||
202e7a6d | 339 | return strcmp(right->srcline, left->srcline); |
409a8be6 ACM |
340 | } |
341 | ||
c824c433 | 342 | static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf, |
5b591669 | 343 | size_t size, unsigned int width) |
409a8be6 | 344 | { |
cecaec63 NK |
345 | if (!he->srcline) |
346 | he->srcline = hist_entry__get_srcline(he); | |
347 | ||
2960ed6f | 348 | return repsep_snprintf(bf, size, "%-.*s", width, he->srcline); |
409a8be6 ACM |
349 | } |
350 | ||
351 | struct sort_entry sort_srcline = { | |
352 | .se_header = "Source:Line", | |
353 | .se_cmp = sort__srcline_cmp, | |
354 | .se_snprintf = hist_entry__srcline_snprintf, | |
355 | .se_width_idx = HISTC_SRCLINE, | |
356 | }; | |
357 | ||
31191a85 AK |
358 | /* --sort srcfile */ |
359 | ||
360 | static char no_srcfile[1]; | |
361 | ||
cecaec63 | 362 | static char *hist_entry__get_srcfile(struct hist_entry *e) |
31191a85 AK |
363 | { |
364 | char *sf, *p; | |
365 | struct map *map = e->ms.map; | |
366 | ||
cecaec63 NK |
367 | if (!map) |
368 | return no_srcfile; | |
369 | ||
2f84b42b AK |
370 | sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip), |
371 | e->ms.sym, false, true); | |
76b10655 AK |
372 | if (!strcmp(sf, SRCLINE_UNKNOWN)) |
373 | return no_srcfile; | |
31191a85 AK |
374 | p = strchr(sf, ':'); |
375 | if (p && *sf) { | |
376 | *p = 0; | |
377 | return sf; | |
378 | } | |
379 | free(sf); | |
380 | return no_srcfile; | |
381 | } | |
382 | ||
383 | static int64_t | |
384 | sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right) | |
385 | { | |
cecaec63 NK |
386 | if (!left->srcfile) |
387 | left->srcfile = hist_entry__get_srcfile(left); | |
388 | if (!right->srcfile) | |
389 | right->srcfile = hist_entry__get_srcfile(right); | |
390 | ||
31191a85 AK |
391 | return strcmp(right->srcfile, left->srcfile); |
392 | } | |
393 | ||
394 | static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf, | |
395 | size_t size, unsigned int width) | |
396 | { | |
cecaec63 NK |
397 | if (!he->srcfile) |
398 | he->srcfile = hist_entry__get_srcfile(he); | |
399 | ||
2960ed6f | 400 | return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile); |
31191a85 AK |
401 | } |
402 | ||
403 | struct sort_entry sort_srcfile = { | |
404 | .se_header = "Source File", | |
405 | .se_cmp = sort__srcfile_cmp, | |
406 | .se_snprintf = hist_entry__srcfile_snprintf, | |
407 | .se_width_idx = HISTC_SRCFILE, | |
408 | }; | |
409 | ||
dd68ada2 JK |
410 | /* --sort parent */ |
411 | ||
872a878f | 412 | static int64_t |
dd68ada2 JK |
413 | sort__parent_cmp(struct hist_entry *left, struct hist_entry *right) |
414 | { | |
415 | struct symbol *sym_l = left->parent; | |
416 | struct symbol *sym_r = right->parent; | |
417 | ||
418 | if (!sym_l || !sym_r) | |
419 | return cmp_null(sym_l, sym_r); | |
420 | ||
202e7a6d | 421 | return strcmp(sym_r->name, sym_l->name); |
dd68ada2 JK |
422 | } |
423 | ||
c824c433 | 424 | static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf, |
a4e3b956 | 425 | size_t size, unsigned int width) |
dd68ada2 | 426 | { |
5b591669 | 427 | return repsep_snprintf(bf, size, "%-*.*s", width, width, |
c824c433 | 428 | he->parent ? he->parent->name : "[other]"); |
dd68ada2 JK |
429 | } |
430 | ||
872a878f FW |
431 | struct sort_entry sort_parent = { |
432 | .se_header = "Parent symbol", | |
433 | .se_cmp = sort__parent_cmp, | |
434 | .se_snprintf = hist_entry__parent_snprintf, | |
435 | .se_width_idx = HISTC_PARENT, | |
436 | }; | |
437 | ||
f60f3593 AS |
438 | /* --sort cpu */ |
439 | ||
872a878f | 440 | static int64_t |
f60f3593 AS |
441 | sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right) |
442 | { | |
443 | return right->cpu - left->cpu; | |
444 | } | |
445 | ||
c824c433 ACM |
446 | static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf, |
447 | size_t size, unsigned int width) | |
f60f3593 | 448 | { |
5b591669 | 449 | return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu); |
f60f3593 AS |
450 | } |
451 | ||
872a878f FW |
452 | struct sort_entry sort_cpu = { |
453 | .se_header = "CPU", | |
454 | .se_cmp = sort__cpu_cmp, | |
455 | .se_snprintf = hist_entry__cpu_snprintf, | |
456 | .se_width_idx = HISTC_CPU, | |
457 | }; | |
458 | ||
2e7ea3ab KL |
459 | /* --sort socket */ |
460 | ||
461 | static int64_t | |
462 | sort__socket_cmp(struct hist_entry *left, struct hist_entry *right) | |
463 | { | |
464 | return right->socket - left->socket; | |
465 | } | |
466 | ||
467 | static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf, | |
468 | size_t size, unsigned int width) | |
469 | { | |
470 | return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket); | |
471 | } | |
472 | ||
54430101 NK |
473 | static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg) |
474 | { | |
475 | int sk = *(const int *)arg; | |
476 | ||
477 | if (type != HIST_FILTER__SOCKET) | |
478 | return -1; | |
479 | ||
480 | return sk >= 0 && he->socket != sk; | |
481 | } | |
482 | ||
2e7ea3ab KL |
483 | struct sort_entry sort_socket = { |
484 | .se_header = "Socket", | |
485 | .se_cmp = sort__socket_cmp, | |
486 | .se_snprintf = hist_entry__socket_snprintf, | |
54430101 | 487 | .se_filter = hist_entry__socket_filter, |
2e7ea3ab KL |
488 | .se_width_idx = HISTC_SOCKET, |
489 | }; | |
490 | ||
a34bb6a0 NK |
491 | /* --sort trace */ |
492 | ||
493 | static char *get_trace_output(struct hist_entry *he) | |
494 | { | |
495 | struct trace_seq seq; | |
496 | struct perf_evsel *evsel; | |
497 | struct pevent_record rec = { | |
498 | .data = he->raw_data, | |
499 | .size = he->raw_size, | |
500 | }; | |
501 | ||
502 | evsel = hists_to_evsel(he->hists); | |
503 | ||
504 | trace_seq_init(&seq); | |
053a3989 NK |
505 | if (symbol_conf.raw_trace) { |
506 | pevent_print_fields(&seq, he->raw_data, he->raw_size, | |
507 | evsel->tp_format); | |
508 | } else { | |
509 | pevent_event_info(&seq, evsel->tp_format, &rec); | |
510 | } | |
a34bb6a0 NK |
511 | return seq.buffer; |
512 | } | |
513 | ||
514 | static int64_t | |
515 | sort__trace_cmp(struct hist_entry *left, struct hist_entry *right) | |
516 | { | |
517 | struct perf_evsel *evsel; | |
518 | ||
519 | evsel = hists_to_evsel(left->hists); | |
520 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) | |
521 | return 0; | |
522 | ||
523 | if (left->trace_output == NULL) | |
524 | left->trace_output = get_trace_output(left); | |
525 | if (right->trace_output == NULL) | |
526 | right->trace_output = get_trace_output(right); | |
527 | ||
a34bb6a0 NK |
528 | return strcmp(right->trace_output, left->trace_output); |
529 | } | |
530 | ||
531 | static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf, | |
532 | size_t size, unsigned int width) | |
533 | { | |
534 | struct perf_evsel *evsel; | |
535 | ||
536 | evsel = hists_to_evsel(he->hists); | |
537 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) | |
2960ed6f | 538 | return scnprintf(bf, size, "%-.*s", width, "N/A"); |
a34bb6a0 NK |
539 | |
540 | if (he->trace_output == NULL) | |
541 | he->trace_output = get_trace_output(he); | |
2960ed6f | 542 | return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output); |
a34bb6a0 NK |
543 | } |
544 | ||
545 | struct sort_entry sort_trace = { | |
546 | .se_header = "Trace output", | |
547 | .se_cmp = sort__trace_cmp, | |
548 | .se_snprintf = hist_entry__trace_snprintf, | |
549 | .se_width_idx = HISTC_TRACE, | |
550 | }; | |
551 | ||
14d1ac74 NK |
552 | /* sort keys for branch stacks */ |
553 | ||
b5387528 RAV |
554 | static int64_t |
555 | sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right) | |
556 | { | |
288a4b91 JO |
557 | if (!left->branch_info || !right->branch_info) |
558 | return cmp_null(left->branch_info, right->branch_info); | |
559 | ||
b5387528 RAV |
560 | return _sort__dso_cmp(left->branch_info->from.map, |
561 | right->branch_info->from.map); | |
562 | } | |
563 | ||
c824c433 | 564 | static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf, |
b5387528 RAV |
565 | size_t size, unsigned int width) |
566 | { | |
288a4b91 JO |
567 | if (he->branch_info) |
568 | return _hist_entry__dso_snprintf(he->branch_info->from.map, | |
569 | bf, size, width); | |
570 | else | |
571 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); | |
b5387528 RAV |
572 | } |
573 | ||
54430101 NK |
574 | static int hist_entry__dso_from_filter(struct hist_entry *he, int type, |
575 | const void *arg) | |
576 | { | |
577 | const struct dso *dso = arg; | |
578 | ||
579 | if (type != HIST_FILTER__DSO) | |
580 | return -1; | |
581 | ||
582 | return dso && (!he->branch_info || !he->branch_info->from.map || | |
583 | he->branch_info->from.map->dso != dso); | |
584 | } | |
585 | ||
b5387528 RAV |
586 | static int64_t |
587 | sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right) | |
588 | { | |
8b62fa59 JO |
589 | if (!left->branch_info || !right->branch_info) |
590 | return cmp_null(left->branch_info, right->branch_info); | |
591 | ||
b5387528 RAV |
592 | return _sort__dso_cmp(left->branch_info->to.map, |
593 | right->branch_info->to.map); | |
594 | } | |
595 | ||
c824c433 | 596 | static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf, |
b5387528 RAV |
597 | size_t size, unsigned int width) |
598 | { | |
8b62fa59 JO |
599 | if (he->branch_info) |
600 | return _hist_entry__dso_snprintf(he->branch_info->to.map, | |
601 | bf, size, width); | |
602 | else | |
603 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); | |
b5387528 RAV |
604 | } |
605 | ||
54430101 NK |
606 | static int hist_entry__dso_to_filter(struct hist_entry *he, int type, |
607 | const void *arg) | |
608 | { | |
609 | const struct dso *dso = arg; | |
610 | ||
611 | if (type != HIST_FILTER__DSO) | |
612 | return -1; | |
613 | ||
614 | return dso && (!he->branch_info || !he->branch_info->to.map || | |
615 | he->branch_info->to.map->dso != dso); | |
616 | } | |
617 | ||
b5387528 RAV |
618 | static int64_t |
619 | sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right) | |
620 | { | |
621 | struct addr_map_symbol *from_l = &left->branch_info->from; | |
622 | struct addr_map_symbol *from_r = &right->branch_info->from; | |
623 | ||
1b9e97a2 JO |
624 | if (!left->branch_info || !right->branch_info) |
625 | return cmp_null(left->branch_info, right->branch_info); | |
626 | ||
627 | from_l = &left->branch_info->from; | |
628 | from_r = &right->branch_info->from; | |
629 | ||
b5387528 | 630 | if (!from_l->sym && !from_r->sym) |
2037be53 | 631 | return _sort__addr_cmp(from_l->addr, from_r->addr); |
b5387528 | 632 | |
51f27d14 | 633 | return _sort__sym_cmp(from_l->sym, from_r->sym); |
b5387528 RAV |
634 | } |
635 | ||
636 | static int64_t | |
637 | sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right) | |
638 | { | |
38cdbd39 JO |
639 | struct addr_map_symbol *to_l, *to_r; |
640 | ||
641 | if (!left->branch_info || !right->branch_info) | |
642 | return cmp_null(left->branch_info, right->branch_info); | |
643 | ||
644 | to_l = &left->branch_info->to; | |
645 | to_r = &right->branch_info->to; | |
b5387528 RAV |
646 | |
647 | if (!to_l->sym && !to_r->sym) | |
2037be53 | 648 | return _sort__addr_cmp(to_l->addr, to_r->addr); |
b5387528 | 649 | |
51f27d14 | 650 | return _sort__sym_cmp(to_l->sym, to_r->sym); |
b5387528 RAV |
651 | } |
652 | ||
c824c433 | 653 | static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf, |
43355522 | 654 | size_t size, unsigned int width) |
b5387528 | 655 | { |
1b9e97a2 JO |
656 | if (he->branch_info) { |
657 | struct addr_map_symbol *from = &he->branch_info->from; | |
b5387528 | 658 | |
1b9e97a2 JO |
659 | return _hist_entry__sym_snprintf(from->map, from->sym, from->addr, |
660 | he->level, bf, size, width); | |
661 | } | |
662 | ||
663 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); | |
b5387528 RAV |
664 | } |
665 | ||
c824c433 | 666 | static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf, |
43355522 | 667 | size_t size, unsigned int width) |
b5387528 | 668 | { |
38cdbd39 JO |
669 | if (he->branch_info) { |
670 | struct addr_map_symbol *to = &he->branch_info->to; | |
671 | ||
672 | return _hist_entry__sym_snprintf(to->map, to->sym, to->addr, | |
673 | he->level, bf, size, width); | |
674 | } | |
b5387528 | 675 | |
38cdbd39 | 676 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); |
b5387528 RAV |
677 | } |
678 | ||
54430101 NK |
679 | static int hist_entry__sym_from_filter(struct hist_entry *he, int type, |
680 | const void *arg) | |
681 | { | |
682 | const char *sym = arg; | |
683 | ||
684 | if (type != HIST_FILTER__SYMBOL) | |
685 | return -1; | |
686 | ||
687 | return sym && !(he->branch_info && he->branch_info->from.sym && | |
688 | strstr(he->branch_info->from.sym->name, sym)); | |
689 | } | |
690 | ||
691 | static int hist_entry__sym_to_filter(struct hist_entry *he, int type, | |
692 | const void *arg) | |
693 | { | |
694 | const char *sym = arg; | |
695 | ||
696 | if (type != HIST_FILTER__SYMBOL) | |
697 | return -1; | |
698 | ||
699 | return sym && !(he->branch_info && he->branch_info->to.sym && | |
700 | strstr(he->branch_info->to.sym->name, sym)); | |
701 | } | |
702 | ||
14d1ac74 NK |
703 | struct sort_entry sort_dso_from = { |
704 | .se_header = "Source Shared Object", | |
705 | .se_cmp = sort__dso_from_cmp, | |
706 | .se_snprintf = hist_entry__dso_from_snprintf, | |
54430101 | 707 | .se_filter = hist_entry__dso_from_filter, |
14d1ac74 NK |
708 | .se_width_idx = HISTC_DSO_FROM, |
709 | }; | |
710 | ||
b5387528 RAV |
711 | struct sort_entry sort_dso_to = { |
712 | .se_header = "Target Shared Object", | |
713 | .se_cmp = sort__dso_to_cmp, | |
714 | .se_snprintf = hist_entry__dso_to_snprintf, | |
54430101 | 715 | .se_filter = hist_entry__dso_to_filter, |
b5387528 RAV |
716 | .se_width_idx = HISTC_DSO_TO, |
717 | }; | |
718 | ||
719 | struct sort_entry sort_sym_from = { | |
720 | .se_header = "Source Symbol", | |
721 | .se_cmp = sort__sym_from_cmp, | |
722 | .se_snprintf = hist_entry__sym_from_snprintf, | |
54430101 | 723 | .se_filter = hist_entry__sym_from_filter, |
b5387528 RAV |
724 | .se_width_idx = HISTC_SYMBOL_FROM, |
725 | }; | |
726 | ||
727 | struct sort_entry sort_sym_to = { | |
728 | .se_header = "Target Symbol", | |
729 | .se_cmp = sort__sym_to_cmp, | |
730 | .se_snprintf = hist_entry__sym_to_snprintf, | |
54430101 | 731 | .se_filter = hist_entry__sym_to_filter, |
b5387528 RAV |
732 | .se_width_idx = HISTC_SYMBOL_TO, |
733 | }; | |
734 | ||
735 | static int64_t | |
736 | sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right) | |
737 | { | |
428560e7 | 738 | unsigned char mp, p; |
b5387528 | 739 | |
428560e7 JO |
740 | if (!left->branch_info || !right->branch_info) |
741 | return cmp_null(left->branch_info, right->branch_info); | |
742 | ||
743 | mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred; | |
744 | p = left->branch_info->flags.predicted != right->branch_info->flags.predicted; | |
b5387528 RAV |
745 | return mp || p; |
746 | } | |
747 | ||
c824c433 | 748 | static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf, |
b5387528 RAV |
749 | size_t size, unsigned int width){ |
750 | static const char *out = "N/A"; | |
751 | ||
428560e7 JO |
752 | if (he->branch_info) { |
753 | if (he->branch_info->flags.predicted) | |
754 | out = "N"; | |
755 | else if (he->branch_info->flags.mispred) | |
756 | out = "Y"; | |
757 | } | |
b5387528 | 758 | |
5b591669 | 759 | return repsep_snprintf(bf, size, "%-*.*s", width, width, out); |
b5387528 RAV |
760 | } |
761 | ||
0e332f03 AK |
762 | static int64_t |
763 | sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right) | |
764 | { | |
765 | return left->branch_info->flags.cycles - | |
766 | right->branch_info->flags.cycles; | |
767 | } | |
768 | ||
769 | static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf, | |
770 | size_t size, unsigned int width) | |
771 | { | |
772 | if (he->branch_info->flags.cycles == 0) | |
773 | return repsep_snprintf(bf, size, "%-*s", width, "-"); | |
774 | return repsep_snprintf(bf, size, "%-*hd", width, | |
775 | he->branch_info->flags.cycles); | |
776 | } | |
777 | ||
778 | struct sort_entry sort_cycles = { | |
779 | .se_header = "Basic Block Cycles", | |
780 | .se_cmp = sort__cycles_cmp, | |
781 | .se_snprintf = hist_entry__cycles_snprintf, | |
782 | .se_width_idx = HISTC_CYCLES, | |
783 | }; | |
784 | ||
98a3b32c SE |
785 | /* --sort daddr_sym */ |
786 | static int64_t | |
787 | sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right) | |
788 | { | |
789 | uint64_t l = 0, r = 0; | |
790 | ||
791 | if (left->mem_info) | |
792 | l = left->mem_info->daddr.addr; | |
793 | if (right->mem_info) | |
794 | r = right->mem_info->daddr.addr; | |
795 | ||
796 | return (int64_t)(r - l); | |
797 | } | |
798 | ||
c824c433 | 799 | static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf, |
98a3b32c SE |
800 | size_t size, unsigned int width) |
801 | { | |
802 | uint64_t addr = 0; | |
803 | struct map *map = NULL; | |
804 | struct symbol *sym = NULL; | |
805 | ||
c824c433 ACM |
806 | if (he->mem_info) { |
807 | addr = he->mem_info->daddr.addr; | |
808 | map = he->mem_info->daddr.map; | |
809 | sym = he->mem_info->daddr.sym; | |
98a3b32c | 810 | } |
c824c433 | 811 | return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size, |
98a3b32c SE |
812 | width); |
813 | } | |
814 | ||
28e6db20 DZ |
815 | static int64_t |
816 | sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right) | |
817 | { | |
818 | uint64_t l = 0, r = 0; | |
819 | ||
820 | if (left->mem_info) | |
821 | l = left->mem_info->iaddr.addr; | |
822 | if (right->mem_info) | |
823 | r = right->mem_info->iaddr.addr; | |
824 | ||
825 | return (int64_t)(r - l); | |
826 | } | |
827 | ||
828 | static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf, | |
829 | size_t size, unsigned int width) | |
830 | { | |
831 | uint64_t addr = 0; | |
832 | struct map *map = NULL; | |
833 | struct symbol *sym = NULL; | |
834 | ||
835 | if (he->mem_info) { | |
836 | addr = he->mem_info->iaddr.addr; | |
837 | map = he->mem_info->iaddr.map; | |
838 | sym = he->mem_info->iaddr.sym; | |
839 | } | |
840 | return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size, | |
841 | width); | |
842 | } | |
843 | ||
98a3b32c SE |
844 | static int64_t |
845 | sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right) | |
846 | { | |
847 | struct map *map_l = NULL; | |
848 | struct map *map_r = NULL; | |
849 | ||
850 | if (left->mem_info) | |
851 | map_l = left->mem_info->daddr.map; | |
852 | if (right->mem_info) | |
853 | map_r = right->mem_info->daddr.map; | |
854 | ||
855 | return _sort__dso_cmp(map_l, map_r); | |
856 | } | |
857 | ||
c824c433 | 858 | static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf, |
98a3b32c SE |
859 | size_t size, unsigned int width) |
860 | { | |
861 | struct map *map = NULL; | |
862 | ||
c824c433 ACM |
863 | if (he->mem_info) |
864 | map = he->mem_info->daddr.map; | |
98a3b32c SE |
865 | |
866 | return _hist_entry__dso_snprintf(map, bf, size, width); | |
867 | } | |
868 | ||
869 | static int64_t | |
870 | sort__locked_cmp(struct hist_entry *left, struct hist_entry *right) | |
871 | { | |
872 | union perf_mem_data_src data_src_l; | |
873 | union perf_mem_data_src data_src_r; | |
874 | ||
875 | if (left->mem_info) | |
876 | data_src_l = left->mem_info->data_src; | |
877 | else | |
878 | data_src_l.mem_lock = PERF_MEM_LOCK_NA; | |
879 | ||
880 | if (right->mem_info) | |
881 | data_src_r = right->mem_info->data_src; | |
882 | else | |
883 | data_src_r.mem_lock = PERF_MEM_LOCK_NA; | |
884 | ||
885 | return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock); | |
886 | } | |
887 | ||
c824c433 | 888 | static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf, |
98a3b32c SE |
889 | size_t size, unsigned int width) |
890 | { | |
69a77275 | 891 | char out[10]; |
98a3b32c | 892 | |
69a77275 | 893 | perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info); |
89fee709 | 894 | return repsep_snprintf(bf, size, "%.*s", width, out); |
98a3b32c SE |
895 | } |
896 | ||
897 | static int64_t | |
898 | sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right) | |
899 | { | |
900 | union perf_mem_data_src data_src_l; | |
901 | union perf_mem_data_src data_src_r; | |
902 | ||
903 | if (left->mem_info) | |
904 | data_src_l = left->mem_info->data_src; | |
905 | else | |
906 | data_src_l.mem_dtlb = PERF_MEM_TLB_NA; | |
907 | ||
908 | if (right->mem_info) | |
909 | data_src_r = right->mem_info->data_src; | |
910 | else | |
911 | data_src_r.mem_dtlb = PERF_MEM_TLB_NA; | |
912 | ||
913 | return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb); | |
914 | } | |
915 | ||
c824c433 | 916 | static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf, |
98a3b32c SE |
917 | size_t size, unsigned int width) |
918 | { | |
919 | char out[64]; | |
98a3b32c | 920 | |
0c877d75 | 921 | perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info); |
98a3b32c SE |
922 | return repsep_snprintf(bf, size, "%-*s", width, out); |
923 | } | |
924 | ||
925 | static int64_t | |
926 | sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right) | |
927 | { | |
928 | union perf_mem_data_src data_src_l; | |
929 | union perf_mem_data_src data_src_r; | |
930 | ||
931 | if (left->mem_info) | |
932 | data_src_l = left->mem_info->data_src; | |
933 | else | |
934 | data_src_l.mem_lvl = PERF_MEM_LVL_NA; | |
935 | ||
936 | if (right->mem_info) | |
937 | data_src_r = right->mem_info->data_src; | |
938 | else | |
939 | data_src_r.mem_lvl = PERF_MEM_LVL_NA; | |
940 | ||
941 | return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl); | |
942 | } | |
943 | ||
c824c433 | 944 | static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf, |
98a3b32c SE |
945 | size_t size, unsigned int width) |
946 | { | |
947 | char out[64]; | |
98a3b32c | 948 | |
071e9a1e | 949 | perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info); |
98a3b32c SE |
950 | return repsep_snprintf(bf, size, "%-*s", width, out); |
951 | } | |
952 | ||
953 | static int64_t | |
954 | sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right) | |
955 | { | |
956 | union perf_mem_data_src data_src_l; | |
957 | union perf_mem_data_src data_src_r; | |
958 | ||
959 | if (left->mem_info) | |
960 | data_src_l = left->mem_info->data_src; | |
961 | else | |
962 | data_src_l.mem_snoop = PERF_MEM_SNOOP_NA; | |
963 | ||
964 | if (right->mem_info) | |
965 | data_src_r = right->mem_info->data_src; | |
966 | else | |
967 | data_src_r.mem_snoop = PERF_MEM_SNOOP_NA; | |
968 | ||
969 | return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop); | |
970 | } | |
971 | ||
c824c433 | 972 | static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf, |
98a3b32c SE |
973 | size_t size, unsigned int width) |
974 | { | |
975 | char out[64]; | |
98a3b32c | 976 | |
2c07af13 | 977 | perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info); |
98a3b32c SE |
978 | return repsep_snprintf(bf, size, "%-*s", width, out); |
979 | } | |
980 | ||
9b32ba71 DZ |
981 | static int64_t |
982 | sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right) | |
983 | { | |
984 | u64 l, r; | |
985 | struct map *l_map, *r_map; | |
986 | ||
987 | if (!left->mem_info) return -1; | |
988 | if (!right->mem_info) return 1; | |
989 | ||
990 | /* group event types together */ | |
991 | if (left->cpumode > right->cpumode) return -1; | |
992 | if (left->cpumode < right->cpumode) return 1; | |
993 | ||
994 | l_map = left->mem_info->daddr.map; | |
995 | r_map = right->mem_info->daddr.map; | |
996 | ||
997 | /* if both are NULL, jump to sort on al_addr instead */ | |
998 | if (!l_map && !r_map) | |
999 | goto addr; | |
1000 | ||
1001 | if (!l_map) return -1; | |
1002 | if (!r_map) return 1; | |
1003 | ||
1004 | if (l_map->maj > r_map->maj) return -1; | |
1005 | if (l_map->maj < r_map->maj) return 1; | |
1006 | ||
1007 | if (l_map->min > r_map->min) return -1; | |
1008 | if (l_map->min < r_map->min) return 1; | |
1009 | ||
1010 | if (l_map->ino > r_map->ino) return -1; | |
1011 | if (l_map->ino < r_map->ino) return 1; | |
1012 | ||
1013 | if (l_map->ino_generation > r_map->ino_generation) return -1; | |
1014 | if (l_map->ino_generation < r_map->ino_generation) return 1; | |
1015 | ||
1016 | /* | |
1017 | * Addresses with no major/minor numbers are assumed to be | |
1018 | * anonymous in userspace. Sort those on pid then address. | |
1019 | * | |
1020 | * The kernel and non-zero major/minor mapped areas are | |
1021 | * assumed to be unity mapped. Sort those on address. | |
1022 | */ | |
1023 | ||
1024 | if ((left->cpumode != PERF_RECORD_MISC_KERNEL) && | |
1025 | (!(l_map->flags & MAP_SHARED)) && | |
1026 | !l_map->maj && !l_map->min && !l_map->ino && | |
1027 | !l_map->ino_generation) { | |
1028 | /* userspace anonymous */ | |
1029 | ||
1030 | if (left->thread->pid_ > right->thread->pid_) return -1; | |
1031 | if (left->thread->pid_ < right->thread->pid_) return 1; | |
1032 | } | |
1033 | ||
1034 | addr: | |
1035 | /* al_addr does all the right addr - start + offset calculations */ | |
1036 | l = cl_address(left->mem_info->daddr.al_addr); | |
1037 | r = cl_address(right->mem_info->daddr.al_addr); | |
1038 | ||
1039 | if (l > r) return -1; | |
1040 | if (l < r) return 1; | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf, | |
1046 | size_t size, unsigned int width) | |
1047 | { | |
1048 | ||
1049 | uint64_t addr = 0; | |
1050 | struct map *map = NULL; | |
1051 | struct symbol *sym = NULL; | |
1052 | char level = he->level; | |
1053 | ||
1054 | if (he->mem_info) { | |
1055 | addr = cl_address(he->mem_info->daddr.al_addr); | |
1056 | map = he->mem_info->daddr.map; | |
1057 | sym = he->mem_info->daddr.sym; | |
1058 | ||
1059 | /* print [s] for shared data mmaps */ | |
1060 | if ((he->cpumode != PERF_RECORD_MISC_KERNEL) && | |
1061 | map && (map->type == MAP__VARIABLE) && | |
1062 | (map->flags & MAP_SHARED) && | |
1063 | (map->maj || map->min || map->ino || | |
1064 | map->ino_generation)) | |
1065 | level = 's'; | |
1066 | else if (!map) | |
1067 | level = 'X'; | |
1068 | } | |
1069 | return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size, | |
1070 | width); | |
1071 | } | |
1072 | ||
b5387528 RAV |
1073 | struct sort_entry sort_mispredict = { |
1074 | .se_header = "Branch Mispredicted", | |
1075 | .se_cmp = sort__mispredict_cmp, | |
1076 | .se_snprintf = hist_entry__mispredict_snprintf, | |
1077 | .se_width_idx = HISTC_MISPREDICT, | |
1078 | }; | |
1079 | ||
05484298 AK |
1080 | static u64 he_weight(struct hist_entry *he) |
1081 | { | |
1082 | return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0; | |
1083 | } | |
1084 | ||
1085 | static int64_t | |
1086 | sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right) | |
1087 | { | |
1088 | return he_weight(left) - he_weight(right); | |
1089 | } | |
1090 | ||
c824c433 | 1091 | static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf, |
05484298 AK |
1092 | size_t size, unsigned int width) |
1093 | { | |
c824c433 | 1094 | return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he)); |
05484298 AK |
1095 | } |
1096 | ||
1097 | struct sort_entry sort_local_weight = { | |
1098 | .se_header = "Local Weight", | |
1099 | .se_cmp = sort__local_weight_cmp, | |
1100 | .se_snprintf = hist_entry__local_weight_snprintf, | |
1101 | .se_width_idx = HISTC_LOCAL_WEIGHT, | |
1102 | }; | |
1103 | ||
1104 | static int64_t | |
1105 | sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right) | |
1106 | { | |
1107 | return left->stat.weight - right->stat.weight; | |
1108 | } | |
1109 | ||
c824c433 | 1110 | static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf, |
05484298 AK |
1111 | size_t size, unsigned int width) |
1112 | { | |
c824c433 | 1113 | return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight); |
05484298 AK |
1114 | } |
1115 | ||
1116 | struct sort_entry sort_global_weight = { | |
1117 | .se_header = "Weight", | |
1118 | .se_cmp = sort__global_weight_cmp, | |
1119 | .se_snprintf = hist_entry__global_weight_snprintf, | |
1120 | .se_width_idx = HISTC_GLOBAL_WEIGHT, | |
1121 | }; | |
1122 | ||
98a3b32c SE |
1123 | struct sort_entry sort_mem_daddr_sym = { |
1124 | .se_header = "Data Symbol", | |
1125 | .se_cmp = sort__daddr_cmp, | |
1126 | .se_snprintf = hist_entry__daddr_snprintf, | |
1127 | .se_width_idx = HISTC_MEM_DADDR_SYMBOL, | |
1128 | }; | |
1129 | ||
28e6db20 DZ |
1130 | struct sort_entry sort_mem_iaddr_sym = { |
1131 | .se_header = "Code Symbol", | |
1132 | .se_cmp = sort__iaddr_cmp, | |
1133 | .se_snprintf = hist_entry__iaddr_snprintf, | |
1134 | .se_width_idx = HISTC_MEM_IADDR_SYMBOL, | |
1135 | }; | |
1136 | ||
98a3b32c SE |
1137 | struct sort_entry sort_mem_daddr_dso = { |
1138 | .se_header = "Data Object", | |
1139 | .se_cmp = sort__dso_daddr_cmp, | |
1140 | .se_snprintf = hist_entry__dso_daddr_snprintf, | |
1141 | .se_width_idx = HISTC_MEM_DADDR_SYMBOL, | |
1142 | }; | |
1143 | ||
1144 | struct sort_entry sort_mem_locked = { | |
1145 | .se_header = "Locked", | |
1146 | .se_cmp = sort__locked_cmp, | |
1147 | .se_snprintf = hist_entry__locked_snprintf, | |
1148 | .se_width_idx = HISTC_MEM_LOCKED, | |
1149 | }; | |
1150 | ||
1151 | struct sort_entry sort_mem_tlb = { | |
1152 | .se_header = "TLB access", | |
1153 | .se_cmp = sort__tlb_cmp, | |
1154 | .se_snprintf = hist_entry__tlb_snprintf, | |
1155 | .se_width_idx = HISTC_MEM_TLB, | |
1156 | }; | |
1157 | ||
1158 | struct sort_entry sort_mem_lvl = { | |
1159 | .se_header = "Memory access", | |
1160 | .se_cmp = sort__lvl_cmp, | |
1161 | .se_snprintf = hist_entry__lvl_snprintf, | |
1162 | .se_width_idx = HISTC_MEM_LVL, | |
1163 | }; | |
1164 | ||
1165 | struct sort_entry sort_mem_snoop = { | |
1166 | .se_header = "Snoop", | |
1167 | .se_cmp = sort__snoop_cmp, | |
1168 | .se_snprintf = hist_entry__snoop_snprintf, | |
1169 | .se_width_idx = HISTC_MEM_SNOOP, | |
1170 | }; | |
1171 | ||
9b32ba71 DZ |
1172 | struct sort_entry sort_mem_dcacheline = { |
1173 | .se_header = "Data Cacheline", | |
1174 | .se_cmp = sort__dcacheline_cmp, | |
1175 | .se_snprintf = hist_entry__dcacheline_snprintf, | |
1176 | .se_width_idx = HISTC_MEM_DCACHELINE, | |
1177 | }; | |
1178 | ||
f5d05bce AK |
1179 | static int64_t |
1180 | sort__abort_cmp(struct hist_entry *left, struct hist_entry *right) | |
1181 | { | |
49f47443 JO |
1182 | if (!left->branch_info || !right->branch_info) |
1183 | return cmp_null(left->branch_info, right->branch_info); | |
1184 | ||
f5d05bce AK |
1185 | return left->branch_info->flags.abort != |
1186 | right->branch_info->flags.abort; | |
1187 | } | |
1188 | ||
c824c433 | 1189 | static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf, |
f5d05bce AK |
1190 | size_t size, unsigned int width) |
1191 | { | |
49f47443 JO |
1192 | static const char *out = "N/A"; |
1193 | ||
1194 | if (he->branch_info) { | |
1195 | if (he->branch_info->flags.abort) | |
1196 | out = "A"; | |
1197 | else | |
1198 | out = "."; | |
1199 | } | |
f5d05bce | 1200 | |
f5d05bce AK |
1201 | return repsep_snprintf(bf, size, "%-*s", width, out); |
1202 | } | |
1203 | ||
1204 | struct sort_entry sort_abort = { | |
1205 | .se_header = "Transaction abort", | |
1206 | .se_cmp = sort__abort_cmp, | |
1207 | .se_snprintf = hist_entry__abort_snprintf, | |
1208 | .se_width_idx = HISTC_ABORT, | |
1209 | }; | |
1210 | ||
1211 | static int64_t | |
1212 | sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right) | |
1213 | { | |
0199d244 JO |
1214 | if (!left->branch_info || !right->branch_info) |
1215 | return cmp_null(left->branch_info, right->branch_info); | |
1216 | ||
f5d05bce AK |
1217 | return left->branch_info->flags.in_tx != |
1218 | right->branch_info->flags.in_tx; | |
1219 | } | |
1220 | ||
c824c433 | 1221 | static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf, |
f5d05bce AK |
1222 | size_t size, unsigned int width) |
1223 | { | |
0199d244 | 1224 | static const char *out = "N/A"; |
f5d05bce | 1225 | |
0199d244 JO |
1226 | if (he->branch_info) { |
1227 | if (he->branch_info->flags.in_tx) | |
1228 | out = "T"; | |
1229 | else | |
1230 | out = "."; | |
1231 | } | |
f5d05bce AK |
1232 | |
1233 | return repsep_snprintf(bf, size, "%-*s", width, out); | |
1234 | } | |
1235 | ||
1236 | struct sort_entry sort_in_tx = { | |
1237 | .se_header = "Branch in transaction", | |
1238 | .se_cmp = sort__in_tx_cmp, | |
1239 | .se_snprintf = hist_entry__in_tx_snprintf, | |
1240 | .se_width_idx = HISTC_IN_TX, | |
1241 | }; | |
1242 | ||
475eeab9 AK |
1243 | static int64_t |
1244 | sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right) | |
1245 | { | |
1246 | return left->transaction - right->transaction; | |
1247 | } | |
1248 | ||
1249 | static inline char *add_str(char *p, const char *str) | |
1250 | { | |
1251 | strcpy(p, str); | |
1252 | return p + strlen(str); | |
1253 | } | |
1254 | ||
1255 | static struct txbit { | |
1256 | unsigned flag; | |
1257 | const char *name; | |
1258 | int skip_for_len; | |
1259 | } txbits[] = { | |
1260 | { PERF_TXN_ELISION, "EL ", 0 }, | |
1261 | { PERF_TXN_TRANSACTION, "TX ", 1 }, | |
1262 | { PERF_TXN_SYNC, "SYNC ", 1 }, | |
1263 | { PERF_TXN_ASYNC, "ASYNC ", 0 }, | |
1264 | { PERF_TXN_RETRY, "RETRY ", 0 }, | |
1265 | { PERF_TXN_CONFLICT, "CON ", 0 }, | |
1266 | { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 }, | |
1267 | { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 }, | |
1268 | { 0, NULL, 0 } | |
1269 | }; | |
1270 | ||
1271 | int hist_entry__transaction_len(void) | |
1272 | { | |
1273 | int i; | |
1274 | int len = 0; | |
1275 | ||
1276 | for (i = 0; txbits[i].name; i++) { | |
1277 | if (!txbits[i].skip_for_len) | |
1278 | len += strlen(txbits[i].name); | |
1279 | } | |
1280 | len += 4; /* :XX<space> */ | |
1281 | return len; | |
1282 | } | |
1283 | ||
c824c433 | 1284 | static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf, |
475eeab9 AK |
1285 | size_t size, unsigned int width) |
1286 | { | |
c824c433 | 1287 | u64 t = he->transaction; |
475eeab9 AK |
1288 | char buf[128]; |
1289 | char *p = buf; | |
1290 | int i; | |
1291 | ||
1292 | buf[0] = 0; | |
1293 | for (i = 0; txbits[i].name; i++) | |
1294 | if (txbits[i].flag & t) | |
1295 | p = add_str(p, txbits[i].name); | |
1296 | if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC))) | |
1297 | p = add_str(p, "NEITHER "); | |
1298 | if (t & PERF_TXN_ABORT_MASK) { | |
1299 | sprintf(p, ":%" PRIx64, | |
1300 | (t & PERF_TXN_ABORT_MASK) >> | |
1301 | PERF_TXN_ABORT_SHIFT); | |
1302 | p += strlen(p); | |
1303 | } | |
1304 | ||
1305 | return repsep_snprintf(bf, size, "%-*s", width, buf); | |
1306 | } | |
1307 | ||
1308 | struct sort_entry sort_transaction = { | |
1309 | .se_header = "Transaction ", | |
1310 | .se_cmp = sort__transaction_cmp, | |
1311 | .se_snprintf = hist_entry__transaction_snprintf, | |
1312 | .se_width_idx = HISTC_TRANSACTION, | |
1313 | }; | |
1314 | ||
872a878f FW |
1315 | struct sort_dimension { |
1316 | const char *name; | |
1317 | struct sort_entry *entry; | |
1318 | int taken; | |
1319 | }; | |
1320 | ||
b5387528 RAV |
1321 | #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) } |
1322 | ||
fc5871ed | 1323 | static struct sort_dimension common_sort_dimensions[] = { |
b5387528 RAV |
1324 | DIM(SORT_PID, "pid", sort_thread), |
1325 | DIM(SORT_COMM, "comm", sort_comm), | |
1326 | DIM(SORT_DSO, "dso", sort_dso), | |
b5387528 | 1327 | DIM(SORT_SYM, "symbol", sort_sym), |
b5387528 RAV |
1328 | DIM(SORT_PARENT, "parent", sort_parent), |
1329 | DIM(SORT_CPU, "cpu", sort_cpu), | |
2e7ea3ab | 1330 | DIM(SORT_SOCKET, "socket", sort_socket), |
409a8be6 | 1331 | DIM(SORT_SRCLINE, "srcline", sort_srcline), |
31191a85 | 1332 | DIM(SORT_SRCFILE, "srcfile", sort_srcfile), |
f9ea55d0 AK |
1333 | DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight), |
1334 | DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight), | |
475eeab9 | 1335 | DIM(SORT_TRANSACTION, "transaction", sort_transaction), |
a34bb6a0 | 1336 | DIM(SORT_TRACE, "trace", sort_trace), |
872a878f FW |
1337 | }; |
1338 | ||
fc5871ed NK |
1339 | #undef DIM |
1340 | ||
1341 | #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) } | |
1342 | ||
1343 | static struct sort_dimension bstack_sort_dimensions[] = { | |
1344 | DIM(SORT_DSO_FROM, "dso_from", sort_dso_from), | |
1345 | DIM(SORT_DSO_TO, "dso_to", sort_dso_to), | |
1346 | DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from), | |
1347 | DIM(SORT_SYM_TO, "symbol_to", sort_sym_to), | |
1348 | DIM(SORT_MISPREDICT, "mispredict", sort_mispredict), | |
f5d05bce AK |
1349 | DIM(SORT_IN_TX, "in_tx", sort_in_tx), |
1350 | DIM(SORT_ABORT, "abort", sort_abort), | |
0e332f03 | 1351 | DIM(SORT_CYCLES, "cycles", sort_cycles), |
fc5871ed NK |
1352 | }; |
1353 | ||
1354 | #undef DIM | |
1355 | ||
afab87b9 NK |
1356 | #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) } |
1357 | ||
1358 | static struct sort_dimension memory_sort_dimensions[] = { | |
afab87b9 | 1359 | DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym), |
28e6db20 | 1360 | DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym), |
afab87b9 NK |
1361 | DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso), |
1362 | DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked), | |
1363 | DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb), | |
1364 | DIM(SORT_MEM_LVL, "mem", sort_mem_lvl), | |
1365 | DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop), | |
9b32ba71 | 1366 | DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline), |
afab87b9 NK |
1367 | }; |
1368 | ||
1369 | #undef DIM | |
1370 | ||
a2ce067e NK |
1371 | struct hpp_dimension { |
1372 | const char *name; | |
1373 | struct perf_hpp_fmt *fmt; | |
1374 | int taken; | |
1375 | }; | |
1376 | ||
1377 | #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], } | |
1378 | ||
1379 | static struct hpp_dimension hpp_sort_dimensions[] = { | |
1380 | DIM(PERF_HPP__OVERHEAD, "overhead"), | |
1381 | DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"), | |
1382 | DIM(PERF_HPP__OVERHEAD_US, "overhead_us"), | |
1383 | DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"), | |
1384 | DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"), | |
594dcbf3 | 1385 | DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"), |
a2ce067e NK |
1386 | DIM(PERF_HPP__SAMPLES, "sample"), |
1387 | DIM(PERF_HPP__PERIOD, "period"), | |
1388 | }; | |
1389 | ||
1390 | #undef DIM | |
1391 | ||
8b536999 NK |
1392 | struct hpp_sort_entry { |
1393 | struct perf_hpp_fmt hpp; | |
1394 | struct sort_entry *se; | |
1395 | }; | |
1396 | ||
e0d66c74 | 1397 | void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists) |
678a500d NK |
1398 | { |
1399 | struct hpp_sort_entry *hse; | |
1400 | ||
1401 | if (!perf_hpp__is_sort_entry(fmt)) | |
1402 | return; | |
1403 | ||
1404 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1ecd4453 | 1405 | hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name)); |
678a500d NK |
1406 | } |
1407 | ||
8b536999 NK |
1408 | static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
1409 | struct perf_evsel *evsel) | |
1410 | { | |
1411 | struct hpp_sort_entry *hse; | |
5b591669 | 1412 | size_t len = fmt->user_len; |
8b536999 NK |
1413 | |
1414 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
8b536999 | 1415 | |
5b591669 | 1416 | if (!len) |
4ea062ed | 1417 | len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx); |
5b591669 | 1418 | |
1ecd4453 | 1419 | return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name); |
8b536999 NK |
1420 | } |
1421 | ||
1422 | static int __sort__hpp_width(struct perf_hpp_fmt *fmt, | |
1423 | struct perf_hpp *hpp __maybe_unused, | |
1424 | struct perf_evsel *evsel) | |
1425 | { | |
1426 | struct hpp_sort_entry *hse; | |
5b591669 | 1427 | size_t len = fmt->user_len; |
8b536999 NK |
1428 | |
1429 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1430 | ||
5b591669 | 1431 | if (!len) |
4ea062ed | 1432 | len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx); |
5b591669 NK |
1433 | |
1434 | return len; | |
8b536999 NK |
1435 | } |
1436 | ||
1437 | static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, | |
1438 | struct hist_entry *he) | |
1439 | { | |
1440 | struct hpp_sort_entry *hse; | |
5b591669 | 1441 | size_t len = fmt->user_len; |
8b536999 NK |
1442 | |
1443 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
5b591669 NK |
1444 | |
1445 | if (!len) | |
1446 | len = hists__col_len(he->hists, hse->se->se_width_idx); | |
8b536999 NK |
1447 | |
1448 | return hse->se->se_snprintf(he, hpp->buf, hpp->size, len); | |
1449 | } | |
1450 | ||
87bbdf76 NK |
1451 | static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt, |
1452 | struct hist_entry *a, struct hist_entry *b) | |
1453 | { | |
1454 | struct hpp_sort_entry *hse; | |
1455 | ||
1456 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1457 | return hse->se->se_cmp(a, b); | |
1458 | } | |
1459 | ||
1460 | static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt, | |
1461 | struct hist_entry *a, struct hist_entry *b) | |
1462 | { | |
1463 | struct hpp_sort_entry *hse; | |
1464 | int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *); | |
1465 | ||
1466 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1467 | collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp; | |
1468 | return collapse_fn(a, b); | |
1469 | } | |
1470 | ||
1471 | static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt, | |
1472 | struct hist_entry *a, struct hist_entry *b) | |
1473 | { | |
1474 | struct hpp_sort_entry *hse; | |
1475 | int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *); | |
1476 | ||
1477 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1478 | sort_fn = hse->se->se_sort ?: hse->se->se_cmp; | |
1479 | return sort_fn(a, b); | |
1480 | } | |
1481 | ||
97358084 JO |
1482 | bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format) |
1483 | { | |
1484 | return format->header == __sort__hpp_header; | |
1485 | } | |
1486 | ||
4945cf2a NK |
1487 | #define MK_SORT_ENTRY_CHK(key) \ |
1488 | bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \ | |
1489 | { \ | |
1490 | struct hpp_sort_entry *hse; \ | |
1491 | \ | |
1492 | if (!perf_hpp__is_sort_entry(fmt)) \ | |
1493 | return false; \ | |
1494 | \ | |
1495 | hse = container_of(fmt, struct hpp_sort_entry, hpp); \ | |
1496 | return hse->se == &sort_ ## key ; \ | |
1497 | } | |
1498 | ||
1499 | MK_SORT_ENTRY_CHK(trace) | |
1500 | MK_SORT_ENTRY_CHK(srcline) | |
1501 | MK_SORT_ENTRY_CHK(srcfile) | |
1502 | MK_SORT_ENTRY_CHK(thread) | |
1503 | MK_SORT_ENTRY_CHK(comm) | |
1504 | MK_SORT_ENTRY_CHK(dso) | |
1505 | MK_SORT_ENTRY_CHK(sym) | |
a9c6e46c | 1506 | |
a9c6e46c | 1507 | |
97358084 JO |
1508 | static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b) |
1509 | { | |
1510 | struct hpp_sort_entry *hse_a; | |
1511 | struct hpp_sort_entry *hse_b; | |
1512 | ||
1513 | if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b)) | |
1514 | return false; | |
1515 | ||
1516 | hse_a = container_of(a, struct hpp_sort_entry, hpp); | |
1517 | hse_b = container_of(b, struct hpp_sort_entry, hpp); | |
1518 | ||
1519 | return hse_a->se == hse_b->se; | |
1520 | } | |
1521 | ||
564132f3 JO |
1522 | static void hse_free(struct perf_hpp_fmt *fmt) |
1523 | { | |
1524 | struct hpp_sort_entry *hse; | |
1525 | ||
1526 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1527 | free(hse); | |
1528 | } | |
1529 | ||
a7d945bc | 1530 | static struct hpp_sort_entry * |
4b633eba | 1531 | __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level) |
8b536999 NK |
1532 | { |
1533 | struct hpp_sort_entry *hse; | |
1534 | ||
1535 | hse = malloc(sizeof(*hse)); | |
1536 | if (hse == NULL) { | |
1537 | pr_err("Memory allocation failed\n"); | |
a7d945bc | 1538 | return NULL; |
8b536999 NK |
1539 | } |
1540 | ||
1541 | hse->se = sd->entry; | |
1ecd4453 | 1542 | hse->hpp.name = sd->entry->se_header; |
8b536999 NK |
1543 | hse->hpp.header = __sort__hpp_header; |
1544 | hse->hpp.width = __sort__hpp_width; | |
1545 | hse->hpp.entry = __sort__hpp_entry; | |
1546 | hse->hpp.color = NULL; | |
1547 | ||
87bbdf76 NK |
1548 | hse->hpp.cmp = __sort__hpp_cmp; |
1549 | hse->hpp.collapse = __sort__hpp_collapse; | |
1550 | hse->hpp.sort = __sort__hpp_sort; | |
97358084 | 1551 | hse->hpp.equal = __sort__hpp_equal; |
564132f3 | 1552 | hse->hpp.free = hse_free; |
8b536999 NK |
1553 | |
1554 | INIT_LIST_HEAD(&hse->hpp.list); | |
1555 | INIT_LIST_HEAD(&hse->hpp.sort_list); | |
f2998422 | 1556 | hse->hpp.elide = false; |
e0d66c74 | 1557 | hse->hpp.len = 0; |
5b591669 | 1558 | hse->hpp.user_len = 0; |
4b633eba | 1559 | hse->hpp.level = level; |
8b536999 | 1560 | |
a7d945bc NK |
1561 | return hse; |
1562 | } | |
1563 | ||
564132f3 JO |
1564 | static void hpp_free(struct perf_hpp_fmt *fmt) |
1565 | { | |
1566 | free(fmt); | |
1567 | } | |
1568 | ||
4b633eba NK |
1569 | static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd, |
1570 | int level) | |
1945c3e7 JO |
1571 | { |
1572 | struct perf_hpp_fmt *fmt; | |
1573 | ||
1574 | fmt = memdup(hd->fmt, sizeof(*fmt)); | |
1575 | if (fmt) { | |
1576 | INIT_LIST_HEAD(&fmt->list); | |
1577 | INIT_LIST_HEAD(&fmt->sort_list); | |
564132f3 | 1578 | fmt->free = hpp_free; |
4b633eba | 1579 | fmt->level = level; |
1945c3e7 JO |
1580 | } |
1581 | ||
1582 | return fmt; | |
1583 | } | |
1584 | ||
54430101 NK |
1585 | int hist_entry__filter(struct hist_entry *he, int type, const void *arg) |
1586 | { | |
1587 | struct perf_hpp_fmt *fmt; | |
1588 | struct hpp_sort_entry *hse; | |
f4954cfb NK |
1589 | int ret = -1; |
1590 | int r; | |
1591 | ||
1592 | perf_hpp_list__for_each_format(he->hpp_list, fmt) { | |
1593 | if (!perf_hpp__is_sort_entry(fmt)) | |
1594 | continue; | |
1595 | ||
1596 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1597 | if (hse->se->se_filter == NULL) | |
1598 | continue; | |
1599 | ||
1600 | /* | |
1601 | * hist entry is filtered if any of sort key in the hpp list | |
1602 | * is applied. But it should skip non-matched filter types. | |
1603 | */ | |
1604 | r = hse->se->se_filter(he, type, arg); | |
1605 | if (r >= 0) { | |
1606 | if (ret < 0) | |
1607 | ret = 0; | |
1608 | ret |= r; | |
1609 | } | |
1610 | } | |
54430101 | 1611 | |
f4954cfb | 1612 | return ret; |
54430101 NK |
1613 | } |
1614 | ||
d7b617f5 JO |
1615 | static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd, |
1616 | struct perf_hpp_list *list, | |
1617 | int level) | |
a7d945bc | 1618 | { |
4b633eba | 1619 | struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level); |
a7d945bc NK |
1620 | |
1621 | if (hse == NULL) | |
1622 | return -1; | |
1623 | ||
d7b617f5 | 1624 | perf_hpp_list__register_sort_field(list, &hse->hpp); |
8b536999 NK |
1625 | return 0; |
1626 | } | |
1627 | ||
d7b617f5 JO |
1628 | static int __sort_dimension__add_hpp_output(struct sort_dimension *sd, |
1629 | struct perf_hpp_list *list) | |
a7d945bc | 1630 | { |
4b633eba | 1631 | struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0); |
a7d945bc NK |
1632 | |
1633 | if (hse == NULL) | |
1634 | return -1; | |
1635 | ||
07600027 | 1636 | perf_hpp_list__column_register(list, &hse->hpp); |
a7d945bc NK |
1637 | return 0; |
1638 | } | |
1639 | ||
c7c2a5e4 NK |
1640 | struct hpp_dynamic_entry { |
1641 | struct perf_hpp_fmt hpp; | |
1642 | struct perf_evsel *evsel; | |
1643 | struct format_field *field; | |
1644 | unsigned dynamic_len; | |
053a3989 | 1645 | bool raw_trace; |
c7c2a5e4 NK |
1646 | }; |
1647 | ||
1648 | static int hde_width(struct hpp_dynamic_entry *hde) | |
1649 | { | |
1650 | if (!hde->hpp.len) { | |
1651 | int len = hde->dynamic_len; | |
1652 | int namelen = strlen(hde->field->name); | |
1653 | int fieldlen = hde->field->size; | |
1654 | ||
1655 | if (namelen > len) | |
1656 | len = namelen; | |
1657 | ||
1658 | if (!(hde->field->flags & FIELD_IS_STRING)) { | |
1659 | /* length for print hex numbers */ | |
1660 | fieldlen = hde->field->size * 2 + 2; | |
1661 | } | |
1662 | if (fieldlen > len) | |
1663 | len = fieldlen; | |
1664 | ||
1665 | hde->hpp.len = len; | |
1666 | } | |
1667 | return hde->hpp.len; | |
1668 | } | |
1669 | ||
60517d28 NK |
1670 | static void update_dynamic_len(struct hpp_dynamic_entry *hde, |
1671 | struct hist_entry *he) | |
1672 | { | |
1673 | char *str, *pos; | |
1674 | struct format_field *field = hde->field; | |
1675 | size_t namelen; | |
1676 | bool last = false; | |
1677 | ||
053a3989 NK |
1678 | if (hde->raw_trace) |
1679 | return; | |
1680 | ||
60517d28 NK |
1681 | /* parse pretty print result and update max length */ |
1682 | if (!he->trace_output) | |
1683 | he->trace_output = get_trace_output(he); | |
1684 | ||
1685 | namelen = strlen(field->name); | |
1686 | str = he->trace_output; | |
1687 | ||
1688 | while (str) { | |
1689 | pos = strchr(str, ' '); | |
1690 | if (pos == NULL) { | |
1691 | last = true; | |
1692 | pos = str + strlen(str); | |
1693 | } | |
1694 | ||
1695 | if (!strncmp(str, field->name, namelen)) { | |
1696 | size_t len; | |
1697 | ||
1698 | str += namelen + 1; | |
1699 | len = pos - str; | |
1700 | ||
1701 | if (len > hde->dynamic_len) | |
1702 | hde->dynamic_len = len; | |
1703 | break; | |
1704 | } | |
1705 | ||
1706 | if (last) | |
1707 | str = NULL; | |
1708 | else | |
1709 | str = pos + 1; | |
1710 | } | |
1711 | } | |
1712 | ||
c7c2a5e4 NK |
1713 | static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
1714 | struct perf_evsel *evsel __maybe_unused) | |
1715 | { | |
1716 | struct hpp_dynamic_entry *hde; | |
1717 | size_t len = fmt->user_len; | |
1718 | ||
1719 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1720 | ||
1721 | if (!len) | |
1722 | len = hde_width(hde); | |
1723 | ||
1724 | return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name); | |
1725 | } | |
1726 | ||
1727 | static int __sort__hde_width(struct perf_hpp_fmt *fmt, | |
1728 | struct perf_hpp *hpp __maybe_unused, | |
1729 | struct perf_evsel *evsel __maybe_unused) | |
1730 | { | |
1731 | struct hpp_dynamic_entry *hde; | |
1732 | size_t len = fmt->user_len; | |
1733 | ||
1734 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1735 | ||
1736 | if (!len) | |
1737 | len = hde_width(hde); | |
1738 | ||
1739 | return len; | |
1740 | } | |
1741 | ||
361459f1 NK |
1742 | bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists) |
1743 | { | |
1744 | struct hpp_dynamic_entry *hde; | |
1745 | ||
1746 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1747 | ||
1748 | return hists_to_evsel(hists) == hde->evsel; | |
1749 | } | |
1750 | ||
c7c2a5e4 NK |
1751 | static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
1752 | struct hist_entry *he) | |
1753 | { | |
1754 | struct hpp_dynamic_entry *hde; | |
1755 | size_t len = fmt->user_len; | |
60517d28 NK |
1756 | char *str, *pos; |
1757 | struct format_field *field; | |
1758 | size_t namelen; | |
1759 | bool last = false; | |
c7c2a5e4 NK |
1760 | int ret; |
1761 | ||
1762 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1763 | ||
1764 | if (!len) | |
1765 | len = hde_width(hde); | |
1766 | ||
053a3989 NK |
1767 | if (hde->raw_trace) |
1768 | goto raw_field; | |
60517d28 | 1769 | |
e049d4a3 NK |
1770 | if (!he->trace_output) |
1771 | he->trace_output = get_trace_output(he); | |
1772 | ||
053a3989 | 1773 | field = hde->field; |
60517d28 NK |
1774 | namelen = strlen(field->name); |
1775 | str = he->trace_output; | |
1776 | ||
1777 | while (str) { | |
1778 | pos = strchr(str, ' '); | |
1779 | if (pos == NULL) { | |
1780 | last = true; | |
1781 | pos = str + strlen(str); | |
1782 | } | |
1783 | ||
1784 | if (!strncmp(str, field->name, namelen)) { | |
1785 | str += namelen + 1; | |
1786 | str = strndup(str, pos - str); | |
1787 | ||
1788 | if (str == NULL) | |
1789 | return scnprintf(hpp->buf, hpp->size, | |
1790 | "%*.*s", len, len, "ERROR"); | |
1791 | break; | |
1792 | } | |
1793 | ||
1794 | if (last) | |
1795 | str = NULL; | |
1796 | else | |
1797 | str = pos + 1; | |
1798 | } | |
1799 | ||
1800 | if (str == NULL) { | |
1801 | struct trace_seq seq; | |
053a3989 | 1802 | raw_field: |
60517d28 NK |
1803 | trace_seq_init(&seq); |
1804 | pevent_print_field(&seq, he->raw_data, hde->field); | |
1805 | str = seq.buffer; | |
1806 | } | |
1807 | ||
1808 | ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str); | |
1809 | free(str); | |
c7c2a5e4 NK |
1810 | return ret; |
1811 | } | |
1812 | ||
1813 | static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt, | |
1814 | struct hist_entry *a, struct hist_entry *b) | |
1815 | { | |
1816 | struct hpp_dynamic_entry *hde; | |
1817 | struct format_field *field; | |
1818 | unsigned offset, size; | |
1819 | ||
1820 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1821 | ||
abab5e7f NK |
1822 | if (b == NULL) { |
1823 | update_dynamic_len(hde, a); | |
1824 | return 0; | |
1825 | } | |
1826 | ||
c7c2a5e4 NK |
1827 | field = hde->field; |
1828 | if (field->flags & FIELD_IS_DYNAMIC) { | |
1829 | unsigned long long dyn; | |
1830 | ||
1831 | pevent_read_number_field(field, a->raw_data, &dyn); | |
1832 | offset = dyn & 0xffff; | |
1833 | size = (dyn >> 16) & 0xffff; | |
1834 | ||
1835 | /* record max width for output */ | |
1836 | if (size > hde->dynamic_len) | |
1837 | hde->dynamic_len = size; | |
1838 | } else { | |
1839 | offset = field->offset; | |
1840 | size = field->size; | |
1841 | } | |
1842 | ||
1843 | return memcmp(a->raw_data + offset, b->raw_data + offset, size); | |
1844 | } | |
1845 | ||
361459f1 NK |
1846 | bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt) |
1847 | { | |
1848 | return fmt->cmp == __sort__hde_cmp; | |
1849 | } | |
1850 | ||
665aa757 NK |
1851 | static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b) |
1852 | { | |
1853 | struct hpp_dynamic_entry *hde_a; | |
1854 | struct hpp_dynamic_entry *hde_b; | |
1855 | ||
1856 | if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b)) | |
1857 | return false; | |
1858 | ||
1859 | hde_a = container_of(a, struct hpp_dynamic_entry, hpp); | |
1860 | hde_b = container_of(b, struct hpp_dynamic_entry, hpp); | |
1861 | ||
1862 | return hde_a->field == hde_b->field; | |
1863 | } | |
1864 | ||
564132f3 JO |
1865 | static void hde_free(struct perf_hpp_fmt *fmt) |
1866 | { | |
1867 | struct hpp_dynamic_entry *hde; | |
1868 | ||
1869 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1870 | free(hde); | |
1871 | } | |
1872 | ||
c7c2a5e4 | 1873 | static struct hpp_dynamic_entry * |
4b633eba NK |
1874 | __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field, |
1875 | int level) | |
c7c2a5e4 NK |
1876 | { |
1877 | struct hpp_dynamic_entry *hde; | |
1878 | ||
1879 | hde = malloc(sizeof(*hde)); | |
1880 | if (hde == NULL) { | |
1881 | pr_debug("Memory allocation failed\n"); | |
1882 | return NULL; | |
1883 | } | |
1884 | ||
1885 | hde->evsel = evsel; | |
1886 | hde->field = field; | |
1887 | hde->dynamic_len = 0; | |
1888 | ||
1889 | hde->hpp.name = field->name; | |
1890 | hde->hpp.header = __sort__hde_header; | |
1891 | hde->hpp.width = __sort__hde_width; | |
1892 | hde->hpp.entry = __sort__hde_entry; | |
1893 | hde->hpp.color = NULL; | |
1894 | ||
1895 | hde->hpp.cmp = __sort__hde_cmp; | |
1896 | hde->hpp.collapse = __sort__hde_cmp; | |
1897 | hde->hpp.sort = __sort__hde_cmp; | |
665aa757 | 1898 | hde->hpp.equal = __sort__hde_equal; |
564132f3 | 1899 | hde->hpp.free = hde_free; |
c7c2a5e4 NK |
1900 | |
1901 | INIT_LIST_HEAD(&hde->hpp.list); | |
1902 | INIT_LIST_HEAD(&hde->hpp.sort_list); | |
1903 | hde->hpp.elide = false; | |
1904 | hde->hpp.len = 0; | |
1905 | hde->hpp.user_len = 0; | |
4b633eba | 1906 | hde->hpp.level = level; |
c7c2a5e4 NK |
1907 | |
1908 | return hde; | |
1909 | } | |
1910 | ||
c3bc0c43 NK |
1911 | struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt) |
1912 | { | |
1913 | struct perf_hpp_fmt *new_fmt = NULL; | |
1914 | ||
1915 | if (perf_hpp__is_sort_entry(fmt)) { | |
1916 | struct hpp_sort_entry *hse, *new_hse; | |
1917 | ||
1918 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
1919 | new_hse = memdup(hse, sizeof(*hse)); | |
1920 | if (new_hse) | |
1921 | new_fmt = &new_hse->hpp; | |
1922 | } else if (perf_hpp__is_dynamic_entry(fmt)) { | |
1923 | struct hpp_dynamic_entry *hde, *new_hde; | |
1924 | ||
1925 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); | |
1926 | new_hde = memdup(hde, sizeof(*hde)); | |
1927 | if (new_hde) | |
1928 | new_fmt = &new_hde->hpp; | |
1929 | } else { | |
1930 | new_fmt = memdup(fmt, sizeof(*fmt)); | |
1931 | } | |
1932 | ||
1933 | INIT_LIST_HEAD(&new_fmt->list); | |
1934 | INIT_LIST_HEAD(&new_fmt->sort_list); | |
1935 | ||
1936 | return new_fmt; | |
1937 | } | |
1938 | ||
5d0cff93 NK |
1939 | static int parse_field_name(char *str, char **event, char **field, char **opt) |
1940 | { | |
1941 | char *event_name, *field_name, *opt_name; | |
1942 | ||
1943 | event_name = str; | |
1944 | field_name = strchr(str, '.'); | |
1945 | ||
1946 | if (field_name) { | |
1947 | *field_name++ = '\0'; | |
1948 | } else { | |
1949 | event_name = NULL; | |
1950 | field_name = str; | |
1951 | } | |
1952 | ||
1953 | opt_name = strchr(field_name, '/'); | |
1954 | if (opt_name) | |
1955 | *opt_name++ = '\0'; | |
1956 | ||
1957 | *event = event_name; | |
1958 | *field = field_name; | |
1959 | *opt = opt_name; | |
1960 | ||
1961 | return 0; | |
1962 | } | |
1963 | ||
1964 | /* find match evsel using a given event name. The event name can be: | |
9735be24 NK |
1965 | * 1. '%' + event index (e.g. '%1' for first event) |
1966 | * 2. full event name (e.g. sched:sched_switch) | |
1967 | * 3. partial event name (should not contain ':') | |
5d0cff93 NK |
1968 | */ |
1969 | static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name) | |
1970 | { | |
1971 | struct perf_evsel *evsel = NULL; | |
1972 | struct perf_evsel *pos; | |
1973 | bool full_name; | |
1974 | ||
1975 | /* case 1 */ | |
5d0cff93 NK |
1976 | if (event_name[0] == '%') { |
1977 | int nr = strtol(event_name+1, NULL, 0); | |
1978 | ||
1979 | if (nr > evlist->nr_entries) | |
1980 | return NULL; | |
1981 | ||
1982 | evsel = perf_evlist__first(evlist); | |
1983 | while (--nr > 0) | |
1984 | evsel = perf_evsel__next(evsel); | |
1985 | ||
1986 | return evsel; | |
1987 | } | |
1988 | ||
1989 | full_name = !!strchr(event_name, ':'); | |
1990 | evlist__for_each(evlist, pos) { | |
9735be24 | 1991 | /* case 2 */ |
5d0cff93 NK |
1992 | if (full_name && !strcmp(pos->name, event_name)) |
1993 | return pos; | |
9735be24 | 1994 | /* case 3 */ |
5d0cff93 NK |
1995 | if (!full_name && strstr(pos->name, event_name)) { |
1996 | if (evsel) { | |
1997 | pr_debug("'%s' event is ambiguous: it can be %s or %s\n", | |
1998 | event_name, evsel->name, pos->name); | |
1999 | return NULL; | |
2000 | } | |
2001 | evsel = pos; | |
2002 | } | |
2003 | } | |
2004 | ||
2005 | return evsel; | |
2006 | } | |
2007 | ||
3b099bf5 NK |
2008 | static int __dynamic_dimension__add(struct perf_evsel *evsel, |
2009 | struct format_field *field, | |
4b633eba | 2010 | bool raw_trace, int level) |
3b099bf5 NK |
2011 | { |
2012 | struct hpp_dynamic_entry *hde; | |
2013 | ||
4b633eba | 2014 | hde = __alloc_dynamic_entry(evsel, field, level); |
3b099bf5 NK |
2015 | if (hde == NULL) |
2016 | return -ENOMEM; | |
2017 | ||
2018 | hde->raw_trace = raw_trace; | |
2019 | ||
2020 | perf_hpp__register_sort_field(&hde->hpp); | |
2021 | return 0; | |
2022 | } | |
2023 | ||
4b633eba | 2024 | static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level) |
2e422fd1 NK |
2025 | { |
2026 | int ret; | |
2027 | struct format_field *field; | |
2028 | ||
2029 | field = evsel->tp_format->format.fields; | |
2030 | while (field) { | |
4b633eba | 2031 | ret = __dynamic_dimension__add(evsel, field, raw_trace, level); |
2e422fd1 NK |
2032 | if (ret < 0) |
2033 | return ret; | |
2034 | ||
2035 | field = field->next; | |
2036 | } | |
2037 | return 0; | |
2038 | } | |
2039 | ||
4b633eba NK |
2040 | static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace, |
2041 | int level) | |
2e422fd1 NK |
2042 | { |
2043 | int ret; | |
2044 | struct perf_evsel *evsel; | |
2045 | ||
2046 | evlist__for_each(evlist, evsel) { | |
2047 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) | |
2048 | continue; | |
2049 | ||
4b633eba | 2050 | ret = add_evsel_fields(evsel, raw_trace, level); |
2e422fd1 NK |
2051 | if (ret < 0) |
2052 | return ret; | |
2053 | } | |
2054 | return 0; | |
2055 | } | |
2056 | ||
9735be24 | 2057 | static int add_all_matching_fields(struct perf_evlist *evlist, |
4b633eba | 2058 | char *field_name, bool raw_trace, int level) |
9735be24 NK |
2059 | { |
2060 | int ret = -ESRCH; | |
2061 | struct perf_evsel *evsel; | |
2062 | struct format_field *field; | |
2063 | ||
2064 | evlist__for_each(evlist, evsel) { | |
2065 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) | |
2066 | continue; | |
2067 | ||
2068 | field = pevent_find_any_field(evsel->tp_format, field_name); | |
2069 | if (field == NULL) | |
2070 | continue; | |
2071 | ||
4b633eba | 2072 | ret = __dynamic_dimension__add(evsel, field, raw_trace, level); |
9735be24 NK |
2073 | if (ret < 0) |
2074 | break; | |
2075 | } | |
2076 | return ret; | |
2077 | } | |
2078 | ||
4b633eba NK |
2079 | static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok, |
2080 | int level) | |
c7c2a5e4 | 2081 | { |
5d0cff93 NK |
2082 | char *str, *event_name, *field_name, *opt_name; |
2083 | struct perf_evsel *evsel; | |
c7c2a5e4 | 2084 | struct format_field *field; |
053a3989 | 2085 | bool raw_trace = symbol_conf.raw_trace; |
c7c2a5e4 NK |
2086 | int ret = 0; |
2087 | ||
2088 | if (evlist == NULL) | |
2089 | return -ENOENT; | |
2090 | ||
2091 | str = strdup(tok); | |
2092 | if (str == NULL) | |
2093 | return -ENOMEM; | |
2094 | ||
5d0cff93 | 2095 | if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) { |
c7c2a5e4 NK |
2096 | ret = -EINVAL; |
2097 | goto out; | |
2098 | } | |
c7c2a5e4 | 2099 | |
5d0cff93 NK |
2100 | if (opt_name) { |
2101 | if (strcmp(opt_name, "raw")) { | |
2102 | pr_debug("unsupported field option %s\n", opt_name); | |
053a3989 NK |
2103 | ret = -EINVAL; |
2104 | goto out; | |
2105 | } | |
2106 | raw_trace = true; | |
2107 | } | |
2108 | ||
2e422fd1 | 2109 | if (!strcmp(field_name, "trace_fields")) { |
4b633eba | 2110 | ret = add_all_dynamic_fields(evlist, raw_trace, level); |
2e422fd1 NK |
2111 | goto out; |
2112 | } | |
2113 | ||
9735be24 | 2114 | if (event_name == NULL) { |
4b633eba | 2115 | ret = add_all_matching_fields(evlist, field_name, raw_trace, level); |
9735be24 NK |
2116 | goto out; |
2117 | } | |
2118 | ||
5d0cff93 | 2119 | evsel = find_evsel(evlist, event_name); |
c7c2a5e4 NK |
2120 | if (evsel == NULL) { |
2121 | pr_debug("Cannot find event: %s\n", event_name); | |
2122 | ret = -ENOENT; | |
2123 | goto out; | |
2124 | } | |
2125 | ||
2126 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) { | |
2127 | pr_debug("%s is not a tracepoint event\n", event_name); | |
2128 | ret = -EINVAL; | |
2129 | goto out; | |
2130 | } | |
2131 | ||
3b099bf5 | 2132 | if (!strcmp(field_name, "*")) { |
4b633eba | 2133 | ret = add_evsel_fields(evsel, raw_trace, level); |
3b099bf5 NK |
2134 | } else { |
2135 | field = pevent_find_any_field(evsel->tp_format, field_name); | |
2136 | if (field == NULL) { | |
2137 | pr_debug("Cannot find event field for %s.%s\n", | |
2138 | event_name, field_name); | |
2139 | return -ENOENT; | |
2140 | } | |
2141 | ||
4b633eba | 2142 | ret = __dynamic_dimension__add(evsel, field, raw_trace, level); |
3b099bf5 | 2143 | } |
c7c2a5e4 NK |
2144 | |
2145 | out: | |
2146 | free(str); | |
2147 | return ret; | |
2148 | } | |
2149 | ||
d7b617f5 JO |
2150 | static int __sort_dimension__add(struct sort_dimension *sd, |
2151 | struct perf_hpp_list *list, | |
2152 | int level) | |
2f532d09 NK |
2153 | { |
2154 | if (sd->taken) | |
8b536999 NK |
2155 | return 0; |
2156 | ||
d7b617f5 | 2157 | if (__sort_dimension__add_hpp_sort(sd, list, level) < 0) |
8b536999 | 2158 | return -1; |
2f532d09 NK |
2159 | |
2160 | if (sd->entry->se_collapse) | |
52225036 | 2161 | list->need_collapse = 1; |
2f532d09 | 2162 | |
2f532d09 | 2163 | sd->taken = 1; |
8b536999 NK |
2164 | |
2165 | return 0; | |
2f532d09 NK |
2166 | } |
2167 | ||
d7b617f5 JO |
2168 | static int __hpp_dimension__add(struct hpp_dimension *hd, |
2169 | struct perf_hpp_list *list, | |
2170 | int level) | |
a2ce067e | 2171 | { |
1945c3e7 | 2172 | struct perf_hpp_fmt *fmt; |
a2ce067e | 2173 | |
1945c3e7 JO |
2174 | if (hd->taken) |
2175 | return 0; | |
2176 | ||
4b633eba | 2177 | fmt = __hpp_dimension__alloc_hpp(hd, level); |
1945c3e7 JO |
2178 | if (!fmt) |
2179 | return -1; | |
2180 | ||
2181 | hd->taken = 1; | |
d7b617f5 | 2182 | perf_hpp_list__register_sort_field(list, fmt); |
a2ce067e NK |
2183 | return 0; |
2184 | } | |
2185 | ||
07600027 JO |
2186 | static int __sort_dimension__add_output(struct perf_hpp_list *list, |
2187 | struct sort_dimension *sd) | |
a7d945bc NK |
2188 | { |
2189 | if (sd->taken) | |
2190 | return 0; | |
2191 | ||
d7b617f5 | 2192 | if (__sort_dimension__add_hpp_output(sd, list) < 0) |
a7d945bc NK |
2193 | return -1; |
2194 | ||
2195 | sd->taken = 1; | |
2196 | return 0; | |
2197 | } | |
2198 | ||
07600027 JO |
2199 | static int __hpp_dimension__add_output(struct perf_hpp_list *list, |
2200 | struct hpp_dimension *hd) | |
a7d945bc | 2201 | { |
1945c3e7 | 2202 | struct perf_hpp_fmt *fmt; |
a7d945bc | 2203 | |
1945c3e7 JO |
2204 | if (hd->taken) |
2205 | return 0; | |
2206 | ||
4b633eba | 2207 | fmt = __hpp_dimension__alloc_hpp(hd, 0); |
1945c3e7 JO |
2208 | if (!fmt) |
2209 | return -1; | |
2210 | ||
2211 | hd->taken = 1; | |
07600027 | 2212 | perf_hpp_list__column_register(list, fmt); |
a7d945bc NK |
2213 | return 0; |
2214 | } | |
2215 | ||
beeaaeb3 JO |
2216 | int hpp_dimension__add_output(unsigned col) |
2217 | { | |
2218 | BUG_ON(col >= PERF_HPP__MAX_INDEX); | |
07600027 | 2219 | return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]); |
beeaaeb3 JO |
2220 | } |
2221 | ||
d7b617f5 | 2222 | static int sort_dimension__add(struct perf_hpp_list *list, const char *tok, |
b8f8eb84 | 2223 | struct perf_evlist *evlist, |
4b633eba | 2224 | int level) |
dd68ada2 JK |
2225 | { |
2226 | unsigned int i; | |
2227 | ||
fc5871ed NK |
2228 | for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) { |
2229 | struct sort_dimension *sd = &common_sort_dimensions[i]; | |
dd68ada2 | 2230 | |
dd68ada2 JK |
2231 | if (strncasecmp(tok, sd->name, strlen(tok))) |
2232 | continue; | |
fc5871ed | 2233 | |
dd68ada2 JK |
2234 | if (sd->entry == &sort_parent) { |
2235 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); | |
2236 | if (ret) { | |
2237 | char err[BUFSIZ]; | |
2238 | ||
2239 | regerror(ret, &parent_regex, err, sizeof(err)); | |
2aefa4f7 ACM |
2240 | pr_err("Invalid regex: %s\n%s", parent_pattern, err); |
2241 | return -EINVAL; | |
dd68ada2 | 2242 | } |
de7e6a7c | 2243 | list->parent = 1; |
930477bd | 2244 | } else if (sd->entry == &sort_sym) { |
2e0453af | 2245 | list->sym = 1; |
94ba462d KL |
2246 | /* |
2247 | * perf diff displays the performance difference amongst | |
2248 | * two or more perf.data files. Those files could come | |
2249 | * from different binaries. So we should not compare | |
2250 | * their ips, but the name of symbol. | |
2251 | */ | |
2252 | if (sort__mode == SORT_MODE__DIFF) | |
2253 | sd->entry->se_collapse = sort__sym_sort; | |
2254 | ||
68f6d022 | 2255 | } else if (sd->entry == &sort_dso) { |
69849fc5 | 2256 | list->dso = 1; |
2e7ea3ab | 2257 | } else if (sd->entry == &sort_socket) { |
35a634f7 | 2258 | list->socket = 1; |
cfd92dad NK |
2259 | } else if (sd->entry == &sort_thread) { |
2260 | sort__has_thread = 1; | |
078b8d4a NK |
2261 | } else if (sd->entry == &sort_comm) { |
2262 | sort__has_comm = 1; | |
dd68ada2 JK |
2263 | } |
2264 | ||
d7b617f5 | 2265 | return __sort_dimension__add(sd, list, level); |
dd68ada2 | 2266 | } |
fc5871ed | 2267 | |
a2ce067e NK |
2268 | for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) { |
2269 | struct hpp_dimension *hd = &hpp_sort_dimensions[i]; | |
2270 | ||
2271 | if (strncasecmp(tok, hd->name, strlen(tok))) | |
2272 | continue; | |
2273 | ||
d7b617f5 | 2274 | return __hpp_dimension__add(hd, list, level); |
a2ce067e NK |
2275 | } |
2276 | ||
fc5871ed NK |
2277 | for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) { |
2278 | struct sort_dimension *sd = &bstack_sort_dimensions[i]; | |
2279 | ||
2280 | if (strncasecmp(tok, sd->name, strlen(tok))) | |
2281 | continue; | |
2282 | ||
55369fc1 | 2283 | if (sort__mode != SORT_MODE__BRANCH) |
fc5871ed NK |
2284 | return -EINVAL; |
2285 | ||
2286 | if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to) | |
2e0453af | 2287 | list->sym = 1; |
fc5871ed | 2288 | |
d7b617f5 | 2289 | __sort_dimension__add(sd, list, level); |
fc5871ed NK |
2290 | return 0; |
2291 | } | |
2292 | ||
afab87b9 NK |
2293 | for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) { |
2294 | struct sort_dimension *sd = &memory_sort_dimensions[i]; | |
2295 | ||
2296 | if (strncasecmp(tok, sd->name, strlen(tok))) | |
2297 | continue; | |
2298 | ||
2299 | if (sort__mode != SORT_MODE__MEMORY) | |
2300 | return -EINVAL; | |
2301 | ||
2302 | if (sd->entry == &sort_mem_daddr_sym) | |
2e0453af | 2303 | list->sym = 1; |
afab87b9 | 2304 | |
d7b617f5 | 2305 | __sort_dimension__add(sd, list, level); |
afab87b9 NK |
2306 | return 0; |
2307 | } | |
2308 | ||
4b633eba | 2309 | if (!add_dynamic_entry(evlist, tok, level)) |
c7c2a5e4 NK |
2310 | return 0; |
2311 | ||
dd68ada2 JK |
2312 | return -ESRCH; |
2313 | } | |
c8829c7a | 2314 | |
d7b617f5 JO |
2315 | static int setup_sort_list(struct perf_hpp_list *list, char *str, |
2316 | struct perf_evlist *evlist) | |
2fbaa390 JO |
2317 | { |
2318 | char *tmp, *tok; | |
2319 | int ret = 0; | |
4b633eba | 2320 | int level = 0; |
a23f37e8 NK |
2321 | int next_level = 1; |
2322 | bool in_group = false; | |
2323 | ||
2324 | do { | |
2325 | tok = str; | |
2326 | tmp = strpbrk(str, "{}, "); | |
2327 | if (tmp) { | |
2328 | if (in_group) | |
2329 | next_level = level; | |
2330 | else | |
2331 | next_level = level + 1; | |
2332 | ||
2333 | if (*tmp == '{') | |
2334 | in_group = true; | |
2335 | else if (*tmp == '}') | |
2336 | in_group = false; | |
2337 | ||
2338 | *tmp = '\0'; | |
2339 | str = tmp + 1; | |
2340 | } | |
2fbaa390 | 2341 | |
a23f37e8 | 2342 | if (*tok) { |
d7b617f5 | 2343 | ret = sort_dimension__add(list, tok, evlist, level); |
a23f37e8 NK |
2344 | if (ret == -EINVAL) { |
2345 | error("Invalid --sort key: `%s'", tok); | |
2346 | break; | |
2347 | } else if (ret == -ESRCH) { | |
2348 | error("Unknown --sort key: `%s'", tok); | |
2349 | break; | |
2350 | } | |
2fbaa390 | 2351 | } |
a23f37e8 NK |
2352 | |
2353 | level = next_level; | |
2354 | } while (tmp); | |
2fbaa390 JO |
2355 | |
2356 | return ret; | |
2357 | } | |
2358 | ||
d49dadea | 2359 | static const char *get_default_sort_order(struct perf_evlist *evlist) |
512ae1bd NK |
2360 | { |
2361 | const char *default_sort_orders[] = { | |
2362 | default_sort_order, | |
2363 | default_branch_sort_order, | |
2364 | default_mem_sort_order, | |
2365 | default_top_sort_order, | |
2366 | default_diff_sort_order, | |
d49dadea | 2367 | default_tracepoint_sort_order, |
512ae1bd | 2368 | }; |
d49dadea NK |
2369 | bool use_trace = true; |
2370 | struct perf_evsel *evsel; | |
512ae1bd NK |
2371 | |
2372 | BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders)); | |
2373 | ||
d49dadea NK |
2374 | if (evlist == NULL) |
2375 | goto out_no_evlist; | |
2376 | ||
2377 | evlist__for_each(evlist, evsel) { | |
2378 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) { | |
2379 | use_trace = false; | |
2380 | break; | |
2381 | } | |
2382 | } | |
2383 | ||
2384 | if (use_trace) { | |
2385 | sort__mode = SORT_MODE__TRACEPOINT; | |
2386 | if (symbol_conf.raw_trace) | |
2387 | return "trace_fields"; | |
2388 | } | |
2389 | out_no_evlist: | |
512ae1bd NK |
2390 | return default_sort_orders[sort__mode]; |
2391 | } | |
2392 | ||
d49dadea | 2393 | static int setup_sort_order(struct perf_evlist *evlist) |
1a1c0ffb JO |
2394 | { |
2395 | char *new_sort_order; | |
2396 | ||
2397 | /* | |
2398 | * Append '+'-prefixed sort order to the default sort | |
2399 | * order string. | |
2400 | */ | |
2401 | if (!sort_order || is_strict_order(sort_order)) | |
2402 | return 0; | |
2403 | ||
2404 | if (sort_order[1] == '\0') { | |
2405 | error("Invalid --sort key: `+'"); | |
2406 | return -EINVAL; | |
2407 | } | |
2408 | ||
2409 | /* | |
2410 | * We allocate new sort_order string, but we never free it, | |
2411 | * because it's checked over the rest of the code. | |
2412 | */ | |
2413 | if (asprintf(&new_sort_order, "%s,%s", | |
d49dadea | 2414 | get_default_sort_order(evlist), sort_order + 1) < 0) { |
1a1c0ffb JO |
2415 | error("Not enough memory to set up --sort"); |
2416 | return -ENOMEM; | |
2417 | } | |
2418 | ||
2419 | sort_order = new_sort_order; | |
2420 | return 0; | |
2421 | } | |
2422 | ||
b97511c5 JO |
2423 | /* |
2424 | * Adds 'pre,' prefix into 'str' is 'pre' is | |
2425 | * not already part of 'str'. | |
2426 | */ | |
2427 | static char *prefix_if_not_in(const char *pre, char *str) | |
2428 | { | |
2429 | char *n; | |
2430 | ||
2431 | if (!str || strstr(str, pre)) | |
2432 | return str; | |
2433 | ||
2434 | if (asprintf(&n, "%s,%s", pre, str) < 0) | |
2435 | return NULL; | |
2436 | ||
2437 | free(str); | |
2438 | return n; | |
2439 | } | |
2440 | ||
2441 | static char *setup_overhead(char *keys) | |
2442 | { | |
2443 | keys = prefix_if_not_in("overhead", keys); | |
2444 | ||
2445 | if (symbol_conf.cumulate_callchain) | |
2446 | keys = prefix_if_not_in("overhead_children", keys); | |
2447 | ||
2448 | return keys; | |
2449 | } | |
2450 | ||
40184c46 | 2451 | static int __setup_sorting(struct perf_evlist *evlist) |
c8829c7a | 2452 | { |
2fbaa390 | 2453 | char *str; |
1a1c0ffb | 2454 | const char *sort_keys; |
55309985 | 2455 | int ret = 0; |
c8829c7a | 2456 | |
d49dadea | 2457 | ret = setup_sort_order(evlist); |
1a1c0ffb JO |
2458 | if (ret) |
2459 | return ret; | |
2460 | ||
2461 | sort_keys = sort_order; | |
a7d945bc | 2462 | if (sort_keys == NULL) { |
2f3f9bcf | 2463 | if (is_strict_order(field_order)) { |
a7d945bc NK |
2464 | /* |
2465 | * If user specified field order but no sort order, | |
2466 | * we'll honor it and not add default sort orders. | |
2467 | */ | |
2468 | return 0; | |
2469 | } | |
2470 | ||
d49dadea | 2471 | sort_keys = get_default_sort_order(evlist); |
a7d945bc | 2472 | } |
512ae1bd NK |
2473 | |
2474 | str = strdup(sort_keys); | |
5936f54d NK |
2475 | if (str == NULL) { |
2476 | error("Not enough memory to setup sort keys"); | |
2477 | return -ENOMEM; | |
2478 | } | |
2479 | ||
b97511c5 JO |
2480 | /* |
2481 | * Prepend overhead fields for backward compatibility. | |
2482 | */ | |
2483 | if (!is_strict_order(field_order)) { | |
2484 | str = setup_overhead(str); | |
2485 | if (str == NULL) { | |
2486 | error("Not enough memory to setup overhead keys"); | |
2487 | return -ENOMEM; | |
2488 | } | |
2489 | } | |
2490 | ||
d7b617f5 | 2491 | ret = setup_sort_list(&perf_hpp_list, str, evlist); |
c8829c7a ACM |
2492 | |
2493 | free(str); | |
55309985 | 2494 | return ret; |
c8829c7a | 2495 | } |
c351c281 | 2496 | |
f2998422 | 2497 | void perf_hpp__set_elide(int idx, bool elide) |
e67d49a7 | 2498 | { |
f2998422 JO |
2499 | struct perf_hpp_fmt *fmt; |
2500 | struct hpp_sort_entry *hse; | |
e67d49a7 | 2501 | |
cf094045 | 2502 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
f2998422 JO |
2503 | if (!perf_hpp__is_sort_entry(fmt)) |
2504 | continue; | |
2505 | ||
2506 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
2507 | if (hse->se->se_width_idx == idx) { | |
2508 | fmt->elide = elide; | |
2509 | break; | |
2510 | } | |
e67d49a7 | 2511 | } |
e67d49a7 NK |
2512 | } |
2513 | ||
f2998422 | 2514 | static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp) |
c351c281 ACM |
2515 | { |
2516 | if (list && strlist__nr_entries(list) == 1) { | |
2517 | if (fp != NULL) | |
2518 | fprintf(fp, "# %s: %s\n", list_name, | |
2519 | strlist__entry(list, 0)->s); | |
f2998422 JO |
2520 | return true; |
2521 | } | |
2522 | return false; | |
2523 | } | |
2524 | ||
2525 | static bool get_elide(int idx, FILE *output) | |
2526 | { | |
2527 | switch (idx) { | |
2528 | case HISTC_SYMBOL: | |
2529 | return __get_elide(symbol_conf.sym_list, "symbol", output); | |
2530 | case HISTC_DSO: | |
2531 | return __get_elide(symbol_conf.dso_list, "dso", output); | |
2532 | case HISTC_COMM: | |
2533 | return __get_elide(symbol_conf.comm_list, "comm", output); | |
2534 | default: | |
2535 | break; | |
c351c281 | 2536 | } |
f2998422 JO |
2537 | |
2538 | if (sort__mode != SORT_MODE__BRANCH) | |
2539 | return false; | |
2540 | ||
2541 | switch (idx) { | |
2542 | case HISTC_SYMBOL_FROM: | |
2543 | return __get_elide(symbol_conf.sym_from_list, "sym_from", output); | |
2544 | case HISTC_SYMBOL_TO: | |
2545 | return __get_elide(symbol_conf.sym_to_list, "sym_to", output); | |
2546 | case HISTC_DSO_FROM: | |
2547 | return __get_elide(symbol_conf.dso_from_list, "dso_from", output); | |
2548 | case HISTC_DSO_TO: | |
2549 | return __get_elide(symbol_conf.dso_to_list, "dso_to", output); | |
2550 | default: | |
2551 | break; | |
2552 | } | |
2553 | ||
2554 | return false; | |
c351c281 | 2555 | } |
08e71542 NK |
2556 | |
2557 | void sort__setup_elide(FILE *output) | |
2558 | { | |
cfaa154b NK |
2559 | struct perf_hpp_fmt *fmt; |
2560 | struct hpp_sort_entry *hse; | |
7524f63b | 2561 | |
cf094045 | 2562 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
f2998422 JO |
2563 | if (!perf_hpp__is_sort_entry(fmt)) |
2564 | continue; | |
2565 | ||
2566 | hse = container_of(fmt, struct hpp_sort_entry, hpp); | |
2567 | fmt->elide = get_elide(hse->se->se_width_idx, output); | |
08e71542 NK |
2568 | } |
2569 | ||
7524f63b NK |
2570 | /* |
2571 | * It makes no sense to elide all of sort entries. | |
2572 | * Just revert them to show up again. | |
2573 | */ | |
cf094045 | 2574 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
cfaa154b NK |
2575 | if (!perf_hpp__is_sort_entry(fmt)) |
2576 | continue; | |
2577 | ||
f2998422 | 2578 | if (!fmt->elide) |
7524f63b NK |
2579 | return; |
2580 | } | |
2581 | ||
cf094045 | 2582 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
cfaa154b NK |
2583 | if (!perf_hpp__is_sort_entry(fmt)) |
2584 | continue; | |
2585 | ||
f2998422 | 2586 | fmt->elide = false; |
cfaa154b | 2587 | } |
08e71542 | 2588 | } |
a7d945bc | 2589 | |
07600027 | 2590 | static int output_field_add(struct perf_hpp_list *list, char *tok) |
a7d945bc NK |
2591 | { |
2592 | unsigned int i; | |
2593 | ||
2594 | for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) { | |
2595 | struct sort_dimension *sd = &common_sort_dimensions[i]; | |
2596 | ||
2597 | if (strncasecmp(tok, sd->name, strlen(tok))) | |
2598 | continue; | |
2599 | ||
07600027 | 2600 | return __sort_dimension__add_output(list, sd); |
a7d945bc NK |
2601 | } |
2602 | ||
2603 | for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) { | |
2604 | struct hpp_dimension *hd = &hpp_sort_dimensions[i]; | |
2605 | ||
2606 | if (strncasecmp(tok, hd->name, strlen(tok))) | |
2607 | continue; | |
2608 | ||
07600027 | 2609 | return __hpp_dimension__add_output(list, hd); |
a7d945bc NK |
2610 | } |
2611 | ||
2612 | for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) { | |
2613 | struct sort_dimension *sd = &bstack_sort_dimensions[i]; | |
2614 | ||
2615 | if (strncasecmp(tok, sd->name, strlen(tok))) | |
2616 | continue; | |
2617 | ||
07600027 | 2618 | return __sort_dimension__add_output(list, sd); |
a7d945bc NK |
2619 | } |
2620 | ||
2621 | for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) { | |
2622 | struct sort_dimension *sd = &memory_sort_dimensions[i]; | |
2623 | ||
2624 | if (strncasecmp(tok, sd->name, strlen(tok))) | |
2625 | continue; | |
2626 | ||
07600027 | 2627 | return __sort_dimension__add_output(list, sd); |
a7d945bc NK |
2628 | } |
2629 | ||
2630 | return -ESRCH; | |
2631 | } | |
2632 | ||
07600027 | 2633 | static int setup_output_list(struct perf_hpp_list *list, char *str) |
6d3375ef JO |
2634 | { |
2635 | char *tmp, *tok; | |
2636 | int ret = 0; | |
2637 | ||
2638 | for (tok = strtok_r(str, ", ", &tmp); | |
2639 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
07600027 | 2640 | ret = output_field_add(list, tok); |
6d3375ef JO |
2641 | if (ret == -EINVAL) { |
2642 | error("Invalid --fields key: `%s'", tok); | |
2643 | break; | |
2644 | } else if (ret == -ESRCH) { | |
2645 | error("Unknown --fields key: `%s'", tok); | |
2646 | break; | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | return ret; | |
2651 | } | |
2652 | ||
a7d945bc NK |
2653 | static void reset_dimensions(void) |
2654 | { | |
2655 | unsigned int i; | |
2656 | ||
2657 | for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) | |
2658 | common_sort_dimensions[i].taken = 0; | |
2659 | ||
2660 | for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) | |
2661 | hpp_sort_dimensions[i].taken = 0; | |
2662 | ||
2663 | for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) | |
2664 | bstack_sort_dimensions[i].taken = 0; | |
2665 | ||
2666 | for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) | |
2667 | memory_sort_dimensions[i].taken = 0; | |
2668 | } | |
2669 | ||
2f3f9bcf JO |
2670 | bool is_strict_order(const char *order) |
2671 | { | |
2672 | return order && (*order != '+'); | |
2673 | } | |
2674 | ||
a7d945bc NK |
2675 | static int __setup_output_field(void) |
2676 | { | |
6d3375ef | 2677 | char *str, *strp; |
2f3f9bcf | 2678 | int ret = -EINVAL; |
a7d945bc NK |
2679 | |
2680 | if (field_order == NULL) | |
2681 | return 0; | |
2682 | ||
2f3f9bcf | 2683 | strp = str = strdup(field_order); |
a7d945bc NK |
2684 | if (str == NULL) { |
2685 | error("Not enough memory to setup output fields"); | |
2686 | return -ENOMEM; | |
2687 | } | |
2688 | ||
2f3f9bcf JO |
2689 | if (!is_strict_order(field_order)) |
2690 | strp++; | |
2691 | ||
2692 | if (!strlen(strp)) { | |
2693 | error("Invalid --fields key: `+'"); | |
2694 | goto out; | |
2695 | } | |
2696 | ||
07600027 | 2697 | ret = setup_output_list(&perf_hpp_list, strp); |
a7d945bc | 2698 | |
2f3f9bcf | 2699 | out: |
a7d945bc NK |
2700 | free(str); |
2701 | return ret; | |
2702 | } | |
2703 | ||
9b240637 ACM |
2704 | int setup_sorting(struct perf_evlist *evlist) |
2705 | { | |
2706 | int err; | |
2707 | ||
2708 | err = __setup_sorting(evlist); | |
2709 | if (err < 0) | |
2710 | return err; | |
2711 | ||
2712 | if (parent_pattern != default_parent_pattern) { | |
d7b617f5 | 2713 | err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1); |
9b240637 ACM |
2714 | if (err < 0) |
2715 | return err; | |
2716 | } | |
2717 | ||
a7d945bc NK |
2718 | reset_dimensions(); |
2719 | ||
2720 | /* | |
2721 | * perf diff doesn't use default hpp output fields. | |
2722 | */ | |
2723 | if (sort__mode != SORT_MODE__DIFF) | |
2724 | perf_hpp__init(); | |
2725 | ||
2726 | err = __setup_output_field(); | |
2727 | if (err < 0) | |
2728 | return err; | |
2729 | ||
2730 | /* copy sort keys to output fields */ | |
43e0a68f | 2731 | perf_hpp__setup_output_field(&perf_hpp_list); |
a7d945bc | 2732 | /* and then copy output fields to sort keys */ |
43e0a68f | 2733 | perf_hpp__append_sort_keys(&perf_hpp_list); |
a7d945bc | 2734 | |
c3bc0c43 NK |
2735 | /* setup hists-specific output fields */ |
2736 | if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0) | |
2737 | return -1; | |
2738 | ||
a7d945bc NK |
2739 | return 0; |
2740 | } | |
1c89fe9b NK |
2741 | |
2742 | void reset_output_field(void) | |
2743 | { | |
52225036 | 2744 | perf_hpp_list.need_collapse = 0; |
de7e6a7c | 2745 | perf_hpp_list.parent = 0; |
2e0453af | 2746 | perf_hpp_list.sym = 0; |
69849fc5 | 2747 | perf_hpp_list.dso = 0; |
1c89fe9b | 2748 | |
d69b2962 NK |
2749 | field_order = NULL; |
2750 | sort_order = NULL; | |
2751 | ||
1c89fe9b | 2752 | reset_dimensions(); |
43e0a68f | 2753 | perf_hpp__reset_output_field(&perf_hpp_list); |
1c89fe9b | 2754 | } |