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