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