| 1 | |
| 2 | |
| 3 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <[email protected]>
|
| 4 | *
|
| 5 | * Parts came from builtin-{top,stat,record}.c, see those files for further
|
| 6 | * copyright notes.
|
| 7 |
|
| 8 | #include "evlist.h"
|
| 9 |
|
| 10 | #include <errno.h>
|
| 11 | #include <inttypes.h>
|
| 12 | #include <signal.h>
|
| 13 | #include <stdlib.h>
|
| 14 | #include <string.h>
|
| 15 |
|
| 16 | #include <fcntl.h>
|
| 17 | #include <linux/bitops.h>
|
| 18 | #include <linux/err.h>
|
| 19 | #include <linux/hash.h>
|
| 20 | #include <linux/log2.h>
|
| 21 | #include <linux/string.h>
|
| 22 | #include <linux/time64.h>
|
| 23 | #include <linux/zalloc.h>
|
| 24 | #include <poll.h>
|
| 25 | #include <sched.h>
|
| 26 | #include <sys/ioctl.h>
|
| 27 | #include <sys/mman.h>
|
| 28 | #include <sys/prctl.h>
|
| 29 | #include <sys/timerfd.h>
|
| 30 | #include <sys/wait.h>
|
| 31 | #include <unistd.h>
|
| 32 |
|
| 33 | #include <api/fs/fs.h>
|
| 34 | #include <internal/lib.h>
|
| 35 | #include <internal/rc_check.h>
|
| 36 | #include <internal/xyarray.h>
|
| 37 | #include <perf/cpumap.h>
|
| 38 | #include <perf/evlist.h>
|
| 39 | #include <perf/evsel.h>
|
| 40 | #include <perf/mmap.h>
|
| 41 | #include <subcmd/parse-options.h>
|
| 42 |
|
| 43 | #include "../perf.h"
|
| 44 | #include "affinity.h"
|
| 45 | #include "asm/bug.h"
|
| 46 | #include "bpf-event.h"
|
| 47 | #include "bpf-filter.h"
|
| 48 | #include "bpf_counter.h"
|
| 49 | #include "cpumap.h"
|
| 50 | #include "debug.h"
|
| 51 | #include "dwarf-regs.h"
|
| 52 | #include "env.h"
|
| 53 | #include "event.h"
|
| 54 | #include "evsel.h"
|
| 55 | #include "evsel_fprintf.h"
|
| 56 | #include "intel-tpebs.h"
|
| 57 | #include "metricgroup.h"
|
| 58 | #include "mmap.h"
|
| 59 | #include "parse-events.h"
|
| 60 | #include "perf_api_probe.h"
|
| 61 | #include "pmu.h"
|
| 62 | #include "pmus.h"
|
| 63 | #include "record.h"
|
| 64 | #include "sample.h"
|
| 65 | #include "stat.h"
|
| 66 | #include "strbuf.h"
|
| 67 | #include "string2.h"
|
| 68 | #include "target.h"
|
| 69 | #include "thread_map.h"
|
| 70 | #include "units.h"
|
| 71 | #include "util.h"
|
| 72 |
|
| 73 | #ifdef LACKS_SIGQUEUE_PROTOTYPE
|
| 74 | int sigqueue(pid_t pid, int sig, const union sigval value);
|
| 75 | #endif
|
| 76 |
|
| 77 | #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
|
| 78 | #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
|
| 79 |
|
| 80 | static void event_enable_timer__exit(struct event_enable_timer **ep);
|
| 81 |
|
| 82 | struct evlist *evlist__new(void)
|
| 83 | {
|
| 84 | struct evlist *result;
|
| 85 | RC_STRUCT(evlist) *evlist;
|
| 86 |
|
| 87 | evlist = zalloc(sizeof(*evlist));
|
| 88 | if (ADD_RC_CHK(result, evlist)) {
|
| 89 | perf_evlist__init(evlist__core(result));
|
| 90 | perf_evlist__set_maps(evlist__core(result), NULL, NULL);
|
| 91 | evlist__set_workload_pid(result, -1);
|
| 92 | evlist__set_bkw_mmap_state(result, BKW_MMAP_NOTREADY);
|
| 93 | evlist__set_ctl_fd_fd(result, -1);
|
| 94 | evlist__set_ctl_fd_ack(result, -1);
|
| 95 | evlist__set_ctl_fd_pos(result, -1);
|
| 96 | evlist__set_nr_br_cntr(result, -1);
|
| 97 | metricgroup__rblist_init(evlist__metric_events(result));
|
| 98 | INIT_LIST_HEAD(&evlist->deferred_samples);
|
| 99 | refcount_set(evlist__refcnt(result), 1);
|
| 100 | } else {
|
| 101 | free(evlist);
|
| 102 | result = NULL;
|
| 103 | }
|
| 104 | return result;
|
| 105 | }
|
| 106 |
|
| 107 | struct evlist *evlist__new_default(const struct target *target, bool sample_callchains)
|
| 108 | {
|
| 109 | struct evlist *evlist = evlist__new();
|
| 110 | bool can_profile_kernel;
|
| 111 | struct perf_pmu *pmu = NULL;
|
| 112 | char buf[256];
|
| 113 | int err;
|
| 114 |
|
| 115 | if (!evlist)
|
| 116 | return NULL;
|
| 117 |
|
| 118 | can_profile_kernel = perf_event_paranoid_check(1);
|
| 119 |
|
| 120 | if (EM_HOST == EM_S390 && sample_callchains) {
|
| 121 | snprintf(buf, sizeof(buf), "software/%s/%s",
|
| 122 | target__has_cpu(target) ? "cpu-clock" : "task-clock",
|
| 123 | can_profile_kernel ? "P" : "Pu");
|
| 124 | err = parse_event(evlist, buf);
|
| 125 | if (err)
|
| 126 | goto out_err;
|
| 127 | } else {
|
| 128 | while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
|
| 129 | snprintf(buf, sizeof(buf), "%s/cycles/%s", pmu->name,
|
| 130 | can_profile_kernel ? "P" : "Pu");
|
| 131 | err = parse_event(evlist, buf);
|
| 132 | if (err)
|
| 133 | goto out_err;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 |
|
| 138 | if (evlist__nr_entries(evlist) > 1) {
|
| 139 | struct evsel *evsel;
|
| 140 |
|
| 141 | evlist__for_each_entry(evlist, evsel)
|
| 142 | evsel__set_sample_id(evsel, false);
|
| 143 | }
|
| 144 |
|
| 145 | return evlist;
|
| 146 | out_err:
|
| 147 | evlist__put(evlist);
|
| 148 | return NULL;
|
| 149 | }
|
| 150 |
|
| 151 | struct evlist *evlist__new_dummy(void)
|
| 152 | {
|
| 153 | struct evlist *evlist = evlist__new();
|
| 154 |
|
| 155 | if (evlist && evlist__add_dummy(evlist)) {
|
| 156 | evlist__put(evlist);
|
| 157 | evlist = NULL;
|
| 158 | }
|
| 159 |
|
| 160 | return evlist;
|
| 161 | }
|
| 162 |
|
| 163 | struct evlist *evlist__get(struct evlist *evlist)
|
| 164 | {
|
| 165 | struct evlist *result;
|
| 166 |
|
| 167 | if (RC_CHK_GET(result, evlist))
|
| 168 | refcount_inc(evlist__refcnt(evlist));
|
| 169 |
|
| 170 | return result;
|
| 171 | }
|
| 172 |
|
| 173 | |
| 174 | * evlist__set_id_pos - set the positions of event ids.
|
| 175 | * @evlist: selected event list
|
| 176 | *
|
| 177 | * Events with compatible sample types all have the same id_pos
|
| 178 | * and is_pos. For convenience, put a copy on evlist.
|
| 179 |
|
| 180 | void evlist__set_id_pos(struct evlist *evlist)
|
| 181 | {
|
| 182 | struct evsel *first = evlist__first(evlist);
|
| 183 |
|
| 184 | RC_CHK_ACCESS(evlist)->id_pos = first->id_pos;
|
| 185 | RC_CHK_ACCESS(evlist)->is_pos = first->is_pos;
|
| 186 | }
|
| 187 |
|
| 188 | static void evlist__update_id_pos(struct evlist *evlist)
|
| 189 | {
|
| 190 | struct evsel *evsel;
|
| 191 |
|
| 192 | evlist__for_each_entry(evlist, evsel)
|
| 193 | evsel__calc_id_pos(evsel);
|
| 194 |
|
| 195 | evlist__set_id_pos(evlist);
|
| 196 | }
|
| 197 |
|
| 198 | static void evlist__purge(struct evlist *evlist)
|
| 199 | {
|
| 200 | struct evsel *pos, *n;
|
| 201 |
|
| 202 | evlist__for_each_entry_safe(evlist, n, pos) {
|
| 203 | list_del_init(&pos->core.node);
|
| 204 | if (pos->evlist) {
|
| 205 | if (!RC_CHK_EQUAL(pos->evlist, evlist)) {
|
| 206 | evlist__put(pos->evlist);
|
| 207 | } else {
|
| 208 | refcount_dec_and_test(evlist__refcnt(pos->evlist));
|
| 209 | RC_CHK_PUT(pos->evlist);
|
| 210 | }
|
| 211 | }
|
| 212 | pos->evlist = NULL;
|
| 213 | evsel__put(pos);
|
| 214 | }
|
| 215 |
|
| 216 | evlist__core(evlist)->nr_entries = 0;
|
| 217 | }
|
| 218 |
|
| 219 | static void evlist__exit(struct evlist *evlist)
|
| 220 | {
|
| 221 | metricgroup__rblist_exit(evlist__metric_events(evlist));
|
| 222 | event_enable_timer__exit(&RC_CHK_ACCESS(evlist)->eet);
|
| 223 | free(evlist__mmap(evlist));
|
| 224 | free(evlist__overwrite_mmap(evlist));
|
| 225 | perf_evlist__exit(evlist__core(evlist));
|
| 226 | }
|
| 227 |
|
| 228 | void evlist__put(struct evlist *evlist)
|
| 229 | {
|
| 230 | struct evsel *evsel;
|
| 231 | unsigned int count, ref_cnt;
|
| 232 |
|
| 233 | if (evlist == NULL)
|
| 234 | return;
|
| 235 |
|
| 236 | if (refcount_dec_and_test(evlist__refcnt(evlist)))
|
| 237 | goto out_delete;
|
| 238 |
|
| 239 | retry:
|
| 240 | count = refcount_read(evlist__refcnt(evlist));
|
| 241 | ref_cnt = count;
|
| 242 | evlist__for_each_entry(evlist, evsel) {
|
| 243 | if (RC_CHK_EQUAL(evsel->evlist, evlist) && count &&
|
| 244 | refcount_read(&evsel->refcnt) == 1)
|
| 245 | count--;
|
| 246 | }
|
| 247 | if (refcount_read(evlist__refcnt(evlist)) != ref_cnt)
|
| 248 | goto retry;
|
| 249 |
|
| 250 | if (count != 0) {
|
| 251 | |
| 252 | * Not the last reference except for back references from
|
| 253 | * evsels.
|
| 254 |
|
| 255 | RC_CHK_PUT(evlist);
|
| 256 | return;
|
| 257 | }
|
| 258 | out_delete:
|
| 259 | evlist__free_stats(evlist);
|
| 260 | evlist__do_munmap(evlist);
|
| 261 | evlist__close(evlist);
|
| 262 | evlist__purge(evlist);
|
| 263 | evlist__exit(evlist);
|
| 264 | RC_CHK_FREE(evlist);
|
| 265 | }
|
| 266 |
|
| 267 | void evlist__add(struct evlist *evlist, struct evsel *entry)
|
| 268 | {
|
| 269 | perf_evlist__add(evlist__core(evlist), &entry->core);
|
| 270 | evlist__put(entry->evlist);
|
| 271 | entry->evlist = evlist__get(evlist);
|
| 272 | entry->tracking = !entry->core.idx;
|
| 273 |
|
| 274 | if (evlist__nr_entries(evlist) == 1)
|
| 275 | evlist__set_id_pos(evlist);
|
| 276 | }
|
| 277 |
|
| 278 | void evlist__remove(struct evlist *evlist, struct evsel *evsel)
|
| 279 | {
|
| 280 | perf_evlist__remove(evlist__core(evlist), &evsel->core);
|
| 281 | evlist__put(evsel->evlist);
|
| 282 | evsel->evlist = NULL;
|
| 283 | }
|
| 284 |
|
| 285 | void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
|
| 286 | {
|
| 287 | while (!list_empty(list)) {
|
| 288 | struct evsel *evsel, *temp, *leader = NULL;
|
| 289 |
|
| 290 | __evlist__for_each_entry_safe(list, temp, evsel) {
|
| 291 | list_del_init(&evsel->core.node);
|
| 292 | evlist__add(evlist, evsel);
|
| 293 | leader = evsel;
|
| 294 | break;
|
| 295 | }
|
| 296 |
|
| 297 | __evlist__for_each_entry_safe(list, temp, evsel) {
|
| 298 | if (evsel__has_leader(evsel, leader)) {
|
| 299 | list_del_init(&evsel->core.node);
|
| 300 | evlist__add(evlist, evsel);
|
| 301 | }
|
| 302 | }
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | int __evlist__set_tracepoints_handlers(struct evlist *evlist,
|
| 307 | const struct evsel_str_handler *assocs, size_t nr_assocs)
|
| 308 | {
|
| 309 | size_t i;
|
| 310 | int err;
|
| 311 |
|
| 312 | for (i = 0; i < nr_assocs; i++) {
|
| 313 | |
| 314 | struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
|
| 315 | if (evsel == NULL)
|
| 316 | continue;
|
| 317 |
|
| 318 | err = -EEXIST;
|
| 319 | if (evsel->handler != NULL)
|
| 320 | goto out;
|
| 321 | evsel->handler = assocs[i].handler;
|
| 322 | }
|
| 323 |
|
| 324 | err = 0;
|
| 325 | out:
|
| 326 | return err;
|
| 327 | }
|
| 328 |
|
| 329 | static void evlist__set_leader(struct evlist *evlist)
|
| 330 | {
|
| 331 | perf_evlist__set_leader(evlist__core(evlist));
|
| 332 | }
|
| 333 |
|
| 334 | static struct evsel *evlist__dummy_event(struct evlist *evlist)
|
| 335 | {
|
| 336 | struct perf_event_attr attr = {
|
| 337 | .type = PERF_TYPE_SOFTWARE,
|
| 338 | .config = PERF_COUNT_SW_DUMMY,
|
| 339 | .size = sizeof(attr),
|
| 340 |
|
| 341 | .freq = 0,
|
| 342 | .sample_period = 1,
|
| 343 | };
|
| 344 |
|
| 345 | return evsel__new_idx(&attr, evlist__nr_entries(evlist));
|
| 346 | }
|
| 347 |
|
| 348 | int evlist__add_dummy(struct evlist *evlist)
|
| 349 | {
|
| 350 | struct evsel *evsel = evlist__dummy_event(evlist);
|
| 351 |
|
| 352 | if (evsel == NULL)
|
| 353 | return -ENOMEM;
|
| 354 |
|
| 355 | evlist__add(evlist, evsel);
|
| 356 | return 0;
|
| 357 | }
|
| 358 |
|
| 359 | struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
|
| 360 | {
|
| 361 | struct evsel *evsel = evlist__dummy_event(evlist);
|
| 362 |
|
| 363 | if (!evsel)
|
| 364 | return NULL;
|
| 365 |
|
| 366 | evsel->core.attr.exclude_kernel = 1;
|
| 367 | evsel->core.attr.exclude_guest = 1;
|
| 368 | evsel->core.attr.exclude_hv = 1;
|
| 369 | evsel->core.system_wide = system_wide;
|
| 370 | evsel->no_aux_samples = true;
|
| 371 | evsel->name = strdup("dummy:u");
|
| 372 |
|
| 373 | evlist__add(evlist, evsel);
|
| 374 | return evsel;
|
| 375 | }
|
| 376 |
|
| 377 | #ifdef HAVE_LIBTRACEEVENT
|
| 378 | struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
|
| 379 | {
|
| 380 | struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0,
|
| 381 | true);
|
| 382 |
|
| 383 | if (IS_ERR(evsel))
|
| 384 | return evsel;
|
| 385 |
|
| 386 | evsel__set_sample_bit(evsel, CPU);
|
| 387 | evsel__set_sample_bit(evsel, TIME);
|
| 388 |
|
| 389 | evsel->core.system_wide = system_wide;
|
| 390 | evsel->no_aux_samples = true;
|
| 391 |
|
| 392 | evlist__add(evlist, evsel);
|
| 393 | return evsel;
|
| 394 | }
|
| 395 | #endif
|
| 396 |
|
| 397 | struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
|
| 398 | {
|
| 399 | struct evsel *evsel;
|
| 400 |
|
| 401 | evlist__for_each_entry(evlist, evsel) {
|
| 402 | if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
|
| 403 | (strcmp(evsel->name, name) == 0))
|
| 404 | return evsel;
|
| 405 | }
|
| 406 |
|
| 407 | return NULL;
|
| 408 | }
|
| 409 |
|
| 410 | #ifdef HAVE_LIBTRACEEVENT
|
| 411 | int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
|
| 412 | {
|
| 413 | struct evsel *evsel = evsel__newtp(sys, name);
|
| 414 |
|
| 415 | if (IS_ERR(evsel))
|
| 416 | return -1;
|
| 417 |
|
| 418 | evsel->handler = handler;
|
| 419 | evlist__add(evlist, evsel);
|
| 420 | return 0;
|
| 421 | }
|
| 422 | #endif
|
| 423 |
|
| 424 | |
| 425 | * Should sched_setaffinity be used with evlist__for_each_cpu? Determine if
|
| 426 | * migrating the thread will avoid possibly numerous IPIs.
|
| 427 |
|
| 428 | static bool evlist__use_affinity(struct evlist *evlist)
|
| 429 | {
|
| 430 | struct evsel *pos;
|
| 431 | struct perf_cpu_map *used_cpus = NULL;
|
| 432 | bool ret = false;
|
| 433 |
|
| 434 | if (evlist__no_affinity(evlist) || !evlist__core(evlist)->user_requested_cpus ||
|
| 435 | cpu_map__is_dummy(evlist__core(evlist)->user_requested_cpus))
|
| 436 | return false;
|
| 437 |
|
| 438 | evlist__for_each_entry(evlist, pos) {
|
| 439 | struct perf_cpu_map *intersect;
|
| 440 |
|
| 441 | if (!perf_pmu__benefits_from_affinity(pos->pmu))
|
| 442 | continue;
|
| 443 |
|
| 444 | if (evsel__is_dummy_event(pos)) {
|
| 445 | |
| 446 | * The dummy event is opened on all CPUs so assume >1
|
| 447 | * event with shared CPUs.
|
| 448 |
|
| 449 | ret = true;
|
| 450 | break;
|
| 451 | }
|
| 452 | if (evsel__is_retire_lat(pos)) {
|
| 453 | |
| 454 | * Retirement latency events are similar to tool ones in
|
| 455 | * their implementation, and so don't require affinity.
|
| 456 |
|
| 457 | continue;
|
| 458 | }
|
| 459 | if (perf_cpu_map__is_empty(used_cpus)) {
|
| 460 |
|
| 461 | used_cpus = perf_cpu_map__get(pos->core.cpus);
|
| 462 | continue;
|
| 463 | }
|
| 464 | if ((pos->core.attr.read_format & PERF_FORMAT_GROUP) &&
|
| 465 | evsel__leader(pos) != pos) {
|
| 466 |
|
| 467 | continue;
|
| 468 | }
|
| 469 | intersect = perf_cpu_map__intersect(used_cpus, pos->core.cpus);
|
| 470 | if (!perf_cpu_map__is_empty(intersect)) {
|
| 471 |
|
| 472 | perf_cpu_map__put(intersect);
|
| 473 | ret = true;
|
| 474 | break;
|
| 475 | }
|
| 476 | perf_cpu_map__put(intersect);
|
| 477 | perf_cpu_map__merge(&used_cpus, pos->core.cpus);
|
| 478 | }
|
| 479 | perf_cpu_map__put(used_cpus);
|
| 480 | return ret;
|
| 481 | }
|
| 482 |
|
| 483 | void evlist_cpu_iterator__init(struct evlist_cpu_iterator *itr, struct evlist *evlist)
|
| 484 | {
|
| 485 | *itr = (struct evlist_cpu_iterator){
|
| 486 | .container = evlist,
|
| 487 | .evsel = NULL,
|
| 488 | .cpu_map_idx = 0,
|
| 489 | .evlist_cpu_map_idx = 0,
|
| 490 | .evlist_cpu_map_nr = perf_cpu_map__nr(evlist__core(evlist)->all_cpus),
|
| 491 | .cpu = (struct perf_cpu){ .cpu = -1},
|
| 492 | .affinity = NULL,
|
| 493 | };
|
| 494 |
|
| 495 | if (evlist__empty(evlist)) {
|
| 496 |
|
| 497 | itr->evlist_cpu_map_idx = itr->evlist_cpu_map_nr;
|
| 498 | return;
|
| 499 | }
|
| 500 |
|
| 501 | if (evlist__use_affinity(evlist)) {
|
| 502 | if (affinity__setup(&itr->saved_affinity) == 0)
|
| 503 | itr->affinity = &itr->saved_affinity;
|
| 504 | }
|
| 505 | itr->evsel = evlist__first(evlist);
|
| 506 | itr->cpu = perf_cpu_map__cpu(evlist__core(evlist)->all_cpus, 0);
|
| 507 | if (itr->affinity)
|
| 508 | affinity__set(itr->affinity, itr->cpu.cpu);
|
| 509 | itr->cpu_map_idx = perf_cpu_map__idx(itr->evsel->core.cpus, itr->cpu);
|
| 510 | |
| 511 | * If this CPU isn't in the evsel's cpu map then advance
|
| 512 | * through the list.
|
| 513 |
|
| 514 | if (itr->cpu_map_idx == -1)
|
| 515 | evlist_cpu_iterator__next(itr);
|
| 516 | }
|
| 517 |
|
| 518 | void evlist_cpu_iterator__exit(struct evlist_cpu_iterator *itr)
|
| 519 | {
|
| 520 | if (!itr->affinity)
|
| 521 | return;
|
| 522 |
|
| 523 | affinity__cleanup(itr->affinity);
|
| 524 | itr->affinity = NULL;
|
| 525 | }
|
| 526 |
|
| 527 | void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
|
| 528 | {
|
| 529 | while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
|
| 530 | evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
|
| 531 | evlist_cpu_itr->cpu_map_idx =
|
| 532 | perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
|
| 533 | evlist_cpu_itr->cpu);
|
| 534 | if (evlist_cpu_itr->cpu_map_idx != -1)
|
| 535 | return;
|
| 536 | }
|
| 537 | evlist_cpu_itr->evlist_cpu_map_idx++;
|
| 538 | if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
|
| 539 | evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
|
| 540 | evlist_cpu_itr->cpu =
|
| 541 | perf_cpu_map__cpu(evlist__core(evlist_cpu_itr->container)->all_cpus,
|
| 542 | evlist_cpu_itr->evlist_cpu_map_idx);
|
| 543 | if (evlist_cpu_itr->affinity)
|
| 544 | affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
|
| 545 | evlist_cpu_itr->cpu_map_idx =
|
| 546 | perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
|
| 547 | evlist_cpu_itr->cpu);
|
| 548 | |
| 549 | * If this CPU isn't in the evsel's cpu map then advance through
|
| 550 | * the list.
|
| 551 |
|
| 552 | if (evlist_cpu_itr->cpu_map_idx == -1)
|
| 553 | evlist_cpu_iterator__next(evlist_cpu_itr);
|
| 554 | } else {
|
| 555 | evlist_cpu_iterator__exit(evlist_cpu_itr);
|
| 556 | }
|
| 557 | }
|
| 558 |
|
| 559 | static int evsel__strcmp(struct evsel *pos, char *evsel_name)
|
| 560 | {
|
| 561 | if (!evsel_name)
|
| 562 | return 0;
|
| 563 | if (evsel__is_dummy_event(pos))
|
| 564 | return 1;
|
| 565 | return !evsel__name_is(pos, evsel_name);
|
| 566 | }
|
| 567 |
|
| 568 | static bool evlist__is_enabled(struct evlist *evlist)
|
| 569 | {
|
| 570 | struct evsel *pos;
|
| 571 |
|
| 572 | evlist__for_each_entry(evlist, pos) {
|
| 573 | if (!evsel__is_group_leader(pos) || !pos->core.fd)
|
| 574 | continue;
|
| 575 |
|
| 576 | if (!pos->disabled)
|
| 577 | return true;
|
| 578 | }
|
| 579 | return false;
|
| 580 | }
|
| 581 |
|
| 582 | static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
|
| 583 | {
|
| 584 | struct evsel *pos, *member;
|
| 585 | struct evlist_cpu_iterator evlist_cpu_itr;
|
| 586 | bool has_imm = false;
|
| 587 |
|
| 588 |
|
| 589 | for (int imm = 0; imm <= 1; imm++) {
|
| 590 | evlist__for_each_cpu(evlist_cpu_itr, evlist) {
|
| 591 | pos = evlist_cpu_itr.evsel;
|
| 592 | if (evsel__strcmp(pos, evsel_name))
|
| 593 | continue;
|
| 594 | if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
|
| 595 | continue;
|
| 596 | if (excl_dummy && evsel__is_dummy_event(pos))
|
| 597 | continue;
|
| 598 | if (pos->immediate)
|
| 599 | has_imm = true;
|
| 600 | if (pos->immediate != imm)
|
| 601 | continue;
|
| 602 | evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
|
| 603 | }
|
| 604 | if (!has_imm)
|
| 605 | break;
|
| 606 | }
|
| 607 |
|
| 608 | evlist__for_each_entry(evlist, pos) {
|
| 609 | if (evsel__strcmp(pos, evsel_name))
|
| 610 | continue;
|
| 611 | if (!evsel__is_group_leader(pos) || !pos->core.fd)
|
| 612 | continue;
|
| 613 | if (excl_dummy && evsel__is_dummy_event(pos))
|
| 614 | continue;
|
| 615 | pos->disabled = true;
|
| 616 |
|
| 617 | for_each_group_member(member, pos)
|
| 618 | member->disabled = true;
|
| 619 | }
|
| 620 |
|
| 621 | |
| 622 | * If we disabled only single event, we need to check
|
| 623 | * the enabled state of the evlist manually.
|
| 624 |
|
| 625 | evlist__set_enabled(evlist, evsel_name ? evlist__is_enabled(evlist) : false);
|
| 626 | }
|
| 627 |
|
| 628 | void evlist__disable(struct evlist *evlist)
|
| 629 | {
|
| 630 | __evlist__disable(evlist, NULL, false);
|
| 631 | }
|
| 632 |
|
| 633 | void evlist__disable_non_dummy(struct evlist *evlist)
|
| 634 | {
|
| 635 | __evlist__disable(evlist, NULL, true);
|
| 636 | }
|
| 637 |
|
| 638 | void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
|
| 639 | {
|
| 640 | __evlist__disable(evlist, evsel_name, false);
|
| 641 | }
|
| 642 |
|
| 643 | static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
|
| 644 | {
|
| 645 | struct evsel *pos, *member;
|
| 646 | struct evlist_cpu_iterator evlist_cpu_itr;
|
| 647 |
|
| 648 | evlist__for_each_cpu(evlist_cpu_itr, evlist) {
|
| 649 | pos = evlist_cpu_itr.evsel;
|
| 650 | if (evsel__strcmp(pos, evsel_name))
|
| 651 | continue;
|
| 652 | if (!evsel__is_group_leader(pos) || !pos->core.fd)
|
| 653 | continue;
|
| 654 | if (excl_dummy && evsel__is_dummy_event(pos))
|
| 655 | continue;
|
| 656 | evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
|
| 657 | }
|
| 658 | evlist__for_each_entry(evlist, pos) {
|
| 659 | if (evsel__strcmp(pos, evsel_name))
|
| 660 | continue;
|
| 661 | if (!evsel__is_group_leader(pos) || !pos->core.fd)
|
| 662 | continue;
|
| 663 | if (excl_dummy && evsel__is_dummy_event(pos))
|
| 664 | continue;
|
| 665 | pos->disabled = false;
|
| 666 |
|
| 667 | for_each_group_member(member, pos)
|
| 668 | member->disabled = false;
|
| 669 | }
|
| 670 |
|
| 671 | |
| 672 | * Even single event sets the 'enabled' for evlist,
|
| 673 | * so the toggle can work properly and toggle to
|
| 674 | * 'disabled' state.
|
| 675 |
|
| 676 | evlist__set_enabled(evlist, true);
|
| 677 | }
|
| 678 |
|
| 679 | void evlist__enable(struct evlist *evlist)
|
| 680 | {
|
| 681 | __evlist__enable(evlist, NULL, false);
|
| 682 | }
|
| 683 |
|
| 684 | void evlist__enable_non_dummy(struct evlist *evlist)
|
| 685 | {
|
| 686 | __evlist__enable(evlist, NULL, true);
|
| 687 | }
|
| 688 |
|
| 689 | void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
|
| 690 | {
|
| 691 | __evlist__enable(evlist, evsel_name, false);
|
| 692 | }
|
| 693 |
|
| 694 | void evlist__toggle_enable(struct evlist *evlist)
|
| 695 | {
|
| 696 | (evlist__enabled(evlist) ? evlist__disable : evlist__enable)(evlist);
|
| 697 | }
|
| 698 |
|
| 699 | int evlist__add_pollfd(struct evlist *evlist, int fd)
|
| 700 | {
|
| 701 | return perf_evlist__add_pollfd(evlist__core(evlist), fd, NULL, POLLIN,
|
| 702 | fdarray_flag__default);
|
| 703 | }
|
| 704 |
|
| 705 | int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
|
| 706 | {
|
| 707 | return perf_evlist__filter_pollfd(evlist__core(evlist), revents_and_mask);
|
| 708 | }
|
| 709 |
|
| 710 | #ifdef HAVE_EVENTFD_SUPPORT
|
| 711 | int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
|
| 712 | {
|
| 713 | return perf_evlist__add_pollfd(evlist__core(evlist), fd, NULL, POLLIN,
|
| 714 | fdarray_flag__nonfilterable |
|
| 715 | fdarray_flag__non_perf_event);
|
| 716 | }
|
| 717 | #endif
|
| 718 |
|
| 719 | int evlist__poll(struct evlist *evlist, int timeout)
|
| 720 | {
|
| 721 | return perf_evlist__poll(evlist__core(evlist), timeout);
|
| 722 | }
|
| 723 |
|
| 724 | struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
|
| 725 | {
|
| 726 | struct hlist_head *head;
|
| 727 | struct perf_sample_id *sid;
|
| 728 | int hash;
|
| 729 |
|
| 730 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
|
| 731 | head = &evlist__core(evlist)->heads[hash];
|
| 732 |
|
| 733 | hlist_for_each_entry(sid, head, node)
|
| 734 | if (sid->id == id)
|
| 735 | return sid;
|
| 736 |
|
| 737 | return NULL;
|
| 738 | }
|
| 739 |
|
| 740 | struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
|
| 741 | {
|
| 742 | struct perf_sample_id *sid;
|
| 743 |
|
| 744 | if (evlist__nr_entries(evlist) == 1 || !id)
|
| 745 | return evlist__first(evlist);
|
| 746 |
|
| 747 | sid = evlist__id2sid(evlist, id);
|
| 748 | if (sid)
|
| 749 | return container_of(sid->evsel, struct evsel, core);
|
| 750 |
|
| 751 | if (!evlist__sample_id_all(evlist))
|
| 752 | return evlist__first(evlist);
|
| 753 |
|
| 754 | return NULL;
|
| 755 | }
|
| 756 |
|
| 757 | struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
|
| 758 | {
|
| 759 | struct perf_sample_id *sid;
|
| 760 |
|
| 761 | if (!id)
|
| 762 | return NULL;
|
| 763 |
|
| 764 | sid = evlist__id2sid(evlist, id);
|
| 765 | if (sid)
|
| 766 | return container_of(sid->evsel, struct evsel, core);
|
| 767 |
|
| 768 | return NULL;
|
| 769 | }
|
| 770 |
|
| 771 | static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
|
| 772 | {
|
| 773 | const __u64 *array = event->sample.array;
|
| 774 | ssize_t n;
|
| 775 |
|
| 776 | n = (event->header.size - sizeof(event->header)) >> 3;
|
| 777 |
|
| 778 | if (event->header.type == PERF_RECORD_SAMPLE) {
|
| 779 | if (evlist__id_pos(evlist) >= n)
|
| 780 | return -1;
|
| 781 | *id = array[evlist__id_pos(evlist)];
|
| 782 | } else {
|
| 783 | if (evlist__is_pos(evlist) > n)
|
| 784 | return -1;
|
| 785 | n -= evlist__is_pos(evlist);
|
| 786 | *id = array[n];
|
| 787 | }
|
| 788 | return 0;
|
| 789 | }
|
| 790 |
|
| 791 | struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
|
| 792 | {
|
| 793 | struct evsel *first = evlist__first(evlist);
|
| 794 | struct hlist_head *head;
|
| 795 | struct perf_sample_id *sid;
|
| 796 | int hash;
|
| 797 | u64 id;
|
| 798 |
|
| 799 | if (evlist__nr_entries(evlist) == 1)
|
| 800 | return first;
|
| 801 |
|
| 802 | if (!first->core.attr.sample_id_all &&
|
| 803 | event->header.type != PERF_RECORD_SAMPLE)
|
| 804 | return first;
|
| 805 |
|
| 806 | if (evlist__event2id(evlist, event, &id))
|
| 807 | return NULL;
|
| 808 |
|
| 809 |
|
| 810 | if (!id)
|
| 811 | return first;
|
| 812 |
|
| 813 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
|
| 814 | head = &evlist__core(evlist)->heads[hash];
|
| 815 |
|
| 816 | hlist_for_each_entry(sid, head, node) {
|
| 817 | if (sid->id == id)
|
| 818 | return container_of(sid->evsel, struct evsel, core);
|
| 819 | }
|
| 820 | return NULL;
|
| 821 | }
|
| 822 |
|
| 823 | static int evlist__set_paused(struct evlist *evlist, bool value)
|
| 824 | {
|
| 825 | int i;
|
| 826 |
|
| 827 | if (!evlist__overwrite_mmap(evlist))
|
| 828 | return 0;
|
| 829 |
|
| 830 | for (i = 0; i < evlist__core(evlist)->nr_mmaps; i++) {
|
| 831 | int fd = evlist__overwrite_mmap(evlist)[i].core.fd;
|
| 832 | int err;
|
| 833 |
|
| 834 | if (fd < 0)
|
| 835 | continue;
|
| 836 | err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
|
| 837 | if (err)
|
| 838 | return err;
|
| 839 | }
|
| 840 | return 0;
|
| 841 | }
|
| 842 |
|
| 843 | static int evlist__pause(struct evlist *evlist)
|
| 844 | {
|
| 845 | return evlist__set_paused(evlist, true);
|
| 846 | }
|
| 847 |
|
| 848 | static int evlist__resume(struct evlist *evlist)
|
| 849 | {
|
| 850 | return evlist__set_paused(evlist, false);
|
| 851 | }
|
| 852 |
|
| 853 | static void evlist__munmap_nofree(struct evlist *evlist)
|
| 854 | {
|
| 855 | int i;
|
| 856 |
|
| 857 | if (evlist__mmap(evlist))
|
| 858 | for (i = 0; i < evlist__core(evlist)->nr_mmaps; i++)
|
| 859 | perf_mmap__munmap(&evlist__mmap(evlist)[i].core);
|
| 860 |
|
| 861 | if (evlist__overwrite_mmap(evlist))
|
| 862 | for (i = 0; i < evlist__core(evlist)->nr_mmaps; i++)
|
| 863 | perf_mmap__munmap(&evlist__overwrite_mmap(evlist)[i].core);
|
| 864 | }
|
| 865 |
|
| 866 | void evlist__do_munmap(struct evlist *evlist)
|
| 867 | {
|
| 868 | evlist__munmap_nofree(evlist);
|
| 869 | zfree(&RC_CHK_ACCESS(evlist)->mmap);
|
| 870 | zfree(&RC_CHK_ACCESS(evlist)->overwrite_mmap);
|
| 871 | }
|
| 872 |
|
| 873 | static void perf_mmap__unmap_cb(struct perf_mmap *map)
|
| 874 | {
|
| 875 | struct mmap *m = container_of(map, struct mmap, core);
|
| 876 |
|
| 877 | mmap__munmap(m);
|
| 878 | }
|
| 879 |
|
| 880 | static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
|
| 881 | bool overwrite)
|
| 882 | {
|
| 883 | int i;
|
| 884 | struct mmap *map = calloc(evlist__core(evlist)->nr_mmaps, sizeof(struct mmap));
|
| 885 |
|
| 886 | if (!map)
|
| 887 | return NULL;
|
| 888 |
|
| 889 | for (i = 0; i < evlist__core(evlist)->nr_mmaps; i++) {
|
| 890 | struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
|
| 891 |
|
| 892 | |
| 893 | * When the perf_mmap() call is made we grab one refcount, plus
|
| 894 | * one extra to let perf_mmap__consume() get the last
|
| 895 | * events after all real references (perf_mmap__get()) are
|
| 896 | * dropped.
|
| 897 | *
|
| 898 | * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
|
| 899 | * thus does perf_mmap__get() on it.
|
| 900 |
|
| 901 | perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
|
| 902 | }
|
| 903 |
|
| 904 | return map;
|
| 905 | }
|
| 906 |
|
| 907 | static struct evlist *from_list_start(struct perf_evlist *core)
|
| 908 | {
|
| 909 | #ifdef REFCNT_CHECKING
|
| 910 | RC_STRUCT(evlist) *core_evlist = container_of(core, RC_STRUCT(evlist), core);
|
| 911 | struct evlist *evlist;
|
| 912 |
|
| 913 | if (ADD_RC_CHK(evlist, core_evlist))
|
| 914 | refcount_inc(evlist__refcnt(evlist));
|
| 915 |
|
| 916 | return evlist;
|
| 917 | #else
|
| 918 | return container_of(core, struct evlist, core);
|
| 919 | #endif
|
| 920 | }
|
| 921 |
|
| 922 | static void from_list_end(struct evlist *evlist __maybe_unused)
|
| 923 | {
|
| 924 | #ifdef REFCNT_CHECKING
|
| 925 | evlist__put(evlist);
|
| 926 | #endif
|
| 927 | }
|
| 928 |
|
| 929 | static void
|
| 930 | perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
|
| 931 | struct perf_evsel *_evsel,
|
| 932 | struct perf_mmap_param *_mp,
|
| 933 | int idx)
|
| 934 | {
|
| 935 | struct evlist *evlist = from_list_start(_evlist);
|
| 936 | struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
|
| 937 | struct evsel *evsel = container_of(_evsel, struct evsel, core);
|
| 938 |
|
| 939 | if (!evlist)
|
| 940 | return;
|
| 941 |
|
| 942 | auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
|
| 943 |
|
| 944 | from_list_end(evlist);
|
| 945 | }
|
| 946 |
|
| 947 | static struct perf_mmap*
|
| 948 | perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
|
| 949 | {
|
| 950 | struct evlist *evlist = from_list_start(_evlist);
|
| 951 | struct mmap *maps;
|
| 952 |
|
| 953 | if (!evlist)
|
| 954 | return NULL;
|
| 955 |
|
| 956 | maps = overwrite ? evlist__overwrite_mmap(evlist) : evlist__mmap(evlist);
|
| 957 |
|
| 958 | if (!maps) {
|
| 959 | maps = evlist__alloc_mmap(evlist, overwrite);
|
| 960 | if (!maps) {
|
| 961 | from_list_end(evlist);
|
| 962 | return NULL;
|
| 963 | }
|
| 964 |
|
| 965 | if (overwrite) {
|
| 966 | RC_CHK_ACCESS(evlist)->overwrite_mmap = maps;
|
| 967 | if (evlist__bkw_mmap_state(evlist) == BKW_MMAP_NOTREADY)
|
| 968 | evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
|
| 969 | } else {
|
| 970 | RC_CHK_ACCESS(evlist)->mmap = maps;
|
| 971 | }
|
| 972 | }
|
| 973 | from_list_end(evlist);
|
| 974 | return &maps[idx].core;
|
| 975 | }
|
| 976 |
|
| 977 | static int
|
| 978 | perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
|
| 979 | int output, struct perf_cpu cpu)
|
| 980 | {
|
| 981 | struct mmap *map = container_of(_map, struct mmap, core);
|
| 982 | struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
|
| 983 |
|
| 984 | return mmap__mmap(map, mp, output, cpu);
|
| 985 | }
|
| 986 |
|
| 987 | unsigned long perf_event_mlock_kb_in_pages(void)
|
| 988 | {
|
| 989 | unsigned long pages;
|
| 990 | int max;
|
| 991 |
|
| 992 | if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
|
| 993 | |
| 994 | * Pick a once upon a time good value, i.e. things look
|
| 995 | * strange since we can't read a sysctl value, but lets not
|
| 996 | * die yet...
|
| 997 |
|
| 998 | max = 512;
|
| 999 | } else {
|
| 1000 | max -= (page_size / 1024);
|
| 1001 | }
|
| 1002 |
|
| 1003 | pages = (max * 1024) / page_size;
|
| 1004 | if (!is_power_of_2(pages))
|
| 1005 | pages = rounddown_pow_of_two(pages);
|
| 1006 |
|
| 1007 | return pages;
|
| 1008 | }
|
| 1009 |
|
| 1010 | size_t evlist__mmap_size(unsigned long pages)
|
| 1011 | {
|
| 1012 | if (pages == UINT_MAX)
|
| 1013 | pages = perf_event_mlock_kb_in_pages();
|
| 1014 | else if (!is_power_of_2(pages))
|
| 1015 | return 0;
|
| 1016 |
|
| 1017 | return (pages + 1) * page_size;
|
| 1018 | }
|
| 1019 |
|
| 1020 | static long parse_pages_arg(const char *str, unsigned long min,
|
| 1021 | unsigned long max)
|
| 1022 | {
|
| 1023 | unsigned long pages, val;
|
| 1024 | static struct parse_tag tags[] = {
|
| 1025 | { .tag = 'B', .mult = 1 },
|
| 1026 | { .tag = 'K', .mult = 1 << 10 },
|
| 1027 | { .tag = 'M', .mult = 1 << 20 },
|
| 1028 | { .tag = 'G', .mult = 1 << 30 },
|
| 1029 | { .tag = 0 },
|
| 1030 | };
|
| 1031 |
|
| 1032 | if (str == NULL)
|
| 1033 | return -EINVAL;
|
| 1034 |
|
| 1035 | val = parse_tag_value(str, tags);
|
| 1036 | if (val != (unsigned long) -1) {
|
| 1037 |
|
| 1038 | pages = PERF_ALIGN(val, page_size) / page_size;
|
| 1039 | } else {
|
| 1040 |
|
| 1041 | char *eptr;
|
| 1042 | pages = strtoul(str, &eptr, 10);
|
| 1043 | if (*eptr != '\0')
|
| 1044 | return -EINVAL;
|
| 1045 | }
|
| 1046 |
|
| 1047 | if (pages == 0 && min == 0) {
|
| 1048 |
|
| 1049 | } else if (!is_power_of_2(pages)) {
|
| 1050 | char buf[100];
|
| 1051 |
|
| 1052 |
|
| 1053 | pages = roundup_pow_of_two(pages);
|
| 1054 | if (!pages)
|
| 1055 | return -EINVAL;
|
| 1056 |
|
| 1057 | unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
|
| 1058 | pr_info("rounding mmap pages size to %s (%lu pages)\n",
|
| 1059 | buf, pages);
|
| 1060 | }
|
| 1061 |
|
| 1062 | if (pages > max)
|
| 1063 | return -EINVAL;
|
| 1064 |
|
| 1065 | return pages;
|
| 1066 | }
|
| 1067 |
|
| 1068 | int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
|
| 1069 | {
|
| 1070 | unsigned long max = UINT_MAX;
|
| 1071 | long pages;
|
| 1072 |
|
| 1073 | if (max > SIZE_MAX / page_size)
|
| 1074 | max = SIZE_MAX / page_size;
|
| 1075 |
|
| 1076 | pages = parse_pages_arg(str, 1, max);
|
| 1077 | if (pages < 0) {
|
| 1078 | pr_err("Invalid argument for --mmap_pages/-m\n");
|
| 1079 | return -1;
|
| 1080 | }
|
| 1081 |
|
| 1082 | *mmap_pages = pages;
|
| 1083 | return 0;
|
| 1084 | }
|
| 1085 |
|
| 1086 | int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
|
| 1087 | {
|
| 1088 | return __evlist__parse_mmap_pages(opt->value, str);
|
| 1089 | }
|
| 1090 |
|
| 1091 | |
| 1092 | * evlist__mmap_ex - Create mmaps to receive events.
|
| 1093 | * @evlist: list of events
|
| 1094 | * @pages: map length in pages
|
| 1095 | * @overwrite: overwrite older events?
|
| 1096 | * @auxtrace_pages - auxtrace map length in pages
|
| 1097 | * @auxtrace_overwrite - overwrite older auxtrace data?
|
| 1098 | *
|
| 1099 | * If @overwrite is %false the user needs to signal event consumption using
|
| 1100 | * perf_mmap__write_tail(). Using evlist__mmap_read() does this
|
| 1101 | * automatically.
|
| 1102 | *
|
| 1103 | * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
|
| 1104 | * consumption using auxtrace_mmap__write_tail().
|
| 1105 | *
|
| 1106 | * Return: %0 on success, negative error code otherwise.
|
| 1107 |
|
| 1108 | int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
|
| 1109 | unsigned int auxtrace_pages,
|
| 1110 | bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
|
| 1111 | int comp_level)
|
| 1112 | {
|
| 1113 | |
| 1114 | * Delay setting mp.prot: set it before calling perf_mmap__mmap.
|
| 1115 | * Its value is decided by evsel's write_backward.
|
| 1116 | * So &mp should not be passed through const pointer.
|
| 1117 |
|
| 1118 | struct mmap_params mp = {
|
| 1119 | .nr_cblocks = nr_cblocks,
|
| 1120 | .affinity = affinity,
|
| 1121 | .flush = flush,
|
| 1122 | .comp_level = comp_level
|
| 1123 | };
|
| 1124 | struct perf_evlist_mmap_ops ops = {
|
| 1125 | .idx = perf_evlist__mmap_cb_idx,
|
| 1126 | .get = perf_evlist__mmap_cb_get,
|
| 1127 | .mmap = perf_evlist__mmap_cb_mmap,
|
| 1128 | };
|
| 1129 |
|
| 1130 | evlist__core(evlist)->mmap_len = evlist__mmap_size(pages);
|
| 1131 | pr_debug("mmap size %zuB\n", evlist__core(evlist)->mmap_len);
|
| 1132 |
|
| 1133 | auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist__core(evlist)->mmap_len,
|
| 1134 | auxtrace_pages, auxtrace_overwrite);
|
| 1135 |
|
| 1136 | return perf_evlist__mmap_ops(evlist__core(evlist), &ops, &mp.core);
|
| 1137 | }
|
| 1138 |
|
| 1139 | int evlist__do_mmap(struct evlist *evlist, unsigned int pages)
|
| 1140 | {
|
| 1141 | return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
|
| 1142 | }
|
| 1143 |
|
| 1144 | int evlist__create_maps(struct evlist *evlist, struct target *target)
|
| 1145 | {
|
| 1146 | bool all_threads = (target->per_thread && target->system_wide);
|
| 1147 | struct perf_cpu_map *cpus;
|
| 1148 | struct perf_thread_map *threads;
|
| 1149 |
|
| 1150 | |
| 1151 | * If specify '-a' and '--per-thread' to perf record, perf record
|
| 1152 | * will override '--per-thread'. target->per_thread = false and
|
| 1153 | * target->system_wide = true.
|
| 1154 | *
|
| 1155 | * If specify '--per-thread' only to perf record,
|
| 1156 | * target->per_thread = true and target->system_wide = false.
|
| 1157 | *
|
| 1158 | * So target->per_thread && target->system_wide is false.
|
| 1159 | * For perf record, thread_map__new_str doesn't call
|
| 1160 | * thread_map__new_all_cpus. That will keep perf record's
|
| 1161 | * current behavior.
|
| 1162 | *
|
| 1163 | * For perf stat, it allows the case that target->per_thread and
|
| 1164 | * target->system_wide are all true. It means to collect system-wide
|
| 1165 | * per-thread data. thread_map__new_str will call
|
| 1166 | * thread_map__new_all_cpus to enumerate all threads.
|
| 1167 |
|
| 1168 | threads = thread_map__new_str(target->pid, target->tid, all_threads);
|
| 1169 |
|
| 1170 | if (!threads)
|
| 1171 | return -1;
|
| 1172 |
|
| 1173 | if (target__uses_dummy_map(target) && !evlist__has_bpf_output(evlist))
|
| 1174 | cpus = perf_cpu_map__new_any_cpu();
|
| 1175 | else
|
| 1176 | cpus = perf_cpu_map__new(target->cpu_list);
|
| 1177 |
|
| 1178 | if (!cpus)
|
| 1179 | goto out_delete_threads;
|
| 1180 |
|
| 1181 | evlist__core(evlist)->has_user_cpus = !!target->cpu_list;
|
| 1182 |
|
| 1183 | perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
|
| 1184 |
|
| 1185 |
|
| 1186 | perf_cpu_map__put(cpus);
|
| 1187 | perf_thread_map__put(threads);
|
| 1188 |
|
| 1189 | return 0;
|
| 1190 |
|
| 1191 | out_delete_threads:
|
| 1192 | perf_thread_map__put(threads);
|
| 1193 | return -1;
|
| 1194 | }
|
| 1195 |
|
| 1196 | int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel,
|
| 1197 | struct target *target)
|
| 1198 | {
|
| 1199 | struct evsel *evsel;
|
| 1200 | int err = 0;
|
| 1201 |
|
| 1202 | evlist__for_each_entry(evlist, evsel) {
|
| 1203 | |
| 1204 | * filters only work for tracepoint event, which doesn't have cpu limit.
|
| 1205 | * So evlist and evsel should always be same.
|
| 1206 |
|
| 1207 | if (evsel->filter) {
|
| 1208 | err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
|
| 1209 | if (err) {
|
| 1210 | *err_evsel = evsel;
|
| 1211 | break;
|
| 1212 | }
|
| 1213 | }
|
| 1214 |
|
| 1215 | |
| 1216 | * non-tracepoint events can have BPF filters.
|
| 1217 |
|
| 1218 | if (!list_empty(&evsel->bpf_filters)) {
|
| 1219 | err = perf_bpf_filter__prepare(evsel, target);
|
| 1220 | if (err) {
|
| 1221 | *err_evsel = evsel;
|
| 1222 | break;
|
| 1223 | }
|
| 1224 | }
|
| 1225 | }
|
| 1226 |
|
| 1227 | return err;
|
| 1228 | }
|
| 1229 |
|
| 1230 | int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
|
| 1231 | {
|
| 1232 | struct evsel *evsel;
|
| 1233 | int err = 0;
|
| 1234 |
|
| 1235 | if (filter == NULL)
|
| 1236 | return -1;
|
| 1237 |
|
| 1238 | evlist__for_each_entry(evlist, evsel) {
|
| 1239 | if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
|
| 1240 | continue;
|
| 1241 |
|
| 1242 | err = evsel__set_filter(evsel, filter);
|
| 1243 | if (err)
|
| 1244 | break;
|
| 1245 | }
|
| 1246 |
|
| 1247 | return err;
|
| 1248 | }
|
| 1249 |
|
| 1250 | int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
|
| 1251 | {
|
| 1252 | struct evsel *evsel;
|
| 1253 | int err = 0;
|
| 1254 |
|
| 1255 | if (filter == NULL)
|
| 1256 | return -1;
|
| 1257 |
|
| 1258 | evlist__for_each_entry(evlist, evsel) {
|
| 1259 | if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
|
| 1260 | continue;
|
| 1261 |
|
| 1262 | err = evsel__append_tp_filter(evsel, filter);
|
| 1263 | if (err)
|
| 1264 | break;
|
| 1265 | }
|
| 1266 |
|
| 1267 | return err;
|
| 1268 | }
|
| 1269 |
|
| 1270 | char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
|
| 1271 | {
|
| 1272 | char *filter;
|
| 1273 | size_t i;
|
| 1274 |
|
| 1275 | for (i = 0; i < npids; ++i) {
|
| 1276 | if (i == 0) {
|
| 1277 | if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
|
| 1278 | return NULL;
|
| 1279 | } else {
|
| 1280 | char *tmp;
|
| 1281 |
|
| 1282 | if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
|
| 1283 | goto out_free;
|
| 1284 |
|
| 1285 | free(filter);
|
| 1286 | filter = tmp;
|
| 1287 | }
|
| 1288 | }
|
| 1289 |
|
| 1290 | return filter;
|
| 1291 | out_free:
|
| 1292 | free(filter);
|
| 1293 | return NULL;
|
| 1294 | }
|
| 1295 |
|
| 1296 | int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
|
| 1297 | {
|
| 1298 | char *filter = asprintf__tp_filter_pids(npids, pids);
|
| 1299 | int ret = evlist__set_tp_filter(evlist, filter);
|
| 1300 |
|
| 1301 | free(filter);
|
| 1302 | return ret;
|
| 1303 | }
|
| 1304 |
|
| 1305 | int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
|
| 1306 | {
|
| 1307 | char *filter = asprintf__tp_filter_pids(npids, pids);
|
| 1308 | int ret = evlist__append_tp_filter(evlist, filter);
|
| 1309 |
|
| 1310 | free(filter);
|
| 1311 | return ret;
|
| 1312 | }
|
| 1313 |
|
| 1314 | int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
|
| 1315 | {
|
| 1316 | return evlist__append_tp_filter_pids(evlist, 1, &pid);
|
| 1317 | }
|
| 1318 |
|
| 1319 | bool evlist__valid_sample_type(struct evlist *evlist)
|
| 1320 | {
|
| 1321 | struct evsel *pos;
|
| 1322 |
|
| 1323 | if (evlist__nr_entries(evlist) == 1)
|
| 1324 | return true;
|
| 1325 |
|
| 1326 | if (evlist__id_pos(evlist) < 0 || evlist__is_pos(evlist) < 0)
|
| 1327 | return false;
|
| 1328 |
|
| 1329 | evlist__for_each_entry(evlist, pos) {
|
| 1330 | if (pos->id_pos != evlist__id_pos(evlist) ||
|
| 1331 | pos->is_pos != evlist__is_pos(evlist))
|
| 1332 | return false;
|
| 1333 | }
|
| 1334 |
|
| 1335 | return true;
|
| 1336 | }
|
| 1337 |
|
| 1338 | u64 __evlist__combined_sample_type(struct evlist *evlist)
|
| 1339 | {
|
| 1340 | struct evsel *evsel;
|
| 1341 |
|
| 1342 | if (RC_CHK_ACCESS(evlist)->combined_sample_type)
|
| 1343 | return RC_CHK_ACCESS(evlist)->combined_sample_type;
|
| 1344 |
|
| 1345 | evlist__for_each_entry(evlist, evsel)
|
| 1346 | RC_CHK_ACCESS(evlist)->combined_sample_type |= evsel->core.attr.sample_type;
|
| 1347 |
|
| 1348 | return RC_CHK_ACCESS(evlist)->combined_sample_type;
|
| 1349 | }
|
| 1350 |
|
| 1351 | u64 evlist__combined_sample_type(struct evlist *evlist)
|
| 1352 | {
|
| 1353 | RC_CHK_ACCESS(evlist)->combined_sample_type = 0;
|
| 1354 | return __evlist__combined_sample_type(evlist);
|
| 1355 | }
|
| 1356 |
|
| 1357 | u64 evlist__combined_branch_type(struct evlist *evlist)
|
| 1358 | {
|
| 1359 | struct evsel *evsel;
|
| 1360 | u64 branch_type = 0;
|
| 1361 |
|
| 1362 | evlist__for_each_entry(evlist, evsel)
|
| 1363 | branch_type |= evsel->core.attr.branch_sample_type;
|
| 1364 | return branch_type;
|
| 1365 | }
|
| 1366 |
|
| 1367 | static struct evsel *
|
| 1368 | evlist__find_dup_event_from_prev(struct evlist *evlist, struct evsel *event)
|
| 1369 | {
|
| 1370 | struct evsel *pos;
|
| 1371 |
|
| 1372 | evlist__for_each_entry(evlist, pos) {
|
| 1373 | if (event == pos)
|
| 1374 | break;
|
| 1375 | if ((pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) &&
|
| 1376 | !strcmp(pos->name, event->name))
|
| 1377 | return pos;
|
| 1378 | }
|
| 1379 | return NULL;
|
| 1380 | }
|
| 1381 |
|
| 1382 | #define MAX_NR_ABBR_NAME (26 * 11)
|
| 1383 |
|
| 1384 | |
| 1385 | * The abbr name is from A to Z9. If the number of event
|
| 1386 | * which requires the branch counter > MAX_NR_ABBR_NAME,
|
| 1387 | * return NA.
|
| 1388 |
|
| 1389 | static void evlist__new_abbr_name(char *name)
|
| 1390 | {
|
| 1391 | static int idx;
|
| 1392 | int i = idx / 26;
|
| 1393 |
|
| 1394 | if (idx >= MAX_NR_ABBR_NAME) {
|
| 1395 | name[0] = 'N';
|
| 1396 | name[1] = 'A';
|
| 1397 | name[2] = '\0';
|
| 1398 | return;
|
| 1399 | }
|
| 1400 |
|
| 1401 | name[0] = 'A' + (idx % 26);
|
| 1402 |
|
| 1403 | if (!i)
|
| 1404 | name[1] = '\0';
|
| 1405 | else {
|
| 1406 | name[1] = '0' + i - 1;
|
| 1407 | name[2] = '\0';
|
| 1408 | }
|
| 1409 |
|
| 1410 | idx++;
|
| 1411 | }
|
| 1412 |
|
| 1413 | void evlist__update_br_cntr(struct evlist *evlist)
|
| 1414 | {
|
| 1415 | struct evsel *evsel, *dup;
|
| 1416 | int i = 0;
|
| 1417 |
|
| 1418 | evlist__for_each_entry(evlist, evsel) {
|
| 1419 | if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) {
|
| 1420 | evsel->br_cntr_idx = i++;
|
| 1421 | evsel__leader(evsel)->br_cntr_nr++;
|
| 1422 |
|
| 1423 | dup = evlist__find_dup_event_from_prev(evlist, evsel);
|
| 1424 | if (dup)
|
| 1425 | memcpy(evsel->abbr_name, dup->abbr_name, 3 * sizeof(char));
|
| 1426 | else
|
| 1427 | evlist__new_abbr_name(evsel->abbr_name);
|
| 1428 | }
|
| 1429 | }
|
| 1430 | evlist__set_nr_br_cntr(evlist, i);
|
| 1431 | }
|
| 1432 |
|
| 1433 | bool evlist__valid_read_format(struct evlist *evlist)
|
| 1434 | {
|
| 1435 | struct evsel *first = evlist__first(evlist), *pos = first;
|
| 1436 | u64 read_format = first->core.attr.read_format;
|
| 1437 | u64 sample_type = first->core.attr.sample_type;
|
| 1438 |
|
| 1439 | evlist__for_each_entry(evlist, pos) {
|
| 1440 | if (read_format != pos->core.attr.read_format) {
|
| 1441 | pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
|
| 1442 | read_format, (u64)pos->core.attr.read_format);
|
| 1443 | }
|
| 1444 | }
|
| 1445 |
|
| 1446 |
|
| 1447 | if ((sample_type & PERF_SAMPLE_READ) &&
|
| 1448 | !(read_format & PERF_FORMAT_ID)) {
|
| 1449 | return false;
|
| 1450 | }
|
| 1451 |
|
| 1452 | return true;
|
| 1453 | }
|
| 1454 |
|
| 1455 | u16 evlist__id_hdr_size(struct evlist *evlist)
|
| 1456 | {
|
| 1457 | struct evsel *first = evlist__first(evlist);
|
| 1458 |
|
| 1459 | return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
|
| 1460 | }
|
| 1461 |
|
| 1462 | bool evlist__valid_sample_id_all(struct evlist *evlist)
|
| 1463 | {
|
| 1464 | struct evsel *first = evlist__first(evlist), *pos = first;
|
| 1465 |
|
| 1466 | evlist__for_each_entry_continue(evlist, pos) {
|
| 1467 | if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
|
| 1468 | return false;
|
| 1469 | }
|
| 1470 |
|
| 1471 | return true;
|
| 1472 | }
|
| 1473 |
|
| 1474 | bool evlist__sample_id_all(struct evlist *evlist)
|
| 1475 | {
|
| 1476 | struct evsel *first = evlist__first(evlist);
|
| 1477 | return first->core.attr.sample_id_all;
|
| 1478 | }
|
| 1479 |
|
| 1480 | void evlist__close(struct evlist *evlist)
|
| 1481 | {
|
| 1482 | struct evsel *evsel;
|
| 1483 | struct evlist_cpu_iterator evlist_cpu_itr;
|
| 1484 |
|
| 1485 | evlist__for_each_cpu(evlist_cpu_itr, evlist) {
|
| 1486 | if (evlist_cpu_itr.cpu_map_idx == 0 && evsel__is_retire_lat(evlist_cpu_itr.evsel))
|
| 1487 | evsel__tpebs_close(evlist_cpu_itr.evsel);
|
| 1488 | perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
|
| 1489 | evlist_cpu_itr.cpu_map_idx);
|
| 1490 | }
|
| 1491 |
|
| 1492 | evlist__for_each_entry_reverse(evlist, evsel) {
|
| 1493 | perf_evsel__free_fd(&evsel->core);
|
| 1494 | perf_evsel__free_id(&evsel->core);
|
| 1495 | }
|
| 1496 | perf_evlist__reset_id_hash(evlist__core(evlist));
|
| 1497 | }
|
| 1498 |
|
| 1499 | static int evlist__create_syswide_maps(struct evlist *evlist)
|
| 1500 | {
|
| 1501 | struct perf_cpu_map *cpus;
|
| 1502 | struct perf_thread_map *threads;
|
| 1503 |
|
| 1504 | |
| 1505 | * Try reading /sys/devices/system/cpu/online to get
|
| 1506 | * an all cpus map.
|
| 1507 | *
|
| 1508 | * FIXME: -ENOMEM is the best we can do here, the cpu_map
|
| 1509 | * code needs an overhaul to properly forward the
|
| 1510 | * error, and we may not want to do that fallback to a
|
| 1511 | * default cpu identity map :-\
|
| 1512 |
|
| 1513 | cpus = perf_cpu_map__new_online_cpus();
|
| 1514 | if (!cpus)
|
| 1515 | return -ENOMEM;
|
| 1516 |
|
| 1517 | threads = perf_thread_map__new_dummy();
|
| 1518 | if (!threads) {
|
| 1519 | perf_cpu_map__put(cpus);
|
| 1520 | return -ENOMEM;
|
| 1521 | }
|
| 1522 |
|
| 1523 | perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
|
| 1524 | perf_thread_map__put(threads);
|
| 1525 | perf_cpu_map__put(cpus);
|
| 1526 | return 0;
|
| 1527 | }
|
| 1528 |
|
| 1529 | int evlist__open(struct evlist *evlist)
|
| 1530 | {
|
| 1531 | struct evsel *evsel;
|
| 1532 | int err;
|
| 1533 |
|
| 1534 | |
| 1535 | * Default: one fd per CPU, all threads, aka systemwide
|
| 1536 | * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
|
| 1537 |
|
| 1538 | if (evlist__core(evlist)->threads == NULL &&
|
| 1539 | evlist__core(evlist)->user_requested_cpus == NULL) {
|
| 1540 | err = evlist__create_syswide_maps(evlist);
|
| 1541 | if (err < 0)
|
| 1542 | goto out_err;
|
| 1543 | }
|
| 1544 |
|
| 1545 | evlist__update_id_pos(evlist);
|
| 1546 |
|
| 1547 | evlist__for_each_entry(evlist, evsel) {
|
| 1548 | err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
|
| 1549 | if (err < 0)
|
| 1550 | goto out_err;
|
| 1551 | }
|
| 1552 |
|
| 1553 | return 0;
|
| 1554 | out_err:
|
| 1555 | evlist__close(evlist);
|
| 1556 | errno = -err;
|
| 1557 | return err;
|
| 1558 | }
|
| 1559 |
|
| 1560 | int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
|
| 1561 | bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
|
| 1562 | {
|
| 1563 | int child_ready_pipe[2], go_pipe[2];
|
| 1564 | char bf;
|
| 1565 |
|
| 1566 | if (target__none(target)) {
|
| 1567 | const char *cmd = strrchr(argv[0], '/');
|
| 1568 |
|
| 1569 | if (cmd)
|
| 1570 | cmd++;
|
| 1571 | else
|
| 1572 | cmd = argv[0];
|
| 1573 |
|
| 1574 | if (!strcmp(cmd, "sleep")) {
|
| 1575 | ui__warning("workload '%s' specified without the system-wide (-a) option\n",
|
| 1576 | cmd);
|
| 1577 | }
|
| 1578 | }
|
| 1579 |
|
| 1580 | evlist__set_workload_cork_fd(evlist, -1);
|
| 1581 |
|
| 1582 | if (pipe(child_ready_pipe) < 0) {
|
| 1583 | perror("failed to create 'ready' pipe");
|
| 1584 | return -1;
|
| 1585 | }
|
| 1586 |
|
| 1587 | if (pipe(go_pipe) < 0) {
|
| 1588 | perror("failed to create 'go' pipe");
|
| 1589 | goto out_close_ready_pipe;
|
| 1590 | }
|
| 1591 |
|
| 1592 | evlist__set_workload_pid(evlist, fork());
|
| 1593 | if (evlist__workload_pid(evlist) < 0) {
|
| 1594 | perror("failed to fork");
|
| 1595 | goto out_close_pipes;
|
| 1596 | }
|
| 1597 |
|
| 1598 | if (!evlist__workload_pid(evlist)) {
|
| 1599 | int ret;
|
| 1600 |
|
| 1601 | if (pipe_output)
|
| 1602 | dup2(2, 1);
|
| 1603 |
|
| 1604 | signal(SIGTERM, SIG_DFL);
|
| 1605 |
|
| 1606 | close(child_ready_pipe[0]);
|
| 1607 | close(go_pipe[1]);
|
| 1608 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
|
| 1609 |
|
| 1610 | |
| 1611 | * Change the name of this process not to confuse --exclude-perf users
|
| 1612 | * that sees 'perf' in the window up to the execvp() and thinks that
|
| 1613 | * perf samples are not being excluded.
|
| 1614 |
|
| 1615 | prctl(PR_SET_NAME, "perf-exec");
|
| 1616 |
|
| 1617 | |
| 1618 | * Tell the parent we're ready to go
|
| 1619 |
|
| 1620 | close(child_ready_pipe[1]);
|
| 1621 |
|
| 1622 | |
| 1623 | * Wait until the parent tells us to go.
|
| 1624 |
|
| 1625 | ret = read(go_pipe[0], &bf, 1);
|
| 1626 | |
| 1627 | * The parent will ask for the execvp() to be performed by
|
| 1628 | * writing exactly one byte, in workload.cork_fd, usually via
|
| 1629 | * evlist__start_workload().
|
| 1630 | *
|
| 1631 | * For cancelling the workload without actually running it,
|
| 1632 | * the parent will just close workload.cork_fd, without writing
|
| 1633 | * anything, i.e. read will return zero and we just exit()
|
| 1634 | * here (See evlist__cancel_workload()).
|
| 1635 |
|
| 1636 | if (ret != 1) {
|
| 1637 | if (ret == -1)
|
| 1638 | perror("unable to read pipe");
|
| 1639 | exit(ret);
|
| 1640 | }
|
| 1641 |
|
| 1642 | execvp(argv[0], (char **)argv);
|
| 1643 |
|
| 1644 | if (exec_error) {
|
| 1645 | union sigval val;
|
| 1646 |
|
| 1647 | val.sival_int = errno;
|
| 1648 | if (sigqueue(getppid(), SIGUSR1, val))
|
| 1649 | perror(argv[0]);
|
| 1650 | } else
|
| 1651 | perror(argv[0]);
|
| 1652 | exit(-1);
|
| 1653 | }
|
| 1654 |
|
| 1655 | if (exec_error) {
|
| 1656 | struct sigaction act = {
|
| 1657 | .sa_flags = SA_SIGINFO,
|
| 1658 | .sa_sigaction = exec_error,
|
| 1659 | };
|
| 1660 | sigaction(SIGUSR1, &act, NULL);
|
| 1661 | }
|
| 1662 |
|
| 1663 | if (target__none(target)) {
|
| 1664 | if (evlist__core(evlist)->threads == NULL) {
|
| 1665 | fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
|
| 1666 | __func__, __LINE__);
|
| 1667 | goto out_close_pipes;
|
| 1668 | }
|
| 1669 | perf_thread_map__set_pid(evlist__core(evlist)->threads, 0,
|
| 1670 | evlist__workload_pid(evlist));
|
| 1671 | }
|
| 1672 |
|
| 1673 | close(child_ready_pipe[1]);
|
| 1674 | close(go_pipe[0]);
|
| 1675 | |
| 1676 | * wait for child to settle
|
| 1677 |
|
| 1678 | if (read(child_ready_pipe[0], &bf, 1) == -1) {
|
| 1679 | perror("unable to read pipe");
|
| 1680 | goto out_close_pipes;
|
| 1681 | }
|
| 1682 |
|
| 1683 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
|
| 1684 | evlist__set_workload_cork_fd(evlist, go_pipe[1]);
|
| 1685 | close(child_ready_pipe[0]);
|
| 1686 | return 0;
|
| 1687 |
|
| 1688 | out_close_pipes:
|
| 1689 | close(go_pipe[0]);
|
| 1690 | close(go_pipe[1]);
|
| 1691 | out_close_ready_pipe:
|
| 1692 | close(child_ready_pipe[0]);
|
| 1693 | close(child_ready_pipe[1]);
|
| 1694 | return -1;
|
| 1695 | }
|
| 1696 |
|
| 1697 | int evlist__start_workload(struct evlist *evlist)
|
| 1698 | {
|
| 1699 | if (evlist__workload_cork_fd(evlist) >= 0) {
|
| 1700 | char bf = 0;
|
| 1701 | int ret;
|
| 1702 | |
| 1703 | * Remove the cork, let it rip!
|
| 1704 |
|
| 1705 | ret = write(evlist__workload_cork_fd(evlist), &bf, 1);
|
| 1706 | if (ret < 0)
|
| 1707 | perror("unable to write to pipe");
|
| 1708 |
|
| 1709 | close(evlist__workload_cork_fd(evlist));
|
| 1710 | evlist__set_workload_cork_fd(evlist, -1);
|
| 1711 | return ret;
|
| 1712 | }
|
| 1713 |
|
| 1714 | return 0;
|
| 1715 | }
|
| 1716 |
|
| 1717 | void evlist__cancel_workload(struct evlist *evlist)
|
| 1718 | {
|
| 1719 | int status;
|
| 1720 |
|
| 1721 | if (evlist__workload_cork_fd(evlist) >= 0) {
|
| 1722 | close(evlist__workload_cork_fd(evlist));
|
| 1723 | evlist__set_workload_cork_fd(evlist, -1);
|
| 1724 | waitpid(evlist__workload_pid(evlist), &status, WNOHANG);
|
| 1725 | }
|
| 1726 | }
|
| 1727 |
|
| 1728 | int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
|
| 1729 | {
|
| 1730 | struct evsel *evsel = evlist__event2evsel(evlist, event);
|
| 1731 | int ret;
|
| 1732 |
|
| 1733 | if (!evsel) {
|
| 1734 |
|
| 1735 | perf_sample__init(sample, false);
|
| 1736 | return -EFAULT;
|
| 1737 | }
|
| 1738 | ret = evsel__parse_sample(evsel, event, sample);
|
| 1739 | if (ret)
|
| 1740 | return ret;
|
| 1741 | if (perf_guest && sample->id) {
|
| 1742 | struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
|
| 1743 |
|
| 1744 | if (sid) {
|
| 1745 | sample->machine_pid = sid->machine_pid;
|
| 1746 | sample->vcpu = sid->vcpu.cpu;
|
| 1747 | }
|
| 1748 | }
|
| 1749 | return 0;
|
| 1750 | }
|
| 1751 |
|
| 1752 | int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
|
| 1753 | {
|
| 1754 | struct evsel *evsel = evlist__event2evsel(evlist, event);
|
| 1755 |
|
| 1756 | if (!evsel)
|
| 1757 | return -EFAULT;
|
| 1758 | return evsel__parse_sample_timestamp(evsel, event, timestamp);
|
| 1759 | }
|
| 1760 |
|
| 1761 | int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
|
| 1762 | {
|
| 1763 | int printed, value;
|
| 1764 |
|
| 1765 | switch (err) {
|
| 1766 | case EACCES:
|
| 1767 | case EPERM:
|
| 1768 | errno = err;
|
| 1769 | printed = scnprintf(buf, size,
|
| 1770 | "Error:\t%m.\n"
|
| 1771 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.");
|
| 1772 |
|
| 1773 | value = perf_event_paranoid();
|
| 1774 |
|
| 1775 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
|
| 1776 |
|
| 1777 | if (value >= 2) {
|
| 1778 | printed += scnprintf(buf + printed, size - printed,
|
| 1779 | "For your workloads it needs to be <= 1\nHint:\t");
|
| 1780 | }
|
| 1781 | printed += scnprintf(buf + printed, size - printed,
|
| 1782 | "For system wide tracing it needs to be set to -1.\n");
|
| 1783 |
|
| 1784 | printed += scnprintf(buf + printed, size - printed,
|
| 1785 | "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
|
| 1786 | "Hint:\tThe current value is %d.", value);
|
| 1787 | break;
|
| 1788 | case EINVAL: {
|
| 1789 | struct evsel *first = evlist__first(evlist);
|
| 1790 | int max_freq;
|
| 1791 |
|
| 1792 | if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
|
| 1793 | goto out_default;
|
| 1794 |
|
| 1795 | if (first->core.attr.sample_freq < (u64)max_freq)
|
| 1796 | goto out_default;
|
| 1797 |
|
| 1798 | errno = err;
|
| 1799 | printed = scnprintf(buf, size,
|
| 1800 | "Error:\t%m.\n"
|
| 1801 | "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
|
| 1802 | "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
|
| 1803 | max_freq, first->core.attr.sample_freq);
|
| 1804 | break;
|
| 1805 | }
|
| 1806 | default:
|
| 1807 | out_default:
|
| 1808 | errno = err;
|
| 1809 | scnprintf(buf, size, "%m");
|
| 1810 | break;
|
| 1811 | }
|
| 1812 |
|
| 1813 | return 0;
|
| 1814 | }
|
| 1815 |
|
| 1816 | int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
|
| 1817 | {
|
| 1818 | int pages_attempted = evlist__core(evlist)->mmap_len / 1024;
|
| 1819 | int pages_max_per_user, printed = 0;
|
| 1820 |
|
| 1821 | switch (err) {
|
| 1822 | case EPERM:
|
| 1823 | sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
|
| 1824 | errno = err;
|
| 1825 | printed += scnprintf(buf + printed, size - printed,
|
| 1826 | "Error:\t%m.\n"
|
| 1827 | "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
|
| 1828 | "Hint:\tTried using %zd kB.\n",
|
| 1829 | pages_max_per_user, pages_attempted);
|
| 1830 |
|
| 1831 | if (pages_attempted >= pages_max_per_user) {
|
| 1832 | printed += scnprintf(buf + printed, size - printed,
|
| 1833 | "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
|
| 1834 | pages_max_per_user + pages_attempted);
|
| 1835 | }
|
| 1836 |
|
| 1837 | printed += scnprintf(buf + printed, size - printed,
|
| 1838 | "Hint:\tTry using a smaller -m/--mmap-pages value.");
|
| 1839 | break;
|
| 1840 | default:
|
| 1841 | errno = err;
|
| 1842 | scnprintf(buf, size, "%m");
|
| 1843 | break;
|
| 1844 | }
|
| 1845 |
|
| 1846 | return 0;
|
| 1847 | }
|
| 1848 |
|
| 1849 | void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
|
| 1850 | {
|
| 1851 | struct evsel *evsel, *n;
|
| 1852 | LIST_HEAD(move);
|
| 1853 |
|
| 1854 | if (move_evsel == evlist__first(evlist))
|
| 1855 | return;
|
| 1856 |
|
| 1857 | evlist__for_each_entry_safe(evlist, n, evsel) {
|
| 1858 | if (evsel__leader(evsel) == evsel__leader(move_evsel))
|
| 1859 | list_move_tail(&evsel->core.node, &move);
|
| 1860 | }
|
| 1861 |
|
| 1862 | list_splice(&move, &evlist__core(evlist)->entries);
|
| 1863 | }
|
| 1864 |
|
| 1865 | struct evsel *evlist__get_tracking_event(struct evlist *evlist)
|
| 1866 | {
|
| 1867 | struct evsel *evsel;
|
| 1868 |
|
| 1869 | evlist__for_each_entry(evlist, evsel) {
|
| 1870 | if (evsel->tracking)
|
| 1871 | return evsel;
|
| 1872 | }
|
| 1873 |
|
| 1874 | return evlist__first(evlist);
|
| 1875 | }
|
| 1876 |
|
| 1877 | void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
|
| 1878 | {
|
| 1879 | struct evsel *evsel;
|
| 1880 |
|
| 1881 | if (tracking_evsel->tracking)
|
| 1882 | return;
|
| 1883 |
|
| 1884 | evlist__for_each_entry(evlist, evsel) {
|
| 1885 | if (evsel != tracking_evsel)
|
| 1886 | evsel->tracking = false;
|
| 1887 | }
|
| 1888 |
|
| 1889 | tracking_evsel->tracking = true;
|
| 1890 | }
|
| 1891 |
|
| 1892 | struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide)
|
| 1893 | {
|
| 1894 | struct evsel *evsel;
|
| 1895 |
|
| 1896 | evsel = evlist__get_tracking_event(evlist);
|
| 1897 | if (!evsel__is_dummy_event(evsel)) {
|
| 1898 | evsel = evlist__add_aux_dummy(evlist, system_wide);
|
| 1899 | if (!evsel)
|
| 1900 | return NULL;
|
| 1901 |
|
| 1902 | evlist__set_tracking_event(evlist, evsel);
|
| 1903 | } else if (system_wide) {
|
| 1904 | perf_evlist__go_system_wide(evlist__core(evlist), &evsel->core);
|
| 1905 | }
|
| 1906 |
|
| 1907 | return evsel;
|
| 1908 | }
|
| 1909 |
|
| 1910 | struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
|
| 1911 | {
|
| 1912 | struct evsel *evsel;
|
| 1913 |
|
| 1914 | evlist__for_each_entry(evlist, evsel) {
|
| 1915 | if (!evsel->name)
|
| 1916 | continue;
|
| 1917 | if (evsel__name_is(evsel, str))
|
| 1918 | return evsel;
|
| 1919 | }
|
| 1920 |
|
| 1921 | return NULL;
|
| 1922 | }
|
| 1923 |
|
| 1924 | void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
|
| 1925 | {
|
| 1926 | enum bkw_mmap_state old_state = evlist__bkw_mmap_state(evlist);
|
| 1927 | enum action {
|
| 1928 | NONE,
|
| 1929 | PAUSE,
|
| 1930 | RESUME,
|
| 1931 | } action = NONE;
|
| 1932 |
|
| 1933 | if (!evlist__overwrite_mmap(evlist))
|
| 1934 | return;
|
| 1935 |
|
| 1936 | switch (old_state) {
|
| 1937 | case BKW_MMAP_NOTREADY: {
|
| 1938 | if (state != BKW_MMAP_RUNNING)
|
| 1939 | goto state_err;
|
| 1940 | break;
|
| 1941 | }
|
| 1942 | case BKW_MMAP_RUNNING: {
|
| 1943 | if (state != BKW_MMAP_DATA_PENDING)
|
| 1944 | goto state_err;
|
| 1945 | action = PAUSE;
|
| 1946 | break;
|
| 1947 | }
|
| 1948 | case BKW_MMAP_DATA_PENDING: {
|
| 1949 | if (state != BKW_MMAP_EMPTY)
|
| 1950 | goto state_err;
|
| 1951 | break;
|
| 1952 | }
|
| 1953 | case BKW_MMAP_EMPTY: {
|
| 1954 | if (state != BKW_MMAP_RUNNING)
|
| 1955 | goto state_err;
|
| 1956 | action = RESUME;
|
| 1957 | break;
|
| 1958 | }
|
| 1959 | default:
|
| 1960 | WARN_ONCE(1, "Shouldn't get there\n");
|
| 1961 | }
|
| 1962 |
|
| 1963 | evlist__set_bkw_mmap_state(evlist, state);
|
| 1964 |
|
| 1965 | switch (action) {
|
| 1966 | case PAUSE:
|
| 1967 | evlist__pause(evlist);
|
| 1968 | break;
|
| 1969 | case RESUME:
|
| 1970 | evlist__resume(evlist);
|
| 1971 | break;
|
| 1972 | case NONE:
|
| 1973 | default:
|
| 1974 | break;
|
| 1975 | }
|
| 1976 |
|
| 1977 | state_err:
|
| 1978 | return;
|
| 1979 | }
|
| 1980 |
|
| 1981 | bool evlist__exclude_kernel(struct evlist *evlist)
|
| 1982 | {
|
| 1983 | struct evsel *evsel;
|
| 1984 |
|
| 1985 | evlist__for_each_entry(evlist, evsel) {
|
| 1986 | if (!evsel->core.attr.exclude_kernel)
|
| 1987 | return false;
|
| 1988 | }
|
| 1989 |
|
| 1990 | return true;
|
| 1991 | }
|
| 1992 |
|
| 1993 | |
| 1994 | * Events in data file are not collect in groups, but we still want
|
| 1995 | * the group display. Set the artificial group and set the leader's
|
| 1996 | * forced_leader flag to notify the display code.
|
| 1997 |
|
| 1998 | void evlist__force_leader(struct evlist *evlist)
|
| 1999 | {
|
| 2000 | if (evlist__nr_groups(evlist) == 0) {
|
| 2001 | struct evsel *leader = evlist__first(evlist);
|
| 2002 |
|
| 2003 | evlist__set_leader(evlist);
|
| 2004 | leader->forced_leader = true;
|
| 2005 | }
|
| 2006 | }
|
| 2007 |
|
| 2008 | struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
|
| 2009 | {
|
| 2010 | struct evsel *c2, *leader;
|
| 2011 | bool is_open = true;
|
| 2012 |
|
| 2013 | leader = evsel__leader(evsel);
|
| 2014 |
|
| 2015 | pr_debug("Weak group for %s/%d failed\n",
|
| 2016 | leader->name, leader->core.nr_members);
|
| 2017 |
|
| 2018 | |
| 2019 | * for_each_group_member doesn't work here because it doesn't
|
| 2020 | * include the first entry.
|
| 2021 |
|
| 2022 | evlist__for_each_entry(evsel_list, c2) {
|
| 2023 | if (c2 == evsel)
|
| 2024 | is_open = false;
|
| 2025 | if (evsel__has_leader(c2, leader)) {
|
| 2026 | if (is_open && close)
|
| 2027 | perf_evsel__close(&c2->core);
|
| 2028 | |
| 2029 | * We want to close all members of the group and reopen
|
| 2030 | * them. Some events, like Intel topdown, require being
|
| 2031 | * in a group and so keep these in the group.
|
| 2032 |
|
| 2033 | evsel__remove_from_group(c2, leader);
|
| 2034 |
|
| 2035 | |
| 2036 | * Set this for all former members of the group
|
| 2037 | * to indicate they get reopened.
|
| 2038 |
|
| 2039 | c2->reset_group = true;
|
| 2040 | }
|
| 2041 | }
|
| 2042 |
|
| 2043 | if (leader->core.nr_members == 1)
|
| 2044 | leader->core.nr_members = 0;
|
| 2045 | return leader;
|
| 2046 | }
|
| 2047 |
|
| 2048 | static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
|
| 2049 | {
|
| 2050 | char *s, *p;
|
| 2051 | int ret = 0, fd;
|
| 2052 |
|
| 2053 | if (strncmp(str, "fifo:", 5))
|
| 2054 | return -EINVAL;
|
| 2055 |
|
| 2056 | str += 5;
|
| 2057 | if (!*str || *str == ',')
|
| 2058 | return -EINVAL;
|
| 2059 |
|
| 2060 | s = strdup(str);
|
| 2061 | if (!s)
|
| 2062 | return -ENOMEM;
|
| 2063 |
|
| 2064 | p = strchr(s, ',');
|
| 2065 | if (p)
|
| 2066 | *p = '\0';
|
| 2067 |
|
| 2068 | |
| 2069 | * O_RDWR avoids POLLHUPs which is necessary to allow the other
|
| 2070 | * end of a FIFO to be repeatedly opened and closed.
|
| 2071 |
|
| 2072 | fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
| 2073 | if (fd < 0) {
|
| 2074 | ret = -errno;
|
| 2075 | pr_err("Failed to open '%s': %m\n", s);
|
| 2076 | goto out_free;
|
| 2077 | }
|
| 2078 | *ctl_fd = fd;
|
| 2079 | *ctl_fd_close = true;
|
| 2080 |
|
| 2081 | if (p && *++p) {
|
| 2082 |
|
| 2083 | fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
| 2084 | if (fd < 0) {
|
| 2085 | pr_err("Failed to open '%s': %m\n", p);
|
| 2086 | ret = -errno;
|
| 2087 | goto out_free;
|
| 2088 | }
|
| 2089 | *ctl_fd_ack = fd;
|
| 2090 | }
|
| 2091 |
|
| 2092 | out_free:
|
| 2093 | free(s);
|
| 2094 | return ret;
|
| 2095 | }
|
| 2096 |
|
| 2097 | int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
|
| 2098 | {
|
| 2099 | const char *comma = NULL;
|
| 2100 | char *endptr = NULL;
|
| 2101 |
|
| 2102 | *ctl_fd_close = false;
|
| 2103 |
|
| 2104 | if (strncmp(str, "fd:", 3))
|
| 2105 | return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
|
| 2106 |
|
| 2107 | *ctl_fd = strtoul(&str[3], &endptr, 0);
|
| 2108 | if (endptr == &str[3])
|
| 2109 | return -EINVAL;
|
| 2110 |
|
| 2111 | comma = strchr(str, ',');
|
| 2112 | if (comma) {
|
| 2113 | if (endptr != comma)
|
| 2114 | return -EINVAL;
|
| 2115 |
|
| 2116 | *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
|
| 2117 | if (endptr == comma + 1 || *endptr != '\0')
|
| 2118 | return -EINVAL;
|
| 2119 | }
|
| 2120 |
|
| 2121 | return 0;
|
| 2122 | }
|
| 2123 |
|
| 2124 | void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
|
| 2125 | {
|
| 2126 | if (*ctl_fd_close) {
|
| 2127 | *ctl_fd_close = false;
|
| 2128 | close(ctl_fd);
|
| 2129 | if (ctl_fd_ack >= 0)
|
| 2130 | close(ctl_fd_ack);
|
| 2131 | }
|
| 2132 | }
|
| 2133 |
|
| 2134 | int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
|
| 2135 | {
|
| 2136 | if (fd == -1) {
|
| 2137 | pr_debug("Control descriptor is not initialized\n");
|
| 2138 | return 0;
|
| 2139 | }
|
| 2140 |
|
| 2141 | evlist__set_ctl_fd_pos(evlist,
|
| 2142 | perf_evlist__add_pollfd(evlist__core(evlist), fd, NULL, POLLIN,
|
| 2143 | fdarray_flag__nonfilterable |
|
| 2144 | fdarray_flag__non_perf_event));
|
| 2145 | if (evlist__ctl_fd_pos(evlist) < 0) {
|
| 2146 | evlist__set_ctl_fd_pos(evlist, -1);
|
| 2147 | pr_err("Failed to add ctl fd entry: %m\n");
|
| 2148 | return -1;
|
| 2149 | }
|
| 2150 |
|
| 2151 | evlist__set_ctl_fd_fd(evlist, fd);
|
| 2152 | evlist__set_ctl_fd_ack(evlist, ack);
|
| 2153 |
|
| 2154 | return 0;
|
| 2155 | }
|
| 2156 |
|
| 2157 | bool evlist__ctlfd_initialized(struct evlist *evlist)
|
| 2158 | {
|
| 2159 | return evlist__ctl_fd_pos(evlist) >= 0;
|
| 2160 | }
|
| 2161 |
|
| 2162 | int evlist__finalize_ctlfd(struct evlist *evlist)
|
| 2163 | {
|
| 2164 | struct pollfd *entries = evlist__core(evlist)->pollfd.entries;
|
| 2165 |
|
| 2166 | if (!evlist__ctlfd_initialized(evlist))
|
| 2167 | return 0;
|
| 2168 |
|
| 2169 | entries[evlist__ctl_fd_pos(evlist)].fd = -1;
|
| 2170 | entries[evlist__ctl_fd_pos(evlist)].events = 0;
|
| 2171 | entries[evlist__ctl_fd_pos(evlist)].revents = 0;
|
| 2172 |
|
| 2173 | evlist__set_ctl_fd_pos(evlist, -1);
|
| 2174 | evlist__set_ctl_fd_ack(evlist, -1);
|
| 2175 | evlist__set_ctl_fd_fd(evlist, -1);
|
| 2176 |
|
| 2177 | return 0;
|
| 2178 | }
|
| 2179 |
|
| 2180 | static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
|
| 2181 | char *cmd_data, size_t data_size)
|
| 2182 | {
|
| 2183 | int err;
|
| 2184 | char c;
|
| 2185 | size_t bytes_read = 0;
|
| 2186 |
|
| 2187 | *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
|
| 2188 | memset(cmd_data, 0, data_size);
|
| 2189 | data_size--;
|
| 2190 |
|
| 2191 | do {
|
| 2192 | err = read(evlist__ctl_fd_fd(evlist), &c, 1);
|
| 2193 | if (err > 0) {
|
| 2194 | if (c == '\n' || c == '\0')
|
| 2195 | break;
|
| 2196 | cmd_data[bytes_read++] = c;
|
| 2197 | if (bytes_read == data_size)
|
| 2198 | break;
|
| 2199 | continue;
|
| 2200 | } else if (err == -1) {
|
| 2201 | if (errno == EINTR)
|
| 2202 | continue;
|
| 2203 | if (errno == EAGAIN || errno == EWOULDBLOCK)
|
| 2204 | err = 0;
|
| 2205 | else
|
| 2206 | pr_err("Failed to read from ctlfd %d: %m\n",
|
| 2207 | evlist__ctl_fd_fd(evlist));
|
| 2208 | }
|
| 2209 | break;
|
| 2210 | } while (1);
|
| 2211 |
|
| 2212 | pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
|
| 2213 | bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
|
| 2214 |
|
| 2215 | if (bytes_read > 0) {
|
| 2216 | if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
|
| 2217 | (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
|
| 2218 | *cmd = EVLIST_CTL_CMD_ENABLE;
|
| 2219 | } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
|
| 2220 | (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
|
| 2221 | *cmd = EVLIST_CTL_CMD_DISABLE;
|
| 2222 | } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
|
| 2223 | (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
|
| 2224 | *cmd = EVLIST_CTL_CMD_SNAPSHOT;
|
| 2225 | pr_debug("is snapshot\n");
|
| 2226 | } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
|
| 2227 | (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
|
| 2228 | *cmd = EVLIST_CTL_CMD_EVLIST;
|
| 2229 | } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
|
| 2230 | (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
|
| 2231 | *cmd = EVLIST_CTL_CMD_STOP;
|
| 2232 | } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
|
| 2233 | (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
|
| 2234 | *cmd = EVLIST_CTL_CMD_PING;
|
| 2235 | }
|
| 2236 | }
|
| 2237 |
|
| 2238 | return bytes_read ? (int)bytes_read : err;
|
| 2239 | }
|
| 2240 |
|
| 2241 | int evlist__ctlfd_ack(struct evlist *evlist)
|
| 2242 | {
|
| 2243 | int err;
|
| 2244 |
|
| 2245 | if (evlist__ctl_fd_ack(evlist) == -1)
|
| 2246 | return 0;
|
| 2247 |
|
| 2248 | err = write(evlist__ctl_fd_ack(evlist), EVLIST_CTL_CMD_ACK_TAG,
|
| 2249 | sizeof(EVLIST_CTL_CMD_ACK_TAG));
|
| 2250 | if (err == -1)
|
| 2251 | pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist__ctl_fd_ack(evlist));
|
| 2252 |
|
| 2253 | return err;
|
| 2254 | }
|
| 2255 |
|
| 2256 | static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
|
| 2257 | {
|
| 2258 | char *data = cmd_data + cmd_size;
|
| 2259 |
|
| 2260 |
|
| 2261 | if (!*data)
|
| 2262 | return 0;
|
| 2263 |
|
| 2264 |
|
| 2265 | if (*data == ' ') {
|
| 2266 | *arg = data + 1;
|
| 2267 | return 1;
|
| 2268 | }
|
| 2269 |
|
| 2270 |
|
| 2271 | return -1;
|
| 2272 | }
|
| 2273 |
|
| 2274 | static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
|
| 2275 | {
|
| 2276 | struct evsel *evsel;
|
| 2277 | char *name;
|
| 2278 | int err;
|
| 2279 |
|
| 2280 | err = get_cmd_arg(cmd_data,
|
| 2281 | enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
|
| 2282 | sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
|
| 2283 | &name);
|
| 2284 | if (err < 0) {
|
| 2285 | pr_info("failed: wrong command\n");
|
| 2286 | return -1;
|
| 2287 | }
|
| 2288 |
|
| 2289 | if (err) {
|
| 2290 | evsel = evlist__find_evsel_by_str(evlist, name);
|
| 2291 | if (evsel) {
|
| 2292 | if (enable)
|
| 2293 | evlist__enable_evsel(evlist, name);
|
| 2294 | else
|
| 2295 | evlist__disable_evsel(evlist, name);
|
| 2296 | pr_info("Event %s %s\n", evsel->name,
|
| 2297 | enable ? "enabled" : "disabled");
|
| 2298 | } else {
|
| 2299 | pr_info("failed: can't find '%s' event\n", name);
|
| 2300 | }
|
| 2301 | } else {
|
| 2302 | if (enable) {
|
| 2303 | evlist__enable(evlist);
|
| 2304 | pr_info(EVLIST_ENABLED_MSG);
|
| 2305 | } else {
|
| 2306 | evlist__disable(evlist);
|
| 2307 | pr_info(EVLIST_DISABLED_MSG);
|
| 2308 | }
|
| 2309 | }
|
| 2310 |
|
| 2311 | return 0;
|
| 2312 | }
|
| 2313 |
|
| 2314 | static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
|
| 2315 | {
|
| 2316 | struct perf_attr_details details = { .verbose = false, };
|
| 2317 | struct evsel *evsel;
|
| 2318 | char *arg;
|
| 2319 | int err;
|
| 2320 |
|
| 2321 | err = get_cmd_arg(cmd_data,
|
| 2322 | sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
|
| 2323 | &arg);
|
| 2324 | if (err < 0) {
|
| 2325 | pr_info("failed: wrong command\n");
|
| 2326 | return -1;
|
| 2327 | }
|
| 2328 |
|
| 2329 | if (err) {
|
| 2330 | if (!strcmp(arg, "-v")) {
|
| 2331 | details.verbose = true;
|
| 2332 | } else if (!strcmp(arg, "-g")) {
|
| 2333 | details.event_group = true;
|
| 2334 | } else if (!strcmp(arg, "-F")) {
|
| 2335 | details.freq = true;
|
| 2336 | } else {
|
| 2337 | pr_info("failed: wrong command\n");
|
| 2338 | return -1;
|
| 2339 | }
|
| 2340 | }
|
| 2341 |
|
| 2342 | evlist__for_each_entry(evlist, evsel)
|
| 2343 | evsel__fprintf(evsel, &details, stderr);
|
| 2344 |
|
| 2345 | return 0;
|
| 2346 | }
|
| 2347 |
|
| 2348 | int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
|
| 2349 | {
|
| 2350 | int err = 0;
|
| 2351 | char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
|
| 2352 | int ctlfd_pos = evlist__ctl_fd_pos(evlist);
|
| 2353 | struct pollfd *entries = evlist__core(evlist)->pollfd.entries;
|
| 2354 |
|
| 2355 | if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
|
| 2356 | return 0;
|
| 2357 |
|
| 2358 | if (entries[ctlfd_pos].revents & POLLIN) {
|
| 2359 | err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
|
| 2360 | EVLIST_CTL_CMD_MAX_LEN);
|
| 2361 | if (err > 0) {
|
| 2362 | switch (*cmd) {
|
| 2363 | case EVLIST_CTL_CMD_ENABLE:
|
| 2364 | case EVLIST_CTL_CMD_DISABLE:
|
| 2365 | err = evlist__ctlfd_enable(evlist, cmd_data,
|
| 2366 | *cmd == EVLIST_CTL_CMD_ENABLE);
|
| 2367 | break;
|
| 2368 | case EVLIST_CTL_CMD_EVLIST:
|
| 2369 | err = evlist__ctlfd_list(evlist, cmd_data);
|
| 2370 | break;
|
| 2371 | case EVLIST_CTL_CMD_SNAPSHOT:
|
| 2372 | case EVLIST_CTL_CMD_STOP:
|
| 2373 | case EVLIST_CTL_CMD_PING:
|
| 2374 | break;
|
| 2375 | case EVLIST_CTL_CMD_ACK:
|
| 2376 | case EVLIST_CTL_CMD_UNSUPPORTED:
|
| 2377 | default:
|
| 2378 | pr_debug("ctlfd: unsupported %d\n", *cmd);
|
| 2379 | break;
|
| 2380 | }
|
| 2381 | if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
|
| 2382 | *cmd == EVLIST_CTL_CMD_SNAPSHOT))
|
| 2383 | evlist__ctlfd_ack(evlist);
|
| 2384 | }
|
| 2385 | }
|
| 2386 |
|
| 2387 | if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
|
| 2388 | evlist__finalize_ctlfd(evlist);
|
| 2389 | else
|
| 2390 | entries[ctlfd_pos].revents = 0;
|
| 2391 |
|
| 2392 | return err;
|
| 2393 | }
|
| 2394 |
|
| 2395 | |
| 2396 | * struct event_enable_time - perf record -D/--delay single time range.
|
| 2397 | * @start: start of time range to enable events in milliseconds
|
| 2398 | * @end: end of time range to enable events in milliseconds
|
| 2399 | *
|
| 2400 | * N.B. this structure is also accessed as an array of int.
|
| 2401 |
|
| 2402 | struct event_enable_time {
|
| 2403 | int start;
|
| 2404 | int end;
|
| 2405 | };
|
| 2406 |
|
| 2407 | static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
|
| 2408 | {
|
| 2409 | const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
|
| 2410 | int ret, start, end, n;
|
| 2411 |
|
| 2412 | ret = sscanf(str, fmt, &start, &end, &n);
|
| 2413 | if (ret != 2 || end <= start)
|
| 2414 | return -EINVAL;
|
| 2415 | if (range) {
|
| 2416 | range->start = start;
|
| 2417 | range->end = end;
|
| 2418 | }
|
| 2419 | return n;
|
| 2420 | }
|
| 2421 |
|
| 2422 | static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
|
| 2423 | {
|
| 2424 | int incr = !!range;
|
| 2425 | bool first = true;
|
| 2426 | ssize_t ret, cnt;
|
| 2427 |
|
| 2428 | for (cnt = 0; *str; cnt++) {
|
| 2429 | ret = parse_event_enable_time(str, range, first);
|
| 2430 | if (ret < 0)
|
| 2431 | return ret;
|
| 2432 |
|
| 2433 | if (!first && range && range->start <= range[-1].end)
|
| 2434 | return -EINVAL;
|
| 2435 | str += ret;
|
| 2436 | range += incr;
|
| 2437 | first = false;
|
| 2438 | }
|
| 2439 | return cnt;
|
| 2440 | }
|
| 2441 |
|
| 2442 | |
| 2443 | * struct event_enable_timer - control structure for perf record -D/--delay.
|
| 2444 | * @evlist: event list
|
| 2445 | * @times: time ranges that events are enabled (N.B. this is also accessed as an
|
| 2446 | * array of int)
|
| 2447 | * @times_cnt: number of time ranges
|
| 2448 | * @timerfd: timer file descriptor
|
| 2449 | * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
|
| 2450 | * @times_step: current position in (int *)@times)[],
|
| 2451 | * refer event_enable_timer__process()
|
| 2452 | *
|
| 2453 | * Note, this structure is only used when there are time ranges, not when there
|
| 2454 | * is only an initial delay.
|
| 2455 |
|
| 2456 | struct event_enable_timer {
|
| 2457 | struct evlist *evlist;
|
| 2458 | struct event_enable_time *times;
|
| 2459 | size_t times_cnt;
|
| 2460 | int timerfd;
|
| 2461 | int pollfd_pos;
|
| 2462 | size_t times_step;
|
| 2463 | };
|
| 2464 |
|
| 2465 | static int str_to_delay(const char *str)
|
| 2466 | {
|
| 2467 | char *endptr;
|
| 2468 | long d;
|
| 2469 |
|
| 2470 | d = strtol(str, &endptr, 10);
|
| 2471 | if (*endptr || d > INT_MAX || d < -1)
|
| 2472 | return 0;
|
| 2473 | return d;
|
| 2474 | }
|
| 2475 |
|
| 2476 | int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
|
| 2477 | const char *str, int unset)
|
| 2478 | {
|
| 2479 | enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
|
| 2480 | struct event_enable_timer *eet;
|
| 2481 | ssize_t times_cnt;
|
| 2482 | ssize_t ret;
|
| 2483 | int err;
|
| 2484 |
|
| 2485 | if (unset)
|
| 2486 | return 0;
|
| 2487 |
|
| 2488 | opts->target.initial_delay = str_to_delay(str);
|
| 2489 | if (opts->target.initial_delay)
|
| 2490 | return 0;
|
| 2491 |
|
| 2492 | ret = parse_event_enable_times(str, NULL);
|
| 2493 | if (ret < 0)
|
| 2494 | return ret;
|
| 2495 |
|
| 2496 | times_cnt = ret;
|
| 2497 | if (times_cnt == 0)
|
| 2498 | return -EINVAL;
|
| 2499 |
|
| 2500 | eet = zalloc(sizeof(*eet));
|
| 2501 | if (!eet)
|
| 2502 | return -ENOMEM;
|
| 2503 |
|
| 2504 | eet->times = calloc(times_cnt, sizeof(*eet->times));
|
| 2505 | if (!eet->times) {
|
| 2506 | err = -ENOMEM;
|
| 2507 | goto free_eet;
|
| 2508 | }
|
| 2509 |
|
| 2510 | if (parse_event_enable_times(str, eet->times) != times_cnt) {
|
| 2511 | err = -EINVAL;
|
| 2512 | goto free_eet_times;
|
| 2513 | }
|
| 2514 |
|
| 2515 | eet->times_cnt = times_cnt;
|
| 2516 |
|
| 2517 | eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
|
| 2518 | if (eet->timerfd == -1) {
|
| 2519 | err = -errno;
|
| 2520 | pr_err("timerfd_create failed: %m\n");
|
| 2521 | goto free_eet_times;
|
| 2522 | }
|
| 2523 |
|
| 2524 | eet->pollfd_pos = perf_evlist__add_pollfd(evlist__core(evlist), eet->timerfd,
|
| 2525 | NULL, POLLIN, flags);
|
| 2526 | if (eet->pollfd_pos < 0) {
|
| 2527 | err = eet->pollfd_pos;
|
| 2528 | goto close_timerfd;
|
| 2529 | }
|
| 2530 |
|
| 2531 | eet->evlist = evlist;
|
| 2532 | RC_CHK_ACCESS(evlist)->eet = eet;
|
| 2533 | opts->target.initial_delay = eet->times[0].start;
|
| 2534 |
|
| 2535 | return 0;
|
| 2536 |
|
| 2537 | close_timerfd:
|
| 2538 | close(eet->timerfd);
|
| 2539 | free_eet_times:
|
| 2540 | zfree(&eet->times);
|
| 2541 | free_eet:
|
| 2542 | free(eet);
|
| 2543 | return err;
|
| 2544 | }
|
| 2545 |
|
| 2546 | static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
|
| 2547 | {
|
| 2548 | struct itimerspec its = {
|
| 2549 | .it_value.tv_sec = ms / MSEC_PER_SEC,
|
| 2550 | .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
|
| 2551 | };
|
| 2552 | int err = 0;
|
| 2553 |
|
| 2554 | if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
|
| 2555 | err = -errno;
|
| 2556 | pr_err("timerfd_settime failed: %m\n");
|
| 2557 | }
|
| 2558 | return err;
|
| 2559 | }
|
| 2560 |
|
| 2561 | int event_enable_timer__start(struct event_enable_timer *eet)
|
| 2562 | {
|
| 2563 | int ms;
|
| 2564 |
|
| 2565 | if (!eet)
|
| 2566 | return 0;
|
| 2567 |
|
| 2568 | ms = eet->times[0].end - eet->times[0].start;
|
| 2569 | eet->times_step = 1;
|
| 2570 |
|
| 2571 | return event_enable_timer__set_timer(eet, ms);
|
| 2572 | }
|
| 2573 |
|
| 2574 | int event_enable_timer__process(struct event_enable_timer *eet)
|
| 2575 | {
|
| 2576 | struct pollfd *entries;
|
| 2577 | short revents;
|
| 2578 |
|
| 2579 | if (!eet)
|
| 2580 | return 0;
|
| 2581 |
|
| 2582 | entries = evlist__core(eet->evlist)->pollfd.entries;
|
| 2583 | revents = entries[eet->pollfd_pos].revents;
|
| 2584 | entries[eet->pollfd_pos].revents = 0;
|
| 2585 |
|
| 2586 | if (revents & POLLIN) {
|
| 2587 | size_t step = eet->times_step;
|
| 2588 | size_t pos = step / 2;
|
| 2589 |
|
| 2590 | if (step & 1) {
|
| 2591 | evlist__disable_non_dummy(eet->evlist);
|
| 2592 | pr_info(EVLIST_DISABLED_MSG);
|
| 2593 | if (pos >= eet->times_cnt - 1) {
|
| 2594 |
|
| 2595 | event_enable_timer__set_timer(eet, 0);
|
| 2596 | return 1;
|
| 2597 | }
|
| 2598 | } else {
|
| 2599 | evlist__enable_non_dummy(eet->evlist);
|
| 2600 | pr_info(EVLIST_ENABLED_MSG);
|
| 2601 | }
|
| 2602 |
|
| 2603 | step += 1;
|
| 2604 | pos = step / 2;
|
| 2605 |
|
| 2606 | if (pos < eet->times_cnt) {
|
| 2607 | int *times = (int *)eet->times;
|
| 2608 | int ms = times[step] - times[step - 1];
|
| 2609 |
|
| 2610 | eet->times_step = step;
|
| 2611 | return event_enable_timer__set_timer(eet, ms);
|
| 2612 | }
|
| 2613 | }
|
| 2614 |
|
| 2615 | return 0;
|
| 2616 | }
|
| 2617 |
|
| 2618 | static void event_enable_timer__exit(struct event_enable_timer **ep)
|
| 2619 | {
|
| 2620 | if (!ep || !*ep)
|
| 2621 | return;
|
| 2622 | zfree(&(*ep)->times);
|
| 2623 | zfree(ep);
|
| 2624 | }
|
| 2625 |
|
| 2626 | struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
|
| 2627 | {
|
| 2628 | struct evsel *evsel;
|
| 2629 |
|
| 2630 | evlist__for_each_entry(evlist, evsel) {
|
| 2631 | if (evsel->core.idx == idx)
|
| 2632 | return evsel;
|
| 2633 | }
|
| 2634 | return NULL;
|
| 2635 | }
|
| 2636 |
|
| 2637 | void evlist__format_evsels(struct evlist *evlist, struct strbuf *sb, size_t max_length)
|
| 2638 | {
|
| 2639 | struct evsel *evsel, *leader = NULL;
|
| 2640 | bool first = true;
|
| 2641 |
|
| 2642 | evlist__for_each_entry(evlist, evsel) {
|
| 2643 | struct evsel *new_leader = evsel__leader(evsel);
|
| 2644 |
|
| 2645 | if (evsel__is_dummy_event(evsel))
|
| 2646 | continue;
|
| 2647 |
|
| 2648 | if (leader != new_leader && leader && leader->core.nr_members > 1)
|
| 2649 | strbuf_addch(sb, '}');
|
| 2650 |
|
| 2651 | if (!first)
|
| 2652 | strbuf_addch(sb, ',');
|
| 2653 |
|
| 2654 | if (sb->len > max_length) {
|
| 2655 | strbuf_addstr(sb, "...");
|
| 2656 | return;
|
| 2657 | }
|
| 2658 | if (leader != new_leader && new_leader->core.nr_members > 1)
|
| 2659 | strbuf_addch(sb, '{');
|
| 2660 |
|
| 2661 | strbuf_addstr(sb, evsel__name(evsel));
|
| 2662 | first = false;
|
| 2663 | leader = new_leader;
|
| 2664 | }
|
| 2665 | if (leader && leader->core.nr_members > 1)
|
| 2666 | strbuf_addch(sb, '}');
|
| 2667 | }
|
| 2668 |
|
| 2669 | void evlist__check_mem_load_aux(struct evlist *evlist)
|
| 2670 | {
|
| 2671 | struct evsel *leader, *evsel, *pos;
|
| 2672 |
|
| 2673 | |
| 2674 | * For some platforms, the 'mem-loads' event is required to use
|
| 2675 | * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
|
| 2676 | * must be the group leader. Now we disable this group before reporting
|
| 2677 | * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
|
| 2678 | * any valid memory load information.
|
| 2679 |
|
| 2680 | evlist__for_each_entry(evlist, evsel) {
|
| 2681 | leader = evsel__leader(evsel);
|
| 2682 | if (leader == evsel)
|
| 2683 | continue;
|
| 2684 |
|
| 2685 | if (leader->name && strstr(leader->name, "mem-loads-aux")) {
|
| 2686 | for_each_group_evsel(pos, leader) {
|
| 2687 | evsel__set_leader(pos, pos);
|
| 2688 | pos->core.nr_members = 0;
|
| 2689 | }
|
| 2690 | }
|
| 2691 | }
|
| 2692 | }
|
| 2693 |
|
| 2694 | |
| 2695 | * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
|
| 2696 | * and warn if the user CPU list is inapplicable for the event's PMU's
|
| 2697 | * CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
|
| 2698 | * user requested CPU and so any online CPU is applicable. Core PMUs handle
|
| 2699 | * events on the CPUs in their list and otherwise the event isn't supported.
|
| 2700 | * @evlist: The list of events being checked.
|
| 2701 | * @cpu_list: The user provided list of CPUs.
|
| 2702 |
|
| 2703 | void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
|
| 2704 | {
|
| 2705 | struct perf_cpu_map *user_requested_cpus;
|
| 2706 | struct evsel *pos;
|
| 2707 |
|
| 2708 | if (!cpu_list)
|
| 2709 | return;
|
| 2710 |
|
| 2711 | user_requested_cpus = perf_cpu_map__new(cpu_list);
|
| 2712 | if (!user_requested_cpus)
|
| 2713 | return;
|
| 2714 |
|
| 2715 | evlist__for_each_entry(evlist, pos) {
|
| 2716 | evsel__warn_user_requested_cpus(pos, user_requested_cpus);
|
| 2717 | }
|
| 2718 | perf_cpu_map__put(user_requested_cpus);
|
| 2719 | }
|
| 2720 |
|
| 2721 |
|
| 2722 | static bool evlist__disable_uniquify(struct evlist *evlist)
|
| 2723 | {
|
| 2724 | struct evsel *counter;
|
| 2725 | struct perf_pmu *last_pmu = NULL;
|
| 2726 | bool first = true;
|
| 2727 |
|
| 2728 | evlist__for_each_entry(evlist, counter) {
|
| 2729 |
|
| 2730 | if (!first && counter->pmu != last_pmu)
|
| 2731 | return false;
|
| 2732 | first = false;
|
| 2733 | if (counter->pmu) {
|
| 2734 |
|
| 2735 | if (!counter->pmu->is_core)
|
| 2736 | return false;
|
| 2737 |
|
| 2738 | if (perf_pmus__num_core_pmus() > 1)
|
| 2739 | return false;
|
| 2740 | }
|
| 2741 | last_pmu = counter->pmu;
|
| 2742 | }
|
| 2743 | return true;
|
| 2744 | }
|
| 2745 |
|
| 2746 | static bool evlist__set_needs_uniquify(struct evlist *evlist, const struct perf_stat_config *config)
|
| 2747 | {
|
| 2748 | struct evsel *counter;
|
| 2749 | bool needs_uniquify = false;
|
| 2750 |
|
| 2751 | if (evlist__disable_uniquify(evlist)) {
|
| 2752 | evlist__for_each_entry(evlist, counter)
|
| 2753 | counter->uniquified_name = true;
|
| 2754 | return false;
|
| 2755 | }
|
| 2756 |
|
| 2757 | evlist__for_each_entry(evlist, counter) {
|
| 2758 | if (evsel__set_needs_uniquify(counter, config))
|
| 2759 | needs_uniquify = true;
|
| 2760 | }
|
| 2761 | return needs_uniquify;
|
| 2762 | }
|
| 2763 |
|
| 2764 | void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf_stat_config *config)
|
| 2765 | {
|
| 2766 | if (evlist__set_needs_uniquify(evlist, config)) {
|
| 2767 | struct evsel *pos;
|
| 2768 |
|
| 2769 | evlist__for_each_entry(evlist, pos)
|
| 2770 | evsel__uniquify_counter(pos);
|
| 2771 | }
|
| 2772 | }
|
| 2773 |
|
| 2774 | bool evlist__has_bpf_output(struct evlist *evlist)
|
| 2775 | {
|
| 2776 | struct evsel *evsel;
|
| 2777 |
|
| 2778 | evlist__for_each_entry(evlist, evsel) {
|
| 2779 | if (evsel__is_bpf_output(evsel))
|
| 2780 | return true;
|
| 2781 | }
|
| 2782 |
|
| 2783 | return false;
|
| 2784 | }
|
| 2785 |
|
| 2786 | bool evlist__needs_bpf_sb_event(struct evlist *evlist)
|
| 2787 | {
|
| 2788 | struct evsel *evsel;
|
| 2789 |
|
| 2790 | evlist__for_each_entry(evlist, evsel) {
|
| 2791 | if (evsel__is_dummy_event(evsel))
|
| 2792 | continue;
|
| 2793 | if (!evsel->core.attr.exclude_kernel)
|
| 2794 | return true;
|
| 2795 | }
|
| 2796 |
|
| 2797 | return false;
|
| 2798 | }
|