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) \
663 static uint64_t total_records(struct c2c_stats *stats)
665 uint64_t lclmiss, ldcnt, total;
667 lclmiss = stats->lcl_dram +
688 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
689 struct hist_entry *he)
691 struct c2c_hist_entry *c2c_he;
692 int width = c2c_width(fmt, hpp, he->hists);
695 c2c_he = container_of(he, struct c2c_hist_entry, he);
696 tot_recs = total_records(&c2c_he->stats);
698 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
702 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
703 struct hist_entry *left, struct hist_entry *right)
705 struct c2c_hist_entry *c2c_left;
706 struct c2c_hist_entry *c2c_right;
707 uint64_t tot_recs_left;
708 uint64_t tot_recs_right;
710 c2c_left = container_of(left, struct c2c_hist_entry, he);
711 c2c_right = container_of(right, struct c2c_hist_entry, he);
713 tot_recs_left = total_records(&c2c_left->stats);
714 tot_recs_right = total_records(&c2c_right->stats);
716 return tot_recs_left - tot_recs_right;
719 static uint64_t total_loads(struct c2c_stats *stats)
721 uint64_t lclmiss, ldcnt;
723 lclmiss = stats->lcl_dram +
739 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
740 struct hist_entry *he)
742 struct c2c_hist_entry *c2c_he;
743 int width = c2c_width(fmt, hpp, he->hists);
746 c2c_he = container_of(he, struct c2c_hist_entry, he);
747 tot_recs = total_loads(&c2c_he->stats);
749 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
753 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
754 struct hist_entry *left, struct hist_entry *right)
756 struct c2c_hist_entry *c2c_left;
757 struct c2c_hist_entry *c2c_right;
758 uint64_t tot_recs_left;
759 uint64_t tot_recs_right;
761 c2c_left = container_of(left, struct c2c_hist_entry, he);
762 c2c_right = container_of(right, struct c2c_hist_entry, he);
764 tot_recs_left = total_loads(&c2c_left->stats);
765 tot_recs_right = total_loads(&c2c_right->stats);
767 return tot_recs_left - tot_recs_right;
770 typedef double (get_percent_cb)(struct c2c_hist_entry *);
773 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
774 struct hist_entry *he, get_percent_cb get_percent)
776 struct c2c_hist_entry *c2c_he;
777 int width = c2c_width(fmt, hpp, he->hists);
780 c2c_he = container_of(he, struct c2c_hist_entry, he);
781 per = get_percent(c2c_he);
783 #ifdef HAVE_SLANG_SUPPORT
785 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
787 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
790 static double percent_hitm(struct c2c_hist_entry *c2c_he)
792 struct c2c_hists *hists;
793 struct c2c_stats *stats;
794 struct c2c_stats *total;
798 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
799 stats = &c2c_he->stats;
800 total = &hists->stats;
802 switch (c2c.display) {
804 st = stats->rmt_hitm;
805 tot = total->rmt_hitm;
808 st = stats->lcl_hitm;
809 tot = total->lcl_hitm;
812 st = stats->tot_hitm;
813 tot = total->tot_hitm;
818 p = tot ? (double) st / tot : 0;
823 #define PERC_STR(__s, __v) \
825 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
830 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
831 struct hist_entry *he)
833 struct c2c_hist_entry *c2c_he;
834 int width = c2c_width(fmt, hpp, he->hists);
838 c2c_he = container_of(he, struct c2c_hist_entry, he);
839 per = percent_hitm(c2c_he);
840 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
844 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
845 struct hist_entry *he)
847 return percent_color(fmt, hpp, he, percent_hitm);
851 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
852 struct hist_entry *left, struct hist_entry *right)
854 struct c2c_hist_entry *c2c_left;
855 struct c2c_hist_entry *c2c_right;
859 c2c_left = container_of(left, struct c2c_hist_entry, he);
860 c2c_right = container_of(right, struct c2c_hist_entry, he);
862 per_left = percent_hitm(c2c_left);
863 per_right = percent_hitm(c2c_right);
865 return per_left - per_right;
868 static struct c2c_stats *he_stats(struct hist_entry *he)
870 struct c2c_hist_entry *c2c_he;
872 c2c_he = container_of(he, struct c2c_hist_entry, he);
873 return &c2c_he->stats;
876 static struct c2c_stats *total_stats(struct hist_entry *he)
878 struct c2c_hists *hists;
880 hists = container_of(he->hists, struct c2c_hists, hists);
881 return &hists->stats;
884 static double percent(u32 st, u32 tot)
886 return tot ? 100. * (double) st / (double) tot : 0;
889 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
891 #define PERCENT_FN(__f) \
892 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
894 struct c2c_hists *hists; \
896 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
897 return percent(c2c_he->stats.__f, hists->stats.__f); \
903 PERCENT_FN(st_l1miss)
907 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
908 struct hist_entry *he)
910 int width = c2c_width(fmt, hpp, he->hists);
911 double per = PERCENT(he, rmt_hitm);
914 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
918 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
919 struct hist_entry *he)
921 return percent_color(fmt, hpp, he, percent_rmt_hitm);
925 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
926 struct hist_entry *left, struct hist_entry *right)
931 per_left = PERCENT(left, rmt_hitm);
932 per_right = PERCENT(right, rmt_hitm);
934 return per_left - per_right;
938 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
939 struct hist_entry *he)
941 int width = c2c_width(fmt, hpp, he->hists);
942 double per = PERCENT(he, lcl_hitm);
945 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
949 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
950 struct hist_entry *he)
952 return percent_color(fmt, hpp, he, percent_lcl_hitm);
956 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
957 struct hist_entry *left, struct hist_entry *right)
962 per_left = PERCENT(left, lcl_hitm);
963 per_right = PERCENT(right, lcl_hitm);
965 return per_left - per_right;
969 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
970 struct hist_entry *he)
972 int width = c2c_width(fmt, hpp, he->hists);
973 double per = PERCENT(he, st_l1hit);
976 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
980 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
981 struct hist_entry *he)
983 return percent_color(fmt, hpp, he, percent_st_l1hit);
987 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
988 struct hist_entry *left, struct hist_entry *right)
993 per_left = PERCENT(left, st_l1hit);
994 per_right = PERCENT(right, st_l1hit);
996 return per_left - per_right;
1000 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1001 struct hist_entry *he)
1003 int width = c2c_width(fmt, hpp, he->hists);
1004 double per = PERCENT(he, st_l1miss);
1007 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1011 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1012 struct hist_entry *he)
1014 return percent_color(fmt, hpp, he, percent_st_l1miss);
1018 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1019 struct hist_entry *left, struct hist_entry *right)
1024 per_left = PERCENT(left, st_l1miss);
1025 per_right = PERCENT(right, st_l1miss);
1027 return per_left - per_right;
1031 percent_stores_na_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1032 struct hist_entry *he)
1034 int width = c2c_width(fmt, hpp, he->hists);
1035 double per = PERCENT(he, st_na);
1038 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1042 percent_stores_na_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1043 struct hist_entry *he)
1045 return percent_color(fmt, hpp, he, percent_st_na);
1049 percent_stores_na_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1050 struct hist_entry *left, struct hist_entry *right)
1055 per_left = PERCENT(left, st_na);
1056 per_right = PERCENT(right, st_na);
1058 return per_left - per_right;
1065 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1066 struct hist_entry *he)
1068 int width = c2c_width(fmt, hpp, he->hists);
1070 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1074 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1075 struct hist_entry *left, struct hist_entry *right)
1077 return left->thread->pid_ - right->thread->pid_;
1081 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1082 struct hist_entry *left __maybe_unused,
1083 struct hist_entry *right __maybe_unused)
1088 static int display_metrics(struct perf_hpp *hpp, u32 val, u32 sum)
1093 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ",
1096 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a");
1102 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1103 struct hist_entry *he)
1105 struct c2c_hist_entry *c2c_he;
1110 c2c_he = container_of(he, struct c2c_hist_entry, he);
1112 for (node = 0; node < c2c.nodes_cnt; node++) {
1113 DECLARE_BITMAP(set, c2c.cpus_cnt);
1115 bitmap_zero(set, c2c.cpus_cnt);
1116 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1118 if (bitmap_empty(set, c2c.cpus_cnt)) {
1119 if (c2c.node_info == 1) {
1120 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1121 advance_hpp(hpp, ret);
1127 ret = scnprintf(hpp->buf, hpp->size, " ");
1128 advance_hpp(hpp, ret);
1131 switch (c2c.node_info) {
1133 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1134 advance_hpp(hpp, ret);
1138 int num = bitmap_weight(set, c2c.cpus_cnt);
1139 struct c2c_stats *stats = &c2c_he->node_stats[node];
1141 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1142 advance_hpp(hpp, ret);
1144 switch (c2c.display) {
1146 ret = display_metrics(hpp, stats->rmt_hitm,
1147 c2c_he->stats.rmt_hitm);
1150 ret = display_metrics(hpp, stats->lcl_hitm,
1151 c2c_he->stats.lcl_hitm);
1154 ret = display_metrics(hpp, stats->tot_hitm,
1155 c2c_he->stats.tot_hitm);
1161 advance_hpp(hpp, ret);
1163 if (c2c_he->stats.store > 0) {
1164 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1165 percent(stats->store, c2c_he->stats.store));
1167 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1170 advance_hpp(hpp, ret);
1174 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1175 advance_hpp(hpp, ret);
1177 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1178 advance_hpp(hpp, ret);
1180 ret = scnprintf(hpp->buf, hpp->size, "}");
1181 advance_hpp(hpp, ret);
1194 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1195 struct hist_entry *he, double mean)
1197 int width = c2c_width(fmt, hpp, he->hists);
1200 scnprintf(buf, 10, "%6.0f", mean);
1201 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1204 #define MEAN_ENTRY(__func, __val) \
1206 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1208 struct c2c_hist_entry *c2c_he; \
1209 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1210 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1213 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1214 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1215 MEAN_ENTRY(mean_load_entry, load);
1218 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1219 struct hist_entry *he)
1221 struct c2c_hist_entry *c2c_he;
1222 int width = c2c_width(fmt, hpp, he->hists);
1225 c2c_he = container_of(he, struct c2c_hist_entry, he);
1227 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1228 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1232 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1233 struct hist_entry *he)
1235 struct c2c_hist_entry *c2c_he;
1236 int width = c2c_width(fmt, hpp, he->hists);
1239 c2c_he = container_of(he, struct c2c_hist_entry, he);
1241 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1242 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1246 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1247 struct hist_entry *he)
1249 int width = c2c_width(fmt, hpp, he->hists);
1251 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1254 #define HEADER_LOW(__h) \
1261 #define HEADER_BOTH(__h0, __h1) \
1271 #define HEADER_SPAN(__h0, __h1, __s) \
1282 #define HEADER_SPAN_LOW(__h) \
1289 static struct c2c_dimension dim_dcacheline = {
1290 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1291 .name = "dcacheline",
1292 .cmp = dcacheline_cmp,
1293 .entry = dcacheline_entry,
1297 static struct c2c_dimension dim_dcacheline_node = {
1298 .header = HEADER_LOW("Node"),
1299 .name = "dcacheline_node",
1301 .entry = dcacheline_node_entry,
1305 static struct c2c_dimension dim_dcacheline_count = {
1306 .header = HEADER_LOW("PA cnt"),
1307 .name = "dcacheline_count",
1309 .entry = dcacheline_node_count,
1313 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1315 static struct c2c_dimension dim_offset = {
1316 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
1319 .entry = offset_entry,
1323 static struct c2c_dimension dim_offset_node = {
1324 .header = HEADER_LOW("Node"),
1325 .name = "offset_node",
1327 .entry = dcacheline_node_entry,
1331 static struct c2c_dimension dim_iaddr = {
1332 .header = HEADER_LOW("Code address"),
1335 .entry = iaddr_entry,
1339 static struct c2c_dimension dim_tot_hitm = {
1340 .header = HEADER_SPAN("------- Load Hitm -------", "Total", 2),
1342 .cmp = tot_hitm_cmp,
1343 .entry = tot_hitm_entry,
1347 static struct c2c_dimension dim_lcl_hitm = {
1348 .header = HEADER_SPAN_LOW("LclHitm"),
1350 .cmp = lcl_hitm_cmp,
1351 .entry = lcl_hitm_entry,
1355 static struct c2c_dimension dim_rmt_hitm = {
1356 .header = HEADER_SPAN_LOW("RmtHitm"),
1358 .cmp = rmt_hitm_cmp,
1359 .entry = rmt_hitm_entry,
1363 static struct c2c_dimension dim_cl_rmt_hitm = {
1364 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1365 .name = "cl_rmt_hitm",
1366 .cmp = rmt_hitm_cmp,
1367 .entry = rmt_hitm_entry,
1371 static struct c2c_dimension dim_cl_lcl_hitm = {
1372 .header = HEADER_SPAN_LOW("Lcl"),
1373 .name = "cl_lcl_hitm",
1374 .cmp = lcl_hitm_cmp,
1375 .entry = lcl_hitm_entry,
1379 static struct c2c_dimension dim_tot_stores = {
1380 .header = HEADER_BOTH("Total", "Stores"),
1381 .name = "tot_stores",
1383 .entry = store_entry,
1387 static struct c2c_dimension dim_stores_l1hit = {
1388 .header = HEADER_SPAN("--------- Stores --------", "L1Hit", 2),
1389 .name = "stores_l1hit",
1390 .cmp = st_l1hit_cmp,
1391 .entry = st_l1hit_entry,
1395 static struct c2c_dimension dim_stores_l1miss = {
1396 .header = HEADER_SPAN_LOW("L1Miss"),
1397 .name = "stores_l1miss",
1398 .cmp = st_l1miss_cmp,
1399 .entry = st_l1miss_entry,
1403 static struct c2c_dimension dim_stores_na = {
1404 .header = HEADER_SPAN_LOW("N/A"),
1405 .name = "stores_na",
1407 .entry = st_na_entry,
1411 static struct c2c_dimension dim_cl_stores_l1hit = {
1412 .header = HEADER_SPAN("------- Store Refs ------", "L1 Hit", 2),
1413 .name = "cl_stores_l1hit",
1414 .cmp = st_l1hit_cmp,
1415 .entry = st_l1hit_entry,
1419 static struct c2c_dimension dim_cl_stores_l1miss = {
1420 .header = HEADER_SPAN_LOW("L1 Miss"),
1421 .name = "cl_stores_l1miss",
1422 .cmp = st_l1miss_cmp,
1423 .entry = st_l1miss_entry,
1427 static struct c2c_dimension dim_cl_stores_na = {
1428 .header = HEADER_SPAN_LOW("N/A"),
1429 .name = "cl_stores_na",
1431 .entry = st_na_entry,
1435 static struct c2c_dimension dim_ld_fbhit = {
1436 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1438 .cmp = ld_fbhit_cmp,
1439 .entry = ld_fbhit_entry,
1443 static struct c2c_dimension dim_ld_l1hit = {
1444 .header = HEADER_SPAN_LOW("L1"),
1446 .cmp = ld_l1hit_cmp,
1447 .entry = ld_l1hit_entry,
1451 static struct c2c_dimension dim_ld_l2hit = {
1452 .header = HEADER_SPAN_LOW("L2"),
1454 .cmp = ld_l2hit_cmp,
1455 .entry = ld_l2hit_entry,
1459 static struct c2c_dimension dim_ld_llchit = {
1460 .header = HEADER_SPAN("- LLC Load Hit --", "LclHit", 1),
1461 .name = "ld_lclhit",
1462 .cmp = ld_llchit_cmp,
1463 .entry = ld_llchit_entry,
1467 static struct c2c_dimension dim_ld_rmthit = {
1468 .header = HEADER_SPAN("- RMT Load Hit --", "RmtHit", 1),
1469 .name = "ld_rmthit",
1471 .entry = rmt_hit_entry,
1475 static struct c2c_dimension dim_tot_recs = {
1476 .header = HEADER_BOTH("Total", "records"),
1478 .cmp = tot_recs_cmp,
1479 .entry = tot_recs_entry,
1483 static struct c2c_dimension dim_tot_loads = {
1484 .header = HEADER_BOTH("Total", "Loads"),
1485 .name = "tot_loads",
1486 .cmp = tot_loads_cmp,
1487 .entry = tot_loads_entry,
1491 static struct c2c_header percent_hitm_header[] = {
1492 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1493 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1494 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1497 static struct c2c_dimension dim_percent_hitm = {
1498 .name = "percent_hitm",
1499 .cmp = percent_hitm_cmp,
1500 .entry = percent_hitm_entry,
1501 .color = percent_hitm_color,
1505 static struct c2c_dimension dim_percent_rmt_hitm = {
1506 .header = HEADER_SPAN("----- HITM -----", "RmtHitm", 1),
1507 .name = "percent_rmt_hitm",
1508 .cmp = percent_rmt_hitm_cmp,
1509 .entry = percent_rmt_hitm_entry,
1510 .color = percent_rmt_hitm_color,
1514 static struct c2c_dimension dim_percent_lcl_hitm = {
1515 .header = HEADER_SPAN_LOW("LclHitm"),
1516 .name = "percent_lcl_hitm",
1517 .cmp = percent_lcl_hitm_cmp,
1518 .entry = percent_lcl_hitm_entry,
1519 .color = percent_lcl_hitm_color,
1523 static struct c2c_dimension dim_percent_stores_l1hit = {
1524 .header = HEADER_SPAN("------- Store Refs ------", "L1 Hit", 2),
1525 .name = "percent_stores_l1hit",
1526 .cmp = percent_stores_l1hit_cmp,
1527 .entry = percent_stores_l1hit_entry,
1528 .color = percent_stores_l1hit_color,
1532 static struct c2c_dimension dim_percent_stores_l1miss = {
1533 .header = HEADER_SPAN_LOW("L1 Miss"),
1534 .name = "percent_stores_l1miss",
1535 .cmp = percent_stores_l1miss_cmp,
1536 .entry = percent_stores_l1miss_entry,
1537 .color = percent_stores_l1miss_color,
1541 static struct c2c_dimension dim_percent_stores_na = {
1542 .header = HEADER_SPAN_LOW("N/A"),
1543 .name = "percent_stores_na",
1544 .cmp = percent_stores_na_cmp,
1545 .entry = percent_stores_na_entry,
1546 .color = percent_stores_na_color,
1550 static struct c2c_dimension dim_dram_lcl = {
1551 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1553 .cmp = lcl_dram_cmp,
1554 .entry = lcl_dram_entry,
1558 static struct c2c_dimension dim_dram_rmt = {
1559 .header = HEADER_SPAN_LOW("Rmt"),
1561 .cmp = rmt_dram_cmp,
1562 .entry = rmt_dram_entry,
1566 static struct c2c_dimension dim_pid = {
1567 .header = HEADER_LOW("Pid"),
1574 static struct c2c_dimension dim_tid = {
1575 .header = HEADER_LOW("Tid"),
1580 static struct c2c_dimension dim_symbol = {
1585 static struct c2c_dimension dim_dso = {
1586 .header = HEADER_BOTH("Shared", "Object"),
1591 static struct c2c_header header_node[3] = {
1593 HEADER_LOW("Node{cpus %hitms %stores}"),
1594 HEADER_LOW("Node{cpu list}"),
1597 static struct c2c_dimension dim_node = {
1600 .entry = node_entry,
1604 static struct c2c_dimension dim_mean_rmt = {
1605 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1608 .entry = mean_rmt_entry,
1612 static struct c2c_dimension dim_mean_lcl = {
1613 .header = HEADER_SPAN_LOW("lcl hitm"),
1616 .entry = mean_lcl_entry,
1620 static struct c2c_dimension dim_mean_load = {
1621 .header = HEADER_SPAN_LOW("load"),
1622 .name = "mean_load",
1624 .entry = mean_load_entry,
1628 static struct c2c_dimension dim_cpucnt = {
1629 .header = HEADER_BOTH("cpu", "cnt"),
1632 .entry = cpucnt_entry,
1636 static struct c2c_dimension dim_srcline = {
1637 .name = "cl_srcline",
1638 .se = &sort_srcline,
1641 static struct c2c_dimension dim_dcacheline_idx = {
1642 .header = HEADER_LOW("Index"),
1645 .entry = cl_idx_entry,
1649 static struct c2c_dimension dim_dcacheline_num = {
1650 .header = HEADER_LOW("Num"),
1653 .entry = cl_idx_entry,
1657 static struct c2c_dimension dim_dcacheline_num_empty = {
1658 .header = HEADER_LOW("Num"),
1659 .name = "cl_num_empty",
1661 .entry = cl_idx_empty_entry,
1665 static struct c2c_dimension *dimensions[] = {
1667 &dim_dcacheline_node,
1668 &dim_dcacheline_count,
1681 &dim_cl_stores_l1hit,
1682 &dim_cl_stores_l1miss,
1692 &dim_percent_rmt_hitm,
1693 &dim_percent_lcl_hitm,
1694 &dim_percent_stores_l1hit,
1695 &dim_percent_stores_l1miss,
1696 &dim_percent_stores_na,
1709 &dim_dcacheline_idx,
1710 &dim_dcacheline_num,
1711 &dim_dcacheline_num_empty,
1715 static void fmt_free(struct perf_hpp_fmt *fmt)
1717 struct c2c_fmt *c2c_fmt;
1719 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1723 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1725 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1726 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1728 return c2c_a->dim == c2c_b->dim;
1731 static struct c2c_dimension *get_dimension(const char *name)
1735 for (i = 0; dimensions[i]; i++) {
1736 struct c2c_dimension *dim = dimensions[i];
1738 if (!strcmp(dim->name, name))
1745 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1746 struct hist_entry *he)
1748 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1749 struct c2c_dimension *dim = c2c_fmt->dim;
1750 size_t len = fmt->user_len;
1753 len = hists__col_len(he->hists, dim->se->se_width_idx);
1755 if (dim == &dim_symbol || dim == &dim_srcline)
1756 len = symbol_width(he->hists, dim->se);
1759 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1762 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1763 struct hist_entry *a, struct hist_entry *b)
1765 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1766 struct c2c_dimension *dim = c2c_fmt->dim;
1768 return dim->se->se_cmp(a, b);
1771 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1772 struct hist_entry *a, struct hist_entry *b)
1774 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1775 struct c2c_dimension *dim = c2c_fmt->dim;
1776 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1778 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1779 return collapse_fn(a, b);
1782 static struct c2c_fmt *get_format(const char *name)
1784 struct c2c_dimension *dim = get_dimension(name);
1785 struct c2c_fmt *c2c_fmt;
1786 struct perf_hpp_fmt *fmt;
1791 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1797 fmt = &c2c_fmt->fmt;
1798 INIT_LIST_HEAD(&fmt->list);
1799 INIT_LIST_HEAD(&fmt->sort_list);
1801 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1802 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1803 fmt->color = dim->se ? NULL : dim->color;
1804 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1805 fmt->header = c2c_header;
1806 fmt->width = c2c_width;
1807 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1808 fmt->equal = fmt_equal;
1809 fmt->free = fmt_free;
1814 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1816 struct c2c_fmt *c2c_fmt = get_format(name);
1820 return output_field_add(hpp_list, name);
1823 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1827 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1829 struct c2c_fmt *c2c_fmt = get_format(name);
1830 struct c2c_dimension *dim;
1834 return sort_dimension__add(hpp_list, name, NULL, 0);
1838 if (dim == &dim_dso)
1841 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1845 #define PARSE_LIST(_list, _fn) \
1853 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1854 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1855 ret = _fn(hpp_list, tok); \
1856 if (ret == -EINVAL) { \
1857 pr_err("Invalid --fields key: `%s'", tok); \
1859 } else if (ret == -ESRCH) { \
1860 pr_err("Unknown --fields key: `%s'", tok); \
1866 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1867 const char *output_,
1870 char *output = output_ ? strdup(output_) : NULL;
1871 char *sort = sort_ ? strdup(sort_) : NULL;
1874 PARSE_LIST(output, c2c_hists__init_output);
1875 PARSE_LIST(sort, c2c_hists__init_sort);
1877 /* copy sort keys to output fields */
1878 perf_hpp__setup_output_field(hpp_list);
1881 * We dont need other sorting keys other than those
1882 * we already specified. It also really slows down
1883 * the processing a lot with big number of output
1884 * fields, so switching this off for c2c.
1888 /* and then copy output fields to sort keys */
1889 perf_hpp__append_sort_keys(&hists->list);
1897 static int c2c_hists__init(struct c2c_hists *hists,
1899 int nr_header_lines)
1901 __hists__init(&hists->hists, &hists->list);
1904 * Initialize only with sort fields, we need to resort
1905 * later anyway, and that's where we add output fields
1908 perf_hpp_list__init(&hists->list);
1910 /* Overload number of header lines.*/
1911 hists->list.nr_header_lines = nr_header_lines;
1913 return hpp_list__parse(&hists->list, NULL, sort);
1916 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1920 perf_hpp__reset_output_field(&c2c_hists->list);
1921 return hpp_list__parse(&c2c_hists->list, output, sort);
1924 #define DISPLAY_LINE_LIMIT 0.001
1926 static u8 filter_display(u32 val, u32 sum)
1928 if (sum == 0 || ((double)val / sum) < DISPLAY_LINE_LIMIT)
1929 return HIST_FILTER__C2C;
1934 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1936 struct c2c_hist_entry *c2c_he;
1941 c2c_he = container_of(he, struct c2c_hist_entry, he);
1943 switch (c2c.display) {
1945 he->filtered = filter_display(c2c_he->stats.lcl_hitm,
1949 he->filtered = filter_display(c2c_he->stats.rmt_hitm,
1953 he->filtered = filter_display(c2c_he->stats.tot_hitm,
1960 return he->filtered == 0;
1963 static inline bool is_valid_hist_entry(struct hist_entry *he)
1965 struct c2c_hist_entry *c2c_he;
1966 bool has_record = false;
1968 c2c_he = container_of(he, struct c2c_hist_entry, he);
1970 /* It's a valid entry if contains stores */
1971 if (c2c_he->stats.store)
1974 switch (c2c.display) {
1976 has_record = !!c2c_he->stats.lcl_hitm;
1979 has_record = !!c2c_he->stats.rmt_hitm;
1982 has_record = !!c2c_he->stats.tot_hitm;
1991 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1993 struct c2c_dimension *dim;
1995 dim = &c2c.hists == c2c_he->hists ?
1996 &dim_dcacheline_node : &dim_offset_node;
1998 if (len > dim->width)
2002 static int set_nodestr(struct c2c_hist_entry *c2c_he)
2007 if (c2c_he->nodestr)
2010 if (!bitmap_empty(c2c_he->nodeset, c2c.nodes_cnt)) {
2011 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
2014 len = scnprintf(buf, sizeof(buf), "N/A");
2017 set_node_width(c2c_he, len);
2018 c2c_he->nodestr = strdup(buf);
2019 return c2c_he->nodestr ? 0 : -ENOMEM;
2022 static void calc_width(struct c2c_hist_entry *c2c_he)
2024 struct c2c_hists *c2c_hists;
2026 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
2027 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
2028 set_nodestr(c2c_he);
2031 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
2033 struct c2c_hist_entry *c2c_he;
2035 c2c_he = container_of(he, struct c2c_hist_entry, he);
2037 if (c2c.show_src && !he->srcline)
2038 he->srcline = hist_entry__srcline(he);
2042 if (!is_valid_hist_entry(he))
2043 he->filtered = HIST_FILTER__C2C;
2048 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
2050 struct c2c_hist_entry *c2c_he;
2051 struct c2c_hists *c2c_hists;
2052 bool display = he__display(he, &c2c.shared_clines_stats);
2054 c2c_he = container_of(he, struct c2c_hist_entry, he);
2055 c2c_hists = c2c_he->hists;
2057 if (display && c2c_hists) {
2058 static unsigned int idx;
2060 c2c_he->cacheline_idx = idx++;
2063 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2065 hists__collapse_resort(&c2c_hists->hists, NULL);
2066 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2072 static void setup_nodes_header(void)
2074 dim_node.header = header_node[c2c.node_info];
2077 static int setup_nodes(struct perf_session *session)
2079 struct numa_node *n;
2080 unsigned long **nodes;
2082 struct perf_cpu cpu;
2085 if (c2c.node_info > 2)
2088 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2089 c2c.cpus_cnt = session->header.env.nr_cpus_avail;
2091 n = session->header.env.numa_nodes;
2095 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2101 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2105 for (idx = 0; idx < c2c.cpus_cnt; idx++)
2108 c2c.cpu2node = cpu2node;
2110 for (node = 0; node < c2c.nodes_cnt; node++) {
2111 struct perf_cpu_map *map = n[node].map;
2114 set = bitmap_zalloc(c2c.cpus_cnt);
2120 /* empty node, skip */
2121 if (perf_cpu_map__empty(map))
2124 perf_cpu_map__for_each_cpu(cpu, idx, map) {
2125 set_bit(cpu.cpu, set);
2127 if (WARN_ONCE(cpu2node[cpu.cpu] != -1, "node/cpu topology bug"))
2130 cpu2node[cpu.cpu] = node;
2134 setup_nodes_header();
2138 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2140 static int resort_shared_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
2142 struct c2c_hist_entry *c2c_he;
2143 c2c_he = container_of(he, struct c2c_hist_entry, he);
2145 if (HAS_HITMS(c2c_he)) {
2146 c2c.shared_clines++;
2147 c2c_add_stats(&c2c.shared_clines_stats, &c2c_he->stats);
2153 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2155 struct rb_node *next = rb_first_cached(&hists->entries);
2159 struct hist_entry *he;
2161 he = rb_entry(next, struct hist_entry, rb_node);
2165 next = rb_next(&he->rb_node);
2171 static void print_c2c__display_stats(FILE *out)
2174 struct c2c_stats *stats = &c2c.hists.stats;
2176 llc_misses = stats->lcl_dram +
2181 fprintf(out, "=================================================\n");
2182 fprintf(out, " Trace Event Information \n");
2183 fprintf(out, "=================================================\n");
2184 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2185 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2186 fprintf(out, " Load Operations : %10d\n", stats->load);
2187 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2188 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2189 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2190 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2191 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2192 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2193 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2194 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2195 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2196 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2197 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2198 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2199 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2200 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2201 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2202 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2203 fprintf(out, " Load access blocked by data : %10d\n", stats->blk_data);
2204 fprintf(out, " Load access blocked by address : %10d\n", stats->blk_addr);
2205 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2206 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2207 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2208 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2209 fprintf(out, " Store Operations : %10d\n", stats->store);
2210 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2211 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2212 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2213 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2214 fprintf(out, " Store No available memory level : %10d\n", stats->st_na);
2215 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2216 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2219 static void print_shared_cacheline_info(FILE *out)
2221 struct c2c_stats *stats = &c2c.shared_clines_stats;
2222 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2224 fprintf(out, "=================================================\n");
2225 fprintf(out, " Global Shared Cache Line Event Information \n");
2226 fprintf(out, "=================================================\n");
2227 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2228 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2229 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2230 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2231 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2232 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2233 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2234 fprintf(out, " Blocked Access on shared lines : %10d\n", stats->blk_data + stats->blk_addr);
2235 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2236 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2237 fprintf(out, " Store No available memory level : %10d\n", stats->st_na);
2238 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2241 static void print_cacheline(struct c2c_hists *c2c_hists,
2242 struct hist_entry *he_cl,
2243 struct perf_hpp_list *hpp_list,
2247 struct perf_hpp hpp = {
2254 hists__fprintf_headers(&c2c_hists->hists, out);
2260 fprintf(out, " ----------------------------------------------------------------------\n");
2261 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2262 fprintf(out, "%s\n", bf);
2263 fprintf(out, " ----------------------------------------------------------------------\n");
2265 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2268 static void print_pareto(FILE *out)
2270 struct perf_hpp_list hpp_list;
2273 const char *cl_output;
2275 cl_output = "cl_num,"
2283 perf_hpp_list__init(&hpp_list);
2284 ret = hpp_list__parse(&hpp_list, cl_output, NULL);
2286 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2289 nd = rb_first_cached(&c2c.hists.hists.entries);
2291 for (; nd; nd = rb_next(nd)) {
2292 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2293 struct c2c_hist_entry *c2c_he;
2298 c2c_he = container_of(he, struct c2c_hist_entry, he);
2299 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2303 static void print_c2c_info(FILE *out, struct perf_session *session)
2305 struct evlist *evlist = session->evlist;
2306 struct evsel *evsel;
2309 fprintf(out, "=================================================\n");
2310 fprintf(out, " c2c details \n");
2311 fprintf(out, "=================================================\n");
2313 evlist__for_each_entry(evlist, evsel) {
2314 fprintf(out, "%-36s: %s\n", first ? " Events" : "", evsel__name(evsel));
2317 fprintf(out, " Cachelines sort on : %s HITMs\n",
2318 display_str[c2c.display]);
2319 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2322 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2326 print_c2c__display_stats(out);
2328 print_shared_cacheline_info(out);
2330 print_c2c_info(out, session);
2336 fprintf(out, "=================================================\n");
2337 fprintf(out, " Shared Data Cache Line Table \n");
2338 fprintf(out, "=================================================\n");
2339 fprintf(out, "#\n");
2341 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2344 fprintf(out, "=================================================\n");
2345 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2346 fprintf(out, "=================================================\n");
2347 fprintf(out, "#\n");
2352 #ifdef HAVE_SLANG_SUPPORT
2353 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2356 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2359 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2367 hb->nr_non_filtered_entries = nr_entries;
2370 struct c2c_cacheline_browser {
2371 struct hist_browser hb;
2372 struct hist_entry *he;
2376 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2377 char *bf, size_t size)
2379 struct c2c_cacheline_browser *cl_browser;
2380 struct hist_entry *he;
2383 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2384 he = cl_browser->he;
2387 addr = cl_address(he->mem_info->daddr.addr);
2389 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2393 static struct c2c_cacheline_browser*
2394 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2396 struct c2c_cacheline_browser *browser;
2398 browser = zalloc(sizeof(*browser));
2400 hist_browser__init(&browser->hb, hists);
2401 browser->hb.c2c_filter = true;
2402 browser->hb.title = perf_c2c_cacheline_browser__title;
2409 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2411 struct c2c_hist_entry *c2c_he;
2412 struct c2c_hists *c2c_hists;
2413 struct c2c_cacheline_browser *cl_browser;
2414 struct hist_browser *browser;
2416 static const char help[] =
2417 " ENTER Toggle callchains (if present) \n"
2418 " n Toggle Node details info \n"
2419 " s Toggle full length of symbol and source line columns \n"
2420 " q Return back to cacheline list \n";
2425 /* Display compact version first. */
2426 c2c.symbol_full = false;
2428 c2c_he = container_of(he, struct c2c_hist_entry, he);
2429 c2c_hists = c2c_he->hists;
2431 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2432 if (cl_browser == NULL)
2435 browser = &cl_browser->hb;
2437 /* reset abort key so that it can get Ctrl-C as a key */
2439 SLang_init_tty(0, 0, 0);
2441 c2c_browser__update_nr_entries(browser);
2444 key = hist_browser__run(browser, "? - help", true, 0);
2448 c2c.symbol_full = !c2c.symbol_full;
2451 c2c.node_info = (c2c.node_info + 1) % 3;
2452 setup_nodes_header();
2457 ui_browser__help_window(&browser->b, help);
2469 static int perf_c2c_browser__title(struct hist_browser *browser,
2470 char *bf, size_t size)
2473 "Shared Data Cache Line Table "
2474 "(%lu entries, sorted on %s HITMs)",
2475 browser->nr_non_filtered_entries,
2476 display_str[c2c.display]);
2480 static struct hist_browser*
2481 perf_c2c_browser__new(struct hists *hists)
2483 struct hist_browser *browser = hist_browser__new(hists);
2486 browser->title = perf_c2c_browser__title;
2487 browser->c2c_filter = true;
2493 static int perf_c2c__hists_browse(struct hists *hists)
2495 struct hist_browser *browser;
2497 static const char help[] =
2498 " d Display cacheline details \n"
2499 " ENTER Toggle callchains (if present) \n"
2502 browser = perf_c2c_browser__new(hists);
2503 if (browser == NULL)
2506 /* reset abort key so that it can get Ctrl-C as a key */
2508 SLang_init_tty(0, 0, 0);
2510 c2c_browser__update_nr_entries(browser);
2513 key = hist_browser__run(browser, "? - help", true, 0);
2519 perf_c2c__browse_cacheline(browser->he_selection);
2522 ui_browser__help_window(&browser->b, help);
2530 hist_browser__delete(browser);
2534 static void perf_c2c_display(struct perf_session *session)
2536 if (use_browser == 0)
2537 perf_c2c__hists_fprintf(stdout, session);
2539 perf_c2c__hists_browse(&c2c.hists.hists);
2542 static void perf_c2c_display(struct perf_session *session)
2545 perf_c2c__hists_fprintf(stdout, session);
2547 #endif /* HAVE_SLANG_SUPPORT */
2549 static char *fill_line(const char *orig, int len)
2551 int i, j, olen = strlen(orig);
2554 buf = zalloc(len + 1);
2558 j = len / 2 - olen / 2;
2560 for (i = 0; i < j - 1; i++)
2565 strcpy(buf + i, orig);
2571 for (; i < len; i++)
2577 static int ui_quirks(void)
2579 const char *nodestr = "Data address";
2582 if (!c2c.use_stdio) {
2583 dim_offset.width = 5;
2584 dim_offset.header = header_offset_tui;
2588 dim_percent_hitm.header = percent_hitm_header[c2c.display];
2590 /* Fix the zero line for dcacheline column. */
2591 buf = fill_line("Cacheline", dim_dcacheline.width +
2592 dim_dcacheline_node.width +
2593 dim_dcacheline_count.width + 4);
2597 dim_dcacheline.header.line[0].text = buf;
2599 /* Fix the zero line for offset column. */
2600 buf = fill_line(nodestr, dim_offset.width +
2601 dim_offset_node.width +
2602 dim_dcacheline_count.width + 4);
2606 dim_offset.header.line[0].text = buf;
2611 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2613 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2614 CALLCHAIN_REPORT_HELP
2615 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2618 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2620 struct callchain_param *callchain = opt->value;
2622 callchain->enabled = !unset;
2627 symbol_conf.use_callchain = false;
2628 callchain->mode = CHAIN_NONE;
2632 return parse_callchain_report_opt(arg);
2635 static int setup_callchain(struct evlist *evlist)
2637 u64 sample_type = evlist__combined_sample_type(evlist);
2638 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2640 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2641 (sample_type & PERF_SAMPLE_STACK_USER)) {
2642 mode = CALLCHAIN_DWARF;
2643 dwarf_callchain_users = true;
2644 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2645 mode = CALLCHAIN_LBR;
2646 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2647 mode = CALLCHAIN_FP;
2649 if (!callchain_param.enabled &&
2650 callchain_param.mode != CHAIN_NONE &&
2651 mode != CALLCHAIN_NONE) {
2652 symbol_conf.use_callchain = true;
2653 if (callchain_register_param(&callchain_param) < 0) {
2654 ui__error("Can't register callchain params.\n");
2659 if (c2c.stitch_lbr && (mode != CALLCHAIN_LBR)) {
2660 ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
2661 "Please apply --call-graph lbr when recording.\n");
2662 c2c.stitch_lbr = false;
2665 callchain_param.record_mode = mode;
2666 callchain_param.min_percent = 0;
2670 static int setup_display(const char *str)
2672 const char *display = str ?: "tot";
2674 if (!strcmp(display, "tot"))
2675 c2c.display = DISPLAY_TOT;
2676 else if (!strcmp(display, "rmt"))
2677 c2c.display = DISPLAY_RMT;
2678 else if (!strcmp(display, "lcl"))
2679 c2c.display = DISPLAY_LCL;
2681 pr_err("failed: unknown display type: %s\n", str);
2688 #define for_each_token(__tok, __buf, __sep, __tmp) \
2689 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2690 __tok = strtok_r(NULL, __sep, &__tmp))
2692 static int build_cl_output(char *cl_sort, bool no_source)
2694 char *tok, *tmp, *buf = strdup(cl_sort);
2695 bool add_pid = false;
2696 bool add_tid = false;
2697 bool add_iaddr = false;
2698 bool add_sym = false;
2699 bool add_dso = false;
2700 bool add_src = false;
2706 for_each_token(tok, buf, ",", tmp) {
2707 if (!strcmp(tok, "tid")) {
2709 } else if (!strcmp(tok, "pid")) {
2711 } else if (!strcmp(tok, "iaddr")) {
2715 add_src = no_source ? false : true;
2716 } else if (!strcmp(tok, "dso")) {
2718 } else if (strcmp(tok, "offset")) {
2719 pr_err("unrecognized sort token: %s\n", tok);
2725 if (asprintf(&c2c.cl_output,
2726 "%s%s%s%s%s%s%s%s%s%s",
2727 c2c.use_stdio ? "cl_num_empty," : "",
2730 "percent_stores_l1hit,"
2731 "percent_stores_l1miss,"
2732 "percent_stores_na,"
2733 "offset,offset_node,dcacheline_count,",
2734 add_pid ? "pid," : "",
2735 add_tid ? "tid," : "",
2736 add_iaddr ? "iaddr," : "",
2742 add_sym ? "symbol," : "",
2743 add_dso ? "dso," : "",
2744 add_src ? "cl_srcline," : "",
2750 c2c.show_src = add_src;
2756 static int setup_coalesce(const char *coalesce, bool no_source)
2758 const char *c = coalesce ?: coalesce_default;
2760 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2763 if (build_cl_output(c2c.cl_sort, no_source))
2766 if (asprintf(&c2c.cl_resort, "offset,%s",
2767 c2c.display == DISPLAY_TOT ?
2769 c2c.display == DISPLAY_RMT ?
2770 "rmt_hitm,lcl_hitm" :
2771 "lcl_hitm,rmt_hitm") < 0)
2774 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2775 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2776 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2780 static int perf_c2c__report(int argc, const char **argv)
2782 struct itrace_synth_opts itrace_synth_opts = {
2784 .mem = true, /* Only enable memory event */
2785 .default_no_sample = true,
2788 struct perf_session *session;
2789 struct ui_progress prog;
2790 struct perf_data data = {
2791 .mode = PERF_DATA_MODE_READ,
2793 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2794 const char *display = NULL;
2795 const char *coalesce = NULL;
2796 bool no_source = false;
2797 const struct option options[] = {
2798 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2799 "file", "vmlinux pathname"),
2800 OPT_STRING('i', "input", &input_name, "file",
2801 "the input file to process"),
2802 OPT_INCR('N', "node-info", &c2c.node_info,
2803 "show extra node info in report (repeat for more info)"),
2804 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2805 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2806 "Display only statistic tables (implies --stdio)"),
2807 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2808 "Display full length of symbols"),
2809 OPT_BOOLEAN(0, "no-source", &no_source,
2810 "Do not display Source Line column"),
2811 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2812 "Show all captured HITM lines."),
2813 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2814 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2815 callchain_help, &parse_callchain_opt,
2816 callchain_default_opt),
2817 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2818 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2819 "coalesce fields: pid,tid,iaddr,dso"),
2820 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2821 OPT_BOOLEAN(0, "stitch-lbr", &c2c.stitch_lbr,
2822 "Enable LBR callgraph stitching approach"),
2823 OPT_PARENT(c2c_options),
2827 const char *output_str, *sort_str = NULL;
2829 argc = parse_options(argc, argv, options, report_c2c_usage,
2830 PARSE_OPT_STOP_AT_NON_OPTION);
2832 usage_with_options(report_c2c_usage, options);
2834 #ifndef HAVE_SLANG_SUPPORT
2835 c2c.use_stdio = true;
2839 c2c.use_stdio = true;
2841 err = symbol__validate_sym_arguments();
2845 if (!input_name || !strlen(input_name))
2846 input_name = "perf.data";
2848 data.path = input_name;
2849 data.force = symbol_conf.force;
2851 err = setup_display(display);
2855 err = setup_coalesce(coalesce, no_source);
2857 pr_debug("Failed to initialize hists\n");
2861 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2863 pr_debug("Failed to initialize hists\n");
2867 session = perf_session__new(&data, &c2c.tool);
2868 if (IS_ERR(session)) {
2869 err = PTR_ERR(session);
2870 pr_debug("Error creating perf session\n");
2874 session->itrace_synth_opts = &itrace_synth_opts;
2876 err = setup_nodes(session);
2878 pr_err("Failed setup nodes\n");
2882 err = mem2node__init(&c2c.mem2node, &session->header.env);
2886 err = setup_callchain(session->evlist);
2890 if (symbol__init(&session->header.env) < 0)
2893 /* No pipe support at the moment. */
2894 if (perf_data__is_pipe(session->data)) {
2895 pr_debug("No pipe support at the moment.\n");
2904 setup_browser(false);
2906 err = perf_session__process_events(session);
2908 pr_err("failed to process sample\n");
2912 output_str = "cl_idx,"
2917 "tot_hitm,lcl_hitm,rmt_hitm,"
2921 "stores_l1hit,stores_l1miss,stores_na,"
2922 "ld_fbhit,ld_l1hit,ld_l2hit,"
2923 "ld_lclhit,lcl_hitm,"
2924 "ld_rmthit,rmt_hitm,"
2925 "dram_lcl,dram_rmt";
2927 if (c2c.display == DISPLAY_TOT)
2928 sort_str = "tot_hitm";
2929 else if (c2c.display == DISPLAY_RMT)
2930 sort_str = "rmt_hitm";
2931 else if (c2c.display == DISPLAY_LCL)
2932 sort_str = "lcl_hitm";
2934 c2c_hists__reinit(&c2c.hists, output_str, sort_str);
2936 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2938 hists__collapse_resort(&c2c.hists.hists, NULL);
2939 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_shared_cl_cb);
2940 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2942 ui_progress__finish();
2945 pr_err("failed to setup UI\n");
2949 perf_c2c_display(session);
2952 mem2node__exit(&c2c.mem2node);
2954 perf_session__delete(session);
2959 static int parse_record_events(const struct option *opt,
2960 const char *str, int unset __maybe_unused)
2962 bool *event_set = (bool *) opt->value;
2964 if (!strcmp(str, "list")) {
2965 perf_mem_events__list();
2968 if (perf_mem_events__parse(str))
2976 static const char * const __usage_record[] = {
2977 "perf c2c record [<options>] [<command>]",
2978 "perf c2c record [<options>] -- <command> [<options>]",
2982 static const char * const *record_mem_usage = __usage_record;
2984 static int perf_c2c__record(int argc, const char **argv)
2986 int rec_argc, i = 0, j, rec_tmp_nr = 0;
2987 const char **rec_argv;
2990 bool all_user = false, all_kernel = false;
2991 bool event_set = false;
2992 struct perf_mem_event *e;
2993 struct option options[] = {
2994 OPT_CALLBACK('e', "event", &event_set, "event",
2995 "event selector. Use 'perf c2c record -e list' to list available events",
2996 parse_record_events),
2997 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2998 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2999 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
3000 OPT_PARENT(c2c_options),
3004 if (perf_mem_events__init()) {
3005 pr_err("failed: memory events not supported\n");
3009 argc = parse_options(argc, argv, options, record_mem_usage,
3010 PARSE_OPT_KEEP_UNKNOWN);
3012 if (!perf_pmu__has_hybrid())
3013 rec_argc = argc + 11; /* max number of arguments */
3015 rec_argc = argc + 11 * perf_pmu__hybrid_pmu_num();
3017 rec_argv = calloc(rec_argc + 1, sizeof(char *));
3021 rec_tmp = calloc(rec_argc + 1, sizeof(char *));
3027 rec_argv[i++] = "record";
3030 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD_STORE);
3032 * The load and store operations are required, use the event
3033 * PERF_MEM_EVENTS__LOAD_STORE if it is supported.
3038 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
3041 e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE);
3046 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
3048 rec_argv[i++] = "-W";
3050 rec_argv[i++] = "-d";
3051 rec_argv[i++] = "--phys-data";
3052 rec_argv[i++] = "--sample-cpu";
3054 ret = perf_mem_events__record_args(rec_argv, &i, rec_tmp, &rec_tmp_nr);
3059 rec_argv[i++] = "--all-user";
3062 rec_argv[i++] = "--all-kernel";
3064 for (j = 0; j < argc; j++, i++)
3065 rec_argv[i] = argv[j];
3068 pr_debug("calling: ");
3072 while (rec_argv[j]) {
3073 pr_debug("%s ", rec_argv[j]);
3079 ret = cmd_record(i, rec_argv);
3081 for (i = 0; i < rec_tmp_nr; i++)
3089 int cmd_c2c(int argc, const char **argv)
3091 argc = parse_options(argc, argv, c2c_options, c2c_usage,
3092 PARSE_OPT_STOP_AT_NON_OPTION);
3095 usage_with_options(c2c_usage, c2c_options);
3097 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3098 return perf_c2c__record(argc, argv);
3099 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
3100 return perf_c2c__report(argc, argv);
3102 usage_with_options(c2c_usage, c2c_options);