evsel.c 6.3 KB
Newer Older
1
#include "evsel.h"
2
#include "evlist.h"
3
#include "../perf.h"
4
#include "util.h"
5 6
#include "cpumap.h"
#include "thread.h"
7

8 9 10
#include <unistd.h>
#include <sys/mman.h>

11 12
#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))

13
struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
14 15 16 17 18
{
	struct perf_evsel *evsel = zalloc(sizeof(*evsel));

	if (evsel != NULL) {
		evsel->idx	   = idx;
19
		evsel->attr	   = *attr;
20 21 22 23 24 25 26 27 28 29 30 31
		INIT_LIST_HEAD(&evsel->node);
	}

	return evsel;
}

int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
{
	evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
	return evsel->fd != NULL ? 0 : -ENOMEM;
}

32 33 34 35 36 37 38
int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
{
	evsel->counts = zalloc((sizeof(*evsel->counts) +
				(ncpus * sizeof(struct perf_counts_values))));
	return evsel->counts != NULL ? 0 : -ENOMEM;
}

39 40 41 42 43 44
void perf_evsel__free_fd(struct perf_evsel *evsel)
{
	xyarray__delete(evsel->fd);
	evsel->fd = NULL;
}

45 46 47 48 49 50 51 52 53 54 55
void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
{
	int cpu, thread;

	for (cpu = 0; cpu < ncpus; cpu++)
		for (thread = 0; thread < nthreads; ++thread) {
			close(FD(evsel, cpu, thread));
			FD(evsel, cpu, thread) = -1;
		}
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
void perf_evsel__munmap(struct perf_evsel *evsel, int ncpus, int nthreads)
{
	struct perf_mmap *mm;
	int cpu, thread;

	for (cpu = 0; cpu < ncpus; cpu++)
		for (thread = 0; thread < nthreads; ++thread) {
			mm = xyarray__entry(evsel->mmap, cpu, thread);
			if (mm->base != NULL) {
				munmap(mm->base, evsel->mmap_len);
				mm->base = NULL;
			}
		}
}

int perf_evsel__alloc_mmap(struct perf_evsel *evsel, int ncpus, int nthreads)
{
	evsel->mmap = xyarray__new(ncpus, nthreads, sizeof(struct perf_mmap));
	return evsel->mmap != NULL ? 0 : -ENOMEM;
}

77 78 79 80
void perf_evsel__delete(struct perf_evsel *evsel)
{
	assert(list_empty(&evsel->node));
	xyarray__delete(evsel->fd);
81
	xyarray__delete(evsel->mmap);
82 83
	free(evsel);
}
84 85 86 87 88 89 90 91 92 93

int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
			      int cpu, int thread, bool scale)
{
	struct perf_counts_values count;
	size_t nv = scale ? 3 : 1;

	if (FD(evsel, cpu, thread) < 0)
		return -EINVAL;

94 95 96
	if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
		return -ENOMEM;

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
		return -errno;

	if (scale) {
		if (count.run == 0)
			count.val = 0;
		else if (count.run < count.ena)
			count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
	} else
		count.ena = count.run = 0;

	evsel->counts->cpu[cpu] = count;
	return 0;
}

int __perf_evsel__read(struct perf_evsel *evsel,
		       int ncpus, int nthreads, bool scale)
{
	size_t nv = scale ? 3 : 1;
	int cpu, thread;
	struct perf_counts_values *aggr = &evsel->counts->aggr, count;

	aggr->val = 0;

	for (cpu = 0; cpu < ncpus; cpu++) {
		for (thread = 0; thread < nthreads; thread++) {
			if (FD(evsel, cpu, thread) < 0)
				continue;

			if (readn(FD(evsel, cpu, thread),
				  &count, nv * sizeof(u64)) < 0)
				return -errno;

			aggr->val += count.val;
			if (scale) {
				aggr->ena += count.ena;
				aggr->run += count.run;
			}
		}
	}

	evsel->counts->scaled = 0;
	if (scale) {
		if (aggr->run == 0) {
			evsel->counts->scaled = -1;
			aggr->val = 0;
			return 0;
		}

		if (aggr->run < aggr->ena) {
			evsel->counts->scaled = 1;
			aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
		}
	} else
		aggr->ena = aggr->run = 0;

	return 0;
}
155

