0001
0002
0003
0004
0005
0006 desc = """
0007 This is a drgn script to monitor the blk-iocost cgroup controller.
0008 See the comment at the top of block/blk-iocost.c for more details.
0009 For drgn, visit https://github.com/osandov/drgn.
0010 """
0011
0012 import sys
0013 import re
0014 import time
0015 import json
0016 import math
0017
0018 import drgn
0019 from drgn import container_of
0020 from drgn.helpers.linux.list import list_for_each_entry,list_empty
0021 from drgn.helpers.linux.radixtree import radix_tree_for_each,radix_tree_lookup
0022
0023 import argparse
0024 parser = argparse.ArgumentParser(description=desc,
0025 formatter_class=argparse.RawTextHelpFormatter)
0026 parser.add_argument('devname', metavar='DEV',
0027 help='Target block device name (e.g. sda)')
0028 parser.add_argument('--cgroup', action='append', metavar='REGEX',
0029 help='Regex for target cgroups, ')
0030 parser.add_argument('--interval', '-i', metavar='SECONDS', type=float, default=1,
0031 help='Monitoring interval in seconds (0 exits immediately '
0032 'after checking requirements)')
0033 parser.add_argument('--json', action='store_true',
0034 help='Output in json')
0035 args = parser.parse_args()
0036
0037 def err(s):
0038 print(s, file=sys.stderr, flush=True)
0039 sys.exit(1)
0040
0041 try:
0042 blkcg_root = prog['blkcg_root']
0043 plid = prog['blkcg_policy_iocost'].plid.value_()
0044 except:
0045 err('The kernel does not have iocost enabled')
0046
0047 IOC_RUNNING = prog['IOC_RUNNING'].value_()
0048 WEIGHT_ONE = prog['WEIGHT_ONE'].value_()
0049 VTIME_PER_SEC = prog['VTIME_PER_SEC'].value_()
0050 VTIME_PER_USEC = prog['VTIME_PER_USEC'].value_()
0051 AUTOP_SSD_FAST = prog['AUTOP_SSD_FAST'].value_()
0052 AUTOP_SSD_DFL = prog['AUTOP_SSD_DFL'].value_()
0053 AUTOP_SSD_QD1 = prog['AUTOP_SSD_QD1'].value_()
0054 AUTOP_HDD = prog['AUTOP_HDD'].value_()
0055
0056 autop_names = {
0057 AUTOP_SSD_FAST: 'ssd_fast',
0058 AUTOP_SSD_DFL: 'ssd_dfl',
0059 AUTOP_SSD_QD1: 'ssd_qd1',
0060 AUTOP_HDD: 'hdd',
0061 }
0062
0063 class BlkgIterator:
0064 def blkcg_name(blkcg):
0065 return blkcg.css.cgroup.kn.name.string_().decode('utf-8')
0066
0067 def walk(self, blkcg, q_id, parent_path):
0068 if not self.include_dying and \
0069 not (blkcg.css.flags.value_() & prog['CSS_ONLINE'].value_()):
0070 return
0071
0072 name = BlkgIterator.blkcg_name(blkcg)
0073 path = parent_path + '/' + name if parent_path else name
0074 blkg = drgn.Object(prog, 'struct blkcg_gq',
0075 address=radix_tree_lookup(blkcg.blkg_tree.address_of_(), q_id))
0076 if not blkg.address_:
0077 return
0078
0079 self.blkgs.append((path if path else '/', blkg))
0080
0081 for c in list_for_each_entry('struct blkcg',
0082 blkcg.css.children.address_of_(), 'css.sibling'):
0083 self.walk(c, q_id, path)
0084
0085 def __init__(self, root_blkcg, q_id, include_dying=False):
0086 self.include_dying = include_dying
0087 self.blkgs = []
0088 self.walk(root_blkcg, q_id, '')
0089
0090 def __iter__(self):
0091 return iter(self.blkgs)
0092
0093 class IocStat:
0094 def __init__(self, ioc):
0095 global autop_names
0096
0097 self.enabled = ioc.enabled.value_()
0098 self.running = ioc.running.value_() == IOC_RUNNING
0099 self.period_ms = ioc.period_us.value_() / 1_000
0100 self.period_at = ioc.period_at.value_() / 1_000_000
0101 self.vperiod_at = ioc.period_at_vtime.value_() / VTIME_PER_SEC
0102 self.vrate_pct = ioc.vtime_base_rate.value_() * 100 / VTIME_PER_USEC
0103 self.busy_level = ioc.busy_level.value_()
0104 self.autop_idx = ioc.autop_idx.value_()
0105 self.user_cost_model = ioc.user_cost_model.value_()
0106 self.user_qos_params = ioc.user_qos_params.value_()
0107
0108 if self.autop_idx in autop_names:
0109 self.autop_name = autop_names[self.autop_idx]
0110 else:
0111 self.autop_name = '?'
0112
0113 def dict(self, now):
0114 return { 'device' : devname,
0115 'timestamp' : now,
0116 'enabled' : self.enabled,
0117 'running' : self.running,
0118 'period_ms' : self.period_ms,
0119 'period_at' : self.period_at,
0120 'period_vtime_at' : self.vperiod_at,
0121 'busy_level' : self.busy_level,
0122 'vrate_pct' : self.vrate_pct, }
0123
0124 def table_preamble_str(self):
0125 state = ('RUN' if self.running else 'IDLE') if self.enabled else 'OFF'
0126 output = f'{devname} {state:4} ' \
0127 f'per={self.period_ms}ms ' \
0128 f'cur_per={self.period_at:.3f}:v{self.vperiod_at:.3f} ' \
0129 f'busy={self.busy_level:+3} ' \
0130 f'vrate={self.vrate_pct:6.2f}% ' \
0131 f'params={self.autop_name}'
0132 if self.user_cost_model or self.user_qos_params:
0133 output += f'({"C" if self.user_cost_model else ""}{"Q" if self.user_qos_params else ""})'
0134 return output
0135
0136 def table_header_str(self):
0137 return f'{"":25} active {"weight":>9} {"hweight%":>13} {"inflt%":>6} ' \
0138 f'{"debt":>7} {"delay":>7} {"usage%"}'
0139
0140 class IocgStat:
0141 def __init__(self, iocg):
0142 ioc = iocg.ioc
0143 blkg = iocg.pd.blkg
0144
0145 self.is_active = not list_empty(iocg.active_list.address_of_())
0146 self.weight = iocg.weight.value_() / WEIGHT_ONE
0147 self.active = iocg.active.value_() / WEIGHT_ONE
0148 self.inuse = iocg.inuse.value_() / WEIGHT_ONE
0149 self.hwa_pct = iocg.hweight_active.value_() * 100 / WEIGHT_ONE
0150 self.hwi_pct = iocg.hweight_inuse.value_() * 100 / WEIGHT_ONE
0151 self.address = iocg.value_()
0152
0153 vdone = iocg.done_vtime.counter.value_()
0154 vtime = iocg.vtime.counter.value_()
0155 vrate = ioc.vtime_rate.counter.value_()
0156 period_vtime = ioc.period_us.value_() * vrate
0157 if period_vtime:
0158 self.inflight_pct = (vtime - vdone) * 100 / period_vtime
0159 else:
0160 self.inflight_pct = 0
0161
0162 self.usage = (100 * iocg.usage_delta_us.value_() /
0163 ioc.period_us.value_()) if self.active else 0
0164 self.debt_ms = iocg.abs_vdebt.value_() / VTIME_PER_USEC / 1000
0165 if blkg.use_delay.counter.value_() != 0:
0166 self.delay_ms = blkg.delay_nsec.counter.value_() / 1_000_000
0167 else:
0168 self.delay_ms = 0
0169
0170 def dict(self, now, path):
0171 out = { 'cgroup' : path,
0172 'timestamp' : now,
0173 'is_active' : self.is_active,
0174 'weight' : self.weight,
0175 'weight_active' : self.active,
0176 'weight_inuse' : self.inuse,
0177 'hweight_active_pct' : self.hwa_pct,
0178 'hweight_inuse_pct' : self.hwi_pct,
0179 'inflight_pct' : self.inflight_pct,
0180 'debt_ms' : self.debt_ms,
0181 'delay_ms' : self.delay_ms,
0182 'usage_pct' : self.usage,
0183 'address' : self.address }
0184 return out
0185
0186 def table_row_str(self, path):
0187 out = f'{path[-28:]:28} ' \
0188 f'{"*" if self.is_active else " "} ' \
0189 f'{round(self.inuse):5}/{round(self.active):5} ' \
0190 f'{self.hwi_pct:6.2f}/{self.hwa_pct:6.2f} ' \
0191 f'{self.inflight_pct:6.2f} ' \
0192 f'{self.debt_ms:7.2f} ' \
0193 f'{self.delay_ms:7.2f} '\
0194 f'{min(self.usage, 999):6.2f}'
0195 out = out.rstrip(':')
0196 return out
0197
0198
0199 table_fmt = not args.json
0200 interval = args.interval
0201 devname = args.devname
0202
0203 if args.json:
0204 table_fmt = False
0205
0206 re_str = None
0207 if args.cgroup:
0208 for r in args.cgroup:
0209 if re_str is None:
0210 re_str = r
0211 else:
0212 re_str += '|' + r
0213
0214 filter_re = re.compile(re_str) if re_str else None
0215
0216
0217 q_id = None
0218 root_iocg = None
0219 ioc = None
0220
0221 for i, ptr in radix_tree_for_each(blkcg_root.blkg_tree.address_of_()):
0222 blkg = drgn.Object(prog, 'struct blkcg_gq', address=ptr)
0223 try:
0224 if devname == blkg.q.kobj.parent.name.string_().decode('utf-8'):
0225 q_id = blkg.q.id.value_()
0226 if blkg.pd[plid]:
0227 root_iocg = container_of(blkg.pd[plid], 'struct ioc_gq', 'pd')
0228 ioc = root_iocg.ioc
0229 break
0230 except:
0231 pass
0232
0233 if ioc is None:
0234 err(f'Could not find ioc for {devname}');
0235
0236 if interval == 0:
0237 sys.exit(0)
0238
0239
0240 while True:
0241 now = time.time()
0242 iocstat = IocStat(ioc)
0243 output = ''
0244
0245 if table_fmt:
0246 output += '\n' + iocstat.table_preamble_str()
0247 output += '\n' + iocstat.table_header_str()
0248 else:
0249 output += json.dumps(iocstat.dict(now))
0250
0251 for path, blkg in BlkgIterator(blkcg_root, q_id):
0252 if filter_re and not filter_re.match(path):
0253 continue
0254 if not blkg.pd[plid]:
0255 continue
0256
0257 iocg = container_of(blkg.pd[plid], 'struct ioc_gq', 'pd')
0258 iocg_stat = IocgStat(iocg)
0259
0260 if not filter_re and not iocg_stat.is_active:
0261 continue
0262
0263 if table_fmt:
0264 output += '\n' + iocg_stat.table_row_str(path)
0265 else:
0266 output += '\n' + json.dumps(iocg_stat.dict(now, path))
0267
0268 print(output)
0269 sys.stdout.flush()
0270 time.sleep(interval)