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