xref: /openbmc/linux/tools/perf/ui/stdio/hist.c (revision e23feb16)
1 #include <stdio.h>
2 
3 #include "../../util/util.h"
4 #include "../../util/hist.h"
5 #include "../../util/sort.h"
6 #include "../../util/evsel.h"
7 
8 
9 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
10 {
11 	int i;
12 	int ret = fprintf(fp, "            ");
13 
14 	for (i = 0; i < left_margin; i++)
15 		ret += fprintf(fp, " ");
16 
17 	return ret;
18 }
19 
20 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
21 					  int left_margin)
22 {
23 	int i;
24 	size_t ret = callchain__fprintf_left_margin(fp, left_margin);
25 
26 	for (i = 0; i < depth; i++)
27 		if (depth_mask & (1 << i))
28 			ret += fprintf(fp, "|          ");
29 		else
30 			ret += fprintf(fp, "           ");
31 
32 	ret += fprintf(fp, "\n");
33 
34 	return ret;
35 }
36 
37 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
38 				     int depth, int depth_mask, int period,
39 				     u64 total_samples, u64 hits,
40 				     int left_margin)
41 {
42 	int i;
43 	size_t ret = 0;
44 
45 	ret += callchain__fprintf_left_margin(fp, left_margin);
46 	for (i = 0; i < depth; i++) {
47 		if (depth_mask & (1 << i))
48 			ret += fprintf(fp, "|");
49 		else
50 			ret += fprintf(fp, " ");
51 		if (!period && i == depth - 1) {
52 			double percent;
53 
54 			percent = hits * 100.0 / total_samples;
55 			ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
56 		} else
57 			ret += fprintf(fp, "%s", "          ");
58 	}
59 	if (chain->ms.sym)
60 		ret += fprintf(fp, "%s\n", chain->ms.sym->name);
61 	else
62 		ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
63 
64 	return ret;
65 }
66 
67 static struct symbol *rem_sq_bracket;
68 static struct callchain_list rem_hits;
69 
70 static void init_rem_hits(void)
71 {
72 	rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
73 	if (!rem_sq_bracket) {
74 		fprintf(stderr, "Not enough memory to display remaining hits\n");
75 		return;
76 	}
77 
78 	strcpy(rem_sq_bracket->name, "[...]");
79 	rem_hits.ms.sym = rem_sq_bracket;
80 }
81 
82 static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
83 					 u64 total_samples, int depth,
84 					 int depth_mask, int left_margin)
85 {
86 	struct rb_node *node, *next;
87 	struct callchain_node *child;
88 	struct callchain_list *chain;
89 	int new_depth_mask = depth_mask;
90 	u64 remaining;
91 	size_t ret = 0;
92 	int i;
93 	uint entries_printed = 0;
94 
95 	remaining = total_samples;
96 
97 	node = rb_first(root);
98 	while (node) {
99 		u64 new_total;
100 		u64 cumul;
101 
102 		child = rb_entry(node, struct callchain_node, rb_node);
103 		cumul = callchain_cumul_hits(child);
104 		remaining -= cumul;
105 
106 		/*
107 		 * The depth mask manages the output of pipes that show
108 		 * the depth. We don't want to keep the pipes of the current
109 		 * level for the last child of this depth.
110 		 * Except if we have remaining filtered hits. They will
111 		 * supersede the last child
112 		 */
113 		next = rb_next(node);
114 		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
115 			new_depth_mask &= ~(1 << (depth - 1));
116 
117 		/*
118 		 * But we keep the older depth mask for the line separator
119 		 * to keep the level link until we reach the last child
120 		 */
121 		ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
122 						   left_margin);
123 		i = 0;
124 		list_for_each_entry(chain, &child->val, list) {
125 			ret += ipchain__fprintf_graph(fp, chain, depth,
126 						      new_depth_mask, i++,
127 						      total_samples,
128 						      cumul,
129 						      left_margin);
130 		}
131 
132 		if (callchain_param.mode == CHAIN_GRAPH_REL)
133 			new_total = child->children_hit;
134 		else
135 			new_total = total_samples;
136 
137 		ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
138 						  depth + 1,
139 						  new_depth_mask | (1 << depth),
140 						  left_margin);
141 		node = next;
142 		if (++entries_printed == callchain_param.print_limit)
143 			break;
144 	}
145 
146 	if (callchain_param.mode == CHAIN_GRAPH_REL &&
147 		remaining && remaining != total_samples) {
148 
149 		if (!rem_sq_bracket)
150 			return ret;
151 
152 		new_depth_mask &= ~(1 << (depth - 1));
153 		ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
154 					      new_depth_mask, 0, total_samples,
155 					      remaining, left_margin);
156 	}
157 
158 	return ret;
159 }
160 
161 static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
162 				       u64 total_samples, int left_margin)
163 {
164 	struct callchain_node *cnode;
165 	struct callchain_list *chain;
166 	u32 entries_printed = 0;
167 	bool printed = false;
168 	struct rb_node *node;
169 	int i = 0;
170 	int ret = 0;
171 
172 	/*
173 	 * If have one single callchain root, don't bother printing
174 	 * its percentage (100 % in fractal mode and the same percentage
175 	 * than the hist in graph mode). This also avoid one level of column.
176 	 */
177 	node = rb_first(root);
178 	if (node && !rb_next(node)) {
179 		cnode = rb_entry(node, struct callchain_node, rb_node);
180 		list_for_each_entry(chain, &cnode->val, list) {
181 			/*
182 			 * If we sort by symbol, the first entry is the same than
183 			 * the symbol. No need to print it otherwise it appears as
184 			 * displayed twice.
185 			 */
186 			if (!i++ && sort__first_dimension == SORT_SYM)
187 				continue;
188 			if (!printed) {
189 				ret += callchain__fprintf_left_margin(fp, left_margin);
190 				ret += fprintf(fp, "|\n");
191 				ret += callchain__fprintf_left_margin(fp, left_margin);
192 				ret += fprintf(fp, "---");
193 				left_margin += 3;
194 				printed = true;
195 			} else
196 				ret += callchain__fprintf_left_margin(fp, left_margin);
197 
198 			if (chain->ms.sym)
199 				ret += fprintf(fp, " %s\n", chain->ms.sym->name);
200 			else
201 				ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
202 
203 			if (++entries_printed == callchain_param.print_limit)
204 				break;
205 		}
206 		root = &cnode->rb_root;
207 	}
208 
209 	ret += __callchain__fprintf_graph(fp, root, total_samples,
210 					  1, 1, left_margin);
211 	ret += fprintf(fp, "\n");
212 
213 	return ret;
214 }
215 
216 static size_t __callchain__fprintf_flat(FILE *fp,
217 					struct callchain_node *self,
218 					u64 total_samples)
219 {
220 	struct callchain_list *chain;
221 	size_t ret = 0;
222 
223 	if (!self)
224 		return 0;
225 
226 	ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
227 
228 
229 	list_for_each_entry(chain, &self->val, list) {
230 		if (chain->ip >= PERF_CONTEXT_MAX)
231 			continue;
232 		if (chain->ms.sym)
233 			ret += fprintf(fp, "                %s\n", chain->ms.sym->name);
234 		else
235 			ret += fprintf(fp, "                %p\n",
236 					(void *)(long)chain->ip);
237 	}
238 
239 	return ret;
240 }
241 
242 static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
243 				      u64 total_samples)
244 {
245 	size_t ret = 0;
246 	u32 entries_printed = 0;
247 	struct rb_node *rb_node;
248 	struct callchain_node *chain;
249 
250 	rb_node = rb_first(self);
251 	while (rb_node) {
252 		double percent;
253 
254 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
255 		percent = chain->hit * 100.0 / total_samples;
256 
257 		ret = percent_color_fprintf(fp, "           %6.2f%%\n", percent);
258 		ret += __callchain__fprintf_flat(fp, chain, total_samples);
259 		ret += fprintf(fp, "\n");
260 		if (++entries_printed == callchain_param.print_limit)
261 			break;
262 
263 		rb_node = rb_next(rb_node);
264 	}
265 
266 	return ret;
267 }
268 
269 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
270 					    u64 total_samples, int left_margin,
271 					    FILE *fp)
272 {
273 	switch (callchain_param.mode) {
274 	case CHAIN_GRAPH_REL:
275 		return callchain__fprintf_graph(fp, &he->sorted_chain, he->stat.period,
276 						left_margin);
277 		break;
278 	case CHAIN_GRAPH_ABS:
279 		return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
280 						left_margin);
281 		break;
282 	case CHAIN_FLAT:
283 		return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
284 		break;
285 	case CHAIN_NONE:
286 		break;
287 	default:
288 		pr_err("Bad callchain mode\n");
289 	}
290 
291 	return 0;
292 }
293 
294 static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
295 					    struct hists *hists,
296 					    FILE *fp)
297 {
298 	int left_margin = 0;
299 	u64 total_period = hists->stats.total_period;
300 
301 	if (sort__first_dimension == SORT_COMM) {
302 		struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
303 							 typeof(*se), list);
304 		left_margin = hists__col_len(hists, se->se_width_idx);
305 		left_margin -= thread__comm_len(he->thread);
306 	}
307 
308 	return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
309 }
310 
311 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
312 {
313 	hpp->buf  += inc;
314 	hpp->size -= inc;
315 }
316 
317 static int hist_entry__period_snprintf(struct perf_hpp *hpp,
318 				       struct hist_entry *he,
319 				       bool color)
320 {
321 	const char *sep = symbol_conf.field_sep;
322 	struct perf_hpp_fmt *fmt;
323 	char *start = hpp->buf;
324 	int ret;
325 	bool first = true;
326 
327 	if (symbol_conf.exclude_other && !he->parent)
328 		return 0;
329 
330 	perf_hpp__for_each_format(fmt) {
331 		/*
332 		 * If there's no field_sep, we still need
333 		 * to display initial '  '.
334 		 */
335 		if (!sep || !first) {
336 			ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: "  ");
337 			advance_hpp(hpp, ret);
338 		} else
339 			first = false;
340 
341 		if (color && fmt->color)
342 			ret = fmt->color(fmt, hpp, he);
343 		else
344 			ret = fmt->entry(fmt, hpp, he);
345 
346 		advance_hpp(hpp, ret);
347 	}
348 
349 	return hpp->buf - start;
350 }
351 
352 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
353 			       struct hists *hists,
354 			       char *bf, size_t bfsz, FILE *fp)
355 {
356 	int ret;
357 	struct perf_hpp hpp = {
358 		.buf		= bf,
359 		.size		= size,
360 	};
361 	bool color = !symbol_conf.field_sep;
362 
363 	if (size == 0 || size > bfsz)
364 		size = hpp.size = bfsz;
365 
366 	ret = hist_entry__period_snprintf(&hpp, he, color);
367 	hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
368 
369 	ret = fprintf(fp, "%s\n", bf);
370 
371 	if (symbol_conf.use_callchain)
372 		ret += hist_entry__callchain_fprintf(he, hists, fp);
373 
374 	return ret;
375 }
376 
377 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
378 		      int max_cols, float min_pcnt, FILE *fp)
379 {
380 	struct perf_hpp_fmt *fmt;
381 	struct sort_entry *se;
382 	struct rb_node *nd;
383 	size_t ret = 0;
384 	unsigned int width;
385 	const char *sep = symbol_conf.field_sep;
386 	const char *col_width = symbol_conf.col_width_list_str;
387 	int nr_rows = 0;
388 	char bf[96];
389 	struct perf_hpp dummy_hpp = {
390 		.buf	= bf,
391 		.size	= sizeof(bf),
392 		.ptr	= hists_to_evsel(hists),
393 	};
394 	bool first = true;
395 	size_t linesz;
396 	char *line = NULL;
397 
398 	init_rem_hits();
399 
400 	if (!show_header)
401 		goto print_entries;
402 
403 	fprintf(fp, "# ");
404 
405 	perf_hpp__for_each_format(fmt) {
406 		if (!first)
407 			fprintf(fp, "%s", sep ?: "  ");
408 		else
409 			first = false;
410 
411 		fmt->header(fmt, &dummy_hpp);
412 		fprintf(fp, "%s", bf);
413 	}
414 
415 	list_for_each_entry(se, &hist_entry__sort_list, list) {
416 		if (se->elide)
417 			continue;
418 		if (sep) {
419 			fprintf(fp, "%c%s", *sep, se->se_header);
420 			continue;
421 		}
422 		width = strlen(se->se_header);
423 		if (symbol_conf.col_width_list_str) {
424 			if (col_width) {
425 				hists__set_col_len(hists, se->se_width_idx,
426 						   atoi(col_width));
427 				col_width = strchr(col_width, ',');
428 				if (col_width)
429 					++col_width;
430 			}
431 		}
432 		if (!hists__new_col_len(hists, se->se_width_idx, width))
433 			width = hists__col_len(hists, se->se_width_idx);
434 		fprintf(fp, "  %*s", width, se->se_header);
435 	}
436 
437 	fprintf(fp, "\n");
438 	if (max_rows && ++nr_rows >= max_rows)
439 		goto out;
440 
441 	if (sep)
442 		goto print_entries;
443 
444 	first = true;
445 
446 	fprintf(fp, "# ");
447 
448 	perf_hpp__for_each_format(fmt) {
449 		unsigned int i;
450 
451 		if (!first)
452 			fprintf(fp, "%s", sep ?: "  ");
453 		else
454 			first = false;
455 
456 		width = fmt->width(fmt, &dummy_hpp);
457 		for (i = 0; i < width; i++)
458 			fprintf(fp, ".");
459 	}
460 
461 	list_for_each_entry(se, &hist_entry__sort_list, list) {
462 		unsigned int i;
463 
464 		if (se->elide)
465 			continue;
466 
467 		fprintf(fp, "  ");
468 		width = hists__col_len(hists, se->se_width_idx);
469 		if (width == 0)
470 			width = strlen(se->se_header);
471 		for (i = 0; i < width; i++)
472 			fprintf(fp, ".");
473 	}
474 
475 	fprintf(fp, "\n");
476 	if (max_rows && ++nr_rows >= max_rows)
477 		goto out;
478 
479 	fprintf(fp, "#\n");
480 	if (max_rows && ++nr_rows >= max_rows)
481 		goto out;
482 
483 print_entries:
484 	linesz = hists__sort_list_width(hists) + 3 + 1;
485 	line = malloc(linesz);
486 	if (line == NULL) {
487 		ret = -1;
488 		goto out;
489 	}
490 
491 	for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
492 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
493 		float percent = h->stat.period * 100.0 /
494 					hists->stats.total_period;
495 
496 		if (h->filtered)
497 			continue;
498 
499 		if (percent < min_pcnt)
500 			continue;
501 
502 		ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
503 
504 		if (max_rows && ++nr_rows >= max_rows)
505 			break;
506 
507 		if (h->ms.map == NULL && verbose > 1) {
508 			__map_groups__fprintf_maps(&h->thread->mg,
509 						   MAP__FUNCTION, verbose, fp);
510 			fprintf(fp, "%.10s end\n", graph_dotted_line);
511 		}
512 	}
513 
514 	free(line);
515 out:
516 	free(rem_sq_bracket);
517 
518 	return ret;
519 }
520 
521 size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
522 {
523 	int i;
524 	size_t ret = 0;
525 
526 	for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
527 		const char *name;
528 
529 		if (stats->nr_events[i] == 0)
530 			continue;
531 
532 		name = perf_event__name(i);
533 		if (!strcmp(name, "UNKNOWN"))
534 			continue;
535 
536 		ret += fprintf(fp, "%16s events: %10d\n", name,
537 			       stats->nr_events[i]);
538 	}
539 
540 	return ret;
541 }
542