1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
16 #include <linux/capability.h>
17 #include <linux/string.h>
20 #include <subcmd/pager.h>
21 #include <subcmd/parse-options.h>
22 #include <api/fs/tracing_path.h>
26 #include "thread_map.h"
28 #include "util/config.h"
29 #include "util/units.h"
30 #include "util/parse-sublevel-options.h"
32 #define DEFAULT_TRACER "function_graph"
35 struct evlist *evlist;
38 bool list_avail_functions;
39 struct list_head filters;
40 struct list_head notrace;
41 struct list_head graph_funcs;
42 struct list_head nograph_funcs;
44 unsigned long percpu_buffer_size;
48 int graph_nosleep_time;
55 struct list_head list;
59 static volatile int workload_exec_errno;
62 static void sig_handler(int sig __maybe_unused)
68 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
69 * we asked by setting its exec_error to the function below,
70 * ftrace__workload_exec_failed_signal.
72 * XXX We need to handle this more appropriately, emitting an error, etc.
74 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
75 siginfo_t *info __maybe_unused,
76 void *ucontext __maybe_unused)
78 workload_exec_errno = info->si_value.sival_int;
82 static int __write_tracing_file(const char *name, const char *val, bool append)
86 ssize_t size = strlen(val);
91 file = get_tracing_file(name);
93 pr_debug("cannot get tracing file: %s\n", name);
102 fd = open(file, flags);
104 pr_debug("cannot open tracing file: %s: %s\n",
105 name, str_error_r(errno, errbuf, sizeof(errbuf)));
110 * Copy the original value and append a '\n'. Without this,
111 * the kernel can hide possible errors.
113 val_copy = strdup(val);
116 val_copy[size] = '\n';
118 if (write(fd, val_copy, size + 1) == size + 1)
121 pr_debug("write '%s' to tracing/%s failed: %s\n",
122 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
128 put_tracing_file(file);
132 static int write_tracing_file(const char *name, const char *val)
134 return __write_tracing_file(name, val, false);
137 static int append_tracing_file(const char *name, const char *val)
139 return __write_tracing_file(name, val, true);
142 static int read_tracing_file_to_stdout(const char *name)
149 file = get_tracing_file(name);
151 pr_debug("cannot get tracing file: %s\n", name);
155 fd = open(file, O_RDONLY);
157 pr_debug("cannot open tracing file: %s: %s\n",
158 name, str_error_r(errno, buf, sizeof(buf)));
162 /* read contents to stdout */
164 int n = read(fd, buf, sizeof(buf));
170 if (fwrite(buf, n, 1, stdout) != 1)
178 put_tracing_file(file);
182 static int write_tracing_file_int(const char *name, int value)
186 snprintf(buf, sizeof(buf), "%d", value);
187 if (write_tracing_file(name, buf) < 0)
193 static int write_tracing_option_file(const char *name, const char *val)
198 if (asprintf(&file, "options/%s", name) < 0)
201 ret = __write_tracing_file(file, val, false);
206 static int reset_tracing_cpu(void);
207 static void reset_tracing_filters(void);
209 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
211 write_tracing_option_file("function-fork", "0");
212 write_tracing_option_file("func_stack_trace", "0");
213 write_tracing_option_file("sleep-time", "1");
214 write_tracing_option_file("funcgraph-irqs", "1");
215 write_tracing_option_file("funcgraph-proc", "0");
216 write_tracing_option_file("funcgraph-abstime", "0");
217 write_tracing_option_file("latency-format", "0");
218 write_tracing_option_file("irq-info", "0");
221 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
223 if (write_tracing_file("tracing_on", "0") < 0)
226 if (write_tracing_file("current_tracer", "nop") < 0)
229 if (write_tracing_file("set_ftrace_pid", " ") < 0)
232 if (reset_tracing_cpu() < 0)
235 if (write_tracing_file("max_graph_depth", "0") < 0)
238 if (write_tracing_file("tracing_thresh", "0") < 0)
241 reset_tracing_filters();
242 reset_tracing_options(ftrace);
246 static int set_tracing_pid(struct perf_ftrace *ftrace)
251 if (target__has_cpu(&ftrace->target))
254 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
255 scnprintf(buf, sizeof(buf), "%d",
256 ftrace->evlist->core.threads->map[i]);
257 if (append_tracing_file("set_ftrace_pid", buf) < 0)
263 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
270 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
271 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
272 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
274 cpumask = malloc(mask_size);
275 if (cpumask == NULL) {
276 pr_debug("failed to allocate cpu mask\n");
280 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
282 ret = write_tracing_file("tracing_cpumask", cpumask);
288 static int set_tracing_cpu(struct perf_ftrace *ftrace)
290 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
292 if (!target__has_cpu(&ftrace->target))
295 return set_tracing_cpumask(cpumap);
298 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
300 if (!ftrace->func_stack_trace)
303 if (write_tracing_option_file("func_stack_trace", "1") < 0)
309 static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
311 if (!ftrace->func_irq_info)
314 if (write_tracing_option_file("irq-info", "1") < 0)
320 static int reset_tracing_cpu(void)
322 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
325 ret = set_tracing_cpumask(cpumap);
326 perf_cpu_map__put(cpumap);
330 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
332 struct filter_entry *pos;
334 list_for_each_entry(pos, funcs, list) {
335 if (append_tracing_file(filter_file, pos->name) < 0)
342 static int set_tracing_filters(struct perf_ftrace *ftrace)
346 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
350 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
354 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
358 /* old kernels do not have this filter */
359 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
364 static void reset_tracing_filters(void)
366 write_tracing_file("set_ftrace_filter", " ");
367 write_tracing_file("set_ftrace_notrace", " ");
368 write_tracing_file("set_graph_function", " ");
369 write_tracing_file("set_graph_notrace", " ");
372 static int set_tracing_depth(struct perf_ftrace *ftrace)
374 if (ftrace->graph_depth == 0)
377 if (ftrace->graph_depth < 0) {
378 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
382 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
388 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
392 if (ftrace->percpu_buffer_size == 0)
395 ret = write_tracing_file_int("buffer_size_kb",
396 ftrace->percpu_buffer_size / 1024);
403 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
405 if (!ftrace->inherit)
408 if (write_tracing_option_file("function-fork", "1") < 0)
414 static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
416 if (!ftrace->graph_nosleep_time)
419 if (write_tracing_option_file("sleep-time", "0") < 0)
425 static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
427 if (!ftrace->graph_noirqs)
430 if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
436 static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
438 if (!ftrace->graph_verbose)
441 if (write_tracing_option_file("funcgraph-proc", "1") < 0)
444 if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
447 if (write_tracing_option_file("latency-format", "1") < 0)
453 static int set_tracing_thresh(struct perf_ftrace *ftrace)
457 if (ftrace->graph_thresh == 0)
460 ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
467 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
472 struct pollfd pollfd = {
476 if (!(perf_cap__capable(CAP_PERFMON) ||
477 perf_cap__capable(CAP_SYS_ADMIN))) {
478 pr_err("ftrace only works for %s!\n",
479 #ifdef HAVE_LIBCAP_SUPPORT
480 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
488 signal(SIGINT, sig_handler);
489 signal(SIGUSR1, sig_handler);
490 signal(SIGCHLD, sig_handler);
491 signal(SIGPIPE, sig_handler);
493 if (ftrace->list_avail_functions)
494 return read_tracing_file_to_stdout("available_filter_functions");
496 if (reset_tracing_files(ftrace) < 0) {
497 pr_err("failed to reset ftrace\n");
501 /* reset ftrace buffer */
502 if (write_tracing_file("trace", "0") < 0)
505 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
506 &ftrace->target, argv, false,
507 ftrace__workload_exec_failed_signal) < 0) {
511 if (set_tracing_pid(ftrace) < 0) {
512 pr_err("failed to set ftrace pid\n");
516 if (set_tracing_cpu(ftrace) < 0) {
517 pr_err("failed to set tracing cpumask\n");
521 if (set_tracing_func_stack_trace(ftrace) < 0) {
522 pr_err("failed to set tracing option func_stack_trace\n");
526 if (set_tracing_func_irqinfo(ftrace) < 0) {
527 pr_err("failed to set tracing option irq-info\n");
531 if (set_tracing_filters(ftrace) < 0) {
532 pr_err("failed to set tracing filters\n");
536 if (set_tracing_depth(ftrace) < 0) {
537 pr_err("failed to set graph depth\n");
541 if (set_tracing_percpu_buffer_size(ftrace) < 0) {
542 pr_err("failed to set tracing per-cpu buffer size\n");
546 if (set_tracing_trace_inherit(ftrace) < 0) {
547 pr_err("failed to set tracing option function-fork\n");
551 if (set_tracing_sleep_time(ftrace) < 0) {
552 pr_err("failed to set tracing option sleep-time\n");
556 if (set_tracing_funcgraph_irqs(ftrace) < 0) {
557 pr_err("failed to set tracing option funcgraph-irqs\n");
561 if (set_tracing_funcgraph_verbose(ftrace) < 0) {
562 pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
566 if (set_tracing_thresh(ftrace) < 0) {
567 pr_err("failed to set tracing thresh\n");
571 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
572 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
578 trace_file = get_tracing_file("trace_pipe");
580 pr_err("failed to open trace_pipe\n");
584 trace_fd = open(trace_file, O_RDONLY);
586 put_tracing_file(trace_file);
589 pr_err("failed to open trace_pipe\n");
593 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
594 pollfd.fd = trace_fd;
596 /* display column headers */
597 read_tracing_file_to_stdout("trace");
599 if (write_tracing_file("tracing_on", "1") < 0) {
600 pr_err("can't enable tracing\n");
604 perf_evlist__start_workload(ftrace->evlist);
607 if (poll(&pollfd, 1, -1) < 0)
610 if (pollfd.revents & POLLIN) {
611 int n = read(trace_fd, buf, sizeof(buf));
614 if (fwrite(buf, n, 1, stdout) != 1)
619 write_tracing_file("tracing_on", "0");
621 if (workload_exec_errno) {
622 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
623 /* flush stdout first so below error msg appears at the end. */
625 pr_err("workload failed: %s\n", emsg);
629 /* read remaining buffer contents */
631 int n = read(trace_fd, buf, sizeof(buf));
634 if (fwrite(buf, n, 1, stdout) != 1)
641 reset_tracing_files(ftrace);
643 return (done && !workload_exec_errno) ? 0 : -1;
646 static int perf_ftrace_config(const char *var, const char *value, void *cb)
648 struct perf_ftrace *ftrace = cb;
650 if (!strstarts(var, "ftrace."))
653 if (strcmp(var, "ftrace.tracer"))
656 if (!strcmp(value, "function_graph") ||
657 !strcmp(value, "function")) {
658 ftrace->tracer = value;
662 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
666 static int parse_filter_func(const struct option *opt, const char *str,
667 int unset __maybe_unused)
669 struct list_head *head = opt->value;
670 struct filter_entry *entry;
672 entry = malloc(sizeof(*entry) + strlen(str) + 1);
676 strcpy(entry->name, str);
677 list_add_tail(&entry->list, head);
682 static void delete_filter_func(struct list_head *head)
684 struct filter_entry *pos, *tmp;
686 list_for_each_entry_safe(pos, tmp, head, list) {
687 list_del_init(&pos->list);
692 static int parse_buffer_size(const struct option *opt,
693 const char *str, int unset)
695 unsigned long *s = (unsigned long *)opt->value;
696 static struct parse_tag tags_size[] = {
697 { .tag = 'B', .mult = 1 },
698 { .tag = 'K', .mult = 1 << 10 },
699 { .tag = 'M', .mult = 1 << 20 },
700 { .tag = 'G', .mult = 1 << 30 },
710 val = parse_tag_value(str, tags_size);
711 if (val != (unsigned long) -1) {
713 pr_err("buffer size too small, must larger than 1KB.");
723 static int parse_func_tracer_opts(const struct option *opt,
724 const char *str, int unset)
727 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
728 struct sublevel_option func_tracer_opts[] = {
729 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
730 { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
737 ret = perf_parse_sublevel_options(str, func_tracer_opts);
744 static int parse_graph_tracer_opts(const struct option *opt,
745 const char *str, int unset)
748 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
749 struct sublevel_option graph_tracer_opts[] = {
750 { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
751 { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
752 { .name = "verbose", .value_ptr = &ftrace->graph_verbose },
753 { .name = "thresh", .value_ptr = &ftrace->graph_thresh },
760 ret = perf_parse_sublevel_options(str, graph_tracer_opts);
767 static void select_tracer(struct perf_ftrace *ftrace)
769 bool graph = !list_empty(&ftrace->graph_funcs) ||
770 !list_empty(&ftrace->nograph_funcs);
771 bool func = !list_empty(&ftrace->filters) ||
772 !list_empty(&ftrace->notrace);
774 /* The function_graph has priority over function tracer. */
776 ftrace->tracer = "function_graph";
778 ftrace->tracer = "function";
779 /* Otherwise, the default tracer is used. */
781 pr_debug("%s tracer is used\n", ftrace->tracer);
784 int cmd_ftrace(int argc, const char **argv)
787 struct perf_ftrace ftrace = {
788 .tracer = DEFAULT_TRACER,
789 .target = { .uid = UINT_MAX, },
791 const char * const ftrace_usage[] = {
792 "perf ftrace [<options>] [<command>]",
793 "perf ftrace [<options>] -- <command> [<options>]",
796 const struct option ftrace_options[] = {
797 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
798 "tracer to use: function_graph(default) or function"),
799 OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
800 "Show available functions to filter"),
801 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
802 "trace on existing process id"),
803 OPT_INCR('v', "verbose", &verbose,
805 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
806 "system-wide collection from all CPUs"),
807 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
808 "list of cpus to monitor"),
809 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
810 "trace given functions using function tracer",
812 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
813 "do not trace given functions", parse_filter_func),
814 OPT_CALLBACK(0, "func-opts", &ftrace, "options",
815 "function tracer options, available options: call-graph,irq-info",
816 parse_func_tracer_opts),
817 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
818 "trace given functions using function_graph tracer",
820 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
821 "Set nograph filter on given functions", parse_filter_func),
822 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
823 "Max depth for function graph tracer"),
824 OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
825 "graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>",
826 parse_graph_tracer_opts),
827 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
828 "size of per cpu buffer", parse_buffer_size),
829 OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
830 "trace children processes"),
834 INIT_LIST_HEAD(&ftrace.filters);
835 INIT_LIST_HEAD(&ftrace.notrace);
836 INIT_LIST_HEAD(&ftrace.graph_funcs);
837 INIT_LIST_HEAD(&ftrace.nograph_funcs);
839 ret = perf_config(perf_ftrace_config, &ftrace);
843 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
844 PARSE_OPT_STOP_AT_NON_OPTION);
845 if (!argc && target__none(&ftrace.target))
846 ftrace.target.system_wide = true;
848 select_tracer(&ftrace);
850 ret = target__validate(&ftrace.target);
854 target__strerror(&ftrace.target, ret, errbuf, 512);
855 pr_err("%s\n", errbuf);
856 goto out_delete_filters;
859 ftrace.evlist = evlist__new();
860 if (ftrace.evlist == NULL) {
862 goto out_delete_filters;
865 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
867 goto out_delete_evlist;
869 ret = __cmd_ftrace(&ftrace, argc, argv);
872 evlist__delete(ftrace.evlist);
875 delete_filter_func(&ftrace.filters);
876 delete_filter_func(&ftrace.notrace);
877 delete_filter_func(&ftrace.graph_funcs);
878 delete_filter_func(&ftrace.nograph_funcs);