Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # uobjnew Summarize object allocations in high-level languages. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: uobjnew [-h] [-T TOP] [-v] {java,ruby,c} pid [interval] |
| 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 |
| 16 | from bcc import BPF, USDT |
| 17 | from time import sleep |
| 18 | |
| 19 | examples = """examples: |
| 20 | ./uobjnew -l java 145 # summarize Java allocations in process 145 |
| 21 | ./uobjnew -l c 2020 1 # grab malloc() sizes and print every second |
| 22 | ./uobjnew -l ruby 6712 -C 10 # top 10 Ruby types by number of allocations |
| 23 | ./uobjnew -l ruby 6712 -S 10 # top 10 Ruby types by total size |
| 24 | """ |
| 25 | parser = argparse.ArgumentParser( |
| 26 | description="Summarize object allocations in high-level languages.", |
| 27 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 28 | epilog=examples) |
| 29 | parser.add_argument("language", choices=["java", "ruby", "c"], |
| 30 | help="language to trace") |
| 31 | parser.add_argument("pid", type=int, help="process id to attach to") |
| 32 | parser.add_argument("interval", type=int, nargs='?', |
| 33 | help="print every specified number of seconds") |
| 34 | parser.add_argument("-C", "--top-count", type=int, |
| 35 | help="number of most frequently allocated types to print") |
| 36 | parser.add_argument("-S", "--top-size", type=int, |
| 37 | help="number of largest types by allocated bytes to print") |
| 38 | parser.add_argument("-v", "--verbose", action="store_true", |
| 39 | help="verbose mode: print the BPF program (for debugging purposes)") |
| 40 | args = parser.parse_args() |
| 41 | |
| 42 | program = """ |
| 43 | #include <linux/ptrace.h> |
| 44 | |
| 45 | struct key_t { |
| 46 | #if MALLOC_TRACING |
| 47 | u64 size; |
| 48 | #else |
| 49 | char name[50]; |
| 50 | #endif |
| 51 | }; |
| 52 | |
| 53 | struct val_t { |
| 54 | u64 total_size; |
| 55 | u64 num_allocs; |
| 56 | }; |
| 57 | |
| 58 | BPF_HASH(allocs, struct key_t, struct val_t); |
| 59 | """.replace("MALLOC_TRACING", "1" if args.language == "c" else "0") |
| 60 | |
| 61 | usdt = USDT(pid=args.pid) |
| 62 | |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 63 | # |
| 64 | # Java |
| 65 | # |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 66 | if args.language == "java": |
| 67 | program += """ |
| 68 | int alloc_entry(struct pt_regs *ctx) { |
| 69 | struct key_t key = {}; |
| 70 | struct val_t *valp, zero = {}; |
| 71 | u64 classptr = 0, size = 0; |
| 72 | bpf_usdt_readarg(2, ctx, &classptr); |
| 73 | bpf_usdt_readarg(4, ctx, &size); |
| 74 | bpf_probe_read(&key.name, sizeof(key.name), (void *)classptr); |
| 75 | valp = allocs.lookup_or_init(&key, &zero); |
| 76 | valp->total_size += size; |
| 77 | valp->num_allocs += 1; |
| 78 | return 0; |
| 79 | } |
| 80 | """ |
| 81 | usdt.enable_probe("object__alloc", "alloc_entry") |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 82 | # |
| 83 | # Ruby |
| 84 | # |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 85 | elif args.language == "ruby": |
| 86 | create_template = """ |
| 87 | int THETHING_alloc_entry(struct pt_regs *ctx) { |
| 88 | struct key_t key = { .name = "THETHING" }; |
| 89 | struct val_t *valp, zero = {}; |
| 90 | u64 size = 0; |
| 91 | bpf_usdt_readarg(1, ctx, &size); |
| 92 | valp = allocs.lookup_or_init(&key, &zero); |
| 93 | valp->total_size += size; |
| 94 | valp->num_allocs += 1; |
| 95 | return 0; |
| 96 | } |
| 97 | """ |
| 98 | program += """ |
| 99 | int object_alloc_entry(struct pt_regs *ctx) { |
| 100 | struct key_t key = {}; |
| 101 | struct val_t *valp, zero = {}; |
| 102 | u64 classptr = 0; |
| 103 | bpf_usdt_readarg(1, ctx, &classptr); |
| 104 | bpf_probe_read(&key.name, sizeof(key.name), (void *)classptr); |
| 105 | valp = allocs.lookup_or_init(&key, &zero); |
| 106 | valp->num_allocs += 1; // We don't know the size, unfortunately |
| 107 | return 0; |
| 108 | } |
| 109 | """ |
| 110 | usdt.enable_probe("object__create", "object_alloc_entry") |
| 111 | for thing in ["string", "hash", "array"]: |
| 112 | program += create_template.replace("THETHING", thing) |
| 113 | usdt.enable_probe("%s__create" % thing, "%s_alloc_entry" % thing) |
Sasha Goldshtein | bee71b2 | 2016-10-26 06:34:06 -0700 | [diff] [blame] | 114 | # |
| 115 | # C |
| 116 | # |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 117 | elif args.language == "c": |
| 118 | program += """ |
| 119 | int alloc_entry(struct pt_regs *ctx, size_t size) { |
| 120 | struct key_t key = {}; |
| 121 | struct val_t *valp, zero = {}; |
| 122 | key.size = size; |
| 123 | valp = allocs.lookup_or_init(&key, &zero); |
| 124 | valp->total_size += size; |
| 125 | valp->num_allocs += 1; |
| 126 | return 0; |
| 127 | } |
| 128 | """ |
| 129 | |
| 130 | if args.verbose: |
| 131 | print(usdt.get_text()) |
| 132 | print(program) |
| 133 | |
| 134 | bpf = BPF(text=program, usdt_contexts=[usdt]) |
| 135 | if args.language == "c": |
Sasha Goldshtein | 39ace6f | 2016-12-19 09:52:34 +0000 | [diff] [blame^] | 136 | bpf.attach_uprobe(name="c", sym="malloc", fn_name="alloc_entry", |
| 137 | pid=args.pid) |
Sasha Goldshtein | 2e7e240 | 2016-10-25 05:28:29 -0700 | [diff] [blame] | 138 | |
| 139 | exit_signaled = False |
| 140 | print("Tracing allocations in process %d (language: %s)... Ctrl-C to quit." % |
| 141 | (args.pid, args.language or "none")) |
| 142 | while True: |
| 143 | try: |
| 144 | sleep(args.interval or 99999999) |
| 145 | except KeyboardInterrupt: |
| 146 | exit_signaled = True |
| 147 | print() |
| 148 | data = bpf["allocs"] |
| 149 | if args.top_count: |
| 150 | data = sorted(data.items(), key=lambda (k, v): v.num_allocs) |
| 151 | data = data[-args.top_count:] |
| 152 | elif args.top_size: |
| 153 | data = sorted(data.items(), key=lambda (k, v): v.total_size) |
| 154 | data = data[-args.top_size:] |
| 155 | else: |
| 156 | data = sorted(data.items(), key=lambda (k, v): v.total_size) |
| 157 | print("%-30s %8s %12s" % ("TYPE", "# ALLOCS", "# BYTES")) |
| 158 | for key, value in data: |
| 159 | if args.language == "c": |
| 160 | obj_type = "block size %d" % key.size |
| 161 | else: |
| 162 | obj_type = key.name |
| 163 | print("%-30s %8d %12d" % |
| 164 | (obj_type, value.num_allocs, value.total_size)) |
| 165 | if args.interval and not exit_signaled: |
| 166 | bpf["allocs"].clear() |
| 167 | else: |
| 168 | exit() |