evlist.c 50.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7
/*
 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
 *
 * Parts came from builtin-{top,stat,record}.c, see those files for further
 * copyright notes.
 */
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 17
#include "evlist.h"
#include "evsel.h"
Adrian Hunter's avatar
Adrian Hunter committed
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 "util/pmu.h"
31
#include <signal.h>
32
#include <unistd.h>
33
#include <sched.h>
34
#include <stdlib.h>
35

36
#include "parse-events.h"
37
#include <subcmd/parse-options.h>
38

39
#include <fcntl.h>
40
#include <sys/ioctl.h>
41
#include <sys/mman.h>
42
#include <sys/prctl.h>
43

44 45
#include <linux/bitops.h>
#include <linux/hash.h>
46
#include <linux/log2.h>
47
#include <linux/err.h>
48
#include <linux/string.h>
49
#include <linux/zalloc.h>
50
#include <perf/evlist.h>
51
#include <perf/evsel.h>
52
#include <perf/cpumap.h>
53
#include <perf/mmap.h>
54

55 56
#include <internal/xyarray.h>

57 58 59 60
#ifdef LACKS_SIGQUEUE_PROTOTYPE
int sigqueue(pid_t pid, int sig, const union sigval value);
#endif

61
#define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
62
#define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
63

64 65
void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
		  struct perf_thread_map *threads)
66
{
67
	perf_evlist__init(&evlist->core);
68
	perf_evlist__set_maps(&evlist->core, cpus, threads);
69
	evlist->workload.pid = -1;
70
	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
71 72 73
	evlist->ctl_fd.fd = -1;
	evlist->ctl_fd.ack = -1;
	evlist->ctl_fd.pos = -1;
74 75
}

76
struct evlist *evlist__new(void)
77
{
78
	struct evlist *evlist = zalloc(sizeof(*evlist));
79

80
	if (evlist != NULL)
81
		evlist__init(evlist, NULL, NULL);
82 83 84 85

	return evlist;
}

86
struct evlist *evlist__new_default(void)
87
{
88
	struct evlist *evlist = evlist__new();
89

90
	if (evlist && evlist__add_default(evlist)) {
91
		evlist__delete(evlist);
92 93 94 95 96 97
		evlist = NULL;
	}

	return evlist;
}

98
struct evlist *evlist__new_dummy(void)
99
{
100
	struct evlist *evlist = evlist__new();
101

102
	if (evlist && evlist__add_dummy(evlist)) {
103
		evlist__delete(evlist);
104 105 106 107 108 109
		evlist = NULL;
	}

	return evlist;
}

110
/**
111
 * evlist__set_id_pos - set the positions of event ids.
112 113 114 115 116
 * @evlist: selected event list
 *
 * Events with compatible sample types all have the same id_pos
 * and is_pos.  For convenience, put a copy on evlist.
 */
117
void evlist__set_id_pos(struct evlist *evlist)
118
{
119
	struct evsel *first = evlist__first(evlist);
120 121 122 123 124

	evlist->id_pos = first->id_pos;
	evlist->is_pos = first->is_pos;
}

125
static void evlist__update_id_pos(struct evlist *evlist)
126
{
127
	struct evsel *evsel;
128

129
	evlist__for_each_entry(evlist, evsel)
130
		evsel__calc_id_pos(evsel);
131

132
	evlist__set_id_pos(evlist);
133 134
}

135
static void evlist__purge(struct evlist *evlist)
136
{
137
	struct evsel *pos, *n;
138

139
	evlist__for_each_entry_safe(evlist, n, pos) {
140
		list_del_init(&pos->core.node);
141
		pos->evlist = NULL;
142
		evsel__delete(pos);
143 144
	}

145
	evlist->core.nr_entries = 0;
146 147
}

148
void evlist__exit(struct evlist *evlist)
149
{
150
	zfree(&evlist->mmap);
151
	zfree(&evlist->overwrite_mmap);
152
	perf_evlist__exit(&evlist->core);
153 154
}

155
void evlist__delete(struct evlist *evlist)
156
{
157 158 159
	if (evlist == NULL)
		return;

160
	evlist__munmap(evlist);
161
	evlist__close(evlist);
162
	evlist__purge(evlist);
163
	evlist__exit(evlist);
164 165 166
	free(evlist);
}

167
void evlist__add(struct evlist *evlist, struct evsel *entry)
168
{
169
	perf_evlist__add(&evlist->core, &entry->core);
170 171
	entry->evlist = evlist;
	entry->tracking = !entry->core.idx;
172 173

	if (evlist->core.nr_entries == 1)
174
		evlist__set_id_pos(evlist);
175 176
}

177
void evlist__remove(struct evlist *evlist, struct evsel *evsel)
178 179
{
	evsel->evlist = NULL;
180
	perf_evlist__remove(&evlist->core, &evsel->core);
181 182
}

183
void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
184
{
185 186 187 188 189 190 191 192 193 194 195
	while (!list_empty(list)) {
		struct evsel *evsel, *temp, *leader = NULL;

		__evlist__for_each_entry_safe(list, temp, evsel) {
			list_del_init(&evsel->core.node);
			evlist__add(evlist, evsel);
			leader = evsel;
			break;
		}

		__evlist__for_each_entry_safe(list, temp, evsel) {
196
			if (evsel__has_leader(evsel, leader)) {
197 198 199 200
				list_del_init(&evsel->core.node);
				evlist__add(evlist, evsel);
			}
		}
201
	}
202 203
}

204 205 206 207 208 209 210 211
int __evlist__set_tracepoints_handlers(struct evlist *evlist,
				       const struct evsel_str_handler *assocs, size_t nr_assocs)
{
	size_t i;
	int err;

	for (i = 0; i < nr_assocs; i++) {
		// Adding a handler for an event not in this evlist, just ignore it.
212
		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
213 214 215 216 217 218 219 220 221 222 223 224 225 226
		if (evsel == NULL)
			continue;

		err = -EEXIST;
		if (evsel->handler != NULL)
			goto out;
		evsel->handler = assocs[i].handler;
	}

	err = 0;
out:
	return err;
}

227
void evlist__set_leader(struct evlist *evlist)
228
{
229
	perf_evlist__set_leader(&evlist->core);
230 231
}

232
int __evlist__add_default(struct evlist *evlist, bool precise)
233
{
234
	struct evsel *evsel;
235

236 237
	evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
				  PERF_COUNT_HW_CPU_CYCLES);
238
	if (evsel == NULL)
239
		return -ENOMEM;
240

241
	evlist__add(evlist, evsel);
242 243
	return 0;
}
244

245
int evlist__add_dummy(struct evlist *evlist)
246 247 248 249 250 251
{
	struct perf_event_attr attr = {
		.type	= PERF_TYPE_SOFTWARE,
		.config = PERF_COUNT_SW_DUMMY,
		.size	= sizeof(attr), /* to capture ABI version */
	};
252
	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
253 254 255 256

	if (evsel == NULL)
		return -ENOMEM;

257
	evlist__add(evlist, evsel);
258 259 260
	return 0;
}

261
static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
262
{
263
	struct evsel *evsel, *n;
264 265 266 267
	LIST_HEAD(head);
	size_t i;

	for (i = 0; i < nr_attrs; i++) {
268
		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
269 270
		if (evsel == NULL)
			goto out_delete_partial_list;
271
		list_add_tail(&evsel->core.node, &head);
272 273
	}

274
	evlist__splice_list_tail(evlist, &head);
275 276 277 278

	return 0;

out_delete_partial_list:
279
	__evlist__for_each_entry_safe(&head, n, evsel)
280
		evsel__delete(evsel);
281 282 283
	return -1;
}

