blob: a7193c5a0422ab4aa5f41b264efe6b186e2290a7 [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{
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300103 if (he->period == 0)
104 return true;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300105 hists->stats.total_period -= he->period;
106 hist_entry__decay(he);
107 hists->stats.total_period += he->period;
108 return he->period == 0;
109}
110
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300111static void __hists__decay_entries(struct hists *hists, bool threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300112{
113 struct rb_node *next = rb_first(&hists->entries);
114 struct hist_entry *n;
115
116 while (next) {
117 n = rb_entry(next, struct hist_entry, rb_node);
118 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300119 /*
120 * We may be annotating this, for instance, so keep it here in
121 * case some it gets new samples, we'll eventually free it when
122 * the user stops browsing and it agains gets fully decayed.
123 */
124 if (hists__decay_entry(hists, n) && !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300125 rb_erase(&n->rb_node, &hists->entries);
126
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300127 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300128 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
129
130 hist_entry__free(n);
131 --hists->nr_entries;
132 }
133 }
134}
135
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300136void hists__decay_entries(struct hists *hists)
137{
138 return __hists__decay_entries(hists, false);
139}
140
141void hists__decay_entries_threaded(struct hists *hists)
142{
143 return __hists__decay_entries(hists, true);
144}
145
John Kacur3d1d07e2009-09-28 15:32:55 +0200146/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300147 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200148 */
149
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300150static struct hist_entry *hist_entry__new(struct hist_entry *template)
151{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200152 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300153 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
154
155 if (self != NULL) {
156 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300157 self->nr_events = 1;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300158 if (self->ms.map)
159 self->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300160 if (symbol_conf.use_callchain)
161 callchain_init(self->callchain);
162 }
163
164 return self;
165}
166
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300167static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300168{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300169 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300170 hists__calc_col_len(hists, h);
171 ++hists->nr_entries;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300172 hists->stats.total_period += h->period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300173 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300174}
175
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300176static u8 symbol__parent_filter(const struct symbol *parent)
177{
178 if (symbol_conf.exclude_other && parent == NULL)
179 return 1 << HIST_FILTER__PARENT;
180 return 0;
181}
182
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300183struct hist_entry *__hists__add_entry(struct hists *hists,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300184 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300185 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300186{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300187 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300188 struct rb_node *parent = NULL;
189 struct hist_entry *he;
190 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200191 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300192 .ms = {
193 .map = al->map,
194 .sym = al->sym,
195 },
Arun Sharmaf60f3592010-06-04 11:27:10 -0300196 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200197 .ip = al->addr,
198 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300199 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300200 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300201 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300202 };
203 int cmp;
204
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300205 pthread_mutex_lock(&hists->lock);
206
207 p = &hists->entries_in->rb_node;
208
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300209 while (*p != NULL) {
210 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300211 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300212
213 cmp = hist_entry__cmp(&entry, he);
214
215 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300216 he->period += period;
217 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300218 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300219 }
220
221 if (cmp < 0)
222 p = &(*p)->rb_left;
223 else
224 p = &(*p)->rb_right;
225 }
226
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300227 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300228 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300229 goto out_unlock;
230
231 rb_link_node(&he->rb_node_in, parent, p);
232 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300233out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300234 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300235out_unlock:
236 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300237 return he;
238}
239
John Kacur3d1d07e2009-09-28 15:32:55 +0200240int64_t
241hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
242{
243 struct sort_entry *se;
244 int64_t cmp = 0;
245
246 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200247 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200248 if (cmp)
249 break;
250 }
251
252 return cmp;
253}
254
255int64_t
256hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
257{
258 struct sort_entry *se;
259 int64_t cmp = 0;
260
261 list_for_each_entry(se, &hist_entry__sort_list, list) {
262 int64_t (*f)(struct hist_entry *, struct hist_entry *);
263
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200264 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200265
266 cmp = f(left, right);
267 if (cmp)
268 break;
269 }
270
271 return cmp;
272}
273
274void hist_entry__free(struct hist_entry *he)
275{
276 free(he);
277}
278
279/*
280 * collapse the histogram
281 */
282
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300283static bool hists__collapse_insert_entry(struct hists *hists,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100284 struct rb_root *root,
285 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200286{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200287 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200288 struct rb_node *parent = NULL;
289 struct hist_entry *iter;
290 int64_t cmp;
291
292 while (*p != NULL) {
293 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300294 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200295
296 cmp = hist_entry__collapse(iter, he);
297
298 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300299 iter->period += he->period;
Stephane Eraniane39622c2011-10-03 11:38:15 +0200300 iter->nr_events += he->nr_events;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100301 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300302 callchain_cursor_reset(&hists->callchain_cursor);
303 callchain_merge(&hists->callchain_cursor, iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100304 he->callchain);
305 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200306 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300307 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200308 }
309
310 if (cmp < 0)
311 p = &(*p)->rb_left;
312 else
313 p = &(*p)->rb_right;
314 }
315
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300316 rb_link_node(&he->rb_node_in, parent, p);
317 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300318 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200319}
320
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300321static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
322{
323 struct rb_root *root;
324
325 pthread_mutex_lock(&hists->lock);
326
327 root = hists->entries_in;
328 if (++hists->entries_in > &hists->entries_in_array[1])
329 hists->entries_in = &hists->entries_in_array[0];
330
331 pthread_mutex_unlock(&hists->lock);
332
333 return root;
334}
335
336static void __hists__collapse_resort(struct hists *hists, bool threaded)
337{
338 struct rb_root *root;
339 struct rb_node *next;
340 struct hist_entry *n;
341
342 if (!sort__need_collapse && !threaded)
343 return;
344
345 root = hists__get_rotate_entries_in(hists);
346 next = rb_first(root);
347 hists->stats.total_period = 0;
348
349 while (next) {
350 n = rb_entry(next, struct hist_entry, rb_node_in);
351 next = rb_next(&n->rb_node_in);
352
353 rb_erase(&n->rb_node_in, root);
354 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n))
355 hists__inc_nr_entries(hists, n);
356 }
357}
358
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300359void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200360{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300361 return __hists__collapse_resort(hists, false);
362}
John Kacur3d1d07e2009-09-28 15:32:55 +0200363
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300364void hists__collapse_resort_threaded(struct hists *hists)
365{
366 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200367}
368
369/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300370 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200371 */
372
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300373static void __hists__insert_output_entry(struct rb_root *entries,
374 struct hist_entry *he,
375 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200376{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300377 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200378 struct rb_node *parent = NULL;
379 struct hist_entry *iter;
380
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200381 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300382 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200383 min_callchain_hits, &callchain_param);
384
385 while (*p != NULL) {
386 parent = *p;
387 iter = rb_entry(parent, struct hist_entry, rb_node);
388
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300389 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200390 p = &(*p)->rb_left;
391 else
392 p = &(*p)->rb_right;
393 }
394
395 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300396 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200397}
398
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300399static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200400{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300401 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200402 struct rb_node *next;
403 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200404 u64 min_callchain_hits;
405
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300406 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200407
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300408 if (sort__need_collapse || threaded)
409 root = &hists->entries_collapsed;
410 else
411 root = hists->entries_in;
412
413 next = rb_first(root);
414 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200415
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300416 hists->nr_entries = 0;
417 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300418
John Kacur3d1d07e2009-09-28 15:32:55 +0200419 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300420 n = rb_entry(next, struct hist_entry, rb_node_in);
421 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200422
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300423 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300424 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200425 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300426}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200427
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300428void hists__output_resort(struct hists *hists)
429{
430 return __hists__output_resort(hists, false);
431}
432
433void hists__output_resort_threaded(struct hists *hists)
434{
435 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200436}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200437
438static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
439{
440 int i;
441 int ret = fprintf(fp, " ");
442
443 for (i = 0; i < left_margin; i++)
444 ret += fprintf(fp, " ");
445
446 return ret;
447}
448
449static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
450 int left_margin)
451{
452 int i;
453 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
454
455 for (i = 0; i < depth; i++)
456 if (depth_mask & (1 << i))
457 ret += fprintf(fp, "| ");
458 else
459 ret += fprintf(fp, " ");
460
461 ret += fprintf(fp, "\n");
462
463 return ret;
464}
465
466static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300467 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100468 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200469 int left_margin)
470{
471 int i;
472 size_t ret = 0;
473
474 ret += callchain__fprintf_left_margin(fp, left_margin);
475 for (i = 0; i < depth; i++) {
476 if (depth_mask & (1 << i))
477 ret += fprintf(fp, "|");
478 else
479 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300480 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200481 double percent;
482
483 percent = hits * 100.0 / total_samples;
484 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
485 } else
486 ret += fprintf(fp, "%s", " ");
487 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300488 if (chain->ms.sym)
489 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200490 else
491 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
492
493 return ret;
494}
495
496static struct symbol *rem_sq_bracket;
497static struct callchain_list rem_hits;
498
499static void init_rem_hits(void)
500{
501 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
502 if (!rem_sq_bracket) {
503 fprintf(stderr, "Not enough memory to display remaining hits\n");
504 return;
505 }
506
507 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300508 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200509}
510
511static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
512 u64 total_samples, int depth,
513 int depth_mask, int left_margin)
514{
515 struct rb_node *node, *next;
516 struct callchain_node *child;
517 struct callchain_list *chain;
518 int new_depth_mask = depth_mask;
519 u64 new_total;
520 u64 remaining;
521 size_t ret = 0;
522 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300523 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200524
525 if (callchain_param.mode == CHAIN_GRAPH_REL)
526 new_total = self->children_hit;
527 else
528 new_total = total_samples;
529
530 remaining = new_total;
531
532 node = rb_first(&self->rb_root);
533 while (node) {
534 u64 cumul;
535
536 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100537 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200538 remaining -= cumul;
539
540 /*
541 * The depth mask manages the output of pipes that show
542 * the depth. We don't want to keep the pipes of the current
543 * level for the last child of this depth.
544 * Except if we have remaining filtered hits. They will
545 * supersede the last child
546 */
547 next = rb_next(node);
548 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
549 new_depth_mask &= ~(1 << (depth - 1));
550
551 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800552 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200553 * to keep the level link until we reach the last child
554 */
555 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
556 left_margin);
557 i = 0;
558 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200559 ret += ipchain__fprintf_graph(fp, chain, depth,
560 new_depth_mask, i++,
561 new_total,
562 cumul,
563 left_margin);
564 }
565 ret += __callchain__fprintf_graph(fp, child, new_total,
566 depth + 1,
567 new_depth_mask | (1 << depth),
568 left_margin);
569 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300570 if (++entries_printed == callchain_param.print_limit)
571 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200572 }
573
574 if (callchain_param.mode == CHAIN_GRAPH_REL &&
575 remaining && remaining != new_total) {
576
577 if (!rem_sq_bracket)
578 return ret;
579
580 new_depth_mask &= ~(1 << (depth - 1));
581
582 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
583 new_depth_mask, 0, new_total,
584 remaining, left_margin);
585 }
586
587 return ret;
588}
589
590static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
591 u64 total_samples, int left_margin)
592{
593 struct callchain_list *chain;
594 bool printed = false;
595 int i = 0;
596 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300597 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200598
599 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200600 if (!i++ && sort__first_dimension == SORT_SYM)
601 continue;
602
603 if (!printed) {
604 ret += callchain__fprintf_left_margin(fp, left_margin);
605 ret += fprintf(fp, "|\n");
606 ret += callchain__fprintf_left_margin(fp, left_margin);
607 ret += fprintf(fp, "---");
608
609 left_margin += 3;
610 printed = true;
611 } else
612 ret += callchain__fprintf_left_margin(fp, left_margin);
613
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300614 if (chain->ms.sym)
615 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200616 else
617 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300618
619 if (++entries_printed == callchain_param.print_limit)
620 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200621 }
622
623 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
624
625 return ret;
626}
627
628static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
629 u64 total_samples)
630{
631 struct callchain_list *chain;
632 size_t ret = 0;
633
634 if (!self)
635 return 0;
636
637 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
638
639
640 list_for_each_entry(chain, &self->val, list) {
641 if (chain->ip >= PERF_CONTEXT_MAX)
642 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300643 if (chain->ms.sym)
644 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200645 else
646 ret += fprintf(fp, " %p\n",
647 (void *)(long)chain->ip);
648 }
649
650 return ret;
651}
652
653static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
654 u64 total_samples, int left_margin)
655{
656 struct rb_node *rb_node;
657 struct callchain_node *chain;
658 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300659 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200660
661 rb_node = rb_first(&self->sorted_chain);
662 while (rb_node) {
663 double percent;
664
665 chain = rb_entry(rb_node, struct callchain_node, rb_node);
666 percent = chain->hit * 100.0 / total_samples;
667 switch (callchain_param.mode) {
668 case CHAIN_FLAT:
669 ret += percent_color_fprintf(fp, " %6.2f%%\n",
670 percent);
671 ret += callchain__fprintf_flat(fp, chain, total_samples);
672 break;
673 case CHAIN_GRAPH_ABS: /* Falldown */
674 case CHAIN_GRAPH_REL:
675 ret += callchain__fprintf_graph(fp, chain, total_samples,
676 left_margin);
677 case CHAIN_NONE:
678 default:
679 break;
680 }
681 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300682 if (++entries_printed == callchain_param.print_limit)
683 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200684 rb_node = rb_next(rb_node);
685 }
686
687 return ret;
688}
689
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300690void hists__output_recalc_col_len(struct hists *hists, int max_rows)
691{
692 struct rb_node *next = rb_first(&hists->entries);
693 struct hist_entry *n;
694 int row = 0;
695
696 hists__reset_col_len(hists);
697
698 while (next && row++ < max_rows) {
699 n = rb_entry(next, struct hist_entry, rb_node);
700 hists__calc_col_len(hists, n);
701 next = rb_next(&n->rb_node);
702 }
703}
704
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300705int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300706 struct hists *hists, struct hists *pair_hists,
707 bool show_displacement, long displacement,
708 bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200709{
710 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300711 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200712 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200713 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300714 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200715
716 if (symbol_conf.exclude_other && !self->parent)
717 return 0;
718
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300719 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300720 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200721 nr_events = self->pair ? self->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300722 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300723 period_sys = self->pair ? self->pair->period_sys : 0;
724 period_us = self->pair ? self->pair->period_us : 0;
725 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
726 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200727 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300728 period = self->period;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200729 nr_events = self->nr_events;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300730 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300731 period_sys = self->period_sys;
732 period_us = self->period_us;
733 period_guest_sys = self->period_guest_sys;
734 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200735 }
736
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300737 if (total) {
738 if (color)
739 ret = percent_color_snprintf(s, size,
740 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300741 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300742 else
743 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300744 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800745 if (symbol_conf.show_cpu_utilization) {
746 ret += percent_color_snprintf(s + ret, size - ret,
747 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300748 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800749 ret += percent_color_snprintf(s + ret, size - ret,
750 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300751 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800752 if (perf_guest) {
753 ret += percent_color_snprintf(s + ret,
754 size - ret,
755 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300756 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800757 total);
758 ret += percent_color_snprintf(s + ret,
759 size - ret,
760 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300761 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800762 total);
763 }
764 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300765 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200766 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200767
768 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200769 if (sep)
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200770 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200771 else
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200772 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200773 }
774
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300775 if (symbol_conf.show_total_period) {
776 if (sep)
777 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
778 else
779 ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
780 }
781
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300782 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200783 char bf[32];
784 double old_percent = 0, new_percent = 0, diff;
785
786 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300787 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300788 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300789 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200790
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200791 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200792
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200793 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200794 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
795 else
796 snprintf(bf, sizeof(bf), " ");
797
798 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300799 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200800 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300801 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200802
803 if (show_displacement) {
804 if (displacement)
805 snprintf(bf, sizeof(bf), "%+4ld", displacement);
806 else
807 snprintf(bf, sizeof(bf), " ");
808
809 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300810 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200811 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300812 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200813 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200814 }
815
816 list_for_each_entry(se, &hist_entry__sort_list, list) {
817 if (se->elide)
818 continue;
819
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300820 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200821 ret += se->se_snprintf(self, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300822 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200823 }
824
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300825 return ret;
826}
827
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300828int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300829 struct hists *pair_hists, bool show_displacement,
830 long displacement, FILE *fp, u64 session_total)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300831{
832 char bf[512];
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300833
834 if (size == 0 || size > sizeof(bf))
835 size = sizeof(bf);
836
837 hist_entry__snprintf(he, bf, size, hists, pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300838 show_displacement, displacement,
839 true, session_total);
840 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300841}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200842
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300843static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
844 struct hists *hists, FILE *fp,
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300845 u64 session_total)
846{
847 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200848
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300849 if (sort__first_dimension == SORT_COMM) {
850 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
851 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300852 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300853 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200854 }
855
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300856 return hist_entry_callchain__fprintf(fp, self, session_total,
857 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200858}
859
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300860size_t hists__fprintf(struct hists *hists, struct hists *pair,
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300861 bool show_displacement, bool show_header, int max_rows,
862 int max_cols, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200863{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200864 struct sort_entry *se;
865 struct rb_node *nd;
866 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200867 unsigned long position = 1;
868 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200869 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200870 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300871 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300872 int nr_rows = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200873
874 init_rem_hits();
875
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300876 if (!show_header)
877 goto print_entries;
878
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200879 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
880
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200881 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200882 if (sep)
883 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200884 else
885 fputs(" Samples ", fp);
886 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200887
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300888 if (symbol_conf.show_total_period) {
889 if (sep)
890 ret += fprintf(fp, "%cPeriod", *sep);
891 else
892 ret += fprintf(fp, " Period ");
893 }
894
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800895 if (symbol_conf.show_cpu_utilization) {
896 if (sep) {
897 ret += fprintf(fp, "%csys", *sep);
898 ret += fprintf(fp, "%cus", *sep);
899 if (perf_guest) {
900 ret += fprintf(fp, "%cguest sys", *sep);
901 ret += fprintf(fp, "%cguest us", *sep);
902 }
903 } else {
904 ret += fprintf(fp, " sys ");
905 ret += fprintf(fp, " us ");
906 if (perf_guest) {
907 ret += fprintf(fp, " guest sys ");
908 ret += fprintf(fp, " guest us ");
909 }
910 }
911 }
912
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200913 if (pair) {
914 if (sep)
915 ret += fprintf(fp, "%cDelta", *sep);
916 else
917 ret += fprintf(fp, " Delta ");
918
919 if (show_displacement) {
920 if (sep)
921 ret += fprintf(fp, "%cDisplacement", *sep);
922 else
923 ret += fprintf(fp, " Displ");
924 }
925 }
926
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200927 list_for_each_entry(se, &hist_entry__sort_list, list) {
928 if (se->elide)
929 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200930 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200931 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200932 continue;
933 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200934 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300935 if (symbol_conf.col_width_list_str) {
936 if (col_width) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300937 hists__set_col_len(hists, se->se_width_idx,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300938 atoi(col_width));
939 col_width = strchr(col_width, ',');
940 if (col_width)
941 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200942 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200943 }
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300944 if (!hists__new_col_len(hists, se->se_width_idx, width))
945 width = hists__col_len(hists, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200946 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200947 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300948
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200949 fprintf(fp, "\n");
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300950 if (max_rows && ++nr_rows >= max_rows)
951 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200952
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200953 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200954 goto print_entries;
955
956 fprintf(fp, "# ........");
957 if (symbol_conf.show_nr_samples)
958 fprintf(fp, " ..........");
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300959 if (symbol_conf.show_total_period)
960 fprintf(fp, " ............");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200961 if (pair) {
962 fprintf(fp, " ..........");
963 if (show_displacement)
964 fprintf(fp, " .....");
965 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200966 list_for_each_entry(se, &hist_entry__sort_list, list) {
967 unsigned int i;
968
969 if (se->elide)
970 continue;
971
972 fprintf(fp, " ");
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300973 width = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300974 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200975 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200976 for (i = 0; i < width; i++)
977 fprintf(fp, ".");
978 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200979
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300980 fprintf(fp, "\n");
981 if (max_rows && ++nr_rows >= max_rows)
982 goto out;
983
984 fprintf(fp, "#\n");
985 if (max_rows && ++nr_rows >= max_rows)
986 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200987
988print_entries:
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300989 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200990 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
991
Frederic Weisbeckere84d2122011-06-29 22:23:03 +0200992 if (h->filtered)
993 continue;
994
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200995 if (show_displacement) {
996 if (h->pair != NULL)
997 displacement = ((long)h->pair->position -
998 (long)position);
999 else
1000 displacement = 0;
1001 ++position;
1002 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001003 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001004 displacement, fp, hists->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -03001005
1006 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001007 ret += hist_entry__fprintf_callchain(h, hists, fp,
1008 hists->stats.total_period);
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001009 if (max_rows && ++nr_rows >= max_rows)
1010 goto out;
1011
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -03001012 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001013 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03001014 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001015 fprintf(fp, "%.10s end\n", graph_dotted_line);
1016 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001017 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001018out:
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001019 free(rem_sq_bracket);
1020
1021 return ret;
1022}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001023
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001024/*
1025 * See hists__fprintf to match the column widths
1026 */
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001027unsigned int hists__sort_list_width(struct hists *hists)
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001028{
1029 struct sort_entry *se;
1030 int ret = 9; /* total % */
1031
1032 if (symbol_conf.show_cpu_utilization) {
1033 ret += 7; /* count_sys % */
1034 ret += 6; /* count_us % */
1035 if (perf_guest) {
1036 ret += 13; /* count_guest_sys % */
1037 ret += 12; /* count_guest_us % */
1038 }
1039 }
1040
1041 if (symbol_conf.show_nr_samples)
1042 ret += 11;
1043
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001044 if (symbol_conf.show_total_period)
1045 ret += 13;
1046
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001047 list_for_each_entry(se, &hist_entry__sort_list, list)
1048 if (!se->elide)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001049 ret += 2 + hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001050
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -03001051 if (verbose) /* Addr + origin */
1052 ret += 3 + BITS_PER_LONG / 4;
1053
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001054 return ret;
1055}
1056
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001057static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001058 enum hist_filter filter)
1059{
1060 h->filtered &= ~(1 << filter);
1061 if (h->filtered)
1062 return;
1063
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001064 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001065 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001066 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001067 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001068 hists->stats.total_period += h->period;
1069 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001070
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001071 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001072}
1073
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001074void hists__filter_by_dso(struct hists *hists, const struct dso *dso)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001075{
1076 struct rb_node *nd;
1077
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001078 hists->nr_entries = hists->stats.total_period = 0;
1079 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1080 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001081
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001082 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001083 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1084
1085 if (symbol_conf.exclude_other && !h->parent)
1086 continue;
1087
1088 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
1089 h->filtered |= (1 << HIST_FILTER__DSO);
1090 continue;
1091 }
1092
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001093 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001094 }
1095}
1096
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001097void hists__filter_by_thread(struct hists *hists, const struct thread *thread)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001098{
1099 struct rb_node *nd;
1100
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001101 hists->nr_entries = hists->stats.total_period = 0;
1102 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1103 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001104
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001105 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001106 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1107
1108 if (thread != NULL && h->thread != thread) {
1109 h->filtered |= (1 << HIST_FILTER__THREAD);
1110 continue;
1111 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001112
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001113 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001114 }
1115}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001116
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001117int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001118{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001119 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001120}
1121
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001122int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001123{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001124 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001125}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001126
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001127void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001128{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001129 ++hists->stats.nr_events[0];
1130 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001131}
1132
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001133size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001134{
1135 int i;
1136 size_t ret = 0;
1137
1138 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001139 const char *name;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001140
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001141 if (hists->stats.nr_events[i] == 0)
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001142 continue;
1143
1144 name = perf_event__name(i);
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001145 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001146 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001147
1148 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001149 hists->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001150 }
1151
1152 return ret;
1153}
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001154
1155void hists__init(struct hists *hists)
1156{
1157 memset(hists, 0, sizeof(*hists));
1158 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1159 hists->entries_in = &hists->entries_in_array[0];
1160 hists->entries_collapsed = RB_ROOT;
1161 hists->entries = RB_ROOT;
1162 pthread_mutex_init(&hists->lock, NULL);
1163}