|
| 1 | +#!/usr/bin/env drgn |
| 2 | +# |
| 3 | +# Copyright (C) 2019 Tejun Heo <tj@kernel.org> |
| 4 | +# Copyright (C) 2019 Facebook |
| 5 | + |
| 6 | +desc = """ |
| 7 | +This is a drgn script to monitor the blk-iocost cgroup controller. |
| 8 | +See the comment at the top of block/blk-iocost.c for more details. |
| 9 | +For drgn, visit https://github.com/osandov/drgn. |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +import re |
| 14 | +import time |
| 15 | +import json |
| 16 | + |
| 17 | +import drgn |
| 18 | +from drgn import container_of |
| 19 | +from drgn.helpers.linux.list import list_for_each_entry,list_empty |
| 20 | +from drgn.helpers.linux.radixtree import radix_tree_for_each,radix_tree_lookup |
| 21 | + |
| 22 | +import argparse |
| 23 | +parser = argparse.ArgumentParser(description=desc, |
| 24 | + formatter_class=argparse.RawTextHelpFormatter) |
| 25 | +parser.add_argument('devname', metavar='DEV', |
| 26 | + help='Target block device name (e.g. sda)') |
| 27 | +parser.add_argument('--cgroup', action='append', metavar='REGEX', |
| 28 | + help='Regex for target cgroups, ') |
| 29 | +parser.add_argument('--interval', '-i', metavar='SECONDS', type=float, default=1, |
| 30 | + help='Monitoring interval in seconds') |
| 31 | +parser.add_argument('--json', action='store_true', |
| 32 | + help='Output in json') |
| 33 | +args = parser.parse_args() |
| 34 | + |
| 35 | +def err(s): |
| 36 | + print(s, file=sys.stderr, flush=True) |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | +try: |
| 40 | + blkcg_root = prog['blkcg_root'] |
| 41 | + plid = prog['blkcg_policy_iocost'].plid.value_() |
| 42 | +except: |
| 43 | + err('The kernel does not have iocost enabled') |
| 44 | + |
| 45 | +IOC_RUNNING = prog['IOC_RUNNING'].value_() |
| 46 | +NR_USAGE_SLOTS = prog['NR_USAGE_SLOTS'].value_() |
| 47 | +HWEIGHT_WHOLE = prog['HWEIGHT_WHOLE'].value_() |
| 48 | +VTIME_PER_SEC = prog['VTIME_PER_SEC'].value_() |
| 49 | +VTIME_PER_USEC = prog['VTIME_PER_USEC'].value_() |
| 50 | +AUTOP_SSD_FAST = prog['AUTOP_SSD_FAST'].value_() |
| 51 | +AUTOP_SSD_DFL = prog['AUTOP_SSD_DFL'].value_() |
| 52 | +AUTOP_SSD_QD1 = prog['AUTOP_SSD_QD1'].value_() |
| 53 | +AUTOP_HDD = prog['AUTOP_HDD'].value_() |
| 54 | + |
| 55 | +autop_names = { |
| 56 | + AUTOP_SSD_FAST: 'ssd_fast', |
| 57 | + AUTOP_SSD_DFL: 'ssd_dfl', |
| 58 | + AUTOP_SSD_QD1: 'ssd_qd1', |
| 59 | + AUTOP_HDD: 'hdd', |
| 60 | +} |
| 61 | + |
| 62 | +class BlkgIterator: |
| 63 | + def blkcg_name(blkcg): |
| 64 | + return blkcg.css.cgroup.kn.name.string_().decode('utf-8') |
| 65 | + |
| 66 | + def walk(self, blkcg, q_id, parent_path): |
| 67 | + if not self.include_dying and \ |
| 68 | + not (blkcg.css.flags.value_() & prog['CSS_ONLINE'].value_()): |
| 69 | + return |
| 70 | + |
| 71 | + name = BlkgIterator.blkcg_name(blkcg) |
| 72 | + path = parent_path + '/' + name if parent_path else name |
| 73 | + blkg = drgn.Object(prog, 'struct blkcg_gq', |
| 74 | + address=radix_tree_lookup(blkcg.blkg_tree, q_id)) |
| 75 | + if not blkg.address_: |
| 76 | + return |
| 77 | + |
| 78 | + self.blkgs.append((path if path else '/', blkg)) |
| 79 | + |
| 80 | + for c in list_for_each_entry('struct blkcg', |
| 81 | + blkcg.css.children.address_of_(), 'css.sibling'): |
| 82 | + self.walk(c, q_id, path) |
| 83 | + |
| 84 | + def __init__(self, root_blkcg, q_id, include_dying=False): |
| 85 | + self.include_dying = include_dying |
| 86 | + self.blkgs = [] |
| 87 | + self.walk(root_blkcg, q_id, '') |
| 88 | + |
| 89 | + def __iter__(self): |
| 90 | + return iter(self.blkgs) |
| 91 | + |
| 92 | +class IocStat: |
| 93 | + def __init__(self, ioc): |
| 94 | + global autop_names |
| 95 | + |
| 96 | + self.enabled = ioc.enabled.value_() |
| 97 | + self.running = ioc.running.value_() == IOC_RUNNING |
| 98 | + self.period_ms = round(ioc.period_us.value_() / 1_000) |
| 99 | + self.period_at = ioc.period_at.value_() / 1_000_000 |
| 100 | + self.vperiod_at = ioc.period_at_vtime.value_() / VTIME_PER_SEC |
| 101 | + self.vrate_pct = ioc.vtime_rate.counter.value_() * 100 / VTIME_PER_USEC |
| 102 | + self.busy_level = ioc.busy_level.value_() |
| 103 | + self.autop_idx = ioc.autop_idx.value_() |
| 104 | + self.user_cost_model = ioc.user_cost_model.value_() |
| 105 | + self.user_qos_params = ioc.user_qos_params.value_() |
| 106 | + |
| 107 | + if self.autop_idx in autop_names: |
| 108 | + self.autop_name = autop_names[self.autop_idx] |
| 109 | + else: |
| 110 | + self.autop_name = '?' |
| 111 | + |
| 112 | + def dict(self, now): |
| 113 | + return { 'device' : devname, |
| 114 | + 'timestamp' : now, |
| 115 | + 'enabled' : self.enabled, |
| 116 | + 'running' : self.running, |
| 117 | + 'period_ms' : self.period_ms, |
| 118 | + 'period_at' : self.period_at, |
| 119 | + 'period_vtime_at' : self.vperiod_at, |
| 120 | + 'busy_level' : self.busy_level, |
| 121 | + 'vrate_pct' : self.vrate_pct, } |
| 122 | + |
| 123 | + def table_preamble_str(self): |
| 124 | + state = ('RUN' if self.running else 'IDLE') if self.enabled else 'OFF' |
| 125 | + output = f'{devname} {state:4} ' \ |
| 126 | + f'per={self.period_ms}ms ' \ |
| 127 | + f'cur_per={self.period_at:.3f}:v{self.vperiod_at:.3f} ' \ |
| 128 | + f'busy={self.busy_level:+3} ' \ |
| 129 | + f'vrate={self.vrate_pct:6.2f}% ' \ |
| 130 | + f'params={self.autop_name}' |
| 131 | + if self.user_cost_model or self.user_qos_params: |
| 132 | + output += f'({"C" if self.user_cost_model else ""}{"Q" if self.user_qos_params else ""})' |
| 133 | + return output |
| 134 | + |
| 135 | + def table_header_str(self): |
| 136 | + return f'{"":25} active {"weight":>9} {"hweight%":>13} {"inflt%":>6} ' \ |
| 137 | + f'{"del_ms":>6} {"usages%"}' |
| 138 | + |
| 139 | +class IocgStat: |
| 140 | + def __init__(self, iocg): |
| 141 | + ioc = iocg.ioc |
| 142 | + blkg = iocg.pd.blkg |
| 143 | + |
| 144 | + self.is_active = not list_empty(iocg.active_list.address_of_()) |
| 145 | + self.weight = iocg.weight.value_() |
| 146 | + self.active = iocg.active.value_() |
| 147 | + self.inuse = iocg.inuse.value_() |
| 148 | + self.hwa_pct = iocg.hweight_active.value_() * 100 / HWEIGHT_WHOLE |
| 149 | + self.hwi_pct = iocg.hweight_inuse.value_() * 100 / HWEIGHT_WHOLE |
| 150 | + |
| 151 | + vdone = iocg.done_vtime.counter.value_() |
| 152 | + vtime = iocg.vtime.counter.value_() |
| 153 | + vrate = ioc.vtime_rate.counter.value_() |
| 154 | + period_vtime = ioc.period_us.value_() * vrate |
| 155 | + if period_vtime: |
| 156 | + self.inflight_pct = (vtime - vdone) * 100 / period_vtime |
| 157 | + else: |
| 158 | + self.inflight_pct = 0 |
| 159 | + |
| 160 | + self.use_delay = min(blkg.use_delay.counter.value_(), 99) |
| 161 | + self.delay_ms = min(round(blkg.delay_nsec.counter.value_() / 1_000_000), 999) |
| 162 | + |
| 163 | + usage_idx = iocg.usage_idx.value_() |
| 164 | + self.usages = [] |
| 165 | + self.usage = 0 |
| 166 | + for i in range(NR_USAGE_SLOTS): |
| 167 | + usage = iocg.usages[(usage_idx + i) % NR_USAGE_SLOTS].value_() |
| 168 | + upct = min(usage * 100 / HWEIGHT_WHOLE, 999) |
| 169 | + self.usages.append(upct) |
| 170 | + self.usage = max(self.usage, upct) |
| 171 | + |
| 172 | + def dict(self, now, path): |
| 173 | + out = { 'cgroup' : path, |
| 174 | + 'timestamp' : now, |
| 175 | + 'is_active' : self.is_active, |
| 176 | + 'weight' : self.weight, |
| 177 | + 'weight_active' : self.active, |
| 178 | + 'weight_inuse' : self.inuse, |
| 179 | + 'hweight_active_pct' : self.hwa_pct, |
| 180 | + 'hweight_inuse_pct' : self.hwi_pct, |
| 181 | + 'inflight_pct' : self.inflight_pct, |
| 182 | + 'use_delay' : self.use_delay, |
| 183 | + 'delay_ms' : self.delay_ms, |
| 184 | + 'usage_pct' : self.usage } |
| 185 | + for i in range(len(self.usages)): |
| 186 | + out[f'usage_pct_{i}'] = f'{self.usages[i]}' |
| 187 | + return out |
| 188 | + |
| 189 | + def table_row_str(self, path): |
| 190 | + out = f'{path[-28:]:28} ' \ |
| 191 | + f'{"*" if self.is_active else " "} ' \ |
| 192 | + f'{self.inuse:5}/{self.active:5} ' \ |
| 193 | + f'{self.hwi_pct:6.2f}/{self.hwa_pct:6.2f} ' \ |
| 194 | + f'{self.inflight_pct:6.2f} ' \ |
| 195 | + f'{self.use_delay:2}*{self.delay_ms:03} ' |
| 196 | + for u in self.usages: |
| 197 | + out += f'{round(u):03d}:' |
| 198 | + out = out.rstrip(':') |
| 199 | + return out |
| 200 | + |
| 201 | +# handle args |
| 202 | +table_fmt = not args.json |
| 203 | +interval = args.interval |
| 204 | +devname = args.devname |
| 205 | + |
| 206 | +if args.json: |
| 207 | + table_fmt = False |
| 208 | + |
| 209 | +re_str = None |
| 210 | +if args.cgroup: |
| 211 | + for r in args.cgroup: |
| 212 | + if re_str is None: |
| 213 | + re_str = r |
| 214 | + else: |
| 215 | + re_str += '|' + r |
| 216 | + |
| 217 | +filter_re = re.compile(re_str) if re_str else None |
| 218 | + |
| 219 | +# Locate the roots |
| 220 | +q_id = None |
| 221 | +root_iocg = None |
| 222 | +ioc = None |
| 223 | + |
| 224 | +for i, ptr in radix_tree_for_each(blkcg_root.blkg_tree): |
| 225 | + blkg = drgn.Object(prog, 'struct blkcg_gq', address=ptr) |
| 226 | + try: |
| 227 | + if devname == blkg.q.kobj.parent.name.string_().decode('utf-8'): |
| 228 | + q_id = blkg.q.id.value_() |
| 229 | + if blkg.pd[plid]: |
| 230 | + root_iocg = container_of(blkg.pd[plid], 'struct ioc_gq', 'pd') |
| 231 | + ioc = root_iocg.ioc |
| 232 | + break |
| 233 | + except: |
| 234 | + pass |
| 235 | + |
| 236 | +if ioc is None: |
| 237 | + err(f'Could not find ioc for {devname}'); |
| 238 | + |
| 239 | +# Keep printing |
| 240 | +while True: |
| 241 | + now = time.time() |
| 242 | + iocstat = IocStat(ioc) |
| 243 | + output = '' |
| 244 | + |
| 245 | + if table_fmt: |
| 246 | + output += '\n' + iocstat.table_preamble_str() |
| 247 | + output += '\n' + iocstat.table_header_str() |
| 248 | + else: |
| 249 | + output += json.dumps(iocstat.dict(now)) |
| 250 | + |
| 251 | + for path, blkg in BlkgIterator(blkcg_root, q_id): |
| 252 | + if filter_re and not filter_re.match(path): |
| 253 | + continue |
| 254 | + if not blkg.pd[plid]: |
| 255 | + continue |
| 256 | + |
| 257 | + iocg = container_of(blkg.pd[plid], 'struct ioc_gq', 'pd') |
| 258 | + iocg_stat = IocgStat(iocg) |
| 259 | + |
| 260 | + if not filter_re and not iocg_stat.is_active: |
| 261 | + continue |
| 262 | + |
| 263 | + if table_fmt: |
| 264 | + output += '\n' + iocg_stat.table_row_str(path) |
| 265 | + else: |
| 266 | + output += '\n' + json.dumps(iocg_stat.dict(now, path)) |
| 267 | + |
| 268 | + print(output) |
| 269 | + sys.stdout.flush() |
| 270 | + time.sleep(interval) |
0 commit comments