Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 2 | # |
| 3 | # cachestat Count cache kernel function calls. |
| 4 | # For Linux, uses BCC, eBPF. See .c file. |
| 5 | # |
| 6 | # USAGE: cachestat |
| 7 | # Taken from funccount by Brendan Gregg |
| 8 | # This is a rewrite of cachestat from perf to bcc |
| 9 | # https://github.com/brendangregg/perf-tools/blob/master/fs/cachestat |
| 10 | # |
| 11 | # Copyright (c) 2016 Allan McAleavy. |
| 12 | # Copyright (c) 2015 Brendan Gregg. |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | # |
| 15 | # 09-Sep-2015 Brendan Gregg Created this. |
| 16 | # 06-Nov-2015 Allan McAleavy |
| 17 | # 13-Jan-2016 Allan McAleavy run pep8 against program |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 18 | # 02-Feb-2019 Brendan Gregg Column shuffle, bring back %ratio |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 19 | |
| 20 | from __future__ import print_function |
| 21 | from bcc import BPF |
| 22 | from time import sleep, strftime |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 23 | import argparse |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 24 | import signal |
| 25 | import re |
| 26 | from sys import argv |
| 27 | |
| 28 | # signal handler |
| 29 | def signal_ignore(signal, frame): |
| 30 | print() |
| 31 | |
| 32 | # Function to gather data from /proc/meminfo |
| 33 | # return dictionary for quicker lookup of both values |
| 34 | def get_meminfo(): |
| 35 | result = dict() |
| 36 | |
| 37 | for line in open('/proc/meminfo'): |
| 38 | k = line.split(':', 3) |
| 39 | v = k[1].split() |
| 40 | result[k[0]] = int(v[0]) |
| 41 | return result |
| 42 | |
| 43 | # set global variables |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 44 | mpa = 0 |
| 45 | mbd = 0 |
| 46 | apcl = 0 |
| 47 | apd = 0 |
Joel Fernandes | 2b34870 | 2018-02-27 20:38:42 -0800 | [diff] [blame] | 48 | total = 0 |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 49 | misses = 0 |
Joel Fernandes | 2b34870 | 2018-02-27 20:38:42 -0800 | [diff] [blame] | 50 | hits = 0 |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 51 | debug = 0 |
| 52 | |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 53 | # arguments |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 54 | parser = argparse.ArgumentParser( |
| 55 | description="Count cache kernel function calls", |
| 56 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 57 | parser.add_argument("-T", "--timestamp", action="store_true", |
| 58 | help="include timestamp on output") |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 59 | parser.add_argument("interval", nargs="?", default=1, |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 60 | help="output interval, in seconds") |
| 61 | parser.add_argument("count", nargs="?", default=-1, |
| 62 | help="number of outputs") |
| 63 | parser.add_argument("--ebpf", action="store_true", |
| 64 | help=argparse.SUPPRESS) |
| 65 | args = parser.parse_args() |
| 66 | count = int(args.count) |
| 67 | tstamp = args.timestamp |
| 68 | interval = int(args.interval) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 69 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 70 | # define BPF program |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 71 | bpf_text = """ |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 72 | #include <uapi/linux/ptrace.h> |
| 73 | struct key_t { |
| 74 | u64 ip; |
| 75 | }; |
| 76 | |
| 77 | BPF_HASH(counts, struct key_t); |
| 78 | |
| 79 | int do_count(struct pt_regs *ctx) { |
| 80 | struct key_t key = {}; |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 81 | u64 ip; |
| 82 | |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 83 | key.ip = PT_REGS_IP(ctx); |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 84 | counts.atomic_increment(key); // update counter |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | """ |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 89 | |
| 90 | if debug or args.ebpf: |
| 91 | print(bpf_text) |
| 92 | if args.ebpf: |
| 93 | exit() |
| 94 | |
| 95 | # load BPF program |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 96 | b = BPF(text=bpf_text) |
| 97 | b.attach_kprobe(event="add_to_page_cache_lru", fn_name="do_count") |
| 98 | b.attach_kprobe(event="mark_page_accessed", fn_name="do_count") |
Yonghong Song | 61087b9 | 2021-11-08 11:12:37 -0800 | [diff] [blame] | 99 | |
| 100 | # Function account_page_dirtied() is changed to folio_account_dirtied() in 5.15. |
| 101 | # FIXME: Both folio_account_dirtied() and account_page_dirtied() are |
| 102 | # static functions and they may be gone during compilation and this may |
| 103 | # introduce some inaccuracy. |
| 104 | if BPF.get_kprobe_functions(b'folio_account_dirtied'): |
| 105 | b.attach_kprobe(event="folio_account_dirtied", fn_name="do_count") |
| 106 | elif BPF.get_kprobe_functions(b'account_page_dirtied'): |
| 107 | b.attach_kprobe(event="account_page_dirtied", fn_name="do_count") |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 108 | b.attach_kprobe(event="mark_buffer_dirty", fn_name="do_count") |
| 109 | |
| 110 | # header |
| 111 | if tstamp: |
| 112 | print("%-8s " % "TIME", end="") |
Joel Fernandes | 2b34870 | 2018-02-27 20:38:42 -0800 | [diff] [blame] | 113 | print("%8s %8s %8s %8s %12s %10s" % |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 114 | ("HITS", "MISSES", "DIRTIES", "HITRATIO", "BUFFERS_MB", "CACHED_MB")) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 115 | |
| 116 | loop = 0 |
| 117 | exiting = 0 |
| 118 | while 1: |
| 119 | if count > 0: |
| 120 | loop += 1 |
| 121 | if loop > count: |
| 122 | exit() |
| 123 | |
| 124 | try: |
| 125 | sleep(interval) |
| 126 | except KeyboardInterrupt: |
| 127 | exiting = 1 |
| 128 | # as cleanup can take many seconds, trap Ctrl-C: |
| 129 | signal.signal(signal.SIGINT, signal_ignore) |
| 130 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 131 | counts = b["counts"] |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 132 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 133 | func = b.ksym(k.ip) |
| 134 | # partial string matches in case of .isra (necessary?) |
10ne1 | 0267b48 | 2019-03-26 03:07:40 +0200 | [diff] [blame] | 135 | if func.find(b"mark_page_accessed") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 136 | mpa = max(0, v.value) |
10ne1 | 0267b48 | 2019-03-26 03:07:40 +0200 | [diff] [blame] | 137 | if func.find(b"mark_buffer_dirty") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 138 | mbd = max(0, v.value) |
10ne1 | 0267b48 | 2019-03-26 03:07:40 +0200 | [diff] [blame] | 139 | if func.find(b"add_to_page_cache_lru") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 140 | apcl = max(0, v.value) |
10ne1 | 0267b48 | 2019-03-26 03:07:40 +0200 | [diff] [blame] | 141 | if func.find(b"account_page_dirtied") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 142 | apd = max(0, v.value) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 143 | |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 144 | # total = total cache accesses without counting dirties |
| 145 | # misses = total of add to lru because of read misses |
| 146 | total = mpa - mbd |
| 147 | misses = apcl - apd |
| 148 | if misses < 0: |
| 149 | misses = 0 |
| 150 | if total < 0: |
| 151 | total = 0 |
| 152 | hits = total - misses |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 153 | |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 154 | # If hits are < 0, then its possible misses are overestimated |
| 155 | # due to possibly page cache read ahead adding more pages than |
| 156 | # needed. In this case just assume misses as total and reset hits. |
| 157 | if hits < 0: |
| 158 | misses = total |
| 159 | hits = 0 |
| 160 | ratio = 0 |
| 161 | if total > 0: |
| 162 | ratio = float(hits) / total |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 163 | |
| 164 | if debug: |
Joel Fernandes | 2b34870 | 2018-02-27 20:38:42 -0800 | [diff] [blame] | 165 | print("%d %d %d %d %d %d %d\n" % |
| 166 | (mpa, mbd, apcl, apd, total, misses, hits)) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 167 | |
| 168 | counts.clear() |
| 169 | |
| 170 | # Get memory info |
| 171 | mem = get_meminfo() |
| 172 | cached = int(mem["Cached"]) / 1024 |
| 173 | buff = int(mem["Buffers"]) / 1024 |
| 174 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 175 | if tstamp: |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 176 | print("%-8s " % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 177 | print("%8d %8d %8d %7.2f%% %12.0f %10.0f" % |
| 178 | (hits, misses, mbd, 100 * ratio, buff, cached)) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 179 | |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame] | 180 | mpa = mbd = apcl = apd = total = misses = hits = cached = buff = 0 |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 181 | |
| 182 | if exiting: |
| 183 | print("Detaching...") |
| 184 | exit() |