]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/perf/builtin-c2c.c
61aaacc2aedd8f5c9054f5b15228c642ee379b1b
[linux.git] / tools / perf / builtin-c2c.c
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/
5  *
6  * The original tool was changed to fit in current perf state.
7  *
8  * Original authors:
9  *   Don Zickus <dzickus@redhat.com>
10  *   Dick Fowles <fowles@inreach.com>
11  *   Joe Mario <jmario@redhat.com>
12  */
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 <linux/zalloc.h>
19 #include <asm/bug.h>
20 #include <sys/param.h>
21 #include "debug.h"
22 #include "builtin.h"
23 #include <perf/cpumap.h>
24 #include <subcmd/pager.h>
25 #include <subcmd/parse-options.h>
26 #include "map_symbol.h"
27 #include "mem-events.h"
28 #include "session.h"
29 #include "hist.h"
30 #include "sort.h"
31 #include "tool.h"
32 #include "cacheline.h"
33 #include "data.h"
34 #include "event.h"
35 #include "evlist.h"
36 #include "evsel.h"
37 #include "ui/browsers/hists.h"
38 #include "thread.h"
39 #include "mem2node.h"
40 #include "symbol.h"
41 #include "ui/ui.h"
42 #include "ui/progress.h"
43 #include "../perf.h"
44
45 struct c2c_hists {
46         struct hists            hists;
47         struct perf_hpp_list    list;
48         struct c2c_stats        stats;
49 };
50
51 struct compute_stats {
52         struct stats             lcl_hitm;
53         struct stats             rmt_hitm;
54         struct stats             load;
55 };
56
57 struct c2c_hist_entry {
58         struct c2c_hists        *hists;
59         struct c2c_stats         stats;
60         unsigned long           *cpuset;
61         unsigned long           *nodeset;
62         struct c2c_stats        *node_stats;
63         unsigned int             cacheline_idx;
64
65         struct compute_stats     cstats;
66
67         unsigned long            paddr;
68         unsigned long            paddr_cnt;
69         bool                     paddr_zero;
70         char                    *nodestr;
71
72         /*
73          * must be at the end,
74          * because of its callchain dynamic entry
75          */
76         struct hist_entry       he;
77 };
78
79 static char const *coalesce_default = "iaddr";
80
81 struct perf_c2c {
82         struct perf_tool        tool;
83         struct c2c_hists        hists;
84         struct mem2node         mem2node;
85
86         unsigned long           **nodes;
87         int                      nodes_cnt;
88         int                      cpus_cnt;
89         int                     *cpu2node;
90         int                      node_info;
91
92         bool                     show_src;
93         bool                     show_all;
94         bool                     use_stdio;
95         bool                     stats_only;
96         bool                     symbol_full;
97
98         /* HITM shared clines stats */
99         struct c2c_stats        hitm_stats;
100         int                     shared_clines;
101
102         int                      display;
103
104         const char              *coalesce;
105         char                    *cl_sort;
106         char                    *cl_resort;
107         char                    *cl_output;
108 };
109
110 enum {
111         DISPLAY_LCL,
112         DISPLAY_RMT,
113         DISPLAY_TOT,
114         DISPLAY_MAX,
115 };
116
117 static const char *display_str[DISPLAY_MAX] = {
118         [DISPLAY_LCL] = "Local",
119         [DISPLAY_RMT] = "Remote",
120         [DISPLAY_TOT] = "Total",
121 };
122
123 static const struct option c2c_options[] = {
124         OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
125         OPT_END()
126 };
127
128 static struct perf_c2c c2c;
129
130 static void *c2c_he_zalloc(size_t size)
131 {
132         struct c2c_hist_entry *c2c_he;
133
134         c2c_he = zalloc(size + sizeof(*c2c_he));
135         if (!c2c_he)
136                 return NULL;
137
138         c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
139         if (!c2c_he->cpuset)
140                 return NULL;
141
142         c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
143         if (!c2c_he->nodeset)
144                 return NULL;
145
146         c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
147         if (!c2c_he->node_stats)
148                 return NULL;
149
150         init_stats(&c2c_he->cstats.lcl_hitm);
151         init_stats(&c2c_he->cstats.rmt_hitm);
152         init_stats(&c2c_he->cstats.load);
153
154         return &c2c_he->he;
155 }
156
157 static void c2c_he_free(void *he)
158 {
159         struct c2c_hist_entry *c2c_he;
160
161         c2c_he = container_of(he, struct c2c_hist_entry, he);
162         if (c2c_he->hists) {
163                 hists__delete_entries(&c2c_he->hists->hists);
164                 free(c2c_he->hists);
165         }
166
167         free(c2c_he->cpuset);
168         free(c2c_he->nodeset);
169         free(c2c_he->nodestr);
170         free(c2c_he->node_stats);
171         free(c2c_he);
172 }
173
174 static struct hist_entry_ops c2c_entry_ops = {
175         .new    = c2c_he_zalloc,
176         .free   = c2c_he_free,
177 };
178
179 static int c2c_hists__init(struct c2c_hists *hists,
180                            const char *sort,
181                            int nr_header_lines);
182
183 static struct c2c_hists*
184 he__get_c2c_hists(struct hist_entry *he,
185                   const char *sort,
186                   int nr_header_lines)
187 {
188         struct c2c_hist_entry *c2c_he;
189         struct c2c_hists *hists;
190         int ret;
191
192         c2c_he = container_of(he, struct c2c_hist_entry, he);
193         if (c2c_he->hists)
194                 return c2c_he->hists;
195
196         hists = c2c_he->hists = zalloc(sizeof(*hists));
197         if (!hists)
198                 return NULL;
199
200         ret = c2c_hists__init(hists, sort, nr_header_lines);
201         if (ret) {
202                 free(hists);
203                 return NULL;
204         }
205
206         return hists;
207 }
208
209 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
210                             struct perf_sample *sample)
211 {
212         if (WARN_ONCE(sample->cpu == (unsigned int) -1,
213                       "WARNING: no sample cpu value"))
214                 return;
215
216         set_bit(sample->cpu, c2c_he->cpuset);
217 }
218
219 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
220                              struct perf_sample *sample)
221 {
222         int node;
223
224         if (!sample->phys_addr) {
225                 c2c_he->paddr_zero = true;
226                 return;
227         }
228
229         node = mem2node__node(&c2c.mem2node, sample->phys_addr);
230         if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
231                 return;
232
233         set_bit(node, c2c_he->nodeset);
234
235         if (c2c_he->paddr != sample->phys_addr) {
236                 c2c_he->paddr_cnt++;
237                 c2c_he->paddr = sample->phys_addr;
238         }
239 }
240
241 static void compute_stats(struct c2c_hist_entry *c2c_he,
242                           struct c2c_stats *stats,
243                           u64 weight)
244 {
245         struct compute_stats *cstats = &c2c_he->cstats;
246
247         if (stats->rmt_hitm)
248                 update_stats(&cstats->rmt_hitm, weight);
249         else if (stats->lcl_hitm)
250                 update_stats(&cstats->lcl_hitm, weight);
251         else if (stats->load)
252                 update_stats(&cstats->load, weight);
253 }
254
255 static int process_sample_event(struct perf_tool *tool __maybe_unused,
256                                 union perf_event *event,
257                                 struct perf_sample *sample,
258                                 struct evsel *evsel,
259                                 struct machine *machine)
260 {
261         struct c2c_hists *c2c_hists = &c2c.hists;
262         struct c2c_hist_entry *c2c_he;
263         struct c2c_stats stats = { .nr_entries = 0, };
264         struct hist_entry *he;
265         struct addr_location al;
266         struct mem_info *mi, *mi_dup;
267         int ret;
268
269         if (machine__resolve(machine, &al, sample) < 0) {
270                 pr_debug("problem processing %d event, skipping it.\n",
271                          event->header.type);
272                 return -1;
273         }
274
275         ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
276                                         evsel, &al, sysctl_perf_event_max_stack);
277         if (ret)
278                 goto out;
279
280         mi = sample__resolve_mem(sample, &al);
281         if (mi == NULL)
282                 return -ENOMEM;
283
284         /*
285          * The mi object is released in hists__add_entry_ops,
286          * if it gets sorted out into existing data, so we need
287          * to take the copy now.
288          */
289         mi_dup = mem_info__get(mi);
290
291         c2c_decode_stats(&stats, mi);
292
293         he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
294                                   &al, NULL, NULL, mi,
295                                   sample, true);
296         if (he == NULL)
297                 goto free_mi;
298
299         c2c_he = container_of(he, struct c2c_hist_entry, he);
300         c2c_add_stats(&c2c_he->stats, &stats);
301         c2c_add_stats(&c2c_hists->stats, &stats);
302
303         c2c_he__set_cpu(c2c_he, sample);
304         c2c_he__set_node(c2c_he, sample);
305
306         hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
307         ret = hist_entry__append_callchain(he, sample);
308
309         if (!ret) {
310                 /*
311                  * There's already been warning about missing
312                  * sample's cpu value. Let's account all to
313                  * node 0 in this case, without any further
314                  * warning.
315                  *
316                  * Doing node stats only for single callchain data.
317                  */
318                 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
319                 int node = c2c.cpu2node[cpu];
320
321                 mi = mi_dup;
322
323                 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
324                 if (!c2c_hists)
325                         goto free_mi;
326
327                 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
328                                           &al, NULL, NULL, mi,
329                                           sample, true);
330                 if (he == NULL)
331                         goto free_mi;
332
333                 c2c_he = container_of(he, struct c2c_hist_entry, he);
334                 c2c_add_stats(&c2c_he->stats, &stats);
335                 c2c_add_stats(&c2c_hists->stats, &stats);
336                 c2c_add_stats(&c2c_he->node_stats[node], &stats);
337
338                 compute_stats(c2c_he, &stats, sample->weight);
339
340                 c2c_he__set_cpu(c2c_he, sample);
341                 c2c_he__set_node(c2c_he, sample);
342
343                 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
344                 ret = hist_entry__append_callchain(he, sample);
345         }
346
347 out:
348         addr_location__put(&al);
349         return ret;
350
351 free_mi:
352         mem_info__put(mi_dup);
353         mem_info__put(mi);
354         ret = -ENOMEM;
355         goto out;
356 }
357
358 static struct perf_c2c c2c = {
359         .tool = {
360                 .sample         = process_sample_event,
361                 .mmap           = perf_event__process_mmap,
362                 .mmap2          = perf_event__process_mmap2,
363                 .comm           = perf_event__process_comm,
364                 .exit           = perf_event__process_exit,
365                 .fork           = perf_event__process_fork,
366                 .lost           = perf_event__process_lost,
367                 .ordered_events = true,
368                 .ordering_requires_timestamps = true,
369         },
370 };
371
372 static const char * const c2c_usage[] = {
373         "perf c2c {record|report}",
374         NULL
375 };
376
377 static const char * const __usage_report[] = {
378         "perf c2c report",
379         NULL
380 };
381
382 static const char * const *report_c2c_usage = __usage_report;
383
384 #define C2C_HEADER_MAX 2
385
386 struct c2c_header {
387         struct {
388                 const char *text;
389                 int         span;
390         } line[C2C_HEADER_MAX];
391 };
392
393 struct c2c_dimension {
394         struct c2c_header        header;
395         const char              *name;
396         int                      width;
397         struct sort_entry       *se;
398
399         int64_t (*cmp)(struct perf_hpp_fmt *fmt,
400                        struct hist_entry *, struct hist_entry *);
401         int   (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
402                        struct hist_entry *he);
403         int   (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
404                        struct hist_entry *he);
405 };
406
407 struct c2c_fmt {
408         struct perf_hpp_fmt      fmt;
409         struct c2c_dimension    *dim;
410 };
411
412 #define SYMBOL_WIDTH 30
413
414 static struct c2c_dimension dim_symbol;
415 static struct c2c_dimension dim_srcline;
416
417 static int symbol_width(struct hists *hists, struct sort_entry *se)
418 {
419         int width = hists__col_len(hists, se->se_width_idx);
420
421         if (!c2c.symbol_full)
422                 width = MIN(width, SYMBOL_WIDTH);
423
424         return width;
425 }
426
427 static int c2c_width(struct perf_hpp_fmt *fmt,
428                      struct perf_hpp *hpp __maybe_unused,
429                      struct hists *hists)
430 {
431         struct c2c_fmt *c2c_fmt;
432         struct c2c_dimension *dim;
433
434         c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
435         dim = c2c_fmt->dim;
436
437         if (dim == &dim_symbol || dim == &dim_srcline)
438                 return symbol_width(hists, dim->se);
439
440         return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
441                          c2c_fmt->dim->width;
442 }
443
444 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
445                       struct hists *hists, int line, int *span)
446 {
447         struct perf_hpp_list *hpp_list = hists->hpp_list;
448         struct c2c_fmt *c2c_fmt;
449         struct c2c_dimension *dim;
450         const char *text = NULL;
451         int width = c2c_width(fmt, hpp, hists);
452
453         c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
454         dim = c2c_fmt->dim;
455
456         if (dim->se) {
457                 text = dim->header.line[line].text;
458                 /* Use the last line from sort_entry if not defined. */
459                 if (!text && (line == hpp_list->nr_header_lines - 1))
460                         text = dim->se->se_header;
461         } else {
462                 text = dim->header.line[line].text;
463
464                 if (*span) {
465                         (*span)--;
466                         return 0;
467                 } else {
468                         *span = dim->header.line[line].span;
469                 }
470         }
471
472         if (text == NULL)
473                 text = "";
474
475         return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
476 }
477
478 #define HEX_STR(__s, __v)                               \
479 ({                                                      \
480         scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
481         __s;                                            \
482 })
483
484 static int64_t
485 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
486                struct hist_entry *left, struct hist_entry *right)
487 {
488         return sort__dcacheline_cmp(left, right);
489 }
490
491 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
492                             struct hist_entry *he)
493 {
494         uint64_t addr = 0;
495         int width = c2c_width(fmt, hpp, he->hists);
496         char buf[20];
497
498         if (he->mem_info)
499                 addr = cl_address(he->mem_info->daddr.addr);
500
501         return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
502 }
503
504 static int
505 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
506                       struct hist_entry *he)
507 {
508         struct c2c_hist_entry *c2c_he;
509         int width = c2c_width(fmt, hpp, he->hists);
510
511         c2c_he = container_of(he, struct c2c_hist_entry, he);
512         if (WARN_ON_ONCE(!c2c_he->nodestr))
513                 return 0;
514
515         return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
516 }
517
518 static int
519 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
520                       struct hist_entry *he)
521 {
522         struct c2c_hist_entry *c2c_he;
523         int width = c2c_width(fmt, hpp, he->hists);
524
525         c2c_he = container_of(he, struct c2c_hist_entry, he);
526         return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
527 }
528
529 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
530                         struct hist_entry *he)
531 {
532         uint64_t addr = 0;
533         int width = c2c_width(fmt, hpp, he->hists);
534         char buf[20];
535
536         if (he->mem_info)
537                 addr = cl_offset(he->mem_info->daddr.al_addr);
538
539         return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
540 }
541
542 static int64_t
543 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
544            struct hist_entry *left, struct hist_entry *right)
545 {
546         uint64_t l = 0, r = 0;
547
548         if (left->mem_info)
549                 l = cl_offset(left->mem_info->daddr.addr);
550         if (right->mem_info)
551                 r = cl_offset(right->mem_info->daddr.addr);
552
553         return (int64_t)(r - l);
554 }
555
556 static int
557 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
558             struct hist_entry *he)
559 {
560         uint64_t addr = 0;
561         int width = c2c_width(fmt, hpp, he->hists);
562         char buf[20];
563
564         if (he->mem_info)
565                 addr = he->mem_info->iaddr.addr;
566
567         return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
568 }
569
570 static int64_t
571 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
572           struct hist_entry *left, struct hist_entry *right)
573 {
574         return sort__iaddr_cmp(left, right);
575 }
576
577 static int
578 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
579                struct hist_entry *he)
580 {
581         struct c2c_hist_entry *c2c_he;
582         int width = c2c_width(fmt, hpp, he->hists);
583         unsigned int tot_hitm;
584
585         c2c_he = container_of(he, struct c2c_hist_entry, he);
586         tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
587
588         return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
589 }
590
591 static int64_t
592 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
593              struct hist_entry *left, struct hist_entry *right)
594 {
595         struct c2c_hist_entry *c2c_left;
596         struct c2c_hist_entry *c2c_right;
597         unsigned int tot_hitm_left;
598         unsigned int tot_hitm_right;
599
600         c2c_left  = container_of(left, struct c2c_hist_entry, he);
601         c2c_right = container_of(right, struct c2c_hist_entry, he);
602
603         tot_hitm_left  = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
604         tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
605
606         return tot_hitm_left - tot_hitm_right;
607 }
608
609 #define STAT_FN_ENTRY(__f)                                      \
610 static int                                                      \
611 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,   \
612               struct hist_entry *he)                            \
613 {                                                               \
614         struct c2c_hist_entry *c2c_he;                          \
615         int width = c2c_width(fmt, hpp, he->hists);             \
616                                                                 \
617         c2c_he = container_of(he, struct c2c_hist_entry, he);   \
618         return scnprintf(hpp->buf, hpp->size, "%*u", width,     \
619                          c2c_he->stats.__f);                    \
620 }
621
622 #define STAT_FN_CMP(__f)                                                \
623 static int64_t                                                          \
624 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused,                    \
625             struct hist_entry *left, struct hist_entry *right)          \
626 {                                                                       \
627         struct c2c_hist_entry *c2c_left, *c2c_right;                    \
628                                                                         \
629         c2c_left  = container_of(left, struct c2c_hist_entry, he);      \
630         c2c_right = container_of(right, struct c2c_hist_entry, he);     \
631         return c2c_left->stats.__f - c2c_right->stats.__f;              \
632 }
633
634 #define STAT_FN(__f)            \
635         STAT_FN_ENTRY(__f)      \
636         STAT_FN_CMP(__f)
637
638 STAT_FN(rmt_hitm)
639 STAT_FN(lcl_hitm)
640 STAT_FN(store)
641 STAT_FN(st_l1hit)
642 STAT_FN(st_l1miss)
643 STAT_FN(ld_fbhit)
644 STAT_FN(ld_l1hit)
645 STAT_FN(ld_l2hit)
646 STAT_FN(ld_llchit)
647 STAT_FN(rmt_hit)
648
649 static uint64_t llc_miss(struct c2c_stats *stats)
650 {
651         uint64_t llcmiss;
652
653         llcmiss = stats->lcl_dram +
654                   stats->rmt_dram +
655                   stats->rmt_hitm +
656                   stats->rmt_hit;
657
658         return llcmiss;
659 }
660
661 static int
662 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
663                  struct hist_entry *he)
664 {
665         struct c2c_hist_entry *c2c_he;
666         int width = c2c_width(fmt, hpp, he->hists);
667
668         c2c_he = container_of(he, struct c2c_hist_entry, he);
669
670         return scnprintf(hpp->buf, hpp->size, "%*lu", width,
671                          llc_miss(&c2c_he->stats));
672 }
673
674 static int64_t
675 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
676                struct hist_entry *left, struct hist_entry *right)
677 {
678         struct c2c_hist_entry *c2c_left;
679         struct c2c_hist_entry *c2c_right;
680
681         c2c_left  = container_of(left, struct c2c_hist_entry, he);
682         c2c_right = container_of(right, struct c2c_hist_entry, he);
683
684         return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
685 }
686
687 static uint64_t total_records(struct c2c_stats *stats)
688 {
689         uint64_t lclmiss, ldcnt, total;
690
691         lclmiss  = stats->lcl_dram +
692                    stats->rmt_dram +
693                    stats->rmt_hitm +
694                    stats->rmt_hit;
695
696         ldcnt    = lclmiss +
697                    stats->ld_fbhit +
698                    stats->ld_l1hit +
699                    stats->ld_l2hit +
700                    stats->ld_llchit +
701                    stats->lcl_hitm;
702
703         total    = ldcnt +
704                    stats->st_l1hit +
705                    stats->st_l1miss;
706
707         return total;
708 }
709
710 static int
711 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
712                 struct hist_entry *he)
713 {
714         struct c2c_hist_entry *c2c_he;
715         int width = c2c_width(fmt, hpp, he->hists);
716         uint64_t tot_recs;
717
718         c2c_he = container_of(he, struct c2c_hist_entry, he);
719         tot_recs = total_records(&c2c_he->stats);
720
721         return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
722 }
723
724 static int64_t
725 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
726              struct hist_entry *left, struct hist_entry *right)
727 {
728         struct c2c_hist_entry *c2c_left;
729         struct c2c_hist_entry *c2c_right;
730         uint64_t tot_recs_left;
731         uint64_t tot_recs_right;
732
733         c2c_left  = container_of(left, struct c2c_hist_entry, he);
734         c2c_right = container_of(right, struct c2c_hist_entry, he);
735
736         tot_recs_left  = total_records(&c2c_left->stats);
737         tot_recs_right = total_records(&c2c_right->stats);
738
739         return tot_recs_left - tot_recs_right;
740 }
741
742 static uint64_t total_loads(struct c2c_stats *stats)
743 {
744         uint64_t lclmiss, ldcnt;
745
746         lclmiss  = stats->lcl_dram +
747                    stats->rmt_dram +
748                    stats->rmt_hitm +
749                    stats->rmt_hit;
750
751         ldcnt    = lclmiss +
752                    stats->ld_fbhit +
753                    stats->ld_l1hit +
754                    stats->ld_l2hit +
755                    stats->ld_llchit +
756                    stats->lcl_hitm;
757
758         return ldcnt;
759 }
760
761 static int
762 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
763                 struct hist_entry *he)
764 {
765         struct c2c_hist_entry *c2c_he;
766         int width = c2c_width(fmt, hpp, he->hists);
767         uint64_t tot_recs;
768
769         c2c_he = container_of(he, struct c2c_hist_entry, he);
770         tot_recs = total_loads(&c2c_he->stats);
771
772         return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
773 }
774
775 static int64_t
776 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
777               struct hist_entry *left, struct hist_entry *right)
778 {
779         struct c2c_hist_entry *c2c_left;
780         struct c2c_hist_entry *c2c_right;
781         uint64_t tot_recs_left;
782         uint64_t tot_recs_right;
783
784         c2c_left  = container_of(left, struct c2c_hist_entry, he);
785         c2c_right = container_of(right, struct c2c_hist_entry, he);
786
787         tot_recs_left  = total_loads(&c2c_left->stats);
788         tot_recs_right = total_loads(&c2c_right->stats);
789
790         return tot_recs_left - tot_recs_right;
791 }
792
793 typedef double (get_percent_cb)(struct c2c_hist_entry *);
794
795 static int
796 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
797               struct hist_entry *he, get_percent_cb get_percent)
798 {
799         struct c2c_hist_entry *c2c_he;
800         int width = c2c_width(fmt, hpp, he->hists);
801         double per;
802
803         c2c_he = container_of(he, struct c2c_hist_entry, he);
804         per = get_percent(c2c_he);
805
806 #ifdef HAVE_SLANG_SUPPORT
807         if (use_browser)
808                 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
809 #endif
810         return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
811 }
812
813 static double percent_hitm(struct c2c_hist_entry *c2c_he)
814 {
815         struct c2c_hists *hists;
816         struct c2c_stats *stats;
817         struct c2c_stats *total;
818         int tot = 0, st = 0;
819         double p;
820
821         hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
822         stats = &c2c_he->stats;
823         total = &hists->stats;
824
825         switch (c2c.display) {
826         case DISPLAY_RMT:
827                 st  = stats->rmt_hitm;
828                 tot = total->rmt_hitm;
829                 break;
830         case DISPLAY_LCL:
831                 st  = stats->lcl_hitm;
832                 tot = total->lcl_hitm;
833                 break;
834         case DISPLAY_TOT:
835                 st  = stats->tot_hitm;
836                 tot = total->tot_hitm;
837         default:
838                 break;
839         }
840
841         p = tot ? (double) st / tot : 0;
842
843         return 100 * p;
844 }
845
846 #define PERC_STR(__s, __v)                              \
847 ({                                                      \
848         scnprintf(__s, sizeof(__s), "%.2F%%", __v);     \
849         __s;                                            \
850 })
851
852 static int
853 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
854                    struct hist_entry *he)
855 {
856         struct c2c_hist_entry *c2c_he;
857         int width = c2c_width(fmt, hpp, he->hists);
858         char buf[10];
859         double per;
860
861         c2c_he = container_of(he, struct c2c_hist_entry, he);
862         per = percent_hitm(c2c_he);
863         return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
864 }
865
866 static int
867 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
868                    struct hist_entry *he)
869 {
870         return percent_color(fmt, hpp, he, percent_hitm);
871 }
872
873 static int64_t
874 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
875                  struct hist_entry *left, struct hist_entry *right)
876 {
877         struct c2c_hist_entry *c2c_left;
878         struct c2c_hist_entry *c2c_right;
879         double per_left;
880         double per_right;
881
882         c2c_left  = container_of(left, struct c2c_hist_entry, he);
883         c2c_right = container_of(right, struct c2c_hist_entry, he);
884
885         per_left  = percent_hitm(c2c_left);
886         per_right = percent_hitm(c2c_right);
887
888         return per_left - per_right;
889 }
890
891 static struct c2c_stats *he_stats(struct hist_entry *he)
892 {
893         struct c2c_hist_entry *c2c_he;
894
895         c2c_he = container_of(he, struct c2c_hist_entry, he);
896         return &c2c_he->stats;
897 }
898
899 static struct c2c_stats *total_stats(struct hist_entry *he)
900 {
901         struct c2c_hists *hists;
902
903         hists = container_of(he->hists, struct c2c_hists, hists);
904         return &hists->stats;
905 }
906
907 static double percent(int st, int tot)
908 {
909         return tot ? 100. * (double) st / (double) tot : 0;
910 }
911
912 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
913
914 #define PERCENT_FN(__f)                                                         \
915 static double percent_ ## __f(struct c2c_hist_entry *c2c_he)                    \
916 {                                                                               \
917         struct c2c_hists *hists;                                                \
918                                                                                 \
919         hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);        \
920         return percent(c2c_he->stats.__f, hists->stats.__f);                    \
921 }
922
923 PERCENT_FN(rmt_hitm)
924 PERCENT_FN(lcl_hitm)
925 PERCENT_FN(st_l1hit)
926 PERCENT_FN(st_l1miss)
927
928 static int
929 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
930                        struct hist_entry *he)
931 {
932         int width = c2c_width(fmt, hpp, he->hists);
933         double per = PERCENT(he, rmt_hitm);
934         char buf[10];
935
936         return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
937 }
938
939 static int
940 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
941                        struct hist_entry *he)
942 {
943         return percent_color(fmt, hpp, he, percent_rmt_hitm);
944 }
945
946 static int64_t
947 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
948                      struct hist_entry *left, struct hist_entry *right)
949 {
950         double per_left;
951         double per_right;
952
953         per_left  = PERCENT(left, lcl_hitm);
954         per_right = PERCENT(right, lcl_hitm);
955
956         return per_left - per_right;
957 }
958
959 static int
960 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
961                        struct hist_entry *he)
962 {
963         int width = c2c_width(fmt, hpp, he->hists);
964         double per = PERCENT(he, lcl_hitm);
965         char buf[10];
966
967         return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
968 }
969
970 static int
971 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
972                        struct hist_entry *he)
973 {
974         return percent_color(fmt, hpp, he, percent_lcl_hitm);
975 }
976
977 static int64_t
978 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
979                      struct hist_entry *left, struct hist_entry *right)
980 {
981         double per_left;
982         double per_right;
983
984         per_left  = PERCENT(left, lcl_hitm);
985         per_right = PERCENT(right, lcl_hitm);
986
987         return per_left - per_right;
988 }
989
990 static int
991 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
992                            struct hist_entry *he)
993 {
994         int width = c2c_width(fmt, hpp, he->hists);
995         double per = PERCENT(he, st_l1hit);
996         char buf[10];
997
998         return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
999 }
1000
1001 static int
1002 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1003                            struct hist_entry *he)
1004 {
1005         return percent_color(fmt, hpp, he, percent_st_l1hit);
1006 }
1007
1008 static int64_t
1009 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1010                         struct hist_entry *left, struct hist_entry *right)
1011 {
1012         double per_left;
1013         double per_right;
1014
1015         per_left  = PERCENT(left, st_l1hit);
1016         per_right = PERCENT(right, st_l1hit);
1017
1018         return per_left - per_right;
1019 }
1020
1021 static int
1022 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1023                            struct hist_entry *he)
1024 {
1025         int width = c2c_width(fmt, hpp, he->hists);
1026         double per = PERCENT(he, st_l1miss);
1027         char buf[10];
1028
1029         return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1030 }
1031
1032 static int
1033 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1034                             struct hist_entry *he)
1035 {
1036         return percent_color(fmt, hpp, he, percent_st_l1miss);
1037 }
1038
1039 static int64_t
1040 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1041                           struct hist_entry *left, struct hist_entry *right)
1042 {
1043         double per_left;
1044         double per_right;
1045
1046         per_left  = PERCENT(left, st_l1miss);
1047         per_right = PERCENT(right, st_l1miss);
1048
1049         return per_left - per_right;
1050 }
1051
1052 STAT_FN(lcl_dram)
1053 STAT_FN(rmt_dram)
1054
1055 static int
1056 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1057           struct hist_entry *he)
1058 {
1059         int width = c2c_width(fmt, hpp, he->hists);
1060
1061         return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1062 }
1063
1064 static int64_t
1065 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1066         struct hist_entry *left, struct hist_entry *right)
1067 {
1068         return left->thread->pid_ - right->thread->pid_;
1069 }
1070
1071 static int64_t
1072 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1073           struct hist_entry *left __maybe_unused,
1074           struct hist_entry *right __maybe_unused)
1075 {
1076         return 0;
1077 }
1078
1079 static int
1080 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1081            struct hist_entry *he)
1082 {
1083         struct c2c_hist_entry *c2c_he;
1084         bool first = true;
1085         int node;
1086         int ret = 0;
1087
1088         c2c_he = container_of(he, struct c2c_hist_entry, he);
1089
1090         for (node = 0; node < c2c.nodes_cnt; node++) {
1091                 DECLARE_BITMAP(set, c2c.cpus_cnt);
1092
1093                 bitmap_zero(set, c2c.cpus_cnt);
1094                 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1095
1096                 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1097                         if (c2c.node_info == 1) {
1098                                 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1099                                 advance_hpp(hpp, ret);
1100                         }
1101                         continue;
1102                 }
1103
1104                 if (!first) {
1105                         ret = scnprintf(hpp->buf, hpp->size, " ");
1106                         advance_hpp(hpp, ret);
1107                 }
1108
1109                 switch (c2c.node_info) {
1110                 case 0:
1111                         ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1112                         advance_hpp(hpp, ret);
1113                         break;
1114                 case 1:
1115                 {
1116                         int num = bitmap_weight(set, c2c.cpus_cnt);
1117                         struct c2c_stats *stats = &c2c_he->node_stats[node];
1118
1119                         ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1120                         advance_hpp(hpp, ret);
1121
1122                 #define DISPLAY_HITM(__h)                                               \
1123                         if (c2c_he->stats.__h> 0) {                                     \
1124                                 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ",        \
1125                                                 percent(stats->__h, c2c_he->stats.__h));\
1126                         } else {                                                        \
1127                                 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a");    \
1128                         }
1129
1130                         switch (c2c.display) {
1131                         case DISPLAY_RMT:
1132                                 DISPLAY_HITM(rmt_hitm);
1133                                 break;
1134                         case DISPLAY_LCL:
1135                                 DISPLAY_HITM(lcl_hitm);
1136                                 break;
1137                         case DISPLAY_TOT:
1138                                 DISPLAY_HITM(tot_hitm);
1139                         default:
1140                                 break;
1141                         }
1142
1143                 #undef DISPLAY_HITM
1144
1145                         advance_hpp(hpp, ret);
1146
1147                         if (c2c_he->stats.store > 0) {
1148                                 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1149                                                 percent(stats->store, c2c_he->stats.store));
1150                         } else {
1151                                 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1152                         }
1153
1154                         advance_hpp(hpp, ret);
1155                         break;
1156                 }
1157                 case 2:
1158                         ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1159                         advance_hpp(hpp, ret);
1160
1161                         ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1162                         advance_hpp(hpp, ret);
1163
1164                         ret = scnprintf(hpp->buf, hpp->size, "}");
1165                         advance_hpp(hpp, ret);
1166                         break;
1167                 default:
1168                         break;
1169                 }
1170
1171                 first = false;
1172         }
1173
1174         return 0;
1175 }
1176
1177 static int
1178 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1179            struct hist_entry *he, double mean)
1180 {
1181         int width = c2c_width(fmt, hpp, he->hists);
1182         char buf[10];
1183
1184         scnprintf(buf, 10, "%6.0f", mean);
1185         return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1186 }
1187
1188 #define MEAN_ENTRY(__func, __val)                                               \
1189 static int                                                                      \
1190 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he)   \
1191 {                                                                               \
1192         struct c2c_hist_entry *c2c_he;                                          \
1193         c2c_he = container_of(he, struct c2c_hist_entry, he);                   \
1194         return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val));      \
1195 }
1196
1197 MEAN_ENTRY(mean_rmt_entry,  rmt_hitm);
1198 MEAN_ENTRY(mean_lcl_entry,  lcl_hitm);
1199 MEAN_ENTRY(mean_load_entry, load);
1200
1201 static int
1202 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1203              struct hist_entry *he)
1204 {
1205         struct c2c_hist_entry *c2c_he;
1206         int width = c2c_width(fmt, hpp, he->hists);
1207         char buf[10];
1208
1209         c2c_he = container_of(he, struct c2c_hist_entry, he);
1210
1211         scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1212         return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1213 }
1214
1215 static int
1216 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1217              struct hist_entry *he)
1218 {
1219         struct c2c_hist_entry *c2c_he;
1220         int width = c2c_width(fmt, hpp, he->hists);
1221         char buf[10];
1222
1223         c2c_he = container_of(he, struct c2c_hist_entry, he);
1224
1225         scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1226         return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1227 }
1228
1229 static int
1230 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1231                    struct hist_entry *he)
1232 {
1233         int width = c2c_width(fmt, hpp, he->hists);
1234
1235         return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1236 }
1237
1238 #define HEADER_LOW(__h)                 \
1239         {                               \
1240                 .line[1] = {            \
1241                         .text = __h,    \
1242                 },                      \
1243         }
1244
1245 #define HEADER_BOTH(__h0, __h1)         \
1246         {                               \
1247                 .line[0] = {            \
1248                         .text = __h0,   \
1249                 },                      \
1250                 .line[1] = {            \
1251                         .text = __h1,   \
1252                 },                      \
1253         }
1254
1255 #define HEADER_SPAN(__h0, __h1, __s)    \
1256         {                               \
1257                 .line[0] = {            \
1258                         .text = __h0,   \
1259                         .span = __s,    \
1260                 },                      \
1261                 .line[1] = {            \
1262                         .text = __h1,   \
1263                 },                      \
1264         }
1265
1266 #define HEADER_SPAN_LOW(__h)            \
1267         {                               \
1268                 .line[1] = {            \
1269                         .text = __h,    \
1270                 },                      \
1271         }
1272
1273 static struct c2c_dimension dim_dcacheline = {
1274         .header         = HEADER_SPAN("--- Cacheline ----", "Address", 2),
1275         .name           = "dcacheline",
1276         .cmp            = dcacheline_cmp,
1277         .entry          = dcacheline_entry,
1278         .width          = 18,
1279 };
1280
1281 static struct c2c_dimension dim_dcacheline_node = {
1282         .header         = HEADER_LOW("Node"),
1283         .name           = "dcacheline_node",
1284         .cmp            = empty_cmp,
1285         .entry          = dcacheline_node_entry,
1286         .width          = 4,
1287 };
1288
1289 static struct c2c_dimension dim_dcacheline_count = {
1290         .header         = HEADER_LOW("PA cnt"),
1291         .name           = "dcacheline_count",
1292         .cmp            = empty_cmp,
1293         .entry          = dcacheline_node_count,
1294         .width          = 6,
1295 };
1296
1297 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
1298
1299 static struct c2c_dimension dim_offset = {
1300         .header         = HEADER_SPAN("--- Data address -", "Offset", 2),
1301         .name           = "offset",
1302         .cmp            = offset_cmp,
1303         .entry          = offset_entry,
1304         .width          = 18,
1305 };
1306
1307 static struct c2c_dimension dim_offset_node = {
1308         .header         = HEADER_LOW("Node"),
1309         .name           = "offset_node",
1310         .cmp            = empty_cmp,
1311         .entry          = dcacheline_node_entry,
1312         .width          = 4,
1313 };
1314
1315 static struct c2c_dimension dim_iaddr = {
1316         .header         = HEADER_LOW("Code address"),
1317         .name           = "iaddr",
1318         .cmp            = iaddr_cmp,
1319         .entry          = iaddr_entry,
1320         .width          = 18,
1321 };
1322
1323 static struct c2c_dimension dim_tot_hitm = {
1324         .header         = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1325         .name           = "tot_hitm",
1326         .cmp            = tot_hitm_cmp,
1327         .entry          = tot_hitm_entry,
1328         .width          = 7,
1329 };
1330
1331 static struct c2c_dimension dim_lcl_hitm = {
1332         .header         = HEADER_SPAN_LOW("Lcl"),
1333         .name           = "lcl_hitm",
1334         .cmp            = lcl_hitm_cmp,
1335         .entry          = lcl_hitm_entry,
1336         .width          = 7,
1337 };
1338
1339 static struct c2c_dimension dim_rmt_hitm = {
1340         .header         = HEADER_SPAN_LOW("Rmt"),
1341         .name           = "rmt_hitm",
1342         .cmp            = rmt_hitm_cmp,
1343         .entry          = rmt_hitm_entry,
1344         .width          = 7,
1345 };
1346
1347 static struct c2c_dimension dim_cl_rmt_hitm = {
1348         .header         = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1349         .name           = "cl_rmt_hitm",
1350         .cmp            = rmt_hitm_cmp,
1351         .entry          = rmt_hitm_entry,
1352         .width          = 7,
1353 };
1354
1355 static struct c2c_dimension dim_cl_lcl_hitm = {
1356         .header         = HEADER_SPAN_LOW("Lcl"),
1357         .name           = "cl_lcl_hitm",
1358         .cmp            = lcl_hitm_cmp,
1359         .entry          = lcl_hitm_entry,
1360         .width          = 7,
1361 };
1362
1363 static struct c2c_dimension dim_stores = {
1364         .header         = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1365         .name           = "stores",
1366         .cmp            = store_cmp,
1367         .entry          = store_entry,
1368         .width          = 7,
1369 };
1370
1371 static struct c2c_dimension dim_stores_l1hit = {
1372         .header         = HEADER_SPAN_LOW("L1Hit"),
1373         .name           = "stores_l1hit",
1374         .cmp            = st_l1hit_cmp,
1375         .entry          = st_l1hit_entry,
1376         .width          = 7,
1377 };
1378
1379 static struct c2c_dimension dim_stores_l1miss = {
1380         .header         = HEADER_SPAN_LOW("L1Miss"),
1381         .name           = "stores_l1miss",
1382         .cmp            = st_l1miss_cmp,
1383         .entry          = st_l1miss_entry,
1384         .width          = 7,
1385 };
1386
1387 static struct c2c_dimension dim_cl_stores_l1hit = {
1388         .header         = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1389         .name           = "cl_stores_l1hit",
1390         .cmp            = st_l1hit_cmp,
1391         .entry          = st_l1hit_entry,
1392         .width          = 7,
1393 };
1394
1395 static struct c2c_dimension dim_cl_stores_l1miss = {
1396         .header         = HEADER_SPAN_LOW("L1 Miss"),
1397         .name           = "cl_stores_l1miss",
1398         .cmp            = st_l1miss_cmp,
1399         .entry          = st_l1miss_entry,
1400         .width          = 7,
1401 };
1402
1403 static struct c2c_dimension dim_ld_fbhit = {
1404         .header         = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1405         .name           = "ld_fbhit",
1406         .cmp            = ld_fbhit_cmp,
1407         .entry          = ld_fbhit_entry,
1408         .width          = 7,
1409 };
1410
1411 static struct c2c_dimension dim_ld_l1hit = {
1412         .header         = HEADER_SPAN_LOW("L1"),
1413         .name           = "ld_l1hit",
1414         .cmp            = ld_l1hit_cmp,
1415         .entry          = ld_l1hit_entry,
1416         .width          = 7,
1417 };
1418
1419 static struct c2c_dimension dim_ld_l2hit = {
1420         .header         = HEADER_SPAN_LOW("L2"),
1421         .name           = "ld_l2hit",
1422         .cmp            = ld_l2hit_cmp,
1423         .entry          = ld_l2hit_entry,
1424         .width          = 7,
1425 };
1426
1427 static struct c2c_dimension dim_ld_llchit = {
1428         .header         = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1429         .name           = "ld_lclhit",
1430         .cmp            = ld_llchit_cmp,
1431         .entry          = ld_llchit_entry,
1432         .width          = 8,
1433 };
1434
1435 static struct c2c_dimension dim_ld_rmthit = {
1436         .header         = HEADER_SPAN_LOW("Rmt"),
1437         .name           = "ld_rmthit",
1438         .cmp            = rmt_hit_cmp,
1439         .entry          = rmt_hit_entry,
1440         .width          = 8,
1441 };
1442
1443 static struct c2c_dimension dim_ld_llcmiss = {
1444         .header         = HEADER_BOTH("LLC", "Ld Miss"),
1445         .name           = "ld_llcmiss",
1446         .cmp            = ld_llcmiss_cmp,
1447         .entry          = ld_llcmiss_entry,
1448         .width          = 7,
1449 };
1450
1451 static struct c2c_dimension dim_tot_recs = {
1452         .header         = HEADER_BOTH("Total", "records"),
1453         .name           = "tot_recs",
1454         .cmp            = tot_recs_cmp,
1455         .entry          = tot_recs_entry,
1456         .width          = 7,
1457 };
1458
1459 static struct c2c_dimension dim_tot_loads = {
1460         .header         = HEADER_BOTH("Total", "Loads"),
1461         .name           = "tot_loads",
1462         .cmp            = tot_loads_cmp,
1463         .entry          = tot_loads_entry,
1464         .width          = 7,
1465 };
1466
1467 static struct c2c_header percent_hitm_header[] = {
1468         [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1469         [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
1470         [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
1471 };
1472
1473 static struct c2c_dimension dim_percent_hitm = {
1474         .name           = "percent_hitm",
1475         .cmp            = percent_hitm_cmp,
1476         .entry          = percent_hitm_entry,
1477         .color          = percent_hitm_color,
1478         .width          = 7,
1479 };
1480
1481 static struct c2c_dimension dim_percent_rmt_hitm = {
1482         .header         = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1483         .name           = "percent_rmt_hitm",
1484         .cmp            = percent_rmt_hitm_cmp,
1485         .entry          = percent_rmt_hitm_entry,
1486         .color          = percent_rmt_hitm_color,
1487         .width          = 7,
1488 };
1489
1490 static struct c2c_dimension dim_percent_lcl_hitm = {
1491         .header         = HEADER_SPAN_LOW("Lcl"),
1492         .name           = "percent_lcl_hitm",
1493         .cmp            = percent_lcl_hitm_cmp,
1494         .entry          = percent_lcl_hitm_entry,
1495         .color          = percent_lcl_hitm_color,
1496         .width          = 7,
1497 };
1498
1499 static struct c2c_dimension dim_percent_stores_l1hit = {
1500         .header         = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1501         .name           = "percent_stores_l1hit",
1502         .cmp            = percent_stores_l1hit_cmp,
1503         .entry          = percent_stores_l1hit_entry,
1504         .color          = percent_stores_l1hit_color,
1505         .width          = 7,
1506 };
1507
1508 static struct c2c_dimension dim_percent_stores_l1miss = {
1509         .header         = HEADER_SPAN_LOW("L1 Miss"),
1510         .name           = "percent_stores_l1miss",
1511         .cmp            = percent_stores_l1miss_cmp,
1512         .entry          = percent_stores_l1miss_entry,
1513         .color          = percent_stores_l1miss_color,
1514         .width          = 7,
1515 };
1516
1517 static struct c2c_dimension dim_dram_lcl = {
1518         .header         = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1519         .name           = "dram_lcl",
1520         .cmp            = lcl_dram_cmp,
1521         .entry          = lcl_dram_entry,
1522         .width          = 8,
1523 };
1524
1525 static struct c2c_dimension dim_dram_rmt = {
1526         .header         = HEADER_SPAN_LOW("Rmt"),
1527         .name           = "dram_rmt",
1528         .cmp            = rmt_dram_cmp,
1529         .entry          = rmt_dram_entry,
1530         .width          = 8,
1531 };
1532
1533 static struct c2c_dimension dim_pid = {
1534         .header         = HEADER_LOW("Pid"),
1535         .name           = "pid",
1536         .cmp            = pid_cmp,
1537         .entry          = pid_entry,
1538         .width          = 7,
1539 };
1540
1541 static struct c2c_dimension dim_tid = {
1542         .header         = HEADER_LOW("Tid"),
1543         .name           = "tid",
1544         .se             = &sort_thread,
1545 };
1546
1547 static struct c2c_dimension dim_symbol = {
1548         .name           = "symbol",
1549         .se             = &sort_sym,
1550 };
1551
1552 static struct c2c_dimension dim_dso = {
1553         .header         = HEADER_BOTH("Shared", "Object"),
1554         .name           = "dso",
1555         .se             = &sort_dso,
1556 };
1557
1558 static struct c2c_header header_node[3] = {
1559         HEADER_LOW("Node"),
1560         HEADER_LOW("Node{cpus %hitms %stores}"),
1561         HEADER_LOW("Node{cpu list}"),
1562 };
1563
1564 static struct c2c_dimension dim_node = {
1565         .name           = "node",
1566         .cmp            = empty_cmp,
1567         .entry          = node_entry,
1568         .width          = 4,
1569 };
1570
1571 static struct c2c_dimension dim_mean_rmt = {
1572         .header         = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1573         .name           = "mean_rmt",
1574         .cmp            = empty_cmp,
1575         .entry          = mean_rmt_entry,
1576         .width          = 8,
1577 };
1578
1579 static struct c2c_dimension dim_mean_lcl = {
1580         .header         = HEADER_SPAN_LOW("lcl hitm"),
1581         .name           = "mean_lcl",
1582         .cmp            = empty_cmp,
1583         .entry          = mean_lcl_entry,
1584         .width          = 8,
1585 };
1586
1587 static struct c2c_dimension dim_mean_load = {
1588         .header         = HEADER_SPAN_LOW("load"),
1589         .name           = "mean_load",
1590         .cmp            = empty_cmp,
1591         .entry          = mean_load_entry,
1592         .width          = 8,
1593 };
1594
1595 static struct c2c_dimension dim_cpucnt = {
1596         .header         = HEADER_BOTH("cpu", "cnt"),
1597         .name           = "cpucnt",
1598         .cmp            = empty_cmp,
1599         .entry          = cpucnt_entry,
1600         .width          = 8,
1601 };
1602
1603 static struct c2c_dimension dim_srcline = {
1604         .name           = "cl_srcline",
1605         .se             = &sort_srcline,
1606 };
1607
1608 static struct c2c_dimension dim_dcacheline_idx = {
1609         .header         = HEADER_LOW("Index"),
1610         .name           = "cl_idx",
1611         .cmp            = empty_cmp,
1612         .entry          = cl_idx_entry,
1613         .width          = 5,
1614 };
1615
1616 static struct c2c_dimension dim_dcacheline_num = {
1617         .header         = HEADER_LOW("Num"),
1618         .name           = "cl_num",
1619         .cmp            = empty_cmp,
1620         .entry          = cl_idx_entry,
1621         .width          = 5,
1622 };
1623
1624 static struct c2c_dimension dim_dcacheline_num_empty = {
1625         .header         = HEADER_LOW("Num"),
1626         .name           = "cl_num_empty",
1627         .cmp            = empty_cmp,
1628         .entry          = cl_idx_empty_entry,
1629         .width          = 5,
1630 };
1631
1632 static struct c2c_dimension *dimensions[] = {
1633         &dim_dcacheline,
1634         &dim_dcacheline_node,
1635         &dim_dcacheline_count,
1636         &dim_offset,
1637         &dim_offset_node,
1638         &dim_iaddr,
1639         &dim_tot_hitm,
1640         &dim_lcl_hitm,
1641         &dim_rmt_hitm,
1642         &dim_cl_lcl_hitm,
1643         &dim_cl_rmt_hitm,
1644         &dim_stores,
1645         &dim_stores_l1hit,
1646         &dim_stores_l1miss,
1647         &dim_cl_stores_l1hit,
1648         &dim_cl_stores_l1miss,
1649         &dim_ld_fbhit,
1650         &dim_ld_l1hit,
1651         &dim_ld_l2hit,
1652         &dim_ld_llchit,
1653         &dim_ld_rmthit,
1654         &dim_ld_llcmiss,
1655         &dim_tot_recs,
1656         &dim_tot_loads,
1657         &dim_percent_hitm,
1658         &dim_percent_rmt_hitm,
1659         &dim_percent_lcl_hitm,
1660         &dim_percent_stores_l1hit,
1661         &dim_percent_stores_l1miss,
1662         &dim_dram_lcl,
1663         &dim_dram_rmt,
1664         &dim_pid,
1665         &dim_tid,
1666         &dim_symbol,
1667         &dim_dso,
1668         &dim_node,
1669         &dim_mean_rmt,
1670         &dim_mean_lcl,
1671         &dim_mean_load,
1672         &dim_cpucnt,
1673         &dim_srcline,
1674         &dim_dcacheline_idx,
1675         &dim_dcacheline_num,
1676         &dim_dcacheline_num_empty,
1677         NULL,
1678 };
1679
1680 static void fmt_free(struct perf_hpp_fmt *fmt)
1681 {
1682         struct c2c_fmt *c2c_fmt;
1683
1684         c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1685         free(c2c_fmt);
1686 }
1687
1688 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1689 {
1690         struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1691         struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1692
1693         return c2c_a->dim == c2c_b->dim;
1694 }
1695
1696 static struct c2c_dimension *get_dimension(const char *name)
1697 {
1698         unsigned int i;
1699
1700         for (i = 0; dimensions[i]; i++) {
1701                 struct c2c_dimension *dim = dimensions[i];
1702
1703                 if (!strcmp(dim->name, name))
1704                         return dim;
1705         };
1706
1707         return NULL;
1708 }
1709
1710 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1711                         struct hist_entry *he)
1712 {
1713         struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1714         struct c2c_dimension *dim = c2c_fmt->dim;
1715         size_t len = fmt->user_len;
1716
1717         if (!len) {
1718                 len = hists__col_len(he->hists, dim->se->se_width_idx);
1719
1720                 if (dim == &dim_symbol || dim == &dim_srcline)
1721                         len = symbol_width(he->hists, dim->se);
1722         }
1723
1724         return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1725 }
1726
1727 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1728                           struct hist_entry *a, struct hist_entry *b)
1729 {
1730         struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1731         struct c2c_dimension *dim = c2c_fmt->dim;
1732
1733         return dim->se->se_cmp(a, b);
1734 }
1735
1736 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1737                                struct hist_entry *a, struct hist_entry *b)
1738 {
1739         struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1740         struct c2c_dimension *dim = c2c_fmt->dim;
1741         int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1742
1743         collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1744         return collapse_fn(a, b);
1745 }
1746
1747 static struct c2c_fmt *get_format(const char *name)
1748 {
1749         struct c2c_dimension *dim = get_dimension(name);
1750         struct c2c_fmt *c2c_fmt;
1751         struct perf_hpp_fmt *fmt;
1752
1753         if (!dim)
1754                 return NULL;
1755
1756         c2c_fmt = zalloc(sizeof(*c2c_fmt));
1757         if (!c2c_fmt)
1758                 return NULL;
1759
1760         c2c_fmt->dim = dim;
1761
1762         fmt = &c2c_fmt->fmt;
1763         INIT_LIST_HEAD(&fmt->list);
1764         INIT_LIST_HEAD(&fmt->sort_list);
1765
1766         fmt->cmp        = dim->se ? c2c_se_cmp   : dim->cmp;
1767         fmt->sort       = dim->se ? c2c_se_cmp   : dim->cmp;
1768         fmt->color      = dim->se ? NULL         : dim->color;
1769         fmt->entry      = dim->se ? c2c_se_entry : dim->entry;
1770         fmt->header     = c2c_header;
1771         fmt->width      = c2c_width;
1772         fmt->collapse   = dim->se ? c2c_se_collapse : dim->cmp;
1773         fmt->equal      = fmt_equal;
1774         fmt->free       = fmt_free;
1775
1776         return c2c_fmt;
1777 }
1778
1779 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1780 {
1781         struct c2c_fmt *c2c_fmt = get_format(name);
1782
1783         if (!c2c_fmt) {
1784                 reset_dimensions();
1785                 return output_field_add(hpp_list, name);
1786         }
1787
1788         perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1789         return 0;
1790 }
1791
1792 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1793 {
1794         struct c2c_fmt *c2c_fmt = get_format(name);
1795         struct c2c_dimension *dim;
1796
1797         if (!c2c_fmt) {
1798                 reset_dimensions();
1799                 return sort_dimension__add(hpp_list, name, NULL, 0);
1800         }
1801
1802         dim = c2c_fmt->dim;
1803         if (dim == &dim_dso)
1804                 hpp_list->dso = 1;
1805
1806         perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1807         return 0;
1808 }
1809
1810 #define PARSE_LIST(_list, _fn)                                                  \
1811         do {                                                                    \
1812                 char *tmp, *tok;                                                \
1813                 ret = 0;                                                        \
1814                                                                                 \
1815                 if (!_list)                                                     \
1816                         break;                                                  \
1817                                                                                 \
1818                 for (tok = strtok_r((char *)_list, ", ", &tmp);                 \
1819                                 tok; tok = strtok_r(NULL, ", ", &tmp)) {        \
1820                         ret = _fn(hpp_list, tok);                               \
1821                         if (ret == -EINVAL) {                                   \
1822                                 pr_err("Invalid --fields key: `%s'", tok);      \
1823                                 break;                                          \
1824                         } else if (ret == -ESRCH) {                             \
1825                                 pr_err("Unknown --fields key: `%s'", tok);      \
1826                                 break;                                          \
1827                         }                                                       \
1828                 }                                                               \
1829         } while (0)
1830
1831 static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1832                            const char *output_,
1833                            const char *sort_)
1834 {
1835         char *output = output_ ? strdup(output_) : NULL;
1836         char *sort   = sort_   ? strdup(sort_) : NULL;
1837         int ret;
1838
1839         PARSE_LIST(output, c2c_hists__init_output);
1840         PARSE_LIST(sort,   c2c_hists__init_sort);
1841
1842         /* copy sort keys to output fields */
1843         perf_hpp__setup_output_field(hpp_list);
1844
1845         /*
1846          * We dont need other sorting keys other than those
1847          * we already specified. It also really slows down
1848          * the processing a lot with big number of output
1849          * fields, so switching this off for c2c.
1850          */
1851
1852 #if 0
1853         /* and then copy output fields to sort keys */
1854         perf_hpp__append_sort_keys(&hists->list);
1855 #endif
1856
1857         free(output);
1858         free(sort);
1859         return ret;
1860 }
1861
1862 static int c2c_hists__init(struct c2c_hists *hists,
1863                            const char *sort,
1864                            int nr_header_lines)
1865 {
1866         __hists__init(&hists->hists, &hists->list);
1867
1868         /*
1869          * Initialize only with sort fields, we need to resort
1870          * later anyway, and that's where we add output fields
1871          * as well.
1872          */
1873         perf_hpp_list__init(&hists->list);
1874
1875         /* Overload number of header lines.*/
1876         hists->list.nr_header_lines = nr_header_lines;
1877
1878         return hpp_list__parse(&hists->list, NULL, sort);
1879 }
1880
1881 static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1882                              const char *output,
1883                              const char *sort)
1884 {
1885         perf_hpp__reset_output_field(&c2c_hists->list);
1886         return hpp_list__parse(&c2c_hists->list, output, sort);
1887 }
1888
1889 #define DISPLAY_LINE_LIMIT  0.001
1890
1891 static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1892 {
1893         struct c2c_hist_entry *c2c_he;
1894         double ld_dist;
1895
1896         if (c2c.show_all)
1897                 return true;
1898
1899         c2c_he = container_of(he, struct c2c_hist_entry, he);
1900
1901 #define FILTER_HITM(__h)                                                \
1902         if (stats->__h) {                                               \
1903                 ld_dist = ((double)c2c_he->stats.__h / stats->__h);     \
1904                 if (ld_dist < DISPLAY_LINE_LIMIT)                       \
1905                         he->filtered = HIST_FILTER__C2C;                \
1906         } else {                                                        \
1907                 he->filtered = HIST_FILTER__C2C;                        \
1908         }
1909
1910         switch (c2c.display) {
1911         case DISPLAY_LCL:
1912                 FILTER_HITM(lcl_hitm);
1913                 break;
1914         case DISPLAY_RMT:
1915                 FILTER_HITM(rmt_hitm);
1916                 break;
1917         case DISPLAY_TOT:
1918                 FILTER_HITM(tot_hitm);
1919         default:
1920                 break;
1921         };
1922
1923 #undef FILTER_HITM
1924
1925         return he->filtered == 0;
1926 }
1927
1928 static inline int valid_hitm_or_store(struct hist_entry *he)
1929 {
1930         struct c2c_hist_entry *c2c_he;
1931         bool has_hitm;
1932
1933         c2c_he = container_of(he, struct c2c_hist_entry, he);
1934         has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1935                    c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1936                                                 c2c_he->stats.rmt_hitm;
1937         return has_hitm || c2c_he->stats.store;
1938 }
1939
1940 static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1941 {
1942         struct c2c_dimension *dim;
1943
1944         dim = &c2c.hists == c2c_he->hists ?
1945               &dim_dcacheline_node : &dim_offset_node;
1946
1947         if (len > dim->width)
1948                 dim->width = len;
1949 }
1950
1951 static int set_nodestr(struct c2c_hist_entry *c2c_he)
1952 {
1953         char buf[30];
1954         int len;
1955
1956         if (c2c_he->nodestr)
1957                 return 0;
1958
1959         if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1960                 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1961                                       buf, sizeof(buf));
1962         } else {
1963                 len = scnprintf(buf, sizeof(buf), "N/A");
1964         }
1965
1966         set_node_width(c2c_he, len);
1967         c2c_he->nodestr = strdup(buf);
1968         return c2c_he->nodestr ? 0 : -ENOMEM;
1969 }
1970
1971 static void calc_width(struct c2c_hist_entry *c2c_he)
1972 {
1973         struct c2c_hists *c2c_hists;
1974
1975         c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1976         hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
1977         set_nodestr(c2c_he);
1978 }
1979
1980 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
1981 {
1982         struct c2c_hist_entry *c2c_he;
1983
1984         c2c_he = container_of(he, struct c2c_hist_entry, he);
1985
1986         if (c2c.show_src && !he->srcline)
1987                 he->srcline = hist_entry__srcline(he);
1988
1989         calc_width(c2c_he);
1990
1991         if (!valid_hitm_or_store(he))
1992                 he->filtered = HIST_FILTER__C2C;
1993
1994         return 0;
1995 }
1996
1997 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
1998 {
1999         struct c2c_hist_entry *c2c_he;
2000         struct c2c_hists *c2c_hists;
2001         bool display = he__display(he, &c2c.hitm_stats);
2002
2003         c2c_he = container_of(he, struct c2c_hist_entry, he);
2004         c2c_hists = c2c_he->hists;
2005
2006         if (display && c2c_hists) {
2007                 static unsigned int idx;
2008
2009                 c2c_he->cacheline_idx = idx++;
2010                 calc_width(c2c_he);
2011
2012                 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
2013
2014                 hists__collapse_resort(&c2c_hists->hists, NULL);
2015                 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2016         }
2017
2018         return 0;
2019 }
2020
2021 static void setup_nodes_header(void)
2022 {
2023         dim_node.header = header_node[c2c.node_info];
2024 }
2025
2026 static int setup_nodes(struct perf_session *session)
2027 {
2028         struct numa_node *n;
2029         unsigned long **nodes;
2030         int node, cpu;
2031         int *cpu2node;
2032
2033         if (c2c.node_info > 2)
2034                 c2c.node_info = 2;
2035
2036         c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2037         c2c.cpus_cnt  = session->header.env.nr_cpus_avail;
2038
2039         n = session->header.env.numa_nodes;
2040         if (!n)
2041                 return -EINVAL;
2042
2043         nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2044         if (!nodes)
2045                 return -ENOMEM;
2046
2047         c2c.nodes = nodes;
2048
2049         cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2050         if (!cpu2node)
2051                 return -ENOMEM;
2052
2053         for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2054                 cpu2node[cpu] = -1;
2055
2056         c2c.cpu2node = cpu2node;
2057
2058         for (node = 0; node < c2c.nodes_cnt; node++) {
2059                 struct perf_cpu_map *map = n[node].map;
2060                 unsigned long *set;
2061
2062                 set = bitmap_alloc(c2c.cpus_cnt);
2063                 if (!set)
2064                         return -ENOMEM;
2065
2066                 nodes[node] = set;
2067
2068                 /* empty node, skip */
2069                 if (perf_cpu_map__empty(map))
2070                         continue;
2071
2072                 for (cpu = 0; cpu < map->nr; cpu++) {
2073                         set_bit(map->map[cpu], set);
2074
2075                         if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2076                                 return -EINVAL;
2077
2078                         cpu2node[map->map[cpu]] = node;
2079                 }
2080         }
2081
2082         setup_nodes_header();
2083         return 0;
2084 }
2085
2086 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2087
2088 static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused)
2089 {
2090         struct c2c_hist_entry *c2c_he;
2091         c2c_he = container_of(he, struct c2c_hist_entry, he);
2092
2093         if (HAS_HITMS(c2c_he)) {
2094                 c2c.shared_clines++;
2095                 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2096         }
2097
2098         return 0;
2099 }
2100
2101 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2102 {
2103         struct rb_node *next = rb_first_cached(&hists->entries);
2104         int ret = 0;
2105
2106         while (next) {
2107                 struct hist_entry *he;
2108
2109                 he = rb_entry(next, struct hist_entry, rb_node);
2110                 ret = cb(he, NULL);
2111                 if (ret)
2112                         break;
2113                 next = rb_next(&he->rb_node);
2114         }
2115
2116         return ret;
2117 }
2118
2119 static void print_c2c__display_stats(FILE *out)
2120 {
2121         int llc_misses;
2122         struct c2c_stats *stats = &c2c.hists.stats;
2123
2124         llc_misses = stats->lcl_dram +
2125                      stats->rmt_dram +
2126                      stats->rmt_hit +
2127                      stats->rmt_hitm;
2128
2129         fprintf(out, "=================================================\n");
2130         fprintf(out, "            Trace Event Information              \n");
2131         fprintf(out, "=================================================\n");
2132         fprintf(out, "  Total records                     : %10d\n", stats->nr_entries);
2133         fprintf(out, "  Locked Load/Store Operations      : %10d\n", stats->locks);
2134         fprintf(out, "  Load Operations                   : %10d\n", stats->load);
2135         fprintf(out, "  Loads - uncacheable               : %10d\n", stats->ld_uncache);
2136         fprintf(out, "  Loads - IO                        : %10d\n", stats->ld_io);
2137         fprintf(out, "  Loads - Miss                      : %10d\n", stats->ld_miss);
2138         fprintf(out, "  Loads - no mapping                : %10d\n", stats->ld_noadrs);
2139         fprintf(out, "  Load Fill Buffer Hit              : %10d\n", stats->ld_fbhit);
2140         fprintf(out, "  Load L1D hit                      : %10d\n", stats->ld_l1hit);
2141         fprintf(out, "  Load L2D hit                      : %10d\n", stats->ld_l2hit);
2142         fprintf(out, "  Load LLC hit                      : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2143         fprintf(out, "  Load Local HITM                   : %10d\n", stats->lcl_hitm);
2144         fprintf(out, "  Load Remote HITM                  : %10d\n", stats->rmt_hitm);
2145         fprintf(out, "  Load Remote HIT                   : %10d\n", stats->rmt_hit);
2146         fprintf(out, "  Load Local DRAM                   : %10d\n", stats->lcl_dram);
2147         fprintf(out, "  Load Remote DRAM                  : %10d\n", stats->rmt_dram);
2148         fprintf(out, "  Load MESI State Exclusive         : %10d\n", stats->ld_excl);
2149         fprintf(out, "  Load MESI State Shared            : %10d\n", stats->ld_shared);
2150         fprintf(out, "  Load LLC Misses                   : %10d\n", llc_misses);
2151         fprintf(out, "  LLC Misses to Local DRAM          : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2152         fprintf(out, "  LLC Misses to Remote DRAM         : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2153         fprintf(out, "  LLC Misses to Remote cache (HIT)  : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2154         fprintf(out, "  LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2155         fprintf(out, "  Store Operations                  : %10d\n", stats->store);
2156         fprintf(out, "  Store - uncacheable               : %10d\n", stats->st_uncache);
2157         fprintf(out, "  Store - no mapping                : %10d\n", stats->st_noadrs);
2158         fprintf(out, "  Store L1D Hit                     : %10d\n", stats->st_l1hit);
2159         fprintf(out, "  Store L1D Miss                    : %10d\n", stats->st_l1miss);
2160         fprintf(out, "  No Page Map Rejects               : %10d\n", stats->nomap);
2161         fprintf(out, "  Unable to parse data source       : %10d\n", stats->noparse);
2162 }
2163
2164 static void print_shared_cacheline_info(FILE *out)
2165 {
2166         struct c2c_stats *stats = &c2c.hitm_stats;
2167         int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2168
2169         fprintf(out, "=================================================\n");
2170         fprintf(out, "    Global Shared Cache Line Event Information   \n");
2171         fprintf(out, "=================================================\n");
2172         fprintf(out, "  Total Shared Cache Lines          : %10d\n", c2c.shared_clines);
2173         fprintf(out, "  Load HITs on shared lines         : %10d\n", stats->load);
2174         fprintf(out, "  Fill Buffer Hits on shared lines  : %10d\n", stats->ld_fbhit);
2175         fprintf(out, "  L1D hits on shared lines          : %10d\n", stats->ld_l1hit);
2176         fprintf(out, "  L2D hits on shared lines          : %10d\n", stats->ld_l2hit);
2177         fprintf(out, "  LLC hits on shared lines          : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2178         fprintf(out, "  Locked Access on shared lines     : %10d\n", stats->locks);
2179         fprintf(out, "  Store HITs on shared lines        : %10d\n", stats->store);
2180         fprintf(out, "  Store L1D hits on shared lines    : %10d\n", stats->st_l1hit);
2181         fprintf(out, "  Total Merged records              : %10d\n", hitm_cnt + stats->store);
2182 }
2183
2184 static void print_cacheline(struct c2c_hists *c2c_hists,
2185                             struct hist_entry *he_cl,
2186                             struct perf_hpp_list *hpp_list,
2187                             FILE *out)
2188 {
2189         char bf[1000];
2190         struct perf_hpp hpp = {
2191                 .buf            = bf,
2192                 .size           = 1000,
2193         };
2194         static bool once;
2195
2196         if (!once) {
2197                 hists__fprintf_headers(&c2c_hists->hists, out);
2198                 once = true;
2199         } else {
2200                 fprintf(out, "\n");
2201         }
2202
2203         fprintf(out, "  -------------------------------------------------------------\n");
2204         __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2205         fprintf(out, "%s\n", bf);
2206         fprintf(out, "  -------------------------------------------------------------\n");
2207
2208         hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
2209 }
2210
2211 static void print_pareto(FILE *out)
2212 {
2213         struct perf_hpp_list hpp_list;
2214         struct rb_node *nd;
2215         int ret;
2216
2217         perf_hpp_list__init(&hpp_list);
2218         ret = hpp_list__parse(&hpp_list,
2219                                 "cl_num,"
2220                                 "cl_rmt_hitm,"
2221                                 "cl_lcl_hitm,"
2222                                 "cl_stores_l1hit,"
2223                                 "cl_stores_l1miss,"
2224                                 "dcacheline",
2225                                 NULL);
2226
2227         if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2228                 return;
2229
2230         nd = rb_first_cached(&c2c.hists.hists.entries);
2231
2232         for (; nd; nd = rb_next(nd)) {
2233                 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2234                 struct c2c_hist_entry *c2c_he;
2235
2236                 if (he->filtered)
2237                         continue;
2238
2239                 c2c_he = container_of(he, struct c2c_hist_entry, he);
2240                 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2241         }
2242 }
2243
2244 static void print_c2c_info(FILE *out, struct perf_session *session)
2245 {
2246         struct evlist *evlist = session->evlist;
2247         struct evsel *evsel;
2248         bool first = true;
2249
2250         fprintf(out, "=================================================\n");
2251         fprintf(out, "                 c2c details                     \n");
2252         fprintf(out, "=================================================\n");
2253
2254         evlist__for_each_entry(evlist, evsel) {
2255                 fprintf(out, "%-36s: %s\n", first ? "  Events" : "",
2256                         perf_evsel__name(evsel));
2257                 first = false;
2258         }
2259         fprintf(out, "  Cachelines sort on                : %s HITMs\n",
2260                 display_str[c2c.display]);
2261         fprintf(out, "  Cacheline data grouping           : %s\n", c2c.cl_sort);
2262 }
2263
2264 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
2265 {
2266         setup_pager();
2267
2268         print_c2c__display_stats(out);
2269         fprintf(out, "\n");
2270         print_shared_cacheline_info(out);
2271         fprintf(out, "\n");
2272         print_c2c_info(out, session);
2273
2274         if (c2c.stats_only)
2275                 return;
2276
2277         fprintf(out, "\n");
2278         fprintf(out, "=================================================\n");
2279         fprintf(out, "           Shared Data Cache Line Table          \n");
2280         fprintf(out, "=================================================\n");
2281         fprintf(out, "#\n");
2282
2283         hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
2284
2285         fprintf(out, "\n");
2286         fprintf(out, "=================================================\n");
2287         fprintf(out, "      Shared Cache Line Distribution Pareto      \n");
2288         fprintf(out, "=================================================\n");
2289         fprintf(out, "#\n");
2290
2291         print_pareto(out);
2292 }
2293
2294 #ifdef HAVE_SLANG_SUPPORT
2295 static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2296 {
2297         u64 nr_entries = 0;
2298         struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2299
2300         while (nd) {
2301                 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2302
2303                 if (!he->filtered)
2304                         nr_entries++;
2305
2306                 nd = rb_next(nd);
2307         }
2308
2309         hb->nr_non_filtered_entries = nr_entries;
2310 }
2311
2312 struct c2c_cacheline_browser {
2313         struct hist_browser      hb;
2314         struct hist_entry       *he;
2315 };
2316
2317 static int
2318 perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2319                                   char *bf, size_t size)
2320 {
2321         struct c2c_cacheline_browser *cl_browser;
2322         struct hist_entry *he;
2323         uint64_t addr = 0;
2324
2325         cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2326         he = cl_browser->he;
2327
2328         if (he->mem_info)
2329                 addr = cl_address(he->mem_info->daddr.addr);
2330
2331         scnprintf(bf, size, "Cacheline 0x%lx", addr);
2332         return 0;
2333 }
2334
2335 static struct c2c_cacheline_browser*
2336 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2337 {
2338         struct c2c_cacheline_browser *browser;
2339
2340         browser = zalloc(sizeof(*browser));
2341         if (browser) {
2342                 hist_browser__init(&browser->hb, hists);
2343                 browser->hb.c2c_filter  = true;
2344                 browser->hb.title       = perf_c2c_cacheline_browser__title;
2345                 browser->he             = he;
2346         }
2347
2348         return browser;
2349 }
2350
2351 static int perf_c2c__browse_cacheline(struct hist_entry *he)
2352 {
2353         struct c2c_hist_entry *c2c_he;
2354         struct c2c_hists *c2c_hists;
2355         struct c2c_cacheline_browser *cl_browser;
2356         struct hist_browser *browser;
2357         int key = -1;
2358         static const char help[] =
2359         " ENTER         Toggle callchains (if present) \n"
2360         " n             Toggle Node details info \n"
2361         " s             Toggle full length of symbol and source line columns \n"
2362         " q             Return back to cacheline list \n";
2363
2364         if (!he)
2365                 return 0;
2366
2367         /* Display compact version first. */
2368         c2c.symbol_full = false;
2369
2370         c2c_he = container_of(he, struct c2c_hist_entry, he);
2371         c2c_hists = c2c_he->hists;
2372
2373         cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2374         if (cl_browser == NULL)
2375                 return -1;
2376
2377         browser = &cl_browser->hb;
2378
2379         /* reset abort key so that it can get Ctrl-C as a key */
2380         SLang_reset_tty();
2381         SLang_init_tty(0, 0, 0);
2382
2383         c2c_browser__update_nr_entries(browser);
2384
2385         while (1) {
2386                 key = hist_browser__run(browser, "? - help", true);
2387
2388                 switch (key) {
2389                 case 's':
2390                         c2c.symbol_full = !c2c.symbol_full;
2391                         break;
2392                 case 'n':
2393                         c2c.node_info = (c2c.node_info + 1) % 3;
2394                         setup_nodes_header();
2395                         break;
2396                 case 'q':
2397                         goto out;
2398                 case '?':
2399                         ui_browser__help_window(&browser->b, help);
2400                         break;
2401                 default:
2402                         break;
2403                 }
2404         }
2405
2406 out:
2407         free(cl_browser);
2408         return 0;
2409 }
2410
2411 static int perf_c2c_browser__title(struct hist_browser *browser,
2412                                    char *bf, size_t size)
2413 {
2414         scnprintf(bf, size,
2415                   "Shared Data Cache Line Table     "
2416                   "(%lu entries, sorted on %s HITMs)",
2417                   browser->nr_non_filtered_entries,
2418                   display_str[c2c.display]);
2419         return 0;
2420 }
2421
2422 static struct hist_browser*
2423 perf_c2c_browser__new(struct hists *hists)
2424 {
2425         struct hist_browser *browser = hist_browser__new(hists);
2426
2427         if (browser) {
2428                 browser->title = perf_c2c_browser__title;
2429                 browser->c2c_filter = true;
2430         }
2431
2432         return browser;
2433 }
2434
2435 static int perf_c2c__hists_browse(struct hists *hists)
2436 {
2437         struct hist_browser *browser;
2438         int key = -1;
2439         static const char help[] =
2440         " d             Display cacheline details \n"
2441         " ENTER         Toggle callchains (if present) \n"
2442         " q             Quit \n";
2443
2444         browser = perf_c2c_browser__new(hists);
2445         if (browser == NULL)
2446                 return -1;
2447
2448         /* reset abort key so that it can get Ctrl-C as a key */
2449         SLang_reset_tty();
2450         SLang_init_tty(0, 0, 0);
2451
2452         c2c_browser__update_nr_entries(browser);
2453
2454         while (1) {
2455                 key = hist_browser__run(browser, "? - help", true);
2456
2457                 switch (key) {
2458                 case 'q':
2459                         goto out;
2460                 case 'd':
2461                         perf_c2c__browse_cacheline(browser->he_selection);
2462                         break;
2463                 case '?':
2464                         ui_browser__help_window(&browser->b, help);
2465                         break;
2466                 default:
2467                         break;
2468                 }
2469         }
2470
2471 out:
2472         hist_browser__delete(browser);
2473         return 0;
2474 }
2475
2476 static void perf_c2c_display(struct perf_session *session)
2477 {
2478         if (use_browser == 0)
2479                 perf_c2c__hists_fprintf(stdout, session);
2480         else
2481                 perf_c2c__hists_browse(&c2c.hists.hists);
2482 }
2483 #else
2484 static void perf_c2c_display(struct perf_session *session)
2485 {
2486         use_browser = 0;
2487         perf_c2c__hists_fprintf(stdout, session);
2488 }
2489 #endif /* HAVE_SLANG_SUPPORT */
2490
2491 static char *fill_line(const char *orig, int len)
2492 {
2493         int i, j, olen = strlen(orig);
2494         char *buf;
2495
2496         buf = zalloc(len + 1);
2497         if (!buf)
2498                 return NULL;
2499
2500         j = len / 2 - olen / 2;
2501
2502         for (i = 0; i < j - 1; i++)
2503                 buf[i] = '-';
2504
2505         buf[i++] = ' ';
2506
2507         strcpy(buf + i, orig);
2508
2509         i += olen;
2510
2511         buf[i++] = ' ';
2512
2513         for (; i < len; i++)
2514                 buf[i] = '-';
2515
2516         return buf;
2517 }
2518
2519 static int ui_quirks(void)
2520 {
2521         const char *nodestr = "Data address";
2522         char *buf;
2523
2524         if (!c2c.use_stdio) {
2525                 dim_offset.width  = 5;
2526                 dim_offset.header = header_offset_tui;
2527                 nodestr = "CL";
2528         }
2529
2530         dim_percent_hitm.header = percent_hitm_header[c2c.display];
2531
2532         /* Fix the zero line for dcacheline column. */
2533         buf = fill_line("Cacheline", dim_dcacheline.width +
2534                                      dim_dcacheline_node.width +
2535                                      dim_dcacheline_count.width + 4);
2536         if (!buf)
2537                 return -ENOMEM;
2538
2539         dim_dcacheline.header.line[0].text = buf;
2540
2541         /* Fix the zero line for offset column. */
2542         buf = fill_line(nodestr, dim_offset.width +
2543                                  dim_offset_node.width +
2544                                  dim_dcacheline_count.width + 4);
2545         if (!buf)
2546                 return -ENOMEM;
2547
2548         dim_offset.header.line[0].text = buf;
2549
2550         return 0;
2551 }
2552
2553 #define CALLCHAIN_DEFAULT_OPT  "graph,0.5,caller,function,percent"
2554
2555 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2556                                 CALLCHAIN_REPORT_HELP
2557                                 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2558
2559 static int
2560 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2561 {
2562         struct callchain_param *callchain = opt->value;
2563
2564         callchain->enabled = !unset;
2565         /*
2566          * --no-call-graph
2567          */
2568         if (unset) {
2569                 symbol_conf.use_callchain = false;
2570                 callchain->mode = CHAIN_NONE;
2571                 return 0;
2572         }
2573
2574         return parse_callchain_report_opt(arg);
2575 }
2576
2577 static int setup_callchain(struct evlist *evlist)
2578 {
2579         u64 sample_type = perf_evlist__combined_sample_type(evlist);
2580         enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2581
2582         if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2583             (sample_type & PERF_SAMPLE_STACK_USER)) {
2584                 mode = CALLCHAIN_DWARF;
2585                 dwarf_callchain_users = true;
2586         } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2587                 mode = CALLCHAIN_LBR;
2588         else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2589                 mode = CALLCHAIN_FP;
2590
2591         if (!callchain_param.enabled &&
2592             callchain_param.mode != CHAIN_NONE &&
2593             mode != CALLCHAIN_NONE) {
2594                 symbol_conf.use_callchain = true;
2595                 if (callchain_register_param(&callchain_param) < 0) {
2596                         ui__error("Can't register callchain params.\n");
2597                         return -EINVAL;
2598                 }
2599         }
2600
2601         callchain_param.record_mode = mode;
2602         callchain_param.min_percent = 0;
2603         return 0;
2604 }
2605
2606 static int setup_display(const char *str)
2607 {
2608         const char *display = str ?: "tot";
2609
2610         if (!strcmp(display, "tot"))
2611                 c2c.display = DISPLAY_TOT;
2612         else if (!strcmp(display, "rmt"))
2613                 c2c.display = DISPLAY_RMT;
2614         else if (!strcmp(display, "lcl"))
2615                 c2c.display = DISPLAY_LCL;
2616         else {
2617                 pr_err("failed: unknown display type: %s\n", str);
2618                 return -1;
2619         }
2620
2621         return 0;
2622 }
2623
2624 #define for_each_token(__tok, __buf, __sep, __tmp)              \
2625         for (__tok = strtok_r(__buf, __sep, &__tmp); __tok;     \
2626              __tok = strtok_r(NULL,  __sep, &__tmp))
2627
2628 static int build_cl_output(char *cl_sort, bool no_source)
2629 {
2630         char *tok, *tmp, *buf = strdup(cl_sort);
2631         bool add_pid   = false;
2632         bool add_tid   = false;
2633         bool add_iaddr = false;
2634         bool add_sym   = false;
2635         bool add_dso   = false;
2636         bool add_src   = false;
2637
2638         if (!buf)
2639                 return -ENOMEM;
2640
2641         for_each_token(tok, buf, ",", tmp) {
2642                 if (!strcmp(tok, "tid")) {
2643                         add_tid = true;
2644                 } else if (!strcmp(tok, "pid")) {
2645                         add_pid = true;
2646                 } else if (!strcmp(tok, "iaddr")) {
2647                         add_iaddr = true;
2648                         add_sym   = true;
2649                         add_dso   = true;
2650                         add_src   = no_source ? false : true;
2651                 } else if (!strcmp(tok, "dso")) {
2652                         add_dso = true;
2653                 } else if (strcmp(tok, "offset")) {
2654                         pr_err("unrecognized sort token: %s\n", tok);
2655                         return -EINVAL;
2656                 }
2657         }
2658
2659         if (asprintf(&c2c.cl_output,
2660                 "%s%s%s%s%s%s%s%s%s%s",
2661                 c2c.use_stdio ? "cl_num_empty," : "",
2662                 "percent_rmt_hitm,"
2663                 "percent_lcl_hitm,"
2664                 "percent_stores_l1hit,"
2665                 "percent_stores_l1miss,"
2666                 "offset,offset_node,dcacheline_count,",
2667                 add_pid   ? "pid," : "",
2668                 add_tid   ? "tid," : "",
2669                 add_iaddr ? "iaddr," : "",
2670                 "mean_rmt,"
2671                 "mean_lcl,"
2672                 "mean_load,"
2673                 "tot_recs,"
2674                 "cpucnt,",
2675                 add_sym ? "symbol," : "",
2676                 add_dso ? "dso," : "",
2677                 add_src ? "cl_srcline," : "",
2678                 "node") < 0)
2679                 return -ENOMEM;
2680
2681         c2c.show_src = add_src;
2682
2683         free(buf);
2684         return 0;
2685 }
2686
2687 static int setup_coalesce(const char *coalesce, bool no_source)
2688 {
2689         const char *c = coalesce ?: coalesce_default;
2690
2691         if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2692                 return -ENOMEM;
2693
2694         if (build_cl_output(c2c.cl_sort, no_source))
2695                 return -1;
2696
2697         if (asprintf(&c2c.cl_resort, "offset,%s",
2698                      c2c.display == DISPLAY_TOT ?
2699                      "tot_hitm" :
2700                      c2c.display == DISPLAY_RMT ?
2701                      "rmt_hitm,lcl_hitm" :
2702                      "lcl_hitm,rmt_hitm") < 0)
2703                 return -ENOMEM;
2704
2705         pr_debug("coalesce sort   fields: %s\n", c2c.cl_sort);
2706         pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2707         pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2708         return 0;
2709 }
2710
2711 static int perf_c2c__report(int argc, const char **argv)
2712 {
2713         struct perf_session *session;
2714         struct ui_progress prog;
2715         struct perf_data data = {
2716                 .mode = PERF_DATA_MODE_READ,
2717         };
2718         char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
2719         const char *display = NULL;
2720         const char *coalesce = NULL;
2721         bool no_source = false;
2722         const struct option options[] = {
2723         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2724                    "file", "vmlinux pathname"),
2725         OPT_STRING('i', "input", &input_name, "file",
2726                    "the input file to process"),
2727         OPT_INCR('N', "node-info", &c2c.node_info,
2728                  "show extra node info in report (repeat for more info)"),
2729 #ifdef HAVE_SLANG_SUPPORT
2730         OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2731 #endif
2732         OPT_BOOLEAN(0, "stats", &c2c.stats_only,
2733                     "Display only statistic tables (implies --stdio)"),
2734         OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2735                     "Display full length of symbols"),
2736         OPT_BOOLEAN(0, "no-source", &no_source,
2737                     "Do not display Source Line column"),
2738         OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2739                     "Show all captured HITM lines."),
2740         OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2741                              "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2742                              callchain_help, &parse_callchain_opt,
2743                              callchain_default_opt),
2744         OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
2745         OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2746                    "coalesce fields: pid,tid,iaddr,dso"),
2747         OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2748         OPT_PARENT(c2c_options),
2749         OPT_END()
2750         };
2751         int err = 0;
2752
2753         argc = parse_options(argc, argv, options, report_c2c_usage,
2754                              PARSE_OPT_STOP_AT_NON_OPTION);
2755         if (argc)
2756                 usage_with_options(report_c2c_usage, options);
2757
2758         if (c2c.stats_only)
2759                 c2c.use_stdio = true;
2760
2761         if (!input_name || !strlen(input_name))
2762                 input_name = "perf.data";
2763
2764         data.path  = input_name;
2765         data.force = symbol_conf.force;
2766
2767         err = setup_display(display);
2768         if (err)
2769                 goto out;
2770
2771         err = setup_coalesce(coalesce, no_source);
2772         if (err) {
2773                 pr_debug("Failed to initialize hists\n");
2774                 goto out;
2775         }
2776
2777         err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
2778         if (err) {
2779                 pr_debug("Failed to initialize hists\n");
2780                 goto out;
2781         }
2782
2783         session = perf_session__new(&data, 0, &c2c.tool);
2784         if (session == NULL) {
2785                 pr_debug("No memory for session\n");
2786                 goto out;
2787         }
2788
2789         err = setup_nodes(session);
2790         if (err) {
2791                 pr_err("Failed setup nodes\n");
2792                 goto out;
2793         }
2794
2795         err = mem2node__init(&c2c.mem2node, &session->header.env);
2796         if (err)
2797                 goto out_session;
2798
2799         err = setup_callchain(session->evlist);
2800         if (err)
2801                 goto out_mem2node;
2802
2803         if (symbol__init(&session->header.env) < 0)
2804                 goto out_mem2node;
2805
2806         /* No pipe support at the moment. */
2807         if (perf_data__is_pipe(session->data)) {
2808                 pr_debug("No pipe support at the moment.\n");
2809                 goto out_mem2node;
2810         }
2811
2812         if (c2c.use_stdio)
2813                 use_browser = 0;
2814         else
2815                 use_browser = 1;
2816
2817         setup_browser(false);
2818
2819         err = perf_session__process_events(session);
2820         if (err) {
2821                 pr_err("failed to process sample\n");
2822                 goto out_mem2node;
2823         }
2824
2825         c2c_hists__reinit(&c2c.hists,
2826                         "cl_idx,"
2827                         "dcacheline,"
2828                         "dcacheline_node,"
2829                         "dcacheline_count,"
2830                         "tot_recs,"
2831                         "percent_hitm,"
2832                         "tot_hitm,lcl_hitm,rmt_hitm,"
2833                         "stores,stores_l1hit,stores_l1miss,"
2834                         "dram_lcl,dram_rmt,"
2835                         "ld_llcmiss,"
2836                         "tot_loads,"
2837                         "ld_fbhit,ld_l1hit,ld_l2hit,"
2838                         "ld_lclhit,ld_rmthit",
2839                         c2c.display == DISPLAY_TOT ? "tot_hitm" :
2840                         c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
2841                         );
2842
2843         ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2844
2845         hists__collapse_resort(&c2c.hists.hists, NULL);
2846         hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2847         hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
2848
2849         ui_progress__finish();
2850
2851         if (ui_quirks()) {
2852                 pr_err("failed to setup UI\n");
2853                 goto out_mem2node;
2854         }
2855
2856         perf_c2c_display(session);
2857
2858 out_mem2node:
2859         mem2node__exit(&c2c.mem2node);
2860 out_session:
2861         perf_session__delete(session);
2862 out:
2863         return err;
2864 }
2865
2866 static int parse_record_events(const struct option *opt,
2867                                const char *str, int unset __maybe_unused)
2868 {
2869         bool *event_set = (bool *) opt->value;
2870
2871         *event_set = true;
2872         return perf_mem_events__parse(str);
2873 }
2874
2875
2876 static const char * const __usage_record[] = {
2877         "perf c2c record [<options>] [<command>]",
2878         "perf c2c record [<options>] -- <command> [<options>]",
2879         NULL
2880 };
2881
2882 static const char * const *record_mem_usage = __usage_record;
2883
2884 static int perf_c2c__record(int argc, const char **argv)
2885 {
2886         int rec_argc, i = 0, j;
2887         const char **rec_argv;
2888         int ret;
2889         bool all_user = false, all_kernel = false;
2890         bool event_set = false;
2891         struct option options[] = {
2892         OPT_CALLBACK('e', "event", &event_set, "event",
2893                      "event selector. Use 'perf mem record -e list' to list available events",
2894                      parse_record_events),
2895         OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2896         OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2897         OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
2898         OPT_PARENT(c2c_options),
2899         OPT_END()
2900         };
2901
2902         if (perf_mem_events__init()) {
2903                 pr_err("failed: memory events not supported\n");
2904                 return -1;
2905         }
2906
2907         argc = parse_options(argc, argv, options, record_mem_usage,
2908                              PARSE_OPT_KEEP_UNKNOWN);
2909
2910         rec_argc = argc + 11; /* max number of arguments */
2911         rec_argv = calloc(rec_argc + 1, sizeof(char *));
2912         if (!rec_argv)
2913                 return -1;
2914
2915         rec_argv[i++] = "record";
2916
2917         if (!event_set) {
2918                 perf_mem_events[PERF_MEM_EVENTS__LOAD].record  = true;
2919                 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2920         }
2921
2922         if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2923                 rec_argv[i++] = "-W";
2924
2925         rec_argv[i++] = "-d";
2926         rec_argv[i++] = "--phys-data";
2927         rec_argv[i++] = "--sample-cpu";
2928
2929         for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2930                 if (!perf_mem_events[j].record)
2931                         continue;
2932
2933                 if (!perf_mem_events[j].supported) {
2934                         pr_err("failed: event '%s' not supported\n",
2935                                perf_mem_events[j].name);
2936                         free(rec_argv);
2937                         return -1;
2938                 }
2939
2940                 rec_argv[i++] = "-e";
2941                 rec_argv[i++] = perf_mem_events__name(j);
2942         };
2943
2944         if (all_user)
2945                 rec_argv[i++] = "--all-user";
2946
2947         if (all_kernel)
2948                 rec_argv[i++] = "--all-kernel";
2949
2950         for (j = 0; j < argc; j++, i++)
2951                 rec_argv[i] = argv[j];
2952
2953         if (verbose > 0) {
2954                 pr_debug("calling: ");
2955
2956                 j = 0;
2957
2958                 while (rec_argv[j]) {
2959                         pr_debug("%s ", rec_argv[j]);
2960                         j++;
2961                 }
2962                 pr_debug("\n");
2963         }
2964
2965         ret = cmd_record(i, rec_argv);
2966         free(rec_argv);
2967         return ret;
2968 }
2969
2970 int cmd_c2c(int argc, const char **argv)
2971 {
2972         argc = parse_options(argc, argv, c2c_options, c2c_usage,
2973                              PARSE_OPT_STOP_AT_NON_OPTION);
2974
2975         if (!argc)
2976                 usage_with_options(c2c_usage, c2c_options);
2977
2978         if (!strncmp(argv[0], "rec", 3)) {
2979                 return perf_c2c__record(argc, argv);
2980         } else if (!strncmp(argv[0], "rep", 3)) {
2981                 return perf_c2c__report(argc, argv);
2982         } else {
2983                 usage_with_options(c2c_usage, c2c_options);
2984         }
2985
2986         return 0;
2987 }