record.c 8.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include "debug.h"
3 4
#include "evlist.h"
#include "evsel.h"
5
#include "parse-events.h"
6
#include <errno.h>
7 8
#include <limits.h>
#include <stdlib.h>
9
#include <api/fs/fs.h>
10
#include <subcmd/parse-options.h>
11
#include <perf/cpumap.h>
12
#include "cloexec.h"
13
#include "record.h"
14
#include "../perf-sys.h"
15

16
typedef void (*setup_probe_fn_t)(struct evsel *evsel);
17 18 19

static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
{
20
	struct evlist *evlist;
21
	struct evsel *evsel;
22
	unsigned long flags = perf_event_open_cloexec_flag();
23
	int err = -EAGAIN, fd;
24
	static pid_t pid = -1;
25

26
	evlist = evlist__new();
27 28 29
	if (!evlist)
		return -ENOMEM;

30
	if (parse_events(evlist, str, NULL))
31 32
		goto out_delete;

33
	evsel = evlist__first(evlist);
34

35
	while (1) {
36
		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
37 38 39 40 41 42 43 44 45
		if (fd < 0) {
			if (pid == -1 && errno == EACCES) {
				pid = 0;
				continue;
			}
			goto out_delete;
		}
		break;
	}
46 47 48 49
	close(fd);

	fn(evsel);

50
	fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
51 52 53 54 55 56 57 58 59
	if (fd < 0) {
		if (errno == EINVAL)
			err = -EINVAL;
		goto out_delete;
	}
	close(fd);
	err = 0;

out_delete:
60
	evlist__delete(evlist);
61 62 63 64 65
	return err;
}

static bool perf_probe_api(setup_probe_fn_t fn)
{
66
	const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
67
	struct perf_cpu_map *cpus;
68 69
	int cpu, ret, i = 0;

70
	cpus = perf_cpu_map__new(NULL);
71 72 73
	if (!cpus)
		return false;
	cpu = cpus->map[0];
74
	perf_cpu_map__put(cpus);
75 76 77 78 79 80 81 82 83 84

	do {
		ret = perf_do_probe_api(fn, cpu, try[i++]);
		if (!ret)
			return true;
	} while (ret == -EAGAIN && try[i]);

	return false;
}

85
static void perf_probe_sample_identifier(struct evsel *evsel)
86
{
87
	evsel->core.attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
88 89
}

90
static void perf_probe_comm_exec(struct evsel *evsel)
91
{
92
	evsel->core.attr.comm_exec = 1;
93 94
}

95
static void perf_probe_context_switch(struct evsel *evsel)
96
{
97
	evsel->core.attr.context_switch = 1;
98 99
}

100 101 102 103
bool perf_can_sample_identifier(void)
{
	return perf_probe_api(perf_probe_sample_identifier);
}
104

105 106 107 108 109
static bool perf_can_comm_exec(void)
{
	return perf_probe_api(perf_probe_comm_exec);
}

110 111 112 113 114
bool perf_can_record_switch_events(void)
{
	return perf_probe_api(perf_probe_context_switch);
}

