Linux Audio

Check our new training course

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