284
int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
285 286 287 288 289 290
{
	size_t i;

	for (i = 0; i < nr_attrs; i++)
		event_attr_init(attrs + i);

291
	return evlist__add_attrs(evlist, attrs, nr_attrs);
292 293
}

294 295 296 297 298
__weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
{
	return 0;
}

299
struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
300
{
301
	struct evsel *evsel;
302

303
	evlist__for_each_entry(evlist, evsel) {
304 305
		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
		    (int)evsel->core.attr.config == id)
306 307 308 309 310 311
			return evsel;
	}

	return NULL;
}

312
struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
313
{
314
	struct evsel *evsel;
315

316
	evlist__for_each_entry(evlist, evsel) {
317
		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
318 319 320 321 322 323 324
		    (strcmp(evsel->name, name) == 0))
			return evsel;
	}

	return NULL;
}

325
int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
326
{
327
	struct evsel *evsel = evsel__newtp(sys, name);
328

329
	if (IS_ERR(evsel))
330 331
		return -1;

332
	evsel->handler = handler;
333
	evlist__add(evlist, evsel);
334 335 336
	return 0;
}

337
static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
338
{
339
	if (evsel->core.system_wide)
340 341
		return 1;
	else
342
		return perf_thread_map__nr(evlist->core.threads);
343 344
}

345 346 347 348
struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
{
	struct evlist_cpu_iterator itr = {
		.container = evlist,
349
		.evsel = NULL,
350 351 352
		.cpu_map_idx = 0,
		.evlist_cpu_map_idx = 0,
		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
353
		.cpu = (struct perf_cpu){ .cpu = -1},
354 355
		.affinity = affinity,
	};
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	if (evlist__empty(evlist)) {
		/* Ensure the empty list doesn't iterate. */
		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
	} else {
		itr.evsel = evlist__first(evlist);
		if (itr.affinity) {
			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
			affinity__set(itr.affinity, itr.cpu.cpu);
			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
			/*
			 * If this CPU isn't in the evsel's cpu map then advance
			 * through the list.
			 */
			if (itr.cpu_map_idx == -1)
				evlist_cpu_iterator__next(&itr);
		}
373 374
	}
	return itr;
375 376
}

377
void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
378
{
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
		evlist_cpu_itr->cpu_map_idx =
			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
					  evlist_cpu_itr->cpu);
		if (evlist_cpu_itr->cpu_map_idx != -1)
			return;
	}
	evlist_cpu_itr->evlist_cpu_map_idx++;
	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
		evlist_cpu_itr->cpu =
			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
					  evlist_cpu_itr->evlist_cpu_map_idx);
		if (evlist_cpu_itr->affinity)
394
			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
395 396 397 398 399 400 401 402 403 404
		evlist_cpu_itr->cpu_map_idx =
			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
					  evlist_cpu_itr->cpu);
		/*
		 * If this CPU isn't in the evsel's cpu map then advance through
		 * the list.
		 */
		if (evlist_cpu_itr->cpu_map_idx == -1)
			evlist_cpu_iterator__next(evlist_cpu_itr);
	}
405 406
}

407
bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
408
{
409
	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
410 411
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
static int evsel__strcmp(struct evsel *pos, char *evsel_name)
{
	if (!evsel_name)
		return 0;
	if (evsel__is_dummy_event(pos))
		return 1;
	return strcmp(pos->name, evsel_name);
}

static int evlist__is_enabled(struct evlist *evlist)
{
	struct evsel *pos;

	evlist__for_each_entry(evlist, pos) {
		if (!evsel__is_group_leader(pos) || !pos->core.fd)
			continue;
		/* If at least one event is enabled, evlist is enabled. */
		if (!pos->disabled)
			return true;
	}
	return false;
}

static void __evlist__disable(struct evlist *evlist, char *evsel_name)
436
{
437
	struct evsel *pos;
438
	struct evlist_cpu_iterator evlist_cpu_itr;
439
	struct affinity saved_affinity, *affinity = NULL;
440
	bool has_imm = false;
441

442
	// See explanation in evlist__close()
443
	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
444 445 446 447
		if (affinity__setup(&saved_affinity) < 0)
			return;
		affinity = &saved_affinity;
	}
448

449
	/* Disable 'immediate' events last */
450
	for (int imm = 0; imm <= 1; imm++) {
451
		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
452 453 454 455 456 457 458 459 460 461
			pos = evlist_cpu_itr.evsel;
			if (evsel__strcmp(pos, evsel_name))
				continue;
			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
				continue;
			if (pos->immediate)
				has_imm = true;
			if (pos->immediate != imm)
				continue;
			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
462
		}
463 464
		if (!has_imm)
			break;
465
	}
466

467
	affinity__cleanup(affinity);
468
	evlist__for_each_entry(evlist, pos) {
469 470
		if (evsel__strcmp(pos, evsel_name))
			continue;
471
		if (!evsel__is_group_leader(pos) || !pos->core.fd)
472
			continue;
473
		pos->disabled = true;
474
	}
475

476 477 478 479 480 481 482 483
	/*
	 * If we disabled only single event, we need to check
	 * the enabled state of the evlist manually.
	 */
	if (evsel_name)
		evlist->enabled = evlist__is_enabled(evlist);
	else
		evlist->enabled = false;
484 485
}

486 487 488 489 490 491 492 493 494 495 496
void evlist__disable(struct evlist *evlist)
{
	__evlist__disable(evlist, NULL);
}

void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
{
	__evlist__disable(evlist, evsel_name);
}

static void __evlist__enable(struct evlist *evlist, char *evsel_name)
497
{
498
	struct evsel *pos;
499
	struct evlist_cpu_iterator evlist_cpu_itr;
500
	struct affinity saved_affinity, *affinity = NULL;
501

502
	// See explanation in evlist__close()
503
	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
504 505 506 507
		if (affinity__setup(&saved_affinity) < 0)
			return;
		affinity = &saved_affinity;
	}
508

509
	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
510 511 512 513 514 515
		pos = evlist_cpu_itr.evsel;
		if (evsel__strcmp(pos, evsel_name))
			continue;
		if (!evsel__is_group_leader(pos) || !pos->core.fd)
			continue;
		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
516
	}
517
	affinity__cleanup(affinity);
518
	evlist__for_each_entry(evlist, pos) {
519 520
		if (evsel__strcmp(pos, evsel_name))
			continue;
521
		if (!evsel__is_group_leader(pos) || !pos->core.fd)
522
			continue;
523
		pos->disabled = false;
524
	}
525

526 527 528 529 530
	/*
	 * Even single event sets the 'enabled' for evlist,
	 * so the toggle can work properly and toggle to
	 * 'disabled' state.
	 */
531 532 533
	evlist->enabled = true;
}

534 535 536 537 538 539 540 541 542 543
void evlist__enable(struct evlist *evlist)
{
	__evlist__enable(evlist, NULL);
}

void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
{
	__evlist__enable(evlist, evsel_name);
}

544
void evlist__toggle_enable(struct evlist *evlist)
545
{
546
	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
547 548
}

