Lines Matching +full:ftrace +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0-only
3 * builtin-ftrace.c
24 #include <subcmd/parse-options.h>
33 #include "util/ftrace.h"
35 #include "util/parse-sublevel-options.h"
58 workload_exec_errno = info->si_value.sival_int; in ftrace__workload_exec_failed_signal()
65 int fd, ret = -1; in __write_tracing_file()
66 ssize_t size = strlen(val); in __write_tracing_file() local
74 return -1; in __write_tracing_file()
96 val_copy[size] = '\n'; in __write_tracing_file()
98 if (write(fd, val_copy, size + 1) == size + 1) in __write_tracing_file()
127 int ret = -1; in read_tracing_file_to_stdout()
132 return -1; in read_tracing_file_to_stdout()
174 return -1; in read_tracing_file_by_line()
181 return -1; in read_tracing_file_by_line()
184 while (getline(&line, &len, fp) != -1) { in read_tracing_file_by_line()
202 return -1; in write_tracing_file_int()
213 return -1; in write_tracing_option_file()
223 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused) in reset_tracing_options()
225 write_tracing_option_file("function-fork", "0"); in reset_tracing_options()
227 write_tracing_option_file("sleep-time", "1"); in reset_tracing_options()
228 write_tracing_option_file("funcgraph-irqs", "1"); in reset_tracing_options()
229 write_tracing_option_file("funcgraph-proc", "0"); in reset_tracing_options()
230 write_tracing_option_file("funcgraph-abstime", "0"); in reset_tracing_options()
231 write_tracing_option_file("latency-format", "0"); in reset_tracing_options()
232 write_tracing_option_file("irq-info", "0"); in reset_tracing_options()
235 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) in reset_tracing_files()
238 return -1; in reset_tracing_files()
241 return -1; in reset_tracing_files()
244 return -1; in reset_tracing_files()
247 return -1; in reset_tracing_files()
250 return -1; in reset_tracing_files()
253 return -1; in reset_tracing_files()
256 reset_tracing_options(ftrace); in reset_tracing_files()
260 static int set_tracing_pid(struct perf_ftrace *ftrace) in set_tracing_pid() argument
265 if (target__has_cpu(&ftrace->target)) in set_tracing_pid()
268 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) { in set_tracing_pid()
270 perf_thread_map__pid(ftrace->evlist->core.threads, i)); in set_tracing_pid()
272 return -1; in set_tracing_pid()
284 last_cpu = perf_cpu_map__cpu(cpumap, perf_cpu_map__nr(cpumap) - 1).cpu; in set_tracing_cpumask()
291 return -1; in set_tracing_cpumask()
302 static int set_tracing_cpu(struct perf_ftrace *ftrace) in set_tracing_cpu() argument
304 struct perf_cpu_map *cpumap = ftrace->evlist->core.user_requested_cpus; in set_tracing_cpu()
306 if (!target__has_cpu(&ftrace->target)) in set_tracing_cpu()
312 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace) in set_tracing_func_stack_trace() argument
314 if (!ftrace->func_stack_trace) in set_tracing_func_stack_trace()
318 return -1; in set_tracing_func_stack_trace()
323 static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace) in set_tracing_func_irqinfo() argument
325 if (!ftrace->func_irq_info) in set_tracing_func_irqinfo()
328 if (write_tracing_option_file("irq-info", "1") < 0) in set_tracing_func_irqinfo()
329 return -1; in set_tracing_func_irqinfo()
349 if (append_tracing_file(filter_file, pos->name) < 0) in __set_tracing_filter()
350 return -1; in __set_tracing_filter()
356 static int set_tracing_filters(struct perf_ftrace *ftrace) in set_tracing_filters() argument
360 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters); in set_tracing_filters()
364 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace); in set_tracing_filters()
368 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs); in set_tracing_filters()
373 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs); in set_tracing_filters()
386 static int set_tracing_depth(struct perf_ftrace *ftrace) in set_tracing_depth() argument
388 if (ftrace->graph_depth == 0) in set_tracing_depth()
391 if (ftrace->graph_depth < 0) { in set_tracing_depth()
392 pr_err("invalid graph depth: %d\n", ftrace->graph_depth); in set_tracing_depth()
393 return -1; in set_tracing_depth()
396 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0) in set_tracing_depth()
397 return -1; in set_tracing_depth()
402 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace) in set_tracing_percpu_buffer_size() argument
406 if (ftrace->percpu_buffer_size == 0) in set_tracing_percpu_buffer_size()
410 ftrace->percpu_buffer_size / 1024); in set_tracing_percpu_buffer_size()
417 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace) in set_tracing_trace_inherit() argument
419 if (!ftrace->inherit) in set_tracing_trace_inherit()
422 if (write_tracing_option_file("function-fork", "1") < 0) in set_tracing_trace_inherit()
423 return -1; in set_tracing_trace_inherit()
428 static int set_tracing_sleep_time(struct perf_ftrace *ftrace) in set_tracing_sleep_time() argument
430 if (!ftrace->graph_nosleep_time) in set_tracing_sleep_time()
433 if (write_tracing_option_file("sleep-time", "0") < 0) in set_tracing_sleep_time()
434 return -1; in set_tracing_sleep_time()
439 static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace) in set_tracing_funcgraph_irqs() argument
441 if (!ftrace->graph_noirqs) in set_tracing_funcgraph_irqs()
444 if (write_tracing_option_file("funcgraph-irqs", "0") < 0) in set_tracing_funcgraph_irqs()
445 return -1; in set_tracing_funcgraph_irqs()
450 static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace) in set_tracing_funcgraph_verbose() argument
452 if (!ftrace->graph_verbose) in set_tracing_funcgraph_verbose()
455 if (write_tracing_option_file("funcgraph-proc", "1") < 0) in set_tracing_funcgraph_verbose()
456 return -1; in set_tracing_funcgraph_verbose()
458 if (write_tracing_option_file("funcgraph-abstime", "1") < 0) in set_tracing_funcgraph_verbose()
459 return -1; in set_tracing_funcgraph_verbose()
461 if (write_tracing_option_file("latency-format", "1") < 0) in set_tracing_funcgraph_verbose()
462 return -1; in set_tracing_funcgraph_verbose()
467 static int set_tracing_thresh(struct perf_ftrace *ftrace) in set_tracing_thresh() argument
471 if (ftrace->graph_thresh == 0) in set_tracing_thresh()
474 ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh); in set_tracing_thresh()
481 static int set_tracing_options(struct perf_ftrace *ftrace) in set_tracing_options() argument
483 if (set_tracing_pid(ftrace) < 0) { in set_tracing_options()
484 pr_err("failed to set ftrace pid\n"); in set_tracing_options()
485 return -1; in set_tracing_options()
488 if (set_tracing_cpu(ftrace) < 0) { in set_tracing_options()
490 return -1; in set_tracing_options()
493 if (set_tracing_func_stack_trace(ftrace) < 0) { in set_tracing_options()
495 return -1; in set_tracing_options()
498 if (set_tracing_func_irqinfo(ftrace) < 0) { in set_tracing_options()
499 pr_err("failed to set tracing option irq-info\n"); in set_tracing_options()
500 return -1; in set_tracing_options()
503 if (set_tracing_filters(ftrace) < 0) { in set_tracing_options()
505 return -1; in set_tracing_options()
508 if (set_tracing_depth(ftrace) < 0) { in set_tracing_options()
510 return -1; in set_tracing_options()
513 if (set_tracing_percpu_buffer_size(ftrace) < 0) { in set_tracing_options()
514 pr_err("failed to set tracing per-cpu buffer size\n"); in set_tracing_options()
515 return -1; in set_tracing_options()
518 if (set_tracing_trace_inherit(ftrace) < 0) { in set_tracing_options()
519 pr_err("failed to set tracing option function-fork\n"); in set_tracing_options()
520 return -1; in set_tracing_options()
523 if (set_tracing_sleep_time(ftrace) < 0) { in set_tracing_options()
524 pr_err("failed to set tracing option sleep-time\n"); in set_tracing_options()
525 return -1; in set_tracing_options()
528 if (set_tracing_funcgraph_irqs(ftrace) < 0) { in set_tracing_options()
529 pr_err("failed to set tracing option funcgraph-irqs\n"); in set_tracing_options()
530 return -1; in set_tracing_options()
533 if (set_tracing_funcgraph_verbose(ftrace) < 0) { in set_tracing_options()
534 pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n"); in set_tracing_options()
535 return -1; in set_tracing_options()
538 if (set_tracing_thresh(ftrace) < 0) { in set_tracing_options()
540 return -1; in set_tracing_options()
546 static void select_tracer(struct perf_ftrace *ftrace) in select_tracer() argument
548 bool graph = !list_empty(&ftrace->graph_funcs) || in select_tracer()
549 !list_empty(&ftrace->nograph_funcs); in select_tracer()
550 bool func = !list_empty(&ftrace->filters) || in select_tracer()
551 !list_empty(&ftrace->notrace); in select_tracer()
555 ftrace->tracer = "function_graph"; in select_tracer()
557 ftrace->tracer = "function"; in select_tracer()
560 pr_debug("%s tracer is used\n", ftrace->tracer); in select_tracer()
563 static int __cmd_ftrace(struct perf_ftrace *ftrace) in __cmd_ftrace() argument
574 pr_err("ftrace only works for %s!\n", in __cmd_ftrace()
581 return -1; in __cmd_ftrace()
584 select_tracer(ftrace); in __cmd_ftrace()
586 if (reset_tracing_files(ftrace) < 0) { in __cmd_ftrace()
587 pr_err("failed to reset ftrace\n"); in __cmd_ftrace()
591 /* reset ftrace buffer */ in __cmd_ftrace()
595 if (set_tracing_options(ftrace) < 0) in __cmd_ftrace()
598 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { in __cmd_ftrace()
599 pr_err("failed to set current_tracer to %s\n", ftrace->tracer); in __cmd_ftrace()
626 if (!ftrace->target.initial_delay) { in __cmd_ftrace()
633 evlist__start_workload(ftrace->evlist); in __cmd_ftrace()
635 if (ftrace->target.initial_delay > 0) { in __cmd_ftrace()
636 usleep(ftrace->target.initial_delay * 1000); in __cmd_ftrace()
644 if (poll(&pollfd, 1, -1) < 0) in __cmd_ftrace()
680 reset_tracing_files(ftrace); in __cmd_ftrace()
682 return (done && !workload_exec_errno) ? 0 : -1; in __cmd_ftrace()
740 i = NUM_BUCKET - 1; in make_histogram()
769 printf("# %14s | %10s | %-*s |\n", in display_histogram()
773 printf(" %4d - %-4d %s | %10d | %.*s%*s |\n", in display_histogram()
774 0, 1, use_nsec ? "ns" : "us", buckets[0], bar_len, bar, bar_total - bar_len, ""); in display_histogram()
776 for (i = 1; i < NUM_BUCKET - 1; i++) { in display_histogram()
777 int start = (1 << (i - 1)); in display_histogram()
787 printf(" %4d - %-4d %s | %10d | %.*s%*s |\n", in display_histogram()
789 bar_total - bar_len, ""); in display_histogram()
792 bar_len = buckets[NUM_BUCKET - 1] * bar_total / total; in display_histogram()
793 printf(" %4d - %-4s %s | %10d | %.*s%*s |\n", in display_histogram()
794 1, "...", use_nsec ? "ms" : " s", buckets[NUM_BUCKET - 1], in display_histogram()
795 bar_len, bar, bar_total - bar_len, ""); in display_histogram()
799 static int prepare_func_latency(struct perf_ftrace *ftrace) in prepare_func_latency() argument
804 if (ftrace->target.use_bpf) in prepare_func_latency()
805 return perf_ftrace__latency_prepare_bpf(ftrace); in prepare_func_latency()
807 if (reset_tracing_files(ftrace) < 0) { in prepare_func_latency()
808 pr_err("failed to reset ftrace\n"); in prepare_func_latency()
809 return -1; in prepare_func_latency()
812 /* reset ftrace buffer */ in prepare_func_latency()
814 return -1; in prepare_func_latency()
816 if (set_tracing_options(ftrace) < 0) in prepare_func_latency()
817 return -1; in prepare_func_latency()
822 return -1; in prepare_func_latency()
828 return -1; in prepare_func_latency()
839 static int start_func_latency(struct perf_ftrace *ftrace) in start_func_latency() argument
841 if (ftrace->target.use_bpf) in start_func_latency()
842 return perf_ftrace__latency_start_bpf(ftrace); in start_func_latency()
846 return -1; in start_func_latency()
852 static int stop_func_latency(struct perf_ftrace *ftrace) in stop_func_latency() argument
854 if (ftrace->target.use_bpf) in stop_func_latency()
855 return perf_ftrace__latency_stop_bpf(ftrace); in stop_func_latency()
861 static int read_func_latency(struct perf_ftrace *ftrace, int buckets[]) in read_func_latency() argument
863 if (ftrace->target.use_bpf) in read_func_latency()
864 return perf_ftrace__latency_read_bpf(ftrace, buckets); in read_func_latency()
869 static int cleanup_func_latency(struct perf_ftrace *ftrace) in cleanup_func_latency() argument
871 if (ftrace->target.use_bpf) in cleanup_func_latency()
872 return perf_ftrace__latency_cleanup_bpf(ftrace); in cleanup_func_latency()
874 reset_tracing_files(ftrace); in cleanup_func_latency()
878 static int __cmd_latency(struct perf_ftrace *ftrace) in __cmd_latency() argument
890 pr_err("ftrace only works for %s!\n", in __cmd_latency()
897 return -1; in __cmd_latency()
900 trace_fd = prepare_func_latency(ftrace); in __cmd_latency()
907 if (start_func_latency(ftrace) < 0) in __cmd_latency()
910 evlist__start_workload(ftrace->evlist); in __cmd_latency()
914 if (poll(&pollfd, 1, -1) < 0) in __cmd_latency()
918 int n = read(trace_fd, buf, sizeof(buf) - 1); in __cmd_latency()
922 make_histogram(buckets, buf, n, line, ftrace->use_nsec); in __cmd_latency()
926 stop_func_latency(ftrace); in __cmd_latency()
935 while (!ftrace->target.use_bpf) { in __cmd_latency()
936 int n = read(trace_fd, buf, sizeof(buf) - 1); in __cmd_latency()
939 make_histogram(buckets, buf, n, line, ftrace->use_nsec); in __cmd_latency()
942 read_func_latency(ftrace, buckets); in __cmd_latency()
944 display_histogram(buckets, ftrace->use_nsec); in __cmd_latency()
948 cleanup_func_latency(ftrace); in __cmd_latency()
950 return (done && !workload_exec_errno) ? 0 : -1; in __cmd_latency()
955 struct perf_ftrace *ftrace = cb; in perf_ftrace_config() local
957 if (!strstarts(var, "ftrace.")) in perf_ftrace_config()
960 if (strcmp(var, "ftrace.tracer")) in perf_ftrace_config()
961 return -1; in perf_ftrace_config()
965 ftrace->tracer = value; in perf_ftrace_config()
970 return -1; in perf_ftrace_config()
989 return -1; in opt_list_avail_functions()
993 return err ? -EINVAL : -ENOMEM; in opt_list_avail_functions()
996 if (ret == -EINVAL) { in opt_list_avail_functions()
997 pr_err("Filter parse error at %td.\n", err - str + 1); in opt_list_avail_functions()
999 pr_err(" %*c\n", (int)(err - str + 1), '^'); in opt_list_avail_functions()
1016 struct list_head *head = opt->value; in parse_filter_func()
1021 return -ENOMEM; in parse_filter_func()
1023 strcpy(entry->name, str); in parse_filter_func()
1024 list_add_tail(&entry->list, head); in parse_filter_func()
1034 list_del_init(&pos->list); in delete_filter_func()
1042 unsigned long *s = (unsigned long *)opt->value; in parse_buffer_size()
1058 if (val != (unsigned long) -1) { in parse_buffer_size()
1060 pr_err("buffer size too small, must larger than 1KB."); in parse_buffer_size()
1061 return -1; in parse_buffer_size()
1067 return -1; in parse_buffer_size()
1074 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; in parse_func_tracer_opts() local
1076 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace }, in parse_func_tracer_opts()
1077 { .name = "irq-info", .value_ptr = &ftrace->func_irq_info }, in parse_func_tracer_opts()
1095 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; in parse_graph_tracer_opts() local
1097 { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time }, in parse_graph_tracer_opts()
1098 { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs }, in parse_graph_tracer_opts()
1099 { .name = "verbose", .value_ptr = &ftrace->graph_verbose }, in parse_graph_tracer_opts()
1100 { .name = "thresh", .value_ptr = &ftrace->graph_thresh }, in parse_graph_tracer_opts()
1101 { .name = "depth", .value_ptr = &ftrace->graph_depth }, in parse_graph_tracer_opts()
1125 struct perf_ftrace ftrace = { in cmd_ftrace() local
1130 OPT_STRING('p', "pid", &ftrace.target.pid, "pid", in cmd_ftrace()
1132 /* TODO: Add short option -t after -t/--tracer can be removed. */ in cmd_ftrace()
1133 OPT_STRING(0, "tid", &ftrace.target.tid, "tid", in cmd_ftrace()
1134 "Trace on existing thread id (exclusive to --pid)"), in cmd_ftrace()
1137 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide, in cmd_ftrace()
1138 "System-wide collection from all CPUs"), in cmd_ftrace()
1139 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu", in cmd_ftrace()
1144 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", in cmd_ftrace()
1149 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", in cmd_ftrace()
1152 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func", in cmd_ftrace()
1154 OPT_CALLBACK(0, "func-opts", &ftrace, "options", in cmd_ftrace()
1155 "Function tracer options, available options: call-graph,irq-info", in cmd_ftrace()
1157 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func", in cmd_ftrace()
1160 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func", in cmd_ftrace()
1162 OPT_CALLBACK(0, "graph-opts", &ftrace, "options", in cmd_ftrace()
1163 "Graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>", in cmd_ftrace()
1165 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size", in cmd_ftrace()
1166 "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size), in cmd_ftrace()
1167 OPT_BOOLEAN(0, "inherit", &ftrace.inherit, in cmd_ftrace()
1169 OPT_INTEGER('D', "delay", &ftrace.target.initial_delay, in cmd_ftrace()
1174 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", in cmd_ftrace()
1177 OPT_BOOLEAN('b', "use-bpf", &ftrace.target.use_bpf, in cmd_ftrace()
1180 OPT_BOOLEAN('n', "use-nsec", &ftrace.use_nsec, in cmd_ftrace()
1181 "Use nano-second histogram"), in cmd_ftrace()
1187 "perf ftrace [<options>] [<command>]", in cmd_ftrace()
1188 "perf ftrace [<options>] -- [<command>] [<options>]", in cmd_ftrace()
1189 "perf ftrace {trace|latency} [<options>] [<command>]", in cmd_ftrace()
1190 "perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]", in cmd_ftrace()
1195 INIT_LIST_HEAD(&ftrace.filters); in cmd_ftrace()
1196 INIT_LIST_HEAD(&ftrace.notrace); in cmd_ftrace()
1197 INIT_LIST_HEAD(&ftrace.graph_funcs); in cmd_ftrace()
1198 INIT_LIST_HEAD(&ftrace.nograph_funcs); in cmd_ftrace()
1205 ret = perf_config(perf_ftrace_config, &ftrace); in cmd_ftrace()
1207 return -1; in cmd_ftrace()
1218 argc--; in cmd_ftrace()
1229 ret = -EINVAL; in cmd_ftrace()
1233 /* Make system wide (-a) the default target. */ in cmd_ftrace()
1234 if (!argc && target__none(&ftrace.target)) in cmd_ftrace()
1235 ftrace.target.system_wide = true; in cmd_ftrace()
1242 if (list_empty(&ftrace.filters)) { in cmd_ftrace()
1245 ret = -EINVAL; in cmd_ftrace()
1253 ret = -EINVAL; in cmd_ftrace()
1257 ret = target__validate(&ftrace.target); in cmd_ftrace()
1261 target__strerror(&ftrace.target, ret, errbuf, 512); in cmd_ftrace()
1266 ftrace.evlist = evlist__new(); in cmd_ftrace()
1267 if (ftrace.evlist == NULL) { in cmd_ftrace()
1268 ret = -ENOMEM; in cmd_ftrace()
1272 ret = evlist__create_maps(ftrace.evlist, &ftrace.target); in cmd_ftrace()
1277 ret = evlist__prepare_workload(ftrace.evlist, &ftrace.target, in cmd_ftrace()
1284 ret = cmd_func(&ftrace); in cmd_ftrace()
1287 evlist__delete(ftrace.evlist); in cmd_ftrace()
1290 delete_filter_func(&ftrace.filters); in cmd_ftrace()
1291 delete_filter_func(&ftrace.notrace); in cmd_ftrace()
1292 delete_filter_func(&ftrace.graph_funcs); in cmd_ftrace()
1293 delete_filter_func(&ftrace.nograph_funcs); in cmd_ftrace()