1 // SPDX-License-Identifier: GPL-2.0
3 * This is rewrite of original c2c tool introduced in here:
4 * http://lwn.net/Articles/588866/
6 * The original tool was changed to fit in current perf state.
9 * Don Zickus <dzickus@redhat.com>
10 * Dick Fowles <fowles@inreach.com>
11 * Joe Mario <jmario@redhat.com>
15 #include <linux/compiler.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/stringify.h>
19 #include <linux/zalloc.h>
21 #include <sys/param.h>
24 #include <perf/cpumap.h>
25 #include <subcmd/pager.h>
26 #include <subcmd/parse-options.h>
27 #include "map_symbol.h"
28 #include "mem-events.h"
33 #include "cacheline.h"
38 #include "ui/browsers/hists.h"
43 #include "ui/progress.h"
46 #include "pmu-hybrid.h"
51 struct perf_hpp_list list;
52 struct c2c_stats stats;
55 struct compute_stats {
56 struct stats lcl_hitm;
57 struct stats rmt_hitm;
58 struct stats lcl_peer;
59 struct stats rmt_peer;
63 struct c2c_hist_entry {
64 struct c2c_hists *hists;
65 struct c2c_stats stats;
66 unsigned long *cpuset;
67 unsigned long *nodeset;
68 struct c2c_stats *node_stats;
69 unsigned int cacheline_idx;
71 struct compute_stats cstats;
74 unsigned long paddr_cnt;
80 * because of its callchain dynamic entry
85 static char const *coalesce_default = "iaddr";
88 struct perf_tool tool;
89 struct c2c_hists hists;
90 struct mem2node mem2node;
92 unsigned long **nodes;
105 /* Shared cache line stats */
106 struct c2c_stats shared_clines_stats;
111 const char *coalesce;
125 static const char *display_str[DISPLAY_MAX] = {
126 [DISPLAY_LCL_HITM] = "Local HITMs",
127 [DISPLAY_RMT_HITM] = "Remote HITMs",
128 [DISPLAY_TOT_HITM] = "Total HITMs",
129 [DISPLAY_SNP_PEER] = "Peer Snoop",
132 static const struct option c2c_options[] = {
133 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
137 static struct perf_c2c c2c;
139 static void *c2c_he_zalloc(size_t size)
141 struct c2c_hist_entry *c2c_he;
143 c2c_he = zalloc(size + sizeof(*c2c_he));
147 c2c_he->cpuset = bitmap_zalloc(c2c.cpus_cnt);
151 c2c_he->nodeset = bitmap_zalloc(c2c.nodes_cnt);
152 if (!c2c_he->nodeset)
155 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
156 if (!c2c_he->node_stats)
159 init_stats(&c2c_he->cstats.lcl_hitm);
160 init_stats(&c2c_he->cstats.rmt_hitm);
161 init_stats(&c2c_he->cstats.lcl_peer);
162 init_stats(&c2c_he->cstats.rmt_peer);
163 init_stats(&c2c_he->cstats.load);
168 free(c2c_he->nodeset);
169 free(c2c_he->cpuset);
174 static void c2c_he_free(void *he)
176 struct c2c_hist_entry *c2c_he;
178 c2c_he = container_of(he, struct c2c_hist_entry, he);
180 hists__delete_entries(&c2c_he->hists->hists);
184 free(c2c_he->cpuset);
185 free(c2c_he->nodeset);
186 free(c2c_he->nodestr);
187 free(c2c_he->node_stats);
191 static struct hist_entry_ops c2c_entry_ops = {
192 .new = c2c_he_zalloc,
196 static int c2c_hists__init(struct c2c_hists *hists,
198 int nr_header_lines);
200 static struct c2c_hists*
201 he__get_c2c_hists(struct hist_entry *he,
205 struct c2c_hist_entry *c2c_he;
206 struct c2c_hists *hists;
209 c2c_he = container_of(he, struct c2c_hist_entry, he);
211 return c2c_he->hists;
213 hists = c2c_he->hists = zalloc(sizeof(*hists));
217 ret = c2c_hists__init(hists, sort, nr_header_lines);
226 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
227 struct perf_sample *sample)
229 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
230 "WARNING: no sample cpu value"))
233 __set_bit(sample->cpu, c2c_he->cpuset);
236 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
237 struct perf_sample *sample)
241 if (!sample->phys_addr) {
242 c2c_he->paddr_zero = true;
246 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
247 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
250 __set_bit(node, c2c_he->nodeset);
252 if (c2c_he->paddr != sample->phys_addr) {
254 c2c_he->paddr = sample->phys_addr;
258 static void compute_stats(struct c2c_hist_entry *c2c_he,
259 struct c2c_stats *stats,
262 struct compute_stats *cstats = &c2c_he->cstats;
265 update_stats(&cstats->rmt_hitm, weight);
266 else if (stats->lcl_hitm)
267 update_stats(&cstats->lcl_hitm, weight);
268 else if (stats->rmt_peer)
269 update_stats(&cstats->rmt_peer, weight);
270 else if (stats->lcl_peer)
271 update_stats(&cstats->lcl_peer, weight);
272 else if (stats->load)
273 update_stats(&cstats->load, weight);
276 static int process_sample_event(struct perf_tool *tool __maybe_unused,
277 union perf_event *event,
278 struct perf_sample *sample,
280 struct machine *machine)
282 struct c2c_hists *c2c_hists = &c2c.hists;
283 struct c2c_hist_entry *c2c_he;
284 struct c2c_stats stats = { .nr_entries = 0, };
285 struct hist_entry *he;
286 struct addr_location al;
287 struct mem_info *mi, *mi_dup;
290 if (machine__resolve(machine, &al, sample) < 0) {
291 pr_debug("problem processing %d event, skipping it.\n",
297 al.thread->lbr_stitch_enable = true;
299 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
300 evsel, &al, sysctl_perf_event_max_stack);
304 mi = sample__resolve_mem(sample, &al);
309 * The mi object is released in hists__add_entry_ops,
310 * if it gets sorted out into existing data, so we need
311 * to take the copy now.
313 mi_dup = mem_info__get(mi);
315 c2c_decode_stats(&stats, mi);
317 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
323 c2c_he = container_of(he, struct c2c_hist_entry, he);
324 c2c_add_stats(&c2c_he->stats, &stats);
325 c2c_add_stats(&c2c_hists->stats, &stats);
327 c2c_he__set_cpu(c2c_he, sample);
328 c2c_he__set_node(c2c_he, sample);
330 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
331 ret = hist_entry__append_callchain(he, sample);
335 * There's already been warning about missing
336 * sample's cpu value. Let's account all to
337 * node 0 in this case, without any further
340 * Doing node stats only for single callchain data.
342 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
343 int node = c2c.cpu2node[cpu];
347 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
351 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
357 c2c_he = container_of(he, struct c2c_hist_entry, he);
358 c2c_add_stats(&c2c_he->stats, &stats);
359 c2c_add_stats(&c2c_hists->stats, &stats);
360 c2c_add_stats(&c2c_he->node_stats[node], &stats);
362 compute_stats(c2c_he, &stats, sample->weight);
364 c2c_he__set_cpu(c2c_he, sample);
365 c2c_he__set_node(c2c_he, sample);
367 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
368 ret = hist_entry__append_callchain(he, sample);
372 addr_location__put(&al);
376 mem_info__put(mi_dup);
382 static struct perf_c2c c2c = {
384 .sample = process_sample_event,
385 .mmap = perf_event__process_mmap,
386 .mmap2 = perf_event__process_mmap2,
387 .comm = perf_event__process_comm,
388 .exit = perf_event__process_exit,
389 .fork = perf_event__process_fork,
390 .lost = perf_event__process_lost,
391 .attr = perf_event__process_attr,
392 .auxtrace_info = perf_event__process_auxtrace_info,
393 .auxtrace = perf_event__process_auxtrace,
394 .auxtrace_error = perf_event__process_auxtrace_error,
395 .ordered_events = true,
396 .ordering_requires_timestamps = true,
400 static const char * const c2c_usage[] = {
401 "perf c2c {record|report}",
405 static const char * const __usage_report[] = {
410 static const char * const *report_c2c_usage = __usage_report;
412 #define C2C_HEADER_MAX 2
418 } line[C2C_HEADER_MAX];
421 struct c2c_dimension {
422 struct c2c_header header;
425 struct sort_entry *se;
427 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
428 struct hist_entry *, struct hist_entry *);
429 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
430 struct hist_entry *he);
431 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
432 struct hist_entry *he);
436 struct perf_hpp_fmt fmt;
437 struct c2c_dimension *dim;
440 #define SYMBOL_WIDTH 30
442 static struct c2c_dimension dim_symbol;
443 static struct c2c_dimension dim_srcline;
445 static int symbol_width(struct hists *hists, struct sort_entry *se)
447 int width = hists__col_len(hists, se->se_width_idx);
449 if (!c2c.symbol_full)
450 width = MIN(width, SYMBOL_WIDTH);
455 static int c2c_width(struct perf_hpp_fmt *fmt,
456 struct perf_hpp *hpp __maybe_unused,
459 struct c2c_fmt *c2c_fmt;
460 struct c2c_dimension *dim;
462 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
465 if (dim == &dim_symbol || dim == &dim_srcline)
466 return symbol_width(hists, dim->se);
468 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
472 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
473 struct hists *hists, int line, int *span)
475 struct perf_hpp_list *hpp_list = hists->hpp_list;
476 struct c2c_fmt *c2c_fmt;
477 struct c2c_dimension *dim;
478 const char *text = NULL;
479 int width = c2c_width(fmt, hpp, hists);
481 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
485 text = dim->header.line[line].text;
486 /* Use the last line from sort_entry if not defined. */
487 if (!text && (line == hpp_list->nr_header_lines - 1))
488 text = dim->se->se_header;
490 text = dim->header.line[line].text;
496 *span = dim->header.line[line].span;
503 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
506 #define HEX_STR(__s, __v) \
508 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
513 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
514 struct hist_entry *left, struct hist_entry *right)
516 return sort__dcacheline_cmp(left, right);
519 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
520 struct hist_entry *he)
523 int width = c2c_width(fmt, hpp, he->hists);
527 addr = cl_address(he->mem_info->daddr.addr, chk_double_cl);
529 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
533 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
534 struct hist_entry *he)
536 struct c2c_hist_entry *c2c_he;
537 int width = c2c_width(fmt, hpp, he->hists);
539 c2c_he = container_of(he, struct c2c_hist_entry, he);
540 if (WARN_ON_ONCE(!c2c_he->nodestr))
543 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
547 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
548 struct hist_entry *he)
550 struct c2c_hist_entry *c2c_he;
551 int width = c2c_width(fmt, hpp, he->hists);
553 c2c_he = container_of(he, struct c2c_hist_entry, he);
554 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
557 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
558 struct hist_entry *he)
561 int width = c2c_width(fmt, hpp, he->hists);
565 addr = cl_offset(he->mem_info->daddr.al_addr, chk_double_cl);
567 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
571 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
572 struct hist_entry *left, struct hist_entry *right)
574 uint64_t l = 0, r = 0;
577 l = cl_offset(left->mem_info->daddr.addr, chk_double_cl);
580 r = cl_offset(right->mem_info->daddr.addr, chk_double_cl);
582 return (int64_t)(r - l);
586 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
587 struct hist_entry *he)
590 int width = c2c_width(fmt, hpp, he->hists);
594 addr = he->mem_info->iaddr.addr;
596 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
600 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
601 struct hist_entry *left, struct hist_entry *right)
603 return sort__iaddr_cmp(left, right);
607 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
608 struct hist_entry *he)
610 struct c2c_hist_entry *c2c_he;
611 int width = c2c_width(fmt, hpp, he->hists);
612 unsigned int tot_hitm;
614 c2c_he = container_of(he, struct c2c_hist_entry, he);
615 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
617 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
621 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
622 struct hist_entry *left, struct hist_entry *right)
624 struct c2c_hist_entry *c2c_left;
625 struct c2c_hist_entry *c2c_right;
626 uint64_t tot_hitm_left;
627 uint64_t tot_hitm_right;
629 c2c_left = container_of(left, struct c2c_hist_entry, he);
630 c2c_right = container_of(right, struct c2c_hist_entry, he);
632 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
633 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
635 return tot_hitm_left - tot_hitm_right;
638 #define STAT_FN_ENTRY(__f) \
640 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
641 struct hist_entry *he) \
643 struct c2c_hist_entry *c2c_he; \
644 int width = c2c_width(fmt, hpp, he->hists); \
646 c2c_he = container_of(he, struct c2c_hist_entry, he); \
647 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
648 c2c_he->stats.__f); \
651 #define STAT_FN_CMP(__f) \
653 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
654 struct hist_entry *left, struct hist_entry *right) \
656 struct c2c_hist_entry *c2c_left, *c2c_right; \
658 c2c_left = container_of(left, struct c2c_hist_entry, he); \
659 c2c_right = container_of(right, struct c2c_hist_entry, he); \
660 return (uint64_t) c2c_left->stats.__f - \
661 (uint64_t) c2c_right->stats.__f; \
664 #define STAT_FN(__f) \
683 static uint64_t get_load_llc_misses(struct c2c_stats *stats)
685 return stats->lcl_dram +
691 static uint64_t get_load_cache_hits(struct c2c_stats *stats)
693 return stats->ld_fbhit +
700 static uint64_t get_stores(struct c2c_stats *stats)
702 return stats->st_l1hit +
707 static uint64_t total_records(struct c2c_stats *stats)
709 return get_load_llc_misses(stats) +
710 get_load_cache_hits(stats) +
715 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
716 struct hist_entry *he)
718 struct c2c_hist_entry *c2c_he;
719 int width = c2c_width(fmt, hpp, he->hists);
722 c2c_he = container_of(he, struct c2c_hist_entry, he);
723 tot_recs = total_records(&c2c_he->stats);
725 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
729 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
730 struct hist_entry *left, struct hist_entry *right)
732 struct c2c_hist_entry *c2c_left;
733 struct c2c_hist_entry *c2c_right;
734 uint64_t tot_recs_left;
735 uint64_t tot_recs_right;
737 c2c_left = container_of(left, struct c2c_hist_entry, he);
738 c2c_right = container_of(right, struct c2c_hist_entry, he);
740 tot_recs_left = total_records(&c2c_left->stats);
741 tot_recs_right = total_records(&c2c_right->stats);
743 return tot_recs_left - tot_recs_right;
746 static uint64_t total_loads(struct c2c_stats *stats)
748 return get_load_llc_misses(stats) +
749 get_load_cache_hits(stats);
753 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
754 struct hist_entry *he)
756 struct c2c_hist_entry *c2c_he;
757 int width = c2c_width(fmt, hpp, he->hists);
760 c2c_he = container_of(he, struct c2c_hist_entry, he);
761 tot_recs = total_loads(&c2c_he->stats);
763 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
767 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
768 struct hist_entry *left, struct hist_entry *right)
770 struct c2c_hist_entry *c2c_left;
771 struct c2c_hist_entry *c2c_right;
772 uint64_t tot_recs_left;
773 uint64_t tot_recs_right;
775 c2c_left = container_of(left, struct c2c_hist_entry, he);
776 c2c_right = container_of(right, struct c2c_hist_entry, he);
778 tot_recs_left = total_loads(&c2c_left->stats);
779 tot_recs_right = total_loads(&c2c_right->stats);
781 return tot_recs_left - tot_recs_right;
784 typedef double (get_percent_cb)(struct c2c_hist_entry *);
787 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
788 struct hist_entry *he, get_percent_cb get_percent)
790 struct c2c_hist_entry *c2c_he;
791 int width = c2c_width(fmt, hpp, he->hists);
794 c2c_he = container_of(he, struct c2c_hist_entry, he);
795 per = get_percent(c2c_he);
797 #ifdef HAVE_SLANG_SUPPORT
799 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
801 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
804 static double percent_costly_snoop(struct c2c_hist_entry *c2c_he)
806 struct c2c_hists *hists;
807 struct c2c_stats *stats;
808 struct c2c_stats *total;
812 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
813 stats = &c2c_he->stats;
814 total = &hists->stats;
816 switch (c2c.display) {
817 case DISPLAY_RMT_HITM:
818 st = stats->rmt_hitm;
819 tot = total->rmt_hitm;
821 case DISPLAY_LCL_HITM:
822 st = stats->lcl_hitm;
823 tot = total->lcl_hitm;
825 case DISPLAY_TOT_HITM:
826 st = stats->tot_hitm;
827 tot = total->tot_hitm;
829 case DISPLAY_SNP_PEER:
830 st = stats->tot_peer;
831 tot = total->tot_peer;
837 p = tot ? (double) st / tot : 0;
842 #define PERC_STR(__s, __v) \
844 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
849 percent_costly_snoop_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
850 struct hist_entry *he)
852 struct c2c_hist_entry *c2c_he;
853 int width = c2c_width(fmt, hpp, he->hists);
857 c2c_he = container_of(he, struct c2c_hist_entry, he);
858 per = percent_costly_snoop(c2c_he);
859 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
863 percent_costly_snoop_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
864 struct hist_entry *he)
866 return percent_color(fmt, hpp, he, percent_costly_snoop);
870 percent_costly_snoop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
871 struct hist_entry *left, struct hist_entry *right)
873 struct c2c_hist_entry *c2c_left;
874 struct c2c_hist_entry *c2c_right;
878 c2c_left = container_of(left, struct c2c_hist_entry, he);
879 c2c_right = container_of(right, struct c2c_hist_entry, he);
881 per_left = percent_costly_snoop(c2c_left);
882 per_right = percent_costly_snoop(c2c_right);
884 return per_left - per_right;
887 static struct c2c_stats *he_stats(struct hist_entry *he)
889 struct c2c_hist_entry *c2c_he;
891 c2c_he = container_of(he, struct c2c_hist_entry, he);
892 return &c2c_he->stats;
895 static struct c2c_stats *total_stats(struct hist_entry *he)
897 struct c2c_hists *hists;
899 hists = container_of(he->hists, struct c2c_hists, hists);
900 return &hists->stats;
903 static double percent(u32 st, u32 tot)
905 return tot ? 100. * (double) st / (double) tot : 0;
908 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
910 #define PERCENT_FN(__f) \
911 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
913 struct c2c_hists *hists; \
915 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
916 return percent(c2c_he->stats.__f, hists->stats.__f); \
924 PERCENT_FN(st_l1miss)
928 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
929 struct hist_entry *he)
931 int width = c2c_width(fmt, hpp, he->hists);
932 double per = PERCENT(he, rmt_hitm);
935 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
939 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
940 struct hist_entry *he)
942 return percent_color(fmt, hpp, he, percent_rmt_hitm);
946 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
947 struct hist_entry *left, struct hist_entry *right)
952 per_left = PERCENT(left, rmt_hitm);
953 per_right = PERCENT(right, rmt_hitm);
955 return per_left - per_right;
959 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
960 struct hist_entry *he)
962 int width = c2c_width(fmt, hpp, he->hists);
963 double per = PERCENT(he, lcl_hitm);
966 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
970 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
971 struct hist_entry *he)
973 return percent_color(fmt, hpp, he, percent_lcl_hitm);
977 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
978 struct hist_entry *left, struct hist_entry *right)
983 per_left = PERCENT(left, lcl_hitm);
984 per_right = PERCENT(right, lcl_hitm);
986 return per_left - per_right;
990 percent_lcl_peer_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
991 struct hist_entry *he)
993 int width = c2c_width(fmt, hpp, he->hists);
994 double per = PERCENT(he, lcl_peer);
997 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1001 percent_lcl_peer_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1002 struct hist_entry *he)
1004 return percent_color(fmt, hpp, he, percent_lcl_peer);
1008 percent_lcl_peer_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1009 struct hist_entry *left, struct hist_entry *right)
1014 per_left = PERCENT(left, lcl_peer);
1015 per_right = PERCENT(right, lcl_peer);
1017 return per_left - per_right;
1021 percent_rmt_peer_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1022 struct hist_entry *he)
1024 int width = c2c_width(fmt, hpp, he->hists);
1025 double per = PERCENT(he, rmt_peer);
1028 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1032 percent_rmt_peer_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1033 struct hist_entry *he)
1035 return percent_color(fmt, hpp, he, percent_rmt_peer);
1039 percent_rmt_peer_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1040 struct hist_entry *left, struct hist_entry *right)
1045 per_left = PERCENT(left, rmt_peer);
1046 per_right = PERCENT(right, rmt_peer);
1048 return per_left - per_right;
1052 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1053 struct hist_entry *he)
1055 int width = c2c_width(fmt, hpp, he->hists);
1056 double per = PERCENT(he, st_l1hit);
1059 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1063 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1064 struct hist_entry *he)
1066 return percent_color(fmt, hpp, he, percent_st_l1hit);
1070 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1071 struct hist_entry *left, struct hist_entry *right)
1076 per_left = PERCENT(left, st_l1hit);
1077 per_right = PERCENT(right, st_l1hit);
1079 return per_left - per_right;
1083 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1084 struct hist_entry *he)
1086 int width = c2c_width(fmt, hpp, he->hists);
1087 double per = PERCENT(he, st_l1miss);
1090 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1094 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1095 struct hist_entry *he)
1097 return percent_color(fmt, hpp, he, percent_st_l1miss);
1101 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1102 struct hist_entry *left, struct hist_entry *right)
1107 per_left = PERCENT(left, st_l1miss);
1108 per_right = PERCENT(right, st_l1miss);
1110 return per_left - per_right;
1114 percent_stores_na_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1115 struct hist_entry *he)
1117 int width = c2c_width(fmt, hpp, he->hists);
1118 double per = PERCENT(he, st_na);
1121 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1125 percent_stores_na_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1126 struct hist_entry *he)
1128 return percent_color(fmt, hpp, he, percent_st_na);
1132 percent_stores_na_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1133 struct hist_entry *left, struct hist_entry *right)
1138 per_left = PERCENT(left, st_na);
1139 per_right = PERCENT(right, st_na);
1141 return per_left - per_right;
1148 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1149 struct hist_entry *he)
1151 int width = c2c_width(fmt, hpp, he->hists);
1153 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1157 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1158 struct hist_entry *left, struct hist_entry *right)
1160 return left->thread->pid_ - right->thread->pid_;
1164 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1165 struct hist_entry *left __maybe_unused,
1166 struct hist_entry *right __maybe_unused)
1171 static int display_metrics(struct perf_hpp *hpp, u32 val, u32 sum)
1176 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ",
1179 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a");
1185 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1186 struct hist_entry *he)
1188 struct c2c_hist_entry *c2c_he;
1193 c2c_he = container_of(he, struct c2c_hist_entry, he);
1195 for (node = 0; node < c2c.nodes_cnt; node++) {
1196 DECLARE_BITMAP(set, c2c.cpus_cnt);
1198 bitmap_zero(set, c2c.cpus_cnt);
1199 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1201 if (bitmap_empty(set, c2c.cpus_cnt)) {
1202 if (c2c.node_info == 1) {
1203 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1204 advance_hpp(hpp, ret);
1210 ret = scnprintf(hpp->buf, hpp->size, " ");
1211 advance_hpp(hpp, ret);
1214 switch (c2c.node_info) {
1216 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1217 advance_hpp(hpp, ret);
1221 int num = bitmap_weight(set, c2c.cpus_cnt);
1222 struct c2c_stats *stats = &c2c_he->node_stats[node];
1224 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1225 advance_hpp(hpp, ret);
1227 switch (c2c.display) {
1228 case DISPLAY_RMT_HITM:
1229 ret = display_metrics(hpp, stats->rmt_hitm,
1230 c2c_he->stats.rmt_hitm);
1232 case DISPLAY_LCL_HITM:
1233 ret = display_metrics(hpp, stats->lcl_hitm,
1234 c2c_he->stats.lcl_hitm);
1236 case DISPLAY_TOT_HITM:
1237 ret = display_metrics(hpp, stats->tot_hitm,
1238 c2c_he->stats.tot_hitm);
1240 case DISPLAY_SNP_PEER:
1241 ret = display_metrics(hpp, stats->tot_peer,
1242 c2c_he->stats.tot_peer);
1248 advance_hpp(hpp, ret);
1250 if (c2c_he->stats.store > 0) {
1251 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1252 percent(stats->store, c2c_he->stats.store));
1254 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1257 advance_hpp(hpp, ret);
1261 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1262 advance_hpp(hpp, ret);
1264 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1265 advance_hpp(hpp, ret);
1267 ret = scnprintf(hpp->buf, hpp->size, "}");
1268 advance_hpp(hpp, ret);
1281 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1282 struct hist_entry *he, double mean)
1284 int width = c2c_width(fmt, hpp, he->hists);
1287 scnprintf(buf, 10, "%6.0f", mean);
1288 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1291 #define MEAN_ENTRY(__func, __val) \
1293 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1295 struct c2c_hist_entry *c2c_he; \
1296 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1297 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1300 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1301 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1302 MEAN_ENTRY(mean_load_entry, load);
1303 MEAN_ENTRY(mean_rmt_peer_entry, rmt_peer);
1304 MEAN_ENTRY(mean_lcl_peer_entry, lcl_peer);
1307 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1308 struct hist_entry *he)
1310 struct c2c_hist_entry *c2c_he;
1311 int width = c2c_width(fmt, hpp, he->hists);
1314 c2c_he = container_of(he, struct c2c_hist_entry, he);
1316 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1317 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1321 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1322 struct hist_entry *he)
1324 struct c2c_hist_entry *c2c_he;
1325 int width = c2c_width(fmt, hpp, he->hists);
1328 c2c_he = container_of(he, struct c2c_hist_entry, he);
1330 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1331 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1335 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1336 struct hist_entry *he)
1338 int width = c2c_width(fmt, hpp, he->hists);
1340 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1343 #define HEADER_LOW(__h) \
1350 #define HEADER_BOTH(__h0, __h1) \
1360 #define HEADER_SPAN(__h0, __h1, __s) \
1371 #define HEADER_SPAN_LOW(__h) \
1378 static struct c2c_dimension dim_dcacheline = {
1379 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1380 .name = "dcacheline",
1381 .cmp = dcacheline_cmp,
1382 .entry = dcacheline_entry,
1386 static struct c2c_dimension dim_dcacheline_node = {
1387 .header = HEADER_LOW("Node"),
1388 .name = "dcacheline_node",
1390 .entry = dcacheline_node_entry,
1394 static struct c2c_dimension dim_dcacheline_count = {
1395 .header = HEADER_LOW("PA cnt"),
1396 .name = "dcacheline_count",
1398 .entry = dcacheline_node_count,
1402 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1404 static struct c2c_dimension dim_offset = {
1405 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
1408 .entry = offset_entry,
1412 static struct c2c_dimension dim_offset_node = {
1413 .header = HEADER_LOW("Node"),
1414 .name = "offset_node",
1416 .entry = dcacheline_node_entry,
1420 static struct c2c_dimension dim_iaddr = {
1421 .header = HEADER_LOW("Code address"),
1424 .entry = iaddr_entry,
1428 static struct c2c_dimension dim_tot_hitm = {
1429 .header = HEADER_SPAN("------- Load Hitm -------", "Total", 2),
1431 .cmp = tot_hitm_cmp,
1432 .entry = tot_hitm_entry,
1436 static struct c2c_dimension dim_lcl_hitm = {
1437 .header = HEADER_SPAN_LOW("LclHitm"),
1439 .cmp = lcl_hitm_cmp,
1440 .entry = lcl_hitm_entry,
1444 static struct c2c_dimension dim_rmt_hitm = {
1445 .header = HEADER_SPAN_LOW("RmtHitm"),
1447 .cmp = rmt_hitm_cmp,
1448 .entry = rmt_hitm_entry,
1452 static struct c2c_dimension dim_tot_peer = {
1453 .header = HEADER_SPAN("------- Load Peer -------", "Total", 2),
1455 .cmp = tot_peer_cmp,
1456 .entry = tot_peer_entry,
1460 static struct c2c_dimension dim_lcl_peer = {
1461 .header = HEADER_SPAN_LOW("Local"),
1463 .cmp = lcl_peer_cmp,
1464 .entry = lcl_peer_entry,
1468 static struct c2c_dimension dim_rmt_peer = {
1469 .header = HEADER_SPAN_LOW("Remote"),
1471 .cmp = rmt_peer_cmp,
1472 .entry = rmt_peer_entry,
1476 static struct c2c_dimension dim_cl_rmt_hitm = {
1477 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1478 .name = "cl_rmt_hitm",
1479 .cmp = rmt_hitm_cmp,
1480 .entry = rmt_hitm_entry,
1484 static struct c2c_dimension dim_cl_lcl_hitm = {
1485 .header = HEADER_SPAN_LOW("Lcl"),
1486 .name = "cl_lcl_hitm",
1487 .cmp = lcl_hitm_cmp,
1488 .entry = lcl_hitm_entry,
1492 static struct c2c_dimension dim_cl_rmt_peer = {
1493 .header = HEADER_SPAN("----- Peer -----", "Rmt", 1),
1494 .name = "cl_rmt_peer",
1495 .cmp = rmt_peer_cmp,
1496 .entry = rmt_peer_entry,
1500 static struct c2c_dimension dim_cl_lcl_peer = {
1501 .header = HEADER_SPAN_LOW("Lcl"),
1502 .name = "cl_lcl_peer",
1503 .cmp = lcl_peer_cmp,
1504 .entry = lcl_peer_entry,
1508 static struct c2c_dimension dim_tot_stores = {
1509 .header = HEADER_BOTH("Total", "Stores"),
1510 .name = "tot_stores",
1512 .entry = store_entry,
1516 static struct c2c_dimension dim_stores_l1hit = {
1517 .header = HEADER_SPAN("--------- Stores --------", "L1Hit", 2),
1518 .name = "stores_l1hit",
1519 .cmp = st_l1hit_cmp,
1520 .entry = st_l1hit_entry,
1524 static struct c2c_dimension dim_stores_l1miss = {
1525 .header = HEADER_SPAN_LOW("L1Miss"),
1526 .name = "stores_l1miss",
1527 .cmp = st_l1miss_cmp,
1528 .entry = st_l1miss_entry,
1532 static struct c2c_dimension dim_stores_na = {
1533 .header = HEADER_SPAN_LOW("N/A"),
1534 .name = "stores_na",
1536 .entry = st_na_entry,
1540 static struct c2c_dimension dim_cl_stores_l1hit = {
1541 .header = HEADER_SPAN("------- Store Refs ------", "L1 Hit", 2),
1542 .name = "cl_stores_l1hit",
1543 .cmp = st_l1hit_cmp,
1544 .entry = st_l1hit_entry,
1548 static struct c2c_dimension dim_cl_stores_l1miss = {
1549 .header = HEADER_SPAN_LOW("L1 Miss"),
1550 .name = "cl_stores_l1miss",
1551 .cmp = st_l1miss_cmp,
1552 .entry = st_l1miss_entry,
1556 static struct c2c_dimension dim_cl_stores_na = {
1557 .header = HEADER_SPAN_LOW("N/A"),
1558 .name = "cl_stores_na",
1560 .entry = st_na_entry,
1564 static struct c2c_dimension dim_ld_fbhit = {
1565 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1567 .cmp = ld_fbhit_cmp,
1568 .entry = ld_fbhit_entry,
1572 static struct c2c_dimension dim_ld_l1hit = {
1573 .header = HEADER_SPAN_LOW("L1"),
1575 .cmp = ld_l1hit_cmp,
1576 .entry = ld_l1hit_entry,
1580 static struct c2c_dimension dim_ld_l2hit = {
1581 .header = HEADER_SPAN_LOW("L2"),
1583 .cmp = ld_l2hit_cmp,
1584 .entry = ld_l2hit_entry,
1588 static struct c2c_dimension dim_ld_llchit = {
1589 .header = HEADER_SPAN("- LLC Load Hit --", "LclHit", 1),
1590 .name = "ld_lclhit",
1591 .cmp = ld_llchit_cmp,
1592 .entry = ld_llchit_entry,
1596 static struct c2c_dimension dim_ld_rmthit = {
1597 .header = HEADER_SPAN("- RMT Load Hit --", "RmtHit", 1),
1598 .name = "ld_rmthit",
1600 .entry = rmt_hit_entry,
1604 static struct c2c_dimension dim_tot_recs = {
1605 .header = HEADER_BOTH("Total", "records"),
1607 .cmp = tot_recs_cmp,
1608 .entry = tot_recs_entry,
1612 static struct c2c_dimension dim_tot_loads = {
1613 .header = HEADER_BOTH("Total", "Loads"),
1614 .name = "tot_loads",
1615 .cmp = tot_loads_cmp,
1616 .entry = tot_loads_entry,
1620 static struct c2c_header percent_costly_snoop_header[] = {
1621 [DISPLAY_LCL_HITM] = HEADER_BOTH("Lcl", "Hitm"),
1622 [DISPLAY_RMT_HITM] = HEADER_BOTH("Rmt", "Hitm"),
1623 [DISPLAY_TOT_HITM] = HEADER_BOTH("Tot", "Hitm"),
1624 [DISPLAY_SNP_PEER] = HEADER_BOTH("Peer", "Snoop"),
1627 static struct c2c_dimension dim_percent_costly_snoop = {
1628 .name = "percent_costly_snoop",
1629 .cmp = percent_costly_snoop_cmp,
1630 .entry = percent_costly_snoop_entry,
1631 .color = percent_costly_snoop_color,
1635 static struct c2c_dimension dim_percent_rmt_hitm = {
1636 .header = HEADER_SPAN("----- HITM -----", "RmtHitm", 1),
1637 .name = "percent_rmt_hitm",
1638 .cmp = percent_rmt_hitm_cmp,
1639 .entry = percent_rmt_hitm_entry,
1640 .color = percent_rmt_hitm_color,
1644 static struct c2c_dimension dim_percent_lcl_hitm = {
1645 .header = HEADER_SPAN_LOW("LclHitm"),
1646 .name = "percent_lcl_hitm",
1647 .cmp = percent_lcl_hitm_cmp,
1648 .entry = percent_lcl_hitm_entry,
1649 .color = percent_lcl_hitm_color,
1653 static struct c2c_dimension dim_percent_rmt_peer = {
1654 .header = HEADER_SPAN("-- Peer Snoop --", "Rmt", 1),
1655 .name = "percent_rmt_peer",
1656 .cmp = percent_rmt_peer_cmp,
1657 .entry = percent_rmt_peer_entry,
1658 .color = percent_rmt_peer_color,
1662 static struct c2c_dimension dim_percent_lcl_peer = {
1663 .header = HEADER_SPAN_LOW("Lcl"),
1664 .name = "percent_lcl_peer",
1665 .cmp = percent_lcl_peer_cmp,
1666 .entry = percent_lcl_peer_entry,
1667 .color = percent_lcl_peer_color,
1671 static struct c2c_dimension dim_percent_stores_l1hit = {
1672 .header = HEADER_SPAN("------- Store Refs ------", "L1 Hit", 2),
1673 .name = "percent_stores_l1hit",
1674 .cmp = percent_stores_l1hit_cmp,
1675 .entry = percent_stores_l1hit_entry,
1676 .color = percent_stores_l1hit_color,
1680 static struct c2c_dimension dim_percent_stores_l1miss = {
1681 .header = HEADER_SPAN_LOW("L1 Miss"),
1682 .name = "percent_stores_l1miss",
1683 .cmp = percent_stores_l1miss_cmp,
1684 .entry = percent_stores_l1miss_entry,
1685 .color = percent_stores_l1miss_color,
1689 static struct c2c_dimension dim_percent_stores_na = {
1690 .header = HEADER_SPAN_LOW("N/A"),
1691 .name = "percent_stores_na",
1692 .cmp = percent_stores_na_cmp,
1693 .entry = percent_stores_na_entry,
1694 .color = percent_stores_na_color,
1698 static struct c2c_dimension dim_dram_lcl = {
1699 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1701 .cmp = lcl_dram_cmp,
1702 .entry = lcl_dram_entry,
1706 static struct c2c_dimension dim_dram_rmt = {
1707 .header = HEADER_SPAN_LOW("Rmt"),
1709 .cmp = rmt_dram_cmp,
1710 .entry = rmt_dram_entry,
1714 static struct c2c_dimension dim_pid = {
1715 .header = HEADER_LOW("Pid"),
1722 static struct c2c_dimension dim_tid = {
1723 .header = HEADER_LOW("Tid"),
1728 static struct c2c_dimension dim_symbol = {
1733 static struct c2c_dimension dim_dso = {
1734 .header = HEADER_BOTH("Shared", "Object"),
1739 static struct c2c_dimension dim_node = {
1742 .entry = node_entry,
1746 static struct c2c_dimension dim_mean_rmt = {
1747 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1750 .entry = mean_rmt_entry,
1754 static struct c2c_dimension dim_mean_lcl = {
1755 .header = HEADER_SPAN_LOW("lcl hitm"),
1758 .entry = mean_lcl_entry,
1762 static struct c2c_dimension dim_mean_load = {
1763 .header = HEADER_SPAN_LOW("load"),
1764 .name = "mean_load",
1766 .entry = mean_load_entry,
1770 static struct c2c_dimension dim_mean_rmt_peer = {
1771 .header = HEADER_SPAN("---------- cycles ----------", "rmt peer", 2),
1772 .name = "mean_rmt_peer",
1774 .entry = mean_rmt_peer_entry,
1778 static struct c2c_dimension dim_mean_lcl_peer = {
1779 .header = HEADER_SPAN_LOW("lcl peer"),
1780 .name = "mean_lcl_peer",
1782 .entry = mean_lcl_peer_entry,
1786 static struct c2c_dimension dim_cpucnt = {
1787 .header = HEADER_BOTH("cpu", "cnt"),
1790 .entry = cpucnt_entry,
1794 static struct c2c_dimension dim_srcline = {
1795 .name = "cl_srcline",
1796 .se = &sort_srcline,
1799 static struct c2c_dimension dim_dcacheline_idx = {
1800 .header = HEADER_LOW("Index"),
1803 .entry = cl_idx_entry,
1807 static struct c2c_dimension dim_dcacheline_num = {
1808 .header = HEADER_LOW("Num"),
1811 .entry = cl_idx_entry,
1815 static struct c2c_dimension dim_dcacheline_num_empty = {
1816 .header = HEADER_LOW("Num"),
1817 .name = "cl_num_empty",
1819 .entry = cl_idx_empty_entry,
1823 static struct c2c_dimension *dimensions[] = {
1825 &dim_dcacheline_node,
1826 &dim_dcacheline_count,
1844 &dim_cl_stores_l1hit,
1845 &dim_cl_stores_l1miss,
1854 &dim_percent_costly_snoop,
1855 &dim_percent_rmt_hitm,
1856 &dim_percent_lcl_hitm,
1857 &dim_percent_rmt_peer,
1858 &dim_percent_lcl_peer,
1859 &dim_percent_stores_l1hit,
1860 &dim_percent_stores_l1miss,
1861 &dim_percent_stores_na,
1876 &dim_dcacheline_idx,
1877 &dim_dcacheline_num,
1878 &dim_dcacheline_num_empty,
1882 static void fmt_free(struct perf_hpp_fmt *fmt)
1884 struct c2c_fmt *c2c_fmt;
1886 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1890 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1892 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1893 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1895 return c2c_a->dim == c2c_b->dim;
1898 static struct c2c_dimension *get_dimension(const char *name)
1902 for (i = 0; dimensions[i]; i++) {
1903 struct c2c_dimension *dim = dimensions[i];
1905 if (!strcmp(dim->name, name))
1912 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1913 struct hist_entry *he)
1915 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1916 struct c2c_dimension *dim = c2c_fmt->dim;
1917 size_t len = fmt->user_len;
1920 len = hists__col_len(he->hists, dim->se->se_width_idx);
1922 if (dim == &dim_symbol || dim == &dim_srcline)
1923 len = symbol_width(he->hists, dim->se);
1926 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1929 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1930 struct hist_entry *a, struct hist_entry *b)
1932 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1933 struct c2c_dimension *dim = c2c_fmt->dim;
1935 return dim->se->se_cmp(a, b);
1938 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1939 struct hist_entry *a, struct hist_entry *b)
1941 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1942 struct c2c_dimension *dim = c2c_fmt->dim;
1943 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1945 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1946 return collapse_fn(a, b);
1949 static struct c2c_fmt *get_format(const char *name)
1951 struct c2c_dimension *dim = get_dimension(name);
1952 struct c2c_fmt *c2c_fmt;
1953 struct perf_hpp_fmt *fmt;
1958 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1964 fmt = &c2c_fmt->fmt;
1965 INIT_LIST_HEAD(&fmt->list);
1966 INIT_LIST_HEAD(&fmt->sort_list);
1968 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1969 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1970 fmt->color = dim->se ? NULL : dim->color;
1971 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1972 fmt->header = c2c_header;
1973 fmt->width = c2c_width;
1974 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1975 fmt->equal = fmt_equal;
1976 fmt->free = fmt_free;
1981 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1983 struct c2c_fmt *c2c_fmt = get_format(name);
1987 return output_field_add(hpp_list, name);
1990 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1994 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1996 struct c2c_fmt *c2c_fmt = get_format(name);
1997 struct c2c_dimension *dim;
2001 return sort_dimension__add(hpp_list, name, NULL, 0);
2005 if (dim == &dim_dso)
2008 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
2012 #define PARSE_LIST(_list, _fn) \
2020 for (tok = strtok_r((char *)_list, ", ", &tmp); \
2021 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
2022 ret = _fn(hpp_list, tok); \
2023 if (ret == -EINVAL) { \
2024 pr_err("Invalid --fields key: `%s'", tok); \
2026 } else if (ret == -ESRCH) { \
2027 pr_err("Unknown --fields key: `%s'", tok); \
2033 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
2034 const char *output_,
2037 char *output = output_ ? strdup(output_) : NULL;
2038 char *sort = sort_ ? strdup(sort_) : NULL;
2041 PARSE_LIST(output, c2c_hists__init_output);
2042 PARSE_LIST(sort, c2c_hists__init_sort);
2044 /* copy sort keys to output fields */
2045 perf_hpp__setup_output_field(hpp_list);
2048 * We dont need other sorting keys other than those
2049 * we already specified. It also really slows down
2050 * the processing a lot with big number of output
2051 * fields, so switching this off for c2c.
2055 /* and then copy output fields to sort keys */
2056 perf_hpp__append_sort_keys(&hists->list);
2064 static int c2c_hists__init(struct c2c_hists *hists,
2066 int nr_header_lines)
2068 __hists__init(&hists->hists, &hists->list);
2071 * Initialize only with sort fields, we need to resort
2072 * later anyway, and that's where we add output fields
2075 perf_hpp_list__init(&hists->list);
2077 /* Overload number of header lines.*/
2078 hists->list.nr_header_lines = nr_header_lines;
2080 return hpp_list__parse(&hists->list, NULL, sort);
2083 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
2087 perf_hpp__reset_output_field(&c2c_hists->list);
2088 return hpp_list__parse(&c2c_hists->list, output, sort);
2091 #define DISPLAY_LINE_LIMIT 0.001
2093 static u8 filter_display(u32 val, u32 sum)
2095 if (sum == 0 || ((double)val / sum) < DISPLAY_LINE_LIMIT)
2096 return HIST_FILTER__C2C;
2101 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
2103 struct c2c_hist_entry *c2c_he;
2108 c2c_he = container_of(he, struct c2c_hist_entry, he);
2110 switch (c2c.display) {
2111 case DISPLAY_LCL_HITM:
2112 he->filtered = filter_display(c2c_he->stats.lcl_hitm,
2115 case DISPLAY_RMT_HITM:
2116 he->filtered = filter_display(c2c_he->stats.rmt_hitm,
2119 case DISPLAY_TOT_HITM:
2120 he->filtered = filter_display(c2c_he->stats.tot_hitm,
2123 case DISPLAY_SNP_PEER:
2124 he->filtered = filter_display(c2c_he->stats.tot_peer,
2131 return he->filtered == 0;
2134 static inline bool is_valid_hist_entry(struct hist_entry *he)
2136 struct c2c_hist_entry *c2c_he;
2137 bool has_record = false;
2139 c2c_he = container_of(he, struct c2c_hist_entry, he);
2141 /* It's a valid entry if contains stores */
2142 if (c2c_he->stats.store)
2145 switch (c2c.display) {
2146 case DISPLAY_LCL_HITM:
2147 has_record = !!c2c_he->stats.lcl_hitm;
2149 case DISPLAY_RMT_HITM:
2150 has_record = !!c2c_he->stats.rmt_hitm;
2152 case DISPLAY_TOT_HITM:
2153 has_record = !!c2c_he->stats.tot_hitm;
2155 case DISPLAY_SNP_PEER:
2156 has_record = !!c2c_he->stats.tot_peer;
2164 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
2166 struct c2c_dimension *dim;
2168 dim = &c2c.hists == c2c_he->hists ?
2169 &dim_dcacheline_node : &dim_offset_node;
2171 if (len > dim->width)
2175 static int set_nodestr(struct c2c_hist_entry *c2c_he)
2180 if (c2c_he->nodestr)
2183 if (!bitmap_empty(c2c_he->nodeset, c2c.nodes_cnt)) {
2184 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
2187 len = scnprintf(buf, sizeof(buf), "N/A");
2190 set_node_width(c2c_he, len);
2191 c2c_he->nodestr = strdup(buf);
2192 return c2c_he->nodestr ? 0 : -ENOMEM;
2195 static void calc_width(struct c2c_hist_entry *c2c_he)
2197 struct c2c_hists *c2c_hists;
2199 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
2200 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
2201 set_nodestr(c2c_he);
2204 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
2206 struct c2c_hist_entry *c2c_he;
2208 c2c_he = container_of(he, struct c2c_hist_entry, he);
2210 if (c2c.show_src && !he->srcline)
2211 he->srcline = hist_entry__srcline(he);
2215 if (!is_valid_hist_entry(he))
2216 he->filtered = HIST_FILTER__C2C;
2221 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
2223 struct c2c_hist_entry *c2c_he;
2224 struct c2c_hists *c2c_hists;
2225 bool display = he__display(he, &c2c.shared_clines_stats);
2227 c2c_he = container_of(he, struct c2c_hist_entry, he);
2228 c2c_hists = c2c_he->hists;
2230 if (display && c2c_hists) {
2231 static unsigned int idx;
2233 c2c_he->cacheline_idx = idx++;
2236 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2238 hists__collapse_resort(&c2c_hists->hists, NULL);
2239 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2245 static struct c2c_header header_node_0 = HEADER_LOW("Node");
2246 static struct c2c_header header_node_1_hitms_stores =
2247 HEADER_LOW("Node{cpus %hitms %stores}");
2248 static struct c2c_header header_node_1_peers_stores =
2249 HEADER_LOW("Node{cpus %peers %stores}");
2250 static struct c2c_header header_node_2 = HEADER_LOW("Node{cpu list}");
2252 static void setup_nodes_header(void)
2254 switch (c2c.node_info) {
2256 dim_node.header = header_node_0;
2259 if (c2c.display == DISPLAY_SNP_PEER)
2260 dim_node.header = header_node_1_peers_stores;
2262 dim_node.header = header_node_1_hitms_stores;
2265 dim_node.header = header_node_2;
2274 static int setup_nodes(struct perf_session *session)
2276 struct numa_node *n;
2277 unsigned long **nodes;
2279 struct perf_cpu cpu;
2282 if (c2c.node_info > 2)
2285 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2286 c2c.cpus_cnt = session->header.env.nr_cpus_avail;
2288 n = session->header.env.numa_nodes;
2292 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2298 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2302 for (idx = 0; idx < c2c.cpus_cnt; idx++)
2305 c2c.cpu2node = cpu2node;
2307 for (node = 0; node < c2c.nodes_cnt; node++) {
2308 struct perf_cpu_map *map = n[node].map;
2311 set = bitmap_zalloc(c2c.cpus_cnt);
2317 /* empty node, skip */
2318 if (perf_cpu_map__empty(map))
2321 perf_cpu_map__for_each_cpu(cpu, idx, map) {
2322 __set_bit(cpu.cpu, set);
2324 if (WARN_ONCE(cpu2node[cpu.cpu] != -1, "node/cpu topology bug"))
2327 cpu2node[cpu.cpu] = node;
2331 setup_nodes_header();
2335 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2336 #define HAS_PEER(__h) ((__h)->stats.lcl_peer || (__h)->stats.rmt_peer)
2338 static int resort_shared_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
2340 struct c2c_hist_entry *c2c_he;
2341 c2c_he = container_of(he, struct c2c_hist_entry, he);
2343 if (HAS_HITMS(c2c_he) || HAS_PEER(c2c_he)) {
2344 c2c.shared_clines++;
2345 c2c_add_stats(&c2c.shared_clines_stats, &c2c_he->stats);
2351 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2353 struct rb_node *next = rb_first_cached(&hists->entries);
2357 struct hist_entry *he;
2359 he = rb_entry(next, struct hist_entry, rb_node);
2363 next = rb_next(&he->rb_node);
2369 static void print_c2c__display_stats(FILE *out)
2372 struct c2c_stats *stats = &c2c.hists.stats;
2374 llc_misses = get_load_llc_misses(stats);
2376 fprintf(out, "=================================================\n");
2377 fprintf(out, " Trace Event Information \n");
2378 fprintf(out, "=================================================\n");
2379 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2380 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2381 fprintf(out, " Load Operations : %10d\n", stats->load);
2382 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2383 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2384 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2385 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2386 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2387 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2388 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2389 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2390 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2391 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2392 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2393 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2394 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2395 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2396 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2397 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2398 fprintf(out, " Load access blocked by data : %10d\n", stats->blk_data);
2399 fprintf(out, " Load access blocked by address : %10d\n", stats->blk_addr);
2400 fprintf(out, " Load HIT Local Peer : %10d\n", stats->lcl_peer);
2401 fprintf(out, " Load HIT Remote Peer : %10d\n", stats->rmt_peer);
2402 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2403 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2404 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2405 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2406 fprintf(out, " Store Operations : %10d\n", stats->store);
2407 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2408 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2409 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2410 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2411 fprintf(out, " Store No available memory level : %10d\n", stats->st_na);
2412 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2413 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2416 static void print_shared_cacheline_info(FILE *out)
2418 struct c2c_stats *stats = &c2c.shared_clines_stats;
2419 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2421 fprintf(out, "=================================================\n");
2422 fprintf(out, " Global Shared Cache Line Event Information \n");
2423 fprintf(out, "=================================================\n");
2424 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2425 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2426 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2427 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2428 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2429 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2430 fprintf(out, " Load hits on peer cache or nodes : %10d\n", stats->lcl_peer + stats->rmt_peer);
2431 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2432 fprintf(out, " Blocked Access on shared lines : %10d\n", stats->blk_data + stats->blk_addr);
2433 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2434 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2435 fprintf(out, " Store No available memory level : %10d\n", stats->st_na);
2436 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2439 static void print_cacheline(struct c2c_hists *c2c_hists,
2440 struct hist_entry *he_cl,
2441 struct perf_hpp_list *hpp_list,
2445 struct perf_hpp hpp = {
2452 hists__fprintf_headers(&c2c_hists->hists, out);
2458 fprintf(out, " ----------------------------------------------------------------------\n");
2459 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2460 fprintf(out, "%s\n", bf);
2461 fprintf(out, " ----------------------------------------------------------------------\n");
2463 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2466 static void print_pareto(FILE *out)
2468 struct perf_hpp_list hpp_list;
2471 const char *cl_output;
2473 if (c2c.display != DISPLAY_SNP_PEER)
2474 cl_output = "cl_num,"
2482 cl_output = "cl_num,"
2490 perf_hpp_list__init(&hpp_list);
2491 ret = hpp_list__parse(&hpp_list, cl_output, NULL);
2493 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2496 nd = rb_first_cached(&c2c.hists.hists.entries);
2498 for (; nd; nd = rb_next(nd)) {
2499 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2500 struct c2c_hist_entry *c2c_he;
2505 c2c_he = container_of(he, struct c2c_hist_entry, he);
2506 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2510 static void print_c2c_info(FILE *out, struct perf_session *session)
2512 struct evlist *evlist = session->evlist;
2513 struct evsel *evsel;
2516 fprintf(out, "=================================================\n");
2517 fprintf(out, " c2c details \n");
2518 fprintf(out, "=================================================\n");
2520 evlist__for_each_entry(evlist, evsel) {
2521 fprintf(out, "%-36s: %s\n", first ? " Events" : "", evsel__name(evsel));
2524 fprintf(out, " Cachelines sort on : %s\n",
2525 display_str[c2c.display]);
2526 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2529 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2533 print_c2c__display_stats(out);
2535 print_shared_cacheline_info(out);
2537 print_c2c_info(out, session);
2543 fprintf(out, "=================================================\n");
2544 fprintf(out, " Shared Data Cache Line Table \n");
2545 fprintf(out, "=================================================\n");
2546 fprintf(out, "#\n");
2548 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2551 fprintf(out, "=================================================\n");
2552 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2553 fprintf(out, "=================================================\n");
2554 fprintf(out, "#\n");
2559 #ifdef HAVE_SLANG_SUPPORT
2560 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2563 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2566 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2574 hb->nr_non_filtered_entries = nr_entries;
2577 struct c2c_cacheline_browser {
2578 struct hist_browser hb;
2579 struct hist_entry *he;
2583 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2584 char *bf, size_t size)
2586 struct c2c_cacheline_browser *cl_browser;
2587 struct hist_entry *he;
2590 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2591 he = cl_browser->he;
2594 addr = cl_address(he->mem_info->daddr.addr, chk_double_cl);
2596 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2600 static struct c2c_cacheline_browser*
2601 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2603 struct c2c_cacheline_browser *browser;
2605 browser = zalloc(sizeof(*browser));
2607 hist_browser__init(&browser->hb, hists);
2608 browser->hb.c2c_filter = true;
2609 browser->hb.title = perf_c2c_cacheline_browser__title;
2616 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2618 struct c2c_hist_entry *c2c_he;
2619 struct c2c_hists *c2c_hists;
2620 struct c2c_cacheline_browser *cl_browser;
2621 struct hist_browser *browser;
2623 static const char help[] =
2624 " ENTER Toggle callchains (if present) \n"
2625 " n Toggle Node details info \n"
2626 " s Toggle full length of symbol and source line columns \n"
2627 " q Return back to cacheline list \n";
2632 /* Display compact version first. */
2633 c2c.symbol_full = false;
2635 c2c_he = container_of(he, struct c2c_hist_entry, he);
2636 c2c_hists = c2c_he->hists;
2638 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2639 if (cl_browser == NULL)
2642 browser = &cl_browser->hb;
2644 /* reset abort key so that it can get Ctrl-C as a key */
2646 SLang_init_tty(0, 0, 0);
2648 c2c_browser__update_nr_entries(browser);
2651 key = hist_browser__run(browser, "? - help", true, 0);
2655 c2c.symbol_full = !c2c.symbol_full;
2658 c2c.node_info = (c2c.node_info + 1) % 3;
2659 setup_nodes_header();
2664 ui_browser__help_window(&browser->b, help);
2676 static int perf_c2c_browser__title(struct hist_browser *browser,
2677 char *bf, size_t size)
2680 "Shared Data Cache Line Table "
2681 "(%lu entries, sorted on %s)",
2682 browser->nr_non_filtered_entries,
2683 display_str[c2c.display]);
2687 static struct hist_browser*
2688 perf_c2c_browser__new(struct hists *hists)
2690 struct hist_browser *browser = hist_browser__new(hists);
2693 browser->title = perf_c2c_browser__title;
2694 browser->c2c_filter = true;
2700 static int perf_c2c__hists_browse(struct hists *hists)
2702 struct hist_browser *browser;
2704 static const char help[] =
2705 " d Display cacheline details \n"
2706 " ENTER Toggle callchains (if present) \n"
2709 browser = perf_c2c_browser__new(hists);
2710 if (browser == NULL)
2713 /* reset abort key so that it can get Ctrl-C as a key */
2715 SLang_init_tty(0, 0, 0);
2717 c2c_browser__update_nr_entries(browser);
2720 key = hist_browser__run(browser, "? - help", true, 0);
2726 perf_c2c__browse_cacheline(browser->he_selection);
2729 ui_browser__help_window(&browser->b, help);
2737 hist_browser__delete(browser);
2741 static void perf_c2c_display(struct perf_session *session)
2743 if (use_browser == 0)
2744 perf_c2c__hists_fprintf(stdout, session);
2746 perf_c2c__hists_browse(&c2c.hists.hists);
2749 static void perf_c2c_display(struct perf_session *session)
2752 perf_c2c__hists_fprintf(stdout, session);
2754 #endif /* HAVE_SLANG_SUPPORT */
2756 static char *fill_line(const char *orig, int len)
2758 int i, j, olen = strlen(orig);
2761 buf = zalloc(len + 1);
2765 j = len / 2 - olen / 2;
2767 for (i = 0; i < j - 1; i++)
2772 strcpy(buf + i, orig);
2778 for (; i < len; i++)
2784 static int ui_quirks(void)
2786 const char *nodestr = "Data address";
2789 if (!c2c.use_stdio) {
2790 dim_offset.width = 5;
2791 dim_offset.header = header_offset_tui;
2792 nodestr = chk_double_cl ? "Double-CL" : "CL";
2795 dim_percent_costly_snoop.header = percent_costly_snoop_header[c2c.display];
2797 /* Fix the zero line for dcacheline column. */
2798 buf = fill_line(chk_double_cl ? "Double-Cacheline" : "Cacheline",
2799 dim_dcacheline.width +
2800 dim_dcacheline_node.width +
2801 dim_dcacheline_count.width + 4);
2805 dim_dcacheline.header.line[0].text = buf;
2807 /* Fix the zero line for offset column. */
2808 buf = fill_line(nodestr, dim_offset.width +
2809 dim_offset_node.width +
2810 dim_dcacheline_count.width + 4);
2814 dim_offset.header.line[0].text = buf;
2819 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2821 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2822 CALLCHAIN_REPORT_HELP
2823 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2826 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2828 struct callchain_param *callchain = opt->value;
2830 callchain->enabled = !unset;
2835 symbol_conf.use_callchain = false;
2836 callchain->mode = CHAIN_NONE;
2840 return parse_callchain_report_opt(arg);
2843 static int setup_callchain(struct evlist *evlist)
2845 u64 sample_type = evlist__combined_sample_type(evlist);
2846 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2848 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2849 (sample_type & PERF_SAMPLE_STACK_USER)) {
2850 mode = CALLCHAIN_DWARF;
2851 dwarf_callchain_users = true;
2852 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2853 mode = CALLCHAIN_LBR;
2854 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2855 mode = CALLCHAIN_FP;
2857 if (!callchain_param.enabled &&
2858 callchain_param.mode != CHAIN_NONE &&
2859 mode != CALLCHAIN_NONE) {
2860 symbol_conf.use_callchain = true;
2861 if (callchain_register_param(&callchain_param) < 0) {
2862 ui__error("Can't register callchain params.\n");
2867 if (c2c.stitch_lbr && (mode != CALLCHAIN_LBR)) {
2868 ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
2869 "Please apply --call-graph lbr when recording.\n");
2870 c2c.stitch_lbr = false;
2873 callchain_param.record_mode = mode;
2874 callchain_param.min_percent = 0;
2878 static int setup_display(const char *str)
2880 const char *display = str;
2882 if (!strcmp(display, "tot"))
2883 c2c.display = DISPLAY_TOT_HITM;
2884 else if (!strcmp(display, "rmt"))
2885 c2c.display = DISPLAY_RMT_HITM;
2886 else if (!strcmp(display, "lcl"))
2887 c2c.display = DISPLAY_LCL_HITM;
2888 else if (!strcmp(display, "peer"))
2889 c2c.display = DISPLAY_SNP_PEER;
2891 pr_err("failed: unknown display type: %s\n", str);
2898 #define for_each_token(__tok, __buf, __sep, __tmp) \
2899 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2900 __tok = strtok_r(NULL, __sep, &__tmp))
2902 static int build_cl_output(char *cl_sort, bool no_source)
2904 char *tok, *tmp, *buf = strdup(cl_sort);
2905 bool add_pid = false;
2906 bool add_tid = false;
2907 bool add_iaddr = false;
2908 bool add_sym = false;
2909 bool add_dso = false;
2910 bool add_src = false;
2916 for_each_token(tok, buf, ",", tmp) {
2917 if (!strcmp(tok, "tid")) {
2919 } else if (!strcmp(tok, "pid")) {
2921 } else if (!strcmp(tok, "iaddr")) {
2925 add_src = no_source ? false : true;
2926 } else if (!strcmp(tok, "dso")) {
2928 } else if (strcmp(tok, "offset")) {
2929 pr_err("unrecognized sort token: %s\n", tok);
2935 if (asprintf(&c2c.cl_output,
2936 "%s%s%s%s%s%s%s%s%s%s%s%s",
2937 c2c.use_stdio ? "cl_num_empty," : "",
2938 c2c.display == DISPLAY_SNP_PEER ? "percent_rmt_peer,"
2939 "percent_lcl_peer," :
2941 "percent_lcl_hitm,",
2942 "percent_stores_l1hit,"
2943 "percent_stores_l1miss,"
2944 "percent_stores_na,"
2945 "offset,offset_node,dcacheline_count,",
2946 add_pid ? "pid," : "",
2947 add_tid ? "tid," : "",
2948 add_iaddr ? "iaddr," : "",
2949 c2c.display == DISPLAY_SNP_PEER ? "mean_rmt_peer,"
2956 add_sym ? "symbol," : "",
2957 add_dso ? "dso," : "",
2958 add_src ? "cl_srcline," : "",
2964 c2c.show_src = add_src;
2970 static int setup_coalesce(const char *coalesce, bool no_source)
2972 const char *c = coalesce ?: coalesce_default;
2973 const char *sort_str = NULL;
2975 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2978 if (build_cl_output(c2c.cl_sort, no_source))
2981 if (c2c.display == DISPLAY_TOT_HITM)
2982 sort_str = "tot_hitm";
2983 else if (c2c.display == DISPLAY_RMT_HITM)
2984 sort_str = "rmt_hitm,lcl_hitm";
2985 else if (c2c.display == DISPLAY_LCL_HITM)
2986 sort_str = "lcl_hitm,rmt_hitm";
2987 else if (c2c.display == DISPLAY_SNP_PEER)
2988 sort_str = "tot_peer";
2990 if (asprintf(&c2c.cl_resort, "offset,%s", sort_str) < 0)
2993 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2994 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2995 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2999 static int perf_c2c__report(int argc, const char **argv)
3001 struct itrace_synth_opts itrace_synth_opts = {
3003 .mem = true, /* Only enable memory event */
3004 .default_no_sample = true,
3007 struct perf_session *session;
3008 struct ui_progress prog;
3009 struct perf_data data = {
3010 .mode = PERF_DATA_MODE_READ,
3012 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
3013 const char *display = NULL;
3014 const char *coalesce = NULL;
3015 bool no_source = false;
3016 const struct option options[] = {
3017 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3018 "file", "vmlinux pathname"),
3019 OPT_STRING('i', "input", &input_name, "file",
3020 "the input file to process"),
3021 OPT_INCR('N', "node-info", &c2c.node_info,
3022 "show extra node info in report (repeat for more info)"),
3023 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
3024 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
3025 "Display only statistic tables (implies --stdio)"),
3026 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
3027 "Display full length of symbols"),
3028 OPT_BOOLEAN(0, "no-source", &no_source,
3029 "Do not display Source Line column"),
3030 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
3031 "Show all captured HITM lines."),
3032 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
3033 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
3034 callchain_help, &parse_callchain_opt,
3035 callchain_default_opt),
3036 OPT_STRING('d', "display", &display, "Switch HITM output type", "tot,lcl,rmt,peer"),
3037 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
3038 "coalesce fields: pid,tid,iaddr,dso"),
3039 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3040 OPT_BOOLEAN(0, "stitch-lbr", &c2c.stitch_lbr,
3041 "Enable LBR callgraph stitching approach"),
3042 OPT_BOOLEAN(0, "double-cl", &chk_double_cl, "Detect adjacent cacheline false sharing"),
3043 OPT_PARENT(c2c_options),
3047 const char *output_str, *sort_str = NULL;
3049 argc = parse_options(argc, argv, options, report_c2c_usage,
3050 PARSE_OPT_STOP_AT_NON_OPTION);
3052 usage_with_options(report_c2c_usage, options);
3054 #ifndef HAVE_SLANG_SUPPORT
3055 c2c.use_stdio = true;
3059 c2c.use_stdio = true;
3061 err = symbol__validate_sym_arguments();
3065 if (!input_name || !strlen(input_name))
3066 input_name = "perf.data";
3068 data.path = input_name;
3069 data.force = symbol_conf.force;
3071 session = perf_session__new(&data, &c2c.tool);
3072 if (IS_ERR(session)) {
3073 err = PTR_ERR(session);
3074 pr_debug("Error creating perf session\n");
3079 * Use the 'tot' as default display type if user doesn't specify it;
3080 * since Arm64 platform doesn't support HITMs flag, use 'peer' as the
3081 * default display type.
3084 if (!strcmp(perf_env__arch(&session->header.env), "arm64"))
3090 err = setup_display(display);
3094 err = setup_coalesce(coalesce, no_source);
3096 pr_debug("Failed to initialize hists\n");
3100 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
3102 pr_debug("Failed to initialize hists\n");
3106 session->itrace_synth_opts = &itrace_synth_opts;
3108 err = setup_nodes(session);
3110 pr_err("Failed setup nodes\n");
3114 err = mem2node__init(&c2c.mem2node, &session->header.env);
3118 err = setup_callchain(session->evlist);
3122 if (symbol__init(&session->header.env) < 0)
3125 /* No pipe support at the moment. */
3126 if (perf_data__is_pipe(session->data)) {
3127 pr_debug("No pipe support at the moment.\n");
3136 setup_browser(false);
3138 err = perf_session__process_events(session);
3140 pr_err("failed to process sample\n");
3144 if (c2c.display != DISPLAY_SNP_PEER)
3145 output_str = "cl_idx,"
3149 "percent_costly_snoop,"
3150 "tot_hitm,lcl_hitm,rmt_hitm,"
3154 "stores_l1hit,stores_l1miss,stores_na,"
3155 "ld_fbhit,ld_l1hit,ld_l2hit,"
3156 "ld_lclhit,lcl_hitm,"
3157 "ld_rmthit,rmt_hitm,"
3158 "dram_lcl,dram_rmt";
3160 output_str = "cl_idx,"
3164 "percent_costly_snoop,"
3165 "tot_peer,lcl_peer,rmt_peer,"
3169 "stores_l1hit,stores_l1miss,stores_na,"
3170 "ld_fbhit,ld_l1hit,ld_l2hit,"
3171 "ld_lclhit,lcl_hitm,"
3172 "ld_rmthit,rmt_hitm,"
3173 "dram_lcl,dram_rmt";
3175 if (c2c.display == DISPLAY_TOT_HITM)
3176 sort_str = "tot_hitm";
3177 else if (c2c.display == DISPLAY_RMT_HITM)
3178 sort_str = "rmt_hitm";
3179 else if (c2c.display == DISPLAY_LCL_HITM)
3180 sort_str = "lcl_hitm";
3181 else if (c2c.display == DISPLAY_SNP_PEER)
3182 sort_str = "tot_peer";
3184 c2c_hists__reinit(&c2c.hists, output_str, sort_str);
3186 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
3188 hists__collapse_resort(&c2c.hists.hists, NULL);
3189 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_shared_cl_cb);
3190 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
3192 ui_progress__finish();
3195 pr_err("failed to setup UI\n");
3199 perf_c2c_display(session);
3202 mem2node__exit(&c2c.mem2node);
3204 perf_session__delete(session);
3209 static int parse_record_events(const struct option *opt,
3210 const char *str, int unset __maybe_unused)
3212 bool *event_set = (bool *) opt->value;
3214 if (!strcmp(str, "list")) {
3215 perf_mem_events__list();
3218 if (perf_mem_events__parse(str))
3226 static const char * const __usage_record[] = {
3227 "perf c2c record [<options>] [<command>]",
3228 "perf c2c record [<options>] -- <command> [<options>]",
3232 static const char * const *record_mem_usage = __usage_record;
3234 static int perf_c2c__record(int argc, const char **argv)
3236 int rec_argc, i = 0, j, rec_tmp_nr = 0;
3237 const char **rec_argv;
3240 bool all_user = false, all_kernel = false;
3241 bool event_set = false;
3242 struct perf_mem_event *e;
3243 struct option options[] = {
3244 OPT_CALLBACK('e', "event", &event_set, "event",
3245 "event selector. Use 'perf c2c record -e list' to list available events",
3246 parse_record_events),
3247 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
3248 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
3249 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
3250 OPT_PARENT(c2c_options),
3254 if (perf_mem_events__init()) {
3255 pr_err("failed: memory events not supported\n");
3259 argc = parse_options(argc, argv, options, record_mem_usage,
3260 PARSE_OPT_KEEP_UNKNOWN);
3262 if (!perf_pmu__has_hybrid())
3263 rec_argc = argc + 11; /* max number of arguments */
3265 rec_argc = argc + 11 * perf_pmu__hybrid_pmu_num();
3267 rec_argv = calloc(rec_argc + 1, sizeof(char *));
3271 rec_tmp = calloc(rec_argc + 1, sizeof(char *));
3277 rec_argv[i++] = "record";
3280 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD_STORE);
3282 * The load and store operations are required, use the event
3283 * PERF_MEM_EVENTS__LOAD_STORE if it is supported.
3287 rec_argv[i++] = "-W";
3289 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
3292 e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE);
3297 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
3299 rec_argv[i++] = "-W";
3301 rec_argv[i++] = "-d";
3302 rec_argv[i++] = "--phys-data";
3303 rec_argv[i++] = "--sample-cpu";
3305 ret = perf_mem_events__record_args(rec_argv, &i, rec_tmp, &rec_tmp_nr);
3310 rec_argv[i++] = "--all-user";
3313 rec_argv[i++] = "--all-kernel";
3315 for (j = 0; j < argc; j++, i++)
3316 rec_argv[i] = argv[j];
3319 pr_debug("calling: ");
3323 while (rec_argv[j]) {
3324 pr_debug("%s ", rec_argv[j]);
3330 ret = cmd_record(i, rec_argv);
3332 for (i = 0; i < rec_tmp_nr; i++)
3340 int cmd_c2c(int argc, const char **argv)
3342 argc = parse_options(argc, argv, c2c_options, c2c_usage,
3343 PARSE_OPT_STOP_AT_NON_OPTION);
3346 usage_with_options(c2c_usage, c2c_options);
3348 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3349 return perf_c2c__record(argc, argv);
3350 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
3351 return perf_c2c__report(argc, argv);
3353 usage_with_options(c2c_usage, c2c_options);