blob: ff5b5b81d333436ad0eda69f115f3ce72f4b13b7 [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 Olsa4d089102016-05-04 10:27:51 +0200421STAT_FN(ld_llchit)
422STAT_FN(rmt_hit)
Jiri Olsa97cb4862016-05-23 16:20:14 +0200423
Jiri Olsa04402d22016-05-19 10:10:51 +0200424static uint64_t llc_miss(struct c2c_stats *stats)
425{
426 uint64_t llcmiss;
427
428 llcmiss = stats->lcl_dram +
429 stats->rmt_dram +
430 stats->rmt_hitm +
431 stats->rmt_hit;
432
433 return llcmiss;
434}
435
436static int
437ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
438 struct hist_entry *he)
439{
440 struct c2c_hist_entry *c2c_he;
441 int width = c2c_width(fmt, hpp, he->hists);
442
443 c2c_he = container_of(he, struct c2c_hist_entry, he);
444
445 return scnprintf(hpp->buf, hpp->size, "%*lu", width,
446 llc_miss(&c2c_he->stats));
447}
448
449static int64_t
450ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
451 struct hist_entry *left, struct hist_entry *right)
452{
453 struct c2c_hist_entry *c2c_left;
454 struct c2c_hist_entry *c2c_right;
455
456 c2c_left = container_of(left, struct c2c_hist_entry, he);
457 c2c_right = container_of(right, struct c2c_hist_entry, he);
458
459 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats);
460}
461
Jiri Olsa01b84d72016-05-04 10:35:29 +0200462static uint64_t total_records(struct c2c_stats *stats)
463{
464 uint64_t lclmiss, ldcnt, total;
465
466 lclmiss = stats->lcl_dram +
467 stats->rmt_dram +
468 stats->rmt_hitm +
469 stats->rmt_hit;
470
471 ldcnt = lclmiss +
472 stats->ld_fbhit +
473 stats->ld_l1hit +
474 stats->ld_l2hit +
475 stats->ld_llchit +
476 stats->lcl_hitm;
477
478 total = ldcnt +
479 stats->st_l1hit +
480 stats->st_l1miss;
481
482 return total;
483}
484
485static int
486tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
487 struct hist_entry *he)
488{
489 struct c2c_hist_entry *c2c_he;
490 int width = c2c_width(fmt, hpp, he->hists);
491 uint64_t tot_recs;
492
493 c2c_he = container_of(he, struct c2c_hist_entry, he);
494 tot_recs = total_records(&c2c_he->stats);
495
496 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
497}
498
499static int64_t
500tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
501 struct hist_entry *left, struct hist_entry *right)
502{
503 struct c2c_hist_entry *c2c_left;
504 struct c2c_hist_entry *c2c_right;
505 uint64_t tot_recs_left;
506 uint64_t tot_recs_right;
507
508 c2c_left = container_of(left, struct c2c_hist_entry, he);
509 c2c_right = container_of(right, struct c2c_hist_entry, he);
510
511 tot_recs_left = total_records(&c2c_left->stats);
512 tot_recs_right = total_records(&c2c_right->stats);
513
514 return tot_recs_left - tot_recs_right;
515}
516
Jiri Olsa600a8cf2016-09-22 17:36:47 +0200517#define HEADER_LOW(__h) \
518 { \
519 .line[1] = { \
520 .text = __h, \
521 }, \
522 }
523
524#define HEADER_BOTH(__h0, __h1) \
525 { \
526 .line[0] = { \
527 .text = __h0, \
528 }, \
529 .line[1] = { \
530 .text = __h1, \
531 }, \
532 }
533
534#define HEADER_SPAN(__h0, __h1, __s) \
535 { \
536 .line[0] = { \
537 .text = __h0, \
538 .span = __s, \
539 }, \
540 .line[1] = { \
541 .text = __h1, \
542 }, \
543 }
544
545#define HEADER_SPAN_LOW(__h) \
546 { \
547 .line[1] = { \
548 .text = __h, \
549 }, \
550 }
551
Jiri Olsacbb88502016-09-22 17:36:48 +0200552static struct c2c_dimension dim_dcacheline = {
553 .header = HEADER_LOW("Cacheline"),
554 .name = "dcacheline",
555 .cmp = dcacheline_cmp,
556 .entry = dcacheline_entry,
557 .width = 18,
558};
559
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200560static struct c2c_dimension dim_offset = {
561 .header = HEADER_BOTH("Data address", "Offset"),
562 .name = "offset",
563 .cmp = offset_cmp,
564 .entry = offset_entry,
565 .width = 18,
566};
567
Jiri Olsa43575a92016-05-03 21:48:56 +0200568static struct c2c_dimension dim_iaddr = {
569 .header = HEADER_LOW("Code address"),
570 .name = "iaddr",
571 .cmp = iaddr_cmp,
572 .entry = iaddr_entry,
573 .width = 18,
574};
575
Jiri Olsa97cb4862016-05-23 16:20:14 +0200576static struct c2c_dimension dim_tot_hitm = {
577 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
578 .name = "tot_hitm",
579 .cmp = tot_hitm_cmp,
580 .entry = tot_hitm_entry,
581 .width = 7,
582};
583
584static struct c2c_dimension dim_lcl_hitm = {
585 .header = HEADER_SPAN_LOW("Lcl"),
586 .name = "lcl_hitm",
587 .cmp = lcl_hitm_cmp,
588 .entry = lcl_hitm_entry,
589 .width = 7,
590};
591
592static struct c2c_dimension dim_rmt_hitm = {
593 .header = HEADER_SPAN_LOW("Rmt"),
594 .name = "rmt_hitm",
595 .cmp = rmt_hitm_cmp,
596 .entry = rmt_hitm_entry,
597 .width = 7,
598};
599
600static struct c2c_dimension dim_cl_rmt_hitm = {
601 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
602 .name = "cl_rmt_hitm",
603 .cmp = rmt_hitm_cmp,
604 .entry = rmt_hitm_entry,
605 .width = 7,
606};
607
608static struct c2c_dimension dim_cl_lcl_hitm = {
609 .header = HEADER_SPAN_LOW("Lcl"),
610 .name = "cl_lcl_hitm",
611 .cmp = lcl_hitm_cmp,
612 .entry = lcl_hitm_entry,
613 .width = 7,
614};
615
Jiri Olsa0f188962016-05-04 10:10:11 +0200616static struct c2c_dimension dim_stores = {
617 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
618 .name = "stores",
619 .cmp = store_cmp,
620 .entry = store_entry,
621 .width = 7,
622};
623
624static struct c2c_dimension dim_stores_l1hit = {
625 .header = HEADER_SPAN_LOW("L1Hit"),
626 .name = "stores_l1hit",
627 .cmp = st_l1hit_cmp,
628 .entry = st_l1hit_entry,
629 .width = 7,
630};
631
632static struct c2c_dimension dim_stores_l1miss = {
633 .header = HEADER_SPAN_LOW("L1Miss"),
634 .name = "stores_l1miss",
635 .cmp = st_l1miss_cmp,
636 .entry = st_l1miss_entry,
637 .width = 7,
638};
639
640static struct c2c_dimension dim_cl_stores_l1hit = {
641 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
642 .name = "cl_stores_l1hit",
643 .cmp = st_l1hit_cmp,
644 .entry = st_l1hit_entry,
645 .width = 7,
646};
647
648static struct c2c_dimension dim_cl_stores_l1miss = {
649 .header = HEADER_SPAN_LOW("L1 Miss"),
650 .name = "cl_stores_l1miss",
651 .cmp = st_l1miss_cmp,
652 .entry = st_l1miss_entry,
653 .width = 7,
654};
655
Jiri Olsa1295f682016-05-04 10:18:24 +0200656static struct c2c_dimension dim_ld_fbhit = {
657 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
658 .name = "ld_fbhit",
659 .cmp = ld_fbhit_cmp,
660 .entry = ld_fbhit_entry,
661 .width = 7,
662};
663
664static struct c2c_dimension dim_ld_l1hit = {
665 .header = HEADER_SPAN_LOW("L1"),
666 .name = "ld_l1hit",
667 .cmp = ld_l1hit_cmp,
668 .entry = ld_l1hit_entry,
669 .width = 7,
670};
671
672static struct c2c_dimension dim_ld_l2hit = {
673 .header = HEADER_SPAN_LOW("L2"),
674 .name = "ld_l2hit",
675 .cmp = ld_l2hit_cmp,
676 .entry = ld_l2hit_entry,
677 .width = 7,
678};
679
Jiri Olsa4d089102016-05-04 10:27:51 +0200680static struct c2c_dimension dim_ld_llchit = {
681 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
682 .name = "ld_lclhit",
683 .cmp = ld_llchit_cmp,
684 .entry = ld_llchit_entry,
685 .width = 8,
686};
687
688static struct c2c_dimension dim_ld_rmthit = {
689 .header = HEADER_SPAN_LOW("Rmt"),
690 .name = "ld_rmthit",
691 .cmp = rmt_hit_cmp,
692 .entry = rmt_hit_entry,
693 .width = 8,
694};
695
Jiri Olsa04402d22016-05-19 10:10:51 +0200696static struct c2c_dimension dim_ld_llcmiss = {
697 .header = HEADER_BOTH("LLC", "Ld Miss"),
698 .name = "ld_llcmiss",
699 .cmp = ld_llcmiss_cmp,
700 .entry = ld_llcmiss_entry,
701 .width = 7,
702};
703
Jiri Olsa01b84d72016-05-04 10:35:29 +0200704static struct c2c_dimension dim_tot_recs = {
705 .header = HEADER_BOTH("Total", "records"),
706 .name = "tot_recs",
707 .cmp = tot_recs_cmp,
708 .entry = tot_recs_entry,
709 .width = 7,
710};
711
Jiri Olsac75540e2016-09-22 17:36:41 +0200712static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +0200713 &dim_dcacheline,
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200714 &dim_offset,
Jiri Olsa43575a92016-05-03 21:48:56 +0200715 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +0200716 &dim_tot_hitm,
717 &dim_lcl_hitm,
718 &dim_rmt_hitm,
719 &dim_cl_lcl_hitm,
720 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +0200721 &dim_stores,
722 &dim_stores_l1hit,
723 &dim_stores_l1miss,
724 &dim_cl_stores_l1hit,
725 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +0200726 &dim_ld_fbhit,
727 &dim_ld_l1hit,
728 &dim_ld_l2hit,
Jiri Olsa4d089102016-05-04 10:27:51 +0200729 &dim_ld_llchit,
730 &dim_ld_rmthit,
Jiri Olsa04402d22016-05-19 10:10:51 +0200731 &dim_ld_llcmiss,
Jiri Olsa01b84d72016-05-04 10:35:29 +0200732 &dim_tot_recs,
Jiri Olsac75540e2016-09-22 17:36:41 +0200733 NULL,
734};
735
736static void fmt_free(struct perf_hpp_fmt *fmt)
737{
738 struct c2c_fmt *c2c_fmt;
739
740 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
741 free(c2c_fmt);
742}
743
744static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
745{
746 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
747 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
748
749 return c2c_a->dim == c2c_b->dim;
750}
751
752static struct c2c_dimension *get_dimension(const char *name)
753{
754 unsigned int i;
755
756 for (i = 0; dimensions[i]; i++) {
757 struct c2c_dimension *dim = dimensions[i];
758
759 if (!strcmp(dim->name, name))
760 return dim;
761 };
762
763 return NULL;
764}
765
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200766static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
767 struct hist_entry *he)
768{
769 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
770 struct c2c_dimension *dim = c2c_fmt->dim;
771 size_t len = fmt->user_len;
772
773 if (!len)
774 len = hists__col_len(he->hists, dim->se->se_width_idx);
775
776 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
777}
778
779static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
780 struct hist_entry *a, struct hist_entry *b)
781{
782 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
783 struct c2c_dimension *dim = c2c_fmt->dim;
784
785 return dim->se->se_cmp(a, b);
786}
787
788static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
789 struct hist_entry *a, struct hist_entry *b)
790{
791 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
792 struct c2c_dimension *dim = c2c_fmt->dim;
793 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
794
795 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
796 return collapse_fn(a, b);
797}
798
Jiri Olsac75540e2016-09-22 17:36:41 +0200799static struct c2c_fmt *get_format(const char *name)
800{
801 struct c2c_dimension *dim = get_dimension(name);
802 struct c2c_fmt *c2c_fmt;
803 struct perf_hpp_fmt *fmt;
804
805 if (!dim)
806 return NULL;
807
808 c2c_fmt = zalloc(sizeof(*c2c_fmt));
809 if (!c2c_fmt)
810 return NULL;
811
812 c2c_fmt->dim = dim;
813
814 fmt = &c2c_fmt->fmt;
815 INIT_LIST_HEAD(&fmt->list);
816 INIT_LIST_HEAD(&fmt->sort_list);
817
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200818 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
819 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
820 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +0200821 fmt->header = c2c_header;
822 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +0200823 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +0200824 fmt->equal = fmt_equal;
825 fmt->free = fmt_free;
826
827 return c2c_fmt;
828}
829
830static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
831{
832 struct c2c_fmt *c2c_fmt = get_format(name);
833
Jiri Olsa5f2eca82016-09-22 17:36:43 +0200834 if (!c2c_fmt) {
835 reset_dimensions();
836 return output_field_add(hpp_list, name);
837 }
Jiri Olsac75540e2016-09-22 17:36:41 +0200838
839 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
840 return 0;
841}
842
843static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
844{
845 struct c2c_fmt *c2c_fmt = get_format(name);
846
Jiri Olsa5f2eca82016-09-22 17:36:43 +0200847 if (!c2c_fmt) {
848 reset_dimensions();
849 return sort_dimension__add(hpp_list, name, NULL, 0);
850 }
Jiri Olsac75540e2016-09-22 17:36:41 +0200851
852 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
853 return 0;
854}
855
856#define PARSE_LIST(_list, _fn) \
857 do { \
858 char *tmp, *tok; \
859 ret = 0; \
860 \
861 if (!_list) \
862 break; \
863 \
864 for (tok = strtok_r((char *)_list, ", ", &tmp); \
865 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
866 ret = _fn(hpp_list, tok); \
867 if (ret == -EINVAL) { \
868 error("Invalid --fields key: `%s'", tok); \
869 break; \
870 } else if (ret == -ESRCH) { \
871 error("Unknown --fields key: `%s'", tok); \
872 break; \
873 } \
874 } \
875 } while (0)
876
877static int hpp_list__parse(struct perf_hpp_list *hpp_list,
878 const char *output_,
879 const char *sort_)
880{
881 char *output = output_ ? strdup(output_) : NULL;
882 char *sort = sort_ ? strdup(sort_) : NULL;
883 int ret;
884
885 PARSE_LIST(output, c2c_hists__init_output);
886 PARSE_LIST(sort, c2c_hists__init_sort);
887
888 /* copy sort keys to output fields */
889 perf_hpp__setup_output_field(hpp_list);
890
891 /*
892 * We dont need other sorting keys other than those
893 * we already specified. It also really slows down
894 * the processing a lot with big number of output
895 * fields, so switching this off for c2c.
896 */
897
898#if 0
899 /* and then copy output fields to sort keys */
900 perf_hpp__append_sort_keys(&hists->list);
901#endif
902
903 free(output);
904 free(sort);
905 return ret;
906}
907
908static int c2c_hists__init(struct c2c_hists *hists,
909 const char *sort)
910{
911 __hists__init(&hists->hists, &hists->list);
912
913 /*
914 * Initialize only with sort fields, we need to resort
915 * later anyway, and that's where we add output fields
916 * as well.
917 */
918 perf_hpp_list__init(&hists->list);
919
920 return hpp_list__parse(&hists->list, NULL, sort);
921}
922
923__maybe_unused
924static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
925 const char *output,
926 const char *sort)
927{
928 perf_hpp__reset_output_field(&c2c_hists->list);
929 return hpp_list__parse(&c2c_hists->list, output, sort);
930}
931
Jiri Olsaec06f9b2016-09-22 17:36:45 +0200932static int filter_cb(struct hist_entry *he __maybe_unused)
933{
934 return 0;
935}
936
937static int resort_cl_cb(struct hist_entry *he)
938{
939 struct c2c_hist_entry *c2c_he;
940 struct c2c_hists *c2c_hists;
941
942 c2c_he = container_of(he, struct c2c_hist_entry, he);
943 c2c_hists = c2c_he->hists;
944
945 if (c2c_hists) {
946 hists__collapse_resort(&c2c_hists->hists, NULL);
947 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
948 }
949
950 return 0;
951}
952
Jiri Olsa903a6f12016-09-22 17:36:40 +0200953static int perf_c2c__report(int argc, const char **argv)
954{
955 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +0200956 struct ui_progress prog;
Jiri Olsa903a6f12016-09-22 17:36:40 +0200957 struct perf_data_file file = {
958 .mode = PERF_DATA_MODE_READ,
959 };
960 const struct option c2c_options[] = {
961 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
962 "file", "vmlinux pathname"),
963 OPT_INCR('v', "verbose", &verbose,
964 "be more verbose (show counter open errors, etc)"),
965 OPT_STRING('i', "input", &input_name, "file",
966 "the input file to process"),
967 OPT_END()
968 };
969 int err = 0;
970
971 argc = parse_options(argc, argv, c2c_options, report_c2c_usage,
972 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +0200973 if (argc)
Jiri Olsa903a6f12016-09-22 17:36:40 +0200974 usage_with_options(report_c2c_usage, c2c_options);
975
Jiri Olsa78b27542016-09-22 17:36:44 +0200976 if (!input_name || !strlen(input_name))
977 input_name = "perf.data";
978
Jiri Olsa903a6f12016-09-22 17:36:40 +0200979 file.path = input_name;
980
Jiri Olsac75540e2016-09-22 17:36:41 +0200981 err = c2c_hists__init(&c2c.hists, "dcacheline");
982 if (err) {
983 pr_debug("Failed to initialize hists\n");
984 goto out;
985 }
986
Jiri Olsa903a6f12016-09-22 17:36:40 +0200987 session = perf_session__new(&file, 0, &c2c.tool);
988 if (session == NULL) {
989 pr_debug("No memory for session\n");
990 goto out;
991 }
992
993 if (symbol__init(&session->header.env) < 0)
994 goto out_session;
995
996 /* No pipe support at the moment. */
997 if (perf_data_file__is_pipe(session->file)) {
998 pr_debug("No pipe support at the moment.\n");
999 goto out_session;
1000 }
1001
Jiri Olsa78b27542016-09-22 17:36:44 +02001002 err = perf_session__process_events(session);
1003 if (err) {
1004 pr_err("failed to process sample\n");
1005 goto out_session;
1006 }
1007
1008 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
1009
1010 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001011 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +02001012
1013 ui_progress__finish();
1014
Jiri Olsa903a6f12016-09-22 17:36:40 +02001015out_session:
1016 perf_session__delete(session);
1017out:
1018 return err;
1019}
1020
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001021static int parse_record_events(const struct option *opt __maybe_unused,
1022 const char *str, int unset __maybe_unused)
1023{
1024 bool *event_set = (bool *) opt->value;
1025
1026 *event_set = true;
1027 return perf_mem_events__parse(str);
1028}
1029
1030
1031static const char * const __usage_record[] = {
1032 "perf c2c record [<options>] [<command>]",
1033 "perf c2c record [<options>] -- <command> [<options>]",
1034 NULL
1035};
1036
1037static const char * const *record_mem_usage = __usage_record;
1038
1039static int perf_c2c__record(int argc, const char **argv)
1040{
1041 int rec_argc, i = 0, j;
1042 const char **rec_argv;
1043 int ret;
1044 bool all_user = false, all_kernel = false;
1045 bool event_set = false;
1046 struct option options[] = {
1047 OPT_CALLBACK('e', "event", &event_set, "event",
1048 "event selector. Use 'perf mem record -e list' to list available events",
1049 parse_record_events),
1050 OPT_INCR('v', "verbose", &verbose,
1051 "be more verbose (show counter open errors, etc)"),
1052 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
1053 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
1054 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
1055 OPT_END()
1056 };
1057
1058 if (perf_mem_events__init()) {
1059 pr_err("failed: memory events not supported\n");
1060 return -1;
1061 }
1062
1063 argc = parse_options(argc, argv, options, record_mem_usage,
1064 PARSE_OPT_KEEP_UNKNOWN);
1065
1066 rec_argc = argc + 10; /* max number of arguments */
1067 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1068 if (!rec_argv)
1069 return -1;
1070
1071 rec_argv[i++] = "record";
1072
1073 if (!event_set) {
1074 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
1075 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
1076 }
1077
1078 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
1079 rec_argv[i++] = "-W";
1080
1081 rec_argv[i++] = "-d";
1082 rec_argv[i++] = "--sample-cpu";
1083
1084 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
1085 if (!perf_mem_events[j].record)
1086 continue;
1087
1088 if (!perf_mem_events[j].supported) {
1089 pr_err("failed: event '%s' not supported\n",
1090 perf_mem_events[j].name);
1091 return -1;
1092 }
1093
1094 rec_argv[i++] = "-e";
1095 rec_argv[i++] = perf_mem_events__name(j);
1096 };
1097
1098 if (all_user)
1099 rec_argv[i++] = "--all-user";
1100
1101 if (all_kernel)
1102 rec_argv[i++] = "--all-kernel";
1103
1104 for (j = 0; j < argc; j++, i++)
1105 rec_argv[i] = argv[j];
1106
1107 if (verbose > 0) {
1108 pr_debug("calling: ");
1109
1110 j = 0;
1111
1112 while (rec_argv[j]) {
1113 pr_debug("%s ", rec_argv[j]);
1114 j++;
1115 }
1116 pr_debug("\n");
1117 }
1118
1119 ret = cmd_record(i, rec_argv, NULL);
1120 free(rec_argv);
1121 return ret;
1122}
1123
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02001124int cmd_c2c(int argc, const char **argv, const char *prefix __maybe_unused)
1125{
1126 const struct option c2c_options[] = {
1127 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1128 OPT_END()
1129 };
1130
1131 argc = parse_options(argc, argv, c2c_options, c2c_usage,
1132 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001133
1134 if (!argc)
1135 usage_with_options(c2c_usage, c2c_options);
1136
1137 if (!strncmp(argv[0], "rec", 3)) {
1138 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02001139 } else if (!strncmp(argv[0], "rep", 3)) {
1140 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001141 } else {
1142 usage_with_options(c2c_usage, c2c_options);
1143 }
1144
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02001145 return 0;
1146}