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