blob: 9f4d2582f0d56ebb7139bc2cfb1e6512573c7d78 [file] [log] [blame]
Sasha Goldshteine5d9d992016-10-17 09:04:37 -07001#!/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 Goldshteinc3252002017-02-08 13:22:40 -05007# USAGE: ugc [-v] [-m] [-M MSEC] [-F FILTER] {java,python,ruby,node} pid
Sasha Goldshteine5d9d992016-10-17 09:04:37 -07008#
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
14from __future__ import print_function
15import argparse
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020016from bcc import BPF, USDT, utils
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070017import ctypes as ct
18import time
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020019import os
20
21languages = ["java", "python", "ruby", "node"]
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070022
23examples = """examples:
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020024 ./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 Goldshteine5d9d992016-10-17 09:04:37 -070027"""
28parser = argparse.ArgumentParser(
29 description="Summarize garbage collection events in high-level languages.",
30 formatter_class=argparse.RawDescriptionHelpFormatter,
31 epilog=examples)
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020032parser.add_argument("-l", "--language", choices=languages,
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070033 help="language to trace")
34parser.add_argument("pid", type=int, help="process id to attach to")
35parser.add_argument("-v", "--verbose", action="store_true",
36 help="verbose mode: print the BPF program (for debugging purposes)")
37parser.add_argument("-m", "--milliseconds", action="store_true",
38 help="report times in milliseconds (default is microseconds)")
Sasha Goldshteinc3252002017-02-08 13:22:40 -050039parser.add_argument("-M", "--minimum", type=int, default=0,
40 help="display only GCs longer than this many milliseconds")
41parser.add_argument("-F", "--filter", type=str,
42 help="display only GCs whose description contains this text")
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070043args = parser.parse_args()
44
45usdt = USDT(pid=args.pid)
46
47program = """
48struct gc_event_t {
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070049 u64 probe_index;
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070050 u64 elapsed_ns;
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070051 u64 field1;
52 u64 field2;
53 u64 field3;
54 u64 field4;
55 char string1[32];
56 char string2[32];
57};
58struct entry_t {
59 u64 start_ns;
60 u64 field1;
61 u64 field2;
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070062};
63
64BPF_PERF_OUTPUT(gcs);
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070065BPF_HASH(entry, u64, struct entry_t);
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070066"""
67
68class Probe(object):
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070069 def __init__(self, begin, end, begin_save, end_save, formatter):
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070070 self.begin = begin
71 self.end = end
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070072 self.begin_save = begin_save
73 self.end_save = end_save
74 self.formatter = formatter
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070075
76 def generate(self):
77 text = """
78int trace_%s(struct pt_regs *ctx) {
79 u64 pid = bpf_get_current_pid_tgid();
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070080 struct entry_t e = {};
81 e.start_ns = bpf_ktime_get_ns();
82 %s
83 entry.update(&pid, &e);
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070084 return 0;
85}
86int trace_%s(struct pt_regs *ctx) {
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070087 u64 elapsed;
88 struct entry_t *e;
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070089 struct gc_event_t event = {};
90 u64 pid = bpf_get_current_pid_tgid();
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070091 e = entry.lookup(&pid);
92 if (!e) {
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070093 return 0; // missed the entry event on this thread
94 }
Sasha Goldshteinddef09a2016-10-20 16:07:23 -070095 elapsed = bpf_ktime_get_ns() - e->start_ns;
Sasha Goldshteinc3252002017-02-08 13:22:40 -050096 if (elapsed < %d) {
97 return 0;
98 }
Sasha Goldshteine5d9d992016-10-17 09:04:37 -070099 event.elapsed_ns = elapsed;
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700100 %s
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700101 gcs.perf_submit(ctx, &event, sizeof(event));
102 return 0;
103}
Sasha Goldshteinc3252002017-02-08 13:22:40 -0500104 """ % (self.begin, self.begin_save, self.end,
105 args.minimum * 1000000, self.end_save)
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700106 return text
107
108 def attach(self):
Sasha Goldshteindc3a57c2017-02-08 16:02:11 -0500109 usdt.enable_probe_or_bail(self.begin, "trace_%s" % self.begin)
110 usdt.enable_probe_or_bail(self.end, "trace_%s" % self.end)
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700111
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700112 def format(self, data):
113 return self.formatter(data)
114
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700115probes = []
116
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200117language = args.language
118if not language:
119 language = utils.detect_language(languages, args.pid)
120
Sasha Goldshteinbee71b22016-10-26 06:34:06 -0700121#
122# Java
123#
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200124if language == "java":
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700125 # 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 Chaignon956ca1c2017-03-04 20:07:56 +0100144
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 Goldshteinddef09a2016-10-20 16:07:23 -0700148 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 Goldshteinbee71b22016-10-26 06:34:06 -0700152#
153# Python
154#
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200155elif language == "python":
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700156 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 Chaignon956ca1c2017-03-04 20:07:56 +0100167
168 def formatter(event):
169 "gen %d GC collected %d objects" % \
170 (event.field1, event.field2)
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700171 probes.append(Probe("gc__start", "gc__done",
172 begin_save, end_save, formatter))
Sasha Goldshteinbee71b22016-10-26 06:34:06 -0700173#
174# Ruby
175#
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200176elif language == "ruby":
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700177 # Ruby GC probes do not have any additional information available.
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700178 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 Goldshteinbee71b22016-10-26 06:34:06 -0700182#
183# Node
184#
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200185elif language == "node":
Sasha Goldshtein88533472016-10-25 00:08:33 -0700186 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 Goldshteine5d9d992016-10-17 09:04:37 -0700197
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200198else:
199 print("No language detected; use -l to trace a language.")
200 exit(1)
201
202
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700203for probe in probes:
204 program += probe.generate()
205 probe.attach()
206
207if args.verbose:
208 print(usdt.get_text())
209 print(program)
210
211bpf = BPF(text=program, usdt_contexts=[usdt])
212print("Tracing garbage collections in %s process %d... Ctrl-C to quit." %
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200213 (language, args.pid))
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700214time_col = "TIME (ms)" if args.milliseconds else "TIME (us)"
Sasha Goldshteinc3252002017-02-08 13:22:40 -0500215print("%-8s %-8s %-40s" % ("START", time_col, "DESCRIPTION"))
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700216
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700217class GCEvent(ct.Structure):
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700218 _fields_ = [
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700219 ("probe_index", ct.c_ulonglong),
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700220 ("elapsed_ns", ct.c_ulonglong),
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700221 ("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 Goldshteine5d9d992016-10-17 09:04:37 -0700227 ]
228
229start_ts = time.time()
230
231def print_event(cpu, data, size):
Sasha Goldshteinddef09a2016-10-20 16:07:23 -0700232 event = ct.cast(data, ct.POINTER(GCEvent)).contents
Paul Chaignon956ca1c2017-03-04 20:07:56 +0100233 elapsed = event.elapsed_ns / 1000000 if args.milliseconds else \
234 event.elapsed_ns / 1000
Sasha Goldshteinc3252002017-02-08 13:22:40 -0500235 description = probes[event.probe_index].format(event)
Paul Chaignon956ca1c2017-03-04 20:07:56 +0100236 if args.filter and args.filter not in description:
Sasha Goldshteinc3252002017-02-08 13:22:40 -0500237 return
238 print("%-8.3f %-8.2f %s" % (time.time() - start_ts, elapsed, description))
Sasha Goldshteine5d9d992016-10-17 09:04:37 -0700239
240bpf["gcs"].open_perf_buffer(print_event)
241while 1:
Teng Qindbf00292018-02-28 21:47:50 -0800242 bpf.perf_buffer_poll()