4 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 * Released under the GPL v2.
19 #include <subcmd/parse-options.h>
20 #include <api/fs/tracing_path.h>
24 #include "thread_map.h"
25 #include "util/config.h"
28 #define DEFAULT_TRACER "function_graph"
31 struct perf_evlist
*evlist
;
38 static void sig_handler(int sig __maybe_unused
)
44 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
45 * we asked by setting its exec_error to the function below,
46 * ftrace__workload_exec_failed_signal.
48 * XXX We need to handle this more appropriately, emitting an error, etc.
50 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused
,
51 siginfo_t
*info __maybe_unused
,
52 void *ucontext __maybe_unused
)
54 /* workload_exec_errno = info->si_value.sival_int; */
58 static int __write_tracing_file(const char *name
, const char *val
, bool append
)
62 ssize_t size
= strlen(val
);
65 file
= get_tracing_file(name
);
67 pr_debug("cannot get tracing file: %s\n", name
);
76 fd
= open(file
, flags
);
78 pr_debug("cannot open tracing file: %s\n", name
);
82 if (write(fd
, val
, size
) == size
)
85 pr_debug("write '%s' to tracing/%s failed\n", val
, name
);
89 put_tracing_file(file
);
93 static int write_tracing_file(const char *name
, const char *val
)
95 return __write_tracing_file(name
, val
, false);
98 static int append_tracing_file(const char *name
, const char *val
)
100 return __write_tracing_file(name
, val
, true);
103 static int reset_tracing_cpu(void);
105 static int reset_tracing_files(struct perf_ftrace
*ftrace __maybe_unused
)
107 if (write_tracing_file("tracing_on", "0") < 0)
110 if (write_tracing_file("current_tracer", "nop") < 0)
113 if (write_tracing_file("set_ftrace_pid", " ") < 0)
116 if (reset_tracing_cpu() < 0)
122 static int set_tracing_pid(struct perf_ftrace
*ftrace
)
127 if (target__has_cpu(&ftrace
->target
))
130 for (i
= 0; i
< thread_map__nr(ftrace
->evlist
->threads
); i
++) {
131 scnprintf(buf
, sizeof(buf
), "%d",
132 ftrace
->evlist
->threads
->map
[i
]);
133 if (append_tracing_file("set_ftrace_pid", buf
) < 0)
139 static int set_tracing_cpumask(struct cpu_map
*cpumap
)
146 last_cpu
= cpu_map__cpu(cpumap
, cpumap
->nr
- 1);
147 mask_size
= (last_cpu
+ 3) / 4 + 1;
148 mask_size
+= last_cpu
/ 32; /* ',' is needed for every 32th cpus */
150 cpumask
= malloc(mask_size
);
151 if (cpumask
== NULL
) {
152 pr_debug("failed to allocate cpu mask\n");
156 cpu_map__snprint_mask(cpumap
, cpumask
, mask_size
);
158 ret
= write_tracing_file("tracing_cpumask", cpumask
);
164 static int set_tracing_cpu(struct perf_ftrace
*ftrace
)
166 struct cpu_map
*cpumap
= ftrace
->evlist
->cpus
;
168 if (!target__has_cpu(&ftrace
->target
))
171 return set_tracing_cpumask(cpumap
);
174 static int reset_tracing_cpu(void)
176 struct cpu_map
*cpumap
= cpu_map__new(NULL
);
179 ret
= set_tracing_cpumask(cpumap
);
180 cpu_map__put(cpumap
);
184 static int __cmd_ftrace(struct perf_ftrace
*ftrace
, int argc
, const char **argv
)
189 struct pollfd pollfd
= {
193 if (geteuid() != 0) {
194 pr_err("ftrace only works for root!\n");
198 signal(SIGINT
, sig_handler
);
199 signal(SIGUSR1
, sig_handler
);
200 signal(SIGCHLD
, sig_handler
);
201 signal(SIGPIPE
, sig_handler
);
203 if (reset_tracing_files(ftrace
) < 0)
206 /* reset ftrace buffer */
207 if (write_tracing_file("trace", "0") < 0)
210 if (argc
&& perf_evlist__prepare_workload(ftrace
->evlist
,
211 &ftrace
->target
, argv
, false,
212 ftrace__workload_exec_failed_signal
) < 0) {
216 if (set_tracing_pid(ftrace
) < 0) {
217 pr_err("failed to set ftrace pid\n");
221 if (set_tracing_cpu(ftrace
) < 0) {
222 pr_err("failed to set tracing cpumask\n");
226 if (write_tracing_file("current_tracer", ftrace
->tracer
) < 0) {
227 pr_err("failed to set current_tracer to %s\n", ftrace
->tracer
);
231 trace_file
= get_tracing_file("trace_pipe");
233 pr_err("failed to open trace_pipe\n");
237 trace_fd
= open(trace_file
, O_RDONLY
);
239 put_tracing_file(trace_file
);
242 pr_err("failed to open trace_pipe\n");
246 fcntl(trace_fd
, F_SETFL
, O_NONBLOCK
);
247 pollfd
.fd
= trace_fd
;
249 if (write_tracing_file("tracing_on", "1") < 0) {
250 pr_err("can't enable tracing\n");
256 perf_evlist__start_workload(ftrace
->evlist
);
259 if (poll(&pollfd
, 1, -1) < 0)
262 if (pollfd
.revents
& POLLIN
) {
263 int n
= read(trace_fd
, buf
, sizeof(buf
));
266 if (fwrite(buf
, n
, 1, stdout
) != 1)
271 write_tracing_file("tracing_on", "0");
273 /* read remaining buffer contents */
275 int n
= read(trace_fd
, buf
, sizeof(buf
));
278 if (fwrite(buf
, n
, 1, stdout
) != 1)
285 reset_tracing_files(ftrace
);
287 return done
? 0 : -1;
290 static int perf_ftrace_config(const char *var
, const char *value
, void *cb
)
292 struct perf_ftrace
*ftrace
= cb
;
294 if (prefixcmp(var
, "ftrace."))
297 if (strcmp(var
, "ftrace.tracer"))
300 if (!strcmp(value
, "function_graph") ||
301 !strcmp(value
, "function")) {
302 ftrace
->tracer
= value
;
306 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
310 int cmd_ftrace(int argc
, const char **argv
)
313 struct perf_ftrace ftrace
= {
314 .tracer
= DEFAULT_TRACER
,
315 .target
= { .uid
= UINT_MAX
, },
317 const char * const ftrace_usage
[] = {
318 "perf ftrace [<options>] [<command>]",
319 "perf ftrace [<options>] -- <command> [<options>]",
322 const struct option ftrace_options
[] = {
323 OPT_STRING('t', "tracer", &ftrace
.tracer
, "tracer",
324 "tracer to use: function_graph(default) or function"),
325 OPT_STRING('p', "pid", &ftrace
.target
.pid
, "pid",
326 "trace on existing process id"),
327 OPT_INCR('v', "verbose", &verbose
,
329 OPT_BOOLEAN('a', "all-cpus", &ftrace
.target
.system_wide
,
330 "system-wide collection from all CPUs"),
331 OPT_STRING('C', "cpu", &ftrace
.target
.cpu_list
, "cpu",
332 "list of cpus to monitor"),
336 ret
= perf_config(perf_ftrace_config
, &ftrace
);
340 argc
= parse_options(argc
, argv
, ftrace_options
, ftrace_usage
,
341 PARSE_OPT_STOP_AT_NON_OPTION
);
342 if (!argc
&& target__none(&ftrace
.target
))
343 usage_with_options(ftrace_usage
, ftrace_options
);
345 ret
= target__validate(&ftrace
.target
);
349 target__strerror(&ftrace
.target
, ret
, errbuf
, 512);
350 pr_err("%s\n", errbuf
);
354 ftrace
.evlist
= perf_evlist__new();
355 if (ftrace
.evlist
== NULL
)
358 ret
= perf_evlist__create_maps(ftrace
.evlist
, &ftrace
.target
);
360 goto out_delete_evlist
;
362 ret
= __cmd_ftrace(&ftrace
, argc
, argv
);
365 perf_evlist__delete(ftrace
.evlist
);