549
static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
550
{
551
	int thread;
552
	int nr_threads = evlist__nr_threads(evlist, evsel);
553

554
	if (!evsel->core.fd)
555 556 557
		return -EINVAL;

	for (thread = 0; thread < nr_threads; thread++) {
558
		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
559 560 561 562 563 564
		if (err)
			return err;
	}
	return 0;
}

565
static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
566
{
567
	int cpu;
568
	int nr_cpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
569

570
	if (!evsel->core.fd)
571 572 573
		return -EINVAL;

	for (cpu = 0; cpu < nr_cpus; cpu++) {
574
		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
575 576 577 578 579 580
		if (err)
			return err;
	}
	return 0;
}

581
int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
582
{
583
	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
584 585

	if (per_cpu_mmaps)
586 587 588
		return evlist__enable_event_cpu(evlist, evsel, idx);

	return evlist__enable_event_thread(evlist, evsel, idx);
589 590
}

591
int evlist__add_pollfd(struct evlist *evlist, int fd)
592
{
593
	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
594 595
}

596
int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
597
{
598
	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
599 600
}

601 602 603 604 605 606 607 608
#ifdef HAVE_EVENTFD_SUPPORT
int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
{
	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
				       fdarray_flag__nonfilterable);
}
#endif

609
int evlist__poll(struct evlist *evlist, int timeout)
610
{
611
	return perf_evlist__poll(&evlist->core, timeout);
612 613
}

614
struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
615 616 617 618 619 620
{
	struct hlist_head *head;
	struct perf_sample_id *sid;
	int hash;

	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
621
	head = &evlist->core.heads[hash];
622

623
	hlist_for_each_entry(sid, head, node)
624
		if (sid->id == id)
625 626 627 628 629
			return sid;

	return NULL;
}

630
struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
631 632 633
{
	struct perf_sample_id *sid;

634
	if (evlist->core.nr_entries == 1 || !id)
635
		return evlist__first(evlist);
636

637
	sid = evlist__id2sid(evlist, id);
638
	if (sid)
639
		return container_of(sid->evsel, struct evsel, core);
640

641
	if (!evlist__sample_id_all(evlist))
642
		return evlist__first(evlist);
643

644 645
	return NULL;
}
646

647
struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
648 649 650 651 652 653
{
	struct perf_sample_id *sid;

	if (!id)
		return NULL;

654
	sid = evlist__id2sid(evlist, id);
655
	if (sid)
656
		return container_of(sid->evsel, struct evsel, core);
657 658 659 660

	return NULL;
}

661
static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
662
{
663
	const __u64 *array = event->sample.array;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
	ssize_t n;

	n = (event->header.size - sizeof(event->header)) >> 3;

	if (event->header.type == PERF_RECORD_SAMPLE) {
		if (evlist->id_pos >= n)
			return -1;
		*id = array[evlist->id_pos];
	} else {
		if (evlist->is_pos > n)
			return -1;
		n -= evlist->is_pos;
		*id = array[n];
	}
	return 0;
}

681
struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
682
{
683
	struct evsel *first = evlist__first(evlist);
684 685 686 687 688
	struct hlist_head *head;
	struct perf_sample_id *sid;
	int hash;
	u64 id;

689
	if (evlist->core.nr_entries == 1)
690 691
		return first;

692
	if (!first->core.attr.sample_id_all &&
693 694
	    event->header.type != PERF_RECORD_SAMPLE)
		return first;
695

696
	if (evlist__event2id(evlist, event, &id))
697 698 699 700
		return NULL;

	/* Synthesized events have an id of zero */
	if (!id)
701
		return first;
702 703

	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
704
	head = &evlist->core.heads[hash];
705 706 707

	hlist_for_each_entry(sid, head, node) {
		if (sid->id == id)
708
			return container_of(sid->evsel, struct evsel, core);
709 710 711 712
	}
	return NULL;
}

713
static int evlist__set_paused(struct evlist *evlist, bool value)
714 715 716
{
	int i;

717
	if (!evlist->overwrite_mmap)
718 719
		return 0;

720
	for (i = 0; i < evlist->core.nr_mmaps; i++) {
721
		int fd = evlist->overwrite_mmap[i].core.fd;
722 723 724 725 726 727 728 729 730 731 732
		int err;

		if (fd < 0)
			continue;
		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
		if (err)
			return err;
	}
	return 0;
}

733
static int evlist__pause(struct evlist *evlist)
734
{
735
	return evlist__set_paused(evlist, true);
736 737
}

738
static int evlist__resume(struct evlist *evlist)
739
{
740
	return evlist__set_paused(evlist, false);
741 742
}

743
static void evlist__munmap_nofree(struct evlist *evlist)
744
{
745
	int i;
746

747
	if (evlist->mmap)
748
		for (i = 0; i < evlist->core.nr_mmaps; i++)
749
			perf_mmap__munmap(&evlist->mmap[i].core);
750

751
	if (evlist->overwrite_mmap)
752
		for (i = 0; i < evlist->core.nr_mmaps; i++)
753
			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
754
}
755

756
void evlist__munmap(struct evlist *evlist)
757
{
758
	evlist__munmap_nofree(evlist);
759
	zfree(&evlist->mmap);
760
	zfree(&evlist->overwrite_mmap);
761 762
}

763 764 765 766 767 768 769
static void perf_mmap__unmap_cb(struct perf_mmap *map)
{
	struct mmap *m = container_of(map, struct mmap, core);

	mmap__munmap(m);
}

770 771
static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
				       bool overwrite)
772
{
773
	int i;
774
	struct mmap *map;
775

776
	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
777 778
	if (!map)
		return NULL;
779

780
	for (i = 0; i < evlist->core.nr_mmaps; i++) {
781 782
		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;

783 784
		/*
		 * When the perf_mmap() call is made we grab one refcount, plus
785
		 * one extra to let perf_mmap__consume() get the last
786 787 788 789 790 791
		 * events after all real references (perf_mmap__get()) are
		 * dropped.
		 *
		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
		 * thus does perf_mmap__get() on it.
		 */
792
		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
793
	}
794

795
	return map;
796 797
}

798 799 800 801 802 803 804 805 806 807 808
static void
perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
			 struct perf_mmap_param *_mp,
			 int idx, bool per_cpu)
{
	struct evlist *evlist = container_of(_evlist, struct evlist, core);
	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);

	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
}

809 810 811 812
static struct perf_mmap*
perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
{
	struct evlist *evlist = container_of(_evlist, struct evlist, core);
813
	struct mmap *maps;
814

815
	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
816

817 818 819 820
	if (!maps) {
		maps = evlist__alloc_mmap(evlist, overwrite);
		if (!maps)
			return NULL;
821

822
		if (overwrite) {
823 824
			evlist->overwrite_mmap = maps;
			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
825
				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
826 827
		} else {
			evlist->mmap = maps;
828 829 830 831 832 833
		}
	}

	return &maps[idx].core;
}

834 835
static int
perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
836
			  int output, struct perf_cpu cpu)
837 838 839 840 841 842 843
{
	struct mmap *map = container_of(_map, struct mmap, core);
	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);

	return mmap__mmap(map, mp, output, cpu);
}

