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/kernel.h>
17 #include <linux/stringify.h>
18 #include <linux/zalloc.h>
20 #include <sys/param.h>
23 #include <subcmd/pager.h>
24 #include <subcmd/parse-options.h>
25 #include "map_symbol.h"
26 #include "mem-events.h"
31 #include "cacheline.h"
36 #include "ui/browsers/hists.h"
41 #include "ui/progress.h"
46 struct perf_hpp_list list;
47 struct c2c_stats stats;
50 struct compute_stats {
51 struct stats lcl_hitm;
52 struct stats rmt_hitm;
56 struct c2c_hist_entry {
57 struct c2c_hists *hists;
58 struct c2c_stats stats;
59 unsigned long *cpuset;
60 unsigned long *nodeset;
61 struct c2c_stats *node_stats;
62 unsigned int cacheline_idx;
64 struct compute_stats cstats;
67 unsigned long paddr_cnt;
73 * because of its callchain dynamic entry
78 static char const *coalesce_default = "iaddr";
81 struct perf_tool tool;
82 struct c2c_hists hists;
83 struct mem2node mem2node;
85 unsigned long **nodes;
97 /* HITM shared clines stats */
98 struct c2c_stats hitm_stats;
103 const char *coalesce;
116 static const char *display_str[DISPLAY_MAX] = {
117 [DISPLAY_LCL] = "Local",
118 [DISPLAY_RMT] = "Remote",
119 [DISPLAY_TOT] = "Total",
122 static const struct option c2c_options[] = {
123 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
127 static struct perf_c2c c2c;
129 static void *c2c_he_zalloc(size_t size)
131 struct c2c_hist_entry *c2c_he;
133 c2c_he = zalloc(size + sizeof(*c2c_he));
137 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
141 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
142 if (!c2c_he->nodeset)
145 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
146 if (!c2c_he->node_stats)
149 init_stats(&c2c_he->cstats.lcl_hitm);
150 init_stats(&c2c_he->cstats.rmt_hitm);
151 init_stats(&c2c_he->cstats.load);
156 static void c2c_he_free(void *he)
158 struct c2c_hist_entry *c2c_he;
160 c2c_he = container_of(he, struct c2c_hist_entry, he);
162 hists__delete_entries(&c2c_he->hists->hists);
166 free(c2c_he->cpuset);
167 free(c2c_he->nodeset);
168 free(c2c_he->nodestr);
169 free(c2c_he->node_stats);
173 static struct hist_entry_ops c2c_entry_ops = {
174 .new = c2c_he_zalloc,
178 static int c2c_hists__init(struct c2c_hists *hists,
180 int nr_header_lines);
182 static struct c2c_hists*
183 he__get_c2c_hists(struct hist_entry *he,
187 struct c2c_hist_entry *c2c_he;
188 struct c2c_hists *hists;
191 c2c_he = container_of(he, struct c2c_hist_entry, he);
193 return c2c_he->hists;
195 hists = c2c_he->hists = zalloc(sizeof(*hists));
199 ret = c2c_hists__init(hists, sort, nr_header_lines);
208 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
209 struct perf_sample *sample)
211 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
212 "WARNING: no sample cpu value"))
215 set_bit(sample->cpu, c2c_he->cpuset);
218 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
219 struct perf_sample *sample)
223 if (!sample->phys_addr) {
224 c2c_he->paddr_zero = true;
228 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
229 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
232 set_bit(node, c2c_he->nodeset);
234 if (c2c_he->paddr != sample->phys_addr) {
236 c2c_he->paddr = sample->phys_addr;
240 static void compute_stats(struct c2c_hist_entry *c2c_he,
241 struct c2c_stats *stats,
244 struct compute_stats *cstats = &c2c_he->cstats;
247 update_stats(&cstats->rmt_hitm, weight);
248 else if (stats->lcl_hitm)
249 update_stats(&cstats->lcl_hitm, weight);
250 else if (stats->load)
251 update_stats(&cstats->load, weight);
254 static int process_sample_event(struct perf_tool *tool __maybe_unused,
255 union perf_event *event,
256 struct perf_sample *sample,
258 struct machine *machine)
260 struct c2c_hists *c2c_hists = &c2c.hists;
261 struct c2c_hist_entry *c2c_he;
262 struct c2c_stats stats = { .nr_entries = 0, };
263 struct hist_entry *he;
264 struct addr_location al;
265 struct mem_info *mi, *mi_dup;
268 if (machine__resolve(machine, &al, sample) < 0) {
269 pr_debug("problem processing %d event, skipping it.\n",
274 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
275 evsel, &al, sysctl_perf_event_max_stack);
279 mi = sample__resolve_mem(sample, &al);
284 * The mi object is released in hists__add_entry_ops,
285 * if it gets sorted out into existing data, so we need
286 * to take the copy now.
288 mi_dup = mem_info__get(mi);
290 c2c_decode_stats(&stats, mi);
292 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
298 c2c_he = container_of(he, struct c2c_hist_entry, he);
299 c2c_add_stats(&c2c_he->stats, &stats);
300 c2c_add_stats(&c2c_hists->stats, &stats);
302 c2c_he__set_cpu(c2c_he, sample);
303 c2c_he__set_node(c2c_he, sample);
305 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
306 ret = hist_entry__append_callchain(he, sample);
310 * There's already been warning about missing
311 * sample's cpu value. Let's account all to
312 * node 0 in this case, without any further
315 * Doing node stats only for single callchain data.
317 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
318 int node = c2c.cpu2node[cpu];
322 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
326 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
332 c2c_he = container_of(he, struct c2c_hist_entry, he);
333 c2c_add_stats(&c2c_he->stats, &stats);
334 c2c_add_stats(&c2c_hists->stats, &stats);
335 c2c_add_stats(&c2c_he->node_stats[node], &stats);
337 compute_stats(c2c_he, &stats, sample->weight);
339 c2c_he__set_cpu(c2c_he, sample);
340 c2c_he__set_node(c2c_he, sample);
342 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
343 ret = hist_entry__append_callchain(he, sample);
347 addr_location__put(&al);
351 mem_info__put(mi_dup);
357 static struct perf_c2c c2c = {
359 .sample = process_sample_event,
360 .mmap = perf_event__process_mmap,
361 .mmap2 = perf_event__process_mmap2,
362 .comm = perf_event__process_comm,
363 .exit = perf_event__process_exit,
364 .fork = perf_event__process_fork,
365 .lost = perf_event__process_lost,
366 .ordered_events = true,
367 .ordering_requires_timestamps = true,
371 static const char * const c2c_usage[] = {
372 "perf c2c {record|report}",
376 static const char * const __usage_report[] = {
381 static const char * const *report_c2c_usage = __usage_report;
383 #define C2C_HEADER_MAX 2
389 } line[C2C_HEADER_MAX];
392 struct c2c_dimension {
393 struct c2c_header header;
396 struct sort_entry *se;
398 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
399 struct hist_entry *, struct hist_entry *);
400 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
401 struct hist_entry *he);
402 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
403 struct hist_entry *he);
407 struct perf_hpp_fmt fmt;
408 struct c2c_dimension *dim;
411 #define SYMBOL_WIDTH 30
413 static struct c2c_dimension dim_symbol;
414 static struct c2c_dimension dim_srcline;
416 static int symbol_width(struct hists *hists, struct sort_entry *se)
418 int width = hists__col_len(hists, se->se_width_idx);
420 if (!c2c.symbol_full)
421 width = MIN(width, SYMBOL_WIDTH);
426 static int c2c_width(struct perf_hpp_fmt *fmt,
427 struct perf_hpp *hpp __maybe_unused,
430 struct c2c_fmt *c2c_fmt;
431 struct c2c_dimension *dim;
433 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
436 if (dim == &dim_symbol || dim == &dim_srcline)
437 return symbol_width(hists, dim->se);
439 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
443 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
444 struct hists *hists, int line, int *span)
446 struct perf_hpp_list *hpp_list = hists->hpp_list;
447 struct c2c_fmt *c2c_fmt;
448 struct c2c_dimension *dim;
449 const char *text = NULL;
450 int width = c2c_width(fmt, hpp, hists);
452 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
456 text = dim->header.line[line].text;
457 /* Use the last line from sort_entry if not defined. */
458 if (!text && (line == hpp_list->nr_header_lines - 1))
459 text = dim->se->se_header;
461 text = dim->header.line[line].text;
467 *span = dim->header.line[line].span;
474 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
477 #define HEX_STR(__s, __v) \
479 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
484 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
485 struct hist_entry *left, struct hist_entry *right)
487 return sort__dcacheline_cmp(left, right);
490 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
491 struct hist_entry *he)
494 int width = c2c_width(fmt, hpp, he->hists);
498 addr = cl_address(he->mem_info->daddr.addr);
500 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
504 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
505 struct hist_entry *he)
507 struct c2c_hist_entry *c2c_he;
508 int width = c2c_width(fmt, hpp, he->hists);
510 c2c_he = container_of(he, struct c2c_hist_entry, he);
511 if (WARN_ON_ONCE(!c2c_he->nodestr))
514 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
518 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
519 struct hist_entry *he)
521 struct c2c_hist_entry *c2c_he;
522 int width = c2c_width(fmt, hpp, he->hists);
524 c2c_he = container_of(he, struct c2c_hist_entry, he);
525 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
528 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
529 struct hist_entry *he)
532 int width = c2c_width(fmt, hpp, he->hists);
536 addr = cl_offset(he->mem_info->daddr.al_addr);
538 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
542 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
543 struct hist_entry *left, struct hist_entry *right)
545 uint64_t l = 0, r = 0;
548 l = cl_offset(left->mem_info->daddr.addr);
550 r = cl_offset(right->mem_info->daddr.addr);
552 return (int64_t)(r - l);
556 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
557 struct hist_entry *he)
560 int width = c2c_width(fmt, hpp, he->hists);
564 addr = he->mem_info->iaddr.addr;
566 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
570 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
571 struct hist_entry *left, struct hist_entry *right)
573 return sort__iaddr_cmp(left, right);
577 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
578 struct hist_entry *he)
580 struct c2c_hist_entry *c2c_he;
581 int width = c2c_width(fmt, hpp, he->hists);
582 unsigned int tot_hitm;
584 c2c_he = container_of(he, struct c2c_hist_entry, he);
585 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
587 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
591 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
592 struct hist_entry *left, struct hist_entry *right)
594 struct c2c_hist_entry *c2c_left;
595 struct c2c_hist_entry *c2c_right;
596 unsigned int tot_hitm_left;
597 unsigned int tot_hitm_right;
599 c2c_left = container_of(left, struct c2c_hist_entry, he);
600 c2c_right = container_of(right, struct c2c_hist_entry, he);
602 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
603 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
605 return tot_hitm_left - tot_hitm_right;
608 #define STAT_FN_ENTRY(__f) \
610 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
611 struct hist_entry *he) \
613 struct c2c_hist_entry *c2c_he; \
614 int width = c2c_width(fmt, hpp, he->hists); \
616 c2c_he = container_of(he, struct c2c_hist_entry, he); \
617 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
618 c2c_he->stats.__f); \
621 #define STAT_FN_CMP(__f) \
623 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
624 struct hist_entry *left, struct hist_entry *right) \
626 struct c2c_hist_entry *c2c_left, *c2c_right; \
628 c2c_left = container_of(left, struct c2c_hist_entry, he); \
629 c2c_right = container_of(right, struct c2c_hist_entry, he); \
630 return c2c_left->stats.__f - c2c_right->stats.__f; \
633 #define STAT_FN(__f) \
648 static uint64_t llc_miss(struct c2c_stats *stats)
652 llcmiss = stats->lcl_dram +
661 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
662 struct hist_entry *he)
664 struct c2c_hist_entry *c2c_he;
665 int width = c2c_width(fmt, hpp, he->hists);
667 c2c_he = container_of(he, struct c2c_hist_entry, he);
669 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
670 llc_miss(&c2c_he->stats));
674 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
675 struct hist_entry *left, struct hist_entry *right)
677 struct c2c_hist_entry *c2c_left;
678 struct c2c_hist_entry *c2c_right;
680 c2c_left = container_of(left, struct c2c_hist_entry, he);
681 c2c_right = container_of(right, struct c2c_hist_entry, he);
683 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
686 static uint64_t total_records(struct c2c_stats *stats)
688 uint64_t lclmiss, ldcnt, total;
690 lclmiss = stats->lcl_dram +
710 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
711 struct hist_entry *he)
713 struct c2c_hist_entry *c2c_he;
714 int width = c2c_width(fmt, hpp, he->hists);
717 c2c_he = container_of(he, struct c2c_hist_entry, he);
718 tot_recs = total_records(&c2c_he->stats);
720 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
724 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
725 struct hist_entry *left, struct hist_entry *right)
727 struct c2c_hist_entry *c2c_left;
728 struct c2c_hist_entry *c2c_right;
729 uint64_t tot_recs_left;
730 uint64_t tot_recs_right;
732 c2c_left = container_of(left, struct c2c_hist_entry, he);
733 c2c_right = container_of(right, struct c2c_hist_entry, he);
735 tot_recs_left = total_records(&c2c_left->stats);
736 tot_recs_right = total_records(&c2c_right->stats);
738 return tot_recs_left - tot_recs_right;
741 static uint64_t total_loads(struct c2c_stats *stats)
743 uint64_t lclmiss, ldcnt;
745 lclmiss = stats->lcl_dram +
761 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
762 struct hist_entry *he)
764 struct c2c_hist_entry *c2c_he;
765 int width = c2c_width(fmt, hpp, he->hists);
768 c2c_he = container_of(he, struct c2c_hist_entry, he);
769 tot_recs = total_loads(&c2c_he->stats);
771 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
775 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
776 struct hist_entry *left, struct hist_entry *right)
778 struct c2c_hist_entry *c2c_left;
779 struct c2c_hist_entry *c2c_right;
780 uint64_t tot_recs_left;
781 uint64_t tot_recs_right;
783 c2c_left = container_of(left, struct c2c_hist_entry, he);
784 c2c_right = container_of(right, struct c2c_hist_entry, he);
786 tot_recs_left = total_loads(&c2c_left->stats);
787 tot_recs_right = total_loads(&c2c_right->stats);
789 return tot_recs_left - tot_recs_right;
792 typedef double (get_percent_cb)(struct c2c_hist_entry *);
795 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
796 struct hist_entry *he, get_percent_cb get_percent)
798 struct c2c_hist_entry *c2c_he;
799 int width = c2c_width(fmt, hpp, he->hists);
802 c2c_he = container_of(he, struct c2c_hist_entry, he);
803 per = get_percent(c2c_he);
805 #ifdef HAVE_SLANG_SUPPORT
807 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
809 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
812 static double percent_hitm(struct c2c_hist_entry *c2c_he)
814 struct c2c_hists *hists;
815 struct c2c_stats *stats;
816 struct c2c_stats *total;
820 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
821 stats = &c2c_he->stats;
822 total = &hists->stats;
824 switch (c2c.display) {
826 st = stats->rmt_hitm;
827 tot = total->rmt_hitm;
830 st = stats->lcl_hitm;
831 tot = total->lcl_hitm;
834 st = stats->tot_hitm;
835 tot = total->tot_hitm;
840 p = tot ? (double) st / tot : 0;
845 #define PERC_STR(__s, __v) \
847 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
852 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
853 struct hist_entry *he)
855 struct c2c_hist_entry *c2c_he;
856 int width = c2c_width(fmt, hpp, he->hists);
860 c2c_he = container_of(he, struct c2c_hist_entry, he);
861 per = percent_hitm(c2c_he);
862 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
866 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
867 struct hist_entry *he)
869 return percent_color(fmt, hpp, he, percent_hitm);
873 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
874 struct hist_entry *left, struct hist_entry *right)
876 struct c2c_hist_entry *c2c_left;
877 struct c2c_hist_entry *c2c_right;
881 c2c_left = container_of(left, struct c2c_hist_entry, he);
882 c2c_right = container_of(right, struct c2c_hist_entry, he);
884 per_left = percent_hitm(c2c_left);
885 per_right = percent_hitm(c2c_right);
887 return per_left - per_right;
890 static struct c2c_stats *he_stats(struct hist_entry *he)
892 struct c2c_hist_entry *c2c_he;
894 c2c_he = container_of(he, struct c2c_hist_entry, he);
895 return &c2c_he->stats;
898 static struct c2c_stats *total_stats(struct hist_entry *he)
900 struct c2c_hists *hists;
902 hists = container_of(he->hists, struct c2c_hists, hists);
903 return &hists->stats;
906 static double percent(int st, int tot)
908 return tot ? 100. * (double) st / (double) tot : 0;
911 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
913 #define PERCENT_FN(__f) \
914 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
916 struct c2c_hists *hists; \
918 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
919 return percent(c2c_he->stats.__f, hists->stats.__f); \
925 PERCENT_FN(st_l1miss)
928 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
929 struct hist_entry *he)
931 int width = c2c_width(fmt, hpp, he->hists);
932 double per = PERCENT(he, rmt_hitm);
935 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
939 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
940 struct hist_entry *he)
942 return percent_color(fmt, hpp, he, percent_rmt_hitm);
946 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
947 struct hist_entry *left, struct hist_entry *right)
952 per_left = PERCENT(left, lcl_hitm);
953 per_right = PERCENT(right, lcl_hitm);
955 return per_left - per_right;
959 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
960 struct hist_entry *he)
962 int width = c2c_width(fmt, hpp, he->hists);
963 double per = PERCENT(he, lcl_hitm);
966 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
970 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
971 struct hist_entry *he)
973 return percent_color(fmt, hpp, he, percent_lcl_hitm);
977 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
978 struct hist_entry *left, struct hist_entry *right)
983 per_left = PERCENT(left, lcl_hitm);
984 per_right = PERCENT(right, lcl_hitm);
986 return per_left - per_right;
990 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
991 struct hist_entry *he)
993 int width = c2c_width(fmt, hpp, he->hists);
994 double per = PERCENT(he, st_l1hit);
997 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1001 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1002 struct hist_entry *he)
1004 return percent_color(fmt, hpp, he, percent_st_l1hit);
1008 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1009 struct hist_entry *left, struct hist_entry *right)
1014 per_left = PERCENT(left, st_l1hit);
1015 per_right = PERCENT(right, st_l1hit);
1017 return per_left - per_right;
1021 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1022 struct hist_entry *he)
1024 int width = c2c_width(fmt, hpp, he->hists);
1025 double per = PERCENT(he, st_l1miss);
1028 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1032 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1033 struct hist_entry *he)
1035 return percent_color(fmt, hpp, he, percent_st_l1miss);
1039 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1040 struct hist_entry *left, struct hist_entry *right)
1045 per_left = PERCENT(left, st_l1miss);
1046 per_right = PERCENT(right, st_l1miss);
1048 return per_left - per_right;
1055 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1056 struct hist_entry *he)
1058 int width = c2c_width(fmt, hpp, he->hists);
1060 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1064 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1065 struct hist_entry *left, struct hist_entry *right)
1067 return left->thread->pid_ - right->thread->pid_;
1071 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1072 struct hist_entry *left __maybe_unused,
1073 struct hist_entry *right __maybe_unused)
1079 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1080 struct hist_entry *he)
1082 struct c2c_hist_entry *c2c_he;
1087 c2c_he = container_of(he, struct c2c_hist_entry, he);
1089 for (node = 0; node < c2c.nodes_cnt; node++) {
1090 DECLARE_BITMAP(set, c2c.cpus_cnt);
1092 bitmap_zero(set, c2c.cpus_cnt);
1093 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1095 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1096 if (c2c.node_info == 1) {
1097 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1098 advance_hpp(hpp, ret);
1104 ret = scnprintf(hpp->buf, hpp->size, " ");
1105 advance_hpp(hpp, ret);
1108 switch (c2c.node_info) {
1110 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1111 advance_hpp(hpp, ret);
1115 int num = bitmap_weight(set, c2c.cpus_cnt);
1116 struct c2c_stats *stats = &c2c_he->node_stats[node];
1118 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1119 advance_hpp(hpp, ret);
1121 #define DISPLAY_HITM(__h) \
1122 if (c2c_he->stats.__h> 0) { \
1123 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1124 percent(stats->__h, c2c_he->stats.__h));\
1126 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
1129 switch (c2c.display) {
1131 DISPLAY_HITM(rmt_hitm);
1134 DISPLAY_HITM(lcl_hitm);
1137 DISPLAY_HITM(tot_hitm);
1144 advance_hpp(hpp, ret);
1146 if (c2c_he->stats.store > 0) {
1147 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1148 percent(stats->store, c2c_he->stats.store));
1150 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1153 advance_hpp(hpp, ret);
1157 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1158 advance_hpp(hpp, ret);
1160 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1161 advance_hpp(hpp, ret);
1163 ret = scnprintf(hpp->buf, hpp->size, "}");
1164 advance_hpp(hpp, ret);
1177 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1178 struct hist_entry *he, double mean)
1180 int width = c2c_width(fmt, hpp, he->hists);
1183 scnprintf(buf, 10, "%6.0f", mean);
1184 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1187 #define MEAN_ENTRY(__func, __val) \
1189 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1191 struct c2c_hist_entry *c2c_he; \
1192 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1193 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1196 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1197 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1198 MEAN_ENTRY(mean_load_entry, load);
1201 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1202 struct hist_entry *he)
1204 struct c2c_hist_entry *c2c_he;
1205 int width = c2c_width(fmt, hpp, he->hists);
1208 c2c_he = container_of(he, struct c2c_hist_entry, he);
1210 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1211 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1215 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1216 struct hist_entry *he)
1218 struct c2c_hist_entry *c2c_he;
1219 int width = c2c_width(fmt, hpp, he->hists);
1222 c2c_he = container_of(he, struct c2c_hist_entry, he);
1224 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1225 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1229 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1230 struct hist_entry *he)
1232 int width = c2c_width(fmt, hpp, he->hists);
1234 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1237 #define HEADER_LOW(__h) \
1244 #define HEADER_BOTH(__h0, __h1) \
1254 #define HEADER_SPAN(__h0, __h1, __s) \
1265 #define HEADER_SPAN_LOW(__h) \
1272 static struct c2c_dimension dim_dcacheline = {
1273 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1274 .name = "dcacheline",
1275 .cmp = dcacheline_cmp,
1276 .entry = dcacheline_entry,
1280 static struct c2c_dimension dim_dcacheline_node = {
1281 .header = HEADER_LOW("Node"),
1282 .name = "dcacheline_node",
1284 .entry = dcacheline_node_entry,
1288 static struct c2c_dimension dim_dcacheline_count = {
1289 .header = HEADER_LOW("PA cnt"),
1290 .name = "dcacheline_count",
1292 .entry = dcacheline_node_count,
1296 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1298 static struct c2c_dimension dim_offset = {
1299 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
1302 .entry = offset_entry,
1306 static struct c2c_dimension dim_offset_node = {
1307 .header = HEADER_LOW("Node"),
1308 .name = "offset_node",
1310 .entry = dcacheline_node_entry,
1314 static struct c2c_dimension dim_iaddr = {
1315 .header = HEADER_LOW("Code address"),
1318 .entry = iaddr_entry,
1322 static struct c2c_dimension dim_tot_hitm = {
1323 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1325 .cmp = tot_hitm_cmp,
1326 .entry = tot_hitm_entry,
1330 static struct c2c_dimension dim_lcl_hitm = {
1331 .header = HEADER_SPAN_LOW("Lcl"),
1333 .cmp = lcl_hitm_cmp,
1334 .entry = lcl_hitm_entry,
1338 static struct c2c_dimension dim_rmt_hitm = {
1339 .header = HEADER_SPAN_LOW("Rmt"),
1341 .cmp = rmt_hitm_cmp,
1342 .entry = rmt_hitm_entry,
1346 static struct c2c_dimension dim_cl_rmt_hitm = {
1347 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1348 .name = "cl_rmt_hitm",
1349 .cmp = rmt_hitm_cmp,
1350 .entry = rmt_hitm_entry,
1354 static struct c2c_dimension dim_cl_lcl_hitm = {
1355 .header = HEADER_SPAN_LOW("Lcl"),
1356 .name = "cl_lcl_hitm",
1357 .cmp = lcl_hitm_cmp,
1358 .entry = lcl_hitm_entry,
1362 static struct c2c_dimension dim_stores = {
1363 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1366 .entry = store_entry,
1370 static struct c2c_dimension dim_stores_l1hit = {
1371 .header = HEADER_SPAN_LOW("L1Hit"),
1372 .name = "stores_l1hit",
1373 .cmp = st_l1hit_cmp,
1374 .entry = st_l1hit_entry,
1378 static struct c2c_dimension dim_stores_l1miss = {
1379 .header = HEADER_SPAN_LOW("L1Miss"),
1380 .name = "stores_l1miss",
1381 .cmp = st_l1miss_cmp,
1382 .entry = st_l1miss_entry,
1386 static struct c2c_dimension dim_cl_stores_l1hit = {
1387 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1388 .name = "cl_stores_l1hit",
1389 .cmp = st_l1hit_cmp,
1390 .entry = st_l1hit_entry,
1394 static struct c2c_dimension dim_cl_stores_l1miss = {
1395 .header = HEADER_SPAN_LOW("L1 Miss"),
1396 .name = "cl_stores_l1miss",
1397 .cmp = st_l1miss_cmp,
1398 .entry = st_l1miss_entry,
1402 static struct c2c_dimension dim_ld_fbhit = {
1403 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1405 .cmp = ld_fbhit_cmp,
1406 .entry = ld_fbhit_entry,
1410 static struct c2c_dimension dim_ld_l1hit = {
1411 .header = HEADER_SPAN_LOW("L1"),
1413 .cmp = ld_l1hit_cmp,
1414 .entry = ld_l1hit_entry,
1418 static struct c2c_dimension dim_ld_l2hit = {
1419 .header = HEADER_SPAN_LOW("L2"),
1421 .cmp = ld_l2hit_cmp,
1422 .entry = ld_l2hit_entry,
1426 static struct c2c_dimension dim_ld_llchit = {
1427 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1428 .name = "ld_lclhit",
1429 .cmp = ld_llchit_cmp,
1430 .entry = ld_llchit_entry,
1434 static struct c2c_dimension dim_ld_rmthit = {
1435 .header = HEADER_SPAN_LOW("Rmt"),
1436 .name = "ld_rmthit",
1438 .entry = rmt_hit_entry,
1442 static struct c2c_dimension dim_ld_llcmiss = {
1443 .header = HEADER_BOTH("LLC", "Ld Miss"),
1444 .name = "ld_llcmiss",
1445 .cmp = ld_llcmiss_cmp,
1446 .entry = ld_llcmiss_entry,
1450 static struct c2c_dimension dim_tot_recs = {
1451 .header = HEADER_BOTH("Total", "records"),
1453 .cmp = tot_recs_cmp,
1454 .entry = tot_recs_entry,
1458 static struct c2c_dimension dim_tot_loads = {
1459 .header = HEADER_BOTH("Total", "Loads"),
1460 .name = "tot_loads",
1461 .cmp = tot_loads_cmp,
1462 .entry = tot_loads_entry,
1466 static struct c2c_header percent_hitm_header[] = {
1467 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1468 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1469 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1472 static struct c2c_dimension dim_percent_hitm = {
1473 .name = "percent_hitm",
1474 .cmp = percent_hitm_cmp,
1475 .entry = percent_hitm_entry,
1476 .color = percent_hitm_color,
1480 static struct c2c_dimension dim_percent_rmt_hitm = {
1481 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1482 .name = "percent_rmt_hitm",
1483 .cmp = percent_rmt_hitm_cmp,
1484 .entry = percent_rmt_hitm_entry,
1485 .color = percent_rmt_hitm_color,
1489 static struct c2c_dimension dim_percent_lcl_hitm = {
1490 .header = HEADER_SPAN_LOW("Lcl"),
1491 .name = "percent_lcl_hitm",
1492 .cmp = percent_lcl_hitm_cmp,
1493 .entry = percent_lcl_hitm_entry,
1494 .color = percent_lcl_hitm_color,
1498 static struct c2c_dimension dim_percent_stores_l1hit = {
1499 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1500 .name = "percent_stores_l1hit",
1501 .cmp = percent_stores_l1hit_cmp,
1502 .entry = percent_stores_l1hit_entry,
1503 .color = percent_stores_l1hit_color,
1507 static struct c2c_dimension dim_percent_stores_l1miss = {
1508 .header = HEADER_SPAN_LOW("L1 Miss"),
1509 .name = "percent_stores_l1miss",
1510 .cmp = percent_stores_l1miss_cmp,
1511 .entry = percent_stores_l1miss_entry,
1512 .color = percent_stores_l1miss_color,
1516 static struct c2c_dimension dim_dram_lcl = {
1517 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1519 .cmp = lcl_dram_cmp,
1520 .entry = lcl_dram_entry,
1524 static struct c2c_dimension dim_dram_rmt = {
1525 .header = HEADER_SPAN_LOW("Rmt"),
1527 .cmp = rmt_dram_cmp,
1528 .entry = rmt_dram_entry,
1532 static struct c2c_dimension dim_pid = {
1533 .header = HEADER_LOW("Pid"),
1540 static struct c2c_dimension dim_tid = {
1541 .header = HEADER_LOW("Tid"),
1546 static struct c2c_dimension dim_symbol = {
1551 static struct c2c_dimension dim_dso = {
1552 .header = HEADER_BOTH("Shared", "Object"),
1557 static struct c2c_header header_node[3] = {
1559 HEADER_LOW("Node{cpus %hitms %stores}"),
1560 HEADER_LOW("Node{cpu list}"),
1563 static struct c2c_dimension dim_node = {
1566 .entry = node_entry,
1570 static struct c2c_dimension dim_mean_rmt = {
1571 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1574 .entry = mean_rmt_entry,
1578 static struct c2c_dimension dim_mean_lcl = {
1579 .header = HEADER_SPAN_LOW("lcl hitm"),
1582 .entry = mean_lcl_entry,
1586 static struct c2c_dimension dim_mean_load = {
1587 .header = HEADER_SPAN_LOW("load"),
1588 .name = "mean_load",
1590 .entry = mean_load_entry,
1594 static struct c2c_dimension dim_cpucnt = {
1595 .header = HEADER_BOTH("cpu", "cnt"),
1598 .entry = cpucnt_entry,
1602 static struct c2c_dimension dim_srcline = {
1603 .name = "cl_srcline",
1604 .se = &sort_srcline,
1607 static struct c2c_dimension dim_dcacheline_idx = {
1608 .header = HEADER_LOW("Index"),
1611 .entry = cl_idx_entry,
1615 static struct c2c_dimension dim_dcacheline_num = {
1616 .header = HEADER_LOW("Num"),
1619 .entry = cl_idx_entry,
1623 static struct c2c_dimension dim_dcacheline_num_empty = {
1624 .header = HEADER_LOW("Num"),
1625 .name = "cl_num_empty",
1627 .entry = cl_idx_empty_entry,
1631 static struct c2c_dimension *dimensions[] = {
1633 &dim_dcacheline_node,
1634 &dim_dcacheline_count,
1646 &dim_cl_stores_l1hit,
1647 &dim_cl_stores_l1miss,
1657 &dim_percent_rmt_hitm,
1658 &dim_percent_lcl_hitm,
1659 &dim_percent_stores_l1hit,
1660 &dim_percent_stores_l1miss,
1673 &dim_dcacheline_idx,
1674 &dim_dcacheline_num,
1675 &dim_dcacheline_num_empty,
1679 static void fmt_free(struct perf_hpp_fmt *fmt)
1681 struct c2c_fmt *c2c_fmt;
1683 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1687 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1689 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1690 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1692 return c2c_a->dim == c2c_b->dim;
1695 static struct c2c_dimension *get_dimension(const char *name)
1699 for (i = 0; dimensions[i]; i++) {
1700 struct c2c_dimension *dim = dimensions[i];
1702 if (!strcmp(dim->name, name))
1709 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1710 struct hist_entry *he)
1712 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1713 struct c2c_dimension *dim = c2c_fmt->dim;
1714 size_t len = fmt->user_len;
1717 len = hists__col_len(he->hists, dim->se->se_width_idx);
1719 if (dim == &dim_symbol || dim == &dim_srcline)
1720 len = symbol_width(he->hists, dim->se);
1723 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1726 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1727 struct hist_entry *a, struct hist_entry *b)
1729 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1730 struct c2c_dimension *dim = c2c_fmt->dim;
1732 return dim->se->se_cmp(a, b);
1735 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1736 struct hist_entry *a, struct hist_entry *b)
1738 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1739 struct c2c_dimension *dim = c2c_fmt->dim;
1740 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1742 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1743 return collapse_fn(a, b);
1746 static struct c2c_fmt *get_format(const char *name)
1748 struct c2c_dimension *dim = get_dimension(name);
1749 struct c2c_fmt *c2c_fmt;
1750 struct perf_hpp_fmt *fmt;
1755 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1761 fmt = &c2c_fmt->fmt;
1762 INIT_LIST_HEAD(&fmt->list);
1763 INIT_LIST_HEAD(&fmt->sort_list);
1765 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1766 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1767 fmt->color = dim->se ? NULL : dim->color;
1768 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1769 fmt->header = c2c_header;
1770 fmt->width = c2c_width;
1771 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1772 fmt->equal = fmt_equal;
1773 fmt->free = fmt_free;
1778 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1780 struct c2c_fmt *c2c_fmt = get_format(name);
1784 return output_field_add(hpp_list, name);
1787 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1791 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1793 struct c2c_fmt *c2c_fmt = get_format(name);
1794 struct c2c_dimension *dim;
1798 return sort_dimension__add(hpp_list, name, NULL, 0);
1802 if (dim == &dim_dso)
1805 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1809 #define PARSE_LIST(_list, _fn) \
1817 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1818 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1819 ret = _fn(hpp_list, tok); \
1820 if (ret == -EINVAL) { \
1821 pr_err("Invalid --fields key: `%s'", tok); \
1823 } else if (ret == -ESRCH) { \
1824 pr_err("Unknown --fields key: `%s'", tok); \
1830 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1831 const char *output_,
1834 char *output = output_ ? strdup(output_) : NULL;
1835 char *sort = sort_ ? strdup(sort_) : NULL;
1838 PARSE_LIST(output, c2c_hists__init_output);
1839 PARSE_LIST(sort, c2c_hists__init_sort);
1841 /* copy sort keys to output fields */
1842 perf_hpp__setup_output_field(hpp_list);
1845 * We dont need other sorting keys other than those
1846 * we already specified. It also really slows down
1847 * the processing a lot with big number of output
1848 * fields, so switching this off for c2c.
1852 /* and then copy output fields to sort keys */
1853 perf_hpp__append_sort_keys(&hists->list);
1861 static int c2c_hists__init(struct c2c_hists *hists,
1863 int nr_header_lines)
1865 __hists__init(&hists->hists, &hists->list);
1868 * Initialize only with sort fields, we need to resort
1869 * later anyway, and that's where we add output fields
1872 perf_hpp_list__init(&hists->list);
1874 /* Overload number of header lines.*/
1875 hists->list.nr_header_lines = nr_header_lines;
1877 return hpp_list__parse(&hists->list, NULL, sort);
1880 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1884 perf_hpp__reset_output_field(&c2c_hists->list);
1885 return hpp_list__parse(&c2c_hists->list, output, sort);
1888 #define DISPLAY_LINE_LIMIT 0.001
1890 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1892 struct c2c_hist_entry *c2c_he;
1898 c2c_he = container_of(he, struct c2c_hist_entry, he);
1900 #define FILTER_HITM(__h) \
1902 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1903 if (ld_dist < DISPLAY_LINE_LIMIT) \
1904 he->filtered = HIST_FILTER__C2C; \
1906 he->filtered = HIST_FILTER__C2C; \
1909 switch (c2c.display) {
1911 FILTER_HITM(lcl_hitm);
1914 FILTER_HITM(rmt_hitm);
1917 FILTER_HITM(tot_hitm);
1924 return he->filtered == 0;
1927 static inline int valid_hitm_or_store(struct hist_entry *he)
1929 struct c2c_hist_entry *c2c_he;
1932 c2c_he = container_of(he, struct c2c_hist_entry, he);
1933 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1934 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1935 c2c_he->stats.rmt_hitm;
1936 return has_hitm || c2c_he->stats.store;
1939 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1941 struct c2c_dimension *dim;
1943 dim = &c2c.hists == c2c_he->hists ?
1944 &dim_dcacheline_node : &dim_offset_node;
1946 if (len > dim->width)
1950 static int set_nodestr(struct c2c_hist_entry *c2c_he)
1955 if (c2c_he->nodestr)
1958 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1959 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1962 len = scnprintf(buf, sizeof(buf), "N/A");
1965 set_node_width(c2c_he, len);
1966 c2c_he->nodestr = strdup(buf);
1967 return c2c_he->nodestr ? 0 : -ENOMEM;
1970 static void calc_width(struct c2c_hist_entry *c2c_he)
1972 struct c2c_hists *c2c_hists;
1974 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1975 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
1976 set_nodestr(c2c_he);
1979 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
1981 struct c2c_hist_entry *c2c_he;
1983 c2c_he = container_of(he, struct c2c_hist_entry, he);
1985 if (c2c.show_src && !he->srcline)
1986 he->srcline = hist_entry__srcline(he);
1990 if (!valid_hitm_or_store(he))
1991 he->filtered = HIST_FILTER__C2C;
1996 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
1998 struct c2c_hist_entry *c2c_he;
1999 struct c2c_hists *c2c_hists;
2000 bool display = he__display(he, &c2c.hitm_stats);
2002 c2c_he = container_of(he, struct c2c_hist_entry, he);
2003 c2c_hists = c2c_he->hists;
2005 if (display && c2c_hists) {
2006 static unsigned int idx;
2008 c2c_he->cacheline_idx = idx++;
2011 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2013 hists__collapse_resort(&c2c_hists->hists, NULL);
2014 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2020 static void setup_nodes_header(void)
2022 dim_node.header = header_node[c2c.node_info];
2025 static int setup_nodes(struct perf_session *session)
2027 struct numa_node *n;
2028 unsigned long **nodes;
2032 if (c2c.node_info > 2)
2035 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2036 c2c.cpus_cnt = session->header.env.nr_cpus_avail;
2038 n = session->header.env.numa_nodes;
2042 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2048 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2052 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2055 c2c.cpu2node = cpu2node;
2057 for (node = 0; node < c2c.nodes_cnt; node++) {
2058 struct perf_cpu_map *map = n[node].map;
2061 set = bitmap_alloc(c2c.cpus_cnt);
2067 /* empty node, skip */
2068 if (perf_cpu_map__empty(map))
2071 for (cpu = 0; cpu < map->nr; cpu++) {
2072 set_bit(map->map[cpu], set);
2074 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2077 cpu2node[map->map[cpu]] = node;
2081 setup_nodes_header();
2085 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2087 static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused)
2089 struct c2c_hist_entry *c2c_he;
2090 c2c_he = container_of(he, struct c2c_hist_entry, he);
2092 if (HAS_HITMS(c2c_he)) {
2093 c2c.shared_clines++;
2094 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2100 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2102 struct rb_node *next = rb_first_cached(&hists->entries);
2106 struct hist_entry *he;
2108 he = rb_entry(next, struct hist_entry, rb_node);
2112 next = rb_next(&he->rb_node);
2118 static void print_c2c__display_stats(FILE *out)
2121 struct c2c_stats *stats = &c2c.hists.stats;
2123 llc_misses = stats->lcl_dram +
2128 fprintf(out, "=================================================\n");
2129 fprintf(out, " Trace Event Information \n");
2130 fprintf(out, "=================================================\n");
2131 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2132 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2133 fprintf(out, " Load Operations : %10d\n", stats->load);
2134 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2135 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2136 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2137 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2138 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2139 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2140 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2141 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2142 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2143 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2144 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2145 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2146 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2147 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2148 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2149 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2150 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2151 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2152 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2153 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2154 fprintf(out, " Store Operations : %10d\n", stats->store);
2155 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2156 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2157 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2158 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2159 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2160 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2163 static void print_shared_cacheline_info(FILE *out)
2165 struct c2c_stats *stats = &c2c.hitm_stats;
2166 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2168 fprintf(out, "=================================================\n");
2169 fprintf(out, " Global Shared Cache Line Event Information \n");
2170 fprintf(out, "=================================================\n");
2171 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2172 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2173 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2174 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2175 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2176 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2177 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2178 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2179 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2180 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2183 static void print_cacheline(struct c2c_hists *c2c_hists,
2184 struct hist_entry *he_cl,
2185 struct perf_hpp_list *hpp_list,
2189 struct perf_hpp hpp = {
2196 hists__fprintf_headers(&c2c_hists->hists, out);
2202 fprintf(out, " -------------------------------------------------------------\n");
2203 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2204 fprintf(out, "%s\n", bf);
2205 fprintf(out, " -------------------------------------------------------------\n");
2207 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2210 static void print_pareto(FILE *out)
2212 struct perf_hpp_list hpp_list;
2216 perf_hpp_list__init(&hpp_list);
2217 ret = hpp_list__parse(&hpp_list,
2226 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2229 nd = rb_first_cached(&c2c.hists.hists.entries);
2231 for (; nd; nd = rb_next(nd)) {
2232 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2233 struct c2c_hist_entry *c2c_he;
2238 c2c_he = container_of(he, struct c2c_hist_entry, he);
2239 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2243 static void print_c2c_info(FILE *out, struct perf_session *session)
2245 struct evlist *evlist = session->evlist;
2246 struct evsel *evsel;
2249 fprintf(out, "=================================================\n");
2250 fprintf(out, " c2c details \n");
2251 fprintf(out, "=================================================\n");
2253 evlist__for_each_entry(evlist, evsel) {
2254 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2255 perf_evsel__name(evsel));
2258 fprintf(out, " Cachelines sort on : %s HITMs\n",
2259 display_str[c2c.display]);
2260 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2263 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2267 print_c2c__display_stats(out);
2269 print_shared_cacheline_info(out);
2271 print_c2c_info(out, session);
2277 fprintf(out, "=================================================\n");
2278 fprintf(out, " Shared Data Cache Line Table \n");
2279 fprintf(out, "=================================================\n");
2280 fprintf(out, "#\n");
2282 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2285 fprintf(out, "=================================================\n");
2286 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2287 fprintf(out, "=================================================\n");
2288 fprintf(out, "#\n");
2293 #ifdef HAVE_SLANG_SUPPORT
2294 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2297 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2300 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2308 hb->nr_non_filtered_entries = nr_entries;
2311 struct c2c_cacheline_browser {
2312 struct hist_browser hb;
2313 struct hist_entry *he;
2317 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2318 char *bf, size_t size)
2320 struct c2c_cacheline_browser *cl_browser;
2321 struct hist_entry *he;
2324 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2325 he = cl_browser->he;
2328 addr = cl_address(he->mem_info->daddr.addr);
2330 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2334 static struct c2c_cacheline_browser*
2335 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2337 struct c2c_cacheline_browser *browser;
2339 browser = zalloc(sizeof(*browser));
2341 hist_browser__init(&browser->hb, hists);
2342 browser->hb.c2c_filter = true;
2343 browser->hb.title = perf_c2c_cacheline_browser__title;
2350 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2352 struct c2c_hist_entry *c2c_he;
2353 struct c2c_hists *c2c_hists;
2354 struct c2c_cacheline_browser *cl_browser;
2355 struct hist_browser *browser;
2357 static const char help[] =
2358 " ENTER Toggle callchains (if present) \n"
2359 " n Toggle Node details info \n"
2360 " s Toggle full length of symbol and source line columns \n"
2361 " q Return back to cacheline list \n";
2366 /* Display compact version first. */
2367 c2c.symbol_full = false;
2369 c2c_he = container_of(he, struct c2c_hist_entry, he);
2370 c2c_hists = c2c_he->hists;
2372 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2373 if (cl_browser == NULL)
2376 browser = &cl_browser->hb;
2378 /* reset abort key so that it can get Ctrl-C as a key */
2380 SLang_init_tty(0, 0, 0);
2382 c2c_browser__update_nr_entries(browser);
2385 key = hist_browser__run(browser, "? - help", true);
2389 c2c.symbol_full = !c2c.symbol_full;
2392 c2c.node_info = (c2c.node_info + 1) % 3;
2393 setup_nodes_header();
2398 ui_browser__help_window(&browser->b, help);
2410 static int perf_c2c_browser__title(struct hist_browser *browser,
2411 char *bf, size_t size)
2414 "Shared Data Cache Line Table "
2415 "(%lu entries, sorted on %s HITMs)",
2416 browser->nr_non_filtered_entries,
2417 display_str[c2c.display]);
2421 static struct hist_browser*
2422 perf_c2c_browser__new(struct hists *hists)
2424 struct hist_browser *browser = hist_browser__new(hists);
2427 browser->title = perf_c2c_browser__title;
2428 browser->c2c_filter = true;
2434 static int perf_c2c__hists_browse(struct hists *hists)
2436 struct hist_browser *browser;
2438 static const char help[] =
2439 " d Display cacheline details \n"
2440 " ENTER Toggle callchains (if present) \n"
2443 browser = perf_c2c_browser__new(hists);
2444 if (browser == NULL)
2447 /* reset abort key so that it can get Ctrl-C as a key */
2449 SLang_init_tty(0, 0, 0);
2451 c2c_browser__update_nr_entries(browser);
2454 key = hist_browser__run(browser, "? - help", true);
2460 perf_c2c__browse_cacheline(browser->he_selection);
2463 ui_browser__help_window(&browser->b, help);
2471 hist_browser__delete(browser);
2475 static void perf_c2c_display(struct perf_session *session)
2477 if (use_browser == 0)
2478 perf_c2c__hists_fprintf(stdout, session);
2480 perf_c2c__hists_browse(&c2c.hists.hists);
2483 static void perf_c2c_display(struct perf_session *session)
2486 perf_c2c__hists_fprintf(stdout, session);
2488 #endif /* HAVE_SLANG_SUPPORT */
2490 static char *fill_line(const char *orig, int len)
2492 int i, j, olen = strlen(orig);
2495 buf = zalloc(len + 1);
2499 j = len / 2 - olen / 2;
2501 for (i = 0; i < j - 1; i++)
2506 strcpy(buf + i, orig);
2512 for (; i < len; i++)
2518 static int ui_quirks(void)
2520 const char *nodestr = "Data address";
2523 if (!c2c.use_stdio) {
2524 dim_offset.width = 5;
2525 dim_offset.header = header_offset_tui;
2529 dim_percent_hitm.header = percent_hitm_header[c2c.display];
2531 /* Fix the zero line for dcacheline column. */
2532 buf = fill_line("Cacheline", dim_dcacheline.width +
2533 dim_dcacheline_node.width +
2534 dim_dcacheline_count.width + 4);
2538 dim_dcacheline.header.line[0].text = buf;
2540 /* Fix the zero line for offset column. */
2541 buf = fill_line(nodestr, dim_offset.width +
2542 dim_offset_node.width +
2543 dim_dcacheline_count.width + 4);
2547 dim_offset.header.line[0].text = buf;
2552 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2554 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2555 CALLCHAIN_REPORT_HELP
2556 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2559 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2561 struct callchain_param *callchain = opt->value;
2563 callchain->enabled = !unset;
2568 symbol_conf.use_callchain = false;
2569 callchain->mode = CHAIN_NONE;
2573 return parse_callchain_report_opt(arg);
2576 static int setup_callchain(struct evlist *evlist)
2578 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2579 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2581 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2582 (sample_type & PERF_SAMPLE_STACK_USER)) {
2583 mode = CALLCHAIN_DWARF;
2584 dwarf_callchain_users = true;
2585 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2586 mode = CALLCHAIN_LBR;
2587 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2588 mode = CALLCHAIN_FP;
2590 if (!callchain_param.enabled &&
2591 callchain_param.mode != CHAIN_NONE &&
2592 mode != CALLCHAIN_NONE) {
2593 symbol_conf.use_callchain = true;
2594 if (callchain_register_param(&callchain_param) < 0) {
2595 ui__error("Can't register callchain params.\n");
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;
2640 for_each_token(tok, buf, ",", tmp) {
2641 if (!strcmp(tok, "tid")) {
2643 } else if (!strcmp(tok, "pid")) {
2645 } else if (!strcmp(tok, "iaddr")) {
2649 add_src = no_source ? false : true;
2650 } else if (!strcmp(tok, "dso")) {
2652 } else if (strcmp(tok, "offset")) {
2653 pr_err("unrecognized sort token: %s\n", tok);
2658 if (asprintf(&c2c.cl_output,
2659 "%s%s%s%s%s%s%s%s%s%s",
2660 c2c.use_stdio ? "cl_num_empty," : "",
2663 "percent_stores_l1hit,"
2664 "percent_stores_l1miss,"
2665 "offset,offset_node,dcacheline_count,",
2666 add_pid ? "pid," : "",
2667 add_tid ? "tid," : "",
2668 add_iaddr ? "iaddr," : "",
2674 add_sym ? "symbol," : "",
2675 add_dso ? "dso," : "",
2676 add_src ? "cl_srcline," : "",
2680 c2c.show_src = add_src;
2686 static int setup_coalesce(const char *coalesce, bool no_source)
2688 const char *c = coalesce ?: coalesce_default;
2690 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2693 if (build_cl_output(c2c.cl_sort, no_source))
2696 if (asprintf(&c2c.cl_resort, "offset,%s",
2697 c2c.display == DISPLAY_TOT ?
2699 c2c.display == DISPLAY_RMT ?
2700 "rmt_hitm,lcl_hitm" :
2701 "lcl_hitm,rmt_hitm") < 0)
2704 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2705 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2706 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2710 static int perf_c2c__report(int argc, const char **argv)
2712 struct perf_session *session;
2713 struct ui_progress prog;
2714 struct perf_data data = {
2715 .mode = PERF_DATA_MODE_READ,
2717 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2718 const char *display = NULL;
2719 const char *coalesce = NULL;
2720 bool no_source = false;
2721 const struct option options[] = {
2722 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2723 "file", "vmlinux pathname"),
2724 OPT_STRING('i', "input", &input_name, "file",
2725 "the input file to process"),
2726 OPT_INCR('N', "node-info", &c2c.node_info,
2727 "show extra node info in report (repeat for more info)"),
2728 #ifdef HAVE_SLANG_SUPPORT
2729 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2731 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2732 "Display only statistic tables (implies --stdio)"),
2733 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2734 "Display full length of symbols"),
2735 OPT_BOOLEAN(0, "no-source", &no_source,
2736 "Do not display Source Line column"),
2737 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2738 "Show all captured HITM lines."),
2739 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2740 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2741 callchain_help, &parse_callchain_opt,
2742 callchain_default_opt),
2743 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2744 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2745 "coalesce fields: pid,tid,iaddr,dso"),
2746 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2747 OPT_PARENT(c2c_options),
2752 argc = parse_options(argc, argv, options, report_c2c_usage,
2753 PARSE_OPT_STOP_AT_NON_OPTION);
2755 usage_with_options(report_c2c_usage, options);
2758 c2c.use_stdio = true;
2760 if (!input_name || !strlen(input_name))
2761 input_name = "perf.data";
2763 data.path = input_name;
2764 data.force = symbol_conf.force;
2766 err = setup_display(display);
2770 err = setup_coalesce(coalesce, no_source);
2772 pr_debug("Failed to initialize hists\n");
2776 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2778 pr_debug("Failed to initialize hists\n");
2782 session = perf_session__new(&data, 0, &c2c.tool);
2783 if (session == NULL) {
2784 pr_debug("No memory for session\n");
2788 err = setup_nodes(session);
2790 pr_err("Failed setup nodes\n");
2794 err = mem2node__init(&c2c.mem2node, &session->header.env);
2798 err = setup_callchain(session->evlist);
2802 if (symbol__init(&session->header.env) < 0)
2805 /* No pipe support at the moment. */
2806 if (perf_data__is_pipe(session->data)) {
2807 pr_debug("No pipe support at the moment.\n");
2816 setup_browser(false);
2818 err = perf_session__process_events(session);
2820 pr_err("failed to process sample\n");
2824 c2c_hists__reinit(&c2c.hists,
2831 "tot_hitm,lcl_hitm,rmt_hitm,"
2832 "stores,stores_l1hit,stores_l1miss,"
2833 "dram_lcl,dram_rmt,"
2836 "ld_fbhit,ld_l1hit,ld_l2hit,"
2837 "ld_lclhit,ld_rmthit",
2838 c2c.display == DISPLAY_TOT ? "tot_hitm" :
2839 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
2842 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2844 hists__collapse_resort(&c2c.hists.hists, NULL);
2845 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2846 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2848 ui_progress__finish();
2851 pr_err("failed to setup UI\n");
2855 perf_c2c_display(session);
2858 mem2node__exit(&c2c.mem2node);
2860 perf_session__delete(session);
2865 static int parse_record_events(const struct option *opt,
2866 const char *str, int unset __maybe_unused)
2868 bool *event_set = (bool *) opt->value;
2871 return perf_mem_events__parse(str);
2875 static const char * const __usage_record[] = {
2876 "perf c2c record [<options>] [<command>]",
2877 "perf c2c record [<options>] -- <command> [<options>]",
2881 static const char * const *record_mem_usage = __usage_record;
2883 static int perf_c2c__record(int argc, const char **argv)
2885 int rec_argc, i = 0, j;
2886 const char **rec_argv;
2888 bool all_user = false, all_kernel = false;
2889 bool event_set = false;
2890 struct option options[] = {
2891 OPT_CALLBACK('e', "event", &event_set, "event",
2892 "event selector. Use 'perf mem record -e list' to list available events",
2893 parse_record_events),
2894 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2895 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2896 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2897 OPT_PARENT(c2c_options),
2901 if (perf_mem_events__init()) {
2902 pr_err("failed: memory events not supported\n");
2906 argc = parse_options(argc, argv, options, record_mem_usage,
2907 PARSE_OPT_KEEP_UNKNOWN);
2909 rec_argc = argc + 11; /* max number of arguments */
2910 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2914 rec_argv[i++] = "record";
2917 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2918 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2921 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2922 rec_argv[i++] = "-W";
2924 rec_argv[i++] = "-d";
2925 rec_argv[i++] = "--phys-data";
2926 rec_argv[i++] = "--sample-cpu";
2928 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2929 if (!perf_mem_events[j].record)
2932 if (!perf_mem_events[j].supported) {
2933 pr_err("failed: event '%s' not supported\n",
2934 perf_mem_events[j].name);
2939 rec_argv[i++] = "-e";
2940 rec_argv[i++] = perf_mem_events__name(j);
2944 rec_argv[i++] = "--all-user";
2947 rec_argv[i++] = "--all-kernel";
2949 for (j = 0; j < argc; j++, i++)
2950 rec_argv[i] = argv[j];
2953 pr_debug("calling: ");
2957 while (rec_argv[j]) {
2958 pr_debug("%s ", rec_argv[j]);
2964 ret = cmd_record(i, rec_argv);
2969 int cmd_c2c(int argc, const char **argv)
2971 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2972 PARSE_OPT_STOP_AT_NON_OPTION);
2975 usage_with_options(c2c_usage, c2c_options);
2977 if (!strncmp(argv[0], "rec", 3)) {
2978 return perf_c2c__record(argc, argv);
2979 } else if (!strncmp(argv[0], "rep", 3)) {
2980 return perf_c2c__report(argc, argv);
2982 usage_with_options(c2c_usage, c2c_options);