11c6bec5bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d01f4e8dSNamhyung Kim /*
3d01f4e8dSNamhyung Kim * builtin-ftrace.c
4d01f4e8dSNamhyung Kim *
5d01f4e8dSNamhyung Kim * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
60094024aSChangbin Du * Copyright (c) 2020 Changbin Du <changbin.du@gmail.com>, significant enhancement.
7d01f4e8dSNamhyung Kim */
8d01f4e8dSNamhyung Kim
9d01f4e8dSNamhyung Kim #include "builtin.h"
10d01f4e8dSNamhyung Kim
11a43783aeSArnaldo Carvalho de Melo #include <errno.h>
12d01f4e8dSNamhyung Kim #include <unistd.h>
13d01f4e8dSNamhyung Kim #include <signal.h>
14f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
15a9af6be5SNamhyung Kim #include <fcntl.h>
160f223813SNamhyung Kim #include <inttypes.h>
1753be5028SNamhyung Kim #include <math.h>
184208735dSArnaldo Carvalho de Melo #include <poll.h>
1953be5028SNamhyung Kim #include <ctype.h>
20c766f3dfSIgor Lubashev #include <linux/capability.h>
218520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
22d01f4e8dSNamhyung Kim
23d01f4e8dSNamhyung Kim #include "debug.h"
248520a98dSArnaldo Carvalho de Melo #include <subcmd/pager.h>
25d01f4e8dSNamhyung Kim #include <subcmd/parse-options.h>
260f223813SNamhyung Kim #include <api/io.h>
2720a9ed28SArnaldo Carvalho de Melo #include <api/fs/tracing_path.h>
28d01f4e8dSNamhyung Kim #include "evlist.h"
29d01f4e8dSNamhyung Kim #include "target.h"
30dc231032SNamhyung Kim #include "cpumap.h"
310f223813SNamhyung Kim #include "hashmap.h"
32d01f4e8dSNamhyung Kim #include "thread_map.h"
332ae05fe0SChangbin Du #include "strfilter.h"
34c766f3dfSIgor Lubashev #include "util/cap.h"
35b05d1093STaeung Song #include "util/config.h"
36177f4eacSNamhyung Kim #include "util/ftrace.h"
370f223813SNamhyung Kim #include "util/stat.h"
38846e1939SChangbin Du #include "util/units.h"
39b1d84af6SChangbin Du #include "util/parse-sublevel-options.h"
40d01f4e8dSNamhyung Kim
41d01f4e8dSNamhyung Kim #define DEFAULT_TRACER "function_graph"
42d01f4e8dSNamhyung Kim
43853596fbSIan Rogers static volatile sig_atomic_t workload_exec_errno;
44853596fbSIan Rogers static volatile sig_atomic_t done;
45d01f4e8dSNamhyung Kim
sig_handler(int sig __maybe_unused)46d01f4e8dSNamhyung Kim static void sig_handler(int sig __maybe_unused)
47d01f4e8dSNamhyung Kim {
48d01f4e8dSNamhyung Kim done = true;
49d01f4e8dSNamhyung Kim }
50d01f4e8dSNamhyung Kim
51d01f4e8dSNamhyung Kim /*
527b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
53d01f4e8dSNamhyung Kim * we asked by setting its exec_error to the function below,
54d01f4e8dSNamhyung Kim * ftrace__workload_exec_failed_signal.
55d01f4e8dSNamhyung Kim *
56d01f4e8dSNamhyung Kim * XXX We need to handle this more appropriately, emitting an error, etc.
57d01f4e8dSNamhyung Kim */
ftrace__workload_exec_failed_signal(int signo __maybe_unused,siginfo_t * info __maybe_unused,void * ucontext __maybe_unused)58d01f4e8dSNamhyung Kim static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
59d01f4e8dSNamhyung Kim siginfo_t *info __maybe_unused,
60d01f4e8dSNamhyung Kim void *ucontext __maybe_unused)
61d01f4e8dSNamhyung Kim {
6251a09d8fSChangbin Du workload_exec_errno = info->si_value.sival_int;
63d01f4e8dSNamhyung Kim done = true;
64d01f4e8dSNamhyung Kim }
65d01f4e8dSNamhyung Kim
check_ftrace_capable(void)66e25ebda7SIan Rogers static bool check_ftrace_capable(void)
67608585f4SNamhyung Kim {
68e25ebda7SIan Rogers bool used_root;
69e25ebda7SIan Rogers
70e25ebda7SIan Rogers if (perf_cap__capable(CAP_PERFMON, &used_root))
71e25ebda7SIan Rogers return true;
72e25ebda7SIan Rogers
73e25ebda7SIan Rogers if (!used_root && perf_cap__capable(CAP_SYS_ADMIN, &used_root))
74e25ebda7SIan Rogers return true;
75e25ebda7SIan Rogers
76608585f4SNamhyung Kim pr_err("ftrace only works for %s!\n",
77e25ebda7SIan Rogers used_root ? "root"
78e25ebda7SIan Rogers : "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
79608585f4SNamhyung Kim );
80e25ebda7SIan Rogers return false;
81608585f4SNamhyung Kim }
82608585f4SNamhyung Kim
is_ftrace_supported(void)83*74298dd8SChangbin Du static bool is_ftrace_supported(void)
84*74298dd8SChangbin Du {
85*74298dd8SChangbin Du char *file;
86*74298dd8SChangbin Du bool supported = false;
87*74298dd8SChangbin Du
88*74298dd8SChangbin Du file = get_tracing_file("set_ftrace_pid");
89*74298dd8SChangbin Du if (!file) {
90*74298dd8SChangbin Du pr_debug("cannot get tracing file set_ftrace_pid\n");
91*74298dd8SChangbin Du return false;
92*74298dd8SChangbin Du }
93*74298dd8SChangbin Du
94*74298dd8SChangbin Du if (!access(file, F_OK))
95*74298dd8SChangbin Du supported = true;
96*74298dd8SChangbin Du
97*74298dd8SChangbin Du put_tracing_file(file);
98*74298dd8SChangbin Du return supported;
99*74298dd8SChangbin Du }
100*74298dd8SChangbin Du
__write_tracing_file(const char * name,const char * val,bool append)101a9af6be5SNamhyung Kim static int __write_tracing_file(const char *name, const char *val, bool append)
102d01f4e8dSNamhyung Kim {
103d01f4e8dSNamhyung Kim char *file;
104d01f4e8dSNamhyung Kim int fd, ret = -1;
105d01f4e8dSNamhyung Kim ssize_t size = strlen(val);
106a9af6be5SNamhyung Kim int flags = O_WRONLY;
107e7bd9ba2SNamhyung Kim char errbuf[512];
10863cd02d8SChangbin Du char *val_copy;
109d01f4e8dSNamhyung Kim
110d01f4e8dSNamhyung Kim file = get_tracing_file(name);
111d01f4e8dSNamhyung Kim if (!file) {
112d01f4e8dSNamhyung Kim pr_debug("cannot get tracing file: %s\n", name);
113d01f4e8dSNamhyung Kim return -1;
114d01f4e8dSNamhyung Kim }
115d01f4e8dSNamhyung Kim
116a9af6be5SNamhyung Kim if (append)
117a9af6be5SNamhyung Kim flags |= O_APPEND;
118a9af6be5SNamhyung Kim else
119a9af6be5SNamhyung Kim flags |= O_TRUNC;
120a9af6be5SNamhyung Kim
121a9af6be5SNamhyung Kim fd = open(file, flags);
122d01f4e8dSNamhyung Kim if (fd < 0) {
123e7bd9ba2SNamhyung Kim pr_debug("cannot open tracing file: %s: %s\n",
124e7bd9ba2SNamhyung Kim name, str_error_r(errno, errbuf, sizeof(errbuf)));
125d01f4e8dSNamhyung Kim goto out;
126d01f4e8dSNamhyung Kim }
127d01f4e8dSNamhyung Kim
12863cd02d8SChangbin Du /*
12963cd02d8SChangbin Du * Copy the original value and append a '\n'. Without this,
13063cd02d8SChangbin Du * the kernel can hide possible errors.
13163cd02d8SChangbin Du */
13263cd02d8SChangbin Du val_copy = strdup(val);
13363cd02d8SChangbin Du if (!val_copy)
13463cd02d8SChangbin Du goto out_close;
13563cd02d8SChangbin Du val_copy[size] = '\n';
13663cd02d8SChangbin Du
13763cd02d8SChangbin Du if (write(fd, val_copy, size + 1) == size + 1)
138d01f4e8dSNamhyung Kim ret = 0;
139d01f4e8dSNamhyung Kim else
140e7bd9ba2SNamhyung Kim pr_debug("write '%s' to tracing/%s failed: %s\n",
141e7bd9ba2SNamhyung Kim val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
142d01f4e8dSNamhyung Kim
14363cd02d8SChangbin Du free(val_copy);
14463cd02d8SChangbin Du out_close:
145d01f4e8dSNamhyung Kim close(fd);
146d01f4e8dSNamhyung Kim out:
147d01f4e8dSNamhyung Kim put_tracing_file(file);
148d01f4e8dSNamhyung Kim return ret;
149d01f4e8dSNamhyung Kim }
150d01f4e8dSNamhyung Kim
write_tracing_file(const char * name,const char * val)151a9af6be5SNamhyung Kim static int write_tracing_file(const char *name, const char *val)
152a9af6be5SNamhyung Kim {
153a9af6be5SNamhyung Kim return __write_tracing_file(name, val, false);
154a9af6be5SNamhyung Kim }
155a9af6be5SNamhyung Kim
append_tracing_file(const char * name,const char * val)156a9af6be5SNamhyung Kim static int append_tracing_file(const char *name, const char *val)
157a9af6be5SNamhyung Kim {
158a9af6be5SNamhyung Kim return __write_tracing_file(name, val, true);
159a9af6be5SNamhyung Kim }
160a9af6be5SNamhyung Kim
read_tracing_file_to_stdout(const char * name)161d6d81bfeSChangbin Du static int read_tracing_file_to_stdout(const char *name)
162d6d81bfeSChangbin Du {
163d6d81bfeSChangbin Du char buf[4096];
164d6d81bfeSChangbin Du char *file;
165d6d81bfeSChangbin Du int fd;
166d6d81bfeSChangbin Du int ret = -1;
167d6d81bfeSChangbin Du
168d6d81bfeSChangbin Du file = get_tracing_file(name);
169d6d81bfeSChangbin Du if (!file) {
170d6d81bfeSChangbin Du pr_debug("cannot get tracing file: %s\n", name);
171d6d81bfeSChangbin Du return -1;
172d6d81bfeSChangbin Du }
173d6d81bfeSChangbin Du
174d6d81bfeSChangbin Du fd = open(file, O_RDONLY);
175d6d81bfeSChangbin Du if (fd < 0) {
176d6d81bfeSChangbin Du pr_debug("cannot open tracing file: %s: %s\n",
177d6d81bfeSChangbin Du name, str_error_r(errno, buf, sizeof(buf)));
178d6d81bfeSChangbin Du goto out;
179d6d81bfeSChangbin Du }
180d6d81bfeSChangbin Du
181d6d81bfeSChangbin Du /* read contents to stdout */
182d6d81bfeSChangbin Du while (true) {
183d6d81bfeSChangbin Du int n = read(fd, buf, sizeof(buf));
184d6d81bfeSChangbin Du if (n == 0)
185d6d81bfeSChangbin Du break;
186d6d81bfeSChangbin Du else if (n < 0)
187d6d81bfeSChangbin Du goto out_close;
188d6d81bfeSChangbin Du
189d6d81bfeSChangbin Du if (fwrite(buf, n, 1, stdout) != 1)
190d6d81bfeSChangbin Du goto out_close;
191d6d81bfeSChangbin Du }
192d6d81bfeSChangbin Du ret = 0;
193d6d81bfeSChangbin Du
194d6d81bfeSChangbin Du out_close:
195d6d81bfeSChangbin Du close(fd);
196d6d81bfeSChangbin Du out:
197d6d81bfeSChangbin Du put_tracing_file(file);
198d6d81bfeSChangbin Du return ret;
199d6d81bfeSChangbin Du }
200d6d81bfeSChangbin Du
read_tracing_file_by_line(const char * name,void (* cb)(char * str,void * arg),void * cb_arg)2012ae05fe0SChangbin Du static int read_tracing_file_by_line(const char *name,
2022ae05fe0SChangbin Du void (*cb)(char *str, void *arg),
2032ae05fe0SChangbin Du void *cb_arg)
2042ae05fe0SChangbin Du {
2052ae05fe0SChangbin Du char *line = NULL;
2062ae05fe0SChangbin Du size_t len = 0;
2072ae05fe0SChangbin Du char *file;
2082ae05fe0SChangbin Du FILE *fp;
2092ae05fe0SChangbin Du
2102ae05fe0SChangbin Du file = get_tracing_file(name);
2112ae05fe0SChangbin Du if (!file) {
2122ae05fe0SChangbin Du pr_debug("cannot get tracing file: %s\n", name);
2132ae05fe0SChangbin Du return -1;
2142ae05fe0SChangbin Du }
2152ae05fe0SChangbin Du
2162ae05fe0SChangbin Du fp = fopen(file, "r");
2172ae05fe0SChangbin Du if (fp == NULL) {
2182ae05fe0SChangbin Du pr_debug("cannot open tracing file: %s\n", name);
2192ae05fe0SChangbin Du put_tracing_file(file);
2202ae05fe0SChangbin Du return -1;
2212ae05fe0SChangbin Du }
2222ae05fe0SChangbin Du
2232ae05fe0SChangbin Du while (getline(&line, &len, fp) != -1) {
2242ae05fe0SChangbin Du cb(line, cb_arg);
2252ae05fe0SChangbin Du }
2262ae05fe0SChangbin Du
2272ae05fe0SChangbin Du if (line)
2282ae05fe0SChangbin Du free(line);
2292ae05fe0SChangbin Du
2302ae05fe0SChangbin Du fclose(fp);
2312ae05fe0SChangbin Du put_tracing_file(file);
2322ae05fe0SChangbin Du return 0;
2332ae05fe0SChangbin Du }
2342ae05fe0SChangbin Du
write_tracing_file_int(const char * name,int value)23568faab0fSChangbin Du static int write_tracing_file_int(const char *name, int value)
23668faab0fSChangbin Du {
23768faab0fSChangbin Du char buf[16];
23868faab0fSChangbin Du
23968faab0fSChangbin Du snprintf(buf, sizeof(buf), "%d", value);
24068faab0fSChangbin Du if (write_tracing_file(name, buf) < 0)
24168faab0fSChangbin Du return -1;
24268faab0fSChangbin Du
24368faab0fSChangbin Du return 0;
24468faab0fSChangbin Du }
24568faab0fSChangbin Du
write_tracing_option_file(const char * name,const char * val)2465b347472SChangbin Du static int write_tracing_option_file(const char *name, const char *val)
2475b347472SChangbin Du {
2485b347472SChangbin Du char *file;
2495b347472SChangbin Du int ret;
2505b347472SChangbin Du
2515b347472SChangbin Du if (asprintf(&file, "options/%s", name) < 0)
2525b347472SChangbin Du return -1;
2535b347472SChangbin Du
2545b347472SChangbin Du ret = __write_tracing_file(file, val, false);
2555b347472SChangbin Du free(file);
2565b347472SChangbin Du return ret;
2575b347472SChangbin Du }
2585b347472SChangbin Du
259dc231032SNamhyung Kim static int reset_tracing_cpu(void);
26078b83e8bSNamhyung Kim static void reset_tracing_filters(void);
261dc231032SNamhyung Kim
reset_tracing_options(struct perf_ftrace * ftrace __maybe_unused)2625b347472SChangbin Du static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
2635b347472SChangbin Du {
2645b347472SChangbin Du write_tracing_option_file("function-fork", "0");
265b1d84af6SChangbin Du write_tracing_option_file("func_stack_trace", "0");
26638988f2eSChangbin Du write_tracing_option_file("sleep-time", "1");
267d1bcf17cSChangbin Du write_tracing_option_file("funcgraph-irqs", "1");
26859486fb0SChangbin Du write_tracing_option_file("funcgraph-proc", "0");
26959486fb0SChangbin Du write_tracing_option_file("funcgraph-abstime", "0");
270c7780089SNamhyung Kim write_tracing_option_file("funcgraph-tail", "0");
27159486fb0SChangbin Du write_tracing_option_file("latency-format", "0");
272c81fc34eSChangbin Du write_tracing_option_file("irq-info", "0");
2735b347472SChangbin Du }
2745b347472SChangbin Du
reset_tracing_files(struct perf_ftrace * ftrace __maybe_unused)275d01f4e8dSNamhyung Kim static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
276d01f4e8dSNamhyung Kim {
277d01f4e8dSNamhyung Kim if (write_tracing_file("tracing_on", "0") < 0)
278d01f4e8dSNamhyung Kim return -1;
279d01f4e8dSNamhyung Kim
280d01f4e8dSNamhyung Kim if (write_tracing_file("current_tracer", "nop") < 0)
281d01f4e8dSNamhyung Kim return -1;
282d01f4e8dSNamhyung Kim
283d01f4e8dSNamhyung Kim if (write_tracing_file("set_ftrace_pid", " ") < 0)
284d01f4e8dSNamhyung Kim return -1;
285d01f4e8dSNamhyung Kim
286dc231032SNamhyung Kim if (reset_tracing_cpu() < 0)
287dc231032SNamhyung Kim return -1;
288dc231032SNamhyung Kim
2891096c35aSNamhyung Kim if (write_tracing_file("max_graph_depth", "0") < 0)
2901096c35aSNamhyung Kim return -1;
2911096c35aSNamhyung Kim
29200c85d5fSChangbin Du if (write_tracing_file("tracing_thresh", "0") < 0)
29300c85d5fSChangbin Du return -1;
29400c85d5fSChangbin Du
29578b83e8bSNamhyung Kim reset_tracing_filters();
2965b347472SChangbin Du reset_tracing_options(ftrace);
297d01f4e8dSNamhyung Kim return 0;
298d01f4e8dSNamhyung Kim }
299d01f4e8dSNamhyung Kim
set_tracing_pid(struct perf_ftrace * ftrace)300a9af6be5SNamhyung Kim static int set_tracing_pid(struct perf_ftrace *ftrace)
301a9af6be5SNamhyung Kim {
302a9af6be5SNamhyung Kim int i;
303a9af6be5SNamhyung Kim char buf[16];
304a9af6be5SNamhyung Kim
305a9af6be5SNamhyung Kim if (target__has_cpu(&ftrace->target))
306a9af6be5SNamhyung Kim return 0;
307a9af6be5SNamhyung Kim
308a2f354e3SJiri Olsa for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
309a9af6be5SNamhyung Kim scnprintf(buf, sizeof(buf), "%d",
310671b60cbSThomas Richter perf_thread_map__pid(ftrace->evlist->core.threads, i));
311a9af6be5SNamhyung Kim if (append_tracing_file("set_ftrace_pid", buf) < 0)
312a9af6be5SNamhyung Kim return -1;
313a9af6be5SNamhyung Kim }
314a9af6be5SNamhyung Kim return 0;
315a9af6be5SNamhyung Kim }
316a9af6be5SNamhyung Kim
set_tracing_cpumask(struct perf_cpu_map * cpumap)317f854839bSJiri Olsa static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
318dc231032SNamhyung Kim {
319dc231032SNamhyung Kim char *cpumask;
320dc231032SNamhyung Kim size_t mask_size;
321dc231032SNamhyung Kim int ret;
322dc231032SNamhyung Kim int last_cpu;
323dc231032SNamhyung Kim
32444028699SIan Rogers last_cpu = perf_cpu_map__cpu(cpumap, perf_cpu_map__nr(cpumap) - 1).cpu;
325cf30ae72SHe Zhe mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
326dc231032SNamhyung Kim mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
327dc231032SNamhyung Kim
328dc231032SNamhyung Kim cpumask = malloc(mask_size);
329dc231032SNamhyung Kim if (cpumask == NULL) {
330dc231032SNamhyung Kim pr_debug("failed to allocate cpu mask\n");
331dc231032SNamhyung Kim return -1;
332dc231032SNamhyung Kim }
333dc231032SNamhyung Kim
334dc231032SNamhyung Kim cpu_map__snprint_mask(cpumap, cpumask, mask_size);
335dc231032SNamhyung Kim
336dc231032SNamhyung Kim ret = write_tracing_file("tracing_cpumask", cpumask);
337dc231032SNamhyung Kim
338dc231032SNamhyung Kim free(cpumask);
339dc231032SNamhyung Kim return ret;
340dc231032SNamhyung Kim }
341dc231032SNamhyung Kim
set_tracing_cpu(struct perf_ftrace * ftrace)342dc231032SNamhyung Kim static int set_tracing_cpu(struct perf_ftrace *ftrace)
343dc231032SNamhyung Kim {
3440df6ade7SIan Rogers struct perf_cpu_map *cpumap = ftrace->evlist->core.user_requested_cpus;
345dc231032SNamhyung Kim
346dc231032SNamhyung Kim if (!target__has_cpu(&ftrace->target))
347dc231032SNamhyung Kim return 0;
348dc231032SNamhyung Kim
349dc231032SNamhyung Kim return set_tracing_cpumask(cpumap);
350dc231032SNamhyung Kim }
351dc231032SNamhyung Kim
set_tracing_func_stack_trace(struct perf_ftrace * ftrace)352b1d84af6SChangbin Du static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
353b1d84af6SChangbin Du {
354b1d84af6SChangbin Du if (!ftrace->func_stack_trace)
355b1d84af6SChangbin Du return 0;
356b1d84af6SChangbin Du
357b1d84af6SChangbin Du if (write_tracing_option_file("func_stack_trace", "1") < 0)
358b1d84af6SChangbin Du return -1;
359b1d84af6SChangbin Du
360b1d84af6SChangbin Du return 0;
361b1d84af6SChangbin Du }
362b1d84af6SChangbin Du
set_tracing_func_irqinfo(struct perf_ftrace * ftrace)363c81fc34eSChangbin Du static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
364c81fc34eSChangbin Du {
365c81fc34eSChangbin Du if (!ftrace->func_irq_info)
366c81fc34eSChangbin Du return 0;
367c81fc34eSChangbin Du
368c81fc34eSChangbin Du if (write_tracing_option_file("irq-info", "1") < 0)
369c81fc34eSChangbin Du return -1;
370c81fc34eSChangbin Du
371c81fc34eSChangbin Du return 0;
372c81fc34eSChangbin Du }
373c81fc34eSChangbin Du
reset_tracing_cpu(void)374dc231032SNamhyung Kim static int reset_tracing_cpu(void)
375dc231032SNamhyung Kim {
376effe957cSIan Rogers struct perf_cpu_map *cpumap = perf_cpu_map__new_online_cpus();
377dc231032SNamhyung Kim int ret;
378dc231032SNamhyung Kim
379dc231032SNamhyung Kim ret = set_tracing_cpumask(cpumap);
38038f01d8dSJiri Olsa perf_cpu_map__put(cpumap);
381dc231032SNamhyung Kim return ret;
382dc231032SNamhyung Kim }
383dc231032SNamhyung Kim
__set_tracing_filter(const char * filter_file,struct list_head * funcs)38478b83e8bSNamhyung Kim static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
38578b83e8bSNamhyung Kim {
38678b83e8bSNamhyung Kim struct filter_entry *pos;
38778b83e8bSNamhyung Kim
38878b83e8bSNamhyung Kim list_for_each_entry(pos, funcs, list) {
38978b83e8bSNamhyung Kim if (append_tracing_file(filter_file, pos->name) < 0)
39078b83e8bSNamhyung Kim return -1;
39178b83e8bSNamhyung Kim }
39278b83e8bSNamhyung Kim
39378b83e8bSNamhyung Kim return 0;
39478b83e8bSNamhyung Kim }
39578b83e8bSNamhyung Kim
set_tracing_filters(struct perf_ftrace * ftrace)39678b83e8bSNamhyung Kim static int set_tracing_filters(struct perf_ftrace *ftrace)
39778b83e8bSNamhyung Kim {
39878b83e8bSNamhyung Kim int ret;
39978b83e8bSNamhyung Kim
40078b83e8bSNamhyung Kim ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
40178b83e8bSNamhyung Kim if (ret < 0)
40278b83e8bSNamhyung Kim return ret;
40378b83e8bSNamhyung Kim
40478b83e8bSNamhyung Kim ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
40578b83e8bSNamhyung Kim if (ret < 0)
40678b83e8bSNamhyung Kim return ret;
40778b83e8bSNamhyung Kim
40878b83e8bSNamhyung Kim ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
40978b83e8bSNamhyung Kim if (ret < 0)
41078b83e8bSNamhyung Kim return ret;
41178b83e8bSNamhyung Kim
41278b83e8bSNamhyung Kim /* old kernels do not have this filter */
41378b83e8bSNamhyung Kim __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
41478b83e8bSNamhyung Kim
41578b83e8bSNamhyung Kim return ret;
41678b83e8bSNamhyung Kim }
41778b83e8bSNamhyung Kim
reset_tracing_filters(void)41878b83e8bSNamhyung Kim static void reset_tracing_filters(void)
41978b83e8bSNamhyung Kim {
42078b83e8bSNamhyung Kim write_tracing_file("set_ftrace_filter", " ");
42178b83e8bSNamhyung Kim write_tracing_file("set_ftrace_notrace", " ");
42278b83e8bSNamhyung Kim write_tracing_file("set_graph_function", " ");
42378b83e8bSNamhyung Kim write_tracing_file("set_graph_notrace", " ");
42478b83e8bSNamhyung Kim }
42578b83e8bSNamhyung Kim
set_tracing_depth(struct perf_ftrace * ftrace)4261096c35aSNamhyung Kim static int set_tracing_depth(struct perf_ftrace *ftrace)
4271096c35aSNamhyung Kim {
4281096c35aSNamhyung Kim if (ftrace->graph_depth == 0)
4291096c35aSNamhyung Kim return 0;
4301096c35aSNamhyung Kim
4311096c35aSNamhyung Kim if (ftrace->graph_depth < 0) {
4321096c35aSNamhyung Kim pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
4331096c35aSNamhyung Kim return -1;
4341096c35aSNamhyung Kim }
4351096c35aSNamhyung Kim
43668faab0fSChangbin Du if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
4371096c35aSNamhyung Kim return -1;
4381096c35aSNamhyung Kim
4391096c35aSNamhyung Kim return 0;
4401096c35aSNamhyung Kim }
4411096c35aSNamhyung Kim
set_tracing_percpu_buffer_size(struct perf_ftrace * ftrace)442846e1939SChangbin Du static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
443846e1939SChangbin Du {
444846e1939SChangbin Du int ret;
445846e1939SChangbin Du
446846e1939SChangbin Du if (ftrace->percpu_buffer_size == 0)
447846e1939SChangbin Du return 0;
448846e1939SChangbin Du
449846e1939SChangbin Du ret = write_tracing_file_int("buffer_size_kb",
450846e1939SChangbin Du ftrace->percpu_buffer_size / 1024);
451846e1939SChangbin Du if (ret < 0)
452846e1939SChangbin Du return ret;
453846e1939SChangbin Du
454846e1939SChangbin Du return 0;
455846e1939SChangbin Du }
456846e1939SChangbin Du
set_tracing_trace_inherit(struct perf_ftrace * ftrace)4575b347472SChangbin Du static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
4585b347472SChangbin Du {
4595b347472SChangbin Du if (!ftrace->inherit)
4605b347472SChangbin Du return 0;
4615b347472SChangbin Du
4625b347472SChangbin Du if (write_tracing_option_file("function-fork", "1") < 0)
4635b347472SChangbin Du return -1;
4645b347472SChangbin Du
4655b347472SChangbin Du return 0;
4665b347472SChangbin Du }
4675b347472SChangbin Du
set_tracing_sleep_time(struct perf_ftrace * ftrace)46838988f2eSChangbin Du static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
46938988f2eSChangbin Du {
47038988f2eSChangbin Du if (!ftrace->graph_nosleep_time)
47138988f2eSChangbin Du return 0;
47238988f2eSChangbin Du
47338988f2eSChangbin Du if (write_tracing_option_file("sleep-time", "0") < 0)
47438988f2eSChangbin Du return -1;
47538988f2eSChangbin Du
47638988f2eSChangbin Du return 0;
47738988f2eSChangbin Du }
47838988f2eSChangbin Du
set_tracing_funcgraph_irqs(struct perf_ftrace * ftrace)479d1bcf17cSChangbin Du static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
480d1bcf17cSChangbin Du {
481d1bcf17cSChangbin Du if (!ftrace->graph_noirqs)
482d1bcf17cSChangbin Du return 0;
483d1bcf17cSChangbin Du
484d1bcf17cSChangbin Du if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
485d1bcf17cSChangbin Du return -1;
486d1bcf17cSChangbin Du
487d1bcf17cSChangbin Du return 0;
488d1bcf17cSChangbin Du }
489d1bcf17cSChangbin Du
set_tracing_funcgraph_verbose(struct perf_ftrace * ftrace)49059486fb0SChangbin Du static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
49159486fb0SChangbin Du {
49259486fb0SChangbin Du if (!ftrace->graph_verbose)
49359486fb0SChangbin Du return 0;
49459486fb0SChangbin Du
49559486fb0SChangbin Du if (write_tracing_option_file("funcgraph-proc", "1") < 0)
49659486fb0SChangbin Du return -1;
49759486fb0SChangbin Du
49859486fb0SChangbin Du if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
49959486fb0SChangbin Du return -1;
50059486fb0SChangbin Du
50159486fb0SChangbin Du if (write_tracing_option_file("latency-format", "1") < 0)
50259486fb0SChangbin Du return -1;
50359486fb0SChangbin Du
50459486fb0SChangbin Du return 0;
50559486fb0SChangbin Du }
50659486fb0SChangbin Du
set_tracing_funcgraph_tail(struct perf_ftrace * ftrace)507c7780089SNamhyung Kim static int set_tracing_funcgraph_tail(struct perf_ftrace *ftrace)
508c7780089SNamhyung Kim {
509c7780089SNamhyung Kim if (!ftrace->graph_tail)
510c7780089SNamhyung Kim return 0;
511c7780089SNamhyung Kim
512c7780089SNamhyung Kim if (write_tracing_option_file("funcgraph-tail", "1") < 0)
513c7780089SNamhyung Kim return -1;
514c7780089SNamhyung Kim
515c7780089SNamhyung Kim return 0;
516c7780089SNamhyung Kim }
517c7780089SNamhyung Kim
set_tracing_thresh(struct perf_ftrace * ftrace)51800c85d5fSChangbin Du static int set_tracing_thresh(struct perf_ftrace *ftrace)
51900c85d5fSChangbin Du {
52000c85d5fSChangbin Du int ret;
52100c85d5fSChangbin Du
52200c85d5fSChangbin Du if (ftrace->graph_thresh == 0)
52300c85d5fSChangbin Du return 0;
52400c85d5fSChangbin Du
52500c85d5fSChangbin Du ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
52600c85d5fSChangbin Du if (ret < 0)
52700c85d5fSChangbin Du return ret;
52800c85d5fSChangbin Du
52900c85d5fSChangbin Du return 0;
53000c85d5fSChangbin Du }
53100c85d5fSChangbin Du
set_tracing_options(struct perf_ftrace * ftrace)5323c4dc21bSChangbin Du static int set_tracing_options(struct perf_ftrace *ftrace)
5333c4dc21bSChangbin Du {
5343c4dc21bSChangbin Du if (set_tracing_pid(ftrace) < 0) {
5353c4dc21bSChangbin Du pr_err("failed to set ftrace pid\n");
5363c4dc21bSChangbin Du return -1;
5373c4dc21bSChangbin Du }
5383c4dc21bSChangbin Du
5393c4dc21bSChangbin Du if (set_tracing_cpu(ftrace) < 0) {
5403c4dc21bSChangbin Du pr_err("failed to set tracing cpumask\n");
5413c4dc21bSChangbin Du return -1;
5423c4dc21bSChangbin Du }
5433c4dc21bSChangbin Du
5443c4dc21bSChangbin Du if (set_tracing_func_stack_trace(ftrace) < 0) {
5453c4dc21bSChangbin Du pr_err("failed to set tracing option func_stack_trace\n");
5463c4dc21bSChangbin Du return -1;
5473c4dc21bSChangbin Du }
5483c4dc21bSChangbin Du
5493c4dc21bSChangbin Du if (set_tracing_func_irqinfo(ftrace) < 0) {
5503c4dc21bSChangbin Du pr_err("failed to set tracing option irq-info\n");
5513c4dc21bSChangbin Du return -1;
5523c4dc21bSChangbin Du }
5533c4dc21bSChangbin Du
5543c4dc21bSChangbin Du if (set_tracing_filters(ftrace) < 0) {
5553c4dc21bSChangbin Du pr_err("failed to set tracing filters\n");
5563c4dc21bSChangbin Du return -1;
5573c4dc21bSChangbin Du }
5583c4dc21bSChangbin Du
5593c4dc21bSChangbin Du if (set_tracing_depth(ftrace) < 0) {
5603c4dc21bSChangbin Du pr_err("failed to set graph depth\n");
5613c4dc21bSChangbin Du return -1;
5623c4dc21bSChangbin Du }
5633c4dc21bSChangbin Du
5643c4dc21bSChangbin Du if (set_tracing_percpu_buffer_size(ftrace) < 0) {
5653c4dc21bSChangbin Du pr_err("failed to set tracing per-cpu buffer size\n");
5663c4dc21bSChangbin Du return -1;
5673c4dc21bSChangbin Du }
5683c4dc21bSChangbin Du
5693c4dc21bSChangbin Du if (set_tracing_trace_inherit(ftrace) < 0) {
5703c4dc21bSChangbin Du pr_err("failed to set tracing option function-fork\n");
5713c4dc21bSChangbin Du return -1;
5723c4dc21bSChangbin Du }
5733c4dc21bSChangbin Du
5743c4dc21bSChangbin Du if (set_tracing_sleep_time(ftrace) < 0) {
5753c4dc21bSChangbin Du pr_err("failed to set tracing option sleep-time\n");
5763c4dc21bSChangbin Du return -1;
5773c4dc21bSChangbin Du }
5783c4dc21bSChangbin Du
5793c4dc21bSChangbin Du if (set_tracing_funcgraph_irqs(ftrace) < 0) {
5803c4dc21bSChangbin Du pr_err("failed to set tracing option funcgraph-irqs\n");
5813c4dc21bSChangbin Du return -1;
5823c4dc21bSChangbin Du }
5833c4dc21bSChangbin Du
5843c4dc21bSChangbin Du if (set_tracing_funcgraph_verbose(ftrace) < 0) {
5853c4dc21bSChangbin Du pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
5863c4dc21bSChangbin Du return -1;
5873c4dc21bSChangbin Du }
5883c4dc21bSChangbin Du
5893c4dc21bSChangbin Du if (set_tracing_thresh(ftrace) < 0) {
5903c4dc21bSChangbin Du pr_err("failed to set tracing thresh\n");
5913c4dc21bSChangbin Du return -1;
5923c4dc21bSChangbin Du }
5933c4dc21bSChangbin Du
594c7780089SNamhyung Kim if (set_tracing_funcgraph_tail(ftrace) < 0) {
595c7780089SNamhyung Kim pr_err("failed to set tracing option funcgraph-tail\n");
596c7780089SNamhyung Kim return -1;
597c7780089SNamhyung Kim }
598c7780089SNamhyung Kim
5993c4dc21bSChangbin Du return 0;
6003c4dc21bSChangbin Du }
6013c4dc21bSChangbin Du
select_tracer(struct perf_ftrace * ftrace)602a9b8ae8aSNamhyung Kim static void select_tracer(struct perf_ftrace *ftrace)
603a9b8ae8aSNamhyung Kim {
604a9b8ae8aSNamhyung Kim bool graph = !list_empty(&ftrace->graph_funcs) ||
605a9b8ae8aSNamhyung Kim !list_empty(&ftrace->nograph_funcs);
606a9b8ae8aSNamhyung Kim bool func = !list_empty(&ftrace->filters) ||
607a9b8ae8aSNamhyung Kim !list_empty(&ftrace->notrace);
608a9b8ae8aSNamhyung Kim
609a9b8ae8aSNamhyung Kim /* The function_graph has priority over function tracer. */
610a9b8ae8aSNamhyung Kim if (graph)
611a9b8ae8aSNamhyung Kim ftrace->tracer = "function_graph";
612a9b8ae8aSNamhyung Kim else if (func)
613a9b8ae8aSNamhyung Kim ftrace->tracer = "function";
614a9b8ae8aSNamhyung Kim /* Otherwise, the default tracer is used. */
615a9b8ae8aSNamhyung Kim
616a9b8ae8aSNamhyung Kim pr_debug("%s tracer is used\n", ftrace->tracer);
617a9b8ae8aSNamhyung Kim }
618a9b8ae8aSNamhyung Kim
__cmd_ftrace(struct perf_ftrace * ftrace)619a9b8ae8aSNamhyung Kim static int __cmd_ftrace(struct perf_ftrace *ftrace)
620d01f4e8dSNamhyung Kim {
621d01f4e8dSNamhyung Kim char *trace_file;
622d01f4e8dSNamhyung Kim int trace_fd;
623d01f4e8dSNamhyung Kim char buf[4096];
624d01f4e8dSNamhyung Kim struct pollfd pollfd = {
625d01f4e8dSNamhyung Kim .events = POLLIN,
626d01f4e8dSNamhyung Kim };
627d01f4e8dSNamhyung Kim
628a9b8ae8aSNamhyung Kim select_tracer(ftrace);
629d01f4e8dSNamhyung Kim
63063cd02d8SChangbin Du if (reset_tracing_files(ftrace) < 0) {
63163cd02d8SChangbin Du pr_err("failed to reset ftrace\n");
632a9af6be5SNamhyung Kim goto out;
63363cd02d8SChangbin Du }
634d01f4e8dSNamhyung Kim
635d01f4e8dSNamhyung Kim /* reset ftrace buffer */
636d01f4e8dSNamhyung Kim if (write_tracing_file("trace", "0") < 0)
637d01f4e8dSNamhyung Kim goto out;
638d01f4e8dSNamhyung Kim
6393c4dc21bSChangbin Du if (set_tracing_options(ftrace) < 0)
640a9af6be5SNamhyung Kim goto out_reset;
64100c85d5fSChangbin Du
642d01f4e8dSNamhyung Kim if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
643d01f4e8dSNamhyung Kim pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
644a9af6be5SNamhyung Kim goto out_reset;
645d01f4e8dSNamhyung Kim }
646d01f4e8dSNamhyung Kim
64729681bc5SNamhyung Kim setup_pager();
64829681bc5SNamhyung Kim
649d01f4e8dSNamhyung Kim trace_file = get_tracing_file("trace_pipe");
650d01f4e8dSNamhyung Kim if (!trace_file) {
651d01f4e8dSNamhyung Kim pr_err("failed to open trace_pipe\n");
652a9af6be5SNamhyung Kim goto out_reset;
653d01f4e8dSNamhyung Kim }
654d01f4e8dSNamhyung Kim
655d01f4e8dSNamhyung Kim trace_fd = open(trace_file, O_RDONLY);
656d01f4e8dSNamhyung Kim
657d01f4e8dSNamhyung Kim put_tracing_file(trace_file);
658d01f4e8dSNamhyung Kim
659d01f4e8dSNamhyung Kim if (trace_fd < 0) {
660d01f4e8dSNamhyung Kim pr_err("failed to open trace_pipe\n");
661a9af6be5SNamhyung Kim goto out_reset;
662d01f4e8dSNamhyung Kim }
663d01f4e8dSNamhyung Kim
664d01f4e8dSNamhyung Kim fcntl(trace_fd, F_SETFL, O_NONBLOCK);
665d01f4e8dSNamhyung Kim pollfd.fd = trace_fd;
666d01f4e8dSNamhyung Kim
66781523c1eSChangbin Du /* display column headers */
66881523c1eSChangbin Du read_tracing_file_to_stdout("trace");
66981523c1eSChangbin Du
670f9f60efbSChangbin Du if (!ftrace->target.initial_delay) {
671d01f4e8dSNamhyung Kim if (write_tracing_file("tracing_on", "1") < 0) {
672d01f4e8dSNamhyung Kim pr_err("can't enable tracing\n");
673d01f4e8dSNamhyung Kim goto out_close_fd;
674d01f4e8dSNamhyung Kim }
6756555c2f6SChangbin Du }
676d01f4e8dSNamhyung Kim
6777b392ef0SArnaldo Carvalho de Melo evlist__start_workload(ftrace->evlist);
678d01f4e8dSNamhyung Kim
679f9f60efbSChangbin Du if (ftrace->target.initial_delay > 0) {
680f9f60efbSChangbin Du usleep(ftrace->target.initial_delay * 1000);
6816555c2f6SChangbin Du if (write_tracing_file("tracing_on", "1") < 0) {
6826555c2f6SChangbin Du pr_err("can't enable tracing\n");
6836555c2f6SChangbin Du goto out_close_fd;
6846555c2f6SChangbin Du }
6856555c2f6SChangbin Du }
6866555c2f6SChangbin Du
687d01f4e8dSNamhyung Kim while (!done) {
688d01f4e8dSNamhyung Kim if (poll(&pollfd, 1, -1) < 0)
689d01f4e8dSNamhyung Kim break;
690d01f4e8dSNamhyung Kim
691d01f4e8dSNamhyung Kim if (pollfd.revents & POLLIN) {
692d01f4e8dSNamhyung Kim int n = read(trace_fd, buf, sizeof(buf));
693d01f4e8dSNamhyung Kim if (n < 0)
694d01f4e8dSNamhyung Kim break;
695d01f4e8dSNamhyung Kim if (fwrite(buf, n, 1, stdout) != 1)
696d01f4e8dSNamhyung Kim break;
69740bf1cb0SChangbin Du /* flush output since stdout is in full buffering mode due to pager */
69840bf1cb0SChangbin Du fflush(stdout);
699d01f4e8dSNamhyung Kim }
700d01f4e8dSNamhyung Kim }
701d01f4e8dSNamhyung Kim
702d01f4e8dSNamhyung Kim write_tracing_file("tracing_on", "0");
703d01f4e8dSNamhyung Kim
70451a09d8fSChangbin Du if (workload_exec_errno) {
70551a09d8fSChangbin Du const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
70651a09d8fSChangbin Du /* flush stdout first so below error msg appears at the end. */
70751a09d8fSChangbin Du fflush(stdout);
70851a09d8fSChangbin Du pr_err("workload failed: %s\n", emsg);
70951a09d8fSChangbin Du goto out_close_fd;
71051a09d8fSChangbin Du }
71151a09d8fSChangbin Du
712d01f4e8dSNamhyung Kim /* read remaining buffer contents */
713d01f4e8dSNamhyung Kim while (true) {
714d01f4e8dSNamhyung Kim int n = read(trace_fd, buf, sizeof(buf));
715d01f4e8dSNamhyung Kim if (n <= 0)
716d01f4e8dSNamhyung Kim break;
717d01f4e8dSNamhyung Kim if (fwrite(buf, n, 1, stdout) != 1)
718d01f4e8dSNamhyung Kim break;
719d01f4e8dSNamhyung Kim }
720d01f4e8dSNamhyung Kim
721d01f4e8dSNamhyung Kim out_close_fd:
722d01f4e8dSNamhyung Kim close(trace_fd);
723a9af6be5SNamhyung Kim out_reset:
724d01f4e8dSNamhyung Kim reset_tracing_files(ftrace);
725a9af6be5SNamhyung Kim out:
72651a09d8fSChangbin Du return (done && !workload_exec_errno) ? 0 : -1;
727d01f4e8dSNamhyung Kim }
728d01f4e8dSNamhyung Kim
make_histogram(int buckets[],char * buf,size_t len,char * linebuf,bool use_nsec)72984005bb6SNamhyung Kim static void make_histogram(int buckets[], char *buf, size_t len, char *linebuf,
73084005bb6SNamhyung Kim bool use_nsec)
73153be5028SNamhyung Kim {
73253be5028SNamhyung Kim char *p, *q;
73353be5028SNamhyung Kim char *unit;
73453be5028SNamhyung Kim double num;
73553be5028SNamhyung Kim int i;
73653be5028SNamhyung Kim
73753be5028SNamhyung Kim /* ensure NUL termination */
73853be5028SNamhyung Kim buf[len] = '\0';
73953be5028SNamhyung Kim
74053be5028SNamhyung Kim /* handle data line by line */
74153be5028SNamhyung Kim for (p = buf; (q = strchr(p, '\n')) != NULL; p = q + 1) {
74253be5028SNamhyung Kim *q = '\0';
74353be5028SNamhyung Kim /* move it to the line buffer */
74453be5028SNamhyung Kim strcat(linebuf, p);
74553be5028SNamhyung Kim
74653be5028SNamhyung Kim /*
74753be5028SNamhyung Kim * parse trace output to get function duration like in
74853be5028SNamhyung Kim *
74953be5028SNamhyung Kim * # tracer: function_graph
75053be5028SNamhyung Kim * #
75153be5028SNamhyung Kim * # CPU DURATION FUNCTION CALLS
75253be5028SNamhyung Kim * # | | | | | | |
75353be5028SNamhyung Kim * 1) + 10.291 us | do_filp_open();
75453be5028SNamhyung Kim * 1) 4.889 us | do_filp_open();
75553be5028SNamhyung Kim * 1) 6.086 us | do_filp_open();
75653be5028SNamhyung Kim *
75753be5028SNamhyung Kim */
75853be5028SNamhyung Kim if (linebuf[0] == '#')
75953be5028SNamhyung Kim goto next;
76053be5028SNamhyung Kim
76153be5028SNamhyung Kim /* ignore CPU */
76253be5028SNamhyung Kim p = strchr(linebuf, ')');
76353be5028SNamhyung Kim if (p == NULL)
76453be5028SNamhyung Kim p = linebuf;
76553be5028SNamhyung Kim
76653be5028SNamhyung Kim while (*p && !isdigit(*p) && (*p != '|'))
76753be5028SNamhyung Kim p++;
76853be5028SNamhyung Kim
76953be5028SNamhyung Kim /* no duration */
77053be5028SNamhyung Kim if (*p == '\0' || *p == '|')
77153be5028SNamhyung Kim goto next;
77253be5028SNamhyung Kim
77353be5028SNamhyung Kim num = strtod(p, &unit);
77453be5028SNamhyung Kim if (!unit || strncmp(unit, " us", 3))
77553be5028SNamhyung Kim goto next;
77653be5028SNamhyung Kim
77784005bb6SNamhyung Kim if (use_nsec)
77884005bb6SNamhyung Kim num *= 1000;
77984005bb6SNamhyung Kim
78053be5028SNamhyung Kim i = log2(num);
78153be5028SNamhyung Kim if (i < 0)
78253be5028SNamhyung Kim i = 0;
78353be5028SNamhyung Kim if (i >= NUM_BUCKET)
78453be5028SNamhyung Kim i = NUM_BUCKET - 1;
78553be5028SNamhyung Kim
78653be5028SNamhyung Kim buckets[i]++;
78753be5028SNamhyung Kim
78853be5028SNamhyung Kim next:
78953be5028SNamhyung Kim /* empty the line buffer for the next output */
79053be5028SNamhyung Kim linebuf[0] = '\0';
79153be5028SNamhyung Kim }
79253be5028SNamhyung Kim
79353be5028SNamhyung Kim /* preserve any remaining output (before newline) */
79453be5028SNamhyung Kim strcat(linebuf, p);
79553be5028SNamhyung Kim }
79653be5028SNamhyung Kim
display_histogram(int buckets[],bool use_nsec)79784005bb6SNamhyung Kim static void display_histogram(int buckets[], bool use_nsec)
79853be5028SNamhyung Kim {
79953be5028SNamhyung Kim int i;
80053be5028SNamhyung Kim int total = 0;
80153be5028SNamhyung Kim int bar_total = 46; /* to fit in 80 column */
80253be5028SNamhyung Kim char bar[] = "###############################################";
80353be5028SNamhyung Kim int bar_len;
80453be5028SNamhyung Kim
80553be5028SNamhyung Kim for (i = 0; i < NUM_BUCKET; i++)
80653be5028SNamhyung Kim total += buckets[i];
80753be5028SNamhyung Kim
80853be5028SNamhyung Kim if (total == 0) {
80953be5028SNamhyung Kim printf("No data found\n");
81053be5028SNamhyung Kim return;
81153be5028SNamhyung Kim }
81253be5028SNamhyung Kim
81353be5028SNamhyung Kim printf("# %14s | %10s | %-*s |\n",
81453be5028SNamhyung Kim " DURATION ", "COUNT", bar_total, "GRAPH");
81553be5028SNamhyung Kim
81653be5028SNamhyung Kim bar_len = buckets[0] * bar_total / total;
81753be5028SNamhyung Kim printf(" %4d - %-4d %s | %10d | %.*s%*s |\n",
81853be5028SNamhyung Kim 0, 1, "us", buckets[0], bar_len, bar, bar_total - bar_len, "");
81953be5028SNamhyung Kim
82053be5028SNamhyung Kim for (i = 1; i < NUM_BUCKET - 1; i++) {
82153be5028SNamhyung Kim int start = (1 << (i - 1));
82253be5028SNamhyung Kim int stop = 1 << i;
82384005bb6SNamhyung Kim const char *unit = use_nsec ? "ns" : "us";
82453be5028SNamhyung Kim
82553be5028SNamhyung Kim if (start >= 1024) {
82653be5028SNamhyung Kim start >>= 10;
82753be5028SNamhyung Kim stop >>= 10;
82884005bb6SNamhyung Kim unit = use_nsec ? "us" : "ms";
82953be5028SNamhyung Kim }
83053be5028SNamhyung Kim bar_len = buckets[i] * bar_total / total;
83153be5028SNamhyung Kim printf(" %4d - %-4d %s | %10d | %.*s%*s |\n",
83253be5028SNamhyung Kim start, stop, unit, buckets[i], bar_len, bar,
83353be5028SNamhyung Kim bar_total - bar_len, "");
83453be5028SNamhyung Kim }
83553be5028SNamhyung Kim
83653be5028SNamhyung Kim bar_len = buckets[NUM_BUCKET - 1] * bar_total / total;
83753be5028SNamhyung Kim printf(" %4d - %-4s %s | %10d | %.*s%*s |\n",
83884005bb6SNamhyung Kim 1, "...", use_nsec ? "ms" : " s", buckets[NUM_BUCKET - 1],
83984005bb6SNamhyung Kim bar_len, bar, bar_total - bar_len, "");
84053be5028SNamhyung Kim
84153be5028SNamhyung Kim }
84253be5028SNamhyung Kim
prepare_func_latency(struct perf_ftrace * ftrace)843177f4eacSNamhyung Kim static int prepare_func_latency(struct perf_ftrace *ftrace)
84453be5028SNamhyung Kim {
84553be5028SNamhyung Kim char *trace_file;
846177f4eacSNamhyung Kim int fd;
847177f4eacSNamhyung Kim
848177f4eacSNamhyung Kim if (ftrace->target.use_bpf)
849177f4eacSNamhyung Kim return perf_ftrace__latency_prepare_bpf(ftrace);
850177f4eacSNamhyung Kim
851177f4eacSNamhyung Kim if (reset_tracing_files(ftrace) < 0) {
852177f4eacSNamhyung Kim pr_err("failed to reset ftrace\n");
853177f4eacSNamhyung Kim return -1;
854177f4eacSNamhyung Kim }
855177f4eacSNamhyung Kim
856177f4eacSNamhyung Kim /* reset ftrace buffer */
857177f4eacSNamhyung Kim if (write_tracing_file("trace", "0") < 0)
858177f4eacSNamhyung Kim return -1;
859177f4eacSNamhyung Kim
860177f4eacSNamhyung Kim if (set_tracing_options(ftrace) < 0)
861177f4eacSNamhyung Kim return -1;
862177f4eacSNamhyung Kim
863177f4eacSNamhyung Kim /* force to use the function_graph tracer to track duration */
864177f4eacSNamhyung Kim if (write_tracing_file("current_tracer", "function_graph") < 0) {
865177f4eacSNamhyung Kim pr_err("failed to set current_tracer to function_graph\n");
866177f4eacSNamhyung Kim return -1;
867177f4eacSNamhyung Kim }
868177f4eacSNamhyung Kim
869177f4eacSNamhyung Kim trace_file = get_tracing_file("trace_pipe");
870177f4eacSNamhyung Kim if (!trace_file) {
871177f4eacSNamhyung Kim pr_err("failed to open trace_pipe\n");
872177f4eacSNamhyung Kim return -1;
873177f4eacSNamhyung Kim }
874177f4eacSNamhyung Kim
875177f4eacSNamhyung Kim fd = open(trace_file, O_RDONLY);
876177f4eacSNamhyung Kim if (fd < 0)
877177f4eacSNamhyung Kim pr_err("failed to open trace_pipe\n");
878177f4eacSNamhyung Kim
879177f4eacSNamhyung Kim put_tracing_file(trace_file);
880177f4eacSNamhyung Kim return fd;
881177f4eacSNamhyung Kim }
882177f4eacSNamhyung Kim
start_func_latency(struct perf_ftrace * ftrace)883177f4eacSNamhyung Kim static int start_func_latency(struct perf_ftrace *ftrace)
884177f4eacSNamhyung Kim {
885177f4eacSNamhyung Kim if (ftrace->target.use_bpf)
886177f4eacSNamhyung Kim return perf_ftrace__latency_start_bpf(ftrace);
887177f4eacSNamhyung Kim
888177f4eacSNamhyung Kim if (write_tracing_file("tracing_on", "1") < 0) {
889177f4eacSNamhyung Kim pr_err("can't enable tracing\n");
890177f4eacSNamhyung Kim return -1;
891177f4eacSNamhyung Kim }
892177f4eacSNamhyung Kim
893177f4eacSNamhyung Kim return 0;
894177f4eacSNamhyung Kim }
895177f4eacSNamhyung Kim
stop_func_latency(struct perf_ftrace * ftrace)896177f4eacSNamhyung Kim static int stop_func_latency(struct perf_ftrace *ftrace)
897177f4eacSNamhyung Kim {
898177f4eacSNamhyung Kim if (ftrace->target.use_bpf)
899177f4eacSNamhyung Kim return perf_ftrace__latency_stop_bpf(ftrace);
900177f4eacSNamhyung Kim
901177f4eacSNamhyung Kim write_tracing_file("tracing_on", "0");
902177f4eacSNamhyung Kim return 0;
903177f4eacSNamhyung Kim }
904177f4eacSNamhyung Kim
read_func_latency(struct perf_ftrace * ftrace,int buckets[])905177f4eacSNamhyung Kim static int read_func_latency(struct perf_ftrace *ftrace, int buckets[])
906177f4eacSNamhyung Kim {
907177f4eacSNamhyung Kim if (ftrace->target.use_bpf)
908177f4eacSNamhyung Kim return perf_ftrace__latency_read_bpf(ftrace, buckets);
909177f4eacSNamhyung Kim
910177f4eacSNamhyung Kim return 0;
911177f4eacSNamhyung Kim }
912177f4eacSNamhyung Kim
cleanup_func_latency(struct perf_ftrace * ftrace)913177f4eacSNamhyung Kim static int cleanup_func_latency(struct perf_ftrace *ftrace)
914177f4eacSNamhyung Kim {
915177f4eacSNamhyung Kim if (ftrace->target.use_bpf)
916177f4eacSNamhyung Kim return perf_ftrace__latency_cleanup_bpf(ftrace);
917177f4eacSNamhyung Kim
918177f4eacSNamhyung Kim reset_tracing_files(ftrace);
919177f4eacSNamhyung Kim return 0;
920177f4eacSNamhyung Kim }
921177f4eacSNamhyung Kim
__cmd_latency(struct perf_ftrace * ftrace)922177f4eacSNamhyung Kim static int __cmd_latency(struct perf_ftrace *ftrace)
923177f4eacSNamhyung Kim {
92453be5028SNamhyung Kim int trace_fd;
92553be5028SNamhyung Kim char buf[4096];
92653be5028SNamhyung Kim char line[256];
92753be5028SNamhyung Kim struct pollfd pollfd = {
92853be5028SNamhyung Kim .events = POLLIN,
92953be5028SNamhyung Kim };
93053be5028SNamhyung Kim int buckets[NUM_BUCKET] = { };
93153be5028SNamhyung Kim
932177f4eacSNamhyung Kim trace_fd = prepare_func_latency(ftrace);
933177f4eacSNamhyung Kim if (trace_fd < 0)
93453be5028SNamhyung Kim goto out;
93553be5028SNamhyung Kim
93653be5028SNamhyung Kim fcntl(trace_fd, F_SETFL, O_NONBLOCK);
93753be5028SNamhyung Kim pollfd.fd = trace_fd;
93853be5028SNamhyung Kim
939177f4eacSNamhyung Kim if (start_func_latency(ftrace) < 0)
940177f4eacSNamhyung Kim goto out;
94153be5028SNamhyung Kim
94253be5028SNamhyung Kim evlist__start_workload(ftrace->evlist);
94353be5028SNamhyung Kim
94453be5028SNamhyung Kim line[0] = '\0';
94553be5028SNamhyung Kim while (!done) {
94653be5028SNamhyung Kim if (poll(&pollfd, 1, -1) < 0)
94753be5028SNamhyung Kim break;
94853be5028SNamhyung Kim
94953be5028SNamhyung Kim if (pollfd.revents & POLLIN) {
95053be5028SNamhyung Kim int n = read(trace_fd, buf, sizeof(buf) - 1);
95153be5028SNamhyung Kim if (n < 0)
95253be5028SNamhyung Kim break;
95353be5028SNamhyung Kim
95484005bb6SNamhyung Kim make_histogram(buckets, buf, n, line, ftrace->use_nsec);
95553be5028SNamhyung Kim }
95653be5028SNamhyung Kim }
95753be5028SNamhyung Kim
958177f4eacSNamhyung Kim stop_func_latency(ftrace);
95953be5028SNamhyung Kim
96053be5028SNamhyung Kim if (workload_exec_errno) {
96153be5028SNamhyung Kim const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
96253be5028SNamhyung Kim pr_err("workload failed: %s\n", emsg);
963177f4eacSNamhyung Kim goto out;
96453be5028SNamhyung Kim }
96553be5028SNamhyung Kim
96653be5028SNamhyung Kim /* read remaining buffer contents */
967177f4eacSNamhyung Kim while (!ftrace->target.use_bpf) {
96853be5028SNamhyung Kim int n = read(trace_fd, buf, sizeof(buf) - 1);
96953be5028SNamhyung Kim if (n <= 0)
97053be5028SNamhyung Kim break;
97184005bb6SNamhyung Kim make_histogram(buckets, buf, n, line, ftrace->use_nsec);
97253be5028SNamhyung Kim }
97353be5028SNamhyung Kim
974177f4eacSNamhyung Kim read_func_latency(ftrace, buckets);
975177f4eacSNamhyung Kim
97684005bb6SNamhyung Kim display_histogram(buckets, ftrace->use_nsec);
97753be5028SNamhyung Kim
97853be5028SNamhyung Kim out:
979177f4eacSNamhyung Kim close(trace_fd);
980177f4eacSNamhyung Kim cleanup_func_latency(ftrace);
981177f4eacSNamhyung Kim
98253be5028SNamhyung Kim return (done && !workload_exec_errno) ? 0 : -1;
98353be5028SNamhyung Kim }
98453be5028SNamhyung Kim
profile_hash(long func,void * ctx __maybe_unused)9850f223813SNamhyung Kim static size_t profile_hash(long func, void *ctx __maybe_unused)
9860f223813SNamhyung Kim {
9870f223813SNamhyung Kim return str_hash((char *)func);
9880f223813SNamhyung Kim }
9890f223813SNamhyung Kim
profile_equal(long func1,long func2,void * ctx __maybe_unused)9900f223813SNamhyung Kim static bool profile_equal(long func1, long func2, void *ctx __maybe_unused)
9910f223813SNamhyung Kim {
9920f223813SNamhyung Kim return !strcmp((char *)func1, (char *)func2);
9930f223813SNamhyung Kim }
9940f223813SNamhyung Kim
prepare_func_profile(struct perf_ftrace * ftrace)9950f223813SNamhyung Kim static int prepare_func_profile(struct perf_ftrace *ftrace)
9960f223813SNamhyung Kim {
9970f223813SNamhyung Kim ftrace->tracer = "function_graph";
9980f223813SNamhyung Kim ftrace->graph_tail = 1;
9990f223813SNamhyung Kim
10000f223813SNamhyung Kim ftrace->profile_hash = hashmap__new(profile_hash, profile_equal, NULL);
10010f223813SNamhyung Kim if (ftrace->profile_hash == NULL)
10020f223813SNamhyung Kim return -ENOMEM;
10030f223813SNamhyung Kim
10040f223813SNamhyung Kim return 0;
10050f223813SNamhyung Kim }
10060f223813SNamhyung Kim
10070f223813SNamhyung Kim /* This is saved in a hashmap keyed by the function name */
10080f223813SNamhyung Kim struct ftrace_profile_data {
10090f223813SNamhyung Kim struct stats st;
10100f223813SNamhyung Kim };
10110f223813SNamhyung Kim
add_func_duration(struct perf_ftrace * ftrace,char * func,double time_ns)10120f223813SNamhyung Kim static int add_func_duration(struct perf_ftrace *ftrace, char *func, double time_ns)
10130f223813SNamhyung Kim {
10140f223813SNamhyung Kim struct ftrace_profile_data *prof = NULL;
10150f223813SNamhyung Kim
10160f223813SNamhyung Kim if (!hashmap__find(ftrace->profile_hash, func, &prof)) {
10170f223813SNamhyung Kim char *key = strdup(func);
10180f223813SNamhyung Kim
10190f223813SNamhyung Kim if (key == NULL)
10200f223813SNamhyung Kim return -ENOMEM;
10210f223813SNamhyung Kim
10220f223813SNamhyung Kim prof = zalloc(sizeof(*prof));
10230f223813SNamhyung Kim if (prof == NULL) {
10240f223813SNamhyung Kim free(key);
10250f223813SNamhyung Kim return -ENOMEM;
10260f223813SNamhyung Kim }
10270f223813SNamhyung Kim
10280f223813SNamhyung Kim init_stats(&prof->st);
10290f223813SNamhyung Kim hashmap__add(ftrace->profile_hash, key, prof);
10300f223813SNamhyung Kim }
10310f223813SNamhyung Kim
10320f223813SNamhyung Kim update_stats(&prof->st, time_ns);
10330f223813SNamhyung Kim return 0;
10340f223813SNamhyung Kim }
10350f223813SNamhyung Kim
10360f223813SNamhyung Kim /*
10370f223813SNamhyung Kim * The ftrace function_graph text output normally looks like below:
10380f223813SNamhyung Kim *
10390f223813SNamhyung Kim * CPU DURATION FUNCTION
10400f223813SNamhyung Kim *
10410f223813SNamhyung Kim * 0) | syscall_trace_enter.isra.0() {
10420f223813SNamhyung Kim * 0) | __audit_syscall_entry() {
10430f223813SNamhyung Kim * 0) | auditd_test_task() {
10440f223813SNamhyung Kim * 0) 0.271 us | __rcu_read_lock();
10450f223813SNamhyung Kim * 0) 0.275 us | __rcu_read_unlock();
10460f223813SNamhyung Kim * 0) 1.254 us | } /\* auditd_test_task *\/
10470f223813SNamhyung Kim * 0) 0.279 us | ktime_get_coarse_real_ts64();
10480f223813SNamhyung Kim * 0) 2.227 us | } /\* __audit_syscall_entry *\/
10490f223813SNamhyung Kim * 0) 2.713 us | } /\* syscall_trace_enter.isra.0 *\/
10500f223813SNamhyung Kim *
10510f223813SNamhyung Kim * Parse the line and get the duration and function name.
10520f223813SNamhyung Kim */
parse_func_duration(struct perf_ftrace * ftrace,char * line,size_t len)10530f223813SNamhyung Kim static int parse_func_duration(struct perf_ftrace *ftrace, char *line, size_t len)
10540f223813SNamhyung Kim {
10550f223813SNamhyung Kim char *p;
10560f223813SNamhyung Kim char *func;
10570f223813SNamhyung Kim double duration;
10580f223813SNamhyung Kim
10590f223813SNamhyung Kim /* skip CPU */
10600f223813SNamhyung Kim p = strchr(line, ')');
10610f223813SNamhyung Kim if (p == NULL)
10620f223813SNamhyung Kim return 0;
10630f223813SNamhyung Kim
10640f223813SNamhyung Kim /* get duration */
10650f223813SNamhyung Kim p = skip_spaces(p + 1);
10660f223813SNamhyung Kim
10670f223813SNamhyung Kim /* no duration? */
10680f223813SNamhyung Kim if (p == NULL || *p == '|')
10690f223813SNamhyung Kim return 0;
10700f223813SNamhyung Kim
10710f223813SNamhyung Kim /* skip markers like '*' or '!' for longer than ms */
10720f223813SNamhyung Kim if (!isdigit(*p))
10730f223813SNamhyung Kim p++;
10740f223813SNamhyung Kim
10750f223813SNamhyung Kim duration = strtod(p, &p);
10760f223813SNamhyung Kim
10770f223813SNamhyung Kim if (strncmp(p, " us", 3)) {
10780f223813SNamhyung Kim pr_debug("non-usec time found.. ignoring\n");
10790f223813SNamhyung Kim return 0;
10800f223813SNamhyung Kim }
10810f223813SNamhyung Kim
10820f223813SNamhyung Kim /*
10830f223813SNamhyung Kim * profile stat keeps the max and min values as integer,
10840f223813SNamhyung Kim * convert to nsec time so that we can have accurate max.
10850f223813SNamhyung Kim */
10860f223813SNamhyung Kim duration *= 1000;
10870f223813SNamhyung Kim
10880f223813SNamhyung Kim /* skip to the pipe */
10890f223813SNamhyung Kim while (p < line + len && *p != '|')
10900f223813SNamhyung Kim p++;
10910f223813SNamhyung Kim
10920f223813SNamhyung Kim if (*p++ != '|')
10930f223813SNamhyung Kim return -EINVAL;
10940f223813SNamhyung Kim
10950f223813SNamhyung Kim /* get function name */
10960f223813SNamhyung Kim func = skip_spaces(p);
10970f223813SNamhyung Kim
10980f223813SNamhyung Kim /* skip the closing bracket and the start of comment */
10990f223813SNamhyung Kim if (*func == '}')
11000f223813SNamhyung Kim func += 5;
11010f223813SNamhyung Kim
11020f223813SNamhyung Kim /* remove semi-colon or end of comment at the end */
11030f223813SNamhyung Kim p = line + len - 1;
11040f223813SNamhyung Kim while (!isalnum(*p) && *p != ']') {
11050f223813SNamhyung Kim *p = '\0';
11060f223813SNamhyung Kim --p;
11070f223813SNamhyung Kim }
11080f223813SNamhyung Kim
11090f223813SNamhyung Kim return add_func_duration(ftrace, func, duration);
11100f223813SNamhyung Kim }
11110f223813SNamhyung Kim
111274ae366cSNamhyung Kim enum perf_ftrace_profile_sort_key {
111374ae366cSNamhyung Kim PFP_SORT_TOTAL = 0,
111474ae366cSNamhyung Kim PFP_SORT_AVG,
111574ae366cSNamhyung Kim PFP_SORT_MAX,
111674ae366cSNamhyung Kim PFP_SORT_COUNT,
111774ae366cSNamhyung Kim PFP_SORT_NAME,
111874ae366cSNamhyung Kim };
111974ae366cSNamhyung Kim
112074ae366cSNamhyung Kim static enum perf_ftrace_profile_sort_key profile_sort = PFP_SORT_TOTAL;
112174ae366cSNamhyung Kim
cmp_profile_data(const void * a,const void * b)11220f223813SNamhyung Kim static int cmp_profile_data(const void *a, const void *b)
11230f223813SNamhyung Kim {
11240f223813SNamhyung Kim const struct hashmap_entry *e1 = *(const struct hashmap_entry **)a;
11250f223813SNamhyung Kim const struct hashmap_entry *e2 = *(const struct hashmap_entry **)b;
11260f223813SNamhyung Kim struct ftrace_profile_data *p1 = e1->pvalue;
11270f223813SNamhyung Kim struct ftrace_profile_data *p2 = e2->pvalue;
112874ae366cSNamhyung Kim double v1, v2;
11290f223813SNamhyung Kim
113074ae366cSNamhyung Kim switch (profile_sort) {
113174ae366cSNamhyung Kim case PFP_SORT_NAME:
113274ae366cSNamhyung Kim return strcmp(e1->pkey, e2->pkey);
113374ae366cSNamhyung Kim case PFP_SORT_AVG:
113474ae366cSNamhyung Kim v1 = p1->st.mean;
113574ae366cSNamhyung Kim v2 = p2->st.mean;
113674ae366cSNamhyung Kim break;
113774ae366cSNamhyung Kim case PFP_SORT_MAX:
113874ae366cSNamhyung Kim v1 = p1->st.max;
113974ae366cSNamhyung Kim v2 = p2->st.max;
114074ae366cSNamhyung Kim break;
114174ae366cSNamhyung Kim case PFP_SORT_COUNT:
114274ae366cSNamhyung Kim v1 = p1->st.n;
114374ae366cSNamhyung Kim v2 = p2->st.n;
114474ae366cSNamhyung Kim break;
114574ae366cSNamhyung Kim case PFP_SORT_TOTAL:
114674ae366cSNamhyung Kim default:
114774ae366cSNamhyung Kim v1 = p1->st.n * p1->st.mean;
114874ae366cSNamhyung Kim v2 = p2->st.n * p2->st.mean;
114974ae366cSNamhyung Kim break;
115074ae366cSNamhyung Kim }
115174ae366cSNamhyung Kim
115274ae366cSNamhyung Kim if (v1 > v2)
11530f223813SNamhyung Kim return -1;
11540f223813SNamhyung Kim else
11550f223813SNamhyung Kim return 1;
11560f223813SNamhyung Kim }
11570f223813SNamhyung Kim
print_profile_result(struct perf_ftrace * ftrace)11580f223813SNamhyung Kim static void print_profile_result(struct perf_ftrace *ftrace)
11590f223813SNamhyung Kim {
11600f223813SNamhyung Kim struct hashmap_entry *entry, **profile;
11610f223813SNamhyung Kim size_t i, nr, bkt;
11620f223813SNamhyung Kim
11630f223813SNamhyung Kim nr = hashmap__size(ftrace->profile_hash);
11640f223813SNamhyung Kim if (nr == 0)
11650f223813SNamhyung Kim return;
11660f223813SNamhyung Kim
11670f223813SNamhyung Kim profile = calloc(nr, sizeof(*profile));
11680f223813SNamhyung Kim if (profile == NULL) {
11690f223813SNamhyung Kim pr_err("failed to allocate memory for the result\n");
11700f223813SNamhyung Kim return;
11710f223813SNamhyung Kim }
11720f223813SNamhyung Kim
11730f223813SNamhyung Kim i = 0;
11740f223813SNamhyung Kim hashmap__for_each_entry(ftrace->profile_hash, entry, bkt)
11750f223813SNamhyung Kim profile[i++] = entry;
11760f223813SNamhyung Kim
11770f223813SNamhyung Kim assert(i == nr);
11780f223813SNamhyung Kim
11790f223813SNamhyung Kim //cmp_profile_data(profile[0], profile[1]);
11800f223813SNamhyung Kim qsort(profile, nr, sizeof(*profile), cmp_profile_data);
11810f223813SNamhyung Kim
11820f223813SNamhyung Kim printf("# %10s %10s %10s %10s %s\n",
11830f223813SNamhyung Kim "Total (us)", "Avg (us)", "Max (us)", "Count", "Function");
11840f223813SNamhyung Kim
11850f223813SNamhyung Kim for (i = 0; i < nr; i++) {
11860f223813SNamhyung Kim const char *name = profile[i]->pkey;
11870f223813SNamhyung Kim struct ftrace_profile_data *p = profile[i]->pvalue;
11880f223813SNamhyung Kim
11890f223813SNamhyung Kim printf("%12.3f %10.3f %6"PRIu64".%03"PRIu64" %10.0f %s\n",
11900f223813SNamhyung Kim p->st.n * p->st.mean / 1000, p->st.mean / 1000,
11910f223813SNamhyung Kim p->st.max / 1000, p->st.max % 1000, p->st.n, name);
11920f223813SNamhyung Kim }
11930f223813SNamhyung Kim
11940f223813SNamhyung Kim free(profile);
11950f223813SNamhyung Kim
11960f223813SNamhyung Kim hashmap__for_each_entry(ftrace->profile_hash, entry, bkt) {
11970f223813SNamhyung Kim free((char *)entry->pkey);
11980f223813SNamhyung Kim free(entry->pvalue);
11990f223813SNamhyung Kim }
12000f223813SNamhyung Kim
12010f223813SNamhyung Kim hashmap__free(ftrace->profile_hash);
12020f223813SNamhyung Kim ftrace->profile_hash = NULL;
12030f223813SNamhyung Kim }
12040f223813SNamhyung Kim
__cmd_profile(struct perf_ftrace * ftrace)12050f223813SNamhyung Kim static int __cmd_profile(struct perf_ftrace *ftrace)
12060f223813SNamhyung Kim {
12070f223813SNamhyung Kim char *trace_file;
12080f223813SNamhyung Kim int trace_fd;
12090f223813SNamhyung Kim char buf[4096];
12100f223813SNamhyung Kim struct io io;
12110f223813SNamhyung Kim char *line = NULL;
12120f223813SNamhyung Kim size_t line_len = 0;
12130f223813SNamhyung Kim
12140f223813SNamhyung Kim if (prepare_func_profile(ftrace) < 0) {
12150f223813SNamhyung Kim pr_err("failed to prepare func profiler\n");
12160f223813SNamhyung Kim goto out;
12170f223813SNamhyung Kim }
12180f223813SNamhyung Kim
12190f223813SNamhyung Kim if (reset_tracing_files(ftrace) < 0) {
12200f223813SNamhyung Kim pr_err("failed to reset ftrace\n");
12210f223813SNamhyung Kim goto out;
12220f223813SNamhyung Kim }
12230f223813SNamhyung Kim
12240f223813SNamhyung Kim /* reset ftrace buffer */
12250f223813SNamhyung Kim if (write_tracing_file("trace", "0") < 0)
12260f223813SNamhyung Kim goto out;
12270f223813SNamhyung Kim
12280f223813SNamhyung Kim if (set_tracing_options(ftrace) < 0)
12290f223813SNamhyung Kim return -1;
12300f223813SNamhyung Kim
12310f223813SNamhyung Kim if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
12320f223813SNamhyung Kim pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
12330f223813SNamhyung Kim goto out_reset;
12340f223813SNamhyung Kim }
12350f223813SNamhyung Kim
12360f223813SNamhyung Kim setup_pager();
12370f223813SNamhyung Kim
12380f223813SNamhyung Kim trace_file = get_tracing_file("trace_pipe");
12390f223813SNamhyung Kim if (!trace_file) {
12400f223813SNamhyung Kim pr_err("failed to open trace_pipe\n");
12410f223813SNamhyung Kim goto out_reset;
12420f223813SNamhyung Kim }
12430f223813SNamhyung Kim
12440f223813SNamhyung Kim trace_fd = open(trace_file, O_RDONLY);
12450f223813SNamhyung Kim
12460f223813SNamhyung Kim put_tracing_file(trace_file);
12470f223813SNamhyung Kim
12480f223813SNamhyung Kim if (trace_fd < 0) {
12490f223813SNamhyung Kim pr_err("failed to open trace_pipe\n");
12500f223813SNamhyung Kim goto out_reset;
12510f223813SNamhyung Kim }
12520f223813SNamhyung Kim
12530f223813SNamhyung Kim fcntl(trace_fd, F_SETFL, O_NONBLOCK);
12540f223813SNamhyung Kim
12550f223813SNamhyung Kim if (write_tracing_file("tracing_on", "1") < 0) {
12560f223813SNamhyung Kim pr_err("can't enable tracing\n");
12570f223813SNamhyung Kim goto out_close_fd;
12580f223813SNamhyung Kim }
12590f223813SNamhyung Kim
12600f223813SNamhyung Kim evlist__start_workload(ftrace->evlist);
12610f223813SNamhyung Kim
12620f223813SNamhyung Kim io__init(&io, trace_fd, buf, sizeof(buf));
12630f223813SNamhyung Kim io.timeout_ms = -1;
12640f223813SNamhyung Kim
12650f223813SNamhyung Kim while (!done && !io.eof) {
12660f223813SNamhyung Kim if (io__getline(&io, &line, &line_len) < 0)
12670f223813SNamhyung Kim break;
12680f223813SNamhyung Kim
12690f223813SNamhyung Kim if (parse_func_duration(ftrace, line, line_len) < 0)
12700f223813SNamhyung Kim break;
12710f223813SNamhyung Kim }
12720f223813SNamhyung Kim
12730f223813SNamhyung Kim write_tracing_file("tracing_on", "0");
12740f223813SNamhyung Kim
12750f223813SNamhyung Kim if (workload_exec_errno) {
12760f223813SNamhyung Kim const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
12770f223813SNamhyung Kim /* flush stdout first so below error msg appears at the end. */
12780f223813SNamhyung Kim fflush(stdout);
12790f223813SNamhyung Kim pr_err("workload failed: %s\n", emsg);
12800f223813SNamhyung Kim goto out_free_line;
12810f223813SNamhyung Kim }
12820f223813SNamhyung Kim
12830f223813SNamhyung Kim /* read remaining buffer contents */
12840f223813SNamhyung Kim io.timeout_ms = 0;
12850f223813SNamhyung Kim while (!io.eof) {
12860f223813SNamhyung Kim if (io__getline(&io, &line, &line_len) < 0)
12870f223813SNamhyung Kim break;
12880f223813SNamhyung Kim
12890f223813SNamhyung Kim if (parse_func_duration(ftrace, line, line_len) < 0)
12900f223813SNamhyung Kim break;
12910f223813SNamhyung Kim }
12920f223813SNamhyung Kim
12930f223813SNamhyung Kim print_profile_result(ftrace);
12940f223813SNamhyung Kim
12950f223813SNamhyung Kim out_free_line:
12960f223813SNamhyung Kim free(line);
12970f223813SNamhyung Kim out_close_fd:
12980f223813SNamhyung Kim close(trace_fd);
12990f223813SNamhyung Kim out_reset:
13000f223813SNamhyung Kim reset_tracing_files(ftrace);
13010f223813SNamhyung Kim out:
13020f223813SNamhyung Kim return (done && !workload_exec_errno) ? 0 : -1;
13030f223813SNamhyung Kim }
13040f223813SNamhyung Kim
perf_ftrace_config(const char * var,const char * value,void * cb)1305b05d1093STaeung Song static int perf_ftrace_config(const char *var, const char *value, void *cb)
1306b05d1093STaeung Song {
1307b05d1093STaeung Song struct perf_ftrace *ftrace = cb;
1308b05d1093STaeung Song
13098e99b6d4SArnaldo Carvalho de Melo if (!strstarts(var, "ftrace."))
1310b05d1093STaeung Song return 0;
1311b05d1093STaeung Song
1312b05d1093STaeung Song if (strcmp(var, "ftrace.tracer"))
1313b05d1093STaeung Song return -1;
1314b05d1093STaeung Song
1315b05d1093STaeung Song if (!strcmp(value, "function_graph") ||
1316b05d1093STaeung Song !strcmp(value, "function")) {
1317b05d1093STaeung Song ftrace->tracer = value;
1318b05d1093STaeung Song return 0;
1319b05d1093STaeung Song }
1320b05d1093STaeung Song
1321b05d1093STaeung Song pr_err("Please select \"function_graph\" (default) or \"function\"\n");
1322b05d1093STaeung Song return -1;
1323b05d1093STaeung Song }
1324b05d1093STaeung Song
list_function_cb(char * str,void * arg)13252ae05fe0SChangbin Du static void list_function_cb(char *str, void *arg)
13262ae05fe0SChangbin Du {
13272ae05fe0SChangbin Du struct strfilter *filter = (struct strfilter *)arg;
13282ae05fe0SChangbin Du
13292ae05fe0SChangbin Du if (strfilter__compare(filter, str))
13302ae05fe0SChangbin Du printf("%s", str);
13312ae05fe0SChangbin Du }
13322ae05fe0SChangbin Du
opt_list_avail_functions(const struct option * opt __maybe_unused,const char * str,int unset)13332ae05fe0SChangbin Du static int opt_list_avail_functions(const struct option *opt __maybe_unused,
13342ae05fe0SChangbin Du const char *str, int unset)
13352ae05fe0SChangbin Du {
13362ae05fe0SChangbin Du struct strfilter *filter;
13372ae05fe0SChangbin Du const char *err = NULL;
13382ae05fe0SChangbin Du int ret;
13392ae05fe0SChangbin Du
13402ae05fe0SChangbin Du if (unset || !str)
13412ae05fe0SChangbin Du return -1;
13422ae05fe0SChangbin Du
13432ae05fe0SChangbin Du filter = strfilter__new(str, &err);
13442ae05fe0SChangbin Du if (!filter)
13452ae05fe0SChangbin Du return err ? -EINVAL : -ENOMEM;
13462ae05fe0SChangbin Du
13472ae05fe0SChangbin Du ret = strfilter__or(filter, str, &err);
13482ae05fe0SChangbin Du if (ret == -EINVAL) {
13492ae05fe0SChangbin Du pr_err("Filter parse error at %td.\n", err - str + 1);
13502ae05fe0SChangbin Du pr_err("Source: \"%s\"\n", str);
13512ae05fe0SChangbin Du pr_err(" %*c\n", (int)(err - str + 1), '^');
13522ae05fe0SChangbin Du strfilter__delete(filter);
13532ae05fe0SChangbin Du return ret;
13542ae05fe0SChangbin Du }
13552ae05fe0SChangbin Du
13562ae05fe0SChangbin Du ret = read_tracing_file_by_line("available_filter_functions",
13572ae05fe0SChangbin Du list_function_cb, filter);
13582ae05fe0SChangbin Du strfilter__delete(filter);
13592ae05fe0SChangbin Du if (ret < 0)
13602ae05fe0SChangbin Du return ret;
13612ae05fe0SChangbin Du
13622ae05fe0SChangbin Du exit(0);
13632ae05fe0SChangbin Du }
13642ae05fe0SChangbin Du
parse_filter_func(const struct option * opt,const char * str,int unset __maybe_unused)136578b83e8bSNamhyung Kim static int parse_filter_func(const struct option *opt, const char *str,
136678b83e8bSNamhyung Kim int unset __maybe_unused)
136778b83e8bSNamhyung Kim {
136878b83e8bSNamhyung Kim struct list_head *head = opt->value;
136978b83e8bSNamhyung Kim struct filter_entry *entry;
137078b83e8bSNamhyung Kim
137178b83e8bSNamhyung Kim entry = malloc(sizeof(*entry) + strlen(str) + 1);
137278b83e8bSNamhyung Kim if (entry == NULL)
137378b83e8bSNamhyung Kim return -ENOMEM;
137478b83e8bSNamhyung Kim
137578b83e8bSNamhyung Kim strcpy(entry->name, str);
137678b83e8bSNamhyung Kim list_add_tail(&entry->list, head);
137778b83e8bSNamhyung Kim
137878b83e8bSNamhyung Kim return 0;
137978b83e8bSNamhyung Kim }
138078b83e8bSNamhyung Kim
delete_filter_func(struct list_head * head)138178b83e8bSNamhyung Kim static void delete_filter_func(struct list_head *head)
138278b83e8bSNamhyung Kim {
138378b83e8bSNamhyung Kim struct filter_entry *pos, *tmp;
138478b83e8bSNamhyung Kim
138578b83e8bSNamhyung Kim list_for_each_entry_safe(pos, tmp, head, list) {
1386e56fbc9dSArnaldo Carvalho de Melo list_del_init(&pos->list);
138778b83e8bSNamhyung Kim free(pos);
138878b83e8bSNamhyung Kim }
138978b83e8bSNamhyung Kim }
139078b83e8bSNamhyung Kim
parse_buffer_size(const struct option * opt,const char * str,int unset)1391846e1939SChangbin Du static int parse_buffer_size(const struct option *opt,
1392846e1939SChangbin Du const char *str, int unset)
1393846e1939SChangbin Du {
1394846e1939SChangbin Du unsigned long *s = (unsigned long *)opt->value;
1395846e1939SChangbin Du static struct parse_tag tags_size[] = {
1396846e1939SChangbin Du { .tag = 'B', .mult = 1 },
1397846e1939SChangbin Du { .tag = 'K', .mult = 1 << 10 },
1398846e1939SChangbin Du { .tag = 'M', .mult = 1 << 20 },
1399846e1939SChangbin Du { .tag = 'G', .mult = 1 << 30 },
1400846e1939SChangbin Du { .tag = 0 },
1401846e1939SChangbin Du };
1402846e1939SChangbin Du unsigned long val;
1403846e1939SChangbin Du
1404846e1939SChangbin Du if (unset) {
1405846e1939SChangbin Du *s = 0;
1406846e1939SChangbin Du return 0;
1407846e1939SChangbin Du }
1408846e1939SChangbin Du
1409846e1939SChangbin Du val = parse_tag_value(str, tags_size);
1410846e1939SChangbin Du if (val != (unsigned long) -1) {
1411846e1939SChangbin Du if (val < 1024) {
1412846e1939SChangbin Du pr_err("buffer size too small, must larger than 1KB.");
1413846e1939SChangbin Du return -1;
1414846e1939SChangbin Du }
1415846e1939SChangbin Du *s = val;
1416846e1939SChangbin Du return 0;
1417846e1939SChangbin Du }
1418846e1939SChangbin Du
1419846e1939SChangbin Du return -1;
1420846e1939SChangbin Du }
1421846e1939SChangbin Du
parse_func_tracer_opts(const struct option * opt,const char * str,int unset)1422b1d84af6SChangbin Du static int parse_func_tracer_opts(const struct option *opt,
1423b1d84af6SChangbin Du const char *str, int unset)
1424b1d84af6SChangbin Du {
1425b1d84af6SChangbin Du int ret;
1426b1d84af6SChangbin Du struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
1427b1d84af6SChangbin Du struct sublevel_option func_tracer_opts[] = {
1428b1d84af6SChangbin Du { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
1429c81fc34eSChangbin Du { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
1430b1d84af6SChangbin Du { .name = NULL, }
1431b1d84af6SChangbin Du };
1432b1d84af6SChangbin Du
1433b1d84af6SChangbin Du if (unset)
1434b1d84af6SChangbin Du return 0;
1435b1d84af6SChangbin Du
1436b1d84af6SChangbin Du ret = perf_parse_sublevel_options(str, func_tracer_opts);
1437b1d84af6SChangbin Du if (ret)
1438b1d84af6SChangbin Du return ret;
1439b1d84af6SChangbin Du
1440b1d84af6SChangbin Du return 0;
1441b1d84af6SChangbin Du }
1442b1d84af6SChangbin Du
parse_graph_tracer_opts(const struct option * opt,const char * str,int unset)144338988f2eSChangbin Du static int parse_graph_tracer_opts(const struct option *opt,
144438988f2eSChangbin Du const char *str, int unset)
144538988f2eSChangbin Du {
144638988f2eSChangbin Du int ret;
144738988f2eSChangbin Du struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
144838988f2eSChangbin Du struct sublevel_option graph_tracer_opts[] = {
144938988f2eSChangbin Du { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
1450d1bcf17cSChangbin Du { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
145159486fb0SChangbin Du { .name = "verbose", .value_ptr = &ftrace->graph_verbose },
145200c85d5fSChangbin Du { .name = "thresh", .value_ptr = &ftrace->graph_thresh },
1453a8f87a5cSChangbin Du { .name = "depth", .value_ptr = &ftrace->graph_depth },
1454c7780089SNamhyung Kim { .name = "tail", .value_ptr = &ftrace->graph_tail },
145538988f2eSChangbin Du { .name = NULL, }
145638988f2eSChangbin Du };
145738988f2eSChangbin Du
145838988f2eSChangbin Du if (unset)
145938988f2eSChangbin Du return 0;
146038988f2eSChangbin Du
146138988f2eSChangbin Du ret = perf_parse_sublevel_options(str, graph_tracer_opts);
146238988f2eSChangbin Du if (ret)
146338988f2eSChangbin Du return ret;
146438988f2eSChangbin Du
146538988f2eSChangbin Du return 0;
146638988f2eSChangbin Du }
146738988f2eSChangbin Du
parse_sort_key(const struct option * opt,const char * str,int unset)146874ae366cSNamhyung Kim static int parse_sort_key(const struct option *opt, const char *str, int unset)
146974ae366cSNamhyung Kim {
147074ae366cSNamhyung Kim enum perf_ftrace_profile_sort_key *key = (void *)opt->value;
147174ae366cSNamhyung Kim
147274ae366cSNamhyung Kim if (unset)
147374ae366cSNamhyung Kim return 0;
147474ae366cSNamhyung Kim
147574ae366cSNamhyung Kim if (!strcmp(str, "total"))
147674ae366cSNamhyung Kim *key = PFP_SORT_TOTAL;
147774ae366cSNamhyung Kim else if (!strcmp(str, "avg"))
147874ae366cSNamhyung Kim *key = PFP_SORT_AVG;
147974ae366cSNamhyung Kim else if (!strcmp(str, "max"))
148074ae366cSNamhyung Kim *key = PFP_SORT_MAX;
148174ae366cSNamhyung Kim else if (!strcmp(str, "count"))
148274ae366cSNamhyung Kim *key = PFP_SORT_COUNT;
148374ae366cSNamhyung Kim else if (!strcmp(str, "name"))
148474ae366cSNamhyung Kim *key = PFP_SORT_NAME;
148574ae366cSNamhyung Kim else {
148674ae366cSNamhyung Kim pr_err("Unknown sort key: %s\n", str);
148774ae366cSNamhyung Kim return -1;
148874ae366cSNamhyung Kim }
148974ae366cSNamhyung Kim return 0;
149074ae366cSNamhyung Kim }
149174ae366cSNamhyung Kim
149253be5028SNamhyung Kim enum perf_ftrace_subcommand {
149353be5028SNamhyung Kim PERF_FTRACE_NONE,
149453be5028SNamhyung Kim PERF_FTRACE_TRACE,
149553be5028SNamhyung Kim PERF_FTRACE_LATENCY,
14960f223813SNamhyung Kim PERF_FTRACE_PROFILE,
149753be5028SNamhyung Kim };
149853be5028SNamhyung Kim
cmd_ftrace(int argc,const char ** argv)1499b0ad8ea6SArnaldo Carvalho de Melo int cmd_ftrace(int argc, const char **argv)
1500d01f4e8dSNamhyung Kim {
1501d01f4e8dSNamhyung Kim int ret;
1502fceb6212SChangbin Du int (*cmd_func)(struct perf_ftrace *) = NULL;
1503d01f4e8dSNamhyung Kim struct perf_ftrace ftrace = {
1504bf062bd2STaeung Song .tracer = DEFAULT_TRACER,
1505d01f4e8dSNamhyung Kim .target = { .uid = UINT_MAX, },
1506d01f4e8dSNamhyung Kim };
1507416e15adSNamhyung Kim const struct option common_options[] = {
1508a9af6be5SNamhyung Kim OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
1509492e4edbSArnaldo Carvalho de Melo "Trace on existing process id"),
151042145d71SChangbin Du /* TODO: Add short option -t after -t/--tracer can be removed. */
151142145d71SChangbin Du OPT_STRING(0, "tid", &ftrace.target.tid, "tid",
1512492e4edbSArnaldo Carvalho de Melo "Trace on existing thread id (exclusive to --pid)"),
1513d01f4e8dSNamhyung Kim OPT_INCR('v', "verbose", &verbose,
1514492e4edbSArnaldo Carvalho de Melo "Be more verbose"),
1515dc231032SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
1516492e4edbSArnaldo Carvalho de Melo "System-wide collection from all CPUs"),
1517dc231032SNamhyung Kim OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
1518492e4edbSArnaldo Carvalho de Melo "List of cpus to monitor"),
1519416e15adSNamhyung Kim OPT_END()
1520416e15adSNamhyung Kim };
1521416e15adSNamhyung Kim const struct option ftrace_options[] = {
1522416e15adSNamhyung Kim OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
1523416e15adSNamhyung Kim "Tracer to use: function_graph(default) or function"),
1524416e15adSNamhyung Kim OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
1525416e15adSNamhyung Kim "Show available functions to filter",
1526416e15adSNamhyung Kim opt_list_avail_functions, "*"),
152778b83e8bSNamhyung Kim OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
1528492e4edbSArnaldo Carvalho de Melo "Trace given functions using function tracer",
1529eb6d31aeSChangbin Du parse_filter_func),
153078b83e8bSNamhyung Kim OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
1531492e4edbSArnaldo Carvalho de Melo "Do not trace given functions", parse_filter_func),
1532b1d84af6SChangbin Du OPT_CALLBACK(0, "func-opts", &ftrace, "options",
1533492e4edbSArnaldo Carvalho de Melo "Function tracer options, available options: call-graph,irq-info",
1534b1d84af6SChangbin Du parse_func_tracer_opts),
153578b83e8bSNamhyung Kim OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
1536492e4edbSArnaldo Carvalho de Melo "Trace given functions using function_graph tracer",
1537eb6d31aeSChangbin Du parse_filter_func),
153878b83e8bSNamhyung Kim OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
153978b83e8bSNamhyung Kim "Set nograph filter on given functions", parse_filter_func),
154038988f2eSChangbin Du OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
1541492e4edbSArnaldo Carvalho de Melo "Graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>",
154238988f2eSChangbin Du parse_graph_tracer_opts),
1543846e1939SChangbin Du OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
1544492e4edbSArnaldo Carvalho de Melo "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
15455b347472SChangbin Du OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
1546492e4edbSArnaldo Carvalho de Melo "Trace children processes"),
1547f9f60efbSChangbin Du OPT_INTEGER('D', "delay", &ftrace.target.initial_delay,
1548492e4edbSArnaldo Carvalho de Melo "Number of milliseconds to wait before starting tracing after program start"),
1549416e15adSNamhyung Kim OPT_PARENT(common_options),
1550416e15adSNamhyung Kim };
155153be5028SNamhyung Kim const struct option latency_options[] = {
155253be5028SNamhyung Kim OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
155353be5028SNamhyung Kim "Show latency of given function", parse_filter_func),
1554177f4eacSNamhyung Kim #ifdef HAVE_BPF_SKEL
1555177f4eacSNamhyung Kim OPT_BOOLEAN('b', "use-bpf", &ftrace.target.use_bpf,
1556177f4eacSNamhyung Kim "Use BPF to measure function latency"),
1557177f4eacSNamhyung Kim #endif
15588d73259eSNamhyung Kim OPT_BOOLEAN('n', "use-nsec", &ftrace.use_nsec,
155984005bb6SNamhyung Kim "Use nano-second histogram"),
156053be5028SNamhyung Kim OPT_PARENT(common_options),
156153be5028SNamhyung Kim };
15620f223813SNamhyung Kim const struct option profile_options[] = {
15630f223813SNamhyung Kim OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
15640f223813SNamhyung Kim "Trace given functions using function tracer",
15650f223813SNamhyung Kim parse_filter_func),
15660f223813SNamhyung Kim OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
15670f223813SNamhyung Kim "Do not trace given functions", parse_filter_func),
15680f223813SNamhyung Kim OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
15690f223813SNamhyung Kim "Trace given functions using function_graph tracer",
15700f223813SNamhyung Kim parse_filter_func),
15710f223813SNamhyung Kim OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
15720f223813SNamhyung Kim "Set nograph filter on given functions", parse_filter_func),
15730f223813SNamhyung Kim OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
15740f223813SNamhyung Kim "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
157574ae366cSNamhyung Kim OPT_CALLBACK('s', "sort", &profile_sort, "key",
157674ae366cSNamhyung Kim "Sort result by key: total (default), avg, max, count, name.",
157774ae366cSNamhyung Kim parse_sort_key),
15780f223813SNamhyung Kim OPT_PARENT(common_options),
15790f223813SNamhyung Kim };
158053be5028SNamhyung Kim const struct option *options = ftrace_options;
1581416e15adSNamhyung Kim
1582416e15adSNamhyung Kim const char * const ftrace_usage[] = {
1583416e15adSNamhyung Kim "perf ftrace [<options>] [<command>]",
1584416e15adSNamhyung Kim "perf ftrace [<options>] -- [<command>] [<options>]",
15850f223813SNamhyung Kim "perf ftrace {trace|latency|profile} [<options>] [<command>]",
15860f223813SNamhyung Kim "perf ftrace {trace|latency|profile} [<options>] -- [<command>] [<options>]",
1587416e15adSNamhyung Kim NULL
1588d01f4e8dSNamhyung Kim };
158953be5028SNamhyung Kim enum perf_ftrace_subcommand subcmd = PERF_FTRACE_NONE;
1590d01f4e8dSNamhyung Kim
159178b83e8bSNamhyung Kim INIT_LIST_HEAD(&ftrace.filters);
159278b83e8bSNamhyung Kim INIT_LIST_HEAD(&ftrace.notrace);
159378b83e8bSNamhyung Kim INIT_LIST_HEAD(&ftrace.graph_funcs);
159478b83e8bSNamhyung Kim INIT_LIST_HEAD(&ftrace.nograph_funcs);
159578b83e8bSNamhyung Kim
1596a9b8ae8aSNamhyung Kim signal(SIGINT, sig_handler);
1597a9b8ae8aSNamhyung Kim signal(SIGUSR1, sig_handler);
1598a9b8ae8aSNamhyung Kim signal(SIGCHLD, sig_handler);
1599a9b8ae8aSNamhyung Kim signal(SIGPIPE, sig_handler);
1600a9b8ae8aSNamhyung Kim
1601e25ebda7SIan Rogers if (!check_ftrace_capable())
1602608585f4SNamhyung Kim return -1;
1603608585f4SNamhyung Kim
1604*74298dd8SChangbin Du if (!is_ftrace_supported()) {
1605*74298dd8SChangbin Du pr_err("ftrace is not supported on this system\n");
1606*74298dd8SChangbin Du return -ENOTSUP;
1607*74298dd8SChangbin Du }
1608*74298dd8SChangbin Du
1609b05d1093STaeung Song ret = perf_config(perf_ftrace_config, &ftrace);
1610b05d1093STaeung Song if (ret < 0)
1611b05d1093STaeung Song return -1;
1612b05d1093STaeung Song
161353be5028SNamhyung Kim if (argc > 1) {
161453be5028SNamhyung Kim if (!strcmp(argv[1], "trace")) {
161553be5028SNamhyung Kim subcmd = PERF_FTRACE_TRACE;
161653be5028SNamhyung Kim } else if (!strcmp(argv[1], "latency")) {
161753be5028SNamhyung Kim subcmd = PERF_FTRACE_LATENCY;
161853be5028SNamhyung Kim options = latency_options;
16190f223813SNamhyung Kim } else if (!strcmp(argv[1], "profile")) {
16200f223813SNamhyung Kim subcmd = PERF_FTRACE_PROFILE;
16210f223813SNamhyung Kim options = profile_options;
162253be5028SNamhyung Kim }
162353be5028SNamhyung Kim
162453be5028SNamhyung Kim if (subcmd != PERF_FTRACE_NONE) {
1625416e15adSNamhyung Kim argc--;
1626416e15adSNamhyung Kim argv++;
1627416e15adSNamhyung Kim }
162853be5028SNamhyung Kim }
162953be5028SNamhyung Kim /* for backward compatibility */
163053be5028SNamhyung Kim if (subcmd == PERF_FTRACE_NONE)
163153be5028SNamhyung Kim subcmd = PERF_FTRACE_TRACE;
1632416e15adSNamhyung Kim
163353be5028SNamhyung Kim argc = parse_options(argc, argv, options, ftrace_usage,
1634d01f4e8dSNamhyung Kim PARSE_OPT_STOP_AT_NON_OPTION);
163553be5028SNamhyung Kim if (argc < 0) {
163653be5028SNamhyung Kim ret = -EINVAL;
163753be5028SNamhyung Kim goto out_delete_filters;
163853be5028SNamhyung Kim }
1639d01f4e8dSNamhyung Kim
1640ecd4960dSYang Jihong /* Make system wide (-a) the default target. */
1641fceb6212SChangbin Du if (!argc && target__none(&ftrace.target))
1642fceb6212SChangbin Du ftrace.target.system_wide = true;
1643ecd4960dSYang Jihong
1644ecd4960dSYang Jihong switch (subcmd) {
1645ecd4960dSYang Jihong case PERF_FTRACE_TRACE:
1646fceb6212SChangbin Du cmd_func = __cmd_ftrace;
1647fceb6212SChangbin Du break;
1648fceb6212SChangbin Du case PERF_FTRACE_LATENCY:
1649fceb6212SChangbin Du if (list_empty(&ftrace.filters)) {
1650fceb6212SChangbin Du pr_err("Should provide a function to measure\n");
1651fceb6212SChangbin Du parse_options_usage(ftrace_usage, options, "T", 1);
1652fceb6212SChangbin Du ret = -EINVAL;
1653fceb6212SChangbin Du goto out_delete_filters;
1654fceb6212SChangbin Du }
1655fceb6212SChangbin Du cmd_func = __cmd_latency;
1656fceb6212SChangbin Du break;
16570f223813SNamhyung Kim case PERF_FTRACE_PROFILE:
16580f223813SNamhyung Kim cmd_func = __cmd_profile;
16590f223813SNamhyung Kim break;
1660fceb6212SChangbin Du case PERF_FTRACE_NONE:
1661fceb6212SChangbin Du default:
1662fceb6212SChangbin Du pr_err("Invalid subcommand\n");
1663fceb6212SChangbin Du ret = -EINVAL;
1664fceb6212SChangbin Du goto out_delete_filters;
1665fceb6212SChangbin Du }
1666fceb6212SChangbin Du
1667a9af6be5SNamhyung Kim ret = target__validate(&ftrace.target);
1668a9af6be5SNamhyung Kim if (ret) {
1669a9af6be5SNamhyung Kim char errbuf[512];
1670a9af6be5SNamhyung Kim
1671a9af6be5SNamhyung Kim target__strerror(&ftrace.target, ret, errbuf, 512);
1672a9af6be5SNamhyung Kim pr_err("%s\n", errbuf);
167378b83e8bSNamhyung Kim goto out_delete_filters;
1674a9af6be5SNamhyung Kim }
1675a9af6be5SNamhyung Kim
16760f98b11cSJiri Olsa ftrace.evlist = evlist__new();
167778b83e8bSNamhyung Kim if (ftrace.evlist == NULL) {
167878b83e8bSNamhyung Kim ret = -ENOMEM;
167978b83e8bSNamhyung Kim goto out_delete_filters;
168078b83e8bSNamhyung Kim }
1681d01f4e8dSNamhyung Kim
16827748bb71SArnaldo Carvalho de Melo ret = evlist__create_maps(ftrace.evlist, &ftrace.target);
1683d01f4e8dSNamhyung Kim if (ret < 0)
1684d01f4e8dSNamhyung Kim goto out_delete_evlist;
1685d01f4e8dSNamhyung Kim
1686a9b8ae8aSNamhyung Kim if (argc) {
1687a9b8ae8aSNamhyung Kim ret = evlist__prepare_workload(ftrace.evlist, &ftrace.target,
1688a9b8ae8aSNamhyung Kim argv, false,
1689a9b8ae8aSNamhyung Kim ftrace__workload_exec_failed_signal);
1690a9b8ae8aSNamhyung Kim if (ret < 0)
1691a9b8ae8aSNamhyung Kim goto out_delete_evlist;
1692a9b8ae8aSNamhyung Kim }
1693a9b8ae8aSNamhyung Kim
1694fceb6212SChangbin Du ret = cmd_func(&ftrace);
1695d01f4e8dSNamhyung Kim
1696d01f4e8dSNamhyung Kim out_delete_evlist:
1697c12995a5SJiri Olsa evlist__delete(ftrace.evlist);
1698d01f4e8dSNamhyung Kim
169978b83e8bSNamhyung Kim out_delete_filters:
170078b83e8bSNamhyung Kim delete_filter_func(&ftrace.filters);
170178b83e8bSNamhyung Kim delete_filter_func(&ftrace.notrace);
170278b83e8bSNamhyung Kim delete_filter_func(&ftrace.graph_funcs);
170378b83e8bSNamhyung Kim delete_filter_func(&ftrace.nograph_funcs);
170478b83e8bSNamhyung Kim
1705d01f4e8dSNamhyung Kim return ret;
1706d01f4e8dSNamhyung Kim }
1707