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 <perf/cpumap.h>
24 #include <subcmd/pager.h>
25 #include <subcmd/parse-options.h>
26 #include "map_symbol.h"
27 #include "mem-events.h"
32 #include "cacheline.h"
37 #include "ui/browsers/hists.h"
42 #include "ui/progress.h"
47 struct perf_hpp_list list;
48 struct c2c_stats stats;
51 struct compute_stats {
52 struct stats lcl_hitm;
53 struct stats rmt_hitm;
57 struct c2c_hist_entry {
58 struct c2c_hists *hists;
59 struct c2c_stats stats;
60 unsigned long *cpuset;
61 unsigned long *nodeset;
62 struct c2c_stats *node_stats;
63 unsigned int cacheline_idx;
65 struct compute_stats cstats;
68 unsigned long paddr_cnt;
74 * because of its callchain dynamic entry
79 static char const *coalesce_default = "iaddr";
82 struct perf_tool tool;
83 struct c2c_hists hists;
84 struct mem2node mem2node;
86 unsigned long **nodes;
98 /* HITM shared clines stats */
99 struct c2c_stats hitm_stats;
104 const char *coalesce;
117 static const char *display_str[DISPLAY_MAX] = {
118 [DISPLAY_LCL] = "Local",
119 [DISPLAY_RMT] = "Remote",
120 [DISPLAY_TOT] = "Total",
123 static const struct option c2c_options[] = {
124 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
128 static struct perf_c2c c2c;
130 static void *c2c_he_zalloc(size_t size)
132 struct c2c_hist_entry *c2c_he;
134 c2c_he = zalloc(size + sizeof(*c2c_he));
138 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
142 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
143 if (!c2c_he->nodeset)
146 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
147 if (!c2c_he->node_stats)
150 init_stats(&c2c_he->cstats.lcl_hitm);
151 init_stats(&c2c_he->cstats.rmt_hitm);
152 init_stats(&c2c_he->cstats.load);
157 static void c2c_he_free(void *he)
159 struct c2c_hist_entry *c2c_he;
161 c2c_he = container_of(he, struct c2c_hist_entry, he);
163 hists__delete_entries(&c2c_he->hists->hists);
167 free(c2c_he->cpuset);
168 free(c2c_he->nodeset);
169 free(c2c_he->nodestr);
170 free(c2c_he->node_stats);
174 static struct hist_entry_ops c2c_entry_ops = {
175 .new = c2c_he_zalloc,
179 static int c2c_hists__init(struct c2c_hists *hists,
181 int nr_header_lines);
183 static struct c2c_hists*
184 he__get_c2c_hists(struct hist_entry *he,
188 struct c2c_hist_entry *c2c_he;
189 struct c2c_hists *hists;
192 c2c_he = container_of(he, struct c2c_hist_entry, he);
194 return c2c_he->hists;
196 hists = c2c_he->hists = zalloc(sizeof(*hists));
200 ret = c2c_hists__init(hists, sort, nr_header_lines);
209 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
210 struct perf_sample *sample)
212 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
213 "WARNING: no sample cpu value"))
216 set_bit(sample->cpu, c2c_he->cpuset);
219 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
220 struct perf_sample *sample)
224 if (!sample->phys_addr) {
225 c2c_he->paddr_zero = true;
229 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
230 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
233 set_bit(node, c2c_he->nodeset);
235 if (c2c_he->paddr != sample->phys_addr) {
237 c2c_he->paddr = sample->phys_addr;
241 static void compute_stats(struct c2c_hist_entry *c2c_he,
242 struct c2c_stats *stats,
245 struct compute_stats *cstats = &c2c_he->cstats;
248 update_stats(&cstats->rmt_hitm, weight);
249 else if (stats->lcl_hitm)
250 update_stats(&cstats->lcl_hitm, weight);
251 else if (stats->load)
252 update_stats(&cstats->load, weight);
255 static int process_sample_event(struct perf_tool *tool __maybe_unused,
256 union perf_event *event,
257 struct perf_sample *sample,
259 struct machine *machine)
261 struct c2c_hists *c2c_hists = &c2c.hists;
262 struct c2c_hist_entry *c2c_he;
263 struct c2c_stats stats = { .nr_entries = 0, };
264 struct hist_entry *he;
265 struct addr_location al;
266 struct mem_info *mi, *mi_dup;
269 if (machine__resolve(machine, &al, sample) < 0) {
270 pr_debug("problem processing %d event, skipping it.\n",
275 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
276 evsel, &al, sysctl_perf_event_max_stack);
280 mi = sample__resolve_mem(sample, &al);
285 * The mi object is released in hists__add_entry_ops,
286 * if it gets sorted out into existing data, so we need
287 * to take the copy now.
289 mi_dup = mem_info__get(mi);
291 c2c_decode_stats(&stats, mi);
293 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
299 c2c_he = container_of(he, struct c2c_hist_entry, he);
300 c2c_add_stats(&c2c_he->stats, &stats);
301 c2c_add_stats(&c2c_hists->stats, &stats);
303 c2c_he__set_cpu(c2c_he, sample);
304 c2c_he__set_node(c2c_he, sample);
306 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
307 ret = hist_entry__append_callchain(he, sample);
311 * There's already been warning about missing
312 * sample's cpu value. Let's account all to
313 * node 0 in this case, without any further
316 * Doing node stats only for single callchain data.
318 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
319 int node = c2c.cpu2node[cpu];
323 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
327 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
333 c2c_he = container_of(he, struct c2c_hist_entry, he);
334 c2c_add_stats(&c2c_he->stats, &stats);
335 c2c_add_stats(&c2c_hists->stats, &stats);
336 c2c_add_stats(&c2c_he->node_stats[node], &stats);
338 compute_stats(c2c_he, &stats, sample->weight);
340 c2c_he__set_cpu(c2c_he, sample);
341 c2c_he__set_node(c2c_he, sample);
343 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
344 ret = hist_entry__append_callchain(he, sample);
348 addr_location__put(&al);
352 mem_info__put(mi_dup);
358 static struct perf_c2c c2c = {
360 .sample = process_sample_event,
361 .mmap = perf_event__process_mmap,
362 .mmap2 = perf_event__process_mmap2,
363 .comm = perf_event__process_comm,
364 .exit = perf_event__process_exit,
365 .fork = perf_event__process_fork,
366 .lost = perf_event__process_lost,
367 .ordered_events = true,
368 .ordering_requires_timestamps = true,
372 static const char * const c2c_usage[] = {
373 "perf c2c {record|report}",
377 static const char * const __usage_report[] = {
382 static const char * const *report_c2c_usage = __usage_report;
384 #define C2C_HEADER_MAX 2
390 } line[C2C_HEADER_MAX];
393 struct c2c_dimension {
394 struct c2c_header header;
397 struct sort_entry *se;
399 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
400 struct hist_entry *, struct hist_entry *);
401 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
402 struct hist_entry *he);
403 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
404 struct hist_entry *he);
408 struct perf_hpp_fmt fmt;
409 struct c2c_dimension *dim;
412 #define SYMBOL_WIDTH 30
414 static struct c2c_dimension dim_symbol;
415 static struct c2c_dimension dim_srcline;
417 static int symbol_width(struct hists *hists, struct sort_entry *se)
419 int width = hists__col_len(hists, se->se_width_idx);
421 if (!c2c.symbol_full)
422 width = MIN(width, SYMBOL_WIDTH);
427 static int c2c_width(struct perf_hpp_fmt *fmt,
428 struct perf_hpp *hpp __maybe_unused,
431 struct c2c_fmt *c2c_fmt;
432 struct c2c_dimension *dim;
434 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
437 if (dim == &dim_symbol || dim == &dim_srcline)
438 return symbol_width(hists, dim->se);
440 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
444 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
445 struct hists *hists, int line, int *span)
447 struct perf_hpp_list *hpp_list = hists->hpp_list;
448 struct c2c_fmt *c2c_fmt;
449 struct c2c_dimension *dim;
450 const char *text = NULL;
451 int width = c2c_width(fmt, hpp, hists);
453 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
457 text = dim->header.line[line].text;
458 /* Use the last line from sort_entry if not defined. */
459 if (!text && (line == hpp_list->nr_header_lines - 1))
460 text = dim->se->se_header;
462 text = dim->header.line[line].text;
468 *span = dim->header.line[line].span;
475 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
478 #define HEX_STR(__s, __v) \
480 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
485 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
486 struct hist_entry *left, struct hist_entry *right)
488 return sort__dcacheline_cmp(left, right);
491 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
492 struct hist_entry *he)
495 int width = c2c_width(fmt, hpp, he->hists);
499 addr = cl_address(he->mem_info->daddr.addr);
501 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
505 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
506 struct hist_entry *he)
508 struct c2c_hist_entry *c2c_he;
509 int width = c2c_width(fmt, hpp, he->hists);
511 c2c_he = container_of(he, struct c2c_hist_entry, he);
512 if (WARN_ON_ONCE(!c2c_he->nodestr))
515 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
519 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
520 struct hist_entry *he)
522 struct c2c_hist_entry *c2c_he;
523 int width = c2c_width(fmt, hpp, he->hists);
525 c2c_he = container_of(he, struct c2c_hist_entry, he);
526 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
529 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
530 struct hist_entry *he)
533 int width = c2c_width(fmt, hpp, he->hists);
537 addr = cl_offset(he->mem_info->daddr.al_addr);
539 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
543 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
544 struct hist_entry *left, struct hist_entry *right)
546 uint64_t l = 0, r = 0;
549 l = cl_offset(left->mem_info->daddr.addr);
551 r = cl_offset(right->mem_info->daddr.addr);
553 return (int64_t)(r - l);
557 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
558 struct hist_entry *he)
561 int width = c2c_width(fmt, hpp, he->hists);
565 addr = he->mem_info->iaddr.addr;
567 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
571 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
572 struct hist_entry *left, struct hist_entry *right)
574 return sort__iaddr_cmp(left, right);
578 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
579 struct hist_entry *he)
581 struct c2c_hist_entry *c2c_he;
582 int width = c2c_width(fmt, hpp, he->hists);
583 unsigned int tot_hitm;
585 c2c_he = container_of(he, struct c2c_hist_entry, he);
586 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
588 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
592 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
593 struct hist_entry *left, struct hist_entry *right)
595 struct c2c_hist_entry *c2c_left;
596 struct c2c_hist_entry *c2c_right;
597 unsigned int tot_hitm_left;
598 unsigned int tot_hitm_right;
600 c2c_left = container_of(left, struct c2c_hist_entry, he);
601 c2c_right = container_of(right, struct c2c_hist_entry, he);
603 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
604 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
606 return tot_hitm_left - tot_hitm_right;
609 #define STAT_FN_ENTRY(__f) \
611 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
612 struct hist_entry *he) \
614 struct c2c_hist_entry *c2c_he; \
615 int width = c2c_width(fmt, hpp, he->hists); \
617 c2c_he = container_of(he, struct c2c_hist_entry, he); \
618 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
619 c2c_he->stats.__f); \
622 #define STAT_FN_CMP(__f) \
624 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
625 struct hist_entry *left, struct hist_entry *right) \
627 struct c2c_hist_entry *c2c_left, *c2c_right; \
629 c2c_left = container_of(left, struct c2c_hist_entry, he); \
630 c2c_right = container_of(right, struct c2c_hist_entry, he); \
631 return c2c_left->stats.__f - c2c_right->stats.__f; \
634 #define STAT_FN(__f) \
649 static uint64_t llc_miss(struct c2c_stats *stats)
653 llcmiss = stats->lcl_dram +
662 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
663 struct hist_entry *he)
665 struct c2c_hist_entry *c2c_he;
666 int width = c2c_width(fmt, hpp, he->hists);
668 c2c_he = container_of(he, struct c2c_hist_entry, he);
670 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
671 llc_miss(&c2c_he->stats));
675 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
676 struct hist_entry *left, struct hist_entry *right)
678 struct c2c_hist_entry *c2c_left;
679 struct c2c_hist_entry *c2c_right;
681 c2c_left = container_of(left, struct c2c_hist_entry, he);
682 c2c_right = container_of(right, struct c2c_hist_entry, he);
684 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
687 static uint64_t total_records(struct c2c_stats *stats)
689 uint64_t lclmiss, ldcnt, total;
691 lclmiss = stats->lcl_dram +
711 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
712 struct hist_entry *he)
714 struct c2c_hist_entry *c2c_he;
715 int width = c2c_width(fmt, hpp, he->hists);
718 c2c_he = container_of(he, struct c2c_hist_entry, he);
719 tot_recs = total_records(&c2c_he->stats);
721 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
725 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
726 struct hist_entry *left, struct hist_entry *right)
728 struct c2c_hist_entry *c2c_left;
729 struct c2c_hist_entry *c2c_right;
730 uint64_t tot_recs_left;
731 uint64_t tot_recs_right;
733 c2c_left = container_of(left, struct c2c_hist_entry, he);
734 c2c_right = container_of(right, struct c2c_hist_entry, he);
736 tot_recs_left = total_records(&c2c_left->stats);
737 tot_recs_right = total_records(&c2c_right->stats);
739 return tot_recs_left - tot_recs_right;
742 static uint64_t total_loads(struct c2c_stats *stats)
744 uint64_t lclmiss, ldcnt;
746 lclmiss = stats->lcl_dram +
762 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
763 struct hist_entry *he)
765 struct c2c_hist_entry *c2c_he;
766 int width = c2c_width(fmt, hpp, he->hists);
769 c2c_he = container_of(he, struct c2c_hist_entry, he);
770 tot_recs = total_loads(&c2c_he->stats);
772 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
776 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
777 struct hist_entry *left, struct hist_entry *right)
779 struct c2c_hist_entry *c2c_left;
780 struct c2c_hist_entry *c2c_right;
781 uint64_t tot_recs_left;
782 uint64_t tot_recs_right;
784 c2c_left = container_of(left, struct c2c_hist_entry, he);
785 c2c_right = container_of(right, struct c2c_hist_entry, he);
787 tot_recs_left = total_loads(&c2c_left->stats);
788 tot_recs_right = total_loads(&c2c_right->stats);
790 return tot_recs_left - tot_recs_right;
793 typedef double (get_percent_cb)(struct c2c_hist_entry *);
796 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
797 struct hist_entry *he, get_percent_cb get_percent)
799 struct c2c_hist_entry *c2c_he;
800 int width = c2c_width(fmt, hpp, he->hists);
803 c2c_he = container_of(he, struct c2c_hist_entry, he);
804 per = get_percent(c2c_he);
806 #ifdef HAVE_SLANG_SUPPORT
808 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
810 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
813 static double percent_hitm(struct c2c_hist_entry *c2c_he)
815 struct c2c_hists *hists;
816 struct c2c_stats *stats;
817 struct c2c_stats *total;
821 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
822 stats = &c2c_he->stats;
823 total = &hists->stats;
825 switch (c2c.display) {
827 st = stats->rmt_hitm;
828 tot = total->rmt_hitm;
831 st = stats->lcl_hitm;
832 tot = total->lcl_hitm;
835 st = stats->tot_hitm;
836 tot = total->tot_hitm;
841 p = tot ? (double) st / tot : 0;
846 #define PERC_STR(__s, __v) \
848 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
853 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
854 struct hist_entry *he)
856 struct c2c_hist_entry *c2c_he;
857 int width = c2c_width(fmt, hpp, he->hists);
861 c2c_he = container_of(he, struct c2c_hist_entry, he);
862 per = percent_hitm(c2c_he);
863 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
867 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
868 struct hist_entry *he)
870 return percent_color(fmt, hpp, he, percent_hitm);
874 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
875 struct hist_entry *left, struct hist_entry *right)
877 struct c2c_hist_entry *c2c_left;
878 struct c2c_hist_entry *c2c_right;
882 c2c_left = container_of(left, struct c2c_hist_entry, he);
883 c2c_right = container_of(right, struct c2c_hist_entry, he);
885 per_left = percent_hitm(c2c_left);
886 per_right = percent_hitm(c2c_right);
888 return per_left - per_right;
891 static struct c2c_stats *he_stats(struct hist_entry *he)
893 struct c2c_hist_entry *c2c_he;
895 c2c_he = container_of(he, struct c2c_hist_entry, he);
896 return &c2c_he->stats;
899 static struct c2c_stats *total_stats(struct hist_entry *he)
901 struct c2c_hists *hists;
903 hists = container_of(he->hists, struct c2c_hists, hists);
904 return &hists->stats;
907 static double percent(int st, int tot)
909 return tot ? 100. * (double) st / (double) tot : 0;
912 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
914 #define PERCENT_FN(__f) \
915 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
917 struct c2c_hists *hists; \
919 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
920 return percent(c2c_he->stats.__f, hists->stats.__f); \
926 PERCENT_FN(st_l1miss)
929 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
930 struct hist_entry *he)
932 int width = c2c_width(fmt, hpp, he->hists);
933 double per = PERCENT(he, rmt_hitm);
936 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
940 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
941 struct hist_entry *he)
943 return percent_color(fmt, hpp, he, percent_rmt_hitm);
947 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
948 struct hist_entry *left, struct hist_entry *right)
953 per_left = PERCENT(left, lcl_hitm);
954 per_right = PERCENT(right, lcl_hitm);
956 return per_left - per_right;
960 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
961 struct hist_entry *he)
963 int width = c2c_width(fmt, hpp, he->hists);
964 double per = PERCENT(he, lcl_hitm);
967 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
971 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
972 struct hist_entry *he)
974 return percent_color(fmt, hpp, he, percent_lcl_hitm);
978 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
979 struct hist_entry *left, struct hist_entry *right)
984 per_left = PERCENT(left, lcl_hitm);
985 per_right = PERCENT(right, lcl_hitm);
987 return per_left - per_right;
991 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
992 struct hist_entry *he)
994 int width = c2c_width(fmt, hpp, he->hists);
995 double per = PERCENT(he, st_l1hit);
998 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1002 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1003 struct hist_entry *he)
1005 return percent_color(fmt, hpp, he, percent_st_l1hit);
1009 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1010 struct hist_entry *left, struct hist_entry *right)
1015 per_left = PERCENT(left, st_l1hit);
1016 per_right = PERCENT(right, st_l1hit);
1018 return per_left - per_right;
1022 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1023 struct hist_entry *he)
1025 int width = c2c_width(fmt, hpp, he->hists);
1026 double per = PERCENT(he, st_l1miss);
1029 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1033 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1034 struct hist_entry *he)
1036 return percent_color(fmt, hpp, he, percent_st_l1miss);
1040 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1041 struct hist_entry *left, struct hist_entry *right)
1046 per_left = PERCENT(left, st_l1miss);
1047 per_right = PERCENT(right, st_l1miss);
1049 return per_left - per_right;
1056 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1057 struct hist_entry *he)
1059 int width = c2c_width(fmt, hpp, he->hists);
1061 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1065 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1066 struct hist_entry *left, struct hist_entry *right)
1068 return left->thread->pid_ - right->thread->pid_;
1072 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1073 struct hist_entry *left __maybe_unused,
1074 struct hist_entry *right __maybe_unused)
1080 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1081 struct hist_entry *he)
1083 struct c2c_hist_entry *c2c_he;
1088 c2c_he = container_of(he, struct c2c_hist_entry, he);
1090 for (node = 0; node < c2c.nodes_cnt; node++) {
1091 DECLARE_BITMAP(set, c2c.cpus_cnt);
1093 bitmap_zero(set, c2c.cpus_cnt);
1094 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1096 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1097 if (c2c.node_info == 1) {
1098 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1099 advance_hpp(hpp, ret);
1105 ret = scnprintf(hpp->buf, hpp->size, " ");
1106 advance_hpp(hpp, ret);
1109 switch (c2c.node_info) {
1111 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1112 advance_hpp(hpp, ret);
1116 int num = bitmap_weight(set, c2c.cpus_cnt);
1117 struct c2c_stats *stats = &c2c_he->node_stats[node];
1119 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1120 advance_hpp(hpp, ret);
1122 #define DISPLAY_HITM(__h) \
1123 if (c2c_he->stats.__h> 0) { \
1124 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1125 percent(stats->__h, c2c_he->stats.__h));\
1127 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
1130 switch (c2c.display) {
1132 DISPLAY_HITM(rmt_hitm);
1135 DISPLAY_HITM(lcl_hitm);
1138 DISPLAY_HITM(tot_hitm);
1145 advance_hpp(hpp, ret);
1147 if (c2c_he->stats.store > 0) {
1148 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1149 percent(stats->store, c2c_he->stats.store));
1151 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1154 advance_hpp(hpp, ret);
1158 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1159 advance_hpp(hpp, ret);
1161 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1162 advance_hpp(hpp, ret);
1164 ret = scnprintf(hpp->buf, hpp->size, "}");
1165 advance_hpp(hpp, ret);
1178 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1179 struct hist_entry *he, double mean)
1181 int width = c2c_width(fmt, hpp, he->hists);
1184 scnprintf(buf, 10, "%6.0f", mean);
1185 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1188 #define MEAN_ENTRY(__func, __val) \
1190 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1192 struct c2c_hist_entry *c2c_he; \
1193 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1194 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1197 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1198 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1199 MEAN_ENTRY(mean_load_entry, load);
1202 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1203 struct hist_entry *he)
1205 struct c2c_hist_entry *c2c_he;
1206 int width = c2c_width(fmt, hpp, he->hists);
1209 c2c_he = container_of(he, struct c2c_hist_entry, he);
1211 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1212 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1216 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1217 struct hist_entry *he)
1219 struct c2c_hist_entry *c2c_he;
1220 int width = c2c_width(fmt, hpp, he->hists);
1223 c2c_he = container_of(he, struct c2c_hist_entry, he);
1225 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1226 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1230 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1231 struct hist_entry *he)
1233 int width = c2c_width(fmt, hpp, he->hists);
1235 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1238 #define HEADER_LOW(__h) \
1245 #define HEADER_BOTH(__h0, __h1) \
1255 #define HEADER_SPAN(__h0, __h1, __s) \
1266 #define HEADER_SPAN_LOW(__h) \
1273 static struct c2c_dimension dim_dcacheline = {
1274 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1275 .name = "dcacheline",
1276 .cmp = dcacheline_cmp,
1277 .entry = dcacheline_entry,
1281 static struct c2c_dimension dim_dcacheline_node = {
1282 .header = HEADER_LOW("Node"),
1283 .name = "dcacheline_node",
1285 .entry = dcacheline_node_entry,
1289 static struct c2c_dimension dim_dcacheline_count = {
1290 .header = HEADER_LOW("PA cnt"),
1291 .name = "dcacheline_count",
1293 .entry = dcacheline_node_count,
1297 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1299 static struct c2c_dimension dim_offset = {
1300 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
1303 .entry = offset_entry,
1307 static struct c2c_dimension dim_offset_node = {
1308 .header = HEADER_LOW("Node"),
1309 .name = "offset_node",
1311 .entry = dcacheline_node_entry,
1315 static struct c2c_dimension dim_iaddr = {
1316 .header = HEADER_LOW("Code address"),
1319 .entry = iaddr_entry,
1323 static struct c2c_dimension dim_tot_hitm = {
1324 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1326 .cmp = tot_hitm_cmp,
1327 .entry = tot_hitm_entry,
1331 static struct c2c_dimension dim_lcl_hitm = {
1332 .header = HEADER_SPAN_LOW("Lcl"),
1334 .cmp = lcl_hitm_cmp,
1335 .entry = lcl_hitm_entry,
1339 static struct c2c_dimension dim_rmt_hitm = {
1340 .header = HEADER_SPAN_LOW("Rmt"),
1342 .cmp = rmt_hitm_cmp,
1343 .entry = rmt_hitm_entry,
1347 static struct c2c_dimension dim_cl_rmt_hitm = {
1348 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1349 .name = "cl_rmt_hitm",
1350 .cmp = rmt_hitm_cmp,
1351 .entry = rmt_hitm_entry,
1355 static struct c2c_dimension dim_cl_lcl_hitm = {
1356 .header = HEADER_SPAN_LOW("Lcl"),
1357 .name = "cl_lcl_hitm",
1358 .cmp = lcl_hitm_cmp,
1359 .entry = lcl_hitm_entry,
1363 static struct c2c_dimension dim_stores = {
1364 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1367 .entry = store_entry,
1371 static struct c2c_dimension dim_stores_l1hit = {
1372 .header = HEADER_SPAN_LOW("L1Hit"),
1373 .name = "stores_l1hit",
1374 .cmp = st_l1hit_cmp,
1375 .entry = st_l1hit_entry,
1379 static struct c2c_dimension dim_stores_l1miss = {
1380 .header = HEADER_SPAN_LOW("L1Miss"),
1381 .name = "stores_l1miss",
1382 .cmp = st_l1miss_cmp,
1383 .entry = st_l1miss_entry,
1387 static struct c2c_dimension dim_cl_stores_l1hit = {
1388 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1389 .name = "cl_stores_l1hit",
1390 .cmp = st_l1hit_cmp,
1391 .entry = st_l1hit_entry,
1395 static struct c2c_dimension dim_cl_stores_l1miss = {
1396 .header = HEADER_SPAN_LOW("L1 Miss"),
1397 .name = "cl_stores_l1miss",
1398 .cmp = st_l1miss_cmp,
1399 .entry = st_l1miss_entry,
1403 static struct c2c_dimension dim_ld_fbhit = {
1404 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1406 .cmp = ld_fbhit_cmp,
1407 .entry = ld_fbhit_entry,
1411 static struct c2c_dimension dim_ld_l1hit = {
1412 .header = HEADER_SPAN_LOW("L1"),
1414 .cmp = ld_l1hit_cmp,
1415 .entry = ld_l1hit_entry,
1419 static struct c2c_dimension dim_ld_l2hit = {
1420 .header = HEADER_SPAN_LOW("L2"),
1422 .cmp = ld_l2hit_cmp,
1423 .entry = ld_l2hit_entry,
1427 static struct c2c_dimension dim_ld_llchit = {
1428 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1429 .name = "ld_lclhit",
1430 .cmp = ld_llchit_cmp,
1431 .entry = ld_llchit_entry,
1435 static struct c2c_dimension dim_ld_rmthit = {
1436 .header = HEADER_SPAN_LOW("Rmt"),
1437 .name = "ld_rmthit",
1439 .entry = rmt_hit_entry,
1443 static struct c2c_dimension dim_ld_llcmiss = {
1444 .header = HEADER_BOTH("LLC", "Ld Miss"),
1445 .name = "ld_llcmiss",
1446 .cmp = ld_llcmiss_cmp,
1447 .entry = ld_llcmiss_entry,
1451 static struct c2c_dimension dim_tot_recs = {
1452 .header = HEADER_BOTH("Total", "records"),
1454 .cmp = tot_recs_cmp,
1455 .entry = tot_recs_entry,
1459 static struct c2c_dimension dim_tot_loads = {
1460 .header = HEADER_BOTH("Total", "Loads"),
1461 .name = "tot_loads",
1462 .cmp = tot_loads_cmp,
1463 .entry = tot_loads_entry,
1467 static struct c2c_header percent_hitm_header[] = {
1468 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1469 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1470 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1473 static struct c2c_dimension dim_percent_hitm = {
1474 .name = "percent_hitm",
1475 .cmp = percent_hitm_cmp,
1476 .entry = percent_hitm_entry,
1477 .color = percent_hitm_color,
1481 static struct c2c_dimension dim_percent_rmt_hitm = {
1482 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1483 .name = "percent_rmt_hitm",
1484 .cmp = percent_rmt_hitm_cmp,
1485 .entry = percent_rmt_hitm_entry,
1486 .color = percent_rmt_hitm_color,
1490 static struct c2c_dimension dim_percent_lcl_hitm = {
1491 .header = HEADER_SPAN_LOW("Lcl"),
1492 .name = "percent_lcl_hitm",
1493 .cmp = percent_lcl_hitm_cmp,
1494 .entry = percent_lcl_hitm_entry,
1495 .color = percent_lcl_hitm_color,
1499 static struct c2c_dimension dim_percent_stores_l1hit = {
1500 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1501 .name = "percent_stores_l1hit",
1502 .cmp = percent_stores_l1hit_cmp,
1503 .entry = percent_stores_l1hit_entry,
1504 .color = percent_stores_l1hit_color,
1508 static struct c2c_dimension dim_percent_stores_l1miss = {
1509 .header = HEADER_SPAN_LOW("L1 Miss"),
1510 .name = "percent_stores_l1miss",
1511 .cmp = percent_stores_l1miss_cmp,
1512 .entry = percent_stores_l1miss_entry,
1513 .color = percent_stores_l1miss_color,
1517 static struct c2c_dimension dim_dram_lcl = {
1518 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1520 .cmp = lcl_dram_cmp,
1521 .entry = lcl_dram_entry,
1525 static struct c2c_dimension dim_dram_rmt = {
1526 .header = HEADER_SPAN_LOW("Rmt"),
1528 .cmp = rmt_dram_cmp,
1529 .entry = rmt_dram_entry,
1533 static struct c2c_dimension dim_pid = {
1534 .header = HEADER_LOW("Pid"),
1541 static struct c2c_dimension dim_tid = {
1542 .header = HEADER_LOW("Tid"),
1547 static struct c2c_dimension dim_symbol = {
1552 static struct c2c_dimension dim_dso = {
1553 .header = HEADER_BOTH("Shared", "Object"),
1558 static struct c2c_header header_node[3] = {
1560 HEADER_LOW("Node{cpus %hitms %stores}"),
1561 HEADER_LOW("Node{cpu list}"),
1564 static struct c2c_dimension dim_node = {
1567 .entry = node_entry,
1571 static struct c2c_dimension dim_mean_rmt = {
1572 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1575 .entry = mean_rmt_entry,
1579 static struct c2c_dimension dim_mean_lcl = {
1580 .header = HEADER_SPAN_LOW("lcl hitm"),
1583 .entry = mean_lcl_entry,
1587 static struct c2c_dimension dim_mean_load = {
1588 .header = HEADER_SPAN_LOW("load"),
1589 .name = "mean_load",
1591 .entry = mean_load_entry,
1595 static struct c2c_dimension dim_cpucnt = {
1596 .header = HEADER_BOTH("cpu", "cnt"),
1599 .entry = cpucnt_entry,
1603 static struct c2c_dimension dim_srcline = {
1604 .name = "cl_srcline",
1605 .se = &sort_srcline,
1608 static struct c2c_dimension dim_dcacheline_idx = {
1609 .header = HEADER_LOW("Index"),
1612 .entry = cl_idx_entry,
1616 static struct c2c_dimension dim_dcacheline_num = {
1617 .header = HEADER_LOW("Num"),
1620 .entry = cl_idx_entry,
1624 static struct c2c_dimension dim_dcacheline_num_empty = {
1625 .header = HEADER_LOW("Num"),
1626 .name = "cl_num_empty",
1628 .entry = cl_idx_empty_entry,
1632 static struct c2c_dimension *dimensions[] = {
1634 &dim_dcacheline_node,
1635 &dim_dcacheline_count,
1647 &dim_cl_stores_l1hit,
1648 &dim_cl_stores_l1miss,
1658 &dim_percent_rmt_hitm,
1659 &dim_percent_lcl_hitm,
1660 &dim_percent_stores_l1hit,
1661 &dim_percent_stores_l1miss,
1674 &dim_dcacheline_idx,
1675 &dim_dcacheline_num,
1676 &dim_dcacheline_num_empty,
1680 static void fmt_free(struct perf_hpp_fmt *fmt)
1682 struct c2c_fmt *c2c_fmt;
1684 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1688 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1690 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1691 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1693 return c2c_a->dim == c2c_b->dim;
1696 static struct c2c_dimension *get_dimension(const char *name)
1700 for (i = 0; dimensions[i]; i++) {
1701 struct c2c_dimension *dim = dimensions[i];
1703 if (!strcmp(dim->name, name))
1710 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1711 struct hist_entry *he)
1713 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1714 struct c2c_dimension *dim = c2c_fmt->dim;
1715 size_t len = fmt->user_len;
1718 len = hists__col_len(he->hists, dim->se->se_width_idx);
1720 if (dim == &dim_symbol || dim == &dim_srcline)
1721 len = symbol_width(he->hists, dim->se);
1724 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1727 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1728 struct hist_entry *a, struct hist_entry *b)
1730 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1731 struct c2c_dimension *dim = c2c_fmt->dim;
1733 return dim->se->se_cmp(a, b);
1736 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1737 struct hist_entry *a, struct hist_entry *b)
1739 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1740 struct c2c_dimension *dim = c2c_fmt->dim;
1741 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1743 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1744 return collapse_fn(a, b);
1747 static struct c2c_fmt *get_format(const char *name)
1749 struct c2c_dimension *dim = get_dimension(name);
1750 struct c2c_fmt *c2c_fmt;
1751 struct perf_hpp_fmt *fmt;
1756 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1762 fmt = &c2c_fmt->fmt;
1763 INIT_LIST_HEAD(&fmt->list);
1764 INIT_LIST_HEAD(&fmt->sort_list);
1766 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1767 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1768 fmt->color = dim->se ? NULL : dim->color;
1769 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1770 fmt->header = c2c_header;
1771 fmt->width = c2c_width;
1772 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1773 fmt->equal = fmt_equal;
1774 fmt->free = fmt_free;
1779 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1781 struct c2c_fmt *c2c_fmt = get_format(name);
1785 return output_field_add(hpp_list, name);
1788 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1792 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1794 struct c2c_fmt *c2c_fmt = get_format(name);
1795 struct c2c_dimension *dim;
1799 return sort_dimension__add(hpp_list, name, NULL, 0);
1803 if (dim == &dim_dso)
1806 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1810 #define PARSE_LIST(_list, _fn) \
1818 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1819 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1820 ret = _fn(hpp_list, tok); \
1821 if (ret == -EINVAL) { \
1822 pr_err("Invalid --fields key: `%s'", tok); \
1824 } else if (ret == -ESRCH) { \
1825 pr_err("Unknown --fields key: `%s'", tok); \
1831 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1832 const char *output_,
1835 char *output = output_ ? strdup(output_) : NULL;
1836 char *sort = sort_ ? strdup(sort_) : NULL;
1839 PARSE_LIST(output, c2c_hists__init_output);
1840 PARSE_LIST(sort, c2c_hists__init_sort);
1842 /* copy sort keys to output fields */
1843 perf_hpp__setup_output_field(hpp_list);
1846 * We dont need other sorting keys other than those
1847 * we already specified. It also really slows down
1848 * the processing a lot with big number of output
1849 * fields, so switching this off for c2c.
1853 /* and then copy output fields to sort keys */
1854 perf_hpp__append_sort_keys(&hists->list);
1862 static int c2c_hists__init(struct c2c_hists *hists,
1864 int nr_header_lines)
1866 __hists__init(&hists->hists, &hists->list);
1869 * Initialize only with sort fields, we need to resort
1870 * later anyway, and that's where we add output fields
1873 perf_hpp_list__init(&hists->list);
1875 /* Overload number of header lines.*/
1876 hists->list.nr_header_lines = nr_header_lines;
1878 return hpp_list__parse(&hists->list, NULL, sort);
1881 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1885 perf_hpp__reset_output_field(&c2c_hists->list);
1886 return hpp_list__parse(&c2c_hists->list, output, sort);
1889 #define DISPLAY_LINE_LIMIT 0.001
1891 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1893 struct c2c_hist_entry *c2c_he;
1899 c2c_he = container_of(he, struct c2c_hist_entry, he);
1901 #define FILTER_HITM(__h) \
1903 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1904 if (ld_dist < DISPLAY_LINE_LIMIT) \
1905 he->filtered = HIST_FILTER__C2C; \
1907 he->filtered = HIST_FILTER__C2C; \
1910 switch (c2c.display) {
1912 FILTER_HITM(lcl_hitm);
1915 FILTER_HITM(rmt_hitm);
1918 FILTER_HITM(tot_hitm);
1925 return he->filtered == 0;
1928 static inline int valid_hitm_or_store(struct hist_entry *he)
1930 struct c2c_hist_entry *c2c_he;
1933 c2c_he = container_of(he, struct c2c_hist_entry, he);
1934 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1935 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1936 c2c_he->stats.rmt_hitm;
1937 return has_hitm || c2c_he->stats.store;
1940 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1942 struct c2c_dimension *dim;
1944 dim = &c2c.hists == c2c_he->hists ?
1945 &dim_dcacheline_node : &dim_offset_node;
1947 if (len > dim->width)
1951 static int set_nodestr(struct c2c_hist_entry *c2c_he)
1956 if (c2c_he->nodestr)
1959 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1960 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1963 len = scnprintf(buf, sizeof(buf), "N/A");
1966 set_node_width(c2c_he, len);
1967 c2c_he->nodestr = strdup(buf);
1968 return c2c_he->nodestr ? 0 : -ENOMEM;
1971 static void calc_width(struct c2c_hist_entry *c2c_he)
1973 struct c2c_hists *c2c_hists;
1975 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1976 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
1977 set_nodestr(c2c_he);
1980 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
1982 struct c2c_hist_entry *c2c_he;
1984 c2c_he = container_of(he, struct c2c_hist_entry, he);
1986 if (c2c.show_src && !he->srcline)
1987 he->srcline = hist_entry__srcline(he);
1991 if (!valid_hitm_or_store(he))
1992 he->filtered = HIST_FILTER__C2C;
1997 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
1999 struct c2c_hist_entry *c2c_he;
2000 struct c2c_hists *c2c_hists;
2001 bool display = he__display(he, &c2c.hitm_stats);
2003 c2c_he = container_of(he, struct c2c_hist_entry, he);
2004 c2c_hists = c2c_he->hists;
2006 if (display && c2c_hists) {
2007 static unsigned int idx;
2009 c2c_he->cacheline_idx = idx++;
2012 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2014 hists__collapse_resort(&c2c_hists->hists, NULL);
2015 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2021 static void setup_nodes_header(void)
2023 dim_node.header = header_node[c2c.node_info];
2026 static int setup_nodes(struct perf_session *session)
2028 struct numa_node *n;
2029 unsigned long **nodes;
2033 if (c2c.node_info > 2)
2036 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2037 c2c.cpus_cnt = session->header.env.nr_cpus_avail;
2039 n = session->header.env.numa_nodes;
2043 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2049 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2053 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2056 c2c.cpu2node = cpu2node;
2058 for (node = 0; node < c2c.nodes_cnt; node++) {
2059 struct perf_cpu_map *map = n[node].map;
2062 set = bitmap_alloc(c2c.cpus_cnt);
2068 /* empty node, skip */
2069 if (perf_cpu_map__empty(map))
2072 for (cpu = 0; cpu < map->nr; cpu++) {
2073 set_bit(map->map[cpu], set);
2075 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2078 cpu2node[map->map[cpu]] = node;
2082 setup_nodes_header();
2086 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2088 static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused)
2090 struct c2c_hist_entry *c2c_he;
2091 c2c_he = container_of(he, struct c2c_hist_entry, he);
2093 if (HAS_HITMS(c2c_he)) {
2094 c2c.shared_clines++;
2095 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2101 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2103 struct rb_node *next = rb_first_cached(&hists->entries);
2107 struct hist_entry *he;
2109 he = rb_entry(next, struct hist_entry, rb_node);
2113 next = rb_next(&he->rb_node);
2119 static void print_c2c__display_stats(FILE *out)
2122 struct c2c_stats *stats = &c2c.hists.stats;
2124 llc_misses = stats->lcl_dram +
2129 fprintf(out, "=================================================\n");
2130 fprintf(out, " Trace Event Information \n");
2131 fprintf(out, "=================================================\n");
2132 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2133 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2134 fprintf(out, " Load Operations : %10d\n", stats->load);
2135 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2136 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2137 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2138 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2139 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2140 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2141 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2142 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2143 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2144 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2145 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2146 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2147 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2148 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2149 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2150 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2151 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2152 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2153 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2154 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2155 fprintf(out, " Store Operations : %10d\n", stats->store);
2156 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2157 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2158 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2159 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2160 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2161 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2164 static void print_shared_cacheline_info(FILE *out)
2166 struct c2c_stats *stats = &c2c.hitm_stats;
2167 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2169 fprintf(out, "=================================================\n");
2170 fprintf(out, " Global Shared Cache Line Event Information \n");
2171 fprintf(out, "=================================================\n");
2172 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2173 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2174 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2175 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2176 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2177 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2178 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2179 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2180 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2181 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2184 static void print_cacheline(struct c2c_hists *c2c_hists,
2185 struct hist_entry *he_cl,
2186 struct perf_hpp_list *hpp_list,
2190 struct perf_hpp hpp = {
2197 hists__fprintf_headers(&c2c_hists->hists, out);
2203 fprintf(out, " -------------------------------------------------------------\n");
2204 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2205 fprintf(out, "%s\n", bf);
2206 fprintf(out, " -------------------------------------------------------------\n");
2208 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2211 static void print_pareto(FILE *out)
2213 struct perf_hpp_list hpp_list;
2217 perf_hpp_list__init(&hpp_list);
2218 ret = hpp_list__parse(&hpp_list,
2227 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2230 nd = rb_first_cached(&c2c.hists.hists.entries);
2232 for (; nd; nd = rb_next(nd)) {
2233 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2234 struct c2c_hist_entry *c2c_he;
2239 c2c_he = container_of(he, struct c2c_hist_entry, he);
2240 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2244 static void print_c2c_info(FILE *out, struct perf_session *session)
2246 struct evlist *evlist = session->evlist;
2247 struct evsel *evsel;
2250 fprintf(out, "=================================================\n");
2251 fprintf(out, " c2c details \n");
2252 fprintf(out, "=================================================\n");
2254 evlist__for_each_entry(evlist, evsel) {
2255 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2256 perf_evsel__name(evsel));
2259 fprintf(out, " Cachelines sort on : %s HITMs\n",
2260 display_str[c2c.display]);
2261 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2264 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2268 print_c2c__display_stats(out);
2270 print_shared_cacheline_info(out);
2272 print_c2c_info(out, session);
2278 fprintf(out, "=================================================\n");
2279 fprintf(out, " Shared Data Cache Line Table \n");
2280 fprintf(out, "=================================================\n");
2281 fprintf(out, "#\n");
2283 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2286 fprintf(out, "=================================================\n");
2287 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2288 fprintf(out, "=================================================\n");
2289 fprintf(out, "#\n");
2294 #ifdef HAVE_SLANG_SUPPORT
2295 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2298 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2301 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2309 hb->nr_non_filtered_entries = nr_entries;
2312 struct c2c_cacheline_browser {
2313 struct hist_browser hb;
2314 struct hist_entry *he;
2318 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2319 char *bf, size_t size)
2321 struct c2c_cacheline_browser *cl_browser;
2322 struct hist_entry *he;
2325 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2326 he = cl_browser->he;
2329 addr = cl_address(he->mem_info->daddr.addr);
2331 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2335 static struct c2c_cacheline_browser*
2336 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2338 struct c2c_cacheline_browser *browser;
2340 browser = zalloc(sizeof(*browser));
2342 hist_browser__init(&browser->hb, hists);
2343 browser->hb.c2c_filter = true;
2344 browser->hb.title = perf_c2c_cacheline_browser__title;
2351 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2353 struct c2c_hist_entry *c2c_he;
2354 struct c2c_hists *c2c_hists;
2355 struct c2c_cacheline_browser *cl_browser;
2356 struct hist_browser *browser;
2358 static const char help[] =
2359 " ENTER Toggle callchains (if present) \n"
2360 " n Toggle Node details info \n"
2361 " s Toggle full length of symbol and source line columns \n"
2362 " q Return back to cacheline list \n";
2367 /* Display compact version first. */
2368 c2c.symbol_full = false;
2370 c2c_he = container_of(he, struct c2c_hist_entry, he);
2371 c2c_hists = c2c_he->hists;
2373 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2374 if (cl_browser == NULL)
2377 browser = &cl_browser->hb;
2379 /* reset abort key so that it can get Ctrl-C as a key */
2381 SLang_init_tty(0, 0, 0);
2383 c2c_browser__update_nr_entries(browser);
2386 key = hist_browser__run(browser, "? - help", true);
2390 c2c.symbol_full = !c2c.symbol_full;
2393 c2c.node_info = (c2c.node_info + 1) % 3;
2394 setup_nodes_header();
2399 ui_browser__help_window(&browser->b, help);
2411 static int perf_c2c_browser__title(struct hist_browser *browser,
2412 char *bf, size_t size)
2415 "Shared Data Cache Line Table "
2416 "(%lu entries, sorted on %s HITMs)",
2417 browser->nr_non_filtered_entries,
2418 display_str[c2c.display]);
2422 static struct hist_browser*
2423 perf_c2c_browser__new(struct hists *hists)
2425 struct hist_browser *browser = hist_browser__new(hists);
2428 browser->title = perf_c2c_browser__title;
2429 browser->c2c_filter = true;
2435 static int perf_c2c__hists_browse(struct hists *hists)
2437 struct hist_browser *browser;
2439 static const char help[] =
2440 " d Display cacheline details \n"
2441 " ENTER Toggle callchains (if present) \n"
2444 browser = perf_c2c_browser__new(hists);
2445 if (browser == NULL)
2448 /* reset abort key so that it can get Ctrl-C as a key */
2450 SLang_init_tty(0, 0, 0);
2452 c2c_browser__update_nr_entries(browser);
2455 key = hist_browser__run(browser, "? - help", true);
2461 perf_c2c__browse_cacheline(browser->he_selection);
2464 ui_browser__help_window(&browser->b, help);
2472 hist_browser__delete(browser);
2476 static void perf_c2c_display(struct perf_session *session)
2478 if (use_browser == 0)
2479 perf_c2c__hists_fprintf(stdout, session);
2481 perf_c2c__hists_browse(&c2c.hists.hists);
2484 static void perf_c2c_display(struct perf_session *session)
2487 perf_c2c__hists_fprintf(stdout, session);
2489 #endif /* HAVE_SLANG_SUPPORT */
2491 static char *fill_line(const char *orig, int len)
2493 int i, j, olen = strlen(orig);
2496 buf = zalloc(len + 1);
2500 j = len / 2 - olen / 2;
2502 for (i = 0; i < j - 1; i++)
2507 strcpy(buf + i, orig);
2513 for (; i < len; i++)
2519 static int ui_quirks(void)
2521 const char *nodestr = "Data address";
2524 if (!c2c.use_stdio) {
2525 dim_offset.width = 5;
2526 dim_offset.header = header_offset_tui;
2530 dim_percent_hitm.header = percent_hitm_header[c2c.display];
2532 /* Fix the zero line for dcacheline column. */
2533 buf = fill_line("Cacheline", dim_dcacheline.width +
2534 dim_dcacheline_node.width +
2535 dim_dcacheline_count.width + 4);
2539 dim_dcacheline.header.line[0].text = buf;
2541 /* Fix the zero line for offset column. */
2542 buf = fill_line(nodestr, dim_offset.width +
2543 dim_offset_node.width +
2544 dim_dcacheline_count.width + 4);
2548 dim_offset.header.line[0].text = buf;
2553 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2555 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2556 CALLCHAIN_REPORT_HELP
2557 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2560 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2562 struct callchain_param *callchain = opt->value;
2564 callchain->enabled = !unset;
2569 symbol_conf.use_callchain = false;
2570 callchain->mode = CHAIN_NONE;
2574 return parse_callchain_report_opt(arg);
2577 static int setup_callchain(struct evlist *evlist)
2579 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2580 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2582 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2583 (sample_type & PERF_SAMPLE_STACK_USER)) {
2584 mode = CALLCHAIN_DWARF;
2585 dwarf_callchain_users = true;
2586 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2587 mode = CALLCHAIN_LBR;
2588 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2589 mode = CALLCHAIN_FP;
2591 if (!callchain_param.enabled &&
2592 callchain_param.mode != CHAIN_NONE &&
2593 mode != CALLCHAIN_NONE) {
2594 symbol_conf.use_callchain = true;
2595 if (callchain_register_param(&callchain_param) < 0) {
2596 ui__error("Can't register callchain params.\n");
2601 callchain_param.record_mode = mode;
2602 callchain_param.min_percent = 0;
2606 static int setup_display(const char *str)
2608 const char *display = str ?: "tot";
2610 if (!strcmp(display, "tot"))
2611 c2c.display = DISPLAY_TOT;
2612 else if (!strcmp(display, "rmt"))
2613 c2c.display = DISPLAY_RMT;
2614 else if (!strcmp(display, "lcl"))
2615 c2c.display = DISPLAY_LCL;
2617 pr_err("failed: unknown display type: %s\n", str);
2624 #define for_each_token(__tok, __buf, __sep, __tmp) \
2625 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2626 __tok = strtok_r(NULL, __sep, &__tmp))
2628 static int build_cl_output(char *cl_sort, bool no_source)
2630 char *tok, *tmp, *buf = strdup(cl_sort);
2631 bool add_pid = false;
2632 bool add_tid = false;
2633 bool add_iaddr = false;
2634 bool add_sym = false;
2635 bool add_dso = false;
2636 bool add_src = false;
2641 for_each_token(tok, buf, ",", tmp) {
2642 if (!strcmp(tok, "tid")) {
2644 } else if (!strcmp(tok, "pid")) {
2646 } else if (!strcmp(tok, "iaddr")) {
2650 add_src = no_source ? false : true;
2651 } else if (!strcmp(tok, "dso")) {
2653 } else if (strcmp(tok, "offset")) {
2654 pr_err("unrecognized sort token: %s\n", tok);
2659 if (asprintf(&c2c.cl_output,
2660 "%s%s%s%s%s%s%s%s%s%s",
2661 c2c.use_stdio ? "cl_num_empty," : "",
2664 "percent_stores_l1hit,"
2665 "percent_stores_l1miss,"
2666 "offset,offset_node,dcacheline_count,",
2667 add_pid ? "pid," : "",
2668 add_tid ? "tid," : "",
2669 add_iaddr ? "iaddr," : "",
2675 add_sym ? "symbol," : "",
2676 add_dso ? "dso," : "",
2677 add_src ? "cl_srcline," : "",
2681 c2c.show_src = add_src;
2687 static int setup_coalesce(const char *coalesce, bool no_source)
2689 const char *c = coalesce ?: coalesce_default;
2691 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2694 if (build_cl_output(c2c.cl_sort, no_source))
2697 if (asprintf(&c2c.cl_resort, "offset,%s",
2698 c2c.display == DISPLAY_TOT ?
2700 c2c.display == DISPLAY_RMT ?
2701 "rmt_hitm,lcl_hitm" :
2702 "lcl_hitm,rmt_hitm") < 0)
2705 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2706 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2707 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2711 static int perf_c2c__report(int argc, const char **argv)
2713 struct perf_session *session;
2714 struct ui_progress prog;
2715 struct perf_data data = {
2716 .mode = PERF_DATA_MODE_READ,
2718 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2719 const char *display = NULL;
2720 const char *coalesce = NULL;
2721 bool no_source = false;
2722 const struct option options[] = {
2723 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2724 "file", "vmlinux pathname"),
2725 OPT_STRING('i', "input", &input_name, "file",
2726 "the input file to process"),
2727 OPT_INCR('N', "node-info", &c2c.node_info,
2728 "show extra node info in report (repeat for more info)"),
2729 #ifdef HAVE_SLANG_SUPPORT
2730 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2732 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2733 "Display only statistic tables (implies --stdio)"),
2734 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2735 "Display full length of symbols"),
2736 OPT_BOOLEAN(0, "no-source", &no_source,
2737 "Do not display Source Line column"),
2738 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2739 "Show all captured HITM lines."),
2740 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2741 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2742 callchain_help, &parse_callchain_opt,
2743 callchain_default_opt),
2744 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2745 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2746 "coalesce fields: pid,tid,iaddr,dso"),
2747 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2748 OPT_PARENT(c2c_options),
2753 argc = parse_options(argc, argv, options, report_c2c_usage,
2754 PARSE_OPT_STOP_AT_NON_OPTION);
2756 usage_with_options(report_c2c_usage, options);
2759 c2c.use_stdio = true;
2761 if (!input_name || !strlen(input_name))
2762 input_name = "perf.data";
2764 data.path = input_name;
2765 data.force = symbol_conf.force;
2767 err = setup_display(display);
2771 err = setup_coalesce(coalesce, no_source);
2773 pr_debug("Failed to initialize hists\n");
2777 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2779 pr_debug("Failed to initialize hists\n");
2783 session = perf_session__new(&data, 0, &c2c.tool);
2784 if (session == NULL) {
2785 pr_debug("No memory for session\n");
2789 err = setup_nodes(session);
2791 pr_err("Failed setup nodes\n");
2795 err = mem2node__init(&c2c.mem2node, &session->header.env);
2799 err = setup_callchain(session->evlist);
2803 if (symbol__init(&session->header.env) < 0)
2806 /* No pipe support at the moment. */
2807 if (perf_data__is_pipe(session->data)) {
2808 pr_debug("No pipe support at the moment.\n");
2817 setup_browser(false);
2819 err = perf_session__process_events(session);
2821 pr_err("failed to process sample\n");
2825 c2c_hists__reinit(&c2c.hists,
2832 "tot_hitm,lcl_hitm,rmt_hitm,"
2833 "stores,stores_l1hit,stores_l1miss,"
2834 "dram_lcl,dram_rmt,"
2837 "ld_fbhit,ld_l1hit,ld_l2hit,"
2838 "ld_lclhit,ld_rmthit",
2839 c2c.display == DISPLAY_TOT ? "tot_hitm" :
2840 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
2843 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2845 hists__collapse_resort(&c2c.hists.hists, NULL);
2846 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2847 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2849 ui_progress__finish();
2852 pr_err("failed to setup UI\n");
2856 perf_c2c_display(session);
2859 mem2node__exit(&c2c.mem2node);
2861 perf_session__delete(session);
2866 static int parse_record_events(const struct option *opt,
2867 const char *str, int unset __maybe_unused)
2869 bool *event_set = (bool *) opt->value;
2872 return perf_mem_events__parse(str);
2876 static const char * const __usage_record[] = {
2877 "perf c2c record [<options>] [<command>]",
2878 "perf c2c record [<options>] -- <command> [<options>]",
2882 static const char * const *record_mem_usage = __usage_record;
2884 static int perf_c2c__record(int argc, const char **argv)
2886 int rec_argc, i = 0, j;
2887 const char **rec_argv;
2889 bool all_user = false, all_kernel = false;
2890 bool event_set = false;
2891 struct option options[] = {
2892 OPT_CALLBACK('e', "event", &event_set, "event",
2893 "event selector. Use 'perf mem record -e list' to list available events",
2894 parse_record_events),
2895 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2896 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2897 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2898 OPT_PARENT(c2c_options),
2902 if (perf_mem_events__init()) {
2903 pr_err("failed: memory events not supported\n");
2907 argc = parse_options(argc, argv, options, record_mem_usage,
2908 PARSE_OPT_KEEP_UNKNOWN);
2910 rec_argc = argc + 11; /* max number of arguments */
2911 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2915 rec_argv[i++] = "record";
2918 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2919 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2922 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2923 rec_argv[i++] = "-W";
2925 rec_argv[i++] = "-d";
2926 rec_argv[i++] = "--phys-data";
2927 rec_argv[i++] = "--sample-cpu";
2929 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2930 if (!perf_mem_events[j].record)
2933 if (!perf_mem_events[j].supported) {
2934 pr_err("failed: event '%s' not supported\n",
2935 perf_mem_events[j].name);
2940 rec_argv[i++] = "-e";
2941 rec_argv[i++] = perf_mem_events__name(j);
2945 rec_argv[i++] = "--all-user";
2948 rec_argv[i++] = "--all-kernel";
2950 for (j = 0; j < argc; j++, i++)
2951 rec_argv[i] = argv[j];
2954 pr_debug("calling: ");
2958 while (rec_argv[j]) {
2959 pr_debug("%s ", rec_argv[j]);
2965 ret = cmd_record(i, rec_argv);
2970 int cmd_c2c(int argc, const char **argv)
2972 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2973 PARSE_OPT_STOP_AT_NON_OPTION);
2976 usage_with_options(c2c_usage, c2c_options);
2978 if (!strncmp(argv[0], "rec", 3)) {
2979 return perf_c2c__record(argc, argv);
2980 } else if (!strncmp(argv[0], "rep", 3)) {
2981 return perf_c2c__report(argc, argv);
2983 usage_with_options(c2c_usage, c2c_options);