Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 4 | # ucalls Summarize method calls in high-level languages and/or system calls. |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 7 | # USAGE: ucalls [-l {java,perl,php,python,ruby,tcl}] [-h] [-T TOP] [-L] [-S] [-v] [-m] |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 8 | # pid [interval] |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 9 | # |
| 10 | # Copyright 2016 Sasha Goldshtein |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 12 | # |
| 13 | # 19-Oct-2016 Sasha Goldshtein Created this. |
| 14 | |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | import argparse |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 17 | from time import sleep |
yonghong-song | cd9334c | 2019-02-18 09:08:28 -0800 | [diff] [blame] | 18 | from bcc import BPF, USDT, utils |
| 19 | from bcc.syscall import syscall_name |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 20 | |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 21 | languages = ["java", "perl", "php", "python", "ruby", "tcl"] |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 22 | |
| 23 | examples = """examples: |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 24 | ./ucalls -l java 185 # trace Java calls and print statistics on ^C |
| 25 | ./ucalls -l python 2020 1 # trace Python calls and print every second |
| 26 | ./ucalls -l java 185 -S # trace Java calls and syscalls |
| 27 | ./ucalls 6712 -S # trace only syscall counts |
| 28 | ./ucalls -l ruby 1344 -T 10 # trace top 10 Ruby method calls |
| 29 | ./ucalls -l ruby 1344 -L # trace Ruby calls including latency |
Sasha Goldshtein | cfb5ee7 | 2017-02-08 14:32:51 -0500 | [diff] [blame] | 30 | ./ucalls -l php 443 -LS # trace PHP calls and syscalls with latency |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 31 | ./ucalls -l python 2020 -mL # trace Python calls including latency in ms |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 32 | """ |
| 33 | parser = argparse.ArgumentParser( |
| 34 | description="Summarize method calls in high-level languages.", |
| 35 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 36 | epilog=examples) |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 37 | parser.add_argument("pid", type=int, help="process id to attach to") |
| 38 | parser.add_argument("interval", type=int, nargs='?', |
| 39 | help="print every specified number of seconds") |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 40 | parser.add_argument("-l", "--language", choices=languages + ["none"], |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 41 | help="language to trace (if none, trace syscalls only)") |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 42 | parser.add_argument("-T", "--top", type=int, |
| 43 | help="number of most frequent/slow calls to print") |
| 44 | parser.add_argument("-L", "--latency", action="store_true", |
| 45 | help="record method latency from enter to exit (except recursive calls)") |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 46 | parser.add_argument("-S", "--syscalls", action="store_true", |
| 47 | help="record syscall latency (adds overhead)") |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 48 | parser.add_argument("-v", "--verbose", action="store_true", |
| 49 | help="verbose mode: print the BPF program (for debugging purposes)") |
| 50 | parser.add_argument("-m", "--milliseconds", action="store_true", |
| 51 | help="report times in milliseconds (default is microseconds)") |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 52 | parser.add_argument("--ebpf", action="store_true", |
| 53 | help=argparse.SUPPRESS) |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 54 | args = parser.parse_args() |
| 55 | |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 56 | language = args.language |
| 57 | if not language: |
| 58 | language = utils.detect_language(languages, args.pid) |
| 59 | |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 60 | # We assume that the entry and return probes have the same arguments. This is |
Sasha Goldshtein | cfb5ee7 | 2017-02-08 14:32:51 -0500 | [diff] [blame] | 61 | # the case for Java, Python, Ruby, and PHP. If there's a language where it's |
| 62 | # not the case, we will need to build a custom correlator from entry to exit. |
Geneviève Bastien | 830c1f7 | 2017-07-14 16:04:12 -0400 | [diff] [blame] | 63 | extra_message = "" |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 64 | if language == "java": |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 65 | # TODO for JVM entries, we actually have the real length of the class |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 66 | # and method strings in arg3 and arg5 respectively, so we can insert |
| 67 | # the null terminator in its proper position. |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 68 | entry_probe = "method__entry" |
| 69 | return_probe = "method__return" |
| 70 | read_class = "bpf_usdt_readarg(2, ctx, &clazz);" |
| 71 | read_method = "bpf_usdt_readarg(4, ctx, &method);" |
Paul Chaignon | c8b4f67 | 2017-10-07 11:51:45 +0200 | [diff] [blame] | 72 | extra_message = ("If you do not see any results, make sure you ran java" |
| 73 | " with option -XX:+ExtendedDTraceProbes") |
Marko Myllynen | 9162be4 | 2018-09-04 19:45:16 +0300 | [diff] [blame] | 74 | elif language == "perl": |
| 75 | entry_probe = "sub__entry" |
| 76 | return_probe = "sub__return" |
| 77 | read_class = "bpf_usdt_readarg(2, ctx, &clazz);" # filename really |
| 78 | read_method = "bpf_usdt_readarg(1, ctx, &method);" |
| 79 | elif language == "php": |
| 80 | entry_probe = "function__entry" |
| 81 | return_probe = "function__return" |
| 82 | read_class = "bpf_usdt_readarg(4, ctx, &clazz);" |
| 83 | read_method = "bpf_usdt_readarg(1, ctx, &method);" |
| 84 | extra_message = ("If you do not see any results, make sure the environment" |
| 85 | " variable USE_ZEND_DTRACE is set to 1") |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 86 | elif language == "python": |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 87 | entry_probe = "function__entry" |
| 88 | return_probe = "function__return" |
| 89 | read_class = "bpf_usdt_readarg(1, ctx, &clazz);" # filename really |
| 90 | read_method = "bpf_usdt_readarg(2, ctx, &method);" |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 91 | elif language == "ruby": |
Sasha Goldshtein | 6e5c621 | 2016-10-25 04:30:54 -0700 | [diff] [blame] | 92 | # TODO Also probe cmethod__entry and cmethod__return with same arguments |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 93 | entry_probe = "method__entry" |
| 94 | return_probe = "method__return" |
| 95 | read_class = "bpf_usdt_readarg(1, ctx, &clazz);" |
| 96 | read_method = "bpf_usdt_readarg(2, ctx, &method);" |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 97 | elif language == "tcl": |
| 98 | # TODO Also consider probe cmd__entry and cmd__return with same arguments |
| 99 | entry_probe = "proc__entry" |
| 100 | return_probe = "proc__return" |
| 101 | read_class = "" # no class/file info available |
| 102 | read_method = "bpf_usdt_readarg(1, ctx, &method);" |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 103 | elif not language or language == "none": |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 104 | if not args.syscalls: |
| 105 | print("Nothing to do; use -S to trace syscalls.") |
| 106 | exit(1) |
| 107 | entry_probe, return_probe, read_class, read_method = ("", "", "", "") |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 108 | if language: |
| 109 | language = None |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 110 | |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 111 | program = """ |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 112 | #include <linux/ptrace.h> |
| 113 | |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 114 | #define MAX_STRING_LENGTH 80 |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 115 | DEFINE_NOLANG |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 116 | DEFINE_LATENCY |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 117 | DEFINE_SYSCALLS |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 118 | |
| 119 | struct method_t { |
| 120 | char clazz[MAX_STRING_LENGTH]; |
| 121 | char method[MAX_STRING_LENGTH]; |
| 122 | }; |
| 123 | struct entry_t { |
| 124 | u64 pid; |
| 125 | struct method_t method; |
| 126 | }; |
| 127 | struct info_t { |
| 128 | u64 num_calls; |
| 129 | u64 total_ns; |
| 130 | }; |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 131 | struct syscall_entry_t { |
| 132 | u64 timestamp; |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 133 | u64 id; |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 134 | }; |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 135 | |
| 136 | #ifndef LATENCY |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 137 | BPF_HASH(counts, struct method_t, u64); // number of calls |
| 138 | #ifdef SYSCALLS |
| 139 | BPF_HASH(syscounts, u64, u64); // number of calls per IP |
| 140 | #endif // SYSCALLS |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 141 | #else |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 142 | BPF_HASH(times, struct method_t, struct info_t); |
| 143 | BPF_HASH(entry, struct entry_t, u64); // timestamp at entry |
| 144 | #ifdef SYSCALLS |
| 145 | BPF_HASH(systimes, u64, struct info_t); // latency per IP |
| 146 | BPF_HASH(sysentry, u64, struct syscall_entry_t); // ts + IP at entry |
| 147 | #endif // SYSCALLS |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 148 | #endif |
| 149 | |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 150 | #ifndef NOLANG |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 151 | int trace_entry(struct pt_regs *ctx) { |
| 152 | u64 clazz = 0, method = 0, val = 0; |
| 153 | u64 *valp; |
| 154 | struct entry_t data = {0}; |
| 155 | #ifdef LATENCY |
| 156 | u64 timestamp = bpf_ktime_get_ns(); |
| 157 | data.pid = bpf_get_current_pid_tgid(); |
| 158 | #endif |
| 159 | READ_CLASS |
| 160 | READ_METHOD |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 161 | bpf_probe_read_user(&data.method.clazz, sizeof(data.method.clazz), |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 162 | (void *)clazz); |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 163 | bpf_probe_read_user(&data.method.method, sizeof(data.method.method), |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 164 | (void *)method); |
| 165 | #ifndef LATENCY |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 166 | valp = counts.lookup_or_try_init(&data.method, &val); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 167 | if (valp) { |
| 168 | ++(*valp); |
| 169 | } |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 170 | #endif |
| 171 | #ifdef LATENCY |
| 172 | entry.update(&data, ×tamp); |
| 173 | #endif |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | #ifdef LATENCY |
| 178 | int trace_return(struct pt_regs *ctx) { |
| 179 | u64 *entry_timestamp, clazz = 0, method = 0; |
| 180 | struct info_t *info, zero = {}; |
| 181 | struct entry_t data = {}; |
| 182 | data.pid = bpf_get_current_pid_tgid(); |
| 183 | READ_CLASS |
| 184 | READ_METHOD |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 185 | bpf_probe_read_user(&data.method.clazz, sizeof(data.method.clazz), |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 186 | (void *)clazz); |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 187 | bpf_probe_read_user(&data.method.method, sizeof(data.method.method), |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 188 | (void *)method); |
| 189 | entry_timestamp = entry.lookup(&data); |
| 190 | if (!entry_timestamp) { |
| 191 | return 0; // missed the entry event |
| 192 | } |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 193 | info = times.lookup_or_try_init(&data.method, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 194 | if (info) { |
| 195 | info->num_calls += 1; |
| 196 | info->total_ns += bpf_ktime_get_ns() - *entry_timestamp; |
| 197 | } |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 198 | entry.delete(&data); |
| 199 | return 0; |
| 200 | } |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 201 | #endif // LATENCY |
| 202 | #endif // NOLANG |
| 203 | |
| 204 | #ifdef SYSCALLS |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 205 | TRACEPOINT_PROBE(raw_syscalls, sys_enter) { |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 206 | u64 pid = bpf_get_current_pid_tgid(); |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 207 | u64 *valp, id = args->id, val = 0; |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 208 | PID_FILTER |
| 209 | #ifdef LATENCY |
| 210 | struct syscall_entry_t data = {}; |
| 211 | data.timestamp = bpf_ktime_get_ns(); |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 212 | data.id = id; |
| 213 | sysentry.update(&pid, &data); |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 214 | #endif |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 215 | #ifndef LATENCY |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 216 | valp = syscounts.lookup_or_try_init(&id, &val); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 217 | if (valp) { |
| 218 | ++(*valp); |
| 219 | } |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 220 | #endif |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | #ifdef LATENCY |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 225 | TRACEPOINT_PROBE(raw_syscalls, sys_exit) { |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 226 | struct syscall_entry_t *e; |
| 227 | struct info_t *info, zero = {}; |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 228 | u64 pid = bpf_get_current_pid_tgid(), id; |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 229 | PID_FILTER |
| 230 | e = sysentry.lookup(&pid); |
| 231 | if (!e) { |
| 232 | return 0; // missed the entry event |
| 233 | } |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 234 | id = e->id; |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 235 | info = systimes.lookup_or_try_init(&id, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 236 | if (info) { |
| 237 | info->num_calls += 1; |
| 238 | info->total_ns += bpf_ktime_get_ns() - e->timestamp; |
| 239 | } |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 240 | sysentry.delete(&pid); |
| 241 | return 0; |
| 242 | } |
| 243 | #endif // LATENCY |
| 244 | #endif // SYSCALLS |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 245 | """.replace("READ_CLASS", read_class) \ |
| 246 | .replace("READ_METHOD", read_method) \ |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 247 | .replace("PID_FILTER", "if ((pid >> 32) != %d) { return 0; }" % args.pid) \ |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 248 | .replace("DEFINE_NOLANG", "#define NOLANG" if not language else "") \ |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 249 | .replace("DEFINE_LATENCY", "#define LATENCY" if args.latency else "") \ |
| 250 | .replace("DEFINE_SYSCALLS", "#define SYSCALLS" if args.syscalls else "") |
| 251 | |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 252 | if language: |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 253 | usdt = USDT(pid=args.pid) |
Sasha Goldshtein | dc3a57c | 2017-02-08 16:02:11 -0500 | [diff] [blame] | 254 | usdt.enable_probe_or_bail(entry_probe, "trace_entry") |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 255 | if args.latency: |
Sasha Goldshtein | dc3a57c | 2017-02-08 16:02:11 -0500 | [diff] [blame] | 256 | usdt.enable_probe_or_bail(return_probe, "trace_return") |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 257 | else: |
| 258 | usdt = None |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 259 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 260 | if args.ebpf or args.verbose: |
| 261 | if args.verbose and usdt: |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 262 | print(usdt.get_text()) |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 263 | print(program) |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 264 | if args.ebpf: |
| 265 | exit() |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 266 | |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 267 | bpf = BPF(text=program, usdt_contexts=[usdt] if usdt else []) |
| 268 | if args.syscalls: |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 269 | print("Attached kernel tracepoints for syscall tracing.") |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 270 | |
| 271 | def get_data(): |
| 272 | # Will be empty when no language was specified for tracing |
| 273 | if args.latency: |
jeromemarchand | 4e4c9e0 | 2018-07-19 22:20:54 +0200 | [diff] [blame] | 274 | data = list(map(lambda kv: (kv[0].clazz.decode('utf-8', 'replace') \ |
| 275 | + "." + \ |
| 276 | kv[0].method.decode('utf-8', 'replace'), |
Rafael Fonseca | 0d66906 | 2017-02-13 15:52:04 +0100 | [diff] [blame] | 277 | (kv[1].num_calls, kv[1].total_ns)), |
Rafael Fonseca | 42900ae | 2017-02-13 15:46:54 +0100 | [diff] [blame] | 278 | bpf["times"].items())) |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 279 | else: |
jeromemarchand | 4e4c9e0 | 2018-07-19 22:20:54 +0200 | [diff] [blame] | 280 | data = list(map(lambda kv: (kv[0].clazz.decode('utf-8', 'replace') \ |
| 281 | + "." + \ |
| 282 | kv[0].method.decode('utf-8', 'replace'), |
Rafael Fonseca | 0d66906 | 2017-02-13 15:52:04 +0100 | [diff] [blame] | 283 | (kv[1].value, 0)), |
Rafael Fonseca | 42900ae | 2017-02-13 15:46:54 +0100 | [diff] [blame] | 284 | bpf["counts"].items())) |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 285 | |
| 286 | if args.syscalls: |
| 287 | if args.latency: |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 288 | syscalls = map(lambda kv: (syscall_name(kv[0].value).decode('utf-8', 'replace'), |
| 289 | (kv[1].num_calls, kv[1].total_ns)), |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 290 | bpf["systimes"].items()) |
| 291 | data.extend(syscalls) |
| 292 | else: |
William Cohen | 218f748 | 2018-12-06 13:41:01 -0500 | [diff] [blame] | 293 | syscalls = map(lambda kv: (syscall_name(kv[0].value).decode('utf-8', 'replace'), |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 294 | (kv[1].value, 0)), |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 295 | bpf["syscounts"].items()) |
| 296 | data.extend(syscalls) |
| 297 | |
Rafael Fonseca | 0d66906 | 2017-02-13 15:52:04 +0100 | [diff] [blame] | 298 | return sorted(data, key=lambda kv: kv[1][1 if args.latency else 0]) |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 299 | |
| 300 | def clear_data(): |
| 301 | if args.latency: |
| 302 | bpf["times"].clear() |
| 303 | else: |
| 304 | bpf["counts"].clear() |
| 305 | |
| 306 | if args.syscalls: |
| 307 | if args.latency: |
| 308 | bpf["systimes"].clear() |
| 309 | else: |
| 310 | bpf["syscounts"].clear() |
| 311 | |
| 312 | exit_signaled = False |
| 313 | print("Tracing calls in process %d (language: %s)... Ctrl-C to quit." % |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 314 | (args.pid, language or "none")) |
Geneviève Bastien | 830c1f7 | 2017-07-14 16:04:12 -0400 | [diff] [blame] | 315 | if extra_message: |
| 316 | print(extra_message) |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 317 | while True: |
| 318 | try: |
| 319 | sleep(args.interval or 99999999) |
| 320 | except KeyboardInterrupt: |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 321 | exit_signaled = True |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 322 | print() |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 323 | data = get_data() # [(function, (num calls, latency in ns))] |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 324 | if args.latency: |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 325 | time_col = "TIME (ms)" if args.milliseconds else "TIME (us)" |
| 326 | print("%-50s %8s %8s" % ("METHOD", "# CALLS", time_col)) |
| 327 | else: |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 328 | print("%-50s %8s" % ("METHOD", "# CALLS")) |
| 329 | if args.top: |
| 330 | data = data[-args.top:] |
| 331 | for key, value in data: |
| 332 | if args.latency: |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 333 | time = value[1] / 1000000.0 if args.milliseconds else \ |
| 334 | value[1] / 1000.0 |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 335 | print("%-50s %8d %6.2f" % (key, value[0], time)) |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 336 | else: |
Sasha Goldshtein | a245c79 | 2016-10-25 02:18:35 -0700 | [diff] [blame] | 337 | print("%-50s %8d" % (key, value[0])) |
| 338 | if args.interval and not exit_signaled: |
| 339 | clear_data() |
| 340 | else: |
| 341 | if args.syscalls: |
| 342 | print("Detaching kernel probes, please wait...") |
Sasha Goldshtein | c13d14f | 2016-10-17 04:13:48 -0700 | [diff] [blame] | 343 | exit() |