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