blob: 45c047fdd7ac463edb13ffee17addf8f60fbcf20 [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"
Jiri Olsa903a6f12016-09-22 17:36:40 +020036
Jiri Olsac75540e2016-09-22 17:36:41 +020037struct c2c_hists {
38 struct hists hists;
39 struct perf_hpp_list list;
Jiri Olsab2252ae2016-09-22 17:36:46 +020040 struct c2c_stats stats;
Jiri Olsac75540e2016-09-22 17:36:41 +020041};
42
Jiri Olsa92062d52016-06-05 13:40:53 +020043struct compute_stats {
44 struct stats lcl_hitm;
45 struct stats rmt_hitm;
46 struct stats load;
47};
48
Jiri Olsa78b27542016-09-22 17:36:44 +020049struct c2c_hist_entry {
50 struct c2c_hists *hists;
Jiri Olsab2252ae2016-09-22 17:36:46 +020051 struct c2c_stats stats;
Jiri Olsa1e181b92016-06-03 15:40:28 +020052 unsigned long *cpuset;
Jiri Olsa7f834c22018-03-09 11:14:40 +010053 unsigned long *nodeset;
Jiri Olsa1e181b92016-06-03 15:40:28 +020054 struct c2c_stats *node_stats;
Jiri Olsabb342dae2016-07-06 15:40:09 +020055 unsigned int cacheline_idx;
Jiri Olsa92062d52016-06-05 13:40:53 +020056
57 struct compute_stats cstats;
58
Jiri Olsa78b27542016-09-22 17:36:44 +020059 /*
60 * must be at the end,
61 * because of its callchain dynamic entry
62 */
63 struct hist_entry he;
Jiri Olsa7f834c22018-03-09 11:14:40 +010064
65 unsigned long paddr;
66 unsigned long paddr_cnt;
67 bool paddr_zero;
68 char *nodestr;
Jiri Olsa78b27542016-09-22 17:36:44 +020069};
70
Jiri Olsa190bacc2017-01-20 10:20:32 +010071static char const *coalesce_default = "pid,iaddr";
Jiri Olsafc9c6302016-05-24 14:14:38 +020072
Jiri Olsa903a6f12016-09-22 17:36:40 +020073struct perf_c2c {
Jiri Olsac75540e2016-09-22 17:36:41 +020074 struct perf_tool tool;
75 struct c2c_hists hists;
Jiri Olsa7f834c22018-03-09 11:14:40 +010076 struct mem2node mem2node;
Jiri Olsa1e181b92016-06-03 15:40:28 +020077
78 unsigned long **nodes;
79 int nodes_cnt;
80 int cpus_cnt;
81 int *cpu2node;
82 int node_info;
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +020083
84 bool show_src;
Jiri Olsaaf09b2d2016-10-11 13:52:05 +020085 bool show_all;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +010086 bool use_stdio;
Jiri Olsa74c63a22016-05-02 20:01:59 +020087 bool stats_only;
Jiri Olsa590b6a32016-07-10 16:25:15 +020088 bool symbol_full;
Jiri Olsa7ef2efa2016-07-01 11:12:11 +020089
90 /* HITM shared clines stats */
91 struct c2c_stats hitm_stats;
92 int shared_clines;
Jiri Olsa55b95772016-05-29 10:21:45 +020093
94 int display;
Jiri Olsafc9c6302016-05-24 14:14:38 +020095
96 const char *coalesce;
97 char *cl_sort;
98 char *cl_resort;
99 char *cl_output;
Jiri Olsa55b95772016-05-29 10:21:45 +0200100};
101
102enum {
103 DISPLAY_LCL,
104 DISPLAY_RMT,
Jiri Olsad940bac2016-11-21 22:33:30 +0100105 DISPLAY_TOT,
106 DISPLAY_MAX,
107};
108
109static const char *display_str[DISPLAY_MAX] = {
110 [DISPLAY_LCL] = "Local",
111 [DISPLAY_RMT] = "Remote",
112 [DISPLAY_TOT] = "Total",
Jiri Olsa903a6f12016-09-22 17:36:40 +0200113};
114
Jiri Olsa3a5bfab2016-11-21 22:33:31 +0100115static const struct option c2c_options[] = {
116 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
117 OPT_END()
118};
119
Jiri Olsa903a6f12016-09-22 17:36:40 +0200120static struct perf_c2c c2c;
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200121
Jiri Olsa78b27542016-09-22 17:36:44 +0200122static void *c2c_he_zalloc(size_t size)
123{
124 struct c2c_hist_entry *c2c_he;
125
126 c2c_he = zalloc(size + sizeof(*c2c_he));
127 if (!c2c_he)
128 return NULL;
129
Jiri Olsa1e181b92016-06-03 15:40:28 +0200130 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
131 if (!c2c_he->cpuset)
132 return NULL;
133
Jiri Olsa7f834c22018-03-09 11:14:40 +0100134 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
135 if (!c2c_he->nodeset)
136 return NULL;
137
Jiri Olsa1e181b92016-06-03 15:40:28 +0200138 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
139 if (!c2c_he->node_stats)
140 return NULL;
141
Jiri Olsa92062d52016-06-05 13:40:53 +0200142 init_stats(&c2c_he->cstats.lcl_hitm);
143 init_stats(&c2c_he->cstats.rmt_hitm);
144 init_stats(&c2c_he->cstats.load);
145
Jiri Olsa78b27542016-09-22 17:36:44 +0200146 return &c2c_he->he;
147}
148
149static void c2c_he_free(void *he)
150{
151 struct c2c_hist_entry *c2c_he;
152
153 c2c_he = container_of(he, struct c2c_hist_entry, he);
154 if (c2c_he->hists) {
155 hists__delete_entries(&c2c_he->hists->hists);
156 free(c2c_he->hists);
157 }
158
Jiri Olsa1e181b92016-06-03 15:40:28 +0200159 free(c2c_he->cpuset);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100160 free(c2c_he->nodeset);
161 free(c2c_he->nodestr);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200162 free(c2c_he->node_stats);
Jiri Olsa78b27542016-09-22 17:36:44 +0200163 free(c2c_he);
164}
165
166static struct hist_entry_ops c2c_entry_ops = {
167 .new = c2c_he_zalloc,
168 .free = c2c_he_free,
169};
170
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200171static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200172 const char *sort,
173 int nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200174
Jiri Olsab2252ae2016-09-22 17:36:46 +0200175static struct c2c_hists*
176he__get_c2c_hists(struct hist_entry *he,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200177 const char *sort,
178 int nr_header_lines)
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200179{
180 struct c2c_hist_entry *c2c_he;
181 struct c2c_hists *hists;
182 int ret;
183
184 c2c_he = container_of(he, struct c2c_hist_entry, he);
185 if (c2c_he->hists)
Jiri Olsab2252ae2016-09-22 17:36:46 +0200186 return c2c_he->hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200187
188 hists = c2c_he->hists = zalloc(sizeof(*hists));
189 if (!hists)
190 return NULL;
191
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200192 ret = c2c_hists__init(hists, sort, nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200193 if (ret) {
194 free(hists);
195 return NULL;
196 }
197
Jiri Olsab2252ae2016-09-22 17:36:46 +0200198 return hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200199}
200
Jiri Olsa1e181b92016-06-03 15:40:28 +0200201static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
202 struct perf_sample *sample)
203{
204 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
205 "WARNING: no sample cpu value"))
206 return;
207
208 set_bit(sample->cpu, c2c_he->cpuset);
209}
210
Jiri Olsa7f834c22018-03-09 11:14:40 +0100211static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
212 struct perf_sample *sample)
213{
214 int node;
215
216 if (!sample->phys_addr) {
217 c2c_he->paddr_zero = true;
218 return;
219 }
220
221 node = mem2node__node(&c2c.mem2node, sample->phys_addr);
222 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
223 return;
224
225 set_bit(node, c2c_he->nodeset);
226
227 if (c2c_he->paddr != sample->phys_addr) {
228 c2c_he->paddr_cnt++;
229 c2c_he->paddr = sample->phys_addr;
230 }
231}
232
Jiri Olsa92062d52016-06-05 13:40:53 +0200233static void compute_stats(struct c2c_hist_entry *c2c_he,
234 struct c2c_stats *stats,
235 u64 weight)
236{
237 struct compute_stats *cstats = &c2c_he->cstats;
238
239 if (stats->rmt_hitm)
240 update_stats(&cstats->rmt_hitm, weight);
241 else if (stats->lcl_hitm)
242 update_stats(&cstats->lcl_hitm, weight);
243 else if (stats->load)
244 update_stats(&cstats->load, weight);
245}
246
Jiri Olsa78b27542016-09-22 17:36:44 +0200247static int process_sample_event(struct perf_tool *tool __maybe_unused,
248 union perf_event *event,
249 struct perf_sample *sample,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300250 struct perf_evsel *evsel,
Jiri Olsa78b27542016-09-22 17:36:44 +0200251 struct machine *machine)
252{
Jiri Olsab2252ae2016-09-22 17:36:46 +0200253 struct c2c_hists *c2c_hists = &c2c.hists;
254 struct c2c_hist_entry *c2c_he;
255 struct c2c_stats stats = { .nr_entries = 0, };
Jiri Olsa78b27542016-09-22 17:36:44 +0200256 struct hist_entry *he;
257 struct addr_location al;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200258 struct mem_info *mi, *mi_dup;
Jiri Olsa78b27542016-09-22 17:36:44 +0200259 int ret;
260
261 if (machine__resolve(machine, &al, sample) < 0) {
262 pr_debug("problem processing %d event, skipping it.\n",
263 event->header.type);
264 return -1;
265 }
266
Jiri Olsadd805762016-05-11 18:23:48 +0200267 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
268 evsel, &al, sysctl_perf_event_max_stack);
269 if (ret)
270 goto out;
271
Jiri Olsa78b27542016-09-22 17:36:44 +0200272 mi = sample__resolve_mem(sample, &al);
273 if (mi == NULL)
274 return -ENOMEM;
275
Jiri Olsa5cedb412018-03-07 16:50:07 +0100276 /*
277 * The mi object is released in hists__add_entry_ops,
278 * if it gets sorted out into existing data, so we need
279 * to take the copy now.
280 */
281 mi_dup = mem_info__get(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200282
Jiri Olsab2252ae2016-09-22 17:36:46 +0200283 c2c_decode_stats(&stats, mi);
284
285 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsa78b27542016-09-22 17:36:44 +0200286 &al, NULL, NULL, mi,
287 sample, true);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200288 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100289 goto free_mi;
Jiri Olsa78b27542016-09-22 17:36:44 +0200290
Jiri Olsab2252ae2016-09-22 17:36:46 +0200291 c2c_he = container_of(he, struct c2c_hist_entry, he);
292 c2c_add_stats(&c2c_he->stats, &stats);
293 c2c_add_stats(&c2c_hists->stats, &stats);
294
Jiri Olsa1e181b92016-06-03 15:40:28 +0200295 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100296 c2c_he__set_node(c2c_he, sample);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200297
Jiri Olsab2252ae2016-09-22 17:36:46 +0200298 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsa78b27542016-09-22 17:36:44 +0200299 ret = hist_entry__append_callchain(he, sample);
300
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200301 if (!ret) {
Jiri Olsa1e181b92016-06-03 15:40:28 +0200302 /*
303 * There's already been warning about missing
304 * sample's cpu value. Let's account all to
305 * node 0 in this case, without any further
306 * warning.
307 *
308 * Doing node stats only for single callchain data.
309 */
310 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
311 int node = c2c.cpu2node[cpu];
312
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200313 mi = mi_dup;
314
Jiri Olsafc9c6302016-05-24 14:14:38 +0200315 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200316 if (!c2c_hists)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100317 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200318
Jiri Olsab2252ae2016-09-22 17:36:46 +0200319 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200320 &al, NULL, NULL, mi,
321 sample, true);
322 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100323 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200324
Jiri Olsab2252ae2016-09-22 17:36:46 +0200325 c2c_he = container_of(he, struct c2c_hist_entry, he);
326 c2c_add_stats(&c2c_he->stats, &stats);
327 c2c_add_stats(&c2c_hists->stats, &stats);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200328 c2c_add_stats(&c2c_he->node_stats[node], &stats);
329
Jiri Olsa92062d52016-06-05 13:40:53 +0200330 compute_stats(c2c_he, &stats, sample->weight);
331
Jiri Olsa1e181b92016-06-03 15:40:28 +0200332 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsa7f834c22018-03-09 11:14:40 +0100333 c2c_he__set_node(c2c_he, sample);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200334
335 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200336 ret = hist_entry__append_callchain(he, sample);
337 }
338
339out:
Jiri Olsa78b27542016-09-22 17:36:44 +0200340 addr_location__put(&al);
341 return ret;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200342
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200343free_mi:
Jiri Olsa5cedb412018-03-07 16:50:07 +0100344 mem_info__put(mi_dup);
345 mem_info__put(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200346 ret = -ENOMEM;
347 goto out;
Jiri Olsa78b27542016-09-22 17:36:44 +0200348}
349
350static struct perf_c2c c2c = {
351 .tool = {
352 .sample = process_sample_event,
353 .mmap = perf_event__process_mmap,
354 .mmap2 = perf_event__process_mmap2,
355 .comm = perf_event__process_comm,
356 .exit = perf_event__process_exit,
357 .fork = perf_event__process_fork,
358 .lost = perf_event__process_lost,
359 .ordered_events = true,
360 .ordering_requires_timestamps = true,
361 },
362};
363
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200364static const char * const c2c_usage[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +0200365 "perf c2c {record|report}",
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200366 NULL
367};
368
Jiri Olsa903a6f12016-09-22 17:36:40 +0200369static const char * const __usage_report[] = {
370 "perf c2c report",
371 NULL
372};
373
374static const char * const *report_c2c_usage = __usage_report;
375
Jiri Olsac75540e2016-09-22 17:36:41 +0200376#define C2C_HEADER_MAX 2
377
378struct c2c_header {
379 struct {
380 const char *text;
381 int span;
382 } line[C2C_HEADER_MAX];
383};
384
385struct c2c_dimension {
386 struct c2c_header header;
387 const char *name;
388 int width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200389 struct sort_entry *se;
Jiri Olsac75540e2016-09-22 17:36:41 +0200390
391 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
392 struct hist_entry *, struct hist_entry *);
393 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
394 struct hist_entry *he);
395 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
396 struct hist_entry *he);
397};
398
399struct c2c_fmt {
400 struct perf_hpp_fmt fmt;
401 struct c2c_dimension *dim;
402};
403
Jiri Olsa590b6a32016-07-10 16:25:15 +0200404#define SYMBOL_WIDTH 30
405
406static struct c2c_dimension dim_symbol;
407static struct c2c_dimension dim_srcline;
408
409static int symbol_width(struct hists *hists, struct sort_entry *se)
410{
411 int width = hists__col_len(hists, se->se_width_idx);
412
413 if (!c2c.symbol_full)
414 width = MIN(width, SYMBOL_WIDTH);
415
416 return width;
417}
418
Jiri Olsac75540e2016-09-22 17:36:41 +0200419static int c2c_width(struct perf_hpp_fmt *fmt,
420 struct perf_hpp *hpp __maybe_unused,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300421 struct hists *hists)
Jiri Olsac75540e2016-09-22 17:36:41 +0200422{
423 struct c2c_fmt *c2c_fmt;
Jiri Olsac75540e2016-09-22 17:36:41 +0200424 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +0200425
426 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
427 dim = c2c_fmt->dim;
428
Jiri Olsa590b6a32016-07-10 16:25:15 +0200429 if (dim == &dim_symbol || dim == &dim_srcline)
430 return symbol_width(hists, dim->se);
431
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200432 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
433 c2c_fmt->dim->width;
434}
435
436static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
437 struct hists *hists, int line, int *span)
438{
439 struct perf_hpp_list *hpp_list = hists->hpp_list;
440 struct c2c_fmt *c2c_fmt;
441 struct c2c_dimension *dim;
442 const char *text = NULL;
443 int width = c2c_width(fmt, hpp, hists);
444
445 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
446 dim = c2c_fmt->dim;
447
448 if (dim->se) {
449 text = dim->header.line[line].text;
450 /* Use the last line from sort_entry if not defined. */
451 if (!text && (line == hpp_list->nr_header_lines - 1))
452 text = dim->se->se_header;
453 } else {
454 text = dim->header.line[line].text;
455
456 if (*span) {
457 (*span)--;
458 return 0;
459 } else {
460 *span = dim->header.line[line].span;
461 }
462 }
463
Jiri Olsac75540e2016-09-22 17:36:41 +0200464 if (text == NULL)
465 text = "";
466
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200467 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
Jiri Olsac75540e2016-09-22 17:36:41 +0200468}
469
Jiri Olsacbb88502016-09-22 17:36:48 +0200470#define HEX_STR(__s, __v) \
471({ \
472 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
473 __s; \
474})
475
476static int64_t
477dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
478 struct hist_entry *left, struct hist_entry *right)
479{
480 return sort__dcacheline_cmp(left, right);
481}
482
483static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
484 struct hist_entry *he)
485{
486 uint64_t addr = 0;
487 int width = c2c_width(fmt, hpp, he->hists);
488 char buf[20];
489
490 if (he->mem_info)
491 addr = cl_address(he->mem_info->daddr.addr);
492
493 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
494}
495
Jiri Olsa7f834c22018-03-09 11:14:40 +0100496static int
497dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
498 struct hist_entry *he)
499{
500 struct c2c_hist_entry *c2c_he;
501 int width = c2c_width(fmt, hpp, he->hists);
502
503 c2c_he = container_of(he, struct c2c_hist_entry, he);
504 if (WARN_ON_ONCE(!c2c_he->nodestr))
505 return 0;
506
507 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
508}
509
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200510static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
511 struct hist_entry *he)
512{
513 uint64_t addr = 0;
514 int width = c2c_width(fmt, hpp, he->hists);
515 char buf[20];
516
517 if (he->mem_info)
518 addr = cl_offset(he->mem_info->daddr.al_addr);
519
520 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
521}
522
523static int64_t
524offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
525 struct hist_entry *left, struct hist_entry *right)
526{
527 uint64_t l = 0, r = 0;
528
529 if (left->mem_info)
530 l = cl_offset(left->mem_info->daddr.addr);
531 if (right->mem_info)
532 r = cl_offset(right->mem_info->daddr.addr);
533
534 return (int64_t)(r - l);
535}
536
Jiri Olsa43575a92016-05-03 21:48:56 +0200537static int
538iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
539 struct hist_entry *he)
540{
541 uint64_t addr = 0;
542 int width = c2c_width(fmt, hpp, he->hists);
543 char buf[20];
544
545 if (he->mem_info)
546 addr = he->mem_info->iaddr.addr;
547
548 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
549}
550
551static int64_t
552iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
553 struct hist_entry *left, struct hist_entry *right)
554{
555 return sort__iaddr_cmp(left, right);
556}
557
Jiri Olsa97cb4862016-05-23 16:20:14 +0200558static int
559tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
560 struct hist_entry *he)
561{
562 struct c2c_hist_entry *c2c_he;
563 int width = c2c_width(fmt, hpp, he->hists);
564 unsigned int tot_hitm;
565
566 c2c_he = container_of(he, struct c2c_hist_entry, he);
567 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
568
569 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
570}
571
572static int64_t
573tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
574 struct hist_entry *left, struct hist_entry *right)
575{
576 struct c2c_hist_entry *c2c_left;
577 struct c2c_hist_entry *c2c_right;
578 unsigned int tot_hitm_left;
579 unsigned int tot_hitm_right;
580
581 c2c_left = container_of(left, struct c2c_hist_entry, he);
582 c2c_right = container_of(right, struct c2c_hist_entry, he);
583
584 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
585 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
586
587 return tot_hitm_left - tot_hitm_right;
588}
589
590#define STAT_FN_ENTRY(__f) \
591static int \
592__f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
593 struct hist_entry *he) \
594{ \
595 struct c2c_hist_entry *c2c_he; \
596 int width = c2c_width(fmt, hpp, he->hists); \
597 \
598 c2c_he = container_of(he, struct c2c_hist_entry, he); \
599 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
600 c2c_he->stats.__f); \
601}
602
603#define STAT_FN_CMP(__f) \
604static int64_t \
605__f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
606 struct hist_entry *left, struct hist_entry *right) \
607{ \
608 struct c2c_hist_entry *c2c_left, *c2c_right; \
609 \
610 c2c_left = container_of(left, struct c2c_hist_entry, he); \
611 c2c_right = container_of(right, struct c2c_hist_entry, he); \
612 return c2c_left->stats.__f - c2c_right->stats.__f; \
613}
614
615#define STAT_FN(__f) \
616 STAT_FN_ENTRY(__f) \
617 STAT_FN_CMP(__f)
618
619STAT_FN(rmt_hitm)
620STAT_FN(lcl_hitm)
Jiri Olsa0f188962016-05-04 10:10:11 +0200621STAT_FN(store)
622STAT_FN(st_l1hit)
623STAT_FN(st_l1miss)
Jiri Olsa1295f682016-05-04 10:18:24 +0200624STAT_FN(ld_fbhit)
625STAT_FN(ld_l1hit)
626STAT_FN(ld_l2hit)
Jiri Olsa4d089102016-05-04 10:27:51 +0200627STAT_FN(ld_llchit)
628STAT_FN(rmt_hit)
Jiri Olsa97cb4862016-05-23 16:20:14 +0200629
Jiri Olsa04402d22016-05-19 10:10:51 +0200630static uint64_t llc_miss(struct c2c_stats *stats)
631{
632 uint64_t llcmiss;
633
634 llcmiss = stats->lcl_dram +
635 stats->rmt_dram +
636 stats->rmt_hitm +
637 stats->rmt_hit;
638
639 return llcmiss;
640}
641
642static int
643ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
644 struct hist_entry *he)
645{
646 struct c2c_hist_entry *c2c_he;
647 int width = c2c_width(fmt, hpp, he->hists);
648
649 c2c_he = container_of(he, struct c2c_hist_entry, he);
650
651 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
652 llc_miss(&c2c_he->stats));
653}
654
655static int64_t
656ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
657 struct hist_entry *left, struct hist_entry *right)
658{
659 struct c2c_hist_entry *c2c_left;
660 struct c2c_hist_entry *c2c_right;
661
662 c2c_left = container_of(left, struct c2c_hist_entry, he);
663 c2c_right = container_of(right, struct c2c_hist_entry, he);
664
665 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
666}
667
Jiri Olsa01b84d72016-05-04 10:35:29 +0200668static uint64_t total_records(struct c2c_stats *stats)
669{
670 uint64_t lclmiss, ldcnt, total;
671
672 lclmiss = stats->lcl_dram +
673 stats->rmt_dram +
674 stats->rmt_hitm +
675 stats->rmt_hit;
676
677 ldcnt = lclmiss +
678 stats->ld_fbhit +
679 stats->ld_l1hit +
680 stats->ld_l2hit +
681 stats->ld_llchit +
682 stats->lcl_hitm;
683
684 total = ldcnt +
685 stats->st_l1hit +
686 stats->st_l1miss;
687
688 return total;
689}
690
691static int
692tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
693 struct hist_entry *he)
694{
695 struct c2c_hist_entry *c2c_he;
696 int width = c2c_width(fmt, hpp, he->hists);
697 uint64_t tot_recs;
698
699 c2c_he = container_of(he, struct c2c_hist_entry, he);
700 tot_recs = total_records(&c2c_he->stats);
701
702 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
703}
704
705static int64_t
706tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
707 struct hist_entry *left, struct hist_entry *right)
708{
709 struct c2c_hist_entry *c2c_left;
710 struct c2c_hist_entry *c2c_right;
711 uint64_t tot_recs_left;
712 uint64_t tot_recs_right;
713
714 c2c_left = container_of(left, struct c2c_hist_entry, he);
715 c2c_right = container_of(right, struct c2c_hist_entry, he);
716
717 tot_recs_left = total_records(&c2c_left->stats);
718 tot_recs_right = total_records(&c2c_right->stats);
719
720 return tot_recs_left - tot_recs_right;
721}
722
Jiri Olsa55177c42016-05-19 09:52:37 +0200723static uint64_t total_loads(struct c2c_stats *stats)
724{
725 uint64_t lclmiss, ldcnt;
726
727 lclmiss = stats->lcl_dram +
728 stats->rmt_dram +
729 stats->rmt_hitm +
730 stats->rmt_hit;
731
732 ldcnt = lclmiss +
733 stats->ld_fbhit +
734 stats->ld_l1hit +
735 stats->ld_l2hit +
736 stats->ld_llchit +
737 stats->lcl_hitm;
738
739 return ldcnt;
740}
741
742static int
743tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
744 struct hist_entry *he)
745{
746 struct c2c_hist_entry *c2c_he;
747 int width = c2c_width(fmt, hpp, he->hists);
748 uint64_t tot_recs;
749
750 c2c_he = container_of(he, struct c2c_hist_entry, he);
751 tot_recs = total_loads(&c2c_he->stats);
752
753 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
754}
755
756static int64_t
757tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
758 struct hist_entry *left, struct hist_entry *right)
759{
760 struct c2c_hist_entry *c2c_left;
761 struct c2c_hist_entry *c2c_right;
762 uint64_t tot_recs_left;
763 uint64_t tot_recs_right;
764
765 c2c_left = container_of(left, struct c2c_hist_entry, he);
766 c2c_right = container_of(right, struct c2c_hist_entry, he);
767
768 tot_recs_left = total_loads(&c2c_left->stats);
769 tot_recs_right = total_loads(&c2c_right->stats);
770
771 return tot_recs_left - tot_recs_right;
772}
773
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200774typedef double (get_percent_cb)(struct c2c_hist_entry *);
775
776static int
777percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
778 struct hist_entry *he, get_percent_cb get_percent)
779{
780 struct c2c_hist_entry *c2c_he;
781 int width = c2c_width(fmt, hpp, he->hists);
782 double per;
783
784 c2c_he = container_of(he, struct c2c_hist_entry, he);
785 per = get_percent(c2c_he);
786
Jiri Olsa5a1a99c2016-01-06 16:59:02 +0100787#ifdef HAVE_SLANG_SUPPORT
788 if (use_browser)
789 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
790#endif
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200791 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
792}
793
794static double percent_hitm(struct c2c_hist_entry *c2c_he)
795{
796 struct c2c_hists *hists;
797 struct c2c_stats *stats;
798 struct c2c_stats *total;
Jiri Olsa55b95772016-05-29 10:21:45 +0200799 int tot = 0, st = 0;
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200800 double p;
801
802 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
803 stats = &c2c_he->stats;
804 total = &hists->stats;
805
Jiri Olsa55b95772016-05-29 10:21:45 +0200806 switch (c2c.display) {
807 case DISPLAY_RMT:
808 st = stats->rmt_hitm;
809 tot = total->rmt_hitm;
810 break;
811 case DISPLAY_LCL:
812 st = stats->lcl_hitm;
813 tot = total->lcl_hitm;
Jiri Olsad940bac2016-11-21 22:33:30 +0100814 break;
815 case DISPLAY_TOT:
816 st = stats->tot_hitm;
817 tot = total->tot_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +0200818 default:
819 break;
820 }
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200821
822 p = tot ? (double) st / tot : 0;
823
824 return 100 * p;
825}
826
827#define PERC_STR(__s, __v) \
828({ \
829 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
830 __s; \
831})
832
833static int
834percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
835 struct hist_entry *he)
836{
837 struct c2c_hist_entry *c2c_he;
838 int width = c2c_width(fmt, hpp, he->hists);
839 char buf[10];
840 double per;
841
842 c2c_he = container_of(he, struct c2c_hist_entry, he);
843 per = percent_hitm(c2c_he);
844 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
845}
846
847static int
848percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
849 struct hist_entry *he)
850{
851 return percent_color(fmt, hpp, he, percent_hitm);
852}
853
854static int64_t
855percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
856 struct hist_entry *left, struct hist_entry *right)
857{
858 struct c2c_hist_entry *c2c_left;
859 struct c2c_hist_entry *c2c_right;
860 double per_left;
861 double per_right;
862
863 c2c_left = container_of(left, struct c2c_hist_entry, he);
864 c2c_right = container_of(right, struct c2c_hist_entry, he);
865
866 per_left = percent_hitm(c2c_left);
867 per_right = percent_hitm(c2c_right);
868
869 return per_left - per_right;
870}
871
Jiri Olsa9cb35002016-05-04 12:16:50 +0200872static struct c2c_stats *he_stats(struct hist_entry *he)
873{
874 struct c2c_hist_entry *c2c_he;
875
876 c2c_he = container_of(he, struct c2c_hist_entry, he);
877 return &c2c_he->stats;
878}
879
880static struct c2c_stats *total_stats(struct hist_entry *he)
881{
882 struct c2c_hists *hists;
883
884 hists = container_of(he->hists, struct c2c_hists, hists);
885 return &hists->stats;
886}
887
888static double percent(int st, int tot)
889{
890 return tot ? 100. * (double) st / (double) tot : 0;
891}
892
893#define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
894
895#define PERCENT_FN(__f) \
896static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
897{ \
898 struct c2c_hists *hists; \
899 \
900 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
901 return percent(c2c_he->stats.__f, hists->stats.__f); \
902}
903
904PERCENT_FN(rmt_hitm)
905PERCENT_FN(lcl_hitm)
906PERCENT_FN(st_l1hit)
907PERCENT_FN(st_l1miss)
908
909static int
910percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
911 struct hist_entry *he)
912{
913 int width = c2c_width(fmt, hpp, he->hists);
914 double per = PERCENT(he, rmt_hitm);
915 char buf[10];
916
917 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
918}
919
920static int
921percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
922 struct hist_entry *he)
923{
924 return percent_color(fmt, hpp, he, percent_rmt_hitm);
925}
926
927static int64_t
928percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
929 struct hist_entry *left, struct hist_entry *right)
930{
931 double per_left;
932 double per_right;
933
934 per_left = PERCENT(left, lcl_hitm);
935 per_right = PERCENT(right, lcl_hitm);
936
937 return per_left - per_right;
938}
939
940static int
941percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
942 struct hist_entry *he)
943{
944 int width = c2c_width(fmt, hpp, he->hists);
945 double per = PERCENT(he, lcl_hitm);
946 char buf[10];
947
948 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
949}
950
951static int
952percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
953 struct hist_entry *he)
954{
955 return percent_color(fmt, hpp, he, percent_lcl_hitm);
956}
957
958static int64_t
959percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
960 struct hist_entry *left, struct hist_entry *right)
961{
962 double per_left;
963 double per_right;
964
965 per_left = PERCENT(left, lcl_hitm);
966 per_right = PERCENT(right, lcl_hitm);
967
968 return per_left - per_right;
969}
970
971static int
972percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
973 struct hist_entry *he)
974{
975 int width = c2c_width(fmt, hpp, he->hists);
976 double per = PERCENT(he, st_l1hit);
977 char buf[10];
978
979 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
980}
981
982static int
983percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
984 struct hist_entry *he)
985{
986 return percent_color(fmt, hpp, he, percent_st_l1hit);
987}
988
989static int64_t
990percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
991 struct hist_entry *left, struct hist_entry *right)
992{
993 double per_left;
994 double per_right;
995
996 per_left = PERCENT(left, st_l1hit);
997 per_right = PERCENT(right, st_l1hit);
998
999 return per_left - per_right;
1000}
1001
1002static int
1003percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1004 struct hist_entry *he)
1005{
1006 int width = c2c_width(fmt, hpp, he->hists);
1007 double per = PERCENT(he, st_l1miss);
1008 char buf[10];
1009
1010 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
1011}
1012
1013static int
1014percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1015 struct hist_entry *he)
1016{
1017 return percent_color(fmt, hpp, he, percent_st_l1miss);
1018}
1019
1020static int64_t
1021percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1022 struct hist_entry *left, struct hist_entry *right)
1023{
1024 double per_left;
1025 double per_right;
1026
1027 per_left = PERCENT(left, st_l1miss);
1028 per_right = PERCENT(right, st_l1miss);
1029
1030 return per_left - per_right;
1031}
1032
Jiri Olsa6c70f542016-05-28 12:30:13 +02001033STAT_FN(lcl_dram)
1034STAT_FN(rmt_dram)
1035
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001036static int
1037pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1038 struct hist_entry *he)
1039{
1040 int width = c2c_width(fmt, hpp, he->hists);
1041
1042 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
1043}
1044
1045static int64_t
1046pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1047 struct hist_entry *left, struct hist_entry *right)
1048{
1049 return left->thread->pid_ - right->thread->pid_;
1050}
1051
Jiri Olsa1e181b92016-06-03 15:40:28 +02001052static int64_t
1053empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1054 struct hist_entry *left __maybe_unused,
1055 struct hist_entry *right __maybe_unused)
1056{
1057 return 0;
1058}
1059
1060static int
1061node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1062 struct hist_entry *he)
1063{
1064 struct c2c_hist_entry *c2c_he;
1065 bool first = true;
1066 int node;
1067 int ret = 0;
1068
1069 c2c_he = container_of(he, struct c2c_hist_entry, he);
1070
1071 for (node = 0; node < c2c.nodes_cnt; node++) {
1072 DECLARE_BITMAP(set, c2c.cpus_cnt);
1073
1074 bitmap_zero(set, c2c.cpus_cnt);
1075 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1076
1077 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1078 if (c2c.node_info == 1) {
1079 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1080 advance_hpp(hpp, ret);
1081 }
1082 continue;
1083 }
1084
1085 if (!first) {
1086 ret = scnprintf(hpp->buf, hpp->size, " ");
1087 advance_hpp(hpp, ret);
1088 }
1089
1090 switch (c2c.node_info) {
1091 case 0:
1092 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1093 advance_hpp(hpp, ret);
1094 break;
1095 case 1:
1096 {
1097 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
1098 struct c2c_stats *stats = &c2c_he->node_stats[node];
1099
1100 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1101 advance_hpp(hpp, ret);
1102
Jiri Olsa55b95772016-05-29 10:21:45 +02001103 #define DISPLAY_HITM(__h) \
1104 if (c2c_he->stats.__h> 0) { \
1105 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1106 percent(stats->__h, c2c_he->stats.__h));\
1107 } else { \
1108 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
Jiri Olsa1e181b92016-06-03 15:40:28 +02001109 }
1110
Jiri Olsa55b95772016-05-29 10:21:45 +02001111 switch (c2c.display) {
1112 case DISPLAY_RMT:
1113 DISPLAY_HITM(rmt_hitm);
1114 break;
1115 case DISPLAY_LCL:
1116 DISPLAY_HITM(lcl_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001117 break;
1118 case DISPLAY_TOT:
1119 DISPLAY_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001120 default:
1121 break;
1122 }
1123
1124 #undef DISPLAY_HITM
1125
Jiri Olsa1e181b92016-06-03 15:40:28 +02001126 advance_hpp(hpp, ret);
1127
1128 if (c2c_he->stats.store > 0) {
1129 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1130 percent(stats->store, c2c_he->stats.store));
1131 } else {
1132 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1133 }
1134
1135 advance_hpp(hpp, ret);
1136 break;
1137 }
1138 case 2:
1139 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1140 advance_hpp(hpp, ret);
1141
1142 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1143 advance_hpp(hpp, ret);
1144
1145 ret = scnprintf(hpp->buf, hpp->size, "}");
1146 advance_hpp(hpp, ret);
1147 break;
1148 default:
1149 break;
1150 }
1151
1152 first = false;
1153 }
1154
1155 return 0;
1156}
1157
Jiri Olsa92062d52016-06-05 13:40:53 +02001158static int
1159mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1160 struct hist_entry *he, double mean)
1161{
1162 int width = c2c_width(fmt, hpp, he->hists);
1163 char buf[10];
1164
1165 scnprintf(buf, 10, "%6.0f", mean);
1166 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1167}
1168
1169#define MEAN_ENTRY(__func, __val) \
1170static int \
1171__func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1172{ \
1173 struct c2c_hist_entry *c2c_he; \
1174 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1175 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1176}
1177
1178MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1179MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1180MEAN_ENTRY(mean_load_entry, load);
1181
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001182static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001183cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001184 struct hist_entry *he)
1185{
1186 struct c2c_hist_entry *c2c_he;
1187 int width = c2c_width(fmt, hpp, he->hists);
1188 char buf[10];
1189
1190 c2c_he = container_of(he, struct c2c_hist_entry, he);
1191
1192 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1193 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1194}
1195
Jiri Olsabb342dae2016-07-06 15:40:09 +02001196static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001197cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001198 struct hist_entry *he)
1199{
1200 struct c2c_hist_entry *c2c_he;
1201 int width = c2c_width(fmt, hpp, he->hists);
1202 char buf[10];
1203
1204 c2c_he = container_of(he, struct c2c_hist_entry, he);
1205
1206 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1207 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1208}
1209
1210static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001211cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001212 struct hist_entry *he)
1213{
1214 int width = c2c_width(fmt, hpp, he->hists);
1215
1216 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1217}
1218
Jiri Olsa600a8cf2016-09-22 17:36:47 +02001219#define HEADER_LOW(__h) \
1220 { \
1221 .line[1] = { \
1222 .text = __h, \
1223 }, \
1224 }
1225
1226#define HEADER_BOTH(__h0, __h1) \
1227 { \
1228 .line[0] = { \
1229 .text = __h0, \
1230 }, \
1231 .line[1] = { \
1232 .text = __h1, \
1233 }, \
1234 }
1235
1236#define HEADER_SPAN(__h0, __h1, __s) \
1237 { \
1238 .line[0] = { \
1239 .text = __h0, \
1240 .span = __s, \
1241 }, \
1242 .line[1] = { \
1243 .text = __h1, \
1244 }, \
1245 }
1246
1247#define HEADER_SPAN_LOW(__h) \
1248 { \
1249 .line[1] = { \
1250 .text = __h, \
1251 }, \
1252 }
1253
Jiri Olsacbb88502016-09-22 17:36:48 +02001254static struct c2c_dimension dim_dcacheline = {
1255 .header = HEADER_LOW("Cacheline"),
1256 .name = "dcacheline",
1257 .cmp = dcacheline_cmp,
1258 .entry = dcacheline_entry,
1259 .width = 18,
1260};
1261
Jiri Olsa7f834c22018-03-09 11:14:40 +01001262static struct c2c_dimension dim_dcacheline_node = {
1263 .header = HEADER_LOW("Node"),
1264 .name = "dcacheline_node",
1265 .cmp = empty_cmp,
1266 .entry = dcacheline_node_entry,
1267 .width = 4,
1268};
1269
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01001270static struct c2c_header header_offset_tui = HEADER_LOW("Off");
1271
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001272static struct c2c_dimension dim_offset = {
1273 .header = HEADER_BOTH("Data address", "Offset"),
1274 .name = "offset",
1275 .cmp = offset_cmp,
1276 .entry = offset_entry,
1277 .width = 18,
1278};
1279
Jiri Olsa7f834c22018-03-09 11:14:40 +01001280static struct c2c_dimension dim_offset_node = {
1281 .header = HEADER_LOW("Node"),
1282 .name = "offset_node",
1283 .cmp = empty_cmp,
1284 .entry = dcacheline_node_entry,
1285 .width = 4,
1286};
1287
Jiri Olsa43575a92016-05-03 21:48:56 +02001288static struct c2c_dimension dim_iaddr = {
1289 .header = HEADER_LOW("Code address"),
1290 .name = "iaddr",
1291 .cmp = iaddr_cmp,
1292 .entry = iaddr_entry,
1293 .width = 18,
1294};
1295
Jiri Olsa97cb4862016-05-23 16:20:14 +02001296static struct c2c_dimension dim_tot_hitm = {
1297 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1298 .name = "tot_hitm",
1299 .cmp = tot_hitm_cmp,
1300 .entry = tot_hitm_entry,
1301 .width = 7,
1302};
1303
1304static struct c2c_dimension dim_lcl_hitm = {
1305 .header = HEADER_SPAN_LOW("Lcl"),
1306 .name = "lcl_hitm",
1307 .cmp = lcl_hitm_cmp,
1308 .entry = lcl_hitm_entry,
1309 .width = 7,
1310};
1311
1312static struct c2c_dimension dim_rmt_hitm = {
1313 .header = HEADER_SPAN_LOW("Rmt"),
1314 .name = "rmt_hitm",
1315 .cmp = rmt_hitm_cmp,
1316 .entry = rmt_hitm_entry,
1317 .width = 7,
1318};
1319
1320static struct c2c_dimension dim_cl_rmt_hitm = {
1321 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1322 .name = "cl_rmt_hitm",
1323 .cmp = rmt_hitm_cmp,
1324 .entry = rmt_hitm_entry,
1325 .width = 7,
1326};
1327
1328static struct c2c_dimension dim_cl_lcl_hitm = {
1329 .header = HEADER_SPAN_LOW("Lcl"),
1330 .name = "cl_lcl_hitm",
1331 .cmp = lcl_hitm_cmp,
1332 .entry = lcl_hitm_entry,
1333 .width = 7,
1334};
1335
Jiri Olsa0f188962016-05-04 10:10:11 +02001336static struct c2c_dimension dim_stores = {
1337 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1338 .name = "stores",
1339 .cmp = store_cmp,
1340 .entry = store_entry,
1341 .width = 7,
1342};
1343
1344static struct c2c_dimension dim_stores_l1hit = {
1345 .header = HEADER_SPAN_LOW("L1Hit"),
1346 .name = "stores_l1hit",
1347 .cmp = st_l1hit_cmp,
1348 .entry = st_l1hit_entry,
1349 .width = 7,
1350};
1351
1352static struct c2c_dimension dim_stores_l1miss = {
1353 .header = HEADER_SPAN_LOW("L1Miss"),
1354 .name = "stores_l1miss",
1355 .cmp = st_l1miss_cmp,
1356 .entry = st_l1miss_entry,
1357 .width = 7,
1358};
1359
1360static struct c2c_dimension dim_cl_stores_l1hit = {
1361 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1362 .name = "cl_stores_l1hit",
1363 .cmp = st_l1hit_cmp,
1364 .entry = st_l1hit_entry,
1365 .width = 7,
1366};
1367
1368static struct c2c_dimension dim_cl_stores_l1miss = {
1369 .header = HEADER_SPAN_LOW("L1 Miss"),
1370 .name = "cl_stores_l1miss",
1371 .cmp = st_l1miss_cmp,
1372 .entry = st_l1miss_entry,
1373 .width = 7,
1374};
1375
Jiri Olsa1295f682016-05-04 10:18:24 +02001376static struct c2c_dimension dim_ld_fbhit = {
1377 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1378 .name = "ld_fbhit",
1379 .cmp = ld_fbhit_cmp,
1380 .entry = ld_fbhit_entry,
1381 .width = 7,
1382};
1383
1384static struct c2c_dimension dim_ld_l1hit = {
1385 .header = HEADER_SPAN_LOW("L1"),
1386 .name = "ld_l1hit",
1387 .cmp = ld_l1hit_cmp,
1388 .entry = ld_l1hit_entry,
1389 .width = 7,
1390};
1391
1392static struct c2c_dimension dim_ld_l2hit = {
1393 .header = HEADER_SPAN_LOW("L2"),
1394 .name = "ld_l2hit",
1395 .cmp = ld_l2hit_cmp,
1396 .entry = ld_l2hit_entry,
1397 .width = 7,
1398};
1399
Jiri Olsa4d089102016-05-04 10:27:51 +02001400static struct c2c_dimension dim_ld_llchit = {
1401 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1402 .name = "ld_lclhit",
1403 .cmp = ld_llchit_cmp,
1404 .entry = ld_llchit_entry,
1405 .width = 8,
1406};
1407
1408static struct c2c_dimension dim_ld_rmthit = {
1409 .header = HEADER_SPAN_LOW("Rmt"),
1410 .name = "ld_rmthit",
1411 .cmp = rmt_hit_cmp,
1412 .entry = rmt_hit_entry,
1413 .width = 8,
1414};
1415
Jiri Olsa04402d22016-05-19 10:10:51 +02001416static struct c2c_dimension dim_ld_llcmiss = {
1417 .header = HEADER_BOTH("LLC", "Ld Miss"),
1418 .name = "ld_llcmiss",
1419 .cmp = ld_llcmiss_cmp,
1420 .entry = ld_llcmiss_entry,
1421 .width = 7,
1422};
1423
Jiri Olsa01b84d72016-05-04 10:35:29 +02001424static struct c2c_dimension dim_tot_recs = {
1425 .header = HEADER_BOTH("Total", "records"),
1426 .name = "tot_recs",
1427 .cmp = tot_recs_cmp,
1428 .entry = tot_recs_entry,
1429 .width = 7,
1430};
1431
Jiri Olsa55177c42016-05-19 09:52:37 +02001432static struct c2c_dimension dim_tot_loads = {
1433 .header = HEADER_BOTH("Total", "Loads"),
1434 .name = "tot_loads",
1435 .cmp = tot_loads_cmp,
1436 .entry = tot_loads_entry,
1437 .width = 7,
1438};
1439
Jiri Olsa55b95772016-05-29 10:21:45 +02001440static struct c2c_header percent_hitm_header[] = {
1441 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1442 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
Jiri Olsad940bac2016-11-21 22:33:30 +01001443 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
Jiri Olsa55b95772016-05-29 10:21:45 +02001444};
1445
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001446static struct c2c_dimension dim_percent_hitm = {
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001447 .name = "percent_hitm",
1448 .cmp = percent_hitm_cmp,
1449 .entry = percent_hitm_entry,
1450 .color = percent_hitm_color,
1451 .width = 7,
1452};
1453
Jiri Olsa9cb35002016-05-04 12:16:50 +02001454static struct c2c_dimension dim_percent_rmt_hitm = {
1455 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1456 .name = "percent_rmt_hitm",
1457 .cmp = percent_rmt_hitm_cmp,
1458 .entry = percent_rmt_hitm_entry,
1459 .color = percent_rmt_hitm_color,
1460 .width = 7,
1461};
1462
1463static struct c2c_dimension dim_percent_lcl_hitm = {
1464 .header = HEADER_SPAN_LOW("Lcl"),
1465 .name = "percent_lcl_hitm",
1466 .cmp = percent_lcl_hitm_cmp,
1467 .entry = percent_lcl_hitm_entry,
1468 .color = percent_lcl_hitm_color,
1469 .width = 7,
1470};
1471
1472static struct c2c_dimension dim_percent_stores_l1hit = {
1473 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1474 .name = "percent_stores_l1hit",
1475 .cmp = percent_stores_l1hit_cmp,
1476 .entry = percent_stores_l1hit_entry,
1477 .color = percent_stores_l1hit_color,
1478 .width = 7,
1479};
1480
1481static struct c2c_dimension dim_percent_stores_l1miss = {
1482 .header = HEADER_SPAN_LOW("L1 Miss"),
1483 .name = "percent_stores_l1miss",
1484 .cmp = percent_stores_l1miss_cmp,
1485 .entry = percent_stores_l1miss_entry,
1486 .color = percent_stores_l1miss_color,
1487 .width = 7,
1488};
1489
Jiri Olsa6c70f542016-05-28 12:30:13 +02001490static struct c2c_dimension dim_dram_lcl = {
1491 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1492 .name = "dram_lcl",
1493 .cmp = lcl_dram_cmp,
1494 .entry = lcl_dram_entry,
1495 .width = 8,
1496};
1497
1498static struct c2c_dimension dim_dram_rmt = {
1499 .header = HEADER_SPAN_LOW("Rmt"),
1500 .name = "dram_rmt",
1501 .cmp = rmt_dram_cmp,
1502 .entry = rmt_dram_entry,
1503 .width = 8,
1504};
1505
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001506static struct c2c_dimension dim_pid = {
1507 .header = HEADER_LOW("Pid"),
1508 .name = "pid",
1509 .cmp = pid_cmp,
1510 .entry = pid_entry,
1511 .width = 7,
1512};
1513
Jiri Olsae87019c2016-05-25 08:50:10 +02001514static struct c2c_dimension dim_tid = {
1515 .header = HEADER_LOW("Tid"),
1516 .name = "tid",
1517 .se = &sort_thread,
1518};
1519
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001520static struct c2c_dimension dim_symbol = {
1521 .name = "symbol",
1522 .se = &sort_sym,
1523};
1524
1525static struct c2c_dimension dim_dso = {
1526 .header = HEADER_BOTH("Shared", "Object"),
1527 .name = "dso",
1528 .se = &sort_dso,
1529};
1530
Jiri Olsa1e181b92016-06-03 15:40:28 +02001531static struct c2c_header header_node[3] = {
1532 HEADER_LOW("Node"),
1533 HEADER_LOW("Node{cpus %hitms %stores}"),
1534 HEADER_LOW("Node{cpu list}"),
1535};
1536
1537static struct c2c_dimension dim_node = {
1538 .name = "node",
1539 .cmp = empty_cmp,
1540 .entry = node_entry,
1541 .width = 4,
1542};
1543
Jiri Olsa92062d52016-06-05 13:40:53 +02001544static struct c2c_dimension dim_mean_rmt = {
1545 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1546 .name = "mean_rmt",
1547 .cmp = empty_cmp,
1548 .entry = mean_rmt_entry,
1549 .width = 8,
1550};
1551
1552static struct c2c_dimension dim_mean_lcl = {
1553 .header = HEADER_SPAN_LOW("lcl hitm"),
1554 .name = "mean_lcl",
1555 .cmp = empty_cmp,
1556 .entry = mean_lcl_entry,
1557 .width = 8,
1558};
1559
1560static struct c2c_dimension dim_mean_load = {
1561 .header = HEADER_SPAN_LOW("load"),
1562 .name = "mean_load",
1563 .cmp = empty_cmp,
1564 .entry = mean_load_entry,
1565 .width = 8,
1566};
1567
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001568static struct c2c_dimension dim_cpucnt = {
1569 .header = HEADER_BOTH("cpu", "cnt"),
1570 .name = "cpucnt",
1571 .cmp = empty_cmp,
1572 .entry = cpucnt_entry,
1573 .width = 8,
1574};
1575
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001576static struct c2c_dimension dim_srcline = {
1577 .name = "cl_srcline",
1578 .se = &sort_srcline,
1579};
1580
Jiri Olsabb342dae2016-07-06 15:40:09 +02001581static struct c2c_dimension dim_dcacheline_idx = {
1582 .header = HEADER_LOW("Index"),
1583 .name = "cl_idx",
1584 .cmp = empty_cmp,
1585 .entry = cl_idx_entry,
1586 .width = 5,
1587};
1588
1589static struct c2c_dimension dim_dcacheline_num = {
1590 .header = HEADER_LOW("Num"),
1591 .name = "cl_num",
1592 .cmp = empty_cmp,
1593 .entry = cl_idx_entry,
1594 .width = 5,
1595};
1596
1597static struct c2c_dimension dim_dcacheline_num_empty = {
1598 .header = HEADER_LOW("Num"),
1599 .name = "cl_num_empty",
1600 .cmp = empty_cmp,
1601 .entry = cl_idx_empty_entry,
1602 .width = 5,
1603};
1604
Jiri Olsac75540e2016-09-22 17:36:41 +02001605static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +02001606 &dim_dcacheline,
Jiri Olsa7f834c22018-03-09 11:14:40 +01001607 &dim_dcacheline_node,
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001608 &dim_offset,
Jiri Olsa7f834c22018-03-09 11:14:40 +01001609 &dim_offset_node,
Jiri Olsa43575a92016-05-03 21:48:56 +02001610 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +02001611 &dim_tot_hitm,
1612 &dim_lcl_hitm,
1613 &dim_rmt_hitm,
1614 &dim_cl_lcl_hitm,
1615 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +02001616 &dim_stores,
1617 &dim_stores_l1hit,
1618 &dim_stores_l1miss,
1619 &dim_cl_stores_l1hit,
1620 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +02001621 &dim_ld_fbhit,
1622 &dim_ld_l1hit,
1623 &dim_ld_l2hit,
Jiri Olsa4d089102016-05-04 10:27:51 +02001624 &dim_ld_llchit,
1625 &dim_ld_rmthit,
Jiri Olsa04402d22016-05-19 10:10:51 +02001626 &dim_ld_llcmiss,
Jiri Olsa01b84d72016-05-04 10:35:29 +02001627 &dim_tot_recs,
Jiri Olsa55177c42016-05-19 09:52:37 +02001628 &dim_tot_loads,
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001629 &dim_percent_hitm,
Jiri Olsa9cb35002016-05-04 12:16:50 +02001630 &dim_percent_rmt_hitm,
1631 &dim_percent_lcl_hitm,
1632 &dim_percent_stores_l1hit,
1633 &dim_percent_stores_l1miss,
Jiri Olsa6c70f542016-05-28 12:30:13 +02001634 &dim_dram_lcl,
1635 &dim_dram_rmt,
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001636 &dim_pid,
Jiri Olsae87019c2016-05-25 08:50:10 +02001637 &dim_tid,
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001638 &dim_symbol,
1639 &dim_dso,
Jiri Olsa1e181b92016-06-03 15:40:28 +02001640 &dim_node,
Jiri Olsa92062d52016-06-05 13:40:53 +02001641 &dim_mean_rmt,
1642 &dim_mean_lcl,
1643 &dim_mean_load,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001644 &dim_cpucnt,
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001645 &dim_srcline,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001646 &dim_dcacheline_idx,
1647 &dim_dcacheline_num,
1648 &dim_dcacheline_num_empty,
Jiri Olsac75540e2016-09-22 17:36:41 +02001649 NULL,
1650};
1651
1652static void fmt_free(struct perf_hpp_fmt *fmt)
1653{
1654 struct c2c_fmt *c2c_fmt;
1655
1656 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1657 free(c2c_fmt);
1658}
1659
1660static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1661{
1662 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1663 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1664
1665 return c2c_a->dim == c2c_b->dim;
1666}
1667
1668static struct c2c_dimension *get_dimension(const char *name)
1669{
1670 unsigned int i;
1671
1672 for (i = 0; dimensions[i]; i++) {
1673 struct c2c_dimension *dim = dimensions[i];
1674
1675 if (!strcmp(dim->name, name))
1676 return dim;
1677 };
1678
1679 return NULL;
1680}
1681
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001682static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1683 struct hist_entry *he)
1684{
1685 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1686 struct c2c_dimension *dim = c2c_fmt->dim;
1687 size_t len = fmt->user_len;
1688
Jiri Olsa590b6a32016-07-10 16:25:15 +02001689 if (!len) {
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001690 len = hists__col_len(he->hists, dim->se->se_width_idx);
1691
Jiri Olsa590b6a32016-07-10 16:25:15 +02001692 if (dim == &dim_symbol || dim == &dim_srcline)
1693 len = symbol_width(he->hists, dim->se);
1694 }
1695
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001696 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1697}
1698
1699static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1700 struct hist_entry *a, struct hist_entry *b)
1701{
1702 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1703 struct c2c_dimension *dim = c2c_fmt->dim;
1704
1705 return dim->se->se_cmp(a, b);
1706}
1707
1708static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1709 struct hist_entry *a, struct hist_entry *b)
1710{
1711 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1712 struct c2c_dimension *dim = c2c_fmt->dim;
1713 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1714
1715 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1716 return collapse_fn(a, b);
1717}
1718
Jiri Olsac75540e2016-09-22 17:36:41 +02001719static struct c2c_fmt *get_format(const char *name)
1720{
1721 struct c2c_dimension *dim = get_dimension(name);
1722 struct c2c_fmt *c2c_fmt;
1723 struct perf_hpp_fmt *fmt;
1724
1725 if (!dim)
1726 return NULL;
1727
1728 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1729 if (!c2c_fmt)
1730 return NULL;
1731
1732 c2c_fmt->dim = dim;
1733
1734 fmt = &c2c_fmt->fmt;
1735 INIT_LIST_HEAD(&fmt->list);
1736 INIT_LIST_HEAD(&fmt->sort_list);
1737
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001738 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1739 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
Jiri Olsa9cb35002016-05-04 12:16:50 +02001740 fmt->color = dim->se ? NULL : dim->color;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001741 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +02001742 fmt->header = c2c_header;
1743 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001744 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +02001745 fmt->equal = fmt_equal;
1746 fmt->free = fmt_free;
1747
1748 return c2c_fmt;
1749}
1750
1751static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1752{
1753 struct c2c_fmt *c2c_fmt = get_format(name);
1754
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001755 if (!c2c_fmt) {
1756 reset_dimensions();
1757 return output_field_add(hpp_list, name);
1758 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001759
1760 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1761 return 0;
1762}
1763
1764static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1765{
1766 struct c2c_fmt *c2c_fmt = get_format(name);
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001767 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +02001768
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001769 if (!c2c_fmt) {
1770 reset_dimensions();
1771 return sort_dimension__add(hpp_list, name, NULL, 0);
1772 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001773
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001774 dim = c2c_fmt->dim;
1775 if (dim == &dim_dso)
1776 hpp_list->dso = 1;
1777
Jiri Olsac75540e2016-09-22 17:36:41 +02001778 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1779 return 0;
1780}
1781
1782#define PARSE_LIST(_list, _fn) \
1783 do { \
1784 char *tmp, *tok; \
1785 ret = 0; \
1786 \
1787 if (!_list) \
1788 break; \
1789 \
1790 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1791 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1792 ret = _fn(hpp_list, tok); \
1793 if (ret == -EINVAL) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001794 pr_err("Invalid --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001795 break; \
1796 } else if (ret == -ESRCH) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001797 pr_err("Unknown --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001798 break; \
1799 } \
1800 } \
1801 } while (0)
1802
1803static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1804 const char *output_,
1805 const char *sort_)
1806{
1807 char *output = output_ ? strdup(output_) : NULL;
1808 char *sort = sort_ ? strdup(sort_) : NULL;
1809 int ret;
1810
1811 PARSE_LIST(output, c2c_hists__init_output);
1812 PARSE_LIST(sort, c2c_hists__init_sort);
1813
1814 /* copy sort keys to output fields */
1815 perf_hpp__setup_output_field(hpp_list);
1816
1817 /*
1818 * We dont need other sorting keys other than those
1819 * we already specified. It also really slows down
1820 * the processing a lot with big number of output
1821 * fields, so switching this off for c2c.
1822 */
1823
1824#if 0
1825 /* and then copy output fields to sort keys */
1826 perf_hpp__append_sort_keys(&hists->list);
1827#endif
1828
1829 free(output);
1830 free(sort);
1831 return ret;
1832}
1833
1834static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001835 const char *sort,
1836 int nr_header_lines)
Jiri Olsac75540e2016-09-22 17:36:41 +02001837{
1838 __hists__init(&hists->hists, &hists->list);
1839
1840 /*
1841 * Initialize only with sort fields, we need to resort
1842 * later anyway, and that's where we add output fields
1843 * as well.
1844 */
1845 perf_hpp_list__init(&hists->list);
1846
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001847 /* Overload number of header lines.*/
1848 hists->list.nr_header_lines = nr_header_lines;
1849
Jiri Olsac75540e2016-09-22 17:36:41 +02001850 return hpp_list__parse(&hists->list, NULL, sort);
1851}
1852
Jiri Olsac75540e2016-09-22 17:36:41 +02001853static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1854 const char *output,
1855 const char *sort)
1856{
1857 perf_hpp__reset_output_field(&c2c_hists->list);
1858 return hpp_list__parse(&c2c_hists->list, output, sort);
1859}
1860
Jiri Olsa9857b712016-08-17 14:55:23 +02001861#define DISPLAY_LINE_LIMIT 0.0005
1862
1863static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1864{
1865 struct c2c_hist_entry *c2c_he;
1866 double ld_dist;
1867
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02001868 if (c2c.show_all)
1869 return true;
Jiri Olsa9857b712016-08-17 14:55:23 +02001870
1871 c2c_he = container_of(he, struct c2c_hist_entry, he);
1872
Jiri Olsa55b95772016-05-29 10:21:45 +02001873#define FILTER_HITM(__h) \
1874 if (stats->__h) { \
1875 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1876 if (ld_dist < DISPLAY_LINE_LIMIT) \
1877 he->filtered = HIST_FILTER__C2C; \
1878 } else { \
1879 he->filtered = HIST_FILTER__C2C; \
Jiri Olsa9857b712016-08-17 14:55:23 +02001880 }
1881
Jiri Olsa55b95772016-05-29 10:21:45 +02001882 switch (c2c.display) {
1883 case DISPLAY_LCL:
1884 FILTER_HITM(lcl_hitm);
1885 break;
1886 case DISPLAY_RMT:
1887 FILTER_HITM(rmt_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001888 break;
1889 case DISPLAY_TOT:
1890 FILTER_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001891 default:
1892 break;
1893 };
1894
1895#undef FILTER_HITM
1896
Jiri Olsa9857b712016-08-17 14:55:23 +02001897 return he->filtered == 0;
1898}
1899
1900static inline int valid_hitm_or_store(struct hist_entry *he)
1901{
1902 struct c2c_hist_entry *c2c_he;
Jiri Olsa55b95772016-05-29 10:21:45 +02001903 bool has_hitm;
Jiri Olsa9857b712016-08-17 14:55:23 +02001904
1905 c2c_he = container_of(he, struct c2c_hist_entry, he);
Jiri Olsad940bac2016-11-21 22:33:30 +01001906 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1907 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1908 c2c_he->stats.rmt_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +02001909 return has_hitm || c2c_he->stats.store;
Jiri Olsa9857b712016-08-17 14:55:23 +02001910}
1911
Jiri Olsa7f834c22018-03-09 11:14:40 +01001912static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
1913{
1914 struct c2c_dimension *dim;
1915
1916 dim = &c2c.hists == c2c_he->hists ?
1917 &dim_dcacheline_node : &dim_offset_node;
1918
1919 if (len > dim->width)
1920 dim->width = len;
1921}
1922
1923static int set_nodestr(struct c2c_hist_entry *c2c_he)
1924{
1925 char buf[30];
1926 int len;
1927
1928 if (c2c_he->nodestr)
1929 return 0;
1930
1931 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
1932 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
1933 buf, sizeof(buf));
1934 } else {
1935 len = scnprintf(buf, sizeof(buf), "N/A");
1936 }
1937
1938 set_node_width(c2c_he, len);
1939 c2c_he->nodestr = strdup(buf);
1940 return c2c_he->nodestr ? 0 : -ENOMEM;
1941}
1942
Jiri Olsa37731382018-03-09 11:14:38 +01001943static void calc_width(struct c2c_hist_entry *c2c_he)
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001944{
1945 struct c2c_hists *c2c_hists;
1946
Jiri Olsa37731382018-03-09 11:14:38 +01001947 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1948 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
Jiri Olsa7f834c22018-03-09 11:14:40 +01001949 set_nodestr(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001950}
1951
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001952static int filter_cb(struct hist_entry *he)
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001953{
Jiri Olsa37731382018-03-09 11:14:38 +01001954 struct c2c_hist_entry *c2c_he;
1955
1956 c2c_he = container_of(he, struct c2c_hist_entry, he);
1957
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001958 if (c2c.show_src && !he->srcline)
1959 he->srcline = hist_entry__get_srcline(he);
1960
Jiri Olsa37731382018-03-09 11:14:38 +01001961 calc_width(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001962
Jiri Olsa9857b712016-08-17 14:55:23 +02001963 if (!valid_hitm_or_store(he))
1964 he->filtered = HIST_FILTER__C2C;
1965
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001966 return 0;
1967}
1968
1969static int resort_cl_cb(struct hist_entry *he)
1970{
1971 struct c2c_hist_entry *c2c_he;
1972 struct c2c_hists *c2c_hists;
Jiri Olsa9857b712016-08-17 14:55:23 +02001973 bool display = he__display(he, &c2c.hitm_stats);
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001974
1975 c2c_he = container_of(he, struct c2c_hist_entry, he);
1976 c2c_hists = c2c_he->hists;
1977
Jiri Olsa9857b712016-08-17 14:55:23 +02001978 if (display && c2c_hists) {
Jiri Olsabb342dae2016-07-06 15:40:09 +02001979 static unsigned int idx;
1980
1981 c2c_he->cacheline_idx = idx++;
Jiri Olsabc229c22018-03-09 11:14:39 +01001982 calc_width(c2c_he);
Jiri Olsabb342dae2016-07-06 15:40:09 +02001983
Jiri Olsafc9c6302016-05-24 14:14:38 +02001984 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
Jiri Olsa22dd59d2016-05-10 14:08:29 +02001985
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001986 hists__collapse_resort(&c2c_hists->hists, NULL);
1987 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
1988 }
1989
1990 return 0;
1991}
1992
Jiri Olsa1e181b92016-06-03 15:40:28 +02001993static void setup_nodes_header(void)
1994{
1995 dim_node.header = header_node[c2c.node_info];
1996}
1997
1998static int setup_nodes(struct perf_session *session)
1999{
2000 struct numa_node *n;
2001 unsigned long **nodes;
2002 int node, cpu;
2003 int *cpu2node;
2004
2005 if (c2c.node_info > 2)
2006 c2c.node_info = 2;
2007
2008 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
2009 c2c.cpus_cnt = session->header.env.nr_cpus_online;
2010
2011 n = session->header.env.numa_nodes;
2012 if (!n)
2013 return -EINVAL;
2014
2015 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
2016 if (!nodes)
2017 return -ENOMEM;
2018
2019 c2c.nodes = nodes;
2020
2021 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
2022 if (!cpu2node)
2023 return -ENOMEM;
2024
2025 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
2026 cpu2node[cpu] = -1;
2027
2028 c2c.cpu2node = cpu2node;
2029
2030 for (node = 0; node < c2c.nodes_cnt; node++) {
2031 struct cpu_map *map = n[node].map;
2032 unsigned long *set;
2033
2034 set = bitmap_alloc(c2c.cpus_cnt);
2035 if (!set)
2036 return -ENOMEM;
2037
2038 for (cpu = 0; cpu < map->nr; cpu++) {
2039 set_bit(map->map[cpu], set);
2040
2041 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
2042 return -EINVAL;
2043
2044 cpu2node[map->map[cpu]] = node;
2045 }
2046
2047 nodes[node] = set;
2048 }
2049
2050 setup_nodes_header();
2051 return 0;
2052}
2053
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002054#define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
2055
2056static int resort_hitm_cb(struct hist_entry *he)
2057{
2058 struct c2c_hist_entry *c2c_he;
2059 c2c_he = container_of(he, struct c2c_hist_entry, he);
2060
2061 if (HAS_HITMS(c2c_he)) {
2062 c2c.shared_clines++;
2063 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
2064 }
2065
2066 return 0;
2067}
2068
2069static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
2070{
2071 struct rb_node *next = rb_first(&hists->entries);
2072 int ret = 0;
2073
2074 while (next) {
2075 struct hist_entry *he;
2076
2077 he = rb_entry(next, struct hist_entry, rb_node);
2078 ret = cb(he);
2079 if (ret)
2080 break;
2081 next = rb_next(&he->rb_node);
2082 }
2083
2084 return ret;
2085}
2086
Jiri Olsa74c63a22016-05-02 20:01:59 +02002087static void print_c2c__display_stats(FILE *out)
2088{
2089 int llc_misses;
2090 struct c2c_stats *stats = &c2c.hists.stats;
2091
2092 llc_misses = stats->lcl_dram +
2093 stats->rmt_dram +
2094 stats->rmt_hit +
2095 stats->rmt_hitm;
2096
2097 fprintf(out, "=================================================\n");
2098 fprintf(out, " Trace Event Information \n");
2099 fprintf(out, "=================================================\n");
2100 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2101 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2102 fprintf(out, " Load Operations : %10d\n", stats->load);
2103 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2104 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2105 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2106 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2107 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2108 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2109 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2110 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2111 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2112 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2113 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2114 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2115 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2116 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2117 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2118 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2119 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2120 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2121 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2122 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2123 fprintf(out, " Store Operations : %10d\n", stats->store);
2124 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2125 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2126 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2127 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2128 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2129 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2130}
2131
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002132static void print_shared_cacheline_info(FILE *out)
2133{
2134 struct c2c_stats *stats = &c2c.hitm_stats;
2135 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2136
2137 fprintf(out, "=================================================\n");
2138 fprintf(out, " Global Shared Cache Line Event Information \n");
2139 fprintf(out, "=================================================\n");
2140 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2141 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2142 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2143 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2144 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2145 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2146 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2147 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2148 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2149 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2150}
2151
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002152static void print_cacheline(struct c2c_hists *c2c_hists,
2153 struct hist_entry *he_cl,
2154 struct perf_hpp_list *hpp_list,
2155 FILE *out)
2156{
2157 char bf[1000];
2158 struct perf_hpp hpp = {
2159 .buf = bf,
2160 .size = 1000,
2161 };
2162 static bool once;
2163
2164 if (!once) {
2165 hists__fprintf_headers(&c2c_hists->hists, out);
2166 once = true;
2167 } else {
2168 fprintf(out, "\n");
2169 }
2170
Jiri Olsabb342dae2016-07-06 15:40:09 +02002171 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002172 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2173 fprintf(out, "%s\n", bf);
Jiri Olsabb342dae2016-07-06 15:40:09 +02002174 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002175
2176 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, true);
2177}
2178
2179static void print_pareto(FILE *out)
2180{
2181 struct perf_hpp_list hpp_list;
2182 struct rb_node *nd;
2183 int ret;
2184
2185 perf_hpp_list__init(&hpp_list);
2186 ret = hpp_list__parse(&hpp_list,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002187 "cl_num,"
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002188 "cl_rmt_hitm,"
2189 "cl_lcl_hitm,"
2190 "cl_stores_l1hit,"
2191 "cl_stores_l1miss,"
2192 "dcacheline",
2193 NULL);
2194
2195 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2196 return;
2197
2198 nd = rb_first(&c2c.hists.hists.entries);
2199
2200 for (; nd; nd = rb_next(nd)) {
2201 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2202 struct c2c_hist_entry *c2c_he;
2203
2204 if (he->filtered)
2205 continue;
2206
2207 c2c_he = container_of(he, struct c2c_hist_entry, he);
2208 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2209 }
2210}
2211
Jiri Olsa2709b972016-08-27 11:40:23 +02002212static void print_c2c_info(FILE *out, struct perf_session *session)
2213{
2214 struct perf_evlist *evlist = session->evlist;
2215 struct perf_evsel *evsel;
2216 bool first = true;
2217
2218 fprintf(out, "=================================================\n");
2219 fprintf(out, " c2c details \n");
2220 fprintf(out, "=================================================\n");
2221
2222 evlist__for_each_entry(evlist, evsel) {
2223 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2224 perf_evsel__name(evsel));
2225 first = false;
2226 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002227 fprintf(out, " Cachelines sort on : %s HITMs\n",
Jiri Olsad940bac2016-11-21 22:33:30 +01002228 display_str[c2c.display]);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002229 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
Jiri Olsa2709b972016-08-27 11:40:23 +02002230}
2231
2232static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002233{
2234 setup_pager();
2235
Jiri Olsa74c63a22016-05-02 20:01:59 +02002236 print_c2c__display_stats(out);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002237 fprintf(out, "\n");
2238 print_shared_cacheline_info(out);
Jiri Olsa2709b972016-08-27 11:40:23 +02002239 fprintf(out, "\n");
2240 print_c2c_info(out, session);
Jiri Olsa74c63a22016-05-02 20:01:59 +02002241
2242 if (c2c.stats_only)
2243 return;
2244
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002245 fprintf(out, "\n");
2246 fprintf(out, "=================================================\n");
2247 fprintf(out, " Shared Data Cache Line Table \n");
2248 fprintf(out, "=================================================\n");
2249 fprintf(out, "#\n");
2250
2251 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, false);
2252
2253 fprintf(out, "\n");
2254 fprintf(out, "=================================================\n");
2255 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2256 fprintf(out, "=================================================\n");
2257 fprintf(out, "#\n");
2258
2259 print_pareto(out);
2260}
Jiri Olsa1e181b92016-06-03 15:40:28 +02002261
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002262#ifdef HAVE_SLANG_SUPPORT
2263static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2264{
2265 u64 nr_entries = 0;
2266 struct rb_node *nd = rb_first(&hb->hists->entries);
2267
2268 while (nd) {
2269 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2270
2271 if (!he->filtered)
2272 nr_entries++;
2273
2274 nd = rb_next(nd);
2275 }
2276
2277 hb->nr_non_filtered_entries = nr_entries;
2278}
2279
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002280struct c2c_cacheline_browser {
2281 struct hist_browser hb;
2282 struct hist_entry *he;
2283};
2284
2285static int
2286perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2287 char *bf, size_t size)
2288{
2289 struct c2c_cacheline_browser *cl_browser;
2290 struct hist_entry *he;
2291 uint64_t addr = 0;
2292
2293 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2294 he = cl_browser->he;
2295
2296 if (he->mem_info)
2297 addr = cl_address(he->mem_info->daddr.addr);
2298
2299 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2300 return 0;
2301}
2302
2303static struct c2c_cacheline_browser*
2304c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2305{
2306 struct c2c_cacheline_browser *browser;
2307
2308 browser = zalloc(sizeof(*browser));
2309 if (browser) {
2310 hist_browser__init(&browser->hb, hists);
2311 browser->hb.c2c_filter = true;
2312 browser->hb.title = perf_c2c_cacheline_browser__title;
2313 browser->he = he;
2314 }
2315
2316 return browser;
2317}
2318
2319static int perf_c2c__browse_cacheline(struct hist_entry *he)
2320{
2321 struct c2c_hist_entry *c2c_he;
2322 struct c2c_hists *c2c_hists;
2323 struct c2c_cacheline_browser *cl_browser;
2324 struct hist_browser *browser;
2325 int key = -1;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002326 const char help[] =
Kim Phillips239fb4f2017-11-14 15:04:47 -06002327 " ENTER Toggle callchains (if present) \n"
2328 " n Toggle Node details info \n"
2329 " s Toggle full length of symbol and source line columns \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002330 " q Return back to cacheline list \n";
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002331
Jiri Olsa590b6a32016-07-10 16:25:15 +02002332 /* Display compact version first. */
2333 c2c.symbol_full = false;
2334
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002335 c2c_he = container_of(he, struct c2c_hist_entry, he);
2336 c2c_hists = c2c_he->hists;
2337
2338 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2339 if (cl_browser == NULL)
2340 return -1;
2341
2342 browser = &cl_browser->hb;
2343
2344 /* reset abort key so that it can get Ctrl-C as a key */
2345 SLang_reset_tty();
2346 SLang_init_tty(0, 0, 0);
2347
2348 c2c_browser__update_nr_entries(browser);
2349
2350 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002351 key = hist_browser__run(browser, "? - help", true);
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002352
2353 switch (key) {
Jiri Olsa590b6a32016-07-10 16:25:15 +02002354 case 's':
2355 c2c.symbol_full = !c2c.symbol_full;
2356 break;
Jiri Olsa1a56a422016-07-10 16:30:27 +02002357 case 'n':
2358 c2c.node_info = (c2c.node_info + 1) % 3;
2359 setup_nodes_header();
2360 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002361 case 'q':
2362 goto out;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002363 case '?':
2364 ui_browser__help_window(&browser->b, help);
2365 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002366 default:
2367 break;
2368 }
2369 }
2370
2371out:
2372 free(cl_browser);
2373 return 0;
2374}
2375
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002376static int perf_c2c_browser__title(struct hist_browser *browser,
2377 char *bf, size_t size)
2378{
2379 scnprintf(bf, size,
Jiri Olsa55b95772016-05-29 10:21:45 +02002380 "Shared Data Cache Line Table "
2381 "(%lu entries, sorted on %s HITMs)",
2382 browser->nr_non_filtered_entries,
Jiri Olsad940bac2016-11-21 22:33:30 +01002383 display_str[c2c.display]);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002384 return 0;
2385}
2386
2387static struct hist_browser*
2388perf_c2c_browser__new(struct hists *hists)
2389{
2390 struct hist_browser *browser = hist_browser__new(hists);
2391
2392 if (browser) {
2393 browser->title = perf_c2c_browser__title;
2394 browser->c2c_filter = true;
2395 }
2396
2397 return browser;
2398}
2399
2400static int perf_c2c__hists_browse(struct hists *hists)
2401{
2402 struct hist_browser *browser;
2403 int key = -1;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002404 const char help[] =
2405 " d Display cacheline details \n"
Kim Phillips239fb4f2017-11-14 15:04:47 -06002406 " ENTER Toggle callchains (if present) \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002407 " q Quit \n";
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002408
2409 browser = perf_c2c_browser__new(hists);
2410 if (browser == NULL)
2411 return -1;
2412
2413 /* reset abort key so that it can get Ctrl-C as a key */
2414 SLang_reset_tty();
2415 SLang_init_tty(0, 0, 0);
2416
2417 c2c_browser__update_nr_entries(browser);
2418
2419 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002420 key = hist_browser__run(browser, "? - help", true);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002421
2422 switch (key) {
2423 case 'q':
2424 goto out;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002425 case 'd':
2426 perf_c2c__browse_cacheline(browser->he_selection);
2427 break;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002428 case '?':
2429 ui_browser__help_window(&browser->b, help);
2430 break;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002431 default:
2432 break;
2433 }
2434 }
2435
2436out:
2437 hist_browser__delete(browser);
2438 return 0;
2439}
2440
Jiri Olsa2709b972016-08-27 11:40:23 +02002441static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002442{
Namhyung Kim1936fea2017-03-08 00:08:33 +09002443 if (use_browser == 0)
Jiri Olsa2709b972016-08-27 11:40:23 +02002444 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002445 else
2446 perf_c2c__hists_browse(&c2c.hists.hists);
2447}
2448#else
Jiri Olsa2709b972016-08-27 11:40:23 +02002449static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002450{
2451 use_browser = 0;
Jiri Olsa2709b972016-08-27 11:40:23 +02002452 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002453}
2454#endif /* HAVE_SLANG_SUPPORT */
2455
2456static void ui_quirks(void)
2457{
2458 if (!c2c.use_stdio) {
2459 dim_offset.width = 5;
2460 dim_offset.header = header_offset_tui;
2461 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002462
2463 dim_percent_hitm.header = percent_hitm_header[c2c.display];
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002464}
2465
Jiri Olsadd805762016-05-11 18:23:48 +02002466#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2467
2468const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2469 CALLCHAIN_REPORT_HELP
2470 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2471
2472static int
2473parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2474{
2475 struct callchain_param *callchain = opt->value;
2476
2477 callchain->enabled = !unset;
2478 /*
2479 * --no-call-graph
2480 */
2481 if (unset) {
2482 symbol_conf.use_callchain = false;
2483 callchain->mode = CHAIN_NONE;
2484 return 0;
2485 }
2486
2487 return parse_callchain_report_opt(arg);
2488}
2489
2490static int setup_callchain(struct perf_evlist *evlist)
2491{
2492 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2493 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2494
2495 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002496 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsadd805762016-05-11 18:23:48 +02002497 mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002498 dwarf_callchain_users = true;
2499 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsadd805762016-05-11 18:23:48 +02002500 mode = CALLCHAIN_LBR;
2501 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2502 mode = CALLCHAIN_FP;
2503
2504 if (!callchain_param.enabled &&
2505 callchain_param.mode != CHAIN_NONE &&
2506 mode != CALLCHAIN_NONE) {
2507 symbol_conf.use_callchain = true;
2508 if (callchain_register_param(&callchain_param) < 0) {
2509 ui__error("Can't register callchain params.\n");
2510 return -EINVAL;
2511 }
2512 }
2513
2514 callchain_param.record_mode = mode;
2515 callchain_param.min_percent = 0;
2516 return 0;
2517}
2518
Jiri Olsa55b95772016-05-29 10:21:45 +02002519static int setup_display(const char *str)
2520{
Jiri Olsad940bac2016-11-21 22:33:30 +01002521 const char *display = str ?: "tot";
Jiri Olsa55b95772016-05-29 10:21:45 +02002522
Jiri Olsad940bac2016-11-21 22:33:30 +01002523 if (!strcmp(display, "tot"))
2524 c2c.display = DISPLAY_TOT;
2525 else if (!strcmp(display, "rmt"))
Jiri Olsa55b95772016-05-29 10:21:45 +02002526 c2c.display = DISPLAY_RMT;
2527 else if (!strcmp(display, "lcl"))
2528 c2c.display = DISPLAY_LCL;
2529 else {
2530 pr_err("failed: unknown display type: %s\n", str);
2531 return -1;
2532 }
2533
2534 return 0;
2535}
2536
Jiri Olsafc9c6302016-05-24 14:14:38 +02002537#define for_each_token(__tok, __buf, __sep, __tmp) \
2538 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2539 __tok = strtok_r(NULL, __sep, &__tmp))
2540
Jiri Olsa18f278d2016-10-11 13:39:47 +02002541static int build_cl_output(char *cl_sort, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002542{
2543 char *tok, *tmp, *buf = strdup(cl_sort);
2544 bool add_pid = false;
2545 bool add_tid = false;
2546 bool add_iaddr = false;
2547 bool add_sym = false;
2548 bool add_dso = false;
2549 bool add_src = false;
2550
2551 if (!buf)
2552 return -ENOMEM;
2553
2554 for_each_token(tok, buf, ",", tmp) {
2555 if (!strcmp(tok, "tid")) {
2556 add_tid = true;
2557 } else if (!strcmp(tok, "pid")) {
2558 add_pid = true;
2559 } else if (!strcmp(tok, "iaddr")) {
2560 add_iaddr = true;
2561 add_sym = true;
2562 add_dso = true;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002563 add_src = no_source ? false : true;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002564 } else if (!strcmp(tok, "dso")) {
2565 add_dso = true;
2566 } else if (strcmp(tok, "offset")) {
2567 pr_err("unrecognized sort token: %s\n", tok);
2568 return -EINVAL;
2569 }
2570 }
2571
2572 if (asprintf(&c2c.cl_output,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002573 "%s%s%s%s%s%s%s%s%s%s",
2574 c2c.use_stdio ? "cl_num_empty," : "",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002575 "percent_rmt_hitm,"
2576 "percent_lcl_hitm,"
2577 "percent_stores_l1hit,"
2578 "percent_stores_l1miss,"
Jiri Olsa7f834c22018-03-09 11:14:40 +01002579 "offset,offset_node,",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002580 add_pid ? "pid," : "",
2581 add_tid ? "tid," : "",
2582 add_iaddr ? "iaddr," : "",
2583 "mean_rmt,"
2584 "mean_lcl,"
2585 "mean_load,"
Jiri Olsa8763e6a2017-01-20 10:20:31 +01002586 "tot_recs,"
Jiri Olsafc9c6302016-05-24 14:14:38 +02002587 "cpucnt,",
2588 add_sym ? "symbol," : "",
2589 add_dso ? "dso," : "",
2590 add_src ? "cl_srcline," : "",
2591 "node") < 0)
2592 return -ENOMEM;
2593
2594 c2c.show_src = add_src;
2595
2596 free(buf);
2597 return 0;
2598}
2599
Jiri Olsa18f278d2016-10-11 13:39:47 +02002600static int setup_coalesce(const char *coalesce, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002601{
2602 const char *c = coalesce ?: coalesce_default;
2603
2604 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2605 return -ENOMEM;
2606
Jiri Olsa18f278d2016-10-11 13:39:47 +02002607 if (build_cl_output(c2c.cl_sort, no_source))
Jiri Olsafc9c6302016-05-24 14:14:38 +02002608 return -1;
2609
2610 if (asprintf(&c2c.cl_resort, "offset,%s",
Jiri Olsad940bac2016-11-21 22:33:30 +01002611 c2c.display == DISPLAY_TOT ?
2612 "tot_hitm" :
Jiri Olsafc9c6302016-05-24 14:14:38 +02002613 c2c.display == DISPLAY_RMT ?
2614 "rmt_hitm,lcl_hitm" :
2615 "lcl_hitm,rmt_hitm") < 0)
2616 return -ENOMEM;
2617
2618 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2619 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2620 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2621 return 0;
2622}
2623
Jiri Olsa903a6f12016-09-22 17:36:40 +02002624static int perf_c2c__report(int argc, const char **argv)
2625{
2626 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +02002627 struct ui_progress prog;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002628 struct perf_data data = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002629 .mode = PERF_DATA_MODE_READ,
2630 };
Jiri Olsadd805762016-05-11 18:23:48 +02002631 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
Jiri Olsa55b95772016-05-29 10:21:45 +02002632 const char *display = NULL;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002633 const char *coalesce = NULL;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002634 bool no_source = false;
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002635 const struct option options[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002636 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2637 "file", "vmlinux pathname"),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002638 OPT_STRING('i', "input", &input_name, "file",
2639 "the input file to process"),
Jiri Olsa1e181b92016-06-03 15:40:28 +02002640 OPT_INCR('N', "node-info", &c2c.node_info,
2641 "show extra node info in report (repeat for more info)"),
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002642#ifdef HAVE_SLANG_SUPPORT
2643 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2644#endif
Jiri Olsa74c63a22016-05-02 20:01:59 +02002645 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
Namhyung Kimf75d2892017-03-08 00:08:32 +09002646 "Display only statistic tables (implies --stdio)"),
Jiri Olsa590b6a32016-07-10 16:25:15 +02002647 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2648 "Display full length of symbols"),
Jiri Olsa18f278d2016-10-11 13:39:47 +02002649 OPT_BOOLEAN(0, "no-source", &no_source,
2650 "Do not display Source Line column"),
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02002651 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2652 "Show all captured HITM lines."),
Jiri Olsadd805762016-05-11 18:23:48 +02002653 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2654 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2655 callchain_help, &parse_callchain_opt,
2656 callchain_default_opt),
Jiri Olsad940bac2016-11-21 22:33:30 +01002657 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
Jiri Olsafc9c6302016-05-24 14:14:38 +02002658 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2659 "coalesce fields: pid,tid,iaddr,dso"),
Jiri Olsab7ac4f92016-11-21 22:33:28 +01002660 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002661 OPT_PARENT(c2c_options),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002662 OPT_END()
2663 };
2664 int err = 0;
2665
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002666 argc = parse_options(argc, argv, options, report_c2c_usage,
Jiri Olsa903a6f12016-09-22 17:36:40 +02002667 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +02002668 if (argc)
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002669 usage_with_options(report_c2c_usage, options);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002670
Jiri Olsa74c63a22016-05-02 20:01:59 +02002671 if (c2c.stats_only)
2672 c2c.use_stdio = true;
2673
Jiri Olsa78b27542016-09-22 17:36:44 +02002674 if (!input_name || !strlen(input_name))
2675 input_name = "perf.data";
2676
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002677 data.file.path = input_name;
2678 data.force = symbol_conf.force;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002679
Jiri Olsa55b95772016-05-29 10:21:45 +02002680 err = setup_display(display);
2681 if (err)
2682 goto out;
2683
Jiri Olsa18f278d2016-10-11 13:39:47 +02002684 err = setup_coalesce(coalesce, no_source);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002685 if (err) {
2686 pr_debug("Failed to initialize hists\n");
2687 goto out;
2688 }
2689
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02002690 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
Jiri Olsac75540e2016-09-22 17:36:41 +02002691 if (err) {
2692 pr_debug("Failed to initialize hists\n");
2693 goto out;
2694 }
2695
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002696 session = perf_session__new(&data, 0, &c2c.tool);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002697 if (session == NULL) {
2698 pr_debug("No memory for session\n");
2699 goto out;
2700 }
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002701
Jiri Olsa1e181b92016-06-03 15:40:28 +02002702 err = setup_nodes(session);
2703 if (err) {
2704 pr_err("Failed setup nodes\n");
2705 goto out;
2706 }
Jiri Olsa903a6f12016-09-22 17:36:40 +02002707
Jiri Olsa7f834c22018-03-09 11:14:40 +01002708 err = mem2node__init(&c2c.mem2node, &session->header.env);
Jiri Olsadd805762016-05-11 18:23:48 +02002709 if (err)
2710 goto out_session;
2711
Jiri Olsa7f834c22018-03-09 11:14:40 +01002712 err = setup_callchain(session->evlist);
2713 if (err)
2714 goto out_mem2node;
2715
Jiri Olsa903a6f12016-09-22 17:36:40 +02002716 if (symbol__init(&session->header.env) < 0)
Jiri Olsa7f834c22018-03-09 11:14:40 +01002717 goto out_mem2node;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002718
2719 /* No pipe support at the moment. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002720 if (perf_data__is_pipe(session->data)) {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002721 pr_debug("No pipe support at the moment.\n");
Jiri Olsa7f834c22018-03-09 11:14:40 +01002722 goto out_mem2node;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002723 }
2724
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002725 if (c2c.use_stdio)
2726 use_browser = 0;
2727 else
2728 use_browser = 1;
2729
2730 setup_browser(false);
2731
Jiri Olsa78b27542016-09-22 17:36:44 +02002732 err = perf_session__process_events(session);
2733 if (err) {
2734 pr_err("failed to process sample\n");
Jiri Olsa7f834c22018-03-09 11:14:40 +01002735 goto out_mem2node;
Jiri Olsa78b27542016-09-22 17:36:44 +02002736 }
2737
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002738 c2c_hists__reinit(&c2c.hists,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002739 "cl_idx,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002740 "dcacheline,"
Jiri Olsa7f834c22018-03-09 11:14:40 +01002741 "dcacheline_node,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002742 "tot_recs,"
2743 "percent_hitm,"
2744 "tot_hitm,lcl_hitm,rmt_hitm,"
2745 "stores,stores_l1hit,stores_l1miss,"
2746 "dram_lcl,dram_rmt,"
2747 "ld_llcmiss,"
2748 "tot_loads,"
2749 "ld_fbhit,ld_l1hit,ld_l2hit,"
2750 "ld_lclhit,ld_rmthit",
Jiri Olsad940bac2016-11-21 22:33:30 +01002751 c2c.display == DISPLAY_TOT ? "tot_hitm" :
Jiri Olsa55b95772016-05-29 10:21:45 +02002752 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002753 );
2754
Jiri Olsa78b27542016-09-22 17:36:44 +02002755 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2756
2757 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002758 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2759 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +02002760
2761 ui_progress__finish();
2762
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002763 ui_quirks();
2764
Jiri Olsa2709b972016-08-27 11:40:23 +02002765 perf_c2c_display(session);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002766
Jiri Olsa7f834c22018-03-09 11:14:40 +01002767out_mem2node:
2768 mem2node__exit(&c2c.mem2node);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002769out_session:
2770 perf_session__delete(session);
2771out:
2772 return err;
2773}
2774
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002775static int parse_record_events(const struct option *opt,
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002776 const char *str, int unset __maybe_unused)
2777{
2778 bool *event_set = (bool *) opt->value;
2779
2780 *event_set = true;
2781 return perf_mem_events__parse(str);
2782}
2783
2784
2785static const char * const __usage_record[] = {
2786 "perf c2c record [<options>] [<command>]",
2787 "perf c2c record [<options>] -- <command> [<options>]",
2788 NULL
2789};
2790
2791static const char * const *record_mem_usage = __usage_record;
2792
2793static int perf_c2c__record(int argc, const char **argv)
2794{
2795 int rec_argc, i = 0, j;
2796 const char **rec_argv;
2797 int ret;
2798 bool all_user = false, all_kernel = false;
2799 bool event_set = false;
2800 struct option options[] = {
2801 OPT_CALLBACK('e', "event", &event_set, "event",
2802 "event selector. Use 'perf mem record -e list' to list available events",
2803 parse_record_events),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002804 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2805 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2806 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002807 OPT_PARENT(c2c_options),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002808 OPT_END()
2809 };
2810
2811 if (perf_mem_events__init()) {
2812 pr_err("failed: memory events not supported\n");
2813 return -1;
2814 }
2815
2816 argc = parse_options(argc, argv, options, record_mem_usage,
2817 PARSE_OPT_KEEP_UNKNOWN);
2818
Jiri Olsa8fab7842018-03-09 11:14:37 +01002819 rec_argc = argc + 11; /* max number of arguments */
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002820 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2821 if (!rec_argv)
2822 return -1;
2823
2824 rec_argv[i++] = "record";
2825
2826 if (!event_set) {
2827 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2828 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2829 }
2830
2831 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2832 rec_argv[i++] = "-W";
2833
2834 rec_argv[i++] = "-d";
Jiri Olsa8fab7842018-03-09 11:14:37 +01002835 rec_argv[i++] = "--phys-data";
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002836 rec_argv[i++] = "--sample-cpu";
2837
2838 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2839 if (!perf_mem_events[j].record)
2840 continue;
2841
2842 if (!perf_mem_events[j].supported) {
2843 pr_err("failed: event '%s' not supported\n",
2844 perf_mem_events[j].name);
Martin Kepplingerc896f852017-09-13 21:14:19 +02002845 free(rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002846 return -1;
2847 }
2848
2849 rec_argv[i++] = "-e";
2850 rec_argv[i++] = perf_mem_events__name(j);
2851 };
2852
2853 if (all_user)
2854 rec_argv[i++] = "--all-user";
2855
2856 if (all_kernel)
2857 rec_argv[i++] = "--all-kernel";
2858
2859 for (j = 0; j < argc; j++, i++)
2860 rec_argv[i] = argv[j];
2861
2862 if (verbose > 0) {
2863 pr_debug("calling: ");
2864
2865 j = 0;
2866
2867 while (rec_argv[j]) {
2868 pr_debug("%s ", rec_argv[j]);
2869 j++;
2870 }
2871 pr_debug("\n");
2872 }
2873
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002874 ret = cmd_record(i, rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002875 free(rec_argv);
2876 return ret;
2877}
2878
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002879int cmd_c2c(int argc, const char **argv)
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002880{
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002881 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2882 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002883
2884 if (!argc)
2885 usage_with_options(c2c_usage, c2c_options);
2886
2887 if (!strncmp(argv[0], "rec", 3)) {
2888 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002889 } else if (!strncmp(argv[0], "rep", 3)) {
2890 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002891 } else {
2892 usage_with_options(c2c_usage, c2c_options);
2893 }
2894
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002895 return 0;
2896}