iocost_monitor.py 9.7 KB

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