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);
66 file = get_tracing_file(name);
68 pr_debug("cannot get tracing file: %s\n", name);
77 fd = open(file, flags);
79 pr_debug("cannot open tracing file: %s: %s\n",
80 name, str_error_r(errno, errbuf, sizeof(errbuf)));
84 if (write(fd, val, size) == size)
87 pr_debug("write '%s' to tracing/%s failed: %s\n",
88 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
92 put_tracing_file(file);
96 static int write_tracing_file(const char *name, const char *val)
98 return __write_tracing_file(name, val, false);
101 static int append_tracing_file(const char *name, const char *val)
103 return __write_tracing_file(name, val, true);
106 static int reset_tracing_cpu(void);
108 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
110 if (write_tracing_file("tracing_on", "0") < 0)
113 if (write_tracing_file("current_tracer", "nop") < 0)
116 if (write_tracing_file("set_ftrace_pid", " ") < 0)
119 if (reset_tracing_cpu() < 0)
125 static int set_tracing_pid(struct perf_ftrace *ftrace)
130 if (target__has_cpu(&ftrace->target))
133 for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
134 scnprintf(buf, sizeof(buf), "%d",
135 ftrace->evlist->threads->map[i]);
136 if (append_tracing_file("set_ftrace_pid", buf) < 0)
142 static int set_tracing_cpumask(struct cpu_map *cpumap)
149 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
150 mask_size = (last_cpu + 3) / 4 + 1;
151 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
153 cpumask = malloc(mask_size);
154 if (cpumask == NULL) {
155 pr_debug("failed to allocate cpu mask\n");
159 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
161 ret = write_tracing_file("tracing_cpumask", cpumask);
167 static int set_tracing_cpu(struct perf_ftrace *ftrace)
169 struct cpu_map *cpumap = ftrace->evlist->cpus;
171 if (!target__has_cpu(&ftrace->target))
174 return set_tracing_cpumask(cpumap);
177 static int reset_tracing_cpu(void)
179 struct cpu_map *cpumap = cpu_map__new(NULL);
182 ret = set_tracing_cpumask(cpumap);
183 cpu_map__put(cpumap);
187 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
192 struct pollfd pollfd = {
196 if (geteuid() != 0) {
197 pr_err("ftrace only works for root!\n");
201 signal(SIGINT, sig_handler);
202 signal(SIGUSR1, sig_handler);
203 signal(SIGCHLD, sig_handler);
204 signal(SIGPIPE, sig_handler);
206 if (reset_tracing_files(ftrace) < 0)
209 /* reset ftrace buffer */
210 if (write_tracing_file("trace", "0") < 0)
213 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
214 &ftrace->target, argv, false,
215 ftrace__workload_exec_failed_signal) < 0) {
219 if (set_tracing_pid(ftrace) < 0) {
220 pr_err("failed to set ftrace pid\n");
224 if (set_tracing_cpu(ftrace) < 0) {
225 pr_err("failed to set tracing cpumask\n");
229 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
230 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
236 trace_file = get_tracing_file("trace_pipe");
238 pr_err("failed to open trace_pipe\n");
242 trace_fd = open(trace_file, O_RDONLY);
244 put_tracing_file(trace_file);
247 pr_err("failed to open trace_pipe\n");
251 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
252 pollfd.fd = trace_fd;
254 if (write_tracing_file("tracing_on", "1") < 0) {
255 pr_err("can't enable tracing\n");
259 perf_evlist__start_workload(ftrace->evlist);
262 if (poll(&pollfd, 1, -1) < 0)
265 if (pollfd.revents & POLLIN) {
266 int n = read(trace_fd, buf, sizeof(buf));
269 if (fwrite(buf, n, 1, stdout) != 1)
274 write_tracing_file("tracing_on", "0");
276 /* read remaining buffer contents */
278 int n = read(trace_fd, buf, sizeof(buf));
281 if (fwrite(buf, n, 1, stdout) != 1)
288 reset_tracing_files(ftrace);
290 return done ? 0 : -1;
293 static int perf_ftrace_config(const char *var, const char *value, void *cb)
295 struct perf_ftrace *ftrace = cb;
297 if (prefixcmp(var, "ftrace."))
300 if (strcmp(var, "ftrace.tracer"))
303 if (!strcmp(value, "function_graph") ||
304 !strcmp(value, "function")) {
305 ftrace->tracer = value;
309 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
313 int cmd_ftrace(int argc, const char **argv)
316 struct perf_ftrace ftrace = {
317 .tracer = DEFAULT_TRACER,
318 .target = { .uid = UINT_MAX, },
320 const char * const ftrace_usage[] = {
321 "perf ftrace [<options>] [<command>]",
322 "perf ftrace [<options>] -- <command> [<options>]",
325 const struct option ftrace_options[] = {
326 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
327 "tracer to use: function_graph(default) or function"),
328 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
329 "trace on existing process id"),
330 OPT_INCR('v', "verbose", &verbose,
332 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
333 "system-wide collection from all CPUs"),
334 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
335 "list of cpus to monitor"),
339 ret = perf_config(perf_ftrace_config, &ftrace);
343 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
344 PARSE_OPT_STOP_AT_NON_OPTION);
345 if (!argc && target__none(&ftrace.target))
346 usage_with_options(ftrace_usage, ftrace_options);
348 ret = target__validate(&ftrace.target);
352 target__strerror(&ftrace.target, ret, errbuf, 512);
353 pr_err("%s\n", errbuf);
357 ftrace.evlist = perf_evlist__new();
358 if (ftrace.evlist == NULL)
361 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
363 goto out_delete_evlist;
365 ret = __cmd_ftrace(&ftrace, argc, argv);
368 perf_evlist__delete(ftrace.evlist);