blob: 8335a4076a5af718a05da88e8e38f4bad234efdc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa465f27a2016-08-26 10:36:12 +02002/*
3 * This is rewrite of original c2c tool introduced in here:
4 * http://lwn.net/Articles/588866/
5 *
6 * The original tool was changed to fit in current perf state.
7 *
8 * Original authors:
9 * Don Zickus <dzickus@redhat.com>
10 * Dick Fowles <fowles@inreach.com>
11 * Joe Mario <jmario@redhat.com>
12 */
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030013#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030014#include <inttypes.h>
Jiri Olsa7aef3bf2016-09-22 17:36:38 +020015#include <linux/compiler.h>
16#include <linux/kernel.h>
Jiri Olsacbb88502016-09-22 17:36:48 +020017#include <linux/stringify.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030018#include <linux/zalloc.h>
Jiri Olsa1e181b92016-06-03 15:40:28 +020019#include <asm/bug.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030020#include <sys/param.h>
Jiri Olsa7aef3bf2016-09-22 17:36:38 +020021#include "debug.h"
22#include "builtin.h"
23#include <subcmd/parse-options.h>
Jiri Olsa39bcd4a2016-09-22 17:36:39 +020024#include "mem-events.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020025#include "session.h"
26#include "hist.h"
Jiri Olsacbb88502016-09-22 17:36:48 +020027#include "sort.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020028#include "tool.h"
Arnaldo Carvalho de Melo12500902019-08-22 16:58:29 -030029#include "cacheline.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020030#include "data.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030031#include "event.h"
Jiri Olsa2709b972016-08-27 11:40:23 +020032#include "evlist.h"
33#include "evsel.h"
Jiri Olsa5a1a99c2016-01-06 16:59:02 +010034#include "ui/browsers/hists.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030035#include "thread.h"
Jiri Olsa7f834c22018-03-09 11:14:40 +010036#include "mem2node.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010037#include "symbol.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020038
Jiri Olsac75540e2016-09-22 17:36:41 +020039struct c2c_hists {
40 struct hists hists;
41 struct perf_hpp_list list;
Jiri Olsab2252ae2016-09-22 17:36:46 +020042 struct c2c_stats stats;
Jiri Olsac75540e2016-09-22 17:36:41 +020043};
44
Jiri Olsa92062d52016-06-05 13:40:53 +020045struct compute_stats {
46 struct stats lcl_hitm;
47 struct stats rmt_hitm;
48 struct stats load;
49};
50
Jiri Olsa78b27542016-09-22 17:36:44 +020051struct c2c_hist_entry {
52 struct c2c_hists *hists;
Jiri Olsab2252ae2016-09-22 17:36:46 +020053 struct c2c_stats stats;
Jiri Olsa1e181b92016-06-03 15:40:28 +020054 unsigned long *cpuset;
Jiri Olsa7f834c22018-03-09 11:14:40 +010055 unsigned long *nodeset;
Jiri Olsa1e181b92016-06-03 15:40:28 +020056 struct c2c_stats *node_stats;
Jiri Olsabb342dae2016-07-06 15:40:09 +020057 unsigned int cacheline_idx;
Jiri Olsa92062d52016-06-05 13:40:53 +020058
59 struct compute_stats cstats;
60
Jiri Olsa4c820522018-06-08 02:22:11 +020061 unsigned long paddr;
62 unsigned long paddr_cnt;
63 bool paddr_zero;
64 char *nodestr;
65
Jiri Olsa78b27542016-09-22 17:36:44 +020066 /*
67 * must be at the end,
68 * because of its callchain dynamic entry
69 */
70 struct hist_entry he;
71};
72
Jiri Olsa423701a2018-12-28 11:18:19 +010073static char const *coalesce_default = "iaddr";
Jiri Olsafc9c6302016-05-24 14:14:38 +020074
Jiri Olsa903a6f12016-09-22 17:36:40 +020075struct perf_c2c {
Jiri Olsac75540e2016-09-22 17:36:41 +020076 struct perf_tool tool;
77 struct c2c_hists hists;
Jiri Olsa7f834c22018-03-09 11:14:40 +010078 struct mem2node mem2node;
Jiri Olsa1e181b92016-06-03 15:40:28 +020079
80 unsigned long **nodes;
81 int nodes_cnt;
82 int cpus_cnt;
83 int *cpu2node;
84 int node_info;
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +020085
86 bool show_src;
Jiri Olsaaf09b2d2016-10-11 13:52:05 +020087 bool show_all;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +010088 bool use_stdio;
Jiri Olsa74c63a22016-05-02 20:01:59 +020089 bool stats_only;
Jiri Olsa590b6a32016-07-10 16:25:15 +020090 bool symbol_full;
Jiri Olsa7ef2efa2016-07-01 11:12:11 +020091
92 /* HITM shared clines stats */
93 struct c2c_stats hitm_stats;
94 int shared_clines;
Jiri Olsa55b95772016-05-29 10:21:45 +020095
96 int display;
Jiri Olsafc9c6302016-05-24 14:14:38 +020097
98 const char *coalesce;
99 char *cl_sort;
100 char *cl_resort;
101 char *cl_output;
Jiri Olsa55b95772016-05-29 10:21:45 +0200102};
103
104enum {
105 DISPLAY_LCL,
106 DISPLAY_RMT,
Jiri Olsad940bac2016-11-21 22:33:30 +0100107 DISPLAY_TOT,
108 DISPLAY_MAX,
109};
110
111static const char *display_str[DISPLAY_MAX] = {
112 [DISPLAY_LCL] = "Local",
113 [DISPLAY_RMT] = "Remote",
114 [DISPLAY_TOT] = "Total",
Jiri Olsa903a6f12016-09-22 17:36:40 +0200115};
116
Jiri Olsa3a5bfab2016-11-21 22:33:31 +0100117static const struct option c2c_options[] = {
118 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
119 OPT_END()
120};
121
Jiri Olsa903a6f12016-09-22 17:36:40 +0200122static struct perf_c2c c2c;
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200123
Jiri Olsa78b27542016-09-22 17:36:44 +0200124static void *c2c_he_zalloc(size_t size)
125{
126 struct c2c_hist_entry *c2c_he;
127
128 c2c_he = zalloc(size + sizeof(*c2c_he));
129 if (!c2c_he)
130 return NULL;
131
Jiri Olsa1e181b92016-06-03 15:40:28 +0200132 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
133 if (!c2c_he->cpuset)
134 return NULL;
135
Jiri Olsa7f834c22018-03-09 11:14:40 +0100136 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
137 if (!c2c_he->nodeset)
138 return NULL;
139
Jiri Olsa1e181b92016-06-03 15:40:28 +0200140 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
141 if (!c2c_he->node_stats)
142 return NULL;
143
Jiri Olsa92062d52016-06-05 13:40:53 +0200144 init_stats(&c2c_he->cstats.lcl_hitm);
145 init_stats(&c2c_he->cstats.rmt_hitm);
146 init_stats(&c2c_he->cstats.load);
147
Jiri Olsa78b27542016-09-22 17:36:44 +0200148 return &c2c_he->he;
149}
150
151static void c2c_he_free(void *he)
152{
153 struct c2c_hist_entry *c2c_he;
154
155 c2c_he = container_of(he, struct c2c_hist_entry, he);
156 if (c2c_he->hists) {
157 hists__delete_entries(&c2c_he->hists->hists);
158 free(c2c_he->hists);
159 }
160
Jiri Olsa1e181b92016-06-03 15:40:28 +0200161 free(c2c_he->cpuset);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100162 free(c2c_he->nodeset);
163 free(c2c_he->nodestr);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200164 free(c2c_he->node_stats);
Jiri Olsa78b27542016-09-22 17:36:44 +0200165 free(c2c_he);
166}
167
168static struct hist_entry_ops c2c_entry_ops = {
169 .new = c2c_he_zalloc,
170 .free = c2c_he_free,
171};
172
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200173static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200174 const char *sort,
175 int nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200176
Jiri Olsab2252ae2016-09-22 17:36:46 +0200177static struct c2c_hists*
178he__get_c2c_hists(struct hist_entry *he,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200179 const char *sort,
180 int nr_header_lines)
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200181{
182 struct c2c_hist_entry *c2c_he;
183 struct c2c_hists *hists;
184 int ret;
185
186 c2c_he = container_of(he, struct c2c_hist_entry, he);
187 if (c2c_he->hists)
Jiri Olsab2252ae2016-09-22 17:36:46 +0200188 return c2c_he->hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200189
190 hists = c2c_he->hists = zalloc(sizeof(*hists));
191 if (!hists)
192 return NULL;
193
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200194 ret = c2c_hists__init(hists, sort, nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200195 if (ret) {
196 free(hists);
197 return NULL;
198 }
199
Jiri Olsab2252ae2016-09-22 17:36:46 +0200200 return hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200201}
202
Jiri Olsa1e181b92016-06-03 15:40:28 +0200203static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
204 struct perf_sample *sample)
205{
206 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
207 "WARNING: no sample cpu value"))
208 return;
209
210 set_bit(sample->cpu, c2c_he->cpuset);
211}
212
Jiri Olsa7f834c22018-03-09 11:14:40 +0100213static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
214 struct perf_sample *sample)
215{
216 int node;
217
218 if (!sample->phys_addr) {
219 c2c_he->paddr_zero = true;
220 return;
221 }
222
223 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
224 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
225 return;
226
227 set_bit(node, c2c_he->nodeset);
228
229 if (c2c_he->paddr != sample->phys_addr) {
230 c2c_he->paddr_cnt++;
231 c2c_he->paddr = sample->phys_addr;
232 }
233}
234
Jiri Olsa92062d52016-06-05 13:40:53 +0200235static void compute_stats(struct c2c_hist_entry *c2c_he,
236 struct c2c_stats *stats,
237 u64 weight)
238{
239 struct compute_stats *cstats = &c2c_he->cstats;
240
241 if (stats->rmt_hitm)
242 update_stats(&cstats->rmt_hitm, weight);
243 else if (stats->lcl_hitm)
244 update_stats(&cstats->lcl_hitm, weight);
245 else if (stats->load)
246 update_stats(&cstats->load, weight);
247}
248
Jiri Olsa78b27542016-09-22 17:36:44 +0200249static int process_sample_event(struct perf_tool *tool __maybe_unused,
250 union perf_event *event,
251 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200252 struct evsel *evsel,
Jiri Olsa78b27542016-09-22 17:36:44 +0200253 struct machine *machine)
254{
Jiri Olsab2252ae2016-09-22 17:36:46 +0200255 struct c2c_hists *c2c_hists = &c2c.hists;
256 struct c2c_hist_entry *c2c_he;
257 struct c2c_stats stats = { .nr_entries = 0, };
Jiri Olsa78b27542016-09-22 17:36:44 +0200258 struct hist_entry *he;
259 struct addr_location al;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200260 struct mem_info *mi, *mi_dup;
Jiri Olsa78b27542016-09-22 17:36:44 +0200261 int ret;
262
263 if (machine__resolve(machine, &al, sample) < 0) {
264 pr_debug("problem processing %d event, skipping it.\n",
265 event->header.type);
266 return -1;
267 }
268
Jiri Olsadd805762016-05-11 18:23:48 +0200269 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
270 evsel, &al, sysctl_perf_event_max_stack);
271 if (ret)
272 goto out;
273
Jiri Olsa78b27542016-09-22 17:36:44 +0200274 mi = sample__resolve_mem(sample, &al);
275 if (mi == NULL)
276 return -ENOMEM;
277
Jiri Olsa5cedb412018-03-07 16:50:07 +0100278 /*
279 * The mi object is released in hists__add_entry_ops,
280 * if it gets sorted out into existing data, so we need
281 * to take the copy now.
282 */
283 mi_dup = mem_info__get(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200284
Jiri Olsab2252ae2016-09-22 17:36:46 +0200285 c2c_decode_stats(&stats, mi);
286
287 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsa78b27542016-09-22 17:36:44 +0200288 &al, NULL, NULL, mi,
289 sample, true);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200290 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100291 goto free_mi;
Jiri Olsa78b27542016-09-22 17:36:44 +0200292
Jiri Olsab2252ae2016-09-22 17:36:46 +0200293 c2c_he = container_of(he, struct c2c_hist_entry, he);
294 c2c_add_stats(&c2c_he->stats, &stats);
295 c2c_add_stats(&c2c_hists->stats, &stats);
296
Jiri Olsa1e181b92016-06-03 15:40:28 +0200297 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100298 c2c_he__set_node(c2c_he, sample);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200299
Jiri Olsab2252ae2016-09-22 17:36:46 +0200300 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsa78b27542016-09-22 17:36:44 +0200301 ret = hist_entry__append_callchain(he, sample);
302
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200303 if (!ret) {
Jiri Olsa1e181b92016-06-03 15:40:28 +0200304 /*
305 * There's already been warning about missing
306 * sample's cpu value. Let's account all to
307 * node 0 in this case, without any further
308 * warning.
309 *
310 * Doing node stats only for single callchain data.
311 */
312 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
313 int node = c2c.cpu2node[cpu];
314
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200315 mi = mi_dup;
316
Jiri Olsafc9c6302016-05-24 14:14:38 +0200317 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200318 if (!c2c_hists)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100319 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200320
Jiri Olsab2252ae2016-09-22 17:36:46 +0200321 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200322 &al, NULL, NULL, mi,
323 sample, true);
324 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100325 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200326
Jiri Olsab2252ae2016-09-22 17:36:46 +0200327 c2c_he = container_of(he, struct c2c_hist_entry, he);
328 c2c_add_stats(&c2c_he->stats, &stats);
329 c2c_add_stats(&c2c_hists->stats, &stats);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200330 c2c_add_stats(&c2c_he->node_stats[node], &stats);
331
Jiri Olsa92062d52016-06-05 13:40:53 +0200332 compute_stats(c2c_he, &stats, sample->weight);
333
Jiri Olsa1e181b92016-06-03 15:40:28 +0200334 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100335 c2c_he__set_node(c2c_he, sample);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200336
337 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200338 ret = hist_entry__append_callchain(he, sample);
339 }
340
341out:
Jiri Olsa78b27542016-09-22 17:36:44 +0200342 addr_location__put(&al);
343 return ret;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200344
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200345free_mi:
Jiri Olsa5cedb412018-03-07 16:50:07 +0100346 mem_info__put(mi_dup);
347 mem_info__put(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200348 ret = -ENOMEM;
349 goto out;
Jiri Olsa78b27542016-09-22 17:36:44 +0200350}
351
352static struct perf_c2c c2c = {
353 .tool = {
354 .sample = process_sample_event,
355 .mmap = perf_event__process_mmap,
356 .mmap2 = perf_event__process_mmap2,
357 .comm = perf_event__process_comm,
358 .exit = perf_event__process_exit,
359 .fork = perf_event__process_fork,
360 .lost = perf_event__process_lost,
361 .ordered_events = true,
362 .ordering_requires_timestamps = true,
363 },
364};
365
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200366static const char * const c2c_usage[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +0200367 "perf c2c {record|report}",
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200368 NULL
369};
370
Jiri Olsa903a6f12016-09-22 17:36:40 +0200371static const char * const __usage_report[] = {
372 "perf c2c report",
373 NULL
374};
375
376static const char * const *report_c2c_usage = __usage_report;
377
Jiri Olsac75540e2016-09-22 17:36:41 +0200378#define C2C_HEADER_MAX 2
379
380struct c2c_header {
381 struct {
382 const char *text;
383 int span;
384 } line[C2C_HEADER_MAX];
385};
386
387struct c2c_dimension {
388 struct c2c_header header;
389 const char *name;
390 int width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200391 struct sort_entry *se;
Jiri Olsac75540e2016-09-22 17:36:41 +0200392
393 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
394 struct hist_entry *, struct hist_entry *);
395 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
396 struct hist_entry *he);
397 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
398 struct hist_entry *he);
399};
400
401struct c2c_fmt {
402 struct perf_hpp_fmt fmt;
403 struct c2c_dimension *dim;
404};
405
Jiri Olsa590b6a32016-07-10 16:25:15 +0200406#define SYMBOL_WIDTH 30
407
408static struct c2c_dimension dim_symbol;
409static struct c2c_dimension dim_srcline;
410
411static int symbol_width(struct hists *hists, struct sort_entry *se)
412{
413 int width = hists__col_len(hists, se->se_width_idx);
414
415 if (!c2c.symbol_full)
416 width = MIN(width, SYMBOL_WIDTH);
417
418 return width;
419}
420
Jiri Olsac75540e2016-09-22 17:36:41 +0200421static int c2c_width(struct perf_hpp_fmt *fmt,
422 struct perf_hpp *hpp __maybe_unused,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300423 struct hists *hists)
Jiri Olsac75540e2016-09-22 17:36:41 +0200424{
425 struct c2c_fmt *c2c_fmt;
Jiri Olsac75540e2016-09-22 17:36:41 +0200426 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +0200427
428 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
429 dim = c2c_fmt->dim;
430
Jiri Olsa590b6a32016-07-10 16:25:15 +0200431 if (dim == &dim_symbol || dim == &dim_srcline)
432 return symbol_width(hists, dim->se);
433
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200434 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
435 c2c_fmt->dim->width;
436}
437
438static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
439 struct hists *hists, int line, int *span)
440{
441 struct perf_hpp_list *hpp_list = hists->hpp_list;
442 struct c2c_fmt *c2c_fmt;
443 struct c2c_dimension *dim;
444 const char *text = NULL;
445 int width = c2c_width(fmt, hpp, hists);
446
447 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
448 dim = c2c_fmt->dim;
449
450 if (dim->se) {
451 text = dim->header.line[line].text;
452 /* Use the last line from sort_entry if not defined. */
453 if (!text && (line == hpp_list->nr_header_lines - 1))
454 text = dim->se->se_header;
455 } else {
456 text = dim->header.line[line].text;
457
458 if (*span) {
459 (*span)--;
460 return 0;
461 } else {
462 *span = dim->header.line[line].span;
463 }
464 }
465
Jiri Olsac75540e2016-09-22 17:36:41 +0200466 if (text == NULL)
467 text = "";
468
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200469 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
Jiri Olsac75540e2016-09-22 17:36:41 +0200470}
471
Jiri Olsacbb88502016-09-22 17:36:48 +0200472#define HEX_STR(__s, __v) \
473({ \
474 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
475 __s; \
476})
477
478static int64_t
479dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
480 struct hist_entry *left, struct hist_entry *right)
481{
482 return sort__dcacheline_cmp(left, right);
483}
484
485static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
486 struct hist_entry *he)
487{
488 uint64_t addr = 0;
489 int width = c2c_width(fmt, hpp, he->hists);
490 char buf[20];
491
492 if (he->mem_info)
493 addr = cl_address(he->mem_info->daddr.addr);
494
495 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
496}
497
Jiri Olsa7f834c22018-03-09 11:14:40 +0100498static int
499dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
500 struct hist_entry *he)
501{
502 struct c2c_hist_entry *c2c_he;
503 int width = c2c_width(fmt, hpp, he->hists);
504
505 c2c_he = container_of(he, struct c2c_hist_entry, he);
506 if (WARN_ON_ONCE(!c2c_he->nodestr))
507 return 0;
508
509 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
510}
511
Jiri Olsa03d9fcb2018-03-09 11:14:42 +0100512static int
513dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
514 struct hist_entry *he)
515{
516 struct c2c_hist_entry *c2c_he;
517 int width = c2c_width(fmt, hpp, he->hists);
518
519 c2c_he = container_of(he, struct c2c_hist_entry, he);
520 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
521}
522
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200523static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
524 struct hist_entry *he)
525{
526 uint64_t addr = 0;
527 int width = c2c_width(fmt, hpp, he->hists);
528 char buf[20];
529
530 if (he->mem_info)
531 addr = cl_offset(he->mem_info->daddr.al_addr);
532
533 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
534}
535
536static int64_t
537offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
538 struct hist_entry *left, struct hist_entry *right)
539{
540 uint64_t l = 0, r = 0;
541
542 if (left->mem_info)
543 l = cl_offset(left->mem_info->daddr.addr);
544 if (right->mem_info)
545 r = cl_offset(right->mem_info->daddr.addr);
546
547 return (int64_t)(r - l);
548}
549
Jiri Olsa43575a92016-05-03 21:48:56 +0200550static int
551iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
552 struct hist_entry *he)
553{
554 uint64_t addr = 0;
555 int width = c2c_width(fmt, hpp, he->hists);
556 char buf[20];
557
558 if (he->mem_info)
559 addr = he->mem_info->iaddr.addr;
560
561 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
562}
563
564static int64_t
565iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
566 struct hist_entry *left, struct hist_entry *right)
567{
568 return sort__iaddr_cmp(left, right);
569}
570
Jiri Olsa97cb4862016-05-23 16:20:14 +0200571static int
572tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
573 struct hist_entry *he)
574{
575 struct c2c_hist_entry *c2c_he;
576 int width = c2c_width(fmt, hpp, he->hists);
577 unsigned int tot_hitm;
578
579 c2c_he = container_of(he, struct c2c_hist_entry, he);
580 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
581
582 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
583}
584
585static int64_t
586tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
587 struct hist_entry *left, struct hist_entry *right)
588{
589 struct c2c_hist_entry *c2c_left;
590 struct c2c_hist_entry *c2c_right;
591 unsigned int tot_hitm_left;
592 unsigned int tot_hitm_right;
593
594 c2c_left = container_of(left, struct c2c_hist_entry, he);
595 c2c_right = container_of(right, struct c2c_hist_entry, he);
596
597 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
598 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
599
600 return tot_hitm_left - tot_hitm_right;
601}
602
603#define STAT_FN_ENTRY(__f) \
604static int \
605__f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
606 struct hist_entry *he) \
607{ \
608 struct c2c_hist_entry *c2c_he; \
609 int width = c2c_width(fmt, hpp, he->hists); \
610 \
611 c2c_he = container_of(he, struct c2c_hist_entry, he); \
612 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
613 c2c_he->stats.__f); \
614}
615
616#define STAT_FN_CMP(__f) \
617static int64_t \
618__f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
619 struct hist_entry *left, struct hist_entry *right) \
620{ \
621 struct c2c_hist_entry *c2c_left, *c2c_right; \
622 \
623 c2c_left = container_of(left, struct c2c_hist_entry, he); \
624 c2c_right = container_of(right, struct c2c_hist_entry, he); \
625 return c2c_left->stats.__f - c2c_right->stats.__f; \
626}
627
628#define STAT_FN(__f) \
629 STAT_FN_ENTRY(__f) \
630 STAT_FN_CMP(__f)
631
632STAT_FN(rmt_hitm)
633STAT_FN(lcl_hitm)
Jiri Olsa0f188962016-05-04 10:10:11 +0200634STAT_FN(store)
635STAT_FN(st_l1hit)
636STAT_FN(st_l1miss)
Jiri Olsa1295f682016-05-04 10:18:24 +0200637STAT_FN(ld_fbhit)
638STAT_FN(ld_l1hit)
639STAT_FN(ld_l2hit)
Jiri Olsa4d089102016-05-04 10:27:51 +0200640STAT_FN(ld_llchit)
641STAT_FN(rmt_hit)
Jiri Olsa97cb4862016-05-23 16:20:14 +0200642
Jiri Olsa04402d22016-05-19 10:10:51 +0200643static uint64_t llc_miss(struct c2c_stats *stats)
644{
645 uint64_t llcmiss;
646
647 llcmiss = stats->lcl_dram +
648 stats->rmt_dram +
649 stats->rmt_hitm +
650 stats->rmt_hit;
651
652 return llcmiss;
653}
654
655static int
656ld_llcmiss_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
662 c2c_he = container_of(he, struct c2c_hist_entry, he);
663
664 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
665 llc_miss(&c2c_he->stats));
666}
667
668static int64_t
669ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
670 struct hist_entry *left, struct hist_entry *right)
671{
672 struct c2c_hist_entry *c2c_left;
673 struct c2c_hist_entry *c2c_right;
674
675 c2c_left = container_of(left, struct c2c_hist_entry, he);
676 c2c_right = container_of(right, struct c2c_hist_entry, he);
677
678 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
679}
680
Jiri Olsa01b84d72016-05-04 10:35:29 +0200681static uint64_t total_records(struct c2c_stats *stats)
682{
683 uint64_t lclmiss, ldcnt, total;
684
685 lclmiss = stats->lcl_dram +
686 stats->rmt_dram +
687 stats->rmt_hitm +
688 stats->rmt_hit;
689
690 ldcnt = lclmiss +
691 stats->ld_fbhit +
692 stats->ld_l1hit +
693 stats->ld_l2hit +
694 stats->ld_llchit +
695 stats->lcl_hitm;
696
697 total = ldcnt +
698 stats->st_l1hit +
699 stats->st_l1miss;
700
701 return total;
702}
703
704static int
705tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
706 struct hist_entry *he)
707{
708 struct c2c_hist_entry *c2c_he;
709 int width = c2c_width(fmt, hpp, he->hists);
710 uint64_t tot_recs;
711
712 c2c_he = container_of(he, struct c2c_hist_entry, he);
713 tot_recs = total_records(&c2c_he->stats);
714
715 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
716}
717
718static int64_t
719tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
720 struct hist_entry *left, struct hist_entry *right)
721{
722 struct c2c_hist_entry *c2c_left;
723 struct c2c_hist_entry *c2c_right;
724 uint64_t tot_recs_left;
725 uint64_t tot_recs_right;
726
727 c2c_left = container_of(left, struct c2c_hist_entry, he);
728 c2c_right = container_of(right, struct c2c_hist_entry, he);
729
730 tot_recs_left = total_records(&c2c_left->stats);
731 tot_recs_right = total_records(&c2c_right->stats);
732
733 return tot_recs_left - tot_recs_right;
734}
735
Jiri Olsa55177c42016-05-19 09:52:37 +0200736static uint64_t total_loads(struct c2c_stats *stats)
737{
738 uint64_t lclmiss, ldcnt;
739
740 lclmiss = stats->lcl_dram +
741 stats->rmt_dram +
742 stats->rmt_hitm +
743 stats->rmt_hit;
744
745 ldcnt = lclmiss +
746 stats->ld_fbhit +
747 stats->ld_l1hit +
748 stats->ld_l2hit +
749 stats->ld_llchit +
750 stats->lcl_hitm;
751
752 return ldcnt;
753}
754
755static int
756tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
757 struct hist_entry *he)
758{
759 struct c2c_hist_entry *c2c_he;
760 int width = c2c_width(fmt, hpp, he->hists);
761 uint64_t tot_recs;
762
763 c2c_he = container_of(he, struct c2c_hist_entry, he);
764 tot_recs = total_loads(&c2c_he->stats);
765
766 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
767}
768
769static int64_t
770tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
771 struct hist_entry *left, struct hist_entry *right)
772{
773 struct c2c_hist_entry *c2c_left;
774 struct c2c_hist_entry *c2c_right;
775 uint64_t tot_recs_left;
776 uint64_t tot_recs_right;
777
778 c2c_left = container_of(left, struct c2c_hist_entry, he);
779 c2c_right = container_of(right, struct c2c_hist_entry, he);
780
781 tot_recs_left = total_loads(&c2c_left->stats);
782 tot_recs_right = total_loads(&c2c_right->stats);
783
784 return tot_recs_left - tot_recs_right;
785}
786
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200787typedef double (get_percent_cb)(struct c2c_hist_entry *);
788
789static int
790percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
791 struct hist_entry *he, get_percent_cb get_percent)
792{
793 struct c2c_hist_entry *c2c_he;
794 int width = c2c_width(fmt, hpp, he->hists);
795 double per;
796
797 c2c_he = container_of(he, struct c2c_hist_entry, he);
798 per = get_percent(c2c_he);
799
Jiri Olsa5a1a99c2016-01-06 16:59:02 +0100800#ifdef HAVE_SLANG_SUPPORT
801 if (use_browser)
802 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
803#endif
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200804 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
805}
806
807static double percent_hitm(struct c2c_hist_entry *c2c_he)
808{
809 struct c2c_hists *hists;
810 struct c2c_stats *stats;
811 struct c2c_stats *total;
Jiri Olsa55b95772016-05-29 10:21:45 +0200812 int tot = 0, st = 0;
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200813 double p;
814
815 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
816 stats = &c2c_he->stats;
817 total = &hists->stats;
818
Jiri Olsa55b95772016-05-29 10:21:45 +0200819 switch (c2c.display) {
820 case DISPLAY_RMT:
821 st = stats->rmt_hitm;
822 tot = total->rmt_hitm;
823 break;
824 case DISPLAY_LCL:
825 st = stats->lcl_hitm;
826 tot = total->lcl_hitm;
Jiri Olsad940bac2016-11-21 22:33:30 +0100827 break;
828 case DISPLAY_TOT:
829 st = stats->tot_hitm;
830 tot = total->tot_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +0200831 default:
832 break;
833 }
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200834
835 p = tot ? (double) st / tot : 0;
836
837 return 100 * p;
838}
839
840#define PERC_STR(__s, __v) \
841({ \
842 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
843 __s; \
844})
845
846static int
847percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
848 struct hist_entry *he)
849{
850 struct c2c_hist_entry *c2c_he;
851 int width = c2c_width(fmt, hpp, he->hists);
852 char buf[10];
853 double per;
854
855 c2c_he = container_of(he, struct c2c_hist_entry, he);
856 per = percent_hitm(c2c_he);
857 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
858}
859
860static int
861percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
862 struct hist_entry *he)
863{
864 return percent_color(fmt, hpp, he, percent_hitm);
865}
866
867static int64_t
868percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
869 struct hist_entry *left, struct hist_entry *right)
870{
871 struct c2c_hist_entry *c2c_left;
872 struct c2c_hist_entry *c2c_right;
873 double per_left;
874 double per_right;
875
876 c2c_left = container_of(left, struct c2c_hist_entry, he);
877 c2c_right = container_of(right, struct c2c_hist_entry, he);
878
879 per_left = percent_hitm(c2c_left);
880 per_right = percent_hitm(c2c_right);
881
882 return per_left - per_right;
883}
884
Jiri Olsa9cb35002016-05-04 12:16:50 +0200885static struct c2c_stats *he_stats(struct hist_entry *he)
886{
887 struct c2c_hist_entry *c2c_he;
888
889 c2c_he = container_of(he, struct c2c_hist_entry, he);
890 return &c2c_he->stats;
891}
892
893static struct c2c_stats *total_stats(struct hist_entry *he)
894{
895 struct c2c_hists *hists;
896
897 hists = container_of(he->hists, struct c2c_hists, hists);
898 return &hists->stats;
899}
900
901static double percent(int st, int tot)
902{
903 return tot ? 100. * (double) st / (double) tot : 0;
904}
905
906#define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
907
908#define PERCENT_FN(__f) \
909static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
910{ \
911 struct c2c_hists *hists; \
912 \
913 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
914 return percent(c2c_he->stats.__f, hists->stats.__f); \
915}
916
917PERCENT_FN(rmt_hitm)
918PERCENT_FN(lcl_hitm)
919PERCENT_FN(st_l1hit)
920PERCENT_FN(st_l1miss)
921
922static int
923percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
924 struct hist_entry *he)
925{
926 int width = c2c_width(fmt, hpp, he->hists);
927 double per = PERCENT(he, rmt_hitm);
928 char buf[10];
929
930 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
931}
932
933static int
934percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
935 struct hist_entry *he)
936{
937 return percent_color(fmt, hpp, he, percent_rmt_hitm);
938}
939
940static int64_t
941percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
942 struct hist_entry *left, struct hist_entry *right)
943{
944 double per_left;
945 double per_right;
946
947 per_left = PERCENT(left, lcl_hitm);
948 per_right = PERCENT(right, lcl_hitm);
949
950 return per_left - per_right;
951}
952
953static int
954percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
955 struct hist_entry *he)
956{
957 int width = c2c_width(fmt, hpp, he->hists);
958 double per = PERCENT(he, lcl_hitm);
959 char buf[10];
960
961 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
962}
963
964static int
965percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
966 struct hist_entry *he)
967{
968 return percent_color(fmt, hpp, he, percent_lcl_hitm);
969}
970
971static int64_t
972percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
973 struct hist_entry *left, struct hist_entry *right)
974{
975 double per_left;
976 double per_right;
977
978 per_left = PERCENT(left, lcl_hitm);
979 per_right = PERCENT(right, lcl_hitm);
980
981 return per_left - per_right;
982}
983
984static int
985percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
986 struct hist_entry *he)
987{
988 int width = c2c_width(fmt, hpp, he->hists);
989 double per = PERCENT(he, st_l1hit);
990 char buf[10];
991
992 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
993}
994
995static int
996percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
997 struct hist_entry *he)
998{
999 return percent_color(fmt, hpp, he, percent_st_l1hit);
1000}
1001
1002static int64_t
1003percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1004 struct hist_entry *left, struct hist_entry *right)
1005{
1006 double per_left;
1007 double per_right;
1008
1009 per_left = PERCENT(left, st_l1hit);
1010 per_right = PERCENT(right, st_l1hit);
1011
1012 return per_left - per_right;
1013}
1014
1015static int
1016percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1017 struct hist_entry *he)
1018{
1019 int width = c2c_width(fmt, hpp, he->hists);
1020 double per = PERCENT(he, st_l1miss);
1021 char buf[10];
1022
1023 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1024}
1025
1026static int
1027percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1028 struct hist_entry *he)
1029{
1030 return percent_color(fmt, hpp, he, percent_st_l1miss);
1031}
1032
1033static int64_t
1034percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1035 struct hist_entry *left, struct hist_entry *right)
1036{
1037 double per_left;
1038 double per_right;
1039
1040 per_left = PERCENT(left, st_l1miss);
1041 per_right = PERCENT(right, st_l1miss);
1042
1043 return per_left - per_right;
1044}
1045
Jiri Olsa6c70f542016-05-28 12:30:13 +02001046STAT_FN(lcl_dram)
1047STAT_FN(rmt_dram)
1048
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001049static int
1050pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1051 struct hist_entry *he)
1052{
1053 int width = c2c_width(fmt, hpp, he->hists);
1054
1055 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1056}
1057
1058static int64_t
1059pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1060 struct hist_entry *left, struct hist_entry *right)
1061{
1062 return left->thread->pid_ - right->thread->pid_;
1063}
1064
Jiri Olsa1e181b92016-06-03 15:40:28 +02001065static int64_t
1066empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1067 struct hist_entry *left __maybe_unused,
1068 struct hist_entry *right __maybe_unused)
1069{
1070 return 0;
1071}
1072
1073static int
1074node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1075 struct hist_entry *he)
1076{
1077 struct c2c_hist_entry *c2c_he;
1078 bool first = true;
1079 int node;
1080 int ret = 0;
1081
1082 c2c_he = container_of(he, struct c2c_hist_entry, he);
1083
1084 for (node = 0; node < c2c.nodes_cnt; node++) {
1085 DECLARE_BITMAP(set, c2c.cpus_cnt);
1086
1087 bitmap_zero(set, c2c.cpus_cnt);
1088 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1089
1090 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1091 if (c2c.node_info == 1) {
1092 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1093 advance_hpp(hpp, ret);
1094 }
1095 continue;
1096 }
1097
1098 if (!first) {
1099 ret = scnprintf(hpp->buf, hpp->size, " ");
1100 advance_hpp(hpp, ret);
1101 }
1102
1103 switch (c2c.node_info) {
1104 case 0:
1105 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1106 advance_hpp(hpp, ret);
1107 break;
1108 case 1:
1109 {
Jiri Olsa67260e82019-08-20 16:02:19 +02001110 int num = bitmap_weight(set, c2c.cpus_cnt);
Jiri Olsa1e181b92016-06-03 15:40:28 +02001111 struct c2c_stats *stats = &c2c_he->node_stats[node];
1112
1113 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1114 advance_hpp(hpp, ret);
1115
Jiri Olsa55b95772016-05-29 10:21:45 +02001116 #define DISPLAY_HITM(__h) \
1117 if (c2c_he->stats.__h> 0) { \
1118 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1119 percent(stats->__h, c2c_he->stats.__h));\
1120 } else { \
1121 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
Jiri Olsa1e181b92016-06-03 15:40:28 +02001122 }
1123
Jiri Olsa55b95772016-05-29 10:21:45 +02001124 switch (c2c.display) {
1125 case DISPLAY_RMT:
1126 DISPLAY_HITM(rmt_hitm);
1127 break;
1128 case DISPLAY_LCL:
1129 DISPLAY_HITM(lcl_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001130 break;
1131 case DISPLAY_TOT:
1132 DISPLAY_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001133 default:
1134 break;
1135 }
1136
1137 #undef DISPLAY_HITM
1138
Jiri Olsa1e181b92016-06-03 15:40:28 +02001139 advance_hpp(hpp, ret);
1140
1141 if (c2c_he->stats.store > 0) {
1142 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1143 percent(stats->store, c2c_he->stats.store));
1144 } else {
1145 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1146 }
1147
1148 advance_hpp(hpp, ret);
1149 break;
1150 }
1151 case 2:
1152 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1153 advance_hpp(hpp, ret);
1154
1155 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1156 advance_hpp(hpp, ret);
1157
1158 ret = scnprintf(hpp->buf, hpp->size, "}");
1159 advance_hpp(hpp, ret);
1160 break;
1161 default:
1162 break;
1163 }
1164
1165 first = false;
1166 }
1167
1168 return 0;
1169}
1170
Jiri Olsa92062d52016-06-05 13:40:53 +02001171static int
1172mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1173 struct hist_entry *he, double mean)
1174{
1175 int width = c2c_width(fmt, hpp, he->hists);
1176 char buf[10];
1177
1178 scnprintf(buf, 10, "%6.0f", mean);
1179 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1180}
1181
1182#define MEAN_ENTRY(__func, __val) \
1183static int \
1184__func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1185{ \
1186 struct c2c_hist_entry *c2c_he; \
1187 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1188 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1189}
1190
1191MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1192MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1193MEAN_ENTRY(mean_load_entry, load);
1194
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001195static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001196cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001197 struct hist_entry *he)
1198{
1199 struct c2c_hist_entry *c2c_he;
1200 int width = c2c_width(fmt, hpp, he->hists);
1201 char buf[10];
1202
1203 c2c_he = container_of(he, struct c2c_hist_entry, he);
1204
1205 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1206 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1207}
1208
Jiri Olsabb342dae2016-07-06 15:40:09 +02001209static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001210cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001211 struct hist_entry *he)
1212{
1213 struct c2c_hist_entry *c2c_he;
1214 int width = c2c_width(fmt, hpp, he->hists);
1215 char buf[10];
1216
1217 c2c_he = container_of(he, struct c2c_hist_entry, he);
1218
1219 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1220 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1221}
1222
1223static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001224cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001225 struct hist_entry *he)
1226{
1227 int width = c2c_width(fmt, hpp, he->hists);
1228
1229 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1230}
1231
Jiri Olsa600a8cf2016-09-22 17:36:47 +02001232#define HEADER_LOW(__h) \
1233 { \
1234 .line[1] = { \
1235 .text = __h, \
1236 }, \
1237 }
1238
1239#define HEADER_BOTH(__h0, __h1) \
1240 { \
1241 .line[0] = { \
1242 .text = __h0, \
1243 }, \
1244 .line[1] = { \
1245 .text = __h1, \
1246 }, \
1247 }
1248
1249#define HEADER_SPAN(__h0, __h1, __s) \
1250 { \
1251 .line[0] = { \
1252 .text = __h0, \
1253 .span = __s, \
1254 }, \
1255 .line[1] = { \
1256 .text = __h1, \
1257 }, \
1258 }
1259
1260#define HEADER_SPAN_LOW(__h) \
1261 { \
1262 .line[1] = { \
1263 .text = __h, \
1264 }, \
1265 }
1266
Jiri Olsacbb88502016-09-22 17:36:48 +02001267static struct c2c_dimension dim_dcacheline = {
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001268 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
Jiri Olsacbb88502016-09-22 17:36:48 +02001269 .name = "dcacheline",
1270 .cmp = dcacheline_cmp,
1271 .entry = dcacheline_entry,
1272 .width = 18,
1273};
1274
Jiri Olsa7f834c22018-03-09 11:14:40 +01001275static struct c2c_dimension dim_dcacheline_node = {
1276 .header = HEADER_LOW("Node"),
1277 .name = "dcacheline_node",
1278 .cmp = empty_cmp,
1279 .entry = dcacheline_node_entry,
1280 .width = 4,
1281};
1282
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001283static struct c2c_dimension dim_dcacheline_count = {
1284 .header = HEADER_LOW("PA cnt"),
1285 .name = "dcacheline_count",
1286 .cmp = empty_cmp,
1287 .entry = dcacheline_node_count,
1288 .width = 6,
1289};
1290
1291static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01001292
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001293static struct c2c_dimension dim_offset = {
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001294 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001295 .name = "offset",
1296 .cmp = offset_cmp,
1297 .entry = offset_entry,
1298 .width = 18,
1299};
1300
Jiri Olsa7f834c22018-03-09 11:14:40 +01001301static struct c2c_dimension dim_offset_node = {
1302 .header = HEADER_LOW("Node"),
1303 .name = "offset_node",
1304 .cmp = empty_cmp,
1305 .entry = dcacheline_node_entry,
1306 .width = 4,
1307};
1308
Jiri Olsa43575a92016-05-03 21:48:56 +02001309static struct c2c_dimension dim_iaddr = {
1310 .header = HEADER_LOW("Code address"),
1311 .name = "iaddr",
1312 .cmp = iaddr_cmp,
1313 .entry = iaddr_entry,
1314 .width = 18,
1315};
1316
Jiri Olsa97cb4862016-05-23 16:20:14 +02001317static struct c2c_dimension dim_tot_hitm = {
1318 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1319 .name = "tot_hitm",
1320 .cmp = tot_hitm_cmp,
1321 .entry = tot_hitm_entry,
1322 .width = 7,
1323};
1324
1325static struct c2c_dimension dim_lcl_hitm = {
1326 .header = HEADER_SPAN_LOW("Lcl"),
1327 .name = "lcl_hitm",
1328 .cmp = lcl_hitm_cmp,
1329 .entry = lcl_hitm_entry,
1330 .width = 7,
1331};
1332
1333static struct c2c_dimension dim_rmt_hitm = {
1334 .header = HEADER_SPAN_LOW("Rmt"),
1335 .name = "rmt_hitm",
1336 .cmp = rmt_hitm_cmp,
1337 .entry = rmt_hitm_entry,
1338 .width = 7,
1339};
1340
1341static struct c2c_dimension dim_cl_rmt_hitm = {
1342 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1343 .name = "cl_rmt_hitm",
1344 .cmp = rmt_hitm_cmp,
1345 .entry = rmt_hitm_entry,
1346 .width = 7,
1347};
1348
1349static struct c2c_dimension dim_cl_lcl_hitm = {
1350 .header = HEADER_SPAN_LOW("Lcl"),
1351 .name = "cl_lcl_hitm",
1352 .cmp = lcl_hitm_cmp,
1353 .entry = lcl_hitm_entry,
1354 .width = 7,
1355};
1356
Jiri Olsa0f188962016-05-04 10:10:11 +02001357static struct c2c_dimension dim_stores = {
1358 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1359 .name = "stores",
1360 .cmp = store_cmp,
1361 .entry = store_entry,
1362 .width = 7,
1363};
1364
1365static struct c2c_dimension dim_stores_l1hit = {
1366 .header = HEADER_SPAN_LOW("L1Hit"),
1367 .name = "stores_l1hit",
1368 .cmp = st_l1hit_cmp,
1369 .entry = st_l1hit_entry,
1370 .width = 7,
1371};
1372
1373static struct c2c_dimension dim_stores_l1miss = {
1374 .header = HEADER_SPAN_LOW("L1Miss"),
1375 .name = "stores_l1miss",
1376 .cmp = st_l1miss_cmp,
1377 .entry = st_l1miss_entry,
1378 .width = 7,
1379};
1380
1381static struct c2c_dimension dim_cl_stores_l1hit = {
1382 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1383 .name = "cl_stores_l1hit",
1384 .cmp = st_l1hit_cmp,
1385 .entry = st_l1hit_entry,
1386 .width = 7,
1387};
1388
1389static struct c2c_dimension dim_cl_stores_l1miss = {
1390 .header = HEADER_SPAN_LOW("L1 Miss"),
1391 .name = "cl_stores_l1miss",
1392 .cmp = st_l1miss_cmp,
1393 .entry = st_l1miss_entry,
1394 .width = 7,
1395};
1396
Jiri Olsa1295f682016-05-04 10:18:24 +02001397static struct c2c_dimension dim_ld_fbhit = {
1398 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1399 .name = "ld_fbhit",
1400 .cmp = ld_fbhit_cmp,
1401 .entry = ld_fbhit_entry,
1402 .width = 7,
1403};
1404
1405static struct c2c_dimension dim_ld_l1hit = {
1406 .header = HEADER_SPAN_LOW("L1"),
1407 .name = "ld_l1hit",
1408 .cmp = ld_l1hit_cmp,
1409 .entry = ld_l1hit_entry,
1410 .width = 7,
1411};
1412
1413static struct c2c_dimension dim_ld_l2hit = {
1414 .header = HEADER_SPAN_LOW("L2"),
1415 .name = "ld_l2hit",
1416 .cmp = ld_l2hit_cmp,
1417 .entry = ld_l2hit_entry,
1418 .width = 7,
1419};
1420
Jiri Olsa4d089102016-05-04 10:27:51 +02001421static struct c2c_dimension dim_ld_llchit = {
1422 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1423 .name = "ld_lclhit",
1424 .cmp = ld_llchit_cmp,
1425 .entry = ld_llchit_entry,
1426 .width = 8,
1427};
1428
1429static struct c2c_dimension dim_ld_rmthit = {
1430 .header = HEADER_SPAN_LOW("Rmt"),
1431 .name = "ld_rmthit",
1432 .cmp = rmt_hit_cmp,
1433 .entry = rmt_hit_entry,
1434 .width = 8,
1435};
1436
Jiri Olsa04402d22016-05-19 10:10:51 +02001437static struct c2c_dimension dim_ld_llcmiss = {
1438 .header = HEADER_BOTH("LLC", "Ld Miss"),
1439 .name = "ld_llcmiss",
1440 .cmp = ld_llcmiss_cmp,
1441 .entry = ld_llcmiss_entry,
1442 .width = 7,
1443};
1444
Jiri Olsa01b84d72016-05-04 10:35:29 +02001445static struct c2c_dimension dim_tot_recs = {
1446 .header = HEADER_BOTH("Total", "records"),
1447 .name = "tot_recs",
1448 .cmp = tot_recs_cmp,
1449 .entry = tot_recs_entry,
1450 .width = 7,
1451};
1452
Jiri Olsa55177c42016-05-19 09:52:37 +02001453static struct c2c_dimension dim_tot_loads = {
1454 .header = HEADER_BOTH("Total", "Loads"),
1455 .name = "tot_loads",
1456 .cmp = tot_loads_cmp,
1457 .entry = tot_loads_entry,
1458 .width = 7,
1459};
1460
Jiri Olsa55b95772016-05-29 10:21:45 +02001461static struct c2c_header percent_hitm_header[] = {
1462 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1463 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
Jiri Olsad940bac2016-11-21 22:33:30 +01001464 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
Jiri Olsa55b95772016-05-29 10:21:45 +02001465};
1466
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001467static struct c2c_dimension dim_percent_hitm = {
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001468 .name = "percent_hitm",
1469 .cmp = percent_hitm_cmp,
1470 .entry = percent_hitm_entry,
1471 .color = percent_hitm_color,
1472 .width = 7,
1473};
1474
Jiri Olsa9cb35002016-05-04 12:16:50 +02001475static struct c2c_dimension dim_percent_rmt_hitm = {
1476 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1477 .name = "percent_rmt_hitm",
1478 .cmp = percent_rmt_hitm_cmp,
1479 .entry = percent_rmt_hitm_entry,
1480 .color = percent_rmt_hitm_color,
1481 .width = 7,
1482};
1483
1484static struct c2c_dimension dim_percent_lcl_hitm = {
1485 .header = HEADER_SPAN_LOW("Lcl"),
1486 .name = "percent_lcl_hitm",
1487 .cmp = percent_lcl_hitm_cmp,
1488 .entry = percent_lcl_hitm_entry,
1489 .color = percent_lcl_hitm_color,
1490 .width = 7,
1491};
1492
1493static struct c2c_dimension dim_percent_stores_l1hit = {
1494 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1495 .name = "percent_stores_l1hit",
1496 .cmp = percent_stores_l1hit_cmp,
1497 .entry = percent_stores_l1hit_entry,
1498 .color = percent_stores_l1hit_color,
1499 .width = 7,
1500};
1501
1502static struct c2c_dimension dim_percent_stores_l1miss = {
1503 .header = HEADER_SPAN_LOW("L1 Miss"),
1504 .name = "percent_stores_l1miss",
1505 .cmp = percent_stores_l1miss_cmp,
1506 .entry = percent_stores_l1miss_entry,
1507 .color = percent_stores_l1miss_color,
1508 .width = 7,
1509};
1510
Jiri Olsa6c70f542016-05-28 12:30:13 +02001511static struct c2c_dimension dim_dram_lcl = {
1512 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1513 .name = "dram_lcl",
1514 .cmp = lcl_dram_cmp,
1515 .entry = lcl_dram_entry,
1516 .width = 8,
1517};
1518
1519static struct c2c_dimension dim_dram_rmt = {
1520 .header = HEADER_SPAN_LOW("Rmt"),
1521 .name = "dram_rmt",
1522 .cmp = rmt_dram_cmp,
1523 .entry = rmt_dram_entry,
1524 .width = 8,
1525};
1526
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001527static struct c2c_dimension dim_pid = {
1528 .header = HEADER_LOW("Pid"),
1529 .name = "pid",
1530 .cmp = pid_cmp,
1531 .entry = pid_entry,
1532 .width = 7,
1533};
1534
Jiri Olsae87019c2016-05-25 08:50:10 +02001535static struct c2c_dimension dim_tid = {
1536 .header = HEADER_LOW("Tid"),
1537 .name = "tid",
1538 .se = &sort_thread,
1539};
1540
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001541static struct c2c_dimension dim_symbol = {
1542 .name = "symbol",
1543 .se = &sort_sym,
1544};
1545
1546static struct c2c_dimension dim_dso = {
1547 .header = HEADER_BOTH("Shared", "Object"),
1548 .name = "dso",
1549 .se = &sort_dso,
1550};
1551
Jiri Olsa1e181b92016-06-03 15:40:28 +02001552static struct c2c_header header_node[3] = {
1553 HEADER_LOW("Node"),
1554 HEADER_LOW("Node{cpus %hitms %stores}"),
1555 HEADER_LOW("Node{cpu list}"),
1556};
1557
1558static struct c2c_dimension dim_node = {
1559 .name = "node",
1560 .cmp = empty_cmp,
1561 .entry = node_entry,
1562 .width = 4,
1563};
1564
Jiri Olsa92062d52016-06-05 13:40:53 +02001565static struct c2c_dimension dim_mean_rmt = {
1566 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1567 .name = "mean_rmt",
1568 .cmp = empty_cmp,
1569 .entry = mean_rmt_entry,
1570 .width = 8,
1571};
1572
1573static struct c2c_dimension dim_mean_lcl = {
1574 .header = HEADER_SPAN_LOW("lcl hitm"),
1575 .name = "mean_lcl",
1576 .cmp = empty_cmp,
1577 .entry = mean_lcl_entry,
1578 .width = 8,
1579};
1580
1581static struct c2c_dimension dim_mean_load = {
1582 .header = HEADER_SPAN_LOW("load"),
1583 .name = "mean_load",
1584 .cmp = empty_cmp,
1585 .entry = mean_load_entry,
1586 .width = 8,
1587};
1588
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001589static struct c2c_dimension dim_cpucnt = {
1590 .header = HEADER_BOTH("cpu", "cnt"),
1591 .name = "cpucnt",
1592 .cmp = empty_cmp,
1593 .entry = cpucnt_entry,
1594 .width = 8,
1595};
1596
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001597static struct c2c_dimension dim_srcline = {
1598 .name = "cl_srcline",
1599 .se = &sort_srcline,
1600};
1601
Jiri Olsabb342dae2016-07-06 15:40:09 +02001602static struct c2c_dimension dim_dcacheline_idx = {
1603 .header = HEADER_LOW("Index"),
1604 .name = "cl_idx",
1605 .cmp = empty_cmp,
1606 .entry = cl_idx_entry,
1607 .width = 5,
1608};
1609
1610static struct c2c_dimension dim_dcacheline_num = {
1611 .header = HEADER_LOW("Num"),
1612 .name = "cl_num",
1613 .cmp = empty_cmp,
1614 .entry = cl_idx_entry,
1615 .width = 5,
1616};
1617
1618static struct c2c_dimension dim_dcacheline_num_empty = {
1619 .header = HEADER_LOW("Num"),
1620 .name = "cl_num_empty",
1621 .cmp = empty_cmp,
1622 .entry = cl_idx_empty_entry,
1623 .width = 5,
1624};
1625
Jiri Olsac75540e2016-09-22 17:36:41 +02001626static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +02001627 &dim_dcacheline,
Jiri Olsa7f834c22018-03-09 11:14:40 +01001628 &dim_dcacheline_node,
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001629 &dim_dcacheline_count,
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001630 &dim_offset,
Jiri Olsa7f834c22018-03-09 11:14:40 +01001631 &dim_offset_node,
Jiri Olsa43575a92016-05-03 21:48:56 +02001632 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +02001633 &dim_tot_hitm,
1634 &dim_lcl_hitm,
1635 &dim_rmt_hitm,
1636 &dim_cl_lcl_hitm,
1637 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +02001638 &dim_stores,
1639 &dim_stores_l1hit,
1640 &dim_stores_l1miss,
1641 &dim_cl_stores_l1hit,
1642 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +02001643 &dim_ld_fbhit,
1644 &dim_ld_l1hit,
1645 &dim_ld_l2hit,
Jiri Olsa4d089102016-05-04 10:27:51 +02001646 &dim_ld_llchit,
1647 &dim_ld_rmthit,
Jiri Olsa04402d22016-05-19 10:10:51 +02001648 &dim_ld_llcmiss,
Jiri Olsa01b84d72016-05-04 10:35:29 +02001649 &dim_tot_recs,
Jiri Olsa55177c42016-05-19 09:52:37 +02001650 &dim_tot_loads,
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001651 &dim_percent_hitm,
Jiri Olsa9cb35002016-05-04 12:16:50 +02001652 &dim_percent_rmt_hitm,
1653 &dim_percent_lcl_hitm,
1654 &dim_percent_stores_l1hit,
1655 &dim_percent_stores_l1miss,
Jiri Olsa6c70f542016-05-28 12:30:13 +02001656 &dim_dram_lcl,
1657 &dim_dram_rmt,
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001658 &dim_pid,
Jiri Olsae87019c2016-05-25 08:50:10 +02001659 &dim_tid,
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001660 &dim_symbol,
1661 &dim_dso,
Jiri Olsa1e181b92016-06-03 15:40:28 +02001662 &dim_node,
Jiri Olsa92062d52016-06-05 13:40:53 +02001663 &dim_mean_rmt,
1664 &dim_mean_lcl,
1665 &dim_mean_load,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001666 &dim_cpucnt,
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001667 &dim_srcline,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001668 &dim_dcacheline_idx,
1669 &dim_dcacheline_num,
1670 &dim_dcacheline_num_empty,
Jiri Olsac75540e2016-09-22 17:36:41 +02001671 NULL,
1672};
1673
1674static void fmt_free(struct perf_hpp_fmt *fmt)
1675{
1676 struct c2c_fmt *c2c_fmt;
1677
1678 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1679 free(c2c_fmt);
1680}
1681
1682static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1683{
1684 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1685 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1686
1687 return c2c_a->dim == c2c_b->dim;
1688}
1689
1690static struct c2c_dimension *get_dimension(const char *name)
1691{
1692 unsigned int i;
1693
1694 for (i = 0; dimensions[i]; i++) {
1695 struct c2c_dimension *dim = dimensions[i];
1696
1697 if (!strcmp(dim->name, name))
1698 return dim;
1699 };
1700
1701 return NULL;
1702}
1703
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001704static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1705 struct hist_entry *he)
1706{
1707 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1708 struct c2c_dimension *dim = c2c_fmt->dim;
1709 size_t len = fmt->user_len;
1710
Jiri Olsa590b6a32016-07-10 16:25:15 +02001711 if (!len) {
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001712 len = hists__col_len(he->hists, dim->se->se_width_idx);
1713
Jiri Olsa590b6a32016-07-10 16:25:15 +02001714 if (dim == &dim_symbol || dim == &dim_srcline)
1715 len = symbol_width(he->hists, dim->se);
1716 }
1717
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001718 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1719}
1720
1721static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1722 struct hist_entry *a, struct hist_entry *b)
1723{
1724 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1725 struct c2c_dimension *dim = c2c_fmt->dim;
1726
1727 return dim->se->se_cmp(a, b);
1728}
1729
1730static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1731 struct hist_entry *a, struct hist_entry *b)
1732{
1733 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1734 struct c2c_dimension *dim = c2c_fmt->dim;
1735 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1736
1737 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1738 return collapse_fn(a, b);
1739}
1740
Jiri Olsac75540e2016-09-22 17:36:41 +02001741static struct c2c_fmt *get_format(const char *name)
1742{
1743 struct c2c_dimension *dim = get_dimension(name);
1744 struct c2c_fmt *c2c_fmt;
1745 struct perf_hpp_fmt *fmt;
1746
1747 if (!dim)
1748 return NULL;
1749
1750 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1751 if (!c2c_fmt)
1752 return NULL;
1753
1754 c2c_fmt->dim = dim;
1755
1756 fmt = &c2c_fmt->fmt;
1757 INIT_LIST_HEAD(&fmt->list);
1758 INIT_LIST_HEAD(&fmt->sort_list);
1759
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001760 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1761 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
Jiri Olsa9cb35002016-05-04 12:16:50 +02001762 fmt->color = dim->se ? NULL : dim->color;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001763 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +02001764 fmt->header = c2c_header;
1765 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001766 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +02001767 fmt->equal = fmt_equal;
1768 fmt->free = fmt_free;
1769
1770 return c2c_fmt;
1771}
1772
1773static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1774{
1775 struct c2c_fmt *c2c_fmt = get_format(name);
1776
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001777 if (!c2c_fmt) {
1778 reset_dimensions();
1779 return output_field_add(hpp_list, name);
1780 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001781
1782 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1783 return 0;
1784}
1785
1786static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1787{
1788 struct c2c_fmt *c2c_fmt = get_format(name);
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001789 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +02001790
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001791 if (!c2c_fmt) {
1792 reset_dimensions();
1793 return sort_dimension__add(hpp_list, name, NULL, 0);
1794 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001795
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001796 dim = c2c_fmt->dim;
1797 if (dim == &dim_dso)
1798 hpp_list->dso = 1;
1799
Jiri Olsac75540e2016-09-22 17:36:41 +02001800 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1801 return 0;
1802}
1803
1804#define PARSE_LIST(_list, _fn) \
1805 do { \
1806 char *tmp, *tok; \
1807 ret = 0; \
1808 \
1809 if (!_list) \
1810 break; \
1811 \
1812 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1813 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1814 ret = _fn(hpp_list, tok); \
1815 if (ret == -EINVAL) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001816 pr_err("Invalid --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001817 break; \
1818 } else if (ret == -ESRCH) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001819 pr_err("Unknown --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001820 break; \
1821 } \
1822 } \
1823 } while (0)
1824
1825static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1826 const char *output_,
1827 const char *sort_)
1828{
1829 char *output = output_ ? strdup(output_) : NULL;
1830 char *sort = sort_ ? strdup(sort_) : NULL;
1831 int ret;
1832
1833 PARSE_LIST(output, c2c_hists__init_output);
1834 PARSE_LIST(sort, c2c_hists__init_sort);
1835
1836 /* copy sort keys to output fields */
1837 perf_hpp__setup_output_field(hpp_list);
1838
1839 /*
1840 * We dont need other sorting keys other than those
1841 * we already specified. It also really slows down
1842 * the processing a lot with big number of output
1843 * fields, so switching this off for c2c.
1844 */
1845
1846#if 0
1847 /* and then copy output fields to sort keys */
1848 perf_hpp__append_sort_keys(&hists->list);
1849#endif
1850
1851 free(output);
1852 free(sort);
1853 return ret;
1854}
1855
1856static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001857 const char *sort,
1858 int nr_header_lines)
Jiri Olsac75540e2016-09-22 17:36:41 +02001859{
1860 __hists__init(&hists->hists, &hists->list);
1861
1862 /*
1863 * Initialize only with sort fields, we need to resort
1864 * later anyway, and that's where we add output fields
1865 * as well.
1866 */
1867 perf_hpp_list__init(&hists->list);
1868
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001869 /* Overload number of header lines.*/
1870 hists->list.nr_header_lines = nr_header_lines;
1871
Jiri Olsac75540e2016-09-22 17:36:41 +02001872 return hpp_list__parse(&hists->list, NULL, sort);
1873}
1874
Jiri Olsac75540e2016-09-22 17:36:41 +02001875static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1876 const char *output,
1877 const char *sort)
1878{
1879 perf_hpp__reset_output_field(&c2c_hists->list);
1880 return hpp_list__parse(&c2c_hists->list, output, sort);
1881}
1882
Jiri Olsac4a75bb2018-12-28 11:18:20 +01001883#define DISPLAY_LINE_LIMIT 0.001
Jiri Olsa9857b712016-08-17 14:55:23 +02001884
1885static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1886{
1887 struct c2c_hist_entry *c2c_he;
1888 double ld_dist;
1889
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02001890 if (c2c.show_all)
1891 return true;
Jiri Olsa9857b712016-08-17 14:55:23 +02001892
1893 c2c_he = container_of(he, struct c2c_hist_entry, he);
1894
Jiri Olsa55b95772016-05-29 10:21:45 +02001895#define FILTER_HITM(__h) \
1896 if (stats->__h) { \
1897 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1898 if (ld_dist < DISPLAY_LINE_LIMIT) \
1899 he->filtered = HIST_FILTER__C2C; \
1900 } else { \
1901 he->filtered = HIST_FILTER__C2C; \
Jiri Olsa9857b712016-08-17 14:55:23 +02001902 }
1903
Jiri Olsa55b95772016-05-29 10:21:45 +02001904 switch (c2c.display) {
1905 case DISPLAY_LCL:
1906 FILTER_HITM(lcl_hitm);
1907 break;
1908 case DISPLAY_RMT:
1909 FILTER_HITM(rmt_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001910 break;
1911 case DISPLAY_TOT:
1912 FILTER_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001913 default:
1914 break;
1915 };
1916
1917#undef FILTER_HITM
1918
Jiri Olsa9857b712016-08-17 14:55:23 +02001919 return he->filtered == 0;
1920}
1921
1922static inline int valid_hitm_or_store(struct hist_entry *he)
1923{
1924 struct c2c_hist_entry *c2c_he;
Jiri Olsa55b95772016-05-29 10:21:45 +02001925 bool has_hitm;
Jiri Olsa9857b712016-08-17 14:55:23 +02001926
1927 c2c_he = container_of(he, struct c2c_hist_entry, he);
Jiri Olsad940bac2016-11-21 22:33:30 +01001928 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1929 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1930 c2c_he->stats.rmt_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +02001931 return has_hitm || c2c_he->stats.store;
Jiri Olsa9857b712016-08-17 14:55:23 +02001932}
1933
Jiri Olsa7f834c22018-03-09 11:14:40 +01001934static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1935{
1936 struct c2c_dimension *dim;
1937
1938 dim = &c2c.hists == c2c_he->hists ?
1939 &dim_dcacheline_node : &dim_offset_node;
1940
1941 if (len > dim->width)
1942 dim->width = len;
1943}
1944
1945static int set_nodestr(struct c2c_hist_entry *c2c_he)
1946{
1947 char buf[30];
1948 int len;
1949
1950 if (c2c_he->nodestr)
1951 return 0;
1952
1953 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1954 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1955 buf, sizeof(buf));
1956 } else {
1957 len = scnprintf(buf, sizeof(buf), "N/A");
1958 }
1959
1960 set_node_width(c2c_he, len);
1961 c2c_he->nodestr = strdup(buf);
1962 return c2c_he->nodestr ? 0 : -ENOMEM;
1963}
1964
Jiri Olsa37731382018-03-09 11:14:38 +01001965static void calc_width(struct c2c_hist_entry *c2c_he)
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001966{
1967 struct c2c_hists *c2c_hists;
1968
Jiri Olsa37731382018-03-09 11:14:38 +01001969 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1970 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
Jiri Olsa7f834c22018-03-09 11:14:40 +01001971 set_nodestr(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001972}
1973
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001974static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001975{
Jiri Olsa37731382018-03-09 11:14:38 +01001976 struct c2c_hist_entry *c2c_he;
1977
1978 c2c_he = container_of(he, struct c2c_hist_entry, he);
1979
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001980 if (c2c.show_src && !he->srcline)
Arnaldo Carvalho de Melo6a53da02018-05-28 11:06:58 -03001981 he->srcline = hist_entry__srcline(he);
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001982
Jiri Olsa37731382018-03-09 11:14:38 +01001983 calc_width(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001984
Jiri Olsa9857b712016-08-17 14:55:23 +02001985 if (!valid_hitm_or_store(he))
1986 he->filtered = HIST_FILTER__C2C;
1987
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001988 return 0;
1989}
1990
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001991static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001992{
1993 struct c2c_hist_entry *c2c_he;
1994 struct c2c_hists *c2c_hists;
Jiri Olsa9857b712016-08-17 14:55:23 +02001995 bool display = he__display(he, &c2c.hitm_stats);
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001996
1997 c2c_he = container_of(he, struct c2c_hist_entry, he);
1998 c2c_hists = c2c_he->hists;
1999
Jiri Olsa9857b712016-08-17 14:55:23 +02002000 if (display && c2c_hists) {
Jiri Olsabb342dae2016-07-06 15:40:09 +02002001 static unsigned int idx;
2002
2003 c2c_he->cacheline_idx = idx++;
Jiri Olsabc229c22018-03-09 11:14:39 +01002004 calc_width(c2c_he);
Jiri Olsabb342dae2016-07-06 15:40:09 +02002005
Jiri Olsafc9c6302016-05-24 14:14:38 +02002006 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002007
Jiri Olsaec06f9b2016-09-22 17:36:45 +02002008 hists__collapse_resort(&c2c_hists->hists, NULL);
2009 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2010 }
2011
2012 return 0;
2013}
2014
Jiri Olsa1e181b92016-06-03 15:40:28 +02002015static void setup_nodes_header(void)
2016{
2017 dim_node.header = header_node[c2c.node_info];
2018}
2019
2020static int setup_nodes(struct perf_session *session)
2021{
2022 struct numa_node *n;
2023 unsigned long **nodes;
2024 int node, cpu;
2025 int *cpu2node;
2026
2027 if (c2c.node_info > 2)
2028 c2c.node_info = 2;
2029
2030 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
Ravi Bangoria1ea770f2019-08-22 14:20:45 +05302031 c2c.cpus_cnt = session->header.env.nr_cpus_avail;
Jiri Olsa1e181b92016-06-03 15:40:28 +02002032
2033 n = session->header.env.numa_nodes;
2034 if (!n)
2035 return -EINVAL;
2036
2037 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2038 if (!nodes)
2039 return -ENOMEM;
2040
2041 c2c.nodes = nodes;
2042
2043 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2044 if (!cpu2node)
2045 return -ENOMEM;
2046
2047 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2048 cpu2node[cpu] = -1;
2049
2050 c2c.cpu2node = cpu2node;
2051
2052 for (node = 0; node < c2c.nodes_cnt; node++) {
Jiri Olsaf8548392019-07-21 13:23:49 +02002053 struct perf_cpu_map *map = n[node].map;
Jiri Olsa1e181b92016-06-03 15:40:28 +02002054 unsigned long *set;
2055
2056 set = bitmap_alloc(c2c.cpus_cnt);
2057 if (!set)
2058 return -ENOMEM;
2059
Jiri Olsae34c9402019-03-05 16:25:29 +01002060 nodes[node] = set;
2061
2062 /* empty node, skip */
Jiri Olsa315c0a12019-08-22 13:11:39 +02002063 if (perf_cpu_map__empty(map))
Jiri Olsae34c9402019-03-05 16:25:29 +01002064 continue;
2065
Jiri Olsa1e181b92016-06-03 15:40:28 +02002066 for (cpu = 0; cpu < map->nr; cpu++) {
2067 set_bit(map->map[cpu], set);
2068
2069 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2070 return -EINVAL;
2071
2072 cpu2node[map->map[cpu]] = node;
2073 }
Jiri Olsa1e181b92016-06-03 15:40:28 +02002074 }
2075
2076 setup_nodes_header();
2077 return 0;
2078}
2079
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002080#define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2081
Jiri Olsae4c38fd2019-02-04 15:18:06 +01002082static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused)
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002083{
2084 struct c2c_hist_entry *c2c_he;
2085 c2c_he = container_of(he, struct c2c_hist_entry, he);
2086
2087 if (HAS_HITMS(c2c_he)) {
2088 c2c.shared_clines++;
2089 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2090 }
2091
2092 return 0;
2093}
2094
2095static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2096{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002097 struct rb_node *next = rb_first_cached(&hists->entries);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002098 int ret = 0;
2099
2100 while (next) {
2101 struct hist_entry *he;
2102
2103 he = rb_entry(next, struct hist_entry, rb_node);
Jiri Olsae4c38fd2019-02-04 15:18:06 +01002104 ret = cb(he, NULL);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002105 if (ret)
2106 break;
2107 next = rb_next(&he->rb_node);
2108 }
2109
2110 return ret;
2111}
2112
Jiri Olsa74c63a22016-05-02 20:01:59 +02002113static void print_c2c__display_stats(FILE *out)
2114{
2115 int llc_misses;
2116 struct c2c_stats *stats = &c2c.hists.stats;
2117
2118 llc_misses = stats->lcl_dram +
2119 stats->rmt_dram +
2120 stats->rmt_hit +
2121 stats->rmt_hitm;
2122
2123 fprintf(out, "=================================================\n");
2124 fprintf(out, " Trace Event Information \n");
2125 fprintf(out, "=================================================\n");
2126 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2127 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2128 fprintf(out, " Load Operations : %10d\n", stats->load);
2129 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2130 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2131 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2132 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2133 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2134 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2135 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2136 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2137 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2138 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2139 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2140 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2141 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2142 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2143 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2144 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2145 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2146 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2147 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2148 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2149 fprintf(out, " Store Operations : %10d\n", stats->store);
2150 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2151 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2152 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2153 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2154 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2155 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2156}
2157
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002158static void print_shared_cacheline_info(FILE *out)
2159{
2160 struct c2c_stats *stats = &c2c.hitm_stats;
2161 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2162
2163 fprintf(out, "=================================================\n");
2164 fprintf(out, " Global Shared Cache Line Event Information \n");
2165 fprintf(out, "=================================================\n");
2166 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2167 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2168 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2169 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2170 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2171 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2172 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2173 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2174 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2175 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2176}
2177
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002178static void print_cacheline(struct c2c_hists *c2c_hists,
2179 struct hist_entry *he_cl,
2180 struct perf_hpp_list *hpp_list,
2181 FILE *out)
2182{
2183 char bf[1000];
2184 struct perf_hpp hpp = {
2185 .buf = bf,
2186 .size = 1000,
2187 };
2188 static bool once;
2189
2190 if (!once) {
2191 hists__fprintf_headers(&c2c_hists->hists, out);
2192 once = true;
2193 } else {
2194 fprintf(out, "\n");
2195 }
2196
Jiri Olsabb342dae2016-07-06 15:40:09 +02002197 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002198 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2199 fprintf(out, "%s\n", bf);
Jiri Olsabb342dae2016-07-06 15:40:09 +02002200 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002201
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -03002202 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002203}
2204
2205static void print_pareto(FILE *out)
2206{
2207 struct perf_hpp_list hpp_list;
2208 struct rb_node *nd;
2209 int ret;
2210
2211 perf_hpp_list__init(&hpp_list);
2212 ret = hpp_list__parse(&hpp_list,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002213 "cl_num,"
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002214 "cl_rmt_hitm,"
2215 "cl_lcl_hitm,"
2216 "cl_stores_l1hit,"
2217 "cl_stores_l1miss,"
2218 "dcacheline",
2219 NULL);
2220
2221 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2222 return;
2223
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002224 nd = rb_first_cached(&c2c.hists.hists.entries);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002225
2226 for (; nd; nd = rb_next(nd)) {
2227 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2228 struct c2c_hist_entry *c2c_he;
2229
2230 if (he->filtered)
2231 continue;
2232
2233 c2c_he = container_of(he, struct c2c_hist_entry, he);
2234 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2235 }
2236}
2237
Jiri Olsa2709b972016-08-27 11:40:23 +02002238static void print_c2c_info(FILE *out, struct perf_session *session)
2239{
Jiri Olsa63503db2019-07-21 13:23:52 +02002240 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002241 struct evsel *evsel;
Jiri Olsa2709b972016-08-27 11:40:23 +02002242 bool first = true;
2243
2244 fprintf(out, "=================================================\n");
2245 fprintf(out, " c2c details \n");
2246 fprintf(out, "=================================================\n");
2247
2248 evlist__for_each_entry(evlist, evsel) {
2249 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2250 perf_evsel__name(evsel));
2251 first = false;
2252 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002253 fprintf(out, " Cachelines sort on : %s HITMs\n",
Jiri Olsad940bac2016-11-21 22:33:30 +01002254 display_str[c2c.display]);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002255 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
Jiri Olsa2709b972016-08-27 11:40:23 +02002256}
2257
2258static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002259{
2260 setup_pager();
2261
Jiri Olsa74c63a22016-05-02 20:01:59 +02002262 print_c2c__display_stats(out);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002263 fprintf(out, "\n");
2264 print_shared_cacheline_info(out);
Jiri Olsa2709b972016-08-27 11:40:23 +02002265 fprintf(out, "\n");
2266 print_c2c_info(out, session);
Jiri Olsa74c63a22016-05-02 20:01:59 +02002267
2268 if (c2c.stats_only)
2269 return;
2270
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002271 fprintf(out, "\n");
2272 fprintf(out, "=================================================\n");
2273 fprintf(out, " Shared Data Cache Line Table \n");
2274 fprintf(out, "=================================================\n");
2275 fprintf(out, "#\n");
2276
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -03002277 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002278
2279 fprintf(out, "\n");
2280 fprintf(out, "=================================================\n");
2281 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2282 fprintf(out, "=================================================\n");
2283 fprintf(out, "#\n");
2284
2285 print_pareto(out);
2286}
Jiri Olsa1e181b92016-06-03 15:40:28 +02002287
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002288#ifdef HAVE_SLANG_SUPPORT
2289static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2290{
2291 u64 nr_entries = 0;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002292 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002293
2294 while (nd) {
2295 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2296
2297 if (!he->filtered)
2298 nr_entries++;
2299
2300 nd = rb_next(nd);
2301 }
2302
2303 hb->nr_non_filtered_entries = nr_entries;
2304}
2305
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002306struct c2c_cacheline_browser {
2307 struct hist_browser hb;
2308 struct hist_entry *he;
2309};
2310
2311static int
2312perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2313 char *bf, size_t size)
2314{
2315 struct c2c_cacheline_browser *cl_browser;
2316 struct hist_entry *he;
2317 uint64_t addr = 0;
2318
2319 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2320 he = cl_browser->he;
2321
2322 if (he->mem_info)
2323 addr = cl_address(he->mem_info->daddr.addr);
2324
2325 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2326 return 0;
2327}
2328
2329static struct c2c_cacheline_browser*
2330c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2331{
2332 struct c2c_cacheline_browser *browser;
2333
2334 browser = zalloc(sizeof(*browser));
2335 if (browser) {
2336 hist_browser__init(&browser->hb, hists);
2337 browser->hb.c2c_filter = true;
2338 browser->hb.title = perf_c2c_cacheline_browser__title;
2339 browser->he = he;
2340 }
2341
2342 return browser;
2343}
2344
2345static int perf_c2c__browse_cacheline(struct hist_entry *he)
2346{
2347 struct c2c_hist_entry *c2c_he;
2348 struct c2c_hists *c2c_hists;
2349 struct c2c_cacheline_browser *cl_browser;
2350 struct hist_browser *browser;
2351 int key = -1;
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +01002352 static const char help[] =
Kim Phillips239fb4f2017-11-14 15:04:47 -06002353 " ENTER Toggle callchains (if present) \n"
2354 " n Toggle Node details info \n"
2355 " s Toggle full length of symbol and source line columns \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002356 " q Return back to cacheline list \n";
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002357
Jiri Olsa73978332018-07-24 08:20:08 +02002358 if (!he)
2359 return 0;
2360
Jiri Olsa590b6a32016-07-10 16:25:15 +02002361 /* Display compact version first. */
2362 c2c.symbol_full = false;
2363
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002364 c2c_he = container_of(he, struct c2c_hist_entry, he);
2365 c2c_hists = c2c_he->hists;
2366
2367 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2368 if (cl_browser == NULL)
2369 return -1;
2370
2371 browser = &cl_browser->hb;
2372
2373 /* reset abort key so that it can get Ctrl-C as a key */
2374 SLang_reset_tty();
2375 SLang_init_tty(0, 0, 0);
2376
2377 c2c_browser__update_nr_entries(browser);
2378
2379 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002380 key = hist_browser__run(browser, "? - help", true);
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002381
2382 switch (key) {
Jiri Olsa590b6a32016-07-10 16:25:15 +02002383 case 's':
2384 c2c.symbol_full = !c2c.symbol_full;
2385 break;
Jiri Olsa1a56a422016-07-10 16:30:27 +02002386 case 'n':
2387 c2c.node_info = (c2c.node_info + 1) % 3;
2388 setup_nodes_header();
2389 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002390 case 'q':
2391 goto out;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002392 case '?':
2393 ui_browser__help_window(&browser->b, help);
2394 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002395 default:
2396 break;
2397 }
2398 }
2399
2400out:
2401 free(cl_browser);
2402 return 0;
2403}
2404
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002405static int perf_c2c_browser__title(struct hist_browser *browser,
2406 char *bf, size_t size)
2407{
2408 scnprintf(bf, size,
Jiri Olsa55b95772016-05-29 10:21:45 +02002409 "Shared Data Cache Line Table "
2410 "(%lu entries, sorted on %s HITMs)",
2411 browser->nr_non_filtered_entries,
Jiri Olsad940bac2016-11-21 22:33:30 +01002412 display_str[c2c.display]);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002413 return 0;
2414}
2415
2416static struct hist_browser*
2417perf_c2c_browser__new(struct hists *hists)
2418{
2419 struct hist_browser *browser = hist_browser__new(hists);
2420
2421 if (browser) {
2422 browser->title = perf_c2c_browser__title;
2423 browser->c2c_filter = true;
2424 }
2425
2426 return browser;
2427}
2428
2429static int perf_c2c__hists_browse(struct hists *hists)
2430{
2431 struct hist_browser *browser;
2432 int key = -1;
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +01002433 static const char help[] =
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002434 " d Display cacheline details \n"
Kim Phillips239fb4f2017-11-14 15:04:47 -06002435 " ENTER Toggle callchains (if present) \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002436 " q Quit \n";
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002437
2438 browser = perf_c2c_browser__new(hists);
2439 if (browser == NULL)
2440 return -1;
2441
2442 /* reset abort key so that it can get Ctrl-C as a key */
2443 SLang_reset_tty();
2444 SLang_init_tty(0, 0, 0);
2445
2446 c2c_browser__update_nr_entries(browser);
2447
2448 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002449 key = hist_browser__run(browser, "? - help", true);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002450
2451 switch (key) {
2452 case 'q':
2453 goto out;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002454 case 'd':
2455 perf_c2c__browse_cacheline(browser->he_selection);
2456 break;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002457 case '?':
2458 ui_browser__help_window(&browser->b, help);
2459 break;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002460 default:
2461 break;
2462 }
2463 }
2464
2465out:
2466 hist_browser__delete(browser);
2467 return 0;
2468}
2469
Jiri Olsa2709b972016-08-27 11:40:23 +02002470static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002471{
Namhyung Kim1936fea2017-03-08 00:08:33 +09002472 if (use_browser == 0)
Jiri Olsa2709b972016-08-27 11:40:23 +02002473 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002474 else
2475 perf_c2c__hists_browse(&c2c.hists.hists);
2476}
2477#else
Jiri Olsa2709b972016-08-27 11:40:23 +02002478static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002479{
2480 use_browser = 0;
Jiri Olsa2709b972016-08-27 11:40:23 +02002481 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002482}
2483#endif /* HAVE_SLANG_SUPPORT */
2484
Jiri Olsad0802b12018-03-09 11:14:41 +01002485static char *fill_line(const char *orig, int len)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002486{
Jiri Olsad0802b12018-03-09 11:14:41 +01002487 int i, j, olen = strlen(orig);
2488 char *buf;
2489
2490 buf = zalloc(len + 1);
2491 if (!buf)
2492 return NULL;
2493
2494 j = len / 2 - olen / 2;
2495
2496 for (i = 0; i < j - 1; i++)
2497 buf[i] = '-';
2498
2499 buf[i++] = ' ';
2500
2501 strcpy(buf + i, orig);
2502
2503 i += olen;
2504
2505 buf[i++] = ' ';
2506
2507 for (; i < len; i++)
2508 buf[i] = '-';
2509
2510 return buf;
2511}
2512
2513static int ui_quirks(void)
2514{
2515 const char *nodestr = "Data address";
2516 char *buf;
2517
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002518 if (!c2c.use_stdio) {
2519 dim_offset.width = 5;
2520 dim_offset.header = header_offset_tui;
Jiri Olsad0802b12018-03-09 11:14:41 +01002521 nodestr = "CL";
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002522 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002523
2524 dim_percent_hitm.header = percent_hitm_header[c2c.display];
Jiri Olsad0802b12018-03-09 11:14:41 +01002525
2526 /* Fix the zero line for dcacheline column. */
2527 buf = fill_line("Cacheline", dim_dcacheline.width +
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002528 dim_dcacheline_node.width +
2529 dim_dcacheline_count.width + 4);
Jiri Olsad0802b12018-03-09 11:14:41 +01002530 if (!buf)
2531 return -ENOMEM;
2532
2533 dim_dcacheline.header.line[0].text = buf;
2534
2535 /* Fix the zero line for offset column. */
2536 buf = fill_line(nodestr, dim_offset.width +
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002537 dim_offset_node.width +
2538 dim_dcacheline_count.width + 4);
Jiri Olsad0802b12018-03-09 11:14:41 +01002539 if (!buf)
2540 return -ENOMEM;
2541
2542 dim_offset.header.line[0].text = buf;
2543
2544 return 0;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002545}
2546
Jiri Olsadd805762016-05-11 18:23:48 +02002547#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2548
2549const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2550 CALLCHAIN_REPORT_HELP
2551 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2552
2553static int
2554parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2555{
2556 struct callchain_param *callchain = opt->value;
2557
2558 callchain->enabled = !unset;
2559 /*
2560 * --no-call-graph
2561 */
2562 if (unset) {
2563 symbol_conf.use_callchain = false;
2564 callchain->mode = CHAIN_NONE;
2565 return 0;
2566 }
2567
2568 return parse_callchain_report_opt(arg);
2569}
2570
Jiri Olsa63503db2019-07-21 13:23:52 +02002571static int setup_callchain(struct evlist *evlist)
Jiri Olsadd805762016-05-11 18:23:48 +02002572{
2573 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2574 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2575
2576 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002577 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsadd805762016-05-11 18:23:48 +02002578 mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002579 dwarf_callchain_users = true;
2580 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsadd805762016-05-11 18:23:48 +02002581 mode = CALLCHAIN_LBR;
2582 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2583 mode = CALLCHAIN_FP;
2584
2585 if (!callchain_param.enabled &&
2586 callchain_param.mode != CHAIN_NONE &&
2587 mode != CALLCHAIN_NONE) {
2588 symbol_conf.use_callchain = true;
2589 if (callchain_register_param(&callchain_param) < 0) {
2590 ui__error("Can't register callchain params.\n");
2591 return -EINVAL;
2592 }
2593 }
2594
2595 callchain_param.record_mode = mode;
2596 callchain_param.min_percent = 0;
2597 return 0;
2598}
2599
Jiri Olsa55b95772016-05-29 10:21:45 +02002600static int setup_display(const char *str)
2601{
Jiri Olsad940bac2016-11-21 22:33:30 +01002602 const char *display = str ?: "tot";
Jiri Olsa55b95772016-05-29 10:21:45 +02002603
Jiri Olsad940bac2016-11-21 22:33:30 +01002604 if (!strcmp(display, "tot"))
2605 c2c.display = DISPLAY_TOT;
2606 else if (!strcmp(display, "rmt"))
Jiri Olsa55b95772016-05-29 10:21:45 +02002607 c2c.display = DISPLAY_RMT;
2608 else if (!strcmp(display, "lcl"))
2609 c2c.display = DISPLAY_LCL;
2610 else {
2611 pr_err("failed: unknown display type: %s\n", str);
2612 return -1;
2613 }
2614
2615 return 0;
2616}
2617
Jiri Olsafc9c6302016-05-24 14:14:38 +02002618#define for_each_token(__tok, __buf, __sep, __tmp) \
2619 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2620 __tok = strtok_r(NULL, __sep, &__tmp))
2621
Jiri Olsa18f278d2016-10-11 13:39:47 +02002622static int build_cl_output(char *cl_sort, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002623{
2624 char *tok, *tmp, *buf = strdup(cl_sort);
2625 bool add_pid = false;
2626 bool add_tid = false;
2627 bool add_iaddr = false;
2628 bool add_sym = false;
2629 bool add_dso = false;
2630 bool add_src = false;
2631
2632 if (!buf)
2633 return -ENOMEM;
2634
2635 for_each_token(tok, buf, ",", tmp) {
2636 if (!strcmp(tok, "tid")) {
2637 add_tid = true;
2638 } else if (!strcmp(tok, "pid")) {
2639 add_pid = true;
2640 } else if (!strcmp(tok, "iaddr")) {
2641 add_iaddr = true;
2642 add_sym = true;
2643 add_dso = true;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002644 add_src = no_source ? false : true;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002645 } else if (!strcmp(tok, "dso")) {
2646 add_dso = true;
2647 } else if (strcmp(tok, "offset")) {
2648 pr_err("unrecognized sort token: %s\n", tok);
2649 return -EINVAL;
2650 }
2651 }
2652
2653 if (asprintf(&c2c.cl_output,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002654 "%s%s%s%s%s%s%s%s%s%s",
2655 c2c.use_stdio ? "cl_num_empty," : "",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002656 "percent_rmt_hitm,"
2657 "percent_lcl_hitm,"
2658 "percent_stores_l1hit,"
2659 "percent_stores_l1miss,"
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002660 "offset,offset_node,dcacheline_count,",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002661 add_pid ? "pid," : "",
2662 add_tid ? "tid," : "",
2663 add_iaddr ? "iaddr," : "",
2664 "mean_rmt,"
2665 "mean_lcl,"
2666 "mean_load,"
Jiri Olsa8763e6a2017-01-20 10:20:31 +01002667 "tot_recs,"
Jiri Olsafc9c6302016-05-24 14:14:38 +02002668 "cpucnt,",
2669 add_sym ? "symbol," : "",
2670 add_dso ? "dso," : "",
2671 add_src ? "cl_srcline," : "",
2672 "node") < 0)
2673 return -ENOMEM;
2674
2675 c2c.show_src = add_src;
2676
2677 free(buf);
2678 return 0;
2679}
2680
Jiri Olsa18f278d2016-10-11 13:39:47 +02002681static int setup_coalesce(const char *coalesce, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002682{
2683 const char *c = coalesce ?: coalesce_default;
2684
2685 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2686 return -ENOMEM;
2687
Jiri Olsa18f278d2016-10-11 13:39:47 +02002688 if (build_cl_output(c2c.cl_sort, no_source))
Jiri Olsafc9c6302016-05-24 14:14:38 +02002689 return -1;
2690
2691 if (asprintf(&c2c.cl_resort, "offset,%s",
Jiri Olsad940bac2016-11-21 22:33:30 +01002692 c2c.display == DISPLAY_TOT ?
2693 "tot_hitm" :
Jiri Olsafc9c6302016-05-24 14:14:38 +02002694 c2c.display == DISPLAY_RMT ?
2695 "rmt_hitm,lcl_hitm" :
2696 "lcl_hitm,rmt_hitm") < 0)
2697 return -ENOMEM;
2698
2699 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2700 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2701 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2702 return 0;
2703}
2704
Jiri Olsa903a6f12016-09-22 17:36:40 +02002705static int perf_c2c__report(int argc, const char **argv)
2706{
2707 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +02002708 struct ui_progress prog;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002709 struct perf_data data = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002710 .mode = PERF_DATA_MODE_READ,
2711 };
Jiri Olsadd805762016-05-11 18:23:48 +02002712 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
Jiri Olsa55b95772016-05-29 10:21:45 +02002713 const char *display = NULL;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002714 const char *coalesce = NULL;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002715 bool no_source = false;
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002716 const struct option options[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002717 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2718 "file", "vmlinux pathname"),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002719 OPT_STRING('i', "input", &input_name, "file",
2720 "the input file to process"),
Jiri Olsa1e181b92016-06-03 15:40:28 +02002721 OPT_INCR('N', "node-info", &c2c.node_info,
2722 "show extra node info in report (repeat for more info)"),
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002723#ifdef HAVE_SLANG_SUPPORT
2724 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2725#endif
Jiri Olsa74c63a22016-05-02 20:01:59 +02002726 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
Namhyung Kimf75d2892017-03-08 00:08:32 +09002727 "Display only statistic tables (implies --stdio)"),
Jiri Olsa590b6a32016-07-10 16:25:15 +02002728 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2729 "Display full length of symbols"),
Jiri Olsa18f278d2016-10-11 13:39:47 +02002730 OPT_BOOLEAN(0, "no-source", &no_source,
2731 "Do not display Source Line column"),
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02002732 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2733 "Show all captured HITM lines."),
Jiri Olsadd805762016-05-11 18:23:48 +02002734 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2735 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2736 callchain_help, &parse_callchain_opt,
2737 callchain_default_opt),
Jiri Olsad940bac2016-11-21 22:33:30 +01002738 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
Jiri Olsafc9c6302016-05-24 14:14:38 +02002739 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2740 "coalesce fields: pid,tid,iaddr,dso"),
Jiri Olsab7ac4f92016-11-21 22:33:28 +01002741 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002742 OPT_PARENT(c2c_options),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002743 OPT_END()
2744 };
2745 int err = 0;
2746
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002747 argc = parse_options(argc, argv, options, report_c2c_usage,
Jiri Olsa903a6f12016-09-22 17:36:40 +02002748 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +02002749 if (argc)
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002750 usage_with_options(report_c2c_usage, options);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002751
Jiri Olsa74c63a22016-05-02 20:01:59 +02002752 if (c2c.stats_only)
2753 c2c.use_stdio = true;
2754
Jiri Olsa78b27542016-09-22 17:36:44 +02002755 if (!input_name || !strlen(input_name))
2756 input_name = "perf.data";
2757
Jiri Olsa2d4f2792019-02-21 10:41:30 +01002758 data.path = input_name;
2759 data.force = symbol_conf.force;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002760
Jiri Olsa55b95772016-05-29 10:21:45 +02002761 err = setup_display(display);
2762 if (err)
2763 goto out;
2764
Jiri Olsa18f278d2016-10-11 13:39:47 +02002765 err = setup_coalesce(coalesce, no_source);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002766 if (err) {
2767 pr_debug("Failed to initialize hists\n");
2768 goto out;
2769 }
2770
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02002771 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
Jiri Olsac75540e2016-09-22 17:36:41 +02002772 if (err) {
2773 pr_debug("Failed to initialize hists\n");
2774 goto out;
2775 }
2776
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002777 session = perf_session__new(&data, 0, &c2c.tool);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002778 if (session == NULL) {
2779 pr_debug("No memory for session\n");
2780 goto out;
2781 }
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002782
Jiri Olsa1e181b92016-06-03 15:40:28 +02002783 err = setup_nodes(session);
2784 if (err) {
2785 pr_err("Failed setup nodes\n");
2786 goto out;
2787 }
Jiri Olsa903a6f12016-09-22 17:36:40 +02002788
Jiri Olsa7f834c22018-03-09 11:14:40 +01002789 err = mem2node__init(&c2c.mem2node, &session->header.env);
Jiri Olsadd805762016-05-11 18:23:48 +02002790 if (err)
2791 goto out_session;
2792
Jiri Olsa7f834c22018-03-09 11:14:40 +01002793 err = setup_callchain(session->evlist);
2794 if (err)
2795 goto out_mem2node;
2796
Jiri Olsa903a6f12016-09-22 17:36:40 +02002797 if (symbol__init(&session->header.env) < 0)
Jiri Olsa7f834c22018-03-09 11:14:40 +01002798 goto out_mem2node;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002799
2800 /* No pipe support at the moment. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002801 if (perf_data__is_pipe(session->data)) {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002802 pr_debug("No pipe support at the moment.\n");
Jiri Olsa7f834c22018-03-09 11:14:40 +01002803 goto out_mem2node;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002804 }
2805
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002806 if (c2c.use_stdio)
2807 use_browser = 0;
2808 else
2809 use_browser = 1;
2810
2811 setup_browser(false);
2812
Jiri Olsa78b27542016-09-22 17:36:44 +02002813 err = perf_session__process_events(session);
2814 if (err) {
2815 pr_err("failed to process sample\n");
Jiri Olsa7f834c22018-03-09 11:14:40 +01002816 goto out_mem2node;
Jiri Olsa78b27542016-09-22 17:36:44 +02002817 }
2818
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002819 c2c_hists__reinit(&c2c.hists,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002820 "cl_idx,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002821 "dcacheline,"
Jiri Olsa7f834c22018-03-09 11:14:40 +01002822 "dcacheline_node,"
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002823 "dcacheline_count,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002824 "tot_recs,"
2825 "percent_hitm,"
2826 "tot_hitm,lcl_hitm,rmt_hitm,"
2827 "stores,stores_l1hit,stores_l1miss,"
2828 "dram_lcl,dram_rmt,"
2829 "ld_llcmiss,"
2830 "tot_loads,"
2831 "ld_fbhit,ld_l1hit,ld_l2hit,"
2832 "ld_lclhit,ld_rmthit",
Jiri Olsad940bac2016-11-21 22:33:30 +01002833 c2c.display == DISPLAY_TOT ? "tot_hitm" :
Jiri Olsa55b95772016-05-29 10:21:45 +02002834 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002835 );
2836
Jiri Olsa78b27542016-09-22 17:36:44 +02002837 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2838
2839 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002840 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2841 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +02002842
2843 ui_progress__finish();
2844
Jiri Olsad0802b12018-03-09 11:14:41 +01002845 if (ui_quirks()) {
2846 pr_err("failed to setup UI\n");
2847 goto out_mem2node;
2848 }
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002849
Jiri Olsa2709b972016-08-27 11:40:23 +02002850 perf_c2c_display(session);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002851
Jiri Olsa7f834c22018-03-09 11:14:40 +01002852out_mem2node:
2853 mem2node__exit(&c2c.mem2node);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002854out_session:
2855 perf_session__delete(session);
2856out:
2857 return err;
2858}
2859
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002860static int parse_record_events(const struct option *opt,
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002861 const char *str, int unset __maybe_unused)
2862{
2863 bool *event_set = (bool *) opt->value;
2864
2865 *event_set = true;
2866 return perf_mem_events__parse(str);
2867}
2868
2869
2870static const char * const __usage_record[] = {
2871 "perf c2c record [<options>] [<command>]",
2872 "perf c2c record [<options>] -- <command> [<options>]",
2873 NULL
2874};
2875
2876static const char * const *record_mem_usage = __usage_record;
2877
2878static int perf_c2c__record(int argc, const char **argv)
2879{
2880 int rec_argc, i = 0, j;
2881 const char **rec_argv;
2882 int ret;
2883 bool all_user = false, all_kernel = false;
2884 bool event_set = false;
2885 struct option options[] = {
2886 OPT_CALLBACK('e', "event", &event_set, "event",
2887 "event selector. Use 'perf mem record -e list' to list available events",
2888 parse_record_events),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002889 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2890 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2891 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002892 OPT_PARENT(c2c_options),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002893 OPT_END()
2894 };
2895
2896 if (perf_mem_events__init()) {
2897 pr_err("failed: memory events not supported\n");
2898 return -1;
2899 }
2900
2901 argc = parse_options(argc, argv, options, record_mem_usage,
2902 PARSE_OPT_KEEP_UNKNOWN);
2903
Jiri Olsa8fab7842018-03-09 11:14:37 +01002904 rec_argc = argc + 11; /* max number of arguments */
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002905 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2906 if (!rec_argv)
2907 return -1;
2908
2909 rec_argv[i++] = "record";
2910
2911 if (!event_set) {
2912 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2913 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2914 }
2915
2916 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2917 rec_argv[i++] = "-W";
2918
2919 rec_argv[i++] = "-d";
Jiri Olsa8fab7842018-03-09 11:14:37 +01002920 rec_argv[i++] = "--phys-data";
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002921 rec_argv[i++] = "--sample-cpu";
2922
2923 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2924 if (!perf_mem_events[j].record)
2925 continue;
2926
2927 if (!perf_mem_events[j].supported) {
2928 pr_err("failed: event '%s' not supported\n",
2929 perf_mem_events[j].name);
Martin Kepplingerc896f852017-09-13 21:14:19 +02002930 free(rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002931 return -1;
2932 }
2933
2934 rec_argv[i++] = "-e";
2935 rec_argv[i++] = perf_mem_events__name(j);
2936 };
2937
2938 if (all_user)
2939 rec_argv[i++] = "--all-user";
2940
2941 if (all_kernel)
2942 rec_argv[i++] = "--all-kernel";
2943
2944 for (j = 0; j < argc; j++, i++)
2945 rec_argv[i] = argv[j];
2946
2947 if (verbose > 0) {
2948 pr_debug("calling: ");
2949
2950 j = 0;
2951
2952 while (rec_argv[j]) {
2953 pr_debug("%s ", rec_argv[j]);
2954 j++;
2955 }
2956 pr_debug("\n");
2957 }
2958
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002959 ret = cmd_record(i, rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002960 free(rec_argv);
2961 return ret;
2962}
2963
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002964int cmd_c2c(int argc, const char **argv)
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002965{
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002966 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2967 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002968
2969 if (!argc)
2970 usage_with_options(c2c_usage, c2c_options);
2971
2972 if (!strncmp(argv[0], "rec", 3)) {
2973 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002974 } else if (!strncmp(argv[0], "rep", 3)) {
2975 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002976 } else {
2977 usage_with_options(c2c_usage, c2c_options);
2978 }
2979
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002980 return 0;
2981}