Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # ugc Summarize garbage collection events in high-level languages. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 7 | # USAGE: ugc [-v] [-m] [-M MSEC] [-F FILTER] {java,python,ruby,node} pid |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 8 | # |
| 9 | # Copyright 2016 Sasha Goldshtein |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
| 12 | # 19-Oct-2016 Sasha Goldshtein Created this. |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | import argparse |
| 16 | from bcc import BPF, USDT |
| 17 | import ctypes as ct |
| 18 | import time |
| 19 | |
| 20 | examples = """examples: |
| 21 | ./ugc java 185 # trace Java GCs in process 185 |
| 22 | ./ugc ruby 1344 -m # trace Ruby GCs reporting in ms |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 23 | ./ugc -M 10 java 185 # trace only Java GCs longer than 10ms |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 24 | """ |
| 25 | parser = argparse.ArgumentParser( |
| 26 | description="Summarize garbage collection events in high-level languages.", |
| 27 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 28 | epilog=examples) |
Sasha Goldshtein | 8853347 | 2016-10-25 00:08:33 -0700 | [diff] [blame] | 29 | parser.add_argument("language", choices=["java", "python", "ruby", "node"], |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 30 | help="language to trace") |
| 31 | parser.add_argument("pid", type=int, help="process id to attach to") |
| 32 | parser.add_argument("-v", "--verbose", action="store_true", |
| 33 | help="verbose mode: print the BPF program (for debugging purposes)") |
| 34 | parser.add_argument("-m", "--milliseconds", action="store_true", |
| 35 | help="report times in milliseconds (default is microseconds)") |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 36 | parser.add_argument("-M", "--minimum", type=int, default=0, |
| 37 | help="display only GCs longer than this many milliseconds") |
| 38 | parser.add_argument("-F", "--filter", type=str, |
| 39 | help="display only GCs whose description contains this text") |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 40 | args = parser.parse_args() |
| 41 | |
| 42 | usdt = USDT(pid=args.pid) |
| 43 | |
| 44 | program = """ |
| 45 | struct gc_event_t { |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 46 | u64 probe_index; |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 47 | u64 elapsed_ns; |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 48 | u64 field1; |
| 49 | u64 field2; |
| 50 | u64 field3; |
| 51 | u64 field4; |
| 52 | char string1[32]; |
| 53 | char string2[32]; |
| 54 | }; |
| 55 | struct entry_t { |
| 56 | u64 start_ns; |
| 57 | u64 field1; |
| 58 | u64 field2; |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | BPF_PERF_OUTPUT(gcs); |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 62 | BPF_HASH(entry, u64, struct entry_t); |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 63 | """ |
| 64 | |
| 65 | class Probe(object): |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 66 | def __init__(self, begin, end, begin_save, end_save, formatter): |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 67 | self.begin = begin |
| 68 | self.end = end |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 69 | self.begin_save = begin_save |
| 70 | self.end_save = end_save |
| 71 | self.formatter = formatter |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 72 | |
| 73 | def generate(self): |
| 74 | text = """ |
| 75 | int trace_%s(struct pt_regs *ctx) { |
| 76 | u64 pid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 77 | struct entry_t e = {}; |
| 78 | e.start_ns = bpf_ktime_get_ns(); |
| 79 | %s |
| 80 | entry.update(&pid, &e); |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | int trace_%s(struct pt_regs *ctx) { |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 84 | u64 elapsed; |
| 85 | struct entry_t *e; |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 86 | struct gc_event_t event = {}; |
| 87 | u64 pid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 88 | e = entry.lookup(&pid); |
| 89 | if (!e) { |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 90 | return 0; // missed the entry event on this thread |
| 91 | } |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 92 | elapsed = bpf_ktime_get_ns() - e->start_ns; |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 93 | if (elapsed < %d) { |
| 94 | return 0; |
| 95 | } |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 96 | event.elapsed_ns = elapsed; |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 97 | %s |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 98 | gcs.perf_submit(ctx, &event, sizeof(event)); |
| 99 | return 0; |
| 100 | } |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 101 | """ % (self.begin, self.begin_save, self.end, |
| 102 | args.minimum * 1000000, self.end_save) |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 103 | return text |
| 104 | |
| 105 | def attach(self): |
Sasha Goldshtein | dc3a57c | 2017-02-08 16:02:11 -0500 | [diff] [blame] | 106 | usdt.enable_probe_or_bail(self.begin, "trace_%s" % self.begin) |
| 107 | usdt.enable_probe_or_bail(self.end, "trace_%s" % self.end) |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 108 | |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 109 | def format(self, data): |
| 110 | return self.formatter(data) |
| 111 | |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 112 | probes = [] |
| 113 | |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 114 | # |
| 115 | # Java |
| 116 | # |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 117 | if args.language == "java": |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 118 | # Oddly, the gc__begin/gc__end probes don't really have any useful |
| 119 | # information, while the mem__pool* ones do. There's also a bunch of |
| 120 | # probes described in the hotspot_gc*.stp file which aren't there |
| 121 | # when looking at a live Java process. |
| 122 | begin_save = """ |
| 123 | bpf_usdt_readarg(6, ctx, &e.field1); // used bytes |
| 124 | bpf_usdt_readarg(8, ctx, &e.field2); // max bytes |
| 125 | """ |
| 126 | end_save = """ |
| 127 | event.field1 = e->field1; // used bytes at start |
| 128 | event.field2 = e->field2; // max bytes at start |
| 129 | bpf_usdt_readarg(6, ctx, &event.field3); // used bytes at end |
| 130 | bpf_usdt_readarg(8, ctx, &event.field4); // max bytes at end |
| 131 | u64 manager = 0, pool = 0; |
| 132 | bpf_usdt_readarg(1, ctx, &manager); // ptr to manager name |
| 133 | bpf_usdt_readarg(3, ctx, &pool); // ptr to pool name |
| 134 | bpf_probe_read(&event.string1, sizeof(event.string1), (void *)manager); |
| 135 | bpf_probe_read(&event.string2, sizeof(event.string2), (void *)pool); |
| 136 | """ |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 137 | |
| 138 | def formatter(e): |
| 139 | "%s %s used=%d->%d max=%d->%d" % \ |
| 140 | (e.string1, e.string2, e.field1, e.field3, e.field2, e.field4) |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 141 | probes.append(Probe("mem__pool__gc__begin", "mem__pool__gc__end", |
| 142 | begin_save, end_save, formatter)) |
| 143 | probes.append(Probe("gc__begin", "gc__end", |
| 144 | "", "", lambda _: "no additional info available")) |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 145 | # |
| 146 | # Python |
| 147 | # |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 148 | elif args.language == "python": |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 149 | begin_save = """ |
| 150 | int gen = 0; |
| 151 | bpf_usdt_readarg(1, ctx, &gen); |
| 152 | e.field1 = gen; |
| 153 | """ |
| 154 | end_save = """ |
| 155 | long objs = 0; |
| 156 | bpf_usdt_readarg(1, ctx, &objs); |
| 157 | event.field1 = e->field1; |
| 158 | event.field2 = objs; |
| 159 | """ |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 160 | |
| 161 | def formatter(event): |
| 162 | "gen %d GC collected %d objects" % \ |
| 163 | (event.field1, event.field2) |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 164 | probes.append(Probe("gc__start", "gc__done", |
| 165 | begin_save, end_save, formatter)) |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 166 | # |
| 167 | # Ruby |
| 168 | # |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 169 | elif args.language == "ruby": |
| 170 | # Ruby GC probes do not have any additional information available. |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 171 | probes.append(Probe("gc__mark__begin", "gc__mark__end", |
| 172 | "", "", lambda _: "GC mark stage")) |
| 173 | probes.append(Probe("gc__sweep__begin", "gc__sweep__end", |
| 174 | "", "", lambda _: "GC sweep stage")) |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 175 | # |
| 176 | # Node |
| 177 | # |
Sasha Goldshtein | 8853347 | 2016-10-25 00:08:33 -0700 | [diff] [blame] | 178 | elif args.language == "node": |
| 179 | end_save = """ |
| 180 | u32 gc_type = 0; |
| 181 | bpf_usdt_readarg(1, ctx, &gc_type); |
| 182 | event.field1 = gc_type; |
| 183 | """ |
| 184 | descs = {"GC scavenge": 1, "GC mark-sweep-compact": 2, |
| 185 | "GC incremental mark": 4, "GC weak callbacks": 8} |
| 186 | probes.append(Probe("gc__start", "gc__done", "", end_save, |
| 187 | lambda e: str.join(", ", |
| 188 | [desc for desc, val in descs.items() |
| 189 | if e.field1 & val != 0]))) |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 190 | |
| 191 | for probe in probes: |
| 192 | program += probe.generate() |
| 193 | probe.attach() |
| 194 | |
| 195 | if args.verbose: |
| 196 | print(usdt.get_text()) |
| 197 | print(program) |
| 198 | |
| 199 | bpf = BPF(text=program, usdt_contexts=[usdt]) |
| 200 | print("Tracing garbage collections in %s process %d... Ctrl-C to quit." % |
| 201 | (args.language, args.pid)) |
| 202 | time_col = "TIME (ms)" if args.milliseconds else "TIME (us)" |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 203 | print("%-8s %-8s %-40s" % ("START", time_col, "DESCRIPTION")) |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 204 | |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 205 | class GCEvent(ct.Structure): |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 206 | _fields_ = [ |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 207 | ("probe_index", ct.c_ulonglong), |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 208 | ("elapsed_ns", ct.c_ulonglong), |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 209 | ("field1", ct.c_ulonglong), |
| 210 | ("field2", ct.c_ulonglong), |
| 211 | ("field3", ct.c_ulonglong), |
| 212 | ("field4", ct.c_ulonglong), |
| 213 | ("string1", ct.c_char * 32), |
| 214 | ("string2", ct.c_char * 32) |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 215 | ] |
| 216 | |
| 217 | start_ts = time.time() |
| 218 | |
| 219 | def print_event(cpu, data, size): |
Sasha Goldshtein | ddef09a | 2016-10-20 16:07:23 -0700 | [diff] [blame] | 220 | event = ct.cast(data, ct.POINTER(GCEvent)).contents |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 221 | elapsed = event.elapsed_ns / 1000000 if args.milliseconds else \ |
| 222 | event.elapsed_ns / 1000 |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 223 | description = probes[event.probe_index].format(event) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 224 | if args.filter and args.filter not in description: |
Sasha Goldshtein | c325200 | 2017-02-08 13:22:40 -0500 | [diff] [blame] | 225 | return |
| 226 | print("%-8.3f %-8.2f %s" % (time.time() - start_ts, elapsed, description)) |
Sasha Goldshtein | e5d9d99 | 2016-10-17 09:04:37 -0700 | [diff] [blame] | 227 | |
| 228 | bpf["gcs"].open_perf_buffer(print_event) |
| 229 | while 1: |
| 230 | bpf.kprobe_poll() |