]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/builtin-c2c.c
perf c2c report: Add 'node' sort key
[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"
903a6f15 16
c75540e3
JO
17struct c2c_hists {
18 struct hists hists;
19 struct perf_hpp_list list;
b2252ae6 20 struct c2c_stats stats;
c75540e3
JO
21};
22
78b27543
JO
23struct c2c_hist_entry {
24 struct c2c_hists *hists;
b2252ae6 25 struct c2c_stats stats;
1e181b92
JO
26 unsigned long *cpuset;
27 struct c2c_stats *node_stats;
78b27543
JO
28 /*
29 * must be at the end,
30 * because of its callchain dynamic entry
31 */
32 struct hist_entry he;
33};
34
903a6f15 35struct perf_c2c {
c75540e3
JO
36 struct perf_tool tool;
37 struct c2c_hists hists;
1e181b92
JO
38
39 unsigned long **nodes;
40 int nodes_cnt;
41 int cpus_cnt;
42 int *cpu2node;
43 int node_info;
903a6f15
JO
44};
45
46static struct perf_c2c c2c;
7aef3bf3 47
78b27543
JO
48static void *c2c_he_zalloc(size_t size)
49{
50 struct c2c_hist_entry *c2c_he;
51
52 c2c_he = zalloc(size + sizeof(*c2c_he));
53 if (!c2c_he)
54 return NULL;
55
1e181b92
JO
56 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
57 if (!c2c_he->cpuset)
58 return NULL;
59
60 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
61 if (!c2c_he->node_stats)
62 return NULL;
63
78b27543
JO
64 return &c2c_he->he;
65}
66
67static void c2c_he_free(void *he)
68{
69 struct c2c_hist_entry *c2c_he;
70
71 c2c_he = container_of(he, struct c2c_hist_entry, he);
72 if (c2c_he->hists) {
73 hists__delete_entries(&c2c_he->hists->hists);
74 free(c2c_he->hists);
75 }
76
1e181b92
JO
77 free(c2c_he->cpuset);
78 free(c2c_he->node_stats);
78b27543
JO
79 free(c2c_he);
80}
81
82static struct hist_entry_ops c2c_entry_ops = {
83 .new = c2c_he_zalloc,
84 .free = c2c_he_free,
85};
86
ec06f9b9
JO
87static int c2c_hists__init(struct c2c_hists *hists,
88 const char *sort);
89
b2252ae6
JO
90static struct c2c_hists*
91he__get_c2c_hists(struct hist_entry *he,
92 const char *sort)
ec06f9b9
JO
93{
94 struct c2c_hist_entry *c2c_he;
95 struct c2c_hists *hists;
96 int ret;
97
98 c2c_he = container_of(he, struct c2c_hist_entry, he);
99 if (c2c_he->hists)
b2252ae6 100 return c2c_he->hists;
ec06f9b9
JO
101
102 hists = c2c_he->hists = zalloc(sizeof(*hists));
103 if (!hists)
104 return NULL;
105
106 ret = c2c_hists__init(hists, sort);
107 if (ret) {
108 free(hists);
109 return NULL;
110 }
111
b2252ae6 112 return hists;
ec06f9b9
JO
113}
114
1e181b92
JO
115static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
116 struct perf_sample *sample)
117{
118 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
119 "WARNING: no sample cpu value"))
120 return;
121
122 set_bit(sample->cpu, c2c_he->cpuset);
123}
124
78b27543
JO
125static int process_sample_event(struct perf_tool *tool __maybe_unused,
126 union perf_event *event,
127 struct perf_sample *sample,
128 struct perf_evsel *evsel __maybe_unused,
129 struct machine *machine)
130{
b2252ae6
JO
131 struct c2c_hists *c2c_hists = &c2c.hists;
132 struct c2c_hist_entry *c2c_he;
133 struct c2c_stats stats = { .nr_entries = 0, };
78b27543
JO
134 struct hist_entry *he;
135 struct addr_location al;
ec06f9b9 136 struct mem_info *mi, *mi_dup;
78b27543
JO
137 int ret;
138
139 if (machine__resolve(machine, &al, sample) < 0) {
140 pr_debug("problem processing %d event, skipping it.\n",
141 event->header.type);
142 return -1;
143 }
144
145 mi = sample__resolve_mem(sample, &al);
146 if (mi == NULL)
147 return -ENOMEM;
148
ec06f9b9
JO
149 mi_dup = memdup(mi, sizeof(*mi));
150 if (!mi_dup)
151 goto free_mi;
152
b2252ae6
JO
153 c2c_decode_stats(&stats, mi);
154
155 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
78b27543
JO
156 &al, NULL, NULL, mi,
157 sample, true);
ec06f9b9
JO
158 if (he == NULL)
159 goto free_mi_dup;
78b27543 160
b2252ae6
JO
161 c2c_he = container_of(he, struct c2c_hist_entry, he);
162 c2c_add_stats(&c2c_he->stats, &stats);
163 c2c_add_stats(&c2c_hists->stats, &stats);
164
1e181b92
JO
165 c2c_he__set_cpu(c2c_he, sample);
166
b2252ae6 167 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
78b27543
JO
168 ret = hist_entry__append_callchain(he, sample);
169
ec06f9b9 170 if (!ret) {
1e181b92
JO
171 /*
172 * There's already been warning about missing
173 * sample's cpu value. Let's account all to
174 * node 0 in this case, without any further
175 * warning.
176 *
177 * Doing node stats only for single callchain data.
178 */
179 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
180 int node = c2c.cpu2node[cpu];
181
ec06f9b9
JO
182 mi = mi_dup;
183
184 mi_dup = memdup(mi, sizeof(*mi));
185 if (!mi_dup)
186 goto free_mi;
187
b2252ae6
JO
188 c2c_hists = he__get_c2c_hists(he, "offset");
189 if (!c2c_hists)
ec06f9b9
JO
190 goto free_mi_dup;
191
b2252ae6 192 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
ec06f9b9
JO
193 &al, NULL, NULL, mi,
194 sample, true);
195 if (he == NULL)
196 goto free_mi_dup;
197
b2252ae6
JO
198 c2c_he = container_of(he, struct c2c_hist_entry, he);
199 c2c_add_stats(&c2c_he->stats, &stats);
200 c2c_add_stats(&c2c_hists->stats, &stats);
1e181b92
JO
201 c2c_add_stats(&c2c_he->node_stats[node], &stats);
202
203 c2c_he__set_cpu(c2c_he, sample);
b2252ae6
JO
204
205 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
ec06f9b9
JO
206 ret = hist_entry__append_callchain(he, sample);
207 }
208
209out:
78b27543
JO
210 addr_location__put(&al);
211 return ret;
ec06f9b9
JO
212
213free_mi_dup:
214 free(mi_dup);
215free_mi:
216 free(mi);
217 ret = -ENOMEM;
218 goto out;
78b27543
JO
219}
220
221static struct perf_c2c c2c = {
222 .tool = {
223 .sample = process_sample_event,
224 .mmap = perf_event__process_mmap,
225 .mmap2 = perf_event__process_mmap2,
226 .comm = perf_event__process_comm,
227 .exit = perf_event__process_exit,
228 .fork = perf_event__process_fork,
229 .lost = perf_event__process_lost,
230 .ordered_events = true,
231 .ordering_requires_timestamps = true,
232 },
233};
234
7aef3bf3 235static const char * const c2c_usage[] = {
903a6f15 236 "perf c2c {record|report}",
7aef3bf3
JO
237 NULL
238};
239
903a6f15
JO
240static const char * const __usage_report[] = {
241 "perf c2c report",
242 NULL
243};
244
245static const char * const *report_c2c_usage = __usage_report;
246
c75540e3
JO
247#define C2C_HEADER_MAX 2
248
249struct c2c_header {
250 struct {
251 const char *text;
252 int span;
253 } line[C2C_HEADER_MAX];
254};
255
256struct c2c_dimension {
257 struct c2c_header header;
258 const char *name;
259 int width;
8d3f938d 260 struct sort_entry *se;
c75540e3
JO
261
262 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
263 struct hist_entry *, struct hist_entry *);
264 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
265 struct hist_entry *he);
266 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
267 struct hist_entry *he);
268};
269
270struct c2c_fmt {
271 struct perf_hpp_fmt fmt;
272 struct c2c_dimension *dim;
273};
274
275static int c2c_width(struct perf_hpp_fmt *fmt,
276 struct perf_hpp *hpp __maybe_unused,
277 struct hists *hists __maybe_unused)
278{
279 struct c2c_fmt *c2c_fmt;
8d3f938d 280 struct c2c_dimension *dim;
c75540e3
JO
281
282 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
8d3f938d
JO
283 dim = c2c_fmt->dim;
284
285 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
286 c2c_fmt->dim->width;
c75540e3
JO
287}
288
289static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
8d3f938d 290 struct hists *hists, int line, int *span)
c75540e3 291{
8d3f938d 292 struct perf_hpp_list *hpp_list = hists->hpp_list;
c75540e3
JO
293 struct c2c_fmt *c2c_fmt;
294 struct c2c_dimension *dim;
8d3f938d
JO
295 const char *text = NULL;
296 int width = c2c_width(fmt, hpp, hists);
c75540e3
JO
297
298 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
299 dim = c2c_fmt->dim;
300
8d3f938d
JO
301 if (dim->se) {
302 text = dim->header.line[line].text;
303 /* Use the last line from sort_entry if not defined. */
304 if (!text && (line == hpp_list->nr_header_lines - 1))
305 text = dim->se->se_header;
c75540e3 306 } else {
8d3f938d
JO
307 text = dim->header.line[line].text;
308
309 if (*span) {
310 (*span)--;
311 return 0;
312 } else {
313 *span = dim->header.line[line].span;
314 }
c75540e3
JO
315 }
316
8d3f938d
JO
317 if (text == NULL)
318 text = "";
319
320 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
c75540e3
JO
321}
322
cbb88500
JO
323#define HEX_STR(__s, __v) \
324({ \
325 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
326 __s; \
327})
328
329static int64_t
330dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
331 struct hist_entry *left, struct hist_entry *right)
332{
333 return sort__dcacheline_cmp(left, right);
334}
335
336static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
337 struct hist_entry *he)
338{
339 uint64_t addr = 0;
340 int width = c2c_width(fmt, hpp, he->hists);
341 char buf[20];
342
343 if (he->mem_info)
344 addr = cl_address(he->mem_info->daddr.addr);
345
346 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
347}
348
48acdebd
JO
349static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
350 struct hist_entry *he)
351{
352 uint64_t addr = 0;
353 int width = c2c_width(fmt, hpp, he->hists);
354 char buf[20];
355
356 if (he->mem_info)
357 addr = cl_offset(he->mem_info->daddr.al_addr);
358
359 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
360}
361
362static int64_t
363offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
364 struct hist_entry *left, struct hist_entry *right)
365{
366 uint64_t l = 0, r = 0;
367
368 if (left->mem_info)
369 l = cl_offset(left->mem_info->daddr.addr);
370 if (right->mem_info)
371 r = cl_offset(right->mem_info->daddr.addr);
372
373 return (int64_t)(r - l);
374}
375
43575a95
JO
376static int
377iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
378 struct hist_entry *he)
379{
380 uint64_t addr = 0;
381 int width = c2c_width(fmt, hpp, he->hists);
382 char buf[20];
383
384 if (he->mem_info)
385 addr = he->mem_info->iaddr.addr;
386
387 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
388}
389
390static int64_t
391iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
392 struct hist_entry *left, struct hist_entry *right)
393{
394 return sort__iaddr_cmp(left, right);
395}
396
97cb486e
JO
397static int
398tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
399 struct hist_entry *he)
400{
401 struct c2c_hist_entry *c2c_he;
402 int width = c2c_width(fmt, hpp, he->hists);
403 unsigned int tot_hitm;
404
405 c2c_he = container_of(he, struct c2c_hist_entry, he);
406 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
407
408 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
409}
410
411static int64_t
412tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
413 struct hist_entry *left, struct hist_entry *right)
414{
415 struct c2c_hist_entry *c2c_left;
416 struct c2c_hist_entry *c2c_right;
417 unsigned int tot_hitm_left;
418 unsigned int tot_hitm_right;
419
420 c2c_left = container_of(left, struct c2c_hist_entry, he);
421 c2c_right = container_of(right, struct c2c_hist_entry, he);
422
423 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
424 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
425
426 return tot_hitm_left - tot_hitm_right;
427}
428
429#define STAT_FN_ENTRY(__f) \
430static int \
431__f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
432 struct hist_entry *he) \
433{ \
434 struct c2c_hist_entry *c2c_he; \
435 int width = c2c_width(fmt, hpp, he->hists); \
436 \
437 c2c_he = container_of(he, struct c2c_hist_entry, he); \
438 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
439 c2c_he->stats.__f); \
440}
441
442#define STAT_FN_CMP(__f) \
443static int64_t \
444__f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
445 struct hist_entry *left, struct hist_entry *right) \
446{ \
447 struct c2c_hist_entry *c2c_left, *c2c_right; \
448 \
449 c2c_left = container_of(left, struct c2c_hist_entry, he); \
450 c2c_right = container_of(right, struct c2c_hist_entry, he); \
451 return c2c_left->stats.__f - c2c_right->stats.__f; \
452}
453
454#define STAT_FN(__f) \
455 STAT_FN_ENTRY(__f) \
456 STAT_FN_CMP(__f)
457
458STAT_FN(rmt_hitm)
459STAT_FN(lcl_hitm)
0f18896d
JO
460STAT_FN(store)
461STAT_FN(st_l1hit)
462STAT_FN(st_l1miss)
1295f685
JO
463STAT_FN(ld_fbhit)
464STAT_FN(ld_l1hit)
465STAT_FN(ld_l2hit)
4d08910c
JO
466STAT_FN(ld_llchit)
467STAT_FN(rmt_hit)
97cb486e 468
04402d20
JO
469static uint64_t llc_miss(struct c2c_stats *stats)
470{
471 uint64_t llcmiss;
472
473 llcmiss = stats->lcl_dram +
474 stats->rmt_dram +
475 stats->rmt_hitm +
476 stats->rmt_hit;
477
478 return llcmiss;
479}
480
481static int
482ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
483 struct hist_entry *he)
484{
485 struct c2c_hist_entry *c2c_he;
486 int width = c2c_width(fmt, hpp, he->hists);
487
488 c2c_he = container_of(he, struct c2c_hist_entry, he);
489
490 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
491 llc_miss(&c2c_he->stats));
492}
493
494static int64_t
495ld_llcmiss_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
501 c2c_left = container_of(left, struct c2c_hist_entry, he);
502 c2c_right = container_of(right, struct c2c_hist_entry, he);
503
504 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
505}
506
01b84d76
JO
507static uint64_t total_records(struct c2c_stats *stats)
508{
509 uint64_t lclmiss, ldcnt, total;
510
511 lclmiss = stats->lcl_dram +
512 stats->rmt_dram +
513 stats->rmt_hitm +
514 stats->rmt_hit;
515
516 ldcnt = lclmiss +
517 stats->ld_fbhit +
518 stats->ld_l1hit +
519 stats->ld_l2hit +
520 stats->ld_llchit +
521 stats->lcl_hitm;
522
523 total = ldcnt +
524 stats->st_l1hit +
525 stats->st_l1miss;
526
527 return total;
528}
529
530static int
531tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
532 struct hist_entry *he)
533{
534 struct c2c_hist_entry *c2c_he;
535 int width = c2c_width(fmt, hpp, he->hists);
536 uint64_t tot_recs;
537
538 c2c_he = container_of(he, struct c2c_hist_entry, he);
539 tot_recs = total_records(&c2c_he->stats);
540
541 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
542}
543
544static int64_t
545tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
546 struct hist_entry *left, struct hist_entry *right)
547{
548 struct c2c_hist_entry *c2c_left;
549 struct c2c_hist_entry *c2c_right;
550 uint64_t tot_recs_left;
551 uint64_t tot_recs_right;
552
553 c2c_left = container_of(left, struct c2c_hist_entry, he);
554 c2c_right = container_of(right, struct c2c_hist_entry, he);
555
556 tot_recs_left = total_records(&c2c_left->stats);
557 tot_recs_right = total_records(&c2c_right->stats);
558
559 return tot_recs_left - tot_recs_right;
560}
561
55177c4e
JO
562static uint64_t total_loads(struct c2c_stats *stats)
563{
564 uint64_t lclmiss, ldcnt;
565
566 lclmiss = stats->lcl_dram +
567 stats->rmt_dram +
568 stats->rmt_hitm +
569 stats->rmt_hit;
570
571 ldcnt = lclmiss +
572 stats->ld_fbhit +
573 stats->ld_l1hit +
574 stats->ld_l2hit +
575 stats->ld_llchit +
576 stats->lcl_hitm;
577
578 return ldcnt;
579}
580
581static int
582tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
583 struct hist_entry *he)
584{
585 struct c2c_hist_entry *c2c_he;
586 int width = c2c_width(fmt, hpp, he->hists);
587 uint64_t tot_recs;
588
589 c2c_he = container_of(he, struct c2c_hist_entry, he);
590 tot_recs = total_loads(&c2c_he->stats);
591
592 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
593}
594
595static int64_t
596tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
597 struct hist_entry *left, struct hist_entry *right)
598{
599 struct c2c_hist_entry *c2c_left;
600 struct c2c_hist_entry *c2c_right;
601 uint64_t tot_recs_left;
602 uint64_t tot_recs_right;
603
604 c2c_left = container_of(left, struct c2c_hist_entry, he);
605 c2c_right = container_of(right, struct c2c_hist_entry, he);
606
607 tot_recs_left = total_loads(&c2c_left->stats);
608 tot_recs_right = total_loads(&c2c_right->stats);
609
610 return tot_recs_left - tot_recs_right;
611}
612
f0c50c15
JO
613typedef double (get_percent_cb)(struct c2c_hist_entry *);
614
615static int
616percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
617 struct hist_entry *he, get_percent_cb get_percent)
618{
619 struct c2c_hist_entry *c2c_he;
620 int width = c2c_width(fmt, hpp, he->hists);
621 double per;
622
623 c2c_he = container_of(he, struct c2c_hist_entry, he);
624 per = get_percent(c2c_he);
625
626 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
627}
628
629static double percent_hitm(struct c2c_hist_entry *c2c_he)
630{
631 struct c2c_hists *hists;
632 struct c2c_stats *stats;
633 struct c2c_stats *total;
634 int tot, st;
635 double p;
636
637 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
638 stats = &c2c_he->stats;
639 total = &hists->stats;
640
641 st = stats->rmt_hitm;
642 tot = total->rmt_hitm;
643
644 p = tot ? (double) st / tot : 0;
645
646 return 100 * p;
647}
648
649#define PERC_STR(__s, __v) \
650({ \
651 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
652 __s; \
653})
654
655static int
656percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
657 struct hist_entry *he)
658{
659 struct c2c_hist_entry *c2c_he;
660 int width = c2c_width(fmt, hpp, he->hists);
661 char buf[10];
662 double per;
663
664 c2c_he = container_of(he, struct c2c_hist_entry, he);
665 per = percent_hitm(c2c_he);
666 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
667}
668
669static int
670percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
671 struct hist_entry *he)
672{
673 return percent_color(fmt, hpp, he, percent_hitm);
674}
675
676static int64_t
677percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
678 struct hist_entry *left, struct hist_entry *right)
679{
680 struct c2c_hist_entry *c2c_left;
681 struct c2c_hist_entry *c2c_right;
682 double per_left;
683 double per_right;
684
685 c2c_left = container_of(left, struct c2c_hist_entry, he);
686 c2c_right = container_of(right, struct c2c_hist_entry, he);
687
688 per_left = percent_hitm(c2c_left);
689 per_right = percent_hitm(c2c_right);
690
691 return per_left - per_right;
692}
693
9cb3500a
JO
694static struct c2c_stats *he_stats(struct hist_entry *he)
695{
696 struct c2c_hist_entry *c2c_he;
697
698 c2c_he = container_of(he, struct c2c_hist_entry, he);
699 return &c2c_he->stats;
700}
701
702static struct c2c_stats *total_stats(struct hist_entry *he)
703{
704 struct c2c_hists *hists;
705
706 hists = container_of(he->hists, struct c2c_hists, hists);
707 return &hists->stats;
708}
709
710static double percent(int st, int tot)
711{
712 return tot ? 100. * (double) st / (double) tot : 0;
713}
714
715#define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
716
717#define PERCENT_FN(__f) \
718static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
719{ \
720 struct c2c_hists *hists; \
721 \
722 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
723 return percent(c2c_he->stats.__f, hists->stats.__f); \
724}
725
726PERCENT_FN(rmt_hitm)
727PERCENT_FN(lcl_hitm)
728PERCENT_FN(st_l1hit)
729PERCENT_FN(st_l1miss)
730
731static int
732percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
733 struct hist_entry *he)
734{
735 int width = c2c_width(fmt, hpp, he->hists);
736 double per = PERCENT(he, rmt_hitm);
737 char buf[10];
738
739 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
740}
741
742static int
743percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
744 struct hist_entry *he)
745{
746 return percent_color(fmt, hpp, he, percent_rmt_hitm);
747}
748
749static int64_t
750percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
751 struct hist_entry *left, struct hist_entry *right)
752{
753 double per_left;
754 double per_right;
755
756 per_left = PERCENT(left, lcl_hitm);
757 per_right = PERCENT(right, lcl_hitm);
758
759 return per_left - per_right;
760}
761
762static int
763percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
764 struct hist_entry *he)
765{
766 int width = c2c_width(fmt, hpp, he->hists);
767 double per = PERCENT(he, lcl_hitm);
768 char buf[10];
769
770 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
771}
772
773static int
774percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
775 struct hist_entry *he)
776{
777 return percent_color(fmt, hpp, he, percent_lcl_hitm);
778}
779
780static int64_t
781percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
782 struct hist_entry *left, struct hist_entry *right)
783{
784 double per_left;
785 double per_right;
786
787 per_left = PERCENT(left, lcl_hitm);
788 per_right = PERCENT(right, lcl_hitm);
789
790 return per_left - per_right;
791}
792
793static int
794percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
795 struct hist_entry *he)
796{
797 int width = c2c_width(fmt, hpp, he->hists);
798 double per = PERCENT(he, st_l1hit);
799 char buf[10];
800
801 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
802}
803
804static int
805percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
806 struct hist_entry *he)
807{
808 return percent_color(fmt, hpp, he, percent_st_l1hit);
809}
810
811static int64_t
812percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
813 struct hist_entry *left, struct hist_entry *right)
814{
815 double per_left;
816 double per_right;
817
818 per_left = PERCENT(left, st_l1hit);
819 per_right = PERCENT(right, st_l1hit);
820
821 return per_left - per_right;
822}
823
824static int
825percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
826 struct hist_entry *he)
827{
828 int width = c2c_width(fmt, hpp, he->hists);
829 double per = PERCENT(he, st_l1miss);
830 char buf[10];
831
832 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
833}
834
835static int
836percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
837 struct hist_entry *he)
838{
839 return percent_color(fmt, hpp, he, percent_st_l1miss);
840}
841
842static int64_t
843percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
844 struct hist_entry *left, struct hist_entry *right)
845{
846 double per_left;
847 double per_right;
848
849 per_left = PERCENT(left, st_l1miss);
850 per_right = PERCENT(right, st_l1miss);
851
852 return per_left - per_right;
853}
854
6c70f54c
JO
855STAT_FN(lcl_dram)
856STAT_FN(rmt_dram)
857
36d3deb9
JO
858static int
859pid_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
864 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
865}
866
867static int64_t
868pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
869 struct hist_entry *left, struct hist_entry *right)
870{
871 return left->thread->pid_ - right->thread->pid_;
872}
873
1e181b92
JO
874static int64_t
875empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
876 struct hist_entry *left __maybe_unused,
877 struct hist_entry *right __maybe_unused)
878{
879 return 0;
880}
881
882static int
883node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
884 struct hist_entry *he)
885{
886 struct c2c_hist_entry *c2c_he;
887 bool first = true;
888 int node;
889 int ret = 0;
890
891 c2c_he = container_of(he, struct c2c_hist_entry, he);
892
893 for (node = 0; node < c2c.nodes_cnt; node++) {
894 DECLARE_BITMAP(set, c2c.cpus_cnt);
895
896 bitmap_zero(set, c2c.cpus_cnt);
897 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
898
899 if (!bitmap_weight(set, c2c.cpus_cnt)) {
900 if (c2c.node_info == 1) {
901 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
902 advance_hpp(hpp, ret);
903 }
904 continue;
905 }
906
907 if (!first) {
908 ret = scnprintf(hpp->buf, hpp->size, " ");
909 advance_hpp(hpp, ret);
910 }
911
912 switch (c2c.node_info) {
913 case 0:
914 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
915 advance_hpp(hpp, ret);
916 break;
917 case 1:
918 {
919 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
920 struct c2c_stats *stats = &c2c_he->node_stats[node];
921
922 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
923 advance_hpp(hpp, ret);
924
925
926 if (c2c_he->stats.rmt_hitm > 0) {
927 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ",
928 percent(stats->rmt_hitm, c2c_he->stats.rmt_hitm));
929 } else {
930 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a");
931 }
932
933 advance_hpp(hpp, ret);
934
935 if (c2c_he->stats.store > 0) {
936 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
937 percent(stats->store, c2c_he->stats.store));
938 } else {
939 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
940 }
941
942 advance_hpp(hpp, ret);
943 break;
944 }
945 case 2:
946 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
947 advance_hpp(hpp, ret);
948
949 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
950 advance_hpp(hpp, ret);
951
952 ret = scnprintf(hpp->buf, hpp->size, "}");
953 advance_hpp(hpp, ret);
954 break;
955 default:
956 break;
957 }
958
959 first = false;
960 }
961
962 return 0;
963}
964
600a8cf4
JO
965#define HEADER_LOW(__h) \
966 { \
967 .line[1] = { \
968 .text = __h, \
969 }, \
970 }
971
972#define HEADER_BOTH(__h0, __h1) \
973 { \
974 .line[0] = { \
975 .text = __h0, \
976 }, \
977 .line[1] = { \
978 .text = __h1, \
979 }, \
980 }
981
982#define HEADER_SPAN(__h0, __h1, __s) \
983 { \
984 .line[0] = { \
985 .text = __h0, \
986 .span = __s, \
987 }, \
988 .line[1] = { \
989 .text = __h1, \
990 }, \
991 }
992
993#define HEADER_SPAN_LOW(__h) \
994 { \
995 .line[1] = { \
996 .text = __h, \
997 }, \
998 }
999
cbb88500
JO
1000static struct c2c_dimension dim_dcacheline = {
1001 .header = HEADER_LOW("Cacheline"),
1002 .name = "dcacheline",
1003 .cmp = dcacheline_cmp,
1004 .entry = dcacheline_entry,
1005 .width = 18,
1006};
1007
48acdebd
JO
1008static struct c2c_dimension dim_offset = {
1009 .header = HEADER_BOTH("Data address", "Offset"),
1010 .name = "offset",
1011 .cmp = offset_cmp,
1012 .entry = offset_entry,
1013 .width = 18,
1014};
1015
43575a95
JO
1016static struct c2c_dimension dim_iaddr = {
1017 .header = HEADER_LOW("Code address"),
1018 .name = "iaddr",
1019 .cmp = iaddr_cmp,
1020 .entry = iaddr_entry,
1021 .width = 18,
1022};
1023
97cb486e
JO
1024static struct c2c_dimension dim_tot_hitm = {
1025 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1026 .name = "tot_hitm",
1027 .cmp = tot_hitm_cmp,
1028 .entry = tot_hitm_entry,
1029 .width = 7,
1030};
1031
1032static struct c2c_dimension dim_lcl_hitm = {
1033 .header = HEADER_SPAN_LOW("Lcl"),
1034 .name = "lcl_hitm",
1035 .cmp = lcl_hitm_cmp,
1036 .entry = lcl_hitm_entry,
1037 .width = 7,
1038};
1039
1040static struct c2c_dimension dim_rmt_hitm = {
1041 .header = HEADER_SPAN_LOW("Rmt"),
1042 .name = "rmt_hitm",
1043 .cmp = rmt_hitm_cmp,
1044 .entry = rmt_hitm_entry,
1045 .width = 7,
1046};
1047
1048static struct c2c_dimension dim_cl_rmt_hitm = {
1049 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1050 .name = "cl_rmt_hitm",
1051 .cmp = rmt_hitm_cmp,
1052 .entry = rmt_hitm_entry,
1053 .width = 7,
1054};
1055
1056static struct c2c_dimension dim_cl_lcl_hitm = {
1057 .header = HEADER_SPAN_LOW("Lcl"),
1058 .name = "cl_lcl_hitm",
1059 .cmp = lcl_hitm_cmp,
1060 .entry = lcl_hitm_entry,
1061 .width = 7,
1062};
1063
0f18896d
JO
1064static struct c2c_dimension dim_stores = {
1065 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1066 .name = "stores",
1067 .cmp = store_cmp,
1068 .entry = store_entry,
1069 .width = 7,
1070};
1071
1072static struct c2c_dimension dim_stores_l1hit = {
1073 .header = HEADER_SPAN_LOW("L1Hit"),
1074 .name = "stores_l1hit",
1075 .cmp = st_l1hit_cmp,
1076 .entry = st_l1hit_entry,
1077 .width = 7,
1078};
1079
1080static struct c2c_dimension dim_stores_l1miss = {
1081 .header = HEADER_SPAN_LOW("L1Miss"),
1082 .name = "stores_l1miss",
1083 .cmp = st_l1miss_cmp,
1084 .entry = st_l1miss_entry,
1085 .width = 7,
1086};
1087
1088static struct c2c_dimension dim_cl_stores_l1hit = {
1089 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1090 .name = "cl_stores_l1hit",
1091 .cmp = st_l1hit_cmp,
1092 .entry = st_l1hit_entry,
1093 .width = 7,
1094};
1095
1096static struct c2c_dimension dim_cl_stores_l1miss = {
1097 .header = HEADER_SPAN_LOW("L1 Miss"),
1098 .name = "cl_stores_l1miss",
1099 .cmp = st_l1miss_cmp,
1100 .entry = st_l1miss_entry,
1101 .width = 7,
1102};
1103
1295f685
JO
1104static struct c2c_dimension dim_ld_fbhit = {
1105 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1106 .name = "ld_fbhit",
1107 .cmp = ld_fbhit_cmp,
1108 .entry = ld_fbhit_entry,
1109 .width = 7,
1110};
1111
1112static struct c2c_dimension dim_ld_l1hit = {
1113 .header = HEADER_SPAN_LOW("L1"),
1114 .name = "ld_l1hit",
1115 .cmp = ld_l1hit_cmp,
1116 .entry = ld_l1hit_entry,
1117 .width = 7,
1118};
1119
1120static struct c2c_dimension dim_ld_l2hit = {
1121 .header = HEADER_SPAN_LOW("L2"),
1122 .name = "ld_l2hit",
1123 .cmp = ld_l2hit_cmp,
1124 .entry = ld_l2hit_entry,
1125 .width = 7,
1126};
1127
4d08910c
JO
1128static struct c2c_dimension dim_ld_llchit = {
1129 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1130 .name = "ld_lclhit",
1131 .cmp = ld_llchit_cmp,
1132 .entry = ld_llchit_entry,
1133 .width = 8,
1134};
1135
1136static struct c2c_dimension dim_ld_rmthit = {
1137 .header = HEADER_SPAN_LOW("Rmt"),
1138 .name = "ld_rmthit",
1139 .cmp = rmt_hit_cmp,
1140 .entry = rmt_hit_entry,
1141 .width = 8,
1142};
1143
04402d20
JO
1144static struct c2c_dimension dim_ld_llcmiss = {
1145 .header = HEADER_BOTH("LLC", "Ld Miss"),
1146 .name = "ld_llcmiss",
1147 .cmp = ld_llcmiss_cmp,
1148 .entry = ld_llcmiss_entry,
1149 .width = 7,
1150};
1151
01b84d76
JO
1152static struct c2c_dimension dim_tot_recs = {
1153 .header = HEADER_BOTH("Total", "records"),
1154 .name = "tot_recs",
1155 .cmp = tot_recs_cmp,
1156 .entry = tot_recs_entry,
1157 .width = 7,
1158};
1159
55177c4e
JO
1160static struct c2c_dimension dim_tot_loads = {
1161 .header = HEADER_BOTH("Total", "Loads"),
1162 .name = "tot_loads",
1163 .cmp = tot_loads_cmp,
1164 .entry = tot_loads_entry,
1165 .width = 7,
1166};
1167
f0c50c15
JO
1168static struct c2c_dimension dim_percent_hitm = {
1169 .header = HEADER_LOW("%hitm"),
1170 .name = "percent_hitm",
1171 .cmp = percent_hitm_cmp,
1172 .entry = percent_hitm_entry,
1173 .color = percent_hitm_color,
1174 .width = 7,
1175};
1176
9cb3500a
JO
1177static struct c2c_dimension dim_percent_rmt_hitm = {
1178 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1179 .name = "percent_rmt_hitm",
1180 .cmp = percent_rmt_hitm_cmp,
1181 .entry = percent_rmt_hitm_entry,
1182 .color = percent_rmt_hitm_color,
1183 .width = 7,
1184};
1185
1186static struct c2c_dimension dim_percent_lcl_hitm = {
1187 .header = HEADER_SPAN_LOW("Lcl"),
1188 .name = "percent_lcl_hitm",
1189 .cmp = percent_lcl_hitm_cmp,
1190 .entry = percent_lcl_hitm_entry,
1191 .color = percent_lcl_hitm_color,
1192 .width = 7,
1193};
1194
1195static struct c2c_dimension dim_percent_stores_l1hit = {
1196 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1197 .name = "percent_stores_l1hit",
1198 .cmp = percent_stores_l1hit_cmp,
1199 .entry = percent_stores_l1hit_entry,
1200 .color = percent_stores_l1hit_color,
1201 .width = 7,
1202};
1203
1204static struct c2c_dimension dim_percent_stores_l1miss = {
1205 .header = HEADER_SPAN_LOW("L1 Miss"),
1206 .name = "percent_stores_l1miss",
1207 .cmp = percent_stores_l1miss_cmp,
1208 .entry = percent_stores_l1miss_entry,
1209 .color = percent_stores_l1miss_color,
1210 .width = 7,
1211};
1212
6c70f54c
JO
1213static struct c2c_dimension dim_dram_lcl = {
1214 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1215 .name = "dram_lcl",
1216 .cmp = lcl_dram_cmp,
1217 .entry = lcl_dram_entry,
1218 .width = 8,
1219};
1220
1221static struct c2c_dimension dim_dram_rmt = {
1222 .header = HEADER_SPAN_LOW("Rmt"),
1223 .name = "dram_rmt",
1224 .cmp = rmt_dram_cmp,
1225 .entry = rmt_dram_entry,
1226 .width = 8,
1227};
1228
36d3deb9
JO
1229static struct c2c_dimension dim_pid = {
1230 .header = HEADER_LOW("Pid"),
1231 .name = "pid",
1232 .cmp = pid_cmp,
1233 .entry = pid_entry,
1234 .width = 7,
1235};
1236
e87019c5
JO
1237static struct c2c_dimension dim_tid = {
1238 .header = HEADER_LOW("Tid"),
1239 .name = "tid",
1240 .se = &sort_thread,
1241};
1242
51dedaa4
JO
1243static struct c2c_dimension dim_symbol = {
1244 .name = "symbol",
1245 .se = &sort_sym,
1246};
1247
1248static struct c2c_dimension dim_dso = {
1249 .header = HEADER_BOTH("Shared", "Object"),
1250 .name = "dso",
1251 .se = &sort_dso,
1252};
1253
1e181b92
JO
1254static struct c2c_header header_node[3] = {
1255 HEADER_LOW("Node"),
1256 HEADER_LOW("Node{cpus %hitms %stores}"),
1257 HEADER_LOW("Node{cpu list}"),
1258};
1259
1260static struct c2c_dimension dim_node = {
1261 .name = "node",
1262 .cmp = empty_cmp,
1263 .entry = node_entry,
1264 .width = 4,
1265};
1266
c75540e3 1267static struct c2c_dimension *dimensions[] = {
cbb88500 1268 &dim_dcacheline,
48acdebd 1269 &dim_offset,
43575a95 1270 &dim_iaddr,
97cb486e
JO
1271 &dim_tot_hitm,
1272 &dim_lcl_hitm,
1273 &dim_rmt_hitm,
1274 &dim_cl_lcl_hitm,
1275 &dim_cl_rmt_hitm,
0f18896d
JO
1276 &dim_stores,
1277 &dim_stores_l1hit,
1278 &dim_stores_l1miss,
1279 &dim_cl_stores_l1hit,
1280 &dim_cl_stores_l1miss,
1295f685
JO
1281 &dim_ld_fbhit,
1282 &dim_ld_l1hit,
1283 &dim_ld_l2hit,
4d08910c
JO
1284 &dim_ld_llchit,
1285 &dim_ld_rmthit,
04402d20 1286 &dim_ld_llcmiss,
01b84d76 1287 &dim_tot_recs,
55177c4e 1288 &dim_tot_loads,
f0c50c15 1289 &dim_percent_hitm,
9cb3500a
JO
1290 &dim_percent_rmt_hitm,
1291 &dim_percent_lcl_hitm,
1292 &dim_percent_stores_l1hit,
1293 &dim_percent_stores_l1miss,
6c70f54c
JO
1294 &dim_dram_lcl,
1295 &dim_dram_rmt,
36d3deb9 1296 &dim_pid,
e87019c5 1297 &dim_tid,
51dedaa4
JO
1298 &dim_symbol,
1299 &dim_dso,
1e181b92 1300 &dim_node,
c75540e3
JO
1301 NULL,
1302};
1303
1304static void fmt_free(struct perf_hpp_fmt *fmt)
1305{
1306 struct c2c_fmt *c2c_fmt;
1307
1308 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1309 free(c2c_fmt);
1310}
1311
1312static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1313{
1314 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1315 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1316
1317 return c2c_a->dim == c2c_b->dim;
1318}
1319
1320static struct c2c_dimension *get_dimension(const char *name)
1321{
1322 unsigned int i;
1323
1324 for (i = 0; dimensions[i]; i++) {
1325 struct c2c_dimension *dim = dimensions[i];
1326
1327 if (!strcmp(dim->name, name))
1328 return dim;
1329 };
1330
1331 return NULL;
1332}
1333
8d3f938d
JO
1334static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1335 struct hist_entry *he)
1336{
1337 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1338 struct c2c_dimension *dim = c2c_fmt->dim;
1339 size_t len = fmt->user_len;
1340
1341 if (!len)
1342 len = hists__col_len(he->hists, dim->se->se_width_idx);
1343
1344 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1345}
1346
1347static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1348 struct hist_entry *a, struct hist_entry *b)
1349{
1350 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1351 struct c2c_dimension *dim = c2c_fmt->dim;
1352
1353 return dim->se->se_cmp(a, b);
1354}
1355
1356static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1357 struct hist_entry *a, struct hist_entry *b)
1358{
1359 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1360 struct c2c_dimension *dim = c2c_fmt->dim;
1361 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1362
1363 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1364 return collapse_fn(a, b);
1365}
1366
c75540e3
JO
1367static struct c2c_fmt *get_format(const char *name)
1368{
1369 struct c2c_dimension *dim = get_dimension(name);
1370 struct c2c_fmt *c2c_fmt;
1371 struct perf_hpp_fmt *fmt;
1372
1373 if (!dim)
1374 return NULL;
1375
1376 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1377 if (!c2c_fmt)
1378 return NULL;
1379
1380 c2c_fmt->dim = dim;
1381
1382 fmt = &c2c_fmt->fmt;
1383 INIT_LIST_HEAD(&fmt->list);
1384 INIT_LIST_HEAD(&fmt->sort_list);
1385
8d3f938d
JO
1386 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1387 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
9cb3500a 1388 fmt->color = dim->se ? NULL : dim->color;
8d3f938d 1389 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
c75540e3
JO
1390 fmt->header = c2c_header;
1391 fmt->width = c2c_width;
8d3f938d 1392 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
c75540e3
JO
1393 fmt->equal = fmt_equal;
1394 fmt->free = fmt_free;
1395
1396 return c2c_fmt;
1397}
1398
1399static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1400{
1401 struct c2c_fmt *c2c_fmt = get_format(name);
1402
5f2eca83
JO
1403 if (!c2c_fmt) {
1404 reset_dimensions();
1405 return output_field_add(hpp_list, name);
1406 }
c75540e3
JO
1407
1408 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1409 return 0;
1410}
1411
1412static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1413{
1414 struct c2c_fmt *c2c_fmt = get_format(name);
51dedaa4 1415 struct c2c_dimension *dim;
c75540e3 1416
5f2eca83
JO
1417 if (!c2c_fmt) {
1418 reset_dimensions();
1419 return sort_dimension__add(hpp_list, name, NULL, 0);
1420 }
c75540e3 1421
51dedaa4
JO
1422 dim = c2c_fmt->dim;
1423 if (dim == &dim_dso)
1424 hpp_list->dso = 1;
1425
c75540e3
JO
1426 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1427 return 0;
1428}
1429
1430#define PARSE_LIST(_list, _fn) \
1431 do { \
1432 char *tmp, *tok; \
1433 ret = 0; \
1434 \
1435 if (!_list) \
1436 break; \
1437 \
1438 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1439 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1440 ret = _fn(hpp_list, tok); \
1441 if (ret == -EINVAL) { \
1442 error("Invalid --fields key: `%s'", tok); \
1443 break; \
1444 } else if (ret == -ESRCH) { \
1445 error("Unknown --fields key: `%s'", tok); \
1446 break; \
1447 } \
1448 } \
1449 } while (0)
1450
1451static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1452 const char *output_,
1453 const char *sort_)
1454{
1455 char *output = output_ ? strdup(output_) : NULL;
1456 char *sort = sort_ ? strdup(sort_) : NULL;
1457 int ret;
1458
1459 PARSE_LIST(output, c2c_hists__init_output);
1460 PARSE_LIST(sort, c2c_hists__init_sort);
1461
1462 /* copy sort keys to output fields */
1463 perf_hpp__setup_output_field(hpp_list);
1464
1465 /*
1466 * We dont need other sorting keys other than those
1467 * we already specified. It also really slows down
1468 * the processing a lot with big number of output
1469 * fields, so switching this off for c2c.
1470 */
1471
1472#if 0
1473 /* and then copy output fields to sort keys */
1474 perf_hpp__append_sort_keys(&hists->list);
1475#endif
1476
1477 free(output);
1478 free(sort);
1479 return ret;
1480}
1481
1482static int c2c_hists__init(struct c2c_hists *hists,
1483 const char *sort)
1484{
1485 __hists__init(&hists->hists, &hists->list);
1486
1487 /*
1488 * Initialize only with sort fields, we need to resort
1489 * later anyway, and that's where we add output fields
1490 * as well.
1491 */
1492 perf_hpp_list__init(&hists->list);
1493
1494 return hpp_list__parse(&hists->list, NULL, sort);
1495}
1496
1497__maybe_unused
1498static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1499 const char *output,
1500 const char *sort)
1501{
1502 perf_hpp__reset_output_field(&c2c_hists->list);
1503 return hpp_list__parse(&c2c_hists->list, output, sort);
1504}
1505
ec06f9b9
JO
1506static int filter_cb(struct hist_entry *he __maybe_unused)
1507{
1508 return 0;
1509}
1510
1511static int resort_cl_cb(struct hist_entry *he)
1512{
1513 struct c2c_hist_entry *c2c_he;
1514 struct c2c_hists *c2c_hists;
1515
1516 c2c_he = container_of(he, struct c2c_hist_entry, he);
1517 c2c_hists = c2c_he->hists;
1518
1519 if (c2c_hists) {
1520 hists__collapse_resort(&c2c_hists->hists, NULL);
1521 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
1522 }
1523
1524 return 0;
1525}
1526
1e181b92
JO
1527static void setup_nodes_header(void)
1528{
1529 dim_node.header = header_node[c2c.node_info];
1530}
1531
1532static int setup_nodes(struct perf_session *session)
1533{
1534 struct numa_node *n;
1535 unsigned long **nodes;
1536 int node, cpu;
1537 int *cpu2node;
1538
1539 if (c2c.node_info > 2)
1540 c2c.node_info = 2;
1541
1542 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
1543 c2c.cpus_cnt = session->header.env.nr_cpus_online;
1544
1545 n = session->header.env.numa_nodes;
1546 if (!n)
1547 return -EINVAL;
1548
1549 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
1550 if (!nodes)
1551 return -ENOMEM;
1552
1553 c2c.nodes = nodes;
1554
1555 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
1556 if (!cpu2node)
1557 return -ENOMEM;
1558
1559 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
1560 cpu2node[cpu] = -1;
1561
1562 c2c.cpu2node = cpu2node;
1563
1564 for (node = 0; node < c2c.nodes_cnt; node++) {
1565 struct cpu_map *map = n[node].map;
1566 unsigned long *set;
1567
1568 set = bitmap_alloc(c2c.cpus_cnt);
1569 if (!set)
1570 return -ENOMEM;
1571
1572 for (cpu = 0; cpu < map->nr; cpu++) {
1573 set_bit(map->map[cpu], set);
1574
1575 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
1576 return -EINVAL;
1577
1578 cpu2node[map->map[cpu]] = node;
1579 }
1580
1581 nodes[node] = set;
1582 }
1583
1584 setup_nodes_header();
1585 return 0;
1586}
1587
1588
903a6f15
JO
1589static int perf_c2c__report(int argc, const char **argv)
1590{
1591 struct perf_session *session;
78b27543 1592 struct ui_progress prog;
903a6f15
JO
1593 struct perf_data_file file = {
1594 .mode = PERF_DATA_MODE_READ,
1595 };
1596 const struct option c2c_options[] = {
1597 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1598 "file", "vmlinux pathname"),
1599 OPT_INCR('v', "verbose", &verbose,
1600 "be more verbose (show counter open errors, etc)"),
1601 OPT_STRING('i', "input", &input_name, "file",
1602 "the input file to process"),
1e181b92
JO
1603 OPT_INCR('N', "node-info", &c2c.node_info,
1604 "show extra node info in report (repeat for more info)"),
903a6f15
JO
1605 OPT_END()
1606 };
1607 int err = 0;
1608
1609 argc = parse_options(argc, argv, c2c_options, report_c2c_usage,
1610 PARSE_OPT_STOP_AT_NON_OPTION);
78b27543 1611 if (argc)
903a6f15
JO
1612 usage_with_options(report_c2c_usage, c2c_options);
1613
78b27543
JO
1614 if (!input_name || !strlen(input_name))
1615 input_name = "perf.data";
1616
903a6f15
JO
1617 file.path = input_name;
1618
c75540e3
JO
1619 err = c2c_hists__init(&c2c.hists, "dcacheline");
1620 if (err) {
1621 pr_debug("Failed to initialize hists\n");
1622 goto out;
1623 }
1624
903a6f15
JO
1625 session = perf_session__new(&file, 0, &c2c.tool);
1626 if (session == NULL) {
1627 pr_debug("No memory for session\n");
1628 goto out;
1629 }
1e181b92
JO
1630 err = setup_nodes(session);
1631 if (err) {
1632 pr_err("Failed setup nodes\n");
1633 goto out;
1634 }
903a6f15
JO
1635
1636 if (symbol__init(&session->header.env) < 0)
1637 goto out_session;
1638
1639 /* No pipe support at the moment. */
1640 if (perf_data_file__is_pipe(session->file)) {
1641 pr_debug("No pipe support at the moment.\n");
1642 goto out_session;
1643 }
1644
78b27543
JO
1645 err = perf_session__process_events(session);
1646 if (err) {
1647 pr_err("failed to process sample\n");
1648 goto out_session;
1649 }
1650
1651 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
1652
1653 hists__collapse_resort(&c2c.hists.hists, NULL);
ec06f9b9 1654 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_cl_cb);
78b27543
JO
1655
1656 ui_progress__finish();
1657
903a6f15
JO
1658out_session:
1659 perf_session__delete(session);
1660out:
1661 return err;
1662}
1663
39bcd4a4
JO
1664static int parse_record_events(const struct option *opt __maybe_unused,
1665 const char *str, int unset __maybe_unused)
1666{
1667 bool *event_set = (bool *) opt->value;
1668
1669 *event_set = true;
1670 return perf_mem_events__parse(str);
1671}
1672
1673
1674static const char * const __usage_record[] = {
1675 "perf c2c record [<options>] [<command>]",
1676 "perf c2c record [<options>] -- <command> [<options>]",
1677 NULL
1678};
1679
1680static const char * const *record_mem_usage = __usage_record;
1681
1682static int perf_c2c__record(int argc, const char **argv)
1683{
1684 int rec_argc, i = 0, j;
1685 const char **rec_argv;
1686 int ret;
1687 bool all_user = false, all_kernel = false;
1688 bool event_set = false;
1689 struct option options[] = {
1690 OPT_CALLBACK('e', "event", &event_set, "event",
1691 "event selector. Use 'perf mem record -e list' to list available events",
1692 parse_record_events),
1693 OPT_INCR('v', "verbose", &verbose,
1694 "be more verbose (show counter open errors, etc)"),
1695 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
1696 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
1697 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
1698 OPT_END()
1699 };
1700
1701 if (perf_mem_events__init()) {
1702 pr_err("failed: memory events not supported\n");
1703 return -1;
1704 }
1705
1706 argc = parse_options(argc, argv, options, record_mem_usage,
1707 PARSE_OPT_KEEP_UNKNOWN);
1708
1709 rec_argc = argc + 10; /* max number of arguments */
1710 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1711 if (!rec_argv)
1712 return -1;
1713
1714 rec_argv[i++] = "record";
1715
1716 if (!event_set) {
1717 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
1718 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
1719 }
1720
1721 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
1722 rec_argv[i++] = "-W";
1723
1724 rec_argv[i++] = "-d";
1725 rec_argv[i++] = "--sample-cpu";
1726
1727 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
1728 if (!perf_mem_events[j].record)
1729 continue;
1730
1731 if (!perf_mem_events[j].supported) {
1732 pr_err("failed: event '%s' not supported\n",
1733 perf_mem_events[j].name);
1734 return -1;
1735 }
1736
1737 rec_argv[i++] = "-e";
1738 rec_argv[i++] = perf_mem_events__name(j);
1739 };
1740
1741 if (all_user)
1742 rec_argv[i++] = "--all-user";
1743
1744 if (all_kernel)
1745 rec_argv[i++] = "--all-kernel";
1746
1747 for (j = 0; j < argc; j++, i++)
1748 rec_argv[i] = argv[j];
1749
1750 if (verbose > 0) {
1751 pr_debug("calling: ");
1752
1753 j = 0;
1754
1755 while (rec_argv[j]) {
1756 pr_debug("%s ", rec_argv[j]);
1757 j++;
1758 }
1759 pr_debug("\n");
1760 }
1761
1762 ret = cmd_record(i, rec_argv, NULL);
1763 free(rec_argv);
1764 return ret;
1765}
1766
7aef3bf3
JO
1767int cmd_c2c(int argc, const char **argv, const char *prefix __maybe_unused)
1768{
1769 const struct option c2c_options[] = {
1770 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1771 OPT_END()
1772 };
1773
1774 argc = parse_options(argc, argv, c2c_options, c2c_usage,
1775 PARSE_OPT_STOP_AT_NON_OPTION);
39bcd4a4
JO
1776
1777 if (!argc)
1778 usage_with_options(c2c_usage, c2c_options);
1779
1780 if (!strncmp(argv[0], "rec", 3)) {
1781 return perf_c2c__record(argc, argv);
903a6f15
JO
1782 } else if (!strncmp(argv[0], "rep", 3)) {
1783 return perf_c2c__report(argc, argv);
39bcd4a4
JO
1784 } else {
1785 usage_with_options(c2c_usage, c2c_options);
1786 }
1787
7aef3bf3
JO
1788 return 0;
1789}