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