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
);
77 file
= get_tracing_file(name
);
79 pr_debug("cannot get tracing file: %s\n", name
);
88 fd
= open(file
, flags
);
90 pr_debug("cannot open tracing file: %s: %s\n",
91 name
, str_error_r(errno
, errbuf
, sizeof(errbuf
)));
96 * Copy the original value and append a '\n'. Without this,
97 * the kernel can hide possible errors.
99 val_copy
= strdup(val
);
102 val_copy
[size
] = '\n';
104 if (write(fd
, val_copy
, size
+ 1) == size
+ 1)
107 pr_debug("write '%s' to tracing/%s failed: %s\n",
108 val
, name
, str_error_r(errno
, errbuf
, sizeof(errbuf
)));
114 put_tracing_file(file
);
118 static int write_tracing_file(const char *name
, const char *val
)
120 return __write_tracing_file(name
, val
, false);
123 static int append_tracing_file(const char *name
, const char *val
)
125 return __write_tracing_file(name
, val
, true);
128 static int reset_tracing_cpu(void);
129 static void reset_tracing_filters(void);
131 static int reset_tracing_files(struct perf_ftrace
*ftrace __maybe_unused
)
133 if (write_tracing_file("tracing_on", "0") < 0)
136 if (write_tracing_file("current_tracer", "nop") < 0)
139 if (write_tracing_file("set_ftrace_pid", " ") < 0)
142 if (reset_tracing_cpu() < 0)
145 if (write_tracing_file("max_graph_depth", "0") < 0)
148 reset_tracing_filters();
152 static int set_tracing_pid(struct perf_ftrace
*ftrace
)
157 if (target__has_cpu(&ftrace
->target
))
160 for (i
= 0; i
< thread_map__nr(ftrace
->evlist
->threads
); i
++) {
161 scnprintf(buf
, sizeof(buf
), "%d",
162 ftrace
->evlist
->threads
->map
[i
]);
163 if (append_tracing_file("set_ftrace_pid", buf
) < 0)
169 static int set_tracing_cpumask(struct cpu_map
*cpumap
)
176 last_cpu
= cpu_map__cpu(cpumap
, cpumap
->nr
- 1);
177 mask_size
= (last_cpu
+ 3) / 4 + 1;
178 mask_size
+= last_cpu
/ 32; /* ',' is needed for every 32th cpus */
180 cpumask
= malloc(mask_size
);
181 if (cpumask
== NULL
) {
182 pr_debug("failed to allocate cpu mask\n");
186 cpu_map__snprint_mask(cpumap
, cpumask
, mask_size
);
188 ret
= write_tracing_file("tracing_cpumask", cpumask
);
194 static int set_tracing_cpu(struct perf_ftrace
*ftrace
)
196 struct cpu_map
*cpumap
= ftrace
->evlist
->cpus
;
198 if (!target__has_cpu(&ftrace
->target
))
201 return set_tracing_cpumask(cpumap
);
204 static int reset_tracing_cpu(void)
206 struct cpu_map
*cpumap
= cpu_map__new(NULL
);
209 ret
= set_tracing_cpumask(cpumap
);
210 cpu_map__put(cpumap
);
214 static int __set_tracing_filter(const char *filter_file
, struct list_head
*funcs
)
216 struct filter_entry
*pos
;
218 list_for_each_entry(pos
, funcs
, list
) {
219 if (append_tracing_file(filter_file
, pos
->name
) < 0)
226 static int set_tracing_filters(struct perf_ftrace
*ftrace
)
230 ret
= __set_tracing_filter("set_ftrace_filter", &ftrace
->filters
);
234 ret
= __set_tracing_filter("set_ftrace_notrace", &ftrace
->notrace
);
238 ret
= __set_tracing_filter("set_graph_function", &ftrace
->graph_funcs
);
242 /* old kernels do not have this filter */
243 __set_tracing_filter("set_graph_notrace", &ftrace
->nograph_funcs
);
248 static void reset_tracing_filters(void)
250 write_tracing_file("set_ftrace_filter", " ");
251 write_tracing_file("set_ftrace_notrace", " ");
252 write_tracing_file("set_graph_function", " ");
253 write_tracing_file("set_graph_notrace", " ");
256 static int set_tracing_depth(struct perf_ftrace
*ftrace
)
260 if (ftrace
->graph_depth
== 0)
263 if (ftrace
->graph_depth
< 0) {
264 pr_err("invalid graph depth: %d\n", ftrace
->graph_depth
);
268 snprintf(buf
, sizeof(buf
), "%d", ftrace
->graph_depth
);
270 if (write_tracing_file("max_graph_depth", buf
) < 0)
276 static int __cmd_ftrace(struct perf_ftrace
*ftrace
, int argc
, const char **argv
)
281 struct pollfd pollfd
= {
285 if (geteuid() != 0) {
286 pr_err("ftrace only works for root!\n");
290 signal(SIGINT
, sig_handler
);
291 signal(SIGUSR1
, sig_handler
);
292 signal(SIGCHLD
, sig_handler
);
293 signal(SIGPIPE
, sig_handler
);
295 if (reset_tracing_files(ftrace
) < 0) {
296 pr_err("failed to reset ftrace\n");
300 /* reset ftrace buffer */
301 if (write_tracing_file("trace", "0") < 0)
304 if (argc
&& perf_evlist__prepare_workload(ftrace
->evlist
,
305 &ftrace
->target
, argv
, false,
306 ftrace__workload_exec_failed_signal
) < 0) {
310 if (set_tracing_pid(ftrace
) < 0) {
311 pr_err("failed to set ftrace pid\n");
315 if (set_tracing_cpu(ftrace
) < 0) {
316 pr_err("failed to set tracing cpumask\n");
320 if (set_tracing_filters(ftrace
) < 0) {
321 pr_err("failed to set tracing filters\n");
325 if (set_tracing_depth(ftrace
) < 0) {
326 pr_err("failed to set graph depth\n");
330 if (write_tracing_file("current_tracer", ftrace
->tracer
) < 0) {
331 pr_err("failed to set current_tracer to %s\n", ftrace
->tracer
);
337 trace_file
= get_tracing_file("trace_pipe");
339 pr_err("failed to open trace_pipe\n");
343 trace_fd
= open(trace_file
, O_RDONLY
);
345 put_tracing_file(trace_file
);
348 pr_err("failed to open trace_pipe\n");
352 fcntl(trace_fd
, F_SETFL
, O_NONBLOCK
);
353 pollfd
.fd
= trace_fd
;
355 if (write_tracing_file("tracing_on", "1") < 0) {
356 pr_err("can't enable tracing\n");
360 perf_evlist__start_workload(ftrace
->evlist
);
363 if (poll(&pollfd
, 1, -1) < 0)
366 if (pollfd
.revents
& POLLIN
) {
367 int n
= read(trace_fd
, buf
, sizeof(buf
));
370 if (fwrite(buf
, n
, 1, stdout
) != 1)
375 write_tracing_file("tracing_on", "0");
377 /* read remaining buffer contents */
379 int n
= read(trace_fd
, buf
, sizeof(buf
));
382 if (fwrite(buf
, n
, 1, stdout
) != 1)
389 reset_tracing_files(ftrace
);
391 return done
? 0 : -1;
394 static int perf_ftrace_config(const char *var
, const char *value
, void *cb
)
396 struct perf_ftrace
*ftrace
= cb
;
398 if (!strstarts(var
, "ftrace."))
401 if (strcmp(var
, "ftrace.tracer"))
404 if (!strcmp(value
, "function_graph") ||
405 !strcmp(value
, "function")) {
406 ftrace
->tracer
= value
;
410 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
414 static int parse_filter_func(const struct option
*opt
, const char *str
,
415 int unset __maybe_unused
)
417 struct list_head
*head
= opt
->value
;
418 struct filter_entry
*entry
;
420 entry
= malloc(sizeof(*entry
) + strlen(str
) + 1);
424 strcpy(entry
->name
, str
);
425 list_add_tail(&entry
->list
, head
);
430 static void delete_filter_func(struct list_head
*head
)
432 struct filter_entry
*pos
, *tmp
;
434 list_for_each_entry_safe(pos
, tmp
, head
, list
) {
435 list_del(&pos
->list
);
440 int cmd_ftrace(int argc
, const char **argv
)
443 struct perf_ftrace ftrace
= {
444 .tracer
= DEFAULT_TRACER
,
445 .target
= { .uid
= UINT_MAX
, },
447 const char * const ftrace_usage
[] = {
448 "perf ftrace [<options>] [<command>]",
449 "perf ftrace [<options>] -- <command> [<options>]",
452 const struct option ftrace_options
[] = {
453 OPT_STRING('t', "tracer", &ftrace
.tracer
, "tracer",
454 "tracer to use: function_graph(default) or function"),
455 OPT_STRING('p', "pid", &ftrace
.target
.pid
, "pid",
456 "trace on existing process id"),
457 OPT_INCR('v', "verbose", &verbose
,
459 OPT_BOOLEAN('a', "all-cpus", &ftrace
.target
.system_wide
,
460 "system-wide collection from all CPUs"),
461 OPT_STRING('C', "cpu", &ftrace
.target
.cpu_list
, "cpu",
462 "list of cpus to monitor"),
463 OPT_CALLBACK('T', "trace-funcs", &ftrace
.filters
, "func",
464 "trace given functions only", parse_filter_func
),
465 OPT_CALLBACK('N', "notrace-funcs", &ftrace
.notrace
, "func",
466 "do not trace given functions", parse_filter_func
),
467 OPT_CALLBACK('G', "graph-funcs", &ftrace
.graph_funcs
, "func",
468 "Set graph filter on given functions", parse_filter_func
),
469 OPT_CALLBACK('g', "nograph-funcs", &ftrace
.nograph_funcs
, "func",
470 "Set nograph filter on given functions", parse_filter_func
),
471 OPT_INTEGER('D', "graph-depth", &ftrace
.graph_depth
,
472 "Max depth for function graph tracer"),
476 INIT_LIST_HEAD(&ftrace
.filters
);
477 INIT_LIST_HEAD(&ftrace
.notrace
);
478 INIT_LIST_HEAD(&ftrace
.graph_funcs
);
479 INIT_LIST_HEAD(&ftrace
.nograph_funcs
);
481 ret
= perf_config(perf_ftrace_config
, &ftrace
);
485 argc
= parse_options(argc
, argv
, ftrace_options
, ftrace_usage
,
486 PARSE_OPT_STOP_AT_NON_OPTION
);
487 if (!argc
&& target__none(&ftrace
.target
))
488 usage_with_options(ftrace_usage
, ftrace_options
);
490 ret
= target__validate(&ftrace
.target
);
494 target__strerror(&ftrace
.target
, ret
, errbuf
, 512);
495 pr_err("%s\n", errbuf
);
496 goto out_delete_filters
;
499 ftrace
.evlist
= perf_evlist__new();
500 if (ftrace
.evlist
== NULL
) {
502 goto out_delete_filters
;
505 ret
= perf_evlist__create_maps(ftrace
.evlist
, &ftrace
.target
);
507 goto out_delete_evlist
;
509 ret
= __cmd_ftrace(&ftrace
, argc
, argv
);
512 perf_evlist__delete(ftrace
.evlist
);
515 delete_filter_func(&ftrace
.filters
);
516 delete_filter_func(&ftrace
.notrace
);
517 delete_filter_func(&ftrace
.graph_funcs
);
518 delete_filter_func(&ftrace
.nograph_funcs
);