diff options
| author | Namhyung Kim <namhyung.kim@lge.com> | 2013-03-07 07:45:20 -0500 |
|---|---|---|
| committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2017-01-26 09:43:01 -0500 |
| commit | d01f4e8db22cf4d04f6c86351d959b584eb1f5f7 (patch) | |
| tree | 3be1870968508a94d2f467184a8aab3ef1340dd2 /tools | |
| parent | a7619aef6dca913d830675dd914ca6fe7241a730 (diff) | |
perf ftrace: Introduce new 'ftrace' tool
The 'perf ftrace' command is a simple wrapper of kernel's ftrace
functionality. It only supports single thread tracing currently and
just reads trace_pipe in text and then write it to stdout.
Committer notes:
Testing it:
# perf ftrace -f function_graph usleep 123456
<SNIP>
2) | SyS_nanosleep() {
2) | _copy_from_user() {
<SNIP>
2) 0.900 us | }
2) 1.354 us | }
2) | hrtimer_nanosleep() {
2) 0.062 us | __hrtimer_init();
2) | do_nanosleep() {
2) | hrtimer_start_range_ns() {
<SNIP>
2) 5.025 us | }
2) | schedule() {
2) 0.125 us | rcu_note_context_switch();
2) 0.057 us | _raw_spin_lock();
2) | deactivate_task() {
2) 0.369 us | update_rq_clock.part.77();
2) | dequeue_task_fair() {
<SNIP>
2) + 22.453 us | }
2) + 23.736 us | }
2) | pick_next_task_fair() {
<SNIP>
2) + 47.167 us | }
2) | pick_next_task_idle() {
<SNIP>
2) 4.462 us | }
------------------------------------------
2) usleep-20387 => <idle>-0
------------------------------------------
2) 0.806 us | switch_mm_irqs_off();
------------------------------------------
2) <idle>-0 => usleep-20387
------------------------------------------
2) 0.151 us | finish_task_switch();
2) @ 123597.2 us | }
2) 0.037 us | _cond_resched();
2) | hrtimer_try_to_cancel() {
2) 0.064 us | hrtimer_active();
2) 0.353 us | }
2) @ 123605.3 us | }
2) @ 123606.2 us | }
2) @ 123608.3 us | } /* SyS_nanosleep */
2) | __do_page_fault() {
<SNIP>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jeremy Eder <jeder@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>,
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/n/tip-r1hgmsj4dxny8arn3o9mw512@git.kernel.org
[ Various foward port fixes, add man page ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/perf/Build | 1 | ||||
| -rw-r--r-- | tools/perf/Documentation/perf-ftrace.txt | 36 | ||||
| -rw-r--r-- | tools/perf/builtin-ftrace.c | 242 | ||||
| -rw-r--r-- | tools/perf/builtin.h | 1 | ||||
| -rw-r--r-- | tools/perf/command-list.txt | 1 | ||||
| -rw-r--r-- | tools/perf/perf.c | 1 |
6 files changed, 282 insertions, 0 deletions
diff --git a/tools/perf/Build b/tools/perf/Build index 7039ecb89170..9b79f8d7db50 100644 --- a/tools/perf/Build +++ b/tools/perf/Build | |||
| @@ -3,6 +3,7 @@ perf-y += builtin-annotate.o | |||
| 3 | perf-y += builtin-config.o | 3 | perf-y += builtin-config.o |
| 4 | perf-y += builtin-diff.o | 4 | perf-y += builtin-diff.o |
| 5 | perf-y += builtin-evlist.o | 5 | perf-y += builtin-evlist.o |
| 6 | perf-y += builtin-ftrace.o | ||
| 6 | perf-y += builtin-help.o | 7 | perf-y += builtin-help.o |
| 7 | perf-y += builtin-sched.o | 8 | perf-y += builtin-sched.o |
| 8 | perf-y += builtin-buildid-list.o | 9 | perf-y += builtin-buildid-list.o |
diff --git a/tools/perf/Documentation/perf-ftrace.txt b/tools/perf/Documentation/perf-ftrace.txt new file mode 100644 index 000000000000..2d96de6132a9 --- /dev/null +++ b/tools/perf/Documentation/perf-ftrace.txt | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | perf-ftrace(1) | ||
| 2 | ============= | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-ftrace - simple wrapper for kernel's ftrace functionality | ||
| 7 | |||
| 8 | |||
| 9 | SYNOPSIS | ||
| 10 | -------- | ||
| 11 | [verse] | ||
| 12 | 'perf ftrace' <command> | ||
| 13 | |||
| 14 | DESCRIPTION | ||
| 15 | ----------- | ||
| 16 | The 'perf ftrace' command is a simple wrapper of kernel's ftrace | ||
| 17 | functionality. It only supports single thread tracing currently and | ||
| 18 | just reads trace_pipe in text and then write it to stdout. | ||
| 19 | |||
| 20 | The following options apply to perf ftrace. | ||
| 21 | |||
| 22 | OPTIONS | ||
| 23 | ------- | ||
| 24 | |||
| 25 | -t:: | ||
| 26 | --tracer=:: | ||
| 27 | Tracer to use: function_graph or function. | ||
| 28 | |||
| 29 | -v:: | ||
| 30 | --verbose=:: | ||
| 31 | Verbosity level. | ||
| 32 | |||
| 33 | |||
| 34 | SEE ALSO | ||
| 35 | -------- | ||
| 36 | linkperf:perf-record[1], linkperf:perf-trace[1] | ||
diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c new file mode 100644 index 000000000000..c320ae25c8e6 --- /dev/null +++ b/tools/perf/builtin-ftrace.c | |||
| @@ -0,0 +1,242 @@ | |||
| 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 | |||
| 12 | #include <unistd.h> | ||
| 13 | #include <signal.h> | ||
| 14 | |||
| 15 | #include "debug.h" | ||
| 16 | #include <subcmd/parse-options.h> | ||
| 17 | #include "evlist.h" | ||
| 18 | #include "target.h" | ||
| 19 | #include "thread_map.h" | ||
| 20 | |||
| 21 | |||
| 22 | #define DEFAULT_TRACER "function_graph" | ||
| 23 | |||
| 24 | struct perf_ftrace { | ||
| 25 | struct perf_evlist *evlist; | ||
| 26 | struct target target; | ||
| 27 | const char *tracer; | ||
| 28 | }; | ||
| 29 | |||
| 30 | static bool done; | ||
| 31 | |||
| 32 | static void sig_handler(int sig __maybe_unused) | ||
| 33 | { | ||
| 34 | done = true; | ||
| 35 | } | ||
| 36 | |||
| 37 | /* | ||
| 38 | * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since | ||
| 39 | * we asked by setting its exec_error to the function below, | ||
| 40 | * ftrace__workload_exec_failed_signal. | ||
| 41 | * | ||
| 42 | * XXX We need to handle this more appropriately, emitting an error, etc. | ||
| 43 | */ | ||
| 44 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, | ||
| 45 | siginfo_t *info __maybe_unused, | ||
| 46 | void *ucontext __maybe_unused) | ||
| 47 | { | ||
| 48 | /* workload_exec_errno = info->si_value.sival_int; */ | ||
| 49 | done = true; | ||
| 50 | } | ||
| 51 | |||
| 52 | static int write_tracing_file(const char *name, const char *val) | ||
| 53 | { | ||
| 54 | char *file; | ||
| 55 | int fd, ret = -1; | ||
| 56 | ssize_t size = strlen(val); | ||
| 57 | |||
| 58 | file = get_tracing_file(name); | ||
| 59 | if (!file) { | ||
| 60 | pr_debug("cannot get tracing file: %s\n", name); | ||
| 61 | return -1; | ||
| 62 | } | ||
| 63 | |||
| 64 | fd = open(file, O_WRONLY); | ||
| 65 | if (fd < 0) { | ||
| 66 | pr_debug("cannot open tracing file: %s\n", name); | ||
| 67 | goto out; | ||
| 68 | } | ||
| 69 | |||
| 70 | if (write(fd, val, size) == size) | ||
| 71 | ret = 0; | ||
| 72 | else | ||
| 73 | pr_debug("write '%s' to tracing/%s failed\n", val, name); | ||
| 74 | |||
| 75 | close(fd); | ||
| 76 | out: | ||
| 77 | put_tracing_file(file); | ||
| 78 | return ret; | ||
| 79 | } | ||
| 80 | |||
| 81 | static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) | ||
| 82 | { | ||
| 83 | if (write_tracing_file("tracing_on", "0") < 0) | ||
| 84 | return -1; | ||
| 85 | |||
| 86 | if (write_tracing_file("current_tracer", "nop") < 0) | ||
| 87 | return -1; | ||
| 88 | |||
| 89 | if (write_tracing_file("set_ftrace_pid", " ") < 0) | ||
| 90 | return -1; | ||
| 91 | |||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) | ||
| 96 | { | ||
| 97 | char *trace_file; | ||
| 98 | int trace_fd; | ||
| 99 | char *trace_pid; | ||
| 100 | char buf[4096]; | ||
| 101 | struct pollfd pollfd = { | ||
| 102 | .events = POLLIN, | ||
| 103 | }; | ||
| 104 | |||
| 105 | if (geteuid() != 0) { | ||
| 106 | pr_err("ftrace only works for root!\n"); | ||
| 107 | return -1; | ||
| 108 | } | ||
| 109 | |||
| 110 | if (argc < 1) | ||
| 111 | return -1; | ||
| 112 | |||
| 113 | signal(SIGINT, sig_handler); | ||
| 114 | signal(SIGUSR1, sig_handler); | ||
| 115 | signal(SIGCHLD, sig_handler); | ||
| 116 | |||
| 117 | reset_tracing_files(ftrace); | ||
| 118 | |||
| 119 | /* reset ftrace buffer */ | ||
| 120 | if (write_tracing_file("trace", "0") < 0) | ||
| 121 | goto out; | ||
| 122 | |||
| 123 | if (perf_evlist__prepare_workload(ftrace->evlist, &ftrace->target, | ||
| 124 | argv, false, ftrace__workload_exec_failed_signal) < 0) | ||
| 125 | goto out; | ||
| 126 | |||
| 127 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { | ||
| 128 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); | ||
| 129 | goto out; | ||
| 130 | } | ||
| 131 | |||
| 132 | if (asprintf(&trace_pid, "%d", thread_map__pid(ftrace->evlist->threads, 0)) < 0) { | ||
| 133 | pr_err("failed to allocate pid string\n"); | ||
| 134 | goto out; | ||
| 135 | } | ||
| 136 | |||
| 137 | if (write_tracing_file("set_ftrace_pid", trace_pid) < 0) { | ||
| 138 | pr_err("failed to set pid: %s\n", trace_pid); | ||
| 139 | goto out_free_pid; | ||
| 140 | } | ||
| 141 | |||
| 142 | trace_file = get_tracing_file("trace_pipe"); | ||
| 143 | if (!trace_file) { | ||
| 144 | pr_err("failed to open trace_pipe\n"); | ||
| 145 | goto out_free_pid; | ||
| 146 | } | ||
| 147 | |||
| 148 | trace_fd = open(trace_file, O_RDONLY); | ||
| 149 | |||
| 150 | put_tracing_file(trace_file); | ||
| 151 | |||
| 152 | if (trace_fd < 0) { | ||
| 153 | pr_err("failed to open trace_pipe\n"); | ||
| 154 | goto out_free_pid; | ||
| 155 | } | ||
| 156 | |||
| 157 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); | ||
| 158 | pollfd.fd = trace_fd; | ||
| 159 | |||
| 160 | if (write_tracing_file("tracing_on", "1") < 0) { | ||
| 161 | pr_err("can't enable tracing\n"); | ||
| 162 | goto out_close_fd; | ||
| 163 | } | ||
| 164 | |||
| 165 | perf_evlist__start_workload(ftrace->evlist); | ||
| 166 | |||
| 167 | while (!done) { | ||
| 168 | if (poll(&pollfd, 1, -1) < 0) | ||
| 169 | break; | ||
| 170 | |||
| 171 | if (pollfd.revents & POLLIN) { | ||
| 172 | int n = read(trace_fd, buf, sizeof(buf)); | ||
| 173 | if (n < 0) | ||
| 174 | break; | ||
| 175 | if (fwrite(buf, n, 1, stdout) != 1) | ||
| 176 | break; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | write_tracing_file("tracing_on", "0"); | ||
| 181 | |||
| 182 | /* read remaining buffer contents */ | ||
| 183 | while (true) { | ||
| 184 | int n = read(trace_fd, buf, sizeof(buf)); | ||
| 185 | if (n <= 0) | ||
| 186 | break; | ||
| 187 | if (fwrite(buf, n, 1, stdout) != 1) | ||
| 188 | break; | ||
| 189 | } | ||
| 190 | |||
| 191 | out_close_fd: | ||
| 192 | close(trace_fd); | ||
| 193 | out_free_pid: | ||
| 194 | free(trace_pid); | ||
| 195 | out: | ||
| 196 | reset_tracing_files(ftrace); | ||
| 197 | |||
| 198 | return done ? 0 : -1; | ||
| 199 | } | ||
| 200 | |||
| 201 | int cmd_ftrace(int argc, const char **argv, const char *prefix __maybe_unused) | ||
| 202 | { | ||
| 203 | int ret; | ||
| 204 | struct perf_ftrace ftrace = { | ||
| 205 | .target = { .uid = UINT_MAX, }, | ||
| 206 | }; | ||
| 207 | const char * const ftrace_usage[] = { | ||
| 208 | "perf ftrace [<options>] <command>", | ||
| 209 | "perf ftrace [<options>] -- <command> [<options>]", | ||
| 210 | NULL | ||
| 211 | }; | ||
| 212 | const struct option ftrace_options[] = { | ||
| 213 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", | ||
| 214 | "tracer to use: function_graph or function"), | ||
| 215 | OPT_INCR('v', "verbose", &verbose, | ||
| 216 | "be more verbose"), | ||
| 217 | OPT_END() | ||
| 218 | }; | ||
| 219 | |||
| 220 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, | ||
| 221 | PARSE_OPT_STOP_AT_NON_OPTION); | ||
| 222 | if (!argc) | ||
| 223 | usage_with_options(ftrace_usage, ftrace_options); | ||
| 224 | |||
| 225 | ftrace.evlist = perf_evlist__new(); | ||
| 226 | if (ftrace.evlist == NULL) | ||
| 227 | return -ENOMEM; | ||
| 228 | |||
| 229 | ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target); | ||
| 230 | if (ret < 0) | ||
| 231 | goto out_delete_evlist; | ||
| 232 | |||
| 233 | if (ftrace.tracer == NULL) | ||
| 234 | ftrace.tracer = DEFAULT_TRACER; | ||
| 235 | |||
| 236 | ret = __cmd_ftrace(&ftrace, argc, argv); | ||
| 237 | |||
| 238 | out_delete_evlist: | ||
| 239 | perf_evlist__delete(ftrace.evlist); | ||
| 240 | |||
| 241 | return ret; | ||
| 242 | } | ||
diff --git a/tools/perf/builtin.h b/tools/perf/builtin.h index b55f5be486a1..036e1e35b1a8 100644 --- a/tools/perf/builtin.h +++ b/tools/perf/builtin.h | |||
| @@ -41,6 +41,7 @@ int cmd_trace(int argc, const char **argv, const char *prefix); | |||
| 41 | int cmd_inject(int argc, const char **argv, const char *prefix); | 41 | int cmd_inject(int argc, const char **argv, const char *prefix); |
| 42 | int cmd_mem(int argc, const char **argv, const char *prefix); | 42 | int cmd_mem(int argc, const char **argv, const char *prefix); |
| 43 | int cmd_data(int argc, const char **argv, const char *prefix); | 43 | int cmd_data(int argc, const char **argv, const char *prefix); |
| 44 | int cmd_ftrace(int argc, const char **argv, const char *prefix); | ||
| 44 | 45 | ||
| 45 | int find_scripts(char **scripts_array, char **scripts_path_array); | 46 | int find_scripts(char **scripts_array, char **scripts_path_array); |
| 46 | #endif | 47 | #endif |
diff --git a/tools/perf/command-list.txt b/tools/perf/command-list.txt index fb45613dba9e..ac3efd396a72 100644 --- a/tools/perf/command-list.txt +++ b/tools/perf/command-list.txt | |||
| @@ -11,6 +11,7 @@ perf-data mainporcelain common | |||
| 11 | perf-diff mainporcelain common | 11 | perf-diff mainporcelain common |
| 12 | perf-config mainporcelain common | 12 | perf-config mainporcelain common |
| 13 | perf-evlist mainporcelain common | 13 | perf-evlist mainporcelain common |
| 14 | perf-ftrace mainporcelain common | ||
| 14 | perf-inject mainporcelain common | 15 | perf-inject mainporcelain common |
| 15 | perf-kallsyms mainporcelain common | 16 | perf-kallsyms mainporcelain common |
| 16 | perf-kmem mainporcelain common | 17 | perf-kmem mainporcelain common |
diff --git a/tools/perf/perf.c b/tools/perf/perf.c index 0324cd15e42a..34bcf90f8871 100644 --- a/tools/perf/perf.c +++ b/tools/perf/perf.c | |||
| @@ -71,6 +71,7 @@ static struct cmd_struct commands[] = { | |||
| 71 | { "inject", cmd_inject, 0 }, | 71 | { "inject", cmd_inject, 0 }, |
| 72 | { "mem", cmd_mem, 0 }, | 72 | { "mem", cmd_mem, 0 }, |
| 73 | { "data", cmd_data, 0 }, | 73 | { "data", cmd_data, 0 }, |
| 74 | { "ftrace", cmd_ftrace, 0 }, | ||
| 74 | }; | 75 | }; |
| 75 | 76 | ||
| 76 | struct pager_config { | 77 | struct pager_config { |
