blob: 876f639d69ed89d7402b84398da648450e03c875 [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 Meloc4104312012-05-25 16:38:11 -030018#include "../../include/linux/perf_event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020019
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020020#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -020021#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020022
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030023int __perf_evsel__sample_size(u64 sample_type)
24{
25 u64 mask = sample_type & PERF_SAMPLE_MASK;
26 int size = 0;
27 int i;
28
29 for (i = 0; i < 64; i++) {
30 if (mask & (1ULL << i))
31 size++;
32 }
33
34 size *= sizeof(u64);
35
36 return size;
37}
38
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010039void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020040{
41 memset(hists, 0, sizeof(*hists));
42 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
43 hists->entries_in = &hists->entries_in_array[0];
44 hists->entries_collapsed = RB_ROOT;
45 hists->entries = RB_ROOT;
46 pthread_mutex_init(&hists->lock, NULL);
47}
48
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020049void perf_evsel__init(struct perf_evsel *evsel,
50 struct perf_event_attr *attr, int idx)
51{
52 evsel->idx = idx;
53 evsel->attr = *attr;
54 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030055 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020056}
57
Lin Ming23a2f3a2011-01-07 11:11:09 +080058struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020059{
60 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020062 if (evsel != NULL)
63 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020064
65 return evsel;
66}
67
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030068static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
69 "cycles",
70 "instructions",
71 "cache-references",
72 "cache-misses",
73 "branches",
74 "branch-misses",
75 "bus-cycles",
76 "stalled-cycles-frontend",
77 "stalled-cycles-backend",
78 "ref-cycles",
79};
80
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -030081static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030082{
83 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84 return perf_evsel__hw_names[config];
85
86 return "unknown-hardware";
87}
88
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030089static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030090{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030091 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030092 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030093 bool exclude_guest_default = false;
94
95#define MOD_PRINT(context, mod) do { \
96 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030097 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030098 r += scnprintf(bf + r, size - r, "%c", mod); \
99 } } while(0)
100
101 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
102 MOD_PRINT(kernel, 'k');
103 MOD_PRINT(user, 'u');
104 MOD_PRINT(hv, 'h');
105 exclude_guest_default = true;
106 }
107
108 if (attr->precise_ip) {
109 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300110 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300111 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
112 exclude_guest_default = true;
113 }
114
115 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
116 MOD_PRINT(host, 'H');
117 MOD_PRINT(guest, 'G');
118 }
119#undef MOD_PRINT
120 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300121 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300122 return r;
123}
124
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300125static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
126{
127 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
128 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
129}
130
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300131static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
132 "cpu-clock",
133 "task-clock",
134 "page-faults",
135 "context-switches",
136 "CPU-migrations",
137 "minor-faults",
138 "major-faults",
139 "alignment-faults",
140 "emulation-faults",
141};
142
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300143static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300144{
145 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
146 return perf_evsel__sw_names[config];
147 return "unknown-software";
148}
149
150static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
151{
152 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
153 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
154}
155
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300156const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
157 [PERF_EVSEL__MAX_ALIASES] = {
158 { "L1-dcache", "l1-d", "l1d", "L1-data", },
159 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
160 { "LLC", "L2", },
161 { "dTLB", "d-tlb", "Data-TLB", },
162 { "iTLB", "i-tlb", "Instruction-TLB", },
163 { "branch", "branches", "bpu", "btb", "bpc", },
164 { "node", },
165};
166
167const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
168 [PERF_EVSEL__MAX_ALIASES] = {
169 { "load", "loads", "read", },
170 { "store", "stores", "write", },
171 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
172};
173
174const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
175 [PERF_EVSEL__MAX_ALIASES] = {
176 { "refs", "Reference", "ops", "access", },
177 { "misses", "miss", },
178};
179
180#define C(x) PERF_COUNT_HW_CACHE_##x
181#define CACHE_READ (1 << C(OP_READ))
182#define CACHE_WRITE (1 << C(OP_WRITE))
183#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
184#define COP(x) (1 << x)
185
186/*
187 * cache operartion stat
188 * L1I : Read and prefetch only
189 * ITLB and BPU : Read-only
190 */
191static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
192 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
193 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
194 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
195 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
196 [C(ITLB)] = (CACHE_READ),
197 [C(BPU)] = (CACHE_READ),
198 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
199};
200
201bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
202{
203 if (perf_evsel__hw_cache_stat[type] & COP(op))
204 return true; /* valid */
205 else
206 return false; /* invalid */
207}
208
209int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
210 char *bf, size_t size)
211{
212 if (result) {
213 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
214 perf_evsel__hw_cache_op[op][0],
215 perf_evsel__hw_cache_result[result][0]);
216 }
217
218 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
219 perf_evsel__hw_cache_op[op][1]);
220}
221
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300222static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300223{
224 u8 op, result, type = (config >> 0) & 0xff;
225 const char *err = "unknown-ext-hardware-cache-type";
226
227 if (type > PERF_COUNT_HW_CACHE_MAX)
228 goto out_err;
229
230 op = (config >> 8) & 0xff;
231 err = "unknown-ext-hardware-cache-op";
232 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
233 goto out_err;
234
235 result = (config >> 16) & 0xff;
236 err = "unknown-ext-hardware-cache-result";
237 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
238 goto out_err;
239
240 err = "invalid-cache";
241 if (!perf_evsel__is_cache_op_valid(type, op))
242 goto out_err;
243
244 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
245out_err:
246 return scnprintf(bf, size, "%s", err);
247}
248
249static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
250{
251 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
252 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
253}
254
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300255static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
256{
257 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
258 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
259}
260
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300261const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300262{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300263 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300264
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300265 if (evsel->name)
266 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300267
268 switch (evsel->attr.type) {
269 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300270 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300271 break;
272
273 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300274 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300275 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300276
277 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300278 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300279 break;
280
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300281 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300282 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300283 break;
284
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300285 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300286 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300287 break;
288
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300289 default:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300290 scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300291 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300292 }
293
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300294 evsel->name = strdup(bf);
295
296 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300297}
298
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900299void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
300 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200301{
302 struct perf_event_attr *attr = &evsel->attr;
303 int track = !evsel->idx; /* only the first counter needs these */
304
David Ahern5e1c81d2012-05-13 22:01:28 -0600305 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200306 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200307 attr->inherit = !opts->no_inherit;
308 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
309 PERF_FORMAT_TOTAL_TIME_RUNNING |
310 PERF_FORMAT_ID;
311
312 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
313
314 /*
315 * We default some events to a 1 default interval. But keep
316 * it a weak assumption overridable by the user.
317 */
318 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
319 opts->user_interval != ULLONG_MAX)) {
320 if (opts->freq) {
321 attr->sample_type |= PERF_SAMPLE_PERIOD;
322 attr->freq = 1;
323 attr->sample_freq = opts->freq;
324 } else {
325 attr->sample_period = opts->default_interval;
326 }
327 }
328
329 if (opts->no_samples)
330 attr->sample_freq = 0;
331
332 if (opts->inherit_stat)
333 attr->inherit_stat = 1;
334
335 if (opts->sample_address) {
336 attr->sample_type |= PERF_SAMPLE_ADDR;
337 attr->mmap_data = track;
338 }
339
340 if (opts->call_graph)
341 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
342
Namhyung Kime40ee742012-05-21 10:42:07 +0900343 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200344 attr->sample_type |= PERF_SAMPLE_CPU;
345
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300346 if (opts->period)
347 attr->sample_type |= PERF_SAMPLE_PERIOD;
348
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200349 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900350 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900351 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200352 attr->sample_type |= PERF_SAMPLE_TIME;
353
354 if (opts->raw_samples) {
355 attr->sample_type |= PERF_SAMPLE_TIME;
356 attr->sample_type |= PERF_SAMPLE_RAW;
357 attr->sample_type |= PERF_SAMPLE_CPU;
358 }
359
360 if (opts->no_delay) {
361 attr->watermark = 0;
362 attr->wakeup_events = 1;
363 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100364 if (opts->branch_stack) {
365 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
366 attr->branch_sample_type = opts->branch_stack;
367 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200368
369 attr->mmap = track;
370 attr->comm = track;
371
Namhyung Kimd67356e2012-05-07 14:09:03 +0900372 if (perf_target__none(&opts->target) &&
373 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200374 attr->enable_on_exec = 1;
375 }
376}
377
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200378int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
379{
David Ahern4af4c952011-05-27 09:58:34 -0600380 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200381 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600382
383 if (evsel->fd) {
384 for (cpu = 0; cpu < ncpus; cpu++) {
385 for (thread = 0; thread < nthreads; thread++) {
386 FD(evsel, cpu, thread) = -1;
387 }
388 }
389 }
390
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200391 return evsel->fd != NULL ? 0 : -ENOMEM;
392}
393
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200394int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
395{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300396 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
397 if (evsel->sample_id == NULL)
398 return -ENOMEM;
399
400 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
401 if (evsel->id == NULL) {
402 xyarray__delete(evsel->sample_id);
403 evsel->sample_id = NULL;
404 return -ENOMEM;
405 }
406
407 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200408}
409
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200410int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
411{
412 evsel->counts = zalloc((sizeof(*evsel->counts) +
413 (ncpus * sizeof(struct perf_counts_values))));
414 return evsel->counts != NULL ? 0 : -ENOMEM;
415}
416
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200417void perf_evsel__free_fd(struct perf_evsel *evsel)
418{
419 xyarray__delete(evsel->fd);
420 evsel->fd = NULL;
421}
422
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200423void perf_evsel__free_id(struct perf_evsel *evsel)
424{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300425 xyarray__delete(evsel->sample_id);
426 evsel->sample_id = NULL;
427 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200428 evsel->id = NULL;
429}
430
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200431void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
432{
433 int cpu, thread;
434
435 for (cpu = 0; cpu < ncpus; cpu++)
436 for (thread = 0; thread < nthreads; ++thread) {
437 close(FD(evsel, cpu, thread));
438 FD(evsel, cpu, thread) = -1;
439 }
440}
441
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200442void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200443{
444 assert(list_empty(&evsel->node));
445 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300446 xyarray__delete(evsel->sample_id);
447 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200448}
449
450void perf_evsel__delete(struct perf_evsel *evsel)
451{
452 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200453 close_cgroup(evsel->cgrp);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200454 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200455 free(evsel);
456}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200457
458int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
459 int cpu, int thread, bool scale)
460{
461 struct perf_counts_values count;
462 size_t nv = scale ? 3 : 1;
463
464 if (FD(evsel, cpu, thread) < 0)
465 return -EINVAL;
466
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200467 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
468 return -ENOMEM;
469
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200470 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
471 return -errno;
472
473 if (scale) {
474 if (count.run == 0)
475 count.val = 0;
476 else if (count.run < count.ena)
477 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
478 } else
479 count.ena = count.run = 0;
480
481 evsel->counts->cpu[cpu] = count;
482 return 0;
483}
484
485int __perf_evsel__read(struct perf_evsel *evsel,
486 int ncpus, int nthreads, bool scale)
487{
488 size_t nv = scale ? 3 : 1;
489 int cpu, thread;
490 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
491
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200492 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200493
494 for (cpu = 0; cpu < ncpus; cpu++) {
495 for (thread = 0; thread < nthreads; thread++) {
496 if (FD(evsel, cpu, thread) < 0)
497 continue;
498
499 if (readn(FD(evsel, cpu, thread),
500 &count, nv * sizeof(u64)) < 0)
501 return -errno;
502
503 aggr->val += count.val;
504 if (scale) {
505 aggr->ena += count.ena;
506 aggr->run += count.run;
507 }
508 }
509 }
510
511 evsel->counts->scaled = 0;
512 if (scale) {
513 if (aggr->run == 0) {
514 evsel->counts->scaled = -1;
515 aggr->val = 0;
516 return 0;
517 }
518
519 if (aggr->run < aggr->ena) {
520 evsel->counts->scaled = 1;
521 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
522 }
523 } else
524 aggr->ena = aggr->run = 0;
525
526 return 0;
527}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200528
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200529static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200530 struct thread_map *threads, bool group,
531 struct xyarray *group_fds)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200532{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200533 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200534 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200535 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200536
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200537 if (evsel->fd == NULL &&
538 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200539 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200540
Stephane Eranian023695d2011-02-14 11:20:01 +0200541 if (evsel->cgrp) {
542 flags = PERF_FLAG_PID_CGROUP;
543 pid = evsel->cgrp->fd;
544 }
545
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200546 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200547 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200548
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200549 for (thread = 0; thread < threads->nr; thread++) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200550
551 if (!evsel->cgrp)
552 pid = threads->map[thread];
553
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200554 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200555 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200556 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200557 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200558 if (FD(evsel, cpu, thread) < 0) {
559 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200560 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200561 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200562
563 if (group && group_fd == -1)
564 group_fd = FD(evsel, cpu, thread);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200565 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200566 }
567
568 return 0;
569
570out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200571 do {
572 while (--thread >= 0) {
573 close(FD(evsel, cpu, thread));
574 FD(evsel, cpu, thread) = -1;
575 }
576 thread = threads->nr;
577 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200578 return err;
579}
580
581void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
582{
583 if (evsel->fd == NULL)
584 return;
585
586 perf_evsel__close_fd(evsel, ncpus, nthreads);
587 perf_evsel__free_fd(evsel);
588 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200589}
590
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200591static struct {
592 struct cpu_map map;
593 int cpus[1];
594} empty_cpu_map = {
595 .map.nr = 1,
596 .cpus = { -1, },
597};
598
599static struct {
600 struct thread_map map;
601 int threads[1];
602} empty_thread_map = {
603 .map.nr = 1,
604 .threads = { -1, },
605};
606
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200607int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200608 struct thread_map *threads, bool group,
609 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200610{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200611 if (cpus == NULL) {
612 /* Work around old compiler warnings about strict aliasing */
613 cpus = &empty_cpu_map.map;
614 }
615
616 if (threads == NULL)
617 threads = &empty_thread_map.map;
618
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200619 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200620}
621
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200622int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200623 struct cpu_map *cpus, bool group,
624 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200625{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200626 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
627 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200628}
629
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200630int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200631 struct thread_map *threads, bool group,
632 struct xyarray *group_fd)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200633{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200634 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
635 group_fd);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200636}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200637
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200638static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200639 struct perf_sample *sample,
640 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200641{
642 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200643 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200644
645 array += ((event->header.size -
646 sizeof(event->header)) / sizeof(u64)) - 1;
647
648 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200649 u.val64 = *array;
650 if (swapped) {
651 /* undo swap of u64, then swap on individual u32s */
652 u.val64 = bswap_64(u.val64);
653 u.val32[0] = bswap_32(u.val32[0]);
654 }
655
656 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200657 array--;
658 }
659
660 if (type & PERF_SAMPLE_STREAM_ID) {
661 sample->stream_id = *array;
662 array--;
663 }
664
665 if (type & PERF_SAMPLE_ID) {
666 sample->id = *array;
667 array--;
668 }
669
670 if (type & PERF_SAMPLE_TIME) {
671 sample->time = *array;
672 array--;
673 }
674
675 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200676 u.val64 = *array;
677 if (swapped) {
678 /* undo swap of u64, then swap on individual u32s */
679 u.val64 = bswap_64(u.val64);
680 u.val32[0] = bswap_32(u.val32[0]);
681 u.val32[1] = bswap_32(u.val32[1]);
682 }
683
684 sample->pid = u.val32[0];
685 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200686 }
687
688 return 0;
689}
690
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200691static bool sample_overlap(const union perf_event *event,
692 const void *offset, u64 size)
693{
694 const void *base = event;
695
696 if (offset + size > base + event->header.size)
697 return true;
698
699 return false;
700}
701
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200702int perf_event__parse_sample(const union perf_event *event, u64 type,
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200703 int sample_size, bool sample_id_all,
David Ahern936be502011-09-06 09:12:26 -0600704 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200705{
706 const u64 *array;
707
David Ahern936be502011-09-06 09:12:26 -0600708 /*
709 * used for cross-endian analysis. See git commit 65014ab3
710 * for why this goofiness is needed.
711 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200712 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600713
Robert Richterf3bda2c2011-12-15 17:32:39 +0100714 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200715 data->cpu = data->pid = data->tid = -1;
716 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530717 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200718
719 if (event->header.type != PERF_RECORD_SAMPLE) {
720 if (!sample_id_all)
721 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200722 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200723 }
724
725 array = event->sample.array;
726
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200727 if (sample_size + sizeof(event->header) > event->header.size)
728 return -EFAULT;
729
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200730 if (type & PERF_SAMPLE_IP) {
731 data->ip = event->ip.ip;
732 array++;
733 }
734
735 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600736 u.val64 = *array;
737 if (swapped) {
738 /* undo swap of u64, then swap on individual u32s */
739 u.val64 = bswap_64(u.val64);
740 u.val32[0] = bswap_32(u.val32[0]);
741 u.val32[1] = bswap_32(u.val32[1]);
742 }
743
744 data->pid = u.val32[0];
745 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200746 array++;
747 }
748
749 if (type & PERF_SAMPLE_TIME) {
750 data->time = *array;
751 array++;
752 }
753
David Ahern7cec0922011-05-30 13:08:23 -0600754 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200755 if (type & PERF_SAMPLE_ADDR) {
756 data->addr = *array;
757 array++;
758 }
759
760 data->id = -1ULL;
761 if (type & PERF_SAMPLE_ID) {
762 data->id = *array;
763 array++;
764 }
765
766 if (type & PERF_SAMPLE_STREAM_ID) {
767 data->stream_id = *array;
768 array++;
769 }
770
771 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600772
773 u.val64 = *array;
774 if (swapped) {
775 /* undo swap of u64, then swap on individual u32s */
776 u.val64 = bswap_64(u.val64);
777 u.val32[0] = bswap_32(u.val32[0]);
778 }
779
780 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200781 array++;
782 }
783
784 if (type & PERF_SAMPLE_PERIOD) {
785 data->period = *array;
786 array++;
787 }
788
789 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100790 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200791 return -1;
792 }
793
794 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200795 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
796 return -EFAULT;
797
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200798 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200799
800 if (sample_overlap(event, array, data->callchain->nr))
801 return -EFAULT;
802
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200803 array += 1 + data->callchain->nr;
804 }
805
806 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200807 const u64 *pdata;
808
David Ahern936be502011-09-06 09:12:26 -0600809 u.val64 = *array;
810 if (WARN_ONCE(swapped,
811 "Endianness of raw data not corrected!\n")) {
812 /* undo swap of u64, then swap on individual u32s */
813 u.val64 = bswap_64(u.val64);
814 u.val32[0] = bswap_32(u.val32[0]);
815 u.val32[1] = bswap_32(u.val32[1]);
816 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200817
818 if (sample_overlap(event, array, sizeof(u32)))
819 return -EFAULT;
820
David Ahern936be502011-09-06 09:12:26 -0600821 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200822 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200823
Jiri Olsa8e303f22011-09-29 17:05:08 +0200824 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200825 return -EFAULT;
826
Jiri Olsa8e303f22011-09-29 17:05:08 +0200827 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100828
829 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200830 }
831
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100832 if (type & PERF_SAMPLE_BRANCH_STACK) {
833 u64 sz;
834
835 data->branch_stack = (struct branch_stack *)array;
836 array++; /* nr */
837
838 sz = data->branch_stack->nr * sizeof(struct branch_entry);
839 sz /= sizeof(u64);
840 array += sz;
841 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200842 return 0;
843}
Andrew Vagin74eec262011-11-28 12:03:31 +0300844
845int perf_event__synthesize_sample(union perf_event *event, u64 type,
846 const struct perf_sample *sample,
847 bool swapped)
848{
849 u64 *array;
850
851 /*
852 * used for cross-endian analysis. See git commit 65014ab3
853 * for why this goofiness is needed.
854 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200855 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300856
857 array = event->sample.array;
858
859 if (type & PERF_SAMPLE_IP) {
860 event->ip.ip = sample->ip;
861 array++;
862 }
863
864 if (type & PERF_SAMPLE_TID) {
865 u.val32[0] = sample->pid;
866 u.val32[1] = sample->tid;
867 if (swapped) {
868 /*
869 * Inverse of what is done in perf_event__parse_sample
870 */
871 u.val32[0] = bswap_32(u.val32[0]);
872 u.val32[1] = bswap_32(u.val32[1]);
873 u.val64 = bswap_64(u.val64);
874 }
875
876 *array = u.val64;
877 array++;
878 }
879
880 if (type & PERF_SAMPLE_TIME) {
881 *array = sample->time;
882 array++;
883 }
884
885 if (type & PERF_SAMPLE_ADDR) {
886 *array = sample->addr;
887 array++;
888 }
889
890 if (type & PERF_SAMPLE_ID) {
891 *array = sample->id;
892 array++;
893 }
894
895 if (type & PERF_SAMPLE_STREAM_ID) {
896 *array = sample->stream_id;
897 array++;
898 }
899
900 if (type & PERF_SAMPLE_CPU) {
901 u.val32[0] = sample->cpu;
902 if (swapped) {
903 /*
904 * Inverse of what is done in perf_event__parse_sample
905 */
906 u.val32[0] = bswap_32(u.val32[0]);
907 u.val64 = bswap_64(u.val64);
908 }
909 *array = u.val64;
910 array++;
911 }
912
913 if (type & PERF_SAMPLE_PERIOD) {
914 *array = sample->period;
915 array++;
916 }
917
918 return 0;
919}