blob: 2b9d24fdcaeeb32d58c2a7e04fc14bf563b10507 [file] [log] [blame]
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02001#include <linux/compiler.h>
2#include <linux/kernel.h>
Jiri Olsacbb88502016-09-22 17:36:48 +02003#include <linux/stringify.h>
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02004#include "util.h"
5#include "debug.h"
6#include "builtin.h"
7#include <subcmd/parse-options.h>
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02008#include "mem-events.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +02009#include "session.h"
10#include "hist.h"
Jiri Olsacbb88502016-09-22 17:36:48 +020011#include "sort.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020012#include "tool.h"
13#include "data.h"
Jiri Olsa8d3f9382016-09-22 17:36:42 +020014#include "sort.h"
Jiri Olsa903a6f12016-09-22 17:36:40 +020015
Jiri Olsac75540e2016-09-22 17:36:41 +020016struct c2c_hists {
17 struct hists hists;
18 struct perf_hpp_list list;
Jiri Olsab2252ae2016-09-22 17:36:46 +020019 struct c2c_stats stats;
Jiri Olsac75540e2016-09-22 17:36:41 +020020};
21
Jiri Olsa78b27542016-09-22 17:36:44 +020022struct c2c_hist_entry {
23 struct c2c_hists *hists;
Jiri Olsab2252ae2016-09-22 17:36:46 +020024 struct c2c_stats stats;
Jiri Olsa78b27542016-09-22 17:36:44 +020025 /*
26 * must be at the end,
27 * because of its callchain dynamic entry
28 */
29 struct hist_entry he;
30};
31
Jiri Olsa903a6f12016-09-22 17:36:40 +020032struct perf_c2c {
Jiri Olsac75540e2016-09-22 17:36:41 +020033 struct perf_tool tool;
34 struct c2c_hists hists;
Jiri Olsa903a6f12016-09-22 17:36:40 +020035};
36
37static struct perf_c2c c2c;
Jiri Olsa7aef3bf2016-09-22 17:36:38 +020038
Jiri Olsa78b27542016-09-22 17:36:44 +020039static void *c2c_he_zalloc(size_t size)
40{
41 struct c2c_hist_entry *c2c_he;
42
43 c2c_he = zalloc(size + sizeof(*c2c_he));
44 if (!c2c_he)
45 return NULL;
46
47 return &c2c_he->he;
48}
49
50static void c2c_he_free(void *he)
51{
52 struct c2c_hist_entry *c2c_he;
53
54 c2c_he = container_of(he, struct c2c_hist_entry, he);
55 if (c2c_he->hists) {
56 hists__delete_entries(&c2c_he->hists->hists);
57 free(c2c_he->hists);
58 }
59
60 free(c2c_he);
61}
62
63static struct hist_entry_ops c2c_entry_ops = {
64 .new = c2c_he_zalloc,
65 .free = c2c_he_free,
66};
67
Jiri Olsaec06f9b2016-09-22 17:36:45 +020068static int c2c_hists__init(struct c2c_hists *hists,
69 const char *sort);
70
Jiri Olsab2252ae2016-09-22 17:36:46 +020071static struct c2c_hists*
72he__get_c2c_hists(struct hist_entry *he,
73 const char *sort)
Jiri Olsaec06f9b2016-09-22 17:36:45 +020074{
75 struct c2c_hist_entry *c2c_he;
76 struct c2c_hists *hists;
77 int ret;
78
79 c2c_he = container_of(he, struct c2c_hist_entry, he);
80 if (c2c_he->hists)
Jiri Olsab2252ae2016-09-22 17:36:46 +020081 return c2c_he->hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +020082
83 hists = c2c_he->hists = zalloc(sizeof(*hists));
84 if (!hists)
85 return NULL;
86
87 ret = c2c_hists__init(hists, sort);
88 if (ret) {
89 free(hists);
90 return NULL;
91 }
92
Jiri Olsab2252ae2016-09-22 17:36:46 +020093 return hists;
Jiri Olsaec06f9b2016-09-22 17:36:45 +020094}
95
Jiri Olsa78b27542016-09-22 17:36:44 +020096static int process_sample_event(struct perf_tool *tool __maybe_unused,
97 union perf_event *event,
98 struct perf_sample *sample,
99 struct perf_evsel *evsel __maybe_unused,
100 struct machine *machine)
101{
Jiri Olsab2252ae2016-09-22 17:36:46 +0200102 struct c2c_hists *c2c_hists = &c2c.hists;
103 struct c2c_hist_entry *c2c_he;
104 struct c2c_stats stats = { .nr_entries = 0, };
Jiri Olsa78b27542016-09-22 17:36:44 +0200105 struct hist_entry *he;
106 struct addr_location al;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200107 struct mem_info *mi, *mi_dup;
Jiri Olsa78b27542016-09-22 17:36:44 +0200108 int ret;
109
110 if (machine__resolve(machine, &al, sample) < 0) {
111 pr_debug("problem processing %d event, skipping it.\n",
112 event->header.type);
113 return -1;
114 }
115
116 mi = sample__resolve_mem(sample, &al);
117 if (mi == NULL)
118 return -ENOMEM;
119
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200120 mi_dup = memdup(mi, sizeof(*mi));
121 if (!mi_dup)
122 goto free_mi;
123
Jiri Olsab2252ae2016-09-22 17:36:46 +0200124 c2c_decode_stats(&stats, mi);
125
126 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsa78b27542016-09-22 17:36:44 +0200127 &al, NULL, NULL, mi,
128 sample, true);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200129 if (he == NULL)
130 goto free_mi_dup;
Jiri Olsa78b27542016-09-22 17:36:44 +0200131
Jiri Olsab2252ae2016-09-22 17:36:46 +0200132 c2c_he = container_of(he, struct c2c_hist_entry, he);
133 c2c_add_stats(&c2c_he->stats, &stats);
134 c2c_add_stats(&c2c_hists->stats, &stats);
135
136 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsa78b27542016-09-22 17:36:44 +0200137 ret = hist_entry__append_callchain(he, sample);
138
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200139 if (!ret) {
140 mi = mi_dup;
141
142 mi_dup = memdup(mi, sizeof(*mi));
143 if (!mi_dup)
144 goto free_mi;
145
Jiri Olsab2252ae2016-09-22 17:36:46 +0200146 c2c_hists = he__get_c2c_hists(he, "offset");
147 if (!c2c_hists)
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200148 goto free_mi_dup;
149
Jiri Olsab2252ae2016-09-22 17:36:46 +0200150 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200151 &al, NULL, NULL, mi,
152 sample, true);
153 if (he == NULL)
154 goto free_mi_dup;
155
Jiri Olsab2252ae2016-09-22 17:36:46 +0200156 c2c_he = container_of(he, struct c2c_hist_entry, he);
157 c2c_add_stats(&c2c_he->stats, &stats);
158 c2c_add_stats(&c2c_hists->stats, &stats);
159
160 hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200161 ret = hist_entry__append_callchain(he, sample);
162 }
163
164out:
Jiri Olsa78b27542016-09-22 17:36:44 +0200165 addr_location__put(&al);
166 return ret;
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200167
168free_mi_dup:
169 free(mi_dup);
170free_mi:
171 free(mi);
172 ret = -ENOMEM;
173 goto out;
Jiri Olsa78b27542016-09-22 17:36:44 +0200174}
175
176static struct perf_c2c c2c = {
177 .tool = {
178 .sample = process_sample_event,
179 .mmap = perf_event__process_mmap,
180 .mmap2 = perf_event__process_mmap2,
181 .comm = perf_event__process_comm,
182 .exit = perf_event__process_exit,
183 .fork = perf_event__process_fork,
184 .lost = perf_event__process_lost,
185 .ordered_events = true,
186 .ordering_requires_timestamps = true,
187 },
188};
189
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200190static const char * const c2c_usage[] = {
Jiri Olsa903a6f12016-09-22 17:36:40 +0200191 "perf c2c {record|report}",
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200192 NULL
193};
194
Jiri Olsa903a6f12016-09-22 17:36:40 +0200195static const char * const __usage_report[] = {
196 "perf c2c report",
197 NULL
198};
199
200static const char * const *report_c2c_usage = __usage_report;
201
Jiri Olsac75540e2016-09-22 17:36:41 +0200202#define C2C_HEADER_MAX 2
203
204struct c2c_header {
205 struct {
206 const char *text;
207 int span;
208 } line[C2C_HEADER_MAX];
209};
210
211struct c2c_dimension {
212 struct c2c_header header;
213 const char *name;
214 int width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200215 struct sort_entry *se;
Jiri Olsac75540e2016-09-22 17:36:41 +0200216
217 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
218 struct hist_entry *, struct hist_entry *);
219 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
220 struct hist_entry *he);
221 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
222 struct hist_entry *he);
223};
224
225struct c2c_fmt {
226 struct perf_hpp_fmt fmt;
227 struct c2c_dimension *dim;
228};
229
230static int c2c_width(struct perf_hpp_fmt *fmt,
231 struct perf_hpp *hpp __maybe_unused,
232 struct hists *hists __maybe_unused)
233{
234 struct c2c_fmt *c2c_fmt;
Jiri Olsac75540e2016-09-22 17:36:41 +0200235 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +0200236
237 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
238 dim = c2c_fmt->dim;
239
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200240 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
241 c2c_fmt->dim->width;
242}
243
244static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
245 struct hists *hists, int line, int *span)
246{
247 struct perf_hpp_list *hpp_list = hists->hpp_list;
248 struct c2c_fmt *c2c_fmt;
249 struct c2c_dimension *dim;
250 const char *text = NULL;
251 int width = c2c_width(fmt, hpp, hists);
252
253 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
254 dim = c2c_fmt->dim;
255
256 if (dim->se) {
257 text = dim->header.line[line].text;
258 /* Use the last line from sort_entry if not defined. */
259 if (!text && (line == hpp_list->nr_header_lines - 1))
260 text = dim->se->se_header;
261 } else {
262 text = dim->header.line[line].text;
263
264 if (*span) {
265 (*span)--;
266 return 0;
267 } else {
268 *span = dim->header.line[line].span;
269 }
270 }
271
Jiri Olsac75540e2016-09-22 17:36:41 +0200272 if (text == NULL)
273 text = "";
274
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200275 return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
Jiri Olsac75540e2016-09-22 17:36:41 +0200276}
277
Jiri Olsacbb88502016-09-22 17:36:48 +0200278#define HEX_STR(__s, __v) \
279({ \
280 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
281 __s; \
282})
283
284static int64_t
285dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
286 struct hist_entry *left, struct hist_entry *right)
287{
288 return sort__dcacheline_cmp(left, right);
289}
290
291static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
292 struct hist_entry *he)
293{
294 uint64_t addr = 0;
295 int width = c2c_width(fmt, hpp, he->hists);
296 char buf[20];
297
298 if (he->mem_info)
299 addr = cl_address(he->mem_info->daddr.addr);
300
301 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
302}
303
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200304static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
305 struct hist_entry *he)
306{
307 uint64_t addr = 0;
308 int width = c2c_width(fmt, hpp, he->hists);
309 char buf[20];
310
311 if (he->mem_info)
312 addr = cl_offset(he->mem_info->daddr.al_addr);
313
314 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
315}
316
317static int64_t
318offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
319 struct hist_entry *left, struct hist_entry *right)
320{
321 uint64_t l = 0, r = 0;
322
323 if (left->mem_info)
324 l = cl_offset(left->mem_info->daddr.addr);
325 if (right->mem_info)
326 r = cl_offset(right->mem_info->daddr.addr);
327
328 return (int64_t)(r - l);
329}
330
Jiri Olsa43575a92016-05-03 21:48:56 +0200331static int
332iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
333 struct hist_entry *he)
334{
335 uint64_t addr = 0;
336 int width = c2c_width(fmt, hpp, he->hists);
337 char buf[20];
338
339 if (he->mem_info)
340 addr = he->mem_info->iaddr.addr;
341
342 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
343}
344
345static int64_t
346iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
347 struct hist_entry *left, struct hist_entry *right)
348{
349 return sort__iaddr_cmp(left, right);
350}
351
Jiri Olsa97cb4862016-05-23 16:20:14 +0200352static int
353tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
354 struct hist_entry *he)
355{
356 struct c2c_hist_entry *c2c_he;
357 int width = c2c_width(fmt, hpp, he->hists);
358 unsigned int tot_hitm;
359
360 c2c_he = container_of(he, struct c2c_hist_entry, he);
361 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
362
363 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
364}
365
366static int64_t
367tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
368 struct hist_entry *left, struct hist_entry *right)
369{
370 struct c2c_hist_entry *c2c_left;
371 struct c2c_hist_entry *c2c_right;
372 unsigned int tot_hitm_left;
373 unsigned int tot_hitm_right;
374
375 c2c_left = container_of(left, struct c2c_hist_entry, he);
376 c2c_right = container_of(right, struct c2c_hist_entry, he);
377
378 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
379 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
380
381 return tot_hitm_left - tot_hitm_right;
382}
383
384#define STAT_FN_ENTRY(__f) \
385static int \
386__f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
387 struct hist_entry *he) \
388{ \
389 struct c2c_hist_entry *c2c_he; \
390 int width = c2c_width(fmt, hpp, he->hists); \
391 \
392 c2c_he = container_of(he, struct c2c_hist_entry, he); \
393 return scnprintf(hpp->buf, hpp->size, "%*u", width, \
394 c2c_he->stats.__f); \
395}
396
397#define STAT_FN_CMP(__f) \
398static int64_t \
399__f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
400 struct hist_entry *left, struct hist_entry *right) \
401{ \
402 struct c2c_hist_entry *c2c_left, *c2c_right; \
403 \
404 c2c_left = container_of(left, struct c2c_hist_entry, he); \
405 c2c_right = container_of(right, struct c2c_hist_entry, he); \
406 return c2c_left->stats.__f - c2c_right->stats.__f; \
407}
408
409#define STAT_FN(__f) \
410 STAT_FN_ENTRY(__f) \
411 STAT_FN_CMP(__f)
412
413STAT_FN(rmt_hitm)
414STAT_FN(lcl_hitm)
Jiri Olsa0f188962016-05-04 10:10:11 +0200415STAT_FN(store)
416STAT_FN(st_l1hit)
417STAT_FN(st_l1miss)
Jiri Olsa1295f682016-05-04 10:18:24 +0200418STAT_FN(ld_fbhit)
419STAT_FN(ld_l1hit)
420STAT_FN(ld_l2hit)
Jiri Olsa97cb4862016-05-23 16:20:14 +0200421
Jiri Olsa600a8cf2016-09-22 17:36:47 +0200422#define HEADER_LOW(__h) \
423 { \
424 .line[1] = { \
425 .text = __h, \
426 }, \
427 }
428
429#define HEADER_BOTH(__h0, __h1) \
430 { \
431 .line[0] = { \
432 .text = __h0, \
433 }, \
434 .line[1] = { \
435 .text = __h1, \
436 }, \
437 }
438
439#define HEADER_SPAN(__h0, __h1, __s) \
440 { \
441 .line[0] = { \
442 .text = __h0, \
443 .span = __s, \
444 }, \
445 .line[1] = { \
446 .text = __h1, \
447 }, \
448 }
449
450#define HEADER_SPAN_LOW(__h) \
451 { \
452 .line[1] = { \
453 .text = __h, \
454 }, \
455 }
456
Jiri Olsacbb88502016-09-22 17:36:48 +0200457static struct c2c_dimension dim_dcacheline = {
458 .header = HEADER_LOW("Cacheline"),
459 .name = "dcacheline",
460 .cmp = dcacheline_cmp,
461 .entry = dcacheline_entry,
462 .width = 18,
463};
464
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200465static struct c2c_dimension dim_offset = {
466 .header = HEADER_BOTH("Data address", "Offset"),
467 .name = "offset",
468 .cmp = offset_cmp,
469 .entry = offset_entry,
470 .width = 18,
471};
472
Jiri Olsa43575a92016-05-03 21:48:56 +0200473static struct c2c_dimension dim_iaddr = {
474 .header = HEADER_LOW("Code address"),
475 .name = "iaddr",
476 .cmp = iaddr_cmp,
477 .entry = iaddr_entry,
478 .width = 18,
479};
480
Jiri Olsa97cb4862016-05-23 16:20:14 +0200481static struct c2c_dimension dim_tot_hitm = {
482 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
483 .name = "tot_hitm",
484 .cmp = tot_hitm_cmp,
485 .entry = tot_hitm_entry,
486 .width = 7,
487};
488
489static struct c2c_dimension dim_lcl_hitm = {
490 .header = HEADER_SPAN_LOW("Lcl"),
491 .name = "lcl_hitm",
492 .cmp = lcl_hitm_cmp,
493 .entry = lcl_hitm_entry,
494 .width = 7,
495};
496
497static struct c2c_dimension dim_rmt_hitm = {
498 .header = HEADER_SPAN_LOW("Rmt"),
499 .name = "rmt_hitm",
500 .cmp = rmt_hitm_cmp,
501 .entry = rmt_hitm_entry,
502 .width = 7,
503};
504
505static struct c2c_dimension dim_cl_rmt_hitm = {
506 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
507 .name = "cl_rmt_hitm",
508 .cmp = rmt_hitm_cmp,
509 .entry = rmt_hitm_entry,
510 .width = 7,
511};
512
513static struct c2c_dimension dim_cl_lcl_hitm = {
514 .header = HEADER_SPAN_LOW("Lcl"),
515 .name = "cl_lcl_hitm",
516 .cmp = lcl_hitm_cmp,
517 .entry = lcl_hitm_entry,
518 .width = 7,
519};
520
Jiri Olsa0f188962016-05-04 10:10:11 +0200521static struct c2c_dimension dim_stores = {
522 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
523 .name = "stores",
524 .cmp = store_cmp,
525 .entry = store_entry,
526 .width = 7,
527};
528
529static struct c2c_dimension dim_stores_l1hit = {
530 .header = HEADER_SPAN_LOW("L1Hit"),
531 .name = "stores_l1hit",
532 .cmp = st_l1hit_cmp,
533 .entry = st_l1hit_entry,
534 .width = 7,
535};
536
537static struct c2c_dimension dim_stores_l1miss = {
538 .header = HEADER_SPAN_LOW("L1Miss"),
539 .name = "stores_l1miss",
540 .cmp = st_l1miss_cmp,
541 .entry = st_l1miss_entry,
542 .width = 7,
543};
544
545static struct c2c_dimension dim_cl_stores_l1hit = {
546 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
547 .name = "cl_stores_l1hit",
548 .cmp = st_l1hit_cmp,
549 .entry = st_l1hit_entry,
550 .width = 7,
551};
552
553static struct c2c_dimension dim_cl_stores_l1miss = {
554 .header = HEADER_SPAN_LOW("L1 Miss"),
555 .name = "cl_stores_l1miss",
556 .cmp = st_l1miss_cmp,
557 .entry = st_l1miss_entry,
558 .width = 7,
559};
560
Jiri Olsa1295f682016-05-04 10:18:24 +0200561static struct c2c_dimension dim_ld_fbhit = {
562 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
563 .name = "ld_fbhit",
564 .cmp = ld_fbhit_cmp,
565 .entry = ld_fbhit_entry,
566 .width = 7,
567};
568
569static struct c2c_dimension dim_ld_l1hit = {
570 .header = HEADER_SPAN_LOW("L1"),
571 .name = "ld_l1hit",
572 .cmp = ld_l1hit_cmp,
573 .entry = ld_l1hit_entry,
574 .width = 7,
575};
576
577static struct c2c_dimension dim_ld_l2hit = {
578 .header = HEADER_SPAN_LOW("L2"),
579 .name = "ld_l2hit",
580 .cmp = ld_l2hit_cmp,
581 .entry = ld_l2hit_entry,
582 .width = 7,
583};
584
Jiri Olsac75540e2016-09-22 17:36:41 +0200585static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +0200586 &dim_dcacheline,
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200587 &dim_offset,
Jiri Olsa43575a92016-05-03 21:48:56 +0200588 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +0200589 &dim_tot_hitm,
590 &dim_lcl_hitm,
591 &dim_rmt_hitm,
592 &dim_cl_lcl_hitm,
593 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +0200594 &dim_stores,
595 &dim_stores_l1hit,
596 &dim_stores_l1miss,
597 &dim_cl_stores_l1hit,
598 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +0200599 &dim_ld_fbhit,
600 &dim_ld_l1hit,
601 &dim_ld_l2hit,
Jiri Olsac75540e2016-09-22 17:36:41 +0200602 NULL,
603};
604
605static void fmt_free(struct perf_hpp_fmt *fmt)
606{
607 struct c2c_fmt *c2c_fmt;
608
609 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
610 free(c2c_fmt);
611}
612
613static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
614{
615 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
616 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
617
618 return c2c_a->dim == c2c_b->dim;
619}
620
621static struct c2c_dimension *get_dimension(const char *name)
622{
623 unsigned int i;
624
625 for (i = 0; dimensions[i]; i++) {
626 struct c2c_dimension *dim = dimensions[i];
627
628 if (!strcmp(dim->name, name))
629 return dim;
630 };
631
632 return NULL;
633}
634
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200635static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
636 struct hist_entry *he)
637{
638 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
639 struct c2c_dimension *dim = c2c_fmt->dim;
640 size_t len = fmt->user_len;
641
642 if (!len)
643 len = hists__col_len(he->hists, dim->se->se_width_idx);
644
645 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
646}
647
648static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
649 struct hist_entry *a, struct hist_entry *b)
650{
651 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
652 struct c2c_dimension *dim = c2c_fmt->dim;
653
654 return dim->se->se_cmp(a, b);
655}
656
657static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
658 struct hist_entry *a, struct hist_entry *b)
659{
660 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
661 struct c2c_dimension *dim = c2c_fmt->dim;
662 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
663
664 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
665 return collapse_fn(a, b);
666}
667
Jiri Olsac75540e2016-09-22 17:36:41 +0200668static struct c2c_fmt *get_format(const char *name)
669{
670 struct c2c_dimension *dim = get_dimension(name);
671 struct c2c_fmt *c2c_fmt;
672 struct perf_hpp_fmt *fmt;
673
674 if (!dim)
675 return NULL;
676
677 c2c_fmt = zalloc(sizeof(*c2c_fmt));
678 if (!c2c_fmt)
679 return NULL;
680
681 c2c_fmt->dim = dim;
682
683 fmt = &c2c_fmt->fmt;
684 INIT_LIST_HEAD(&fmt->list);
685 INIT_LIST_HEAD(&fmt->sort_list);
686
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200687 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
688 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
689 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +0200690 fmt->header = c2c_header;
691 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200692 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +0200693 fmt->equal = fmt_equal;
694 fmt->free = fmt_free;
695
696 return c2c_fmt;
697}
698
699static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
700{
701 struct c2c_fmt *c2c_fmt = get_format(name);
702
Jiri Olsa5f2eca82016-09-22 17:36:43 +0200703 if (!c2c_fmt) {
704 reset_dimensions();
705 return output_field_add(hpp_list, name);
706 }
Jiri Olsac75540e2016-09-22 17:36:41 +0200707
708 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
709 return 0;
710}
711
712static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
713{
714 struct c2c_fmt *c2c_fmt = get_format(name);
715
Jiri Olsa5f2eca82016-09-22 17:36:43 +0200716 if (!c2c_fmt) {
717 reset_dimensions();
718 return sort_dimension__add(hpp_list, name, NULL, 0);
719 }
Jiri Olsac75540e2016-09-22 17:36:41 +0200720
721 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
722 return 0;
723}
724
725#define PARSE_LIST(_list, _fn) \
726 do { \
727 char *tmp, *tok; \
728 ret = 0; \
729 \
730 if (!_list) \
731 break; \
732 \
733 for (tok = strtok_r((char *)_list, ", ", &tmp); \
734 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
735 ret = _fn(hpp_list, tok); \
736 if (ret == -EINVAL) { \
737 error("Invalid --fields key: `%s'", tok); \
738 break; \
739 } else if (ret == -ESRCH) { \
740 error("Unknown --fields key: `%s'", tok); \
741 break; \
742 } \
743 } \
744 } while (0)
745
746static int hpp_list__parse(struct perf_hpp_list *hpp_list,
747 const char *output_,
748 const char *sort_)
749{
750 char *output = output_ ? strdup(output_) : NULL;
751 char *sort = sort_ ? strdup(sort_) : NULL;
752 int ret;
753
754 PARSE_LIST(output, c2c_hists__init_output);
755 PARSE_LIST(sort, c2c_hists__init_sort);
756
757 /* copy sort keys to output fields */
758 perf_hpp__setup_output_field(hpp_list);
759
760 /*
761 * We dont need other sorting keys other than those
762 * we already specified. It also really slows down
763 * the processing a lot with big number of output
764 * fields, so switching this off for c2c.
765 */
766
767#if 0
768 /* and then copy output fields to sort keys */
769 perf_hpp__append_sort_keys(&hists->list);
770#endif
771
772 free(output);
773 free(sort);
774 return ret;
775}
776
777static int c2c_hists__init(struct c2c_hists *hists,
778 const char *sort)
779{
780 __hists__init(&hists->hists, &hists->list);
781
782 /*
783 * Initialize only with sort fields, we need to resort
784 * later anyway, and that's where we add output fields
785 * as well.
786 */
787 perf_hpp_list__init(&hists->list);
788
789 return hpp_list__parse(&hists->list, NULL, sort);
790}
791
792__maybe_unused
793static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
794 const char *output,
795 const char *sort)
796{
797 perf_hpp__reset_output_field(&c2c_hists->list);
798 return hpp_list__parse(&c2c_hists->list, output, sort);
799}
800
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200801static int filter_cb(struct hist_entry *he __maybe_unused)
802{
803 return 0;
804}
805
806static int resort_cl_cb(struct hist_entry *he)
807{
808 struct c2c_hist_entry *c2c_he;
809 struct c2c_hists *c2c_hists;
810
811 c2c_he = container_of(he, struct c2c_hist_entry, he);
812 c2c_hists = c2c_he->hists;
813
814 if (c2c_hists) {
815 hists__collapse_resort(&c2c_hists->hists, NULL);
816 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
817 }
818
819 return 0;
820}
821
Jiri Olsa903a6f12016-09-22 17:36:40 +0200822static int perf_c2c__report(int argc, const char **argv)
823{
824 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +0200825 struct ui_progress prog;
Jiri Olsa903a6f12016-09-22 17:36:40 +0200826 struct perf_data_file file = {
827 .mode = PERF_DATA_MODE_READ,
828 };
829 const struct option c2c_options[] = {
830 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
831 "file", "vmlinux pathname"),
832 OPT_INCR('v', "verbose", &verbose,
833 "be more verbose (show counter open errors, etc)"),
834 OPT_STRING('i', "input", &input_name, "file",
835 "the input file to process"),
836 OPT_END()
837 };
838 int err = 0;
839
840 argc = parse_options(argc, argv, c2c_options, report_c2c_usage,
841 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +0200842 if (argc)
Jiri Olsa903a6f12016-09-22 17:36:40 +0200843 usage_with_options(report_c2c_usage, c2c_options);
844
Jiri Olsa78b27542016-09-22 17:36:44 +0200845 if (!input_name || !strlen(input_name))
846 input_name = "perf.data";
847
Jiri Olsa903a6f12016-09-22 17:36:40 +0200848 file.path = input_name;
849
Jiri Olsac75540e2016-09-22 17:36:41 +0200850 err = c2c_hists__init(&c2c.hists, "dcacheline");
851 if (err) {
852 pr_debug("Failed to initialize hists\n");
853 goto out;
854 }
855
Jiri Olsa903a6f12016-09-22 17:36:40 +0200856 session = perf_session__new(&file, 0, &c2c.tool);
857 if (session == NULL) {
858 pr_debug("No memory for session\n");
859 goto out;
860 }
861
862 if (symbol__init(&session->header.env) < 0)
863 goto out_session;
864
865 /* No pipe support at the moment. */
866 if (perf_data_file__is_pipe(session->file)) {
867 pr_debug("No pipe support at the moment.\n");
868 goto out_session;
869 }
870
Jiri Olsa78b27542016-09-22 17:36:44 +0200871 err = perf_session__process_events(session);
872 if (err) {
873 pr_err("failed to process sample\n");
874 goto out_session;
875 }
876
877 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
878
879 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200880 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +0200881
882 ui_progress__finish();
883
Jiri Olsa903a6f12016-09-22 17:36:40 +0200884out_session:
885 perf_session__delete(session);
886out:
887 return err;
888}
889
Jiri Olsa39bcd4a2016-09-22 17:36:39 +0200890static int parse_record_events(const struct option *opt __maybe_unused,
891 const char *str, int unset __maybe_unused)
892{
893 bool *event_set = (bool *) opt->value;
894
895 *event_set = true;
896 return perf_mem_events__parse(str);
897}
898
899
900static const char * const __usage_record[] = {
901 "perf c2c record [<options>] [<command>]",
902 "perf c2c record [<options>] -- <command> [<options>]",
903 NULL
904};
905
906static const char * const *record_mem_usage = __usage_record;
907
908static int perf_c2c__record(int argc, const char **argv)
909{
910 int rec_argc, i = 0, j;
911 const char **rec_argv;
912 int ret;
913 bool all_user = false, all_kernel = false;
914 bool event_set = false;
915 struct option options[] = {
916 OPT_CALLBACK('e', "event", &event_set, "event",
917 "event selector. Use 'perf mem record -e list' to list available events",
918 parse_record_events),
919 OPT_INCR('v', "verbose", &verbose,
920 "be more verbose (show counter open errors, etc)"),
921 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
922 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
923 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
924 OPT_END()
925 };
926
927 if (perf_mem_events__init()) {
928 pr_err("failed: memory events not supported\n");
929 return -1;
930 }
931
932 argc = parse_options(argc, argv, options, record_mem_usage,
933 PARSE_OPT_KEEP_UNKNOWN);
934
935 rec_argc = argc + 10; /* max number of arguments */
936 rec_argv = calloc(rec_argc + 1, sizeof(char *));
937 if (!rec_argv)
938 return -1;
939
940 rec_argv[i++] = "record";
941
942 if (!event_set) {
943 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
944 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
945 }
946
947 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
948 rec_argv[i++] = "-W";
949
950 rec_argv[i++] = "-d";
951 rec_argv[i++] = "--sample-cpu";
952
953 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
954 if (!perf_mem_events[j].record)
955 continue;
956
957 if (!perf_mem_events[j].supported) {
958 pr_err("failed: event '%s' not supported\n",
959 perf_mem_events[j].name);
960 return -1;
961 }
962
963 rec_argv[i++] = "-e";
964 rec_argv[i++] = perf_mem_events__name(j);
965 };
966
967 if (all_user)
968 rec_argv[i++] = "--all-user";
969
970 if (all_kernel)
971 rec_argv[i++] = "--all-kernel";
972
973 for (j = 0; j < argc; j++, i++)
974 rec_argv[i] = argv[j];
975
976 if (verbose > 0) {
977 pr_debug("calling: ");
978
979 j = 0;
980
981 while (rec_argv[j]) {
982 pr_debug("%s ", rec_argv[j]);
983 j++;
984 }
985 pr_debug("\n");
986 }
987
988 ret = cmd_record(i, rec_argv, NULL);
989 free(rec_argv);
990 return ret;
991}
992
Jiri Olsa7aef3bf2016-09-22 17:36:38 +0200993int cmd_c2c(int argc, const char **argv, const char *prefix __maybe_unused)
994{
995 const struct option c2c_options[] = {
996 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
997 OPT_END()
998 };
999
1000 argc = parse_options(argc, argv, c2c_options, c2c_usage,
1001 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001002
1003 if (!argc)
1004 usage_with_options(c2c_usage, c2c_options);
1005
1006 if (!strncmp(argv[0], "rec", 3)) {
1007 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02001008 } else if (!strncmp(argv[0], "rep", 3)) {
1009 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001010 } else {
1011 usage_with_options(c2c_usage, c2c_options);
1012 }
1013
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02001014 return 0;
1015}