844
unsigned long perf_event_mlock_kb_in_pages(void)
845
{
846 847
	unsigned long pages;
	int max;
848

849 850 851 852 853 854 855 856 857 858
	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
		/*
		 * Pick a once upon a time good value, i.e. things look
		 * strange since we can't read a sysctl value, but lets not
		 * die yet...
		 */
		max = 512;
	} else {
		max -= (page_size / 1024);
	}
859

860 861 862 863 864 865 866
	pages = (max * 1024) / page_size;
	if (!is_power_of_2(pages))
		pages = rounddown_pow_of_two(pages);

	return pages;
}

867
size_t evlist__mmap_size(unsigned long pages)
868 869 870 871
{
	if (pages == UINT_MAX)
		pages = perf_event_mlock_kb_in_pages();
	else if (!is_power_of_2(pages))
872 873 874 875 876
		return 0;

	return (pages + 1) * page_size;
}

877 878
static long parse_pages_arg(const char *str, unsigned long min,
			    unsigned long max)
879
{
880
	unsigned long pages, val;
881 882 883 884 885 886 887
	static struct parse_tag tags[] = {
		{ .tag  = 'B', .mult = 1       },
		{ .tag  = 'K', .mult = 1 << 10 },
		{ .tag  = 'M', .mult = 1 << 20 },
		{ .tag  = 'G', .mult = 1 << 30 },
		{ .tag  = 0 },
	};
888

889
	if (str == NULL)
890
		return -EINVAL;
891

892
	val = parse_tag_value(str, tags);
893
	if (val != (unsigned long) -1) {
894 895 896 897 898 899
		/* we got file size value */
		pages = PERF_ALIGN(val, page_size) / page_size;
	} else {
		/* we got pages count value */
		char *eptr;
		pages = strtoul(str, &eptr, 10);
900 901
		if (*eptr != '\0')
			return -EINVAL;
902 903
	}

904
	if (pages == 0 && min == 0) {
905
		/* leave number of pages at 0 */
906
	} else if (!is_power_of_2(pages)) {
907 908
		char buf[100];

909
		/* round pages up to next power of 2 */
910
		pages = roundup_pow_of_two(pages);
911 912
		if (!pages)
			return -EINVAL;
913 914 915 916

		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
		pr_info("rounding mmap pages size to %s (%lu pages)\n",
			buf, pages);
917 918
	}

919 920 921 922 923 924
	if (pages > max)
		return -EINVAL;

	return pages;
}

925
int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
926 927 928 929
{
	unsigned long max = UINT_MAX;
	long pages;

930
	if (max > SIZE_MAX / page_size)
931 932 933 934 935
		max = SIZE_MAX / page_size;

	pages = parse_pages_arg(str, 1, max);
	if (pages < 0) {
		pr_err("Invalid argument for --mmap_pages/-m\n");
936 937 938 939 940 941 942
		return -1;
	}

	*mmap_pages = pages;
	return 0;
}

943
int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
944
{
945
	return __evlist__parse_mmap_pages(opt->value, str);
946 947
}

948
/**
949
 * evlist__mmap_ex - Create mmaps to receive events.
950 951 952
 * @evlist: list of events
 * @pages: map length in pages
 * @overwrite: overwrite older events?
953 954
 * @auxtrace_pages - auxtrace map length in pages
 * @auxtrace_overwrite - overwrite older auxtrace data?
955
 *
956
 * If @overwrite is %false the user needs to signal event consumption using
957
 * perf_mmap__write_tail().  Using evlist__mmap_read() does this
958
 * automatically.
959
 *
960 961 962
 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
 * consumption using auxtrace_mmap__write_tail().
 *
963
 * Return: %0 on success, negative error code otherwise.
964
 */
965
int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
966
			 unsigned int auxtrace_pages,
967 968
			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
			 int comp_level)
969
{
970 971 972 973 974
	/*
	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
	 * Its value is decided by evsel's write_backward.
	 * So &mp should not be passed through const pointer.
	 */
975 976 977 978 979 980
	struct mmap_params mp = {
		.nr_cblocks	= nr_cblocks,
		.affinity	= affinity,
		.flush		= flush,
		.comp_level	= comp_level
	};
981
	struct perf_evlist_mmap_ops ops = {
982 983 984
		.idx  = perf_evlist__mmap_cb_idx,
		.get  = perf_evlist__mmap_cb_get,
		.mmap = perf_evlist__mmap_cb_mmap,
985
	};
986

987 988
	evlist->core.mmap_len = evlist__mmap_size(pages);
	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
989

990
	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
991 992
				   auxtrace_pages, auxtrace_overwrite);

993
	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
994
}
995

996
int evlist__mmap(struct evlist *evlist, unsigned int pages)
997
{
998
	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
999 1000
}

1001
int evlist__create_maps(struct evlist *evlist, struct target *target)
1002
{
1003
	bool all_threads = (target->per_thread && target->system_wide);
1004
	struct perf_cpu_map *cpus;
1005
	struct perf_thread_map *threads;
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	/*
	 * If specify '-a' and '--per-thread' to perf record, perf record
	 * will override '--per-thread'. target->per_thread = false and
	 * target->system_wide = true.
	 *
	 * If specify '--per-thread' only to perf record,
	 * target->per_thread = true and target->system_wide = false.
	 *
	 * So target->per_thread && target->system_wide is false.
	 * For perf record, thread_map__new_str doesn't call
	 * thread_map__new_all_cpus. That will keep perf record's
	 * current behavior.
	 *
	 * For perf stat, it allows the case that target->per_thread and
	 * target->system_wide are all true. It means to collect system-wide
	 * per-thread data. thread_map__new_str will call
	 * thread_map__new_all_cpus to enumerate all threads.
	 */
1025
	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1026
				      all_threads);
1027

1028
	if (!threads)
1029 1030
		return -1;

1031
	if (target__uses_dummy_map(target))
1032
		cpus = perf_cpu_map__dummy_new();
1033
	else
1034
		cpus = perf_cpu_map__new(target->cpu_list);
1035

1036
	if (!cpus)
1037 1038
		goto out_delete_threads;

1039
	evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1040

1041
	perf_evlist__set_maps(&evlist->core, cpus, threads);
1042

1043 1044 1045 1046
	/* as evlist now has references, put count here */
	perf_cpu_map__put(cpus);
	perf_thread_map__put(threads);

1047
	return 0;
1048 1049

out_delete_threads:
1050
	perf_thread_map__put(threads);
1051 1052 1053
	return -1;
}

1054
int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1055
{
1056
	struct evsel *evsel;
1057
	int err = 0;
1058

1059
	evlist__for_each_entry(evlist, evsel) {
1060
		if (evsel->filter == NULL)
1061
			continue;
1062

1063 1064 1065 1066
		/*
		 * filters only work for tracepoint event, which doesn't have cpu limit.
		 * So evlist and evsel should always be same.
		 */
1067
		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1068 1069
		if (err) {
			*err_evsel = evsel;
1070
			break;
1071
		}
1072 1073
	}

1074 1075 1076
	return err;
}

1077
int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1078
{
1079
	struct evsel *evsel;
1080 1081
	int err = 0;

1082 1083 1084
	if (filter == NULL)
		return -1;

1085
	evlist__for_each_entry(evlist, evsel) {
1086
		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1087 1088
			continue;

1089
		err = evsel__set_filter(evsel, filter);
1090 1091 1092 1093 1094
		if (err)
			break;
	}

	return err;
1095
}
1096

