blob: 9b9d12bbdc1d8efb050cd862253e4fc358f3e265 [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 Meloab81f3fd2011-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 Meloab81f3fd2011-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
111void hists__decay_entries(struct hists *hists)
112{
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 Meloab81f3fd2011-10-05 19:16:15 -0300125 rb_erase(&n->rb_node, &hists->entries);
126
127 if (sort__need_collapse)
128 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
129
130 hist_entry__free(n);
131 --hists->nr_entries;
132 }
133 }
134}
135
John Kacur3d1d07e2009-09-28 15:32:55 +0200136/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300137 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200138 */
139
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300140static struct hist_entry *hist_entry__new(struct hist_entry *template)
141{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200142 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300143 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
144
145 if (self != NULL) {
146 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300147 self->nr_events = 1;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300148 if (self->ms.map)
149 self->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300150 if (symbol_conf.use_callchain)
151 callchain_init(self->callchain);
152 }
153
154 return self;
155}
156
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300157static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300158{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300159 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300160 hists__calc_col_len(hists, h);
161 ++hists->nr_entries;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300162 hists->stats.total_period += h->period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300163 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300164}
165
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300166static u8 symbol__parent_filter(const struct symbol *parent)
167{
168 if (symbol_conf.exclude_other && parent == NULL)
169 return 1 << HIST_FILTER__PARENT;
170 return 0;
171}
172
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300173struct hist_entry *__hists__add_entry(struct hists *hists,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300174 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300175 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300176{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300177 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300178 struct rb_node *parent = NULL;
179 struct hist_entry *he;
180 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200181 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300182 .ms = {
183 .map = al->map,
184 .sym = al->sym,
185 },
Arun Sharmaf60f3592010-06-04 11:27:10 -0300186 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200187 .ip = al->addr,
188 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300189 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300190 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300191 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300192 };
193 int cmp;
194
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300195 pthread_mutex_lock(&hists->lock);
196
197 p = &hists->entries_in->rb_node;
198
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300199 while (*p != NULL) {
200 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300201 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300202
203 cmp = hist_entry__cmp(&entry, he);
204
205 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300206 he->period += period;
207 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300208 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300209 }
210
211 if (cmp < 0)
212 p = &(*p)->rb_left;
213 else
214 p = &(*p)->rb_right;
215 }
216
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300217 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300218 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300219 goto out_unlock;
220
221 rb_link_node(&he->rb_node_in, parent, p);
222 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300223out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300224 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300225out_unlock:
226 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300227 return he;
228}
229
John Kacur3d1d07e2009-09-28 15:32:55 +0200230int64_t
231hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
232{
233 struct sort_entry *se;
234 int64_t cmp = 0;
235
236 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200237 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200238 if (cmp)
239 break;
240 }
241
242 return cmp;
243}
244
245int64_t
246hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
247{
248 struct sort_entry *se;
249 int64_t cmp = 0;
250
251 list_for_each_entry(se, &hist_entry__sort_list, list) {
252 int64_t (*f)(struct hist_entry *, struct hist_entry *);
253
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200254 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200255
256 cmp = f(left, right);
257 if (cmp)
258 break;
259 }
260
261 return cmp;
262}
263
264void hist_entry__free(struct hist_entry *he)
265{
266 free(he);
267}
268
269/*
270 * collapse the histogram
271 */
272
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300273static bool hists__collapse_insert_entry(struct hists *hists,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100274 struct rb_root *root,
275 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200276{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200277 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200278 struct rb_node *parent = NULL;
279 struct hist_entry *iter;
280 int64_t cmp;
281
282 while (*p != NULL) {
283 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300284 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200285
286 cmp = hist_entry__collapse(iter, he);
287
288 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300289 iter->period += he->period;
Stephane Eraniane39622c2011-10-03 11:38:15 +0200290 iter->nr_events += he->nr_events;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100291 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300292 callchain_cursor_reset(&hists->callchain_cursor);
293 callchain_merge(&hists->callchain_cursor, iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100294 he->callchain);
295 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200296 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300297 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200298 }
299
300 if (cmp < 0)
301 p = &(*p)->rb_left;
302 else
303 p = &(*p)->rb_right;
304 }
305
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300306 rb_link_node(&he->rb_node_in, parent, p);
307 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300308 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200309}
310
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300311static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
312{
313 struct rb_root *root;
314
315 pthread_mutex_lock(&hists->lock);
316
317 root = hists->entries_in;
318 if (++hists->entries_in > &hists->entries_in_array[1])
319 hists->entries_in = &hists->entries_in_array[0];
320
321 pthread_mutex_unlock(&hists->lock);
322
323 return root;
324}
325
326static void __hists__collapse_resort(struct hists *hists, bool threaded)
327{
328 struct rb_root *root;
329 struct rb_node *next;
330 struct hist_entry *n;
331
332 if (!sort__need_collapse && !threaded)
333 return;
334
335 root = hists__get_rotate_entries_in(hists);
336 next = rb_first(root);
337 hists->stats.total_period = 0;
338
339 while (next) {
340 n = rb_entry(next, struct hist_entry, rb_node_in);
341 next = rb_next(&n->rb_node_in);
342
343 rb_erase(&n->rb_node_in, root);
344 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n))
345 hists__inc_nr_entries(hists, n);
346 }
347}
348
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300349void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200350{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300351 return __hists__collapse_resort(hists, false);
352}
John Kacur3d1d07e2009-09-28 15:32:55 +0200353
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300354void hists__collapse_resort_threaded(struct hists *hists)
355{
356 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200357}
358
359/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300360 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200361 */
362
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300363static void __hists__insert_output_entry(struct rb_root *entries,
364 struct hist_entry *he,
365 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200366{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300367 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200368 struct rb_node *parent = NULL;
369 struct hist_entry *iter;
370
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200371 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300372 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200373 min_callchain_hits, &callchain_param);
374
375 while (*p != NULL) {
376 parent = *p;
377 iter = rb_entry(parent, struct hist_entry, rb_node);
378
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300379 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200380 p = &(*p)->rb_left;
381 else
382 p = &(*p)->rb_right;
383 }
384
385 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300386 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200387}
388
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300389static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200390{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300391 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200392 struct rb_node *next;
393 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200394 u64 min_callchain_hits;
395
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300396 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200397
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300398 if (sort__need_collapse || threaded)
399 root = &hists->entries_collapsed;
400 else
401 root = hists->entries_in;
402
403 next = rb_first(root);
404 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200405
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300406 hists->nr_entries = 0;
407 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300408
John Kacur3d1d07e2009-09-28 15:32:55 +0200409 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300410 n = rb_entry(next, struct hist_entry, rb_node_in);
411 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200412
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300413 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300414 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200415 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300416}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200417
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300418void hists__output_resort(struct hists *hists)
419{
420 return __hists__output_resort(hists, false);
421}
422
423void hists__output_resort_threaded(struct hists *hists)
424{
425 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200426}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200427
428static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
429{
430 int i;
431 int ret = fprintf(fp, " ");
432
433 for (i = 0; i < left_margin; i++)
434 ret += fprintf(fp, " ");
435
436 return ret;
437}
438
439static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
440 int left_margin)
441{
442 int i;
443 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
444
445 for (i = 0; i < depth; i++)
446 if (depth_mask & (1 << i))
447 ret += fprintf(fp, "| ");
448 else
449 ret += fprintf(fp, " ");
450
451 ret += fprintf(fp, "\n");
452
453 return ret;
454}
455
456static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300457 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100458 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200459 int left_margin)
460{
461 int i;
462 size_t ret = 0;
463
464 ret += callchain__fprintf_left_margin(fp, left_margin);
465 for (i = 0; i < depth; i++) {
466 if (depth_mask & (1 << i))
467 ret += fprintf(fp, "|");
468 else
469 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300470 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200471 double percent;
472
473 percent = hits * 100.0 / total_samples;
474 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
475 } else
476 ret += fprintf(fp, "%s", " ");
477 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300478 if (chain->ms.sym)
479 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200480 else
481 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
482
483 return ret;
484}
485
486static struct symbol *rem_sq_bracket;
487static struct callchain_list rem_hits;
488
489static void init_rem_hits(void)
490{
491 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
492 if (!rem_sq_bracket) {
493 fprintf(stderr, "Not enough memory to display remaining hits\n");
494 return;
495 }
496
497 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300498 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200499}
500
501static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
502 u64 total_samples, int depth,
503 int depth_mask, int left_margin)
504{
505 struct rb_node *node, *next;
506 struct callchain_node *child;
507 struct callchain_list *chain;
508 int new_depth_mask = depth_mask;
509 u64 new_total;
510 u64 remaining;
511 size_t ret = 0;
512 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300513 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200514
515 if (callchain_param.mode == CHAIN_GRAPH_REL)
516 new_total = self->children_hit;
517 else
518 new_total = total_samples;
519
520 remaining = new_total;
521
522 node = rb_first(&self->rb_root);
523 while (node) {
524 u64 cumul;
525
526 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100527 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200528 remaining -= cumul;
529
530 /*
531 * The depth mask manages the output of pipes that show
532 * the depth. We don't want to keep the pipes of the current
533 * level for the last child of this depth.
534 * Except if we have remaining filtered hits. They will
535 * supersede the last child
536 */
537 next = rb_next(node);
538 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
539 new_depth_mask &= ~(1 << (depth - 1));
540
541 /*
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800542 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200543 * to keep the level link until we reach the last child
544 */
545 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
546 left_margin);
547 i = 0;
548 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200549 ret += ipchain__fprintf_graph(fp, chain, depth,
550 new_depth_mask, i++,
551 new_total,
552 cumul,
553 left_margin);
554 }
555 ret += __callchain__fprintf_graph(fp, child, new_total,
556 depth + 1,
557 new_depth_mask | (1 << depth),
558 left_margin);
559 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300560 if (++entries_printed == callchain_param.print_limit)
561 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200562 }
563
564 if (callchain_param.mode == CHAIN_GRAPH_REL &&
565 remaining && remaining != new_total) {
566
567 if (!rem_sq_bracket)
568 return ret;
569
570 new_depth_mask &= ~(1 << (depth - 1));
571
572 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
573 new_depth_mask, 0, new_total,
574 remaining, left_margin);
575 }
576
577 return ret;
578}
579
580static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
581 u64 total_samples, int left_margin)
582{
583 struct callchain_list *chain;
584 bool printed = false;
585 int i = 0;
586 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300587 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200588
589 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200590 if (!i++ && sort__first_dimension == SORT_SYM)
591 continue;
592
593 if (!printed) {
594 ret += callchain__fprintf_left_margin(fp, left_margin);
595 ret += fprintf(fp, "|\n");
596 ret += callchain__fprintf_left_margin(fp, left_margin);
597 ret += fprintf(fp, "---");
598
599 left_margin += 3;
600 printed = true;
601 } else
602 ret += callchain__fprintf_left_margin(fp, left_margin);
603
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300604 if (chain->ms.sym)
605 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200606 else
607 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300608
609 if (++entries_printed == callchain_param.print_limit)
610 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200611 }
612
613 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
614
615 return ret;
616}
617
618static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
619 u64 total_samples)
620{
621 struct callchain_list *chain;
622 size_t ret = 0;
623
624 if (!self)
625 return 0;
626
627 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
628
629
630 list_for_each_entry(chain, &self->val, list) {
631 if (chain->ip >= PERF_CONTEXT_MAX)
632 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300633 if (chain->ms.sym)
634 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200635 else
636 ret += fprintf(fp, " %p\n",
637 (void *)(long)chain->ip);
638 }
639
640 return ret;
641}
642
643static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
644 u64 total_samples, int left_margin)
645{
646 struct rb_node *rb_node;
647 struct callchain_node *chain;
648 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300649 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200650
651 rb_node = rb_first(&self->sorted_chain);
652 while (rb_node) {
653 double percent;
654
655 chain = rb_entry(rb_node, struct callchain_node, rb_node);
656 percent = chain->hit * 100.0 / total_samples;
657 switch (callchain_param.mode) {
658 case CHAIN_FLAT:
659 ret += percent_color_fprintf(fp, " %6.2f%%\n",
660 percent);
661 ret += callchain__fprintf_flat(fp, chain, total_samples);
662 break;
663 case CHAIN_GRAPH_ABS: /* Falldown */
664 case CHAIN_GRAPH_REL:
665 ret += callchain__fprintf_graph(fp, chain, total_samples,
666 left_margin);
667 case CHAIN_NONE:
668 default:
669 break;
670 }
671 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300672 if (++entries_printed == callchain_param.print_limit)
673 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200674 rb_node = rb_next(rb_node);
675 }
676
677 return ret;
678}
679
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300680void hists__output_recalc_col_len(struct hists *hists, int max_rows)
681{
682 struct rb_node *next = rb_first(&hists->entries);
683 struct hist_entry *n;
684 int row = 0;
685
686 hists__reset_col_len(hists);
687
688 while (next && row++ < max_rows) {
689 n = rb_entry(next, struct hist_entry, rb_node);
690 hists__calc_col_len(hists, n);
691 next = rb_next(&n->rb_node);
692 }
693}
694
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300695int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300696 struct hists *hists, struct hists *pair_hists,
697 bool show_displacement, long displacement,
698 bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200699{
700 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300701 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200702 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200703 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300704 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200705
706 if (symbol_conf.exclude_other && !self->parent)
707 return 0;
708
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300709 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300710 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200711 nr_events = self->pair ? self->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300712 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300713 period_sys = self->pair ? self->pair->period_sys : 0;
714 period_us = self->pair ? self->pair->period_us : 0;
715 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
716 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200717 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300718 period = self->period;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200719 nr_events = self->nr_events;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300720 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300721 period_sys = self->period_sys;
722 period_us = self->period_us;
723 period_guest_sys = self->period_guest_sys;
724 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200725 }
726
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300727 if (total) {
728 if (color)
729 ret = percent_color_snprintf(s, size,
730 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300731 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300732 else
733 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300734 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800735 if (symbol_conf.show_cpu_utilization) {
736 ret += percent_color_snprintf(s + ret, size - ret,
737 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300738 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800739 ret += percent_color_snprintf(s + ret, size - ret,
740 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300741 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800742 if (perf_guest) {
743 ret += percent_color_snprintf(s + ret,
744 size - ret,
745 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300746 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800747 total);
748 ret += percent_color_snprintf(s + ret,
749 size - ret,
750 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300751 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800752 total);
753 }
754 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300755 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200756 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200757
758 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200759 if (sep)
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200760 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200761 else
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200762 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200763 }
764
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300765 if (symbol_conf.show_total_period) {
766 if (sep)
767 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
768 else
769 ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
770 }
771
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300772 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200773 char bf[32];
774 double old_percent = 0, new_percent = 0, diff;
775
776 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300777 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300778 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300779 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200780
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200781 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200782
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200783 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200784 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
785 else
786 snprintf(bf, sizeof(bf), " ");
787
788 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300789 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200790 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300791 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200792
793 if (show_displacement) {
794 if (displacement)
795 snprintf(bf, sizeof(bf), "%+4ld", displacement);
796 else
797 snprintf(bf, sizeof(bf), " ");
798
799 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300800 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200801 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300802 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200803 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200804 }
805
806 list_for_each_entry(se, &hist_entry__sort_list, list) {
807 if (se->elide)
808 continue;
809
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300810 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200811 ret += se->se_snprintf(self, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300812 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200813 }
814
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300815 return ret;
816}
817
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300818int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300819 struct hists *pair_hists, bool show_displacement,
820 long displacement, FILE *fp, u64 session_total)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300821{
822 char bf[512];
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300823
824 if (size == 0 || size > sizeof(bf))
825 size = sizeof(bf);
826
827 hist_entry__snprintf(he, bf, size, hists, pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300828 show_displacement, displacement,
829 true, session_total);
830 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300831}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200832
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300833static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
834 struct hists *hists, FILE *fp,
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300835 u64 session_total)
836{
837 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200838
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300839 if (sort__first_dimension == SORT_COMM) {
840 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
841 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300842 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300843 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200844 }
845
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300846 return hist_entry_callchain__fprintf(fp, self, session_total,
847 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200848}
849
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300850size_t hists__fprintf(struct hists *hists, struct hists *pair,
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300851 bool show_displacement, bool show_header, int max_rows,
852 int max_cols, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200853{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200854 struct sort_entry *se;
855 struct rb_node *nd;
856 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200857 unsigned long position = 1;
858 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200859 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200860 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300861 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300862 int nr_rows = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200863
864 init_rem_hits();
865
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300866 if (!show_header)
867 goto print_entries;
868
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200869 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
870
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200871 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200872 if (sep)
873 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200874 else
875 fputs(" Samples ", fp);
876 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200877
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300878 if (symbol_conf.show_total_period) {
879 if (sep)
880 ret += fprintf(fp, "%cPeriod", *sep);
881 else
882 ret += fprintf(fp, " Period ");
883 }
884
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800885 if (symbol_conf.show_cpu_utilization) {
886 if (sep) {
887 ret += fprintf(fp, "%csys", *sep);
888 ret += fprintf(fp, "%cus", *sep);
889 if (perf_guest) {
890 ret += fprintf(fp, "%cguest sys", *sep);
891 ret += fprintf(fp, "%cguest us", *sep);
892 }
893 } else {
894 ret += fprintf(fp, " sys ");
895 ret += fprintf(fp, " us ");
896 if (perf_guest) {
897 ret += fprintf(fp, " guest sys ");
898 ret += fprintf(fp, " guest us ");
899 }
900 }
901 }
902
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200903 if (pair) {
904 if (sep)
905 ret += fprintf(fp, "%cDelta", *sep);
906 else
907 ret += fprintf(fp, " Delta ");
908
909 if (show_displacement) {
910 if (sep)
911 ret += fprintf(fp, "%cDisplacement", *sep);
912 else
913 ret += fprintf(fp, " Displ");
914 }
915 }
916
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200917 list_for_each_entry(se, &hist_entry__sort_list, list) {
918 if (se->elide)
919 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200920 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200921 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200922 continue;
923 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200924 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300925 if (symbol_conf.col_width_list_str) {
926 if (col_width) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300927 hists__set_col_len(hists, se->se_width_idx,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300928 atoi(col_width));
929 col_width = strchr(col_width, ',');
930 if (col_width)
931 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200932 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200933 }
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300934 if (!hists__new_col_len(hists, se->se_width_idx, width))
935 width = hists__col_len(hists, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200936 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200937 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300938
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200939 fprintf(fp, "\n");
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300940 if (max_rows && ++nr_rows >= max_rows)
941 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200942
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200943 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200944 goto print_entries;
945
946 fprintf(fp, "# ........");
947 if (symbol_conf.show_nr_samples)
948 fprintf(fp, " ..........");
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300949 if (symbol_conf.show_total_period)
950 fprintf(fp, " ............");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200951 if (pair) {
952 fprintf(fp, " ..........");
953 if (show_displacement)
954 fprintf(fp, " .....");
955 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200956 list_for_each_entry(se, &hist_entry__sort_list, list) {
957 unsigned int i;
958
959 if (se->elide)
960 continue;
961
962 fprintf(fp, " ");
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300963 width = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300964 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200965 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200966 for (i = 0; i < width; i++)
967 fprintf(fp, ".");
968 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200969
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300970 fprintf(fp, "\n");
971 if (max_rows && ++nr_rows >= max_rows)
972 goto out;
973
974 fprintf(fp, "#\n");
975 if (max_rows && ++nr_rows >= max_rows)
976 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200977
978print_entries:
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300979 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200980 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
981
Frederic Weisbeckere84d2122011-06-29 22:23:03 +0200982 if (h->filtered)
983 continue;
984
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200985 if (show_displacement) {
986 if (h->pair != NULL)
987 displacement = ((long)h->pair->position -
988 (long)position);
989 else
990 displacement = 0;
991 ++position;
992 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300993 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300994 displacement, fp, hists->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300995
996 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300997 ret += hist_entry__fprintf_callchain(h, hists, fp,
998 hists->stats.total_period);
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300999 if (max_rows && ++nr_rows >= max_rows)
1000 goto out;
1001
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -03001002 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001003 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03001004 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001005 fprintf(fp, "%.10s end\n", graph_dotted_line);
1006 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001007 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001008out:
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001009 free(rem_sq_bracket);
1010
1011 return ret;
1012}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001013
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001014/*
1015 * See hists__fprintf to match the column widths
1016 */
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001017unsigned int hists__sort_list_width(struct hists *hists)
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001018{
1019 struct sort_entry *se;
1020 int ret = 9; /* total % */
1021
1022 if (symbol_conf.show_cpu_utilization) {
1023 ret += 7; /* count_sys % */
1024 ret += 6; /* count_us % */
1025 if (perf_guest) {
1026 ret += 13; /* count_guest_sys % */
1027 ret += 12; /* count_guest_us % */
1028 }
1029 }
1030
1031 if (symbol_conf.show_nr_samples)
1032 ret += 11;
1033
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001034 if (symbol_conf.show_total_period)
1035 ret += 13;
1036
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001037 list_for_each_entry(se, &hist_entry__sort_list, list)
1038 if (!se->elide)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001039 ret += 2 + hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001040
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -03001041 if (verbose) /* Addr + origin */
1042 ret += 3 + BITS_PER_LONG / 4;
1043
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001044 return ret;
1045}
1046
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001047static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001048 enum hist_filter filter)
1049{
1050 h->filtered &= ~(1 << filter);
1051 if (h->filtered)
1052 return;
1053
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001054 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001055 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001056 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001057 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001058 hists->stats.total_period += h->period;
1059 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001060
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001061 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001062}
1063
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001064void hists__filter_by_dso(struct hists *hists, const struct dso *dso)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001065{
1066 struct rb_node *nd;
1067
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001068 hists->nr_entries = hists->stats.total_period = 0;
1069 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1070 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001071
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001072 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001073 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1074
1075 if (symbol_conf.exclude_other && !h->parent)
1076 continue;
1077
1078 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
1079 h->filtered |= (1 << HIST_FILTER__DSO);
1080 continue;
1081 }
1082
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001083 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001084 }
1085}
1086
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001087void hists__filter_by_thread(struct hists *hists, const struct thread *thread)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001088{
1089 struct rb_node *nd;
1090
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001091 hists->nr_entries = hists->stats.total_period = 0;
1092 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1093 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001094
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001095 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001096 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1097
1098 if (thread != NULL && h->thread != thread) {
1099 h->filtered |= (1 << HIST_FILTER__THREAD);
1100 continue;
1101 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001102
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001103 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001104 }
1105}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001106
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001107int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001108{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001109 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001110}
1111
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001112int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001113{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001114 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001115}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001116
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001117void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001118{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001119 ++hists->stats.nr_events[0];
1120 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001121}
1122
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001123size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001124{
1125 int i;
1126 size_t ret = 0;
1127
1128 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001129 const char *name;
Thomas Gleixner3835bc002010-12-07 12:48:42 +00001130
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001131 if (hists->stats.nr_events[i] == 0)
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001132 continue;
1133
1134 name = perf_event__name(i);
Thomas Gleixner3835bc002010-12-07 12:48:42 +00001135 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001136 continue;
Thomas Gleixner3835bc002010-12-07 12:48:42 +00001137
1138 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001139 hists->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001140 }
1141
1142 return ret;
1143}
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001144
1145void hists__init(struct hists *hists)
1146{
1147 memset(hists, 0, sizeof(*hists));
1148 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1149 hists->entries_in = &hists->entries_in_array[0];
1150 hists->entries_collapsed = RB_ROOT;
1151 hists->entries = RB_ROOT;
1152 pthread_mutex_init(&hists->lock, NULL);
1153}