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