blob: 6f505d1abac72e493043e87f029ea9c0d9d4cea5 [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 Melo90cf1fb2011-10-19 13:09:10 -02009static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
13
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030014enum hist_filter {
15 HIST_FILTER__DSO,
16 HIST_FILTER__THREAD,
17 HIST_FILTER__PARENT,
18};
19
John Kacur3d1d07e2009-09-28 15:32:55 +020020struct callchain_param callchain_param = {
21 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080022 .min_percent = 0.5,
23 .order = ORDER_CALLEE
John Kacur3d1d07e2009-09-28 15:32:55 +020024};
25
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029}
30
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031void hists__set_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 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030034}
35
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030036bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030037{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030038 if (len > hists__col_len(hists, col)) {
39 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030040 return true;
41 }
42 return false;
43}
44
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030045static void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030046{
47 enum hist_column col;
48
49 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030050 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030051}
52
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030053static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030054{
55 u16 len;
56
57 if (h->ms.sym)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030058 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen);
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -030059 else {
60 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
61
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030062 if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width &&
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -030063 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
64 !symbol_conf.dso_list)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030065 hists__set_col_len(hists, HISTC_DSO,
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -030066 unresolved_col_width);
67 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030068
69 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030070 if (hists__new_col_len(hists, HISTC_COMM, len))
71 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030072
73 if (h->ms.map) {
74 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030075 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030076 }
77}
78
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -020079static void hist_entry__add_cpumode_period(struct hist_entry *he,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030080 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080081{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030082 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080083 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -020084 he->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080085 break;
86 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -020087 he->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080088 break;
89 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -020090 he->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080091 break;
92 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -020093 he->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080094 break;
95 default:
96 break;
97 }
98}
99
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300100static void hist_entry__decay(struct hist_entry *he)
101{
102 he->period = (he->period * 7) / 8;
103 he->nr_events = (he->nr_events * 7) / 8;
104}
105
106static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
107{
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200108 u64 prev_period = he->period;
109
110 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300111 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200112
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300113 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200114
115 if (!he->filtered)
116 hists->stats.total_period -= prev_period - he->period;
117
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300118 return he->period == 0;
119}
120
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200121static void __hists__decay_entries(struct hists *hists, bool zap_user,
122 bool zap_kernel, bool threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300123{
124 struct rb_node *next = rb_first(&hists->entries);
125 struct hist_entry *n;
126
127 while (next) {
128 n = rb_entry(next, struct hist_entry, rb_node);
129 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300130 /*
131 * We may be annotating this, for instance, so keep it here in
132 * case some it gets new samples, we'll eventually free it when
133 * the user stops browsing and it agains gets fully decayed.
134 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200135 if (((zap_user && n->level == '.') ||
136 (zap_kernel && n->level != '.') ||
137 hists__decay_entry(hists, n)) &&
138 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300139 rb_erase(&n->rb_node, &hists->entries);
140
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300141 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300142 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
143
144 hist_entry__free(n);
145 --hists->nr_entries;
146 }
147 }
148}
149
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200150void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300151{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200152 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300153}
154
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200155void hists__decay_entries_threaded(struct hists *hists,
156 bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300157{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200158 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300159}
160
John Kacur3d1d07e2009-09-28 15:32:55 +0200161/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300162 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200163 */
164
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300165static struct hist_entry *hist_entry__new(struct hist_entry *template)
166{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200167 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200168 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300169
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200170 if (he != NULL) {
171 *he = *template;
172 he->nr_events = 1;
173 if (he->ms.map)
174 he->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300175 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200176 callchain_init(he->callchain);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300177 }
178
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200179 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300180}
181
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300182static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300183{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300184 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300185 hists__calc_col_len(hists, h);
186 ++hists->nr_entries;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300187 hists->stats.total_period += h->period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300188 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300189}
190
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300191static u8 symbol__parent_filter(const struct symbol *parent)
192{
193 if (symbol_conf.exclude_other && parent == NULL)
194 return 1 << HIST_FILTER__PARENT;
195 return 0;
196}
197
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300198struct hist_entry *__hists__add_entry(struct hists *hists,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300199 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300200 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300201{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300202 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300203 struct rb_node *parent = NULL;
204 struct hist_entry *he;
205 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200206 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300207 .ms = {
208 .map = al->map,
209 .sym = al->sym,
210 },
Arun Sharmaf60f3592010-06-04 11:27:10 -0300211 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200212 .ip = al->addr,
213 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300214 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300215 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300216 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300217 };
218 int cmp;
219
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300220 pthread_mutex_lock(&hists->lock);
221
222 p = &hists->entries_in->rb_node;
223
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300224 while (*p != NULL) {
225 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300226 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300227
228 cmp = hist_entry__cmp(&entry, he);
229
230 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300231 he->period += period;
232 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300233 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300234 }
235
236 if (cmp < 0)
237 p = &(*p)->rb_left;
238 else
239 p = &(*p)->rb_right;
240 }
241
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300242 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300243 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300244 goto out_unlock;
245
246 rb_link_node(&he->rb_node_in, parent, p);
247 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300248out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300249 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300250out_unlock:
251 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300252 return he;
253}
254
John Kacur3d1d07e2009-09-28 15:32:55 +0200255int64_t
256hist_entry__cmp(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) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200262 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200263 if (cmp)
264 break;
265 }
266
267 return cmp;
268}
269
270int64_t
271hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
272{
273 struct sort_entry *se;
274 int64_t cmp = 0;
275
276 list_for_each_entry(se, &hist_entry__sort_list, list) {
277 int64_t (*f)(struct hist_entry *, struct hist_entry *);
278
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200279 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200280
281 cmp = f(left, right);
282 if (cmp)
283 break;
284 }
285
286 return cmp;
287}
288
289void hist_entry__free(struct hist_entry *he)
290{
291 free(he);
292}
293
294/*
295 * collapse the histogram
296 */
297
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300298static bool hists__collapse_insert_entry(struct hists *hists,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100299 struct rb_root *root,
300 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200301{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200302 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200303 struct rb_node *parent = NULL;
304 struct hist_entry *iter;
305 int64_t cmp;
306
307 while (*p != NULL) {
308 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300309 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200310
311 cmp = hist_entry__collapse(iter, he);
312
313 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300314 iter->period += he->period;
Stephane Eraniane39622c2011-10-03 11:38:15 +0200315 iter->nr_events += he->nr_events;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100316 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300317 callchain_cursor_reset(&hists->callchain_cursor);
318 callchain_merge(&hists->callchain_cursor, iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100319 he->callchain);
320 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200321 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300322 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200323 }
324
325 if (cmp < 0)
326 p = &(*p)->rb_left;
327 else
328 p = &(*p)->rb_right;
329 }
330
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300331 rb_link_node(&he->rb_node_in, parent, p);
332 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300333 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200334}
335
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300336static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
337{
338 struct rb_root *root;
339
340 pthread_mutex_lock(&hists->lock);
341
342 root = hists->entries_in;
343 if (++hists->entries_in > &hists->entries_in_array[1])
344 hists->entries_in = &hists->entries_in_array[0];
345
346 pthread_mutex_unlock(&hists->lock);
347
348 return root;
349}
350
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200351static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
352{
353 hists__filter_entry_by_dso(hists, he);
354 hists__filter_entry_by_thread(hists, he);
355}
356
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300357static void __hists__collapse_resort(struct hists *hists, bool threaded)
358{
359 struct rb_root *root;
360 struct rb_node *next;
361 struct hist_entry *n;
362
363 if (!sort__need_collapse && !threaded)
364 return;
365
366 root = hists__get_rotate_entries_in(hists);
367 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300368
369 while (next) {
370 n = rb_entry(next, struct hist_entry, rb_node_in);
371 next = rb_next(&n->rb_node_in);
372
373 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200374 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
375 /*
376 * If it wasn't combined with one of the entries already
377 * collapsed, we need to apply the filters that may have
378 * been set by, say, the hist_browser.
379 */
380 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200381 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300382 }
383}
384
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300385void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200386{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300387 return __hists__collapse_resort(hists, false);
388}
John Kacur3d1d07e2009-09-28 15:32:55 +0200389
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300390void hists__collapse_resort_threaded(struct hists *hists)
391{
392 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200393}
394
395/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300396 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200397 */
398
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300399static void __hists__insert_output_entry(struct rb_root *entries,
400 struct hist_entry *he,
401 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200402{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300403 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200404 struct rb_node *parent = NULL;
405 struct hist_entry *iter;
406
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200407 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300408 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200409 min_callchain_hits, &callchain_param);
410
411 while (*p != NULL) {
412 parent = *p;
413 iter = rb_entry(parent, struct hist_entry, rb_node);
414
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300415 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200416 p = &(*p)->rb_left;
417 else
418 p = &(*p)->rb_right;
419 }
420
421 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300422 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200423}
424
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300425static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200426{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300427 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200428 struct rb_node *next;
429 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200430 u64 min_callchain_hits;
431
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300432 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200433
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300434 if (sort__need_collapse || threaded)
435 root = &hists->entries_collapsed;
436 else
437 root = hists->entries_in;
438
439 next = rb_first(root);
440 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200441
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300442 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200443 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300444 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300445
John Kacur3d1d07e2009-09-28 15:32:55 +0200446 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300447 n = rb_entry(next, struct hist_entry, rb_node_in);
448 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200449
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300450 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300451 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200452 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300453}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200454
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300455void hists__output_resort(struct hists *hists)
456{
457 return __hists__output_resort(hists, false);
458}
459
460void hists__output_resort_threaded(struct hists *hists)
461{
462 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200463}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200464
465static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
466{
467 int i;
468 int ret = fprintf(fp, " ");
469
470 for (i = 0; i < left_margin; i++)
471 ret += fprintf(fp, " ");
472
473 return ret;
474}
475
476static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
477 int left_margin)
478{
479 int i;
480 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
481
482 for (i = 0; i < depth; i++)
483 if (depth_mask & (1 << i))
484 ret += fprintf(fp, "| ");
485 else
486 ret += fprintf(fp, " ");
487
488 ret += fprintf(fp, "\n");
489
490 return ret;
491}
492
493static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300494 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100495 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200496 int left_margin)
497{
498 int i;
499 size_t ret = 0;
500
501 ret += callchain__fprintf_left_margin(fp, left_margin);
502 for (i = 0; i < depth; i++) {
503 if (depth_mask & (1 << i))
504 ret += fprintf(fp, "|");
505 else
506 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300507 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200508 double percent;
509
510 percent = hits * 100.0 / total_samples;
511 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
512 } else
513 ret += fprintf(fp, "%s", " ");
514 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300515 if (chain->ms.sym)
516 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200517 else
518 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
519
520 return ret;
521}
522
523static struct symbol *rem_sq_bracket;
524static struct callchain_list rem_hits;
525
526static void init_rem_hits(void)
527{
528 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
529 if (!rem_sq_bracket) {
530 fprintf(stderr, "Not enough memory to display remaining hits\n");
531 return;
532 }
533
534 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300535 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200536}
537
538static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
539 u64 total_samples, int depth,
540 int depth_mask, int left_margin)
541{
542 struct rb_node *node, *next;
543 struct callchain_node *child;
544 struct callchain_list *chain;
545 int new_depth_mask = depth_mask;
546 u64 new_total;
547 u64 remaining;
548 size_t ret = 0;
549 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300550 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200551
552 if (callchain_param.mode == CHAIN_GRAPH_REL)
553 new_total = self->children_hit;
554 else
555 new_total = total_samples;
556
557 remaining = new_total;
558
559 node = rb_first(&self->rb_root);
560 while (node) {
561 u64 cumul;
562
563 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100564 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200565 remaining -= cumul;
566
567 /*
568 * The depth mask manages the output of pipes that show
569 * the depth. We don't want to keep the pipes of the current
570 * level for the last child of this depth.
571 * Except if we have remaining filtered hits. They will
572 * supersede the last child
573 */
574 next = rb_next(node);
575 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
576 new_depth_mask &= ~(1 << (depth - 1));
577
578 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800579 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200580 * to keep the level link until we reach the last child
581 */
582 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
583 left_margin);
584 i = 0;
585 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200586 ret += ipchain__fprintf_graph(fp, chain, depth,
587 new_depth_mask, i++,
588 new_total,
589 cumul,
590 left_margin);
591 }
592 ret += __callchain__fprintf_graph(fp, child, new_total,
593 depth + 1,
594 new_depth_mask | (1 << depth),
595 left_margin);
596 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300597 if (++entries_printed == callchain_param.print_limit)
598 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200599 }
600
601 if (callchain_param.mode == CHAIN_GRAPH_REL &&
602 remaining && remaining != new_total) {
603
604 if (!rem_sq_bracket)
605 return ret;
606
607 new_depth_mask &= ~(1 << (depth - 1));
608
609 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
610 new_depth_mask, 0, new_total,
611 remaining, left_margin);
612 }
613
614 return ret;
615}
616
617static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
618 u64 total_samples, int left_margin)
619{
620 struct callchain_list *chain;
621 bool printed = false;
622 int i = 0;
623 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300624 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200625
626 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200627 if (!i++ && sort__first_dimension == SORT_SYM)
628 continue;
629
630 if (!printed) {
631 ret += callchain__fprintf_left_margin(fp, left_margin);
632 ret += fprintf(fp, "|\n");
633 ret += callchain__fprintf_left_margin(fp, left_margin);
634 ret += fprintf(fp, "---");
635
636 left_margin += 3;
637 printed = true;
638 } else
639 ret += callchain__fprintf_left_margin(fp, left_margin);
640
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300641 if (chain->ms.sym)
642 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200643 else
644 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300645
646 if (++entries_printed == callchain_param.print_limit)
647 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200648 }
649
650 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
651
652 return ret;
653}
654
655static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
656 u64 total_samples)
657{
658 struct callchain_list *chain;
659 size_t ret = 0;
660
661 if (!self)
662 return 0;
663
664 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
665
666
667 list_for_each_entry(chain, &self->val, list) {
668 if (chain->ip >= PERF_CONTEXT_MAX)
669 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300670 if (chain->ms.sym)
671 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200672 else
673 ret += fprintf(fp, " %p\n",
674 (void *)(long)chain->ip);
675 }
676
677 return ret;
678}
679
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200680static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
681 u64 total_samples, int left_margin,
682 FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200683{
684 struct rb_node *rb_node;
685 struct callchain_node *chain;
686 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300687 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200688
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200689 rb_node = rb_first(&he->sorted_chain);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200690 while (rb_node) {
691 double percent;
692
693 chain = rb_entry(rb_node, struct callchain_node, rb_node);
694 percent = chain->hit * 100.0 / total_samples;
695 switch (callchain_param.mode) {
696 case CHAIN_FLAT:
697 ret += percent_color_fprintf(fp, " %6.2f%%\n",
698 percent);
699 ret += callchain__fprintf_flat(fp, chain, total_samples);
700 break;
701 case CHAIN_GRAPH_ABS: /* Falldown */
702 case CHAIN_GRAPH_REL:
703 ret += callchain__fprintf_graph(fp, chain, total_samples,
704 left_margin);
705 case CHAIN_NONE:
706 default:
707 break;
708 }
709 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300710 if (++entries_printed == callchain_param.print_limit)
711 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200712 rb_node = rb_next(rb_node);
713 }
714
715 return ret;
716}
717
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300718void hists__output_recalc_col_len(struct hists *hists, int max_rows)
719{
720 struct rb_node *next = rb_first(&hists->entries);
721 struct hist_entry *n;
722 int row = 0;
723
724 hists__reset_col_len(hists);
725
726 while (next && row++ < max_rows) {
727 n = rb_entry(next, struct hist_entry, rb_node);
Arnaldo Carvalho de Melod197fd52011-10-20 07:35:45 -0200728 if (!n->filtered)
729 hists__calc_col_len(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300730 next = rb_next(&n->rb_node);
731 }
732}
733
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200734static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200735 size_t size, struct hists *pair_hists,
736 bool show_displacement, long displacement,
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200737 bool color, u64 total_period)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200738{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300739 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200740 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200741 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300742 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200743
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200744 if (symbol_conf.exclude_other && !he->parent)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200745 return 0;
746
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300747 if (pair_hists) {
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200748 period = he->pair ? he->pair->period : 0;
749 nr_events = he->pair ? he->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300750 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200751 period_sys = he->pair ? he->pair->period_sys : 0;
752 period_us = he->pair ? he->pair->period_us : 0;
753 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
754 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200755 } else {
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200756 period = he->period;
757 nr_events = he->nr_events;
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200758 total = total_period;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200759 period_sys = he->period_sys;
760 period_us = he->period_us;
761 period_guest_sys = he->period_guest_sys;
762 period_guest_us = he->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200763 }
764
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300765 if (total) {
766 if (color)
767 ret = percent_color_snprintf(s, size,
768 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300769 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300770 else
771 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300772 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800773 if (symbol_conf.show_cpu_utilization) {
774 ret += percent_color_snprintf(s + ret, size - ret,
775 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300776 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800777 ret += percent_color_snprintf(s + ret, size - ret,
778 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300779 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800780 if (perf_guest) {
781 ret += percent_color_snprintf(s + ret,
782 size - ret,
783 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300784 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800785 total);
786 ret += percent_color_snprintf(s + ret,
787 size - ret,
788 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300789 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800790 total);
791 }
792 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300793 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200794 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200795
796 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200797 if (sep)
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200798 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200799 else
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200800 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200801 }
802
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300803 if (symbol_conf.show_total_period) {
804 if (sep)
805 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
806 else
807 ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
808 }
809
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300810 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200811 char bf[32];
812 double old_percent = 0, new_percent = 0, diff;
813
814 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300815 old_percent = (period * 100.0) / total;
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200816 if (total_period > 0)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200817 new_percent = (he->period * 100.0) / total_period;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200818
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200819 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200820
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200821 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200822 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
823 else
824 snprintf(bf, sizeof(bf), " ");
825
826 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300827 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200828 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300829 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200830
831 if (show_displacement) {
832 if (displacement)
833 snprintf(bf, sizeof(bf), "%+4ld", displacement);
834 else
835 snprintf(bf, sizeof(bf), " ");
836
837 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300838 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200839 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300840 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200841 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200842 }
843
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200844 return ret;
845}
846
847int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
848 struct hists *hists)
849{
850 const char *sep = symbol_conf.field_sep;
851 struct sort_entry *se;
852 int ret = 0;
853
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200854 list_for_each_entry(se, &hist_entry__sort_list, list) {
855 if (se->elide)
856 continue;
857
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300858 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200859 ret += se->se_snprintf(he, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300860 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200861 }
862
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300863 return ret;
864}
865
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200866static int hist_entry__fprintf(struct hist_entry *he, size_t size,
867 struct hists *hists, struct hists *pair_hists,
868 bool show_displacement, long displacement,
869 u64 total_period, FILE *fp)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300870{
871 char bf[512];
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200872 int ret;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300873
874 if (size == 0 || size > sizeof(bf))
875 size = sizeof(bf);
876
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200877 ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
878 show_displacement, displacement,
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200879 true, total_period);
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200880 hist_entry__snprintf(he, bf + ret, size - ret, hists);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300881 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300882}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200883
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200884static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
885 struct hists *hists,
886 u64 total_period, FILE *fp)
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300887{
888 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200889
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300890 if (sort__first_dimension == SORT_COMM) {
891 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
892 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300893 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200894 left_margin -= thread__comm_len(he->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200895 }
896
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200897 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200898}
899
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300900size_t hists__fprintf(struct hists *hists, struct hists *pair,
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300901 bool show_displacement, bool show_header, int max_rows,
902 int max_cols, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200903{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200904 struct sort_entry *se;
905 struct rb_node *nd;
906 size_t ret = 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200907 u64 total_period;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200908 unsigned long position = 1;
909 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200910 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200911 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300912 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300913 int nr_rows = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200914
915 init_rem_hits();
916
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300917 if (!show_header)
918 goto print_entries;
919
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200920 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
921
Namhyung Kim0ed35abc2012-01-08 02:25:32 +0900922 if (symbol_conf.show_cpu_utilization) {
923 if (sep) {
924 ret += fprintf(fp, "%csys", *sep);
925 ret += fprintf(fp, "%cus", *sep);
926 if (perf_guest) {
927 ret += fprintf(fp, "%cguest sys", *sep);
928 ret += fprintf(fp, "%cguest us", *sep);
929 }
930 } else {
931 ret += fprintf(fp, " sys ");
932 ret += fprintf(fp, " us ");
933 if (perf_guest) {
934 ret += fprintf(fp, " guest sys ");
935 ret += fprintf(fp, " guest us ");
936 }
937 }
938 }
939
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200940 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200941 if (sep)
942 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200943 else
944 fputs(" Samples ", fp);
945 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200946
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300947 if (symbol_conf.show_total_period) {
948 if (sep)
949 ret += fprintf(fp, "%cPeriod", *sep);
950 else
951 ret += fprintf(fp, " Period ");
952 }
953
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200954 if (pair) {
955 if (sep)
956 ret += fprintf(fp, "%cDelta", *sep);
957 else
958 ret += fprintf(fp, " Delta ");
959
960 if (show_displacement) {
961 if (sep)
962 ret += fprintf(fp, "%cDisplacement", *sep);
963 else
964 ret += fprintf(fp, " Displ");
965 }
966 }
967
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200968 list_for_each_entry(se, &hist_entry__sort_list, list) {
969 if (se->elide)
970 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200971 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200972 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200973 continue;
974 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200975 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300976 if (symbol_conf.col_width_list_str) {
977 if (col_width) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300978 hists__set_col_len(hists, se->se_width_idx,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300979 atoi(col_width));
980 col_width = strchr(col_width, ',');
981 if (col_width)
982 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200983 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200984 }
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300985 if (!hists__new_col_len(hists, se->se_width_idx, width))
986 width = hists__col_len(hists, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200987 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200988 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300989
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200990 fprintf(fp, "\n");
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300991 if (max_rows && ++nr_rows >= max_rows)
992 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200993
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200994 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200995 goto print_entries;
996
997 fprintf(fp, "# ........");
Namhyung Kim0ed35abc2012-01-08 02:25:32 +0900998 if (symbol_conf.show_cpu_utilization)
999 fprintf(fp, " ....... .......");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001000 if (symbol_conf.show_nr_samples)
1001 fprintf(fp, " ..........");
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001002 if (symbol_conf.show_total_period)
1003 fprintf(fp, " ............");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001004 if (pair) {
1005 fprintf(fp, " ..........");
1006 if (show_displacement)
1007 fprintf(fp, " .....");
1008 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001009 list_for_each_entry(se, &hist_entry__sort_list, list) {
1010 unsigned int i;
1011
1012 if (se->elide)
1013 continue;
1014
1015 fprintf(fp, " ");
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001016 width = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001017 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001018 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001019 for (i = 0; i < width; i++)
1020 fprintf(fp, ".");
1021 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001022
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001023 fprintf(fp, "\n");
1024 if (max_rows && ++nr_rows >= max_rows)
1025 goto out;
1026
1027 fprintf(fp, "#\n");
1028 if (max_rows && ++nr_rows >= max_rows)
1029 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001030
1031print_entries:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001032 total_period = hists->stats.total_period;
1033
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001034 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001035 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1036
Frederic Weisbeckere84d2122011-06-29 22:23:03 +02001037 if (h->filtered)
1038 continue;
1039
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001040 if (show_displacement) {
1041 if (h->pair != NULL)
1042 displacement = ((long)h->pair->position -
1043 (long)position);
1044 else
1045 displacement = 0;
1046 ++position;
1047 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001048 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001049 displacement, total_period, fp);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -03001050
1051 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001052 ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001053 if (max_rows && ++nr_rows >= max_rows)
1054 goto out;
1055
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -03001056 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001057 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03001058 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001059 fprintf(fp, "%.10s end\n", graph_dotted_line);
1060 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001061 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001062out:
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001063 free(rem_sq_bracket);
1064
1065 return ret;
1066}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001067
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001068/*
1069 * See hists__fprintf to match the column widths
1070 */
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001071unsigned int hists__sort_list_width(struct hists *hists)
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001072{
1073 struct sort_entry *se;
1074 int ret = 9; /* total % */
1075
1076 if (symbol_conf.show_cpu_utilization) {
1077 ret += 7; /* count_sys % */
1078 ret += 6; /* count_us % */
1079 if (perf_guest) {
1080 ret += 13; /* count_guest_sys % */
1081 ret += 12; /* count_guest_us % */
1082 }
1083 }
1084
1085 if (symbol_conf.show_nr_samples)
1086 ret += 11;
1087
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001088 if (symbol_conf.show_total_period)
1089 ret += 13;
1090
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001091 list_for_each_entry(se, &hist_entry__sort_list, list)
1092 if (!se->elide)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001093 ret += 2 + hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001094
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -03001095 if (verbose) /* Addr + origin */
1096 ret += 3 + BITS_PER_LONG / 4;
1097
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001098 return ret;
1099}
1100
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001101static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001102 enum hist_filter filter)
1103{
1104 h->filtered &= ~(1 << filter);
1105 if (h->filtered)
1106 return;
1107
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001108 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001109 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001110 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001111 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001112 hists->stats.total_period += h->period;
1113 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001114
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001115 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001116}
1117
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001118
1119static bool hists__filter_entry_by_dso(struct hists *hists,
1120 struct hist_entry *he)
1121{
1122 if (hists->dso_filter != NULL &&
1123 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1124 he->filtered |= (1 << HIST_FILTER__DSO);
1125 return true;
1126 }
1127
1128 return false;
1129}
1130
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001131void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001132{
1133 struct rb_node *nd;
1134
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001135 hists->nr_entries = hists->stats.total_period = 0;
1136 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1137 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001138
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001139 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001140 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1141
1142 if (symbol_conf.exclude_other && !h->parent)
1143 continue;
1144
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001145 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001146 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001147
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001148 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001149 }
1150}
1151
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001152static bool hists__filter_entry_by_thread(struct hists *hists,
1153 struct hist_entry *he)
1154{
1155 if (hists->thread_filter != NULL &&
1156 he->thread != hists->thread_filter) {
1157 he->filtered |= (1 << HIST_FILTER__THREAD);
1158 return true;
1159 }
1160
1161 return false;
1162}
1163
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001164void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001165{
1166 struct rb_node *nd;
1167
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001168 hists->nr_entries = hists->stats.total_period = 0;
1169 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1170 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001171
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001172 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001173 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1174
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001175 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001176 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001177
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001178 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001179 }
1180}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001181
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001182int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001183{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001184 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001185}
1186
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001187int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001188{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001189 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001190}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001191
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001192void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001193{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001194 ++hists->stats.nr_events[0];
1195 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001196}
1197
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001198size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001199{
1200 int i;
1201 size_t ret = 0;
1202
1203 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001204 const char *name;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001205
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001206 if (hists->stats.nr_events[i] == 0)
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001207 continue;
1208
1209 name = perf_event__name(i);
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001210 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001211 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001212
1213 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001214 hists->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001215 }
1216
1217 return ret;
1218}