blob: 9e6cc868bdb4743418bff6eb91364986aceb8f3e [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>
Jiri Olsa1e181b92016-06-03 15:40:28 +020018#include <asm/bug.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030019#include <sys/param.h>
Jiri Olsa7aef3bf2016-09-22 17:36:38 +020020#include "util.h"
21#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"
29#include "data.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030030#include "event.h"
Jiri Olsa2709b972016-08-27 11:40:23 +020031#include "evlist.h"
32#include "evsel.h"
Jiri Olsa5a1a99c2016-01-06 16:59:02 +010033#include "ui/browsers/hists.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030034#include "thread.h"
Jiri Olsa7f834c22018-03-09 11:14:40 +010035#include "mem2node.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010036#include "symbol.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020037
Jiri Olsac75540e2016-09-22 17:36:41 +020038struct c2c_hists {
39 struct hists hists;
40 struct perf_hpp_list list;
Jiri Olsab2252ae2016-09-22 17:36:46 +020041 struct c2c_stats stats;
Jiri Olsac75540e2016-09-22 17:36:41 +020042};
43
Jiri Olsa92062d52016-06-05 13:40:53 +020044struct compute_stats {
45 struct stats lcl_hitm;
46 struct stats rmt_hitm;
47 struct stats load;
48};
49
Jiri Olsa78b27542016-09-22 17:36:44 +020050struct c2c_hist_entry {
51 struct c2c_hists *hists;
Jiri Olsab2252ae2016-09-22 17:36:46 +020052 struct c2c_stats stats;
Jiri Olsa1e181b92016-06-03 15:40:28 +020053 unsigned long *cpuset;
Jiri Olsa7f834c22018-03-09 11:14:40 +010054 unsigned long *nodeset;
Jiri Olsa1e181b92016-06-03 15:40:28 +020055 struct c2c_stats *node_stats;
Jiri Olsabb342dae2016-07-06 15:40:09 +020056 unsigned int cacheline_idx;
Jiri Olsa92062d52016-06-05 13:40:53 +020057
58 struct compute_stats cstats;
59
Jiri Olsa4c820522018-06-08 02:22:11 +020060 unsigned long paddr;
61 unsigned long paddr_cnt;
62 bool paddr_zero;
63 char *nodestr;
64
Jiri Olsa78b27542016-09-22 17:36:44 +020065 /*
66 * must be at the end,
67 * because of its callchain dynamic entry
68 */
69 struct hist_entry he;
70};
71
Jiri Olsa423701a2018-12-28 11:18:19 +010072static char const *coalesce_default = "iaddr";
Jiri Olsafc9c6302016-05-24 14:14:38 +020073
Jiri Olsa903a6f12016-09-22 17:36:40 +020074struct perf_c2c {
Jiri Olsac75540e2016-09-22 17:36:41 +020075 struct perf_tool tool;
76 struct c2c_hists hists;
Jiri Olsa7f834c22018-03-09 11:14:40 +010077 struct mem2node mem2node;
Jiri Olsa1e181b92016-06-03 15:40:28 +020078
79 unsigned long **nodes;
80 int nodes_cnt;
81 int cpus_cnt;
82 int *cpu2node;
83 int node_info;
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +020084
85 bool show_src;
Jiri Olsaaf09b2d2016-10-11 13:52:05 +020086 bool show_all;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +010087 bool use_stdio;
Jiri Olsa74c63a22016-05-02 20:01:59 +020088 bool stats_only;
Jiri Olsa590b6a32016-07-10 16:25:15 +020089 bool symbol_full;
Jiri Olsa7ef2efa2016-07-01 11:12:11 +020090
91 /* HITM shared clines stats */
92 struct c2c_stats hitm_stats;
93 int shared_clines;
Jiri Olsa55b95772016-05-29 10:21:45 +020094
95 int display;
Jiri Olsafc9c6302016-05-24 14:14:38 +020096
97 const char *coalesce;
98 char *cl_sort;
99 char *cl_resort;
100 char *cl_output;
Jiri Olsa55b95772016-05-29 10:21:45 +0200101};
102
103enum {
104 DISPLAY_LCL,
105 DISPLAY_RMT,
Jiri Olsad940bac2016-11-21 22:33:30 +0100106 DISPLAY_TOT,
107 DISPLAY_MAX,
108};
109
110static const char *display_str[DISPLAY_MAX] = {
111 [DISPLAY_LCL] = "Local",
112 [DISPLAY_RMT] = "Remote",
113 [DISPLAY_TOT] = "Total",
Jiri Olsa903a6f12016-09-22 17:36:40 +0200114};
115
Jiri Olsa3a5bfab2016-11-21 22:33:31 +0100116static const struct option c2c_options[] = {
117 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
118 OPT_END()
119};
120
Jiri Olsa903a6f12016-09-22 17:36:40 +0200121static struct perf_c2c c2c;
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200122
Jiri Olsa78b27542016-09-22 17:36:44 +0200123static void *c2c_he_zalloc(size_t size)
124{
125 struct c2c_hist_entry *c2c_he;
126
127 c2c_he = zalloc(size + sizeof(*c2c_he));
128 if (!c2c_he)
129 return NULL;
130
Jiri Olsa1e181b92016-06-03 15:40:28 +0200131 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
132 if (!c2c_he->cpuset)
133 return NULL;
134
Jiri Olsa7f834c22018-03-09 11:14:40 +0100135 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
136 if (!c2c_he->nodeset)
137 return NULL;
138
Jiri Olsa1e181b92016-06-03 15:40:28 +0200139 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
140 if (!c2c_he->node_stats)
141 return NULL;
142
Jiri Olsa92062d52016-06-05 13:40:53 +0200143 init_stats(&c2c_he->cstats.lcl_hitm);
144 init_stats(&c2c_he->cstats.rmt_hitm);
145 init_stats(&c2c_he->cstats.load);
146
Jiri Olsa78b27542016-09-22 17:36:44 +0200147 return &c2c_he->he;
148}
149
150static void c2c_he_free(void *he)
151{
152 struct c2c_hist_entry *c2c_he;
153
154 c2c_he = container_of(he, struct c2c_hist_entry, he);
155 if (c2c_he->hists) {
156 hists__delete_entries(&c2c_he->hists->hists);
157 free(c2c_he->hists);
158 }
159
Jiri Olsa1e181b92016-06-03 15:40:28 +0200160 free(c2c_he->cpuset);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100161 free(c2c_he->nodeset);
162 free(c2c_he->nodestr);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200163 free(c2c_he->node_stats);
Jiri Olsa78b27542016-09-22 17:36:44 +0200164 free(c2c_he);
165}
166
167static struct hist_entry_ops c2c_entry_ops = {
168 .new = c2c_he_zalloc,
169 .free = c2c_he_free,
170};
171
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200172static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200173 const char *sort,
174 int nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200175
Jiri Olsab2252ae2016-09-22 17:36:46 +0200176static struct c2c_hists*
177he__get_c2c_hists(struct hist_entry *he,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200178 const char *sort,
179 int nr_header_lines)
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200180{
181 struct c2c_hist_entry *c2c_he;
182 struct c2c_hists *hists;
183 int ret;
184
185 c2c_he = container_of(he, struct c2c_hist_entry, he);
186 if (c2c_he->hists)
Jiri Olsab2252ae2016-09-22 17:36:46 +0200187 return c2c_he->hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200188
189 hists = c2c_he->hists = zalloc(sizeof(*hists));
190 if (!hists)
191 return NULL;
192
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200193 ret = c2c_hists__init(hists, sort, nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200194 if (ret) {
195 free(hists);
196 return NULL;
197 }
198
Jiri Olsab2252ae2016-09-22 17:36:46 +0200199 return hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200200}
201
Jiri Olsa1e181b92016-06-03 15:40:28 +0200202static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
203 struct perf_sample *sample)
204{
205 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
206 "WARNING: no sample cpu value"))
207 return;
208
209 set_bit(sample->cpu, c2c_he->cpuset);
210}
211
Jiri Olsa7f834c22018-03-09 11:14:40 +0100212static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
213 struct perf_sample *sample)
214{
215 int node;
216
217 if (!sample->phys_addr) {
218 c2c_he->paddr_zero = true;
219 return;
220 }
221
222 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
223 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
224 return;
225
226 set_bit(node, c2c_he->nodeset);
227
228 if (c2c_he->paddr != sample->phys_addr) {
229 c2c_he->paddr_cnt++;
230 c2c_he->paddr = sample->phys_addr;
231 }
232}
233
Jiri Olsa92062d52016-06-05 13:40:53 +0200234static void compute_stats(struct c2c_hist_entry *c2c_he,
235 struct c2c_stats *stats,
236 u64 weight)
237{
238 struct compute_stats *cstats = &c2c_he->cstats;
239
240 if (stats->rmt_hitm)
241 update_stats(&cstats->rmt_hitm, weight);
242 else if (stats->lcl_hitm)
243 update_stats(&cstats->lcl_hitm, weight);
244 else if (stats->load)
245 update_stats(&cstats->load, weight);
246}
247
Jiri Olsa78b27542016-09-22 17:36:44 +0200248static int process_sample_event(struct perf_tool *tool __maybe_unused,
249 union perf_event *event,
250 struct perf_sample *sample,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300251 struct perf_evsel *evsel,
Jiri Olsa78b27542016-09-22 17:36:44 +0200252 struct machine *machine)
253{
Jiri Olsab2252ae2016-09-22 17:36:46 +0200254 struct c2c_hists *c2c_hists = &c2c.hists;
255 struct c2c_hist_entry *c2c_he;
256 struct c2c_stats stats = { .nr_entries = 0, };
Jiri Olsa78b27542016-09-22 17:36:44 +0200257 struct hist_entry *he;
258 struct addr_location al;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200259 struct mem_info *mi, *mi_dup;
Jiri Olsa78b27542016-09-22 17:36:44 +0200260 int ret;
261
262 if (machine__resolve(machine, &al, sample) < 0) {
263 pr_debug("problem processing %d event, skipping it.\n",
264 event->header.type);
265 return -1;
266 }
267
Jiri Olsadd805762016-05-11 18:23:48 +0200268 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
269 evsel, &al, sysctl_perf_event_max_stack);
270 if (ret)
271 goto out;
272
Jiri Olsa78b27542016-09-22 17:36:44 +0200273 mi = sample__resolve_mem(sample, &al);
274 if (mi == NULL)
275 return -ENOMEM;
276
Jiri Olsa5cedb412018-03-07 16:50:07 +0100277 /*
278 * The mi object is released in hists__add_entry_ops,
279 * if it gets sorted out into existing data, so we need
280 * to take the copy now.
281 */
282 mi_dup = mem_info__get(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200283
Jiri Olsab2252ae2016-09-22 17:36:46 +0200284 c2c_decode_stats(&stats, mi);
285
286 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsa78b27542016-09-22 17:36:44 +0200287 &al, NULL, NULL, mi,
288 sample, true);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200289 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100290 goto free_mi;
Jiri Olsa78b27542016-09-22 17:36:44 +0200291
Jiri Olsab2252ae2016-09-22 17:36:46 +0200292 c2c_he = container_of(he, struct c2c_hist_entry, he);
293 c2c_add_stats(&c2c_he->stats, &stats);
294 c2c_add_stats(&c2c_hists->stats, &stats);
295
Jiri Olsa1e181b92016-06-03 15:40:28 +0200296 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100297 c2c_he__set_node(c2c_he, sample);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200298
Jiri Olsab2252ae2016-09-22 17:36:46 +0200299 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsa78b27542016-09-22 17:36:44 +0200300 ret = hist_entry__append_callchain(he, sample);
301
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200302 if (!ret) {
Jiri Olsa1e181b92016-06-03 15:40:28 +0200303 /*
304 * There's already been warning about missing
305 * sample's cpu value. Let's account all to
306 * node 0 in this case, without any further
307 * warning.
308 *
309 * Doing node stats only for single callchain data.
310 */
311 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
312 int node = c2c.cpu2node[cpu];
313
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200314 mi = mi_dup;
315
Jiri Olsafc9c6302016-05-24 14:14:38 +0200316 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200317 if (!c2c_hists)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100318 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200319
Jiri Olsab2252ae2016-09-22 17:36:46 +0200320 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200321 &al, NULL, NULL, mi,
322 sample, true);
323 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100324 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200325
Jiri Olsab2252ae2016-09-22 17:36:46 +0200326 c2c_he = container_of(he, struct c2c_hist_entry, he);
327 c2c_add_stats(&c2c_he->stats, &stats);
328 c2c_add_stats(&c2c_hists->stats, &stats);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200329 c2c_add_stats(&c2c_he->node_stats[node], &stats);
330
Jiri Olsa92062d52016-06-05 13:40:53 +0200331 compute_stats(c2c_he, &stats, sample->weight);
332
Jiri Olsa1e181b92016-06-03 15:40:28 +0200333 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100334 c2c_he__set_node(c2c_he, sample);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200335
336 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200337 ret = hist_entry__append_callchain(he, sample);
338 }
339
340out:
Jiri Olsa78b27542016-09-22 17:36:44 +0200341 addr_location__put(&al);
342 return ret;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200343
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200344free_mi:
Jiri Olsa5cedb412018-03-07 16:50:07 +0100345 mem_info__put(mi_dup);
346 mem_info__put(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200347 ret = -ENOMEM;
348 goto out;
Jiri Olsa78b27542016-09-22 17:36:44 +0200349}
350
351static struct perf_c2c c2c = {
352 .tool = {
353 .sample = process_sample_event,
354 .mmap = perf_event__process_mmap,
355 .mmap2 = perf_event__process_mmap2,
356 .comm = perf_event__process_comm,
357 .exit = perf_event__process_exit,
358 .fork = perf_event__process_fork,
359 .lost = perf_event__process_lost,
360 .ordered_events = true,
361 .ordering_requires_timestamps = true,
362 },
363};
364
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200365static const char * const c2c_usage[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +0200366 "perf c2c {record|report}",
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200367 NULL
368};
369
Jiri Olsa903a6f12016-09-22 17:36:40 +0200370static const char * const __usage_report[] = {
371 "perf c2c report",
372 NULL
373};
374
375static const char * const *report_c2c_usage = __usage_report;
376
Jiri Olsac75540e2016-09-22 17:36:41 +0200377#define C2C_HEADER_MAX 2
378
379struct c2c_header {
380 struct {
381 const char *text;
382 int span;
383 } line[C2C_HEADER_MAX];
384};
385
386struct c2c_dimension {
387 struct c2c_header header;
388 const char *name;
389 int width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200390 struct sort_entry *se;
Jiri Olsac75540e2016-09-22 17:36:41 +0200391
392 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
393 struct hist_entry *, struct hist_entry *);
394 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
395 struct hist_entry *he);
396 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
397 struct hist_entry *he);
398};
399
400struct c2c_fmt {
401 struct perf_hpp_fmt fmt;
402 struct c2c_dimension *dim;
403};
404
Jiri Olsa590b6a32016-07-10 16:25:15 +0200405#define SYMBOL_WIDTH 30
406
407static struct c2c_dimension dim_symbol;
408static struct c2c_dimension dim_srcline;
409
410static int symbol_width(struct hists *hists, struct sort_entry *se)
411{
412 int width = hists__col_len(hists, se->se_width_idx);
413
414 if (!c2c.symbol_full)
415 width = MIN(width, SYMBOL_WIDTH);
416
417 return width;
418}
419
Jiri Olsac75540e2016-09-22 17:36:41 +0200420static int c2c_width(struct perf_hpp_fmt *fmt,
421 struct perf_hpp *hpp __maybe_unused,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300422 struct hists *hists)
Jiri Olsac75540e2016-09-22 17:36:41 +0200423{
424 struct c2c_fmt *c2c_fmt;
Jiri Olsac75540e2016-09-22 17:36:41 +0200425 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +0200426
427 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
428 dim = c2c_fmt->dim;
429
Jiri Olsa590b6a32016-07-10 16:25:15 +0200430 if (dim == &dim_symbol || dim == &dim_srcline)
431 return symbol_width(hists, dim->se);
432
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200433 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
434 c2c_fmt->dim->width;
435}
436
437static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
438 struct hists *hists, int line, int *span)
439{
440 struct perf_hpp_list *hpp_list = hists->hpp_list;
441 struct c2c_fmt *c2c_fmt;
442 struct c2c_dimension *dim;
443 const char *text = NULL;
444 int width = c2c_width(fmt, hpp, hists);
445
446 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
447 dim = c2c_fmt->dim;
448
449 if (dim->se) {
450 text = dim->header.line[line].text;
451 /* Use the last line from sort_entry if not defined. */
452 if (!text && (line == hpp_list->nr_header_lines - 1))
453 text = dim->se->se_header;
454 } else {
455 text = dim->header.line[line].text;
456
457 if (*span) {
458 (*span)--;
459 return 0;
460 } else {
461 *span = dim->header.line[line].span;
462 }
463 }
464
Jiri Olsac75540e2016-09-22 17:36:41 +0200465 if (text == NULL)
466 text = "";
467
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200468 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
Jiri Olsac75540e2016-09-22 17:36:41 +0200469}
470
Jiri Olsacbb88502016-09-22 17:36:48 +0200471#define HEX_STR(__s, __v) \
472({ \
473 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
474 __s; \
475})
476
477static int64_t
478dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
479 struct hist_entry *left, struct hist_entry *right)
480{
481 return sort__dcacheline_cmp(left, right);
482}
483
484static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
485 struct hist_entry *he)
486{
487 uint64_t addr = 0;
488 int width = c2c_width(fmt, hpp, he->hists);
489 char buf[20];
490
491 if (he->mem_info)
492 addr = cl_address(he->mem_info->daddr.addr);
493
494 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
495}
496
Jiri Olsa7f834c22018-03-09 11:14:40 +0100497static int
498dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
499 struct hist_entry *he)
500{
501 struct c2c_hist_entry *c2c_he;
502 int width = c2c_width(fmt, hpp, he->hists);
503
504 c2c_he = container_of(he, struct c2c_hist_entry, he);
505 if (WARN_ON_ONCE(!c2c_he->nodestr))
506 return 0;
507
508 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
509}
510
Jiri Olsa03d9fcb2018-03-09 11:14:42 +0100511static int
512dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
513 struct hist_entry *he)
514{
515 struct c2c_hist_entry *c2c_he;
516 int width = c2c_width(fmt, hpp, he->hists);
517
518 c2c_he = container_of(he, struct c2c_hist_entry, he);
519 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
520}
521
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200522static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
523 struct hist_entry *he)
524{
525 uint64_t addr = 0;
526 int width = c2c_width(fmt, hpp, he->hists);
527 char buf[20];
528
529 if (he->mem_info)
530 addr = cl_offset(he->mem_info->daddr.al_addr);
531
532 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
533}
534
535static int64_t
536offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
537 struct hist_entry *left, struct hist_entry *right)
538{
539 uint64_t l = 0, r = 0;
540
541 if (left->mem_info)
542 l = cl_offset(left->mem_info->daddr.addr);
543 if (right->mem_info)
544 r = cl_offset(right->mem_info->daddr.addr);
545
546 return (int64_t)(r - l);
547}
548
Jiri Olsa43575a92016-05-03 21:48:56 +0200549static int
550iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
551 struct hist_entry *he)
552{
553 uint64_t addr = 0;
554 int width = c2c_width(fmt, hpp, he->hists);
555 char buf[20];
556
557 if (he->mem_info)
558 addr = he->mem_info->iaddr.addr;
559
560 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
561}
562
563static int64_t
564iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
565 struct hist_entry *left, struct hist_entry *right)
566{
567 return sort__iaddr_cmp(left, right);
568}
569
Jiri Olsa97cb4862016-05-23 16:20:14 +0200570static int
571tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
572 struct hist_entry *he)
573{
574 struct c2c_hist_entry *c2c_he;
575 int width = c2c_width(fmt, hpp, he->hists);
576 unsigned int tot_hitm;
577
578 c2c_he = container_of(he, struct c2c_hist_entry, he);
579 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
580
581 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
582}
583
584static int64_t
585tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
586 struct hist_entry *left, struct hist_entry *right)
587{
588 struct c2c_hist_entry *c2c_left;
589 struct c2c_hist_entry *c2c_right;
590 unsigned int tot_hitm_left;
591 unsigned int tot_hitm_right;
592
593 c2c_left = container_of(left, struct c2c_hist_entry, he);
594 c2c_right = container_of(right, struct c2c_hist_entry, he);
595
596 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
597 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
598
599 return tot_hitm_left - tot_hitm_right;
600}
601
602#define STAT_FN_ENTRY(__f) \
603static int \
604__f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
605 struct hist_entry *he) \
606{ \
607 struct c2c_hist_entry *c2c_he; \
608 int width = c2c_width(fmt, hpp, he->hists); \
609 \
610 c2c_he = container_of(he, struct c2c_hist_entry, he); \
611 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
612 c2c_he->stats.__f); \
613}
614
615#define STAT_FN_CMP(__f) \
616static int64_t \
617__f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
618 struct hist_entry *left, struct hist_entry *right) \
619{ \
620 struct c2c_hist_entry *c2c_left, *c2c_right; \
621 \
622 c2c_left = container_of(left, struct c2c_hist_entry, he); \
623 c2c_right = container_of(right, struct c2c_hist_entry, he); \
624 return c2c_left->stats.__f - c2c_right->stats.__f; \
625}
626
627#define STAT_FN(__f) \
628 STAT_FN_ENTRY(__f) \
629 STAT_FN_CMP(__f)
630
631STAT_FN(rmt_hitm)
632STAT_FN(lcl_hitm)
Jiri Olsa0f188962016-05-04 10:10:11 +0200633STAT_FN(store)
634STAT_FN(st_l1hit)
635STAT_FN(st_l1miss)
Jiri Olsa1295f682016-05-04 10:18:24 +0200636STAT_FN(ld_fbhit)
637STAT_FN(ld_l1hit)
638STAT_FN(ld_l2hit)
Jiri Olsa4d089102016-05-04 10:27:51 +0200639STAT_FN(ld_llchit)
640STAT_FN(rmt_hit)
Jiri Olsa97cb4862016-05-23 16:20:14 +0200641
Jiri Olsa04402d22016-05-19 10:10:51 +0200642static uint64_t llc_miss(struct c2c_stats *stats)
643{
644 uint64_t llcmiss;
645
646 llcmiss = stats->lcl_dram +
647 stats->rmt_dram +
648 stats->rmt_hitm +
649 stats->rmt_hit;
650
651 return llcmiss;
652}
653
654static int
655ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
656 struct hist_entry *he)
657{
658 struct c2c_hist_entry *c2c_he;
659 int width = c2c_width(fmt, hpp, he->hists);
660
661 c2c_he = container_of(he, struct c2c_hist_entry, he);
662
663 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
664 llc_miss(&c2c_he->stats));
665}
666
667static int64_t
668ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
669 struct hist_entry *left, struct hist_entry *right)
670{
671 struct c2c_hist_entry *c2c_left;
672 struct c2c_hist_entry *c2c_right;
673
674 c2c_left = container_of(left, struct c2c_hist_entry, he);
675 c2c_right = container_of(right, struct c2c_hist_entry, he);
676
677 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
678}
679
Jiri Olsa01b84d72016-05-04 10:35:29 +0200680static uint64_t total_records(struct c2c_stats *stats)
681{
682 uint64_t lclmiss, ldcnt, total;
683
684 lclmiss = stats->lcl_dram +
685 stats->rmt_dram +
686 stats->rmt_hitm +
687 stats->rmt_hit;
688
689 ldcnt = lclmiss +
690 stats->ld_fbhit +
691 stats->ld_l1hit +
692 stats->ld_l2hit +
693 stats->ld_llchit +
694 stats->lcl_hitm;
695
696 total = ldcnt +
697 stats->st_l1hit +
698 stats->st_l1miss;
699
700 return total;
701}
702
703static int
704tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
705 struct hist_entry *he)
706{
707 struct c2c_hist_entry *c2c_he;
708 int width = c2c_width(fmt, hpp, he->hists);
709 uint64_t tot_recs;
710
711 c2c_he = container_of(he, struct c2c_hist_entry, he);
712 tot_recs = total_records(&c2c_he->stats);
713
714 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
715}
716
717static int64_t
718tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
719 struct hist_entry *left, struct hist_entry *right)
720{
721 struct c2c_hist_entry *c2c_left;
722 struct c2c_hist_entry *c2c_right;
723 uint64_t tot_recs_left;
724 uint64_t tot_recs_right;
725
726 c2c_left = container_of(left, struct c2c_hist_entry, he);
727 c2c_right = container_of(right, struct c2c_hist_entry, he);
728
729 tot_recs_left = total_records(&c2c_left->stats);
730 tot_recs_right = total_records(&c2c_right->stats);
731
732 return tot_recs_left - tot_recs_right;
733}
734
Jiri Olsa55177c42016-05-19 09:52:37 +0200735static uint64_t total_loads(struct c2c_stats *stats)
736{
737 uint64_t lclmiss, ldcnt;
738
739 lclmiss = stats->lcl_dram +
740 stats->rmt_dram +
741 stats->rmt_hitm +
742 stats->rmt_hit;
743
744 ldcnt = lclmiss +
745 stats->ld_fbhit +
746 stats->ld_l1hit +
747 stats->ld_l2hit +
748 stats->ld_llchit +
749 stats->lcl_hitm;
750
751 return ldcnt;
752}
753
754static int
755tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
756 struct hist_entry *he)
757{
758 struct c2c_hist_entry *c2c_he;
759 int width = c2c_width(fmt, hpp, he->hists);
760 uint64_t tot_recs;
761
762 c2c_he = container_of(he, struct c2c_hist_entry, he);
763 tot_recs = total_loads(&c2c_he->stats);
764
765 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
766}
767
768static int64_t
769tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
770 struct hist_entry *left, struct hist_entry *right)
771{
772 struct c2c_hist_entry *c2c_left;
773 struct c2c_hist_entry *c2c_right;
774 uint64_t tot_recs_left;
775 uint64_t tot_recs_right;
776
777 c2c_left = container_of(left, struct c2c_hist_entry, he);
778 c2c_right = container_of(right, struct c2c_hist_entry, he);
779
780 tot_recs_left = total_loads(&c2c_left->stats);
781 tot_recs_right = total_loads(&c2c_right->stats);
782
783 return tot_recs_left - tot_recs_right;
784}
785
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200786typedef double (get_percent_cb)(struct c2c_hist_entry *);
787
788static int
789percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
790 struct hist_entry *he, get_percent_cb get_percent)
791{
792 struct c2c_hist_entry *c2c_he;
793 int width = c2c_width(fmt, hpp, he->hists);
794 double per;
795
796 c2c_he = container_of(he, struct c2c_hist_entry, he);
797 per = get_percent(c2c_he);
798
Jiri Olsa5a1a99c2016-01-06 16:59:02 +0100799#ifdef HAVE_SLANG_SUPPORT
800 if (use_browser)
801 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
802#endif
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200803 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
804}
805
806static double percent_hitm(struct c2c_hist_entry *c2c_he)
807{
808 struct c2c_hists *hists;
809 struct c2c_stats *stats;
810 struct c2c_stats *total;
Jiri Olsa55b95772016-05-29 10:21:45 +0200811 int tot = 0, st = 0;
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200812 double p;
813
814 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
815 stats = &c2c_he->stats;
816 total = &hists->stats;
817
Jiri Olsa55b95772016-05-29 10:21:45 +0200818 switch (c2c.display) {
819 case DISPLAY_RMT:
820 st = stats->rmt_hitm;
821 tot = total->rmt_hitm;
822 break;
823 case DISPLAY_LCL:
824 st = stats->lcl_hitm;
825 tot = total->lcl_hitm;
Jiri Olsad940bac2016-11-21 22:33:30 +0100826 break;
827 case DISPLAY_TOT:
828 st = stats->tot_hitm;
829 tot = total->tot_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +0200830 default:
831 break;
832 }
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200833
834 p = tot ? (double) st / tot : 0;
835
836 return 100 * p;
837}
838
839#define PERC_STR(__s, __v) \
840({ \
841 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
842 __s; \
843})
844
845static int
846percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
847 struct hist_entry *he)
848{
849 struct c2c_hist_entry *c2c_he;
850 int width = c2c_width(fmt, hpp, he->hists);
851 char buf[10];
852 double per;
853
854 c2c_he = container_of(he, struct c2c_hist_entry, he);
855 per = percent_hitm(c2c_he);
856 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
857}
858
859static int
860percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
861 struct hist_entry *he)
862{
863 return percent_color(fmt, hpp, he, percent_hitm);
864}
865
866static int64_t
867percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
868 struct hist_entry *left, struct hist_entry *right)
869{
870 struct c2c_hist_entry *c2c_left;
871 struct c2c_hist_entry *c2c_right;
872 double per_left;
873 double per_right;
874
875 c2c_left = container_of(left, struct c2c_hist_entry, he);
876 c2c_right = container_of(right, struct c2c_hist_entry, he);
877
878 per_left = percent_hitm(c2c_left);
879 per_right = percent_hitm(c2c_right);
880
881 return per_left - per_right;
882}
883
Jiri Olsa9cb35002016-05-04 12:16:50 +0200884static struct c2c_stats *he_stats(struct hist_entry *he)
885{
886 struct c2c_hist_entry *c2c_he;
887
888 c2c_he = container_of(he, struct c2c_hist_entry, he);
889 return &c2c_he->stats;
890}
891
892static struct c2c_stats *total_stats(struct hist_entry *he)
893{
894 struct c2c_hists *hists;
895
896 hists = container_of(he->hists, struct c2c_hists, hists);
897 return &hists->stats;
898}
899
900static double percent(int st, int tot)
901{
902 return tot ? 100. * (double) st / (double) tot : 0;
903}
904
905#define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
906
907#define PERCENT_FN(__f) \
908static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
909{ \
910 struct c2c_hists *hists; \
911 \
912 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
913 return percent(c2c_he->stats.__f, hists->stats.__f); \
914}
915
916PERCENT_FN(rmt_hitm)
917PERCENT_FN(lcl_hitm)
918PERCENT_FN(st_l1hit)
919PERCENT_FN(st_l1miss)
920
921static int
922percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
923 struct hist_entry *he)
924{
925 int width = c2c_width(fmt, hpp, he->hists);
926 double per = PERCENT(he, rmt_hitm);
927 char buf[10];
928
929 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
930}
931
932static int
933percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
934 struct hist_entry *he)
935{
936 return percent_color(fmt, hpp, he, percent_rmt_hitm);
937}
938
939static int64_t
940percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
941 struct hist_entry *left, struct hist_entry *right)
942{
943 double per_left;
944 double per_right;
945
946 per_left = PERCENT(left, lcl_hitm);
947 per_right = PERCENT(right, lcl_hitm);
948
949 return per_left - per_right;
950}
951
952static int
953percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
954 struct hist_entry *he)
955{
956 int width = c2c_width(fmt, hpp, he->hists);
957 double per = PERCENT(he, lcl_hitm);
958 char buf[10];
959
960 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
961}
962
963static int
964percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
965 struct hist_entry *he)
966{
967 return percent_color(fmt, hpp, he, percent_lcl_hitm);
968}
969
970static int64_t
971percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
972 struct hist_entry *left, struct hist_entry *right)
973{
974 double per_left;
975 double per_right;
976
977 per_left = PERCENT(left, lcl_hitm);
978 per_right = PERCENT(right, lcl_hitm);
979
980 return per_left - per_right;
981}
982
983static int
984percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
985 struct hist_entry *he)
986{
987 int width = c2c_width(fmt, hpp, he->hists);
988 double per = PERCENT(he, st_l1hit);
989 char buf[10];
990
991 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
992}
993
994static int
995percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
996 struct hist_entry *he)
997{
998 return percent_color(fmt, hpp, he, percent_st_l1hit);
999}
1000
1001static int64_t
1002percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1003 struct hist_entry *left, struct hist_entry *right)
1004{
1005 double per_left;
1006 double per_right;
1007
1008 per_left = PERCENT(left, st_l1hit);
1009 per_right = PERCENT(right, st_l1hit);
1010
1011 return per_left - per_right;
1012}
1013
1014static int
1015percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1016 struct hist_entry *he)
1017{
1018 int width = c2c_width(fmt, hpp, he->hists);
1019 double per = PERCENT(he, st_l1miss);
1020 char buf[10];
1021
1022 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1023}
1024
1025static int
1026percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1027 struct hist_entry *he)
1028{
1029 return percent_color(fmt, hpp, he, percent_st_l1miss);
1030}
1031
1032static int64_t
1033percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1034 struct hist_entry *left, struct hist_entry *right)
1035{
1036 double per_left;
1037 double per_right;
1038
1039 per_left = PERCENT(left, st_l1miss);
1040 per_right = PERCENT(right, st_l1miss);
1041
1042 return per_left - per_right;
1043}
1044
Jiri Olsa6c70f542016-05-28 12:30:13 +02001045STAT_FN(lcl_dram)
1046STAT_FN(rmt_dram)
1047
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001048static int
1049pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1050 struct hist_entry *he)
1051{
1052 int width = c2c_width(fmt, hpp, he->hists);
1053
1054 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1055}
1056
1057static int64_t
1058pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1059 struct hist_entry *left, struct hist_entry *right)
1060{
1061 return left->thread->pid_ - right->thread->pid_;
1062}
1063
Jiri Olsa1e181b92016-06-03 15:40:28 +02001064static int64_t
1065empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1066 struct hist_entry *left __maybe_unused,
1067 struct hist_entry *right __maybe_unused)
1068{
1069 return 0;
1070}
1071
1072static int
1073node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1074 struct hist_entry *he)
1075{
1076 struct c2c_hist_entry *c2c_he;
1077 bool first = true;
1078 int node;
1079 int ret = 0;
1080
1081 c2c_he = container_of(he, struct c2c_hist_entry, he);
1082
1083 for (node = 0; node < c2c.nodes_cnt; node++) {
1084 DECLARE_BITMAP(set, c2c.cpus_cnt);
1085
1086 bitmap_zero(set, c2c.cpus_cnt);
1087 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1088
1089 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1090 if (c2c.node_info == 1) {
1091 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1092 advance_hpp(hpp, ret);
1093 }
1094 continue;
1095 }
1096
1097 if (!first) {
1098 ret = scnprintf(hpp->buf, hpp->size, " ");
1099 advance_hpp(hpp, ret);
1100 }
1101
1102 switch (c2c.node_info) {
1103 case 0:
1104 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1105 advance_hpp(hpp, ret);
1106 break;
1107 case 1:
1108 {
1109 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
1110 struct c2c_stats *stats = &c2c_he->node_stats[node];
1111
1112 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1113 advance_hpp(hpp, ret);
1114
Jiri Olsa55b95772016-05-29 10:21:45 +02001115 #define DISPLAY_HITM(__h) \
1116 if (c2c_he->stats.__h> 0) { \
1117 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1118 percent(stats->__h, c2c_he->stats.__h));\
1119 } else { \
1120 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
Jiri Olsa1e181b92016-06-03 15:40:28 +02001121 }
1122
Jiri Olsa55b95772016-05-29 10:21:45 +02001123 switch (c2c.display) {
1124 case DISPLAY_RMT:
1125 DISPLAY_HITM(rmt_hitm);
1126 break;
1127 case DISPLAY_LCL:
1128 DISPLAY_HITM(lcl_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001129 break;
1130 case DISPLAY_TOT:
1131 DISPLAY_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001132 default:
1133 break;
1134 }
1135
1136 #undef DISPLAY_HITM
1137
Jiri Olsa1e181b92016-06-03 15:40:28 +02001138 advance_hpp(hpp, ret);
1139
1140 if (c2c_he->stats.store > 0) {
1141 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1142 percent(stats->store, c2c_he->stats.store));
1143 } else {
1144 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1145 }
1146
1147 advance_hpp(hpp, ret);
1148 break;
1149 }
1150 case 2:
1151 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1152 advance_hpp(hpp, ret);
1153
1154 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1155 advance_hpp(hpp, ret);
1156
1157 ret = scnprintf(hpp->buf, hpp->size, "}");
1158 advance_hpp(hpp, ret);
1159 break;
1160 default:
1161 break;
1162 }
1163
1164 first = false;
1165 }
1166
1167 return 0;
1168}
1169
Jiri Olsa92062d52016-06-05 13:40:53 +02001170static int
1171mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1172 struct hist_entry *he, double mean)
1173{
1174 int width = c2c_width(fmt, hpp, he->hists);
1175 char buf[10];
1176
1177 scnprintf(buf, 10, "%6.0f", mean);
1178 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1179}
1180
1181#define MEAN_ENTRY(__func, __val) \
1182static int \
1183__func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1184{ \
1185 struct c2c_hist_entry *c2c_he; \
1186 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1187 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1188}
1189
1190MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1191MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1192MEAN_ENTRY(mean_load_entry, load);
1193
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001194static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001195cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001196 struct hist_entry *he)
1197{
1198 struct c2c_hist_entry *c2c_he;
1199 int width = c2c_width(fmt, hpp, he->hists);
1200 char buf[10];
1201
1202 c2c_he = container_of(he, struct c2c_hist_entry, he);
1203
1204 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1205 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1206}
1207
Jiri Olsabb342dae2016-07-06 15:40:09 +02001208static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001209cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001210 struct hist_entry *he)
1211{
1212 struct c2c_hist_entry *c2c_he;
1213 int width = c2c_width(fmt, hpp, he->hists);
1214 char buf[10];
1215
1216 c2c_he = container_of(he, struct c2c_hist_entry, he);
1217
1218 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1219 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1220}
1221
1222static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001223cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001224 struct hist_entry *he)
1225{
1226 int width = c2c_width(fmt, hpp, he->hists);
1227
1228 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1229}
1230
Jiri Olsa600a8cf2016-09-22 17:36:47 +02001231#define HEADER_LOW(__h) \
1232 { \
1233 .line[1] = { \
1234 .text = __h, \
1235 }, \
1236 }
1237
1238#define HEADER_BOTH(__h0, __h1) \
1239 { \
1240 .line[0] = { \
1241 .text = __h0, \
1242 }, \
1243 .line[1] = { \
1244 .text = __h1, \
1245 }, \
1246 }
1247
1248#define HEADER_SPAN(__h0, __h1, __s) \
1249 { \
1250 .line[0] = { \
1251 .text = __h0, \
1252 .span = __s, \
1253 }, \
1254 .line[1] = { \
1255 .text = __h1, \
1256 }, \
1257 }
1258
1259#define HEADER_SPAN_LOW(__h) \
1260 { \
1261 .line[1] = { \
1262 .text = __h, \
1263 }, \
1264 }
1265
Jiri Olsacbb88502016-09-22 17:36:48 +02001266static struct c2c_dimension dim_dcacheline = {
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001267 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
Jiri Olsacbb88502016-09-22 17:36:48 +02001268 .name = "dcacheline",
1269 .cmp = dcacheline_cmp,
1270 .entry = dcacheline_entry,
1271 .width = 18,
1272};
1273
Jiri Olsa7f834c22018-03-09 11:14:40 +01001274static struct c2c_dimension dim_dcacheline_node = {
1275 .header = HEADER_LOW("Node"),
1276 .name = "dcacheline_node",
1277 .cmp = empty_cmp,
1278 .entry = dcacheline_node_entry,
1279 .width = 4,
1280};
1281
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001282static struct c2c_dimension dim_dcacheline_count = {
1283 .header = HEADER_LOW("PA cnt"),
1284 .name = "dcacheline_count",
1285 .cmp = empty_cmp,
1286 .entry = dcacheline_node_count,
1287 .width = 6,
1288};
1289
1290static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01001291
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001292static struct c2c_dimension dim_offset = {
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001293 .header = HEADER_SPAN("--- Data address -", "Offset", 2),
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001294 .name = "offset",
1295 .cmp = offset_cmp,
1296 .entry = offset_entry,
1297 .width = 18,
1298};
1299
Jiri Olsa7f834c22018-03-09 11:14:40 +01001300static struct c2c_dimension dim_offset_node = {
1301 .header = HEADER_LOW("Node"),
1302 .name = "offset_node",
1303 .cmp = empty_cmp,
1304 .entry = dcacheline_node_entry,
1305 .width = 4,
1306};
1307
Jiri Olsa43575a92016-05-03 21:48:56 +02001308static struct c2c_dimension dim_iaddr = {
1309 .header = HEADER_LOW("Code address"),
1310 .name = "iaddr",
1311 .cmp = iaddr_cmp,
1312 .entry = iaddr_entry,
1313 .width = 18,
1314};
1315
Jiri Olsa97cb4862016-05-23 16:20:14 +02001316static struct c2c_dimension dim_tot_hitm = {
1317 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1318 .name = "tot_hitm",
1319 .cmp = tot_hitm_cmp,
1320 .entry = tot_hitm_entry,
1321 .width = 7,
1322};
1323
1324static struct c2c_dimension dim_lcl_hitm = {
1325 .header = HEADER_SPAN_LOW("Lcl"),
1326 .name = "lcl_hitm",
1327 .cmp = lcl_hitm_cmp,
1328 .entry = lcl_hitm_entry,
1329 .width = 7,
1330};
1331
1332static struct c2c_dimension dim_rmt_hitm = {
1333 .header = HEADER_SPAN_LOW("Rmt"),
1334 .name = "rmt_hitm",
1335 .cmp = rmt_hitm_cmp,
1336 .entry = rmt_hitm_entry,
1337 .width = 7,
1338};
1339
1340static struct c2c_dimension dim_cl_rmt_hitm = {
1341 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1342 .name = "cl_rmt_hitm",
1343 .cmp = rmt_hitm_cmp,
1344 .entry = rmt_hitm_entry,
1345 .width = 7,
1346};
1347
1348static struct c2c_dimension dim_cl_lcl_hitm = {
1349 .header = HEADER_SPAN_LOW("Lcl"),
1350 .name = "cl_lcl_hitm",
1351 .cmp = lcl_hitm_cmp,
1352 .entry = lcl_hitm_entry,
1353 .width = 7,
1354};
1355
Jiri Olsa0f188962016-05-04 10:10:11 +02001356static struct c2c_dimension dim_stores = {
1357 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1358 .name = "stores",
1359 .cmp = store_cmp,
1360 .entry = store_entry,
1361 .width = 7,
1362};
1363
1364static struct c2c_dimension dim_stores_l1hit = {
1365 .header = HEADER_SPAN_LOW("L1Hit"),
1366 .name = "stores_l1hit",
1367 .cmp = st_l1hit_cmp,
1368 .entry = st_l1hit_entry,
1369 .width = 7,
1370};
1371
1372static struct c2c_dimension dim_stores_l1miss = {
1373 .header = HEADER_SPAN_LOW("L1Miss"),
1374 .name = "stores_l1miss",
1375 .cmp = st_l1miss_cmp,
1376 .entry = st_l1miss_entry,
1377 .width = 7,
1378};
1379
1380static struct c2c_dimension dim_cl_stores_l1hit = {
1381 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1382 .name = "cl_stores_l1hit",
1383 .cmp = st_l1hit_cmp,
1384 .entry = st_l1hit_entry,
1385 .width = 7,
1386};
1387
1388static struct c2c_dimension dim_cl_stores_l1miss = {
1389 .header = HEADER_SPAN_LOW("L1 Miss"),
1390 .name = "cl_stores_l1miss",
1391 .cmp = st_l1miss_cmp,
1392 .entry = st_l1miss_entry,
1393 .width = 7,
1394};
1395
Jiri Olsa1295f682016-05-04 10:18:24 +02001396static struct c2c_dimension dim_ld_fbhit = {
1397 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1398 .name = "ld_fbhit",
1399 .cmp = ld_fbhit_cmp,
1400 .entry = ld_fbhit_entry,
1401 .width = 7,
1402};
1403
1404static struct c2c_dimension dim_ld_l1hit = {
1405 .header = HEADER_SPAN_LOW("L1"),
1406 .name = "ld_l1hit",
1407 .cmp = ld_l1hit_cmp,
1408 .entry = ld_l1hit_entry,
1409 .width = 7,
1410};
1411
1412static struct c2c_dimension dim_ld_l2hit = {
1413 .header = HEADER_SPAN_LOW("L2"),
1414 .name = "ld_l2hit",
1415 .cmp = ld_l2hit_cmp,
1416 .entry = ld_l2hit_entry,
1417 .width = 7,
1418};
1419
Jiri Olsa4d089102016-05-04 10:27:51 +02001420static struct c2c_dimension dim_ld_llchit = {
1421 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1422 .name = "ld_lclhit",
1423 .cmp = ld_llchit_cmp,
1424 .entry = ld_llchit_entry,
1425 .width = 8,
1426};
1427
1428static struct c2c_dimension dim_ld_rmthit = {
1429 .header = HEADER_SPAN_LOW("Rmt"),
1430 .name = "ld_rmthit",
1431 .cmp = rmt_hit_cmp,
1432 .entry = rmt_hit_entry,
1433 .width = 8,
1434};
1435
Jiri Olsa04402d22016-05-19 10:10:51 +02001436static struct c2c_dimension dim_ld_llcmiss = {
1437 .header = HEADER_BOTH("LLC", "Ld Miss"),
1438 .name = "ld_llcmiss",
1439 .cmp = ld_llcmiss_cmp,
1440 .entry = ld_llcmiss_entry,
1441 .width = 7,
1442};
1443
Jiri Olsa01b84d72016-05-04 10:35:29 +02001444static struct c2c_dimension dim_tot_recs = {
1445 .header = HEADER_BOTH("Total", "records"),
1446 .name = "tot_recs",
1447 .cmp = tot_recs_cmp,
1448 .entry = tot_recs_entry,
1449 .width = 7,
1450};
1451
Jiri Olsa55177c42016-05-19 09:52:37 +02001452static struct c2c_dimension dim_tot_loads = {
1453 .header = HEADER_BOTH("Total", "Loads"),
1454 .name = "tot_loads",
1455 .cmp = tot_loads_cmp,
1456 .entry = tot_loads_entry,
1457 .width = 7,
1458};
1459
Jiri Olsa55b95772016-05-29 10:21:45 +02001460static struct c2c_header percent_hitm_header[] = {
1461 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1462 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
Jiri Olsad940bac2016-11-21 22:33:30 +01001463 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
Jiri Olsa55b95772016-05-29 10:21:45 +02001464};
1465
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001466static struct c2c_dimension dim_percent_hitm = {
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001467 .name = "percent_hitm",
1468 .cmp = percent_hitm_cmp,
1469 .entry = percent_hitm_entry,
1470 .color = percent_hitm_color,
1471 .width = 7,
1472};
1473
Jiri Olsa9cb35002016-05-04 12:16:50 +02001474static struct c2c_dimension dim_percent_rmt_hitm = {
1475 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1476 .name = "percent_rmt_hitm",
1477 .cmp = percent_rmt_hitm_cmp,
1478 .entry = percent_rmt_hitm_entry,
1479 .color = percent_rmt_hitm_color,
1480 .width = 7,
1481};
1482
1483static struct c2c_dimension dim_percent_lcl_hitm = {
1484 .header = HEADER_SPAN_LOW("Lcl"),
1485 .name = "percent_lcl_hitm",
1486 .cmp = percent_lcl_hitm_cmp,
1487 .entry = percent_lcl_hitm_entry,
1488 .color = percent_lcl_hitm_color,
1489 .width = 7,
1490};
1491
1492static struct c2c_dimension dim_percent_stores_l1hit = {
1493 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1494 .name = "percent_stores_l1hit",
1495 .cmp = percent_stores_l1hit_cmp,
1496 .entry = percent_stores_l1hit_entry,
1497 .color = percent_stores_l1hit_color,
1498 .width = 7,
1499};
1500
1501static struct c2c_dimension dim_percent_stores_l1miss = {
1502 .header = HEADER_SPAN_LOW("L1 Miss"),
1503 .name = "percent_stores_l1miss",
1504 .cmp = percent_stores_l1miss_cmp,
1505 .entry = percent_stores_l1miss_entry,
1506 .color = percent_stores_l1miss_color,
1507 .width = 7,
1508};
1509
Jiri Olsa6c70f542016-05-28 12:30:13 +02001510static struct c2c_dimension dim_dram_lcl = {
1511 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1512 .name = "dram_lcl",
1513 .cmp = lcl_dram_cmp,
1514 .entry = lcl_dram_entry,
1515 .width = 8,
1516};
1517
1518static struct c2c_dimension dim_dram_rmt = {
1519 .header = HEADER_SPAN_LOW("Rmt"),
1520 .name = "dram_rmt",
1521 .cmp = rmt_dram_cmp,
1522 .entry = rmt_dram_entry,
1523 .width = 8,
1524};
1525
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001526static struct c2c_dimension dim_pid = {
1527 .header = HEADER_LOW("Pid"),
1528 .name = "pid",
1529 .cmp = pid_cmp,
1530 .entry = pid_entry,
1531 .width = 7,
1532};
1533
Jiri Olsae87019c2016-05-25 08:50:10 +02001534static struct c2c_dimension dim_tid = {
1535 .header = HEADER_LOW("Tid"),
1536 .name = "tid",
1537 .se = &sort_thread,
1538};
1539
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001540static struct c2c_dimension dim_symbol = {
1541 .name = "symbol",
1542 .se = &sort_sym,
1543};
1544
1545static struct c2c_dimension dim_dso = {
1546 .header = HEADER_BOTH("Shared", "Object"),
1547 .name = "dso",
1548 .se = &sort_dso,
1549};
1550
Jiri Olsa1e181b92016-06-03 15:40:28 +02001551static struct c2c_header header_node[3] = {
1552 HEADER_LOW("Node"),
1553 HEADER_LOW("Node{cpus %hitms %stores}"),
1554 HEADER_LOW("Node{cpu list}"),
1555};
1556
1557static struct c2c_dimension dim_node = {
1558 .name = "node",
1559 .cmp = empty_cmp,
1560 .entry = node_entry,
1561 .width = 4,
1562};
1563
Jiri Olsa92062d52016-06-05 13:40:53 +02001564static struct c2c_dimension dim_mean_rmt = {
1565 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1566 .name = "mean_rmt",
1567 .cmp = empty_cmp,
1568 .entry = mean_rmt_entry,
1569 .width = 8,
1570};
1571
1572static struct c2c_dimension dim_mean_lcl = {
1573 .header = HEADER_SPAN_LOW("lcl hitm"),
1574 .name = "mean_lcl",
1575 .cmp = empty_cmp,
1576 .entry = mean_lcl_entry,
1577 .width = 8,
1578};
1579
1580static struct c2c_dimension dim_mean_load = {
1581 .header = HEADER_SPAN_LOW("load"),
1582 .name = "mean_load",
1583 .cmp = empty_cmp,
1584 .entry = mean_load_entry,
1585 .width = 8,
1586};
1587
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001588static struct c2c_dimension dim_cpucnt = {
1589 .header = HEADER_BOTH("cpu", "cnt"),
1590 .name = "cpucnt",
1591 .cmp = empty_cmp,
1592 .entry = cpucnt_entry,
1593 .width = 8,
1594};
1595
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001596static struct c2c_dimension dim_srcline = {
1597 .name = "cl_srcline",
1598 .se = &sort_srcline,
1599};
1600
Jiri Olsabb342dae2016-07-06 15:40:09 +02001601static struct c2c_dimension dim_dcacheline_idx = {
1602 .header = HEADER_LOW("Index"),
1603 .name = "cl_idx",
1604 .cmp = empty_cmp,
1605 .entry = cl_idx_entry,
1606 .width = 5,
1607};
1608
1609static struct c2c_dimension dim_dcacheline_num = {
1610 .header = HEADER_LOW("Num"),
1611 .name = "cl_num",
1612 .cmp = empty_cmp,
1613 .entry = cl_idx_entry,
1614 .width = 5,
1615};
1616
1617static struct c2c_dimension dim_dcacheline_num_empty = {
1618 .header = HEADER_LOW("Num"),
1619 .name = "cl_num_empty",
1620 .cmp = empty_cmp,
1621 .entry = cl_idx_empty_entry,
1622 .width = 5,
1623};
1624
Jiri Olsac75540e2016-09-22 17:36:41 +02001625static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +02001626 &dim_dcacheline,
Jiri Olsa7f834c22018-03-09 11:14:40 +01001627 &dim_dcacheline_node,
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01001628 &dim_dcacheline_count,
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001629 &dim_offset,
Jiri Olsa7f834c22018-03-09 11:14:40 +01001630 &dim_offset_node,
Jiri Olsa43575a92016-05-03 21:48:56 +02001631 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +02001632 &dim_tot_hitm,
1633 &dim_lcl_hitm,
1634 &dim_rmt_hitm,
1635 &dim_cl_lcl_hitm,
1636 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +02001637 &dim_stores,
1638 &dim_stores_l1hit,
1639 &dim_stores_l1miss,
1640 &dim_cl_stores_l1hit,
1641 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +02001642 &dim_ld_fbhit,
1643 &dim_ld_l1hit,
1644 &dim_ld_l2hit,
Jiri Olsa4d089102016-05-04 10:27:51 +02001645 &dim_ld_llchit,
1646 &dim_ld_rmthit,
Jiri Olsa04402d22016-05-19 10:10:51 +02001647 &dim_ld_llcmiss,
Jiri Olsa01b84d72016-05-04 10:35:29 +02001648 &dim_tot_recs,
Jiri Olsa55177c42016-05-19 09:52:37 +02001649 &dim_tot_loads,
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001650 &dim_percent_hitm,
Jiri Olsa9cb35002016-05-04 12:16:50 +02001651 &dim_percent_rmt_hitm,
1652 &dim_percent_lcl_hitm,
1653 &dim_percent_stores_l1hit,
1654 &dim_percent_stores_l1miss,
Jiri Olsa6c70f542016-05-28 12:30:13 +02001655 &dim_dram_lcl,
1656 &dim_dram_rmt,
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001657 &dim_pid,
Jiri Olsae87019c2016-05-25 08:50:10 +02001658 &dim_tid,
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001659 &dim_symbol,
1660 &dim_dso,
Jiri Olsa1e181b92016-06-03 15:40:28 +02001661 &dim_node,
Jiri Olsa92062d52016-06-05 13:40:53 +02001662 &dim_mean_rmt,
1663 &dim_mean_lcl,
1664 &dim_mean_load,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001665 &dim_cpucnt,
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001666 &dim_srcline,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001667 &dim_dcacheline_idx,
1668 &dim_dcacheline_num,
1669 &dim_dcacheline_num_empty,
Jiri Olsac75540e2016-09-22 17:36:41 +02001670 NULL,
1671};
1672
1673static void fmt_free(struct perf_hpp_fmt *fmt)
1674{
1675 struct c2c_fmt *c2c_fmt;
1676
1677 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1678 free(c2c_fmt);
1679}
1680
1681static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1682{
1683 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1684 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1685
1686 return c2c_a->dim == c2c_b->dim;
1687}
1688
1689static struct c2c_dimension *get_dimension(const char *name)
1690{
1691 unsigned int i;
1692
1693 for (i = 0; dimensions[i]; i++) {
1694 struct c2c_dimension *dim = dimensions[i];
1695
1696 if (!strcmp(dim->name, name))
1697 return dim;
1698 };
1699
1700 return NULL;
1701}
1702
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001703static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1704 struct hist_entry *he)
1705{
1706 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1707 struct c2c_dimension *dim = c2c_fmt->dim;
1708 size_t len = fmt->user_len;
1709
Jiri Olsa590b6a32016-07-10 16:25:15 +02001710 if (!len) {
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001711 len = hists__col_len(he->hists, dim->se->se_width_idx);
1712
Jiri Olsa590b6a32016-07-10 16:25:15 +02001713 if (dim == &dim_symbol || dim == &dim_srcline)
1714 len = symbol_width(he->hists, dim->se);
1715 }
1716
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001717 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1718}
1719
1720static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1721 struct hist_entry *a, struct hist_entry *b)
1722{
1723 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1724 struct c2c_dimension *dim = c2c_fmt->dim;
1725
1726 return dim->se->se_cmp(a, b);
1727}
1728
1729static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1730 struct hist_entry *a, struct hist_entry *b)
1731{
1732 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1733 struct c2c_dimension *dim = c2c_fmt->dim;
1734 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1735
1736 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1737 return collapse_fn(a, b);
1738}
1739
Jiri Olsac75540e2016-09-22 17:36:41 +02001740static struct c2c_fmt *get_format(const char *name)
1741{
1742 struct c2c_dimension *dim = get_dimension(name);
1743 struct c2c_fmt *c2c_fmt;
1744 struct perf_hpp_fmt *fmt;
1745
1746 if (!dim)
1747 return NULL;
1748
1749 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1750 if (!c2c_fmt)
1751 return NULL;
1752
1753 c2c_fmt->dim = dim;
1754
1755 fmt = &c2c_fmt->fmt;
1756 INIT_LIST_HEAD(&fmt->list);
1757 INIT_LIST_HEAD(&fmt->sort_list);
1758
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001759 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1760 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
Jiri Olsa9cb35002016-05-04 12:16:50 +02001761 fmt->color = dim->se ? NULL : dim->color;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001762 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +02001763 fmt->header = c2c_header;
1764 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001765 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +02001766 fmt->equal = fmt_equal;
1767 fmt->free = fmt_free;
1768
1769 return c2c_fmt;
1770}
1771
1772static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1773{
1774 struct c2c_fmt *c2c_fmt = get_format(name);
1775
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001776 if (!c2c_fmt) {
1777 reset_dimensions();
1778 return output_field_add(hpp_list, name);
1779 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001780
1781 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1782 return 0;
1783}
1784
1785static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1786{
1787 struct c2c_fmt *c2c_fmt = get_format(name);
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001788 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +02001789
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001790 if (!c2c_fmt) {
1791 reset_dimensions();
1792 return sort_dimension__add(hpp_list, name, NULL, 0);
1793 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001794
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001795 dim = c2c_fmt->dim;
1796 if (dim == &dim_dso)
1797 hpp_list->dso = 1;
1798
Jiri Olsac75540e2016-09-22 17:36:41 +02001799 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1800 return 0;
1801}
1802
1803#define PARSE_LIST(_list, _fn) \
1804 do { \
1805 char *tmp, *tok; \
1806 ret = 0; \
1807 \
1808 if (!_list) \
1809 break; \
1810 \
1811 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1812 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1813 ret = _fn(hpp_list, tok); \
1814 if (ret == -EINVAL) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001815 pr_err("Invalid --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001816 break; \
1817 } else if (ret == -ESRCH) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001818 pr_err("Unknown --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001819 break; \
1820 } \
1821 } \
1822 } while (0)
1823
1824static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1825 const char *output_,
1826 const char *sort_)
1827{
1828 char *output = output_ ? strdup(output_) : NULL;
1829 char *sort = sort_ ? strdup(sort_) : NULL;
1830 int ret;
1831
1832 PARSE_LIST(output, c2c_hists__init_output);
1833 PARSE_LIST(sort, c2c_hists__init_sort);
1834
1835 /* copy sort keys to output fields */
1836 perf_hpp__setup_output_field(hpp_list);
1837
1838 /*
1839 * We dont need other sorting keys other than those
1840 * we already specified. It also really slows down
1841 * the processing a lot with big number of output
1842 * fields, so switching this off for c2c.
1843 */
1844
1845#if 0
1846 /* and then copy output fields to sort keys */
1847 perf_hpp__append_sort_keys(&hists->list);
1848#endif
1849
1850 free(output);
1851 free(sort);
1852 return ret;
1853}
1854
1855static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001856 const char *sort,
1857 int nr_header_lines)
Jiri Olsac75540e2016-09-22 17:36:41 +02001858{
1859 __hists__init(&hists->hists, &hists->list);
1860
1861 /*
1862 * Initialize only with sort fields, we need to resort
1863 * later anyway, and that's where we add output fields
1864 * as well.
1865 */
1866 perf_hpp_list__init(&hists->list);
1867
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001868 /* Overload number of header lines.*/
1869 hists->list.nr_header_lines = nr_header_lines;
1870
Jiri Olsac75540e2016-09-22 17:36:41 +02001871 return hpp_list__parse(&hists->list, NULL, sort);
1872}
1873
Jiri Olsac75540e2016-09-22 17:36:41 +02001874static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1875 const char *output,
1876 const char *sort)
1877{
1878 perf_hpp__reset_output_field(&c2c_hists->list);
1879 return hpp_list__parse(&c2c_hists->list, output, sort);
1880}
1881
Jiri Olsac4a75bb2018-12-28 11:18:20 +01001882#define DISPLAY_LINE_LIMIT 0.001
Jiri Olsa9857b712016-08-17 14:55:23 +02001883
1884static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1885{
1886 struct c2c_hist_entry *c2c_he;
1887 double ld_dist;
1888
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02001889 if (c2c.show_all)
1890 return true;
Jiri Olsa9857b712016-08-17 14:55:23 +02001891
1892 c2c_he = container_of(he, struct c2c_hist_entry, he);
1893
Jiri Olsa55b95772016-05-29 10:21:45 +02001894#define FILTER_HITM(__h) \
1895 if (stats->__h) { \
1896 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1897 if (ld_dist < DISPLAY_LINE_LIMIT) \
1898 he->filtered = HIST_FILTER__C2C; \
1899 } else { \
1900 he->filtered = HIST_FILTER__C2C; \
Jiri Olsa9857b712016-08-17 14:55:23 +02001901 }
1902
Jiri Olsa55b95772016-05-29 10:21:45 +02001903 switch (c2c.display) {
1904 case DISPLAY_LCL:
1905 FILTER_HITM(lcl_hitm);
1906 break;
1907 case DISPLAY_RMT:
1908 FILTER_HITM(rmt_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001909 break;
1910 case DISPLAY_TOT:
1911 FILTER_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001912 default:
1913 break;
1914 };
1915
1916#undef FILTER_HITM
1917
Jiri Olsa9857b712016-08-17 14:55:23 +02001918 return he->filtered == 0;
1919}
1920
1921static inline int valid_hitm_or_store(struct hist_entry *he)
1922{
1923 struct c2c_hist_entry *c2c_he;
Jiri Olsa55b95772016-05-29 10:21:45 +02001924 bool has_hitm;
Jiri Olsa9857b712016-08-17 14:55:23 +02001925
1926 c2c_he = container_of(he, struct c2c_hist_entry, he);
Jiri Olsad940bac2016-11-21 22:33:30 +01001927 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1928 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1929 c2c_he->stats.rmt_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +02001930 return has_hitm || c2c_he->stats.store;
Jiri Olsa9857b712016-08-17 14:55:23 +02001931}
1932
Jiri Olsa7f834c22018-03-09 11:14:40 +01001933static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1934{
1935 struct c2c_dimension *dim;
1936
1937 dim = &c2c.hists == c2c_he->hists ?
1938 &dim_dcacheline_node : &dim_offset_node;
1939
1940 if (len > dim->width)
1941 dim->width = len;
1942}
1943
1944static int set_nodestr(struct c2c_hist_entry *c2c_he)
1945{
1946 char buf[30];
1947 int len;
1948
1949 if (c2c_he->nodestr)
1950 return 0;
1951
1952 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1953 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1954 buf, sizeof(buf));
1955 } else {
1956 len = scnprintf(buf, sizeof(buf), "N/A");
1957 }
1958
1959 set_node_width(c2c_he, len);
1960 c2c_he->nodestr = strdup(buf);
1961 return c2c_he->nodestr ? 0 : -ENOMEM;
1962}
1963
Jiri Olsa37731382018-03-09 11:14:38 +01001964static void calc_width(struct c2c_hist_entry *c2c_he)
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001965{
1966 struct c2c_hists *c2c_hists;
1967
Jiri Olsa37731382018-03-09 11:14:38 +01001968 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1969 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
Jiri Olsa7f834c22018-03-09 11:14:40 +01001970 set_nodestr(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001971}
1972
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001973static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001974{
Jiri Olsa37731382018-03-09 11:14:38 +01001975 struct c2c_hist_entry *c2c_he;
1976
1977 c2c_he = container_of(he, struct c2c_hist_entry, he);
1978
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001979 if (c2c.show_src && !he->srcline)
Arnaldo Carvalho de Melo6a53da02018-05-28 11:06:58 -03001980 he->srcline = hist_entry__srcline(he);
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001981
Jiri Olsa37731382018-03-09 11:14:38 +01001982 calc_width(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001983
Jiri Olsa9857b712016-08-17 14:55:23 +02001984 if (!valid_hitm_or_store(he))
1985 he->filtered = HIST_FILTER__C2C;
1986
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001987 return 0;
1988}
1989
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001990static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001991{
1992 struct c2c_hist_entry *c2c_he;
1993 struct c2c_hists *c2c_hists;
Jiri Olsa9857b712016-08-17 14:55:23 +02001994 bool display = he__display(he, &c2c.hitm_stats);
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001995
1996 c2c_he = container_of(he, struct c2c_hist_entry, he);
1997 c2c_hists = c2c_he->hists;
1998
Jiri Olsa9857b712016-08-17 14:55:23 +02001999 if (display && c2c_hists) {
Jiri Olsabb342dae2016-07-06 15:40:09 +02002000 static unsigned int idx;
2001
2002 c2c_he->cacheline_idx = idx++;
Jiri Olsabc229c22018-03-09 11:14:39 +01002003 calc_width(c2c_he);
Jiri Olsabb342dae2016-07-06 15:40:09 +02002004
Jiri Olsafc9c6302016-05-24 14:14:38 +02002005 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002006
Jiri Olsaec06f9b2016-09-22 17:36:45 +02002007 hists__collapse_resort(&c2c_hists->hists, NULL);
2008 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
2009 }
2010
2011 return 0;
2012}
2013
Jiri Olsa1e181b92016-06-03 15:40:28 +02002014static void setup_nodes_header(void)
2015{
2016 dim_node.header = header_node[c2c.node_info];
2017}
2018
2019static int setup_nodes(struct perf_session *session)
2020{
2021 struct numa_node *n;
2022 unsigned long **nodes;
2023 int node, cpu;
2024 int *cpu2node;
2025
2026 if (c2c.node_info > 2)
2027 c2c.node_info = 2;
2028
2029 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2030 c2c.cpus_cnt = session->header.env.nr_cpus_online;
2031
2032 n = session->header.env.numa_nodes;
2033 if (!n)
2034 return -EINVAL;
2035
2036 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2037 if (!nodes)
2038 return -ENOMEM;
2039
2040 c2c.nodes = nodes;
2041
2042 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2043 if (!cpu2node)
2044 return -ENOMEM;
2045
2046 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2047 cpu2node[cpu] = -1;
2048
2049 c2c.cpu2node = cpu2node;
2050
2051 for (node = 0; node < c2c.nodes_cnt; node++) {
2052 struct cpu_map *map = n[node].map;
2053 unsigned long *set;
2054
2055 set = bitmap_alloc(c2c.cpus_cnt);
2056 if (!set)
2057 return -ENOMEM;
2058
Jiri Olsae34c9402019-03-05 16:25:29 +01002059 nodes[node] = set;
2060
2061 /* empty node, skip */
2062 if (cpu_map__empty(map))
2063 continue;
2064
Jiri Olsa1e181b92016-06-03 15:40:28 +02002065 for (cpu = 0; cpu < map->nr; cpu++) {
2066 set_bit(map->map[cpu], set);
2067
2068 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2069 return -EINVAL;
2070
2071 cpu2node[map->map[cpu]] = node;
2072 }
Jiri Olsa1e181b92016-06-03 15:40:28 +02002073 }
2074
2075 setup_nodes_header();
2076 return 0;
2077}
2078
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002079#define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2080
Jiri Olsae4c38fd2019-02-04 15:18:06 +01002081static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused)
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002082{
2083 struct c2c_hist_entry *c2c_he;
2084 c2c_he = container_of(he, struct c2c_hist_entry, he);
2085
2086 if (HAS_HITMS(c2c_he)) {
2087 c2c.shared_clines++;
2088 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2089 }
2090
2091 return 0;
2092}
2093
2094static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2095{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002096 struct rb_node *next = rb_first_cached(&hists->entries);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002097 int ret = 0;
2098
2099 while (next) {
2100 struct hist_entry *he;
2101
2102 he = rb_entry(next, struct hist_entry, rb_node);
Jiri Olsae4c38fd2019-02-04 15:18:06 +01002103 ret = cb(he, NULL);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002104 if (ret)
2105 break;
2106 next = rb_next(&he->rb_node);
2107 }
2108
2109 return ret;
2110}
2111
Jiri Olsa74c63a22016-05-02 20:01:59 +02002112static void print_c2c__display_stats(FILE *out)
2113{
2114 int llc_misses;
2115 struct c2c_stats *stats = &c2c.hists.stats;
2116
2117 llc_misses = stats->lcl_dram +
2118 stats->rmt_dram +
2119 stats->rmt_hit +
2120 stats->rmt_hitm;
2121
2122 fprintf(out, "=================================================\n");
2123 fprintf(out, " Trace Event Information \n");
2124 fprintf(out, "=================================================\n");
2125 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2126 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2127 fprintf(out, " Load Operations : %10d\n", stats->load);
2128 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2129 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2130 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2131 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2132 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2133 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2134 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2135 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2136 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2137 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2138 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2139 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2140 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2141 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2142 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2143 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2144 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2145 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2146 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2147 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2148 fprintf(out, " Store Operations : %10d\n", stats->store);
2149 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2150 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2151 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2152 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2153 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2154 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2155}
2156
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002157static void print_shared_cacheline_info(FILE *out)
2158{
2159 struct c2c_stats *stats = &c2c.hitm_stats;
2160 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2161
2162 fprintf(out, "=================================================\n");
2163 fprintf(out, " Global Shared Cache Line Event Information \n");
2164 fprintf(out, "=================================================\n");
2165 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2166 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2167 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2168 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2169 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2170 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2171 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2172 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2173 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2174 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2175}
2176
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002177static void print_cacheline(struct c2c_hists *c2c_hists,
2178 struct hist_entry *he_cl,
2179 struct perf_hpp_list *hpp_list,
2180 FILE *out)
2181{
2182 char bf[1000];
2183 struct perf_hpp hpp = {
2184 .buf = bf,
2185 .size = 1000,
2186 };
2187 static bool once;
2188
2189 if (!once) {
2190 hists__fprintf_headers(&c2c_hists->hists, out);
2191 once = true;
2192 } else {
2193 fprintf(out, "\n");
2194 }
2195
Jiri Olsabb342dae2016-07-06 15:40:09 +02002196 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002197 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2198 fprintf(out, "%s\n", bf);
Jiri Olsabb342dae2016-07-06 15:40:09 +02002199 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002200
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -03002201 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002202}
2203
2204static void print_pareto(FILE *out)
2205{
2206 struct perf_hpp_list hpp_list;
2207 struct rb_node *nd;
2208 int ret;
2209
2210 perf_hpp_list__init(&hpp_list);
2211 ret = hpp_list__parse(&hpp_list,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002212 "cl_num,"
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002213 "cl_rmt_hitm,"
2214 "cl_lcl_hitm,"
2215 "cl_stores_l1hit,"
2216 "cl_stores_l1miss,"
2217 "dcacheline",
2218 NULL);
2219
2220 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2221 return;
2222
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002223 nd = rb_first_cached(&c2c.hists.hists.entries);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002224
2225 for (; nd; nd = rb_next(nd)) {
2226 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2227 struct c2c_hist_entry *c2c_he;
2228
2229 if (he->filtered)
2230 continue;
2231
2232 c2c_he = container_of(he, struct c2c_hist_entry, he);
2233 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2234 }
2235}
2236
Jiri Olsa2709b972016-08-27 11:40:23 +02002237static void print_c2c_info(FILE *out, struct perf_session *session)
2238{
2239 struct perf_evlist *evlist = session->evlist;
2240 struct perf_evsel *evsel;
2241 bool first = true;
2242
2243 fprintf(out, "=================================================\n");
2244 fprintf(out, " c2c details \n");
2245 fprintf(out, "=================================================\n");
2246
2247 evlist__for_each_entry(evlist, evsel) {
2248 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2249 perf_evsel__name(evsel));
2250 first = false;
2251 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002252 fprintf(out, " Cachelines sort on : %s HITMs\n",
Jiri Olsad940bac2016-11-21 22:33:30 +01002253 display_str[c2c.display]);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002254 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
Jiri Olsa2709b972016-08-27 11:40:23 +02002255}
2256
2257static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002258{
2259 setup_pager();
2260
Jiri Olsa74c63a22016-05-02 20:01:59 +02002261 print_c2c__display_stats(out);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002262 fprintf(out, "\n");
2263 print_shared_cacheline_info(out);
Jiri Olsa2709b972016-08-27 11:40:23 +02002264 fprintf(out, "\n");
2265 print_c2c_info(out, session);
Jiri Olsa74c63a22016-05-02 20:01:59 +02002266
2267 if (c2c.stats_only)
2268 return;
2269
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002270 fprintf(out, "\n");
2271 fprintf(out, "=================================================\n");
2272 fprintf(out, " Shared Data Cache Line Table \n");
2273 fprintf(out, "=================================================\n");
2274 fprintf(out, "#\n");
2275
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -03002276 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002277
2278 fprintf(out, "\n");
2279 fprintf(out, "=================================================\n");
2280 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2281 fprintf(out, "=================================================\n");
2282 fprintf(out, "#\n");
2283
2284 print_pareto(out);
2285}
Jiri Olsa1e181b92016-06-03 15:40:28 +02002286
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002287#ifdef HAVE_SLANG_SUPPORT
2288static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2289{
2290 u64 nr_entries = 0;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002291 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002292
2293 while (nd) {
2294 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2295
2296 if (!he->filtered)
2297 nr_entries++;
2298
2299 nd = rb_next(nd);
2300 }
2301
2302 hb->nr_non_filtered_entries = nr_entries;
2303}
2304
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002305struct c2c_cacheline_browser {
2306 struct hist_browser hb;
2307 struct hist_entry *he;
2308};
2309
2310static int
2311perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2312 char *bf, size_t size)
2313{
2314 struct c2c_cacheline_browser *cl_browser;
2315 struct hist_entry *he;
2316 uint64_t addr = 0;
2317
2318 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2319 he = cl_browser->he;
2320
2321 if (he->mem_info)
2322 addr = cl_address(he->mem_info->daddr.addr);
2323
2324 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2325 return 0;
2326}
2327
2328static struct c2c_cacheline_browser*
2329c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2330{
2331 struct c2c_cacheline_browser *browser;
2332
2333 browser = zalloc(sizeof(*browser));
2334 if (browser) {
2335 hist_browser__init(&browser->hb, hists);
2336 browser->hb.c2c_filter = true;
2337 browser->hb.title = perf_c2c_cacheline_browser__title;
2338 browser->he = he;
2339 }
2340
2341 return browser;
2342}
2343
2344static int perf_c2c__browse_cacheline(struct hist_entry *he)
2345{
2346 struct c2c_hist_entry *c2c_he;
2347 struct c2c_hists *c2c_hists;
2348 struct c2c_cacheline_browser *cl_browser;
2349 struct hist_browser *browser;
2350 int key = -1;
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +01002351 static const char help[] =
Kim Phillips239fb4f2017-11-14 15:04:47 -06002352 " ENTER Toggle callchains (if present) \n"
2353 " n Toggle Node details info \n"
2354 " s Toggle full length of symbol and source line columns \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002355 " q Return back to cacheline list \n";
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002356
Jiri Olsa73978332018-07-24 08:20:08 +02002357 if (!he)
2358 return 0;
2359
Jiri Olsa590b6a32016-07-10 16:25:15 +02002360 /* Display compact version first. */
2361 c2c.symbol_full = false;
2362
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002363 c2c_he = container_of(he, struct c2c_hist_entry, he);
2364 c2c_hists = c2c_he->hists;
2365
2366 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2367 if (cl_browser == NULL)
2368 return -1;
2369
2370 browser = &cl_browser->hb;
2371
2372 /* reset abort key so that it can get Ctrl-C as a key */
2373 SLang_reset_tty();
2374 SLang_init_tty(0, 0, 0);
2375
2376 c2c_browser__update_nr_entries(browser);
2377
2378 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002379 key = hist_browser__run(browser, "? - help", true);
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002380
2381 switch (key) {
Jiri Olsa590b6a32016-07-10 16:25:15 +02002382 case 's':
2383 c2c.symbol_full = !c2c.symbol_full;
2384 break;
Jiri Olsa1a56a422016-07-10 16:30:27 +02002385 case 'n':
2386 c2c.node_info = (c2c.node_info + 1) % 3;
2387 setup_nodes_header();
2388 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002389 case 'q':
2390 goto out;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002391 case '?':
2392 ui_browser__help_window(&browser->b, help);
2393 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002394 default:
2395 break;
2396 }
2397 }
2398
2399out:
2400 free(cl_browser);
2401 return 0;
2402}
2403
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002404static int perf_c2c_browser__title(struct hist_browser *browser,
2405 char *bf, size_t size)
2406{
2407 scnprintf(bf, size,
Jiri Olsa55b95772016-05-29 10:21:45 +02002408 "Shared Data Cache Line Table "
2409 "(%lu entries, sorted on %s HITMs)",
2410 browser->nr_non_filtered_entries,
Jiri Olsad940bac2016-11-21 22:33:30 +01002411 display_str[c2c.display]);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002412 return 0;
2413}
2414
2415static struct hist_browser*
2416perf_c2c_browser__new(struct hists *hists)
2417{
2418 struct hist_browser *browser = hist_browser__new(hists);
2419
2420 if (browser) {
2421 browser->title = perf_c2c_browser__title;
2422 browser->c2c_filter = true;
2423 }
2424
2425 return browser;
2426}
2427
2428static int perf_c2c__hists_browse(struct hists *hists)
2429{
2430 struct hist_browser *browser;
2431 int key = -1;
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +01002432 static const char help[] =
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002433 " d Display cacheline details \n"
Kim Phillips239fb4f2017-11-14 15:04:47 -06002434 " ENTER Toggle callchains (if present) \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002435 " q Quit \n";
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002436
2437 browser = perf_c2c_browser__new(hists);
2438 if (browser == NULL)
2439 return -1;
2440
2441 /* reset abort key so that it can get Ctrl-C as a key */
2442 SLang_reset_tty();
2443 SLang_init_tty(0, 0, 0);
2444
2445 c2c_browser__update_nr_entries(browser);
2446
2447 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002448 key = hist_browser__run(browser, "? - help", true);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002449
2450 switch (key) {
2451 case 'q':
2452 goto out;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002453 case 'd':
2454 perf_c2c__browse_cacheline(browser->he_selection);
2455 break;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002456 case '?':
2457 ui_browser__help_window(&browser->b, help);
2458 break;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002459 default:
2460 break;
2461 }
2462 }
2463
2464out:
2465 hist_browser__delete(browser);
2466 return 0;
2467}
2468
Jiri Olsa2709b972016-08-27 11:40:23 +02002469static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002470{
Namhyung Kim1936fea2017-03-08 00:08:33 +09002471 if (use_browser == 0)
Jiri Olsa2709b972016-08-27 11:40:23 +02002472 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002473 else
2474 perf_c2c__hists_browse(&c2c.hists.hists);
2475}
2476#else
Jiri Olsa2709b972016-08-27 11:40:23 +02002477static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002478{
2479 use_browser = 0;
Jiri Olsa2709b972016-08-27 11:40:23 +02002480 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002481}
2482#endif /* HAVE_SLANG_SUPPORT */
2483
Jiri Olsad0802b12018-03-09 11:14:41 +01002484static char *fill_line(const char *orig, int len)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002485{
Jiri Olsad0802b12018-03-09 11:14:41 +01002486 int i, j, olen = strlen(orig);
2487 char *buf;
2488
2489 buf = zalloc(len + 1);
2490 if (!buf)
2491 return NULL;
2492
2493 j = len / 2 - olen / 2;
2494
2495 for (i = 0; i < j - 1; i++)
2496 buf[i] = '-';
2497
2498 buf[i++] = ' ';
2499
2500 strcpy(buf + i, orig);
2501
2502 i += olen;
2503
2504 buf[i++] = ' ';
2505
2506 for (; i < len; i++)
2507 buf[i] = '-';
2508
2509 return buf;
2510}
2511
2512static int ui_quirks(void)
2513{
2514 const char *nodestr = "Data address";
2515 char *buf;
2516
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002517 if (!c2c.use_stdio) {
2518 dim_offset.width = 5;
2519 dim_offset.header = header_offset_tui;
Jiri Olsad0802b12018-03-09 11:14:41 +01002520 nodestr = "CL";
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002521 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002522
2523 dim_percent_hitm.header = percent_hitm_header[c2c.display];
Jiri Olsad0802b12018-03-09 11:14:41 +01002524
2525 /* Fix the zero line for dcacheline column. */
2526 buf = fill_line("Cacheline", dim_dcacheline.width +
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002527 dim_dcacheline_node.width +
2528 dim_dcacheline_count.width + 4);
Jiri Olsad0802b12018-03-09 11:14:41 +01002529 if (!buf)
2530 return -ENOMEM;
2531
2532 dim_dcacheline.header.line[0].text = buf;
2533
2534 /* Fix the zero line for offset column. */
2535 buf = fill_line(nodestr, dim_offset.width +
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002536 dim_offset_node.width +
2537 dim_dcacheline_count.width + 4);
Jiri Olsad0802b12018-03-09 11:14:41 +01002538 if (!buf)
2539 return -ENOMEM;
2540
2541 dim_offset.header.line[0].text = buf;
2542
2543 return 0;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002544}
2545
Jiri Olsadd805762016-05-11 18:23:48 +02002546#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2547
2548const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2549 CALLCHAIN_REPORT_HELP
2550 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2551
2552static int
2553parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2554{
2555 struct callchain_param *callchain = opt->value;
2556
2557 callchain->enabled = !unset;
2558 /*
2559 * --no-call-graph
2560 */
2561 if (unset) {
2562 symbol_conf.use_callchain = false;
2563 callchain->mode = CHAIN_NONE;
2564 return 0;
2565 }
2566
2567 return parse_callchain_report_opt(arg);
2568}
2569
2570static int setup_callchain(struct perf_evlist *evlist)
2571{
2572 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2573 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2574
2575 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002576 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsadd805762016-05-11 18:23:48 +02002577 mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002578 dwarf_callchain_users = true;
2579 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsadd805762016-05-11 18:23:48 +02002580 mode = CALLCHAIN_LBR;
2581 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2582 mode = CALLCHAIN_FP;
2583
2584 if (!callchain_param.enabled &&
2585 callchain_param.mode != CHAIN_NONE &&
2586 mode != CALLCHAIN_NONE) {
2587 symbol_conf.use_callchain = true;
2588 if (callchain_register_param(&callchain_param) < 0) {
2589 ui__error("Can't register callchain params.\n");
2590 return -EINVAL;
2591 }
2592 }
2593
2594 callchain_param.record_mode = mode;
2595 callchain_param.min_percent = 0;
2596 return 0;
2597}
2598
Jiri Olsa55b95772016-05-29 10:21:45 +02002599static int setup_display(const char *str)
2600{
Jiri Olsad940bac2016-11-21 22:33:30 +01002601 const char *display = str ?: "tot";
Jiri Olsa55b95772016-05-29 10:21:45 +02002602
Jiri Olsad940bac2016-11-21 22:33:30 +01002603 if (!strcmp(display, "tot"))
2604 c2c.display = DISPLAY_TOT;
2605 else if (!strcmp(display, "rmt"))
Jiri Olsa55b95772016-05-29 10:21:45 +02002606 c2c.display = DISPLAY_RMT;
2607 else if (!strcmp(display, "lcl"))
2608 c2c.display = DISPLAY_LCL;
2609 else {
2610 pr_err("failed: unknown display type: %s\n", str);
2611 return -1;
2612 }
2613
2614 return 0;
2615}
2616
Jiri Olsafc9c6302016-05-24 14:14:38 +02002617#define for_each_token(__tok, __buf, __sep, __tmp) \
2618 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2619 __tok = strtok_r(NULL, __sep, &__tmp))
2620
Jiri Olsa18f278d2016-10-11 13:39:47 +02002621static int build_cl_output(char *cl_sort, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002622{
2623 char *tok, *tmp, *buf = strdup(cl_sort);
2624 bool add_pid = false;
2625 bool add_tid = false;
2626 bool add_iaddr = false;
2627 bool add_sym = false;
2628 bool add_dso = false;
2629 bool add_src = false;
2630
2631 if (!buf)
2632 return -ENOMEM;
2633
2634 for_each_token(tok, buf, ",", tmp) {
2635 if (!strcmp(tok, "tid")) {
2636 add_tid = true;
2637 } else if (!strcmp(tok, "pid")) {
2638 add_pid = true;
2639 } else if (!strcmp(tok, "iaddr")) {
2640 add_iaddr = true;
2641 add_sym = true;
2642 add_dso = true;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002643 add_src = no_source ? false : true;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002644 } else if (!strcmp(tok, "dso")) {
2645 add_dso = true;
2646 } else if (strcmp(tok, "offset")) {
2647 pr_err("unrecognized sort token: %s\n", tok);
2648 return -EINVAL;
2649 }
2650 }
2651
2652 if (asprintf(&c2c.cl_output,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002653 "%s%s%s%s%s%s%s%s%s%s",
2654 c2c.use_stdio ? "cl_num_empty," : "",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002655 "percent_rmt_hitm,"
2656 "percent_lcl_hitm,"
2657 "percent_stores_l1hit,"
2658 "percent_stores_l1miss,"
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002659 "offset,offset_node,dcacheline_count,",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002660 add_pid ? "pid," : "",
2661 add_tid ? "tid," : "",
2662 add_iaddr ? "iaddr," : "",
2663 "mean_rmt,"
2664 "mean_lcl,"
2665 "mean_load,"
Jiri Olsa8763e6a2017-01-20 10:20:31 +01002666 "tot_recs,"
Jiri Olsafc9c6302016-05-24 14:14:38 +02002667 "cpucnt,",
2668 add_sym ? "symbol," : "",
2669 add_dso ? "dso," : "",
2670 add_src ? "cl_srcline," : "",
2671 "node") < 0)
2672 return -ENOMEM;
2673
2674 c2c.show_src = add_src;
2675
2676 free(buf);
2677 return 0;
2678}
2679
Jiri Olsa18f278d2016-10-11 13:39:47 +02002680static int setup_coalesce(const char *coalesce, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002681{
2682 const char *c = coalesce ?: coalesce_default;
2683
2684 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2685 return -ENOMEM;
2686
Jiri Olsa18f278d2016-10-11 13:39:47 +02002687 if (build_cl_output(c2c.cl_sort, no_source))
Jiri Olsafc9c6302016-05-24 14:14:38 +02002688 return -1;
2689
2690 if (asprintf(&c2c.cl_resort, "offset,%s",
Jiri Olsad940bac2016-11-21 22:33:30 +01002691 c2c.display == DISPLAY_TOT ?
2692 "tot_hitm" :
Jiri Olsafc9c6302016-05-24 14:14:38 +02002693 c2c.display == DISPLAY_RMT ?
2694 "rmt_hitm,lcl_hitm" :
2695 "lcl_hitm,rmt_hitm") < 0)
2696 return -ENOMEM;
2697
2698 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2699 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2700 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2701 return 0;
2702}
2703
Jiri Olsa903a6f12016-09-22 17:36:40 +02002704static int perf_c2c__report(int argc, const char **argv)
2705{
2706 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +02002707 struct ui_progress prog;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002708 struct perf_data data = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002709 .mode = PERF_DATA_MODE_READ,
2710 };
Jiri Olsadd805762016-05-11 18:23:48 +02002711 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
Jiri Olsa55b95772016-05-29 10:21:45 +02002712 const char *display = NULL;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002713 const char *coalesce = NULL;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002714 bool no_source = false;
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002715 const struct option options[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002716 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2717 "file", "vmlinux pathname"),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002718 OPT_STRING('i', "input", &input_name, "file",
2719 "the input file to process"),
Jiri Olsa1e181b92016-06-03 15:40:28 +02002720 OPT_INCR('N', "node-info", &c2c.node_info,
2721 "show extra node info in report (repeat for more info)"),
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002722#ifdef HAVE_SLANG_SUPPORT
2723 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2724#endif
Jiri Olsa74c63a22016-05-02 20:01:59 +02002725 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
Namhyung Kimf75d2892017-03-08 00:08:32 +09002726 "Display only statistic tables (implies --stdio)"),
Jiri Olsa590b6a32016-07-10 16:25:15 +02002727 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2728 "Display full length of symbols"),
Jiri Olsa18f278d2016-10-11 13:39:47 +02002729 OPT_BOOLEAN(0, "no-source", &no_source,
2730 "Do not display Source Line column"),
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02002731 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2732 "Show all captured HITM lines."),
Jiri Olsadd805762016-05-11 18:23:48 +02002733 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2734 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2735 callchain_help, &parse_callchain_opt,
2736 callchain_default_opt),
Jiri Olsad940bac2016-11-21 22:33:30 +01002737 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
Jiri Olsafc9c6302016-05-24 14:14:38 +02002738 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2739 "coalesce fields: pid,tid,iaddr,dso"),
Jiri Olsab7ac4f92016-11-21 22:33:28 +01002740 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002741 OPT_PARENT(c2c_options),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002742 OPT_END()
2743 };
2744 int err = 0;
2745
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002746 argc = parse_options(argc, argv, options, report_c2c_usage,
Jiri Olsa903a6f12016-09-22 17:36:40 +02002747 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +02002748 if (argc)
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002749 usage_with_options(report_c2c_usage, options);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002750
Jiri Olsa74c63a22016-05-02 20:01:59 +02002751 if (c2c.stats_only)
2752 c2c.use_stdio = true;
2753
Jiri Olsa78b27542016-09-22 17:36:44 +02002754 if (!input_name || !strlen(input_name))
2755 input_name = "perf.data";
2756
Jiri Olsa2d4f27992019-02-21 10:41:30 +01002757 data.path = input_name;
2758 data.force = symbol_conf.force;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002759
Jiri Olsa55b95772016-05-29 10:21:45 +02002760 err = setup_display(display);
2761 if (err)
2762 goto out;
2763
Jiri Olsa18f278d2016-10-11 13:39:47 +02002764 err = setup_coalesce(coalesce, no_source);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002765 if (err) {
2766 pr_debug("Failed to initialize hists\n");
2767 goto out;
2768 }
2769
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02002770 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
Jiri Olsac75540e2016-09-22 17:36:41 +02002771 if (err) {
2772 pr_debug("Failed to initialize hists\n");
2773 goto out;
2774 }
2775
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002776 session = perf_session__new(&data, 0, &c2c.tool);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002777 if (session == NULL) {
2778 pr_debug("No memory for session\n");
2779 goto out;
2780 }
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002781
Jiri Olsa1e181b92016-06-03 15:40:28 +02002782 err = setup_nodes(session);
2783 if (err) {
2784 pr_err("Failed setup nodes\n");
2785 goto out;
2786 }
Jiri Olsa903a6f12016-09-22 17:36:40 +02002787
Jiri Olsa7f834c22018-03-09 11:14:40 +01002788 err = mem2node__init(&c2c.mem2node, &session->header.env);
Jiri Olsadd805762016-05-11 18:23:48 +02002789 if (err)
2790 goto out_session;
2791
Jiri Olsa7f834c22018-03-09 11:14:40 +01002792 err = setup_callchain(session->evlist);
2793 if (err)
2794 goto out_mem2node;
2795
Jiri Olsa903a6f12016-09-22 17:36:40 +02002796 if (symbol__init(&session->header.env) < 0)
Jiri Olsa7f834c22018-03-09 11:14:40 +01002797 goto out_mem2node;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002798
2799 /* No pipe support at the moment. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002800 if (perf_data__is_pipe(session->data)) {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002801 pr_debug("No pipe support at the moment.\n");
Jiri Olsa7f834c22018-03-09 11:14:40 +01002802 goto out_mem2node;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002803 }
2804
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002805 if (c2c.use_stdio)
2806 use_browser = 0;
2807 else
2808 use_browser = 1;
2809
2810 setup_browser(false);
2811
Jiri Olsa78b27542016-09-22 17:36:44 +02002812 err = perf_session__process_events(session);
2813 if (err) {
2814 pr_err("failed to process sample\n");
Jiri Olsa7f834c22018-03-09 11:14:40 +01002815 goto out_mem2node;
Jiri Olsa78b27542016-09-22 17:36:44 +02002816 }
2817
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002818 c2c_hists__reinit(&c2c.hists,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002819 "cl_idx,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002820 "dcacheline,"
Jiri Olsa7f834c22018-03-09 11:14:40 +01002821 "dcacheline_node,"
Jiri Olsa03d9fcb2018-03-09 11:14:42 +01002822 "dcacheline_count,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002823 "tot_recs,"
2824 "percent_hitm,"
2825 "tot_hitm,lcl_hitm,rmt_hitm,"
2826 "stores,stores_l1hit,stores_l1miss,"
2827 "dram_lcl,dram_rmt,"
2828 "ld_llcmiss,"
2829 "tot_loads,"
2830 "ld_fbhit,ld_l1hit,ld_l2hit,"
2831 "ld_lclhit,ld_rmthit",
Jiri Olsad940bac2016-11-21 22:33:30 +01002832 c2c.display == DISPLAY_TOT ? "tot_hitm" :
Jiri Olsa55b95772016-05-29 10:21:45 +02002833 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002834 );
2835
Jiri Olsa78b27542016-09-22 17:36:44 +02002836 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2837
2838 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002839 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2840 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +02002841
2842 ui_progress__finish();
2843
Jiri Olsad0802b12018-03-09 11:14:41 +01002844 if (ui_quirks()) {
2845 pr_err("failed to setup UI\n");
2846 goto out_mem2node;
2847 }
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002848
Jiri Olsa2709b972016-08-27 11:40:23 +02002849 perf_c2c_display(session);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002850
Jiri Olsa7f834c22018-03-09 11:14:40 +01002851out_mem2node:
2852 mem2node__exit(&c2c.mem2node);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002853out_session:
2854 perf_session__delete(session);
2855out:
2856 return err;
2857}
2858
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002859static int parse_record_events(const struct option *opt,
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002860 const char *str, int unset __maybe_unused)
2861{
2862 bool *event_set = (bool *) opt->value;
2863
2864 *event_set = true;
2865 return perf_mem_events__parse(str);
2866}
2867
2868
2869static const char * const __usage_record[] = {
2870 "perf c2c record [<options>] [<command>]",
2871 "perf c2c record [<options>] -- <command> [<options>]",
2872 NULL
2873};
2874
2875static const char * const *record_mem_usage = __usage_record;
2876
2877static int perf_c2c__record(int argc, const char **argv)
2878{
2879 int rec_argc, i = 0, j;
2880 const char **rec_argv;
2881 int ret;
2882 bool all_user = false, all_kernel = false;
2883 bool event_set = false;
2884 struct option options[] = {
2885 OPT_CALLBACK('e', "event", &event_set, "event",
2886 "event selector. Use 'perf mem record -e list' to list available events",
2887 parse_record_events),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002888 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2889 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2890 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002891 OPT_PARENT(c2c_options),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002892 OPT_END()
2893 };
2894
2895 if (perf_mem_events__init()) {
2896 pr_err("failed: memory events not supported\n");
2897 return -1;
2898 }
2899
2900 argc = parse_options(argc, argv, options, record_mem_usage,
2901 PARSE_OPT_KEEP_UNKNOWN);
2902
Jiri Olsa8fab7842018-03-09 11:14:37 +01002903 rec_argc = argc + 11; /* max number of arguments */
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002904 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2905 if (!rec_argv)
2906 return -1;
2907
2908 rec_argv[i++] = "record";
2909
2910 if (!event_set) {
2911 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2912 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2913 }
2914
2915 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2916 rec_argv[i++] = "-W";
2917
2918 rec_argv[i++] = "-d";
Jiri Olsa8fab7842018-03-09 11:14:37 +01002919 rec_argv[i++] = "--phys-data";
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002920 rec_argv[i++] = "--sample-cpu";
2921
2922 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2923 if (!perf_mem_events[j].record)
2924 continue;
2925
2926 if (!perf_mem_events[j].supported) {
2927 pr_err("failed: event '%s' not supported\n",
2928 perf_mem_events[j].name);
Martin Kepplingerc896f852017-09-13 21:14:19 +02002929 free(rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002930 return -1;
2931 }
2932
2933 rec_argv[i++] = "-e";
2934 rec_argv[i++] = perf_mem_events__name(j);
2935 };
2936
2937 if (all_user)
2938 rec_argv[i++] = "--all-user";
2939
2940 if (all_kernel)
2941 rec_argv[i++] = "--all-kernel";
2942
2943 for (j = 0; j < argc; j++, i++)
2944 rec_argv[i] = argv[j];
2945
2946 if (verbose > 0) {
2947 pr_debug("calling: ");
2948
2949 j = 0;
2950
2951 while (rec_argv[j]) {
2952 pr_debug("%s ", rec_argv[j]);
2953 j++;
2954 }
2955 pr_debug("\n");
2956 }
2957
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002958 ret = cmd_record(i, rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002959 free(rec_argv);
2960 return ret;
2961}
2962
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002963int cmd_c2c(int argc, const char **argv)
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002964{
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002965 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2966 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002967
2968 if (!argc)
2969 usage_with_options(c2c_usage, c2c_options);
2970
2971 if (!strncmp(argv[0], "rec", 3)) {
2972 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002973 } else if (!strncmp(argv[0], "rep", 3)) {
2974 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002975 } else {
2976 usage_with_options(c2c_usage, c2c_options);
2977 }
2978
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002979 return 0;
2980}