Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # uobjnew Summarize object allocations in high-level languages. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 7 | # USAGE: uobjnew [-h] [-T TOP] [-v] {c,java,ruby,tcl} pid [interval] |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 8 | # |
| 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 | |
| 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 | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 17 | from time import sleep |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 18 | import os |
| 19 | |
| 20 | # C needs to be the last language. |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 21 | languages = ["c", "java", "ruby", "tcl"] |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 22 | |
| 23 | examples = """examples: |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 24 | ./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 Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 28 | """ |
| 29 | parser = argparse.ArgumentParser( |
| 30 | description="Summarize object allocations in high-level languages.", |
| 31 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 32 | epilog=examples) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 33 | parser.add_argument("-l", "--language", choices=languages, |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 34 | help="language to trace") |
| 35 | parser.add_argument("pid", type=int, help="process id to attach to") |
| 36 | parser.add_argument("interval", type=int, nargs='?', |
| 37 | help="print every specified number of seconds") |
| 38 | parser.add_argument("-C", "--top-count", type=int, |
| 39 | help="number of most frequently allocated types to print") |
| 40 | parser.add_argument("-S", "--top-size", type=int, |
| 41 | help="number of largest types by allocated bytes to print") |
| 42 | parser.add_argument("-v", "--verbose", action="store_true", |
| 43 | help="verbose mode: print the BPF program (for debugging purposes)") |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 44 | parser.add_argument("--ebpf", action="store_true", |
| 45 | help=argparse.SUPPRESS) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 46 | args = parser.parse_args() |
| 47 | |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 48 | language = args.language |
| 49 | if not language: |
| 50 | language = utils.detect_language(languages, args.pid) |
| 51 | |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 52 | program = """ |
| 53 | #include <linux/ptrace.h> |
| 54 | |
| 55 | struct key_t { |
| 56 | #if MALLOC_TRACING |
| 57 | u64 size; |
| 58 | #else |
| 59 | char name[50]; |
| 60 | #endif |
| 61 | }; |
| 62 | |
| 63 | struct val_t { |
| 64 | u64 total_size; |
| 65 | u64 num_allocs; |
| 66 | }; |
| 67 | |
| 68 | BPF_HASH(allocs, struct key_t, struct val_t); |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 69 | """.replace("MALLOC_TRACING", "1" if language == "c" else "0") |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 70 | |
| 71 | usdt = USDT(pid=args.pid) |
| 72 | |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 73 | # |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 74 | # C |
| 75 | # |
| 76 | if language == "c": |
| 77 | program += """ |
| 78 | int 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-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 82 | valp = allocs.lookup_or_try_init(&key, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 83 | if (valp) { |
| 84 | valp->total_size += size; |
| 85 | valp->num_allocs += 1; |
| 86 | } |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | """ |
| 90 | # |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 91 | # Java |
| 92 | # |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 93 | elif language == "java": |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 94 | program += """ |
| 95 | int alloc_entry(struct pt_regs *ctx) { |
| 96 | struct key_t key = {}; |
| 97 | struct val_t *valp, zero = {}; |
| 98 | u64 classptr = 0, size = 0; |
denghui.ddh | 039cef6 | 2021-11-09 21:11:40 +0800 | [diff] [blame] | 99 | u32 length = 0; |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 100 | bpf_usdt_readarg(2, ctx, &classptr); |
denghui.ddh | 039cef6 | 2021-11-09 21:11:40 +0800 | [diff] [blame] | 101 | bpf_usdt_readarg(3, ctx, &length); |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 102 | bpf_usdt_readarg(4, ctx, &size); |
denghui.ddh | 039cef6 | 2021-11-09 21:11:40 +0800 | [diff] [blame] | 103 | bpf_probe_read_user(&key.name, min(sizeof(key.name), (size_t)length), (void *)classptr); |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 104 | valp = allocs.lookup_or_try_init(&key, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 105 | if (valp) { |
| 106 | valp->total_size += size; |
| 107 | valp->num_allocs += 1; |
| 108 | } |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | """ |
Sasha Goldshtein | dc3a57c | 2017-02-08 16:02:11 -0500 | [diff] [blame] | 112 | usdt.enable_probe_or_bail("object__alloc", "alloc_entry") |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 113 | # |
| 114 | # Ruby |
| 115 | # |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 116 | elif language == "ruby": |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 117 | create_template = """ |
| 118 | int THETHING_alloc_entry(struct pt_regs *ctx) { |
| 119 | struct key_t key = { .name = "THETHING" }; |
| 120 | struct val_t *valp, zero = {}; |
| 121 | u64 size = 0; |
| 122 | bpf_usdt_readarg(1, ctx, &size); |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 123 | valp = allocs.lookup_or_try_init(&key, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 124 | if (valp) { |
| 125 | valp->total_size += size; |
| 126 | valp->num_allocs += 1; |
| 127 | } |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | """ |
| 131 | program += """ |
| 132 | int object_alloc_entry(struct pt_regs *ctx) { |
| 133 | struct key_t key = {}; |
| 134 | struct val_t *valp, zero = {}; |
| 135 | u64 classptr = 0; |
| 136 | bpf_usdt_readarg(1, ctx, &classptr); |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 137 | bpf_probe_read_user(&key.name, sizeof(key.name), (void *)classptr); |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 138 | valp = allocs.lookup_or_try_init(&key, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 139 | if (valp) { |
| 140 | valp->num_allocs += 1; // We don't know the size, unfortunately |
| 141 | } |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | """ |
Sasha Goldshtein | dc3a57c | 2017-02-08 16:02:11 -0500 | [diff] [blame] | 145 | usdt.enable_probe_or_bail("object__create", "object_alloc_entry") |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 146 | for thing in ["string", "hash", "array"]: |
| 147 | program += create_template.replace("THETHING", thing) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 148 | usdt.enable_probe_or_bail("%s__create" % thing, |
| 149 | "%s_alloc_entry" % thing) |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 150 | # |
| 151 | # Tcl |
| 152 | # |
| 153 | elif language == "tcl": |
| 154 | program += """ |
| 155 | int alloc_entry(struct pt_regs *ctx) { |
| 156 | struct key_t key = { .name = "<ALL>" }; |
| 157 | struct val_t *valp, zero = {}; |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 158 | valp = allocs.lookup_or_try_init(&key, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 159 | if (valp) { |
| 160 | valp->num_allocs += 1; |
| 161 | } |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | """ |
| 165 | usdt.enable_probe_or_bail("obj__create", "alloc_entry") |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 166 | else: |
| 167 | print("No language detected; use -l to trace a language.") |
| 168 | exit(1) |
| 169 | |
| 170 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 171 | if args.ebpf or args.verbose: |
| 172 | if args.verbose: |
| 173 | print(usdt.get_text()) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 174 | print(program) |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 175 | if args.ebpf: |
| 176 | exit() |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 177 | |
| 178 | bpf = BPF(text=program, usdt_contexts=[usdt]) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 179 | if language == "c": |
Sasha Goldshtein | 39ace6f | 2016-12-19 09:52:34 +0000 | [diff] [blame] | 180 | bpf.attach_uprobe(name="c", sym="malloc", fn_name="alloc_entry", |
| 181 | pid=args.pid) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 182 | |
| 183 | exit_signaled = False |
| 184 | print("Tracing allocations in process %d (language: %s)... Ctrl-C to quit." % |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 185 | (args.pid, language or "none")) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 186 | while True: |
| 187 | try: |
| 188 | sleep(args.interval or 99999999) |
| 189 | except KeyboardInterrupt: |
| 190 | exit_signaled = True |
| 191 | print() |
| 192 | data = bpf["allocs"] |
| 193 | if args.top_count: |
Rafael Fonseca | c465a24 | 2017-02-13 16:04:33 +0100 | [diff] [blame] | 194 | data = sorted(data.items(), key=lambda kv: kv[1].num_allocs) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 195 | data = data[-args.top_count:] |
| 196 | elif args.top_size: |
Rafael Fonseca | c465a24 | 2017-02-13 16:04:33 +0100 | [diff] [blame] | 197 | data = sorted(data.items(), key=lambda kv: kv[1].total_size) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 198 | data = data[-args.top_size:] |
| 199 | else: |
Rafael Fonseca | c465a24 | 2017-02-13 16:04:33 +0100 | [diff] [blame] | 200 | data = sorted(data.items(), key=lambda kv: kv[1].total_size) |
Marko Myllynen | 9f3662e | 2018-10-10 21:48:53 +0300 | [diff] [blame] | 201 | print("%-30s %8s %12s" % ("NAME/TYPE", "# ALLOCS", "# BYTES")) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 202 | for key, value in data: |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 203 | if language == "c": |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 204 | obj_type = "block size %d" % key.size |
| 205 | else: |
| 206 | obj_type = key.name |
| 207 | print("%-30s %8d %12d" % |
| 208 | (obj_type, value.num_allocs, value.total_size)) |
| 209 | if args.interval and not exit_signaled: |
| 210 | bpf["allocs"].clear() |
| 211 | else: |
| 212 | exit() |