blob: 7b5848ce15051c9787c9a8941e51009d94c0f8cb [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02006#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02007
8struct callchain_param callchain_param = {
9 .mode = CHAIN_GRAPH_REL,
10 .min_percent = 0.5
11};
12
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030013static void hist_entry__add_cpumode_period(struct hist_entry *self,
14 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030016 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080017 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030018 self->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080019 break;
20 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030021 self->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080022 break;
23 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030024 self->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080025 break;
26 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030027 self->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080028 break;
29 default:
30 break;
31 }
32}
33
John Kacur3d1d07e2009-09-28 15:32:55 +020034/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030035 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +020036 */
37
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030038static struct hist_entry *hist_entry__new(struct hist_entry *template)
39{
40 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
41 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
42
43 if (self != NULL) {
44 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030045 self->nr_events = 1;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030046 if (symbol_conf.use_callchain)
47 callchain_init(self->callchain);
48 }
49
50 return self;
51}
52
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -030053static void hists__inc_nr_entries(struct hists *self, struct hist_entry *entry)
54{
55 if (entry->ms.sym && self->max_sym_namelen < entry->ms.sym->namelen)
56 self->max_sym_namelen = entry->ms.sym->namelen;
57 ++self->nr_entries;
58}
59
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030060struct hist_entry *__hists__add_entry(struct hists *self,
61 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030062 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030063{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030064 struct rb_node **p = &self->entries.rb_node;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030065 struct rb_node *parent = NULL;
66 struct hist_entry *he;
67 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020068 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030069 .ms = {
70 .map = al->map,
71 .sym = al->sym,
72 },
Arun Sharmaf60f3592010-06-04 11:27:10 -030073 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020074 .ip = al->addr,
75 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030076 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030077 .parent = sym_parent,
78 };
79 int cmp;
80
81 while (*p != NULL) {
82 parent = *p;
83 he = rb_entry(parent, struct hist_entry, rb_node);
84
85 cmp = hist_entry__cmp(&entry, he);
86
87 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030088 he->period += period;
89 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030090 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030091 }
92
93 if (cmp < 0)
94 p = &(*p)->rb_left;
95 else
96 p = &(*p)->rb_right;
97 }
98
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030099 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300100 if (!he)
101 return NULL;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300102 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300103 rb_insert_color(&he->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300104 hists__inc_nr_entries(self, he);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300105out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300106 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300107 return he;
108}
109
John Kacur3d1d07e2009-09-28 15:32:55 +0200110int64_t
111hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
112{
113 struct sort_entry *se;
114 int64_t cmp = 0;
115
116 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200117 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200118 if (cmp)
119 break;
120 }
121
122 return cmp;
123}
124
125int64_t
126hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
127{
128 struct sort_entry *se;
129 int64_t cmp = 0;
130
131 list_for_each_entry(se, &hist_entry__sort_list, list) {
132 int64_t (*f)(struct hist_entry *, struct hist_entry *);
133
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200134 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200135
136 cmp = f(left, right);
137 if (cmp)
138 break;
139 }
140
141 return cmp;
142}
143
144void hist_entry__free(struct hist_entry *he)
145{
146 free(he);
147}
148
149/*
150 * collapse the histogram
151 */
152
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300153static bool collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200154{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200155 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200156 struct rb_node *parent = NULL;
157 struct hist_entry *iter;
158 int64_t cmp;
159
160 while (*p != NULL) {
161 parent = *p;
162 iter = rb_entry(parent, struct hist_entry, rb_node);
163
164 cmp = hist_entry__collapse(iter, he);
165
166 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300167 iter->period += he->period;
John Kacur3d1d07e2009-09-28 15:32:55 +0200168 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300169 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200170 }
171
172 if (cmp < 0)
173 p = &(*p)->rb_left;
174 else
175 p = &(*p)->rb_right;
176 }
177
178 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200179 rb_insert_color(&he->rb_node, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300180 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200181}
182
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300183void hists__collapse_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200184{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200185 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200186 struct rb_node *next;
187 struct hist_entry *n;
188
189 if (!sort__need_collapse)
190 return;
191
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200192 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300193 next = rb_first(&self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300194 self->nr_entries = 0;
195 self->max_sym_namelen = 0;
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200196
John Kacur3d1d07e2009-09-28 15:32:55 +0200197 while (next) {
198 n = rb_entry(next, struct hist_entry, rb_node);
199 next = rb_next(&n->rb_node);
200
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300201 rb_erase(&n->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300202 if (collapse__insert_entry(&tmp, n))
203 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200204 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200205
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300206 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200207}
208
209/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300210 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200211 */
212
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300213static void __hists__insert_output_entry(struct rb_root *entries,
214 struct hist_entry *he,
215 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200216{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300217 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200218 struct rb_node *parent = NULL;
219 struct hist_entry *iter;
220
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200221 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300222 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200223 min_callchain_hits, &callchain_param);
224
225 while (*p != NULL) {
226 parent = *p;
227 iter = rb_entry(parent, struct hist_entry, rb_node);
228
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300229 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200230 p = &(*p)->rb_left;
231 else
232 p = &(*p)->rb_right;
233 }
234
235 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300236 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200237}
238
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300239void hists__output_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200240{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200241 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200242 struct rb_node *next;
243 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200244 u64 min_callchain_hits;
245
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300246 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200247
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200248 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300249 next = rb_first(&self->entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200250
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300251 self->nr_entries = 0;
252 self->max_sym_namelen = 0;
253
John Kacur3d1d07e2009-09-28 15:32:55 +0200254 while (next) {
255 n = rb_entry(next, struct hist_entry, rb_node);
256 next = rb_next(&n->rb_node);
257
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300258 rb_erase(&n->rb_node, &self->entries);
259 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300260 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200261 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200262
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300263 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200264}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200265
266static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
267{
268 int i;
269 int ret = fprintf(fp, " ");
270
271 for (i = 0; i < left_margin; i++)
272 ret += fprintf(fp, " ");
273
274 return ret;
275}
276
277static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
278 int left_margin)
279{
280 int i;
281 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
282
283 for (i = 0; i < depth; i++)
284 if (depth_mask & (1 << i))
285 ret += fprintf(fp, "| ");
286 else
287 ret += fprintf(fp, " ");
288
289 ret += fprintf(fp, "\n");
290
291 return ret;
292}
293
294static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300295 int depth, int depth_mask, int period,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200296 u64 total_samples, int hits,
297 int left_margin)
298{
299 int i;
300 size_t ret = 0;
301
302 ret += callchain__fprintf_left_margin(fp, left_margin);
303 for (i = 0; i < depth; i++) {
304 if (depth_mask & (1 << i))
305 ret += fprintf(fp, "|");
306 else
307 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300308 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200309 double percent;
310
311 percent = hits * 100.0 / total_samples;
312 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
313 } else
314 ret += fprintf(fp, "%s", " ");
315 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300316 if (chain->ms.sym)
317 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200318 else
319 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
320
321 return ret;
322}
323
324static struct symbol *rem_sq_bracket;
325static struct callchain_list rem_hits;
326
327static void init_rem_hits(void)
328{
329 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
330 if (!rem_sq_bracket) {
331 fprintf(stderr, "Not enough memory to display remaining hits\n");
332 return;
333 }
334
335 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300336 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200337}
338
339static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
340 u64 total_samples, int depth,
341 int depth_mask, int left_margin)
342{
343 struct rb_node *node, *next;
344 struct callchain_node *child;
345 struct callchain_list *chain;
346 int new_depth_mask = depth_mask;
347 u64 new_total;
348 u64 remaining;
349 size_t ret = 0;
350 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300351 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200352
353 if (callchain_param.mode == CHAIN_GRAPH_REL)
354 new_total = self->children_hit;
355 else
356 new_total = total_samples;
357
358 remaining = new_total;
359
360 node = rb_first(&self->rb_root);
361 while (node) {
362 u64 cumul;
363
364 child = rb_entry(node, struct callchain_node, rb_node);
365 cumul = cumul_hits(child);
366 remaining -= cumul;
367
368 /*
369 * The depth mask manages the output of pipes that show
370 * the depth. We don't want to keep the pipes of the current
371 * level for the last child of this depth.
372 * Except if we have remaining filtered hits. They will
373 * supersede the last child
374 */
375 next = rb_next(node);
376 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
377 new_depth_mask &= ~(1 << (depth - 1));
378
379 /*
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800380 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200381 * to keep the level link until we reach the last child
382 */
383 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
384 left_margin);
385 i = 0;
386 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200387 ret += ipchain__fprintf_graph(fp, chain, depth,
388 new_depth_mask, i++,
389 new_total,
390 cumul,
391 left_margin);
392 }
393 ret += __callchain__fprintf_graph(fp, child, new_total,
394 depth + 1,
395 new_depth_mask | (1 << depth),
396 left_margin);
397 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300398 if (++entries_printed == callchain_param.print_limit)
399 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200400 }
401
402 if (callchain_param.mode == CHAIN_GRAPH_REL &&
403 remaining && remaining != new_total) {
404
405 if (!rem_sq_bracket)
406 return ret;
407
408 new_depth_mask &= ~(1 << (depth - 1));
409
410 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
411 new_depth_mask, 0, new_total,
412 remaining, left_margin);
413 }
414
415 return ret;
416}
417
418static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
419 u64 total_samples, int left_margin)
420{
421 struct callchain_list *chain;
422 bool printed = false;
423 int i = 0;
424 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300425 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200426
427 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200428 if (!i++ && sort__first_dimension == SORT_SYM)
429 continue;
430
431 if (!printed) {
432 ret += callchain__fprintf_left_margin(fp, left_margin);
433 ret += fprintf(fp, "|\n");
434 ret += callchain__fprintf_left_margin(fp, left_margin);
435 ret += fprintf(fp, "---");
436
437 left_margin += 3;
438 printed = true;
439 } else
440 ret += callchain__fprintf_left_margin(fp, left_margin);
441
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300442 if (chain->ms.sym)
443 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200444 else
445 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300446
447 if (++entries_printed == callchain_param.print_limit)
448 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200449 }
450
451 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
452
453 return ret;
454}
455
456static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
457 u64 total_samples)
458{
459 struct callchain_list *chain;
460 size_t ret = 0;
461
462 if (!self)
463 return 0;
464
465 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
466
467
468 list_for_each_entry(chain, &self->val, list) {
469 if (chain->ip >= PERF_CONTEXT_MAX)
470 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300471 if (chain->ms.sym)
472 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200473 else
474 ret += fprintf(fp, " %p\n",
475 (void *)(long)chain->ip);
476 }
477
478 return ret;
479}
480
481static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
482 u64 total_samples, int left_margin)
483{
484 struct rb_node *rb_node;
485 struct callchain_node *chain;
486 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300487 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200488
489 rb_node = rb_first(&self->sorted_chain);
490 while (rb_node) {
491 double percent;
492
493 chain = rb_entry(rb_node, struct callchain_node, rb_node);
494 percent = chain->hit * 100.0 / total_samples;
495 switch (callchain_param.mode) {
496 case CHAIN_FLAT:
497 ret += percent_color_fprintf(fp, " %6.2f%%\n",
498 percent);
499 ret += callchain__fprintf_flat(fp, chain, total_samples);
500 break;
501 case CHAIN_GRAPH_ABS: /* Falldown */
502 case CHAIN_GRAPH_REL:
503 ret += callchain__fprintf_graph(fp, chain, total_samples,
504 left_margin);
505 case CHAIN_NONE:
506 default:
507 break;
508 }
509 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300510 if (++entries_printed == callchain_param.print_limit)
511 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200512 rb_node = rb_next(rb_node);
513 }
514
515 return ret;
516}
517
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300518int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
519 struct hists *pair_hists, bool show_displacement,
520 long displacement, bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200521{
522 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300523 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200524 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300525 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200526
527 if (symbol_conf.exclude_other && !self->parent)
528 return 0;
529
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300530 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300531 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300532 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300533 period_sys = self->pair ? self->pair->period_sys : 0;
534 period_us = self->pair ? self->pair->period_us : 0;
535 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
536 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200537 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300538 period = self->period;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300539 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300540 period_sys = self->period_sys;
541 period_us = self->period_us;
542 period_guest_sys = self->period_guest_sys;
543 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200544 }
545
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300546 if (total) {
547 if (color)
548 ret = percent_color_snprintf(s, size,
549 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300550 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300551 else
552 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300553 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800554 if (symbol_conf.show_cpu_utilization) {
555 ret += percent_color_snprintf(s + ret, size - ret,
556 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300557 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800558 ret += percent_color_snprintf(s + ret, size - ret,
559 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300560 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800561 if (perf_guest) {
562 ret += percent_color_snprintf(s + ret,
563 size - ret,
564 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300565 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800566 total);
567 ret += percent_color_snprintf(s + ret,
568 size - ret,
569 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300570 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800571 total);
572 }
573 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300574 } else
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300575 ret = snprintf(s, size, sep ? "%lld" : "%12lld ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200576
577 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200578 if (sep)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300579 ret += snprintf(s + ret, size - ret, "%c%lld", *sep, period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200580 else
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300581 ret += snprintf(s + ret, size - ret, "%11lld", period);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200582 }
583
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300584 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200585 char bf[32];
586 double old_percent = 0, new_percent = 0, diff;
587
588 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300589 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300590 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300591 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200592
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200593 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200594
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200595 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200596 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
597 else
598 snprintf(bf, sizeof(bf), " ");
599
600 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300601 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200602 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300603 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200604
605 if (show_displacement) {
606 if (displacement)
607 snprintf(bf, sizeof(bf), "%+4ld", displacement);
608 else
609 snprintf(bf, sizeof(bf), " ");
610
611 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300612 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200613 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300614 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200615 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200616 }
617
618 list_for_each_entry(se, &hist_entry__sort_list, list) {
619 if (se->elide)
620 continue;
621
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300622 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200623 ret += se->se_snprintf(self, s + ret, size - ret,
624 se->se_width ? *se->se_width : 0);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200625 }
626
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300627 return ret;
628}
629
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300630int hist_entry__fprintf(struct hist_entry *self, struct hists *pair_hists,
631 bool show_displacement, long displacement, FILE *fp,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300632 u64 session_total)
633{
634 char bf[512];
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300635 hist_entry__snprintf(self, bf, sizeof(bf), pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300636 show_displacement, displacement,
637 true, session_total);
638 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300639}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200640
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300641static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
642 u64 session_total)
643{
644 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200645
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300646 if (sort__first_dimension == SORT_COMM) {
647 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
648 typeof(*se), list);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200649 left_margin = se->se_width ? *se->se_width : 0;
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300650 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200651 }
652
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300653 return hist_entry_callchain__fprintf(fp, self, session_total,
654 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200655}
656
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300657size_t hists__fprintf(struct hists *self, struct hists *pair,
658 bool show_displacement, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200659{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200660 struct sort_entry *se;
661 struct rb_node *nd;
662 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200663 unsigned long position = 1;
664 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200665 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200666 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300667 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200668
669 init_rem_hits();
670
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200671 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
672
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200673 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200674 if (sep)
675 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200676 else
677 fputs(" Samples ", fp);
678 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200679
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800680 if (symbol_conf.show_cpu_utilization) {
681 if (sep) {
682 ret += fprintf(fp, "%csys", *sep);
683 ret += fprintf(fp, "%cus", *sep);
684 if (perf_guest) {
685 ret += fprintf(fp, "%cguest sys", *sep);
686 ret += fprintf(fp, "%cguest us", *sep);
687 }
688 } else {
689 ret += fprintf(fp, " sys ");
690 ret += fprintf(fp, " us ");
691 if (perf_guest) {
692 ret += fprintf(fp, " guest sys ");
693 ret += fprintf(fp, " guest us ");
694 }
695 }
696 }
697
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200698 if (pair) {
699 if (sep)
700 ret += fprintf(fp, "%cDelta", *sep);
701 else
702 ret += fprintf(fp, " Delta ");
703
704 if (show_displacement) {
705 if (sep)
706 ret += fprintf(fp, "%cDisplacement", *sep);
707 else
708 ret += fprintf(fp, " Displ");
709 }
710 }
711
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200712 list_for_each_entry(se, &hist_entry__sort_list, list) {
713 if (se->elide)
714 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200715 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200716 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200717 continue;
718 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200719 width = strlen(se->se_header);
720 if (se->se_width) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200721 if (symbol_conf.col_width_list_str) {
722 if (col_width) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200723 *se->se_width = atoi(col_width);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200724 col_width = strchr(col_width, ',');
725 if (col_width)
726 ++col_width;
727 }
728 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200729 width = *se->se_width = max(*se->se_width, width);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200730 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200731 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200732 }
733 fprintf(fp, "\n");
734
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200735 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200736 goto print_entries;
737
738 fprintf(fp, "# ........");
739 if (symbol_conf.show_nr_samples)
740 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200741 if (pair) {
742 fprintf(fp, " ..........");
743 if (show_displacement)
744 fprintf(fp, " .....");
745 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200746 list_for_each_entry(se, &hist_entry__sort_list, list) {
747 unsigned int i;
748
749 if (se->elide)
750 continue;
751
752 fprintf(fp, " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200753 if (se->se_width)
754 width = *se->se_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200755 else
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200756 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200757 for (i = 0; i < width; i++)
758 fprintf(fp, ".");
759 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200760
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200761 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200762
763print_entries:
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300764 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200765 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
766
767 if (show_displacement) {
768 if (h->pair != NULL)
769 displacement = ((long)h->pair->position -
770 (long)position);
771 else
772 displacement = 0;
773 ++position;
774 }
Eric B Munsoneefc4652010-03-05 12:51:08 -0300775 ret += hist_entry__fprintf(h, pair, show_displacement,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300776 displacement, fp, self->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300777
778 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300779 ret += hist_entry__fprintf_callchain(h, fp, self->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300780
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300781 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300782 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300783 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300784 fprintf(fp, "%.10s end\n", graph_dotted_line);
785 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200786 }
787
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200788 free(rem_sq_bracket);
789
790 return ret;
791}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300792
793enum hist_filter {
794 HIST_FILTER__DSO,
795 HIST_FILTER__THREAD,
796};
797
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300798static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
799 enum hist_filter filter)
800{
801 h->filtered &= ~(1 << filter);
802 if (h->filtered)
803 return;
804
805 ++self->nr_entries;
806 self->stats.total_period += h->period;
807 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
808
809 if (h->ms.sym && self->max_sym_namelen < h->ms.sym->namelen)
810 self->max_sym_namelen = h->ms.sym->namelen;
811}
812
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300813void hists__filter_by_dso(struct hists *self, const struct dso *dso)
814{
815 struct rb_node *nd;
816
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300817 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300818 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300819 self->max_sym_namelen = 0;
820
821 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
822 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
823
824 if (symbol_conf.exclude_other && !h->parent)
825 continue;
826
827 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
828 h->filtered |= (1 << HIST_FILTER__DSO);
829 continue;
830 }
831
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300832 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300833 }
834}
835
836void hists__filter_by_thread(struct hists *self, const struct thread *thread)
837{
838 struct rb_node *nd;
839
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300840 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300841 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300842 self->max_sym_namelen = 0;
843
844 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
845 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
846
847 if (thread != NULL && h->thread != thread) {
848 h->filtered |= (1 << HIST_FILTER__THREAD);
849 continue;
850 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300851
852 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300853 }
854}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300855
856static int symbol__alloc_hist(struct symbol *self)
857{
858 struct sym_priv *priv = symbol__priv(self);
859 const int size = (sizeof(*priv->hist) +
860 (self->end - self->start) * sizeof(u64));
861
862 priv->hist = zalloc(size);
863 return priv->hist == NULL ? -1 : 0;
864}
865
866int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
867{
868 unsigned int sym_size, offset;
869 struct symbol *sym = self->ms.sym;
870 struct sym_priv *priv;
871 struct sym_hist *h;
872
873 if (!sym || !self->ms.map)
874 return 0;
875
876 priv = symbol__priv(sym);
877 if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
878 return -ENOMEM;
879
880 sym_size = sym->end - sym->start;
881 offset = ip - sym->start;
882
883 pr_debug3("%s: ip=%#Lx\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
884
885 if (offset >= sym_size)
886 return 0;
887
888 h = priv->hist;
889 h->sum++;
890 h->ip[offset]++;
891
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300892 pr_debug3("%#Lx %s: period++ [ip: %#Lx, %#Lx] => %Ld\n", self->ms.sym->start,
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300893 self->ms.sym->name, ip, ip - self->ms.sym->start, h->ip[offset]);
894 return 0;
895}
896
897static struct objdump_line *objdump_line__new(s64 offset, char *line)
898{
899 struct objdump_line *self = malloc(sizeof(*self));
900
901 if (self != NULL) {
902 self->offset = offset;
903 self->line = line;
904 }
905
906 return self;
907}
908
909void objdump_line__free(struct objdump_line *self)
910{
911 free(self->line);
912 free(self);
913}
914
915static void objdump__add_line(struct list_head *head, struct objdump_line *line)
916{
917 list_add_tail(&line->node, head);
918}
919
920struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
921 struct objdump_line *pos)
922{
923 list_for_each_entry_continue(pos, head, node)
924 if (pos->offset >= 0)
925 return pos;
926
927 return NULL;
928}
929
930static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
931 struct list_head *head)
932{
933 struct symbol *sym = self->ms.sym;
934 struct objdump_line *objdump_line;
935 char *line = NULL, *tmp, *tmp2, *c;
936 size_t line_len;
937 s64 line_ip, offset = -1;
938
939 if (getline(&line, &line_len, file) < 0)
940 return -1;
941
942 if (!line)
943 return -1;
944
945 while (line_len != 0 && isspace(line[line_len - 1]))
946 line[--line_len] = '\0';
947
948 c = strchr(line, '\n');
949 if (c)
950 *c = 0;
951
952 line_ip = -1;
953
954 /*
955 * Strip leading spaces:
956 */
957 tmp = line;
958 while (*tmp) {
959 if (*tmp != ' ')
960 break;
961 tmp++;
962 }
963
964 if (*tmp) {
965 /*
966 * Parse hexa addresses followed by ':'
967 */
968 line_ip = strtoull(tmp, &tmp2, 16);
Konstantin Stepanyuk75d9ef172010-05-27 02:10:11 +0400969 if (*tmp2 != ':' || tmp == tmp2)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300970 line_ip = -1;
971 }
972
973 if (line_ip != -1) {
974 u64 start = map__rip_2objdump(self->ms.map, sym->start);
975 offset = line_ip - start;
976 }
977
978 objdump_line = objdump_line__new(offset, line);
979 if (objdump_line == NULL) {
980 free(line);
981 return -1;
982 }
983 objdump__add_line(head, objdump_line);
984
985 return 0;
986}
987
988int hist_entry__annotate(struct hist_entry *self, struct list_head *head)
989{
990 struct symbol *sym = self->ms.sym;
991 struct map *map = self->ms.map;
992 struct dso *dso = map->dso;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -0300993 char *filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -0300994 bool free_filename = true;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300995 char command[PATH_MAX * 2];
996 FILE *file;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300997 int err = 0;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300998 u64 len;
999
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001000 if (filename == NULL) {
1001 if (dso->has_build_id) {
1002 pr_err("Can't annotate %s: not enough memory\n",
1003 sym->name);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001004 return -ENOMEM;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001005 }
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001006 goto fallback;
1007 } else if (readlink(filename, command, sizeof(command)) < 0 ||
1008 strstr(command, "[kernel.kallsyms]") ||
1009 access(filename, R_OK)) {
1010 free(filename);
1011fallback:
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001012 /*
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001013 * If we don't have build-ids or the build-id file isn't in the
1014 * cache, or is just a kallsyms file, well, lets hope that this
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001015 * DSO is the same as when 'perf record' ran.
1016 */
1017 filename = dso->long_name;
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001018 free_filename = false;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001019 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001020
1021 if (dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001022 if (dso->annotate_warned)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001023 goto out_free_filename;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001024 err = -ENOENT;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001025 dso->annotate_warned = 1;
1026 pr_err("Can't annotate %s: No vmlinux file was found in the "
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001027 "path\n", sym->name);
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001028 goto out_free_filename;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001029 }
1030
1031 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
1032 filename, sym->name, map->unmap_ip(map, sym->start),
1033 map->unmap_ip(map, sym->end));
1034
1035 len = sym->end - sym->start;
1036
1037 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1038 dso, dso->long_name, sym, sym->name);
1039
1040 snprintf(command, sizeof(command),
Stephane Eranian45d8e802010-06-03 15:50:01 +02001041 "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS -C %s|grep -v %s|expand",
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001042 map__rip_2objdump(map, sym->start),
1043 map__rip_2objdump(map, sym->end),
1044 filename, filename);
1045
1046 pr_debug("Executing: %s\n", command);
1047
1048 file = popen(command, "r");
1049 if (!file)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001050 goto out_free_filename;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001051
1052 while (!feof(file))
1053 if (hist_entry__parse_objdump_line(self, file, head) < 0)
1054 break;
1055
1056 pclose(file);
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001057out_free_filename:
Arnaldo Carvalho de Melo44bf4602010-05-23 19:12:25 -03001058 if (free_filename)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001059 free(filename);
1060 return err;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001061}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001062
1063void hists__inc_nr_events(struct hists *self, u32 type)
1064{
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001065 ++self->stats.nr_events[0];
1066 ++self->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001067}
1068
1069size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
1070{
1071 int i;
1072 size_t ret = 0;
1073
1074 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1075 if (!event__name[i])
1076 continue;
1077 ret += fprintf(fp, "%10s events: %10d\n",
1078 event__name[i], self->stats.nr_events[i]);
1079 }
1080
1081 return ret;
1082}