1097
int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
{
	struct evsel *evsel;
	int err = 0;

	if (filter == NULL)
		return -1;

	evlist__for_each_entry(evlist, evsel) {
		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
			continue;

1109
		err = evsel__append_tp_filter(evsel, filter);
1110 1111 1112 1113 1114 1115 1116
		if (err)
			break;
	}

	return err;
}

1117
char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1118 1119
{
	char *filter;
1120
	size_t i;
1121

1122 1123 1124
	for (i = 0; i < npids; ++i) {
		if (i == 0) {
			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1125
				return NULL;
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
		} else {
			char *tmp;

			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
				goto out_free;

			free(filter);
			filter = tmp;
		}
	}
1136

1137
	return filter;
1138
out_free:
1139 1140 1141 1142
	free(filter);
	return NULL;
}

1143
int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1144 1145
{
	char *filter = asprintf__tp_filter_pids(npids, pids);
1146
	int ret = evlist__set_tp_filter(evlist, filter);
1147

1148 1149 1150 1151
	free(filter);
	return ret;
}

1152
int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1153
{
1154
	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1155 1156
}

1157
int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1158 1159
{
	char *filter = asprintf__tp_filter_pids(npids, pids);
1160
	int ret = evlist__append_tp_filter(evlist, filter);
1161 1162 1163 1164 1165

	free(filter);
	return ret;
}

1166
int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1167
{
1168
	return evlist__append_tp_filter_pids(evlist, 1, &pid);
1169 1170
}

1171
bool evlist__valid_sample_type(struct evlist *evlist)
1172
{
1173
	struct evsel *pos;
1174

1175
	if (evlist->core.nr_entries == 1)
1176 1177 1178 1179 1180
		return true;

	if (evlist->id_pos < 0 || evlist->is_pos < 0)
		return false;

1181
	evlist__for_each_entry(evlist, pos) {
1182 1183
		if (pos->id_pos != evlist->id_pos ||
		    pos->is_pos != evlist->is_pos)
1184
			return false;
1185 1186
	}

1187
	return true;
1188 1189
}

1190
u64 __evlist__combined_sample_type(struct evlist *evlist)
1191
{
1192
	struct evsel *evsel;
1193 1194 1195 1196

	if (evlist->combined_sample_type)
		return evlist->combined_sample_type;

1197
	evlist__for_each_entry(evlist, evsel)
1198
		evlist->combined_sample_type |= evsel->core.attr.sample_type;
1199 1200 1201 1202

	return evlist->combined_sample_type;
}

1203
u64 evlist__combined_sample_type(struct evlist *evlist)
1204 1205
{
	evlist->combined_sample_type = 0;
1206
	return __evlist__combined_sample_type(evlist);
1207 1208
}

1209
u64 evlist__combined_branch_type(struct evlist *evlist)
1210
{
1211
	struct evsel *evsel;
1212 1213
	u64 branch_type = 0;

1214
	evlist__for_each_entry(evlist, evsel)
1215
		branch_type |= evsel->core.attr.branch_sample_type;
1216 1217 1218
	return branch_type;
}

1219
bool evlist__valid_read_format(struct evlist *evlist)
1220
{
1221
	struct evsel *first = evlist__first(evlist), *pos = first;
1222 1223
	u64 read_format = first->core.attr.read_format;
	u64 sample_type = first->core.attr.sample_type;
1224

1225
	evlist__for_each_entry(evlist, pos) {
1226 1227 1228 1229
		if (read_format != pos->core.attr.read_format) {
			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
				 read_format, (u64)pos->core.attr.read_format);
		}
1230 1231
	}

1232
	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1233 1234 1235 1236 1237 1238 1239 1240
	if ((sample_type & PERF_SAMPLE_READ) &&
	    !(read_format & PERF_FORMAT_ID)) {
		return false;
	}

	return true;
}

1241
u16 evlist__id_hdr_size(struct evlist *evlist)
1242
{
1243
	struct evsel *first = evlist__first(evlist);
1244 1245 1246 1247
	struct perf_sample *data;
	u64 sample_type;
	u16 size = 0;

1248
	if (!first->core.attr.sample_id_all)
1249 1250
		goto out;

1251
	sample_type = first->core.attr.sample_type;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

	if (sample_type & PERF_SAMPLE_TID)
		size += sizeof(data->tid) * 2;

       if (sample_type & PERF_SAMPLE_TIME)
		size += sizeof(data->time);

	if (sample_type & PERF_SAMPLE_ID)
		size += sizeof(data->id);

	if (sample_type & PERF_SAMPLE_STREAM_ID)
		size += sizeof(data->stream_id);

	if (sample_type & PERF_SAMPLE_CPU)
		size += sizeof(data->cpu) * 2;
1267 1268 1269

	if (sample_type & PERF_SAMPLE_IDENTIFIER)
		size += sizeof(data->id);
1270 1271 1272 1273
out:
	return size;
}

1274
bool evlist__valid_sample_id_all(struct evlist *evlist)
1275
{
1276
	struct evsel *first = evlist__first(evlist), *pos = first;
1277

1278
	evlist__for_each_entry_continue(evlist, pos) {
1279
		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1280
			return false;
1281 1282
	}

1283 1284 1285
	return true;
}

1286
bool evlist__sample_id_all(struct evlist *evlist)
1287
{
1288
	struct evsel *first = evlist__first(evlist);
1289
	return first->core.attr.sample_id_all;
1290
}
1291

1292
void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1293 1294 1295
{
	evlist->selected = evsel;
}
1296

1297
void evlist__close(struct evlist *evlist)
1298
{
1299
	struct evsel *evsel;
1300
	struct evlist_cpu_iterator evlist_cpu_itr;
1301
	struct affinity affinity;
1302

1303
	/*
1304
	 * With perf record core.user_requested_cpus is usually NULL.
1305 1306
	 * Use the old method to handle this for now.
	 */
1307 1308
	if (!evlist->core.user_requested_cpus ||
	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1309 1310 1311 1312 1313 1314 1315 1316
		evlist__for_each_entry_reverse(evlist, evsel)
			evsel__close(evsel);
		return;
	}

	if (affinity__setup(&affinity) < 0)
		return;

1317 1318 1319
	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
				      evlist_cpu_itr.cpu_map_idx);
1320
	}
1321

1322 1323 1324 1325 1326
	affinity__cleanup(&affinity);
	evlist__for_each_entry_reverse(evlist, evsel) {
		perf_evsel__free_fd(&evsel->core);
		perf_evsel__free_id(&evsel->core);
	}
1327
	perf_evlist__reset_id_hash(&evlist->core);
1328 1329
}

1330
static int evlist__create_syswide_maps(struct evlist *evlist)
1331
{
1332
	struct perf_cpu_map *cpus;
1333
	struct perf_thread_map *threads;
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
	int err = -ENOMEM;

	/*
	 * Try reading /sys/devices/system/cpu/online to get
	 * an all cpus map.
	 *
	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
	 * code needs an overhaul to properly forward the
	 * error, and we may not want to do that fallback to a
	 * default cpu identity map :-\
	 */
1345
	cpus = perf_cpu_map__new(NULL);
1346
	if (!cpus)
1347 1348
		goto out;

1349
	threads = perf_thread_map__new_dummy();
1350 1351
	if (!threads)
		goto out_put;
1352

1353
	perf_evlist__set_maps(&evlist->core, cpus, threads);
1354 1355

	perf_thread_map__put(threads);
1356
out_put:
1357
	perf_cpu_map__put(cpus);
1358 1359
out:
	return err;
1360 1361
}

