]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/builtin-ftrace.c
perf ftrace: Add support for -a and -C option
[mirror_ubuntu-artful-kernel.git] / tools / perf / builtin-ftrace.c
CommitLineData
d01f4e8d
NK
1/*
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>
a9af6be5 14#include <fcntl.h>
d01f4e8d
NK
15
16#include "debug.h"
17#include <subcmd/parse-options.h>
18#include "evlist.h"
19#include "target.h"
dc231032 20#include "cpumap.h"
d01f4e8d 21#include "thread_map.h"
b05d1093 22#include "util/config.h"
d01f4e8d
NK
23
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
a9af6be5 55static int __write_tracing_file(const char *name, const char *val, bool append)
d01f4e8d
NK
56{
57 char *file;
58 int fd, ret = -1;
59 ssize_t size = strlen(val);
a9af6be5 60 int flags = O_WRONLY;
d01f4e8d
NK
61
62 file = get_tracing_file(name);
63 if (!file) {
64 pr_debug("cannot get tracing file: %s\n", name);
65 return -1;
66 }
67
a9af6be5
NK
68 if (append)
69 flags |= O_APPEND;
70 else
71 flags |= O_TRUNC;
72
73 fd = open(file, flags);
d01f4e8d
NK
74 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
a9af6be5
NK
90static 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
dc231032
NK
100static int reset_tracing_cpu(void);
101
d01f4e8d
NK
102static 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
dc231032
NK
113 if (reset_tracing_cpu() < 0)
114 return -1;
115
d01f4e8d
NK
116 return 0;
117}
118
a9af6be5
NK
119static 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
dc231032
NK
136static 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
d01f4e8d
NK
181static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
182{
183 char *trace_file;
184 int trace_fd;
d01f4e8d
NK
185 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
d01f4e8d
NK
195 signal(SIGINT, sig_handler);
196 signal(SIGUSR1, sig_handler);
197 signal(SIGCHLD, sig_handler);
198
a9af6be5
NK
199 if (reset_tracing_files(ftrace) < 0)
200 goto out;
d01f4e8d
NK
201
202 /* reset ftrace buffer */
203 if (write_tracing_file("trace", "0") < 0)
204 goto out;
205
a9af6be5
NK
206 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
207 &ftrace->target, argv, false,
208 ftrace__workload_exec_failed_signal) < 0) {
d01f4e8d
NK
209 goto out;
210 }
211
a9af6be5
NK
212 if (set_tracing_pid(ftrace) < 0) {
213 pr_err("failed to set ftrace pid\n");
214 goto out_reset;
d01f4e8d
NK
215 }
216
dc231032
NK
217 if (set_tracing_cpu(ftrace) < 0) {
218 pr_err("failed to set tracing cpumask\n");
219 goto out_reset;
220 }
221
a9af6be5
NK
222 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
223 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
224 goto out_reset;
d01f4e8d
NK
225 }
226
227 trace_file = get_tracing_file("trace_pipe");
228 if (!trace_file) {
229 pr_err("failed to open trace_pipe\n");
a9af6be5 230 goto out_reset;
d01f4e8d
NK
231 }
232
233 trace_fd = open(trace_file, O_RDONLY);
234
235 put_tracing_file(trace_file);
236
237 if (trace_fd < 0) {
238 pr_err("failed to open trace_pipe\n");
a9af6be5 239 goto out_reset;
d01f4e8d
NK
240 }
241
242 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
243 pollfd.fd = trace_fd;
244
245 if (write_tracing_file("tracing_on", "1") < 0) {
246 pr_err("can't enable tracing\n");
247 goto out_close_fd;
248 }
249
250 perf_evlist__start_workload(ftrace->evlist);
251
252 while (!done) {
253 if (poll(&pollfd, 1, -1) < 0)
254 break;
255
256 if (pollfd.revents & POLLIN) {
257 int n = read(trace_fd, buf, sizeof(buf));
258 if (n < 0)
259 break;
260 if (fwrite(buf, n, 1, stdout) != 1)
261 break;
262 }
263 }
264
265 write_tracing_file("tracing_on", "0");
266
267 /* read remaining buffer contents */
268 while (true) {
269 int n = read(trace_fd, buf, sizeof(buf));
270 if (n <= 0)
271 break;
272 if (fwrite(buf, n, 1, stdout) != 1)
273 break;
274 }
275
276out_close_fd:
277 close(trace_fd);
a9af6be5 278out_reset:
d01f4e8d 279 reset_tracing_files(ftrace);
a9af6be5 280out:
d01f4e8d
NK
281 return done ? 0 : -1;
282}
283
b05d1093
TS
284static int perf_ftrace_config(const char *var, const char *value, void *cb)
285{
286 struct perf_ftrace *ftrace = cb;
287
288 if (prefixcmp(var, "ftrace."))
289 return 0;
290
291 if (strcmp(var, "ftrace.tracer"))
292 return -1;
293
294 if (!strcmp(value, "function_graph") ||
295 !strcmp(value, "function")) {
296 ftrace->tracer = value;
297 return 0;
298 }
299
300 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
301 return -1;
302}
303
d01f4e8d
NK
304int cmd_ftrace(int argc, const char **argv, const char *prefix __maybe_unused)
305{
306 int ret;
307 struct perf_ftrace ftrace = {
bf062bd2 308 .tracer = DEFAULT_TRACER,
d01f4e8d
NK
309 .target = { .uid = UINT_MAX, },
310 };
311 const char * const ftrace_usage[] = {
a9af6be5 312 "perf ftrace [<options>] [<command>]",
d01f4e8d
NK
313 "perf ftrace [<options>] -- <command> [<options>]",
314 NULL
315 };
316 const struct option ftrace_options[] = {
317 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
ec347870 318 "tracer to use: function_graph(default) or function"),
a9af6be5
NK
319 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
320 "trace on existing process id"),
d01f4e8d
NK
321 OPT_INCR('v', "verbose", &verbose,
322 "be more verbose"),
dc231032
NK
323 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
324 "system-wide collection from all CPUs"),
325 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
326 "list of cpus to monitor"),
d01f4e8d
NK
327 OPT_END()
328 };
329
b05d1093
TS
330 ret = perf_config(perf_ftrace_config, &ftrace);
331 if (ret < 0)
332 return -1;
333
d01f4e8d
NK
334 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
335 PARSE_OPT_STOP_AT_NON_OPTION);
a9af6be5 336 if (!argc && target__none(&ftrace.target))
d01f4e8d
NK
337 usage_with_options(ftrace_usage, ftrace_options);
338
a9af6be5
NK
339 ret = target__validate(&ftrace.target);
340 if (ret) {
341 char errbuf[512];
342
343 target__strerror(&ftrace.target, ret, errbuf, 512);
344 pr_err("%s\n", errbuf);
345 return -EINVAL;
346 }
347
d01f4e8d
NK
348 ftrace.evlist = perf_evlist__new();
349 if (ftrace.evlist == NULL)
350 return -ENOMEM;
351
352 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
353 if (ret < 0)
354 goto out_delete_evlist;
355
d01f4e8d
NK
356 ret = __cmd_ftrace(&ftrace, argc, argv);
357
358out_delete_evlist:
359 perf_evlist__delete(ftrace.evlist);
360
361 return ret;
362}