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