1362
int evlist__open(struct evlist *evlist)
1363
{
1364
	struct evsel *evsel;
1365
	int err;
1366

1367 1368 1369 1370
	/*
	 * Default: one fd per CPU, all threads, aka systemwide
	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
	 */
1371
	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1372
		err = evlist__create_syswide_maps(evlist);
1373 1374 1375 1376
		if (err < 0)
			goto out_err;
	}

1377
	evlist__update_id_pos(evlist);
1378

1379
	evlist__for_each_entry(evlist, evsel) {
1380
		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1381 1382 1383 1384 1385 1386
		if (err < 0)
			goto out_err;
	}

	return 0;
out_err:
1387
	evlist__close(evlist);
1388
	errno = -err;
1389 1390
	return err;
}
1391

1392 1393
int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
{
	int child_ready_pipe[2], go_pipe[2];
	char bf;

	if (pipe(child_ready_pipe) < 0) {
		perror("failed to create 'ready' pipe");
		return -1;
	}

	if (pipe(go_pipe) < 0) {
		perror("failed to create 'go' pipe");
		goto out_close_ready_pipe;
	}

	evlist->workload.pid = fork();
	if (evlist->workload.pid < 0) {
		perror("failed to fork");
		goto out_close_pipes;
	}

	if (!evlist->workload.pid) {
1415 1416
		int ret;

1417
		if (pipe_output)
1418 1419
			dup2(2, 1);

1420 1421
		signal(SIGTERM, SIG_DFL);

1422 1423 1424 1425
		close(child_ready_pipe[0]);
		close(go_pipe[1]);
		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);

1426 1427 1428 1429 1430 1431 1432
		/*
		 * Change the name of this process not to confuse --exclude-perf users
		 * that sees 'perf' in the window up to the execvp() and thinks that
		 * perf samples are not being excluded.
		 */
		prctl(PR_SET_NAME, "perf-exec");

1433 1434 1435 1436 1437 1438 1439 1440
		/*
		 * Tell the parent we're ready to go
		 */
		close(child_ready_pipe[1]);

		/*
		 * Wait until the parent tells us to go.
		 */
1441 1442 1443 1444
		ret = read(go_pipe[0], &bf, 1);
		/*
		 * The parent will ask for the execvp() to be performed by
		 * writing exactly one byte, in workload.cork_fd, usually via
1445
		 * evlist__start_workload().
1446
		 *
1447
		 * For cancelling the workload without actually running it,
1448 1449 1450 1451 1452 1453 1454 1455 1456
		 * the parent will just close workload.cork_fd, without writing
		 * anything, i.e. read will return zero and we just exit()
		 * here.
		 */
		if (ret != 1) {
			if (ret == -1)
				perror("unable to read pipe");
			exit(ret);
		}
1457 1458 1459

		execvp(argv[0], (char **)argv);

1460
		if (exec_error) {
1461 1462 1463 1464 1465 1466 1467
			union sigval val;

			val.sival_int = errno;
			if (sigqueue(getppid(), SIGUSR1, val))
				perror(argv[0]);
		} else
			perror(argv[0]);
1468 1469 1470
		exit(-1);
	}

1471 1472 1473 1474 1475 1476 1477 1478
	if (exec_error) {
		struct sigaction act = {
			.sa_flags     = SA_SIGINFO,
			.sa_sigaction = exec_error,
		};
		sigaction(SIGUSR1, &act, NULL);
	}

1479
	if (target__none(target)) {
1480
		if (evlist->core.threads == NULL) {
1481 1482 1483 1484
			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
				__func__, __LINE__);
			goto out_close_pipes;
		}
1485
		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1486
	}
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

	close(child_ready_pipe[1]);
	close(go_pipe[0]);
	/*
	 * wait for child to settle
	 */
	if (read(child_ready_pipe[0], &bf, 1) == -1) {
		perror("unable to read pipe");
		goto out_close_pipes;
	}

1498
	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	evlist->workload.cork_fd = go_pipe[1];
	close(child_ready_pipe[0]);
	return 0;

out_close_pipes:
	close(go_pipe[0]);
	close(go_pipe[1]);
out_close_ready_pipe:
	close(child_ready_pipe[0]);
	close(child_ready_pipe[1]);
	return -1;
}

1512
int evlist__start_workload(struct evlist *evlist)
1513 1514
{
	if (evlist->workload.cork_fd > 0) {
1515
		char bf = 0;
1516
		int ret;
1517 1518 1519
		/*
		 * Remove the cork, let it rip!
		 */
1520 1521
		ret = write(evlist->workload.cork_fd, &bf, 1);
		if (ret < 0)
1522
			perror("unable to write to pipe");
1523 1524 1525

		close(evlist->workload.cork_fd);
		return ret;
1526 1527 1528 1529
	}

	return 0;
}
1530

1531
int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1532
{
1533
	struct evsel *evsel = evlist__event2evsel(evlist, event);
1534 1535 1536

	if (!evsel)
		return -EFAULT;
1537
	return evsel__parse_sample(evsel, event, sample);
1538
}
1539

1540
int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1541
{
1542
	struct evsel *evsel = evlist__event2evsel(evlist, event);
1543 1544 1545

	if (!evsel)
		return -EFAULT;
1546
	return evsel__parse_sample_timestamp(evsel, event, timestamp);
1547 1548
}

1549
int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1550 1551
{
	int printed, value;
1552
	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1553 1554 1555 1556 1557 1558 1559 1560

	switch (err) {
	case EACCES:
	case EPERM:
		printed = scnprintf(buf, size,
				    "Error:\t%s.\n"
				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);

1561
		value = perf_event_paranoid();
1562 1563 1564 1565 1566 1567 1568 1569

		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");

		if (value >= 2) {
			printed += scnprintf(buf + printed, size - printed,
					     "For your workloads it needs to be <= 1\nHint:\t");
		}
		printed += scnprintf(buf + printed, size - printed,
1570
				     "For system wide tracing it needs to be set to -1.\n");
1571 1572

		printed += scnprintf(buf + printed, size - printed,
1573 1574
				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
				    "Hint:\tThe current value is %d.", value);
1575
		break;
1576
	case EINVAL: {
1577
		struct evsel *first = evlist__first(evlist);
1578 1579 1580 1581 1582
		int max_freq;

		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
			goto out_default;

1583
		if (first->core.attr.sample_freq < (u64)max_freq)
1584 1585 1586 1587 1588 1589
			goto out_default;

		printed = scnprintf(buf, size,
				    "Error:\t%s.\n"
				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1590
				    emsg, max_freq, first->core.attr.sample_freq);
1591 1592
		break;
	}
1593
	default:
1594
out_default:
1595 1596 1597 1598 1599 1600
		scnprintf(buf, size, "%s", emsg);
		break;
	}

	return 0;
}
1601

