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); |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 84 | counts.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") |
| 99 | b.attach_kprobe(event="account_page_dirtied", fn_name="do_count") |
| 100 | b.attach_kprobe(event="mark_buffer_dirty", fn_name="do_count") |
| 101 | |
| 102 | # header |
| 103 | if tstamp: |
| 104 | print("%-8s " % "TIME", end="") |
Joel Fernandes | 2b34870 | 2018-02-27 20:38:42 -0800 | [diff] [blame] | 105 | print("%8s %8s %8s %8s %12s %10s" % |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 106 | ("HITS", "MISSES", "DIRTIES", "HITRATIO", "BUFFERS_MB", "CACHED_MB")) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 107 | |
| 108 | loop = 0 |
| 109 | exiting = 0 |
| 110 | while 1: |
| 111 | if count > 0: |
| 112 | loop += 1 |
| 113 | if loop > count: |
| 114 | exit() |
| 115 | |
| 116 | try: |
| 117 | sleep(interval) |
| 118 | except KeyboardInterrupt: |
| 119 | exiting = 1 |
| 120 | # as cleanup can take many seconds, trap Ctrl-C: |
| 121 | signal.signal(signal.SIGINT, signal_ignore) |
| 122 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 123 | counts = b["counts"] |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 124 | 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^] | 125 | func = b.ksym(k.ip) |
| 126 | # partial string matches in case of .isra (necessary?) |
| 127 | if func.find("mark_page_accessed") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 128 | mpa = max(0, v.value) |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 129 | if func.find("mark_buffer_dirty") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 130 | mbd = max(0, v.value) |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 131 | if func.find("add_to_page_cache_lru") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 132 | apcl = max(0, v.value) |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 133 | if func.find("account_page_dirtied") == 0: |
chantra | a2d669c | 2016-07-29 14:10:15 -0700 | [diff] [blame] | 134 | apd = max(0, v.value) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 135 | |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 136 | # total = total cache accesses without counting dirties |
| 137 | # misses = total of add to lru because of read misses |
| 138 | total = mpa - mbd |
| 139 | misses = apcl - apd |
| 140 | if misses < 0: |
| 141 | misses = 0 |
| 142 | if total < 0: |
| 143 | total = 0 |
| 144 | hits = total - misses |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 145 | |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 146 | # If hits are < 0, then its possible misses are overestimated |
| 147 | # due to possibly page cache read ahead adding more pages than |
| 148 | # needed. In this case just assume misses as total and reset hits. |
| 149 | if hits < 0: |
| 150 | misses = total |
| 151 | hits = 0 |
| 152 | ratio = 0 |
| 153 | if total > 0: |
| 154 | ratio = float(hits) / total |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 155 | |
| 156 | if debug: |
Joel Fernandes | 2b34870 | 2018-02-27 20:38:42 -0800 | [diff] [blame] | 157 | print("%d %d %d %d %d %d %d\n" % |
| 158 | (mpa, mbd, apcl, apd, total, misses, hits)) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 159 | |
| 160 | counts.clear() |
| 161 | |
| 162 | # Get memory info |
| 163 | mem = get_meminfo() |
| 164 | cached = int(mem["Cached"]) / 1024 |
| 165 | buff = int(mem["Buffers"]) / 1024 |
| 166 | |
Marko Myllynen | 27e7aea | 2018-09-26 20:09:07 +0300 | [diff] [blame] | 167 | if tstamp: |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 168 | print("%-8s " % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 169 | print("%8d %8d %8d %7.2f%% %12.0f %10.0f" % |
| 170 | (hits, misses, mbd, 100 * ratio, buff, cached)) |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 171 | |
Brendan Gregg | 6af7b84 | 2019-02-02 12:45:23 -0800 | [diff] [blame^] | 172 | mpa = mbd = apcl = apd = total = misses = hits = cached = buff = 0 |
unixtest | 57abe5b | 2016-01-31 10:47:03 +0000 | [diff] [blame] | 173 | |
| 174 | if exiting: |
| 175 | print("Detaching...") |
| 176 | exit() |