blob: 50c8fece1681ca783cfcc2bfc4c0e8e1313d2bf0 [file] [log] [blame]
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001#include "annotate.h"
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02002#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02003#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02004#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02005#include "session.h"
6#include "sort.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02007#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02008
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -03009enum hist_filter {
10 HIST_FILTER__DSO,
11 HIST_FILTER__THREAD,
12 HIST_FILTER__PARENT,
13};
14
John Kacur3d1d07e2009-09-28 15:32:55 +020015struct callchain_param callchain_param = {
16 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080017 .min_percent = 0.5,
18 .order = ORDER_CALLEE
John Kacur3d1d07e2009-09-28 15:32:55 +020019};
20
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030021u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030022{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030023 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030024}
25
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029}
30
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035 return true;
36 }
37 return false;
38}
39
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030040static void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041{
42 enum hist_column col;
43
44 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030045 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030046}
47
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030048static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030049{
50 u16 len;
51
52 if (h->ms.sym)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030053 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen);
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -030054 else {
55 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
56
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030057 if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width &&
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -030058 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
59 !symbol_conf.dso_list)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030060 hists__set_col_len(hists, HISTC_DSO,
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -030061 unresolved_col_width);
62 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030063
64 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030065 if (hists__new_col_len(hists, HISTC_COMM, len))
66 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030067
68 if (h->ms.map) {
69 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030070 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030071 }
72}
73
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030074static void hist_entry__add_cpumode_period(struct hist_entry *self,
75 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080076{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030077 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080078 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030079 self->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080080 break;
81 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030082 self->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080083 break;
84 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030085 self->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080086 break;
87 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030088 self->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080089 break;
90 default:
91 break;
92 }
93}
94
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -030095static void hist_entry__decay(struct hist_entry *he)
96{
97 he->period = (he->period * 7) / 8;
98 he->nr_events = (he->nr_events * 7) / 8;
99}
100
101static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
102{
103 hists->stats.total_period -= he->period;
104 hist_entry__decay(he);
105 hists->stats.total_period += he->period;
106 return he->period == 0;
107}
108
109void hists__decay_entries(struct hists *hists)
110{
111 struct rb_node *next = rb_first(&hists->entries);
112 struct hist_entry *n;
113
114 while (next) {
115 n = rb_entry(next, struct hist_entry, rb_node);
116 next = rb_next(&n->rb_node);
117
118 if (hists__decay_entry(hists, n)) {
119 rb_erase(&n->rb_node, &hists->entries);
120
121 if (sort__need_collapse)
122 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
123
124 hist_entry__free(n);
125 --hists->nr_entries;
126 }
127 }
128}
129
John Kacur3d1d07e2009-09-28 15:32:55 +0200130/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300131 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200132 */
133
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300134static struct hist_entry *hist_entry__new(struct hist_entry *template)
135{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200136 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300137 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
138
139 if (self != NULL) {
140 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300141 self->nr_events = 1;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300142 if (self->ms.map)
143 self->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300144 if (symbol_conf.use_callchain)
145 callchain_init(self->callchain);
146 }
147
148 return self;
149}
150
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300151static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300152{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300153 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300154 hists__calc_col_len(hists, h);
155 ++hists->nr_entries;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300156 hists->stats.total_period += h->period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300157 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300158}
159
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300160static u8 symbol__parent_filter(const struct symbol *parent)
161{
162 if (symbol_conf.exclude_other && parent == NULL)
163 return 1 << HIST_FILTER__PARENT;
164 return 0;
165}
166
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300167struct hist_entry *__hists__add_entry(struct hists *hists,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300168 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300169 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300170{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300171 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300172 struct rb_node *parent = NULL;
173 struct hist_entry *he;
174 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200175 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300176 .ms = {
177 .map = al->map,
178 .sym = al->sym,
179 },
Arun Sharmaf60f3592010-06-04 11:27:10 -0300180 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200181 .ip = al->addr,
182 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300183 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300184 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300185 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300186 };
187 int cmp;
188
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300189 pthread_mutex_lock(&hists->lock);
190
191 p = &hists->entries_in->rb_node;
192
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300193 while (*p != NULL) {
194 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300195 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300196
197 cmp = hist_entry__cmp(&entry, he);
198
199 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300200 he->period += period;
201 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300202 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300203 }
204
205 if (cmp < 0)
206 p = &(*p)->rb_left;
207 else
208 p = &(*p)->rb_right;
209 }
210
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300211 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300212 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300213 goto out_unlock;
214
215 rb_link_node(&he->rb_node_in, parent, p);
216 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300217out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300218 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300219out_unlock:
220 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300221 return he;
222}
223
John Kacur3d1d07e2009-09-28 15:32:55 +0200224int64_t
225hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
226{
227 struct sort_entry *se;
228 int64_t cmp = 0;
229
230 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200231 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200232 if (cmp)
233 break;
234 }
235
236 return cmp;
237}
238
239int64_t
240hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
241{
242 struct sort_entry *se;
243 int64_t cmp = 0;
244
245 list_for_each_entry(se, &hist_entry__sort_list, list) {
246 int64_t (*f)(struct hist_entry *, struct hist_entry *);
247
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200248 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200249
250 cmp = f(left, right);
251 if (cmp)
252 break;
253 }
254
255 return cmp;
256}
257
258void hist_entry__free(struct hist_entry *he)
259{
260 free(he);
261}
262
263/*
264 * collapse the histogram
265 */
266
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300267static bool hists__collapse_insert_entry(struct hists *hists,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100268 struct rb_root *root,
269 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200270{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200271 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200272 struct rb_node *parent = NULL;
273 struct hist_entry *iter;
274 int64_t cmp;
275
276 while (*p != NULL) {
277 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300278 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200279
280 cmp = hist_entry__collapse(iter, he);
281
282 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300283 iter->period += he->period;
Stephane Eraniane39622c2011-10-03 11:38:15 +0200284 iter->nr_events += he->nr_events;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100285 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300286 callchain_cursor_reset(&hists->callchain_cursor);
287 callchain_merge(&hists->callchain_cursor, iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100288 he->callchain);
289 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200290 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300291 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200292 }
293
294 if (cmp < 0)
295 p = &(*p)->rb_left;
296 else
297 p = &(*p)->rb_right;
298 }
299
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300300 rb_link_node(&he->rb_node_in, parent, p);
301 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300302 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200303}
304
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300305static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
306{
307 struct rb_root *root;
308
309 pthread_mutex_lock(&hists->lock);
310
311 root = hists->entries_in;
312 if (++hists->entries_in > &hists->entries_in_array[1])
313 hists->entries_in = &hists->entries_in_array[0];
314
315 pthread_mutex_unlock(&hists->lock);
316
317 return root;
318}
319
320static void __hists__collapse_resort(struct hists *hists, bool threaded)
321{
322 struct rb_root *root;
323 struct rb_node *next;
324 struct hist_entry *n;
325
326 if (!sort__need_collapse && !threaded)
327 return;
328
329 root = hists__get_rotate_entries_in(hists);
330 next = rb_first(root);
331 hists->stats.total_period = 0;
332
333 while (next) {
334 n = rb_entry(next, struct hist_entry, rb_node_in);
335 next = rb_next(&n->rb_node_in);
336
337 rb_erase(&n->rb_node_in, root);
338 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n))
339 hists__inc_nr_entries(hists, n);
340 }
341}
342
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300343void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200344{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300345 return __hists__collapse_resort(hists, false);
346}
John Kacur3d1d07e2009-09-28 15:32:55 +0200347
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300348void hists__collapse_resort_threaded(struct hists *hists)
349{
350 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200351}
352
353/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300354 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200355 */
356
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300357static void __hists__insert_output_entry(struct rb_root *entries,
358 struct hist_entry *he,
359 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200360{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300361 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200362 struct rb_node *parent = NULL;
363 struct hist_entry *iter;
364
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200365 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300366 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200367 min_callchain_hits, &callchain_param);
368
369 while (*p != NULL) {
370 parent = *p;
371 iter = rb_entry(parent, struct hist_entry, rb_node);
372
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300373 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200374 p = &(*p)->rb_left;
375 else
376 p = &(*p)->rb_right;
377 }
378
379 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300380 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200381}
382
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300383static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200384{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300385 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200386 struct rb_node *next;
387 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200388 u64 min_callchain_hits;
389
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300390 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200391
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300392 if (sort__need_collapse || threaded)
393 root = &hists->entries_collapsed;
394 else
395 root = hists->entries_in;
396
397 next = rb_first(root);
398 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200399
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300400 hists->nr_entries = 0;
401 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300402
John Kacur3d1d07e2009-09-28 15:32:55 +0200403 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300404 n = rb_entry(next, struct hist_entry, rb_node_in);
405 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200406
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300407 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300408 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200409 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300410}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200411
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300412void hists__output_resort(struct hists *hists)
413{
414 return __hists__output_resort(hists, false);
415}
416
417void hists__output_resort_threaded(struct hists *hists)
418{
419 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200420}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200421
422static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
423{
424 int i;
425 int ret = fprintf(fp, " ");
426
427 for (i = 0; i < left_margin; i++)
428 ret += fprintf(fp, " ");
429
430 return ret;
431}
432
433static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
434 int left_margin)
435{
436 int i;
437 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
438
439 for (i = 0; i < depth; i++)
440 if (depth_mask & (1 << i))
441 ret += fprintf(fp, "| ");
442 else
443 ret += fprintf(fp, " ");
444
445 ret += fprintf(fp, "\n");
446
447 return ret;
448}
449
450static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300451 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100452 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200453 int left_margin)
454{
455 int i;
456 size_t ret = 0;
457
458 ret += callchain__fprintf_left_margin(fp, left_margin);
459 for (i = 0; i < depth; i++) {
460 if (depth_mask & (1 << i))
461 ret += fprintf(fp, "|");
462 else
463 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300464 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200465 double percent;
466
467 percent = hits * 100.0 / total_samples;
468 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
469 } else
470 ret += fprintf(fp, "%s", " ");
471 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300472 if (chain->ms.sym)
473 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200474 else
475 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
476
477 return ret;
478}
479
480static struct symbol *rem_sq_bracket;
481static struct callchain_list rem_hits;
482
483static void init_rem_hits(void)
484{
485 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
486 if (!rem_sq_bracket) {
487 fprintf(stderr, "Not enough memory to display remaining hits\n");
488 return;
489 }
490
491 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300492 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200493}
494
495static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
496 u64 total_samples, int depth,
497 int depth_mask, int left_margin)
498{
499 struct rb_node *node, *next;
500 struct callchain_node *child;
501 struct callchain_list *chain;
502 int new_depth_mask = depth_mask;
503 u64 new_total;
504 u64 remaining;
505 size_t ret = 0;
506 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300507 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200508
509 if (callchain_param.mode == CHAIN_GRAPH_REL)
510 new_total = self->children_hit;
511 else
512 new_total = total_samples;
513
514 remaining = new_total;
515
516 node = rb_first(&self->rb_root);
517 while (node) {
518 u64 cumul;
519
520 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100521 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200522 remaining -= cumul;
523
524 /*
525 * The depth mask manages the output of pipes that show
526 * the depth. We don't want to keep the pipes of the current
527 * level for the last child of this depth.
528 * Except if we have remaining filtered hits. They will
529 * supersede the last child
530 */
531 next = rb_next(node);
532 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
533 new_depth_mask &= ~(1 << (depth - 1));
534
535 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800536 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200537 * to keep the level link until we reach the last child
538 */
539 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
540 left_margin);
541 i = 0;
542 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200543 ret += ipchain__fprintf_graph(fp, chain, depth,
544 new_depth_mask, i++,
545 new_total,
546 cumul,
547 left_margin);
548 }
549 ret += __callchain__fprintf_graph(fp, child, new_total,
550 depth + 1,
551 new_depth_mask | (1 << depth),
552 left_margin);
553 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300554 if (++entries_printed == callchain_param.print_limit)
555 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200556 }
557
558 if (callchain_param.mode == CHAIN_GRAPH_REL &&
559 remaining && remaining != new_total) {
560
561 if (!rem_sq_bracket)
562 return ret;
563
564 new_depth_mask &= ~(1 << (depth - 1));
565
566 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
567 new_depth_mask, 0, new_total,
568 remaining, left_margin);
569 }
570
571 return ret;
572}
573
574static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
575 u64 total_samples, int left_margin)
576{
577 struct callchain_list *chain;
578 bool printed = false;
579 int i = 0;
580 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300581 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200582
583 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200584 if (!i++ && sort__first_dimension == SORT_SYM)
585 continue;
586
587 if (!printed) {
588 ret += callchain__fprintf_left_margin(fp, left_margin);
589 ret += fprintf(fp, "|\n");
590 ret += callchain__fprintf_left_margin(fp, left_margin);
591 ret += fprintf(fp, "---");
592
593 left_margin += 3;
594 printed = true;
595 } else
596 ret += callchain__fprintf_left_margin(fp, left_margin);
597
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300598 if (chain->ms.sym)
599 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200600 else
601 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300602
603 if (++entries_printed == callchain_param.print_limit)
604 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200605 }
606
607 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
608
609 return ret;
610}
611
612static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
613 u64 total_samples)
614{
615 struct callchain_list *chain;
616 size_t ret = 0;
617
618 if (!self)
619 return 0;
620
621 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
622
623
624 list_for_each_entry(chain, &self->val, list) {
625 if (chain->ip >= PERF_CONTEXT_MAX)
626 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300627 if (chain->ms.sym)
628 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200629 else
630 ret += fprintf(fp, " %p\n",
631 (void *)(long)chain->ip);
632 }
633
634 return ret;
635}
636
637static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
638 u64 total_samples, int left_margin)
639{
640 struct rb_node *rb_node;
641 struct callchain_node *chain;
642 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300643 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200644
645 rb_node = rb_first(&self->sorted_chain);
646 while (rb_node) {
647 double percent;
648
649 chain = rb_entry(rb_node, struct callchain_node, rb_node);
650 percent = chain->hit * 100.0 / total_samples;
651 switch (callchain_param.mode) {
652 case CHAIN_FLAT:
653 ret += percent_color_fprintf(fp, " %6.2f%%\n",
654 percent);
655 ret += callchain__fprintf_flat(fp, chain, total_samples);
656 break;
657 case CHAIN_GRAPH_ABS: /* Falldown */
658 case CHAIN_GRAPH_REL:
659 ret += callchain__fprintf_graph(fp, chain, total_samples,
660 left_margin);
661 case CHAIN_NONE:
662 default:
663 break;
664 }
665 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300666 if (++entries_printed == callchain_param.print_limit)
667 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200668 rb_node = rb_next(rb_node);
669 }
670
671 return ret;
672}
673
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300674void hists__output_recalc_col_len(struct hists *hists, int max_rows)
675{
676 struct rb_node *next = rb_first(&hists->entries);
677 struct hist_entry *n;
678 int row = 0;
679
680 hists__reset_col_len(hists);
681
682 while (next && row++ < max_rows) {
683 n = rb_entry(next, struct hist_entry, rb_node);
684 hists__calc_col_len(hists, n);
685 next = rb_next(&n->rb_node);
686 }
687}
688
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300689int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300690 struct hists *hists, struct hists *pair_hists,
691 bool show_displacement, long displacement,
692 bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200693{
694 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300695 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200696 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200697 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300698 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200699
700 if (symbol_conf.exclude_other && !self->parent)
701 return 0;
702
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300703 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300704 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200705 nr_events = self->pair ? self->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300706 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300707 period_sys = self->pair ? self->pair->period_sys : 0;
708 period_us = self->pair ? self->pair->period_us : 0;
709 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
710 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200711 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300712 period = self->period;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200713 nr_events = self->nr_events;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300714 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300715 period_sys = self->period_sys;
716 period_us = self->period_us;
717 period_guest_sys = self->period_guest_sys;
718 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200719 }
720
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300721 if (total) {
722 if (color)
723 ret = percent_color_snprintf(s, size,
724 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300725 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300726 else
727 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300728 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800729 if (symbol_conf.show_cpu_utilization) {
730 ret += percent_color_snprintf(s + ret, size - ret,
731 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300732 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800733 ret += percent_color_snprintf(s + ret, size - ret,
734 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300735 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800736 if (perf_guest) {
737 ret += percent_color_snprintf(s + ret,
738 size - ret,
739 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300740 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800741 total);
742 ret += percent_color_snprintf(s + ret,
743 size - ret,
744 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300745 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800746 total);
747 }
748 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300749 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200750 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200751
752 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200753 if (sep)
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200754 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200755 else
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200756 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200757 }
758
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300759 if (symbol_conf.show_total_period) {
760 if (sep)
761 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
762 else
763 ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
764 }
765
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300766 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200767 char bf[32];
768 double old_percent = 0, new_percent = 0, diff;
769
770 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300771 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300772 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300773 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200774
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200775 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200776
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200777 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200778 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
779 else
780 snprintf(bf, sizeof(bf), " ");
781
782 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300783 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200784 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300785 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200786
787 if (show_displacement) {
788 if (displacement)
789 snprintf(bf, sizeof(bf), "%+4ld", displacement);
790 else
791 snprintf(bf, sizeof(bf), " ");
792
793 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300794 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200795 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300796 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200797 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200798 }
799
800 list_for_each_entry(se, &hist_entry__sort_list, list) {
801 if (se->elide)
802 continue;
803
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300804 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200805 ret += se->se_snprintf(self, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300806 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200807 }
808
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300809 return ret;
810}
811
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300812int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300813 struct hists *pair_hists, bool show_displacement,
814 long displacement, FILE *fp, u64 session_total)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300815{
816 char bf[512];
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300817
818 if (size == 0 || size > sizeof(bf))
819 size = sizeof(bf);
820
821 hist_entry__snprintf(he, bf, size, hists, pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300822 show_displacement, displacement,
823 true, session_total);
824 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300825}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200826
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300827static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
828 struct hists *hists, FILE *fp,
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300829 u64 session_total)
830{
831 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200832
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300833 if (sort__first_dimension == SORT_COMM) {
834 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
835 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300836 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300837 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200838 }
839
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300840 return hist_entry_callchain__fprintf(fp, self, session_total,
841 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200842}
843
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300844size_t hists__fprintf(struct hists *hists, struct hists *pair,
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300845 bool show_displacement, bool show_header, int max_rows,
846 int max_cols, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200847{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200848 struct sort_entry *se;
849 struct rb_node *nd;
850 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200851 unsigned long position = 1;
852 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200853 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200854 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300855 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300856 int nr_rows = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200857
858 init_rem_hits();
859
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300860 if (!show_header)
861 goto print_entries;
862
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200863 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
864
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200865 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200866 if (sep)
867 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200868 else
869 fputs(" Samples ", fp);
870 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200871
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300872 if (symbol_conf.show_total_period) {
873 if (sep)
874 ret += fprintf(fp, "%cPeriod", *sep);
875 else
876 ret += fprintf(fp, " Period ");
877 }
878
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800879 if (symbol_conf.show_cpu_utilization) {
880 if (sep) {
881 ret += fprintf(fp, "%csys", *sep);
882 ret += fprintf(fp, "%cus", *sep);
883 if (perf_guest) {
884 ret += fprintf(fp, "%cguest sys", *sep);
885 ret += fprintf(fp, "%cguest us", *sep);
886 }
887 } else {
888 ret += fprintf(fp, " sys ");
889 ret += fprintf(fp, " us ");
890 if (perf_guest) {
891 ret += fprintf(fp, " guest sys ");
892 ret += fprintf(fp, " guest us ");
893 }
894 }
895 }
896
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200897 if (pair) {
898 if (sep)
899 ret += fprintf(fp, "%cDelta", *sep);
900 else
901 ret += fprintf(fp, " Delta ");
902
903 if (show_displacement) {
904 if (sep)
905 ret += fprintf(fp, "%cDisplacement", *sep);
906 else
907 ret += fprintf(fp, " Displ");
908 }
909 }
910
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200911 list_for_each_entry(se, &hist_entry__sort_list, list) {
912 if (se->elide)
913 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200914 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200915 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200916 continue;
917 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200918 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300919 if (symbol_conf.col_width_list_str) {
920 if (col_width) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300921 hists__set_col_len(hists, se->se_width_idx,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300922 atoi(col_width));
923 col_width = strchr(col_width, ',');
924 if (col_width)
925 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200926 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200927 }
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300928 if (!hists__new_col_len(hists, se->se_width_idx, width))
929 width = hists__col_len(hists, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200930 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200931 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300932
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200933 fprintf(fp, "\n");
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300934 if (max_rows && ++nr_rows >= max_rows)
935 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200936
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200937 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200938 goto print_entries;
939
940 fprintf(fp, "# ........");
941 if (symbol_conf.show_nr_samples)
942 fprintf(fp, " ..........");
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300943 if (symbol_conf.show_total_period)
944 fprintf(fp, " ............");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200945 if (pair) {
946 fprintf(fp, " ..........");
947 if (show_displacement)
948 fprintf(fp, " .....");
949 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200950 list_for_each_entry(se, &hist_entry__sort_list, list) {
951 unsigned int i;
952
953 if (se->elide)
954 continue;
955
956 fprintf(fp, " ");
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300957 width = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300958 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200959 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200960 for (i = 0; i < width; i++)
961 fprintf(fp, ".");
962 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200963
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300964 fprintf(fp, "\n");
965 if (max_rows && ++nr_rows >= max_rows)
966 goto out;
967
968 fprintf(fp, "#\n");
969 if (max_rows && ++nr_rows >= max_rows)
970 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200971
972print_entries:
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300973 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200974 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
975
Frederic Weisbeckere84d2122011-06-29 22:23:03 +0200976 if (h->filtered)
977 continue;
978
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200979 if (show_displacement) {
980 if (h->pair != NULL)
981 displacement = ((long)h->pair->position -
982 (long)position);
983 else
984 displacement = 0;
985 ++position;
986 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300987 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300988 displacement, fp, hists->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300989
990 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300991 ret += hist_entry__fprintf_callchain(h, hists, fp,
992 hists->stats.total_period);
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300993 if (max_rows && ++nr_rows >= max_rows)
994 goto out;
995
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300996 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300997 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300998 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300999 fprintf(fp, "%.10s end\n", graph_dotted_line);
1000 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001001 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001002out:
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001003 free(rem_sq_bracket);
1004
1005 return ret;
1006}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001007
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001008/*
1009 * See hists__fprintf to match the column widths
1010 */
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001011unsigned int hists__sort_list_width(struct hists *hists)
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001012{
1013 struct sort_entry *se;
1014 int ret = 9; /* total % */
1015
1016 if (symbol_conf.show_cpu_utilization) {
1017 ret += 7; /* count_sys % */
1018 ret += 6; /* count_us % */
1019 if (perf_guest) {
1020 ret += 13; /* count_guest_sys % */
1021 ret += 12; /* count_guest_us % */
1022 }
1023 }
1024
1025 if (symbol_conf.show_nr_samples)
1026 ret += 11;
1027
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001028 if (symbol_conf.show_total_period)
1029 ret += 13;
1030
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001031 list_for_each_entry(se, &hist_entry__sort_list, list)
1032 if (!se->elide)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001033 ret += 2 + hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001034
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -03001035 if (verbose) /* Addr + origin */
1036 ret += 3 + BITS_PER_LONG / 4;
1037
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001038 return ret;
1039}
1040
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001041static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001042 enum hist_filter filter)
1043{
1044 h->filtered &= ~(1 << filter);
1045 if (h->filtered)
1046 return;
1047
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001048 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001049 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001050 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001051 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001052 hists->stats.total_period += h->period;
1053 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001054
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001055 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001056}
1057
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001058void hists__filter_by_dso(struct hists *hists, const struct dso *dso)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001059{
1060 struct rb_node *nd;
1061
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001062 hists->nr_entries = hists->stats.total_period = 0;
1063 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1064 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001065
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001066 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001067 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1068
1069 if (symbol_conf.exclude_other && !h->parent)
1070 continue;
1071
1072 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
1073 h->filtered |= (1 << HIST_FILTER__DSO);
1074 continue;
1075 }
1076
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001077 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001078 }
1079}
1080
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001081void hists__filter_by_thread(struct hists *hists, const struct thread *thread)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001082{
1083 struct rb_node *nd;
1084
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001085 hists->nr_entries = hists->stats.total_period = 0;
1086 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1087 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001088
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001089 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001090 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1091
1092 if (thread != NULL && h->thread != thread) {
1093 h->filtered |= (1 << HIST_FILTER__THREAD);
1094 continue;
1095 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001096
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001097 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001098 }
1099}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001100
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001101int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001102{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001103 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001104}
1105
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001106int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001107{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001108 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001109}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001110
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001111void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001112{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001113 ++hists->stats.nr_events[0];
1114 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001115}
1116
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001117size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001118{
1119 int i;
1120 size_t ret = 0;
1121
1122 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001123 const char *name;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001124
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001125 if (hists->stats.nr_events[i] == 0)
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001126 continue;
1127
1128 name = perf_event__name(i);
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001129 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001130 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001131
1132 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001133 hists->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001134 }
1135
1136 return ret;
1137}
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001138
1139void hists__init(struct hists *hists)
1140{
1141 memset(hists, 0, sizeof(*hists));
1142 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1143 hists->entries_in = &hists->entries_in_array[0];
1144 hists->entries_collapsed = RB_ROOT;
1145 hists->entries = RB_ROOT;
1146 pthread_mutex_init(&hists->lock, NULL);
1147}