blob: f80fb60b00b0b658f44eeb758d52b7e5065192ce [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
12#include <unistd.h>
13#include <signal.h>
Namhyung Kima9af6be2017-02-24 10:12:48 +090014#include <fcntl.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090015
16#include "debug.h"
17#include <subcmd/parse-options.h>
18#include "evlist.h"
19#include "target.h"
Namhyung Kimdc231032017-02-24 10:12:50 +090020#include "cpumap.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090021#include "thread_map.h"
Taeung Songb05d1092017-01-31 20:38:29 +090022#include "util/config.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090023
24
25#define DEFAULT_TRACER "function_graph"
26
27struct perf_ftrace {
28 struct perf_evlist *evlist;
29 struct target target;
30 const char *tracer;
31};
32
33static bool done;
34
35static void sig_handler(int sig __maybe_unused)
36{
37 done = true;
38}
39
40/*
41 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
42 * we asked by setting its exec_error to the function below,
43 * ftrace__workload_exec_failed_signal.
44 *
45 * XXX We need to handle this more appropriately, emitting an error, etc.
46 */
47static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
48 siginfo_t *info __maybe_unused,
49 void *ucontext __maybe_unused)
50{
51 /* workload_exec_errno = info->si_value.sival_int; */
52 done = true;
53}
54
Namhyung Kima9af6be2017-02-24 10:12:48 +090055static int __write_tracing_file(const char *name, const char *val, bool append)
Namhyung Kimd01f4e82013-03-07 21:45:20 +090056{
57 char *file;
58 int fd, ret = -1;
59 ssize_t size = strlen(val);
Namhyung Kima9af6be2017-02-24 10:12:48 +090060 int flags = O_WRONLY;
Namhyung Kimd01f4e82013-03-07 21:45:20 +090061
62 file = get_tracing_file(name);
63 if (!file) {
64 pr_debug("cannot get tracing file: %s\n", name);
65 return -1;
66 }
67
Namhyung Kima9af6be2017-02-24 10:12:48 +090068 if (append)
69 flags |= O_APPEND;
70 else
71 flags |= O_TRUNC;
72
73 fd = open(file, flags);
Namhyung Kimd01f4e82013-03-07 21:45:20 +090074 if (fd < 0) {
75 pr_debug("cannot open tracing file: %s\n", name);
76 goto out;
77 }
78
79 if (write(fd, val, size) == size)
80 ret = 0;
81 else
82 pr_debug("write '%s' to tracing/%s failed\n", val, name);
83
84 close(fd);
85out:
86 put_tracing_file(file);
87 return ret;
88}
89
Namhyung Kima9af6be2017-02-24 10:12:48 +090090static int write_tracing_file(const char *name, const char *val)
91{
92 return __write_tracing_file(name, val, false);
93}
94
95static int append_tracing_file(const char *name, const char *val)
96{
97 return __write_tracing_file(name, val, true);
98}
99
Namhyung Kimdc231032017-02-24 10:12:50 +0900100static int reset_tracing_cpu(void);
101
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900102static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
103{
104 if (write_tracing_file("tracing_on", "0") < 0)
105 return -1;
106
107 if (write_tracing_file("current_tracer", "nop") < 0)
108 return -1;
109
110 if (write_tracing_file("set_ftrace_pid", " ") < 0)
111 return -1;
112
Namhyung Kimdc231032017-02-24 10:12:50 +0900113 if (reset_tracing_cpu() < 0)
114 return -1;
115
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900116 return 0;
117}
118
Namhyung Kima9af6be2017-02-24 10:12:48 +0900119static int set_tracing_pid(struct perf_ftrace *ftrace)
120{
121 int i;
122 char buf[16];
123
124 if (target__has_cpu(&ftrace->target))
125 return 0;
126
127 for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
128 scnprintf(buf, sizeof(buf), "%d",
129 ftrace->evlist->threads->map[i]);
130 if (append_tracing_file("set_ftrace_pid", buf) < 0)
131 return -1;
132 }
133 return 0;
134}
135
Namhyung Kimdc231032017-02-24 10:12:50 +0900136static int set_tracing_cpumask(struct cpu_map *cpumap)
137{
138 char *cpumask;
139 size_t mask_size;
140 int ret;
141 int last_cpu;
142
143 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
144 mask_size = (last_cpu + 3) / 4 + 1;
145 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
146
147 cpumask = malloc(mask_size);
148 if (cpumask == NULL) {
149 pr_debug("failed to allocate cpu mask\n");
150 return -1;
151 }
152
153 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
154
155 ret = write_tracing_file("tracing_cpumask", cpumask);
156
157 free(cpumask);
158 return ret;
159}
160
161static int set_tracing_cpu(struct perf_ftrace *ftrace)
162{
163 struct cpu_map *cpumap = ftrace->evlist->cpus;
164
165 if (!target__has_cpu(&ftrace->target))
166 return 0;
167
168 return set_tracing_cpumask(cpumap);
169}
170
171static int reset_tracing_cpu(void)
172{
173 struct cpu_map *cpumap = cpu_map__new(NULL);
174 int ret;
175
176 ret = set_tracing_cpumask(cpumap);
177 cpu_map__put(cpumap);
178 return ret;
179}
180
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900181static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
182{
183 char *trace_file;
184 int trace_fd;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900185 char buf[4096];
186 struct pollfd pollfd = {
187 .events = POLLIN,
188 };
189
190 if (geteuid() != 0) {
191 pr_err("ftrace only works for root!\n");
192 return -1;
193 }
194
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900195 signal(SIGINT, sig_handler);
196 signal(SIGUSR1, sig_handler);
197 signal(SIGCHLD, sig_handler);
Namhyung Kim58335962017-02-24 10:12:51 +0900198 signal(SIGPIPE, sig_handler);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900199
Namhyung Kima9af6be2017-02-24 10:12:48 +0900200 if (reset_tracing_files(ftrace) < 0)
201 goto out;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900202
203 /* reset ftrace buffer */
204 if (write_tracing_file("trace", "0") < 0)
205 goto out;
206
Namhyung Kima9af6be2017-02-24 10:12:48 +0900207 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
208 &ftrace->target, argv, false,
209 ftrace__workload_exec_failed_signal) < 0) {
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900210 goto out;
Namhyung Kima9af6be2017-02-24 10:12:48 +0900211 }
212
213 if (set_tracing_pid(ftrace) < 0) {
214 pr_err("failed to set ftrace pid\n");
215 goto out_reset;
216 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900217
Namhyung Kimdc231032017-02-24 10:12:50 +0900218 if (set_tracing_cpu(ftrace) < 0) {
219 pr_err("failed to set tracing cpumask\n");
220 goto out_reset;
221 }
222
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900223 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
224 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900225 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900226 }
227
228 trace_file = get_tracing_file("trace_pipe");
229 if (!trace_file) {
230 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900231 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900232 }
233
234 trace_fd = open(trace_file, O_RDONLY);
235
236 put_tracing_file(trace_file);
237
238 if (trace_fd < 0) {
239 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900240 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900241 }
242
243 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
244 pollfd.fd = trace_fd;
245
246 if (write_tracing_file("tracing_on", "1") < 0) {
247 pr_err("can't enable tracing\n");
248 goto out_close_fd;
249 }
250
Namhyung Kim58335962017-02-24 10:12:51 +0900251 setup_pager();
252
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900253 perf_evlist__start_workload(ftrace->evlist);
254
255 while (!done) {
256 if (poll(&pollfd, 1, -1) < 0)
257 break;
258
259 if (pollfd.revents & POLLIN) {
260 int n = read(trace_fd, buf, sizeof(buf));
261 if (n < 0)
262 break;
263 if (fwrite(buf, n, 1, stdout) != 1)
264 break;
265 }
266 }
267
268 write_tracing_file("tracing_on", "0");
269
270 /* read remaining buffer contents */
271 while (true) {
272 int n = read(trace_fd, buf, sizeof(buf));
273 if (n <= 0)
274 break;
275 if (fwrite(buf, n, 1, stdout) != 1)
276 break;
277 }
278
279out_close_fd:
280 close(trace_fd);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900281out_reset:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900282 reset_tracing_files(ftrace);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900283out:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900284 return done ? 0 : -1;
285}
286
Taeung Songb05d1092017-01-31 20:38:29 +0900287static int perf_ftrace_config(const char *var, const char *value, void *cb)
288{
289 struct perf_ftrace *ftrace = cb;
290
291 if (prefixcmp(var, "ftrace."))
292 return 0;
293
294 if (strcmp(var, "ftrace.tracer"))
295 return -1;
296
297 if (!strcmp(value, "function_graph") ||
298 !strcmp(value, "function")) {
299 ftrace->tracer = value;
300 return 0;
301 }
302
303 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
304 return -1;
305}
306
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300307int cmd_ftrace(int argc, const char **argv)
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900308{
309 int ret;
310 struct perf_ftrace ftrace = {
Taeung Songbf062bd2017-01-26 18:35:37 +0900311 .tracer = DEFAULT_TRACER,
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900312 .target = { .uid = UINT_MAX, },
313 };
314 const char * const ftrace_usage[] = {
Namhyung Kima9af6be2017-02-24 10:12:48 +0900315 "perf ftrace [<options>] [<command>]",
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900316 "perf ftrace [<options>] -- <command> [<options>]",
317 NULL
318 };
319 const struct option ftrace_options[] = {
320 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
Arnaldo Carvalho de Meloec347872017-01-18 21:49:14 -0300321 "tracer to use: function_graph(default) or function"),
Namhyung Kima9af6be2017-02-24 10:12:48 +0900322 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
323 "trace on existing process id"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900324 OPT_INCR('v', "verbose", &verbose,
325 "be more verbose"),
Namhyung Kimdc231032017-02-24 10:12:50 +0900326 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
327 "system-wide collection from all CPUs"),
328 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
329 "list of cpus to monitor"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900330 OPT_END()
331 };
332
Taeung Songb05d1092017-01-31 20:38:29 +0900333 ret = perf_config(perf_ftrace_config, &ftrace);
334 if (ret < 0)
335 return -1;
336
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900337 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
338 PARSE_OPT_STOP_AT_NON_OPTION);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900339 if (!argc && target__none(&ftrace.target))
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900340 usage_with_options(ftrace_usage, ftrace_options);
341
Namhyung Kima9af6be2017-02-24 10:12:48 +0900342 ret = target__validate(&ftrace.target);
343 if (ret) {
344 char errbuf[512];
345
346 target__strerror(&ftrace.target, ret, errbuf, 512);
347 pr_err("%s\n", errbuf);
348 return -EINVAL;
349 }
350
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900351 ftrace.evlist = perf_evlist__new();
352 if (ftrace.evlist == NULL)
353 return -ENOMEM;
354
355 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
356 if (ret < 0)
357 goto out_delete_evlist;
358
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900359 ret = __cmd_ftrace(&ftrace, argc, argv);
360
361out_delete_evlist:
362 perf_evlist__delete(ftrace.evlist);
363
364 return ret;
365}