1 // SPDX-License-Identifier: GPL-2.0-only
20 #include <linux/capability.h>
21 #include <linux/string.h>
24 #include <subcmd/pager.h>
25 #include <subcmd/parse-options.h>
27 #include <api/fs/tracing_path.h>
32 #include "thread_map.h"
33 #include "strfilter.h"
35 #include "util/config.h"
36 #include "util/ftrace.h"
37 #include "util/stat.h"
38 #include "util/units.h"
39 #include "util/parse-sublevel-options.h"
41 #define DEFAULT_TRACER "function_graph"
43 static volatile sig_atomic_t workload_exec_errno;
44 static volatile sig_atomic_t done;
46 static void sig_handler(int sig __maybe_unused)
52 * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
53 * we asked by setting its exec_error to the function below,
54 * ftrace__workload_exec_failed_signal.
56 * XXX We need to handle this more appropriately, emitting an error, etc.
58 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
59 siginfo_t *info __maybe_unused,
60 void *ucontext __maybe_unused)
62 workload_exec_errno = info->si_value.sival_int;
66 static bool check_ftrace_capable(void)
70 if (perf_cap__capable(CAP_PERFMON, &used_root))
73 if (!used_root && perf_cap__capable(CAP_SYS_ADMIN, &used_root))
76 pr_err("ftrace only works for %s!\n",
78 : "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
83 static bool is_ftrace_supported(void)
86 bool supported = false;
88 file = get_tracing_file("set_ftrace_pid");
90 pr_debug("cannot get tracing file set_ftrace_pid\n");
94 if (!access(file, F_OK))
97 put_tracing_file(file);
101 static int __write_tracing_file(const char *name, const char *val, bool append)
105 ssize_t size = strlen(val);
106 int flags = O_WRONLY;
110 file = get_tracing_file(name);
112 pr_debug("cannot get tracing file: %s\n", name);
121 fd = open(file, flags);
123 pr_debug("cannot open tracing file: %s: %s\n",
124 name, str_error_r(errno, errbuf, sizeof(errbuf)));
129 * Copy the original value and append a '\n'. Without this,
130 * the kernel can hide possible errors.
132 val_copy = strdup(val);
135 val_copy[size] = '\n';
137 if (write(fd, val_copy, size + 1) == size + 1)
140 pr_debug("write '%s' to tracing/%s failed: %s\n",
141 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
147 put_tracing_file(file);
151 static int write_tracing_file(const char *name, const char *val)
153 return __write_tracing_file(name, val, false);
156 static int append_tracing_file(const char *name, const char *val)
158 return __write_tracing_file(name, val, true);
161 static int read_tracing_file_to_stdout(const char *name)
168 file = get_tracing_file(name);
170 pr_debug("cannot get tracing file: %s\n", name);
174 fd = open(file, O_RDONLY);
176 pr_debug("cannot open tracing file: %s: %s\n",
177 name, str_error_r(errno, buf, sizeof(buf)));
181 /* read contents to stdout */
183 int n = read(fd, buf, sizeof(buf));
189 if (fwrite(buf, n, 1, stdout) != 1)
197 put_tracing_file(file);
201 static int read_tracing_file_by_line(const char *name,
202 void (*cb)(char *str, void *arg),
210 file = get_tracing_file(name);
212 pr_debug("cannot get tracing file: %s\n", name);
216 fp = fopen(file, "r");
218 pr_debug("cannot open tracing file: %s\n", name);
219 put_tracing_file(file);
223 while (getline(&line, &len, fp) != -1) {
231 put_tracing_file(file);
235 static int write_tracing_file_int(const char *name, int value)
239 snprintf(buf, sizeof(buf), "%d", value);
240 if (write_tracing_file(name, buf) < 0)
246 static int write_tracing_option_file(const char *name, const char *val)
251 if (asprintf(&file, "options/%s", name) < 0)
254 ret = __write_tracing_file(file, val, false);
259 static int reset_tracing_cpu(void);
260 static void reset_tracing_filters(void);
262 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
264 write_tracing_option_file("function-fork", "0");
265 write_tracing_option_file("func_stack_trace", "0");
266 write_tracing_option_file("sleep-time", "1");
267 write_tracing_option_file("funcgraph-irqs", "1");
268 write_tracing_option_file("funcgraph-proc", "0");
269 write_tracing_option_file("funcgraph-abstime", "0");
270 write_tracing_option_file("funcgraph-tail", "0");
271 write_tracing_option_file("latency-format", "0");
272 write_tracing_option_file("irq-info", "0");
275 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
277 if (write_tracing_file("tracing_on", "0") < 0)
280 if (write_tracing_file("current_tracer", "nop") < 0)
283 if (write_tracing_file("set_ftrace_pid", " ") < 0)
286 if (reset_tracing_cpu() < 0)
289 if (write_tracing_file("max_graph_depth", "0") < 0)
292 if (write_tracing_file("tracing_thresh", "0") < 0)
295 reset_tracing_filters();
296 reset_tracing_options(ftrace);
300 static int set_tracing_pid(struct perf_ftrace *ftrace)
305 if (target__has_cpu(&ftrace->target))
308 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
309 scnprintf(buf, sizeof(buf), "%d",
310 perf_thread_map__pid(ftrace->evlist->core.threads, i));
311 if (append_tracing_file("set_ftrace_pid", buf) < 0)
317 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
324 last_cpu = perf_cpu_map__cpu(cpumap, perf_cpu_map__nr(cpumap) - 1).cpu;
325 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
326 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
328 cpumask = malloc(mask_size);
329 if (cpumask == NULL) {
330 pr_debug("failed to allocate cpu mask\n");
334 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
336 ret = write_tracing_file("tracing_cpumask", cpumask);
342 static int set_tracing_cpu(struct perf_ftrace *ftrace)
344 struct perf_cpu_map *cpumap = ftrace->evlist->core.user_requested_cpus;
346 if (!target__has_cpu(&ftrace->target))
349 return set_tracing_cpumask(cpumap);
352 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
354 if (!ftrace->func_stack_trace)
357 if (write_tracing_option_file("func_stack_trace", "1") < 0)
363 static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
365 if (!ftrace->func_irq_info)
368 if (write_tracing_option_file("irq-info", "1") < 0)
374 static int reset_tracing_cpu(void)
376 struct perf_cpu_map *cpumap = perf_cpu_map__new_online_cpus();
379 ret = set_tracing_cpumask(cpumap);
380 perf_cpu_map__put(cpumap);
384 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
386 struct filter_entry *pos;
388 list_for_each_entry(pos, funcs, list) {
389 if (append_tracing_file(filter_file, pos->name) < 0)
396 static int set_tracing_filters(struct perf_ftrace *ftrace)
400 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
404 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
408 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
412 /* old kernels do not have this filter */
413 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
418 static void reset_tracing_filters(void)
420 write_tracing_file("set_ftrace_filter", " ");
421 write_tracing_file("set_ftrace_notrace", " ");
422 write_tracing_file("set_graph_function", " ");
423 write_tracing_file("set_graph_notrace", " ");
426 static int set_tracing_depth(struct perf_ftrace *ftrace)
428 if (ftrace->graph_depth == 0)
431 if (ftrace->graph_depth < 0) {
432 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
436 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
442 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
446 if (ftrace->percpu_buffer_size == 0)
449 ret = write_tracing_file_int("buffer_size_kb",
450 ftrace->percpu_buffer_size / 1024);
457 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
459 if (!ftrace->inherit)
462 if (write_tracing_option_file("function-fork", "1") < 0)
468 static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
470 if (!ftrace->graph_nosleep_time)
473 if (write_tracing_option_file("sleep-time", "0") < 0)
479 static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
481 if (!ftrace->graph_noirqs)
484 if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
490 static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
492 if (!ftrace->graph_verbose)
495 if (write_tracing_option_file("funcgraph-proc", "1") < 0)
498 if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
501 if (write_tracing_option_file("latency-format", "1") < 0)
507 static int set_tracing_funcgraph_tail(struct perf_ftrace *ftrace)
509 if (!ftrace->graph_tail)
512 if (write_tracing_option_file("funcgraph-tail", "1") < 0)
518 static int set_tracing_thresh(struct perf_ftrace *ftrace)
522 if (ftrace->graph_thresh == 0)
525 ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
532 static int set_tracing_options(struct perf_ftrace *ftrace)
534 if (set_tracing_pid(ftrace) < 0) {
535 pr_err("failed to set ftrace pid\n");
539 if (set_tracing_cpu(ftrace) < 0) {
540 pr_err("failed to set tracing cpumask\n");
544 if (set_tracing_func_stack_trace(ftrace) < 0) {
545 pr_err("failed to set tracing option func_stack_trace\n");
549 if (set_tracing_func_irqinfo(ftrace) < 0) {
550 pr_err("failed to set tracing option irq-info\n");
554 if (set_tracing_filters(ftrace) < 0) {
555 pr_err("failed to set tracing filters\n");
559 if (set_tracing_depth(ftrace) < 0) {
560 pr_err("failed to set graph depth\n");
564 if (set_tracing_percpu_buffer_size(ftrace) < 0) {
565 pr_err("failed to set tracing per-cpu buffer size\n");
569 if (set_tracing_trace_inherit(ftrace) < 0) {
570 pr_err("failed to set tracing option function-fork\n");
574 if (set_tracing_sleep_time(ftrace) < 0) {
575 pr_err("failed to set tracing option sleep-time\n");
579 if (set_tracing_funcgraph_irqs(ftrace) < 0) {
580 pr_err("failed to set tracing option funcgraph-irqs\n");
584 if (set_tracing_funcgraph_verbose(ftrace) < 0) {
585 pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
589 if (set_tracing_thresh(ftrace) < 0) {
590 pr_err("failed to set tracing thresh\n");
594 if (set_tracing_funcgraph_tail(ftrace) < 0) {
595 pr_err("failed to set tracing option funcgraph-tail\n");
602 static void select_tracer(struct perf_ftrace *ftrace)
604 bool graph = !list_empty(&ftrace->graph_funcs) ||
605 !list_empty(&ftrace->nograph_funcs);
606 bool func = !list_empty(&ftrace->filters) ||
607 !list_empty(&ftrace->notrace);
609 /* The function_graph has priority over function tracer. */
611 ftrace->tracer = "function_graph";
613 ftrace->tracer = "function";
614 /* Otherwise, the default tracer is used. */
616 pr_debug("%s tracer is used\n", ftrace->tracer);
619 static int __cmd_ftrace(struct perf_ftrace *ftrace)
624 struct pollfd pollfd = {
628 select_tracer(ftrace);
630 if (reset_tracing_files(ftrace) < 0) {
631 pr_err("failed to reset ftrace\n");
635 /* reset ftrace buffer */
636 if (write_tracing_file("trace", "0") < 0)
639 if (set_tracing_options(ftrace) < 0)
642 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
643 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
649 trace_file = get_tracing_file("trace_pipe");
651 pr_err("failed to open trace_pipe\n");
655 trace_fd = open(trace_file, O_RDONLY);
657 put_tracing_file(trace_file);
660 pr_err("failed to open trace_pipe\n");
664 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
665 pollfd.fd = trace_fd;
667 /* display column headers */
668 read_tracing_file_to_stdout("trace");
670 if (!ftrace->target.initial_delay) {
671 if (write_tracing_file("tracing_on", "1") < 0) {
672 pr_err("can't enable tracing\n");
677 evlist__start_workload(ftrace->evlist);
679 if (ftrace->target.initial_delay > 0) {
680 usleep(ftrace->target.initial_delay * 1000);
681 if (write_tracing_file("tracing_on", "1") < 0) {
682 pr_err("can't enable tracing\n");
688 if (poll(&pollfd, 1, -1) < 0)
691 if (pollfd.revents & POLLIN) {
692 int n = read(trace_fd, buf, sizeof(buf));
695 if (fwrite(buf, n, 1, stdout) != 1)
697 /* flush output since stdout is in full buffering mode due to pager */
702 write_tracing_file("tracing_on", "0");
704 if (workload_exec_errno) {
705 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
706 /* flush stdout first so below error msg appears at the end. */
708 pr_err("workload failed: %s\n", emsg);
712 /* read remaining buffer contents */
714 int n = read(trace_fd, buf, sizeof(buf));
717 if (fwrite(buf, n, 1, stdout) != 1)
724 reset_tracing_files(ftrace);
726 return (done && !workload_exec_errno) ? 0 : -1;
729 static void make_histogram(int buckets[], char *buf, size_t len, char *linebuf,
737 /* ensure NUL termination */
740 /* handle data line by line */
741 for (p = buf; (q = strchr(p, '\n')) != NULL; p = q + 1) {
743 /* move it to the line buffer */
747 * parse trace output to get function duration like in
749 * # tracer: function_graph
751 * # CPU DURATION FUNCTION CALLS
753 * 1) + 10.291 us | do_filp_open();
754 * 1) 4.889 us | do_filp_open();
755 * 1) 6.086 us | do_filp_open();
758 if (linebuf[0] == '#')
762 p = strchr(linebuf, ')');
766 while (*p && !isdigit(*p) && (*p != '|'))
770 if (*p == '\0' || *p == '|')
773 num = strtod(p, &unit);
774 if (!unit || strncmp(unit, " us", 3))
789 /* empty the line buffer for the next output */
793 /* preserve any remaining output (before newline) */
797 static void display_histogram(int buckets[], bool use_nsec)
801 int bar_total = 46; /* to fit in 80 column */
802 char bar[] = "###############################################";
805 for (i = 0; i < NUM_BUCKET; i++)
809 printf("No data found\n");
813 printf("# %14s | %10s | %-*s |\n",
814 " DURATION ", "COUNT", bar_total, "GRAPH");
816 bar_len = buckets[0] * bar_total / total;
817 printf(" %4d - %-4d %s | %10d | %.*s%*s |\n",
818 0, 1, use_nsec ? "ns" : "us", buckets[0], bar_len, bar, bar_total - bar_len, "");
820 for (i = 1; i < NUM_BUCKET - 1; i++) {
821 int start = (1 << (i - 1));
823 const char *unit = use_nsec ? "ns" : "us";
828 unit = use_nsec ? "us" : "ms";
830 bar_len = buckets[i] * bar_total / total;
831 printf(" %4d - %-4d %s | %10d | %.*s%*s |\n",
832 start, stop, unit, buckets[i], bar_len, bar,
833 bar_total - bar_len, "");
836 bar_len = buckets[NUM_BUCKET - 1] * bar_total / total;
837 printf(" %4d - %-4s %s | %10d | %.*s%*s |\n",
838 1, "...", use_nsec ? "ms" : " s", buckets[NUM_BUCKET - 1],
839 bar_len, bar, bar_total - bar_len, "");
843 static int prepare_func_latency(struct perf_ftrace *ftrace)
848 if (ftrace->target.use_bpf)
849 return perf_ftrace__latency_prepare_bpf(ftrace);
851 if (reset_tracing_files(ftrace) < 0) {
852 pr_err("failed to reset ftrace\n");
856 /* reset ftrace buffer */
857 if (write_tracing_file("trace", "0") < 0)
860 if (set_tracing_options(ftrace) < 0)
863 /* force to use the function_graph tracer to track duration */
864 if (write_tracing_file("current_tracer", "function_graph") < 0) {
865 pr_err("failed to set current_tracer to function_graph\n");
869 trace_file = get_tracing_file("trace_pipe");
871 pr_err("failed to open trace_pipe\n");
875 fd = open(trace_file, O_RDONLY);
877 pr_err("failed to open trace_pipe\n");
879 put_tracing_file(trace_file);
883 static int start_func_latency(struct perf_ftrace *ftrace)
885 if (ftrace->target.use_bpf)
886 return perf_ftrace__latency_start_bpf(ftrace);
888 if (write_tracing_file("tracing_on", "1") < 0) {
889 pr_err("can't enable tracing\n");
896 static int stop_func_latency(struct perf_ftrace *ftrace)
898 if (ftrace->target.use_bpf)
899 return perf_ftrace__latency_stop_bpf(ftrace);
901 write_tracing_file("tracing_on", "0");
905 static int read_func_latency(struct perf_ftrace *ftrace, int buckets[])
907 if (ftrace->target.use_bpf)
908 return perf_ftrace__latency_read_bpf(ftrace, buckets);
913 static int cleanup_func_latency(struct perf_ftrace *ftrace)
915 if (ftrace->target.use_bpf)
916 return perf_ftrace__latency_cleanup_bpf(ftrace);
918 reset_tracing_files(ftrace);
922 static int __cmd_latency(struct perf_ftrace *ftrace)
927 struct pollfd pollfd = {
930 int buckets[NUM_BUCKET] = { };
932 trace_fd = prepare_func_latency(ftrace);
936 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
937 pollfd.fd = trace_fd;
939 if (start_func_latency(ftrace) < 0)
942 evlist__start_workload(ftrace->evlist);
946 if (poll(&pollfd, 1, -1) < 0)
949 if (pollfd.revents & POLLIN) {
950 int n = read(trace_fd, buf, sizeof(buf) - 1);
954 make_histogram(buckets, buf, n, line, ftrace->use_nsec);
958 stop_func_latency(ftrace);
960 if (workload_exec_errno) {
961 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
962 pr_err("workload failed: %s\n", emsg);
966 /* read remaining buffer contents */
967 while (!ftrace->target.use_bpf) {
968 int n = read(trace_fd, buf, sizeof(buf) - 1);
971 make_histogram(buckets, buf, n, line, ftrace->use_nsec);
974 read_func_latency(ftrace, buckets);
976 display_histogram(buckets, ftrace->use_nsec);
980 cleanup_func_latency(ftrace);
982 return (done && !workload_exec_errno) ? 0 : -1;
985 static size_t profile_hash(long func, void *ctx __maybe_unused)
987 return str_hash((char *)func);
990 static bool profile_equal(long func1, long func2, void *ctx __maybe_unused)
992 return !strcmp((char *)func1, (char *)func2);
995 static int prepare_func_profile(struct perf_ftrace *ftrace)
997 ftrace->tracer = "function_graph";
998 ftrace->graph_tail = 1;
1000 ftrace->profile_hash = hashmap__new(profile_hash, profile_equal, NULL);
1001 if (ftrace->profile_hash == NULL)
1007 /* This is saved in a hashmap keyed by the function name */
1008 struct ftrace_profile_data {
1012 static int add_func_duration(struct perf_ftrace *ftrace, char *func, double time_ns)
1014 struct ftrace_profile_data *prof = NULL;
1016 if (!hashmap__find(ftrace->profile_hash, func, &prof)) {
1017 char *key = strdup(func);
1022 prof = zalloc(sizeof(*prof));
1028 init_stats(&prof->st);
1029 hashmap__add(ftrace->profile_hash, key, prof);
1032 update_stats(&prof->st, time_ns);
1037 * The ftrace function_graph text output normally looks like below:
1039 * CPU DURATION FUNCTION
1041 * 0) | syscall_trace_enter.isra.0() {
1042 * 0) | __audit_syscall_entry() {
1043 * 0) | auditd_test_task() {
1044 * 0) 0.271 us | __rcu_read_lock();
1045 * 0) 0.275 us | __rcu_read_unlock();
1046 * 0) 1.254 us | } /\* auditd_test_task *\/
1047 * 0) 0.279 us | ktime_get_coarse_real_ts64();
1048 * 0) 2.227 us | } /\* __audit_syscall_entry *\/
1049 * 0) 2.713 us | } /\* syscall_trace_enter.isra.0 *\/
1051 * Parse the line and get the duration and function name.
1053 static int parse_func_duration(struct perf_ftrace *ftrace, char *line, size_t len)
1060 p = strchr(line, ')');
1065 p = skip_spaces(p + 1);
1068 if (p == NULL || *p == '|')
1071 /* skip markers like '*' or '!' for longer than ms */
1075 duration = strtod(p, &p);
1077 if (strncmp(p, " us", 3)) {
1078 pr_debug("non-usec time found.. ignoring\n");
1083 * profile stat keeps the max and min values as integer,
1084 * convert to nsec time so that we can have accurate max.
1088 /* skip to the pipe */
1089 while (p < line + len && *p != '|')
1095 /* get function name */
1096 func = skip_spaces(p);
1098 /* skip the closing bracket and the start of comment */
1102 /* remove semi-colon or end of comment at the end */
1104 while (!isalnum(*p) && *p != ']') {
1109 return add_func_duration(ftrace, func, duration);
1112 enum perf_ftrace_profile_sort_key {
1120 static enum perf_ftrace_profile_sort_key profile_sort = PFP_SORT_TOTAL;
1122 static int cmp_profile_data(const void *a, const void *b)
1124 const struct hashmap_entry *e1 = *(const struct hashmap_entry **)a;
1125 const struct hashmap_entry *e2 = *(const struct hashmap_entry **)b;
1126 struct ftrace_profile_data *p1 = e1->pvalue;
1127 struct ftrace_profile_data *p2 = e2->pvalue;
1130 switch (profile_sort) {
1132 return strcmp(e1->pkey, e2->pkey);
1141 case PFP_SORT_COUNT:
1145 case PFP_SORT_TOTAL:
1147 v1 = p1->st.n * p1->st.mean;
1148 v2 = p2->st.n * p2->st.mean;
1159 static void print_profile_result(struct perf_ftrace *ftrace)
1161 struct hashmap_entry *entry, **profile;
1164 nr = hashmap__size(ftrace->profile_hash);
1168 profile = calloc(nr, sizeof(*profile));
1169 if (profile == NULL) {
1170 pr_err("failed to allocate memory for the result\n");
1175 hashmap__for_each_entry(ftrace->profile_hash, entry, bkt)
1176 profile[i++] = entry;
1180 //cmp_profile_data(profile[0], profile[1]);
1181 qsort(profile, nr, sizeof(*profile), cmp_profile_data);
1183 printf("# %10s %10s %10s %10s %s\n",
1184 "Total (us)", "Avg (us)", "Max (us)", "Count", "Function");
1186 for (i = 0; i < nr; i++) {
1187 const char *name = profile[i]->pkey;
1188 struct ftrace_profile_data *p = profile[i]->pvalue;
1190 printf("%12.3f %10.3f %6"PRIu64".%03"PRIu64" %10.0f %s\n",
1191 p->st.n * p->st.mean / 1000, p->st.mean / 1000,
1192 p->st.max / 1000, p->st.max % 1000, p->st.n, name);
1197 hashmap__for_each_entry(ftrace->profile_hash, entry, bkt) {
1198 free((char *)entry->pkey);
1199 free(entry->pvalue);
1202 hashmap__free(ftrace->profile_hash);
1203 ftrace->profile_hash = NULL;
1206 static int __cmd_profile(struct perf_ftrace *ftrace)
1213 size_t line_len = 0;
1215 if (prepare_func_profile(ftrace) < 0) {
1216 pr_err("failed to prepare func profiler\n");
1220 if (reset_tracing_files(ftrace) < 0) {
1221 pr_err("failed to reset ftrace\n");
1225 /* reset ftrace buffer */
1226 if (write_tracing_file("trace", "0") < 0)
1229 if (set_tracing_options(ftrace) < 0)
1232 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
1233 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
1239 trace_file = get_tracing_file("trace_pipe");
1241 pr_err("failed to open trace_pipe\n");
1245 trace_fd = open(trace_file, O_RDONLY);
1247 put_tracing_file(trace_file);
1250 pr_err("failed to open trace_pipe\n");
1254 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
1256 if (write_tracing_file("tracing_on", "1") < 0) {
1257 pr_err("can't enable tracing\n");
1261 evlist__start_workload(ftrace->evlist);
1263 io__init(&io, trace_fd, buf, sizeof(buf));
1266 while (!done && !io.eof) {
1267 if (io__getline(&io, &line, &line_len) < 0)
1270 if (parse_func_duration(ftrace, line, line_len) < 0)
1274 write_tracing_file("tracing_on", "0");
1276 if (workload_exec_errno) {
1277 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
1278 /* flush stdout first so below error msg appears at the end. */
1280 pr_err("workload failed: %s\n", emsg);
1284 /* read remaining buffer contents */
1287 if (io__getline(&io, &line, &line_len) < 0)
1290 if (parse_func_duration(ftrace, line, line_len) < 0)
1294 print_profile_result(ftrace);
1301 reset_tracing_files(ftrace);
1303 return (done && !workload_exec_errno) ? 0 : -1;
1306 static int perf_ftrace_config(const char *var, const char *value, void *cb)
1308 struct perf_ftrace *ftrace = cb;
1310 if (!strstarts(var, "ftrace."))
1313 if (strcmp(var, "ftrace.tracer"))
1316 if (!strcmp(value, "function_graph") ||
1317 !strcmp(value, "function")) {
1318 ftrace->tracer = value;
1322 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
1326 static void list_function_cb(char *str, void *arg)
1328 struct strfilter *filter = (struct strfilter *)arg;
1330 if (strfilter__compare(filter, str))
1334 static int opt_list_avail_functions(const struct option *opt __maybe_unused,
1335 const char *str, int unset)
1337 struct strfilter *filter;
1338 const char *err = NULL;
1344 filter = strfilter__new(str, &err);
1346 return err ? -EINVAL : -ENOMEM;
1348 ret = strfilter__or(filter, str, &err);
1349 if (ret == -EINVAL) {
1350 pr_err("Filter parse error at %td.\n", err - str + 1);
1351 pr_err("Source: \"%s\"\n", str);
1352 pr_err(" %*c\n", (int)(err - str + 1), '^');
1353 strfilter__delete(filter);
1357 ret = read_tracing_file_by_line("available_filter_functions",
1358 list_function_cb, filter);
1359 strfilter__delete(filter);
1366 static int parse_filter_func(const struct option *opt, const char *str,
1367 int unset __maybe_unused)
1369 struct list_head *head = opt->value;
1370 struct filter_entry *entry;
1372 entry = malloc(sizeof(*entry) + strlen(str) + 1);
1376 strcpy(entry->name, str);
1377 list_add_tail(&entry->list, head);
1382 static void delete_filter_func(struct list_head *head)
1384 struct filter_entry *pos, *tmp;
1386 list_for_each_entry_safe(pos, tmp, head, list) {
1387 list_del_init(&pos->list);
1392 static int parse_buffer_size(const struct option *opt,
1393 const char *str, int unset)
1395 unsigned long *s = (unsigned long *)opt->value;
1396 static struct parse_tag tags_size[] = {
1397 { .tag = 'B', .mult = 1 },
1398 { .tag = 'K', .mult = 1 << 10 },
1399 { .tag = 'M', .mult = 1 << 20 },
1400 { .tag = 'G', .mult = 1 << 30 },
1410 val = parse_tag_value(str, tags_size);
1411 if (val != (unsigned long) -1) {
1413 pr_err("buffer size too small, must larger than 1KB.");
1423 static int parse_func_tracer_opts(const struct option *opt,
1424 const char *str, int unset)
1427 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
1428 struct sublevel_option func_tracer_opts[] = {
1429 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
1430 { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
1437 ret = perf_parse_sublevel_options(str, func_tracer_opts);
1444 static int parse_graph_tracer_opts(const struct option *opt,
1445 const char *str, int unset)
1448 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
1449 struct sublevel_option graph_tracer_opts[] = {
1450 { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
1451 { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
1452 { .name = "verbose", .value_ptr = &ftrace->graph_verbose },
1453 { .name = "thresh", .value_ptr = &ftrace->graph_thresh },
1454 { .name = "depth", .value_ptr = &ftrace->graph_depth },
1455 { .name = "tail", .value_ptr = &ftrace->graph_tail },
1462 ret = perf_parse_sublevel_options(str, graph_tracer_opts);
1469 static int parse_sort_key(const struct option *opt, const char *str, int unset)
1471 enum perf_ftrace_profile_sort_key *key = (void *)opt->value;
1476 if (!strcmp(str, "total"))
1477 *key = PFP_SORT_TOTAL;
1478 else if (!strcmp(str, "avg"))
1479 *key = PFP_SORT_AVG;
1480 else if (!strcmp(str, "max"))
1481 *key = PFP_SORT_MAX;
1482 else if (!strcmp(str, "count"))
1483 *key = PFP_SORT_COUNT;
1484 else if (!strcmp(str, "name"))
1485 *key = PFP_SORT_NAME;
1487 pr_err("Unknown sort key: %s\n", str);
1493 enum perf_ftrace_subcommand {
1496 PERF_FTRACE_LATENCY,
1497 PERF_FTRACE_PROFILE,
1500 int cmd_ftrace(int argc, const char **argv)
1503 int (*cmd_func)(struct perf_ftrace *) = NULL;
1504 struct perf_ftrace ftrace = {
1505 .tracer = DEFAULT_TRACER,
1506 .target = { .uid = UINT_MAX, },
1508 const struct option common_options[] = {
1509 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
1510 "Trace on existing process id"),
1511 /* TODO: Add short option -t after -t/--tracer can be removed. */
1512 OPT_STRING(0, "tid", &ftrace.target.tid, "tid",
1513 "Trace on existing thread id (exclusive to --pid)"),
1514 OPT_INCR('v', "verbose", &verbose,
1516 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
1517 "System-wide collection from all CPUs"),
1518 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
1519 "List of cpus to monitor"),
1522 const struct option ftrace_options[] = {
1523 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
1524 "Tracer to use: function_graph(default) or function"),
1525 OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
1526 "Show available functions to filter",
1527 opt_list_avail_functions, "*"),
1528 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
1529 "Trace given functions using function tracer",
1531 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
1532 "Do not trace given functions", parse_filter_func),
1533 OPT_CALLBACK(0, "func-opts", &ftrace, "options",
1534 "Function tracer options, available options: call-graph,irq-info",
1535 parse_func_tracer_opts),
1536 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
1537 "Trace given functions using function_graph tracer",
1539 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
1540 "Set nograph filter on given functions", parse_filter_func),
1541 OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
1542 "Graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>",
1543 parse_graph_tracer_opts),
1544 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
1545 "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
1546 OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
1547 "Trace children processes"),
1548 OPT_INTEGER('D', "delay", &ftrace.target.initial_delay,
1549 "Number of milliseconds to wait before starting tracing after program start"),
1550 OPT_PARENT(common_options),
1552 const struct option latency_options[] = {
1553 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
1554 "Show latency of given function", parse_filter_func),
1555 #ifdef HAVE_BPF_SKEL
1556 OPT_BOOLEAN('b', "use-bpf", &ftrace.target.use_bpf,
1557 "Use BPF to measure function latency"),
1559 OPT_BOOLEAN('n', "use-nsec", &ftrace.use_nsec,
1560 "Use nano-second histogram"),
1561 OPT_PARENT(common_options),
1563 const struct option profile_options[] = {
1564 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
1565 "Trace given functions using function tracer",
1567 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
1568 "Do not trace given functions", parse_filter_func),
1569 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
1570 "Trace given functions using function_graph tracer",
1572 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
1573 "Set nograph filter on given functions", parse_filter_func),
1574 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
1575 "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
1576 OPT_CALLBACK('s', "sort", &profile_sort, "key",
1577 "Sort result by key: total (default), avg, max, count, name.",
1579 OPT_PARENT(common_options),
1581 const struct option *options = ftrace_options;
1583 const char * const ftrace_usage[] = {
1584 "perf ftrace [<options>] [<command>]",
1585 "perf ftrace [<options>] -- [<command>] [<options>]",
1586 "perf ftrace {trace|latency|profile} [<options>] [<command>]",
1587 "perf ftrace {trace|latency|profile} [<options>] -- [<command>] [<options>]",
1590 enum perf_ftrace_subcommand subcmd = PERF_FTRACE_NONE;
1592 INIT_LIST_HEAD(&ftrace.filters);
1593 INIT_LIST_HEAD(&ftrace.notrace);
1594 INIT_LIST_HEAD(&ftrace.graph_funcs);
1595 INIT_LIST_HEAD(&ftrace.nograph_funcs);
1597 signal(SIGINT, sig_handler);
1598 signal(SIGUSR1, sig_handler);
1599 signal(SIGCHLD, sig_handler);
1600 signal(SIGPIPE, sig_handler);
1602 if (!check_ftrace_capable())
1605 if (!is_ftrace_supported()) {
1606 pr_err("ftrace is not supported on this system\n");
1610 ret = perf_config(perf_ftrace_config, &ftrace);
1615 if (!strcmp(argv[1], "trace")) {
1616 subcmd = PERF_FTRACE_TRACE;
1617 } else if (!strcmp(argv[1], "latency")) {
1618 subcmd = PERF_FTRACE_LATENCY;
1619 options = latency_options;
1620 } else if (!strcmp(argv[1], "profile")) {
1621 subcmd = PERF_FTRACE_PROFILE;
1622 options = profile_options;
1625 if (subcmd != PERF_FTRACE_NONE) {
1630 /* for backward compatibility */
1631 if (subcmd == PERF_FTRACE_NONE)
1632 subcmd = PERF_FTRACE_TRACE;
1634 argc = parse_options(argc, argv, options, ftrace_usage,
1635 PARSE_OPT_STOP_AT_NON_OPTION);
1638 goto out_delete_filters;
1641 /* Make system wide (-a) the default target. */
1642 if (!argc && target__none(&ftrace.target))
1643 ftrace.target.system_wide = true;
1646 case PERF_FTRACE_TRACE:
1647 cmd_func = __cmd_ftrace;
1649 case PERF_FTRACE_LATENCY:
1650 if (list_empty(&ftrace.filters)) {
1651 pr_err("Should provide a function to measure\n");
1652 parse_options_usage(ftrace_usage, options, "T", 1);
1654 goto out_delete_filters;
1656 cmd_func = __cmd_latency;
1658 case PERF_FTRACE_PROFILE:
1659 cmd_func = __cmd_profile;
1661 case PERF_FTRACE_NONE:
1663 pr_err("Invalid subcommand\n");
1665 goto out_delete_filters;
1668 ret = target__validate(&ftrace.target);
1672 target__strerror(&ftrace.target, ret, errbuf, 512);
1673 pr_err("%s\n", errbuf);
1674 goto out_delete_filters;
1677 ftrace.evlist = evlist__new();
1678 if (ftrace.evlist == NULL) {
1680 goto out_delete_filters;
1683 ret = evlist__create_maps(ftrace.evlist, &ftrace.target);
1685 goto out_delete_evlist;
1688 ret = evlist__prepare_workload(ftrace.evlist, &ftrace.target,
1690 ftrace__workload_exec_failed_signal);
1692 goto out_delete_evlist;
1695 ret = cmd_func(&ftrace);
1698 evlist__delete(ftrace.evlist);
1701 delete_filter_func(&ftrace.filters);
1702 delete_filter_func(&ftrace.notrace);
1703 delete_filter_func(&ftrace.graph_funcs);
1704 delete_filter_func(&ftrace.nograph_funcs);