1602
int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1603
{
1604
	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1605
	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1606 1607 1608

	switch (err) {
	case EPERM:
1609
		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1610 1611
		printed += scnprintf(buf + printed, size - printed,
				     "Error:\t%s.\n"
1612
				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1613
				     "Hint:\tTried using %zd kB.\n",
1614
				     emsg, pages_max_per_user, pages_attempted);
1615 1616 1617 1618 1619 1620 1621 1622 1623

		if (pages_attempted >= pages_max_per_user) {
			printed += scnprintf(buf + printed, size - printed,
					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
					     pages_max_per_user + pages_attempted);
		}

		printed += scnprintf(buf + printed, size - printed,
				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1624 1625 1626 1627 1628 1629 1630 1631 1632
		break;
	default:
		scnprintf(buf, size, "%s", emsg);
		break;
	}

	return 0;
}

1633
void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1634
{
1635
	struct evsel *evsel, *n;
1636 1637
	LIST_HEAD(move);

1638
	if (move_evsel == evlist__first(evlist))
1639 1640
		return;

1641
	evlist__for_each_entry_safe(evlist, n, evsel) {
1642
		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1643
			list_move_tail(&evsel->core.node, &move);
1644 1645
	}

1646
	list_splice(&move, &evlist->core.entries);
1647
}
1648

1649
struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
{
	struct evsel *evsel;

	evlist__for_each_entry(evlist, evsel) {
		if (evsel->tracking)
			return evsel;
	}

	return evlist__first(evlist);
}

1661
void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1662
{
1663
	struct evsel *evsel;
1664 1665 1666 1667

	if (tracking_evsel->tracking)
		return;

1668
	evlist__for_each_entry(evlist, evsel) {
1669 1670 1671 1672 1673 1674
		if (evsel != tracking_evsel)
			evsel->tracking = false;
	}

	tracking_evsel->tracking = true;
}
1675

1676
struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1677
{
1678
	struct evsel *evsel;
1679

1680
	evlist__for_each_entry(evlist, evsel) {
1681 1682 1683 1684 1685 1686 1687 1688
		if (!evsel->name)
			continue;
		if (strcmp(str, evsel->name) == 0)
			return evsel;
	}

	return NULL;
}
1689

1690
void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1691 1692 1693 1694 1695 1696 1697 1698
{
	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
	enum action {
		NONE,
		PAUSE,
		RESUME,
	} action = NONE;

1699
	if (!evlist->overwrite_mmap)
1700 1701 1702 1703 1704
		return;

	switch (old_state) {
	case BKW_MMAP_NOTREADY: {
		if (state != BKW_MMAP_RUNNING)
1705
			goto state_err;
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
		break;
	}
	case BKW_MMAP_RUNNING: {
		if (state != BKW_MMAP_DATA_PENDING)
			goto state_err;
		action = PAUSE;
		break;
	}
	case BKW_MMAP_DATA_PENDING: {
		if (state != BKW_MMAP_EMPTY)
			goto state_err;
		break;
	}
	case BKW_MMAP_EMPTY: {
		if (state != BKW_MMAP_RUNNING)
			goto state_err;
		action = RESUME;
		break;
	}
	default:
		WARN_ONCE(1, "Shouldn't get there\n");
	}

	evlist->bkw_mmap_state = state;

	switch (action) {
	case PAUSE:
1733
		evlist__pause(evlist);
1734 1735
		break;
	case RESUME:
1736
		evlist__resume(evlist);
1737 1738 1739 1740 1741 1742 1743 1744 1745
		break;
	case NONE:
	default:
		break;
	}

state_err:
	return;
}
1746

1747
bool evlist__exclude_kernel(struct evlist *evlist)
1748
{
1749
	struct evsel *evsel;
1750 1751

	evlist__for_each_entry(evlist, evsel) {
1752
		if (!evsel->core.attr.exclude_kernel)
1753 1754 1755 1756 1757
			return false;
	}

	return true;
}
1758 1759 1760 1761 1762 1763

/*
 * Events in data file are not collect in groups, but we still want
 * the group display. Set the artificial group and set the leader's
 * forced_leader flag to notify the display code.
 */
1764
void evlist__force_leader(struct evlist *evlist)
1765
{
1766
	if (!evlist->core.nr_groups) {
1767
		struct evsel *leader = evlist__first(evlist);
1768

1769
		evlist__set_leader(evlist);
1770 1771 1772
		leader->forced_leader = true;
	}
}
1773

1774
struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1775
{
1776
	struct evsel *c2, *leader;
1777 1778
	bool is_open = true;

1779 1780
	leader = evsel__leader(evsel);

1781
	pr_debug("Weak group for %s/%d failed\n",
1782
			leader->name, leader->core.nr_members);
1783 1784 1785 1786 1787 1788 1789 1790

	/*
	 * for_each_group_member doesn't work here because it doesn't
	 * include the first entry.
	 */
	evlist__for_each_entry(evsel_list, c2) {
		if (c2 == evsel)
			is_open = false;
1791
		if (evsel__has_leader(c2, leader)) {
1792
			if (is_open && close)
1793
				perf_evsel__close(&c2->core);
1794
			evsel__set_leader(c2, c2);
1795
			c2->core.nr_members = 0;
1796 1797 1798 1799 1800
			/*
			 * Set this for all former members of the group
			 * to indicate they get reopened.
			 */
			c2->reset_group = true;
1801 1802 1803 1804
		}
	}
	return leader;
}
1805

1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
{
	char *s, *p;
	int ret = 0, fd;

	if (strncmp(str, "fifo:", 5))
		return -EINVAL;

	str += 5;
	if (!*str || *str == ',')
		return -EINVAL;

	s = strdup(str);
	if (!s)
		return -ENOMEM;

	p = strchr(s, ',');
	if (p)
		*p = '\0';

	/*
	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
	 * end of a FIFO to be repeatedly opened and closed.
	 */
	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
	if (fd < 0) {
		pr_err("Failed to open '%s'\n", s);
		ret = -errno;
		goto out_free;
	}
	*ctl_fd = fd;
	*ctl_fd_close = true;

	if (p && *++p) {
		/* O_RDWR | O_NONBLOCK means the other end need not be open */
		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
		if (fd < 0) {
			pr_err("Failed to open '%s'\n", p);
			ret = -errno;
			goto out_free;
		}
		*ctl_fd_ack = fd;
	}

out_free:
	free(s);
	return ret;
}

int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1856 1857 1858
{
	char *comma = NULL, *endptr = NULL;

1859 1860
	*ctl_fd_close = false;

1861
	if (strncmp(str, "fd:", 3))
1862
		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880

	*ctl_fd = strtoul(&str[3], &endptr, 0);
	if (endptr == &str[3])
		return -EINVAL;

	comma = strchr(str, ',');
	if (comma) {
		if (endptr != comma)
			return -EINVAL;

		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
		if (endptr == comma + 1 || *endptr != '\0')
			return -EINVAL;
	}

	return 0;
}

1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
{
	if (*ctl_fd_close) {
		*ctl_fd_close = false;
		close(ctl_fd);
		if (ctl_fd_ack >= 0)
			close(ctl_fd_ack);
	}
}

1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
{
	if (fd == -1) {
		pr_debug("Control descriptor is not initialized\n");
		return 0;
	}

	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
						     fdarray_flag__nonfilterable);
	if (evlist->ctl_fd.pos < 0) {
		evlist->ctl_fd.pos = -1;
		pr_err("Failed to add ctl fd entry: %m\n");
		return -1;
	}

	evlist->ctl_fd.fd = fd;
	evlist->ctl_fd.ack = ack;

	return 0;
}

bool evlist__ctlfd_initialized(struct evlist *evlist)
{
	return evlist->ctl_fd.pos >= 0;
}

