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