blob: 966a94fa8200bb189b6b2d3c5c884130c2344b9c [file] [log] [blame]
Namhyung Kimd01f4e82013-03-07 21:45:20 +09001/*
2 * builtin-ftrace.c
3 *
4 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
5 *
6 * Released under the GPL v2.
7 */
8
9#include "builtin.h"
10#include "perf.h"
11
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030012#include <errno.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090013#include <unistd.h>
14#include <signal.h>
Namhyung Kima9af6be2017-02-24 10:12:48 +090015#include <fcntl.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030016#include <poll.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090017
18#include "debug.h"
19#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo20a9ed22017-04-18 11:44:58 -030020#include <api/fs/tracing_path.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090021#include "evlist.h"
22#include "target.h"
Namhyung Kimdc231032017-02-24 10:12:50 +090023#include "cpumap.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090024#include "thread_map.h"
Taeung Songb05d1092017-01-31 20:38:29 +090025#include "util/config.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090026
27
28#define DEFAULT_TRACER "function_graph"
29
30struct perf_ftrace {
31 struct perf_evlist *evlist;
32 struct target target;
33 const char *tracer;
34};
35
36static bool done;
37
38static void sig_handler(int sig __maybe_unused)
39{
40 done = true;
41}
42
43/*
44 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
45 * we asked by setting its exec_error to the function below,
46 * ftrace__workload_exec_failed_signal.
47 *
48 * XXX We need to handle this more appropriately, emitting an error, etc.
49 */
50static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
51 siginfo_t *info __maybe_unused,
52 void *ucontext __maybe_unused)
53{
54 /* workload_exec_errno = info->si_value.sival_int; */
55 done = true;
56}
57
Namhyung Kima9af6be2017-02-24 10:12:48 +090058static int __write_tracing_file(const char *name, const char *val, bool append)
Namhyung Kimd01f4e82013-03-07 21:45:20 +090059{
60 char *file;
61 int fd, ret = -1;
62 ssize_t size = strlen(val);
Namhyung Kima9af6be2017-02-24 10:12:48 +090063 int flags = O_WRONLY;
Namhyung Kime7bd9ba2017-06-18 23:22:59 +090064 char errbuf[512];
Namhyung Kimd01f4e82013-03-07 21:45:20 +090065
66 file = get_tracing_file(name);
67 if (!file) {
68 pr_debug("cannot get tracing file: %s\n", name);
69 return -1;
70 }
71
Namhyung Kima9af6be2017-02-24 10:12:48 +090072 if (append)
73 flags |= O_APPEND;
74 else
75 flags |= O_TRUNC;
76
77 fd = open(file, flags);
Namhyung Kimd01f4e82013-03-07 21:45:20 +090078 if (fd < 0) {
Namhyung Kime7bd9ba2017-06-18 23:22:59 +090079 pr_debug("cannot open tracing file: %s: %s\n",
80 name, str_error_r(errno, errbuf, sizeof(errbuf)));
Namhyung Kimd01f4e82013-03-07 21:45:20 +090081 goto out;
82 }
83
84 if (write(fd, val, size) == size)
85 ret = 0;
86 else
Namhyung Kime7bd9ba2017-06-18 23:22:59 +090087 pr_debug("write '%s' to tracing/%s failed: %s\n",
88 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
Namhyung Kimd01f4e82013-03-07 21:45:20 +090089
90 close(fd);
91out:
92 put_tracing_file(file);
93 return ret;
94}
95
Namhyung Kima9af6be2017-02-24 10:12:48 +090096static int write_tracing_file(const char *name, const char *val)
97{
98 return __write_tracing_file(name, val, false);
99}
100
101static int append_tracing_file(const char *name, const char *val)
102{
103 return __write_tracing_file(name, val, true);
104}
105
Namhyung Kimdc231032017-02-24 10:12:50 +0900106static int reset_tracing_cpu(void);
107
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900108static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
109{
110 if (write_tracing_file("tracing_on", "0") < 0)
111 return -1;
112
113 if (write_tracing_file("current_tracer", "nop") < 0)
114 return -1;
115
116 if (write_tracing_file("set_ftrace_pid", " ") < 0)
117 return -1;
118
Namhyung Kimdc231032017-02-24 10:12:50 +0900119 if (reset_tracing_cpu() < 0)
120 return -1;
121
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900122 return 0;
123}
124
Namhyung Kima9af6be2017-02-24 10:12:48 +0900125static int set_tracing_pid(struct perf_ftrace *ftrace)
126{
127 int i;
128 char buf[16];
129
130 if (target__has_cpu(&ftrace->target))
131 return 0;
132
133 for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
134 scnprintf(buf, sizeof(buf), "%d",
135 ftrace->evlist->threads->map[i]);
136 if (append_tracing_file("set_ftrace_pid", buf) < 0)
137 return -1;
138 }
139 return 0;
140}
141
Namhyung Kimdc231032017-02-24 10:12:50 +0900142static int set_tracing_cpumask(struct cpu_map *cpumap)
143{
144 char *cpumask;
145 size_t mask_size;
146 int ret;
147 int last_cpu;
148
149 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
150 mask_size = (last_cpu + 3) / 4 + 1;
151 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
152
153 cpumask = malloc(mask_size);
154 if (cpumask == NULL) {
155 pr_debug("failed to allocate cpu mask\n");
156 return -1;
157 }
158
159 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
160
161 ret = write_tracing_file("tracing_cpumask", cpumask);
162
163 free(cpumask);
164 return ret;
165}
166
167static int set_tracing_cpu(struct perf_ftrace *ftrace)
168{
169 struct cpu_map *cpumap = ftrace->evlist->cpus;
170
171 if (!target__has_cpu(&ftrace->target))
172 return 0;
173
174 return set_tracing_cpumask(cpumap);
175}
176
177static int reset_tracing_cpu(void)
178{
179 struct cpu_map *cpumap = cpu_map__new(NULL);
180 int ret;
181
182 ret = set_tracing_cpumask(cpumap);
183 cpu_map__put(cpumap);
184 return ret;
185}
186
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900187static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
188{
189 char *trace_file;
190 int trace_fd;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900191 char buf[4096];
192 struct pollfd pollfd = {
193 .events = POLLIN,
194 };
195
196 if (geteuid() != 0) {
197 pr_err("ftrace only works for root!\n");
198 return -1;
199 }
200
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900201 signal(SIGINT, sig_handler);
202 signal(SIGUSR1, sig_handler);
203 signal(SIGCHLD, sig_handler);
Namhyung Kim58335962017-02-24 10:12:51 +0900204 signal(SIGPIPE, sig_handler);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900205
Namhyung Kima9af6be2017-02-24 10:12:48 +0900206 if (reset_tracing_files(ftrace) < 0)
207 goto out;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900208
209 /* reset ftrace buffer */
210 if (write_tracing_file("trace", "0") < 0)
211 goto out;
212
Namhyung Kima9af6be2017-02-24 10:12:48 +0900213 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
214 &ftrace->target, argv, false,
215 ftrace__workload_exec_failed_signal) < 0) {
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900216 goto out;
Namhyung Kima9af6be2017-02-24 10:12:48 +0900217 }
218
219 if (set_tracing_pid(ftrace) < 0) {
220 pr_err("failed to set ftrace pid\n");
221 goto out_reset;
222 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900223
Namhyung Kimdc231032017-02-24 10:12:50 +0900224 if (set_tracing_cpu(ftrace) < 0) {
225 pr_err("failed to set tracing cpumask\n");
226 goto out_reset;
227 }
228
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900229 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
230 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900231 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900232 }
233
234 trace_file = get_tracing_file("trace_pipe");
235 if (!trace_file) {
236 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900237 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900238 }
239
240 trace_fd = open(trace_file, O_RDONLY);
241
242 put_tracing_file(trace_file);
243
244 if (trace_fd < 0) {
245 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900246 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900247 }
248
249 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
250 pollfd.fd = trace_fd;
251
252 if (write_tracing_file("tracing_on", "1") < 0) {
253 pr_err("can't enable tracing\n");
254 goto out_close_fd;
255 }
256
Namhyung Kim58335962017-02-24 10:12:51 +0900257 setup_pager();
258
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900259 perf_evlist__start_workload(ftrace->evlist);
260
261 while (!done) {
262 if (poll(&pollfd, 1, -1) < 0)
263 break;
264
265 if (pollfd.revents & POLLIN) {
266 int n = read(trace_fd, buf, sizeof(buf));
267 if (n < 0)
268 break;
269 if (fwrite(buf, n, 1, stdout) != 1)
270 break;
271 }
272 }
273
274 write_tracing_file("tracing_on", "0");
275
276 /* read remaining buffer contents */
277 while (true) {
278 int n = read(trace_fd, buf, sizeof(buf));
279 if (n <= 0)
280 break;
281 if (fwrite(buf, n, 1, stdout) != 1)
282 break;
283 }
284
285out_close_fd:
286 close(trace_fd);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900287out_reset:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900288 reset_tracing_files(ftrace);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900289out:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900290 return done ? 0 : -1;
291}
292
Taeung Songb05d1092017-01-31 20:38:29 +0900293static int perf_ftrace_config(const char *var, const char *value, void *cb)
294{
295 struct perf_ftrace *ftrace = cb;
296
297 if (prefixcmp(var, "ftrace."))
298 return 0;
299
300 if (strcmp(var, "ftrace.tracer"))
301 return -1;
302
303 if (!strcmp(value, "function_graph") ||
304 !strcmp(value, "function")) {
305 ftrace->tracer = value;
306 return 0;
307 }
308
309 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
310 return -1;
311}
312
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300313int cmd_ftrace(int argc, const char **argv)
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900314{
315 int ret;
316 struct perf_ftrace ftrace = {
Taeung Songbf062bd2017-01-26 18:35:37 +0900317 .tracer = DEFAULT_TRACER,
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900318 .target = { .uid = UINT_MAX, },
319 };
320 const char * const ftrace_usage[] = {
Namhyung Kima9af6be2017-02-24 10:12:48 +0900321 "perf ftrace [<options>] [<command>]",
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900322 "perf ftrace [<options>] -- <command> [<options>]",
323 NULL
324 };
325 const struct option ftrace_options[] = {
326 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
Arnaldo Carvalho de Meloec347872017-01-18 21:49:14 -0300327 "tracer to use: function_graph(default) or function"),
Namhyung Kima9af6be2017-02-24 10:12:48 +0900328 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
329 "trace on existing process id"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900330 OPT_INCR('v', "verbose", &verbose,
331 "be more verbose"),
Namhyung Kimdc231032017-02-24 10:12:50 +0900332 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
333 "system-wide collection from all CPUs"),
334 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
335 "list of cpus to monitor"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900336 OPT_END()
337 };
338
Taeung Songb05d1092017-01-31 20:38:29 +0900339 ret = perf_config(perf_ftrace_config, &ftrace);
340 if (ret < 0)
341 return -1;
342
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900343 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
344 PARSE_OPT_STOP_AT_NON_OPTION);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900345 if (!argc && target__none(&ftrace.target))
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900346 usage_with_options(ftrace_usage, ftrace_options);
347
Namhyung Kima9af6be2017-02-24 10:12:48 +0900348 ret = target__validate(&ftrace.target);
349 if (ret) {
350 char errbuf[512];
351
352 target__strerror(&ftrace.target, ret, errbuf, 512);
353 pr_err("%s\n", errbuf);
354 return -EINVAL;
355 }
356
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900357 ftrace.evlist = perf_evlist__new();
358 if (ftrace.evlist == NULL)
359 return -ENOMEM;
360
361 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
362 if (ret < 0)
363 goto out_delete_evlist;
364
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900365 ret = __cmd_ftrace(&ftrace, argc, argv);
366
367out_delete_evlist:
368 perf_evlist__delete(ftrace.evlist);
369
370 return ret;
371}