4 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 * Released under the GPL v2.
17 #include <subcmd/parse-options.h>
21 #include "thread_map.h"
22 #include "util/config.h"
25 #define DEFAULT_TRACER "function_graph"
28 struct perf_evlist
*evlist
;
35 static void sig_handler(int sig __maybe_unused
)
41 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
42 * we asked by setting its exec_error to the function below,
43 * ftrace__workload_exec_failed_signal.
45 * XXX We need to handle this more appropriately, emitting an error, etc.
47 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused
,
48 siginfo_t
*info __maybe_unused
,
49 void *ucontext __maybe_unused
)
51 /* workload_exec_errno = info->si_value.sival_int; */
55 static int __write_tracing_file(const char *name
, const char *val
, bool append
)
59 ssize_t size
= strlen(val
);
62 file
= get_tracing_file(name
);
64 pr_debug("cannot get tracing file: %s\n", name
);
73 fd
= open(file
, flags
);
75 pr_debug("cannot open tracing file: %s\n", name
);
79 if (write(fd
, val
, size
) == size
)
82 pr_debug("write '%s' to tracing/%s failed\n", val
, name
);
86 put_tracing_file(file
);
90 static int write_tracing_file(const char *name
, const char *val
)
92 return __write_tracing_file(name
, val
, false);
95 static int append_tracing_file(const char *name
, const char *val
)
97 return __write_tracing_file(name
, val
, true);
100 static int reset_tracing_cpu(void);
102 static int reset_tracing_files(struct perf_ftrace
*ftrace __maybe_unused
)
104 if (write_tracing_file("tracing_on", "0") < 0)
107 if (write_tracing_file("current_tracer", "nop") < 0)
110 if (write_tracing_file("set_ftrace_pid", " ") < 0)
113 if (reset_tracing_cpu() < 0)
119 static int set_tracing_pid(struct perf_ftrace
*ftrace
)
124 if (target__has_cpu(&ftrace
->target
))
127 for (i
= 0; i
< thread_map__nr(ftrace
->evlist
->threads
); i
++) {
128 scnprintf(buf
, sizeof(buf
), "%d",
129 ftrace
->evlist
->threads
->map
[i
]);
130 if (append_tracing_file("set_ftrace_pid", buf
) < 0)
136 static int set_tracing_cpumask(struct cpu_map
*cpumap
)
143 last_cpu
= cpu_map__cpu(cpumap
, cpumap
->nr
- 1);
144 mask_size
= (last_cpu
+ 3) / 4 + 1;
145 mask_size
+= last_cpu
/ 32; /* ',' is needed for every 32th cpus */
147 cpumask
= malloc(mask_size
);
148 if (cpumask
== NULL
) {
149 pr_debug("failed to allocate cpu mask\n");
153 cpu_map__snprint_mask(cpumap
, cpumask
, mask_size
);
155 ret
= write_tracing_file("tracing_cpumask", cpumask
);
161 static int set_tracing_cpu(struct perf_ftrace
*ftrace
)
163 struct cpu_map
*cpumap
= ftrace
->evlist
->cpus
;
165 if (!target__has_cpu(&ftrace
->target
))
168 return set_tracing_cpumask(cpumap
);
171 static int reset_tracing_cpu(void)
173 struct cpu_map
*cpumap
= cpu_map__new(NULL
);
176 ret
= set_tracing_cpumask(cpumap
);
177 cpu_map__put(cpumap
);
181 static int __cmd_ftrace(struct perf_ftrace
*ftrace
, int argc
, const char **argv
)
186 struct pollfd pollfd
= {
190 if (geteuid() != 0) {
191 pr_err("ftrace only works for root!\n");
195 signal(SIGINT
, sig_handler
);
196 signal(SIGUSR1
, sig_handler
);
197 signal(SIGCHLD
, sig_handler
);
198 signal(SIGPIPE
, sig_handler
);
200 if (reset_tracing_files(ftrace
) < 0)
203 /* reset ftrace buffer */
204 if (write_tracing_file("trace", "0") < 0)
207 if (argc
&& perf_evlist__prepare_workload(ftrace
->evlist
,
208 &ftrace
->target
, argv
, false,
209 ftrace__workload_exec_failed_signal
) < 0) {
213 if (set_tracing_pid(ftrace
) < 0) {
214 pr_err("failed to set ftrace pid\n");
218 if (set_tracing_cpu(ftrace
) < 0) {
219 pr_err("failed to set tracing cpumask\n");
223 if (write_tracing_file("current_tracer", ftrace
->tracer
) < 0) {
224 pr_err("failed to set current_tracer to %s\n", ftrace
->tracer
);
228 trace_file
= get_tracing_file("trace_pipe");
230 pr_err("failed to open trace_pipe\n");
234 trace_fd
= open(trace_file
, O_RDONLY
);
236 put_tracing_file(trace_file
);
239 pr_err("failed to open trace_pipe\n");
243 fcntl(trace_fd
, F_SETFL
, O_NONBLOCK
);
244 pollfd
.fd
= trace_fd
;
246 if (write_tracing_file("tracing_on", "1") < 0) {
247 pr_err("can't enable tracing\n");
253 perf_evlist__start_workload(ftrace
->evlist
);
256 if (poll(&pollfd
, 1, -1) < 0)
259 if (pollfd
.revents
& POLLIN
) {
260 int n
= read(trace_fd
, buf
, sizeof(buf
));
263 if (fwrite(buf
, n
, 1, stdout
) != 1)
268 write_tracing_file("tracing_on", "0");
270 /* read remaining buffer contents */
272 int n
= read(trace_fd
, buf
, sizeof(buf
));
275 if (fwrite(buf
, n
, 1, stdout
) != 1)
282 reset_tracing_files(ftrace
);
284 return done
? 0 : -1;
287 static int perf_ftrace_config(const char *var
, const char *value
, void *cb
)
289 struct perf_ftrace
*ftrace
= cb
;
291 if (prefixcmp(var
, "ftrace."))
294 if (strcmp(var
, "ftrace.tracer"))
297 if (!strcmp(value
, "function_graph") ||
298 !strcmp(value
, "function")) {
299 ftrace
->tracer
= value
;
303 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
307 int cmd_ftrace(int argc
, const char **argv
)
310 struct perf_ftrace ftrace
= {
311 .tracer
= DEFAULT_TRACER
,
312 .target
= { .uid
= UINT_MAX
, },
314 const char * const ftrace_usage
[] = {
315 "perf ftrace [<options>] [<command>]",
316 "perf ftrace [<options>] -- <command> [<options>]",
319 const struct option ftrace_options
[] = {
320 OPT_STRING('t', "tracer", &ftrace
.tracer
, "tracer",
321 "tracer to use: function_graph(default) or function"),
322 OPT_STRING('p', "pid", &ftrace
.target
.pid
, "pid",
323 "trace on existing process id"),
324 OPT_INCR('v', "verbose", &verbose
,
326 OPT_BOOLEAN('a', "all-cpus", &ftrace
.target
.system_wide
,
327 "system-wide collection from all CPUs"),
328 OPT_STRING('C', "cpu", &ftrace
.target
.cpu_list
, "cpu",
329 "list of cpus to monitor"),
333 ret
= perf_config(perf_ftrace_config
, &ftrace
);
337 argc
= parse_options(argc
, argv
, ftrace_options
, ftrace_usage
,
338 PARSE_OPT_STOP_AT_NON_OPTION
);
339 if (!argc
&& target__none(&ftrace
.target
))
340 usage_with_options(ftrace_usage
, ftrace_options
);
342 ret
= target__validate(&ftrace
.target
);
346 target__strerror(&ftrace
.target
, ret
, errbuf
, 512);
347 pr_err("%s\n", errbuf
);
351 ftrace
.evlist
= perf_evlist__new();
352 if (ftrace
.evlist
== NULL
)
355 ret
= perf_evlist__create_maps(ftrace
.evlist
, &ftrace
.target
);
357 goto out_delete_evlist
;
359 ret
= __cmd_ftrace(&ftrace
, argc
, argv
);
362 perf_evlist__delete(ftrace
.evlist
);