4 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 * Released under the GPL v2.
19 #include <subcmd/parse-options.h>
20 #include <api/fs/tracing_path.h>
24 #include "thread_map.h"
25 #include "util/config.h"
28 #define DEFAULT_TRACER "function_graph"
31 struct perf_evlist
*evlist
;
34 struct list_head filters
;
35 struct list_head notrace
;
36 struct list_head graph_funcs
;
37 struct list_head nograph_funcs
;
42 struct list_head list
;
48 static void sig_handler(int sig __maybe_unused
)
54 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
55 * we asked by setting its exec_error to the function below,
56 * ftrace__workload_exec_failed_signal.
58 * XXX We need to handle this more appropriately, emitting an error, etc.
60 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused
,
61 siginfo_t
*info __maybe_unused
,
62 void *ucontext __maybe_unused
)
64 /* workload_exec_errno = info->si_value.sival_int; */
68 static int __write_tracing_file(const char *name
, const char *val
, bool append
)
72 ssize_t size
= strlen(val
);
76 file
= get_tracing_file(name
);
78 pr_debug("cannot get tracing file: %s\n", name
);
87 fd
= open(file
, flags
);
89 pr_debug("cannot open tracing file: %s: %s\n",
90 name
, str_error_r(errno
, errbuf
, sizeof(errbuf
)));
94 if (write(fd
, val
, size
) == size
)
97 pr_debug("write '%s' to tracing/%s failed: %s\n",
98 val
, name
, str_error_r(errno
, errbuf
, sizeof(errbuf
)));
102 put_tracing_file(file
);
106 static int write_tracing_file(const char *name
, const char *val
)
108 return __write_tracing_file(name
, val
, false);
111 static int append_tracing_file(const char *name
, const char *val
)
113 return __write_tracing_file(name
, val
, true);
116 static int reset_tracing_cpu(void);
117 static void reset_tracing_filters(void);
119 static int reset_tracing_files(struct perf_ftrace
*ftrace __maybe_unused
)
121 if (write_tracing_file("tracing_on", "0") < 0)
124 if (write_tracing_file("current_tracer", "nop") < 0)
127 if (write_tracing_file("set_ftrace_pid", " ") < 0)
130 if (reset_tracing_cpu() < 0)
133 if (write_tracing_file("max_graph_depth", "0") < 0)
136 reset_tracing_filters();
140 static int set_tracing_pid(struct perf_ftrace
*ftrace
)
145 if (target__has_cpu(&ftrace
->target
))
148 for (i
= 0; i
< thread_map__nr(ftrace
->evlist
->threads
); i
++) {
149 scnprintf(buf
, sizeof(buf
), "%d",
150 ftrace
->evlist
->threads
->map
[i
]);
151 if (append_tracing_file("set_ftrace_pid", buf
) < 0)
157 static int set_tracing_cpumask(struct cpu_map
*cpumap
)
164 last_cpu
= cpu_map__cpu(cpumap
, cpumap
->nr
- 1);
165 mask_size
= (last_cpu
+ 3) / 4 + 1;
166 mask_size
+= last_cpu
/ 32; /* ',' is needed for every 32th cpus */
168 cpumask
= malloc(mask_size
);
169 if (cpumask
== NULL
) {
170 pr_debug("failed to allocate cpu mask\n");
174 cpu_map__snprint_mask(cpumap
, cpumask
, mask_size
);
176 ret
= write_tracing_file("tracing_cpumask", cpumask
);
182 static int set_tracing_cpu(struct perf_ftrace
*ftrace
)
184 struct cpu_map
*cpumap
= ftrace
->evlist
->cpus
;
186 if (!target__has_cpu(&ftrace
->target
))
189 return set_tracing_cpumask(cpumap
);
192 static int reset_tracing_cpu(void)
194 struct cpu_map
*cpumap
= cpu_map__new(NULL
);
197 ret
= set_tracing_cpumask(cpumap
);
198 cpu_map__put(cpumap
);
202 static int __set_tracing_filter(const char *filter_file
, struct list_head
*funcs
)
204 struct filter_entry
*pos
;
206 list_for_each_entry(pos
, funcs
, list
) {
207 if (append_tracing_file(filter_file
, pos
->name
) < 0)
214 static int set_tracing_filters(struct perf_ftrace
*ftrace
)
218 ret
= __set_tracing_filter("set_ftrace_filter", &ftrace
->filters
);
222 ret
= __set_tracing_filter("set_ftrace_notrace", &ftrace
->notrace
);
226 ret
= __set_tracing_filter("set_graph_function", &ftrace
->graph_funcs
);
230 /* old kernels do not have this filter */
231 __set_tracing_filter("set_graph_notrace", &ftrace
->nograph_funcs
);
236 static void reset_tracing_filters(void)
238 write_tracing_file("set_ftrace_filter", " ");
239 write_tracing_file("set_ftrace_notrace", " ");
240 write_tracing_file("set_graph_function", " ");
241 write_tracing_file("set_graph_notrace", " ");
244 static int set_tracing_depth(struct perf_ftrace
*ftrace
)
248 if (ftrace
->graph_depth
== 0)
251 if (ftrace
->graph_depth
< 0) {
252 pr_err("invalid graph depth: %d\n", ftrace
->graph_depth
);
256 snprintf(buf
, sizeof(buf
), "%d", ftrace
->graph_depth
);
258 if (write_tracing_file("max_graph_depth", buf
) < 0)
264 static int __cmd_ftrace(struct perf_ftrace
*ftrace
, int argc
, const char **argv
)
269 struct pollfd pollfd
= {
273 if (geteuid() != 0) {
274 pr_err("ftrace only works for root!\n");
278 signal(SIGINT
, sig_handler
);
279 signal(SIGUSR1
, sig_handler
);
280 signal(SIGCHLD
, sig_handler
);
281 signal(SIGPIPE
, sig_handler
);
283 if (reset_tracing_files(ftrace
) < 0)
286 /* reset ftrace buffer */
287 if (write_tracing_file("trace", "0") < 0)
290 if (argc
&& perf_evlist__prepare_workload(ftrace
->evlist
,
291 &ftrace
->target
, argv
, false,
292 ftrace__workload_exec_failed_signal
) < 0) {
296 if (set_tracing_pid(ftrace
) < 0) {
297 pr_err("failed to set ftrace pid\n");
301 if (set_tracing_cpu(ftrace
) < 0) {
302 pr_err("failed to set tracing cpumask\n");
306 if (set_tracing_filters(ftrace
) < 0) {
307 pr_err("failed to set tracing filters\n");
311 if (set_tracing_depth(ftrace
) < 0) {
312 pr_err("failed to set graph depth\n");
316 if (write_tracing_file("current_tracer", ftrace
->tracer
) < 0) {
317 pr_err("failed to set current_tracer to %s\n", ftrace
->tracer
);
323 trace_file
= get_tracing_file("trace_pipe");
325 pr_err("failed to open trace_pipe\n");
329 trace_fd
= open(trace_file
, O_RDONLY
);
331 put_tracing_file(trace_file
);
334 pr_err("failed to open trace_pipe\n");
338 fcntl(trace_fd
, F_SETFL
, O_NONBLOCK
);
339 pollfd
.fd
= trace_fd
;
341 if (write_tracing_file("tracing_on", "1") < 0) {
342 pr_err("can't enable tracing\n");
346 perf_evlist__start_workload(ftrace
->evlist
);
349 if (poll(&pollfd
, 1, -1) < 0)
352 if (pollfd
.revents
& POLLIN
) {
353 int n
= read(trace_fd
, buf
, sizeof(buf
));
356 if (fwrite(buf
, n
, 1, stdout
) != 1)
361 write_tracing_file("tracing_on", "0");
363 /* read remaining buffer contents */
365 int n
= read(trace_fd
, buf
, sizeof(buf
));
368 if (fwrite(buf
, n
, 1, stdout
) != 1)
375 reset_tracing_files(ftrace
);
377 return done
? 0 : -1;
380 static int perf_ftrace_config(const char *var
, const char *value
, void *cb
)
382 struct perf_ftrace
*ftrace
= cb
;
384 if (!strstarts(var
, "ftrace."))
387 if (strcmp(var
, "ftrace.tracer"))
390 if (!strcmp(value
, "function_graph") ||
391 !strcmp(value
, "function")) {
392 ftrace
->tracer
= value
;
396 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
400 static int parse_filter_func(const struct option
*opt
, const char *str
,
401 int unset __maybe_unused
)
403 struct list_head
*head
= opt
->value
;
404 struct filter_entry
*entry
;
406 entry
= malloc(sizeof(*entry
) + strlen(str
) + 1);
410 strcpy(entry
->name
, str
);
411 list_add_tail(&entry
->list
, head
);
416 static void delete_filter_func(struct list_head
*head
)
418 struct filter_entry
*pos
, *tmp
;
420 list_for_each_entry_safe(pos
, tmp
, head
, list
) {
421 list_del(&pos
->list
);
426 int cmd_ftrace(int argc
, const char **argv
)
429 struct perf_ftrace ftrace
= {
430 .tracer
= DEFAULT_TRACER
,
431 .target
= { .uid
= UINT_MAX
, },
433 const char * const ftrace_usage
[] = {
434 "perf ftrace [<options>] [<command>]",
435 "perf ftrace [<options>] -- <command> [<options>]",
438 const struct option ftrace_options
[] = {
439 OPT_STRING('t', "tracer", &ftrace
.tracer
, "tracer",
440 "tracer to use: function_graph(default) or function"),
441 OPT_STRING('p', "pid", &ftrace
.target
.pid
, "pid",
442 "trace on existing process id"),
443 OPT_INCR('v', "verbose", &verbose
,
445 OPT_BOOLEAN('a', "all-cpus", &ftrace
.target
.system_wide
,
446 "system-wide collection from all CPUs"),
447 OPT_STRING('C', "cpu", &ftrace
.target
.cpu_list
, "cpu",
448 "list of cpus to monitor"),
449 OPT_CALLBACK('T', "trace-funcs", &ftrace
.filters
, "func",
450 "trace given functions only", parse_filter_func
),
451 OPT_CALLBACK('N', "notrace-funcs", &ftrace
.notrace
, "func",
452 "do not trace given functions", parse_filter_func
),
453 OPT_CALLBACK('G', "graph-funcs", &ftrace
.graph_funcs
, "func",
454 "Set graph filter on given functions", parse_filter_func
),
455 OPT_CALLBACK('g', "nograph-funcs", &ftrace
.nograph_funcs
, "func",
456 "Set nograph filter on given functions", parse_filter_func
),
457 OPT_INTEGER('D', "graph-depth", &ftrace
.graph_depth
,
458 "Max depth for function graph tracer"),
462 INIT_LIST_HEAD(&ftrace
.filters
);
463 INIT_LIST_HEAD(&ftrace
.notrace
);
464 INIT_LIST_HEAD(&ftrace
.graph_funcs
);
465 INIT_LIST_HEAD(&ftrace
.nograph_funcs
);
467 ret
= perf_config(perf_ftrace_config
, &ftrace
);
471 argc
= parse_options(argc
, argv
, ftrace_options
, ftrace_usage
,
472 PARSE_OPT_STOP_AT_NON_OPTION
);
473 if (!argc
&& target__none(&ftrace
.target
))
474 usage_with_options(ftrace_usage
, ftrace_options
);
476 ret
= target__validate(&ftrace
.target
);
480 target__strerror(&ftrace
.target
, ret
, errbuf
, 512);
481 pr_err("%s\n", errbuf
);
482 goto out_delete_filters
;
485 ftrace
.evlist
= perf_evlist__new();
486 if (ftrace
.evlist
== NULL
) {
488 goto out_delete_filters
;
491 ret
= perf_evlist__create_maps(ftrace
.evlist
, &ftrace
.target
);
493 goto out_delete_evlist
;
495 ret
= __cmd_ftrace(&ftrace
, argc
, argv
);
498 perf_evlist__delete(ftrace
.evlist
);
501 delete_filter_func(&ftrace
.filters
);
502 delete_filter_func(&ftrace
.notrace
);
503 delete_filter_func(&ftrace
.graph_funcs
);
504 delete_filter_func(&ftrace
.nograph_funcs
);