blob: 67d14037c03e780cb87b076ac193f7c66474c0c7 [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>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090016
17#include "debug.h"
18#include <subcmd/parse-options.h>
19#include "evlist.h"
20#include "target.h"
Namhyung Kimdc231032017-02-24 10:12:50 +090021#include "cpumap.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090022#include "thread_map.h"
Taeung Songb05d1092017-01-31 20:38:29 +090023#include "util/config.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090024
25
26#define DEFAULT_TRACER "function_graph"
27
28struct perf_ftrace {
29 struct perf_evlist *evlist;
30 struct target target;
31 const char *tracer;
32};
33
34static bool done;
35
36static void sig_handler(int sig __maybe_unused)
37{
38 done = true;
39}
40
41/*
42 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
43 * we asked by setting its exec_error to the function below,
44 * ftrace__workload_exec_failed_signal.
45 *
46 * XXX We need to handle this more appropriately, emitting an error, etc.
47 */
48static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
49 siginfo_t *info __maybe_unused,
50 void *ucontext __maybe_unused)
51{
52 /* workload_exec_errno = info->si_value.sival_int; */
53 done = true;
54}
55
Namhyung Kima9af6be2017-02-24 10:12:48 +090056static int __write_tracing_file(const char *name, const char *val, bool append)
Namhyung Kimd01f4e82013-03-07 21:45:20 +090057{
58 char *file;
59 int fd, ret = -1;
60 ssize_t size = strlen(val);
Namhyung Kima9af6be2017-02-24 10:12:48 +090061 int flags = O_WRONLY;
Namhyung Kimd01f4e82013-03-07 21:45:20 +090062
63 file = get_tracing_file(name);
64 if (!file) {
65 pr_debug("cannot get tracing file: %s\n", name);
66 return -1;
67 }
68
Namhyung Kima9af6be2017-02-24 10:12:48 +090069 if (append)
70 flags |= O_APPEND;
71 else
72 flags |= O_TRUNC;
73
74 fd = open(file, flags);
Namhyung Kimd01f4e82013-03-07 21:45:20 +090075 if (fd < 0) {
76 pr_debug("cannot open tracing file: %s\n", name);
77 goto out;
78 }
79
80 if (write(fd, val, size) == size)
81 ret = 0;
82 else
83 pr_debug("write '%s' to tracing/%s failed\n", val, name);
84
85 close(fd);
86out:
87 put_tracing_file(file);
88 return ret;
89}
90
Namhyung Kima9af6be2017-02-24 10:12:48 +090091static int write_tracing_file(const char *name, const char *val)
92{
93 return __write_tracing_file(name, val, false);
94}
95
96static int append_tracing_file(const char *name, const char *val)
97{
98 return __write_tracing_file(name, val, true);
99}
100
Namhyung Kimdc231032017-02-24 10:12:50 +0900101static int reset_tracing_cpu(void);
102
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900103static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
104{
105 if (write_tracing_file("tracing_on", "0") < 0)
106 return -1;
107
108 if (write_tracing_file("current_tracer", "nop") < 0)
109 return -1;
110
111 if (write_tracing_file("set_ftrace_pid", " ") < 0)
112 return -1;
113
Namhyung Kimdc231032017-02-24 10:12:50 +0900114 if (reset_tracing_cpu() < 0)
115 return -1;
116
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900117 return 0;
118}
119
Namhyung Kima9af6be2017-02-24 10:12:48 +0900120static int set_tracing_pid(struct perf_ftrace *ftrace)
121{
122 int i;
123 char buf[16];
124
125 if (target__has_cpu(&ftrace->target))
126 return 0;
127
128 for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
129 scnprintf(buf, sizeof(buf), "%d",
130 ftrace->evlist->threads->map[i]);
131 if (append_tracing_file("set_ftrace_pid", buf) < 0)
132 return -1;
133 }
134 return 0;
135}
136
Namhyung Kimdc231032017-02-24 10:12:50 +0900137static int set_tracing_cpumask(struct cpu_map *cpumap)
138{
139 char *cpumask;
140 size_t mask_size;
141 int ret;
142 int last_cpu;
143
144 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
145 mask_size = (last_cpu + 3) / 4 + 1;
146 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
147
148 cpumask = malloc(mask_size);
149 if (cpumask == NULL) {
150 pr_debug("failed to allocate cpu mask\n");
151 return -1;
152 }
153
154 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
155
156 ret = write_tracing_file("tracing_cpumask", cpumask);
157
158 free(cpumask);
159 return ret;
160}
161
162static int set_tracing_cpu(struct perf_ftrace *ftrace)
163{
164 struct cpu_map *cpumap = ftrace->evlist->cpus;
165
166 if (!target__has_cpu(&ftrace->target))
167 return 0;
168
169 return set_tracing_cpumask(cpumap);
170}
171
172static int reset_tracing_cpu(void)
173{
174 struct cpu_map *cpumap = cpu_map__new(NULL);
175 int ret;
176
177 ret = set_tracing_cpumask(cpumap);
178 cpu_map__put(cpumap);
179 return ret;
180}
181
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900182static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
183{
184 char *trace_file;
185 int trace_fd;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900186 char buf[4096];
187 struct pollfd pollfd = {
188 .events = POLLIN,
189 };
190
191 if (geteuid() != 0) {
192 pr_err("ftrace only works for root!\n");
193 return -1;
194 }
195
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900196 signal(SIGINT, sig_handler);
197 signal(SIGUSR1, sig_handler);
198 signal(SIGCHLD, sig_handler);
Namhyung Kim58335962017-02-24 10:12:51 +0900199 signal(SIGPIPE, sig_handler);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900200
Namhyung Kima9af6be2017-02-24 10:12:48 +0900201 if (reset_tracing_files(ftrace) < 0)
202 goto out;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900203
204 /* reset ftrace buffer */
205 if (write_tracing_file("trace", "0") < 0)
206 goto out;
207
Namhyung Kima9af6be2017-02-24 10:12:48 +0900208 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
209 &ftrace->target, argv, false,
210 ftrace__workload_exec_failed_signal) < 0) {
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900211 goto out;
Namhyung Kima9af6be2017-02-24 10:12:48 +0900212 }
213
214 if (set_tracing_pid(ftrace) < 0) {
215 pr_err("failed to set ftrace pid\n");
216 goto out_reset;
217 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900218
Namhyung Kimdc231032017-02-24 10:12:50 +0900219 if (set_tracing_cpu(ftrace) < 0) {
220 pr_err("failed to set tracing cpumask\n");
221 goto out_reset;
222 }
223
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900224 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
225 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900226 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900227 }
228
229 trace_file = get_tracing_file("trace_pipe");
230 if (!trace_file) {
231 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900232 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900233 }
234
235 trace_fd = open(trace_file, O_RDONLY);
236
237 put_tracing_file(trace_file);
238
239 if (trace_fd < 0) {
240 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900241 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900242 }
243
244 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
245 pollfd.fd = trace_fd;
246
247 if (write_tracing_file("tracing_on", "1") < 0) {
248 pr_err("can't enable tracing\n");
249 goto out_close_fd;
250 }
251
Namhyung Kim58335962017-02-24 10:12:51 +0900252 setup_pager();
253
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900254 perf_evlist__start_workload(ftrace->evlist);
255
256 while (!done) {
257 if (poll(&pollfd, 1, -1) < 0)
258 break;
259
260 if (pollfd.revents & POLLIN) {
261 int n = read(trace_fd, buf, sizeof(buf));
262 if (n < 0)
263 break;
264 if (fwrite(buf, n, 1, stdout) != 1)
265 break;
266 }
267 }
268
269 write_tracing_file("tracing_on", "0");
270
271 /* read remaining buffer contents */
272 while (true) {
273 int n = read(trace_fd, buf, sizeof(buf));
274 if (n <= 0)
275 break;
276 if (fwrite(buf, n, 1, stdout) != 1)
277 break;
278 }
279
280out_close_fd:
281 close(trace_fd);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900282out_reset:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900283 reset_tracing_files(ftrace);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900284out:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900285 return done ? 0 : -1;
286}
287
Taeung Songb05d1092017-01-31 20:38:29 +0900288static int perf_ftrace_config(const char *var, const char *value, void *cb)
289{
290 struct perf_ftrace *ftrace = cb;
291
292 if (prefixcmp(var, "ftrace."))
293 return 0;
294
295 if (strcmp(var, "ftrace.tracer"))
296 return -1;
297
298 if (!strcmp(value, "function_graph") ||
299 !strcmp(value, "function")) {
300 ftrace->tracer = value;
301 return 0;
302 }
303
304 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
305 return -1;
306}
307
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300308int cmd_ftrace(int argc, const char **argv)
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900309{
310 int ret;
311 struct perf_ftrace ftrace = {
Taeung Songbf062bd2017-01-26 18:35:37 +0900312 .tracer = DEFAULT_TRACER,
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900313 .target = { .uid = UINT_MAX, },
314 };
315 const char * const ftrace_usage[] = {
Namhyung Kima9af6be2017-02-24 10:12:48 +0900316 "perf ftrace [<options>] [<command>]",
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900317 "perf ftrace [<options>] -- <command> [<options>]",
318 NULL
319 };
320 const struct option ftrace_options[] = {
321 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
Arnaldo Carvalho de Meloec347872017-01-18 21:49:14 -0300322 "tracer to use: function_graph(default) or function"),
Namhyung Kima9af6be2017-02-24 10:12:48 +0900323 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
324 "trace on existing process id"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900325 OPT_INCR('v', "verbose", &verbose,
326 "be more verbose"),
Namhyung Kimdc231032017-02-24 10:12:50 +0900327 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
328 "system-wide collection from all CPUs"),
329 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
330 "list of cpus to monitor"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900331 OPT_END()
332 };
333
Taeung Songb05d1092017-01-31 20:38:29 +0900334 ret = perf_config(perf_ftrace_config, &ftrace);
335 if (ret < 0)
336 return -1;
337
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900338 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
339 PARSE_OPT_STOP_AT_NON_OPTION);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900340 if (!argc && target__none(&ftrace.target))
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900341 usage_with_options(ftrace_usage, ftrace_options);
342
Namhyung Kima9af6be2017-02-24 10:12:48 +0900343 ret = target__validate(&ftrace.target);
344 if (ret) {
345 char errbuf[512];
346
347 target__strerror(&ftrace.target, ret, errbuf, 512);
348 pr_err("%s\n", errbuf);
349 return -EINVAL;
350 }
351
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900352 ftrace.evlist = perf_evlist__new();
353 if (ftrace.evlist == NULL)
354 return -ENOMEM;
355
356 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
357 if (ret < 0)
358 goto out_delete_evlist;
359
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900360 ret = __cmd_ftrace(&ftrace, argc, argv);
361
362out_delete_evlist:
363 perf_evlist__delete(ftrace.evlist);
364
365 return ret;
366}