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