115 116 117 118 119 120 121
bool perf_can_record_cpu_wide(void)
{
	struct perf_event_attr attr = {
		.type = PERF_TYPE_SOFTWARE,
		.config = PERF_COUNT_SW_CPU_CLOCK,
		.exclude_kernel = 1,
	};
122
	struct perf_cpu_map *cpus;
123 124
	int cpu, fd;

125
	cpus = perf_cpu_map__new(NULL);
126 127 128
	if (!cpus)
		return false;
	cpu = cpus->map[0];
129
	perf_cpu_map__put(cpus);
130 131 132 133 134 135 136 137 138

	fd = sys_perf_event_open(&attr, -1, cpu, -1, 0);
	if (fd < 0)
		return false;
	close(fd);

	return true;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*
 * Architectures are expected to know if AUX area sampling is supported by the
 * hardware. Here we check for kernel support.
 */
bool perf_can_aux_sample(void)
{
	struct perf_event_attr attr = {
		.size = sizeof(struct perf_event_attr),
		.exclude_kernel = 1,
		/*
		 * Non-zero value causes the kernel to calculate the effective
		 * attribute size up to that byte.
		 */
		.aux_sample_size = 1,
	};
	int fd;

	fd = sys_perf_event_open(&attr, -1, 0, -1, 0);
	/*
	 * If the kernel attribute is big enough to contain aux_sample_size
	 * then we assume that it is supported. We are relying on the kernel to
	 * validate the attribute size before anything else that could be wrong.
	 */
	if (fd < 0 && errno == E2BIG)
		return false;
	if (fd >= 0)
		close(fd);

	return true;
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static void perf_evsel__config_leader_sampling(struct evsel *evsel)
{
	struct perf_event_attr *attr = &evsel->core.attr;
	struct evsel *leader = evsel->leader;

	/*
	 * Disable sampling for all group members other
	 * than leader in case leader 'leads' the sampling.
	 */
	if (leader != evsel && leader->sample_read) {
		attr->freq           = 0;
		attr->sample_freq    = 0;
		attr->sample_period  = 0;
		attr->write_backward = 0;

		/*
		 * We don't get sample for slave events, we make them
		 * when delivering group leader sample. Set the slave
		 * event to follow the master sample_type to ease up
		 * report.
		 */
		attr->sample_type = leader->core.attr.sample_type;
	}
}

195
void perf_evlist__config(struct evlist *evlist, struct record_opts *opts,
196
			 struct callchain_param *callchain)
197
{
198
	struct evsel *evsel;
199
	bool use_sample_identifier = false;
200
	bool use_comm_exec;
J
Jiri Olsa 已提交
201
	bool sample_id = opts->sample_id;
202

203 204 205 206 207 208 209
	/*
	 * Set the evsel leader links before we configure attributes,
	 * since some might depend on this info.
	 */
	if (opts->group)
		perf_evlist__set_leader(evlist);

210
	if (evlist->core.cpus->map[0] < 0)
211 212
		opts->no_inherit = true;

213 214
	use_comm_exec = perf_can_comm_exec();

215
	evlist__for_each_entry(evlist, evsel) {
216
		perf_evsel__config(evsel, opts, callchain);
217
		if (evsel->tracking && use_comm_exec)
218
			evsel->core.attr.comm_exec = 1;
219
	}
220

221 222 223 224
	/* Configure leader sampling here now that the sample type is known */
	evlist__for_each_entry(evlist, evsel)
		perf_evsel__config_leader_sampling(evsel);

225 226 227 228 229 230 231
	if (opts->full_auxtrace) {
		/*
		 * Need to be able to synthesize and parse selected events with
		 * arbitrary sample types, which requires always being able to
		 * match the id.
		 */
		use_sample_identifier = perf_can_sample_identifier();
J
Jiri Olsa 已提交
232
		sample_id = true;
233
	} else if (evlist->core.nr_entries > 1) {
234
		struct evsel *first = evlist__first(evlist);
235

236
		evlist__for_each_entry(evlist, evsel) {
237
			if (evsel->core.attr.sample_type == first->core.attr.sample_type)
238 239 240 241
				continue;
			use_sample_identifier = perf_can_sample_identifier();
			break;
		}
J
Jiri Olsa 已提交
242 243 244 245
		sample_id = true;
	}

	if (sample_id) {
246
		evlist__for_each_entry(evlist, evsel)
247
			perf_evsel__set_sample_id(evsel, use_sample_identifier);
248
	}
249 250

	perf_evlist__set_id_pos(evlist);
251
}
252 253 254

static int get_max_rate(unsigned int *rate)
{
255
	return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
256 257
}

258
static int record_opts__config_freq(struct record_opts *opts)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
{
	bool user_freq = opts->user_freq != UINT_MAX;
	unsigned int max_rate;

	if (opts->user_interval != ULLONG_MAX)
		opts->default_interval = opts->user_interval;
	if (user_freq)
		opts->freq = opts->user_freq;

	/*
	 * User specified count overrides default frequency.
	 */
	if (opts->default_interval)
		opts->freq = 0;
	else if (opts->freq) {
		opts->default_interval = opts->freq;
	} else {
		pr_err("frequency and count are zero, aborting\n");
		return -1;
	}

	if (get_max_rate(&max_rate))
		return 0;

	/*
	 * User specified frequency is over current maximum.
	 */
	if (user_freq && (max_rate < opts->freq)) {
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		if (opts->strict_freq) {
			pr_err("error: Maximum frequency rate (%'u Hz) exceeded.\n"
			       "       Please use -F freq option with a lower value or consider\n"
			       "       tweaking /proc/sys/kernel/perf_event_max_sample_rate.\n",
			       max_rate);
			return -1;
		} else {
			pr_warning("warning: Maximum frequency rate (%'u Hz) exceeded, throttling from %'u Hz to %'u Hz.\n"
				   "         The limit can be raised via /proc/sys/kernel/perf_event_max_sample_rate.\n"
				   "         The kernel will lower it when perf's interrupts take too long.\n"
				   "         Use --strict-freq to disable this throttling, refusing to record.\n",
				   max_rate, opts->freq, max_rate);

			opts->freq = max_rate;
		}
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	}

	/*
	 * Default frequency is over current maximum.
	 */
	if (max_rate < opts->freq) {
		pr_warning("Lowering default frequency rate to %u.\n"
			   "Please consider tweaking "
			   "/proc/sys/kernel/perf_event_max_sample_rate.\n",
			   max_rate);
		opts->freq = max_rate;
	}

	return 0;
}

318
int record_opts__config(struct record_opts *opts)
319
{
320
	return record_opts__config_freq(opts);
321
}
322

323
bool perf_evlist__can_select_event(struct evlist *evlist, const char *str)
324
{
325
	struct evlist *temp_evlist;
326
	struct evsel *evsel;
327 328
	int err, fd, cpu;
	bool ret = false;
329
	pid_t pid = -1;
330

331
	temp_evlist = evlist__new();
332 333 334
	if (!temp_evlist)
		return false;

335
	err = parse_events(temp_evlist, str, NULL);
336 337 338
	if (err)
		goto out_delete;

339
	evsel = evlist__last(temp_evlist);
340

341
	if (!evlist || perf_cpu_map__empty(evlist->core.cpus)) {
342
		struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
343 344

		cpu =  cpus ? cpus->map[0] : 0;
345
		perf_cpu_map__put(cpus);
346
	} else {
347
		cpu = evlist->core.cpus->map[0];
348 349
	}

350
	while (1) {
351
		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1,
352 353 354 355 356 357 358 359 360
					 perf_event_open_cloexec_flag());
		if (fd < 0) {
			if (pid == -1 && errno == EACCES) {
				pid = 0;
				continue;
			}
			goto out_delete;
		}
		break;
361
	}
362 363
	close(fd);
	ret = true;
364 365

out_delete:
366
	evlist__delete(temp_evlist);
367 368
	return ret;
}
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

int record__parse_freq(const struct option *opt, const char *str, int unset __maybe_unused)
{
	unsigned int freq;
	struct record_opts *opts = opt->value;

	if (!str)
		return -EINVAL;

	if (strcasecmp(str, "max") == 0) {
		if (get_max_rate(&freq)) {
			pr_err("couldn't read /proc/sys/kernel/perf_event_max_sample_rate\n");
			return -1;
		}
		pr_info("info: Using a maximum frequency rate of %'d Hz\n", freq);
	} else {
		freq = atoi(str);
	}

	opts->user_freq = freq;
	return 0;
}