• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This is rewrite of original c2c tool introduced in here:
4  *   http://lwn.net/Articles/588866/
5  *
6  * The original tool was changed to fit in current perf state.
7  *
8  * Original authors:
9  *   Don Zickus <dzickus@redhat.com>
10  *   Dick Fowles <fowles@inreach.com>
11  *   Joe Mario <jmario@redhat.com>
12  */
13 #include <errno.h>
14 #include <inttypes.h>
15 #include <linux/compiler.h>
16 #include <linux/kernel.h>
17 #include <linux/stringify.h>
18 #include <asm/bug.h>
19 #include <sys/param.h>
20 #include "util.h"
21 #include "debug.h"
22 #include "builtin.h"
23 #include <subcmd/parse-options.h>
24 #include "mem-events.h"
25 #include "session.h"
26 #include "hist.h"
27 #include "sort.h"
28 #include "tool.h"
29 #include "data.h"
30 #include "sort.h"
31 #include "event.h"
32 #include "evlist.h"
33 #include "evsel.h"
34 #include <asm/bug.h>
35 #include "ui/browsers/hists.h"
36 #include "evlist.h"
37 #include "thread.h"
38 
39 struct c2c_hists {
40 	struct hists		hists;
41 	struct perf_hpp_list	list;
42 	struct c2c_stats	stats;
43 };
44 
45 struct compute_stats {
46 	struct stats		 lcl_hitm;
47 	struct stats		 rmt_hitm;
48 	struct stats		 load;
49 };
50 
51 struct c2c_hist_entry {
52 	struct c2c_hists	*hists;
53 	struct c2c_stats	 stats;
54 	unsigned long		*cpuset;
55 	struct c2c_stats	*node_stats;
56 	unsigned int		 cacheline_idx;
57 
58 	struct compute_stats	 cstats;
59 
60 	/*
61 	 * must be at the end,
62 	 * because of its callchain dynamic entry
63 	 */
64 	struct hist_entry	he;
65 };
66 
67 static char const *coalesce_default = "pid,iaddr";
68 
69 struct perf_c2c {
70 	struct perf_tool	tool;
71 	struct c2c_hists	hists;
72 
73 	unsigned long		**nodes;
74 	int			 nodes_cnt;
75 	int			 cpus_cnt;
76 	int			*cpu2node;
77 	int			 node_info;
78 
79 	bool			 show_src;
80 	bool			 show_all;
81 	bool			 use_stdio;
82 	bool			 stats_only;
83 	bool			 symbol_full;
84 
85 	/* HITM shared clines stats */
86 	struct c2c_stats	hitm_stats;
87 	int			shared_clines;
88 
89 	int			 display;
90 
91 	const char		*coalesce;
92 	char			*cl_sort;
93 	char			*cl_resort;
94 	char			*cl_output;
95 };
96 
97 enum {
98 	DISPLAY_LCL,
99 	DISPLAY_RMT,
100 	DISPLAY_TOT,
101 	DISPLAY_MAX,
102 };
103 
104 static const char *display_str[DISPLAY_MAX] = {
105 	[DISPLAY_LCL] = "Local",
106 	[DISPLAY_RMT] = "Remote",
107 	[DISPLAY_TOT] = "Total",
108 };
109 
110 static const struct option c2c_options[] = {
111 	OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
112 	OPT_END()
113 };
114 
115 static struct perf_c2c c2c;
116 
c2c_he_zalloc(size_t size)117 static void *c2c_he_zalloc(size_t size)
118 {
119 	struct c2c_hist_entry *c2c_he;
120 
121 	c2c_he = zalloc(size + sizeof(*c2c_he));
122 	if (!c2c_he)
123 		return NULL;
124 
125 	c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
126 	if (!c2c_he->cpuset)
127 		return NULL;
128 
129 	c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
130 	if (!c2c_he->node_stats)
131 		return NULL;
132 
133 	init_stats(&c2c_he->cstats.lcl_hitm);
134 	init_stats(&c2c_he->cstats.rmt_hitm);
135 	init_stats(&c2c_he->cstats.load);
136 
137 	return &c2c_he->he;
138 }
139 
c2c_he_free(void * he)140 static void c2c_he_free(void *he)
141 {
142 	struct c2c_hist_entry *c2c_he;
143 
144 	c2c_he = container_of(he, struct c2c_hist_entry, he);
145 	if (c2c_he->hists) {
146 		hists__delete_entries(&c2c_he->hists->hists);
147 		free(c2c_he->hists);
148 	}
149 
150 	free(c2c_he->cpuset);
151 	free(c2c_he->node_stats);
152 	free(c2c_he);
153 }
154 
155 static struct hist_entry_ops c2c_entry_ops = {
156 	.new	= c2c_he_zalloc,
157 	.free	= c2c_he_free,
158 };
159 
160 static int c2c_hists__init(struct c2c_hists *hists,
161 			   const char *sort,
162 			   int nr_header_lines);
163 
164 static struct c2c_hists*
he__get_c2c_hists(struct hist_entry * he,const char * sort,int nr_header_lines)165 he__get_c2c_hists(struct hist_entry *he,
166 		  const char *sort,
167 		  int nr_header_lines)
168 {
169 	struct c2c_hist_entry *c2c_he;
170 	struct c2c_hists *hists;
171 	int ret;
172 
173 	c2c_he = container_of(he, struct c2c_hist_entry, he);
174 	if (c2c_he->hists)
175 		return c2c_he->hists;
176 
177 	hists = c2c_he->hists = zalloc(sizeof(*hists));
178 	if (!hists)
179 		return NULL;
180 
181 	ret = c2c_hists__init(hists, sort, nr_header_lines);
182 	if (ret) {
183 		free(hists);
184 		return NULL;
185 	}
186 
187 	return hists;
188 }
189 
c2c_he__set_cpu(struct c2c_hist_entry * c2c_he,struct perf_sample * sample)190 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
191 			    struct perf_sample *sample)
192 {
193 	if (WARN_ONCE(sample->cpu == (unsigned int) -1,
194 		      "WARNING: no sample cpu value"))
195 		return;
196 
197 	set_bit(sample->cpu, c2c_he->cpuset);
198 }
199 
compute_stats(struct c2c_hist_entry * c2c_he,struct c2c_stats * stats,u64 weight)200 static void compute_stats(struct c2c_hist_entry *c2c_he,
201 			  struct c2c_stats *stats,
202 			  u64 weight)
203 {
204 	struct compute_stats *cstats = &c2c_he->cstats;
205 
206 	if (stats->rmt_hitm)
207 		update_stats(&cstats->rmt_hitm, weight);
208 	else if (stats->lcl_hitm)
209 		update_stats(&cstats->lcl_hitm, weight);
210 	else if (stats->load)
211 		update_stats(&cstats->load, weight);
212 }
213 
process_sample_event(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct perf_evsel * evsel,struct machine * machine)214 static int process_sample_event(struct perf_tool *tool __maybe_unused,
215 				union perf_event *event,
216 				struct perf_sample *sample,
217 				struct perf_evsel *evsel,
218 				struct machine *machine)
219 {
220 	struct c2c_hists *c2c_hists = &c2c.hists;
221 	struct c2c_hist_entry *c2c_he;
222 	struct c2c_stats stats = { .nr_entries = 0, };
223 	struct hist_entry *he;
224 	struct addr_location al;
225 	struct mem_info *mi, *mi_dup;
226 	int ret;
227 
228 	if (machine__resolve(machine, &al, sample) < 0) {
229 		pr_debug("problem processing %d event, skipping it.\n",
230 			 event->header.type);
231 		return -1;
232 	}
233 
234 	ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
235 					evsel, &al, sysctl_perf_event_max_stack);
236 	if (ret)
237 		goto out;
238 
239 	mi = sample__resolve_mem(sample, &al);
240 	if (mi == NULL)
241 		return -ENOMEM;
242 
243 	mi_dup = memdup(mi, sizeof(*mi));
244 	if (!mi_dup)
245 		goto free_mi;
246 
247 	c2c_decode_stats(&stats, mi);
248 
249 	he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
250 				  &al, NULL, NULL, mi,
251 				  sample, true);
252 	if (he == NULL)
253 		goto free_mi_dup;
254 
255 	c2c_he = container_of(he, struct c2c_hist_entry, he);
256 	c2c_add_stats(&c2c_he->stats, &stats);
257 	c2c_add_stats(&c2c_hists->stats, &stats);
258 
259 	c2c_he__set_cpu(c2c_he, sample);
260 
261 	hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
262 	ret = hist_entry__append_callchain(he, sample);
263 
264 	if (!ret) {
265 		/*
266 		 * There's already been warning about missing
267 		 * sample's cpu value. Let's account all to
268 		 * node 0 in this case, without any further
269 		 * warning.
270 		 *
271 		 * Doing node stats only for single callchain data.
272 		 */
273 		int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
274 		int node = c2c.cpu2node[cpu];
275 
276 		mi = mi_dup;
277 
278 		mi_dup = memdup(mi, sizeof(*mi));
279 		if (!mi_dup)
280 			goto free_mi;
281 
282 		c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
283 		if (!c2c_hists)
284 			goto free_mi_dup;
285 
286 		he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
287 					  &al, NULL, NULL, mi,
288 					  sample, true);
289 		if (he == NULL)
290 			goto free_mi_dup;
291 
292 		c2c_he = container_of(he, struct c2c_hist_entry, he);
293 		c2c_add_stats(&c2c_he->stats, &stats);
294 		c2c_add_stats(&c2c_hists->stats, &stats);
295 		c2c_add_stats(&c2c_he->node_stats[node], &stats);
296 
297 		compute_stats(c2c_he, &stats, sample->weight);
298 
299 		c2c_he__set_cpu(c2c_he, sample);
300 
301 		hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
302 		ret = hist_entry__append_callchain(he, sample);
303 	}
304 
305 out:
306 	addr_location__put(&al);
307 	return ret;
308 
309 free_mi_dup:
310 	free(mi_dup);
311 free_mi:
312 	free(mi);
313 	ret = -ENOMEM;
314 	goto out;
315 }
316 
317 static struct perf_c2c c2c = {
318 	.tool = {
319 		.sample		= process_sample_event,
320 		.mmap		= perf_event__process_mmap,
321 		.mmap2		= perf_event__process_mmap2,
322 		.comm		= perf_event__process_comm,
323 		.exit		= perf_event__process_exit,
324 		.fork		= perf_event__process_fork,
325 		.lost		= perf_event__process_lost,
326 		.ordered_events	= true,
327 		.ordering_requires_timestamps = true,
328 	},
329 };
330 
331 static const char * const c2c_usage[] = {
332 	"perf c2c {record|report}",
333 	NULL
334 };
335 
336 static const char * const __usage_report[] = {
337 	"perf c2c report",
338 	NULL
339 };
340 
341 static const char * const *report_c2c_usage = __usage_report;
342 
343 #define C2C_HEADER_MAX 2
344 
345 struct c2c_header {
346 	struct {
347 		const char *text;
348 		int	    span;
349 	} line[C2C_HEADER_MAX];
350 };
351 
352 struct c2c_dimension {
353 	struct c2c_header	 header;
354 	const char		*name;
355 	int			 width;
356 	struct sort_entry	*se;
357 
358 	int64_t (*cmp)(struct perf_hpp_fmt *fmt,
359 		       struct hist_entry *, struct hist_entry *);
360 	int   (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
361 		       struct hist_entry *he);
362 	int   (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
363 		       struct hist_entry *he);
364 };
365 
366 struct c2c_fmt {
367 	struct perf_hpp_fmt	 fmt;
368 	struct c2c_dimension	*dim;
369 };
370 
371 #define SYMBOL_WIDTH 30
372 
373 static struct c2c_dimension dim_symbol;
374 static struct c2c_dimension dim_srcline;
375 
symbol_width(struct hists * hists,struct sort_entry * se)376 static int symbol_width(struct hists *hists, struct sort_entry *se)
377 {
378 	int width = hists__col_len(hists, se->se_width_idx);
379 
380 	if (!c2c.symbol_full)
381 		width = MIN(width, SYMBOL_WIDTH);
382 
383 	return width;
384 }
385 
c2c_width(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp __maybe_unused,struct hists * hists)386 static int c2c_width(struct perf_hpp_fmt *fmt,
387 		     struct perf_hpp *hpp __maybe_unused,
388 		     struct hists *hists)
389 {
390 	struct c2c_fmt *c2c_fmt;
391 	struct c2c_dimension *dim;
392 
393 	c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
394 	dim = c2c_fmt->dim;
395 
396 	if (dim == &dim_symbol || dim == &dim_srcline)
397 		return symbol_width(hists, dim->se);
398 
399 	return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
400 			 c2c_fmt->dim->width;
401 }
402 
c2c_header(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hists * hists,int line,int * span)403 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
404 		      struct hists *hists, int line, int *span)
405 {
406 	struct perf_hpp_list *hpp_list = hists->hpp_list;
407 	struct c2c_fmt *c2c_fmt;
408 	struct c2c_dimension *dim;
409 	const char *text = NULL;
410 	int width = c2c_width(fmt, hpp, hists);
411 
412 	c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
413 	dim = c2c_fmt->dim;
414 
415 	if (dim->se) {
416 		text = dim->header.line[line].text;
417 		/* Use the last line from sort_entry if not defined. */
418 		if (!text && (line == hpp_list->nr_header_lines - 1))
419 			text = dim->se->se_header;
420 	} else {
421 		text = dim->header.line[line].text;
422 
423 		if (*span) {
424 			(*span)--;
425 			return 0;
426 		} else {
427 			*span = dim->header.line[line].span;
428 		}
429 	}
430 
431 	if (text == NULL)
432 		text = "";
433 
434 	return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
435 }
436 
437 #define HEX_STR(__s, __v)				\
438 ({							\
439 	scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v);	\
440 	__s;						\
441 })
442 
443 static int64_t
dcacheline_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)444 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
445 	       struct hist_entry *left, struct hist_entry *right)
446 {
447 	return sort__dcacheline_cmp(left, right);
448 }
449 
dcacheline_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)450 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
451 			    struct hist_entry *he)
452 {
453 	uint64_t addr = 0;
454 	int width = c2c_width(fmt, hpp, he->hists);
455 	char buf[20];
456 
457 	if (he->mem_info)
458 		addr = cl_address(he->mem_info->daddr.addr);
459 
460 	return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
461 }
462 
offset_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)463 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
464 			struct hist_entry *he)
465 {
466 	uint64_t addr = 0;
467 	int width = c2c_width(fmt, hpp, he->hists);
468 	char buf[20];
469 
470 	if (he->mem_info)
471 		addr = cl_offset(he->mem_info->daddr.al_addr);
472 
473 	return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
474 }
475 
476 static int64_t
offset_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)477 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
478 	   struct hist_entry *left, struct hist_entry *right)
479 {
480 	uint64_t l = 0, r = 0;
481 
482 	if (left->mem_info)
483 		l = cl_offset(left->mem_info->daddr.addr);
484 	if (right->mem_info)
485 		r = cl_offset(right->mem_info->daddr.addr);
486 
487 	return (int64_t)(r - l);
488 }
489 
490 static int
iaddr_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)491 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
492 	    struct hist_entry *he)
493 {
494 	uint64_t addr = 0;
495 	int width = c2c_width(fmt, hpp, he->hists);
496 	char buf[20];
497 
498 	if (he->mem_info)
499 		addr = he->mem_info->iaddr.addr;
500 
501 	return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
502 }
503 
504 static int64_t
iaddr_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)505 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
506 	  struct hist_entry *left, struct hist_entry *right)
507 {
508 	return sort__iaddr_cmp(left, right);
509 }
510 
511 static int
tot_hitm_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)512 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
513 	       struct hist_entry *he)
514 {
515 	struct c2c_hist_entry *c2c_he;
516 	int width = c2c_width(fmt, hpp, he->hists);
517 	unsigned int tot_hitm;
518 
519 	c2c_he = container_of(he, struct c2c_hist_entry, he);
520 	tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
521 
522 	return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
523 }
524 
525 static int64_t
tot_hitm_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)526 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
527 	     struct hist_entry *left, struct hist_entry *right)
528 {
529 	struct c2c_hist_entry *c2c_left;
530 	struct c2c_hist_entry *c2c_right;
531 	uint64_t tot_hitm_left;
532 	uint64_t tot_hitm_right;
533 
534 	c2c_left  = container_of(left, struct c2c_hist_entry, he);
535 	c2c_right = container_of(right, struct c2c_hist_entry, he);
536 
537 	tot_hitm_left  = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
538 	tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
539 
540 	return tot_hitm_left - tot_hitm_right;
541 }
542 
543 #define STAT_FN_ENTRY(__f)					\
544 static int							\
545 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,	\
546 	      struct hist_entry *he)				\
547 {								\
548 	struct c2c_hist_entry *c2c_he;				\
549 	int width = c2c_width(fmt, hpp, he->hists);		\
550 								\
551 	c2c_he = container_of(he, struct c2c_hist_entry, he);	\
552 	return scnprintf(hpp->buf, hpp->size, "%*u", width,	\
553 			 c2c_he->stats.__f);			\
554 }
555 
556 #define STAT_FN_CMP(__f)						\
557 static int64_t								\
558 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused,			\
559 	    struct hist_entry *left, struct hist_entry *right)		\
560 {									\
561 	struct c2c_hist_entry *c2c_left, *c2c_right;			\
562 									\
563 	c2c_left  = container_of(left, struct c2c_hist_entry, he);	\
564 	c2c_right = container_of(right, struct c2c_hist_entry, he);	\
565 	return (uint64_t) c2c_left->stats.__f -				\
566 	       (uint64_t) c2c_right->stats.__f;				\
567 }
568 
569 #define STAT_FN(__f)		\
570 	STAT_FN_ENTRY(__f)	\
571 	STAT_FN_CMP(__f)
572 
573 STAT_FN(rmt_hitm)
STAT_FN(lcl_hitm)574 STAT_FN(lcl_hitm)
575 STAT_FN(store)
576 STAT_FN(st_l1hit)
577 STAT_FN(st_l1miss)
578 STAT_FN(ld_fbhit)
579 STAT_FN(ld_l1hit)
580 STAT_FN(ld_l2hit)
581 STAT_FN(ld_llchit)
582 STAT_FN(rmt_hit)
583 
584 static uint64_t llc_miss(struct c2c_stats *stats)
585 {
586 	uint64_t llcmiss;
587 
588 	llcmiss = stats->lcl_dram +
589 		  stats->rmt_dram +
590 		  stats->rmt_hitm +
591 		  stats->rmt_hit;
592 
593 	return llcmiss;
594 }
595 
596 static int
ld_llcmiss_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)597 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
598 		 struct hist_entry *he)
599 {
600 	struct c2c_hist_entry *c2c_he;
601 	int width = c2c_width(fmt, hpp, he->hists);
602 
603 	c2c_he = container_of(he, struct c2c_hist_entry, he);
604 
605 	return scnprintf(hpp->buf, hpp->size, "%*lu", width,
606 			 llc_miss(&c2c_he->stats));
607 }
608 
609 static int64_t
ld_llcmiss_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)610 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
611 	       struct hist_entry *left, struct hist_entry *right)
612 {
613 	struct c2c_hist_entry *c2c_left;
614 	struct c2c_hist_entry *c2c_right;
615 
616 	c2c_left  = container_of(left, struct c2c_hist_entry, he);
617 	c2c_right = container_of(right, struct c2c_hist_entry, he);
618 
619 	return (uint64_t) llc_miss(&c2c_left->stats) -
620 	       (uint64_t) llc_miss(&c2c_right->stats);
621 }
622 
total_records(struct c2c_stats * stats)623 static uint64_t total_records(struct c2c_stats *stats)
624 {
625 	uint64_t lclmiss, ldcnt, total;
626 
627 	lclmiss  = stats->lcl_dram +
628 		   stats->rmt_dram +
629 		   stats->rmt_hitm +
630 		   stats->rmt_hit;
631 
632 	ldcnt    = lclmiss +
633 		   stats->ld_fbhit +
634 		   stats->ld_l1hit +
635 		   stats->ld_l2hit +
636 		   stats->ld_llchit +
637 		   stats->lcl_hitm;
638 
639 	total    = ldcnt +
640 		   stats->st_l1hit +
641 		   stats->st_l1miss;
642 
643 	return total;
644 }
645 
646 static int
tot_recs_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)647 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
648 		struct hist_entry *he)
649 {
650 	struct c2c_hist_entry *c2c_he;
651 	int width = c2c_width(fmt, hpp, he->hists);
652 	uint64_t tot_recs;
653 
654 	c2c_he = container_of(he, struct c2c_hist_entry, he);
655 	tot_recs = total_records(&c2c_he->stats);
656 
657 	return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
658 }
659 
660 static int64_t
tot_recs_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)661 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
662 	     struct hist_entry *left, struct hist_entry *right)
663 {
664 	struct c2c_hist_entry *c2c_left;
665 	struct c2c_hist_entry *c2c_right;
666 	uint64_t tot_recs_left;
667 	uint64_t tot_recs_right;
668 
669 	c2c_left  = container_of(left, struct c2c_hist_entry, he);
670 	c2c_right = container_of(right, struct c2c_hist_entry, he);
671 
672 	tot_recs_left  = total_records(&c2c_left->stats);
673 	tot_recs_right = total_records(&c2c_right->stats);
674 
675 	return tot_recs_left - tot_recs_right;
676 }
677 
total_loads(struct c2c_stats * stats)678 static uint64_t total_loads(struct c2c_stats *stats)
679 {
680 	uint64_t lclmiss, ldcnt;
681 
682 	lclmiss  = stats->lcl_dram +
683 		   stats->rmt_dram +
684 		   stats->rmt_hitm +
685 		   stats->rmt_hit;
686 
687 	ldcnt    = lclmiss +
688 		   stats->ld_fbhit +
689 		   stats->ld_l1hit +
690 		   stats->ld_l2hit +
691 		   stats->ld_llchit +
692 		   stats->lcl_hitm;
693 
694 	return ldcnt;
695 }
696 
697 static int
tot_loads_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)698 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
699 		struct hist_entry *he)
700 {
701 	struct c2c_hist_entry *c2c_he;
702 	int width = c2c_width(fmt, hpp, he->hists);
703 	uint64_t tot_recs;
704 
705 	c2c_he = container_of(he, struct c2c_hist_entry, he);
706 	tot_recs = total_loads(&c2c_he->stats);
707 
708 	return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
709 }
710 
711 static int64_t
tot_loads_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)712 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
713 	      struct hist_entry *left, struct hist_entry *right)
714 {
715 	struct c2c_hist_entry *c2c_left;
716 	struct c2c_hist_entry *c2c_right;
717 	uint64_t tot_recs_left;
718 	uint64_t tot_recs_right;
719 
720 	c2c_left  = container_of(left, struct c2c_hist_entry, he);
721 	c2c_right = container_of(right, struct c2c_hist_entry, he);
722 
723 	tot_recs_left  = total_loads(&c2c_left->stats);
724 	tot_recs_right = total_loads(&c2c_right->stats);
725 
726 	return tot_recs_left - tot_recs_right;
727 }
728 
729 typedef double (get_percent_cb)(struct c2c_hist_entry *);
730 
731 static int
percent_color(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he,get_percent_cb get_percent)732 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
733 	      struct hist_entry *he, get_percent_cb get_percent)
734 {
735 	struct c2c_hist_entry *c2c_he;
736 	int width = c2c_width(fmt, hpp, he->hists);
737 	double per;
738 
739 	c2c_he = container_of(he, struct c2c_hist_entry, he);
740 	per = get_percent(c2c_he);
741 
742 #ifdef HAVE_SLANG_SUPPORT
743 	if (use_browser)
744 		return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
745 #endif
746 	return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
747 }
748 
percent_hitm(struct c2c_hist_entry * c2c_he)749 static double percent_hitm(struct c2c_hist_entry *c2c_he)
750 {
751 	struct c2c_hists *hists;
752 	struct c2c_stats *stats;
753 	struct c2c_stats *total;
754 	int tot = 0, st = 0;
755 	double p;
756 
757 	hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
758 	stats = &c2c_he->stats;
759 	total = &hists->stats;
760 
761 	switch (c2c.display) {
762 	case DISPLAY_RMT:
763 		st  = stats->rmt_hitm;
764 		tot = total->rmt_hitm;
765 		break;
766 	case DISPLAY_LCL:
767 		st  = stats->lcl_hitm;
768 		tot = total->lcl_hitm;
769 		break;
770 	case DISPLAY_TOT:
771 		st  = stats->tot_hitm;
772 		tot = total->tot_hitm;
773 	default:
774 		break;
775 	}
776 
777 	p = tot ? (double) st / tot : 0;
778 
779 	return 100 * p;
780 }
781 
782 #define PERC_STR(__s, __v)				\
783 ({							\
784 	scnprintf(__s, sizeof(__s), "%.2F%%", __v);	\
785 	__s;						\
786 })
787 
788 static int
percent_hitm_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)789 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
790 		   struct hist_entry *he)
791 {
792 	struct c2c_hist_entry *c2c_he;
793 	int width = c2c_width(fmt, hpp, he->hists);
794 	char buf[10];
795 	double per;
796 
797 	c2c_he = container_of(he, struct c2c_hist_entry, he);
798 	per = percent_hitm(c2c_he);
799 	return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
800 }
801 
802 static int
percent_hitm_color(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)803 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
804 		   struct hist_entry *he)
805 {
806 	return percent_color(fmt, hpp, he, percent_hitm);
807 }
808 
809 static int64_t
percent_hitm_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)810 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
811 		 struct hist_entry *left, struct hist_entry *right)
812 {
813 	struct c2c_hist_entry *c2c_left;
814 	struct c2c_hist_entry *c2c_right;
815 	double per_left;
816 	double per_right;
817 
818 	c2c_left  = container_of(left, struct c2c_hist_entry, he);
819 	c2c_right = container_of(right, struct c2c_hist_entry, he);
820 
821 	per_left  = percent_hitm(c2c_left);
822 	per_right = percent_hitm(c2c_right);
823 
824 	return per_left - per_right;
825 }
826 
he_stats(struct hist_entry * he)827 static struct c2c_stats *he_stats(struct hist_entry *he)
828 {
829 	struct c2c_hist_entry *c2c_he;
830 
831 	c2c_he = container_of(he, struct c2c_hist_entry, he);
832 	return &c2c_he->stats;
833 }
834 
total_stats(struct hist_entry * he)835 static struct c2c_stats *total_stats(struct hist_entry *he)
836 {
837 	struct c2c_hists *hists;
838 
839 	hists = container_of(he->hists, struct c2c_hists, hists);
840 	return &hists->stats;
841 }
842 
percent(int st,int tot)843 static double percent(int st, int tot)
844 {
845 	return tot ? 100. * (double) st / (double) tot : 0;
846 }
847 
848 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
849 
850 #define PERCENT_FN(__f)								\
851 static double percent_ ## __f(struct c2c_hist_entry *c2c_he)			\
852 {										\
853 	struct c2c_hists *hists;						\
854 										\
855 	hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);	\
856 	return percent(c2c_he->stats.__f, hists->stats.__f);			\
857 }
858 
859 PERCENT_FN(rmt_hitm)
PERCENT_FN(lcl_hitm)860 PERCENT_FN(lcl_hitm)
861 PERCENT_FN(st_l1hit)
862 PERCENT_FN(st_l1miss)
863 
864 static int
865 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
866 		       struct hist_entry *he)
867 {
868 	int width = c2c_width(fmt, hpp, he->hists);
869 	double per = PERCENT(he, rmt_hitm);
870 	char buf[10];
871 
872 	return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
873 }
874 
875 static int
percent_rmt_hitm_color(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)876 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
877 		       struct hist_entry *he)
878 {
879 	return percent_color(fmt, hpp, he, percent_rmt_hitm);
880 }
881 
882 static int64_t
percent_rmt_hitm_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)883 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
884 		     struct hist_entry *left, struct hist_entry *right)
885 {
886 	double per_left;
887 	double per_right;
888 
889 	per_left  = PERCENT(left, lcl_hitm);
890 	per_right = PERCENT(right, lcl_hitm);
891 
892 	return per_left - per_right;
893 }
894 
895 static int
percent_lcl_hitm_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)896 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
897 		       struct hist_entry *he)
898 {
899 	int width = c2c_width(fmt, hpp, he->hists);
900 	double per = PERCENT(he, lcl_hitm);
901 	char buf[10];
902 
903 	return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
904 }
905 
906 static int
percent_lcl_hitm_color(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)907 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
908 		       struct hist_entry *he)
909 {
910 	return percent_color(fmt, hpp, he, percent_lcl_hitm);
911 }
912 
913 static int64_t
percent_lcl_hitm_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)914 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
915 		     struct hist_entry *left, struct hist_entry *right)
916 {
917 	double per_left;
918 	double per_right;
919 
920 	per_left  = PERCENT(left, lcl_hitm);
921 	per_right = PERCENT(right, lcl_hitm);
922 
923 	return per_left - per_right;
924 }
925 
926 static int
percent_stores_l1hit_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)927 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
928 			   struct hist_entry *he)
929 {
930 	int width = c2c_width(fmt, hpp, he->hists);
931 	double per = PERCENT(he, st_l1hit);
932 	char buf[10];
933 
934 	return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
935 }
936 
937 static int
percent_stores_l1hit_color(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)938 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
939 			   struct hist_entry *he)
940 {
941 	return percent_color(fmt, hpp, he, percent_st_l1hit);
942 }
943 
944 static int64_t
percent_stores_l1hit_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)945 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
946 			struct hist_entry *left, struct hist_entry *right)
947 {
948 	double per_left;
949 	double per_right;
950 
951 	per_left  = PERCENT(left, st_l1hit);
952 	per_right = PERCENT(right, st_l1hit);
953 
954 	return per_left - per_right;
955 }
956 
957 static int
percent_stores_l1miss_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)958 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
959 			   struct hist_entry *he)
960 {
961 	int width = c2c_width(fmt, hpp, he->hists);
962 	double per = PERCENT(he, st_l1miss);
963 	char buf[10];
964 
965 	return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
966 }
967 
968 static int
percent_stores_l1miss_color(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)969 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
970 			    struct hist_entry *he)
971 {
972 	return percent_color(fmt, hpp, he, percent_st_l1miss);
973 }
974 
975 static int64_t
percent_stores_l1miss_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)976 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
977 			  struct hist_entry *left, struct hist_entry *right)
978 {
979 	double per_left;
980 	double per_right;
981 
982 	per_left  = PERCENT(left, st_l1miss);
983 	per_right = PERCENT(right, st_l1miss);
984 
985 	return per_left - per_right;
986 }
987 
988 STAT_FN(lcl_dram)
STAT_FN(rmt_dram)989 STAT_FN(rmt_dram)
990 
991 static int
992 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
993 	  struct hist_entry *he)
994 {
995 	int width = c2c_width(fmt, hpp, he->hists);
996 
997 	return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
998 }
999 
1000 static int64_t
pid_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left,struct hist_entry * right)1001 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1002 	struct hist_entry *left, struct hist_entry *right)
1003 {
1004 	return left->thread->pid_ - right->thread->pid_;
1005 }
1006 
1007 static int64_t
empty_cmp(struct perf_hpp_fmt * fmt __maybe_unused,struct hist_entry * left __maybe_unused,struct hist_entry * right __maybe_unused)1008 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1009 	  struct hist_entry *left __maybe_unused,
1010 	  struct hist_entry *right __maybe_unused)
1011 {
1012 	return 0;
1013 }
1014 
1015 static int
node_entry(struct perf_hpp_fmt * fmt __maybe_unused,struct perf_hpp * hpp,struct hist_entry * he)1016 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1017 	   struct hist_entry *he)
1018 {
1019 	struct c2c_hist_entry *c2c_he;
1020 	bool first = true;
1021 	int node;
1022 	int ret = 0;
1023 
1024 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1025 
1026 	for (node = 0; node < c2c.nodes_cnt; node++) {
1027 		DECLARE_BITMAP(set, c2c.cpus_cnt);
1028 
1029 		bitmap_zero(set, c2c.cpus_cnt);
1030 		bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1031 
1032 		if (!bitmap_weight(set, c2c.cpus_cnt)) {
1033 			if (c2c.node_info == 1) {
1034 				ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1035 				advance_hpp(hpp, ret);
1036 			}
1037 			continue;
1038 		}
1039 
1040 		if (!first) {
1041 			ret = scnprintf(hpp->buf, hpp->size, " ");
1042 			advance_hpp(hpp, ret);
1043 		}
1044 
1045 		switch (c2c.node_info) {
1046 		case 0:
1047 			ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1048 			advance_hpp(hpp, ret);
1049 			break;
1050 		case 1:
1051 		{
1052 			int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
1053 			struct c2c_stats *stats = &c2c_he->node_stats[node];
1054 
1055 			ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1056 			advance_hpp(hpp, ret);
1057 
1058 		#define DISPLAY_HITM(__h)						\
1059 			if (c2c_he->stats.__h> 0) {					\
1060 				ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ",	\
1061 						percent(stats->__h, c2c_he->stats.__h));\
1062 			} else {							\
1063 				ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a");	\
1064 			}
1065 
1066 			switch (c2c.display) {
1067 			case DISPLAY_RMT:
1068 				DISPLAY_HITM(rmt_hitm);
1069 				break;
1070 			case DISPLAY_LCL:
1071 				DISPLAY_HITM(lcl_hitm);
1072 				break;
1073 			case DISPLAY_TOT:
1074 				DISPLAY_HITM(tot_hitm);
1075 			default:
1076 				break;
1077 			}
1078 
1079 		#undef DISPLAY_HITM
1080 
1081 			advance_hpp(hpp, ret);
1082 
1083 			if (c2c_he->stats.store > 0) {
1084 				ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1085 						percent(stats->store, c2c_he->stats.store));
1086 			} else {
1087 				ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1088 			}
1089 
1090 			advance_hpp(hpp, ret);
1091 			break;
1092 		}
1093 		case 2:
1094 			ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1095 			advance_hpp(hpp, ret);
1096 
1097 			ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1098 			advance_hpp(hpp, ret);
1099 
1100 			ret = scnprintf(hpp->buf, hpp->size, "}");
1101 			advance_hpp(hpp, ret);
1102 			break;
1103 		default:
1104 			break;
1105 		}
1106 
1107 		first = false;
1108 	}
1109 
1110 	return 0;
1111 }
1112 
1113 static int
mean_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he,double mean)1114 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1115 	   struct hist_entry *he, double mean)
1116 {
1117 	int width = c2c_width(fmt, hpp, he->hists);
1118 	char buf[10];
1119 
1120 	scnprintf(buf, 10, "%6.0f", mean);
1121 	return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1122 }
1123 
1124 #define MEAN_ENTRY(__func, __val)						\
1125 static int									\
1126 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he)	\
1127 {										\
1128 	struct c2c_hist_entry *c2c_he;						\
1129 	c2c_he = container_of(he, struct c2c_hist_entry, he);			\
1130 	return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val));	\
1131 }
1132 
1133 MEAN_ENTRY(mean_rmt_entry,  rmt_hitm);
1134 MEAN_ENTRY(mean_lcl_entry,  lcl_hitm);
1135 MEAN_ENTRY(mean_load_entry, load);
1136 
1137 static int
cpucnt_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)1138 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1139 	     struct hist_entry *he)
1140 {
1141 	struct c2c_hist_entry *c2c_he;
1142 	int width = c2c_width(fmt, hpp, he->hists);
1143 	char buf[10];
1144 
1145 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1146 
1147 	scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1148 	return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1149 }
1150 
1151 static int
cl_idx_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)1152 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1153 	     struct hist_entry *he)
1154 {
1155 	struct c2c_hist_entry *c2c_he;
1156 	int width = c2c_width(fmt, hpp, he->hists);
1157 	char buf[10];
1158 
1159 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1160 
1161 	scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1162 	return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1163 }
1164 
1165 static int
cl_idx_empty_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)1166 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1167 		   struct hist_entry *he)
1168 {
1169 	int width = c2c_width(fmt, hpp, he->hists);
1170 
1171 	return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1172 }
1173 
1174 #define HEADER_LOW(__h)			\
1175 	{				\
1176 		.line[1] = {		\
1177 			.text = __h,	\
1178 		},			\
1179 	}
1180 
1181 #define HEADER_BOTH(__h0, __h1)		\
1182 	{				\
1183 		.line[0] = {		\
1184 			.text = __h0,	\
1185 		},			\
1186 		.line[1] = {		\
1187 			.text = __h1,	\
1188 		},			\
1189 	}
1190 
1191 #define HEADER_SPAN(__h0, __h1, __s)	\
1192 	{				\
1193 		.line[0] = {		\
1194 			.text = __h0,	\
1195 			.span = __s,	\
1196 		},			\
1197 		.line[1] = {		\
1198 			.text = __h1,	\
1199 		},			\
1200 	}
1201 
1202 #define HEADER_SPAN_LOW(__h)		\
1203 	{				\
1204 		.line[1] = {		\
1205 			.text = __h,	\
1206 		},			\
1207 	}
1208 
1209 static struct c2c_dimension dim_dcacheline = {
1210 	.header		= HEADER_LOW("Cacheline"),
1211 	.name		= "dcacheline",
1212 	.cmp		= dcacheline_cmp,
1213 	.entry		= dcacheline_entry,
1214 	.width		= 18,
1215 };
1216 
1217 static struct c2c_header header_offset_tui = HEADER_LOW("Off");
1218 
1219 static struct c2c_dimension dim_offset = {
1220 	.header		= HEADER_BOTH("Data address", "Offset"),
1221 	.name		= "offset",
1222 	.cmp		= offset_cmp,
1223 	.entry		= offset_entry,
1224 	.width		= 18,
1225 };
1226 
1227 static struct c2c_dimension dim_iaddr = {
1228 	.header		= HEADER_LOW("Code address"),
1229 	.name		= "iaddr",
1230 	.cmp		= iaddr_cmp,
1231 	.entry		= iaddr_entry,
1232 	.width		= 18,
1233 };
1234 
1235 static struct c2c_dimension dim_tot_hitm = {
1236 	.header		= HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1237 	.name		= "tot_hitm",
1238 	.cmp		= tot_hitm_cmp,
1239 	.entry		= tot_hitm_entry,
1240 	.width		= 7,
1241 };
1242 
1243 static struct c2c_dimension dim_lcl_hitm = {
1244 	.header		= HEADER_SPAN_LOW("Lcl"),
1245 	.name		= "lcl_hitm",
1246 	.cmp		= lcl_hitm_cmp,
1247 	.entry		= lcl_hitm_entry,
1248 	.width		= 7,
1249 };
1250 
1251 static struct c2c_dimension dim_rmt_hitm = {
1252 	.header		= HEADER_SPAN_LOW("Rmt"),
1253 	.name		= "rmt_hitm",
1254 	.cmp		= rmt_hitm_cmp,
1255 	.entry		= rmt_hitm_entry,
1256 	.width		= 7,
1257 };
1258 
1259 static struct c2c_dimension dim_cl_rmt_hitm = {
1260 	.header		= HEADER_SPAN("----- HITM -----", "Rmt", 1),
1261 	.name		= "cl_rmt_hitm",
1262 	.cmp		= rmt_hitm_cmp,
1263 	.entry		= rmt_hitm_entry,
1264 	.width		= 7,
1265 };
1266 
1267 static struct c2c_dimension dim_cl_lcl_hitm = {
1268 	.header		= HEADER_SPAN_LOW("Lcl"),
1269 	.name		= "cl_lcl_hitm",
1270 	.cmp		= lcl_hitm_cmp,
1271 	.entry		= lcl_hitm_entry,
1272 	.width		= 7,
1273 };
1274 
1275 static struct c2c_dimension dim_stores = {
1276 	.header		= HEADER_SPAN("---- Store Reference ----", "Total", 2),
1277 	.name		= "stores",
1278 	.cmp		= store_cmp,
1279 	.entry		= store_entry,
1280 	.width		= 7,
1281 };
1282 
1283 static struct c2c_dimension dim_stores_l1hit = {
1284 	.header		= HEADER_SPAN_LOW("L1Hit"),
1285 	.name		= "stores_l1hit",
1286 	.cmp		= st_l1hit_cmp,
1287 	.entry		= st_l1hit_entry,
1288 	.width		= 7,
1289 };
1290 
1291 static struct c2c_dimension dim_stores_l1miss = {
1292 	.header		= HEADER_SPAN_LOW("L1Miss"),
1293 	.name		= "stores_l1miss",
1294 	.cmp		= st_l1miss_cmp,
1295 	.entry		= st_l1miss_entry,
1296 	.width		= 7,
1297 };
1298 
1299 static struct c2c_dimension dim_cl_stores_l1hit = {
1300 	.header		= HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1301 	.name		= "cl_stores_l1hit",
1302 	.cmp		= st_l1hit_cmp,
1303 	.entry		= st_l1hit_entry,
1304 	.width		= 7,
1305 };
1306 
1307 static struct c2c_dimension dim_cl_stores_l1miss = {
1308 	.header		= HEADER_SPAN_LOW("L1 Miss"),
1309 	.name		= "cl_stores_l1miss",
1310 	.cmp		= st_l1miss_cmp,
1311 	.entry		= st_l1miss_entry,
1312 	.width		= 7,
1313 };
1314 
1315 static struct c2c_dimension dim_ld_fbhit = {
1316 	.header		= HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1317 	.name		= "ld_fbhit",
1318 	.cmp		= ld_fbhit_cmp,
1319 	.entry		= ld_fbhit_entry,
1320 	.width		= 7,
1321 };
1322 
1323 static struct c2c_dimension dim_ld_l1hit = {
1324 	.header		= HEADER_SPAN_LOW("L1"),
1325 	.name		= "ld_l1hit",
1326 	.cmp		= ld_l1hit_cmp,
1327 	.entry		= ld_l1hit_entry,
1328 	.width		= 7,
1329 };
1330 
1331 static struct c2c_dimension dim_ld_l2hit = {
1332 	.header		= HEADER_SPAN_LOW("L2"),
1333 	.name		= "ld_l2hit",
1334 	.cmp		= ld_l2hit_cmp,
1335 	.entry		= ld_l2hit_entry,
1336 	.width		= 7,
1337 };
1338 
1339 static struct c2c_dimension dim_ld_llchit = {
1340 	.header		= HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1341 	.name		= "ld_lclhit",
1342 	.cmp		= ld_llchit_cmp,
1343 	.entry		= ld_llchit_entry,
1344 	.width		= 8,
1345 };
1346 
1347 static struct c2c_dimension dim_ld_rmthit = {
1348 	.header		= HEADER_SPAN_LOW("Rmt"),
1349 	.name		= "ld_rmthit",
1350 	.cmp		= rmt_hit_cmp,
1351 	.entry		= rmt_hit_entry,
1352 	.width		= 8,
1353 };
1354 
1355 static struct c2c_dimension dim_ld_llcmiss = {
1356 	.header		= HEADER_BOTH("LLC", "Ld Miss"),
1357 	.name		= "ld_llcmiss",
1358 	.cmp		= ld_llcmiss_cmp,
1359 	.entry		= ld_llcmiss_entry,
1360 	.width		= 7,
1361 };
1362 
1363 static struct c2c_dimension dim_tot_recs = {
1364 	.header		= HEADER_BOTH("Total", "records"),
1365 	.name		= "tot_recs",
1366 	.cmp		= tot_recs_cmp,
1367 	.entry		= tot_recs_entry,
1368 	.width		= 7,
1369 };
1370 
1371 static struct c2c_dimension dim_tot_loads = {
1372 	.header		= HEADER_BOTH("Total", "Loads"),
1373 	.name		= "tot_loads",
1374 	.cmp		= tot_loads_cmp,
1375 	.entry		= tot_loads_entry,
1376 	.width		= 7,
1377 };
1378 
1379 static struct c2c_header percent_hitm_header[] = {
1380 	[DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1381 	[DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1382 	[DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1383 };
1384 
1385 static struct c2c_dimension dim_percent_hitm = {
1386 	.name		= "percent_hitm",
1387 	.cmp		= percent_hitm_cmp,
1388 	.entry		= percent_hitm_entry,
1389 	.color		= percent_hitm_color,
1390 	.width		= 7,
1391 };
1392 
1393 static struct c2c_dimension dim_percent_rmt_hitm = {
1394 	.header		= HEADER_SPAN("----- HITM -----", "Rmt", 1),
1395 	.name		= "percent_rmt_hitm",
1396 	.cmp		= percent_rmt_hitm_cmp,
1397 	.entry		= percent_rmt_hitm_entry,
1398 	.color		= percent_rmt_hitm_color,
1399 	.width		= 7,
1400 };
1401 
1402 static struct c2c_dimension dim_percent_lcl_hitm = {
1403 	.header		= HEADER_SPAN_LOW("Lcl"),
1404 	.name		= "percent_lcl_hitm",
1405 	.cmp		= percent_lcl_hitm_cmp,
1406 	.entry		= percent_lcl_hitm_entry,
1407 	.color		= percent_lcl_hitm_color,
1408 	.width		= 7,
1409 };
1410 
1411 static struct c2c_dimension dim_percent_stores_l1hit = {
1412 	.header		= HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1413 	.name		= "percent_stores_l1hit",
1414 	.cmp		= percent_stores_l1hit_cmp,
1415 	.entry		= percent_stores_l1hit_entry,
1416 	.color		= percent_stores_l1hit_color,
1417 	.width		= 7,
1418 };
1419 
1420 static struct c2c_dimension dim_percent_stores_l1miss = {
1421 	.header		= HEADER_SPAN_LOW("L1 Miss"),
1422 	.name		= "percent_stores_l1miss",
1423 	.cmp		= percent_stores_l1miss_cmp,
1424 	.entry		= percent_stores_l1miss_entry,
1425 	.color		= percent_stores_l1miss_color,
1426 	.width		= 7,
1427 };
1428 
1429 static struct c2c_dimension dim_dram_lcl = {
1430 	.header		= HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1431 	.name		= "dram_lcl",
1432 	.cmp		= lcl_dram_cmp,
1433 	.entry		= lcl_dram_entry,
1434 	.width		= 8,
1435 };
1436 
1437 static struct c2c_dimension dim_dram_rmt = {
1438 	.header		= HEADER_SPAN_LOW("Rmt"),
1439 	.name		= "dram_rmt",
1440 	.cmp		= rmt_dram_cmp,
1441 	.entry		= rmt_dram_entry,
1442 	.width		= 8,
1443 };
1444 
1445 static struct c2c_dimension dim_pid = {
1446 	.header		= HEADER_LOW("Pid"),
1447 	.name		= "pid",
1448 	.cmp		= pid_cmp,
1449 	.entry		= pid_entry,
1450 	.width		= 7,
1451 };
1452 
1453 static struct c2c_dimension dim_tid = {
1454 	.header		= HEADER_LOW("Tid"),
1455 	.name		= "tid",
1456 	.se		= &sort_thread,
1457 };
1458 
1459 static struct c2c_dimension dim_symbol = {
1460 	.name		= "symbol",
1461 	.se		= &sort_sym,
1462 };
1463 
1464 static struct c2c_dimension dim_dso = {
1465 	.header		= HEADER_BOTH("Shared", "Object"),
1466 	.name		= "dso",
1467 	.se		= &sort_dso,
1468 };
1469 
1470 static struct c2c_header header_node[3] = {
1471 	HEADER_LOW("Node"),
1472 	HEADER_LOW("Node{cpus %hitms %stores}"),
1473 	HEADER_LOW("Node{cpu list}"),
1474 };
1475 
1476 static struct c2c_dimension dim_node = {
1477 	.name		= "node",
1478 	.cmp		= empty_cmp,
1479 	.entry		= node_entry,
1480 	.width		= 4,
1481 };
1482 
1483 static struct c2c_dimension dim_mean_rmt = {
1484 	.header		= HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1485 	.name		= "mean_rmt",
1486 	.cmp		= empty_cmp,
1487 	.entry		= mean_rmt_entry,
1488 	.width		= 8,
1489 };
1490 
1491 static struct c2c_dimension dim_mean_lcl = {
1492 	.header		= HEADER_SPAN_LOW("lcl hitm"),
1493 	.name		= "mean_lcl",
1494 	.cmp		= empty_cmp,
1495 	.entry		= mean_lcl_entry,
1496 	.width		= 8,
1497 };
1498 
1499 static struct c2c_dimension dim_mean_load = {
1500 	.header		= HEADER_SPAN_LOW("load"),
1501 	.name		= "mean_load",
1502 	.cmp		= empty_cmp,
1503 	.entry		= mean_load_entry,
1504 	.width		= 8,
1505 };
1506 
1507 static struct c2c_dimension dim_cpucnt = {
1508 	.header		= HEADER_BOTH("cpu", "cnt"),
1509 	.name		= "cpucnt",
1510 	.cmp		= empty_cmp,
1511 	.entry		= cpucnt_entry,
1512 	.width		= 8,
1513 };
1514 
1515 static struct c2c_dimension dim_srcline = {
1516 	.name		= "cl_srcline",
1517 	.se		= &sort_srcline,
1518 };
1519 
1520 static struct c2c_dimension dim_dcacheline_idx = {
1521 	.header		= HEADER_LOW("Index"),
1522 	.name		= "cl_idx",
1523 	.cmp		= empty_cmp,
1524 	.entry		= cl_idx_entry,
1525 	.width		= 5,
1526 };
1527 
1528 static struct c2c_dimension dim_dcacheline_num = {
1529 	.header		= HEADER_LOW("Num"),
1530 	.name		= "cl_num",
1531 	.cmp		= empty_cmp,
1532 	.entry		= cl_idx_entry,
1533 	.width		= 5,
1534 };
1535 
1536 static struct c2c_dimension dim_dcacheline_num_empty = {
1537 	.header		= HEADER_LOW("Num"),
1538 	.name		= "cl_num_empty",
1539 	.cmp		= empty_cmp,
1540 	.entry		= cl_idx_empty_entry,
1541 	.width		= 5,
1542 };
1543 
1544 static struct c2c_dimension *dimensions[] = {
1545 	&dim_dcacheline,
1546 	&dim_offset,
1547 	&dim_iaddr,
1548 	&dim_tot_hitm,
1549 	&dim_lcl_hitm,
1550 	&dim_rmt_hitm,
1551 	&dim_cl_lcl_hitm,
1552 	&dim_cl_rmt_hitm,
1553 	&dim_stores,
1554 	&dim_stores_l1hit,
1555 	&dim_stores_l1miss,
1556 	&dim_cl_stores_l1hit,
1557 	&dim_cl_stores_l1miss,
1558 	&dim_ld_fbhit,
1559 	&dim_ld_l1hit,
1560 	&dim_ld_l2hit,
1561 	&dim_ld_llchit,
1562 	&dim_ld_rmthit,
1563 	&dim_ld_llcmiss,
1564 	&dim_tot_recs,
1565 	&dim_tot_loads,
1566 	&dim_percent_hitm,
1567 	&dim_percent_rmt_hitm,
1568 	&dim_percent_lcl_hitm,
1569 	&dim_percent_stores_l1hit,
1570 	&dim_percent_stores_l1miss,
1571 	&dim_dram_lcl,
1572 	&dim_dram_rmt,
1573 	&dim_pid,
1574 	&dim_tid,
1575 	&dim_symbol,
1576 	&dim_dso,
1577 	&dim_node,
1578 	&dim_mean_rmt,
1579 	&dim_mean_lcl,
1580 	&dim_mean_load,
1581 	&dim_cpucnt,
1582 	&dim_srcline,
1583 	&dim_dcacheline_idx,
1584 	&dim_dcacheline_num,
1585 	&dim_dcacheline_num_empty,
1586 	NULL,
1587 };
1588 
fmt_free(struct perf_hpp_fmt * fmt)1589 static void fmt_free(struct perf_hpp_fmt *fmt)
1590 {
1591 	struct c2c_fmt *c2c_fmt;
1592 
1593 	c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1594 	free(c2c_fmt);
1595 }
1596 
fmt_equal(struct perf_hpp_fmt * a,struct perf_hpp_fmt * b)1597 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1598 {
1599 	struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1600 	struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1601 
1602 	return c2c_a->dim == c2c_b->dim;
1603 }
1604 
get_dimension(const char * name)1605 static struct c2c_dimension *get_dimension(const char *name)
1606 {
1607 	unsigned int i;
1608 
1609 	for (i = 0; dimensions[i]; i++) {
1610 		struct c2c_dimension *dim = dimensions[i];
1611 
1612 		if (!strcmp(dim->name, name))
1613 			return dim;
1614 	};
1615 
1616 	return NULL;
1617 }
1618 
c2c_se_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)1619 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1620 			struct hist_entry *he)
1621 {
1622 	struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1623 	struct c2c_dimension *dim = c2c_fmt->dim;
1624 	size_t len = fmt->user_len;
1625 
1626 	if (!len) {
1627 		len = hists__col_len(he->hists, dim->se->se_width_idx);
1628 
1629 		if (dim == &dim_symbol || dim == &dim_srcline)
1630 			len = symbol_width(he->hists, dim->se);
1631 	}
1632 
1633 	return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1634 }
1635 
c2c_se_cmp(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)1636 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1637 			  struct hist_entry *a, struct hist_entry *b)
1638 {
1639 	struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1640 	struct c2c_dimension *dim = c2c_fmt->dim;
1641 
1642 	return dim->se->se_cmp(a, b);
1643 }
1644 
c2c_se_collapse(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)1645 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1646 			       struct hist_entry *a, struct hist_entry *b)
1647 {
1648 	struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1649 	struct c2c_dimension *dim = c2c_fmt->dim;
1650 	int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1651 
1652 	collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1653 	return collapse_fn(a, b);
1654 }
1655 
get_format(const char * name)1656 static struct c2c_fmt *get_format(const char *name)
1657 {
1658 	struct c2c_dimension *dim = get_dimension(name);
1659 	struct c2c_fmt *c2c_fmt;
1660 	struct perf_hpp_fmt *fmt;
1661 
1662 	if (!dim)
1663 		return NULL;
1664 
1665 	c2c_fmt = zalloc(sizeof(*c2c_fmt));
1666 	if (!c2c_fmt)
1667 		return NULL;
1668 
1669 	c2c_fmt->dim = dim;
1670 
1671 	fmt = &c2c_fmt->fmt;
1672 	INIT_LIST_HEAD(&fmt->list);
1673 	INIT_LIST_HEAD(&fmt->sort_list);
1674 
1675 	fmt->cmp	= dim->se ? c2c_se_cmp   : dim->cmp;
1676 	fmt->sort	= dim->se ? c2c_se_cmp   : dim->cmp;
1677 	fmt->color	= dim->se ? NULL	 : dim->color;
1678 	fmt->entry	= dim->se ? c2c_se_entry : dim->entry;
1679 	fmt->header	= c2c_header;
1680 	fmt->width	= c2c_width;
1681 	fmt->collapse	= dim->se ? c2c_se_collapse : dim->cmp;
1682 	fmt->equal	= fmt_equal;
1683 	fmt->free	= fmt_free;
1684 
1685 	return c2c_fmt;
1686 }
1687 
c2c_hists__init_output(struct perf_hpp_list * hpp_list,char * name)1688 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1689 {
1690 	struct c2c_fmt *c2c_fmt = get_format(name);
1691 
1692 	if (!c2c_fmt) {
1693 		reset_dimensions();
1694 		return output_field_add(hpp_list, name);
1695 	}
1696 
1697 	perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1698 	return 0;
1699 }
1700 
c2c_hists__init_sort(struct perf_hpp_list * hpp_list,char * name)1701 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1702 {
1703 	struct c2c_fmt *c2c_fmt = get_format(name);
1704 	struct c2c_dimension *dim;
1705 
1706 	if (!c2c_fmt) {
1707 		reset_dimensions();
1708 		return sort_dimension__add(hpp_list, name, NULL, 0);
1709 	}
1710 
1711 	dim = c2c_fmt->dim;
1712 	if (dim == &dim_dso)
1713 		hpp_list->dso = 1;
1714 
1715 	perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1716 	return 0;
1717 }
1718 
1719 #define PARSE_LIST(_list, _fn)							\
1720 	do {									\
1721 		char *tmp, *tok;						\
1722 		ret = 0;							\
1723 										\
1724 		if (!_list)							\
1725 			break;							\
1726 										\
1727 		for (tok = strtok_r((char *)_list, ", ", &tmp);			\
1728 				tok; tok = strtok_r(NULL, ", ", &tmp)) {	\
1729 			ret = _fn(hpp_list, tok);				\
1730 			if (ret == -EINVAL) {					\
1731 				pr_err("Invalid --fields key: `%s'", tok);	\
1732 				break;						\
1733 			} else if (ret == -ESRCH) {				\
1734 				pr_err("Unknown --fields key: `%s'", tok);	\
1735 				break;						\
1736 			}							\
1737 		}								\
1738 	} while (0)
1739 
hpp_list__parse(struct perf_hpp_list * hpp_list,const char * output_,const char * sort_)1740 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1741 			   const char *output_,
1742 			   const char *sort_)
1743 {
1744 	char *output = output_ ? strdup(output_) : NULL;
1745 	char *sort   = sort_   ? strdup(sort_) : NULL;
1746 	int ret;
1747 
1748 	PARSE_LIST(output, c2c_hists__init_output);
1749 	PARSE_LIST(sort,   c2c_hists__init_sort);
1750 
1751 	/* copy sort keys to output fields */
1752 	perf_hpp__setup_output_field(hpp_list);
1753 
1754 	/*
1755 	 * We dont need other sorting keys other than those
1756 	 * we already specified. It also really slows down
1757 	 * the processing a lot with big number of output
1758 	 * fields, so switching this off for c2c.
1759 	 */
1760 
1761 #if 0
1762 	/* and then copy output fields to sort keys */
1763 	perf_hpp__append_sort_keys(&hists->list);
1764 #endif
1765 
1766 	free(output);
1767 	free(sort);
1768 	return ret;
1769 }
1770 
c2c_hists__init(struct c2c_hists * hists,const char * sort,int nr_header_lines)1771 static int c2c_hists__init(struct c2c_hists *hists,
1772 			   const char *sort,
1773 			   int nr_header_lines)
1774 {
1775 	__hists__init(&hists->hists, &hists->list);
1776 
1777 	/*
1778 	 * Initialize only with sort fields, we need to resort
1779 	 * later anyway, and that's where we add output fields
1780 	 * as well.
1781 	 */
1782 	perf_hpp_list__init(&hists->list);
1783 
1784 	/* Overload number of header lines.*/
1785 	hists->list.nr_header_lines = nr_header_lines;
1786 
1787 	return hpp_list__parse(&hists->list, NULL, sort);
1788 }
1789 
c2c_hists__reinit(struct c2c_hists * c2c_hists,const char * output,const char * sort)1790 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1791 			     const char *output,
1792 			     const char *sort)
1793 {
1794 	perf_hpp__reset_output_field(&c2c_hists->list);
1795 	return hpp_list__parse(&c2c_hists->list, output, sort);
1796 }
1797 
1798 #define DISPLAY_LINE_LIMIT  0.0005
1799 
he__display(struct hist_entry * he,struct c2c_stats * stats)1800 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1801 {
1802 	struct c2c_hist_entry *c2c_he;
1803 	double ld_dist;
1804 
1805 	if (c2c.show_all)
1806 		return true;
1807 
1808 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1809 
1810 #define FILTER_HITM(__h)						\
1811 	if (stats->__h) {						\
1812 		ld_dist = ((double)c2c_he->stats.__h / stats->__h);	\
1813 		if (ld_dist < DISPLAY_LINE_LIMIT)			\
1814 			he->filtered = HIST_FILTER__C2C;		\
1815 	} else {							\
1816 		he->filtered = HIST_FILTER__C2C;			\
1817 	}
1818 
1819 	switch (c2c.display) {
1820 	case DISPLAY_LCL:
1821 		FILTER_HITM(lcl_hitm);
1822 		break;
1823 	case DISPLAY_RMT:
1824 		FILTER_HITM(rmt_hitm);
1825 		break;
1826 	case DISPLAY_TOT:
1827 		FILTER_HITM(tot_hitm);
1828 	default:
1829 		break;
1830 	};
1831 
1832 #undef FILTER_HITM
1833 
1834 	return he->filtered == 0;
1835 }
1836 
valid_hitm_or_store(struct hist_entry * he)1837 static inline int valid_hitm_or_store(struct hist_entry *he)
1838 {
1839 	struct c2c_hist_entry *c2c_he;
1840 	bool has_hitm;
1841 
1842 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1843 	has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1844 		   c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1845 						c2c_he->stats.rmt_hitm;
1846 	return has_hitm || c2c_he->stats.store;
1847 }
1848 
calc_width(struct hist_entry * he)1849 static void calc_width(struct hist_entry *he)
1850 {
1851 	struct c2c_hists *c2c_hists;
1852 
1853 	c2c_hists = container_of(he->hists, struct c2c_hists, hists);
1854 	hists__calc_col_len(&c2c_hists->hists, he);
1855 }
1856 
filter_cb(struct hist_entry * he)1857 static int filter_cb(struct hist_entry *he)
1858 {
1859 	if (c2c.show_src && !he->srcline)
1860 		he->srcline = hist_entry__get_srcline(he);
1861 
1862 	calc_width(he);
1863 
1864 	if (!valid_hitm_or_store(he))
1865 		he->filtered = HIST_FILTER__C2C;
1866 
1867 	return 0;
1868 }
1869 
resort_cl_cb(struct hist_entry * he)1870 static int resort_cl_cb(struct hist_entry *he)
1871 {
1872 	struct c2c_hist_entry *c2c_he;
1873 	struct c2c_hists *c2c_hists;
1874 	bool display = he__display(he, &c2c.hitm_stats);
1875 
1876 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1877 	c2c_hists = c2c_he->hists;
1878 
1879 	calc_width(he);
1880 
1881 	if (display && c2c_hists) {
1882 		static unsigned int idx;
1883 
1884 		c2c_he->cacheline_idx = idx++;
1885 
1886 		c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
1887 
1888 		hists__collapse_resort(&c2c_hists->hists, NULL);
1889 		hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
1890 	}
1891 
1892 	return 0;
1893 }
1894 
setup_nodes_header(void)1895 static void setup_nodes_header(void)
1896 {
1897 	dim_node.header = header_node[c2c.node_info];
1898 }
1899 
setup_nodes(struct perf_session * session)1900 static int setup_nodes(struct perf_session *session)
1901 {
1902 	struct numa_node *n;
1903 	unsigned long **nodes;
1904 	int node, cpu;
1905 	int *cpu2node;
1906 
1907 	if (c2c.node_info > 2)
1908 		c2c.node_info = 2;
1909 
1910 	c2c.nodes_cnt = session->header.env.nr_numa_nodes;
1911 	c2c.cpus_cnt  = session->header.env.nr_cpus_online;
1912 
1913 	n = session->header.env.numa_nodes;
1914 	if (!n)
1915 		return -EINVAL;
1916 
1917 	nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
1918 	if (!nodes)
1919 		return -ENOMEM;
1920 
1921 	c2c.nodes = nodes;
1922 
1923 	cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
1924 	if (!cpu2node)
1925 		return -ENOMEM;
1926 
1927 	for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
1928 		cpu2node[cpu] = -1;
1929 
1930 	c2c.cpu2node = cpu2node;
1931 
1932 	for (node = 0; node < c2c.nodes_cnt; node++) {
1933 		struct cpu_map *map = n[node].map;
1934 		unsigned long *set;
1935 
1936 		set = bitmap_alloc(c2c.cpus_cnt);
1937 		if (!set)
1938 			return -ENOMEM;
1939 
1940 		nodes[node] = set;
1941 
1942 		/* empty node, skip */
1943 		if (cpu_map__empty(map))
1944 			continue;
1945 
1946 		for (cpu = 0; cpu < map->nr; cpu++) {
1947 			set_bit(map->map[cpu], set);
1948 
1949 			if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
1950 				return -EINVAL;
1951 
1952 			cpu2node[map->map[cpu]] = node;
1953 		}
1954 	}
1955 
1956 	setup_nodes_header();
1957 	return 0;
1958 }
1959 
1960 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
1961 
resort_hitm_cb(struct hist_entry * he)1962 static int resort_hitm_cb(struct hist_entry *he)
1963 {
1964 	struct c2c_hist_entry *c2c_he;
1965 	c2c_he = container_of(he, struct c2c_hist_entry, he);
1966 
1967 	if (HAS_HITMS(c2c_he)) {
1968 		c2c.shared_clines++;
1969 		c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
1970 	}
1971 
1972 	return 0;
1973 }
1974 
hists__iterate_cb(struct hists * hists,hists__resort_cb_t cb)1975 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
1976 {
1977 	struct rb_node *next = rb_first(&hists->entries);
1978 	int ret = 0;
1979 
1980 	while (next) {
1981 		struct hist_entry *he;
1982 
1983 		he = rb_entry(next, struct hist_entry, rb_node);
1984 		ret = cb(he);
1985 		if (ret)
1986 			break;
1987 		next = rb_next(&he->rb_node);
1988 	}
1989 
1990 	return ret;
1991 }
1992 
print_c2c__display_stats(FILE * out)1993 static void print_c2c__display_stats(FILE *out)
1994 {
1995 	int llc_misses;
1996 	struct c2c_stats *stats = &c2c.hists.stats;
1997 
1998 	llc_misses = stats->lcl_dram +
1999 		     stats->rmt_dram +
2000 		     stats->rmt_hit +
2001 		     stats->rmt_hitm;
2002 
2003 	fprintf(out, "=================================================\n");
2004 	fprintf(out, "            Trace Event Information              \n");
2005 	fprintf(out, "=================================================\n");
2006 	fprintf(out, "  Total records                     : %10d\n", stats->nr_entries);
2007 	fprintf(out, "  Locked Load/Store Operations      : %10d\n", stats->locks);
2008 	fprintf(out, "  Load Operations                   : %10d\n", stats->load);
2009 	fprintf(out, "  Loads - uncacheable               : %10d\n", stats->ld_uncache);
2010 	fprintf(out, "  Loads - IO                        : %10d\n", stats->ld_io);
2011 	fprintf(out, "  Loads - Miss                      : %10d\n", stats->ld_miss);
2012 	fprintf(out, "  Loads - no mapping                : %10d\n", stats->ld_noadrs);
2013 	fprintf(out, "  Load Fill Buffer Hit              : %10d\n", stats->ld_fbhit);
2014 	fprintf(out, "  Load L1D hit                      : %10d\n", stats->ld_l1hit);
2015 	fprintf(out, "  Load L2D hit                      : %10d\n", stats->ld_l2hit);
2016 	fprintf(out, "  Load LLC hit                      : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2017 	fprintf(out, "  Load Local HITM                   : %10d\n", stats->lcl_hitm);
2018 	fprintf(out, "  Load Remote HITM                  : %10d\n", stats->rmt_hitm);
2019 	fprintf(out, "  Load Remote HIT                   : %10d\n", stats->rmt_hit);
2020 	fprintf(out, "  Load Local DRAM                   : %10d\n", stats->lcl_dram);
2021 	fprintf(out, "  Load Remote DRAM                  : %10d\n", stats->rmt_dram);
2022 	fprintf(out, "  Load MESI State Exclusive         : %10d\n", stats->ld_excl);
2023 	fprintf(out, "  Load MESI State Shared            : %10d\n", stats->ld_shared);
2024 	fprintf(out, "  Load LLC Misses                   : %10d\n", llc_misses);
2025 	fprintf(out, "  LLC Misses to Local DRAM          : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2026 	fprintf(out, "  LLC Misses to Remote DRAM         : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2027 	fprintf(out, "  LLC Misses to Remote cache (HIT)  : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2028 	fprintf(out, "  LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2029 	fprintf(out, "  Store Operations                  : %10d\n", stats->store);
2030 	fprintf(out, "  Store - uncacheable               : %10d\n", stats->st_uncache);
2031 	fprintf(out, "  Store - no mapping                : %10d\n", stats->st_noadrs);
2032 	fprintf(out, "  Store L1D Hit                     : %10d\n", stats->st_l1hit);
2033 	fprintf(out, "  Store L1D Miss                    : %10d\n", stats->st_l1miss);
2034 	fprintf(out, "  No Page Map Rejects               : %10d\n", stats->nomap);
2035 	fprintf(out, "  Unable to parse data source       : %10d\n", stats->noparse);
2036 }
2037 
print_shared_cacheline_info(FILE * out)2038 static void print_shared_cacheline_info(FILE *out)
2039 {
2040 	struct c2c_stats *stats = &c2c.hitm_stats;
2041 	int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2042 
2043 	fprintf(out, "=================================================\n");
2044 	fprintf(out, "    Global Shared Cache Line Event Information   \n");
2045 	fprintf(out, "=================================================\n");
2046 	fprintf(out, "  Total Shared Cache Lines          : %10d\n", c2c.shared_clines);
2047 	fprintf(out, "  Load HITs on shared lines         : %10d\n", stats->load);
2048 	fprintf(out, "  Fill Buffer Hits on shared lines  : %10d\n", stats->ld_fbhit);
2049 	fprintf(out, "  L1D hits on shared lines          : %10d\n", stats->ld_l1hit);
2050 	fprintf(out, "  L2D hits on shared lines          : %10d\n", stats->ld_l2hit);
2051 	fprintf(out, "  LLC hits on shared lines          : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2052 	fprintf(out, "  Locked Access on shared lines     : %10d\n", stats->locks);
2053 	fprintf(out, "  Store HITs on shared lines        : %10d\n", stats->store);
2054 	fprintf(out, "  Store L1D hits on shared lines    : %10d\n", stats->st_l1hit);
2055 	fprintf(out, "  Total Merged records              : %10d\n", hitm_cnt + stats->store);
2056 }
2057 
print_cacheline(struct c2c_hists * c2c_hists,struct hist_entry * he_cl,struct perf_hpp_list * hpp_list,FILE * out)2058 static void print_cacheline(struct c2c_hists *c2c_hists,
2059 			    struct hist_entry *he_cl,
2060 			    struct perf_hpp_list *hpp_list,
2061 			    FILE *out)
2062 {
2063 	char bf[1000];
2064 	struct perf_hpp hpp = {
2065 		.buf            = bf,
2066 		.size           = 1000,
2067 	};
2068 	static bool once;
2069 
2070 	if (!once) {
2071 		hists__fprintf_headers(&c2c_hists->hists, out);
2072 		once = true;
2073 	} else {
2074 		fprintf(out, "\n");
2075 	}
2076 
2077 	fprintf(out, "  -------------------------------------------------------------\n");
2078 	__hist_entry__snprintf(he_cl, &hpp, hpp_list);
2079 	fprintf(out, "%s\n", bf);
2080 	fprintf(out, "  -------------------------------------------------------------\n");
2081 
2082 	hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, true);
2083 }
2084 
print_pareto(FILE * out)2085 static void print_pareto(FILE *out)
2086 {
2087 	struct perf_hpp_list hpp_list;
2088 	struct rb_node *nd;
2089 	int ret;
2090 
2091 	perf_hpp_list__init(&hpp_list);
2092 	ret = hpp_list__parse(&hpp_list,
2093 				"cl_num,"
2094 				"cl_rmt_hitm,"
2095 				"cl_lcl_hitm,"
2096 				"cl_stores_l1hit,"
2097 				"cl_stores_l1miss,"
2098 				"dcacheline",
2099 				NULL);
2100 
2101 	if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2102 		return;
2103 
2104 	nd = rb_first(&c2c.hists.hists.entries);
2105 
2106 	for (; nd; nd = rb_next(nd)) {
2107 		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2108 		struct c2c_hist_entry *c2c_he;
2109 
2110 		if (he->filtered)
2111 			continue;
2112 
2113 		c2c_he = container_of(he, struct c2c_hist_entry, he);
2114 		print_cacheline(c2c_he->hists, he, &hpp_list, out);
2115 	}
2116 }
2117 
print_c2c_info(FILE * out,struct perf_session * session)2118 static void print_c2c_info(FILE *out, struct perf_session *session)
2119 {
2120 	struct perf_evlist *evlist = session->evlist;
2121 	struct perf_evsel *evsel;
2122 	bool first = true;
2123 
2124 	fprintf(out, "=================================================\n");
2125 	fprintf(out, "                 c2c details                     \n");
2126 	fprintf(out, "=================================================\n");
2127 
2128 	evlist__for_each_entry(evlist, evsel) {
2129 		fprintf(out, "%-36s: %s\n", first ? "  Events" : "",
2130 			perf_evsel__name(evsel));
2131 		first = false;
2132 	}
2133 	fprintf(out, "  Cachelines sort on                : %s HITMs\n",
2134 		display_str[c2c.display]);
2135 	fprintf(out, "  Cacheline data grouping           : %s\n", c2c.cl_sort);
2136 }
2137 
perf_c2c__hists_fprintf(FILE * out,struct perf_session * session)2138 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2139 {
2140 	setup_pager();
2141 
2142 	print_c2c__display_stats(out);
2143 	fprintf(out, "\n");
2144 	print_shared_cacheline_info(out);
2145 	fprintf(out, "\n");
2146 	print_c2c_info(out, session);
2147 
2148 	if (c2c.stats_only)
2149 		return;
2150 
2151 	fprintf(out, "\n");
2152 	fprintf(out, "=================================================\n");
2153 	fprintf(out, "           Shared Data Cache Line Table          \n");
2154 	fprintf(out, "=================================================\n");
2155 	fprintf(out, "#\n");
2156 
2157 	hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, false);
2158 
2159 	fprintf(out, "\n");
2160 	fprintf(out, "=================================================\n");
2161 	fprintf(out, "      Shared Cache Line Distribution Pareto      \n");
2162 	fprintf(out, "=================================================\n");
2163 	fprintf(out, "#\n");
2164 
2165 	print_pareto(out);
2166 }
2167 
2168 #ifdef HAVE_SLANG_SUPPORT
c2c_browser__update_nr_entries(struct hist_browser * hb)2169 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2170 {
2171 	u64 nr_entries = 0;
2172 	struct rb_node *nd = rb_first(&hb->hists->entries);
2173 
2174 	while (nd) {
2175 		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2176 
2177 		if (!he->filtered)
2178 			nr_entries++;
2179 
2180 		nd = rb_next(nd);
2181 	}
2182 
2183 	hb->nr_non_filtered_entries = nr_entries;
2184 }
2185 
2186 struct c2c_cacheline_browser {
2187 	struct hist_browser	 hb;
2188 	struct hist_entry	*he;
2189 };
2190 
2191 static int
perf_c2c_cacheline_browser__title(struct hist_browser * browser,char * bf,size_t size)2192 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2193 				  char *bf, size_t size)
2194 {
2195 	struct c2c_cacheline_browser *cl_browser;
2196 	struct hist_entry *he;
2197 	uint64_t addr = 0;
2198 
2199 	cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2200 	he = cl_browser->he;
2201 
2202 	if (he->mem_info)
2203 		addr = cl_address(he->mem_info->daddr.addr);
2204 
2205 	scnprintf(bf, size, "Cacheline 0x%lx", addr);
2206 	return 0;
2207 }
2208 
2209 static struct c2c_cacheline_browser*
c2c_cacheline_browser__new(struct hists * hists,struct hist_entry * he)2210 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2211 {
2212 	struct c2c_cacheline_browser *browser;
2213 
2214 	browser = zalloc(sizeof(*browser));
2215 	if (browser) {
2216 		hist_browser__init(&browser->hb, hists);
2217 		browser->hb.c2c_filter	= true;
2218 		browser->hb.title	= perf_c2c_cacheline_browser__title;
2219 		browser->he		= he;
2220 	}
2221 
2222 	return browser;
2223 }
2224 
perf_c2c__browse_cacheline(struct hist_entry * he)2225 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2226 {
2227 	struct c2c_hist_entry *c2c_he;
2228 	struct c2c_hists *c2c_hists;
2229 	struct c2c_cacheline_browser *cl_browser;
2230 	struct hist_browser *browser;
2231 	int key = -1;
2232 	const char help[] =
2233 	" ENTER         Togle callchains (if present) \n"
2234 	" n             Togle Node details info \n"
2235 	" s             Togle full lenght of symbol and source line columns \n"
2236 	" q             Return back to cacheline list \n";
2237 
2238 	if (!he)
2239 		return 0;
2240 
2241 	/* Display compact version first. */
2242 	c2c.symbol_full = false;
2243 
2244 	c2c_he = container_of(he, struct c2c_hist_entry, he);
2245 	c2c_hists = c2c_he->hists;
2246 
2247 	cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2248 	if (cl_browser == NULL)
2249 		return -1;
2250 
2251 	browser = &cl_browser->hb;
2252 
2253 	/* reset abort key so that it can get Ctrl-C as a key */
2254 	SLang_reset_tty();
2255 	SLang_init_tty(0, 0, 0);
2256 
2257 	c2c_browser__update_nr_entries(browser);
2258 
2259 	while (1) {
2260 		key = hist_browser__run(browser, "? - help");
2261 
2262 		switch (key) {
2263 		case 's':
2264 			c2c.symbol_full = !c2c.symbol_full;
2265 			break;
2266 		case 'n':
2267 			c2c.node_info = (c2c.node_info + 1) % 3;
2268 			setup_nodes_header();
2269 			break;
2270 		case 'q':
2271 			goto out;
2272 		case '?':
2273 			ui_browser__help_window(&browser->b, help);
2274 			break;
2275 		default:
2276 			break;
2277 		}
2278 	}
2279 
2280 out:
2281 	free(cl_browser);
2282 	return 0;
2283 }
2284 
perf_c2c_browser__title(struct hist_browser * browser,char * bf,size_t size)2285 static int perf_c2c_browser__title(struct hist_browser *browser,
2286 				   char *bf, size_t size)
2287 {
2288 	scnprintf(bf, size,
2289 		  "Shared Data Cache Line Table     "
2290 		  "(%lu entries, sorted on %s HITMs)",
2291 		  browser->nr_non_filtered_entries,
2292 		  display_str[c2c.display]);
2293 	return 0;
2294 }
2295 
2296 static struct hist_browser*
perf_c2c_browser__new(struct hists * hists)2297 perf_c2c_browser__new(struct hists *hists)
2298 {
2299 	struct hist_browser *browser = hist_browser__new(hists);
2300 
2301 	if (browser) {
2302 		browser->title = perf_c2c_browser__title;
2303 		browser->c2c_filter = true;
2304 	}
2305 
2306 	return browser;
2307 }
2308 
perf_c2c__hists_browse(struct hists * hists)2309 static int perf_c2c__hists_browse(struct hists *hists)
2310 {
2311 	struct hist_browser *browser;
2312 	int key = -1;
2313 	const char help[] =
2314 	" d             Display cacheline details \n"
2315 	" ENTER         Togle callchains (if present) \n"
2316 	" q             Quit \n";
2317 
2318 	browser = perf_c2c_browser__new(hists);
2319 	if (browser == NULL)
2320 		return -1;
2321 
2322 	/* reset abort key so that it can get Ctrl-C as a key */
2323 	SLang_reset_tty();
2324 	SLang_init_tty(0, 0, 0);
2325 
2326 	c2c_browser__update_nr_entries(browser);
2327 
2328 	while (1) {
2329 		key = hist_browser__run(browser, "? - help");
2330 
2331 		switch (key) {
2332 		case 'q':
2333 			goto out;
2334 		case 'd':
2335 			perf_c2c__browse_cacheline(browser->he_selection);
2336 			break;
2337 		case '?':
2338 			ui_browser__help_window(&browser->b, help);
2339 			break;
2340 		default:
2341 			break;
2342 		}
2343 	}
2344 
2345 out:
2346 	hist_browser__delete(browser);
2347 	return 0;
2348 }
2349 
perf_c2c_display(struct perf_session * session)2350 static void perf_c2c_display(struct perf_session *session)
2351 {
2352 	if (use_browser == 0)
2353 		perf_c2c__hists_fprintf(stdout, session);
2354 	else
2355 		perf_c2c__hists_browse(&c2c.hists.hists);
2356 }
2357 #else
perf_c2c_display(struct perf_session * session)2358 static void perf_c2c_display(struct perf_session *session)
2359 {
2360 	use_browser = 0;
2361 	perf_c2c__hists_fprintf(stdout, session);
2362 }
2363 #endif /* HAVE_SLANG_SUPPORT */
2364 
ui_quirks(void)2365 static void ui_quirks(void)
2366 {
2367 	if (!c2c.use_stdio) {
2368 		dim_offset.width  = 5;
2369 		dim_offset.header = header_offset_tui;
2370 	}
2371 
2372 	dim_percent_hitm.header = percent_hitm_header[c2c.display];
2373 }
2374 
2375 #define CALLCHAIN_DEFAULT_OPT  "graph,0.5,caller,function,percent"
2376 
2377 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2378 				CALLCHAIN_REPORT_HELP
2379 				"\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2380 
2381 static int
parse_callchain_opt(const struct option * opt,const char * arg,int unset)2382 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2383 {
2384 	struct callchain_param *callchain = opt->value;
2385 
2386 	callchain->enabled = !unset;
2387 	/*
2388 	 * --no-call-graph
2389 	 */
2390 	if (unset) {
2391 		symbol_conf.use_callchain = false;
2392 		callchain->mode = CHAIN_NONE;
2393 		return 0;
2394 	}
2395 
2396 	return parse_callchain_report_opt(arg);
2397 }
2398 
setup_callchain(struct perf_evlist * evlist)2399 static int setup_callchain(struct perf_evlist *evlist)
2400 {
2401 	u64 sample_type = perf_evlist__combined_sample_type(evlist);
2402 	enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2403 
2404 	if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2405 	    (sample_type & PERF_SAMPLE_STACK_USER)) {
2406 		mode = CALLCHAIN_DWARF;
2407 		dwarf_callchain_users = true;
2408 	} else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2409 		mode = CALLCHAIN_LBR;
2410 	else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2411 		mode = CALLCHAIN_FP;
2412 
2413 	if (!callchain_param.enabled &&
2414 	    callchain_param.mode != CHAIN_NONE &&
2415 	    mode != CALLCHAIN_NONE) {
2416 		symbol_conf.use_callchain = true;
2417 		if (callchain_register_param(&callchain_param) < 0) {
2418 			ui__error("Can't register callchain params.\n");
2419 			return -EINVAL;
2420 		}
2421 	}
2422 
2423 	callchain_param.record_mode = mode;
2424 	callchain_param.min_percent = 0;
2425 	return 0;
2426 }
2427 
setup_display(const char * str)2428 static int setup_display(const char *str)
2429 {
2430 	const char *display = str ?: "tot";
2431 
2432 	if (!strcmp(display, "tot"))
2433 		c2c.display = DISPLAY_TOT;
2434 	else if (!strcmp(display, "rmt"))
2435 		c2c.display = DISPLAY_RMT;
2436 	else if (!strcmp(display, "lcl"))
2437 		c2c.display = DISPLAY_LCL;
2438 	else {
2439 		pr_err("failed: unknown display type: %s\n", str);
2440 		return -1;
2441 	}
2442 
2443 	return 0;
2444 }
2445 
2446 #define for_each_token(__tok, __buf, __sep, __tmp)		\
2447 	for (__tok = strtok_r(__buf, __sep, &__tmp); __tok;	\
2448 	     __tok = strtok_r(NULL,  __sep, &__tmp))
2449 
build_cl_output(char * cl_sort,bool no_source)2450 static int build_cl_output(char *cl_sort, bool no_source)
2451 {
2452 	char *tok, *tmp, *buf = strdup(cl_sort);
2453 	bool add_pid   = false;
2454 	bool add_tid   = false;
2455 	bool add_iaddr = false;
2456 	bool add_sym   = false;
2457 	bool add_dso   = false;
2458 	bool add_src   = false;
2459 	int ret = 0;
2460 
2461 	if (!buf)
2462 		return -ENOMEM;
2463 
2464 	for_each_token(tok, buf, ",", tmp) {
2465 		if (!strcmp(tok, "tid")) {
2466 			add_tid = true;
2467 		} else if (!strcmp(tok, "pid")) {
2468 			add_pid = true;
2469 		} else if (!strcmp(tok, "iaddr")) {
2470 			add_iaddr = true;
2471 			add_sym   = true;
2472 			add_dso   = true;
2473 			add_src   = no_source ? false : true;
2474 		} else if (!strcmp(tok, "dso")) {
2475 			add_dso = true;
2476 		} else if (strcmp(tok, "offset")) {
2477 			pr_err("unrecognized sort token: %s\n", tok);
2478 			ret = -EINVAL;
2479 			goto err;
2480 		}
2481 	}
2482 
2483 	if (asprintf(&c2c.cl_output,
2484 		"%s%s%s%s%s%s%s%s%s%s",
2485 		c2c.use_stdio ? "cl_num_empty," : "",
2486 		"percent_rmt_hitm,"
2487 		"percent_lcl_hitm,"
2488 		"percent_stores_l1hit,"
2489 		"percent_stores_l1miss,"
2490 		"offset,",
2491 		add_pid   ? "pid," : "",
2492 		add_tid   ? "tid," : "",
2493 		add_iaddr ? "iaddr," : "",
2494 		"mean_rmt,"
2495 		"mean_lcl,"
2496 		"mean_load,"
2497 		"tot_recs,"
2498 		"cpucnt,",
2499 		add_sym ? "symbol," : "",
2500 		add_dso ? "dso," : "",
2501 		add_src ? "cl_srcline," : "",
2502 		"node") < 0) {
2503 		ret = -ENOMEM;
2504 		goto err;
2505 	}
2506 
2507 	c2c.show_src = add_src;
2508 err:
2509 	free(buf);
2510 	return ret;
2511 }
2512 
setup_coalesce(const char * coalesce,bool no_source)2513 static int setup_coalesce(const char *coalesce, bool no_source)
2514 {
2515 	const char *c = coalesce ?: coalesce_default;
2516 
2517 	if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2518 		return -ENOMEM;
2519 
2520 	if (build_cl_output(c2c.cl_sort, no_source))
2521 		return -1;
2522 
2523 	if (asprintf(&c2c.cl_resort, "offset,%s",
2524 		     c2c.display == DISPLAY_TOT ?
2525 		     "tot_hitm" :
2526 		     c2c.display == DISPLAY_RMT ?
2527 		     "rmt_hitm,lcl_hitm" :
2528 		     "lcl_hitm,rmt_hitm") < 0)
2529 		return -ENOMEM;
2530 
2531 	pr_debug("coalesce sort   fields: %s\n", c2c.cl_sort);
2532 	pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2533 	pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2534 	return 0;
2535 }
2536 
perf_c2c__report(int argc,const char ** argv)2537 static int perf_c2c__report(int argc, const char **argv)
2538 {
2539 	struct perf_session *session;
2540 	struct ui_progress prog;
2541 	struct perf_data_file file = {
2542 		.mode = PERF_DATA_MODE_READ,
2543 	};
2544 	char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2545 	const char *display = NULL;
2546 	const char *coalesce = NULL;
2547 	bool no_source = false;
2548 	const struct option options[] = {
2549 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2550 		   "file", "vmlinux pathname"),
2551 	OPT_STRING('i', "input", &input_name, "file",
2552 		   "the input file to process"),
2553 	OPT_INCR('N', "node-info", &c2c.node_info,
2554 		 "show extra node info in report (repeat for more info)"),
2555 #ifdef HAVE_SLANG_SUPPORT
2556 	OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2557 #endif
2558 	OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2559 		    "Display only statistic tables (implies --stdio)"),
2560 	OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2561 		    "Display full length of symbols"),
2562 	OPT_BOOLEAN(0, "no-source", &no_source,
2563 		    "Do not display Source Line column"),
2564 	OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2565 		    "Show all captured HITM lines."),
2566 	OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2567 			     "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2568 			     callchain_help, &parse_callchain_opt,
2569 			     callchain_default_opt),
2570 	OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2571 	OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2572 		   "coalesce fields: pid,tid,iaddr,dso"),
2573 	OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2574 	OPT_PARENT(c2c_options),
2575 	OPT_END()
2576 	};
2577 	int err = 0;
2578 
2579 	argc = parse_options(argc, argv, options, report_c2c_usage,
2580 			     PARSE_OPT_STOP_AT_NON_OPTION);
2581 	if (argc)
2582 		usage_with_options(report_c2c_usage, options);
2583 
2584 	if (c2c.stats_only)
2585 		c2c.use_stdio = true;
2586 
2587 	if (!input_name || !strlen(input_name))
2588 		input_name = "perf.data";
2589 
2590 	file.path  = input_name;
2591 	file.force = symbol_conf.force;
2592 
2593 	err = setup_display(display);
2594 	if (err)
2595 		goto out;
2596 
2597 	err = setup_coalesce(coalesce, no_source);
2598 	if (err) {
2599 		pr_debug("Failed to initialize hists\n");
2600 		goto out;
2601 	}
2602 
2603 	err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2604 	if (err) {
2605 		pr_debug("Failed to initialize hists\n");
2606 		goto out;
2607 	}
2608 
2609 	session = perf_session__new(&file, 0, &c2c.tool);
2610 	if (session == NULL) {
2611 		pr_debug("No memory for session\n");
2612 		goto out;
2613 	}
2614 
2615 	err = setup_nodes(session);
2616 	if (err) {
2617 		pr_err("Failed setup nodes\n");
2618 		goto out;
2619 	}
2620 
2621 	err = setup_callchain(session->evlist);
2622 	if (err)
2623 		goto out_session;
2624 
2625 	if (symbol__init(&session->header.env) < 0)
2626 		goto out_session;
2627 
2628 	/* No pipe support at the moment. */
2629 	if (perf_data_file__is_pipe(session->file)) {
2630 		pr_debug("No pipe support at the moment.\n");
2631 		goto out_session;
2632 	}
2633 
2634 	if (c2c.use_stdio)
2635 		use_browser = 0;
2636 	else
2637 		use_browser = 1;
2638 
2639 	setup_browser(false);
2640 
2641 	err = perf_session__process_events(session);
2642 	if (err) {
2643 		pr_err("failed to process sample\n");
2644 		goto out_session;
2645 	}
2646 
2647 	c2c_hists__reinit(&c2c.hists,
2648 			"cl_idx,"
2649 			"dcacheline,"
2650 			"tot_recs,"
2651 			"percent_hitm,"
2652 			"tot_hitm,lcl_hitm,rmt_hitm,"
2653 			"stores,stores_l1hit,stores_l1miss,"
2654 			"dram_lcl,dram_rmt,"
2655 			"ld_llcmiss,"
2656 			"tot_loads,"
2657 			"ld_fbhit,ld_l1hit,ld_l2hit,"
2658 			"ld_lclhit,ld_rmthit",
2659 			c2c.display == DISPLAY_TOT ? "tot_hitm" :
2660 			c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
2661 			);
2662 
2663 	ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2664 
2665 	hists__collapse_resort(&c2c.hists.hists, NULL);
2666 	hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2667 	hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2668 
2669 	ui_progress__finish();
2670 
2671 	ui_quirks();
2672 
2673 	perf_c2c_display(session);
2674 
2675 out_session:
2676 	perf_session__delete(session);
2677 out:
2678 	return err;
2679 }
2680 
parse_record_events(const struct option * opt,const char * str,int unset __maybe_unused)2681 static int parse_record_events(const struct option *opt,
2682 			       const char *str, int unset __maybe_unused)
2683 {
2684 	bool *event_set = (bool *) opt->value;
2685 
2686 	*event_set = true;
2687 	return perf_mem_events__parse(str);
2688 }
2689 
2690 
2691 static const char * const __usage_record[] = {
2692 	"perf c2c record [<options>] [<command>]",
2693 	"perf c2c record [<options>] -- <command> [<options>]",
2694 	NULL
2695 };
2696 
2697 static const char * const *record_mem_usage = __usage_record;
2698 
perf_c2c__record(int argc,const char ** argv)2699 static int perf_c2c__record(int argc, const char **argv)
2700 {
2701 	int rec_argc, i = 0, j;
2702 	const char **rec_argv;
2703 	int ret;
2704 	bool all_user = false, all_kernel = false;
2705 	bool event_set = false;
2706 	struct option options[] = {
2707 	OPT_CALLBACK('e', "event", &event_set, "event",
2708 		     "event selector. Use 'perf mem record -e list' to list available events",
2709 		     parse_record_events),
2710 	OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2711 	OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2712 	OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2713 	OPT_PARENT(c2c_options),
2714 	OPT_END()
2715 	};
2716 
2717 	if (perf_mem_events__init()) {
2718 		pr_err("failed: memory events not supported\n");
2719 		return -1;
2720 	}
2721 
2722 	argc = parse_options(argc, argv, options, record_mem_usage,
2723 			     PARSE_OPT_KEEP_UNKNOWN);
2724 
2725 	rec_argc = argc + 10; /* max number of arguments */
2726 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
2727 	if (!rec_argv)
2728 		return -1;
2729 
2730 	rec_argv[i++] = "record";
2731 
2732 	if (!event_set) {
2733 		perf_mem_events[PERF_MEM_EVENTS__LOAD].record  = true;
2734 		perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2735 	}
2736 
2737 	if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2738 		rec_argv[i++] = "-W";
2739 
2740 	rec_argv[i++] = "-d";
2741 	rec_argv[i++] = "--sample-cpu";
2742 
2743 	for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2744 		if (!perf_mem_events[j].record)
2745 			continue;
2746 
2747 		if (!perf_mem_events[j].supported) {
2748 			pr_err("failed: event '%s' not supported\n",
2749 			       perf_mem_events[j].name);
2750 			free(rec_argv);
2751 			return -1;
2752 		}
2753 
2754 		rec_argv[i++] = "-e";
2755 		rec_argv[i++] = perf_mem_events__name(j);
2756 	};
2757 
2758 	if (all_user)
2759 		rec_argv[i++] = "--all-user";
2760 
2761 	if (all_kernel)
2762 		rec_argv[i++] = "--all-kernel";
2763 
2764 	for (j = 0; j < argc; j++, i++)
2765 		rec_argv[i] = argv[j];
2766 
2767 	if (verbose > 0) {
2768 		pr_debug("calling: ");
2769 
2770 		j = 0;
2771 
2772 		while (rec_argv[j]) {
2773 			pr_debug("%s ", rec_argv[j]);
2774 			j++;
2775 		}
2776 		pr_debug("\n");
2777 	}
2778 
2779 	ret = cmd_record(i, rec_argv);
2780 	free(rec_argv);
2781 	return ret;
2782 }
2783 
cmd_c2c(int argc,const char ** argv)2784 int cmd_c2c(int argc, const char **argv)
2785 {
2786 	argc = parse_options(argc, argv, c2c_options, c2c_usage,
2787 			     PARSE_OPT_STOP_AT_NON_OPTION);
2788 
2789 	if (!argc)
2790 		usage_with_options(c2c_usage, c2c_options);
2791 
2792 	if (!strncmp(argv[0], "rec", 3)) {
2793 		return perf_c2c__record(argc, argv);
2794 	} else if (!strncmp(argv[0], "rep", 3)) {
2795 		return perf_c2c__report(argc, argv);
2796 	} else {
2797 		usage_with_options(c2c_usage, c2c_options);
2798 	}
2799 
2800 	return 0;
2801 }
2802