blob: 3d1f6968f1751e3f8d28d12a636a79d0793b0ea5 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
11#include "asm/bug.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020012#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020013#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020014#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020015#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020016#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090017#include "target.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020018
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020019#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -020020#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020021
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030022int __perf_evsel__sample_size(u64 sample_type)
23{
24 u64 mask = sample_type & PERF_SAMPLE_MASK;
25 int size = 0;
26 int i;
27
28 for (i = 0; i < 64; i++) {
29 if (mask & (1ULL << i))
30 size++;
31 }
32
33 size *= sizeof(u64);
34
35 return size;
36}
37
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010038void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020039{
40 memset(hists, 0, sizeof(*hists));
41 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
42 hists->entries_in = &hists->entries_in_array[0];
43 hists->entries_collapsed = RB_ROOT;
44 hists->entries = RB_ROOT;
45 pthread_mutex_init(&hists->lock, NULL);
46}
47
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020048void perf_evsel__init(struct perf_evsel *evsel,
49 struct perf_event_attr *attr, int idx)
50{
51 evsel->idx = idx;
52 evsel->attr = *attr;
53 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030054 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020055}
56
Lin Ming23a2f3a2011-01-07 11:11:09 +080057struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020058{
59 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
60
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020061 if (evsel != NULL)
62 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020063
64 return evsel;
65}
66
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030067static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
68 "cycles",
69 "instructions",
70 "cache-references",
71 "cache-misses",
72 "branches",
73 "branch-misses",
74 "bus-cycles",
75 "stalled-cycles-frontend",
76 "stalled-cycles-backend",
77 "ref-cycles",
78};
79
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -030080static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030081{
82 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
83 return perf_evsel__hw_names[config];
84
85 return "unknown-hardware";
86}
87
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030088static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030089{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030090 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030091 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030092 bool exclude_guest_default = false;
93
94#define MOD_PRINT(context, mod) do { \
95 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030096 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030097 r += scnprintf(bf + r, size - r, "%c", mod); \
98 } } while(0)
99
100 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
101 MOD_PRINT(kernel, 'k');
102 MOD_PRINT(user, 'u');
103 MOD_PRINT(hv, 'h');
104 exclude_guest_default = true;
105 }
106
107 if (attr->precise_ip) {
108 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300109 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300110 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
111 exclude_guest_default = true;
112 }
113
114 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
115 MOD_PRINT(host, 'H');
116 MOD_PRINT(guest, 'G');
117 }
118#undef MOD_PRINT
119 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300120 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300121 return r;
122}
123
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300124static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
125{
126 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
127 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
128}
129
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300130static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
131 "cpu-clock",
132 "task-clock",
133 "page-faults",
134 "context-switches",
135 "CPU-migrations",
136 "minor-faults",
137 "major-faults",
138 "alignment-faults",
139 "emulation-faults",
140};
141
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300142static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300143{
144 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
145 return perf_evsel__sw_names[config];
146 return "unknown-software";
147}
148
149static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
150{
151 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
152 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
153}
154
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300155const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
156 [PERF_EVSEL__MAX_ALIASES] = {
157 { "L1-dcache", "l1-d", "l1d", "L1-data", },
158 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
159 { "LLC", "L2", },
160 { "dTLB", "d-tlb", "Data-TLB", },
161 { "iTLB", "i-tlb", "Instruction-TLB", },
162 { "branch", "branches", "bpu", "btb", "bpc", },
163 { "node", },
164};
165
166const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
167 [PERF_EVSEL__MAX_ALIASES] = {
168 { "load", "loads", "read", },
169 { "store", "stores", "write", },
170 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
171};
172
173const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
174 [PERF_EVSEL__MAX_ALIASES] = {
175 { "refs", "Reference", "ops", "access", },
176 { "misses", "miss", },
177};
178
179#define C(x) PERF_COUNT_HW_CACHE_##x
180#define CACHE_READ (1 << C(OP_READ))
181#define CACHE_WRITE (1 << C(OP_WRITE))
182#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
183#define COP(x) (1 << x)
184
185/*
186 * cache operartion stat
187 * L1I : Read and prefetch only
188 * ITLB and BPU : Read-only
189 */
190static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
191 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
192 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
193 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
194 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
195 [C(ITLB)] = (CACHE_READ),
196 [C(BPU)] = (CACHE_READ),
197 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
198};
199
200bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
201{
202 if (perf_evsel__hw_cache_stat[type] & COP(op))
203 return true; /* valid */
204 else
205 return false; /* invalid */
206}
207
208int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
209 char *bf, size_t size)
210{
211 if (result) {
212 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
213 perf_evsel__hw_cache_op[op][0],
214 perf_evsel__hw_cache_result[result][0]);
215 }
216
217 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
218 perf_evsel__hw_cache_op[op][1]);
219}
220
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300221static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300222{
223 u8 op, result, type = (config >> 0) & 0xff;
224 const char *err = "unknown-ext-hardware-cache-type";
225
226 if (type > PERF_COUNT_HW_CACHE_MAX)
227 goto out_err;
228
229 op = (config >> 8) & 0xff;
230 err = "unknown-ext-hardware-cache-op";
231 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
232 goto out_err;
233
234 result = (config >> 16) & 0xff;
235 err = "unknown-ext-hardware-cache-result";
236 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
237 goto out_err;
238
239 err = "invalid-cache";
240 if (!perf_evsel__is_cache_op_valid(type, op))
241 goto out_err;
242
243 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
244out_err:
245 return scnprintf(bf, size, "%s", err);
246}
247
248static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
249{
250 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
251 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
252}
253
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300254static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
255{
256 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
257 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
258}
259
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300260const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300261{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300262 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300263
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300264 if (evsel->name)
265 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300266
267 switch (evsel->attr.type) {
268 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300269 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300270 break;
271
272 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300273 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300274 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300275
276 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300277 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300278 break;
279
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300280 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300281 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300282 break;
283
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300284 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300285 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300286 break;
287
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300288 default:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300289 scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300290 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300291 }
292
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300293 evsel->name = strdup(bf);
294
295 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300296}
297
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900298void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
299 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200300{
301 struct perf_event_attr *attr = &evsel->attr;
302 int track = !evsel->idx; /* only the first counter needs these */
303
David Ahern5e1c81d2012-05-13 22:01:28 -0600304 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200305 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200306 attr->inherit = !opts->no_inherit;
307 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
308 PERF_FORMAT_TOTAL_TIME_RUNNING |
309 PERF_FORMAT_ID;
310
311 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
312
313 /*
314 * We default some events to a 1 default interval. But keep
315 * it a weak assumption overridable by the user.
316 */
317 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
318 opts->user_interval != ULLONG_MAX)) {
319 if (opts->freq) {
320 attr->sample_type |= PERF_SAMPLE_PERIOD;
321 attr->freq = 1;
322 attr->sample_freq = opts->freq;
323 } else {
324 attr->sample_period = opts->default_interval;
325 }
326 }
327
328 if (opts->no_samples)
329 attr->sample_freq = 0;
330
331 if (opts->inherit_stat)
332 attr->inherit_stat = 1;
333
334 if (opts->sample_address) {
335 attr->sample_type |= PERF_SAMPLE_ADDR;
336 attr->mmap_data = track;
337 }
338
339 if (opts->call_graph)
340 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
341
Namhyung Kime40ee742012-05-21 10:42:07 +0900342 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200343 attr->sample_type |= PERF_SAMPLE_CPU;
344
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300345 if (opts->period)
346 attr->sample_type |= PERF_SAMPLE_PERIOD;
347
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200348 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900349 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900350 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200351 attr->sample_type |= PERF_SAMPLE_TIME;
352
353 if (opts->raw_samples) {
354 attr->sample_type |= PERF_SAMPLE_TIME;
355 attr->sample_type |= PERF_SAMPLE_RAW;
356 attr->sample_type |= PERF_SAMPLE_CPU;
357 }
358
359 if (opts->no_delay) {
360 attr->watermark = 0;
361 attr->wakeup_events = 1;
362 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100363 if (opts->branch_stack) {
364 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
365 attr->branch_sample_type = opts->branch_stack;
366 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200367
368 attr->mmap = track;
369 attr->comm = track;
370
Namhyung Kimd67356e2012-05-07 14:09:03 +0900371 if (perf_target__none(&opts->target) &&
372 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200373 attr->enable_on_exec = 1;
374 }
375}
376
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200377int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
378{
David Ahern4af4c952011-05-27 09:58:34 -0600379 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200380 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600381
382 if (evsel->fd) {
383 for (cpu = 0; cpu < ncpus; cpu++) {
384 for (thread = 0; thread < nthreads; thread++) {
385 FD(evsel, cpu, thread) = -1;
386 }
387 }
388 }
389
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200390 return evsel->fd != NULL ? 0 : -ENOMEM;
391}
392
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200393int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
394{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300395 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
396 if (evsel->sample_id == NULL)
397 return -ENOMEM;
398
399 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
400 if (evsel->id == NULL) {
401 xyarray__delete(evsel->sample_id);
402 evsel->sample_id = NULL;
403 return -ENOMEM;
404 }
405
406 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200407}
408
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200409int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
410{
411 evsel->counts = zalloc((sizeof(*evsel->counts) +
412 (ncpus * sizeof(struct perf_counts_values))));
413 return evsel->counts != NULL ? 0 : -ENOMEM;
414}
415
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200416void perf_evsel__free_fd(struct perf_evsel *evsel)
417{
418 xyarray__delete(evsel->fd);
419 evsel->fd = NULL;
420}
421
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200422void perf_evsel__free_id(struct perf_evsel *evsel)
423{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300424 xyarray__delete(evsel->sample_id);
425 evsel->sample_id = NULL;
426 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200427 evsel->id = NULL;
428}
429
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200430void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
431{
432 int cpu, thread;
433
434 for (cpu = 0; cpu < ncpus; cpu++)
435 for (thread = 0; thread < nthreads; ++thread) {
436 close(FD(evsel, cpu, thread));
437 FD(evsel, cpu, thread) = -1;
438 }
439}
440
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200441void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200442{
443 assert(list_empty(&evsel->node));
444 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300445 xyarray__delete(evsel->sample_id);
446 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200447}
448
449void perf_evsel__delete(struct perf_evsel *evsel)
450{
451 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200452 close_cgroup(evsel->cgrp);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200453 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200454 free(evsel);
455}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200456
457int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
458 int cpu, int thread, bool scale)
459{
460 struct perf_counts_values count;
461 size_t nv = scale ? 3 : 1;
462
463 if (FD(evsel, cpu, thread) < 0)
464 return -EINVAL;
465
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200466 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
467 return -ENOMEM;
468
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200469 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
470 return -errno;
471
472 if (scale) {
473 if (count.run == 0)
474 count.val = 0;
475 else if (count.run < count.ena)
476 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
477 } else
478 count.ena = count.run = 0;
479
480 evsel->counts->cpu[cpu] = count;
481 return 0;
482}
483
484int __perf_evsel__read(struct perf_evsel *evsel,
485 int ncpus, int nthreads, bool scale)
486{
487 size_t nv = scale ? 3 : 1;
488 int cpu, thread;
489 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
490
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200491 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200492
493 for (cpu = 0; cpu < ncpus; cpu++) {
494 for (thread = 0; thread < nthreads; thread++) {
495 if (FD(evsel, cpu, thread) < 0)
496 continue;
497
498 if (readn(FD(evsel, cpu, thread),
499 &count, nv * sizeof(u64)) < 0)
500 return -errno;
501
502 aggr->val += count.val;
503 if (scale) {
504 aggr->ena += count.ena;
505 aggr->run += count.run;
506 }
507 }
508 }
509
510 evsel->counts->scaled = 0;
511 if (scale) {
512 if (aggr->run == 0) {
513 evsel->counts->scaled = -1;
514 aggr->val = 0;
515 return 0;
516 }
517
518 if (aggr->run < aggr->ena) {
519 evsel->counts->scaled = 1;
520 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
521 }
522 } else
523 aggr->ena = aggr->run = 0;
524
525 return 0;
526}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200527
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200528static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200529 struct thread_map *threads, bool group,
530 struct xyarray *group_fds)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200531{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200532 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200533 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200534 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200535
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200536 if (evsel->fd == NULL &&
537 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200538 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200539
Stephane Eranian023695d2011-02-14 11:20:01 +0200540 if (evsel->cgrp) {
541 flags = PERF_FLAG_PID_CGROUP;
542 pid = evsel->cgrp->fd;
543 }
544
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200545 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200546 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200547
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200548 for (thread = 0; thread < threads->nr; thread++) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200549
550 if (!evsel->cgrp)
551 pid = threads->map[thread];
552
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200553 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200554 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200555 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200556 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200557 if (FD(evsel, cpu, thread) < 0) {
558 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200559 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200560 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200561
562 if (group && group_fd == -1)
563 group_fd = FD(evsel, cpu, thread);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200564 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200565 }
566
567 return 0;
568
569out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200570 do {
571 while (--thread >= 0) {
572 close(FD(evsel, cpu, thread));
573 FD(evsel, cpu, thread) = -1;
574 }
575 thread = threads->nr;
576 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200577 return err;
578}
579
580void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
581{
582 if (evsel->fd == NULL)
583 return;
584
585 perf_evsel__close_fd(evsel, ncpus, nthreads);
586 perf_evsel__free_fd(evsel);
587 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200588}
589
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200590static struct {
591 struct cpu_map map;
592 int cpus[1];
593} empty_cpu_map = {
594 .map.nr = 1,
595 .cpus = { -1, },
596};
597
598static struct {
599 struct thread_map map;
600 int threads[1];
601} empty_thread_map = {
602 .map.nr = 1,
603 .threads = { -1, },
604};
605
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200606int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200607 struct thread_map *threads, bool group,
608 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200609{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200610 if (cpus == NULL) {
611 /* Work around old compiler warnings about strict aliasing */
612 cpus = &empty_cpu_map.map;
613 }
614
615 if (threads == NULL)
616 threads = &empty_thread_map.map;
617
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200618 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200619}
620
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200621int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200622 struct cpu_map *cpus, bool group,
623 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200624{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200625 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
626 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200627}
628
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200629int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200630 struct thread_map *threads, bool group,
631 struct xyarray *group_fd)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200632{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200633 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
634 group_fd);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200635}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200636
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200637static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200638 struct perf_sample *sample,
639 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200640{
641 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200642 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200643
644 array += ((event->header.size -
645 sizeof(event->header)) / sizeof(u64)) - 1;
646
647 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200648 u.val64 = *array;
649 if (swapped) {
650 /* undo swap of u64, then swap on individual u32s */
651 u.val64 = bswap_64(u.val64);
652 u.val32[0] = bswap_32(u.val32[0]);
653 }
654
655 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200656 array--;
657 }
658
659 if (type & PERF_SAMPLE_STREAM_ID) {
660 sample->stream_id = *array;
661 array--;
662 }
663
664 if (type & PERF_SAMPLE_ID) {
665 sample->id = *array;
666 array--;
667 }
668
669 if (type & PERF_SAMPLE_TIME) {
670 sample->time = *array;
671 array--;
672 }
673
674 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200675 u.val64 = *array;
676 if (swapped) {
677 /* undo swap of u64, then swap on individual u32s */
678 u.val64 = bswap_64(u.val64);
679 u.val32[0] = bswap_32(u.val32[0]);
680 u.val32[1] = bswap_32(u.val32[1]);
681 }
682
683 sample->pid = u.val32[0];
684 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200685 }
686
687 return 0;
688}
689
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200690static bool sample_overlap(const union perf_event *event,
691 const void *offset, u64 size)
692{
693 const void *base = event;
694
695 if (offset + size > base + event->header.size)
696 return true;
697
698 return false;
699}
700
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200701int perf_event__parse_sample(const union perf_event *event, u64 type,
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200702 int sample_size, bool sample_id_all,
David Ahern936be502011-09-06 09:12:26 -0600703 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200704{
705 const u64 *array;
706
David Ahern936be502011-09-06 09:12:26 -0600707 /*
708 * used for cross-endian analysis. See git commit 65014ab3
709 * for why this goofiness is needed.
710 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200711 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600712
Robert Richterf3bda2c2011-12-15 17:32:39 +0100713 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200714 data->cpu = data->pid = data->tid = -1;
715 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530716 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200717
718 if (event->header.type != PERF_RECORD_SAMPLE) {
719 if (!sample_id_all)
720 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200721 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200722 }
723
724 array = event->sample.array;
725
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200726 if (sample_size + sizeof(event->header) > event->header.size)
727 return -EFAULT;
728
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200729 if (type & PERF_SAMPLE_IP) {
730 data->ip = event->ip.ip;
731 array++;
732 }
733
734 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600735 u.val64 = *array;
736 if (swapped) {
737 /* undo swap of u64, then swap on individual u32s */
738 u.val64 = bswap_64(u.val64);
739 u.val32[0] = bswap_32(u.val32[0]);
740 u.val32[1] = bswap_32(u.val32[1]);
741 }
742
743 data->pid = u.val32[0];
744 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200745 array++;
746 }
747
748 if (type & PERF_SAMPLE_TIME) {
749 data->time = *array;
750 array++;
751 }
752
David Ahern7cec0922011-05-30 13:08:23 -0600753 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200754 if (type & PERF_SAMPLE_ADDR) {
755 data->addr = *array;
756 array++;
757 }
758
759 data->id = -1ULL;
760 if (type & PERF_SAMPLE_ID) {
761 data->id = *array;
762 array++;
763 }
764
765 if (type & PERF_SAMPLE_STREAM_ID) {
766 data->stream_id = *array;
767 array++;
768 }
769
770 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600771
772 u.val64 = *array;
773 if (swapped) {
774 /* undo swap of u64, then swap on individual u32s */
775 u.val64 = bswap_64(u.val64);
776 u.val32[0] = bswap_32(u.val32[0]);
777 }
778
779 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200780 array++;
781 }
782
783 if (type & PERF_SAMPLE_PERIOD) {
784 data->period = *array;
785 array++;
786 }
787
788 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100789 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200790 return -1;
791 }
792
793 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200794 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
795 return -EFAULT;
796
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200797 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200798
799 if (sample_overlap(event, array, data->callchain->nr))
800 return -EFAULT;
801
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200802 array += 1 + data->callchain->nr;
803 }
804
805 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200806 const u64 *pdata;
807
David Ahern936be502011-09-06 09:12:26 -0600808 u.val64 = *array;
809 if (WARN_ONCE(swapped,
810 "Endianness of raw data not corrected!\n")) {
811 /* undo swap of u64, then swap on individual u32s */
812 u.val64 = bswap_64(u.val64);
813 u.val32[0] = bswap_32(u.val32[0]);
814 u.val32[1] = bswap_32(u.val32[1]);
815 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200816
817 if (sample_overlap(event, array, sizeof(u32)))
818 return -EFAULT;
819
David Ahern936be502011-09-06 09:12:26 -0600820 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200821 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200822
Jiri Olsa8e303f22011-09-29 17:05:08 +0200823 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200824 return -EFAULT;
825
Jiri Olsa8e303f22011-09-29 17:05:08 +0200826 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100827
828 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200829 }
830
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100831 if (type & PERF_SAMPLE_BRANCH_STACK) {
832 u64 sz;
833
834 data->branch_stack = (struct branch_stack *)array;
835 array++; /* nr */
836
837 sz = data->branch_stack->nr * sizeof(struct branch_entry);
838 sz /= sizeof(u64);
839 array += sz;
840 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200841 return 0;
842}
Andrew Vagin74eec262011-11-28 12:03:31 +0300843
844int perf_event__synthesize_sample(union perf_event *event, u64 type,
845 const struct perf_sample *sample,
846 bool swapped)
847{
848 u64 *array;
849
850 /*
851 * used for cross-endian analysis. See git commit 65014ab3
852 * for why this goofiness is needed.
853 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200854 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300855
856 array = event->sample.array;
857
858 if (type & PERF_SAMPLE_IP) {
859 event->ip.ip = sample->ip;
860 array++;
861 }
862
863 if (type & PERF_SAMPLE_TID) {
864 u.val32[0] = sample->pid;
865 u.val32[1] = sample->tid;
866 if (swapped) {
867 /*
868 * Inverse of what is done in perf_event__parse_sample
869 */
870 u.val32[0] = bswap_32(u.val32[0]);
871 u.val32[1] = bswap_32(u.val32[1]);
872 u.val64 = bswap_64(u.val64);
873 }
874
875 *array = u.val64;
876 array++;
877 }
878
879 if (type & PERF_SAMPLE_TIME) {
880 *array = sample->time;
881 array++;
882 }
883
884 if (type & PERF_SAMPLE_ADDR) {
885 *array = sample->addr;
886 array++;
887 }
888
889 if (type & PERF_SAMPLE_ID) {
890 *array = sample->id;
891 array++;
892 }
893
894 if (type & PERF_SAMPLE_STREAM_ID) {
895 *array = sample->stream_id;
896 array++;
897 }
898
899 if (type & PERF_SAMPLE_CPU) {
900 u.val32[0] = sample->cpu;
901 if (swapped) {
902 /*
903 * Inverse of what is done in perf_event__parse_sample
904 */
905 u.val32[0] = bswap_32(u.val32[0]);
906 u.val64 = bswap_64(u.val64);
907 }
908 *array = u.val64;
909 array++;
910 }
911
912 if (type & PERF_SAMPLE_PERIOD) {
913 *array = sample->period;
914 array++;
915 }
916
917 return 0;
918}