int evlist__finalize_ctlfd(struct evlist *evlist)
{
	struct pollfd *entries = evlist->core.pollfd.entries;

	if (!evlist__ctlfd_initialized(evlist))
		return 0;

	entries[evlist->ctl_fd.pos].fd = -1;
	entries[evlist->ctl_fd.pos].events = 0;
	entries[evlist->ctl_fd.pos].revents = 0;

	evlist->ctl_fd.pos = -1;
	evlist->ctl_fd.ack = -1;
	evlist->ctl_fd.fd = -1;

	return 0;
}

static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
			      char *cmd_data, size_t data_size)
{
	int err;
	char c;
	size_t bytes_read = 0;

1942
	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
	memset(cmd_data, 0, data_size);
	data_size--;

	do {
		err = read(evlist->ctl_fd.fd, &c, 1);
		if (err > 0) {
			if (c == '\n' || c == '\0')
				break;
			cmd_data[bytes_read++] = c;
			if (bytes_read == data_size)
				break;
1954 1955 1956 1957 1958 1959 1960
			continue;
		} else if (err == -1) {
			if (errno == EINTR)
				continue;
			if (errno == EAGAIN || errno == EWOULDBLOCK)
				err = 0;
			else
1961 1962
				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
		}
1963
		break;
1964 1965 1966 1967 1968
	} while (1);

	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");

1969
	if (bytes_read > 0) {
1970 1971 1972 1973 1974 1975
		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
			*cmd = EVLIST_CTL_CMD_ENABLE;
		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
			*cmd = EVLIST_CTL_CMD_DISABLE;
1976 1977 1978 1979
		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
			pr_debug("is snapshot\n");
1980 1981 1982
		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
			*cmd = EVLIST_CTL_CMD_EVLIST;
1983 1984 1985
		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
			*cmd = EVLIST_CTL_CMD_STOP;
1986 1987 1988
		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
			*cmd = EVLIST_CTL_CMD_PING;
1989 1990 1991
		}
	}

1992
	return bytes_read ? (int)bytes_read : err;
1993 1994
}

1995
int evlist__ctlfd_ack(struct evlist *evlist)
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
{
	int err;

	if (evlist->ctl_fd.ack == -1)
		return 0;

	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
	if (err == -1)
		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);

	return err;
}

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
{
	char *data = cmd_data + cmd_size;

	/* no argument */
	if (!*data)
		return 0;

	/* there's argument */
	if (*data == ' ') {
		*arg = data + 1;
		return 1;
	}

	/* malformed */
	return -1;
}

static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
{
	struct evsel *evsel;
	char *name;
	int err;

	err = get_cmd_arg(cmd_data,
			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
			  &name);
	if (err < 0) {
		pr_info("failed: wrong command\n");
		return -1;
	}

	if (err) {
		evsel = evlist__find_evsel_by_str(evlist, name);
		if (evsel) {
			if (enable)
				evlist__enable_evsel(evlist, name);
			else
				evlist__disable_evsel(evlist, name);
			pr_info("Event %s %s\n", evsel->name,
				enable ? "enabled" : "disabled");
		} else {
			pr_info("failed: can't find '%s' event\n", name);
		}
	} else {
		if (enable) {
			evlist__enable(evlist);
			pr_info(EVLIST_ENABLED_MSG);
		} else {
			evlist__disable(evlist);
			pr_info(EVLIST_DISABLED_MSG);
		}
	}

	return 0;
}

2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
{
	struct perf_attr_details details = { .verbose = false, };
	struct evsel *evsel;
	char *arg;
	int err;

	err = get_cmd_arg(cmd_data,
			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
			  &arg);
	if (err < 0) {
		pr_info("failed: wrong command\n");
		return -1;
	}

	if (err) {
		if (!strcmp(arg, "-v")) {
			details.verbose = true;
		} else if (!strcmp(arg, "-g")) {
			details.event_group = true;
		} else if (!strcmp(arg, "-F")) {
			details.freq = true;
		} else {
			pr_info("failed: wrong command\n");
			return -1;
		}
	}

	evlist__for_each_entry(evlist, evsel)
		evsel__fprintf(evsel, &details, stderr);

	return 0;
}

2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
{
	int err = 0;
	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
	int ctlfd_pos = evlist->ctl_fd.pos;
	struct pollfd *entries = evlist->core.pollfd.entries;

	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
		return 0;

	if (entries[ctlfd_pos].revents & POLLIN) {
		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
					 EVLIST_CTL_CMD_MAX_LEN);
		if (err > 0) {
			switch (*cmd) {
			case EVLIST_CTL_CMD_ENABLE:
			case EVLIST_CTL_CMD_DISABLE:
2119 2120
				err = evlist__ctlfd_enable(evlist, cmd_data,
							   *cmd == EVLIST_CTL_CMD_ENABLE);
2121
				break;
2122 2123 2124
			case EVLIST_CTL_CMD_EVLIST:
				err = evlist__ctlfd_list(evlist, cmd_data);
				break;
2125
			case EVLIST_CTL_CMD_SNAPSHOT:
2126
			case EVLIST_CTL_CMD_STOP:
2127
			case EVLIST_CTL_CMD_PING:
2128
				break;
2129 2130 2131 2132 2133 2134
			case EVLIST_CTL_CMD_ACK:
			case EVLIST_CTL_CMD_UNSUPPORTED:
			default:
				pr_debug("ctlfd: unsupported %d\n", *cmd);
				break;
			}
2135 2136
			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
				evlist__ctlfd_ack(evlist);
		}
	}

	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
		evlist__finalize_ctlfd(evlist);
	else
		entries[ctlfd_pos].revents = 0;

	return err;
}
2148

2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
int evlist__ctlfd_update(struct evlist *evlist, struct pollfd *update)
{
	int ctlfd_pos = evlist->ctl_fd.pos;
	struct pollfd *entries = evlist->core.pollfd.entries;

	if (!evlist__ctlfd_initialized(evlist))
		return 0;

	if (entries[ctlfd_pos].fd != update->fd ||
	    entries[ctlfd_pos].events != update->events)
		return -1;

	entries[ctlfd_pos].revents = update->revents;
	return 0;
}

2165 2166 2167 2168 2169
struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
{
	struct evsel *evsel;

	evlist__for_each_entry(evlist, evsel) {
2170
		if (evsel->core.idx == idx)
2171 2172 2173 2174
			return evsel;
	}
	return NULL;
}
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193

int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
{
	struct evsel *evsel;
	int printed = 0;

	evlist__for_each_entry(evlist, evsel) {
		if (evsel__is_dummy_event(evsel))
			continue;
		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
		} else {
			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
			break;
		}
	}

	return printed;
}
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206

void evlist__check_mem_load_aux(struct evlist *evlist)
{
	struct evsel *leader, *evsel, *pos;

	/*
	 * For some platforms, the 'mem-loads' event is required to use
	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
	 * must be the group leader. Now we disable this group before reporting
	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
	 * any valid memory load information.
	 */
	evlist__for_each_entry(evlist, evsel) {
2207
		leader = evsel__leader(evsel);
2208 2209 2210 2211 2212
		if (leader == evsel)
			continue;

		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
			for_each_group_evsel(pos, leader) {
2213
				evsel__set_leader(pos, pos);
2214 2215 2216 2217 2218
				pos->core.nr_members = 0;
			}
		}
	}
}