Linux Audio

Check our new training course

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