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