1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 * Copyright (c) 2020 Changbin Du <changbin.du@gmail.com>, significant enhancement.
17 #include <linux/capability.h>
18 #include <linux/string.h>
21 #include <subcmd/pager.h>
22 #include <subcmd/parse-options.h>
23 #include <api/fs/tracing_path.h>
27 #include "thread_map.h"
28 #include "strfilter.h"
30 #include "util/config.h"
31 #include "util/units.h"
32 #include "util/parse-sublevel-options.h"
34 #define DEFAULT_TRACER "function_graph"
37 struct evlist *evlist;
40 struct list_head filters;
41 struct list_head notrace;
42 struct list_head graph_funcs;
43 struct list_head nograph_funcs;
45 unsigned long percpu_buffer_size;
49 int graph_nosleep_time;
53 unsigned int initial_delay;
57 struct list_head list;
61 static volatile int workload_exec_errno;
64 static void sig_handler(int sig __maybe_unused)
70 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
71 * we asked by setting its exec_error to the function below,
72 * ftrace__workload_exec_failed_signal.
74 * XXX We need to handle this more appropriately, emitting an error, etc.
76 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
77 siginfo_t *info __maybe_unused,
78 void *ucontext __maybe_unused)
80 workload_exec_errno = info->si_value.sival_int;
84 static int __write_tracing_file(const char *name, const char *val, bool append)
88 ssize_t size = strlen(val);
93 file = get_tracing_file(name);
95 pr_debug("cannot get tracing file: %s\n", name);
104 fd = open(file, flags);
106 pr_debug("cannot open tracing file: %s: %s\n",
107 name, str_error_r(errno, errbuf, sizeof(errbuf)));
112 * Copy the original value and append a '\n'. Without this,
113 * the kernel can hide possible errors.
115 val_copy = strdup(val);
118 val_copy[size] = '\n';
120 if (write(fd, val_copy, size + 1) == size + 1)
123 pr_debug("write '%s' to tracing/%s failed: %s\n",
124 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
130 put_tracing_file(file);
134 static int write_tracing_file(const char *name, const char *val)
136 return __write_tracing_file(name, val, false);
139 static int append_tracing_file(const char *name, const char *val)
141 return __write_tracing_file(name, val, true);
144 static int read_tracing_file_to_stdout(const char *name)
151 file = get_tracing_file(name);
153 pr_debug("cannot get tracing file: %s\n", name);
157 fd = open(file, O_RDONLY);
159 pr_debug("cannot open tracing file: %s: %s\n",
160 name, str_error_r(errno, buf, sizeof(buf)));
164 /* read contents to stdout */
166 int n = read(fd, buf, sizeof(buf));
172 if (fwrite(buf, n, 1, stdout) != 1)
180 put_tracing_file(file);
184 static int read_tracing_file_by_line(const char *name,
185 void (*cb)(char *str, void *arg),
193 file = get_tracing_file(name);
195 pr_debug("cannot get tracing file: %s\n", name);
199 fp = fopen(file, "r");
201 pr_debug("cannot open tracing file: %s\n", name);
202 put_tracing_file(file);
206 while (getline(&line, &len, fp) != -1) {
214 put_tracing_file(file);
218 static int write_tracing_file_int(const char *name, int value)
222 snprintf(buf, sizeof(buf), "%d", value);
223 if (write_tracing_file(name, buf) < 0)
229 static int write_tracing_option_file(const char *name, const char *val)
234 if (asprintf(&file, "options/%s", name) < 0)
237 ret = __write_tracing_file(file, val, false);
242 static int reset_tracing_cpu(void);
243 static void reset_tracing_filters(void);
245 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
247 write_tracing_option_file("function-fork", "0");
248 write_tracing_option_file("func_stack_trace", "0");
249 write_tracing_option_file("sleep-time", "1");
250 write_tracing_option_file("funcgraph-irqs", "1");
251 write_tracing_option_file("funcgraph-proc", "0");
252 write_tracing_option_file("funcgraph-abstime", "0");
253 write_tracing_option_file("latency-format", "0");
254 write_tracing_option_file("irq-info", "0");
257 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
259 if (write_tracing_file("tracing_on", "0") < 0)
262 if (write_tracing_file("current_tracer", "nop") < 0)
265 if (write_tracing_file("set_ftrace_pid", " ") < 0)
268 if (reset_tracing_cpu() < 0)
271 if (write_tracing_file("max_graph_depth", "0") < 0)
274 if (write_tracing_file("tracing_thresh", "0") < 0)
277 reset_tracing_filters();
278 reset_tracing_options(ftrace);
282 static int set_tracing_pid(struct perf_ftrace *ftrace)
287 if (target__has_cpu(&ftrace->target))
290 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
291 scnprintf(buf, sizeof(buf), "%d",
292 perf_thread_map__pid(ftrace->evlist->core.threads, i));
293 if (append_tracing_file("set_ftrace_pid", buf) < 0)
299 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
306 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
307 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
308 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
310 cpumask = malloc(mask_size);
311 if (cpumask == NULL) {
312 pr_debug("failed to allocate cpu mask\n");
316 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
318 ret = write_tracing_file("tracing_cpumask", cpumask);
324 static int set_tracing_cpu(struct perf_ftrace *ftrace)
326 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
328 if (!target__has_cpu(&ftrace->target))
331 return set_tracing_cpumask(cpumap);
334 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
336 if (!ftrace->func_stack_trace)
339 if (write_tracing_option_file("func_stack_trace", "1") < 0)
345 static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
347 if (!ftrace->func_irq_info)
350 if (write_tracing_option_file("irq-info", "1") < 0)
356 static int reset_tracing_cpu(void)
358 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
361 ret = set_tracing_cpumask(cpumap);
362 perf_cpu_map__put(cpumap);
366 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
368 struct filter_entry *pos;
370 list_for_each_entry(pos, funcs, list) {
371 if (append_tracing_file(filter_file, pos->name) < 0)
378 static int set_tracing_filters(struct perf_ftrace *ftrace)
382 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
386 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
390 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
394 /* old kernels do not have this filter */
395 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
400 static void reset_tracing_filters(void)
402 write_tracing_file("set_ftrace_filter", " ");
403 write_tracing_file("set_ftrace_notrace", " ");
404 write_tracing_file("set_graph_function", " ");
405 write_tracing_file("set_graph_notrace", " ");
408 static int set_tracing_depth(struct perf_ftrace *ftrace)
410 if (ftrace->graph_depth == 0)
413 if (ftrace->graph_depth < 0) {
414 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
418 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
424 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
428 if (ftrace->percpu_buffer_size == 0)
431 ret = write_tracing_file_int("buffer_size_kb",
432 ftrace->percpu_buffer_size / 1024);
439 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
441 if (!ftrace->inherit)
444 if (write_tracing_option_file("function-fork", "1") < 0)
450 static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
452 if (!ftrace->graph_nosleep_time)
455 if (write_tracing_option_file("sleep-time", "0") < 0)
461 static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
463 if (!ftrace->graph_noirqs)
466 if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
472 static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
474 if (!ftrace->graph_verbose)
477 if (write_tracing_option_file("funcgraph-proc", "1") < 0)
480 if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
483 if (write_tracing_option_file("latency-format", "1") < 0)
489 static int set_tracing_thresh(struct perf_ftrace *ftrace)
493 if (ftrace->graph_thresh == 0)
496 ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
503 static int set_tracing_options(struct perf_ftrace *ftrace)
505 if (set_tracing_pid(ftrace) < 0) {
506 pr_err("failed to set ftrace pid\n");
510 if (set_tracing_cpu(ftrace) < 0) {
511 pr_err("failed to set tracing cpumask\n");
515 if (set_tracing_func_stack_trace(ftrace) < 0) {
516 pr_err("failed to set tracing option func_stack_trace\n");
520 if (set_tracing_func_irqinfo(ftrace) < 0) {
521 pr_err("failed to set tracing option irq-info\n");
525 if (set_tracing_filters(ftrace) < 0) {
526 pr_err("failed to set tracing filters\n");
530 if (set_tracing_depth(ftrace) < 0) {
531 pr_err("failed to set graph depth\n");
535 if (set_tracing_percpu_buffer_size(ftrace) < 0) {
536 pr_err("failed to set tracing per-cpu buffer size\n");
540 if (set_tracing_trace_inherit(ftrace) < 0) {
541 pr_err("failed to set tracing option function-fork\n");
545 if (set_tracing_sleep_time(ftrace) < 0) {
546 pr_err("failed to set tracing option sleep-time\n");
550 if (set_tracing_funcgraph_irqs(ftrace) < 0) {
551 pr_err("failed to set tracing option funcgraph-irqs\n");
555 if (set_tracing_funcgraph_verbose(ftrace) < 0) {
556 pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
560 if (set_tracing_thresh(ftrace) < 0) {
561 pr_err("failed to set tracing thresh\n");
568 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
573 struct pollfd pollfd = {
577 if (!(perf_cap__capable(CAP_PERFMON) ||
578 perf_cap__capable(CAP_SYS_ADMIN))) {
579 pr_err("ftrace only works for %s!\n",
580 #ifdef HAVE_LIBCAP_SUPPORT
581 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
589 signal(SIGINT, sig_handler);
590 signal(SIGUSR1, sig_handler);
591 signal(SIGCHLD, sig_handler);
592 signal(SIGPIPE, sig_handler);
594 if (reset_tracing_files(ftrace) < 0) {
595 pr_err("failed to reset ftrace\n");
599 /* reset ftrace buffer */
600 if (write_tracing_file("trace", "0") < 0)
603 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
604 &ftrace->target, argv, false,
605 ftrace__workload_exec_failed_signal) < 0) {
609 if (set_tracing_options(ftrace) < 0)
612 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
613 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
619 trace_file = get_tracing_file("trace_pipe");
621 pr_err("failed to open trace_pipe\n");
625 trace_fd = open(trace_file, O_RDONLY);
627 put_tracing_file(trace_file);
630 pr_err("failed to open trace_pipe\n");
634 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
635 pollfd.fd = trace_fd;
637 /* display column headers */
638 read_tracing_file_to_stdout("trace");
640 if (!ftrace->initial_delay) {
641 if (write_tracing_file("tracing_on", "1") < 0) {
642 pr_err("can't enable tracing\n");
647 perf_evlist__start_workload(ftrace->evlist);
649 if (ftrace->initial_delay) {
650 usleep(ftrace->initial_delay * 1000);
651 if (write_tracing_file("tracing_on", "1") < 0) {
652 pr_err("can't enable tracing\n");
658 if (poll(&pollfd, 1, -1) < 0)
661 if (pollfd.revents & POLLIN) {
662 int n = read(trace_fd, buf, sizeof(buf));
665 if (fwrite(buf, n, 1, stdout) != 1)
670 write_tracing_file("tracing_on", "0");
672 if (workload_exec_errno) {
673 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
674 /* flush stdout first so below error msg appears at the end. */
676 pr_err("workload failed: %s\n", emsg);
680 /* read remaining buffer contents */
682 int n = read(trace_fd, buf, sizeof(buf));
685 if (fwrite(buf, n, 1, stdout) != 1)
692 reset_tracing_files(ftrace);
694 return (done && !workload_exec_errno) ? 0 : -1;
697 static int perf_ftrace_config(const char *var, const char *value, void *cb)
699 struct perf_ftrace *ftrace = cb;
701 if (!strstarts(var, "ftrace."))
704 if (strcmp(var, "ftrace.tracer"))
707 if (!strcmp(value, "function_graph") ||
708 !strcmp(value, "function")) {
709 ftrace->tracer = value;
713 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
717 static void list_function_cb(char *str, void *arg)
719 struct strfilter *filter = (struct strfilter *)arg;
721 if (strfilter__compare(filter, str))
725 static int opt_list_avail_functions(const struct option *opt __maybe_unused,
726 const char *str, int unset)
728 struct strfilter *filter;
729 const char *err = NULL;
735 filter = strfilter__new(str, &err);
737 return err ? -EINVAL : -ENOMEM;
739 ret = strfilter__or(filter, str, &err);
740 if (ret == -EINVAL) {
741 pr_err("Filter parse error at %td.\n", err - str + 1);
742 pr_err("Source: \"%s\"\n", str);
743 pr_err(" %*c\n", (int)(err - str + 1), '^');
744 strfilter__delete(filter);
748 ret = read_tracing_file_by_line("available_filter_functions",
749 list_function_cb, filter);
750 strfilter__delete(filter);
757 static int parse_filter_func(const struct option *opt, const char *str,
758 int unset __maybe_unused)
760 struct list_head *head = opt->value;
761 struct filter_entry *entry;
763 entry = malloc(sizeof(*entry) + strlen(str) + 1);
767 strcpy(entry->name, str);
768 list_add_tail(&entry->list, head);
773 static void delete_filter_func(struct list_head *head)
775 struct filter_entry *pos, *tmp;
777 list_for_each_entry_safe(pos, tmp, head, list) {
778 list_del_init(&pos->list);
783 static int parse_buffer_size(const struct option *opt,
784 const char *str, int unset)
786 unsigned long *s = (unsigned long *)opt->value;
787 static struct parse_tag tags_size[] = {
788 { .tag = 'B', .mult = 1 },
789 { .tag = 'K', .mult = 1 << 10 },
790 { .tag = 'M', .mult = 1 << 20 },
791 { .tag = 'G', .mult = 1 << 30 },
801 val = parse_tag_value(str, tags_size);
802 if (val != (unsigned long) -1) {
804 pr_err("buffer size too small, must larger than 1KB.");
814 static int parse_func_tracer_opts(const struct option *opt,
815 const char *str, int unset)
818 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
819 struct sublevel_option func_tracer_opts[] = {
820 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
821 { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
828 ret = perf_parse_sublevel_options(str, func_tracer_opts);
835 static int parse_graph_tracer_opts(const struct option *opt,
836 const char *str, int unset)
839 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
840 struct sublevel_option graph_tracer_opts[] = {
841 { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
842 { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
843 { .name = "verbose", .value_ptr = &ftrace->graph_verbose },
844 { .name = "thresh", .value_ptr = &ftrace->graph_thresh },
845 { .name = "depth", .value_ptr = &ftrace->graph_depth },
852 ret = perf_parse_sublevel_options(str, graph_tracer_opts);
859 static void select_tracer(struct perf_ftrace *ftrace)
861 bool graph = !list_empty(&ftrace->graph_funcs) ||
862 !list_empty(&ftrace->nograph_funcs);
863 bool func = !list_empty(&ftrace->filters) ||
864 !list_empty(&ftrace->notrace);
866 /* The function_graph has priority over function tracer. */
868 ftrace->tracer = "function_graph";
870 ftrace->tracer = "function";
871 /* Otherwise, the default tracer is used. */
873 pr_debug("%s tracer is used\n", ftrace->tracer);
876 int cmd_ftrace(int argc, const char **argv)
879 struct perf_ftrace ftrace = {
880 .tracer = DEFAULT_TRACER,
881 .target = { .uid = UINT_MAX, },
883 const char * const ftrace_usage[] = {
884 "perf ftrace [<options>] [<command>]",
885 "perf ftrace [<options>] -- <command> [<options>]",
888 const struct option ftrace_options[] = {
889 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
890 "Tracer to use: function_graph(default) or function"),
891 OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
892 "Show available functions to filter",
893 opt_list_avail_functions, "*"),
894 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
895 "Trace on existing process id"),
896 /* TODO: Add short option -t after -t/--tracer can be removed. */
897 OPT_STRING(0, "tid", &ftrace.target.tid, "tid",
898 "Trace on existing thread id (exclusive to --pid)"),
899 OPT_INCR('v', "verbose", &verbose,
901 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
902 "System-wide collection from all CPUs"),
903 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
904 "List of cpus to monitor"),
905 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
906 "Trace given functions using function tracer",
908 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
909 "Do not trace given functions", parse_filter_func),
910 OPT_CALLBACK(0, "func-opts", &ftrace, "options",
911 "Function tracer options, available options: call-graph,irq-info",
912 parse_func_tracer_opts),
913 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
914 "Trace given functions using function_graph tracer",
916 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
917 "Set nograph filter on given functions", parse_filter_func),
918 OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
919 "Graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>",
920 parse_graph_tracer_opts),
921 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
922 "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
923 OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
924 "Trace children processes"),
925 OPT_UINTEGER('D', "delay", &ftrace.initial_delay,
926 "Number of milliseconds to wait before starting tracing after program start"),
930 INIT_LIST_HEAD(&ftrace.filters);
931 INIT_LIST_HEAD(&ftrace.notrace);
932 INIT_LIST_HEAD(&ftrace.graph_funcs);
933 INIT_LIST_HEAD(&ftrace.nograph_funcs);
935 ret = perf_config(perf_ftrace_config, &ftrace);
939 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
940 PARSE_OPT_STOP_AT_NON_OPTION);
941 if (!argc && target__none(&ftrace.target))
942 ftrace.target.system_wide = true;
944 select_tracer(&ftrace);
946 ret = target__validate(&ftrace.target);
950 target__strerror(&ftrace.target, ret, errbuf, 512);
951 pr_err("%s\n", errbuf);
952 goto out_delete_filters;
955 ftrace.evlist = evlist__new();
956 if (ftrace.evlist == NULL) {
958 goto out_delete_filters;
961 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
963 goto out_delete_evlist;
965 ret = __cmd_ftrace(&ftrace, argc, argv);
968 evlist__delete(ftrace.evlist);
971 delete_filter_func(&ftrace.filters);
972 delete_filter_func(&ftrace.notrace);
973 delete_filter_func(&ftrace.graph_funcs);
974 delete_filter_func(&ftrace.nograph_funcs);