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