Linux Audio

Check our new training course

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