blob: 43ce55550c9d14d3e774a5b0d59b139e20426f2c [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 Olsa903a6f12016-09-22 17:36:40 +020035
Jiri Olsac75540e2016-09-22 17:36:41 +020036struct c2c_hists {
37 struct hists hists;
38 struct perf_hpp_list list;
Jiri Olsab2252ae2016-09-22 17:36:46 +020039 struct c2c_stats stats;
Jiri Olsac75540e2016-09-22 17:36:41 +020040};
41
Jiri Olsa92062d52016-06-05 13:40:53 +020042struct compute_stats {
43 struct stats lcl_hitm;
44 struct stats rmt_hitm;
45 struct stats load;
46};
47
Jiri Olsa78b27542016-09-22 17:36:44 +020048struct c2c_hist_entry {
49 struct c2c_hists *hists;
Jiri Olsab2252ae2016-09-22 17:36:46 +020050 struct c2c_stats stats;
Jiri Olsa1e181b92016-06-03 15:40:28 +020051 unsigned long *cpuset;
52 struct c2c_stats *node_stats;
Jiri Olsabb342dae2016-07-06 15:40:09 +020053 unsigned int cacheline_idx;
Jiri Olsa92062d52016-06-05 13:40:53 +020054
55 struct compute_stats cstats;
56
Jiri Olsa78b27542016-09-22 17:36:44 +020057 /*
58 * must be at the end,
59 * because of its callchain dynamic entry
60 */
61 struct hist_entry he;
62};
63
Jiri Olsa190bacc2017-01-20 10:20:32 +010064static char const *coalesce_default = "pid,iaddr";
Jiri Olsafc9c6302016-05-24 14:14:38 +020065
Jiri Olsa903a6f12016-09-22 17:36:40 +020066struct perf_c2c {
Jiri Olsac75540e2016-09-22 17:36:41 +020067 struct perf_tool tool;
68 struct c2c_hists hists;
Jiri Olsa1e181b92016-06-03 15:40:28 +020069
70 unsigned long **nodes;
71 int nodes_cnt;
72 int cpus_cnt;
73 int *cpu2node;
74 int node_info;
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +020075
76 bool show_src;
Jiri Olsaaf09b2d2016-10-11 13:52:05 +020077 bool show_all;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +010078 bool use_stdio;
Jiri Olsa74c63a22016-05-02 20:01:59 +020079 bool stats_only;
Jiri Olsa590b6a32016-07-10 16:25:15 +020080 bool symbol_full;
Jiri Olsa7ef2efa2016-07-01 11:12:11 +020081
82 /* HITM shared clines stats */
83 struct c2c_stats hitm_stats;
84 int shared_clines;
Jiri Olsa55b95772016-05-29 10:21:45 +020085
86 int display;
Jiri Olsafc9c6302016-05-24 14:14:38 +020087
88 const char *coalesce;
89 char *cl_sort;
90 char *cl_resort;
91 char *cl_output;
Jiri Olsa55b95772016-05-29 10:21:45 +020092};
93
94enum {
95 DISPLAY_LCL,
96 DISPLAY_RMT,
Jiri Olsad940bac2016-11-21 22:33:30 +010097 DISPLAY_TOT,
98 DISPLAY_MAX,
99};
100
101static const char *display_str[DISPLAY_MAX] = {
102 [DISPLAY_LCL] = "Local",
103 [DISPLAY_RMT] = "Remote",
104 [DISPLAY_TOT] = "Total",
Jiri Olsa903a6f12016-09-22 17:36:40 +0200105};
106
Jiri Olsa3a5bfab2016-11-21 22:33:31 +0100107static const struct option c2c_options[] = {
108 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
109 OPT_END()
110};
111
Jiri Olsa903a6f12016-09-22 17:36:40 +0200112static struct perf_c2c c2c;
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200113
Jiri Olsa78b27542016-09-22 17:36:44 +0200114static void *c2c_he_zalloc(size_t size)
115{
116 struct c2c_hist_entry *c2c_he;
117
118 c2c_he = zalloc(size + sizeof(*c2c_he));
119 if (!c2c_he)
120 return NULL;
121
Jiri Olsa1e181b92016-06-03 15:40:28 +0200122 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
123 if (!c2c_he->cpuset)
124 return NULL;
125
126 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
127 if (!c2c_he->node_stats)
128 return NULL;
129
Jiri Olsa92062d52016-06-05 13:40:53 +0200130 init_stats(&c2c_he->cstats.lcl_hitm);
131 init_stats(&c2c_he->cstats.rmt_hitm);
132 init_stats(&c2c_he->cstats.load);
133
Jiri Olsa78b27542016-09-22 17:36:44 +0200134 return &c2c_he->he;
135}
136
137static void c2c_he_free(void *he)
138{
139 struct c2c_hist_entry *c2c_he;
140
141 c2c_he = container_of(he, struct c2c_hist_entry, he);
142 if (c2c_he->hists) {
143 hists__delete_entries(&c2c_he->hists->hists);
144 free(c2c_he->hists);
145 }
146
Jiri Olsa1e181b92016-06-03 15:40:28 +0200147 free(c2c_he->cpuset);
148 free(c2c_he->node_stats);
Jiri Olsa78b27542016-09-22 17:36:44 +0200149 free(c2c_he);
150}
151
152static struct hist_entry_ops c2c_entry_ops = {
153 .new = c2c_he_zalloc,
154 .free = c2c_he_free,
155};
156
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200157static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200158 const char *sort,
159 int nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200160
Jiri Olsab2252ae2016-09-22 17:36:46 +0200161static struct c2c_hists*
162he__get_c2c_hists(struct hist_entry *he,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200163 const char *sort,
164 int nr_header_lines)
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200165{
166 struct c2c_hist_entry *c2c_he;
167 struct c2c_hists *hists;
168 int ret;
169
170 c2c_he = container_of(he, struct c2c_hist_entry, he);
171 if (c2c_he->hists)
Jiri Olsab2252ae2016-09-22 17:36:46 +0200172 return c2c_he->hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200173
174 hists = c2c_he->hists = zalloc(sizeof(*hists));
175 if (!hists)
176 return NULL;
177
Jiri Olsa1d62fcd2016-05-24 10:12:31 +0200178 ret = c2c_hists__init(hists, sort, nr_header_lines);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200179 if (ret) {
180 free(hists);
181 return NULL;
182 }
183
Jiri Olsab2252ae2016-09-22 17:36:46 +0200184 return hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200185}
186
Jiri Olsa1e181b92016-06-03 15:40:28 +0200187static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
188 struct perf_sample *sample)
189{
190 if (WARN_ONCE(sample->cpu == (unsigned int) -1,
191 "WARNING: no sample cpu value"))
192 return;
193
194 set_bit(sample->cpu, c2c_he->cpuset);
195}
196
Jiri Olsa92062d52016-06-05 13:40:53 +0200197static void compute_stats(struct c2c_hist_entry *c2c_he,
198 struct c2c_stats *stats,
199 u64 weight)
200{
201 struct compute_stats *cstats = &c2c_he->cstats;
202
203 if (stats->rmt_hitm)
204 update_stats(&cstats->rmt_hitm, weight);
205 else if (stats->lcl_hitm)
206 update_stats(&cstats->lcl_hitm, weight);
207 else if (stats->load)
208 update_stats(&cstats->load, weight);
209}
210
Jiri Olsa78b27542016-09-22 17:36:44 +0200211static int process_sample_event(struct perf_tool *tool __maybe_unused,
212 union perf_event *event,
213 struct perf_sample *sample,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300214 struct perf_evsel *evsel,
Jiri Olsa78b27542016-09-22 17:36:44 +0200215 struct machine *machine)
216{
Jiri Olsab2252ae2016-09-22 17:36:46 +0200217 struct c2c_hists *c2c_hists = &c2c.hists;
218 struct c2c_hist_entry *c2c_he;
219 struct c2c_stats stats = { .nr_entries = 0, };
Jiri Olsa78b27542016-09-22 17:36:44 +0200220 struct hist_entry *he;
221 struct addr_location al;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200222 struct mem_info *mi, *mi_dup;
Jiri Olsa78b27542016-09-22 17:36:44 +0200223 int ret;
224
225 if (machine__resolve(machine, &al, sample) < 0) {
226 pr_debug("problem processing %d event, skipping it.\n",
227 event->header.type);
228 return -1;
229 }
230
Jiri Olsadd805762016-05-11 18:23:48 +0200231 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
232 evsel, &al, sysctl_perf_event_max_stack);
233 if (ret)
234 goto out;
235
Jiri Olsa78b27542016-09-22 17:36:44 +0200236 mi = sample__resolve_mem(sample, &al);
237 if (mi == NULL)
238 return -ENOMEM;
239
Jiri Olsa5cedb412018-03-07 16:50:07 +0100240 /*
241 * The mi object is released in hists__add_entry_ops,
242 * if it gets sorted out into existing data, so we need
243 * to take the copy now.
244 */
245 mi_dup = mem_info__get(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200246
Jiri Olsab2252ae2016-09-22 17:36:46 +0200247 c2c_decode_stats(&stats, mi);
248
249 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsa78b27542016-09-22 17:36:44 +0200250 &al, NULL, NULL, mi,
251 sample, true);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200252 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100253 goto free_mi;
Jiri Olsa78b27542016-09-22 17:36:44 +0200254
Jiri Olsab2252ae2016-09-22 17:36:46 +0200255 c2c_he = container_of(he, struct c2c_hist_entry, he);
256 c2c_add_stats(&c2c_he->stats, &stats);
257 c2c_add_stats(&c2c_hists->stats, &stats);
258
Jiri Olsa1e181b92016-06-03 15:40:28 +0200259 c2c_he__set_cpu(c2c_he, sample);
260
Jiri Olsab2252ae2016-09-22 17:36:46 +0200261 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsa78b27542016-09-22 17:36:44 +0200262 ret = hist_entry__append_callchain(he, sample);
263
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200264 if (!ret) {
Jiri Olsa1e181b92016-06-03 15:40:28 +0200265 /*
266 * There's already been warning about missing
267 * sample's cpu value. Let's account all to
268 * node 0 in this case, without any further
269 * warning.
270 *
271 * Doing node stats only for single callchain data.
272 */
273 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
274 int node = c2c.cpu2node[cpu];
275
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200276 mi = mi_dup;
277
Jiri Olsafc9c6302016-05-24 14:14:38 +0200278 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200279 if (!c2c_hists)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100280 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200281
Jiri Olsab2252ae2016-09-22 17:36:46 +0200282 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200283 &al, NULL, NULL, mi,
284 sample, true);
285 if (he == NULL)
Jiri Olsa5cedb412018-03-07 16:50:07 +0100286 goto free_mi;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200287
Jiri Olsab2252ae2016-09-22 17:36:46 +0200288 c2c_he = container_of(he, struct c2c_hist_entry, he);
289 c2c_add_stats(&c2c_he->stats, &stats);
290 c2c_add_stats(&c2c_hists->stats, &stats);
Jiri Olsa1e181b92016-06-03 15:40:28 +0200291 c2c_add_stats(&c2c_he->node_stats[node], &stats);
292
Jiri Olsa92062d52016-06-05 13:40:53 +0200293 compute_stats(c2c_he, &stats, sample->weight);
294
Jiri Olsa1e181b92016-06-03 15:40:28 +0200295 c2c_he__set_cpu(c2c_he, sample);
Jiri Olsab2252ae2016-09-22 17:36:46 +0200296
297 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200298 ret = hist_entry__append_callchain(he, sample);
299 }
300
301out:
Jiri Olsa78b27542016-09-22 17:36:44 +0200302 addr_location__put(&al);
303 return ret;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200304
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200305free_mi:
Jiri Olsa5cedb412018-03-07 16:50:07 +0100306 mem_info__put(mi_dup);
307 mem_info__put(mi);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200308 ret = -ENOMEM;
309 goto out;
Jiri Olsa78b27542016-09-22 17:36:44 +0200310}
311
312static struct perf_c2c c2c = {
313 .tool = {
314 .sample = process_sample_event,
315 .mmap = perf_event__process_mmap,
316 .mmap2 = perf_event__process_mmap2,
317 .comm = perf_event__process_comm,
318 .exit = perf_event__process_exit,
319 .fork = perf_event__process_fork,
320 .lost = perf_event__process_lost,
321 .ordered_events = true,
322 .ordering_requires_timestamps = true,
323 },
324};
325
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200326static const char * const c2c_usage[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +0200327 "perf c2c {record|report}",
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200328 NULL
329};
330
Jiri Olsa903a6f12016-09-22 17:36:40 +0200331static const char * const __usage_report[] = {
332 "perf c2c report",
333 NULL
334};
335
336static const char * const *report_c2c_usage = __usage_report;
337
Jiri Olsac75540e2016-09-22 17:36:41 +0200338#define C2C_HEADER_MAX 2
339
340struct c2c_header {
341 struct {
342 const char *text;
343 int span;
344 } line[C2C_HEADER_MAX];
345};
346
347struct c2c_dimension {
348 struct c2c_header header;
349 const char *name;
350 int width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200351 struct sort_entry *se;
Jiri Olsac75540e2016-09-22 17:36:41 +0200352
353 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
354 struct hist_entry *, struct hist_entry *);
355 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
356 struct hist_entry *he);
357 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
358 struct hist_entry *he);
359};
360
361struct c2c_fmt {
362 struct perf_hpp_fmt fmt;
363 struct c2c_dimension *dim;
364};
365
Jiri Olsa590b6a32016-07-10 16:25:15 +0200366#define SYMBOL_WIDTH 30
367
368static struct c2c_dimension dim_symbol;
369static struct c2c_dimension dim_srcline;
370
371static int symbol_width(struct hists *hists, struct sort_entry *se)
372{
373 int width = hists__col_len(hists, se->se_width_idx);
374
375 if (!c2c.symbol_full)
376 width = MIN(width, SYMBOL_WIDTH);
377
378 return width;
379}
380
Jiri Olsac75540e2016-09-22 17:36:41 +0200381static int c2c_width(struct perf_hpp_fmt *fmt,
382 struct perf_hpp *hpp __maybe_unused,
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -0300383 struct hists *hists)
Jiri Olsac75540e2016-09-22 17:36:41 +0200384{
385 struct c2c_fmt *c2c_fmt;
Jiri Olsac75540e2016-09-22 17:36:41 +0200386 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +0200387
388 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
389 dim = c2c_fmt->dim;
390
Jiri Olsa590b6a32016-07-10 16:25:15 +0200391 if (dim == &dim_symbol || dim == &dim_srcline)
392 return symbol_width(hists, dim->se);
393
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200394 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
395 c2c_fmt->dim->width;
396}
397
398static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
399 struct hists *hists, int line, int *span)
400{
401 struct perf_hpp_list *hpp_list = hists->hpp_list;
402 struct c2c_fmt *c2c_fmt;
403 struct c2c_dimension *dim;
404 const char *text = NULL;
405 int width = c2c_width(fmt, hpp, hists);
406
407 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
408 dim = c2c_fmt->dim;
409
410 if (dim->se) {
411 text = dim->header.line[line].text;
412 /* Use the last line from sort_entry if not defined. */
413 if (!text && (line == hpp_list->nr_header_lines - 1))
414 text = dim->se->se_header;
415 } else {
416 text = dim->header.line[line].text;
417
418 if (*span) {
419 (*span)--;
420 return 0;
421 } else {
422 *span = dim->header.line[line].span;
423 }
424 }
425
Jiri Olsac75540e2016-09-22 17:36:41 +0200426 if (text == NULL)
427 text = "";
428
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200429 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
Jiri Olsac75540e2016-09-22 17:36:41 +0200430}
431
Jiri Olsacbb88502016-09-22 17:36:48 +0200432#define HEX_STR(__s, __v) \
433({ \
434 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
435 __s; \
436})
437
438static int64_t
439dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
440 struct hist_entry *left, struct hist_entry *right)
441{
442 return sort__dcacheline_cmp(left, right);
443}
444
445static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
446 struct hist_entry *he)
447{
448 uint64_t addr = 0;
449 int width = c2c_width(fmt, hpp, he->hists);
450 char buf[20];
451
452 if (he->mem_info)
453 addr = cl_address(he->mem_info->daddr.addr);
454
455 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
456}
457
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200458static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
459 struct hist_entry *he)
460{
461 uint64_t addr = 0;
462 int width = c2c_width(fmt, hpp, he->hists);
463 char buf[20];
464
465 if (he->mem_info)
466 addr = cl_offset(he->mem_info->daddr.al_addr);
467
468 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
469}
470
471static int64_t
472offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
473 struct hist_entry *left, struct hist_entry *right)
474{
475 uint64_t l = 0, r = 0;
476
477 if (left->mem_info)
478 l = cl_offset(left->mem_info->daddr.addr);
479 if (right->mem_info)
480 r = cl_offset(right->mem_info->daddr.addr);
481
482 return (int64_t)(r - l);
483}
484
Jiri Olsa43575a92016-05-03 21:48:56 +0200485static int
486iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
487 struct hist_entry *he)
488{
489 uint64_t addr = 0;
490 int width = c2c_width(fmt, hpp, he->hists);
491 char buf[20];
492
493 if (he->mem_info)
494 addr = he->mem_info->iaddr.addr;
495
496 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
497}
498
499static int64_t
500iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
501 struct hist_entry *left, struct hist_entry *right)
502{
503 return sort__iaddr_cmp(left, right);
504}
505
Jiri Olsa97cb4862016-05-23 16:20:14 +0200506static int
507tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
508 struct hist_entry *he)
509{
510 struct c2c_hist_entry *c2c_he;
511 int width = c2c_width(fmt, hpp, he->hists);
512 unsigned int tot_hitm;
513
514 c2c_he = container_of(he, struct c2c_hist_entry, he);
515 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
516
517 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
518}
519
520static int64_t
521tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
522 struct hist_entry *left, struct hist_entry *right)
523{
524 struct c2c_hist_entry *c2c_left;
525 struct c2c_hist_entry *c2c_right;
526 unsigned int tot_hitm_left;
527 unsigned int tot_hitm_right;
528
529 c2c_left = container_of(left, struct c2c_hist_entry, he);
530 c2c_right = container_of(right, struct c2c_hist_entry, he);
531
532 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
533 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
534
535 return tot_hitm_left - tot_hitm_right;
536}
537
538#define STAT_FN_ENTRY(__f) \
539static int \
540__f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
541 struct hist_entry *he) \
542{ \
543 struct c2c_hist_entry *c2c_he; \
544 int width = c2c_width(fmt, hpp, he->hists); \
545 \
546 c2c_he = container_of(he, struct c2c_hist_entry, he); \
547 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
548 c2c_he->stats.__f); \
549}
550
551#define STAT_FN_CMP(__f) \
552static int64_t \
553__f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
554 struct hist_entry *left, struct hist_entry *right) \
555{ \
556 struct c2c_hist_entry *c2c_left, *c2c_right; \
557 \
558 c2c_left = container_of(left, struct c2c_hist_entry, he); \
559 c2c_right = container_of(right, struct c2c_hist_entry, he); \
560 return c2c_left->stats.__f - c2c_right->stats.__f; \
561}
562
563#define STAT_FN(__f) \
564 STAT_FN_ENTRY(__f) \
565 STAT_FN_CMP(__f)
566
567STAT_FN(rmt_hitm)
568STAT_FN(lcl_hitm)
Jiri Olsa0f188962016-05-04 10:10:11 +0200569STAT_FN(store)
570STAT_FN(st_l1hit)
571STAT_FN(st_l1miss)
Jiri Olsa1295f682016-05-04 10:18:24 +0200572STAT_FN(ld_fbhit)
573STAT_FN(ld_l1hit)
574STAT_FN(ld_l2hit)
Jiri Olsa4d089102016-05-04 10:27:51 +0200575STAT_FN(ld_llchit)
576STAT_FN(rmt_hit)
Jiri Olsa97cb4862016-05-23 16:20:14 +0200577
Jiri Olsa04402d22016-05-19 10:10:51 +0200578static uint64_t llc_miss(struct c2c_stats *stats)
579{
580 uint64_t llcmiss;
581
582 llcmiss = stats->lcl_dram +
583 stats->rmt_dram +
584 stats->rmt_hitm +
585 stats->rmt_hit;
586
587 return llcmiss;
588}
589
590static int
591ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
592 struct hist_entry *he)
593{
594 struct c2c_hist_entry *c2c_he;
595 int width = c2c_width(fmt, hpp, he->hists);
596
597 c2c_he = container_of(he, struct c2c_hist_entry, he);
598
599 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
600 llc_miss(&c2c_he->stats));
601}
602
603static int64_t
604ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
605 struct hist_entry *left, struct hist_entry *right)
606{
607 struct c2c_hist_entry *c2c_left;
608 struct c2c_hist_entry *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
613 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
614}
615
Jiri Olsa01b84d72016-05-04 10:35:29 +0200616static uint64_t total_records(struct c2c_stats *stats)
617{
618 uint64_t lclmiss, ldcnt, total;
619
620 lclmiss = stats->lcl_dram +
621 stats->rmt_dram +
622 stats->rmt_hitm +
623 stats->rmt_hit;
624
625 ldcnt = lclmiss +
626 stats->ld_fbhit +
627 stats->ld_l1hit +
628 stats->ld_l2hit +
629 stats->ld_llchit +
630 stats->lcl_hitm;
631
632 total = ldcnt +
633 stats->st_l1hit +
634 stats->st_l1miss;
635
636 return total;
637}
638
639static int
640tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
641 struct hist_entry *he)
642{
643 struct c2c_hist_entry *c2c_he;
644 int width = c2c_width(fmt, hpp, he->hists);
645 uint64_t tot_recs;
646
647 c2c_he = container_of(he, struct c2c_hist_entry, he);
648 tot_recs = total_records(&c2c_he->stats);
649
650 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
651}
652
653static int64_t
654tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
655 struct hist_entry *left, struct hist_entry *right)
656{
657 struct c2c_hist_entry *c2c_left;
658 struct c2c_hist_entry *c2c_right;
659 uint64_t tot_recs_left;
660 uint64_t tot_recs_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 tot_recs_left = total_records(&c2c_left->stats);
666 tot_recs_right = total_records(&c2c_right->stats);
667
668 return tot_recs_left - tot_recs_right;
669}
670
Jiri Olsa55177c42016-05-19 09:52:37 +0200671static uint64_t total_loads(struct c2c_stats *stats)
672{
673 uint64_t lclmiss, ldcnt;
674
675 lclmiss = stats->lcl_dram +
676 stats->rmt_dram +
677 stats->rmt_hitm +
678 stats->rmt_hit;
679
680 ldcnt = lclmiss +
681 stats->ld_fbhit +
682 stats->ld_l1hit +
683 stats->ld_l2hit +
684 stats->ld_llchit +
685 stats->lcl_hitm;
686
687 return ldcnt;
688}
689
690static int
691tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
692 struct hist_entry *he)
693{
694 struct c2c_hist_entry *c2c_he;
695 int width = c2c_width(fmt, hpp, he->hists);
696 uint64_t tot_recs;
697
698 c2c_he = container_of(he, struct c2c_hist_entry, he);
699 tot_recs = total_loads(&c2c_he->stats);
700
701 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
702}
703
704static int64_t
705tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
706 struct hist_entry *left, struct hist_entry *right)
707{
708 struct c2c_hist_entry *c2c_left;
709 struct c2c_hist_entry *c2c_right;
710 uint64_t tot_recs_left;
711 uint64_t tot_recs_right;
712
713 c2c_left = container_of(left, struct c2c_hist_entry, he);
714 c2c_right = container_of(right, struct c2c_hist_entry, he);
715
716 tot_recs_left = total_loads(&c2c_left->stats);
717 tot_recs_right = total_loads(&c2c_right->stats);
718
719 return tot_recs_left - tot_recs_right;
720}
721
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200722typedef double (get_percent_cb)(struct c2c_hist_entry *);
723
724static int
725percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
726 struct hist_entry *he, get_percent_cb get_percent)
727{
728 struct c2c_hist_entry *c2c_he;
729 int width = c2c_width(fmt, hpp, he->hists);
730 double per;
731
732 c2c_he = container_of(he, struct c2c_hist_entry, he);
733 per = get_percent(c2c_he);
734
Jiri Olsa5a1a99c2016-01-06 16:59:02 +0100735#ifdef HAVE_SLANG_SUPPORT
736 if (use_browser)
737 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
738#endif
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200739 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
740}
741
742static double percent_hitm(struct c2c_hist_entry *c2c_he)
743{
744 struct c2c_hists *hists;
745 struct c2c_stats *stats;
746 struct c2c_stats *total;
Jiri Olsa55b95772016-05-29 10:21:45 +0200747 int tot = 0, st = 0;
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200748 double p;
749
750 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
751 stats = &c2c_he->stats;
752 total = &hists->stats;
753
Jiri Olsa55b95772016-05-29 10:21:45 +0200754 switch (c2c.display) {
755 case DISPLAY_RMT:
756 st = stats->rmt_hitm;
757 tot = total->rmt_hitm;
758 break;
759 case DISPLAY_LCL:
760 st = stats->lcl_hitm;
761 tot = total->lcl_hitm;
Jiri Olsad940bac2016-11-21 22:33:30 +0100762 break;
763 case DISPLAY_TOT:
764 st = stats->tot_hitm;
765 tot = total->tot_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +0200766 default:
767 break;
768 }
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200769
770 p = tot ? (double) st / tot : 0;
771
772 return 100 * p;
773}
774
775#define PERC_STR(__s, __v) \
776({ \
777 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
778 __s; \
779})
780
781static int
782percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
783 struct hist_entry *he)
784{
785 struct c2c_hist_entry *c2c_he;
786 int width = c2c_width(fmt, hpp, he->hists);
787 char buf[10];
788 double per;
789
790 c2c_he = container_of(he, struct c2c_hist_entry, he);
791 per = percent_hitm(c2c_he);
792 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
793}
794
795static int
796percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
797 struct hist_entry *he)
798{
799 return percent_color(fmt, hpp, he, percent_hitm);
800}
801
802static int64_t
803percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
804 struct hist_entry *left, struct hist_entry *right)
805{
806 struct c2c_hist_entry *c2c_left;
807 struct c2c_hist_entry *c2c_right;
808 double per_left;
809 double per_right;
810
811 c2c_left = container_of(left, struct c2c_hist_entry, he);
812 c2c_right = container_of(right, struct c2c_hist_entry, he);
813
814 per_left = percent_hitm(c2c_left);
815 per_right = percent_hitm(c2c_right);
816
817 return per_left - per_right;
818}
819
Jiri Olsa9cb35002016-05-04 12:16:50 +0200820static struct c2c_stats *he_stats(struct hist_entry *he)
821{
822 struct c2c_hist_entry *c2c_he;
823
824 c2c_he = container_of(he, struct c2c_hist_entry, he);
825 return &c2c_he->stats;
826}
827
828static struct c2c_stats *total_stats(struct hist_entry *he)
829{
830 struct c2c_hists *hists;
831
832 hists = container_of(he->hists, struct c2c_hists, hists);
833 return &hists->stats;
834}
835
836static double percent(int st, int tot)
837{
838 return tot ? 100. * (double) st / (double) tot : 0;
839}
840
841#define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
842
843#define PERCENT_FN(__f) \
844static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
845{ \
846 struct c2c_hists *hists; \
847 \
848 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
849 return percent(c2c_he->stats.__f, hists->stats.__f); \
850}
851
852PERCENT_FN(rmt_hitm)
853PERCENT_FN(lcl_hitm)
854PERCENT_FN(st_l1hit)
855PERCENT_FN(st_l1miss)
856
857static int
858percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
859 struct hist_entry *he)
860{
861 int width = c2c_width(fmt, hpp, he->hists);
862 double per = PERCENT(he, rmt_hitm);
863 char buf[10];
864
865 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
866}
867
868static int
869percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
870 struct hist_entry *he)
871{
872 return percent_color(fmt, hpp, he, percent_rmt_hitm);
873}
874
875static int64_t
876percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
877 struct hist_entry *left, struct hist_entry *right)
878{
879 double per_left;
880 double per_right;
881
882 per_left = PERCENT(left, lcl_hitm);
883 per_right = PERCENT(right, lcl_hitm);
884
885 return per_left - per_right;
886}
887
888static int
889percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
890 struct hist_entry *he)
891{
892 int width = c2c_width(fmt, hpp, he->hists);
893 double per = PERCENT(he, lcl_hitm);
894 char buf[10];
895
896 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
897}
898
899static int
900percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
901 struct hist_entry *he)
902{
903 return percent_color(fmt, hpp, he, percent_lcl_hitm);
904}
905
906static int64_t
907percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
908 struct hist_entry *left, struct hist_entry *right)
909{
910 double per_left;
911 double per_right;
912
913 per_left = PERCENT(left, lcl_hitm);
914 per_right = PERCENT(right, lcl_hitm);
915
916 return per_left - per_right;
917}
918
919static int
920percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
921 struct hist_entry *he)
922{
923 int width = c2c_width(fmt, hpp, he->hists);
924 double per = PERCENT(he, st_l1hit);
925 char buf[10];
926
927 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
928}
929
930static int
931percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
932 struct hist_entry *he)
933{
934 return percent_color(fmt, hpp, he, percent_st_l1hit);
935}
936
937static int64_t
938percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
939 struct hist_entry *left, struct hist_entry *right)
940{
941 double per_left;
942 double per_right;
943
944 per_left = PERCENT(left, st_l1hit);
945 per_right = PERCENT(right, st_l1hit);
946
947 return per_left - per_right;
948}
949
950static int
951percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
952 struct hist_entry *he)
953{
954 int width = c2c_width(fmt, hpp, he->hists);
955 double per = PERCENT(he, st_l1miss);
956 char buf[10];
957
958 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
959}
960
961static int
962percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
963 struct hist_entry *he)
964{
965 return percent_color(fmt, hpp, he, percent_st_l1miss);
966}
967
968static int64_t
969percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
970 struct hist_entry *left, struct hist_entry *right)
971{
972 double per_left;
973 double per_right;
974
975 per_left = PERCENT(left, st_l1miss);
976 per_right = PERCENT(right, st_l1miss);
977
978 return per_left - per_right;
979}
980
Jiri Olsa6c70f542016-05-28 12:30:13 +0200981STAT_FN(lcl_dram)
982STAT_FN(rmt_dram)
983
Jiri Olsa36d3deb2016-05-24 13:09:47 +0200984static int
985pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
986 struct hist_entry *he)
987{
988 int width = c2c_width(fmt, hpp, he->hists);
989
990 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
991}
992
993static int64_t
994pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
995 struct hist_entry *left, struct hist_entry *right)
996{
997 return left->thread->pid_ - right->thread->pid_;
998}
999
Jiri Olsa1e181b92016-06-03 15:40:28 +02001000static int64_t
1001empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
1002 struct hist_entry *left __maybe_unused,
1003 struct hist_entry *right __maybe_unused)
1004{
1005 return 0;
1006}
1007
1008static int
1009node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
1010 struct hist_entry *he)
1011{
1012 struct c2c_hist_entry *c2c_he;
1013 bool first = true;
1014 int node;
1015 int ret = 0;
1016
1017 c2c_he = container_of(he, struct c2c_hist_entry, he);
1018
1019 for (node = 0; node < c2c.nodes_cnt; node++) {
1020 DECLARE_BITMAP(set, c2c.cpus_cnt);
1021
1022 bitmap_zero(set, c2c.cpus_cnt);
1023 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
1024
1025 if (!bitmap_weight(set, c2c.cpus_cnt)) {
1026 if (c2c.node_info == 1) {
1027 ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
1028 advance_hpp(hpp, ret);
1029 }
1030 continue;
1031 }
1032
1033 if (!first) {
1034 ret = scnprintf(hpp->buf, hpp->size, " ");
1035 advance_hpp(hpp, ret);
1036 }
1037
1038 switch (c2c.node_info) {
1039 case 0:
1040 ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
1041 advance_hpp(hpp, ret);
1042 break;
1043 case 1:
1044 {
1045 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt);
1046 struct c2c_stats *stats = &c2c_he->node_stats[node];
1047
1048 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
1049 advance_hpp(hpp, ret);
1050
Jiri Olsa55b95772016-05-29 10:21:45 +02001051 #define DISPLAY_HITM(__h) \
1052 if (c2c_he->stats.__h> 0) { \
1053 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
1054 percent(stats->__h, c2c_he->stats.__h));\
1055 } else { \
1056 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
Jiri Olsa1e181b92016-06-03 15:40:28 +02001057 }
1058
Jiri Olsa55b95772016-05-29 10:21:45 +02001059 switch (c2c.display) {
1060 case DISPLAY_RMT:
1061 DISPLAY_HITM(rmt_hitm);
1062 break;
1063 case DISPLAY_LCL:
1064 DISPLAY_HITM(lcl_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001065 break;
1066 case DISPLAY_TOT:
1067 DISPLAY_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001068 default:
1069 break;
1070 }
1071
1072 #undef DISPLAY_HITM
1073
Jiri Olsa1e181b92016-06-03 15:40:28 +02001074 advance_hpp(hpp, ret);
1075
1076 if (c2c_he->stats.store > 0) {
1077 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
1078 percent(stats->store, c2c_he->stats.store));
1079 } else {
1080 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
1081 }
1082
1083 advance_hpp(hpp, ret);
1084 break;
1085 }
1086 case 2:
1087 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
1088 advance_hpp(hpp, ret);
1089
1090 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
1091 advance_hpp(hpp, ret);
1092
1093 ret = scnprintf(hpp->buf, hpp->size, "}");
1094 advance_hpp(hpp, ret);
1095 break;
1096 default:
1097 break;
1098 }
1099
1100 first = false;
1101 }
1102
1103 return 0;
1104}
1105
Jiri Olsa92062d52016-06-05 13:40:53 +02001106static int
1107mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1108 struct hist_entry *he, double mean)
1109{
1110 int width = c2c_width(fmt, hpp, he->hists);
1111 char buf[10];
1112
1113 scnprintf(buf, 10, "%6.0f", mean);
1114 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1115}
1116
1117#define MEAN_ENTRY(__func, __val) \
1118static int \
1119__func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
1120{ \
1121 struct c2c_hist_entry *c2c_he; \
1122 c2c_he = container_of(he, struct c2c_hist_entry, he); \
1123 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
1124}
1125
1126MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
1127MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
1128MEAN_ENTRY(mean_load_entry, load);
1129
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001130static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001131cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001132 struct hist_entry *he)
1133{
1134 struct c2c_hist_entry *c2c_he;
1135 int width = c2c_width(fmt, hpp, he->hists);
1136 char buf[10];
1137
1138 c2c_he = container_of(he, struct c2c_hist_entry, he);
1139
1140 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
1141 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1142}
1143
Jiri Olsabb342dae2016-07-06 15:40:09 +02001144static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001145cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001146 struct hist_entry *he)
1147{
1148 struct c2c_hist_entry *c2c_he;
1149 int width = c2c_width(fmt, hpp, he->hists);
1150 char buf[10];
1151
1152 c2c_he = container_of(he, struct c2c_hist_entry, he);
1153
1154 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
1155 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1156}
1157
1158static int
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03001159cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001160 struct hist_entry *he)
1161{
1162 int width = c2c_width(fmt, hpp, he->hists);
1163
1164 return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
1165}
1166
Jiri Olsa600a8cf2016-09-22 17:36:47 +02001167#define HEADER_LOW(__h) \
1168 { \
1169 .line[1] = { \
1170 .text = __h, \
1171 }, \
1172 }
1173
1174#define HEADER_BOTH(__h0, __h1) \
1175 { \
1176 .line[0] = { \
1177 .text = __h0, \
1178 }, \
1179 .line[1] = { \
1180 .text = __h1, \
1181 }, \
1182 }
1183
1184#define HEADER_SPAN(__h0, __h1, __s) \
1185 { \
1186 .line[0] = { \
1187 .text = __h0, \
1188 .span = __s, \
1189 }, \
1190 .line[1] = { \
1191 .text = __h1, \
1192 }, \
1193 }
1194
1195#define HEADER_SPAN_LOW(__h) \
1196 { \
1197 .line[1] = { \
1198 .text = __h, \
1199 }, \
1200 }
1201
Jiri Olsacbb88502016-09-22 17:36:48 +02001202static struct c2c_dimension dim_dcacheline = {
1203 .header = HEADER_LOW("Cacheline"),
1204 .name = "dcacheline",
1205 .cmp = dcacheline_cmp,
1206 .entry = dcacheline_entry,
1207 .width = 18,
1208};
1209
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01001210static struct c2c_header header_offset_tui = HEADER_LOW("Off");
1211
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001212static struct c2c_dimension dim_offset = {
1213 .header = HEADER_BOTH("Data address", "Offset"),
1214 .name = "offset",
1215 .cmp = offset_cmp,
1216 .entry = offset_entry,
1217 .width = 18,
1218};
1219
Jiri Olsa43575a92016-05-03 21:48:56 +02001220static struct c2c_dimension dim_iaddr = {
1221 .header = HEADER_LOW("Code address"),
1222 .name = "iaddr",
1223 .cmp = iaddr_cmp,
1224 .entry = iaddr_entry,
1225 .width = 18,
1226};
1227
Jiri Olsa97cb4862016-05-23 16:20:14 +02001228static struct c2c_dimension dim_tot_hitm = {
1229 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
1230 .name = "tot_hitm",
1231 .cmp = tot_hitm_cmp,
1232 .entry = tot_hitm_entry,
1233 .width = 7,
1234};
1235
1236static struct c2c_dimension dim_lcl_hitm = {
1237 .header = HEADER_SPAN_LOW("Lcl"),
1238 .name = "lcl_hitm",
1239 .cmp = lcl_hitm_cmp,
1240 .entry = lcl_hitm_entry,
1241 .width = 7,
1242};
1243
1244static struct c2c_dimension dim_rmt_hitm = {
1245 .header = HEADER_SPAN_LOW("Rmt"),
1246 .name = "rmt_hitm",
1247 .cmp = rmt_hitm_cmp,
1248 .entry = rmt_hitm_entry,
1249 .width = 7,
1250};
1251
1252static struct c2c_dimension dim_cl_rmt_hitm = {
1253 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1254 .name = "cl_rmt_hitm",
1255 .cmp = rmt_hitm_cmp,
1256 .entry = rmt_hitm_entry,
1257 .width = 7,
1258};
1259
1260static struct c2c_dimension dim_cl_lcl_hitm = {
1261 .header = HEADER_SPAN_LOW("Lcl"),
1262 .name = "cl_lcl_hitm",
1263 .cmp = lcl_hitm_cmp,
1264 .entry = lcl_hitm_entry,
1265 .width = 7,
1266};
1267
Jiri Olsa0f188962016-05-04 10:10:11 +02001268static struct c2c_dimension dim_stores = {
1269 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
1270 .name = "stores",
1271 .cmp = store_cmp,
1272 .entry = store_entry,
1273 .width = 7,
1274};
1275
1276static struct c2c_dimension dim_stores_l1hit = {
1277 .header = HEADER_SPAN_LOW("L1Hit"),
1278 .name = "stores_l1hit",
1279 .cmp = st_l1hit_cmp,
1280 .entry = st_l1hit_entry,
1281 .width = 7,
1282};
1283
1284static struct c2c_dimension dim_stores_l1miss = {
1285 .header = HEADER_SPAN_LOW("L1Miss"),
1286 .name = "stores_l1miss",
1287 .cmp = st_l1miss_cmp,
1288 .entry = st_l1miss_entry,
1289 .width = 7,
1290};
1291
1292static struct c2c_dimension dim_cl_stores_l1hit = {
1293 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1294 .name = "cl_stores_l1hit",
1295 .cmp = st_l1hit_cmp,
1296 .entry = st_l1hit_entry,
1297 .width = 7,
1298};
1299
1300static struct c2c_dimension dim_cl_stores_l1miss = {
1301 .header = HEADER_SPAN_LOW("L1 Miss"),
1302 .name = "cl_stores_l1miss",
1303 .cmp = st_l1miss_cmp,
1304 .entry = st_l1miss_entry,
1305 .width = 7,
1306};
1307
Jiri Olsa1295f682016-05-04 10:18:24 +02001308static struct c2c_dimension dim_ld_fbhit = {
1309 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
1310 .name = "ld_fbhit",
1311 .cmp = ld_fbhit_cmp,
1312 .entry = ld_fbhit_entry,
1313 .width = 7,
1314};
1315
1316static struct c2c_dimension dim_ld_l1hit = {
1317 .header = HEADER_SPAN_LOW("L1"),
1318 .name = "ld_l1hit",
1319 .cmp = ld_l1hit_cmp,
1320 .entry = ld_l1hit_entry,
1321 .width = 7,
1322};
1323
1324static struct c2c_dimension dim_ld_l2hit = {
1325 .header = HEADER_SPAN_LOW("L2"),
1326 .name = "ld_l2hit",
1327 .cmp = ld_l2hit_cmp,
1328 .entry = ld_l2hit_entry,
1329 .width = 7,
1330};
1331
Jiri Olsa4d089102016-05-04 10:27:51 +02001332static struct c2c_dimension dim_ld_llchit = {
1333 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
1334 .name = "ld_lclhit",
1335 .cmp = ld_llchit_cmp,
1336 .entry = ld_llchit_entry,
1337 .width = 8,
1338};
1339
1340static struct c2c_dimension dim_ld_rmthit = {
1341 .header = HEADER_SPAN_LOW("Rmt"),
1342 .name = "ld_rmthit",
1343 .cmp = rmt_hit_cmp,
1344 .entry = rmt_hit_entry,
1345 .width = 8,
1346};
1347
Jiri Olsa04402d22016-05-19 10:10:51 +02001348static struct c2c_dimension dim_ld_llcmiss = {
1349 .header = HEADER_BOTH("LLC", "Ld Miss"),
1350 .name = "ld_llcmiss",
1351 .cmp = ld_llcmiss_cmp,
1352 .entry = ld_llcmiss_entry,
1353 .width = 7,
1354};
1355
Jiri Olsa01b84d72016-05-04 10:35:29 +02001356static struct c2c_dimension dim_tot_recs = {
1357 .header = HEADER_BOTH("Total", "records"),
1358 .name = "tot_recs",
1359 .cmp = tot_recs_cmp,
1360 .entry = tot_recs_entry,
1361 .width = 7,
1362};
1363
Jiri Olsa55177c42016-05-19 09:52:37 +02001364static struct c2c_dimension dim_tot_loads = {
1365 .header = HEADER_BOTH("Total", "Loads"),
1366 .name = "tot_loads",
1367 .cmp = tot_loads_cmp,
1368 .entry = tot_loads_entry,
1369 .width = 7,
1370};
1371
Jiri Olsa55b95772016-05-29 10:21:45 +02001372static struct c2c_header percent_hitm_header[] = {
1373 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
1374 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
Jiri Olsad940bac2016-11-21 22:33:30 +01001375 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
Jiri Olsa55b95772016-05-29 10:21:45 +02001376};
1377
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001378static struct c2c_dimension dim_percent_hitm = {
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001379 .name = "percent_hitm",
1380 .cmp = percent_hitm_cmp,
1381 .entry = percent_hitm_entry,
1382 .color = percent_hitm_color,
1383 .width = 7,
1384};
1385
Jiri Olsa9cb35002016-05-04 12:16:50 +02001386static struct c2c_dimension dim_percent_rmt_hitm = {
1387 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1388 .name = "percent_rmt_hitm",
1389 .cmp = percent_rmt_hitm_cmp,
1390 .entry = percent_rmt_hitm_entry,
1391 .color = percent_rmt_hitm_color,
1392 .width = 7,
1393};
1394
1395static struct c2c_dimension dim_percent_lcl_hitm = {
1396 .header = HEADER_SPAN_LOW("Lcl"),
1397 .name = "percent_lcl_hitm",
1398 .cmp = percent_lcl_hitm_cmp,
1399 .entry = percent_lcl_hitm_entry,
1400 .color = percent_lcl_hitm_color,
1401 .width = 7,
1402};
1403
1404static struct c2c_dimension dim_percent_stores_l1hit = {
1405 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1406 .name = "percent_stores_l1hit",
1407 .cmp = percent_stores_l1hit_cmp,
1408 .entry = percent_stores_l1hit_entry,
1409 .color = percent_stores_l1hit_color,
1410 .width = 7,
1411};
1412
1413static struct c2c_dimension dim_percent_stores_l1miss = {
1414 .header = HEADER_SPAN_LOW("L1 Miss"),
1415 .name = "percent_stores_l1miss",
1416 .cmp = percent_stores_l1miss_cmp,
1417 .entry = percent_stores_l1miss_entry,
1418 .color = percent_stores_l1miss_color,
1419 .width = 7,
1420};
1421
Jiri Olsa6c70f542016-05-28 12:30:13 +02001422static struct c2c_dimension dim_dram_lcl = {
1423 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1424 .name = "dram_lcl",
1425 .cmp = lcl_dram_cmp,
1426 .entry = lcl_dram_entry,
1427 .width = 8,
1428};
1429
1430static struct c2c_dimension dim_dram_rmt = {
1431 .header = HEADER_SPAN_LOW("Rmt"),
1432 .name = "dram_rmt",
1433 .cmp = rmt_dram_cmp,
1434 .entry = rmt_dram_entry,
1435 .width = 8,
1436};
1437
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001438static struct c2c_dimension dim_pid = {
1439 .header = HEADER_LOW("Pid"),
1440 .name = "pid",
1441 .cmp = pid_cmp,
1442 .entry = pid_entry,
1443 .width = 7,
1444};
1445
Jiri Olsae87019c2016-05-25 08:50:10 +02001446static struct c2c_dimension dim_tid = {
1447 .header = HEADER_LOW("Tid"),
1448 .name = "tid",
1449 .se = &sort_thread,
1450};
1451
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001452static struct c2c_dimension dim_symbol = {
1453 .name = "symbol",
1454 .se = &sort_sym,
1455};
1456
1457static struct c2c_dimension dim_dso = {
1458 .header = HEADER_BOTH("Shared", "Object"),
1459 .name = "dso",
1460 .se = &sort_dso,
1461};
1462
Jiri Olsa1e181b92016-06-03 15:40:28 +02001463static struct c2c_header header_node[3] = {
1464 HEADER_LOW("Node"),
1465 HEADER_LOW("Node{cpus %hitms %stores}"),
1466 HEADER_LOW("Node{cpu list}"),
1467};
1468
1469static struct c2c_dimension dim_node = {
1470 .name = "node",
1471 .cmp = empty_cmp,
1472 .entry = node_entry,
1473 .width = 4,
1474};
1475
Jiri Olsa92062d52016-06-05 13:40:53 +02001476static struct c2c_dimension dim_mean_rmt = {
1477 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
1478 .name = "mean_rmt",
1479 .cmp = empty_cmp,
1480 .entry = mean_rmt_entry,
1481 .width = 8,
1482};
1483
1484static struct c2c_dimension dim_mean_lcl = {
1485 .header = HEADER_SPAN_LOW("lcl hitm"),
1486 .name = "mean_lcl",
1487 .cmp = empty_cmp,
1488 .entry = mean_lcl_entry,
1489 .width = 8,
1490};
1491
1492static struct c2c_dimension dim_mean_load = {
1493 .header = HEADER_SPAN_LOW("load"),
1494 .name = "mean_load",
1495 .cmp = empty_cmp,
1496 .entry = mean_load_entry,
1497 .width = 8,
1498};
1499
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001500static struct c2c_dimension dim_cpucnt = {
1501 .header = HEADER_BOTH("cpu", "cnt"),
1502 .name = "cpucnt",
1503 .cmp = empty_cmp,
1504 .entry = cpucnt_entry,
1505 .width = 8,
1506};
1507
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001508static struct c2c_dimension dim_srcline = {
1509 .name = "cl_srcline",
1510 .se = &sort_srcline,
1511};
1512
Jiri Olsabb342dae2016-07-06 15:40:09 +02001513static struct c2c_dimension dim_dcacheline_idx = {
1514 .header = HEADER_LOW("Index"),
1515 .name = "cl_idx",
1516 .cmp = empty_cmp,
1517 .entry = cl_idx_entry,
1518 .width = 5,
1519};
1520
1521static struct c2c_dimension dim_dcacheline_num = {
1522 .header = HEADER_LOW("Num"),
1523 .name = "cl_num",
1524 .cmp = empty_cmp,
1525 .entry = cl_idx_entry,
1526 .width = 5,
1527};
1528
1529static struct c2c_dimension dim_dcacheline_num_empty = {
1530 .header = HEADER_LOW("Num"),
1531 .name = "cl_num_empty",
1532 .cmp = empty_cmp,
1533 .entry = cl_idx_empty_entry,
1534 .width = 5,
1535};
1536
Jiri Olsac75540e2016-09-22 17:36:41 +02001537static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +02001538 &dim_dcacheline,
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001539 &dim_offset,
Jiri Olsa43575a92016-05-03 21:48:56 +02001540 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +02001541 &dim_tot_hitm,
1542 &dim_lcl_hitm,
1543 &dim_rmt_hitm,
1544 &dim_cl_lcl_hitm,
1545 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +02001546 &dim_stores,
1547 &dim_stores_l1hit,
1548 &dim_stores_l1miss,
1549 &dim_cl_stores_l1hit,
1550 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +02001551 &dim_ld_fbhit,
1552 &dim_ld_l1hit,
1553 &dim_ld_l2hit,
Jiri Olsa4d089102016-05-04 10:27:51 +02001554 &dim_ld_llchit,
1555 &dim_ld_rmthit,
Jiri Olsa04402d22016-05-19 10:10:51 +02001556 &dim_ld_llcmiss,
Jiri Olsa01b84d72016-05-04 10:35:29 +02001557 &dim_tot_recs,
Jiri Olsa55177c42016-05-19 09:52:37 +02001558 &dim_tot_loads,
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001559 &dim_percent_hitm,
Jiri Olsa9cb35002016-05-04 12:16:50 +02001560 &dim_percent_rmt_hitm,
1561 &dim_percent_lcl_hitm,
1562 &dim_percent_stores_l1hit,
1563 &dim_percent_stores_l1miss,
Jiri Olsa6c70f542016-05-28 12:30:13 +02001564 &dim_dram_lcl,
1565 &dim_dram_rmt,
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001566 &dim_pid,
Jiri Olsae87019c2016-05-25 08:50:10 +02001567 &dim_tid,
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001568 &dim_symbol,
1569 &dim_dso,
Jiri Olsa1e181b92016-06-03 15:40:28 +02001570 &dim_node,
Jiri Olsa92062d52016-06-05 13:40:53 +02001571 &dim_mean_rmt,
1572 &dim_mean_lcl,
1573 &dim_mean_load,
Jiri Olsab6fe2bb2016-06-23 23:05:52 +02001574 &dim_cpucnt,
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001575 &dim_srcline,
Jiri Olsabb342dae2016-07-06 15:40:09 +02001576 &dim_dcacheline_idx,
1577 &dim_dcacheline_num,
1578 &dim_dcacheline_num_empty,
Jiri Olsac75540e2016-09-22 17:36:41 +02001579 NULL,
1580};
1581
1582static void fmt_free(struct perf_hpp_fmt *fmt)
1583{
1584 struct c2c_fmt *c2c_fmt;
1585
1586 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1587 free(c2c_fmt);
1588}
1589
1590static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1591{
1592 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1593 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1594
1595 return c2c_a->dim == c2c_b->dim;
1596}
1597
1598static struct c2c_dimension *get_dimension(const char *name)
1599{
1600 unsigned int i;
1601
1602 for (i = 0; dimensions[i]; i++) {
1603 struct c2c_dimension *dim = dimensions[i];
1604
1605 if (!strcmp(dim->name, name))
1606 return dim;
1607 };
1608
1609 return NULL;
1610}
1611
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001612static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1613 struct hist_entry *he)
1614{
1615 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1616 struct c2c_dimension *dim = c2c_fmt->dim;
1617 size_t len = fmt->user_len;
1618
Jiri Olsa590b6a32016-07-10 16:25:15 +02001619 if (!len) {
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001620 len = hists__col_len(he->hists, dim->se->se_width_idx);
1621
Jiri Olsa590b6a32016-07-10 16:25:15 +02001622 if (dim == &dim_symbol || dim == &dim_srcline)
1623 len = symbol_width(he->hists, dim->se);
1624 }
1625
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001626 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1627}
1628
1629static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1630 struct hist_entry *a, struct hist_entry *b)
1631{
1632 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1633 struct c2c_dimension *dim = c2c_fmt->dim;
1634
1635 return dim->se->se_cmp(a, b);
1636}
1637
1638static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1639 struct hist_entry *a, struct hist_entry *b)
1640{
1641 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1642 struct c2c_dimension *dim = c2c_fmt->dim;
1643 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1644
1645 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1646 return collapse_fn(a, b);
1647}
1648
Jiri Olsac75540e2016-09-22 17:36:41 +02001649static struct c2c_fmt *get_format(const char *name)
1650{
1651 struct c2c_dimension *dim = get_dimension(name);
1652 struct c2c_fmt *c2c_fmt;
1653 struct perf_hpp_fmt *fmt;
1654
1655 if (!dim)
1656 return NULL;
1657
1658 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1659 if (!c2c_fmt)
1660 return NULL;
1661
1662 c2c_fmt->dim = dim;
1663
1664 fmt = &c2c_fmt->fmt;
1665 INIT_LIST_HEAD(&fmt->list);
1666 INIT_LIST_HEAD(&fmt->sort_list);
1667
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001668 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1669 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
Jiri Olsa9cb35002016-05-04 12:16:50 +02001670 fmt->color = dim->se ? NULL : dim->color;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001671 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +02001672 fmt->header = c2c_header;
1673 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001674 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +02001675 fmt->equal = fmt_equal;
1676 fmt->free = fmt_free;
1677
1678 return c2c_fmt;
1679}
1680
1681static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1682{
1683 struct c2c_fmt *c2c_fmt = get_format(name);
1684
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001685 if (!c2c_fmt) {
1686 reset_dimensions();
1687 return output_field_add(hpp_list, name);
1688 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001689
1690 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1691 return 0;
1692}
1693
1694static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1695{
1696 struct c2c_fmt *c2c_fmt = get_format(name);
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001697 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +02001698
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001699 if (!c2c_fmt) {
1700 reset_dimensions();
1701 return sort_dimension__add(hpp_list, name, NULL, 0);
1702 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001703
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001704 dim = c2c_fmt->dim;
1705 if (dim == &dim_dso)
1706 hpp_list->dso = 1;
1707
Jiri Olsac75540e2016-09-22 17:36:41 +02001708 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1709 return 0;
1710}
1711
1712#define PARSE_LIST(_list, _fn) \
1713 do { \
1714 char *tmp, *tok; \
1715 ret = 0; \
1716 \
1717 if (!_list) \
1718 break; \
1719 \
1720 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1721 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1722 ret = _fn(hpp_list, tok); \
1723 if (ret == -EINVAL) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001724 pr_err("Invalid --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001725 break; \
1726 } else if (ret == -ESRCH) { \
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001727 pr_err("Unknown --fields key: `%s'", tok); \
Jiri Olsac75540e2016-09-22 17:36:41 +02001728 break; \
1729 } \
1730 } \
1731 } while (0)
1732
1733static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1734 const char *output_,
1735 const char *sort_)
1736{
1737 char *output = output_ ? strdup(output_) : NULL;
1738 char *sort = sort_ ? strdup(sort_) : NULL;
1739 int ret;
1740
1741 PARSE_LIST(output, c2c_hists__init_output);
1742 PARSE_LIST(sort, c2c_hists__init_sort);
1743
1744 /* copy sort keys to output fields */
1745 perf_hpp__setup_output_field(hpp_list);
1746
1747 /*
1748 * We dont need other sorting keys other than those
1749 * we already specified. It also really slows down
1750 * the processing a lot with big number of output
1751 * fields, so switching this off for c2c.
1752 */
1753
1754#if 0
1755 /* and then copy output fields to sort keys */
1756 perf_hpp__append_sort_keys(&hists->list);
1757#endif
1758
1759 free(output);
1760 free(sort);
1761 return ret;
1762}
1763
1764static int c2c_hists__init(struct c2c_hists *hists,
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001765 const char *sort,
1766 int nr_header_lines)
Jiri Olsac75540e2016-09-22 17:36:41 +02001767{
1768 __hists__init(&hists->hists, &hists->list);
1769
1770 /*
1771 * Initialize only with sort fields, we need to resort
1772 * later anyway, and that's where we add output fields
1773 * as well.
1774 */
1775 perf_hpp_list__init(&hists->list);
1776
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02001777 /* Overload number of header lines.*/
1778 hists->list.nr_header_lines = nr_header_lines;
1779
Jiri Olsac75540e2016-09-22 17:36:41 +02001780 return hpp_list__parse(&hists->list, NULL, sort);
1781}
1782
Jiri Olsac75540e2016-09-22 17:36:41 +02001783static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1784 const char *output,
1785 const char *sort)
1786{
1787 perf_hpp__reset_output_field(&c2c_hists->list);
1788 return hpp_list__parse(&c2c_hists->list, output, sort);
1789}
1790
Jiri Olsa9857b712016-08-17 14:55:23 +02001791#define DISPLAY_LINE_LIMIT 0.0005
1792
1793static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
1794{
1795 struct c2c_hist_entry *c2c_he;
1796 double ld_dist;
1797
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02001798 if (c2c.show_all)
1799 return true;
Jiri Olsa9857b712016-08-17 14:55:23 +02001800
1801 c2c_he = container_of(he, struct c2c_hist_entry, he);
1802
Jiri Olsa55b95772016-05-29 10:21:45 +02001803#define FILTER_HITM(__h) \
1804 if (stats->__h) { \
1805 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
1806 if (ld_dist < DISPLAY_LINE_LIMIT) \
1807 he->filtered = HIST_FILTER__C2C; \
1808 } else { \
1809 he->filtered = HIST_FILTER__C2C; \
Jiri Olsa9857b712016-08-17 14:55:23 +02001810 }
1811
Jiri Olsa55b95772016-05-29 10:21:45 +02001812 switch (c2c.display) {
1813 case DISPLAY_LCL:
1814 FILTER_HITM(lcl_hitm);
1815 break;
1816 case DISPLAY_RMT:
1817 FILTER_HITM(rmt_hitm);
Jiri Olsad940bac2016-11-21 22:33:30 +01001818 break;
1819 case DISPLAY_TOT:
1820 FILTER_HITM(tot_hitm);
Jiri Olsa55b95772016-05-29 10:21:45 +02001821 default:
1822 break;
1823 };
1824
1825#undef FILTER_HITM
1826
Jiri Olsa9857b712016-08-17 14:55:23 +02001827 return he->filtered == 0;
1828}
1829
1830static inline int valid_hitm_or_store(struct hist_entry *he)
1831{
1832 struct c2c_hist_entry *c2c_he;
Jiri Olsa55b95772016-05-29 10:21:45 +02001833 bool has_hitm;
Jiri Olsa9857b712016-08-17 14:55:23 +02001834
1835 c2c_he = container_of(he, struct c2c_hist_entry, he);
Jiri Olsad940bac2016-11-21 22:33:30 +01001836 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
1837 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
1838 c2c_he->stats.rmt_hitm;
Jiri Olsa55b95772016-05-29 10:21:45 +02001839 return has_hitm || c2c_he->stats.store;
Jiri Olsa9857b712016-08-17 14:55:23 +02001840}
1841
Jiri Olsa37731382018-03-09 11:14:38 +01001842static void calc_width(struct c2c_hist_entry *c2c_he)
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001843{
1844 struct c2c_hists *c2c_hists;
1845
Jiri Olsa37731382018-03-09 11:14:38 +01001846 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
1847 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001848}
1849
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001850static int filter_cb(struct hist_entry *he)
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001851{
Jiri Olsa37731382018-03-09 11:14:38 +01001852 struct c2c_hist_entry *c2c_he;
1853
1854 c2c_he = container_of(he, struct c2c_hist_entry, he);
1855
Jiri Olsa89d9ba8f2016-07-10 15:47:40 +02001856 if (c2c.show_src && !he->srcline)
1857 he->srcline = hist_entry__get_srcline(he);
1858
Jiri Olsa37731382018-03-09 11:14:38 +01001859 calc_width(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001860
Jiri Olsa9857b712016-08-17 14:55:23 +02001861 if (!valid_hitm_or_store(he))
1862 he->filtered = HIST_FILTER__C2C;
1863
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001864 return 0;
1865}
1866
1867static int resort_cl_cb(struct hist_entry *he)
1868{
1869 struct c2c_hist_entry *c2c_he;
1870 struct c2c_hists *c2c_hists;
Jiri Olsa9857b712016-08-17 14:55:23 +02001871 bool display = he__display(he, &c2c.hitm_stats);
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001872
1873 c2c_he = container_of(he, struct c2c_hist_entry, he);
1874 c2c_hists = c2c_he->hists;
1875
Jiri Olsa37731382018-03-09 11:14:38 +01001876 calc_width(c2c_he);
Jiri Olsa25aa84e2016-06-07 19:02:43 +02001877
Jiri Olsa9857b712016-08-17 14:55:23 +02001878 if (display && c2c_hists) {
Jiri Olsabb342dae2016-07-06 15:40:09 +02001879 static unsigned int idx;
1880
1881 c2c_he->cacheline_idx = idx++;
1882
Jiri Olsafc9c6302016-05-24 14:14:38 +02001883 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
Jiri Olsa22dd59d2016-05-10 14:08:29 +02001884
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001885 hists__collapse_resort(&c2c_hists->hists, NULL);
1886 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
1887 }
1888
1889 return 0;
1890}
1891
Jiri Olsa1e181b92016-06-03 15:40:28 +02001892static void setup_nodes_header(void)
1893{
1894 dim_node.header = header_node[c2c.node_info];
1895}
1896
1897static int setup_nodes(struct perf_session *session)
1898{
1899 struct numa_node *n;
1900 unsigned long **nodes;
1901 int node, cpu;
1902 int *cpu2node;
1903
1904 if (c2c.node_info > 2)
1905 c2c.node_info = 2;
1906
1907 c2c.nodes_cnt = session->header.env.nr_numa_nodes;
1908 c2c.cpus_cnt = session->header.env.nr_cpus_online;
1909
1910 n = session->header.env.numa_nodes;
1911 if (!n)
1912 return -EINVAL;
1913
1914 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
1915 if (!nodes)
1916 return -ENOMEM;
1917
1918 c2c.nodes = nodes;
1919
1920 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
1921 if (!cpu2node)
1922 return -ENOMEM;
1923
1924 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
1925 cpu2node[cpu] = -1;
1926
1927 c2c.cpu2node = cpu2node;
1928
1929 for (node = 0; node < c2c.nodes_cnt; node++) {
1930 struct cpu_map *map = n[node].map;
1931 unsigned long *set;
1932
1933 set = bitmap_alloc(c2c.cpus_cnt);
1934 if (!set)
1935 return -ENOMEM;
1936
1937 for (cpu = 0; cpu < map->nr; cpu++) {
1938 set_bit(map->map[cpu], set);
1939
1940 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
1941 return -EINVAL;
1942
1943 cpu2node[map->map[cpu]] = node;
1944 }
1945
1946 nodes[node] = set;
1947 }
1948
1949 setup_nodes_header();
1950 return 0;
1951}
1952
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02001953#define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
1954
1955static int resort_hitm_cb(struct hist_entry *he)
1956{
1957 struct c2c_hist_entry *c2c_he;
1958 c2c_he = container_of(he, struct c2c_hist_entry, he);
1959
1960 if (HAS_HITMS(c2c_he)) {
1961 c2c.shared_clines++;
1962 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
1963 }
1964
1965 return 0;
1966}
1967
1968static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
1969{
1970 struct rb_node *next = rb_first(&hists->entries);
1971 int ret = 0;
1972
1973 while (next) {
1974 struct hist_entry *he;
1975
1976 he = rb_entry(next, struct hist_entry, rb_node);
1977 ret = cb(he);
1978 if (ret)
1979 break;
1980 next = rb_next(&he->rb_node);
1981 }
1982
1983 return ret;
1984}
1985
Jiri Olsa74c63a22016-05-02 20:01:59 +02001986static void print_c2c__display_stats(FILE *out)
1987{
1988 int llc_misses;
1989 struct c2c_stats *stats = &c2c.hists.stats;
1990
1991 llc_misses = stats->lcl_dram +
1992 stats->rmt_dram +
1993 stats->rmt_hit +
1994 stats->rmt_hitm;
1995
1996 fprintf(out, "=================================================\n");
1997 fprintf(out, " Trace Event Information \n");
1998 fprintf(out, "=================================================\n");
1999 fprintf(out, " Total records : %10d\n", stats->nr_entries);
2000 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
2001 fprintf(out, " Load Operations : %10d\n", stats->load);
2002 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
2003 fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
2004 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
2005 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
2006 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
2007 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
2008 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
2009 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2010 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
2011 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
2012 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
2013 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
2014 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
2015 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
2016 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
2017 fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
2018 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
2019 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
2020 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
2021 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
2022 fprintf(out, " Store Operations : %10d\n", stats->store);
2023 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
2024 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
2025 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
2026 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
2027 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
2028 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
2029}
2030
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002031static void print_shared_cacheline_info(FILE *out)
2032{
2033 struct c2c_stats *stats = &c2c.hitm_stats;
2034 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
2035
2036 fprintf(out, "=================================================\n");
2037 fprintf(out, " Global Shared Cache Line Event Information \n");
2038 fprintf(out, "=================================================\n");
2039 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
2040 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
2041 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
2042 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
2043 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
2044 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
2045 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
2046 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
2047 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
2048 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
2049}
2050
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002051static void print_cacheline(struct c2c_hists *c2c_hists,
2052 struct hist_entry *he_cl,
2053 struct perf_hpp_list *hpp_list,
2054 FILE *out)
2055{
2056 char bf[1000];
2057 struct perf_hpp hpp = {
2058 .buf = bf,
2059 .size = 1000,
2060 };
2061 static bool once;
2062
2063 if (!once) {
2064 hists__fprintf_headers(&c2c_hists->hists, out);
2065 once = true;
2066 } else {
2067 fprintf(out, "\n");
2068 }
2069
Jiri Olsabb342dae2016-07-06 15:40:09 +02002070 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002071 __hist_entry__snprintf(he_cl, &hpp, hpp_list);
2072 fprintf(out, "%s\n", bf);
Jiri Olsabb342dae2016-07-06 15:40:09 +02002073 fprintf(out, " -------------------------------------------------------------\n");
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002074
2075 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, true);
2076}
2077
2078static void print_pareto(FILE *out)
2079{
2080 struct perf_hpp_list hpp_list;
2081 struct rb_node *nd;
2082 int ret;
2083
2084 perf_hpp_list__init(&hpp_list);
2085 ret = hpp_list__parse(&hpp_list,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002086 "cl_num,"
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002087 "cl_rmt_hitm,"
2088 "cl_lcl_hitm,"
2089 "cl_stores_l1hit,"
2090 "cl_stores_l1miss,"
2091 "dcacheline",
2092 NULL);
2093
2094 if (WARN_ONCE(ret, "failed to setup sort entries\n"))
2095 return;
2096
2097 nd = rb_first(&c2c.hists.hists.entries);
2098
2099 for (; nd; nd = rb_next(nd)) {
2100 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2101 struct c2c_hist_entry *c2c_he;
2102
2103 if (he->filtered)
2104 continue;
2105
2106 c2c_he = container_of(he, struct c2c_hist_entry, he);
2107 print_cacheline(c2c_he->hists, he, &hpp_list, out);
2108 }
2109}
2110
Jiri Olsa2709b972016-08-27 11:40:23 +02002111static void print_c2c_info(FILE *out, struct perf_session *session)
2112{
2113 struct perf_evlist *evlist = session->evlist;
2114 struct perf_evsel *evsel;
2115 bool first = true;
2116
2117 fprintf(out, "=================================================\n");
2118 fprintf(out, " c2c details \n");
2119 fprintf(out, "=================================================\n");
2120
2121 evlist__for_each_entry(evlist, evsel) {
2122 fprintf(out, "%-36s: %s\n", first ? " Events" : "",
2123 perf_evsel__name(evsel));
2124 first = false;
2125 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002126 fprintf(out, " Cachelines sort on : %s HITMs\n",
Jiri Olsad940bac2016-11-21 22:33:30 +01002127 display_str[c2c.display]);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002128 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
Jiri Olsa2709b972016-08-27 11:40:23 +02002129}
2130
2131static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002132{
2133 setup_pager();
2134
Jiri Olsa74c63a22016-05-02 20:01:59 +02002135 print_c2c__display_stats(out);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002136 fprintf(out, "\n");
2137 print_shared_cacheline_info(out);
Jiri Olsa2709b972016-08-27 11:40:23 +02002138 fprintf(out, "\n");
2139 print_c2c_info(out, session);
Jiri Olsa74c63a22016-05-02 20:01:59 +02002140
2141 if (c2c.stats_only)
2142 return;
2143
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002144 fprintf(out, "\n");
2145 fprintf(out, "=================================================\n");
2146 fprintf(out, " Shared Data Cache Line Table \n");
2147 fprintf(out, "=================================================\n");
2148 fprintf(out, "#\n");
2149
2150 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, false);
2151
2152 fprintf(out, "\n");
2153 fprintf(out, "=================================================\n");
2154 fprintf(out, " Shared Cache Line Distribution Pareto \n");
2155 fprintf(out, "=================================================\n");
2156 fprintf(out, "#\n");
2157
2158 print_pareto(out);
2159}
Jiri Olsa1e181b92016-06-03 15:40:28 +02002160
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002161#ifdef HAVE_SLANG_SUPPORT
2162static void c2c_browser__update_nr_entries(struct hist_browser *hb)
2163{
2164 u64 nr_entries = 0;
2165 struct rb_node *nd = rb_first(&hb->hists->entries);
2166
2167 while (nd) {
2168 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
2169
2170 if (!he->filtered)
2171 nr_entries++;
2172
2173 nd = rb_next(nd);
2174 }
2175
2176 hb->nr_non_filtered_entries = nr_entries;
2177}
2178
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002179struct c2c_cacheline_browser {
2180 struct hist_browser hb;
2181 struct hist_entry *he;
2182};
2183
2184static int
2185perf_c2c_cacheline_browser__title(struct hist_browser *browser,
2186 char *bf, size_t size)
2187{
2188 struct c2c_cacheline_browser *cl_browser;
2189 struct hist_entry *he;
2190 uint64_t addr = 0;
2191
2192 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
2193 he = cl_browser->he;
2194
2195 if (he->mem_info)
2196 addr = cl_address(he->mem_info->daddr.addr);
2197
2198 scnprintf(bf, size, "Cacheline 0x%lx", addr);
2199 return 0;
2200}
2201
2202static struct c2c_cacheline_browser*
2203c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
2204{
2205 struct c2c_cacheline_browser *browser;
2206
2207 browser = zalloc(sizeof(*browser));
2208 if (browser) {
2209 hist_browser__init(&browser->hb, hists);
2210 browser->hb.c2c_filter = true;
2211 browser->hb.title = perf_c2c_cacheline_browser__title;
2212 browser->he = he;
2213 }
2214
2215 return browser;
2216}
2217
2218static int perf_c2c__browse_cacheline(struct hist_entry *he)
2219{
2220 struct c2c_hist_entry *c2c_he;
2221 struct c2c_hists *c2c_hists;
2222 struct c2c_cacheline_browser *cl_browser;
2223 struct hist_browser *browser;
2224 int key = -1;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002225 const char help[] =
Kim Phillips239fb4f2017-11-14 15:04:47 -06002226 " ENTER Toggle callchains (if present) \n"
2227 " n Toggle Node details info \n"
2228 " s Toggle full length of symbol and source line columns \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002229 " q Return back to cacheline list \n";
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002230
Jiri Olsa590b6a32016-07-10 16:25:15 +02002231 /* Display compact version first. */
2232 c2c.symbol_full = false;
2233
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002234 c2c_he = container_of(he, struct c2c_hist_entry, he);
2235 c2c_hists = c2c_he->hists;
2236
2237 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
2238 if (cl_browser == NULL)
2239 return -1;
2240
2241 browser = &cl_browser->hb;
2242
2243 /* reset abort key so that it can get Ctrl-C as a key */
2244 SLang_reset_tty();
2245 SLang_init_tty(0, 0, 0);
2246
2247 c2c_browser__update_nr_entries(browser);
2248
2249 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002250 key = hist_browser__run(browser, "? - help", true);
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002251
2252 switch (key) {
Jiri Olsa590b6a32016-07-10 16:25:15 +02002253 case 's':
2254 c2c.symbol_full = !c2c.symbol_full;
2255 break;
Jiri Olsa1a56a422016-07-10 16:30:27 +02002256 case 'n':
2257 c2c.node_info = (c2c.node_info + 1) % 3;
2258 setup_nodes_header();
2259 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002260 case 'q':
2261 goto out;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002262 case '?':
2263 ui_browser__help_window(&browser->b, help);
2264 break;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002265 default:
2266 break;
2267 }
2268 }
2269
2270out:
2271 free(cl_browser);
2272 return 0;
2273}
2274
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002275static int perf_c2c_browser__title(struct hist_browser *browser,
2276 char *bf, size_t size)
2277{
2278 scnprintf(bf, size,
Jiri Olsa55b95772016-05-29 10:21:45 +02002279 "Shared Data Cache Line Table "
2280 "(%lu entries, sorted on %s HITMs)",
2281 browser->nr_non_filtered_entries,
Jiri Olsad940bac2016-11-21 22:33:30 +01002282 display_str[c2c.display]);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002283 return 0;
2284}
2285
2286static struct hist_browser*
2287perf_c2c_browser__new(struct hists *hists)
2288{
2289 struct hist_browser *browser = hist_browser__new(hists);
2290
2291 if (browser) {
2292 browser->title = perf_c2c_browser__title;
2293 browser->c2c_filter = true;
2294 }
2295
2296 return browser;
2297}
2298
2299static int perf_c2c__hists_browse(struct hists *hists)
2300{
2301 struct hist_browser *browser;
2302 int key = -1;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002303 const char help[] =
2304 " d Display cacheline details \n"
Kim Phillips239fb4f2017-11-14 15:04:47 -06002305 " ENTER Toggle callchains (if present) \n"
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002306 " q Quit \n";
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002307
2308 browser = perf_c2c_browser__new(hists);
2309 if (browser == NULL)
2310 return -1;
2311
2312 /* reset abort key so that it can get Ctrl-C as a key */
2313 SLang_reset_tty();
2314 SLang_init_tty(0, 0, 0);
2315
2316 c2c_browser__update_nr_entries(browser);
2317
2318 while (1) {
Kan Liang06cc1a42018-01-18 13:26:29 -08002319 key = hist_browser__run(browser, "? - help", true);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002320
2321 switch (key) {
2322 case 'q':
2323 goto out;
Jiri Olsaf1c5fd42016-05-02 18:30:44 +02002324 case 'd':
2325 perf_c2c__browse_cacheline(browser->he_selection);
2326 break;
Jiri Olsa9a406eb2016-08-17 15:54:58 +02002327 case '?':
2328 ui_browser__help_window(&browser->b, help);
2329 break;
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002330 default:
2331 break;
2332 }
2333 }
2334
2335out:
2336 hist_browser__delete(browser);
2337 return 0;
2338}
2339
Jiri Olsa2709b972016-08-27 11:40:23 +02002340static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002341{
Namhyung Kim1936fea2017-03-08 00:08:33 +09002342 if (use_browser == 0)
Jiri Olsa2709b972016-08-27 11:40:23 +02002343 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002344 else
2345 perf_c2c__hists_browse(&c2c.hists.hists);
2346}
2347#else
Jiri Olsa2709b972016-08-27 11:40:23 +02002348static void perf_c2c_display(struct perf_session *session)
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002349{
2350 use_browser = 0;
Jiri Olsa2709b972016-08-27 11:40:23 +02002351 perf_c2c__hists_fprintf(stdout, session);
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002352}
2353#endif /* HAVE_SLANG_SUPPORT */
2354
2355static void ui_quirks(void)
2356{
2357 if (!c2c.use_stdio) {
2358 dim_offset.width = 5;
2359 dim_offset.header = header_offset_tui;
2360 }
Jiri Olsa55b95772016-05-29 10:21:45 +02002361
2362 dim_percent_hitm.header = percent_hitm_header[c2c.display];
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002363}
2364
Jiri Olsadd805762016-05-11 18:23:48 +02002365#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
2366
2367const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
2368 CALLCHAIN_REPORT_HELP
2369 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
2370
2371static int
2372parse_callchain_opt(const struct option *opt, const char *arg, int unset)
2373{
2374 struct callchain_param *callchain = opt->value;
2375
2376 callchain->enabled = !unset;
2377 /*
2378 * --no-call-graph
2379 */
2380 if (unset) {
2381 symbol_conf.use_callchain = false;
2382 callchain->mode = CHAIN_NONE;
2383 return 0;
2384 }
2385
2386 return parse_callchain_report_opt(arg);
2387}
2388
2389static int setup_callchain(struct perf_evlist *evlist)
2390{
2391 u64 sample_type = perf_evlist__combined_sample_type(evlist);
2392 enum perf_call_graph_mode mode = CALLCHAIN_NONE;
2393
2394 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002395 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsadd805762016-05-11 18:23:48 +02002396 mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002397 dwarf_callchain_users = true;
2398 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsadd805762016-05-11 18:23:48 +02002399 mode = CALLCHAIN_LBR;
2400 else if (sample_type & PERF_SAMPLE_CALLCHAIN)
2401 mode = CALLCHAIN_FP;
2402
2403 if (!callchain_param.enabled &&
2404 callchain_param.mode != CHAIN_NONE &&
2405 mode != CALLCHAIN_NONE) {
2406 symbol_conf.use_callchain = true;
2407 if (callchain_register_param(&callchain_param) < 0) {
2408 ui__error("Can't register callchain params.\n");
2409 return -EINVAL;
2410 }
2411 }
2412
2413 callchain_param.record_mode = mode;
2414 callchain_param.min_percent = 0;
2415 return 0;
2416}
2417
Jiri Olsa55b95772016-05-29 10:21:45 +02002418static int setup_display(const char *str)
2419{
Jiri Olsad940bac2016-11-21 22:33:30 +01002420 const char *display = str ?: "tot";
Jiri Olsa55b95772016-05-29 10:21:45 +02002421
Jiri Olsad940bac2016-11-21 22:33:30 +01002422 if (!strcmp(display, "tot"))
2423 c2c.display = DISPLAY_TOT;
2424 else if (!strcmp(display, "rmt"))
Jiri Olsa55b95772016-05-29 10:21:45 +02002425 c2c.display = DISPLAY_RMT;
2426 else if (!strcmp(display, "lcl"))
2427 c2c.display = DISPLAY_LCL;
2428 else {
2429 pr_err("failed: unknown display type: %s\n", str);
2430 return -1;
2431 }
2432
2433 return 0;
2434}
2435
Jiri Olsafc9c6302016-05-24 14:14:38 +02002436#define for_each_token(__tok, __buf, __sep, __tmp) \
2437 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
2438 __tok = strtok_r(NULL, __sep, &__tmp))
2439
Jiri Olsa18f278d2016-10-11 13:39:47 +02002440static int build_cl_output(char *cl_sort, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002441{
2442 char *tok, *tmp, *buf = strdup(cl_sort);
2443 bool add_pid = false;
2444 bool add_tid = false;
2445 bool add_iaddr = false;
2446 bool add_sym = false;
2447 bool add_dso = false;
2448 bool add_src = false;
2449
2450 if (!buf)
2451 return -ENOMEM;
2452
2453 for_each_token(tok, buf, ",", tmp) {
2454 if (!strcmp(tok, "tid")) {
2455 add_tid = true;
2456 } else if (!strcmp(tok, "pid")) {
2457 add_pid = true;
2458 } else if (!strcmp(tok, "iaddr")) {
2459 add_iaddr = true;
2460 add_sym = true;
2461 add_dso = true;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002462 add_src = no_source ? false : true;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002463 } else if (!strcmp(tok, "dso")) {
2464 add_dso = true;
2465 } else if (strcmp(tok, "offset")) {
2466 pr_err("unrecognized sort token: %s\n", tok);
2467 return -EINVAL;
2468 }
2469 }
2470
2471 if (asprintf(&c2c.cl_output,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002472 "%s%s%s%s%s%s%s%s%s%s",
2473 c2c.use_stdio ? "cl_num_empty," : "",
Jiri Olsafc9c6302016-05-24 14:14:38 +02002474 "percent_rmt_hitm,"
2475 "percent_lcl_hitm,"
2476 "percent_stores_l1hit,"
2477 "percent_stores_l1miss,"
2478 "offset,",
2479 add_pid ? "pid," : "",
2480 add_tid ? "tid," : "",
2481 add_iaddr ? "iaddr," : "",
2482 "mean_rmt,"
2483 "mean_lcl,"
2484 "mean_load,"
Jiri Olsa8763e6a2017-01-20 10:20:31 +01002485 "tot_recs,"
Jiri Olsafc9c6302016-05-24 14:14:38 +02002486 "cpucnt,",
2487 add_sym ? "symbol," : "",
2488 add_dso ? "dso," : "",
2489 add_src ? "cl_srcline," : "",
2490 "node") < 0)
2491 return -ENOMEM;
2492
2493 c2c.show_src = add_src;
2494
2495 free(buf);
2496 return 0;
2497}
2498
Jiri Olsa18f278d2016-10-11 13:39:47 +02002499static int setup_coalesce(const char *coalesce, bool no_source)
Jiri Olsafc9c6302016-05-24 14:14:38 +02002500{
2501 const char *c = coalesce ?: coalesce_default;
2502
2503 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
2504 return -ENOMEM;
2505
Jiri Olsa18f278d2016-10-11 13:39:47 +02002506 if (build_cl_output(c2c.cl_sort, no_source))
Jiri Olsafc9c6302016-05-24 14:14:38 +02002507 return -1;
2508
2509 if (asprintf(&c2c.cl_resort, "offset,%s",
Jiri Olsad940bac2016-11-21 22:33:30 +01002510 c2c.display == DISPLAY_TOT ?
2511 "tot_hitm" :
Jiri Olsafc9c6302016-05-24 14:14:38 +02002512 c2c.display == DISPLAY_RMT ?
2513 "rmt_hitm,lcl_hitm" :
2514 "lcl_hitm,rmt_hitm") < 0)
2515 return -ENOMEM;
2516
2517 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
2518 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
2519 pr_debug("coalesce output fields: %s\n", c2c.cl_output);
2520 return 0;
2521}
2522
Jiri Olsa903a6f12016-09-22 17:36:40 +02002523static int perf_c2c__report(int argc, const char **argv)
2524{
2525 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +02002526 struct ui_progress prog;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002527 struct perf_data data = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002528 .mode = PERF_DATA_MODE_READ,
2529 };
Jiri Olsadd805762016-05-11 18:23:48 +02002530 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
Jiri Olsa55b95772016-05-29 10:21:45 +02002531 const char *display = NULL;
Jiri Olsafc9c6302016-05-24 14:14:38 +02002532 const char *coalesce = NULL;
Jiri Olsa18f278d2016-10-11 13:39:47 +02002533 bool no_source = false;
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002534 const struct option options[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002535 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2536 "file", "vmlinux pathname"),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002537 OPT_STRING('i', "input", &input_name, "file",
2538 "the input file to process"),
Jiri Olsa1e181b92016-06-03 15:40:28 +02002539 OPT_INCR('N', "node-info", &c2c.node_info,
2540 "show extra node info in report (repeat for more info)"),
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002541#ifdef HAVE_SLANG_SUPPORT
2542 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
2543#endif
Jiri Olsa74c63a22016-05-02 20:01:59 +02002544 OPT_BOOLEAN(0, "stats", &c2c.stats_only,
Namhyung Kimf75d2892017-03-08 00:08:32 +09002545 "Display only statistic tables (implies --stdio)"),
Jiri Olsa590b6a32016-07-10 16:25:15 +02002546 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
2547 "Display full length of symbols"),
Jiri Olsa18f278d2016-10-11 13:39:47 +02002548 OPT_BOOLEAN(0, "no-source", &no_source,
2549 "Do not display Source Line column"),
Jiri Olsaaf09b2d2016-10-11 13:52:05 +02002550 OPT_BOOLEAN(0, "show-all", &c2c.show_all,
2551 "Show all captured HITM lines."),
Jiri Olsadd805762016-05-11 18:23:48 +02002552 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
2553 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
2554 callchain_help, &parse_callchain_opt,
2555 callchain_default_opt),
Jiri Olsad940bac2016-11-21 22:33:30 +01002556 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
Jiri Olsafc9c6302016-05-24 14:14:38 +02002557 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
2558 "coalesce fields: pid,tid,iaddr,dso"),
Jiri Olsab7ac4f92016-11-21 22:33:28 +01002559 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002560 OPT_PARENT(c2c_options),
Jiri Olsa903a6f12016-09-22 17:36:40 +02002561 OPT_END()
2562 };
2563 int err = 0;
2564
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002565 argc = parse_options(argc, argv, options, report_c2c_usage,
Jiri Olsa903a6f12016-09-22 17:36:40 +02002566 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +02002567 if (argc)
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002568 usage_with_options(report_c2c_usage, options);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002569
Jiri Olsa74c63a22016-05-02 20:01:59 +02002570 if (c2c.stats_only)
2571 c2c.use_stdio = true;
2572
Jiri Olsa78b27542016-09-22 17:36:44 +02002573 if (!input_name || !strlen(input_name))
2574 input_name = "perf.data";
2575
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002576 data.file.path = input_name;
2577 data.force = symbol_conf.force;
Jiri Olsa903a6f12016-09-22 17:36:40 +02002578
Jiri Olsa55b95772016-05-29 10:21:45 +02002579 err = setup_display(display);
2580 if (err)
2581 goto out;
2582
Jiri Olsa18f278d2016-10-11 13:39:47 +02002583 err = setup_coalesce(coalesce, no_source);
Jiri Olsafc9c6302016-05-24 14:14:38 +02002584 if (err) {
2585 pr_debug("Failed to initialize hists\n");
2586 goto out;
2587 }
2588
Jiri Olsa1d62fcd2016-05-24 10:12:31 +02002589 err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
Jiri Olsac75540e2016-09-22 17:36:41 +02002590 if (err) {
2591 pr_debug("Failed to initialize hists\n");
2592 goto out;
2593 }
2594
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002595 session = perf_session__new(&data, 0, &c2c.tool);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002596 if (session == NULL) {
2597 pr_debug("No memory for session\n");
2598 goto out;
2599 }
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002600
Jiri Olsa1e181b92016-06-03 15:40:28 +02002601 err = setup_nodes(session);
2602 if (err) {
2603 pr_err("Failed setup nodes\n");
2604 goto out;
2605 }
Jiri Olsa903a6f12016-09-22 17:36:40 +02002606
Jiri Olsadd805762016-05-11 18:23:48 +02002607 err = setup_callchain(session->evlist);
2608 if (err)
2609 goto out_session;
2610
Jiri Olsa903a6f12016-09-22 17:36:40 +02002611 if (symbol__init(&session->header.env) < 0)
2612 goto out_session;
2613
2614 /* No pipe support at the moment. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002615 if (perf_data__is_pipe(session->data)) {
Jiri Olsa903a6f12016-09-22 17:36:40 +02002616 pr_debug("No pipe support at the moment.\n");
2617 goto out_session;
2618 }
2619
Jiri Olsae8c5fe12016-11-21 22:33:27 +01002620 if (c2c.use_stdio)
2621 use_browser = 0;
2622 else
2623 use_browser = 1;
2624
2625 setup_browser(false);
2626
Jiri Olsa78b27542016-09-22 17:36:44 +02002627 err = perf_session__process_events(session);
2628 if (err) {
2629 pr_err("failed to process sample\n");
2630 goto out_session;
2631 }
2632
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002633 c2c_hists__reinit(&c2c.hists,
Jiri Olsabb342dae2016-07-06 15:40:09 +02002634 "cl_idx,"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002635 "dcacheline,"
2636 "tot_recs,"
2637 "percent_hitm,"
2638 "tot_hitm,lcl_hitm,rmt_hitm,"
2639 "stores,stores_l1hit,stores_l1miss,"
2640 "dram_lcl,dram_rmt,"
2641 "ld_llcmiss,"
2642 "tot_loads,"
2643 "ld_fbhit,ld_l1hit,ld_l2hit,"
2644 "ld_lclhit,ld_rmthit",
Jiri Olsad940bac2016-11-21 22:33:30 +01002645 c2c.display == DISPLAY_TOT ? "tot_hitm" :
Jiri Olsa55b95772016-05-29 10:21:45 +02002646 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
Jiri Olsa22dd59d2016-05-10 14:08:29 +02002647 );
2648
Jiri Olsa78b27542016-09-22 17:36:44 +02002649 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
2650
2651 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsa7ef2efa2016-07-01 11:12:11 +02002652 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
2653 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +02002654
2655 ui_progress__finish();
2656
Jiri Olsa5a1a99c2016-01-06 16:59:02 +01002657 ui_quirks();
2658
Jiri Olsa2709b972016-08-27 11:40:23 +02002659 perf_c2c_display(session);
Jiri Olsa2d388bd2016-05-03 14:32:56 +02002660
Jiri Olsa903a6f12016-09-22 17:36:40 +02002661out_session:
2662 perf_session__delete(session);
2663out:
2664 return err;
2665}
2666
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002667static int parse_record_events(const struct option *opt,
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002668 const char *str, int unset __maybe_unused)
2669{
2670 bool *event_set = (bool *) opt->value;
2671
2672 *event_set = true;
2673 return perf_mem_events__parse(str);
2674}
2675
2676
2677static const char * const __usage_record[] = {
2678 "perf c2c record [<options>] [<command>]",
2679 "perf c2c record [<options>] -- <command> [<options>]",
2680 NULL
2681};
2682
2683static const char * const *record_mem_usage = __usage_record;
2684
2685static int perf_c2c__record(int argc, const char **argv)
2686{
2687 int rec_argc, i = 0, j;
2688 const char **rec_argv;
2689 int ret;
2690 bool all_user = false, all_kernel = false;
2691 bool event_set = false;
2692 struct option options[] = {
2693 OPT_CALLBACK('e', "event", &event_set, "event",
2694 "event selector. Use 'perf mem record -e list' to list available events",
2695 parse_record_events),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002696 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
2697 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
2698 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
Jiri Olsa3a5bfab2016-11-21 22:33:31 +01002699 OPT_PARENT(c2c_options),
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002700 OPT_END()
2701 };
2702
2703 if (perf_mem_events__init()) {
2704 pr_err("failed: memory events not supported\n");
2705 return -1;
2706 }
2707
2708 argc = parse_options(argc, argv, options, record_mem_usage,
2709 PARSE_OPT_KEEP_UNKNOWN);
2710
Jiri Olsa8fab7842018-03-09 11:14:37 +01002711 rec_argc = argc + 11; /* max number of arguments */
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002712 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2713 if (!rec_argv)
2714 return -1;
2715
2716 rec_argv[i++] = "record";
2717
2718 if (!event_set) {
2719 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
2720 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
2721 }
2722
2723 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
2724 rec_argv[i++] = "-W";
2725
2726 rec_argv[i++] = "-d";
Jiri Olsa8fab7842018-03-09 11:14:37 +01002727 rec_argv[i++] = "--phys-data";
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002728 rec_argv[i++] = "--sample-cpu";
2729
2730 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
2731 if (!perf_mem_events[j].record)
2732 continue;
2733
2734 if (!perf_mem_events[j].supported) {
2735 pr_err("failed: event '%s' not supported\n",
2736 perf_mem_events[j].name);
Martin Kepplingerc896f852017-09-13 21:14:19 +02002737 free(rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002738 return -1;
2739 }
2740
2741 rec_argv[i++] = "-e";
2742 rec_argv[i++] = perf_mem_events__name(j);
2743 };
2744
2745 if (all_user)
2746 rec_argv[i++] = "--all-user";
2747
2748 if (all_kernel)
2749 rec_argv[i++] = "--all-kernel";
2750
2751 for (j = 0; j < argc; j++, i++)
2752 rec_argv[i] = argv[j];
2753
2754 if (verbose > 0) {
2755 pr_debug("calling: ");
2756
2757 j = 0;
2758
2759 while (rec_argv[j]) {
2760 pr_debug("%s ", rec_argv[j]);
2761 j++;
2762 }
2763 pr_debug("\n");
2764 }
2765
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002766 ret = cmd_record(i, rec_argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002767 free(rec_argv);
2768 return ret;
2769}
2770
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002771int cmd_c2c(int argc, const char **argv)
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002772{
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002773 argc = parse_options(argc, argv, c2c_options, c2c_usage,
2774 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002775
2776 if (!argc)
2777 usage_with_options(c2c_usage, c2c_options);
2778
2779 if (!strncmp(argv[0], "rec", 3)) {
2780 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02002781 } else if (!strncmp(argv[0], "rep", 3)) {
2782 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02002783 } else {
2784 usage_with_options(c2c_usage, c2c_options);
2785 }
2786
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02002787 return 0;
2788}