Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <perf/evsel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <perf/threadmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <internal/evsel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <internal/xyarray.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <internal/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <internal/threadmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <internal/lib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	INIT_LIST_HEAD(&evsel->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	evsel->attr = *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct perf_evsel *evsel = zalloc(sizeof(*evsel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (evsel != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		perf_evsel__init(evsel, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) void perf_evsel__delete(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	free(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define FD(e, x, y) (*(int *) xyarray__entry(e->fd, x, y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (evsel->fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		int cpu, thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		for (cpu = 0; cpu < ncpus; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			for (thread = 0; thread < nthreads; thread++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				FD(evsel, cpu, thread) = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return evsel->fd != NULL ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) sys_perf_event_open(struct perf_event_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		    pid_t pid, int cpu, int group_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		    unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		     struct perf_thread_map *threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int cpu, thread, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (cpus == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		static struct perf_cpu_map *empty_cpu_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		if (empty_cpu_map == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			empty_cpu_map = perf_cpu_map__dummy_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			if (empty_cpu_map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		cpus = empty_cpu_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (threads == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		static struct perf_thread_map *empty_thread_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		if (empty_thread_map == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			empty_thread_map = perf_thread_map__new_dummy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			if (empty_thread_map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		threads = empty_thread_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (evsel->fd == NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	    perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	for (cpu = 0; cpu < cpus->nr; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		for (thread = 0; thread < threads->nr; thread++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			fd = sys_perf_event_open(&evsel->attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 						 threads->map[thread].pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 						 cpus->map[cpu], -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			FD(evsel, cpu, thread) = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static void perf_evsel__close_fd_cpu(struct perf_evsel *evsel, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (FD(evsel, cpu, thread) >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			close(FD(evsel, cpu, thread));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		FD(evsel, cpu, thread) = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void perf_evsel__close_fd(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		perf_evsel__close_fd_cpu(evsel, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void perf_evsel__free_fd(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	xyarray__delete(evsel->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	evsel->fd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void perf_evsel__close(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (evsel->fd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	perf_evsel__close_fd(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	perf_evsel__free_fd(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (evsel->fd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	perf_evsel__close_fd_cpu(evsel, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int perf_evsel__read_size(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	u64 read_format = evsel->attr.read_format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int entry = sizeof(u64); /* value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		size += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		size += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (read_format & PERF_FORMAT_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		entry += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (read_format & PERF_FORMAT_GROUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		nr = evsel->nr_members;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		size += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	size += entry * nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		     struct perf_counts_values *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	size_t size = perf_evsel__read_size(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	memset(count, 0, sizeof(*count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (FD(evsel, cpu, thread) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				 int ioc,  void *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				 int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		int fd = FD(evsel, cpu, thread),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		    err = ioctl(fd, ioc, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int perf_evsel__enable(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int perf_evsel__disable(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	for (i = 0; i < evsel->cpus->nr && !err; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		err = perf_evsel__run_ioctl(evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				     PERF_EVENT_IOC_SET_FILTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				     (void *)filter, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return evsel->cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return evsel->threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return &evsel->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (ncpus == 0 || nthreads == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (evsel->system_wide)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		nthreads = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (evsel->sample_id == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (evsel->id == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		xyarray__delete(evsel->sample_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		evsel->sample_id = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) void perf_evsel__free_id(struct perf_evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	xyarray__delete(evsel->sample_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	evsel->sample_id = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	zfree(&evsel->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	evsel->ids = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }