Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 1 | /* |
| 2 | * builtin-ftrace.c |
| 3 | * |
| 4 | * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org> |
| 5 | * |
| 6 | * Released under the GPL v2. |
| 7 | */ |
| 8 | |
| 9 | #include "builtin.h" |
| 10 | #include "perf.h" |
| 11 | |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 12 | #include <errno.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | #include <signal.h> |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 15 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 16 | #include <poll.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 17 | |
| 18 | #include "debug.h" |
| 19 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 20a9ed2 | 2017-04-18 11:44:58 -0300 | [diff] [blame] | 20 | #include <api/fs/tracing_path.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 21 | #include "evlist.h" |
| 22 | #include "target.h" |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 23 | #include "cpumap.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 24 | #include "thread_map.h" |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 25 | #include "util/config.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 26 | |
| 27 | |
| 28 | #define DEFAULT_TRACER "function_graph" |
| 29 | |
| 30 | struct perf_ftrace { |
| 31 | struct perf_evlist *evlist; |
| 32 | struct target target; |
| 33 | const char *tracer; |
| 34 | }; |
| 35 | |
| 36 | static bool done; |
| 37 | |
| 38 | static void sig_handler(int sig __maybe_unused) |
| 39 | { |
| 40 | done = true; |
| 41 | } |
| 42 | |
| 43 | /* |
| 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. |
| 47 | * |
| 48 | * XXX We need to handle this more appropriately, emitting an error, etc. |
| 49 | */ |
| 50 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, |
| 51 | siginfo_t *info __maybe_unused, |
| 52 | void *ucontext __maybe_unused) |
| 53 | { |
| 54 | /* workload_exec_errno = info->si_value.sival_int; */ |
| 55 | done = true; |
| 56 | } |
| 57 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 58 | static int __write_tracing_file(const char *name, const char *val, bool append) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 59 | { |
| 60 | char *file; |
| 61 | int fd, ret = -1; |
| 62 | ssize_t size = strlen(val); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 63 | int flags = O_WRONLY; |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame^] | 64 | char errbuf[512]; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 65 | |
| 66 | file = get_tracing_file(name); |
| 67 | if (!file) { |
| 68 | pr_debug("cannot get tracing file: %s\n", name); |
| 69 | return -1; |
| 70 | } |
| 71 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 72 | if (append) |
| 73 | flags |= O_APPEND; |
| 74 | else |
| 75 | flags |= O_TRUNC; |
| 76 | |
| 77 | fd = open(file, flags); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 78 | if (fd < 0) { |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame^] | 79 | pr_debug("cannot open tracing file: %s: %s\n", |
| 80 | name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 81 | goto out; |
| 82 | } |
| 83 | |
| 84 | if (write(fd, val, size) == size) |
| 85 | ret = 0; |
| 86 | else |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame^] | 87 | pr_debug("write '%s' to tracing/%s failed: %s\n", |
| 88 | val, name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 89 | |
| 90 | close(fd); |
| 91 | out: |
| 92 | put_tracing_file(file); |
| 93 | return ret; |
| 94 | } |
| 95 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 96 | static int write_tracing_file(const char *name, const char *val) |
| 97 | { |
| 98 | return __write_tracing_file(name, val, false); |
| 99 | } |
| 100 | |
| 101 | static int append_tracing_file(const char *name, const char *val) |
| 102 | { |
| 103 | return __write_tracing_file(name, val, true); |
| 104 | } |
| 105 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 106 | static int reset_tracing_cpu(void); |
| 107 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 108 | static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) |
| 109 | { |
| 110 | if (write_tracing_file("tracing_on", "0") < 0) |
| 111 | return -1; |
| 112 | |
| 113 | if (write_tracing_file("current_tracer", "nop") < 0) |
| 114 | return -1; |
| 115 | |
| 116 | if (write_tracing_file("set_ftrace_pid", " ") < 0) |
| 117 | return -1; |
| 118 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 119 | if (reset_tracing_cpu() < 0) |
| 120 | return -1; |
| 121 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 125 | static int set_tracing_pid(struct perf_ftrace *ftrace) |
| 126 | { |
| 127 | int i; |
| 128 | char buf[16]; |
| 129 | |
| 130 | if (target__has_cpu(&ftrace->target)) |
| 131 | return 0; |
| 132 | |
| 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) |
| 137 | return -1; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 142 | static int set_tracing_cpumask(struct cpu_map *cpumap) |
| 143 | { |
| 144 | char *cpumask; |
| 145 | size_t mask_size; |
| 146 | int ret; |
| 147 | int last_cpu; |
| 148 | |
| 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 */ |
| 152 | |
| 153 | cpumask = malloc(mask_size); |
| 154 | if (cpumask == NULL) { |
| 155 | pr_debug("failed to allocate cpu mask\n"); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | cpu_map__snprint_mask(cpumap, cpumask, mask_size); |
| 160 | |
| 161 | ret = write_tracing_file("tracing_cpumask", cpumask); |
| 162 | |
| 163 | free(cpumask); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int set_tracing_cpu(struct perf_ftrace *ftrace) |
| 168 | { |
| 169 | struct cpu_map *cpumap = ftrace->evlist->cpus; |
| 170 | |
| 171 | if (!target__has_cpu(&ftrace->target)) |
| 172 | return 0; |
| 173 | |
| 174 | return set_tracing_cpumask(cpumap); |
| 175 | } |
| 176 | |
| 177 | static int reset_tracing_cpu(void) |
| 178 | { |
| 179 | struct cpu_map *cpumap = cpu_map__new(NULL); |
| 180 | int ret; |
| 181 | |
| 182 | ret = set_tracing_cpumask(cpumap); |
| 183 | cpu_map__put(cpumap); |
| 184 | return ret; |
| 185 | } |
| 186 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 187 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) |
| 188 | { |
| 189 | char *trace_file; |
| 190 | int trace_fd; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 191 | char buf[4096]; |
| 192 | struct pollfd pollfd = { |
| 193 | .events = POLLIN, |
| 194 | }; |
| 195 | |
| 196 | if (geteuid() != 0) { |
| 197 | pr_err("ftrace only works for root!\n"); |
| 198 | return -1; |
| 199 | } |
| 200 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 201 | signal(SIGINT, sig_handler); |
| 202 | signal(SIGUSR1, sig_handler); |
| 203 | signal(SIGCHLD, sig_handler); |
Namhyung Kim | 5833596 | 2017-02-24 10:12:51 +0900 | [diff] [blame] | 204 | signal(SIGPIPE, sig_handler); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 205 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 206 | if (reset_tracing_files(ftrace) < 0) |
| 207 | goto out; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 208 | |
| 209 | /* reset ftrace buffer */ |
| 210 | if (write_tracing_file("trace", "0") < 0) |
| 211 | goto out; |
| 212 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 213 | if (argc && perf_evlist__prepare_workload(ftrace->evlist, |
| 214 | &ftrace->target, argv, false, |
| 215 | ftrace__workload_exec_failed_signal) < 0) { |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 216 | goto out; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (set_tracing_pid(ftrace) < 0) { |
| 220 | pr_err("failed to set ftrace pid\n"); |
| 221 | goto out_reset; |
| 222 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 223 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 224 | if (set_tracing_cpu(ftrace) < 0) { |
| 225 | pr_err("failed to set tracing cpumask\n"); |
| 226 | goto out_reset; |
| 227 | } |
| 228 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 229 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
| 230 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 231 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | trace_file = get_tracing_file("trace_pipe"); |
| 235 | if (!trace_file) { |
| 236 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 237 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | trace_fd = open(trace_file, O_RDONLY); |
| 241 | |
| 242 | put_tracing_file(trace_file); |
| 243 | |
| 244 | if (trace_fd < 0) { |
| 245 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 246 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); |
| 250 | pollfd.fd = trace_fd; |
| 251 | |
| 252 | if (write_tracing_file("tracing_on", "1") < 0) { |
| 253 | pr_err("can't enable tracing\n"); |
| 254 | goto out_close_fd; |
| 255 | } |
| 256 | |
Namhyung Kim | 5833596 | 2017-02-24 10:12:51 +0900 | [diff] [blame] | 257 | setup_pager(); |
| 258 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 259 | perf_evlist__start_workload(ftrace->evlist); |
| 260 | |
| 261 | while (!done) { |
| 262 | if (poll(&pollfd, 1, -1) < 0) |
| 263 | break; |
| 264 | |
| 265 | if (pollfd.revents & POLLIN) { |
| 266 | int n = read(trace_fd, buf, sizeof(buf)); |
| 267 | if (n < 0) |
| 268 | break; |
| 269 | if (fwrite(buf, n, 1, stdout) != 1) |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | write_tracing_file("tracing_on", "0"); |
| 275 | |
| 276 | /* read remaining buffer contents */ |
| 277 | while (true) { |
| 278 | int n = read(trace_fd, buf, sizeof(buf)); |
| 279 | if (n <= 0) |
| 280 | break; |
| 281 | if (fwrite(buf, n, 1, stdout) != 1) |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | out_close_fd: |
| 286 | close(trace_fd); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 287 | out_reset: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 288 | reset_tracing_files(ftrace); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 289 | out: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 290 | return done ? 0 : -1; |
| 291 | } |
| 292 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 293 | static int perf_ftrace_config(const char *var, const char *value, void *cb) |
| 294 | { |
| 295 | struct perf_ftrace *ftrace = cb; |
| 296 | |
| 297 | if (prefixcmp(var, "ftrace.")) |
| 298 | return 0; |
| 299 | |
| 300 | if (strcmp(var, "ftrace.tracer")) |
| 301 | return -1; |
| 302 | |
| 303 | if (!strcmp(value, "function_graph") || |
| 304 | !strcmp(value, "function")) { |
| 305 | ftrace->tracer = value; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | pr_err("Please select \"function_graph\" (default) or \"function\"\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 313 | int cmd_ftrace(int argc, const char **argv) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 314 | { |
| 315 | int ret; |
| 316 | struct perf_ftrace ftrace = { |
Taeung Song | bf062bd | 2017-01-26 18:35:37 +0900 | [diff] [blame] | 317 | .tracer = DEFAULT_TRACER, |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 318 | .target = { .uid = UINT_MAX, }, |
| 319 | }; |
| 320 | const char * const ftrace_usage[] = { |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 321 | "perf ftrace [<options>] [<command>]", |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 322 | "perf ftrace [<options>] -- <command> [<options>]", |
| 323 | NULL |
| 324 | }; |
| 325 | const struct option ftrace_options[] = { |
| 326 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", |
Arnaldo Carvalho de Melo | ec34787 | 2017-01-18 21:49:14 -0300 | [diff] [blame] | 327 | "tracer to use: function_graph(default) or function"), |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 328 | OPT_STRING('p', "pid", &ftrace.target.pid, "pid", |
| 329 | "trace on existing process id"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 330 | OPT_INCR('v', "verbose", &verbose, |
| 331 | "be more verbose"), |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 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"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 336 | OPT_END() |
| 337 | }; |
| 338 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 339 | ret = perf_config(perf_ftrace_config, &ftrace); |
| 340 | if (ret < 0) |
| 341 | return -1; |
| 342 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 343 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
| 344 | PARSE_OPT_STOP_AT_NON_OPTION); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 345 | if (!argc && target__none(&ftrace.target)) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 346 | usage_with_options(ftrace_usage, ftrace_options); |
| 347 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 348 | ret = target__validate(&ftrace.target); |
| 349 | if (ret) { |
| 350 | char errbuf[512]; |
| 351 | |
| 352 | target__strerror(&ftrace.target, ret, errbuf, 512); |
| 353 | pr_err("%s\n", errbuf); |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 357 | ftrace.evlist = perf_evlist__new(); |
| 358 | if (ftrace.evlist == NULL) |
| 359 | return -ENOMEM; |
| 360 | |
| 361 | ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target); |
| 362 | if (ret < 0) |
| 363 | goto out_delete_evlist; |
| 364 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 365 | ret = __cmd_ftrace(&ftrace, argc, argv); |
| 366 | |
| 367 | out_delete_evlist: |
| 368 | perf_evlist__delete(ftrace.evlist); |
| 369 | |
| 370 | return ret; |
| 371 | } |