156
static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
157
			      struct thread_map *threads, bool group, bool inherit)
158
{
159
	int cpu, thread;
160

161 162
	if (evsel->fd == NULL &&
	    perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
163 164
		return -1;

165
	for (cpu = 0; cpu < cpus->nr; cpu++) {
166 167
		int group_fd = -1;

168 169
		evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit;

170 171 172
		for (thread = 0; thread < threads->nr; thread++) {
			FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
								     threads->map[thread],
173 174
								     cpus->map[cpu],
								     group_fd, 0);
175 176
			if (FD(evsel, cpu, thread) < 0)
				goto out_close;
177 178 179

			if (group && group_fd == -1)
				group_fd = FD(evsel, cpu, thread);
180
		}
181 182 183 184 185
	}

	return 0;

out_close:
186 187 188 189 190 191 192
	do {
		while (--thread >= 0) {
			close(FD(evsel, cpu, thread));
			FD(evsel, cpu, thread) = -1;
		}
		thread = threads->nr;
	} while (--cpu >= 0);
193 194 195
	return -1;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static struct {
	struct cpu_map map;
	int cpus[1];
} empty_cpu_map = {
	.map.nr	= 1,
	.cpus	= { -1, },
};

static struct {
	struct thread_map map;
	int threads[1];
} empty_thread_map = {
	.map.nr	 = 1,
	.threads = { -1, },
};

212
int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
213
		     struct thread_map *threads, bool group, bool inherit)
214
{
215 216 217
	if (cpus == NULL) {
		/* Work around old compiler warnings about strict aliasing */
		cpus = &empty_cpu_map.map;
218 219
	}

220 221
	if (threads == NULL)
		threads = &empty_thread_map.map;
222

223
	return __perf_evsel__open(evsel, cpus, threads, group, inherit);
224 225
}

226
int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
227
			     struct cpu_map *cpus, bool group, bool inherit)
228
{
229
	return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
230
}
231

232
int perf_evsel__open_per_thread(struct perf_evsel *evsel,
233
				struct thread_map *threads, bool group, bool inherit)
234
{
235
	return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
236
}
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

int perf_evsel__mmap(struct perf_evsel *evsel, struct cpu_map *cpus,
		     struct thread_map *threads, int pages,
		     struct perf_evlist *evlist)
{
	unsigned int page_size = sysconf(_SC_PAGE_SIZE);
	int mask = pages * page_size - 1, cpu;
	struct perf_mmap *mm;
	int thread;

	if (evsel->mmap == NULL &&
	    perf_evsel__alloc_mmap(evsel, cpus->nr, threads->nr) < 0)
		return -ENOMEM;

	evsel->mmap_len = (pages + 1) * page_size;

	for (cpu = 0; cpu < cpus->nr; cpu++) {
		for (thread = 0; thread < threads->nr; thread++) {
			mm = xyarray__entry(evsel->mmap, cpu, thread);
			mm->prev = 0;
			mm->mask = mask;
			mm->base = mmap(NULL, evsel->mmap_len, PROT_READ,
					MAP_SHARED, FD(evsel, cpu, thread), 0);
			if (mm->base == MAP_FAILED)
				goto out_unmap;

			if (evlist != NULL)
				 perf_evlist__add_pollfd(evlist, FD(evsel, cpu, thread));
		}
	}

	return 0;

out_unmap:
	do {
		while (--thread >= 0) {
			mm = xyarray__entry(evsel->mmap, cpu, thread);
			munmap(mm->base, evsel->mmap_len);
			mm->base = NULL;
		}
		thread = threads->nr;
	} while (--cpu >= 0);

	return -1;
}