blob: ffd41744886e4d55b730b0ebc06c2b91d5c12d62 [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 Olsa55177c42016-05-19 09:52:37 +0200517static uint64_t total_loads(struct c2c_stats *stats)
518{
519 uint64_t lclmiss, ldcnt;
520
521 lclmiss = stats->lcl_dram +
522 stats->rmt_dram +
523 stats->rmt_hitm +
524 stats->rmt_hit;
525
526 ldcnt = lclmiss +
527 stats->ld_fbhit +
528 stats->ld_l1hit +
529 stats->ld_l2hit +
530 stats->ld_llchit +
531 stats->lcl_hitm;
532
533 return ldcnt;
534}
535
536static int
537tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
538 struct hist_entry *he)
539{
540 struct c2c_hist_entry *c2c_he;
541 int width = c2c_width(fmt, hpp, he->hists);
542 uint64_t tot_recs;
543
544 c2c_he = container_of(he, struct c2c_hist_entry, he);
545 tot_recs = total_loads(&c2c_he->stats);
546
547 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
548}
549
550static int64_t
551tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
552 struct hist_entry *left, struct hist_entry *right)
553{
554 struct c2c_hist_entry *c2c_left;
555 struct c2c_hist_entry *c2c_right;
556 uint64_t tot_recs_left;
557 uint64_t tot_recs_right;
558
559 c2c_left = container_of(left, struct c2c_hist_entry, he);
560 c2c_right = container_of(right, struct c2c_hist_entry, he);
561
562 tot_recs_left = total_loads(&c2c_left->stats);
563 tot_recs_right = total_loads(&c2c_right->stats);
564
565 return tot_recs_left - tot_recs_right;
566}
567
Jiri Olsaf0c50c12016-05-04 10:50:09 +0200568typedef double (get_percent_cb)(struct c2c_hist_entry *);
569
570static int
571percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
572 struct hist_entry *he, get_percent_cb get_percent)
573{
574 struct c2c_hist_entry *c2c_he;
575 int width = c2c_width(fmt, hpp, he->hists);
576 double per;
577
578 c2c_he = container_of(he, struct c2c_hist_entry, he);
579 per = get_percent(c2c_he);
580
581 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
582}
583
584static double percent_hitm(struct c2c_hist_entry *c2c_he)
585{
586 struct c2c_hists *hists;
587 struct c2c_stats *stats;
588 struct c2c_stats *total;
589 int tot, st;
590 double p;
591
592 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
593 stats = &c2c_he->stats;
594 total = &hists->stats;
595
596 st = stats->rmt_hitm;
597 tot = total->rmt_hitm;
598
599 p = tot ? (double) st / tot : 0;
600
601 return 100 * p;
602}
603
604#define PERC_STR(__s, __v) \
605({ \
606 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
607 __s; \
608})
609
610static int
611percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
612 struct hist_entry *he)
613{
614 struct c2c_hist_entry *c2c_he;
615 int width = c2c_width(fmt, hpp, he->hists);
616 char buf[10];
617 double per;
618
619 c2c_he = container_of(he, struct c2c_hist_entry, he);
620 per = percent_hitm(c2c_he);
621 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
622}
623
624static int
625percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
626 struct hist_entry *he)
627{
628 return percent_color(fmt, hpp, he, percent_hitm);
629}
630
631static int64_t
632percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
633 struct hist_entry *left, struct hist_entry *right)
634{
635 struct c2c_hist_entry *c2c_left;
636 struct c2c_hist_entry *c2c_right;
637 double per_left;
638 double per_right;
639
640 c2c_left = container_of(left, struct c2c_hist_entry, he);
641 c2c_right = container_of(right, struct c2c_hist_entry, he);
642
643 per_left = percent_hitm(c2c_left);
644 per_right = percent_hitm(c2c_right);
645
646 return per_left - per_right;
647}
648
Jiri Olsa9cb35002016-05-04 12:16:50 +0200649static struct c2c_stats *he_stats(struct hist_entry *he)
650{
651 struct c2c_hist_entry *c2c_he;
652
653 c2c_he = container_of(he, struct c2c_hist_entry, he);
654 return &c2c_he->stats;
655}
656
657static struct c2c_stats *total_stats(struct hist_entry *he)
658{
659 struct c2c_hists *hists;
660
661 hists = container_of(he->hists, struct c2c_hists, hists);
662 return &hists->stats;
663}
664
665static double percent(int st, int tot)
666{
667 return tot ? 100. * (double) st / (double) tot : 0;
668}
669
670#define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
671
672#define PERCENT_FN(__f) \
673static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
674{ \
675 struct c2c_hists *hists; \
676 \
677 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
678 return percent(c2c_he->stats.__f, hists->stats.__f); \
679}
680
681PERCENT_FN(rmt_hitm)
682PERCENT_FN(lcl_hitm)
683PERCENT_FN(st_l1hit)
684PERCENT_FN(st_l1miss)
685
686static int
687percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
688 struct hist_entry *he)
689{
690 int width = c2c_width(fmt, hpp, he->hists);
691 double per = PERCENT(he, rmt_hitm);
692 char buf[10];
693
694 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
695}
696
697static int
698percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
699 struct hist_entry *he)
700{
701 return percent_color(fmt, hpp, he, percent_rmt_hitm);
702}
703
704static int64_t
705percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
706 struct hist_entry *left, struct hist_entry *right)
707{
708 double per_left;
709 double per_right;
710
711 per_left = PERCENT(left, lcl_hitm);
712 per_right = PERCENT(right, lcl_hitm);
713
714 return per_left - per_right;
715}
716
717static int
718percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
719 struct hist_entry *he)
720{
721 int width = c2c_width(fmt, hpp, he->hists);
722 double per = PERCENT(he, lcl_hitm);
723 char buf[10];
724
725 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
726}
727
728static int
729percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
730 struct hist_entry *he)
731{
732 return percent_color(fmt, hpp, he, percent_lcl_hitm);
733}
734
735static int64_t
736percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
737 struct hist_entry *left, struct hist_entry *right)
738{
739 double per_left;
740 double per_right;
741
742 per_left = PERCENT(left, lcl_hitm);
743 per_right = PERCENT(right, lcl_hitm);
744
745 return per_left - per_right;
746}
747
748static int
749percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
750 struct hist_entry *he)
751{
752 int width = c2c_width(fmt, hpp, he->hists);
753 double per = PERCENT(he, st_l1hit);
754 char buf[10];
755
756 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
757}
758
759static int
760percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
761 struct hist_entry *he)
762{
763 return percent_color(fmt, hpp, he, percent_st_l1hit);
764}
765
766static int64_t
767percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
768 struct hist_entry *left, struct hist_entry *right)
769{
770 double per_left;
771 double per_right;
772
773 per_left = PERCENT(left, st_l1hit);
774 per_right = PERCENT(right, st_l1hit);
775
776 return per_left - per_right;
777}
778
779static int
780percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
781 struct hist_entry *he)
782{
783 int width = c2c_width(fmt, hpp, he->hists);
784 double per = PERCENT(he, st_l1miss);
785 char buf[10];
786
787 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
788}
789
790static int
791percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
792 struct hist_entry *he)
793{
794 return percent_color(fmt, hpp, he, percent_st_l1miss);
795}
796
797static int64_t
798percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
799 struct hist_entry *left, struct hist_entry *right)
800{
801 double per_left;
802 double per_right;
803
804 per_left = PERCENT(left, st_l1miss);
805 per_right = PERCENT(right, st_l1miss);
806
807 return per_left - per_right;
808}
809
Jiri Olsa6c70f542016-05-28 12:30:13 +0200810STAT_FN(lcl_dram)
811STAT_FN(rmt_dram)
812
Jiri Olsa36d3deb2016-05-24 13:09:47 +0200813static int
814pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
815 struct hist_entry *he)
816{
817 int width = c2c_width(fmt, hpp, he->hists);
818
819 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
820}
821
822static int64_t
823pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
824 struct hist_entry *left, struct hist_entry *right)
825{
826 return left->thread->pid_ - right->thread->pid_;
827}
828
Jiri Olsa600a8cf2016-09-22 17:36:47 +0200829#define HEADER_LOW(__h) \
830 { \
831 .line[1] = { \
832 .text = __h, \
833 }, \
834 }
835
836#define HEADER_BOTH(__h0, __h1) \
837 { \
838 .line[0] = { \
839 .text = __h0, \
840 }, \
841 .line[1] = { \
842 .text = __h1, \
843 }, \
844 }
845
846#define HEADER_SPAN(__h0, __h1, __s) \
847 { \
848 .line[0] = { \
849 .text = __h0, \
850 .span = __s, \
851 }, \
852 .line[1] = { \
853 .text = __h1, \
854 }, \
855 }
856
857#define HEADER_SPAN_LOW(__h) \
858 { \
859 .line[1] = { \
860 .text = __h, \
861 }, \
862 }
863
Jiri Olsacbb88502016-09-22 17:36:48 +0200864static struct c2c_dimension dim_dcacheline = {
865 .header = HEADER_LOW("Cacheline"),
866 .name = "dcacheline",
867 .cmp = dcacheline_cmp,
868 .entry = dcacheline_entry,
869 .width = 18,
870};
871
Jiri Olsa48acdeb2016-04-29 14:37:06 +0200872static struct c2c_dimension dim_offset = {
873 .header = HEADER_BOTH("Data address", "Offset"),
874 .name = "offset",
875 .cmp = offset_cmp,
876 .entry = offset_entry,
877 .width = 18,
878};
879
Jiri Olsa43575a92016-05-03 21:48:56 +0200880static struct c2c_dimension dim_iaddr = {
881 .header = HEADER_LOW("Code address"),
882 .name = "iaddr",
883 .cmp = iaddr_cmp,
884 .entry = iaddr_entry,
885 .width = 18,
886};
887
Jiri Olsa97cb4862016-05-23 16:20:14 +0200888static struct c2c_dimension dim_tot_hitm = {
889 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2),
890 .name = "tot_hitm",
891 .cmp = tot_hitm_cmp,
892 .entry = tot_hitm_entry,
893 .width = 7,
894};
895
896static struct c2c_dimension dim_lcl_hitm = {
897 .header = HEADER_SPAN_LOW("Lcl"),
898 .name = "lcl_hitm",
899 .cmp = lcl_hitm_cmp,
900 .entry = lcl_hitm_entry,
901 .width = 7,
902};
903
904static struct c2c_dimension dim_rmt_hitm = {
905 .header = HEADER_SPAN_LOW("Rmt"),
906 .name = "rmt_hitm",
907 .cmp = rmt_hitm_cmp,
908 .entry = rmt_hitm_entry,
909 .width = 7,
910};
911
912static struct c2c_dimension dim_cl_rmt_hitm = {
913 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
914 .name = "cl_rmt_hitm",
915 .cmp = rmt_hitm_cmp,
916 .entry = rmt_hitm_entry,
917 .width = 7,
918};
919
920static struct c2c_dimension dim_cl_lcl_hitm = {
921 .header = HEADER_SPAN_LOW("Lcl"),
922 .name = "cl_lcl_hitm",
923 .cmp = lcl_hitm_cmp,
924 .entry = lcl_hitm_entry,
925 .width = 7,
926};
927
Jiri Olsa0f188962016-05-04 10:10:11 +0200928static struct c2c_dimension dim_stores = {
929 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2),
930 .name = "stores",
931 .cmp = store_cmp,
932 .entry = store_entry,
933 .width = 7,
934};
935
936static struct c2c_dimension dim_stores_l1hit = {
937 .header = HEADER_SPAN_LOW("L1Hit"),
938 .name = "stores_l1hit",
939 .cmp = st_l1hit_cmp,
940 .entry = st_l1hit_entry,
941 .width = 7,
942};
943
944static struct c2c_dimension dim_stores_l1miss = {
945 .header = HEADER_SPAN_LOW("L1Miss"),
946 .name = "stores_l1miss",
947 .cmp = st_l1miss_cmp,
948 .entry = st_l1miss_entry,
949 .width = 7,
950};
951
952static struct c2c_dimension dim_cl_stores_l1hit = {
953 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
954 .name = "cl_stores_l1hit",
955 .cmp = st_l1hit_cmp,
956 .entry = st_l1hit_entry,
957 .width = 7,
958};
959
960static struct c2c_dimension dim_cl_stores_l1miss = {
961 .header = HEADER_SPAN_LOW("L1 Miss"),
962 .name = "cl_stores_l1miss",
963 .cmp = st_l1miss_cmp,
964 .entry = st_l1miss_entry,
965 .width = 7,
966};
967
Jiri Olsa1295f682016-05-04 10:18:24 +0200968static struct c2c_dimension dim_ld_fbhit = {
969 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
970 .name = "ld_fbhit",
971 .cmp = ld_fbhit_cmp,
972 .entry = ld_fbhit_entry,
973 .width = 7,
974};
975
976static struct c2c_dimension dim_ld_l1hit = {
977 .header = HEADER_SPAN_LOW("L1"),
978 .name = "ld_l1hit",
979 .cmp = ld_l1hit_cmp,
980 .entry = ld_l1hit_entry,
981 .width = 7,
982};
983
984static struct c2c_dimension dim_ld_l2hit = {
985 .header = HEADER_SPAN_LOW("L2"),
986 .name = "ld_l2hit",
987 .cmp = ld_l2hit_cmp,
988 .entry = ld_l2hit_entry,
989 .width = 7,
990};
991
Jiri Olsa4d089102016-05-04 10:27:51 +0200992static struct c2c_dimension dim_ld_llchit = {
993 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1),
994 .name = "ld_lclhit",
995 .cmp = ld_llchit_cmp,
996 .entry = ld_llchit_entry,
997 .width = 8,
998};
999
1000static struct c2c_dimension dim_ld_rmthit = {
1001 .header = HEADER_SPAN_LOW("Rmt"),
1002 .name = "ld_rmthit",
1003 .cmp = rmt_hit_cmp,
1004 .entry = rmt_hit_entry,
1005 .width = 8,
1006};
1007
Jiri Olsa04402d22016-05-19 10:10:51 +02001008static struct c2c_dimension dim_ld_llcmiss = {
1009 .header = HEADER_BOTH("LLC", "Ld Miss"),
1010 .name = "ld_llcmiss",
1011 .cmp = ld_llcmiss_cmp,
1012 .entry = ld_llcmiss_entry,
1013 .width = 7,
1014};
1015
Jiri Olsa01b84d72016-05-04 10:35:29 +02001016static struct c2c_dimension dim_tot_recs = {
1017 .header = HEADER_BOTH("Total", "records"),
1018 .name = "tot_recs",
1019 .cmp = tot_recs_cmp,
1020 .entry = tot_recs_entry,
1021 .width = 7,
1022};
1023
Jiri Olsa55177c42016-05-19 09:52:37 +02001024static struct c2c_dimension dim_tot_loads = {
1025 .header = HEADER_BOTH("Total", "Loads"),
1026 .name = "tot_loads",
1027 .cmp = tot_loads_cmp,
1028 .entry = tot_loads_entry,
1029 .width = 7,
1030};
1031
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001032static struct c2c_dimension dim_percent_hitm = {
1033 .header = HEADER_LOW("%hitm"),
1034 .name = "percent_hitm",
1035 .cmp = percent_hitm_cmp,
1036 .entry = percent_hitm_entry,
1037 .color = percent_hitm_color,
1038 .width = 7,
1039};
1040
Jiri Olsa9cb35002016-05-04 12:16:50 +02001041static struct c2c_dimension dim_percent_rmt_hitm = {
1042 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
1043 .name = "percent_rmt_hitm",
1044 .cmp = percent_rmt_hitm_cmp,
1045 .entry = percent_rmt_hitm_entry,
1046 .color = percent_rmt_hitm_color,
1047 .width = 7,
1048};
1049
1050static struct c2c_dimension dim_percent_lcl_hitm = {
1051 .header = HEADER_SPAN_LOW("Lcl"),
1052 .name = "percent_lcl_hitm",
1053 .cmp = percent_lcl_hitm_cmp,
1054 .entry = percent_lcl_hitm_entry,
1055 .color = percent_lcl_hitm_color,
1056 .width = 7,
1057};
1058
1059static struct c2c_dimension dim_percent_stores_l1hit = {
1060 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
1061 .name = "percent_stores_l1hit",
1062 .cmp = percent_stores_l1hit_cmp,
1063 .entry = percent_stores_l1hit_entry,
1064 .color = percent_stores_l1hit_color,
1065 .width = 7,
1066};
1067
1068static struct c2c_dimension dim_percent_stores_l1miss = {
1069 .header = HEADER_SPAN_LOW("L1 Miss"),
1070 .name = "percent_stores_l1miss",
1071 .cmp = percent_stores_l1miss_cmp,
1072 .entry = percent_stores_l1miss_entry,
1073 .color = percent_stores_l1miss_color,
1074 .width = 7,
1075};
1076
Jiri Olsa6c70f542016-05-28 12:30:13 +02001077static struct c2c_dimension dim_dram_lcl = {
1078 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
1079 .name = "dram_lcl",
1080 .cmp = lcl_dram_cmp,
1081 .entry = lcl_dram_entry,
1082 .width = 8,
1083};
1084
1085static struct c2c_dimension dim_dram_rmt = {
1086 .header = HEADER_SPAN_LOW("Rmt"),
1087 .name = "dram_rmt",
1088 .cmp = rmt_dram_cmp,
1089 .entry = rmt_dram_entry,
1090 .width = 8,
1091};
1092
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001093static struct c2c_dimension dim_pid = {
1094 .header = HEADER_LOW("Pid"),
1095 .name = "pid",
1096 .cmp = pid_cmp,
1097 .entry = pid_entry,
1098 .width = 7,
1099};
1100
Jiri Olsae87019c2016-05-25 08:50:10 +02001101static struct c2c_dimension dim_tid = {
1102 .header = HEADER_LOW("Tid"),
1103 .name = "tid",
1104 .se = &sort_thread,
1105};
1106
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001107static struct c2c_dimension dim_symbol = {
1108 .name = "symbol",
1109 .se = &sort_sym,
1110};
1111
1112static struct c2c_dimension dim_dso = {
1113 .header = HEADER_BOTH("Shared", "Object"),
1114 .name = "dso",
1115 .se = &sort_dso,
1116};
1117
Jiri Olsac75540e2016-09-22 17:36:41 +02001118static struct c2c_dimension *dimensions[] = {
Jiri Olsacbb88502016-09-22 17:36:48 +02001119 &dim_dcacheline,
Jiri Olsa48acdeb2016-04-29 14:37:06 +02001120 &dim_offset,
Jiri Olsa43575a92016-05-03 21:48:56 +02001121 &dim_iaddr,
Jiri Olsa97cb4862016-05-23 16:20:14 +02001122 &dim_tot_hitm,
1123 &dim_lcl_hitm,
1124 &dim_rmt_hitm,
1125 &dim_cl_lcl_hitm,
1126 &dim_cl_rmt_hitm,
Jiri Olsa0f188962016-05-04 10:10:11 +02001127 &dim_stores,
1128 &dim_stores_l1hit,
1129 &dim_stores_l1miss,
1130 &dim_cl_stores_l1hit,
1131 &dim_cl_stores_l1miss,
Jiri Olsa1295f682016-05-04 10:18:24 +02001132 &dim_ld_fbhit,
1133 &dim_ld_l1hit,
1134 &dim_ld_l2hit,
Jiri Olsa4d089102016-05-04 10:27:51 +02001135 &dim_ld_llchit,
1136 &dim_ld_rmthit,
Jiri Olsa04402d22016-05-19 10:10:51 +02001137 &dim_ld_llcmiss,
Jiri Olsa01b84d72016-05-04 10:35:29 +02001138 &dim_tot_recs,
Jiri Olsa55177c42016-05-19 09:52:37 +02001139 &dim_tot_loads,
Jiri Olsaf0c50c12016-05-04 10:50:09 +02001140 &dim_percent_hitm,
Jiri Olsa9cb35002016-05-04 12:16:50 +02001141 &dim_percent_rmt_hitm,
1142 &dim_percent_lcl_hitm,
1143 &dim_percent_stores_l1hit,
1144 &dim_percent_stores_l1miss,
Jiri Olsa6c70f542016-05-28 12:30:13 +02001145 &dim_dram_lcl,
1146 &dim_dram_rmt,
Jiri Olsa36d3deb2016-05-24 13:09:47 +02001147 &dim_pid,
Jiri Olsae87019c2016-05-25 08:50:10 +02001148 &dim_tid,
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001149 &dim_symbol,
1150 &dim_dso,
Jiri Olsac75540e2016-09-22 17:36:41 +02001151 NULL,
1152};
1153
1154static void fmt_free(struct perf_hpp_fmt *fmt)
1155{
1156 struct c2c_fmt *c2c_fmt;
1157
1158 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1159 free(c2c_fmt);
1160}
1161
1162static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1163{
1164 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
1165 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
1166
1167 return c2c_a->dim == c2c_b->dim;
1168}
1169
1170static struct c2c_dimension *get_dimension(const char *name)
1171{
1172 unsigned int i;
1173
1174 for (i = 0; dimensions[i]; i++) {
1175 struct c2c_dimension *dim = dimensions[i];
1176
1177 if (!strcmp(dim->name, name))
1178 return dim;
1179 };
1180
1181 return NULL;
1182}
1183
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001184static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1185 struct hist_entry *he)
1186{
1187 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1188 struct c2c_dimension *dim = c2c_fmt->dim;
1189 size_t len = fmt->user_len;
1190
1191 if (!len)
1192 len = hists__col_len(he->hists, dim->se->se_width_idx);
1193
1194 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
1195}
1196
1197static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
1198 struct hist_entry *a, struct hist_entry *b)
1199{
1200 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1201 struct c2c_dimension *dim = c2c_fmt->dim;
1202
1203 return dim->se->se_cmp(a, b);
1204}
1205
1206static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
1207 struct hist_entry *a, struct hist_entry *b)
1208{
1209 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
1210 struct c2c_dimension *dim = c2c_fmt->dim;
1211 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1212
1213 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
1214 return collapse_fn(a, b);
1215}
1216
Jiri Olsac75540e2016-09-22 17:36:41 +02001217static struct c2c_fmt *get_format(const char *name)
1218{
1219 struct c2c_dimension *dim = get_dimension(name);
1220 struct c2c_fmt *c2c_fmt;
1221 struct perf_hpp_fmt *fmt;
1222
1223 if (!dim)
1224 return NULL;
1225
1226 c2c_fmt = zalloc(sizeof(*c2c_fmt));
1227 if (!c2c_fmt)
1228 return NULL;
1229
1230 c2c_fmt->dim = dim;
1231
1232 fmt = &c2c_fmt->fmt;
1233 INIT_LIST_HEAD(&fmt->list);
1234 INIT_LIST_HEAD(&fmt->sort_list);
1235
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001236 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
1237 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
Jiri Olsa9cb35002016-05-04 12:16:50 +02001238 fmt->color = dim->se ? NULL : dim->color;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001239 fmt->entry = dim->se ? c2c_se_entry : dim->entry;
Jiri Olsac75540e2016-09-22 17:36:41 +02001240 fmt->header = c2c_header;
1241 fmt->width = c2c_width;
Jiri Olsa8d3f9382016-09-22 17:36:42 +02001242 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
Jiri Olsac75540e2016-09-22 17:36:41 +02001243 fmt->equal = fmt_equal;
1244 fmt->free = fmt_free;
1245
1246 return c2c_fmt;
1247}
1248
1249static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
1250{
1251 struct c2c_fmt *c2c_fmt = get_format(name);
1252
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001253 if (!c2c_fmt) {
1254 reset_dimensions();
1255 return output_field_add(hpp_list, name);
1256 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001257
1258 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
1259 return 0;
1260}
1261
1262static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
1263{
1264 struct c2c_fmt *c2c_fmt = get_format(name);
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001265 struct c2c_dimension *dim;
Jiri Olsac75540e2016-09-22 17:36:41 +02001266
Jiri Olsa5f2eca82016-09-22 17:36:43 +02001267 if (!c2c_fmt) {
1268 reset_dimensions();
1269 return sort_dimension__add(hpp_list, name, NULL, 0);
1270 }
Jiri Olsac75540e2016-09-22 17:36:41 +02001271
Jiri Olsa51dedaa2016-05-24 23:41:52 +02001272 dim = c2c_fmt->dim;
1273 if (dim == &dim_dso)
1274 hpp_list->dso = 1;
1275
Jiri Olsac75540e2016-09-22 17:36:41 +02001276 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
1277 return 0;
1278}
1279
1280#define PARSE_LIST(_list, _fn) \
1281 do { \
1282 char *tmp, *tok; \
1283 ret = 0; \
1284 \
1285 if (!_list) \
1286 break; \
1287 \
1288 for (tok = strtok_r((char *)_list, ", ", &tmp); \
1289 tok; tok = strtok_r(NULL, ", ", &tmp)) { \
1290 ret = _fn(hpp_list, tok); \
1291 if (ret == -EINVAL) { \
1292 error("Invalid --fields key: `%s'", tok); \
1293 break; \
1294 } else if (ret == -ESRCH) { \
1295 error("Unknown --fields key: `%s'", tok); \
1296 break; \
1297 } \
1298 } \
1299 } while (0)
1300
1301static int hpp_list__parse(struct perf_hpp_list *hpp_list,
1302 const char *output_,
1303 const char *sort_)
1304{
1305 char *output = output_ ? strdup(output_) : NULL;
1306 char *sort = sort_ ? strdup(sort_) : NULL;
1307 int ret;
1308
1309 PARSE_LIST(output, c2c_hists__init_output);
1310 PARSE_LIST(sort, c2c_hists__init_sort);
1311
1312 /* copy sort keys to output fields */
1313 perf_hpp__setup_output_field(hpp_list);
1314
1315 /*
1316 * We dont need other sorting keys other than those
1317 * we already specified. It also really slows down
1318 * the processing a lot with big number of output
1319 * fields, so switching this off for c2c.
1320 */
1321
1322#if 0
1323 /* and then copy output fields to sort keys */
1324 perf_hpp__append_sort_keys(&hists->list);
1325#endif
1326
1327 free(output);
1328 free(sort);
1329 return ret;
1330}
1331
1332static int c2c_hists__init(struct c2c_hists *hists,
1333 const char *sort)
1334{
1335 __hists__init(&hists->hists, &hists->list);
1336
1337 /*
1338 * Initialize only with sort fields, we need to resort
1339 * later anyway, and that's where we add output fields
1340 * as well.
1341 */
1342 perf_hpp_list__init(&hists->list);
1343
1344 return hpp_list__parse(&hists->list, NULL, sort);
1345}
1346
1347__maybe_unused
1348static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
1349 const char *output,
1350 const char *sort)
1351{
1352 perf_hpp__reset_output_field(&c2c_hists->list);
1353 return hpp_list__parse(&c2c_hists->list, output, sort);
1354}
1355
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001356static int filter_cb(struct hist_entry *he __maybe_unused)
1357{
1358 return 0;
1359}
1360
1361static int resort_cl_cb(struct hist_entry *he)
1362{
1363 struct c2c_hist_entry *c2c_he;
1364 struct c2c_hists *c2c_hists;
1365
1366 c2c_he = container_of(he, struct c2c_hist_entry, he);
1367 c2c_hists = c2c_he->hists;
1368
1369 if (c2c_hists) {
1370 hists__collapse_resort(&c2c_hists->hists, NULL);
1371 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
1372 }
1373
1374 return 0;
1375}
1376
Jiri Olsa903a6f12016-09-22 17:36:40 +02001377static int perf_c2c__report(int argc, const char **argv)
1378{
1379 struct perf_session *session;
Jiri Olsa78b27542016-09-22 17:36:44 +02001380 struct ui_progress prog;
Jiri Olsa903a6f12016-09-22 17:36:40 +02001381 struct perf_data_file file = {
1382 .mode = PERF_DATA_MODE_READ,
1383 };
1384 const struct option c2c_options[] = {
1385 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1386 "file", "vmlinux pathname"),
1387 OPT_INCR('v', "verbose", &verbose,
1388 "be more verbose (show counter open errors, etc)"),
1389 OPT_STRING('i', "input", &input_name, "file",
1390 "the input file to process"),
1391 OPT_END()
1392 };
1393 int err = 0;
1394
1395 argc = parse_options(argc, argv, c2c_options, report_c2c_usage,
1396 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa78b27542016-09-22 17:36:44 +02001397 if (argc)
Jiri Olsa903a6f12016-09-22 17:36:40 +02001398 usage_with_options(report_c2c_usage, c2c_options);
1399
Jiri Olsa78b27542016-09-22 17:36:44 +02001400 if (!input_name || !strlen(input_name))
1401 input_name = "perf.data";
1402
Jiri Olsa903a6f12016-09-22 17:36:40 +02001403 file.path = input_name;
1404
Jiri Olsac75540e2016-09-22 17:36:41 +02001405 err = c2c_hists__init(&c2c.hists, "dcacheline");
1406 if (err) {
1407 pr_debug("Failed to initialize hists\n");
1408 goto out;
1409 }
1410
Jiri Olsa903a6f12016-09-22 17:36:40 +02001411 session = perf_session__new(&file, 0, &c2c.tool);
1412 if (session == NULL) {
1413 pr_debug("No memory for session\n");
1414 goto out;
1415 }
1416
1417 if (symbol__init(&session->header.env) < 0)
1418 goto out_session;
1419
1420 /* No pipe support at the moment. */
1421 if (perf_data_file__is_pipe(session->file)) {
1422 pr_debug("No pipe support at the moment.\n");
1423 goto out_session;
1424 }
1425
Jiri Olsa78b27542016-09-22 17:36:44 +02001426 err = perf_session__process_events(session);
1427 if (err) {
1428 pr_err("failed to process sample\n");
1429 goto out_session;
1430 }
1431
1432 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
1433
1434 hists__collapse_resort(&c2c.hists.hists, NULL);
Jiri Olsaec06f9b2016-09-22 17:36:45 +02001435 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_cl_cb);
Jiri Olsa78b27542016-09-22 17:36:44 +02001436
1437 ui_progress__finish();
1438
Jiri Olsa903a6f12016-09-22 17:36:40 +02001439out_session:
1440 perf_session__delete(session);
1441out:
1442 return err;
1443}
1444
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001445static int parse_record_events(const struct option *opt __maybe_unused,
1446 const char *str, int unset __maybe_unused)
1447{
1448 bool *event_set = (bool *) opt->value;
1449
1450 *event_set = true;
1451 return perf_mem_events__parse(str);
1452}
1453
1454
1455static const char * const __usage_record[] = {
1456 "perf c2c record [<options>] [<command>]",
1457 "perf c2c record [<options>] -- <command> [<options>]",
1458 NULL
1459};
1460
1461static const char * const *record_mem_usage = __usage_record;
1462
1463static int perf_c2c__record(int argc, const char **argv)
1464{
1465 int rec_argc, i = 0, j;
1466 const char **rec_argv;
1467 int ret;
1468 bool all_user = false, all_kernel = false;
1469 bool event_set = false;
1470 struct option options[] = {
1471 OPT_CALLBACK('e', "event", &event_set, "event",
1472 "event selector. Use 'perf mem record -e list' to list available events",
1473 parse_record_events),
1474 OPT_INCR('v', "verbose", &verbose,
1475 "be more verbose (show counter open errors, etc)"),
1476 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
1477 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
1478 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
1479 OPT_END()
1480 };
1481
1482 if (perf_mem_events__init()) {
1483 pr_err("failed: memory events not supported\n");
1484 return -1;
1485 }
1486
1487 argc = parse_options(argc, argv, options, record_mem_usage,
1488 PARSE_OPT_KEEP_UNKNOWN);
1489
1490 rec_argc = argc + 10; /* max number of arguments */
1491 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1492 if (!rec_argv)
1493 return -1;
1494
1495 rec_argv[i++] = "record";
1496
1497 if (!event_set) {
1498 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
1499 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
1500 }
1501
1502 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
1503 rec_argv[i++] = "-W";
1504
1505 rec_argv[i++] = "-d";
1506 rec_argv[i++] = "--sample-cpu";
1507
1508 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
1509 if (!perf_mem_events[j].record)
1510 continue;
1511
1512 if (!perf_mem_events[j].supported) {
1513 pr_err("failed: event '%s' not supported\n",
1514 perf_mem_events[j].name);
1515 return -1;
1516 }
1517
1518 rec_argv[i++] = "-e";
1519 rec_argv[i++] = perf_mem_events__name(j);
1520 };
1521
1522 if (all_user)
1523 rec_argv[i++] = "--all-user";
1524
1525 if (all_kernel)
1526 rec_argv[i++] = "--all-kernel";
1527
1528 for (j = 0; j < argc; j++, i++)
1529 rec_argv[i] = argv[j];
1530
1531 if (verbose > 0) {
1532 pr_debug("calling: ");
1533
1534 j = 0;
1535
1536 while (rec_argv[j]) {
1537 pr_debug("%s ", rec_argv[j]);
1538 j++;
1539 }
1540 pr_debug("\n");
1541 }
1542
1543 ret = cmd_record(i, rec_argv, NULL);
1544 free(rec_argv);
1545 return ret;
1546}
1547
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02001548int cmd_c2c(int argc, const char **argv, const char *prefix __maybe_unused)
1549{
1550 const struct option c2c_options[] = {
1551 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1552 OPT_END()
1553 };
1554
1555 argc = parse_options(argc, argv, c2c_options, c2c_usage,
1556 PARSE_OPT_STOP_AT_NON_OPTION);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001557
1558 if (!argc)
1559 usage_with_options(c2c_usage, c2c_options);
1560
1561 if (!strncmp(argv[0], "rec", 3)) {
1562 return perf_c2c__record(argc, argv);
Jiri Olsa903a6f12016-09-22 17:36:40 +02001563 } else if (!strncmp(argv[0], "rep", 3)) {
1564 return perf_c2c__report(argc, argv);
Jiri Olsa39bcd4a2016-09-22 17:36:39 +02001565 } else {
1566 usage_with_options(c2c_usage, c2c_options);
1567 }
1568
Jiri Olsa7aef3bf2016-09-22 17:36:38 +02001569 return 0;
1570}