Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # slabratetop Summarize kmem_cache_alloc() calls. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: slabratetop [-h] [-C] [-r MAXROWS] [interval] [count] |
| 8 | # |
| 9 | # This uses in-kernel BPF maps to store cache summaries for efficiency. |
| 10 | # |
| 11 | # SEE ALSO: slabtop(1), which shows the cache volumes. |
| 12 | # |
| 13 | # Copyright 2016 Netflix, Inc. |
| 14 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 15 | # |
| 16 | # 15-Oct-2016 Brendan Gregg Created this. |
| 17 | |
| 18 | from __future__ import print_function |
| 19 | from bcc import BPF |
Brenden Blanco | 42d6098 | 2017-04-24 14:31:28 -0700 | [diff] [blame] | 20 | from bcc.utils import printb |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 21 | from time import sleep, strftime |
| 22 | import argparse |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 23 | from subprocess import call |
| 24 | |
| 25 | # arguments |
| 26 | examples = """examples: |
| 27 | ./slabratetop # kmem_cache_alloc() top, 1 second refresh |
| 28 | ./slabratetop -C # don't clear the screen |
| 29 | ./slabratetop 5 # 5 second summaries |
| 30 | ./slabratetop 5 10 # 5 second summaries, 10 times only |
| 31 | """ |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description="Kernel SLAB/SLUB memory cache allocation rate top", |
| 34 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 35 | epilog=examples) |
| 36 | parser.add_argument("-C", "--noclear", action="store_true", |
| 37 | help="don't clear the screen") |
| 38 | parser.add_argument("-r", "--maxrows", default=20, |
| 39 | help="maximum rows to print, default 20") |
| 40 | parser.add_argument("interval", nargs="?", default=1, |
| 41 | help="output interval, in seconds") |
| 42 | parser.add_argument("count", nargs="?", default=99999999, |
| 43 | help="number of outputs") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 44 | parser.add_argument("--ebpf", action="store_true", |
| 45 | help=argparse.SUPPRESS) |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 46 | args = parser.parse_args() |
| 47 | interval = int(args.interval) |
| 48 | countdown = int(args.count) |
| 49 | maxrows = int(args.maxrows) |
| 50 | clear = not int(args.noclear) |
| 51 | debug = 0 |
| 52 | |
| 53 | # linux stats |
| 54 | loadavg = "/proc/loadavg" |
| 55 | |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 56 | # define BPF program |
| 57 | bpf_text = """ |
| 58 | #include <uapi/linux/ptrace.h> |
| 59 | #include <linux/mm.h> |
Jerome Marchand | 33393d3 | 2021-02-18 11:33:20 +0100 | [diff] [blame] | 60 | #include <linux/kasan.h> |
Daniel Rank | 788303e | 2020-09-27 16:55:22 -0700 | [diff] [blame] | 61 | |
| 62 | // memcg_cache_params is a part of kmem_cache, but is not publicly exposed in |
| 63 | // kernel versions 5.4 to 5.8. Define an empty struct for it here to allow the |
| 64 | // bpf program to compile. It has been completely removed in kernel version |
| 65 | // 5.9, but it does not hurt to have it here for versions 5.4 to 5.8. |
| 66 | struct memcg_cache_params {}; |
| 67 | |
Gary Lin | fa10345 | 2018-03-14 18:14:56 +0800 | [diff] [blame] | 68 | #ifdef CONFIG_SLUB |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 69 | #include <linux/slub_def.h> |
Gary Lin | fa10345 | 2018-03-14 18:14:56 +0800 | [diff] [blame] | 70 | #else |
| 71 | #include <linux/slab_def.h> |
| 72 | #endif |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 73 | |
| 74 | #define CACHE_NAME_SIZE 32 |
| 75 | |
| 76 | // the key for the output summary |
| 77 | struct info_t { |
| 78 | char name[CACHE_NAME_SIZE]; |
| 79 | }; |
| 80 | |
| 81 | // the value of the output summary |
| 82 | struct val_t { |
| 83 | u64 count; |
| 84 | u64 size; |
| 85 | }; |
| 86 | |
| 87 | BPF_HASH(counts, struct info_t, struct val_t); |
| 88 | |
| 89 | int kprobe__kmem_cache_alloc(struct pt_regs *ctx, struct kmem_cache *cachep) |
| 90 | { |
| 91 | struct info_t info = {}; |
Paul Chaignon | f86f7e8 | 2018-06-14 02:20:03 +0200 | [diff] [blame] | 92 | const char *name = cachep->name; |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 93 | bpf_probe_read_kernel(&info.name, sizeof(info.name), name); |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 94 | |
| 95 | struct val_t *valp, zero = {}; |
yonghong-song | 82f4302 | 2019-10-31 08:16:12 -0700 | [diff] [blame] | 96 | valp = counts.lookup_or_try_init(&info, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame] | 97 | if (valp) { |
| 98 | valp->count++; |
| 99 | valp->size += cachep->size; |
| 100 | } |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | """ |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 105 | if debug or args.ebpf: |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 106 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 107 | if args.ebpf: |
| 108 | exit() |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 109 | |
| 110 | # initialize BPF |
| 111 | b = BPF(text=bpf_text) |
| 112 | |
| 113 | print('Tracing... Output every %d secs. Hit Ctrl-C to end' % interval) |
| 114 | |
| 115 | # output |
| 116 | exiting = 0 |
| 117 | while 1: |
| 118 | try: |
| 119 | sleep(interval) |
| 120 | except KeyboardInterrupt: |
| 121 | exiting = 1 |
| 122 | |
| 123 | # header |
| 124 | if clear: |
| 125 | call("clear") |
| 126 | else: |
| 127 | print() |
| 128 | with open(loadavg) as stats: |
| 129 | print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read())) |
| 130 | print("%-32s %6s %10s" % ("CACHE", "ALLOCS", "BYTES")) |
| 131 | |
| 132 | # by-TID output |
| 133 | counts = b.get_table("counts") |
| 134 | line = 0 |
| 135 | for k, v in reversed(sorted(counts.items(), |
| 136 | key=lambda counts: counts[1].size)): |
Brenden Blanco | 42d6098 | 2017-04-24 14:31:28 -0700 | [diff] [blame] | 137 | printb(b"%-32s %6d %10d" % (k.name, v.count, v.size)) |
Brendan Gregg | 203b4c9 | 2016-10-18 12:10:24 -0700 | [diff] [blame] | 138 | |
| 139 | line += 1 |
| 140 | if line >= maxrows: |
| 141 | break |
| 142 | counts.clear() |
| 143 | |
| 144 | countdown -= 1 |
| 145 | if exiting or countdown == 0: |
| 146 | print("Detaching...") |
| 147 | exit() |