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;
61 struct c2c_hist_entry {
62 struct c2c_hists *hists;
63 struct c2c_stats stats;
64 unsigned long *cpuset;
65 unsigned long *nodeset;
66 struct c2c_stats *node_stats;
67 unsigned int cacheline_idx;
69 struct compute_stats cstats;
72 unsigned long paddr_cnt;
78 * because of its callchain dynamic entry
83 static char const *coalesce_default = "iaddr";
86 struct perf_tool tool;
87 struct c2c_hists hists;
88 struct mem2node mem2node;
90 unsigned long **nodes;
103 /* Shared cache line stats */
104 struct c2c_stats shared_clines_stats;
109 const char *coalesce;
122 static const char *display_str[DISPLAY_MAX] = {
123 [DISPLAY_LCL] = "Local",
124 [DISPLAY_RMT] = "Remote",
125 [DISPLAY_TOT] = "Total",
128 static const struct option c2c_options[] = {
129 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
133 static struct perf_c2c c2c;
135 static void *c2c_he_zalloc(size_t size)
137 struct c2c_hist_entry *c2c_he;
139 c2c_he = zalloc(size + sizeof(*c2c_he));
143 c2c_he->cpuset = bitmap_zalloc(c2c.cpus_cnt);
147 c2c_he->nodeset = bitmap_zalloc(c2c.nodes_cnt);
148 if (!c2c_he->nodeset)
151 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
152 if (!c2c_he->node_stats)
155 init_stats(&c2c_he->cstats.lcl_hitm);
156 init_stats(&c2c_he->cstats.rmt_hitm);
157 init_stats(&c2c_he->cstats.load);
162 static void c2c_he_free(void *he)
164 struct c2c_hist_entry *c2c_he;
166 c2c_he = container_of(he, struct c2c_hist_entry, he);
168 hists__delete_entries(&c2c_he->hists->hists);
172 free(c2c_he->cpuset);
173 free(c2c_he->nodeset);
174 free(c2c_he->nodestr);
175 free(c2c_he->node_stats);
179 static struct hist_entry_ops c2c_entry_ops = {
180 .new = c2c_he_zalloc,
184 static int c2c_hists__init(struct c2c_hists *hists,
186 int nr_header_lines);
188 static struct c2c_hists*
189 he__get_c2c_hists(struct hist_entry *he,
193 struct c2c_hist_entry *c2c_he;
194 struct c2c_hists *hists;
197 c2c_he = container_of(he, struct c2c_hist_entry, he);
199 return c2c_he->hists;
201 hists = c2c_he->hists = zalloc(sizeof(*hists));
205 ret = c2c_hists__init(hists, sort, nr_header_lines);
214 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
215 struct perf_sample *sample)
217 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
218 "WARNING: no sample cpu value"))
221 set_bit(sample->cpu, c2c_he->cpuset);
224 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
225 struct perf_sample *sample)
229 if (!sample->phys_addr) {
230 c2c_he->paddr_zero = true;
234 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
235 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
238 set_bit(node, c2c_he->nodeset);
240 if (c2c_he->paddr != sample->phys_addr) {
242 c2c_he->paddr = sample->phys_addr;
246 static void compute_stats(struct c2c_hist_entry *c2c_he,
247 struct c2c_stats *stats,
250 struct compute_stats *cstats = &c2c_he->cstats;
253 update_stats(&cstats->rmt_hitm, weight);
254 else if (stats->lcl_hitm)
255 update_stats(&cstats->lcl_hitm, weight);
256 else if (stats->load)
257 update_stats(&cstats->load, weight);
260 static int process_sample_event(struct perf_tool *tool __maybe_unused,
261 union perf_event *event,
262 struct perf_sample *sample,
264 struct machine *machine)
266 struct c2c_hists *c2c_hists = &c2c.hists;
267 struct c2c_hist_entry *c2c_he;
268 struct c2c_stats stats = { .nr_entries = 0, };
269 struct hist_entry *he;
270 struct addr_location al;
271 struct mem_info *mi, *mi_dup;
274 if (machine__resolve(machine, &al, sample) < 0) {
275 pr_debug("problem processing %d event, skipping it.\n",
281 al.thread->lbr_stitch_enable = true;
283 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
284 evsel, &al, sysctl_perf_event_max_stack);
288 mi = sample__resolve_mem(sample, &al);
293 * The mi object is released in hists__add_entry_ops,
294 * if it gets sorted out into existing data, so we need
295 * to take the copy now.
297 mi_dup = mem_info__get(mi);
299 c2c_decode_stats(&stats, mi);
301 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
307 c2c_he = container_of(he, struct c2c_hist_entry, he);
308 c2c_add_stats(&c2c_he->stats, &stats);
309 c2c_add_stats(&c2c_hists->stats, &stats);
311 c2c_he__set_cpu(c2c_he, sample);
312 c2c_he__set_node(c2c_he, sample);
314 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
315 ret = hist_entry__append_callchain(he, sample);
319 * There's already been warning about missing
320 * sample's cpu value. Let's account all to
321 * node 0 in this case, without any further
324 * Doing node stats only for single callchain data.
326 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
327 int node = c2c.cpu2node[cpu];
331 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
335 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
341 c2c_he = container_of(he, struct c2c_hist_entry, he);
342 c2c_add_stats(&c2c_he->stats, &stats);
343 c2c_add_stats(&c2c_hists->stats, &stats);
344 c2c_add_stats(&c2c_he->node_stats[node], &stats);
346 compute_stats(c2c_he, &stats, sample->weight);
348 c2c_he__set_cpu(c2c_he, sample);
349 c2c_he__set_node(c2c_he, sample);
351 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
352 ret = hist_entry__append_callchain(he, sample);
356 addr_location__put(&al);
360 mem_info__put(mi_dup);
366 static struct perf_c2c c2c = {
368 .sample = process_sample_event,
369 .mmap = perf_event__process_mmap,
370 .mmap2 = perf_event__process_mmap2,
371 .comm = perf_event__process_comm,
372 .exit = perf_event__process_exit,
373 .fork = perf_event__process_fork,
374 .lost = perf_event__process_lost,
375 .attr = perf_event__process_attr,
376 .auxtrace_info = perf_event__process_auxtrace_info,
377 .auxtrace = perf_event__process_auxtrace,
378 .auxtrace_error = perf_event__process_auxtrace_error,
379 .ordered_events = true,
380 .ordering_requires_timestamps = true,
384 static const char * const c2c_usage[] = {
385 "perf c2c {record|report}",
389 static const char * const __usage_report[] = {
394 static const char * const *report_c2c_usage = __usage_report;
396 #define C2C_HEADER_MAX 2
402 } line[C2C_HEADER_MAX];
405 struct c2c_dimension {
406 struct c2c_header header;
409 struct sort_entry *se;
411 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
412 struct hist_entry *, struct hist_entry *);
413 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
414 struct hist_entry *he);
415 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
416 struct hist_entry *he);
420 struct perf_hpp_fmt fmt;
421 struct c2c_dimension *dim;
424 #define SYMBOL_WIDTH 30
426 static struct c2c_dimension dim_symbol;
427 static struct c2c_dimension dim_srcline;
429 static int symbol_width(struct hists *hists, struct sort_entry *se)
431 int width = hists__col_len(hists, se->se_width_idx);
433 if (!c2c.symbol_full)
434 width = MIN(width, SYMBOL_WIDTH);
439 static int c2c_width(struct perf_hpp_fmt *fmt,
440 struct perf_hpp *hpp __maybe_unused,
443 struct c2c_fmt *c2c_fmt;
444 struct c2c_dimension *dim;
446 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
449 if (dim == &dim_symbol || dim == &dim_srcline)
450 return symbol_width(hists, dim->se);
452 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
456 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
457 struct hists *hists, int line, int *span)
459 struct perf_hpp_list *hpp_list = hists->hpp_list;
460 struct c2c_fmt *c2c_fmt;
461 struct c2c_dimension *dim;
462 const char *text = NULL;
463 int width = c2c_width(fmt, hpp, hists);
465 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
469 text = dim->header.line[line].text;
470 /* Use the last line from sort_entry if not defined. */
471 if (!text && (line == hpp_list->nr_header_lines - 1))
472 text = dim->se->se_header;
474 text = dim->header.line[line].text;
480 *span = dim->header.line[line].span;
487 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
490 #define HEX_STR(__s, __v) \
492 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
497 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
498 struct hist_entry *left, struct hist_entry *right)
500 return sort__dcacheline_cmp(left, right);
503 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
504 struct hist_entry *he)
507 int width = c2c_width(fmt, hpp, he->hists);
511 addr = cl_address(he->mem_info->daddr.addr);
513 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
517 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
518 struct hist_entry *he)
520 struct c2c_hist_entry *c2c_he;
521 int width = c2c_width(fmt, hpp, he->hists);
523 c2c_he = container_of(he, struct c2c_hist_entry, he);
524 if (WARN_ON_ONCE(!c2c_he->nodestr))
527 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
531 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
532 struct hist_entry *he)
534 struct c2c_hist_entry *c2c_he;
535 int width = c2c_width(fmt, hpp, he->hists);
537 c2c_he = container_of(he, struct c2c_hist_entry, he);
538 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
541 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
542 struct hist_entry *he)
545 int width = c2c_width(fmt, hpp, he->hists);
549 addr = cl_offset(he->mem_info->daddr.al_addr);
551 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
555 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
556 struct hist_entry *left, struct hist_entry *right)
558 uint64_t l = 0, r = 0;
561 l = cl_offset(left->mem_info->daddr.addr);
563 r = cl_offset(right->mem_info->daddr.addr);
565 return (int64_t)(r - l);
569 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
570 struct hist_entry *he)
573 int width = c2c_width(fmt, hpp, he->hists);
577 addr = he->mem_info->iaddr.addr;
579 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
583 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
584 struct hist_entry *left, struct hist_entry *right)
586 return sort__iaddr_cmp(left, right);
590 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
591 struct hist_entry *he)
593 struct c2c_hist_entry *c2c_he;
594 int width = c2c_width(fmt, hpp, he->hists);
595 unsigned int tot_hitm;
597 c2c_he = container_of(he, struct c2c_hist_entry, he);
598 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
600 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
604 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
605 struct hist_entry *left, struct hist_entry *right)
607 struct c2c_hist_entry *c2c_left;
608 struct c2c_hist_entry *c2c_right;
609 uint64_t tot_hitm_left;
610 uint64_t tot_hitm_right;
612 c2c_left = container_of(left, struct c2c_hist_entry, he);
613 c2c_right = container_of(right, struct c2c_hist_entry, he);
615 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
616 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
618 return tot_hitm_left - tot_hitm_right;
621 #define STAT_FN_ENTRY(__f) \
623 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
624 struct hist_entry *he) \
626 struct c2c_hist_entry *c2c_he; \
627 int width = c2c_width(fmt, hpp, he->hists); \
629 c2c_he = container_of(he, struct c2c_hist_entry, he); \
630 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
631 c2c_he->stats.__f); \
634 #define STAT_FN_CMP(__f) \
636 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
637 struct hist_entry *left, struct hist_entry *right) \
639 struct c2c_hist_entry *c2c_left, *c2c_right; \
641 c2c_left = container_of(left, struct c2c_hist_entry, he); \
642 c2c_right = container_of(right, struct c2c_hist_entry, he); \
643 return (uint64_t) c2c_left->stats.__f - \
644 (uint64_t) c2c_right->stats.__f; \
647 #define STAT_FN(__f) \
662 static uint64_t total_records(struct c2c_stats *stats)
664 uint64_t lclmiss, ldcnt, total;
666 lclmiss = stats->lcl_dram +
686 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
687 struct hist_entry *he)
689 struct c2c_hist_entry *c2c_he;
690 int width = c2c_width(fmt, hpp, he->hists);
693 c2c_he = container_of(he, struct c2c_hist_entry, he);
694 tot_recs = total_records(&c2c_he->stats);
696 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
700 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
701 struct hist_entry *left, struct hist_entry *right)
703 struct c2c_hist_entry *c2c_left;
704 struct c2c_hist_entry *c2c_right;
705 uint64_t tot_recs_left;
706 uint64_t tot_recs_right;
708 c2c_left = container_of(left, struct c2c_hist_entry, he);
709 c2c_right = container_of(right, struct c2c_hist_entry, he);
711 tot_recs_left = total_records(&c2c_left->stats);
712 tot_recs_right = total_records(&c2c_right->stats);
714 return tot_recs_left - tot_recs_right;
717 static uint64_t total_loads(struct c2c_stats *stats)
719 uint64_t lclmiss, ldcnt;
721 lclmiss = stats->lcl_dram +
737 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
738 struct hist_entry *he)
740 struct c2c_hist_entry *c2c_he;
741 int width = c2c_width(fmt, hpp, he->hists);
744 c2c_he = container_of(he, struct c2c_hist_entry, he);
745 tot_recs = total_loads(&c2c_he->stats);
747 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
751 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
752 struct hist_entry *left, struct hist_entry *right)
754 struct c2c_hist_entry *c2c_left;
755 struct c2c_hist_entry *c2c_right;
756 uint64_t tot_recs_left;
757 uint64_t tot_recs_right;
759 c2c_left = container_of(left, struct c2c_hist_entry, he);
760 c2c_right = container_of(right, struct c2c_hist_entry, he);
762 tot_recs_left = total_loads(&c2c_left->stats);
763 tot_recs_right = total_loads(&c2c_right->stats);
765 return tot_recs_left - tot_recs_right;
768 typedef double (get_percent_cb)(struct c2c_hist_entry *);
771 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
772 struct hist_entry *he, get_percent_cb get_percent)
774 struct c2c_hist_entry *c2c_he;
775 int width = c2c_width(fmt, hpp, he->hists);
778 c2c_he = container_of(he, struct c2c_hist_entry, he);
779 per = get_percent(c2c_he);
781 #ifdef HAVE_SLANG_SUPPORT
783 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
785 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
788 static double percent_hitm(struct c2c_hist_entry *c2c_he)
790 struct c2c_hists *hists;
791 struct c2c_stats *stats;
792 struct c2c_stats *total;
796 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
797 stats = &c2c_he->stats;
798 total = &hists->stats;
800 switch (c2c.display) {
802 st = stats->rmt_hitm;
803 tot = total->rmt_hitm;
806 st = stats->lcl_hitm;
807 tot = total->lcl_hitm;
810 st = stats->tot_hitm;
811 tot = total->tot_hitm;
816 p = tot ? (double) st / tot : 0;
821 #define PERC_STR(__s, __v) \
823 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
828 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
829 struct hist_entry *he)
831 struct c2c_hist_entry *c2c_he;
832 int width = c2c_width(fmt, hpp, he->hists);
836 c2c_he = container_of(he, struct c2c_hist_entry, he);
837 per = percent_hitm(c2c_he);
838 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
842 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
843 struct hist_entry *he)
845 return percent_color(fmt, hpp, he, percent_hitm);
849 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
850 struct hist_entry *left, struct hist_entry *right)
852 struct c2c_hist_entry *c2c_left;
853 struct c2c_hist_entry *c2c_right;
857 c2c_left = container_of(left, struct c2c_hist_entry, he);
858 c2c_right = container_of(right, struct c2c_hist_entry, he);
860 per_left = percent_hitm(c2c_left);
861 per_right = percent_hitm(c2c_right);
863 return per_left - per_right;
866 static struct c2c_stats *he_stats(struct hist_entry *he)
868 struct c2c_hist_entry *c2c_he;
870 c2c_he = container_of(he, struct c2c_hist_entry, he);
871 return &c2c_he->stats;
874 static struct c2c_stats *total_stats(struct hist_entry *he)
876 struct c2c_hists *hists;
878 hists = container_of(he->hists, struct c2c_hists, hists);
879 return &hists->stats;
882 static double percent(u32 st, u32 tot)
884 return tot ? 100. * (double) st / (double) tot : 0;
887 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
889 #define PERCENT_FN(__f) \
890 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
892 struct c2c_hists *hists; \
894 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
895 return percent(c2c_he->stats.__f, hists->stats.__f); \
901 PERCENT_FN(st_l1miss)
904 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
905 struct hist_entry *he)
907 int width = c2c_width(fmt, hpp, he->hists);
908 double per = PERCENT(he, rmt_hitm);
911 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
915 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
916 struct hist_entry *he)
918 return percent_color(fmt, hpp, he, percent_rmt_hitm);
922 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
923 struct hist_entry *left, struct hist_entry *right)
928 per_left = PERCENT(left, lcl_hitm);
929 per_right = PERCENT(right, lcl_hitm);
931 return per_left - per_right;
935 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
936 struct hist_entry *he)
938 int width = c2c_width(fmt, hpp, he->hists);
939 double per = PERCENT(he, lcl_hitm);
942 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
946 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
947 struct hist_entry *he)
949 return percent_color(fmt, hpp, he, percent_lcl_hitm);
953 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
954 struct hist_entry *left, struct hist_entry *right)
959 per_left = PERCENT(left, lcl_hitm);
960 per_right = PERCENT(right, lcl_hitm);
962 return per_left - per_right;
966 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
967 struct hist_entry *he)
969 int width = c2c_width(fmt, hpp, he->hists);
970 double per = PERCENT(he, st_l1hit);
973 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
977 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
978 struct hist_entry *he)
980 return percent_color(fmt, hpp, he, percent_st_l1hit);
984 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
985 struct hist_entry *left, struct hist_entry *right)
990 per_left = PERCENT(left, st_l1hit);
991 per_right = PERCENT(right, st_l1hit);
993 return per_left - per_right;
997 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
998 struct hist_entry *he)
1000 int width = c2c_width(fmt, hpp, he->hists);
1001 double per = PERCENT(he, st_l1miss);
1004 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1008 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1009 struct hist_entry *he)
1011 return percent_color(fmt, hpp, he, percent_st_l1miss);
1015 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1016 struct hist_entry *left, struct hist_entry *right)
1021 per_left = PERCENT(left, st_l1miss);
1022 per_right = PERCENT(right, st_l1miss);
1024 return per_left - per_right;
1031 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1032 struct hist_entry *he)
1034 int width = c2c_width(fmt, hpp, he->hists);
1036 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1040 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1041 struct hist_entry *left, struct hist_entry *right)
1043 return left->thread->pid_ - right->thread->pid_;
1047 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1048 struct hist_entry *left __maybe_unused,
1049 struct hist_entry *right __maybe_unused)
1054 static int display_metrics(struct perf_hpp *hpp, u32 val, u32 sum)
1059 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ",
1062 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a");
1068 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1069 struct hist_entry *he)
1071 struct c2c_hist_entry *c2c_he;
1076 c2c_he = container_of(he, struct c2c_hist_entry, he);
1078 for (node = 0; node < c2c.nodes_cnt; node++) {
1079 DECLARE_BITMAP(set, c2c.cpus_cnt);
1081 bitmap_zero(set, c2c.cpus_cnt);
1082 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1084 if (bitmap_empty(set, c2c.cpus_cnt)) {
1085 if (c2c.node_info == 1) {
1086 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1087 advance_hpp(hpp, ret);
1093 ret = scnprintf(hpp->buf, hpp->size, " ");
1094 advance_hpp(hpp, ret);
1097 switch (c2c.node_info) {
1099 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1100 advance_hpp(hpp, ret);
1104 int num = bitmap_weight(set, c2c.cpus_cnt);
1105 struct c2c_stats *stats = &c2c_he->node_stats[node];
1107 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1108 advance_hpp(hpp, ret);
1110 switch (c2c.display) {
1112 ret = display_metrics(hpp, stats->rmt_hitm,
1113 c2c_he->stats.rmt_hitm);
1116 ret = display_metrics(hpp, stats->lcl_hitm,
1117 c2c_he->stats.lcl_hitm);
1120 ret = display_metrics(hpp, stats->tot_hitm,
1121 c2c_he->stats.tot_hitm);
1127 advance_hpp(hpp, ret);
1129 if (c2c_he->stats.store > 0) {
1130 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1131 percent(stats->store, c2c_he->stats.store));
1133 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1136 advance_hpp(hpp, ret);
1140 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1141 advance_hpp(hpp, ret);
1143 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1144 advance_hpp(hpp, ret);
1146 ret = scnprintf(hpp->buf, hpp->size, "}");
1147 advance_hpp(hpp, ret);
1160 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1161 struct hist_entry *he, double mean)
1163 int width = c2c_width(fmt, hpp, he->hists);
1166 scnprintf(buf, 10, "%6.0f", mean);
1167 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1170 #define MEAN_ENTRY(__func, __val) \
1172 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1174 struct c2c_hist_entry *c2c_he; \
1175 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1176 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1179 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1180 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1181 MEAN_ENTRY(mean_load_entry, load);
1184 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1185 struct hist_entry *he)
1187 struct c2c_hist_entry *c2c_he;
1188 int width = c2c_width(fmt, hpp, he->hists);
1191 c2c_he = container_of(he, struct c2c_hist_entry, he);
1193 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1194 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1198 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1199 struct hist_entry *he)
1201 struct c2c_hist_entry *c2c_he;
1202 int width = c2c_width(fmt, hpp, he->hists);
1205 c2c_he = container_of(he, struct c2c_hist_entry, he);
1207 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1208 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1212 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1213 struct hist_entry *he)
1215 int width = c2c_width(fmt, hpp, he->hists);
1217 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1220 #define HEADER_LOW(__h) \
1227 #define HEADER_BOTH(__h0, __h1) \
1237 #define HEADER_SPAN(__h0, __h1, __s) \
1248 #define HEADER_SPAN_LOW(__h) \
1255 static struct c2c_dimension dim_dcacheline = {
1256 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1257 .name = "dcacheline",
1258 .cmp = dcacheline_cmp,
1259 .entry = dcacheline_entry,
1263 static struct c2c_dimension dim_dcacheline_node = {
1264 .header = HEADER_LOW("Node"),
1265 .name = "dcacheline_node",
1267 .entry = dcacheline_node_entry,
1271 static struct c2c_dimension dim_dcacheline_count = {
1272 .header = HEADER_LOW("PA cnt"),
1273 .name = "dcacheline_count",
1275 .entry = dcacheline_node_count,
1279 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1281 static struct c2c_dimension dim_offset = {
1282 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
1285 .entry = offset_entry,
1289 static struct c2c_dimension dim_offset_node = {
1290 .header = HEADER_LOW("Node"),
1291 .name = "offset_node",
1293 .entry = dcacheline_node_entry,
1297 static struct c2c_dimension dim_iaddr = {
1298 .header = HEADER_LOW("Code address"),
1301 .entry = iaddr_entry,
1305 static struct c2c_dimension dim_tot_hitm = {
1306 .header = HEADER_SPAN("------- Load Hitm -------", "Total", 2),
1308 .cmp = tot_hitm_cmp,
1309 .entry = tot_hitm_entry,
1313 static struct c2c_dimension dim_lcl_hitm = {
1314 .header = HEADER_SPAN_LOW("LclHitm"),
1316 .cmp = lcl_hitm_cmp,
1317 .entry = lcl_hitm_entry,
1321 static struct c2c_dimension dim_rmt_hitm = {
1322 .header = HEADER_SPAN_LOW("RmtHitm"),
1324 .cmp = rmt_hitm_cmp,
1325 .entry = rmt_hitm_entry,
1329 static struct c2c_dimension dim_cl_rmt_hitm = {
1330 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1331 .name = "cl_rmt_hitm",
1332 .cmp = rmt_hitm_cmp,
1333 .entry = rmt_hitm_entry,
1337 static struct c2c_dimension dim_cl_lcl_hitm = {
1338 .header = HEADER_SPAN_LOW("Lcl"),
1339 .name = "cl_lcl_hitm",
1340 .cmp = lcl_hitm_cmp,
1341 .entry = lcl_hitm_entry,
1345 static struct c2c_dimension dim_tot_stores = {
1346 .header = HEADER_BOTH("Total", "Stores"),
1347 .name = "tot_stores",
1349 .entry = store_entry,
1353 static struct c2c_dimension dim_stores_l1hit = {
1354 .header = HEADER_SPAN("---- Stores ----", "L1Hit", 1),
1355 .name = "stores_l1hit",
1356 .cmp = st_l1hit_cmp,
1357 .entry = st_l1hit_entry,
1361 static struct c2c_dimension dim_stores_l1miss = {
1362 .header = HEADER_SPAN_LOW("L1Miss"),
1363 .name = "stores_l1miss",
1364 .cmp = st_l1miss_cmp,
1365 .entry = st_l1miss_entry,
1369 static struct c2c_dimension dim_cl_stores_l1hit = {
1370 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1371 .name = "cl_stores_l1hit",
1372 .cmp = st_l1hit_cmp,
1373 .entry = st_l1hit_entry,
1377 static struct c2c_dimension dim_cl_stores_l1miss = {
1378 .header = HEADER_SPAN_LOW("L1 Miss"),
1379 .name = "cl_stores_l1miss",
1380 .cmp = st_l1miss_cmp,
1381 .entry = st_l1miss_entry,
1385 static struct c2c_dimension dim_ld_fbhit = {
1386 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1388 .cmp = ld_fbhit_cmp,
1389 .entry = ld_fbhit_entry,
1393 static struct c2c_dimension dim_ld_l1hit = {
1394 .header = HEADER_SPAN_LOW("L1"),
1396 .cmp = ld_l1hit_cmp,
1397 .entry = ld_l1hit_entry,
1401 static struct c2c_dimension dim_ld_l2hit = {
1402 .header = HEADER_SPAN_LOW("L2"),
1404 .cmp = ld_l2hit_cmp,
1405 .entry = ld_l2hit_entry,
1409 static struct c2c_dimension dim_ld_llchit = {
1410 .header = HEADER_SPAN("- LLC Load Hit --", "LclHit", 1),
1411 .name = "ld_lclhit",
1412 .cmp = ld_llchit_cmp,
1413 .entry = ld_llchit_entry,
1417 static struct c2c_dimension dim_ld_rmthit = {
1418 .header = HEADER_SPAN("- RMT Load Hit --", "RmtHit", 1),
1419 .name = "ld_rmthit",
1421 .entry = rmt_hit_entry,
1425 static struct c2c_dimension dim_tot_recs = {
1426 .header = HEADER_BOTH("Total", "records"),
1428 .cmp = tot_recs_cmp,
1429 .entry = tot_recs_entry,
1433 static struct c2c_dimension dim_tot_loads = {
1434 .header = HEADER_BOTH("Total", "Loads"),
1435 .name = "tot_loads",
1436 .cmp = tot_loads_cmp,
1437 .entry = tot_loads_entry,
1441 static struct c2c_header percent_hitm_header[] = {
1442 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1443 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1444 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1447 static struct c2c_dimension dim_percent_hitm = {
1448 .name = "percent_hitm",
1449 .cmp = percent_hitm_cmp,
1450 .entry = percent_hitm_entry,
1451 .color = percent_hitm_color,
1455 static struct c2c_dimension dim_percent_rmt_hitm = {
1456 .header = HEADER_SPAN("----- HITM -----", "RmtHitm", 1),
1457 .name = "percent_rmt_hitm",
1458 .cmp = percent_rmt_hitm_cmp,
1459 .entry = percent_rmt_hitm_entry,
1460 .color = percent_rmt_hitm_color,
1464 static struct c2c_dimension dim_percent_lcl_hitm = {
1465 .header = HEADER_SPAN_LOW("LclHitm"),
1466 .name = "percent_lcl_hitm",
1467 .cmp = percent_lcl_hitm_cmp,
1468 .entry = percent_lcl_hitm_entry,
1469 .color = percent_lcl_hitm_color,
1473 static struct c2c_dimension dim_percent_stores_l1hit = {
1474 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1475 .name = "percent_stores_l1hit",
1476 .cmp = percent_stores_l1hit_cmp,
1477 .entry = percent_stores_l1hit_entry,
1478 .color = percent_stores_l1hit_color,
1482 static struct c2c_dimension dim_percent_stores_l1miss = {
1483 .header = HEADER_SPAN_LOW("L1 Miss"),
1484 .name = "percent_stores_l1miss",
1485 .cmp = percent_stores_l1miss_cmp,
1486 .entry = percent_stores_l1miss_entry,
1487 .color = percent_stores_l1miss_color,
1491 static struct c2c_dimension dim_dram_lcl = {
1492 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1494 .cmp = lcl_dram_cmp,
1495 .entry = lcl_dram_entry,
1499 static struct c2c_dimension dim_dram_rmt = {
1500 .header = HEADER_SPAN_LOW("Rmt"),
1502 .cmp = rmt_dram_cmp,
1503 .entry = rmt_dram_entry,
1507 static struct c2c_dimension dim_pid = {
1508 .header = HEADER_LOW("Pid"),
1515 static struct c2c_dimension dim_tid = {
1516 .header = HEADER_LOW("Tid"),
1521 static struct c2c_dimension dim_symbol = {
1526 static struct c2c_dimension dim_dso = {
1527 .header = HEADER_BOTH("Shared", "Object"),
1532 static struct c2c_header header_node[3] = {
1534 HEADER_LOW("Node{cpus %hitms %stores}"),
1535 HEADER_LOW("Node{cpu list}"),
1538 static struct c2c_dimension dim_node = {
1541 .entry = node_entry,
1545 static struct c2c_dimension dim_mean_rmt = {
1546 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1549 .entry = mean_rmt_entry,
1553 static struct c2c_dimension dim_mean_lcl = {
1554 .header = HEADER_SPAN_LOW("lcl hitm"),
1557 .entry = mean_lcl_entry,
1561 static struct c2c_dimension dim_mean_load = {
1562 .header = HEADER_SPAN_LOW("load"),
1563 .name = "mean_load",
1565 .entry = mean_load_entry,
1569 static struct c2c_dimension dim_cpucnt = {
1570 .header = HEADER_BOTH("cpu", "cnt"),
1573 .entry = cpucnt_entry,
1577 static struct c2c_dimension dim_srcline = {
1578 .name = "cl_srcline",
1579 .se = &sort_srcline,
1582 static struct c2c_dimension dim_dcacheline_idx = {
1583 .header = HEADER_LOW("Index"),
1586 .entry = cl_idx_entry,
1590 static struct c2c_dimension dim_dcacheline_num = {
1591 .header = HEADER_LOW("Num"),
1594 .entry = cl_idx_entry,
1598 static struct c2c_dimension dim_dcacheline_num_empty = {
1599 .header = HEADER_LOW("Num"),
1600 .name = "cl_num_empty",
1602 .entry = cl_idx_empty_entry,
1606 static struct c2c_dimension *dimensions[] = {
1608 &dim_dcacheline_node,
1609 &dim_dcacheline_count,
1621 &dim_cl_stores_l1hit,
1622 &dim_cl_stores_l1miss,
1631 &dim_percent_rmt_hitm,
1632 &dim_percent_lcl_hitm,
1633 &dim_percent_stores_l1hit,
1634 &dim_percent_stores_l1miss,
1647 &dim_dcacheline_idx,
1648 &dim_dcacheline_num,
1649 &dim_dcacheline_num_empty,
1653 static void fmt_free(struct perf_hpp_fmt *fmt)
1655 struct c2c_fmt *c2c_fmt;
1657 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1661 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1663 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1664 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1666 return c2c_a->dim == c2c_b->dim;
1669 static struct c2c_dimension *get_dimension(const char *name)
1673 for (i = 0; dimensions[i]; i++) {
1674 struct c2c_dimension *dim = dimensions[i];
1676 if (!strcmp(dim->name, name))
1683 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1684 struct hist_entry *he)
1686 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1687 struct c2c_dimension *dim = c2c_fmt->dim;
1688 size_t len = fmt->user_len;
1691 len = hists__col_len(he->hists, dim->se->se_width_idx);
1693 if (dim == &dim_symbol || dim == &dim_srcline)
1694 len = symbol_width(he->hists, dim->se);
1697 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1700 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1701 struct hist_entry *a, struct hist_entry *b)
1703 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1704 struct c2c_dimension *dim = c2c_fmt->dim;
1706 return dim->se->se_cmp(a, b);
1709 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1710 struct hist_entry *a, struct hist_entry *b)
1712 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1713 struct c2c_dimension *dim = c2c_fmt->dim;
1714 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1716 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1717 return collapse_fn(a, b);
1720 static struct c2c_fmt *get_format(const char *name)
1722 struct c2c_dimension *dim = get_dimension(name);
1723 struct c2c_fmt *c2c_fmt;
1724 struct perf_hpp_fmt *fmt;
1729 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1735 fmt = &c2c_fmt->fmt;
1736 INIT_LIST_HEAD(&fmt->list);
1737 INIT_LIST_HEAD(&fmt->sort_list);
1739 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1740 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1741 fmt->color = dim->se ? NULL : dim->color;
1742 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1743 fmt->header = c2c_header;
1744 fmt->width = c2c_width;
1745 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1746 fmt->equal = fmt_equal;
1747 fmt->free = fmt_free;
1752 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1754 struct c2c_fmt *c2c_fmt = get_format(name);
1758 return output_field_add(hpp_list, name);
1761 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1765 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1767 struct c2c_fmt *c2c_fmt = get_format(name);
1768 struct c2c_dimension *dim;
1772 return sort_dimension__add(hpp_list, name, NULL, 0);
1776 if (dim == &dim_dso)
1779 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1783 #define PARSE_LIST(_list, _fn) \
1791 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1792 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1793 ret = _fn(hpp_list, tok); \
1794 if (ret == -EINVAL) { \
1795 pr_err("Invalid --fields key: `%s'", tok); \
1797 } else if (ret == -ESRCH) { \
1798 pr_err("Unknown --fields key: `%s'", tok); \
1804 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1805 const char *output_,
1808 char *output = output_ ? strdup(output_) : NULL;
1809 char *sort = sort_ ? strdup(sort_) : NULL;
1812 PARSE_LIST(output, c2c_hists__init_output);
1813 PARSE_LIST(sort, c2c_hists__init_sort);
1815 /* copy sort keys to output fields */
1816 perf_hpp__setup_output_field(hpp_list);
1819 * We dont need other sorting keys other than those
1820 * we already specified. It also really slows down
1821 * the processing a lot with big number of output
1822 * fields, so switching this off for c2c.
1826 /* and then copy output fields to sort keys */
1827 perf_hpp__append_sort_keys(&hists->list);
1835 static int c2c_hists__init(struct c2c_hists *hists,
1837 int nr_header_lines)
1839 __hists__init(&hists->hists, &hists->list);
1842 * Initialize only with sort fields, we need to resort
1843 * later anyway, and that's where we add output fields
1846 perf_hpp_list__init(&hists->list);
1848 /* Overload number of header lines.*/
1849 hists->list.nr_header_lines = nr_header_lines;
1851 return hpp_list__parse(&hists->list, NULL, sort);
1854 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1858 perf_hpp__reset_output_field(&c2c_hists->list);
1859 return hpp_list__parse(&c2c_hists->list, output, sort);
1862 #define DISPLAY_LINE_LIMIT 0.001
1864 static u8 filter_display(u32 val, u32 sum)
1866 if (sum == 0 || ((double)val / sum) < DISPLAY_LINE_LIMIT)
1867 return HIST_FILTER__C2C;
1872 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1874 struct c2c_hist_entry *c2c_he;
1879 c2c_he = container_of(he, struct c2c_hist_entry, he);
1881 switch (c2c.display) {
1883 he->filtered = filter_display(c2c_he->stats.lcl_hitm,
1887 he->filtered = filter_display(c2c_he->stats.rmt_hitm,
1891 he->filtered = filter_display(c2c_he->stats.tot_hitm,
1898 return he->filtered == 0;
1901 static inline bool is_valid_hist_entry(struct hist_entry *he)
1903 struct c2c_hist_entry *c2c_he;
1904 bool has_record = false;
1906 c2c_he = container_of(he, struct c2c_hist_entry, he);
1908 /* It's a valid entry if contains stores */
1909 if (c2c_he->stats.store)
1912 switch (c2c.display) {
1914 has_record = !!c2c_he->stats.lcl_hitm;
1917 has_record = !!c2c_he->stats.rmt_hitm;
1920 has_record = !!c2c_he->stats.tot_hitm;
1929 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1931 struct c2c_dimension *dim;
1933 dim = &c2c.hists == c2c_he->hists ?
1934 &dim_dcacheline_node : &dim_offset_node;
1936 if (len > dim->width)
1940 static int set_nodestr(struct c2c_hist_entry *c2c_he)
1945 if (c2c_he->nodestr)
1948 if (!bitmap_empty(c2c_he->nodeset, c2c.nodes_cnt)) {
1949 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1952 len = scnprintf(buf, sizeof(buf), "N/A");
1955 set_node_width(c2c_he, len);
1956 c2c_he->nodestr = strdup(buf);
1957 return c2c_he->nodestr ? 0 : -ENOMEM;
1960 static void calc_width(struct c2c_hist_entry *c2c_he)
1962 struct c2c_hists *c2c_hists;
1964 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1965 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
1966 set_nodestr(c2c_he);
1969 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
1971 struct c2c_hist_entry *c2c_he;
1973 c2c_he = container_of(he, struct c2c_hist_entry, he);
1975 if (c2c.show_src && !he->srcline)
1976 he->srcline = hist_entry__srcline(he);
1980 if (!is_valid_hist_entry(he))
1981 he->filtered = HIST_FILTER__C2C;
1986 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
1988 struct c2c_hist_entry *c2c_he;
1989 struct c2c_hists *c2c_hists;
1990 bool display = he__display(he, &c2c.shared_clines_stats);
1992 c2c_he = container_of(he, struct c2c_hist_entry, he);
1993 c2c_hists = c2c_he->hists;
1995 if (display && c2c_hists) {
1996 static unsigned int idx;
1998 c2c_he->cacheline_idx = idx++;
2001 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2003 hists__collapse_resort(&c2c_hists->hists, NULL);
2004 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2010 static void setup_nodes_header(void)
2012 dim_node.header = header_node[c2c.node_info];
2015 static int setup_nodes(struct perf_session *session)
2017 struct numa_node *n;
2018 unsigned long **nodes;
2020 struct perf_cpu cpu;
2023 if (c2c.node_info > 2)
2026 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2027 c2c.cpus_cnt = session->header.env.nr_cpus_avail;
2029 n = session->header.env.numa_nodes;
2033 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2039 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2043 for (idx = 0; idx < c2c.cpus_cnt; idx++)
2046 c2c.cpu2node = cpu2node;
2048 for (node = 0; node < c2c.nodes_cnt; node++) {
2049 struct perf_cpu_map *map = n[node].map;
2052 set = bitmap_zalloc(c2c.cpus_cnt);
2058 /* empty node, skip */
2059 if (perf_cpu_map__empty(map))
2062 perf_cpu_map__for_each_cpu(cpu, idx, map) {
2063 set_bit(cpu.cpu, set);
2065 if (WARN_ONCE(cpu2node[cpu.cpu] != -1, "node/cpu topology bug"))
2068 cpu2node[cpu.cpu] = node;
2072 setup_nodes_header();
2076 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2078 static int resort_shared_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
2080 struct c2c_hist_entry *c2c_he;
2081 c2c_he = container_of(he, struct c2c_hist_entry, he);
2083 if (HAS_HITMS(c2c_he)) {
2084 c2c.shared_clines++;
2085 c2c_add_stats(&c2c.shared_clines_stats, &c2c_he->stats);
2091 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2093 struct rb_node *next = rb_first_cached(&hists->entries);
2097 struct hist_entry *he;
2099 he = rb_entry(next, struct hist_entry, rb_node);
2103 next = rb_next(&he->rb_node);
2109 static void print_c2c__display_stats(FILE *out)
2112 struct c2c_stats *stats = &c2c.hists.stats;
2114 llc_misses = stats->lcl_dram +
2119 fprintf(out, "=================================================\n");
2120 fprintf(out, " Trace Event Information \n");
2121 fprintf(out, "=================================================\n");
2122 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2123 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2124 fprintf(out, " Load Operations : %10d\n", stats->load);
2125 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2126 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2127 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2128 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2129 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2130 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2131 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2132 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2133 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2134 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2135 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2136 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2137 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2138 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2139 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2140 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2141 fprintf(out, " Load access blocked by data : %10d\n", stats->blk_data);
2142 fprintf(out, " Load access blocked by address : %10d\n", stats->blk_addr);
2143 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2144 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2145 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2146 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2147 fprintf(out, " Store Operations : %10d\n", stats->store);
2148 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2149 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2150 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2151 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2152 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2153 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2156 static void print_shared_cacheline_info(FILE *out)
2158 struct c2c_stats *stats = &c2c.shared_clines_stats;
2159 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2161 fprintf(out, "=================================================\n");
2162 fprintf(out, " Global Shared Cache Line Event Information \n");
2163 fprintf(out, "=================================================\n");
2164 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2165 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2166 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2167 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2168 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2169 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2170 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2171 fprintf(out, " Blocked Access on shared lines : %10d\n", stats->blk_data + stats->blk_addr);
2172 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2173 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2174 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2177 static void print_cacheline(struct c2c_hists *c2c_hists,
2178 struct hist_entry *he_cl,
2179 struct perf_hpp_list *hpp_list,
2183 struct perf_hpp hpp = {
2190 hists__fprintf_headers(&c2c_hists->hists, out);
2196 fprintf(out, " -------------------------------------------------------------\n");
2197 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2198 fprintf(out, "%s\n", bf);
2199 fprintf(out, " -------------------------------------------------------------\n");
2201 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2204 static void print_pareto(FILE *out)
2206 struct perf_hpp_list hpp_list;
2209 const char *cl_output;
2211 cl_output = "cl_num,"
2218 perf_hpp_list__init(&hpp_list);
2219 ret = hpp_list__parse(&hpp_list, cl_output, NULL);
2221 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2224 nd = rb_first_cached(&c2c.hists.hists.entries);
2226 for (; nd; nd = rb_next(nd)) {
2227 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2228 struct c2c_hist_entry *c2c_he;
2233 c2c_he = container_of(he, struct c2c_hist_entry, he);
2234 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2238 static void print_c2c_info(FILE *out, struct perf_session *session)
2240 struct evlist *evlist = session->evlist;
2241 struct evsel *evsel;
2244 fprintf(out, "=================================================\n");
2245 fprintf(out, " c2c details \n");
2246 fprintf(out, "=================================================\n");
2248 evlist__for_each_entry(evlist, evsel) {
2249 fprintf(out, "%-36s: %s\n", first ? " Events" : "", evsel__name(evsel));
2252 fprintf(out, " Cachelines sort on : %s HITMs\n",
2253 display_str[c2c.display]);
2254 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2257 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2261 print_c2c__display_stats(out);
2263 print_shared_cacheline_info(out);
2265 print_c2c_info(out, session);
2271 fprintf(out, "=================================================\n");
2272 fprintf(out, " Shared Data Cache Line Table \n");
2273 fprintf(out, "=================================================\n");
2274 fprintf(out, "#\n");
2276 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2279 fprintf(out, "=================================================\n");
2280 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2281 fprintf(out, "=================================================\n");
2282 fprintf(out, "#\n");
2287 #ifdef HAVE_SLANG_SUPPORT
2288 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2291 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2294 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2302 hb->nr_non_filtered_entries = nr_entries;
2305 struct c2c_cacheline_browser {
2306 struct hist_browser hb;
2307 struct hist_entry *he;
2311 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2312 char *bf, size_t size)
2314 struct c2c_cacheline_browser *cl_browser;
2315 struct hist_entry *he;
2318 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2319 he = cl_browser->he;
2322 addr = cl_address(he->mem_info->daddr.addr);
2324 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2328 static struct c2c_cacheline_browser*
2329 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2331 struct c2c_cacheline_browser *browser;
2333 browser = zalloc(sizeof(*browser));
2335 hist_browser__init(&browser->hb, hists);
2336 browser->hb.c2c_filter = true;
2337 browser->hb.title = perf_c2c_cacheline_browser__title;
2344 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2346 struct c2c_hist_entry *c2c_he;
2347 struct c2c_hists *c2c_hists;
2348 struct c2c_cacheline_browser *cl_browser;
2349 struct hist_browser *browser;
2351 static const char help[] =
2352 " ENTER Toggle callchains (if present) \n"
2353 " n Toggle Node details info \n"
2354 " s Toggle full length of symbol and source line columns \n"
2355 " q Return back to cacheline list \n";
2360 /* Display compact version first. */
2361 c2c.symbol_full = false;
2363 c2c_he = container_of(he, struct c2c_hist_entry, he);
2364 c2c_hists = c2c_he->hists;
2366 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2367 if (cl_browser == NULL)
2370 browser = &cl_browser->hb;
2372 /* reset abort key so that it can get Ctrl-C as a key */
2374 SLang_init_tty(0, 0, 0);
2376 c2c_browser__update_nr_entries(browser);
2379 key = hist_browser__run(browser, "? - help", true, 0);
2383 c2c.symbol_full = !c2c.symbol_full;
2386 c2c.node_info = (c2c.node_info + 1) % 3;
2387 setup_nodes_header();
2392 ui_browser__help_window(&browser->b, help);
2404 static int perf_c2c_browser__title(struct hist_browser *browser,
2405 char *bf, size_t size)
2408 "Shared Data Cache Line Table "
2409 "(%lu entries, sorted on %s HITMs)",
2410 browser->nr_non_filtered_entries,
2411 display_str[c2c.display]);
2415 static struct hist_browser*
2416 perf_c2c_browser__new(struct hists *hists)
2418 struct hist_browser *browser = hist_browser__new(hists);
2421 browser->title = perf_c2c_browser__title;
2422 browser->c2c_filter = true;
2428 static int perf_c2c__hists_browse(struct hists *hists)
2430 struct hist_browser *browser;
2432 static const char help[] =
2433 " d Display cacheline details \n"
2434 " ENTER Toggle callchains (if present) \n"
2437 browser = perf_c2c_browser__new(hists);
2438 if (browser == NULL)
2441 /* reset abort key so that it can get Ctrl-C as a key */
2443 SLang_init_tty(0, 0, 0);
2445 c2c_browser__update_nr_entries(browser);
2448 key = hist_browser__run(browser, "? - help", true, 0);
2454 perf_c2c__browse_cacheline(browser->he_selection);
2457 ui_browser__help_window(&browser->b, help);
2465 hist_browser__delete(browser);
2469 static void perf_c2c_display(struct perf_session *session)
2471 if (use_browser == 0)
2472 perf_c2c__hists_fprintf(stdout, session);
2474 perf_c2c__hists_browse(&c2c.hists.hists);
2477 static void perf_c2c_display(struct perf_session *session)
2480 perf_c2c__hists_fprintf(stdout, session);
2482 #endif /* HAVE_SLANG_SUPPORT */
2484 static char *fill_line(const char *orig, int len)
2486 int i, j, olen = strlen(orig);
2489 buf = zalloc(len + 1);
2493 j = len / 2 - olen / 2;
2495 for (i = 0; i < j - 1; i++)
2500 strcpy(buf + i, orig);
2506 for (; i < len; i++)
2512 static int ui_quirks(void)
2514 const char *nodestr = "Data address";
2517 if (!c2c.use_stdio) {
2518 dim_offset.width = 5;
2519 dim_offset.header = header_offset_tui;
2523 dim_percent_hitm.header = percent_hitm_header[c2c.display];
2525 /* Fix the zero line for dcacheline column. */
2526 buf = fill_line("Cacheline", dim_dcacheline.width +
2527 dim_dcacheline_node.width +
2528 dim_dcacheline_count.width + 4);
2532 dim_dcacheline.header.line[0].text = buf;
2534 /* Fix the zero line for offset column. */
2535 buf = fill_line(nodestr, dim_offset.width +
2536 dim_offset_node.width +
2537 dim_dcacheline_count.width + 4);
2541 dim_offset.header.line[0].text = buf;
2546 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2548 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2549 CALLCHAIN_REPORT_HELP
2550 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2553 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2555 struct callchain_param *callchain = opt->value;
2557 callchain->enabled = !unset;
2562 symbol_conf.use_callchain = false;
2563 callchain->mode = CHAIN_NONE;
2567 return parse_callchain_report_opt(arg);
2570 static int setup_callchain(struct evlist *evlist)
2572 u64 sample_type = evlist__combined_sample_type(evlist);
2573 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2575 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2576 (sample_type & PERF_SAMPLE_STACK_USER)) {
2577 mode = CALLCHAIN_DWARF;
2578 dwarf_callchain_users = true;
2579 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2580 mode = CALLCHAIN_LBR;
2581 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2582 mode = CALLCHAIN_FP;
2584 if (!callchain_param.enabled &&
2585 callchain_param.mode != CHAIN_NONE &&
2586 mode != CALLCHAIN_NONE) {
2587 symbol_conf.use_callchain = true;
2588 if (callchain_register_param(&callchain_param) < 0) {
2589 ui__error("Can't register callchain params.\n");
2594 if (c2c.stitch_lbr && (mode != CALLCHAIN_LBR)) {
2595 ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
2596 "Please apply --call-graph lbr when recording.\n");
2597 c2c.stitch_lbr = false;
2600 callchain_param.record_mode = mode;
2601 callchain_param.min_percent = 0;
2605 static int setup_display(const char *str)
2607 const char *display = str ?: "tot";
2609 if (!strcmp(display, "tot"))
2610 c2c.display = DISPLAY_TOT;
2611 else if (!strcmp(display, "rmt"))
2612 c2c.display = DISPLAY_RMT;
2613 else if (!strcmp(display, "lcl"))
2614 c2c.display = DISPLAY_LCL;
2616 pr_err("failed: unknown display type: %s\n", str);
2623 #define for_each_token(__tok, __buf, __sep, __tmp) \
2624 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2625 __tok = strtok_r(NULL, __sep, &__tmp))
2627 static int build_cl_output(char *cl_sort, bool no_source)
2629 char *tok, *tmp, *buf = strdup(cl_sort);
2630 bool add_pid = false;
2631 bool add_tid = false;
2632 bool add_iaddr = false;
2633 bool add_sym = false;
2634 bool add_dso = false;
2635 bool add_src = false;
2641 for_each_token(tok, buf, ",", tmp) {
2642 if (!strcmp(tok, "tid")) {
2644 } else if (!strcmp(tok, "pid")) {
2646 } else if (!strcmp(tok, "iaddr")) {
2650 add_src = no_source ? false : true;
2651 } else if (!strcmp(tok, "dso")) {
2653 } else if (strcmp(tok, "offset")) {
2654 pr_err("unrecognized sort token: %s\n", tok);
2660 if (asprintf(&c2c.cl_output,
2661 "%s%s%s%s%s%s%s%s%s%s",
2662 c2c.use_stdio ? "cl_num_empty," : "",
2665 "percent_stores_l1hit,"
2666 "percent_stores_l1miss,"
2667 "offset,offset_node,dcacheline_count,",
2668 add_pid ? "pid," : "",
2669 add_tid ? "tid," : "",
2670 add_iaddr ? "iaddr," : "",
2676 add_sym ? "symbol," : "",
2677 add_dso ? "dso," : "",
2678 add_src ? "cl_srcline," : "",
2684 c2c.show_src = add_src;
2690 static int setup_coalesce(const char *coalesce, bool no_source)
2692 const char *c = coalesce ?: coalesce_default;
2694 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2697 if (build_cl_output(c2c.cl_sort, no_source))
2700 if (asprintf(&c2c.cl_resort, "offset,%s",
2701 c2c.display == DISPLAY_TOT ?
2703 c2c.display == DISPLAY_RMT ?
2704 "rmt_hitm,lcl_hitm" :
2705 "lcl_hitm,rmt_hitm") < 0)
2708 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2709 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2710 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2714 static int perf_c2c__report(int argc, const char **argv)
2716 struct itrace_synth_opts itrace_synth_opts = {
2718 .mem = true, /* Only enable memory event */
2719 .default_no_sample = true,
2722 struct perf_session *session;
2723 struct ui_progress prog;
2724 struct perf_data data = {
2725 .mode = PERF_DATA_MODE_READ,
2727 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2728 const char *display = NULL;
2729 const char *coalesce = NULL;
2730 bool no_source = false;
2731 const struct option options[] = {
2732 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2733 "file", "vmlinux pathname"),
2734 OPT_STRING('i', "input", &input_name, "file",
2735 "the input file to process"),
2736 OPT_INCR('N', "node-info", &c2c.node_info,
2737 "show extra node info in report (repeat for more info)"),
2738 #ifdef HAVE_SLANG_SUPPORT
2739 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2741 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2742 "Display only statistic tables (implies --stdio)"),
2743 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2744 "Display full length of symbols"),
2745 OPT_BOOLEAN(0, "no-source", &no_source,
2746 "Do not display Source Line column"),
2747 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2748 "Show all captured HITM lines."),
2749 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2750 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2751 callchain_help, &parse_callchain_opt,
2752 callchain_default_opt),
2753 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2754 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2755 "coalesce fields: pid,tid,iaddr,dso"),
2756 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2757 OPT_BOOLEAN(0, "stitch-lbr", &c2c.stitch_lbr,
2758 "Enable LBR callgraph stitching approach"),
2759 OPT_PARENT(c2c_options),
2763 const char *output_str, *sort_str = NULL;
2765 argc = parse_options(argc, argv, options, report_c2c_usage,
2766 PARSE_OPT_STOP_AT_NON_OPTION);
2768 usage_with_options(report_c2c_usage, options);
2771 c2c.use_stdio = true;
2773 err = symbol__validate_sym_arguments();
2777 if (!input_name || !strlen(input_name))
2778 input_name = "perf.data";
2780 data.path = input_name;
2781 data.force = symbol_conf.force;
2783 err = setup_display(display);
2787 err = setup_coalesce(coalesce, no_source);
2789 pr_debug("Failed to initialize hists\n");
2793 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2795 pr_debug("Failed to initialize hists\n");
2799 session = perf_session__new(&data, &c2c.tool);
2800 if (IS_ERR(session)) {
2801 err = PTR_ERR(session);
2802 pr_debug("Error creating perf session\n");
2806 session->itrace_synth_opts = &itrace_synth_opts;
2808 err = setup_nodes(session);
2810 pr_err("Failed setup nodes\n");
2814 err = mem2node__init(&c2c.mem2node, &session->header.env);
2818 err = setup_callchain(session->evlist);
2822 if (symbol__init(&session->header.env) < 0)
2825 /* No pipe support at the moment. */
2826 if (perf_data__is_pipe(session->data)) {
2827 pr_debug("No pipe support at the moment.\n");
2836 setup_browser(false);
2838 err = perf_session__process_events(session);
2840 pr_err("failed to process sample\n");
2844 output_str = "cl_idx,"
2849 "tot_hitm,lcl_hitm,rmt_hitm,"
2853 "stores_l1hit,stores_l1miss,"
2854 "ld_fbhit,ld_l1hit,ld_l2hit,"
2855 "ld_lclhit,lcl_hitm,"
2856 "ld_rmthit,rmt_hitm,"
2857 "dram_lcl,dram_rmt";
2859 if (c2c.display == DISPLAY_TOT)
2860 sort_str = "tot_hitm";
2861 else if (c2c.display == DISPLAY_RMT)
2862 sort_str = "rmt_hitm";
2863 else if (c2c.display == DISPLAY_LCL)
2864 sort_str = "lcl_hitm";
2866 c2c_hists__reinit(&c2c.hists, output_str, sort_str);
2868 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2870 hists__collapse_resort(&c2c.hists.hists, NULL);
2871 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_shared_cl_cb);
2872 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2874 ui_progress__finish();
2877 pr_err("failed to setup UI\n");
2881 perf_c2c_display(session);
2884 mem2node__exit(&c2c.mem2node);
2886 perf_session__delete(session);
2891 static int parse_record_events(const struct option *opt,
2892 const char *str, int unset __maybe_unused)
2894 bool *event_set = (bool *) opt->value;
2896 if (!strcmp(str, "list")) {
2897 perf_mem_events__list();
2900 if (perf_mem_events__parse(str))
2908 static const char * const __usage_record[] = {
2909 "perf c2c record [<options>] [<command>]",
2910 "perf c2c record [<options>] -- <command> [<options>]",
2914 static const char * const *record_mem_usage = __usage_record;
2916 static int perf_c2c__record(int argc, const char **argv)
2918 int rec_argc, i = 0, j, rec_tmp_nr = 0;
2919 const char **rec_argv;
2922 bool all_user = false, all_kernel = false;
2923 bool event_set = false;
2924 struct perf_mem_event *e;
2925 struct option options[] = {
2926 OPT_CALLBACK('e', "event", &event_set, "event",
2927 "event selector. Use 'perf c2c record -e list' to list available events",
2928 parse_record_events),
2929 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2930 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2931 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2932 OPT_PARENT(c2c_options),
2936 if (perf_mem_events__init()) {
2937 pr_err("failed: memory events not supported\n");
2941 argc = parse_options(argc, argv, options, record_mem_usage,
2942 PARSE_OPT_KEEP_UNKNOWN);
2944 if (!perf_pmu__has_hybrid())
2945 rec_argc = argc + 11; /* max number of arguments */
2947 rec_argc = argc + 11 * perf_pmu__hybrid_pmu_num();
2949 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2953 rec_tmp = calloc(rec_argc + 1, sizeof(char *));
2959 rec_argv[i++] = "record";
2962 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD_STORE);
2964 * The load and store operations are required, use the event
2965 * PERF_MEM_EVENTS__LOAD_STORE if it is supported.
2970 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
2973 e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE);
2978 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
2980 rec_argv[i++] = "-W";
2982 rec_argv[i++] = "-d";
2983 rec_argv[i++] = "--phys-data";
2984 rec_argv[i++] = "--sample-cpu";
2986 ret = perf_mem_events__record_args(rec_argv, &i, rec_tmp, &rec_tmp_nr);
2991 rec_argv[i++] = "--all-user";
2994 rec_argv[i++] = "--all-kernel";
2996 for (j = 0; j < argc; j++, i++)
2997 rec_argv[i] = argv[j];
3000 pr_debug("calling: ");
3004 while (rec_argv[j]) {
3005 pr_debug("%s ", rec_argv[j]);
3011 ret = cmd_record(i, rec_argv);
3013 for (i = 0; i < rec_tmp_nr; i++)
3021 int cmd_c2c(int argc, const char **argv)
3023 argc = parse_options(argc, argv, c2c_options, c2c_usage,
3024 PARSE_OPT_STOP_AT_NON_OPTION);
3027 usage_with_options(c2c_usage, c2c_options);
3029 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3030 return perf_c2c__record(argc, argv);
3031 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
3032 return perf_c2c__report(argc, argv);
3034 usage_with_options(c2c_usage, c2c_options);