Linux 4.19.133
[linux/fpc-iii.git] / tools / perf / builtin-c2c.c
blob2bd39fdc8ab0356e2aff2b4ac821eb0d3a9b6ae4
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
8 * Original authors:
9 * Don Zickus <dzickus@redhat.com>
10 * Dick Fowles <fowles@inreach.com>
11 * Joe Mario <jmario@redhat.com>
13 #include <errno.h>
14 #include <inttypes.h>
15 #include <linux/compiler.h>
16 #include <linux/kernel.h>
17 #include <linux/stringify.h>
18 #include <asm/bug.h>
19 #include <sys/param.h>
20 #include "util.h"
21 #include "debug.h"
22 #include "builtin.h"
23 #include <subcmd/parse-options.h>
24 #include "mem-events.h"
25 #include "session.h"
26 #include "hist.h"
27 #include "sort.h"
28 #include "tool.h"
29 #include "data.h"
30 #include "event.h"
31 #include "evlist.h"
32 #include "evsel.h"
33 #include "ui/browsers/hists.h"
34 #include "thread.h"
35 #include "mem2node.h"
37 struct c2c_hists {
38 struct hists hists;
39 struct perf_hpp_list list;
40 struct c2c_stats stats;
43 struct compute_stats {
44 struct stats lcl_hitm;
45 struct stats rmt_hitm;
46 struct stats load;
49 struct c2c_hist_entry {
50 struct c2c_hists *hists;
51 struct c2c_stats stats;
52 unsigned long *cpuset;
53 unsigned long *nodeset;
54 struct c2c_stats *node_stats;
55 unsigned int cacheline_idx;
57 struct compute_stats cstats;
59 unsigned long paddr;
60 unsigned long paddr_cnt;
61 bool paddr_zero;
62 char *nodestr;
65 * must be at the end,
66 * because of its callchain dynamic entry
68 struct hist_entry he;
71 static char const *coalesce_default = "pid,iaddr";
73 struct perf_c2c {
74 struct perf_tool tool;
75 struct c2c_hists hists;
76 struct mem2node mem2node;
78 unsigned long **nodes;
79 int nodes_cnt;
80 int cpus_cnt;
81 int *cpu2node;
82 int node_info;
84 bool show_src;
85 bool show_all;
86 bool use_stdio;
87 bool stats_only;
88 bool symbol_full;
90 /* HITM shared clines stats */
91 struct c2c_stats hitm_stats;
92 int shared_clines;
94 int display;
96 const char *coalesce;
97 char *cl_sort;
98 char *cl_resort;
99 char *cl_output;
102 enum {
103 DISPLAY_LCL,
104 DISPLAY_RMT,
105 DISPLAY_TOT,
106 DISPLAY_MAX,
109 static const char *display_str[DISPLAY_MAX] = {
110 [DISPLAY_LCL] = "Local",
111 [DISPLAY_RMT] = "Remote",
112 [DISPLAY_TOT] = "Total",
115 static const struct option c2c_options[] = {
116 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
117 OPT_END()
120 static struct perf_c2c c2c;
122 static void *c2c_he_zalloc(size_t size)
124 struct c2c_hist_entry *c2c_he;
126 c2c_he = zalloc(size + sizeof(*c2c_he));
127 if (!c2c_he)
128 return NULL;
130 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
131 if (!c2c_he->cpuset)
132 return NULL;
134 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
135 if (!c2c_he->nodeset)
136 return NULL;
138 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
139 if (!c2c_he->node_stats)
140 return NULL;
142 init_stats(&c2c_he->cstats.lcl_hitm);
143 init_stats(&c2c_he->cstats.rmt_hitm);
144 init_stats(&c2c_he->cstats.load);
146 return &c2c_he->he;
149 static void c2c_he_free(void *he)
151 struct c2c_hist_entry *c2c_he;
153 c2c_he = container_of(he, struct c2c_hist_entry, he);
154 if (c2c_he->hists) {
155 hists__delete_entries(&c2c_he->hists->hists);
156 free(c2c_he->hists);
159 free(c2c_he->cpuset);
160 free(c2c_he->nodeset);
161 free(c2c_he->nodestr);
162 free(c2c_he->node_stats);
163 free(c2c_he);
166 static struct hist_entry_ops c2c_entry_ops = {
167 .new = c2c_he_zalloc,
168 .free = c2c_he_free,
171 static int c2c_hists__init(struct c2c_hists *hists,
172 const char *sort,
173 int nr_header_lines);
175 static struct c2c_hists*
176 he__get_c2c_hists(struct hist_entry *he,
177 const char *sort,
178 int nr_header_lines)
180 struct c2c_hist_entry *c2c_he;
181 struct c2c_hists *hists;
182 int ret;
184 c2c_he = container_of(he, struct c2c_hist_entry, he);
185 if (c2c_he->hists)
186 return c2c_he->hists;
188 hists = c2c_he->hists = zalloc(sizeof(*hists));
189 if (!hists)
190 return NULL;
192 ret = c2c_hists__init(hists, sort, nr_header_lines);
193 if (ret) {
194 free(hists);
195 return NULL;
198 return hists;
201 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
202 struct perf_sample *sample)
204 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
205 "WARNING: no sample cpu value"))
206 return;
208 set_bit(sample->cpu, c2c_he->cpuset);
211 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
212 struct perf_sample *sample)
214 int node;
216 if (!sample->phys_addr) {
217 c2c_he->paddr_zero = true;
218 return;
221 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
222 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
223 return;
225 set_bit(node, c2c_he->nodeset);
227 if (c2c_he->paddr != sample->phys_addr) {
228 c2c_he->paddr_cnt++;
229 c2c_he->paddr = sample->phys_addr;
233 static void compute_stats(struct c2c_hist_entry *c2c_he,
234 struct c2c_stats *stats,
235 u64 weight)
237 struct compute_stats *cstats = &c2c_he->cstats;
239 if (stats->rmt_hitm)
240 update_stats(&cstats->rmt_hitm, weight);
241 else if (stats->lcl_hitm)
242 update_stats(&cstats->lcl_hitm, weight);
243 else if (stats->load)
244 update_stats(&cstats->load, weight);
247 static int process_sample_event(struct perf_tool *tool __maybe_unused,
248 union perf_event *event,
249 struct perf_sample *sample,
250 struct perf_evsel *evsel,
251 struct machine *machine)
253 struct c2c_hists *c2c_hists = &c2c.hists;
254 struct c2c_hist_entry *c2c_he;
255 struct c2c_stats stats = { .nr_entries = 0, };
256 struct hist_entry *he;
257 struct addr_location al;
258 struct mem_info *mi, *mi_dup;
259 int ret;
261 if (machine__resolve(machine, &al, sample) < 0) {
262 pr_debug("problem processing %d event, skipping it.\n",
263 event->header.type);
264 return -1;
267 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
268 evsel, &al, sysctl_perf_event_max_stack);
269 if (ret)
270 goto out;
272 mi = sample__resolve_mem(sample, &al);
273 if (mi == NULL)
274 return -ENOMEM;
277 * The mi object is released in hists__add_entry_ops,
278 * if it gets sorted out into existing data, so we need
279 * to take the copy now.
281 mi_dup = mem_info__get(mi);
283 c2c_decode_stats(&stats, mi);
285 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
286 &al, NULL, NULL, mi,
287 sample, true);
288 if (he == NULL)
289 goto free_mi;
291 c2c_he = container_of(he, struct c2c_hist_entry, he);
292 c2c_add_stats(&c2c_he->stats, &stats);
293 c2c_add_stats(&c2c_hists->stats, &stats);
295 c2c_he__set_cpu(c2c_he, sample);
296 c2c_he__set_node(c2c_he, sample);
298 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
299 ret = hist_entry__append_callchain(he, sample);
301 if (!ret) {
303 * There's already been warning about missing
304 * sample's cpu value. Let's account all to
305 * node 0 in this case, without any further
306 * warning.
308 * Doing node stats only for single callchain data.
310 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
311 int node = c2c.cpu2node[cpu];
313 mi = mi_dup;
315 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
316 if (!c2c_hists)
317 goto free_mi;
319 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
320 &al, NULL, NULL, mi,
321 sample, true);
322 if (he == NULL)
323 goto free_mi;
325 c2c_he = container_of(he, struct c2c_hist_entry, he);
326 c2c_add_stats(&c2c_he->stats, &stats);
327 c2c_add_stats(&c2c_hists->stats, &stats);
328 c2c_add_stats(&c2c_he->node_stats[node], &stats);
330 compute_stats(c2c_he, &stats, sample->weight);
332 c2c_he__set_cpu(c2c_he, sample);
333 c2c_he__set_node(c2c_he, sample);
335 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
336 ret = hist_entry__append_callchain(he, sample);
339 out:
340 addr_location__put(&al);
341 return ret;
343 free_mi:
344 mem_info__put(mi_dup);
345 mem_info__put(mi);
346 ret = -ENOMEM;
347 goto out;
350 static struct perf_c2c c2c = {
351 .tool = {
352 .sample = process_sample_event,
353 .mmap = perf_event__process_mmap,
354 .mmap2 = perf_event__process_mmap2,
355 .comm = perf_event__process_comm,
356 .exit = perf_event__process_exit,
357 .fork = perf_event__process_fork,
358 .lost = perf_event__process_lost,
359 .ordered_events = true,
360 .ordering_requires_timestamps = true,
364 static const char * const c2c_usage[] = {
365 "perf c2c {record|report}",
366 NULL
369 static const char * const __usage_report[] = {
370 "perf c2c report",
371 NULL
374 static const char * const *report_c2c_usage = __usage_report;
376 #define C2C_HEADER_MAX 2
378 struct c2c_header {
379 struct {
380 const char *text;
381 int span;
382 } line[C2C_HEADER_MAX];
385 struct c2c_dimension {
386 struct c2c_header header;
387 const char *name;
388 int width;
389 struct sort_entry *se;
391 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
392 struct hist_entry *, struct hist_entry *);
393 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
394 struct hist_entry *he);
395 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
396 struct hist_entry *he);
399 struct c2c_fmt {
400 struct perf_hpp_fmt fmt;
401 struct c2c_dimension *dim;
404 #define SYMBOL_WIDTH 30
406 static struct c2c_dimension dim_symbol;
407 static struct c2c_dimension dim_srcline;
409 static int symbol_width(struct hists *hists, struct sort_entry *se)
411 int width = hists__col_len(hists, se->se_width_idx);
413 if (!c2c.symbol_full)
414 width = MIN(width, SYMBOL_WIDTH);
416 return width;
419 static int c2c_width(struct perf_hpp_fmt *fmt,
420 struct perf_hpp *hpp __maybe_unused,
421 struct hists *hists)
423 struct c2c_fmt *c2c_fmt;
424 struct c2c_dimension *dim;
426 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
427 dim = c2c_fmt->dim;
429 if (dim == &dim_symbol || dim == &dim_srcline)
430 return symbol_width(hists, dim->se);
432 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
433 c2c_fmt->dim->width;
436 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
437 struct hists *hists, int line, int *span)
439 struct perf_hpp_list *hpp_list = hists->hpp_list;
440 struct c2c_fmt *c2c_fmt;
441 struct c2c_dimension *dim;
442 const char *text = NULL;
443 int width = c2c_width(fmt, hpp, hists);
445 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
446 dim = c2c_fmt->dim;
448 if (dim->se) {
449 text = dim->header.line[line].text;
450 /* Use the last line from sort_entry if not defined. */
451 if (!text && (line == hpp_list->nr_header_lines - 1))
452 text = dim->se->se_header;
453 } else {
454 text = dim->header.line[line].text;
456 if (*span) {
457 (*span)--;
458 return 0;
459 } else {
460 *span = dim->header.line[line].span;
464 if (text == NULL)
465 text = "";
467 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
470 #define HEX_STR(__s, __v) \
471 ({ \
472 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
473 __s; \
476 static int64_t
477 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
478 struct hist_entry *left, struct hist_entry *right)
480 return sort__dcacheline_cmp(left, right);
483 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
484 struct hist_entry *he)
486 uint64_t addr = 0;
487 int width = c2c_width(fmt, hpp, he->hists);
488 char buf[20];
490 if (he->mem_info)
491 addr = cl_address(he->mem_info->daddr.addr);
493 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
496 static int
497 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
498 struct hist_entry *he)
500 struct c2c_hist_entry *c2c_he;
501 int width = c2c_width(fmt, hpp, he->hists);
503 c2c_he = container_of(he, struct c2c_hist_entry, he);
504 if (WARN_ON_ONCE(!c2c_he->nodestr))
505 return 0;
507 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
510 static int
511 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
512 struct hist_entry *he)
514 struct c2c_hist_entry *c2c_he;
515 int width = c2c_width(fmt, hpp, he->hists);
517 c2c_he = container_of(he, struct c2c_hist_entry, he);
518 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
521 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
522 struct hist_entry *he)
524 uint64_t addr = 0;
525 int width = c2c_width(fmt, hpp, he->hists);
526 char buf[20];
528 if (he->mem_info)
529 addr = cl_offset(he->mem_info->daddr.al_addr);
531 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
534 static int64_t
535 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
536 struct hist_entry *left, struct hist_entry *right)
538 uint64_t l = 0, r = 0;
540 if (left->mem_info)
541 l = cl_offset(left->mem_info->daddr.addr);
542 if (right->mem_info)
543 r = cl_offset(right->mem_info->daddr.addr);
545 return (int64_t)(r - l);
548 static int
549 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
550 struct hist_entry *he)
552 uint64_t addr = 0;
553 int width = c2c_width(fmt, hpp, he->hists);
554 char buf[20];
556 if (he->mem_info)
557 addr = he->mem_info->iaddr.addr;
559 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
562 static int64_t
563 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
564 struct hist_entry *left, struct hist_entry *right)
566 return sort__iaddr_cmp(left, right);
569 static int
570 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
571 struct hist_entry *he)
573 struct c2c_hist_entry *c2c_he;
574 int width = c2c_width(fmt, hpp, he->hists);
575 unsigned int tot_hitm;
577 c2c_he = container_of(he, struct c2c_hist_entry, he);
578 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
580 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
583 static int64_t
584 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
585 struct hist_entry *left, struct hist_entry *right)
587 struct c2c_hist_entry *c2c_left;
588 struct c2c_hist_entry *c2c_right;
589 uint64_t tot_hitm_left;
590 uint64_t tot_hitm_right;
592 c2c_left = container_of(left, struct c2c_hist_entry, he);
593 c2c_right = container_of(right, struct c2c_hist_entry, he);
595 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
596 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
598 return tot_hitm_left - tot_hitm_right;
601 #define STAT_FN_ENTRY(__f) \
602 static int \
603 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
604 struct hist_entry *he) \
606 struct c2c_hist_entry *c2c_he; \
607 int width = c2c_width(fmt, hpp, he->hists); \
609 c2c_he = container_of(he, struct c2c_hist_entry, he); \
610 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
611 c2c_he->stats.__f); \
614 #define STAT_FN_CMP(__f) \
615 static int64_t \
616 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
617 struct hist_entry *left, struct hist_entry *right) \
619 struct c2c_hist_entry *c2c_left, *c2c_right; \
621 c2c_left = container_of(left, struct c2c_hist_entry, he); \
622 c2c_right = container_of(right, struct c2c_hist_entry, he); \
623 return (uint64_t) c2c_left->stats.__f - \
624 (uint64_t) c2c_right->stats.__f; \
627 #define STAT_FN(__f) \
628 STAT_FN_ENTRY(__f) \
629 STAT_FN_CMP(__f)
631 STAT_FN(rmt_hitm)
632 STAT_FN(lcl_hitm)
633 STAT_FN(store)
634 STAT_FN(st_l1hit)
635 STAT_FN(st_l1miss)
636 STAT_FN(ld_fbhit)
637 STAT_FN(ld_l1hit)
638 STAT_FN(ld_l2hit)
639 STAT_FN(ld_llchit)
640 STAT_FN(rmt_hit)
642 static uint64_t llc_miss(struct c2c_stats *stats)
644 uint64_t llcmiss;
646 llcmiss = stats->lcl_dram +
647 stats->rmt_dram +
648 stats->rmt_hitm +
649 stats->rmt_hit;
651 return llcmiss;
654 static int
655 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
656 struct hist_entry *he)
658 struct c2c_hist_entry *c2c_he;
659 int width = c2c_width(fmt, hpp, he->hists);
661 c2c_he = container_of(he, struct c2c_hist_entry, he);
663 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
664 llc_miss(&c2c_he->stats));
667 static int64_t
668 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
669 struct hist_entry *left, struct hist_entry *right)
671 struct c2c_hist_entry *c2c_left;
672 struct c2c_hist_entry *c2c_right;
674 c2c_left = container_of(left, struct c2c_hist_entry, he);
675 c2c_right = container_of(right, struct c2c_hist_entry, he);
677 return (uint64_t) llc_miss(&c2c_left->stats) -
678 (uint64_t) llc_miss(&c2c_right->stats);
681 static uint64_t total_records(struct c2c_stats *stats)
683 uint64_t lclmiss, ldcnt, total;
685 lclmiss = stats->lcl_dram +
686 stats->rmt_dram +
687 stats->rmt_hitm +
688 stats->rmt_hit;
690 ldcnt = lclmiss +
691 stats->ld_fbhit +
692 stats->ld_l1hit +
693 stats->ld_l2hit +
694 stats->ld_llchit +
695 stats->lcl_hitm;
697 total = ldcnt +
698 stats->st_l1hit +
699 stats->st_l1miss;
701 return total;
704 static int
705 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
706 struct hist_entry *he)
708 struct c2c_hist_entry *c2c_he;
709 int width = c2c_width(fmt, hpp, he->hists);
710 uint64_t tot_recs;
712 c2c_he = container_of(he, struct c2c_hist_entry, he);
713 tot_recs = total_records(&c2c_he->stats);
715 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
718 static int64_t
719 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
720 struct hist_entry *left, struct hist_entry *right)
722 struct c2c_hist_entry *c2c_left;
723 struct c2c_hist_entry *c2c_right;
724 uint64_t tot_recs_left;
725 uint64_t tot_recs_right;
727 c2c_left = container_of(left, struct c2c_hist_entry, he);
728 c2c_right = container_of(right, struct c2c_hist_entry, he);
730 tot_recs_left = total_records(&c2c_left->stats);
731 tot_recs_right = total_records(&c2c_right->stats);
733 return tot_recs_left - tot_recs_right;
736 static uint64_t total_loads(struct c2c_stats *stats)
738 uint64_t lclmiss, ldcnt;
740 lclmiss = stats->lcl_dram +
741 stats->rmt_dram +
742 stats->rmt_hitm +
743 stats->rmt_hit;
745 ldcnt = lclmiss +
746 stats->ld_fbhit +
747 stats->ld_l1hit +
748 stats->ld_l2hit +
749 stats->ld_llchit +
750 stats->lcl_hitm;
752 return ldcnt;
755 static int
756 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
757 struct hist_entry *he)
759 struct c2c_hist_entry *c2c_he;
760 int width = c2c_width(fmt, hpp, he->hists);
761 uint64_t tot_recs;
763 c2c_he = container_of(he, struct c2c_hist_entry, he);
764 tot_recs = total_loads(&c2c_he->stats);
766 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
769 static int64_t
770 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
771 struct hist_entry *left, struct hist_entry *right)
773 struct c2c_hist_entry *c2c_left;
774 struct c2c_hist_entry *c2c_right;
775 uint64_t tot_recs_left;
776 uint64_t tot_recs_right;
778 c2c_left = container_of(left, struct c2c_hist_entry, he);
779 c2c_right = container_of(right, struct c2c_hist_entry, he);
781 tot_recs_left = total_loads(&c2c_left->stats);
782 tot_recs_right = total_loads(&c2c_right->stats);
784 return tot_recs_left - tot_recs_right;
787 typedef double (get_percent_cb)(struct c2c_hist_entry *);
789 static int
790 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
791 struct hist_entry *he, get_percent_cb get_percent)
793 struct c2c_hist_entry *c2c_he;
794 int width = c2c_width(fmt, hpp, he->hists);
795 double per;
797 c2c_he = container_of(he, struct c2c_hist_entry, he);
798 per = get_percent(c2c_he);
800 #ifdef HAVE_SLANG_SUPPORT
801 if (use_browser)
802 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
803 #endif
804 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
807 static double percent_hitm(struct c2c_hist_entry *c2c_he)
809 struct c2c_hists *hists;
810 struct c2c_stats *stats;
811 struct c2c_stats *total;
812 int tot = 0, st = 0;
813 double p;
815 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
816 stats = &c2c_he->stats;
817 total = &hists->stats;
819 switch (c2c.display) {
820 case DISPLAY_RMT:
821 st = stats->rmt_hitm;
822 tot = total->rmt_hitm;
823 break;
824 case DISPLAY_LCL:
825 st = stats->lcl_hitm;
826 tot = total->lcl_hitm;
827 break;
828 case DISPLAY_TOT:
829 st = stats->tot_hitm;
830 tot = total->tot_hitm;
831 default:
832 break;
835 p = tot ? (double) st / tot : 0;
837 return 100 * p;
840 #define PERC_STR(__s, __v) \
841 ({ \
842 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
843 __s; \
846 static int
847 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
848 struct hist_entry *he)
850 struct c2c_hist_entry *c2c_he;
851 int width = c2c_width(fmt, hpp, he->hists);
852 char buf[10];
853 double per;
855 c2c_he = container_of(he, struct c2c_hist_entry, he);
856 per = percent_hitm(c2c_he);
857 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
860 static int
861 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
862 struct hist_entry *he)
864 return percent_color(fmt, hpp, he, percent_hitm);
867 static int64_t
868 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
869 struct hist_entry *left, struct hist_entry *right)
871 struct c2c_hist_entry *c2c_left;
872 struct c2c_hist_entry *c2c_right;
873 double per_left;
874 double per_right;
876 c2c_left = container_of(left, struct c2c_hist_entry, he);
877 c2c_right = container_of(right, struct c2c_hist_entry, he);
879 per_left = percent_hitm(c2c_left);
880 per_right = percent_hitm(c2c_right);
882 return per_left - per_right;
885 static struct c2c_stats *he_stats(struct hist_entry *he)
887 struct c2c_hist_entry *c2c_he;
889 c2c_he = container_of(he, struct c2c_hist_entry, he);
890 return &c2c_he->stats;
893 static struct c2c_stats *total_stats(struct hist_entry *he)
895 struct c2c_hists *hists;
897 hists = container_of(he->hists, struct c2c_hists, hists);
898 return &hists->stats;
901 static double percent(int st, int tot)
903 return tot ? 100. * (double) st / (double) tot : 0;
906 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
908 #define PERCENT_FN(__f) \
909 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
911 struct c2c_hists *hists; \
913 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
914 return percent(c2c_he->stats.__f, hists->stats.__f); \
917 PERCENT_FN(rmt_hitm)
918 PERCENT_FN(lcl_hitm)
919 PERCENT_FN(st_l1hit)
920 PERCENT_FN(st_l1miss)
922 static int
923 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
924 struct hist_entry *he)
926 int width = c2c_width(fmt, hpp, he->hists);
927 double per = PERCENT(he, rmt_hitm);
928 char buf[10];
930 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
933 static int
934 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
935 struct hist_entry *he)
937 return percent_color(fmt, hpp, he, percent_rmt_hitm);
940 static int64_t
941 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
942 struct hist_entry *left, struct hist_entry *right)
944 double per_left;
945 double per_right;
947 per_left = PERCENT(left, lcl_hitm);
948 per_right = PERCENT(right, lcl_hitm);
950 return per_left - per_right;
953 static int
954 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
955 struct hist_entry *he)
957 int width = c2c_width(fmt, hpp, he->hists);
958 double per = PERCENT(he, lcl_hitm);
959 char buf[10];
961 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
964 static int
965 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
966 struct hist_entry *he)
968 return percent_color(fmt, hpp, he, percent_lcl_hitm);
971 static int64_t
972 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
973 struct hist_entry *left, struct hist_entry *right)
975 double per_left;
976 double per_right;
978 per_left = PERCENT(left, lcl_hitm);
979 per_right = PERCENT(right, lcl_hitm);
981 return per_left - per_right;
984 static int
985 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
986 struct hist_entry *he)
988 int width = c2c_width(fmt, hpp, he->hists);
989 double per = PERCENT(he, st_l1hit);
990 char buf[10];
992 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
995 static int
996 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
997 struct hist_entry *he)
999 return percent_color(fmt, hpp, he, percent_st_l1hit);
1002 static int64_t
1003 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1004 struct hist_entry *left, struct hist_entry *right)
1006 double per_left;
1007 double per_right;
1009 per_left = PERCENT(left, st_l1hit);
1010 per_right = PERCENT(right, st_l1hit);
1012 return per_left - per_right;
1015 static int
1016 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1017 struct hist_entry *he)
1019 int width = c2c_width(fmt, hpp, he->hists);
1020 double per = PERCENT(he, st_l1miss);
1021 char buf[10];
1023 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1026 static int
1027 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1028 struct hist_entry *he)
1030 return percent_color(fmt, hpp, he, percent_st_l1miss);
1033 static int64_t
1034 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1035 struct hist_entry *left, struct hist_entry *right)
1037 double per_left;
1038 double per_right;
1040 per_left = PERCENT(left, st_l1miss);
1041 per_right = PERCENT(right, st_l1miss);
1043 return per_left - per_right;
1046 STAT_FN(lcl_dram)
1047 STAT_FN(rmt_dram)
1049 static int
1050 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1051 struct hist_entry *he)
1053 int width = c2c_width(fmt, hpp, he->hists);
1055 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1058 static int64_t
1059 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1060 struct hist_entry *left, struct hist_entry *right)
1062 return left->thread->pid_ - right->thread->pid_;
1065 static int64_t
1066 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1067 struct hist_entry *left __maybe_unused,
1068 struct hist_entry *right __maybe_unused)
1070 return 0;
1073 static int
1074 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1075 struct hist_entry *he)
1077 struct c2c_hist_entry *c2c_he;
1078 bool first = true;
1079 int node;
1080 int ret = 0;
1082 c2c_he = container_of(he, struct c2c_hist_entry, he);
1084 for (node = 0; node < c2c.nodes_cnt; node++) {
1085 DECLARE_BITMAP(set, c2c.cpus_cnt);
1087 bitmap_zero(set, c2c.cpus_cnt);
1088 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1090 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1091 if (c2c.node_info == 1) {
1092 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1093 advance_hpp(hpp, ret);
1095 continue;
1098 if (!first) {
1099 ret = scnprintf(hpp->buf, hpp->size, " ");
1100 advance_hpp(hpp, ret);
1103 switch (c2c.node_info) {
1104 case 0:
1105 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1106 advance_hpp(hpp, ret);
1107 break;
1108 case 1:
1110 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
1111 struct c2c_stats *stats = &c2c_he->node_stats[node];
1113 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1114 advance_hpp(hpp, ret);
1116 #define DISPLAY_HITM(__h) \
1117 if (c2c_he->stats.__h> 0) { \
1118 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1119 percent(stats->__h, c2c_he->stats.__h));\
1120 } else { \
1121 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
1124 switch (c2c.display) {
1125 case DISPLAY_RMT:
1126 DISPLAY_HITM(rmt_hitm);
1127 break;
1128 case DISPLAY_LCL:
1129 DISPLAY_HITM(lcl_hitm);
1130 break;
1131 case DISPLAY_TOT:
1132 DISPLAY_HITM(tot_hitm);
1133 default:
1134 break;
1137 #undef DISPLAY_HITM
1139 advance_hpp(hpp, ret);
1141 if (c2c_he->stats.store > 0) {
1142 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1143 percent(stats->store, c2c_he->stats.store));
1144 } else {
1145 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1148 advance_hpp(hpp, ret);
1149 break;
1151 case 2:
1152 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1153 advance_hpp(hpp, ret);
1155 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1156 advance_hpp(hpp, ret);
1158 ret = scnprintf(hpp->buf, hpp->size, "}");
1159 advance_hpp(hpp, ret);
1160 break;
1161 default:
1162 break;
1165 first = false;
1168 return 0;
1171 static int
1172 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1173 struct hist_entry *he, double mean)
1175 int width = c2c_width(fmt, hpp, he->hists);
1176 char buf[10];
1178 scnprintf(buf, 10, "%6.0f", mean);
1179 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1182 #define MEAN_ENTRY(__func, __val) \
1183 static int \
1184 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1186 struct c2c_hist_entry *c2c_he; \
1187 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1188 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1191 MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1192 MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1193 MEAN_ENTRY(mean_load_entry, load);
1195 static int
1196 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1197 struct hist_entry *he)
1199 struct c2c_hist_entry *c2c_he;
1200 int width = c2c_width(fmt, hpp, he->hists);
1201 char buf[10];
1203 c2c_he = container_of(he, struct c2c_hist_entry, he);
1205 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1206 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1209 static int
1210 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1211 struct hist_entry *he)
1213 struct c2c_hist_entry *c2c_he;
1214 int width = c2c_width(fmt, hpp, he->hists);
1215 char buf[10];
1217 c2c_he = container_of(he, struct c2c_hist_entry, he);
1219 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1220 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1223 static int
1224 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1225 struct hist_entry *he)
1227 int width = c2c_width(fmt, hpp, he->hists);
1229 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1232 #define HEADER_LOW(__h) \
1234 .line[1] = { \
1235 .text = __h, \
1236 }, \
1239 #define HEADER_BOTH(__h0, __h1) \
1241 .line[0] = { \
1242 .text = __h0, \
1243 }, \
1244 .line[1] = { \
1245 .text = __h1, \
1246 }, \
1249 #define HEADER_SPAN(__h0, __h1, __s) \
1251 .line[0] = { \
1252 .text = __h0, \
1253 .span = __s, \
1254 }, \
1255 .line[1] = { \
1256 .text = __h1, \
1257 }, \
1260 #define HEADER_SPAN_LOW(__h) \
1262 .line[1] = { \
1263 .text = __h, \
1264 }, \
1267 static struct c2c_dimension dim_dcacheline = {
1268 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1269 .name = "dcacheline",
1270 .cmp = dcacheline_cmp,
1271 .entry = dcacheline_entry,
1272 .width = 18,
1275 static struct c2c_dimension dim_dcacheline_node = {
1276 .header = HEADER_LOW("Node"),
1277 .name = "dcacheline_node",
1278 .cmp = empty_cmp,
1279 .entry = dcacheline_node_entry,
1280 .width = 4,
1283 static struct c2c_dimension dim_dcacheline_count = {
1284 .header = HEADER_LOW("PA cnt"),
1285 .name = "dcacheline_count",
1286 .cmp = empty_cmp,
1287 .entry = dcacheline_node_count,
1288 .width = 6,
1291 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1293 static struct c2c_dimension dim_offset = {
1294 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
1295 .name = "offset",
1296 .cmp = offset_cmp,
1297 .entry = offset_entry,
1298 .width = 18,
1301 static struct c2c_dimension dim_offset_node = {
1302 .header = HEADER_LOW("Node"),
1303 .name = "offset_node",
1304 .cmp = empty_cmp,
1305 .entry = dcacheline_node_entry,
1306 .width = 4,
1309 static struct c2c_dimension dim_iaddr = {
1310 .header = HEADER_LOW("Code address"),
1311 .name = "iaddr",
1312 .cmp = iaddr_cmp,
1313 .entry = iaddr_entry,
1314 .width = 18,
1317 static struct c2c_dimension dim_tot_hitm = {
1318 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1319 .name = "tot_hitm",
1320 .cmp = tot_hitm_cmp,
1321 .entry = tot_hitm_entry,
1322 .width = 7,
1325 static struct c2c_dimension dim_lcl_hitm = {
1326 .header = HEADER_SPAN_LOW("Lcl"),
1327 .name = "lcl_hitm",
1328 .cmp = lcl_hitm_cmp,
1329 .entry = lcl_hitm_entry,
1330 .width = 7,
1333 static struct c2c_dimension dim_rmt_hitm = {
1334 .header = HEADER_SPAN_LOW("Rmt"),
1335 .name = "rmt_hitm",
1336 .cmp = rmt_hitm_cmp,
1337 .entry = rmt_hitm_entry,
1338 .width = 7,
1341 static struct c2c_dimension dim_cl_rmt_hitm = {
1342 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1343 .name = "cl_rmt_hitm",
1344 .cmp = rmt_hitm_cmp,
1345 .entry = rmt_hitm_entry,
1346 .width = 7,
1349 static struct c2c_dimension dim_cl_lcl_hitm = {
1350 .header = HEADER_SPAN_LOW("Lcl"),
1351 .name = "cl_lcl_hitm",
1352 .cmp = lcl_hitm_cmp,
1353 .entry = lcl_hitm_entry,
1354 .width = 7,
1357 static struct c2c_dimension dim_stores = {
1358 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1359 .name = "stores",
1360 .cmp = store_cmp,
1361 .entry = store_entry,
1362 .width = 7,
1365 static struct c2c_dimension dim_stores_l1hit = {
1366 .header = HEADER_SPAN_LOW("L1Hit"),
1367 .name = "stores_l1hit",
1368 .cmp = st_l1hit_cmp,
1369 .entry = st_l1hit_entry,
1370 .width = 7,
1373 static struct c2c_dimension dim_stores_l1miss = {
1374 .header = HEADER_SPAN_LOW("L1Miss"),
1375 .name = "stores_l1miss",
1376 .cmp = st_l1miss_cmp,
1377 .entry = st_l1miss_entry,
1378 .width = 7,
1381 static struct c2c_dimension dim_cl_stores_l1hit = {
1382 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1383 .name = "cl_stores_l1hit",
1384 .cmp = st_l1hit_cmp,
1385 .entry = st_l1hit_entry,
1386 .width = 7,
1389 static struct c2c_dimension dim_cl_stores_l1miss = {
1390 .header = HEADER_SPAN_LOW("L1 Miss"),
1391 .name = "cl_stores_l1miss",
1392 .cmp = st_l1miss_cmp,
1393 .entry = st_l1miss_entry,
1394 .width = 7,
1397 static struct c2c_dimension dim_ld_fbhit = {
1398 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1399 .name = "ld_fbhit",
1400 .cmp = ld_fbhit_cmp,
1401 .entry = ld_fbhit_entry,
1402 .width = 7,
1405 static struct c2c_dimension dim_ld_l1hit = {
1406 .header = HEADER_SPAN_LOW("L1"),
1407 .name = "ld_l1hit",
1408 .cmp = ld_l1hit_cmp,
1409 .entry = ld_l1hit_entry,
1410 .width = 7,
1413 static struct c2c_dimension dim_ld_l2hit = {
1414 .header = HEADER_SPAN_LOW("L2"),
1415 .name = "ld_l2hit",
1416 .cmp = ld_l2hit_cmp,
1417 .entry = ld_l2hit_entry,
1418 .width = 7,
1421 static struct c2c_dimension dim_ld_llchit = {
1422 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1423 .name = "ld_lclhit",
1424 .cmp = ld_llchit_cmp,
1425 .entry = ld_llchit_entry,
1426 .width = 8,
1429 static struct c2c_dimension dim_ld_rmthit = {
1430 .header = HEADER_SPAN_LOW("Rmt"),
1431 .name = "ld_rmthit",
1432 .cmp = rmt_hit_cmp,
1433 .entry = rmt_hit_entry,
1434 .width = 8,
1437 static struct c2c_dimension dim_ld_llcmiss = {
1438 .header = HEADER_BOTH("LLC", "Ld Miss"),
1439 .name = "ld_llcmiss",
1440 .cmp = ld_llcmiss_cmp,
1441 .entry = ld_llcmiss_entry,
1442 .width = 7,
1445 static struct c2c_dimension dim_tot_recs = {
1446 .header = HEADER_BOTH("Total", "records"),
1447 .name = "tot_recs",
1448 .cmp = tot_recs_cmp,
1449 .entry = tot_recs_entry,
1450 .width = 7,
1453 static struct c2c_dimension dim_tot_loads = {
1454 .header = HEADER_BOTH("Total", "Loads"),
1455 .name = "tot_loads",
1456 .cmp = tot_loads_cmp,
1457 .entry = tot_loads_entry,
1458 .width = 7,
1461 static struct c2c_header percent_hitm_header[] = {
1462 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1463 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1464 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1467 static struct c2c_dimension dim_percent_hitm = {
1468 .name = "percent_hitm",
1469 .cmp = percent_hitm_cmp,
1470 .entry = percent_hitm_entry,
1471 .color = percent_hitm_color,
1472 .width = 7,
1475 static struct c2c_dimension dim_percent_rmt_hitm = {
1476 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1477 .name = "percent_rmt_hitm",
1478 .cmp = percent_rmt_hitm_cmp,
1479 .entry = percent_rmt_hitm_entry,
1480 .color = percent_rmt_hitm_color,
1481 .width = 7,
1484 static struct c2c_dimension dim_percent_lcl_hitm = {
1485 .header = HEADER_SPAN_LOW("Lcl"),
1486 .name = "percent_lcl_hitm",
1487 .cmp = percent_lcl_hitm_cmp,
1488 .entry = percent_lcl_hitm_entry,
1489 .color = percent_lcl_hitm_color,
1490 .width = 7,
1493 static struct c2c_dimension dim_percent_stores_l1hit = {
1494 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1495 .name = "percent_stores_l1hit",
1496 .cmp = percent_stores_l1hit_cmp,
1497 .entry = percent_stores_l1hit_entry,
1498 .color = percent_stores_l1hit_color,
1499 .width = 7,
1502 static struct c2c_dimension dim_percent_stores_l1miss = {
1503 .header = HEADER_SPAN_LOW("L1 Miss"),
1504 .name = "percent_stores_l1miss",
1505 .cmp = percent_stores_l1miss_cmp,
1506 .entry = percent_stores_l1miss_entry,
1507 .color = percent_stores_l1miss_color,
1508 .width = 7,
1511 static struct c2c_dimension dim_dram_lcl = {
1512 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1513 .name = "dram_lcl",
1514 .cmp = lcl_dram_cmp,
1515 .entry = lcl_dram_entry,
1516 .width = 8,
1519 static struct c2c_dimension dim_dram_rmt = {
1520 .header = HEADER_SPAN_LOW("Rmt"),
1521 .name = "dram_rmt",
1522 .cmp = rmt_dram_cmp,
1523 .entry = rmt_dram_entry,
1524 .width = 8,
1527 static struct c2c_dimension dim_pid = {
1528 .header = HEADER_LOW("Pid"),
1529 .name = "pid",
1530 .cmp = pid_cmp,
1531 .entry = pid_entry,
1532 .width = 7,
1535 static struct c2c_dimension dim_tid = {
1536 .header = HEADER_LOW("Tid"),
1537 .name = "tid",
1538 .se = &sort_thread,
1541 static struct c2c_dimension dim_symbol = {
1542 .name = "symbol",
1543 .se = &sort_sym,
1546 static struct c2c_dimension dim_dso = {
1547 .header = HEADER_BOTH("Shared", "Object"),
1548 .name = "dso",
1549 .se = &sort_dso,
1552 static struct c2c_header header_node[3] = {
1553 HEADER_LOW("Node"),
1554 HEADER_LOW("Node{cpus %hitms %stores}"),
1555 HEADER_LOW("Node{cpu list}"),
1558 static struct c2c_dimension dim_node = {
1559 .name = "node",
1560 .cmp = empty_cmp,
1561 .entry = node_entry,
1562 .width = 4,
1565 static struct c2c_dimension dim_mean_rmt = {
1566 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1567 .name = "mean_rmt",
1568 .cmp = empty_cmp,
1569 .entry = mean_rmt_entry,
1570 .width = 8,
1573 static struct c2c_dimension dim_mean_lcl = {
1574 .header = HEADER_SPAN_LOW("lcl hitm"),
1575 .name = "mean_lcl",
1576 .cmp = empty_cmp,
1577 .entry = mean_lcl_entry,
1578 .width = 8,
1581 static struct c2c_dimension dim_mean_load = {
1582 .header = HEADER_SPAN_LOW("load"),
1583 .name = "mean_load",
1584 .cmp = empty_cmp,
1585 .entry = mean_load_entry,
1586 .width = 8,
1589 static struct c2c_dimension dim_cpucnt = {
1590 .header = HEADER_BOTH("cpu", "cnt"),
1591 .name = "cpucnt",
1592 .cmp = empty_cmp,
1593 .entry = cpucnt_entry,
1594 .width = 8,
1597 static struct c2c_dimension dim_srcline = {
1598 .name = "cl_srcline",
1599 .se = &sort_srcline,
1602 static struct c2c_dimension dim_dcacheline_idx = {
1603 .header = HEADER_LOW("Index"),
1604 .name = "cl_idx",
1605 .cmp = empty_cmp,
1606 .entry = cl_idx_entry,
1607 .width = 5,
1610 static struct c2c_dimension dim_dcacheline_num = {
1611 .header = HEADER_LOW("Num"),
1612 .name = "cl_num",
1613 .cmp = empty_cmp,
1614 .entry = cl_idx_entry,
1615 .width = 5,
1618 static struct c2c_dimension dim_dcacheline_num_empty = {
1619 .header = HEADER_LOW("Num"),
1620 .name = "cl_num_empty",
1621 .cmp = empty_cmp,
1622 .entry = cl_idx_empty_entry,
1623 .width = 5,
1626 static struct c2c_dimension *dimensions[] = {
1627 &dim_dcacheline,
1628 &dim_dcacheline_node,
1629 &dim_dcacheline_count,
1630 &dim_offset,
1631 &dim_offset_node,
1632 &dim_iaddr,
1633 &dim_tot_hitm,
1634 &dim_lcl_hitm,
1635 &dim_rmt_hitm,
1636 &dim_cl_lcl_hitm,
1637 &dim_cl_rmt_hitm,
1638 &dim_stores,
1639 &dim_stores_l1hit,
1640 &dim_stores_l1miss,
1641 &dim_cl_stores_l1hit,
1642 &dim_cl_stores_l1miss,
1643 &dim_ld_fbhit,
1644 &dim_ld_l1hit,
1645 &dim_ld_l2hit,
1646 &dim_ld_llchit,
1647 &dim_ld_rmthit,
1648 &dim_ld_llcmiss,
1649 &dim_tot_recs,
1650 &dim_tot_loads,
1651 &dim_percent_hitm,
1652 &dim_percent_rmt_hitm,
1653 &dim_percent_lcl_hitm,
1654 &dim_percent_stores_l1hit,
1655 &dim_percent_stores_l1miss,
1656 &dim_dram_lcl,
1657 &dim_dram_rmt,
1658 &dim_pid,
1659 &dim_tid,
1660 &dim_symbol,
1661 &dim_dso,
1662 &dim_node,
1663 &dim_mean_rmt,
1664 &dim_mean_lcl,
1665 &dim_mean_load,
1666 &dim_cpucnt,
1667 &dim_srcline,
1668 &dim_dcacheline_idx,
1669 &dim_dcacheline_num,
1670 &dim_dcacheline_num_empty,
1671 NULL,
1674 static void fmt_free(struct perf_hpp_fmt *fmt)
1676 struct c2c_fmt *c2c_fmt;
1678 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1679 free(c2c_fmt);
1682 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1684 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1685 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1687 return c2c_a->dim == c2c_b->dim;
1690 static struct c2c_dimension *get_dimension(const char *name)
1692 unsigned int i;
1694 for (i = 0; dimensions[i]; i++) {
1695 struct c2c_dimension *dim = dimensions[i];
1697 if (!strcmp(dim->name, name))
1698 return dim;
1701 return NULL;
1704 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1705 struct hist_entry *he)
1707 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1708 struct c2c_dimension *dim = c2c_fmt->dim;
1709 size_t len = fmt->user_len;
1711 if (!len) {
1712 len = hists__col_len(he->hists, dim->se->se_width_idx);
1714 if (dim == &dim_symbol || dim == &dim_srcline)
1715 len = symbol_width(he->hists, dim->se);
1718 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1721 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1722 struct hist_entry *a, struct hist_entry *b)
1724 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1725 struct c2c_dimension *dim = c2c_fmt->dim;
1727 return dim->se->se_cmp(a, b);
1730 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1731 struct hist_entry *a, struct hist_entry *b)
1733 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1734 struct c2c_dimension *dim = c2c_fmt->dim;
1735 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1737 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1738 return collapse_fn(a, b);
1741 static struct c2c_fmt *get_format(const char *name)
1743 struct c2c_dimension *dim = get_dimension(name);
1744 struct c2c_fmt *c2c_fmt;
1745 struct perf_hpp_fmt *fmt;
1747 if (!dim)
1748 return NULL;
1750 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1751 if (!c2c_fmt)
1752 return NULL;
1754 c2c_fmt->dim = dim;
1756 fmt = &c2c_fmt->fmt;
1757 INIT_LIST_HEAD(&fmt->list);
1758 INIT_LIST_HEAD(&fmt->sort_list);
1760 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1761 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
1762 fmt->color = dim->se ? NULL : dim->color;
1763 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
1764 fmt->header = c2c_header;
1765 fmt->width = c2c_width;
1766 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
1767 fmt->equal = fmt_equal;
1768 fmt->free = fmt_free;
1770 return c2c_fmt;
1773 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1775 struct c2c_fmt *c2c_fmt = get_format(name);
1777 if (!c2c_fmt) {
1778 reset_dimensions();
1779 return output_field_add(hpp_list, name);
1782 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1783 return 0;
1786 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1788 struct c2c_fmt *c2c_fmt = get_format(name);
1789 struct c2c_dimension *dim;
1791 if (!c2c_fmt) {
1792 reset_dimensions();
1793 return sort_dimension__add(hpp_list, name, NULL, 0);
1796 dim = c2c_fmt->dim;
1797 if (dim == &dim_dso)
1798 hpp_list->dso = 1;
1800 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1801 return 0;
1804 #define PARSE_LIST(_list, _fn) \
1805 do { \
1806 char *tmp, *tok; \
1807 ret = 0; \
1809 if (!_list) \
1810 break; \
1812 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1813 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1814 ret = _fn(hpp_list, tok); \
1815 if (ret == -EINVAL) { \
1816 pr_err("Invalid --fields key: `%s'", tok); \
1817 break; \
1818 } else if (ret == -ESRCH) { \
1819 pr_err("Unknown --fields key: `%s'", tok); \
1820 break; \
1823 } while (0)
1825 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1826 const char *output_,
1827 const char *sort_)
1829 char *output = output_ ? strdup(output_) : NULL;
1830 char *sort = sort_ ? strdup(sort_) : NULL;
1831 int ret;
1833 PARSE_LIST(output, c2c_hists__init_output);
1834 PARSE_LIST(sort, c2c_hists__init_sort);
1836 /* copy sort keys to output fields */
1837 perf_hpp__setup_output_field(hpp_list);
1840 * We dont need other sorting keys other than those
1841 * we already specified. It also really slows down
1842 * the processing a lot with big number of output
1843 * fields, so switching this off for c2c.
1846 #if 0
1847 /* and then copy output fields to sort keys */
1848 perf_hpp__append_sort_keys(&hists->list);
1849 #endif
1851 free(output);
1852 free(sort);
1853 return ret;
1856 static int c2c_hists__init(struct c2c_hists *hists,
1857 const char *sort,
1858 int nr_header_lines)
1860 __hists__init(&hists->hists, &hists->list);
1863 * Initialize only with sort fields, we need to resort
1864 * later anyway, and that's where we add output fields
1865 * as well.
1867 perf_hpp_list__init(&hists->list);
1869 /* Overload number of header lines.*/
1870 hists->list.nr_header_lines = nr_header_lines;
1872 return hpp_list__parse(&hists->list, NULL, sort);
1875 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1876 const char *output,
1877 const char *sort)
1879 perf_hpp__reset_output_field(&c2c_hists->list);
1880 return hpp_list__parse(&c2c_hists->list, output, sort);
1883 #define DISPLAY_LINE_LIMIT 0.0005
1885 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1887 struct c2c_hist_entry *c2c_he;
1888 double ld_dist;
1890 if (c2c.show_all)
1891 return true;
1893 c2c_he = container_of(he, struct c2c_hist_entry, he);
1895 #define FILTER_HITM(__h) \
1896 if (stats->__h) { \
1897 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1898 if (ld_dist < DISPLAY_LINE_LIMIT) \
1899 he->filtered = HIST_FILTER__C2C; \
1900 } else { \
1901 he->filtered = HIST_FILTER__C2C; \
1904 switch (c2c.display) {
1905 case DISPLAY_LCL:
1906 FILTER_HITM(lcl_hitm);
1907 break;
1908 case DISPLAY_RMT:
1909 FILTER_HITM(rmt_hitm);
1910 break;
1911 case DISPLAY_TOT:
1912 FILTER_HITM(tot_hitm);
1913 default:
1914 break;
1917 #undef FILTER_HITM
1919 return he->filtered == 0;
1922 static inline int valid_hitm_or_store(struct hist_entry *he)
1924 struct c2c_hist_entry *c2c_he;
1925 bool has_hitm;
1927 c2c_he = container_of(he, struct c2c_hist_entry, he);
1928 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1929 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1930 c2c_he->stats.rmt_hitm;
1931 return has_hitm || c2c_he->stats.store;
1934 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1936 struct c2c_dimension *dim;
1938 dim = &c2c.hists == c2c_he->hists ?
1939 &dim_dcacheline_node : &dim_offset_node;
1941 if (len > dim->width)
1942 dim->width = len;
1945 static int set_nodestr(struct c2c_hist_entry *c2c_he)
1947 char buf[30];
1948 int len;
1950 if (c2c_he->nodestr)
1951 return 0;
1953 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1954 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1955 buf, sizeof(buf));
1956 } else {
1957 len = scnprintf(buf, sizeof(buf), "N/A");
1960 set_node_width(c2c_he, len);
1961 c2c_he->nodestr = strdup(buf);
1962 return c2c_he->nodestr ? 0 : -ENOMEM;
1965 static void calc_width(struct c2c_hist_entry *c2c_he)
1967 struct c2c_hists *c2c_hists;
1969 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1970 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
1971 set_nodestr(c2c_he);
1974 static int filter_cb(struct hist_entry *he)
1976 struct c2c_hist_entry *c2c_he;
1978 c2c_he = container_of(he, struct c2c_hist_entry, he);
1980 if (c2c.show_src && !he->srcline)
1981 he->srcline = hist_entry__srcline(he);
1983 calc_width(c2c_he);
1985 if (!valid_hitm_or_store(he))
1986 he->filtered = HIST_FILTER__C2C;
1988 return 0;
1991 static int resort_cl_cb(struct hist_entry *he)
1993 struct c2c_hist_entry *c2c_he;
1994 struct c2c_hists *c2c_hists;
1995 bool display = he__display(he, &c2c.hitm_stats);
1997 c2c_he = container_of(he, struct c2c_hist_entry, he);
1998 c2c_hists = c2c_he->hists;
2000 if (display && c2c_hists) {
2001 static unsigned int idx;
2003 c2c_he->cacheline_idx = idx++;
2004 calc_width(c2c_he);
2006 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2008 hists__collapse_resort(&c2c_hists->hists, NULL);
2009 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2012 return 0;
2015 static void setup_nodes_header(void)
2017 dim_node.header = header_node[c2c.node_info];
2020 static int setup_nodes(struct perf_session *session)
2022 struct numa_node *n;
2023 unsigned long **nodes;
2024 int node, cpu;
2025 int *cpu2node;
2027 if (c2c.node_info > 2)
2028 c2c.node_info = 2;
2030 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2031 c2c.cpus_cnt = session->header.env.nr_cpus_online;
2033 n = session->header.env.numa_nodes;
2034 if (!n)
2035 return -EINVAL;
2037 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2038 if (!nodes)
2039 return -ENOMEM;
2041 c2c.nodes = nodes;
2043 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2044 if (!cpu2node)
2045 return -ENOMEM;
2047 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2048 cpu2node[cpu] = -1;
2050 c2c.cpu2node = cpu2node;
2052 for (node = 0; node < c2c.nodes_cnt; node++) {
2053 struct cpu_map *map = n[node].map;
2054 unsigned long *set;
2056 set = bitmap_alloc(c2c.cpus_cnt);
2057 if (!set)
2058 return -ENOMEM;
2060 nodes[node] = set;
2062 /* empty node, skip */
2063 if (cpu_map__empty(map))
2064 continue;
2066 for (cpu = 0; cpu < map->nr; cpu++) {
2067 set_bit(map->map[cpu], set);
2069 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2070 return -EINVAL;
2072 cpu2node[map->map[cpu]] = node;
2076 setup_nodes_header();
2077 return 0;
2080 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2082 static int resort_hitm_cb(struct hist_entry *he)
2084 struct c2c_hist_entry *c2c_he;
2085 c2c_he = container_of(he, struct c2c_hist_entry, he);
2087 if (HAS_HITMS(c2c_he)) {
2088 c2c.shared_clines++;
2089 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2092 return 0;
2095 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2097 struct rb_node *next = rb_first(&hists->entries);
2098 int ret = 0;
2100 while (next) {
2101 struct hist_entry *he;
2103 he = rb_entry(next, struct hist_entry, rb_node);
2104 ret = cb(he);
2105 if (ret)
2106 break;
2107 next = rb_next(&he->rb_node);
2110 return ret;
2113 static void print_c2c__display_stats(FILE *out)
2115 int llc_misses;
2116 struct c2c_stats *stats = &c2c.hists.stats;
2118 llc_misses = stats->lcl_dram +
2119 stats->rmt_dram +
2120 stats->rmt_hit +
2121 stats->rmt_hitm;
2123 fprintf(out, "=================================================\n");
2124 fprintf(out, " Trace Event Information \n");
2125 fprintf(out, "=================================================\n");
2126 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2127 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2128 fprintf(out, " Load Operations : %10d\n", stats->load);
2129 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2130 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2131 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2132 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2133 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2134 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2135 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2136 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2137 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2138 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2139 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2140 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2141 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2142 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2143 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2144 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2145 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2146 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2147 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2148 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2149 fprintf(out, " Store Operations : %10d\n", stats->store);
2150 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2151 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2152 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2153 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2154 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2155 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2158 static void print_shared_cacheline_info(FILE *out)
2160 struct c2c_stats *stats = &c2c.hitm_stats;
2161 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2163 fprintf(out, "=================================================\n");
2164 fprintf(out, " Global Shared Cache Line Event Information \n");
2165 fprintf(out, "=================================================\n");
2166 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2167 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2168 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2169 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2170 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2171 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2172 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2173 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2174 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2175 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2178 static void print_cacheline(struct c2c_hists *c2c_hists,
2179 struct hist_entry *he_cl,
2180 struct perf_hpp_list *hpp_list,
2181 FILE *out)
2183 char bf[1000];
2184 struct perf_hpp hpp = {
2185 .buf = bf,
2186 .size = 1000,
2188 static bool once;
2190 if (!once) {
2191 hists__fprintf_headers(&c2c_hists->hists, out);
2192 once = true;
2193 } else {
2194 fprintf(out, "\n");
2197 fprintf(out, " -------------------------------------------------------------\n");
2198 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2199 fprintf(out, "%s\n", bf);
2200 fprintf(out, " -------------------------------------------------------------\n");
2202 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2205 static void print_pareto(FILE *out)
2207 struct perf_hpp_list hpp_list;
2208 struct rb_node *nd;
2209 int ret;
2211 perf_hpp_list__init(&hpp_list);
2212 ret = hpp_list__parse(&hpp_list,
2213 "cl_num,"
2214 "cl_rmt_hitm,"
2215 "cl_lcl_hitm,"
2216 "cl_stores_l1hit,"
2217 "cl_stores_l1miss,"
2218 "dcacheline",
2219 NULL);
2221 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2222 return;
2224 nd = rb_first(&c2c.hists.hists.entries);
2226 for (; nd; nd = rb_next(nd)) {
2227 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2228 struct c2c_hist_entry *c2c_he;
2230 if (he->filtered)
2231 continue;
2233 c2c_he = container_of(he, struct c2c_hist_entry, he);
2234 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2238 static void print_c2c_info(FILE *out, struct perf_session *session)
2240 struct perf_evlist *evlist = session->evlist;
2241 struct perf_evsel *evsel;
2242 bool first = true;
2244 fprintf(out, "=================================================\n");
2245 fprintf(out, " c2c details \n");
2246 fprintf(out, "=================================================\n");
2248 evlist__for_each_entry(evlist, evsel) {
2249 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2250 perf_evsel__name(evsel));
2251 first = false;
2253 fprintf(out, " Cachelines sort on : %s HITMs\n",
2254 display_str[c2c.display]);
2255 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
2258 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2260 setup_pager();
2262 print_c2c__display_stats(out);
2263 fprintf(out, "\n");
2264 print_shared_cacheline_info(out);
2265 fprintf(out, "\n");
2266 print_c2c_info(out, session);
2268 if (c2c.stats_only)
2269 return;
2271 fprintf(out, "\n");
2272 fprintf(out, "=================================================\n");
2273 fprintf(out, " Shared Data Cache Line Table \n");
2274 fprintf(out, "=================================================\n");
2275 fprintf(out, "#\n");
2277 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2279 fprintf(out, "\n");
2280 fprintf(out, "=================================================\n");
2281 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2282 fprintf(out, "=================================================\n");
2283 fprintf(out, "#\n");
2285 print_pareto(out);
2288 #ifdef HAVE_SLANG_SUPPORT
2289 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2291 u64 nr_entries = 0;
2292 struct rb_node *nd = rb_first(&hb->hists->entries);
2294 while (nd) {
2295 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2297 if (!he->filtered)
2298 nr_entries++;
2300 nd = rb_next(nd);
2303 hb->nr_non_filtered_entries = nr_entries;
2306 struct c2c_cacheline_browser {
2307 struct hist_browser hb;
2308 struct hist_entry *he;
2311 static int
2312 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2313 char *bf, size_t size)
2315 struct c2c_cacheline_browser *cl_browser;
2316 struct hist_entry *he;
2317 uint64_t addr = 0;
2319 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2320 he = cl_browser->he;
2322 if (he->mem_info)
2323 addr = cl_address(he->mem_info->daddr.addr);
2325 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2326 return 0;
2329 static struct c2c_cacheline_browser*
2330 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2332 struct c2c_cacheline_browser *browser;
2334 browser = zalloc(sizeof(*browser));
2335 if (browser) {
2336 hist_browser__init(&browser->hb, hists);
2337 browser->hb.c2c_filter = true;
2338 browser->hb.title = perf_c2c_cacheline_browser__title;
2339 browser->he = he;
2342 return browser;
2345 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2347 struct c2c_hist_entry *c2c_he;
2348 struct c2c_hists *c2c_hists;
2349 struct c2c_cacheline_browser *cl_browser;
2350 struct hist_browser *browser;
2351 int key = -1;
2352 const char help[] =
2353 " ENTER Toggle callchains (if present) \n"
2354 " n Toggle Node details info \n"
2355 " s Toggle full length of symbol and source line columns \n"
2356 " q Return back to cacheline list \n";
2358 if (!he)
2359 return 0;
2361 /* Display compact version first. */
2362 c2c.symbol_full = false;
2364 c2c_he = container_of(he, struct c2c_hist_entry, he);
2365 c2c_hists = c2c_he->hists;
2367 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2368 if (cl_browser == NULL)
2369 return -1;
2371 browser = &cl_browser->hb;
2373 /* reset abort key so that it can get Ctrl-C as a key */
2374 SLang_reset_tty();
2375 SLang_init_tty(0, 0, 0);
2377 c2c_browser__update_nr_entries(browser);
2379 while (1) {
2380 key = hist_browser__run(browser, "? - help", true);
2382 switch (key) {
2383 case 's':
2384 c2c.symbol_full = !c2c.symbol_full;
2385 break;
2386 case 'n':
2387 c2c.node_info = (c2c.node_info + 1) % 3;
2388 setup_nodes_header();
2389 break;
2390 case 'q':
2391 goto out;
2392 case '?':
2393 ui_browser__help_window(&browser->b, help);
2394 break;
2395 default:
2396 break;
2400 out:
2401 free(cl_browser);
2402 return 0;
2405 static int perf_c2c_browser__title(struct hist_browser *browser,
2406 char *bf, size_t size)
2408 scnprintf(bf, size,
2409 "Shared Data Cache Line Table "
2410 "(%lu entries, sorted on %s HITMs)",
2411 browser->nr_non_filtered_entries,
2412 display_str[c2c.display]);
2413 return 0;
2416 static struct hist_browser*
2417 perf_c2c_browser__new(struct hists *hists)
2419 struct hist_browser *browser = hist_browser__new(hists);
2421 if (browser) {
2422 browser->title = perf_c2c_browser__title;
2423 browser->c2c_filter = true;
2426 return browser;
2429 static int perf_c2c__hists_browse(struct hists *hists)
2431 struct hist_browser *browser;
2432 int key = -1;
2433 const char help[] =
2434 " d Display cacheline details \n"
2435 " ENTER Toggle callchains (if present) \n"
2436 " q Quit \n";
2438 browser = perf_c2c_browser__new(hists);
2439 if (browser == NULL)
2440 return -1;
2442 /* reset abort key so that it can get Ctrl-C as a key */
2443 SLang_reset_tty();
2444 SLang_init_tty(0, 0, 0);
2446 c2c_browser__update_nr_entries(browser);
2448 while (1) {
2449 key = hist_browser__run(browser, "? - help", true);
2451 switch (key) {
2452 case 'q':
2453 goto out;
2454 case 'd':
2455 perf_c2c__browse_cacheline(browser->he_selection);
2456 break;
2457 case '?':
2458 ui_browser__help_window(&browser->b, help);
2459 break;
2460 default:
2461 break;
2465 out:
2466 hist_browser__delete(browser);
2467 return 0;
2470 static void perf_c2c_display(struct perf_session *session)
2472 if (use_browser == 0)
2473 perf_c2c__hists_fprintf(stdout, session);
2474 else
2475 perf_c2c__hists_browse(&c2c.hists.hists);
2477 #else
2478 static void perf_c2c_display(struct perf_session *session)
2480 use_browser = 0;
2481 perf_c2c__hists_fprintf(stdout, session);
2483 #endif /* HAVE_SLANG_SUPPORT */
2485 static char *fill_line(const char *orig, int len)
2487 int i, j, olen = strlen(orig);
2488 char *buf;
2490 buf = zalloc(len + 1);
2491 if (!buf)
2492 return NULL;
2494 j = len / 2 - olen / 2;
2496 for (i = 0; i < j - 1; i++)
2497 buf[i] = '-';
2499 buf[i++] = ' ';
2501 strcpy(buf + i, orig);
2503 i += olen;
2505 buf[i++] = ' ';
2507 for (; i < len; i++)
2508 buf[i] = '-';
2510 return buf;
2513 static int ui_quirks(void)
2515 const char *nodestr = "Data address";
2516 char *buf;
2518 if (!c2c.use_stdio) {
2519 dim_offset.width = 5;
2520 dim_offset.header = header_offset_tui;
2521 nodestr = "CL";
2524 dim_percent_hitm.header = percent_hitm_header[c2c.display];
2526 /* Fix the zero line for dcacheline column. */
2527 buf = fill_line("Cacheline", dim_dcacheline.width +
2528 dim_dcacheline_node.width +
2529 dim_dcacheline_count.width + 4);
2530 if (!buf)
2531 return -ENOMEM;
2533 dim_dcacheline.header.line[0].text = buf;
2535 /* Fix the zero line for offset column. */
2536 buf = fill_line(nodestr, dim_offset.width +
2537 dim_offset_node.width +
2538 dim_dcacheline_count.width + 4);
2539 if (!buf)
2540 return -ENOMEM;
2542 dim_offset.header.line[0].text = buf;
2544 return 0;
2547 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2549 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2550 CALLCHAIN_REPORT_HELP
2551 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2553 static int
2554 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2556 struct callchain_param *callchain = opt->value;
2558 callchain->enabled = !unset;
2560 * --no-call-graph
2562 if (unset) {
2563 symbol_conf.use_callchain = false;
2564 callchain->mode = CHAIN_NONE;
2565 return 0;
2568 return parse_callchain_report_opt(arg);
2571 static int setup_callchain(struct perf_evlist *evlist)
2573 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2574 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2576 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2577 (sample_type & PERF_SAMPLE_STACK_USER)) {
2578 mode = CALLCHAIN_DWARF;
2579 dwarf_callchain_users = true;
2580 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2581 mode = CALLCHAIN_LBR;
2582 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2583 mode = CALLCHAIN_FP;
2585 if (!callchain_param.enabled &&
2586 callchain_param.mode != CHAIN_NONE &&
2587 mode != CALLCHAIN_NONE) {
2588 symbol_conf.use_callchain = true;
2589 if (callchain_register_param(&callchain_param) < 0) {
2590 ui__error("Can't register callchain params.\n");
2591 return -EINVAL;
2595 callchain_param.record_mode = mode;
2596 callchain_param.min_percent = 0;
2597 return 0;
2600 static int setup_display(const char *str)
2602 const char *display = str ?: "tot";
2604 if (!strcmp(display, "tot"))
2605 c2c.display = DISPLAY_TOT;
2606 else if (!strcmp(display, "rmt"))
2607 c2c.display = DISPLAY_RMT;
2608 else if (!strcmp(display, "lcl"))
2609 c2c.display = DISPLAY_LCL;
2610 else {
2611 pr_err("failed: unknown display type: %s\n", str);
2612 return -1;
2615 return 0;
2618 #define for_each_token(__tok, __buf, __sep, __tmp) \
2619 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2620 __tok = strtok_r(NULL, __sep, &__tmp))
2622 static int build_cl_output(char *cl_sort, bool no_source)
2624 char *tok, *tmp, *buf = strdup(cl_sort);
2625 bool add_pid = false;
2626 bool add_tid = false;
2627 bool add_iaddr = false;
2628 bool add_sym = false;
2629 bool add_dso = false;
2630 bool add_src = false;
2631 int ret = 0;
2633 if (!buf)
2634 return -ENOMEM;
2636 for_each_token(tok, buf, ",", tmp) {
2637 if (!strcmp(tok, "tid")) {
2638 add_tid = true;
2639 } else if (!strcmp(tok, "pid")) {
2640 add_pid = true;
2641 } else if (!strcmp(tok, "iaddr")) {
2642 add_iaddr = true;
2643 add_sym = true;
2644 add_dso = true;
2645 add_src = no_source ? false : true;
2646 } else if (!strcmp(tok, "dso")) {
2647 add_dso = true;
2648 } else if (strcmp(tok, "offset")) {
2649 pr_err("unrecognized sort token: %s\n", tok);
2650 ret = -EINVAL;
2651 goto err;
2655 if (asprintf(&c2c.cl_output,
2656 "%s%s%s%s%s%s%s%s%s%s",
2657 c2c.use_stdio ? "cl_num_empty," : "",
2658 "percent_rmt_hitm,"
2659 "percent_lcl_hitm,"
2660 "percent_stores_l1hit,"
2661 "percent_stores_l1miss,"
2662 "offset,offset_node,dcacheline_count,",
2663 add_pid ? "pid," : "",
2664 add_tid ? "tid," : "",
2665 add_iaddr ? "iaddr," : "",
2666 "mean_rmt,"
2667 "mean_lcl,"
2668 "mean_load,"
2669 "tot_recs,"
2670 "cpucnt,",
2671 add_sym ? "symbol," : "",
2672 add_dso ? "dso," : "",
2673 add_src ? "cl_srcline," : "",
2674 "node") < 0) {
2675 ret = -ENOMEM;
2676 goto err;
2679 c2c.show_src = add_src;
2680 err:
2681 free(buf);
2682 return ret;
2685 static int setup_coalesce(const char *coalesce, bool no_source)
2687 const char *c = coalesce ?: coalesce_default;
2689 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2690 return -ENOMEM;
2692 if (build_cl_output(c2c.cl_sort, no_source))
2693 return -1;
2695 if (asprintf(&c2c.cl_resort, "offset,%s",
2696 c2c.display == DISPLAY_TOT ?
2697 "tot_hitm" :
2698 c2c.display == DISPLAY_RMT ?
2699 "rmt_hitm,lcl_hitm" :
2700 "lcl_hitm,rmt_hitm") < 0)
2701 return -ENOMEM;
2703 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2704 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2705 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2706 return 0;
2709 static int perf_c2c__report(int argc, const char **argv)
2711 struct perf_session *session;
2712 struct ui_progress prog;
2713 struct perf_data data = {
2714 .mode = PERF_DATA_MODE_READ,
2716 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2717 const char *display = NULL;
2718 const char *coalesce = NULL;
2719 bool no_source = false;
2720 const struct option options[] = {
2721 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2722 "file", "vmlinux pathname"),
2723 OPT_STRING('i', "input", &input_name, "file",
2724 "the input file to process"),
2725 OPT_INCR('N', "node-info", &c2c.node_info,
2726 "show extra node info in report (repeat for more info)"),
2727 #ifdef HAVE_SLANG_SUPPORT
2728 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2729 #endif
2730 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2731 "Display only statistic tables (implies --stdio)"),
2732 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2733 "Display full length of symbols"),
2734 OPT_BOOLEAN(0, "no-source", &no_source,
2735 "Do not display Source Line column"),
2736 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2737 "Show all captured HITM lines."),
2738 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2739 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2740 callchain_help, &parse_callchain_opt,
2741 callchain_default_opt),
2742 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2743 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2744 "coalesce fields: pid,tid,iaddr,dso"),
2745 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2746 OPT_PARENT(c2c_options),
2747 OPT_END()
2749 int err = 0;
2751 argc = parse_options(argc, argv, options, report_c2c_usage,
2752 PARSE_OPT_STOP_AT_NON_OPTION);
2753 if (argc)
2754 usage_with_options(report_c2c_usage, options);
2756 if (c2c.stats_only)
2757 c2c.use_stdio = true;
2759 if (!input_name || !strlen(input_name))
2760 input_name = "perf.data";
2762 data.file.path = input_name;
2763 data.force = symbol_conf.force;
2765 err = setup_display(display);
2766 if (err)
2767 goto out;
2769 err = setup_coalesce(coalesce, no_source);
2770 if (err) {
2771 pr_debug("Failed to initialize hists\n");
2772 goto out;
2775 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2776 if (err) {
2777 pr_debug("Failed to initialize hists\n");
2778 goto out;
2781 session = perf_session__new(&data, 0, &c2c.tool);
2782 if (session == NULL) {
2783 pr_debug("No memory for session\n");
2784 goto out;
2787 err = setup_nodes(session);
2788 if (err) {
2789 pr_err("Failed setup nodes\n");
2790 goto out;
2793 err = mem2node__init(&c2c.mem2node, &session->header.env);
2794 if (err)
2795 goto out_session;
2797 err = setup_callchain(session->evlist);
2798 if (err)
2799 goto out_mem2node;
2801 if (symbol__init(&session->header.env) < 0)
2802 goto out_mem2node;
2804 /* No pipe support at the moment. */
2805 if (perf_data__is_pipe(session->data)) {
2806 pr_debug("No pipe support at the moment.\n");
2807 goto out_mem2node;
2810 if (c2c.use_stdio)
2811 use_browser = 0;
2812 else
2813 use_browser = 1;
2815 setup_browser(false);
2817 err = perf_session__process_events(session);
2818 if (err) {
2819 pr_err("failed to process sample\n");
2820 goto out_mem2node;
2823 c2c_hists__reinit(&c2c.hists,
2824 "cl_idx,"
2825 "dcacheline,"
2826 "dcacheline_node,"
2827 "dcacheline_count,"
2828 "tot_recs,"
2829 "percent_hitm,"
2830 "tot_hitm,lcl_hitm,rmt_hitm,"
2831 "stores,stores_l1hit,stores_l1miss,"
2832 "dram_lcl,dram_rmt,"
2833 "ld_llcmiss,"
2834 "tot_loads,"
2835 "ld_fbhit,ld_l1hit,ld_l2hit,"
2836 "ld_lclhit,ld_rmthit",
2837 c2c.display == DISPLAY_TOT ? "tot_hitm" :
2838 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
2841 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2843 hists__collapse_resort(&c2c.hists.hists, NULL);
2844 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2845 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2847 ui_progress__finish();
2849 if (ui_quirks()) {
2850 pr_err("failed to setup UI\n");
2851 goto out_mem2node;
2854 perf_c2c_display(session);
2856 out_mem2node:
2857 mem2node__exit(&c2c.mem2node);
2858 out_session:
2859 perf_session__delete(session);
2860 out:
2861 return err;
2864 static int parse_record_events(const struct option *opt,
2865 const char *str, int unset __maybe_unused)
2867 bool *event_set = (bool *) opt->value;
2869 *event_set = true;
2870 return perf_mem_events__parse(str);
2874 static const char * const __usage_record[] = {
2875 "perf c2c record [<options>] [<command>]",
2876 "perf c2c record [<options>] -- <command> [<options>]",
2877 NULL
2880 static const char * const *record_mem_usage = __usage_record;
2882 static int perf_c2c__record(int argc, const char **argv)
2884 int rec_argc, i = 0, j;
2885 const char **rec_argv;
2886 int ret;
2887 bool all_user = false, all_kernel = false;
2888 bool event_set = false;
2889 struct option options[] = {
2890 OPT_CALLBACK('e', "event", &event_set, "event",
2891 "event selector. Use 'perf mem record -e list' to list available events",
2892 parse_record_events),
2893 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2894 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2895 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2896 OPT_PARENT(c2c_options),
2897 OPT_END()
2900 if (perf_mem_events__init()) {
2901 pr_err("failed: memory events not supported\n");
2902 return -1;
2905 argc = parse_options(argc, argv, options, record_mem_usage,
2906 PARSE_OPT_KEEP_UNKNOWN);
2908 rec_argc = argc + 11; /* max number of arguments */
2909 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2910 if (!rec_argv)
2911 return -1;
2913 rec_argv[i++] = "record";
2915 if (!event_set) {
2916 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2917 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2920 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2921 rec_argv[i++] = "-W";
2923 rec_argv[i++] = "-d";
2924 rec_argv[i++] = "--phys-data";
2925 rec_argv[i++] = "--sample-cpu";
2927 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2928 if (!perf_mem_events[j].record)
2929 continue;
2931 if (!perf_mem_events[j].supported) {
2932 pr_err("failed: event '%s' not supported\n",
2933 perf_mem_events[j].name);
2934 free(rec_argv);
2935 return -1;
2938 rec_argv[i++] = "-e";
2939 rec_argv[i++] = perf_mem_events__name(j);
2942 if (all_user)
2943 rec_argv[i++] = "--all-user";
2945 if (all_kernel)
2946 rec_argv[i++] = "--all-kernel";
2948 for (j = 0; j < argc; j++, i++)
2949 rec_argv[i] = argv[j];
2951 if (verbose > 0) {
2952 pr_debug("calling: ");
2954 j = 0;
2956 while (rec_argv[j]) {
2957 pr_debug("%s ", rec_argv[j]);
2958 j++;
2960 pr_debug("\n");
2963 ret = cmd_record(i, rec_argv);
2964 free(rec_argv);
2965 return ret;
2968 int cmd_c2c(int argc, const char **argv)
2970 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2971 PARSE_OPT_STOP_AT_NON_OPTION);
2973 if (!argc)
2974 usage_with_options(c2c_usage, c2c_options);
2976 if (!strncmp(argv[0], "rec", 3)) {
2977 return perf_c2c__record(argc, argv);
2978 } else if (!strncmp(argv[0], "rep", 3)) {
2979 return perf_c2c__report(argc, argv);
2980 } else {
2981 usage_with_options(c2c_usage, c2c_options);
2984 return 0;