blob: 8acd0df88b5c4b75063d7ad2f83a55ef4f4f6286 [file] [log] [blame]
Adrian Hunterfaf96702013-08-27 11:23:08 +03001#include "evlist.h"
2#include "evsel.h"
3#include "cpumap.h"
Adrian Hunter75562572013-08-27 11:23:09 +03004#include "parse-events.h"
Borislav Petkovcd0cfad2013-12-09 17:14:24 +01005#include <api/fs/fs.h>
Jiri Olsa714647b2013-11-05 15:14:47 +01006#include "util.h"
Yann Droneaud57480d22014-06-30 22:28:47 +02007#include "cloexec.h"
Adrian Hunter75562572013-08-27 11:23:09 +03008
9typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel);
10
11static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
12{
13 struct perf_evlist *evlist;
14 struct perf_evsel *evsel;
Yann Droneaud57480d22014-06-30 22:28:47 +020015 unsigned long flags = perf_event_open_cloexec_flag();
Adrian Hunter75562572013-08-27 11:23:09 +030016 int err = -EAGAIN, fd;
Adrian Hunter46ec69a2014-08-12 18:04:30 +030017 static pid_t pid = -1;
Adrian Hunter75562572013-08-27 11:23:09 +030018
19 evlist = perf_evlist__new();
20 if (!evlist)
21 return -ENOMEM;
22
23 if (parse_events(evlist, str))
24 goto out_delete;
25
26 evsel = perf_evlist__first(evlist);
27
Adrian Hunter46ec69a2014-08-12 18:04:30 +030028 while (1) {
29 fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1, flags);
30 if (fd < 0) {
31 if (pid == -1 && errno == EACCES) {
32 pid = 0;
33 continue;
34 }
35 goto out_delete;
36 }
37 break;
38 }
Adrian Hunter75562572013-08-27 11:23:09 +030039 close(fd);
40
41 fn(evsel);
42
Adrian Hunter46ec69a2014-08-12 18:04:30 +030043 fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1, flags);
Adrian Hunter75562572013-08-27 11:23:09 +030044 if (fd < 0) {
45 if (errno == EINVAL)
46 err = -EINVAL;
47 goto out_delete;
48 }
49 close(fd);
50 err = 0;
51
52out_delete:
53 perf_evlist__delete(evlist);
54 return err;
55}
56
57static bool perf_probe_api(setup_probe_fn_t fn)
58{
Adrian Hunterc6fa3562014-08-12 18:04:29 +030059 const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
Adrian Hunter75562572013-08-27 11:23:09 +030060 struct cpu_map *cpus;
61 int cpu, ret, i = 0;
62
63 cpus = cpu_map__new(NULL);
64 if (!cpus)
65 return false;
66 cpu = cpus->map[0];
67 cpu_map__delete(cpus);
68
69 do {
70 ret = perf_do_probe_api(fn, cpu, try[i++]);
71 if (!ret)
72 return true;
73 } while (ret == -EAGAIN && try[i]);
74
75 return false;
76}
77
78static void perf_probe_sample_identifier(struct perf_evsel *evsel)
79{
80 evsel->attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
81}
82
Adrian Hunter39e09d42014-07-14 13:02:28 +030083static void perf_probe_comm_exec(struct perf_evsel *evsel)
84{
85 evsel->attr.comm_exec = 1;
86}
87
Adrian Hunter75562572013-08-27 11:23:09 +030088bool perf_can_sample_identifier(void)
89{
90 return perf_probe_api(perf_probe_sample_identifier);
91}
Adrian Hunterfaf96702013-08-27 11:23:08 +030092
Adrian Hunter39e09d42014-07-14 13:02:28 +030093static bool perf_can_comm_exec(void)
94{
95 return perf_probe_api(perf_probe_comm_exec);
96}
97
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030098void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts)
Adrian Hunterfaf96702013-08-27 11:23:08 +030099{
100 struct perf_evsel *evsel;
Adrian Hunter75562572013-08-27 11:23:09 +0300101 bool use_sample_identifier = false;
Adrian Hunter39e09d42014-07-14 13:02:28 +0300102 bool use_comm_exec;
Adrian Hunter75562572013-08-27 11:23:09 +0300103
Adrian Hunterfaf96702013-08-27 11:23:08 +0300104 /*
105 * Set the evsel leader links before we configure attributes,
106 * since some might depend on this info.
107 */
108 if (opts->group)
109 perf_evlist__set_leader(evlist);
110
111 if (evlist->cpus->map[0] < 0)
112 opts->no_inherit = true;
113
Adrian Hunter39e09d42014-07-14 13:02:28 +0300114 use_comm_exec = perf_can_comm_exec();
115
116 evlist__for_each(evlist, evsel) {
Adrian Hunterfaf96702013-08-27 11:23:08 +0300117 perf_evsel__config(evsel, opts);
Adrian Hunter60b08962014-07-31 09:00:52 +0300118 if (evsel->tracking && use_comm_exec)
Adrian Hunter39e09d42014-07-14 13:02:28 +0300119 evsel->attr.comm_exec = 1;
120 }
Adrian Hunterfaf96702013-08-27 11:23:08 +0300121
Adrian Hunter75562572013-08-27 11:23:09 +0300122 if (evlist->nr_entries > 1) {
123 struct perf_evsel *first = perf_evlist__first(evlist);
124
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300125 evlist__for_each(evlist, evsel) {
Adrian Hunter75562572013-08-27 11:23:09 +0300126 if (evsel->attr.sample_type == first->attr.sample_type)
127 continue;
128 use_sample_identifier = perf_can_sample_identifier();
129 break;
130 }
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300131 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +0300132 perf_evsel__set_sample_id(evsel, use_sample_identifier);
Adrian Hunterfaf96702013-08-27 11:23:08 +0300133 }
Adrian Hunter75562572013-08-27 11:23:09 +0300134
135 perf_evlist__set_id_pos(evlist);
Adrian Hunterfaf96702013-08-27 11:23:08 +0300136}
Jiri Olsa714647b2013-11-05 15:14:47 +0100137
138static int get_max_rate(unsigned int *rate)
139{
Arnaldo Carvalho de Meloce273092014-12-11 13:37:59 -0300140 return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
Jiri Olsa714647b2013-11-05 15:14:47 +0100141}
142
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300143static int record_opts__config_freq(struct record_opts *opts)
Jiri Olsa714647b2013-11-05 15:14:47 +0100144{
145 bool user_freq = opts->user_freq != UINT_MAX;
146 unsigned int max_rate;
147
148 if (opts->user_interval != ULLONG_MAX)
149 opts->default_interval = opts->user_interval;
150 if (user_freq)
151 opts->freq = opts->user_freq;
152
153 /*
154 * User specified count overrides default frequency.
155 */
156 if (opts->default_interval)
157 opts->freq = 0;
158 else if (opts->freq) {
159 opts->default_interval = opts->freq;
160 } else {
161 pr_err("frequency and count are zero, aborting\n");
162 return -1;
163 }
164
165 if (get_max_rate(&max_rate))
166 return 0;
167
168 /*
169 * User specified frequency is over current maximum.
170 */
171 if (user_freq && (max_rate < opts->freq)) {
172 pr_err("Maximum frequency rate (%u) reached.\n"
173 "Please use -F freq option with lower value or consider\n"
174 "tweaking /proc/sys/kernel/perf_event_max_sample_rate.\n",
175 max_rate);
176 return -1;
177 }
178
179 /*
180 * Default frequency is over current maximum.
181 */
182 if (max_rate < opts->freq) {
183 pr_warning("Lowering default frequency rate to %u.\n"
184 "Please consider tweaking "
185 "/proc/sys/kernel/perf_event_max_sample_rate.\n",
186 max_rate);
187 opts->freq = max_rate;
188 }
189
190 return 0;
191}
192
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300193int record_opts__config(struct record_opts *opts)
Jiri Olsa714647b2013-11-05 15:14:47 +0100194{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300195 return record_opts__config_freq(opts);
Jiri Olsa714647b2013-11-05 15:14:47 +0100196}
Adrian Hunterc09ec622013-12-11 14:36:29 +0200197
198bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str)
199{
200 struct perf_evlist *temp_evlist;
201 struct perf_evsel *evsel;
202 int err, fd, cpu;
203 bool ret = false;
Adrian Hunter46ec69a2014-08-12 18:04:30 +0300204 pid_t pid = -1;
Adrian Hunterc09ec622013-12-11 14:36:29 +0200205
206 temp_evlist = perf_evlist__new();
207 if (!temp_evlist)
208 return false;
209
210 err = parse_events(temp_evlist, str);
211 if (err)
212 goto out_delete;
213
214 evsel = perf_evlist__last(temp_evlist);
215
216 if (!evlist || cpu_map__empty(evlist->cpus)) {
217 struct cpu_map *cpus = cpu_map__new(NULL);
218
219 cpu = cpus ? cpus->map[0] : 0;
220 cpu_map__delete(cpus);
221 } else {
222 cpu = evlist->cpus->map[0];
223 }
224
Adrian Hunter46ec69a2014-08-12 18:04:30 +0300225 while (1) {
226 fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1,
227 perf_event_open_cloexec_flag());
228 if (fd < 0) {
229 if (pid == -1 && errno == EACCES) {
230 pid = 0;
231 continue;
232 }
233 goto out_delete;
234 }
235 break;
Adrian Hunterc09ec622013-12-11 14:36:29 +0200236 }
Adrian Hunter46ec69a2014-08-12 18:04:30 +0300237 close(fd);
238 ret = true;
Adrian Hunterc09ec622013-12-11 14:36:29 +0200239
240out_delete:
241 perf_evlist__delete(temp_evlist);
242 return ret;
243}