2 * This is rewrite of original c2c tool introduced in here:
3 * http://lwn.net/Articles/588866/
5 * The original tool was changed to fit in current perf state.
8 * Don Zickus <dzickus@redhat.com>
9 * Dick Fowles <fowles@inreach.com>
10 * Joe Mario <jmario@redhat.com>
14 #include <linux/compiler.h>
15 #include <linux/kernel.h>
16 #include <linux/stringify.h>
21 #include <subcmd/parse-options.h>
22 #include "mem-events.h"
32 #include "ui/browsers/hists.h"
37 struct perf_hpp_list list;
38 struct c2c_stats stats;
41 struct compute_stats {
42 struct stats lcl_hitm;
43 struct stats rmt_hitm;
47 struct c2c_hist_entry {
48 struct c2c_hists *hists;
49 struct c2c_stats stats;
50 unsigned long *cpuset;
51 struct c2c_stats *node_stats;
52 unsigned int cacheline_idx;
54 struct compute_stats cstats;
58 * because of its callchain dynamic entry
63 static char const *coalesce_default = "pid,iaddr";
66 struct perf_tool tool;
67 struct c2c_hists hists;
69 unsigned long **nodes;
81 /* HITM shared clines stats */
82 struct c2c_stats hitm_stats;
100 static const char *display_str[DISPLAY_MAX] = {
101 [DISPLAY_LCL] = "Local",
102 [DISPLAY_RMT] = "Remote",
103 [DISPLAY_TOT] = "Total",
106 static const struct option c2c_options[] = {
107 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
111 static struct perf_c2c c2c;
113 static void *c2c_he_zalloc(size_t size)
115 struct c2c_hist_entry *c2c_he;
117 c2c_he = zalloc(size + sizeof(*c2c_he));
121 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
125 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
126 if (!c2c_he->node_stats)
129 init_stats(&c2c_he->cstats.lcl_hitm);
130 init_stats(&c2c_he->cstats.rmt_hitm);
131 init_stats(&c2c_he->cstats.load);
136 static void c2c_he_free(void *he)
138 struct c2c_hist_entry *c2c_he;
140 c2c_he = container_of(he, struct c2c_hist_entry, he);
142 hists__delete_entries(&c2c_he->hists->hists);
146 free(c2c_he->cpuset);
147 free(c2c_he->node_stats);
151 static struct hist_entry_ops c2c_entry_ops = {
152 .new = c2c_he_zalloc,
156 static int c2c_hists__init(struct c2c_hists *hists,
158 int nr_header_lines);
160 static struct c2c_hists*
161 he__get_c2c_hists(struct hist_entry *he,
165 struct c2c_hist_entry *c2c_he;
166 struct c2c_hists *hists;
169 c2c_he = container_of(he, struct c2c_hist_entry, he);
171 return c2c_he->hists;
173 hists = c2c_he->hists = zalloc(sizeof(*hists));
177 ret = c2c_hists__init(hists, sort, nr_header_lines);
186 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
187 struct perf_sample *sample)
189 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
190 "WARNING: no sample cpu value"))
193 set_bit(sample->cpu, c2c_he->cpuset);
196 static void compute_stats(struct c2c_hist_entry *c2c_he,
197 struct c2c_stats *stats,
200 struct compute_stats *cstats = &c2c_he->cstats;
203 update_stats(&cstats->rmt_hitm, weight);
204 else if (stats->lcl_hitm)
205 update_stats(&cstats->lcl_hitm, weight);
206 else if (stats->load)
207 update_stats(&cstats->load, weight);
210 static int process_sample_event(struct perf_tool *tool __maybe_unused,
211 union perf_event *event,
212 struct perf_sample *sample,
213 struct perf_evsel *evsel,
214 struct machine *machine)
216 struct c2c_hists *c2c_hists = &c2c.hists;
217 struct c2c_hist_entry *c2c_he;
218 struct c2c_stats stats = { .nr_entries = 0, };
219 struct hist_entry *he;
220 struct addr_location al;
221 struct mem_info *mi, *mi_dup;
224 if (machine__resolve(machine, &al, sample) < 0) {
225 pr_debug("problem processing %d event, skipping it.\n",
230 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
231 evsel, &al, sysctl_perf_event_max_stack);
235 mi = sample__resolve_mem(sample, &al);
239 mi_dup = memdup(mi, sizeof(*mi));
243 c2c_decode_stats(&stats, mi);
245 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
251 c2c_he = container_of(he, struct c2c_hist_entry, he);
252 c2c_add_stats(&c2c_he->stats, &stats);
253 c2c_add_stats(&c2c_hists->stats, &stats);
255 c2c_he__set_cpu(c2c_he, sample);
257 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
258 ret = hist_entry__append_callchain(he, sample);
262 * There's already been warning about missing
263 * sample's cpu value. Let's account all to
264 * node 0 in this case, without any further
267 * Doing node stats only for single callchain data.
269 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
270 int node = c2c.cpu2node[cpu];
274 mi_dup = memdup(mi, sizeof(*mi));
278 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
282 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
288 c2c_he = container_of(he, struct c2c_hist_entry, he);
289 c2c_add_stats(&c2c_he->stats, &stats);
290 c2c_add_stats(&c2c_hists->stats, &stats);
291 c2c_add_stats(&c2c_he->node_stats[node], &stats);
293 compute_stats(c2c_he, &stats, sample->weight);
295 c2c_he__set_cpu(c2c_he, sample);
297 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
298 ret = hist_entry__append_callchain(he, sample);
302 addr_location__put(&al);
313 static struct perf_c2c c2c = {
315 .sample = process_sample_event,
316 .mmap = perf_event__process_mmap,
317 .mmap2 = perf_event__process_mmap2,
318 .comm = perf_event__process_comm,
319 .exit = perf_event__process_exit,
320 .fork = perf_event__process_fork,
321 .lost = perf_event__process_lost,
322 .ordered_events = true,
323 .ordering_requires_timestamps = true,
327 static const char * const c2c_usage[] = {
328 "perf c2c {record|report}",
332 static const char * const __usage_report[] = {
337 static const char * const *report_c2c_usage = __usage_report;
339 #define C2C_HEADER_MAX 2
345 } line[C2C_HEADER_MAX];
348 struct c2c_dimension {
349 struct c2c_header header;
352 struct sort_entry *se;
354 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
355 struct hist_entry *, struct hist_entry *);
356 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
357 struct hist_entry *he);
358 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
359 struct hist_entry *he);
363 struct perf_hpp_fmt fmt;
364 struct c2c_dimension *dim;
367 #define SYMBOL_WIDTH 30
369 static struct c2c_dimension dim_symbol;
370 static struct c2c_dimension dim_srcline;
372 static int symbol_width(struct hists *hists, struct sort_entry *se)
374 int width = hists__col_len(hists, se->se_width_idx);
376 if (!c2c.symbol_full)
377 width = MIN(width, SYMBOL_WIDTH);
382 static int c2c_width(struct perf_hpp_fmt *fmt,
383 struct perf_hpp *hpp __maybe_unused,
386 struct c2c_fmt *c2c_fmt;
387 struct c2c_dimension *dim;
389 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
392 if (dim == &dim_symbol || dim == &dim_srcline)
393 return symbol_width(hists, dim->se);
395 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
399 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
400 struct hists *hists, int line, int *span)
402 struct perf_hpp_list *hpp_list = hists->hpp_list;
403 struct c2c_fmt *c2c_fmt;
404 struct c2c_dimension *dim;
405 const char *text = NULL;
406 int width = c2c_width(fmt, hpp, hists);
408 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
412 text = dim->header.line[line].text;
413 /* Use the last line from sort_entry if not defined. */
414 if (!text && (line == hpp_list->nr_header_lines - 1))
415 text = dim->se->se_header;
417 text = dim->header.line[line].text;
423 *span = dim->header.line[line].span;
430 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
433 #define HEX_STR(__s, __v) \
435 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
440 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
441 struct hist_entry *left, struct hist_entry *right)
443 return sort__dcacheline_cmp(left, right);
446 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
447 struct hist_entry *he)
450 int width = c2c_width(fmt, hpp, he->hists);
454 addr = cl_address(he->mem_info->daddr.addr);
456 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
459 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
460 struct hist_entry *he)
463 int width = c2c_width(fmt, hpp, he->hists);
467 addr = cl_offset(he->mem_info->daddr.al_addr);
469 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
473 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
474 struct hist_entry *left, struct hist_entry *right)
476 uint64_t l = 0, r = 0;
479 l = cl_offset(left->mem_info->daddr.addr);
481 r = cl_offset(right->mem_info->daddr.addr);
483 return (int64_t)(r - l);
487 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
488 struct hist_entry *he)
491 int width = c2c_width(fmt, hpp, he->hists);
495 addr = he->mem_info->iaddr.addr;
497 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
501 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
502 struct hist_entry *left, struct hist_entry *right)
504 return sort__iaddr_cmp(left, right);
508 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
509 struct hist_entry *he)
511 struct c2c_hist_entry *c2c_he;
512 int width = c2c_width(fmt, hpp, he->hists);
513 unsigned int tot_hitm;
515 c2c_he = container_of(he, struct c2c_hist_entry, he);
516 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
518 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
522 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
523 struct hist_entry *left, struct hist_entry *right)
525 struct c2c_hist_entry *c2c_left;
526 struct c2c_hist_entry *c2c_right;
527 unsigned int tot_hitm_left;
528 unsigned int tot_hitm_right;
530 c2c_left = container_of(left, struct c2c_hist_entry, he);
531 c2c_right = container_of(right, struct c2c_hist_entry, he);
533 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
534 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
536 return tot_hitm_left - tot_hitm_right;
539 #define STAT_FN_ENTRY(__f) \
541 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
542 struct hist_entry *he) \
544 struct c2c_hist_entry *c2c_he; \
545 int width = c2c_width(fmt, hpp, he->hists); \
547 c2c_he = container_of(he, struct c2c_hist_entry, he); \
548 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
549 c2c_he->stats.__f); \
552 #define STAT_FN_CMP(__f) \
554 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
555 struct hist_entry *left, struct hist_entry *right) \
557 struct c2c_hist_entry *c2c_left, *c2c_right; \
559 c2c_left = container_of(left, struct c2c_hist_entry, he); \
560 c2c_right = container_of(right, struct c2c_hist_entry, he); \
561 return c2c_left->stats.__f - c2c_right->stats.__f; \
564 #define STAT_FN(__f) \
579 static uint64_t llc_miss(struct c2c_stats *stats)
583 llcmiss = stats->lcl_dram +
592 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
593 struct hist_entry *he)
595 struct c2c_hist_entry *c2c_he;
596 int width = c2c_width(fmt, hpp, he->hists);
598 c2c_he = container_of(he, struct c2c_hist_entry, he);
600 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
601 llc_miss(&c2c_he->stats));
605 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
606 struct hist_entry *left, struct hist_entry *right)
608 struct c2c_hist_entry *c2c_left;
609 struct c2c_hist_entry *c2c_right;
611 c2c_left = container_of(left, struct c2c_hist_entry, he);
612 c2c_right = container_of(right, struct c2c_hist_entry, he);
614 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
617 static uint64_t total_records(struct c2c_stats *stats)
619 uint64_t lclmiss, ldcnt, total;
621 lclmiss = stats->lcl_dram +
641 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
642 struct hist_entry *he)
644 struct c2c_hist_entry *c2c_he;
645 int width = c2c_width(fmt, hpp, he->hists);
648 c2c_he = container_of(he, struct c2c_hist_entry, he);
649 tot_recs = total_records(&c2c_he->stats);
651 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
655 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
656 struct hist_entry *left, struct hist_entry *right)
658 struct c2c_hist_entry *c2c_left;
659 struct c2c_hist_entry *c2c_right;
660 uint64_t tot_recs_left;
661 uint64_t tot_recs_right;
663 c2c_left = container_of(left, struct c2c_hist_entry, he);
664 c2c_right = container_of(right, struct c2c_hist_entry, he);
666 tot_recs_left = total_records(&c2c_left->stats);
667 tot_recs_right = total_records(&c2c_right->stats);
669 return tot_recs_left - tot_recs_right;
672 static uint64_t total_loads(struct c2c_stats *stats)
674 uint64_t lclmiss, ldcnt;
676 lclmiss = stats->lcl_dram +
692 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
693 struct hist_entry *he)
695 struct c2c_hist_entry *c2c_he;
696 int width = c2c_width(fmt, hpp, he->hists);
699 c2c_he = container_of(he, struct c2c_hist_entry, he);
700 tot_recs = total_loads(&c2c_he->stats);
702 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
706 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
707 struct hist_entry *left, struct hist_entry *right)
709 struct c2c_hist_entry *c2c_left;
710 struct c2c_hist_entry *c2c_right;
711 uint64_t tot_recs_left;
712 uint64_t tot_recs_right;
714 c2c_left = container_of(left, struct c2c_hist_entry, he);
715 c2c_right = container_of(right, struct c2c_hist_entry, he);
717 tot_recs_left = total_loads(&c2c_left->stats);
718 tot_recs_right = total_loads(&c2c_right->stats);
720 return tot_recs_left - tot_recs_right;
723 typedef double (get_percent_cb)(struct c2c_hist_entry *);
726 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
727 struct hist_entry *he, get_percent_cb get_percent)
729 struct c2c_hist_entry *c2c_he;
730 int width = c2c_width(fmt, hpp, he->hists);
733 c2c_he = container_of(he, struct c2c_hist_entry, he);
734 per = get_percent(c2c_he);
736 #ifdef HAVE_SLANG_SUPPORT
738 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
740 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
743 static double percent_hitm(struct c2c_hist_entry *c2c_he)
745 struct c2c_hists *hists;
746 struct c2c_stats *stats;
747 struct c2c_stats *total;
751 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
752 stats = &c2c_he->stats;
753 total = &hists->stats;
755 switch (c2c.display) {
757 st = stats->rmt_hitm;
758 tot = total->rmt_hitm;
761 st = stats->lcl_hitm;
762 tot = total->lcl_hitm;
765 st = stats->tot_hitm;
766 tot = total->tot_hitm;
771 p = tot ? (double) st / tot : 0;
776 #define PERC_STR(__s, __v) \
778 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
783 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
784 struct hist_entry *he)
786 struct c2c_hist_entry *c2c_he;
787 int width = c2c_width(fmt, hpp, he->hists);
791 c2c_he = container_of(he, struct c2c_hist_entry, he);
792 per = percent_hitm(c2c_he);
793 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
797 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
798 struct hist_entry *he)
800 return percent_color(fmt, hpp, he, percent_hitm);
804 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
805 struct hist_entry *left, struct hist_entry *right)
807 struct c2c_hist_entry *c2c_left;
808 struct c2c_hist_entry *c2c_right;
812 c2c_left = container_of(left, struct c2c_hist_entry, he);
813 c2c_right = container_of(right, struct c2c_hist_entry, he);
815 per_left = percent_hitm(c2c_left);
816 per_right = percent_hitm(c2c_right);
818 return per_left - per_right;
821 static struct c2c_stats *he_stats(struct hist_entry *he)
823 struct c2c_hist_entry *c2c_he;
825 c2c_he = container_of(he, struct c2c_hist_entry, he);
826 return &c2c_he->stats;
829 static struct c2c_stats *total_stats(struct hist_entry *he)
831 struct c2c_hists *hists;
833 hists = container_of(he->hists, struct c2c_hists, hists);
834 return &hists->stats;
837 static double percent(int st, int tot)
839 return tot ? 100. * (double) st / (double) tot : 0;
842 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
844 #define PERCENT_FN(__f) \
845 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
847 struct c2c_hists *hists; \
849 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
850 return percent(c2c_he->stats.__f, hists->stats.__f); \
856 PERCENT_FN(st_l1miss)
859 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
860 struct hist_entry *he)
862 int width = c2c_width(fmt, hpp, he->hists);
863 double per = PERCENT(he, rmt_hitm);
866 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
870 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
871 struct hist_entry *he)
873 return percent_color(fmt, hpp, he, percent_rmt_hitm);
877 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
878 struct hist_entry *left, struct hist_entry *right)
883 per_left = PERCENT(left, lcl_hitm);
884 per_right = PERCENT(right, lcl_hitm);
886 return per_left - per_right;
890 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
891 struct hist_entry *he)
893 int width = c2c_width(fmt, hpp, he->hists);
894 double per = PERCENT(he, lcl_hitm);
897 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
901 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
902 struct hist_entry *he)
904 return percent_color(fmt, hpp, he, percent_lcl_hitm);
908 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
909 struct hist_entry *left, struct hist_entry *right)
914 per_left = PERCENT(left, lcl_hitm);
915 per_right = PERCENT(right, lcl_hitm);
917 return per_left - per_right;
921 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
922 struct hist_entry *he)
924 int width = c2c_width(fmt, hpp, he->hists);
925 double per = PERCENT(he, st_l1hit);
928 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
932 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
933 struct hist_entry *he)
935 return percent_color(fmt, hpp, he, percent_st_l1hit);
939 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
940 struct hist_entry *left, struct hist_entry *right)
945 per_left = PERCENT(left, st_l1hit);
946 per_right = PERCENT(right, st_l1hit);
948 return per_left - per_right;
952 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
953 struct hist_entry *he)
955 int width = c2c_width(fmt, hpp, he->hists);
956 double per = PERCENT(he, st_l1miss);
959 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
963 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
964 struct hist_entry *he)
966 return percent_color(fmt, hpp, he, percent_st_l1miss);
970 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
971 struct hist_entry *left, struct hist_entry *right)
976 per_left = PERCENT(left, st_l1miss);
977 per_right = PERCENT(right, st_l1miss);
979 return per_left - per_right;
986 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
987 struct hist_entry *he)
989 int width = c2c_width(fmt, hpp, he->hists);
991 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
995 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
996 struct hist_entry *left, struct hist_entry *right)
998 return left->thread->pid_ - right->thread->pid_;
1002 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1003 struct hist_entry *left __maybe_unused,
1004 struct hist_entry *right __maybe_unused)
1010 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1011 struct hist_entry *he)
1013 struct c2c_hist_entry *c2c_he;
1018 c2c_he = container_of(he, struct c2c_hist_entry, he);
1020 for (node = 0; node < c2c.nodes_cnt; node++) {
1021 DECLARE_BITMAP(set, c2c.cpus_cnt);
1023 bitmap_zero(set, c2c.cpus_cnt);
1024 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1026 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1027 if (c2c.node_info == 1) {
1028 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1029 advance_hpp(hpp, ret);
1035 ret = scnprintf(hpp->buf, hpp->size, " ");
1036 advance_hpp(hpp, ret);
1039 switch (c2c.node_info) {
1041 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1042 advance_hpp(hpp, ret);
1046 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
1047 struct c2c_stats *stats = &c2c_he->node_stats[node];
1049 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1050 advance_hpp(hpp, ret);
1052 #define DISPLAY_HITM(__h) \
1053 if (c2c_he->stats.__h> 0) { \
1054 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1055 percent(stats->__h, c2c_he->stats.__h));\
1057 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
1060 switch (c2c.display) {
1062 DISPLAY_HITM(rmt_hitm);
1065 DISPLAY_HITM(lcl_hitm);
1068 DISPLAY_HITM(tot_hitm);
1075 advance_hpp(hpp, ret);
1077 if (c2c_he->stats.store > 0) {
1078 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1079 percent(stats->store, c2c_he->stats.store));
1081 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1084 advance_hpp(hpp, ret);
1088 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1089 advance_hpp(hpp, ret);
1091 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1092 advance_hpp(hpp, ret);
1094 ret = scnprintf(hpp->buf, hpp->size, "}");
1095 advance_hpp(hpp, ret);
1108 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1109 struct hist_entry *he, double mean)
1111 int width = c2c_width(fmt, hpp, he->hists);
1114 scnprintf(buf, 10, "%6.0f", mean);
1115 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1118 #define MEAN_ENTRY(__func, __val) \
1120 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1122 struct c2c_hist_entry *c2c_he; \
1123 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1124 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1127 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1128 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1129 MEAN_ENTRY(mean_load_entry, load);
1132 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1133 struct hist_entry *he)
1135 struct c2c_hist_entry *c2c_he;
1136 int width = c2c_width(fmt, hpp, he->hists);
1139 c2c_he = container_of(he, struct c2c_hist_entry, he);
1141 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1142 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1146 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1147 struct hist_entry *he)
1149 struct c2c_hist_entry *c2c_he;
1150 int width = c2c_width(fmt, hpp, he->hists);
1153 c2c_he = container_of(he, struct c2c_hist_entry, he);
1155 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1156 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1160 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1161 struct hist_entry *he)
1163 int width = c2c_width(fmt, hpp, he->hists);
1165 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1168 #define HEADER_LOW(__h) \
1175 #define HEADER_BOTH(__h0, __h1) \
1185 #define HEADER_SPAN(__h0, __h1, __s) \
1196 #define HEADER_SPAN_LOW(__h) \
1203 static struct c2c_dimension dim_dcacheline = {
1204 .header = HEADER_LOW("Cacheline"),
1205 .name = "dcacheline",
1206 .cmp = dcacheline_cmp,
1207 .entry = dcacheline_entry,
1211 static struct c2c_header header_offset_tui = HEADER_LOW("Off");
1213 static struct c2c_dimension dim_offset = {
1214 .header = HEADER_BOTH("Data address", "Offset"),
1217 .entry = offset_entry,
1221 static struct c2c_dimension dim_iaddr = {
1222 .header = HEADER_LOW("Code address"),
1225 .entry = iaddr_entry,
1229 static struct c2c_dimension dim_tot_hitm = {
1230 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1232 .cmp = tot_hitm_cmp,
1233 .entry = tot_hitm_entry,
1237 static struct c2c_dimension dim_lcl_hitm = {
1238 .header = HEADER_SPAN_LOW("Lcl"),
1240 .cmp = lcl_hitm_cmp,
1241 .entry = lcl_hitm_entry,
1245 static struct c2c_dimension dim_rmt_hitm = {
1246 .header = HEADER_SPAN_LOW("Rmt"),
1248 .cmp = rmt_hitm_cmp,
1249 .entry = rmt_hitm_entry,
1253 static struct c2c_dimension dim_cl_rmt_hitm = {
1254 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1255 .name = "cl_rmt_hitm",
1256 .cmp = rmt_hitm_cmp,
1257 .entry = rmt_hitm_entry,
1261 static struct c2c_dimension dim_cl_lcl_hitm = {
1262 .header = HEADER_SPAN_LOW("Lcl"),
1263 .name = "cl_lcl_hitm",
1264 .cmp = lcl_hitm_cmp,
1265 .entry = lcl_hitm_entry,
1269 static struct c2c_dimension dim_stores = {
1270 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1273 .entry = store_entry,
1277 static struct c2c_dimension dim_stores_l1hit = {
1278 .header = HEADER_SPAN_LOW("L1Hit"),
1279 .name = "stores_l1hit",
1280 .cmp = st_l1hit_cmp,
1281 .entry = st_l1hit_entry,
1285 static struct c2c_dimension dim_stores_l1miss = {
1286 .header = HEADER_SPAN_LOW("L1Miss"),
1287 .name = "stores_l1miss",
1288 .cmp = st_l1miss_cmp,
1289 .entry = st_l1miss_entry,
1293 static struct c2c_dimension dim_cl_stores_l1hit = {
1294 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1295 .name = "cl_stores_l1hit",
1296 .cmp = st_l1hit_cmp,
1297 .entry = st_l1hit_entry,
1301 static struct c2c_dimension dim_cl_stores_l1miss = {
1302 .header = HEADER_SPAN_LOW("L1 Miss"),
1303 .name = "cl_stores_l1miss",
1304 .cmp = st_l1miss_cmp,
1305 .entry = st_l1miss_entry,
1309 static struct c2c_dimension dim_ld_fbhit = {
1310 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1312 .cmp = ld_fbhit_cmp,
1313 .entry = ld_fbhit_entry,
1317 static struct c2c_dimension dim_ld_l1hit = {
1318 .header = HEADER_SPAN_LOW("L1"),
1320 .cmp = ld_l1hit_cmp,
1321 .entry = ld_l1hit_entry,
1325 static struct c2c_dimension dim_ld_l2hit = {
1326 .header = HEADER_SPAN_LOW("L2"),
1328 .cmp = ld_l2hit_cmp,
1329 .entry = ld_l2hit_entry,
1333 static struct c2c_dimension dim_ld_llchit = {
1334 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1335 .name = "ld_lclhit",
1336 .cmp = ld_llchit_cmp,
1337 .entry = ld_llchit_entry,
1341 static struct c2c_dimension dim_ld_rmthit = {
1342 .header = HEADER_SPAN_LOW("Rmt"),
1343 .name = "ld_rmthit",
1345 .entry = rmt_hit_entry,
1349 static struct c2c_dimension dim_ld_llcmiss = {
1350 .header = HEADER_BOTH("LLC", "Ld Miss"),
1351 .name = "ld_llcmiss",
1352 .cmp = ld_llcmiss_cmp,
1353 .entry = ld_llcmiss_entry,
1357 static struct c2c_dimension dim_tot_recs = {
1358 .header = HEADER_BOTH("Total", "records"),
1360 .cmp = tot_recs_cmp,
1361 .entry = tot_recs_entry,
1365 static struct c2c_dimension dim_tot_loads = {
1366 .header = HEADER_BOTH("Total", "Loads"),
1367 .name = "tot_loads",
1368 .cmp = tot_loads_cmp,
1369 .entry = tot_loads_entry,
1373 static struct c2c_header percent_hitm_header[] = {
1374 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1375 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1376 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1379 static struct c2c_dimension dim_percent_hitm = {
1380 .name = "percent_hitm",
1381 .cmp = percent_hitm_cmp,
1382 .entry = percent_hitm_entry,
1383 .color = percent_hitm_color,
1387 static struct c2c_dimension dim_percent_rmt_hitm = {
1388 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1389 .name = "percent_rmt_hitm",
1390 .cmp = percent_rmt_hitm_cmp,
1391 .entry = percent_rmt_hitm_entry,
1392 .color = percent_rmt_hitm_color,
1396 static struct c2c_dimension dim_percent_lcl_hitm = {
1397 .header = HEADER_SPAN_LOW("Lcl"),
1398 .name = "percent_lcl_hitm",
1399 .cmp = percent_lcl_hitm_cmp,
1400 .entry = percent_lcl_hitm_entry,
1401 .color = percent_lcl_hitm_color,
1405 static struct c2c_dimension dim_percent_stores_l1hit = {
1406 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1407 .name = "percent_stores_l1hit",
1408 .cmp = percent_stores_l1hit_cmp,
1409 .entry = percent_stores_l1hit_entry,
1410 .color = percent_stores_l1hit_color,
1414 static struct c2c_dimension dim_percent_stores_l1miss = {
1415 .header = HEADER_SPAN_LOW("L1 Miss"),
1416 .name = "percent_stores_l1miss",
1417 .cmp = percent_stores_l1miss_cmp,
1418 .entry = percent_stores_l1miss_entry,
1419 .color = percent_stores_l1miss_color,
1423 static struct c2c_dimension dim_dram_lcl = {
1424 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1426 .cmp = lcl_dram_cmp,
1427 .entry = lcl_dram_entry,
1431 static struct c2c_dimension dim_dram_rmt = {
1432 .header = HEADER_SPAN_LOW("Rmt"),
1434 .cmp = rmt_dram_cmp,
1435 .entry = rmt_dram_entry,
1439 static struct c2c_dimension dim_pid = {
1440 .header = HEADER_LOW("Pid"),
1447 static struct c2c_dimension dim_tid = {
1448 .header = HEADER_LOW("Tid"),
1453 static struct c2c_dimension dim_symbol = {
1458 static struct c2c_dimension dim_dso = {
1459 .header = HEADER_BOTH("Shared", "Object"),
1464 static struct c2c_header header_node[3] = {
1466 HEADER_LOW("Node{cpus %hitms %stores}"),
1467 HEADER_LOW("Node{cpu list}"),
1470 static struct c2c_dimension dim_node = {
1473 .entry = node_entry,
1477 static struct c2c_dimension dim_mean_rmt = {
1478 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1481 .entry = mean_rmt_entry,
1485 static struct c2c_dimension dim_mean_lcl = {
1486 .header = HEADER_SPAN_LOW("lcl hitm"),
1489 .entry = mean_lcl_entry,
1493 static struct c2c_dimension dim_mean_load = {
1494 .header = HEADER_SPAN_LOW("load"),
1495 .name = "mean_load",
1497 .entry = mean_load_entry,
1501 static struct c2c_dimension dim_cpucnt = {
1502 .header = HEADER_BOTH("cpu", "cnt"),
1505 .entry = cpucnt_entry,
1509 static struct c2c_dimension dim_srcline = {
1510 .name = "cl_srcline",
1511 .se = &sort_srcline,
1514 static struct c2c_dimension dim_dcacheline_idx = {
1515 .header = HEADER_LOW("Index"),
1518 .entry = cl_idx_entry,
1522 static struct c2c_dimension dim_dcacheline_num = {
1523 .header = HEADER_LOW("Num"),
1526 .entry = cl_idx_entry,
1530 static struct c2c_dimension dim_dcacheline_num_empty = {
1531 .header = HEADER_LOW("Num"),
1532 .name = "cl_num_empty",
1534 .entry = cl_idx_empty_entry,
1538 static struct c2c_dimension *dimensions[] = {
1550 &dim_cl_stores_l1hit,
1551 &dim_cl_stores_l1miss,
1561 &dim_percent_rmt_hitm,
1562 &dim_percent_lcl_hitm,
1563 &dim_percent_stores_l1hit,
1564 &dim_percent_stores_l1miss,
1577 &dim_dcacheline_idx,
1578 &dim_dcacheline_num,
1579 &dim_dcacheline_num_empty,
1583 static void fmt_free(struct perf_hpp_fmt *fmt)
1585 struct c2c_fmt *c2c_fmt;
1587 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1591 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1593 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1594 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1596 return c2c_a->dim == c2c_b->dim;
1599 static struct c2c_dimension *get_dimension(const char *name)
1603 for (i = 0; dimensions[i]; i++) {
1604 struct c2c_dimension *dim = dimensions[i];
1606 if (!strcmp(dim->name, name))
1613 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1614 struct hist_entry *he)
1616 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1617 struct c2c_dimension *dim = c2c_fmt->dim;
1618 size_t len = fmt->user_len;
1621 len = hists__col_len(he->hists, dim->se->se_width_idx);
1623 if (dim == &dim_symbol || dim == &dim_srcline)
1624 len = symbol_width(he->hists, dim->se);
1627 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1630 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1631 struct hist_entry *a, struct hist_entry *b)
1633 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1634 struct c2c_dimension *dim = c2c_fmt->dim;
1636 return dim->se->se_cmp(a, b);
1639 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1640 struct hist_entry *a, struct hist_entry *b)
1642 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1643 struct c2c_dimension *dim = c2c_fmt->dim;
1644 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1646 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1647 return collapse_fn(a, b);
1650 static struct c2c_fmt *get_format(const char *name)
1652 struct c2c_dimension *dim = get_dimension(name);
1653 struct c2c_fmt *c2c_fmt;
1654 struct perf_hpp_fmt *fmt;
1659 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1665 fmt = &c2c_fmt->fmt;
1666 INIT_LIST_HEAD(&fmt->list);
1667 INIT_LIST_HEAD(&fmt->sort_list);
1669 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1670 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1671 fmt->color = dim->se ? NULL : dim->color;
1672 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1673 fmt->header = c2c_header;
1674 fmt->width = c2c_width;
1675 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1676 fmt->equal = fmt_equal;
1677 fmt->free = fmt_free;
1682 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1684 struct c2c_fmt *c2c_fmt = get_format(name);
1688 return output_field_add(hpp_list, name);
1691 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1695 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1697 struct c2c_fmt *c2c_fmt = get_format(name);
1698 struct c2c_dimension *dim;
1702 return sort_dimension__add(hpp_list, name, NULL, 0);
1706 if (dim == &dim_dso)
1709 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1713 #define PARSE_LIST(_list, _fn) \
1721 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1722 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1723 ret = _fn(hpp_list, tok); \
1724 if (ret == -EINVAL) { \
1725 error("Invalid --fields key: `%s'", tok); \
1727 } else if (ret == -ESRCH) { \
1728 error("Unknown --fields key: `%s'", tok); \
1734 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1735 const char *output_,
1738 char *output = output_ ? strdup(output_) : NULL;
1739 char *sort = sort_ ? strdup(sort_) : NULL;
1742 PARSE_LIST(output, c2c_hists__init_output);
1743 PARSE_LIST(sort, c2c_hists__init_sort);
1745 /* copy sort keys to output fields */
1746 perf_hpp__setup_output_field(hpp_list);
1749 * We dont need other sorting keys other than those
1750 * we already specified. It also really slows down
1751 * the processing a lot with big number of output
1752 * fields, so switching this off for c2c.
1756 /* and then copy output fields to sort keys */
1757 perf_hpp__append_sort_keys(&hists->list);
1765 static int c2c_hists__init(struct c2c_hists *hists,
1767 int nr_header_lines)
1769 __hists__init(&hists->hists, &hists->list);
1772 * Initialize only with sort fields, we need to resort
1773 * later anyway, and that's where we add output fields
1776 perf_hpp_list__init(&hists->list);
1778 /* Overload number of header lines.*/
1779 hists->list.nr_header_lines = nr_header_lines;
1781 return hpp_list__parse(&hists->list, NULL, sort);
1784 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1788 perf_hpp__reset_output_field(&c2c_hists->list);
1789 return hpp_list__parse(&c2c_hists->list, output, sort);
1792 #define DISPLAY_LINE_LIMIT 0.0005
1794 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1796 struct c2c_hist_entry *c2c_he;
1802 c2c_he = container_of(he, struct c2c_hist_entry, he);
1804 #define FILTER_HITM(__h) \
1806 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1807 if (ld_dist < DISPLAY_LINE_LIMIT) \
1808 he->filtered = HIST_FILTER__C2C; \
1810 he->filtered = HIST_FILTER__C2C; \
1813 switch (c2c.display) {
1815 FILTER_HITM(lcl_hitm);
1818 FILTER_HITM(rmt_hitm);
1821 FILTER_HITM(tot_hitm);
1828 return he->filtered == 0;
1831 static inline int valid_hitm_or_store(struct hist_entry *he)
1833 struct c2c_hist_entry *c2c_he;
1836 c2c_he = container_of(he, struct c2c_hist_entry, he);
1837 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1838 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1839 c2c_he->stats.rmt_hitm;
1840 return has_hitm || c2c_he->stats.store;
1843 static void calc_width(struct hist_entry *he)
1845 struct c2c_hists *c2c_hists;
1847 c2c_hists = container_of(he->hists, struct c2c_hists, hists);
1848 hists__calc_col_len(&c2c_hists->hists, he);
1851 static int filter_cb(struct hist_entry *he)
1853 if (c2c.show_src && !he->srcline)
1854 he->srcline = hist_entry__get_srcline(he);
1858 if (!valid_hitm_or_store(he))
1859 he->filtered = HIST_FILTER__C2C;
1864 static int resort_cl_cb(struct hist_entry *he)
1866 struct c2c_hist_entry *c2c_he;
1867 struct c2c_hists *c2c_hists;
1868 bool display = he__display(he, &c2c.hitm_stats);
1870 c2c_he = container_of(he, struct c2c_hist_entry, he);
1871 c2c_hists = c2c_he->hists;
1875 if (display && c2c_hists) {
1876 static unsigned int idx;
1878 c2c_he->cacheline_idx = idx++;
1880 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
1882 hists__collapse_resort(&c2c_hists->hists, NULL);
1883 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
1889 static void setup_nodes_header(void)
1891 dim_node.header = header_node[c2c.node_info];
1894 static int setup_nodes(struct perf_session *session)
1896 struct numa_node *n;
1897 unsigned long **nodes;
1901 if (c2c.node_info > 2)
1904 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
1905 c2c.cpus_cnt = session->header.env.nr_cpus_online;
1907 n = session->header.env.numa_nodes;
1911 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
1917 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
1921 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
1924 c2c.cpu2node = cpu2node;
1926 for (node = 0; node < c2c.nodes_cnt; node++) {
1927 struct cpu_map *map = n[node].map;
1930 set = bitmap_alloc(c2c.cpus_cnt);
1934 for (cpu = 0; cpu < map->nr; cpu++) {
1935 set_bit(map->map[cpu], set);
1937 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
1940 cpu2node[map->map[cpu]] = node;
1946 setup_nodes_header();
1950 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
1952 static int resort_hitm_cb(struct hist_entry *he)
1954 struct c2c_hist_entry *c2c_he;
1955 c2c_he = container_of(he, struct c2c_hist_entry, he);
1957 if (HAS_HITMS(c2c_he)) {
1958 c2c.shared_clines++;
1959 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
1965 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
1967 struct rb_node *next = rb_first(&hists->entries);
1971 struct hist_entry *he;
1973 he = rb_entry(next, struct hist_entry, rb_node);
1977 next = rb_next(&he->rb_node);
1983 static void print_c2c__display_stats(FILE *out)
1986 struct c2c_stats *stats = &c2c.hists.stats;
1988 llc_misses = stats->lcl_dram +
1993 fprintf(out, "=================================================\n");
1994 fprintf(out, " Trace Event Information \n");
1995 fprintf(out, "=================================================\n");
1996 fprintf(out, " Total records : %10d\n", stats->nr_entries);
1997 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
1998 fprintf(out, " Load Operations : %10d\n", stats->load);
1999 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2000 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2001 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2002 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2003 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2004 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2005 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2006 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2007 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2008 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2009 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2010 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2011 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2012 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2013 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2014 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2015 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2016 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2017 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2018 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2019 fprintf(out, " Store Operations : %10d\n", stats->store);
2020 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2021 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2022 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2023 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2024 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2025 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2028 static void print_shared_cacheline_info(FILE *out)
2030 struct c2c_stats *stats = &c2c.hitm_stats;
2031 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2033 fprintf(out, "=================================================\n");
2034 fprintf(out, " Global Shared Cache Line Event Information \n");
2035 fprintf(out, "=================================================\n");
2036 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2037 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2038 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2039 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2040 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2041 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2042 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2043 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2044 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2045 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2048 static void print_cacheline(struct c2c_hists *c2c_hists,
2049 struct hist_entry *he_cl,
2050 struct perf_hpp_list *hpp_list,
2054 struct perf_hpp hpp = {
2061 hists__fprintf_headers(&c2c_hists->hists, out);
2067 fprintf(out, " -------------------------------------------------------------\n");
2068 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2069 fprintf(out, "%s\n", bf);
2070 fprintf(out, " -------------------------------------------------------------\n");
2072 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, true);
2075 static void print_pareto(FILE *out)
2077 struct perf_hpp_list hpp_list;
2081 perf_hpp_list__init(&hpp_list);
2082 ret = hpp_list__parse(&hpp_list,
2091 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2094 nd = rb_first(&c2c.hists.hists.entries);
2096 for (; nd; nd = rb_next(nd)) {
2097 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2098 struct c2c_hist_entry *c2c_he;
2103 c2c_he = container_of(he, struct c2c_hist_entry, he);
2104 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2108 static void print_c2c_info(FILE *out, struct perf_session *session)
2110 struct perf_evlist *evlist = session->evlist;
2111 struct perf_evsel *evsel;
2114 fprintf(out, "=================================================\n");
2115 fprintf(out, " c2c details \n");
2116 fprintf(out, "=================================================\n");
2118 evlist__for_each_entry(evlist, evsel) {
2119 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2120 perf_evsel__name(evsel));
2123 fprintf(out, " Cachelines sort on : %s HITMs\n",
2124 display_str[c2c.display]);
2125 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2128 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2132 print_c2c__display_stats(out);
2134 print_shared_cacheline_info(out);
2136 print_c2c_info(out, session);
2142 fprintf(out, "=================================================\n");
2143 fprintf(out, " Shared Data Cache Line Table \n");
2144 fprintf(out, "=================================================\n");
2145 fprintf(out, "#\n");
2147 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, false);
2150 fprintf(out, "=================================================\n");
2151 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2152 fprintf(out, "=================================================\n");
2153 fprintf(out, "#\n");
2158 #ifdef HAVE_SLANG_SUPPORT
2159 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2162 struct rb_node *nd = rb_first(&hb->hists->entries);
2165 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2173 hb->nr_non_filtered_entries = nr_entries;
2176 struct c2c_cacheline_browser {
2177 struct hist_browser hb;
2178 struct hist_entry *he;
2182 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2183 char *bf, size_t size)
2185 struct c2c_cacheline_browser *cl_browser;
2186 struct hist_entry *he;
2189 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2190 he = cl_browser->he;
2193 addr = cl_address(he->mem_info->daddr.addr);
2195 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2199 static struct c2c_cacheline_browser*
2200 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2202 struct c2c_cacheline_browser *browser;
2204 browser = zalloc(sizeof(*browser));
2206 hist_browser__init(&browser->hb, hists);
2207 browser->hb.c2c_filter = true;
2208 browser->hb.title = perf_c2c_cacheline_browser__title;
2215 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2217 struct c2c_hist_entry *c2c_he;
2218 struct c2c_hists *c2c_hists;
2219 struct c2c_cacheline_browser *cl_browser;
2220 struct hist_browser *browser;
2223 " ENTER Togle callchains (if present) \n"
2224 " n Togle Node details info \n"
2225 " s Togle full lenght of symbol and source line columns \n"
2226 " q Return back to cacheline list \n";
2228 /* Display compact version first. */
2229 c2c.symbol_full = false;
2231 c2c_he = container_of(he, struct c2c_hist_entry, he);
2232 c2c_hists = c2c_he->hists;
2234 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2235 if (cl_browser == NULL)
2238 browser = &cl_browser->hb;
2240 /* reset abort key so that it can get Ctrl-C as a key */
2242 SLang_init_tty(0, 0, 0);
2244 c2c_browser__update_nr_entries(browser);
2247 key = hist_browser__run(browser, "? - help");
2251 c2c.symbol_full = !c2c.symbol_full;
2254 c2c.node_info = (c2c.node_info + 1) % 3;
2255 setup_nodes_header();
2260 ui_browser__help_window(&browser->b, help);
2272 static int perf_c2c_browser__title(struct hist_browser *browser,
2273 char *bf, size_t size)
2276 "Shared Data Cache Line Table "
2277 "(%lu entries, sorted on %s HITMs)",
2278 browser->nr_non_filtered_entries,
2279 display_str[c2c.display]);
2283 static struct hist_browser*
2284 perf_c2c_browser__new(struct hists *hists)
2286 struct hist_browser *browser = hist_browser__new(hists);
2289 browser->title = perf_c2c_browser__title;
2290 browser->c2c_filter = true;
2296 static int perf_c2c__hists_browse(struct hists *hists)
2298 struct hist_browser *browser;
2301 " d Display cacheline details \n"
2302 " ENTER Togle callchains (if present) \n"
2305 browser = perf_c2c_browser__new(hists);
2306 if (browser == NULL)
2309 /* reset abort key so that it can get Ctrl-C as a key */
2311 SLang_init_tty(0, 0, 0);
2313 c2c_browser__update_nr_entries(browser);
2316 key = hist_browser__run(browser, "? - help");
2322 perf_c2c__browse_cacheline(browser->he_selection);
2325 ui_browser__help_window(&browser->b, help);
2333 hist_browser__delete(browser);
2337 static void perf_c2c_display(struct perf_session *session)
2339 if (use_browser == 0)
2340 perf_c2c__hists_fprintf(stdout, session);
2342 perf_c2c__hists_browse(&c2c.hists.hists);
2345 static void perf_c2c_display(struct perf_session *session)
2348 perf_c2c__hists_fprintf(stdout, session);
2350 #endif /* HAVE_SLANG_SUPPORT */
2352 static void ui_quirks(void)
2354 if (!c2c.use_stdio) {
2355 dim_offset.width = 5;
2356 dim_offset.header = header_offset_tui;
2359 dim_percent_hitm.header = percent_hitm_header[c2c.display];
2362 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2364 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2365 CALLCHAIN_REPORT_HELP
2366 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2369 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2371 struct callchain_param *callchain = opt->value;
2373 callchain->enabled = !unset;
2378 symbol_conf.use_callchain = false;
2379 callchain->mode = CHAIN_NONE;
2383 return parse_callchain_report_opt(arg);
2386 static int setup_callchain(struct perf_evlist *evlist)
2388 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2389 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2391 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2392 (sample_type & PERF_SAMPLE_STACK_USER))
2393 mode = CALLCHAIN_DWARF;
2394 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2395 mode = CALLCHAIN_LBR;
2396 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2397 mode = CALLCHAIN_FP;
2399 if (!callchain_param.enabled &&
2400 callchain_param.mode != CHAIN_NONE &&
2401 mode != CALLCHAIN_NONE) {
2402 symbol_conf.use_callchain = true;
2403 if (callchain_register_param(&callchain_param) < 0) {
2404 ui__error("Can't register callchain params.\n");
2409 callchain_param.record_mode = mode;
2410 callchain_param.min_percent = 0;
2414 static int setup_display(const char *str)
2416 const char *display = str ?: "tot";
2418 if (!strcmp(display, "tot"))
2419 c2c.display = DISPLAY_TOT;
2420 else if (!strcmp(display, "rmt"))
2421 c2c.display = DISPLAY_RMT;
2422 else if (!strcmp(display, "lcl"))
2423 c2c.display = DISPLAY_LCL;
2425 pr_err("failed: unknown display type: %s\n", str);
2432 #define for_each_token(__tok, __buf, __sep, __tmp) \
2433 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2434 __tok = strtok_r(NULL, __sep, &__tmp))
2436 static int build_cl_output(char *cl_sort, bool no_source)
2438 char *tok, *tmp, *buf = strdup(cl_sort);
2439 bool add_pid = false;
2440 bool add_tid = false;
2441 bool add_iaddr = false;
2442 bool add_sym = false;
2443 bool add_dso = false;
2444 bool add_src = false;
2449 for_each_token(tok, buf, ",", tmp) {
2450 if (!strcmp(tok, "tid")) {
2452 } else if (!strcmp(tok, "pid")) {
2454 } else if (!strcmp(tok, "iaddr")) {
2458 add_src = no_source ? false : true;
2459 } else if (!strcmp(tok, "dso")) {
2461 } else if (strcmp(tok, "offset")) {
2462 pr_err("unrecognized sort token: %s\n", tok);
2467 if (asprintf(&c2c.cl_output,
2468 "%s%s%s%s%s%s%s%s%s%s",
2469 c2c.use_stdio ? "cl_num_empty," : "",
2472 "percent_stores_l1hit,"
2473 "percent_stores_l1miss,"
2475 add_pid ? "pid," : "",
2476 add_tid ? "tid," : "",
2477 add_iaddr ? "iaddr," : "",
2483 add_sym ? "symbol," : "",
2484 add_dso ? "dso," : "",
2485 add_src ? "cl_srcline," : "",
2489 c2c.show_src = add_src;
2495 static int setup_coalesce(const char *coalesce, bool no_source)
2497 const char *c = coalesce ?: coalesce_default;
2499 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2502 if (build_cl_output(c2c.cl_sort, no_source))
2505 if (asprintf(&c2c.cl_resort, "offset,%s",
2506 c2c.display == DISPLAY_TOT ?
2508 c2c.display == DISPLAY_RMT ?
2509 "rmt_hitm,lcl_hitm" :
2510 "lcl_hitm,rmt_hitm") < 0)
2513 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2514 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2515 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2519 static int perf_c2c__report(int argc, const char **argv)
2521 struct perf_session *session;
2522 struct ui_progress prog;
2523 struct perf_data_file file = {
2524 .mode = PERF_DATA_MODE_READ,
2526 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2527 const char *display = NULL;
2528 const char *coalesce = NULL;
2529 bool no_source = false;
2530 const struct option options[] = {
2531 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2532 "file", "vmlinux pathname"),
2533 OPT_STRING('i', "input", &input_name, "file",
2534 "the input file to process"),
2535 OPT_INCR('N', "node-info", &c2c.node_info,
2536 "show extra node info in report (repeat for more info)"),
2537 #ifdef HAVE_SLANG_SUPPORT
2538 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2540 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2541 "Display only statistic tables (implies --stdio)"),
2542 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2543 "Display full length of symbols"),
2544 OPT_BOOLEAN(0, "no-source", &no_source,
2545 "Do not display Source Line column"),
2546 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2547 "Show all captured HITM lines."),
2548 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2549 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2550 callchain_help, &parse_callchain_opt,
2551 callchain_default_opt),
2552 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2553 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2554 "coalesce fields: pid,tid,iaddr,dso"),
2555 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2556 OPT_PARENT(c2c_options),
2561 argc = parse_options(argc, argv, options, report_c2c_usage,
2562 PARSE_OPT_STOP_AT_NON_OPTION);
2564 usage_with_options(report_c2c_usage, options);
2567 c2c.use_stdio = true;
2569 if (!input_name || !strlen(input_name))
2570 input_name = "perf.data";
2572 file.path = input_name;
2573 file.force = symbol_conf.force;
2575 err = setup_display(display);
2579 err = setup_coalesce(coalesce, no_source);
2581 pr_debug("Failed to initialize hists\n");
2585 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2587 pr_debug("Failed to initialize hists\n");
2591 session = perf_session__new(&file, 0, &c2c.tool);
2592 if (session == NULL) {
2593 pr_debug("No memory for session\n");
2597 err = setup_nodes(session);
2599 pr_err("Failed setup nodes\n");
2603 err = setup_callchain(session->evlist);
2607 if (symbol__init(&session->header.env) < 0)
2610 /* No pipe support at the moment. */
2611 if (perf_data_file__is_pipe(session->file)) {
2612 pr_debug("No pipe support at the moment.\n");
2621 setup_browser(false);
2623 err = perf_session__process_events(session);
2625 pr_err("failed to process sample\n");
2629 c2c_hists__reinit(&c2c.hists,
2634 "tot_hitm,lcl_hitm,rmt_hitm,"
2635 "stores,stores_l1hit,stores_l1miss,"
2636 "dram_lcl,dram_rmt,"
2639 "ld_fbhit,ld_l1hit,ld_l2hit,"
2640 "ld_lclhit,ld_rmthit",
2641 c2c.display == DISPLAY_TOT ? "tot_hitm" :
2642 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
2645 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2647 hists__collapse_resort(&c2c.hists.hists, NULL);
2648 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2649 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2651 ui_progress__finish();
2655 perf_c2c_display(session);
2658 perf_session__delete(session);
2663 static int parse_record_events(const struct option *opt,
2664 const char *str, int unset __maybe_unused)
2666 bool *event_set = (bool *) opt->value;
2669 return perf_mem_events__parse(str);
2673 static const char * const __usage_record[] = {
2674 "perf c2c record [<options>] [<command>]",
2675 "perf c2c record [<options>] -- <command> [<options>]",
2679 static const char * const *record_mem_usage = __usage_record;
2681 static int perf_c2c__record(int argc, const char **argv)
2683 int rec_argc, i = 0, j;
2684 const char **rec_argv;
2686 bool all_user = false, all_kernel = false;
2687 bool event_set = false;
2688 struct option options[] = {
2689 OPT_CALLBACK('e', "event", &event_set, "event",
2690 "event selector. Use 'perf mem record -e list' to list available events",
2691 parse_record_events),
2692 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2693 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2694 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2695 OPT_PARENT(c2c_options),
2699 if (perf_mem_events__init()) {
2700 pr_err("failed: memory events not supported\n");
2704 argc = parse_options(argc, argv, options, record_mem_usage,
2705 PARSE_OPT_KEEP_UNKNOWN);
2707 rec_argc = argc + 10; /* max number of arguments */
2708 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2712 rec_argv[i++] = "record";
2715 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2716 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2719 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2720 rec_argv[i++] = "-W";
2722 rec_argv[i++] = "-d";
2723 rec_argv[i++] = "--sample-cpu";
2725 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2726 if (!perf_mem_events[j].record)
2729 if (!perf_mem_events[j].supported) {
2730 pr_err("failed: event '%s' not supported\n",
2731 perf_mem_events[j].name);
2735 rec_argv[i++] = "-e";
2736 rec_argv[i++] = perf_mem_events__name(j);
2740 rec_argv[i++] = "--all-user";
2743 rec_argv[i++] = "--all-kernel";
2745 for (j = 0; j < argc; j++, i++)
2746 rec_argv[i] = argv[j];
2749 pr_debug("calling: ");
2753 while (rec_argv[j]) {
2754 pr_debug("%s ", rec_argv[j]);
2760 ret = cmd_record(i, rec_argv);
2765 int cmd_c2c(int argc, const char **argv)
2767 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2768 PARSE_OPT_STOP_AT_NON_OPTION);
2771 usage_with_options(c2c_usage, c2c_options);
2773 if (!strncmp(argv[0], "rec", 3)) {
2774 return perf_c2c__record(argc, argv);
2775 } else if (!strncmp(argv[0], "rep", 3)) {
2776 return perf_c2c__report(argc, argv);
2778 usage_with_options(c2c_usage, c2c_options);