blob: b8eed0f7457d0266ef6fbca52307d2c745a991bb [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -07002# @lint-avoid-python-3-compatibility-imports
3#
4# uobjnew Summarize object allocations in high-level languages.
5# For Linux, uses BCC, eBPF.
6#
Marko Myllynen9f3662e2018-10-10 21:48:53 +03007# USAGE: uobjnew [-h] [-T TOP] [-v] {c,java,ruby,tcl} pid [interval]
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -07008#
9# Copyright 2016 Sasha Goldshtein
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
12# 25-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 Goldshtein2e7e2402016-10-25 05:28:29 -070017from time import sleep
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020018import os
19
20# C needs to be the last language.
Marko Myllynen9f3662e2018-10-10 21:48:53 +030021languages = ["c", "java", "ruby", "tcl"]
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070022
23examples = """examples:
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020024 ./uobjnew -l java 145 # summarize Java allocations in process 145
25 ./uobjnew -l c 2020 1 # grab malloc() sizes and print every second
26 ./uobjnew -l ruby 6712 -C 10 # top 10 Ruby types by number of allocations
27 ./uobjnew -l ruby 6712 -S 10 # top 10 Ruby types by total size
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070028"""
29parser = argparse.ArgumentParser(
30 description="Summarize object allocations in high-level languages.",
31 formatter_class=argparse.RawDescriptionHelpFormatter,
32 epilog=examples)
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020033parser.add_argument("-l", "--language", choices=languages,
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070034 help="language to trace")
35parser.add_argument("pid", type=int, help="process id to attach to")
36parser.add_argument("interval", type=int, nargs='?',
37 help="print every specified number of seconds")
38parser.add_argument("-C", "--top-count", type=int,
39 help="number of most frequently allocated types to print")
40parser.add_argument("-S", "--top-size", type=int,
41 help="number of largest types by allocated bytes to print")
42parser.add_argument("-v", "--verbose", action="store_true",
43 help="verbose mode: print the BPF program (for debugging purposes)")
Marko Myllynen27e7aea2018-09-26 20:09:07 +030044parser.add_argument("--ebpf", action="store_true",
45 help=argparse.SUPPRESS)
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070046args = parser.parse_args()
47
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020048language = args.language
49if not language:
50 language = utils.detect_language(languages, args.pid)
51
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070052program = """
53#include <linux/ptrace.h>
54
55struct key_t {
56#if MALLOC_TRACING
57 u64 size;
58#else
59 char name[50];
60#endif
61};
62
63struct val_t {
64 u64 total_size;
65 u64 num_allocs;
66};
67
68BPF_HASH(allocs, struct key_t, struct val_t);
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020069""".replace("MALLOC_TRACING", "1" if language == "c" else "0")
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070070
71usdt = USDT(pid=args.pid)
72
Sasha Goldshteinbee71b22016-10-26 06:34:06 -070073#
Marko Myllynen27e7aea2018-09-26 20:09:07 +030074# C
75#
76if language == "c":
77 program += """
78int alloc_entry(struct pt_regs *ctx, size_t size) {
79 struct key_t key = {};
80 struct val_t *valp, zero = {};
81 key.size = size;
yonghong-song82f43022019-10-31 08:16:12 -070082 valp = allocs.lookup_or_try_init(&key, &zero);
Philip Gladstoneba64f032019-09-20 01:12:01 -040083 if (valp) {
84 valp->total_size += size;
85 valp->num_allocs += 1;
86 }
Marko Myllynen27e7aea2018-09-26 20:09:07 +030087 return 0;
88}
89 """
90#
Sasha Goldshteinbee71b22016-10-26 06:34:06 -070091# Java
92#
Marko Myllynen27e7aea2018-09-26 20:09:07 +030093elif language == "java":
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -070094 program += """
95int alloc_entry(struct pt_regs *ctx) {
96 struct key_t key = {};
97 struct val_t *valp, zero = {};
98 u64 classptr = 0, size = 0;
99 bpf_usdt_readarg(2, ctx, &classptr);
100 bpf_usdt_readarg(4, ctx, &size);
101 bpf_probe_read(&key.name, sizeof(key.name), (void *)classptr);
yonghong-song82f43022019-10-31 08:16:12 -0700102 valp = allocs.lookup_or_try_init(&key, &zero);
Philip Gladstoneba64f032019-09-20 01:12:01 -0400103 if (valp) {
104 valp->total_size += size;
105 valp->num_allocs += 1;
106 }
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700107 return 0;
108}
109 """
Sasha Goldshteindc3a57c2017-02-08 16:02:11 -0500110 usdt.enable_probe_or_bail("object__alloc", "alloc_entry")
Sasha Goldshteinbee71b22016-10-26 06:34:06 -0700111#
112# Ruby
113#
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200114elif language == "ruby":
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700115 create_template = """
116int THETHING_alloc_entry(struct pt_regs *ctx) {
117 struct key_t key = { .name = "THETHING" };
118 struct val_t *valp, zero = {};
119 u64 size = 0;
120 bpf_usdt_readarg(1, ctx, &size);
yonghong-song82f43022019-10-31 08:16:12 -0700121 valp = allocs.lookup_or_try_init(&key, &zero);
Philip Gladstoneba64f032019-09-20 01:12:01 -0400122 if (valp) {
123 valp->total_size += size;
124 valp->num_allocs += 1;
125 }
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700126 return 0;
127}
128 """
129 program += """
130int object_alloc_entry(struct pt_regs *ctx) {
131 struct key_t key = {};
132 struct val_t *valp, zero = {};
133 u64 classptr = 0;
134 bpf_usdt_readarg(1, ctx, &classptr);
135 bpf_probe_read(&key.name, sizeof(key.name), (void *)classptr);
yonghong-song82f43022019-10-31 08:16:12 -0700136 valp = allocs.lookup_or_try_init(&key, &zero);
Philip Gladstoneba64f032019-09-20 01:12:01 -0400137 if (valp) {
138 valp->num_allocs += 1; // We don't know the size, unfortunately
139 }
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700140 return 0;
141}
142 """
Sasha Goldshteindc3a57c2017-02-08 16:02:11 -0500143 usdt.enable_probe_or_bail("object__create", "object_alloc_entry")
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700144 for thing in ["string", "hash", "array"]:
145 program += create_template.replace("THETHING", thing)
Paul Chaignon956ca1c2017-03-04 20:07:56 +0100146 usdt.enable_probe_or_bail("%s__create" % thing,
147 "%s_alloc_entry" % thing)
Marko Myllynen9f3662e2018-10-10 21:48:53 +0300148#
149# Tcl
150#
151elif language == "tcl":
152 program += """
153int alloc_entry(struct pt_regs *ctx) {
154 struct key_t key = { .name = "<ALL>" };
155 struct val_t *valp, zero = {};
yonghong-song82f43022019-10-31 08:16:12 -0700156 valp = allocs.lookup_or_try_init(&key, &zero);
Philip Gladstoneba64f032019-09-20 01:12:01 -0400157 if (valp) {
158 valp->num_allocs += 1;
159 }
Marko Myllynen9f3662e2018-10-10 21:48:53 +0300160 return 0;
161}
162 """
163 usdt.enable_probe_or_bail("obj__create", "alloc_entry")
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200164else:
165 print("No language detected; use -l to trace a language.")
166 exit(1)
167
168
Marko Myllynen27e7aea2018-09-26 20:09:07 +0300169if args.ebpf or args.verbose:
170 if args.verbose:
171 print(usdt.get_text())
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700172 print(program)
Marko Myllynen27e7aea2018-09-26 20:09:07 +0300173 if args.ebpf:
174 exit()
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700175
176bpf = BPF(text=program, usdt_contexts=[usdt])
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200177if language == "c":
Sasha Goldshtein39ace6f2016-12-19 09:52:34 +0000178 bpf.attach_uprobe(name="c", sym="malloc", fn_name="alloc_entry",
179 pid=args.pid)
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700180
181exit_signaled = False
182print("Tracing allocations in process %d (language: %s)... Ctrl-C to quit." %
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200183 (args.pid, language or "none"))
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700184while True:
185 try:
186 sleep(args.interval or 99999999)
187 except KeyboardInterrupt:
188 exit_signaled = True
189 print()
190 data = bpf["allocs"]
191 if args.top_count:
Rafael Fonsecac465a242017-02-13 16:04:33 +0100192 data = sorted(data.items(), key=lambda kv: kv[1].num_allocs)
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700193 data = data[-args.top_count:]
194 elif args.top_size:
Rafael Fonsecac465a242017-02-13 16:04:33 +0100195 data = sorted(data.items(), key=lambda kv: kv[1].total_size)
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700196 data = data[-args.top_size:]
197 else:
Rafael Fonsecac465a242017-02-13 16:04:33 +0100198 data = sorted(data.items(), key=lambda kv: kv[1].total_size)
Marko Myllynen9f3662e2018-10-10 21:48:53 +0300199 print("%-30s %8s %12s" % ("NAME/TYPE", "# ALLOCS", "# BYTES"))
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700200 for key, value in data:
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200201 if language == "c":
Sasha Goldshtein2e7e2402016-10-25 05:28:29 -0700202 obj_type = "block size %d" % key.size
203 else:
204 obj_type = key.name
205 print("%-30s %8d %12d" %
206 (obj_type, value.num_allocs, value.total_size))
207 if args.interval and not exit_signaled:
208 bpf["allocs"].clear()
209 else:
210 exit()