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