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