blob: fe8079edbdc11a239ee26524ac51d04b3d40f895 [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;
17
18 evlist = perf_evlist__new();
19 if (!evlist)
20 return -ENOMEM;
21
22 if (parse_events(evlist, str))
23 goto out_delete;
24
25 evsel = perf_evlist__first(evlist);
26
Yann Droneaud57480d22014-06-30 22:28:47 +020027 fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, flags);
Adrian Hunter75562572013-08-27 11:23:09 +030028 if (fd < 0)
29 goto out_delete;
30 close(fd);
31
32 fn(evsel);
33
Yann Droneaud57480d22014-06-30 22:28:47 +020034 fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, flags);
Adrian Hunter75562572013-08-27 11:23:09 +030035 if (fd < 0) {
36 if (errno == EINVAL)
37 err = -EINVAL;
38 goto out_delete;
39 }
40 close(fd);
41 err = 0;
42
43out_delete:
44 perf_evlist__delete(evlist);
45 return err;
46}
47
48static bool perf_probe_api(setup_probe_fn_t fn)
49{
50 const char *try[] = {"cycles:u", "instructions:u", "cpu-clock", NULL};
51 struct cpu_map *cpus;
52 int cpu, ret, i = 0;
53
54 cpus = cpu_map__new(NULL);
55 if (!cpus)
56 return false;
57 cpu = cpus->map[0];
58 cpu_map__delete(cpus);
59
60 do {
61 ret = perf_do_probe_api(fn, cpu, try[i++]);
62 if (!ret)
63 return true;
64 } while (ret == -EAGAIN && try[i]);
65
66 return false;
67}
68
69static void perf_probe_sample_identifier(struct perf_evsel *evsel)
70{
71 evsel->attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
72}
73
Adrian Hunter39e09d42014-07-14 13:02:28 +030074static void perf_probe_comm_exec(struct perf_evsel *evsel)
75{
76 evsel->attr.comm_exec = 1;
77}
78
Adrian Hunter75562572013-08-27 11:23:09 +030079bool perf_can_sample_identifier(void)
80{
81 return perf_probe_api(perf_probe_sample_identifier);
82}
Adrian Hunterfaf96702013-08-27 11:23:08 +030083
Adrian Hunter39e09d42014-07-14 13:02:28 +030084static bool perf_can_comm_exec(void)
85{
86 return perf_probe_api(perf_probe_comm_exec);
87}
88
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030089void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts)
Adrian Hunterfaf96702013-08-27 11:23:08 +030090{
91 struct perf_evsel *evsel;
Adrian Hunter75562572013-08-27 11:23:09 +030092 bool use_sample_identifier = false;
Adrian Hunter39e09d42014-07-14 13:02:28 +030093 bool use_comm_exec;
Adrian Hunter75562572013-08-27 11:23:09 +030094
Adrian Hunterfaf96702013-08-27 11:23:08 +030095 /*
96 * Set the evsel leader links before we configure attributes,
97 * since some might depend on this info.
98 */
99 if (opts->group)
100 perf_evlist__set_leader(evlist);
101
102 if (evlist->cpus->map[0] < 0)
103 opts->no_inherit = true;
104
Adrian Hunter39e09d42014-07-14 13:02:28 +0300105 use_comm_exec = perf_can_comm_exec();
106
107 evlist__for_each(evlist, evsel) {
Adrian Hunterfaf96702013-08-27 11:23:08 +0300108 perf_evsel__config(evsel, opts);
Adrian Hunter39e09d42014-07-14 13:02:28 +0300109 if (!evsel->idx && use_comm_exec)
110 evsel->attr.comm_exec = 1;
111 }
Adrian Hunterfaf96702013-08-27 11:23:08 +0300112
Adrian Hunter75562572013-08-27 11:23:09 +0300113 if (evlist->nr_entries > 1) {
114 struct perf_evsel *first = perf_evlist__first(evlist);
115
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300116 evlist__for_each(evlist, evsel) {
Adrian Hunter75562572013-08-27 11:23:09 +0300117 if (evsel->attr.sample_type == first->attr.sample_type)
118 continue;
119 use_sample_identifier = perf_can_sample_identifier();
120 break;
121 }
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300122 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +0300123 perf_evsel__set_sample_id(evsel, use_sample_identifier);
Adrian Hunterfaf96702013-08-27 11:23:08 +0300124 }
Adrian Hunter75562572013-08-27 11:23:09 +0300125
126 perf_evlist__set_id_pos(evlist);
Adrian Hunterfaf96702013-08-27 11:23:08 +0300127}
Jiri Olsa714647b2013-11-05 15:14:47 +0100128
129static int get_max_rate(unsigned int *rate)
130{
131 char path[PATH_MAX];
132 const char *procfs = procfs__mountpoint();
133
134 if (!procfs)
135 return -1;
136
137 snprintf(path, PATH_MAX,
138 "%s/sys/kernel/perf_event_max_sample_rate", procfs);
139
140 return filename__read_int(path, (int *) rate);
141}
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;
204
205 temp_evlist = perf_evlist__new();
206 if (!temp_evlist)
207 return false;
208
209 err = parse_events(temp_evlist, str);
210 if (err)
211 goto out_delete;
212
213 evsel = perf_evlist__last(temp_evlist);
214
215 if (!evlist || cpu_map__empty(evlist->cpus)) {
216 struct cpu_map *cpus = cpu_map__new(NULL);
217
218 cpu = cpus ? cpus->map[0] : 0;
219 cpu_map__delete(cpus);
220 } else {
221 cpu = evlist->cpus->map[0];
222 }
223
Yann Droneaud57480d22014-06-30 22:28:47 +0200224 fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1,
225 perf_event_open_cloexec_flag());
Adrian Hunterc09ec622013-12-11 14:36:29 +0200226 if (fd >= 0) {
227 close(fd);
228 ret = true;
229 }
230
231out_delete:
232 perf_evlist__delete(temp_evlist);
233 return ret;
234}