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