tracebd.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. #!/usr/bin/env python3
  2. #
  3. # Display operations on block devices based on trace output
  4. #
  5. # Example:
  6. # ./scripts/tracebd.py trace
  7. #
  8. # Copyright (c) 2022, The littlefs authors.
  9. # SPDX-License-Identifier: BSD-3-Clause
  10. #
  11. import collections as co
  12. import functools as ft
  13. import itertools as it
  14. import math as m
  15. import os
  16. import re
  17. import shutil
  18. import threading as th
  19. import time
  20. def openio(path, mode='r'):
  21. if path == '-':
  22. if mode == 'r':
  23. return os.fdopen(os.dup(sys.stdin.fileno()), 'r')
  24. else:
  25. return os.fdopen(os.dup(sys.stdout.fileno()), 'w')
  26. else:
  27. return open(path, mode)
  28. # space filling Hilbert-curve
  29. #
  30. # note we memoize the last curve since this is a bit expensive
  31. #
  32. @ft.lru_cache(1)
  33. def hilbert_curve(width, height):
  34. # based on generalized Hilbert curves:
  35. # https://github.com/jakubcerveny/gilbert
  36. #
  37. def hilbert_(x, y, a_x, a_y, b_x, b_y):
  38. w = abs(a_x+a_y)
  39. h = abs(b_x+b_y)
  40. a_dx = -1 if a_x < 0 else +1 if a_x > 0 else 0
  41. a_dy = -1 if a_y < 0 else +1 if a_y > 0 else 0
  42. b_dx = -1 if b_x < 0 else +1 if b_x > 0 else 0
  43. b_dy = -1 if b_y < 0 else +1 if b_y > 0 else 0
  44. # trivial row
  45. if h == 1:
  46. for _ in range(w):
  47. yield (x,y)
  48. x, y = x+a_dx, y+a_dy
  49. return
  50. # trivial column
  51. if w == 1:
  52. for _ in range(h):
  53. yield (x,y)
  54. x, y = x+b_dx, y+b_dy
  55. return
  56. a_x_, a_y_ = a_x//2, a_y//2
  57. b_x_, b_y_ = b_x//2, b_y//2
  58. w_ = abs(a_x_+a_y_)
  59. h_ = abs(b_x_+b_y_)
  60. if 2*w > 3*h:
  61. # prefer even steps
  62. if w_ % 2 != 0 and w > 2:
  63. a_x_, a_y_ = a_x_+a_dx, a_y_+a_dy
  64. # split in two
  65. yield from hilbert_(x, y, a_x_, a_y_, b_x, b_y)
  66. yield from hilbert_(x+a_x_, y+a_y_, a_x-a_x_, a_y-a_y_, b_x, b_y)
  67. else:
  68. # prefer even steps
  69. if h_ % 2 != 0 and h > 2:
  70. b_x_, b_y_ = b_x_+b_dx, b_y_+b_dy
  71. # split in three
  72. yield from hilbert_(x, y, b_x_, b_y_, a_x_, a_y_)
  73. yield from hilbert_(x+b_x_, y+b_y_, a_x, a_y, b_x-b_x_, b_y-b_y_)
  74. yield from hilbert_(
  75. x+(a_x-a_dx)+(b_x_-b_dx), y+(a_y-a_dy)+(b_y_-b_dy),
  76. -b_x_, -b_y_, -(a_x-a_x_), -(a_y-a_y_))
  77. if width >= height:
  78. curve = hilbert_(0, 0, +width, 0, 0, +height)
  79. else:
  80. curve = hilbert_(0, 0, 0, +height, +width, 0)
  81. return list(curve)
  82. # space filling Z-curve/Lebesgue-curve
  83. #
  84. # note we memoize the last curve since this is a bit expensive
  85. #
  86. @ft.lru_cache(1)
  87. def lebesgue_curve(width, height):
  88. # we create a truncated Z-curve by simply filtering out the points
  89. # that are outside our region
  90. curve = []
  91. for i in range(2**(2*m.ceil(m.log2(max(width, height))))):
  92. # we just operate on binary strings here because it's easier
  93. b = '{:0{}b}'.format(i, 2*m.ceil(m.log2(i+1)/2))
  94. x = int(b[1::2], 2) if b[1::2] else 0
  95. y = int(b[0::2], 2) if b[0::2] else 0
  96. if x < width and y < height:
  97. curve.append((x, y))
  98. return curve
  99. class Block:
  100. def __init__(self, wear=0, readed=False, proged=False, erased=False):
  101. self._ = ((wear << 3)
  102. | (1 if readed else 0)
  103. | (2 if proged else 0)
  104. | (4 if erased else False))
  105. @property
  106. def wear(self):
  107. return self._ >> 3
  108. @property
  109. def readed(self):
  110. return (self._ & 1) != 0
  111. @property
  112. def proged(self):
  113. return (self._ & 2) != 0
  114. @property
  115. def erased(self):
  116. return (self._ & 4) != 0
  117. def read(self):
  118. self._ |= 1
  119. def prog(self):
  120. self._ |= 2
  121. def erase(self):
  122. self._ = (self._ | 4) + 8
  123. def clear(self):
  124. self._ &= ~7
  125. def reset(self):
  126. self._ = 0
  127. def copy(self):
  128. return Block(self.wear, self.readed, self.proged, self.erased)
  129. def __add__(self, other):
  130. return Block(
  131. max(self.wear, other.wear),
  132. self.readed | other.readed,
  133. self.proged | other.proged,
  134. self.erased | other.erased)
  135. def draw(self, *,
  136. subscripts=False,
  137. chars=None,
  138. wear_chars=None,
  139. color=True,
  140. read=True,
  141. prog=True,
  142. erase=True,
  143. wear=False,
  144. max_wear=None,
  145. block_cycles=None,
  146. **_):
  147. if not chars: chars = '.rpe'
  148. c = chars[0]
  149. f = []
  150. if wear:
  151. if not wear_chars and subscripts: wear_chars = '.₁₂₃₄₅₆789'
  152. elif not wear_chars: wear_chars = '0123456789'
  153. if block_cycles:
  154. w = self.wear / block_cycles
  155. else:
  156. w = self.wear / max(max_wear, len(wear_chars)-1)
  157. c = wear_chars[min(
  158. int(w*(len(wear_chars)-1)),
  159. len(wear_chars)-1)]
  160. if color:
  161. if w*9 >= 9: f.append('\x1b[1;31m')
  162. elif w*9 >= 7: f.append('\x1b[35m')
  163. if erase and self.erased: c = chars[3]
  164. elif prog and self.proged: c = chars[2]
  165. elif read and self.readed: c = chars[1]
  166. if color:
  167. if erase and self.erased: f.append('\x1b[44m')
  168. elif prog and self.proged: f.append('\x1b[45m')
  169. elif read and self.readed: f.append('\x1b[42m')
  170. if color:
  171. return '%s%c\x1b[m' % (''.join(f), c)
  172. else:
  173. return c
  174. class Bd:
  175. def __init__(self, *, blocks=None, size=1, count=1, width=80):
  176. if blocks is not None:
  177. self.blocks = blocks
  178. self.size = size
  179. self.count = count
  180. self.width = width
  181. else:
  182. self.blocks = []
  183. self.size = None
  184. self.count = None
  185. self.width = None
  186. self.smoosh(size=size, count=count, width=width)
  187. def get(self, block=slice(None), off=slice(None)):
  188. if not isinstance(block, slice):
  189. block = slice(block, block+1)
  190. if not isinstance(off, slice):
  191. off = slice(off, off+1)
  192. if (not self.blocks
  193. or not self.width
  194. or not self.size
  195. or not self.count):
  196. return
  197. if self.count >= self.width:
  198. scale = (self.count+self.width-1) // self.width
  199. for i in range(
  200. (block.start if block.start is not None else 0)//scale,
  201. (min(block.stop if block.stop is not None else self.count,
  202. self.count)+scale-1)//scale):
  203. yield self.blocks[i]
  204. else:
  205. scale = self.width // self.count
  206. for i in range(
  207. block.start if block.start is not None else 0,
  208. min(block.stop if block.stop is not None else self.count,
  209. self.count)):
  210. for j in range(
  211. ((off.start if off.start is not None else 0)
  212. *scale)//self.size,
  213. (min(off.stop if off.stop is not None else self.size,
  214. self.size)*scale+self.size-1)//self.size):
  215. yield self.blocks[i*scale+j]
  216. def __getitem__(self, block=slice(None), off=slice(None)):
  217. if isinstance(block, tuple):
  218. block, off = block
  219. if not isinstance(block, slice):
  220. block = slice(block, block+1)
  221. if not isinstance(off, slice):
  222. off = slice(off, off+1)
  223. # needs resize?
  224. if ((block.stop is not None and block.stop > self.count)
  225. or (off.stop is not None and off.stop > self.size)):
  226. self.smoosh(
  227. count=max(block.stop or self.count, self.count),
  228. size=max(off.stop or self.size, self.size))
  229. return self.get(block, off)
  230. def smoosh(self, *, size=None, count=None, width=None):
  231. size = size or self.size
  232. count = count or self.count
  233. width = width or self.width
  234. if count >= width:
  235. scale = (count+width-1) // width
  236. self.blocks = [
  237. sum(self.get(slice(i,i+scale)), start=Block())
  238. for i in range(0, count, scale)]
  239. else:
  240. scale = width // count
  241. self.blocks = [
  242. sum(self.get(i, slice(j*(size//width),(j+1)*(size//width))),
  243. start=Block())
  244. for i in range(0, count)
  245. for j in range(scale)]
  246. self.size = size
  247. self.count = count
  248. self.width = width
  249. def read(self, block=slice(None), off=slice(None)):
  250. for c in self[block, off]:
  251. c.read()
  252. def prog(self, block=slice(None), off=slice(None)):
  253. for c in self[block, off]:
  254. c.prog()
  255. def erase(self, block=slice(None), off=slice(None)):
  256. for c in self[block, off]:
  257. c.erase()
  258. def clear(self, block=slice(None), off=slice(None)):
  259. for c in self[block, off]:
  260. c.clear()
  261. def reset(self, block=slice(None), off=slice(None)):
  262. for c in self[block, off]:
  263. c.reset()
  264. def copy(self):
  265. return Bd(
  266. blocks=[b.copy() for b in self.blocks],
  267. size=self.size, count=self.count, width=self.width)
  268. def main(path='-', *,
  269. read=False,
  270. prog=False,
  271. erase=False,
  272. wear=False,
  273. color='auto',
  274. block=(None,None),
  275. off=(None,None),
  276. block_size=None,
  277. block_count=None,
  278. block_cycles=None,
  279. reset=False,
  280. width=None,
  281. height=1,
  282. scale=None,
  283. lines=None,
  284. coalesce=None,
  285. sleep=None,
  286. hilbert=False,
  287. lebesgue=False,
  288. keep_open=False,
  289. **args):
  290. # exclusive wear or read/prog/erase by default
  291. if not read and not prog and not erase and not wear:
  292. read = True
  293. prog = True
  294. erase = True
  295. # figure out what color should be
  296. if color == 'auto':
  297. color = sys.stdout.isatty()
  298. elif color == 'always':
  299. color = True
  300. else:
  301. color = False
  302. block_start = block[0]
  303. block_stop = block[1] if len(block) > 1 else block[0]+1
  304. off_start = off[0]
  305. off_stop = off[1] if len(off) > 1 else off[0]+1
  306. if block_start is None:
  307. block_start = 0
  308. if block_stop is None and block_count is not None:
  309. block_stop = block_count
  310. if off_start is None:
  311. off_start = 0
  312. if off_stop is None and block_size is not None:
  313. off_stop = block_size
  314. bd = Bd(
  315. size=(block_size if block_size is not None
  316. else off_stop-off_start if off_stop is not None
  317. else 1),
  318. count=(block_count if block_count is not None
  319. else block_stop-block_start if block_stop is not None
  320. else 1),
  321. width=(width or 80)*height)
  322. lock = th.Lock()
  323. event = th.Event()
  324. done = False
  325. # adjust width?
  326. def resmoosh():
  327. if width is None:
  328. w = shutil.get_terminal_size((80, 0))[0] * height
  329. elif width == 0:
  330. w = max(int(bd.count*(scale or 1)), 1)
  331. else:
  332. w = width * height
  333. if scale and int(bd.count*scale) > w:
  334. c = int(w/scale)
  335. elif scale and int(bd.count*scale) < w:
  336. w = max(int(bd.count*(scale or 1)), 1)
  337. c = bd.count
  338. else:
  339. c = bd.count
  340. if w != bd.width or c != bd.count:
  341. bd.smoosh(width=w, count=c)
  342. resmoosh()
  343. # parse a line of trace output
  344. pattern = re.compile(
  345. 'trace.*?bd_(?:'
  346. '(?P<create>create\w*)\('
  347. '(?:'
  348. 'block_size=(?P<block_size>\w+)'
  349. '|' 'block_count=(?P<block_count>\w+)'
  350. '|' '.*?' ')*' '\)'
  351. '|' '(?P<read>read)\('
  352. '\s*(?P<read_ctx>\w+)\s*' ','
  353. '\s*(?P<read_block>\w+)\s*' ','
  354. '\s*(?P<read_off>\w+)\s*' ','
  355. '\s*(?P<read_buffer>\w+)\s*' ','
  356. '\s*(?P<read_size>\w+)\s*' '\)'
  357. '|' '(?P<prog>prog)\('
  358. '\s*(?P<prog_ctx>\w+)\s*' ','
  359. '\s*(?P<prog_block>\w+)\s*' ','
  360. '\s*(?P<prog_off>\w+)\s*' ','
  361. '\s*(?P<prog_buffer>\w+)\s*' ','
  362. '\s*(?P<prog_size>\w+)\s*' '\)'
  363. '|' '(?P<erase>erase)\('
  364. '\s*(?P<erase_ctx>\w+)\s*' ','
  365. '\s*(?P<erase_block>\w+)\s*' '\)'
  366. '|' '(?P<sync>sync)\('
  367. '\s*(?P<sync_ctx>\w+)\s*' '\)' ')')
  368. def parse_line(line):
  369. # string searching is actually much faster than
  370. # the regex here
  371. if 'trace' not in line or 'bd' not in line:
  372. return False
  373. m = pattern.search(line)
  374. if not m:
  375. return False
  376. if m.group('create'):
  377. # update our block size/count
  378. size = int(m.group('block_size'), 0)
  379. count = int(m.group('block_count'), 0)
  380. if off_stop is not None:
  381. size = off_stop-off_start
  382. if block_stop is not None:
  383. count = block_stop-block_start
  384. with lock:
  385. if reset:
  386. bd.reset()
  387. # ignore the new values if block_stop/off_stop is explicit
  388. bd.smoosh(
  389. size=(size if off_stop is None
  390. else off_stop-off_start),
  391. count=(count if block_stop is None
  392. else block_stop-block_start))
  393. return True
  394. elif m.group('read') and read:
  395. block = int(m.group('read_block'), 0)
  396. off = int(m.group('read_off'), 0)
  397. size = int(m.group('read_size'), 0)
  398. if block_stop is not None and block >= block_stop:
  399. return False
  400. block -= block_start
  401. if off_stop is not None:
  402. if off >= off_stop:
  403. return False
  404. size = min(size, off_stop-off)
  405. off -= off_start
  406. with lock:
  407. bd.read(block, slice(off,off+size))
  408. return True
  409. elif m.group('prog') and prog:
  410. block = int(m.group('prog_block'), 0)
  411. off = int(m.group('prog_off'), 0)
  412. size = int(m.group('prog_size'), 0)
  413. if block_stop is not None and block >= block_stop:
  414. return False
  415. block -= block_start
  416. if off_stop is not None:
  417. if off >= off_stop:
  418. return False
  419. size = min(size, off_stop-off)
  420. off -= off_start
  421. with lock:
  422. bd.prog(block, slice(off,off+size))
  423. return True
  424. elif m.group('erase') and (erase or wear):
  425. block = int(m.group('erase_block'), 0)
  426. if block_stop is not None and block >= block_stop:
  427. return False
  428. block -= block_start
  429. with lock:
  430. bd.erase(block)
  431. return True
  432. else:
  433. return False
  434. # print a pretty line of trace output
  435. history = []
  436. def push_line():
  437. # create copy to avoid corrupt output
  438. with lock:
  439. resmoosh()
  440. bd_ = bd.copy()
  441. bd.clear()
  442. max_wear = None
  443. if wear:
  444. max_wear = max(b.wear for b in bd_.blocks)
  445. def draw(b):
  446. return b.draw(
  447. read=read,
  448. prog=prog,
  449. erase=erase,
  450. wear=wear,
  451. color=color,
  452. max_wear=max_wear,
  453. block_cycles=block_cycles,
  454. **args)
  455. # fold via a curve?
  456. if height > 1:
  457. w = (len(bd.blocks)+height-1) // height
  458. if hilbert:
  459. grid = {}
  460. for (x,y),b in zip(hilbert_curve(w, height), bd_.blocks):
  461. grid[(x,y)] = draw(b)
  462. line = [
  463. ''.join(grid.get((x,y), ' ') for x in range(w))
  464. for y in range(height)]
  465. elif lebesgue:
  466. grid = {}
  467. for (x,y),b in zip(lebesgue_curve(w, height), bd_.blocks):
  468. grid[(x,y)] = draw(b)
  469. line = [
  470. ''.join(grid.get((x,y), ' ') for x in range(w))
  471. for y in range(height)]
  472. else:
  473. line = [
  474. ''.join(draw(b) for b in bd_.blocks[y*w:y*w+w])
  475. for y in range(height)]
  476. else:
  477. line = [''.join(draw(b) for b in bd_.blocks)]
  478. if not lines:
  479. # just go ahead and print here
  480. for row in line:
  481. sys.stdout.write(row)
  482. sys.stdout.write('\n')
  483. sys.stdout.flush()
  484. else:
  485. history.append(line)
  486. del history[:-lines]
  487. last_rows = 1
  488. def print_line():
  489. nonlocal last_rows
  490. if not lines:
  491. return
  492. # give ourself a canvas
  493. while last_rows < len(history)*height:
  494. sys.stdout.write('\n')
  495. last_rows += 1
  496. for i, row in enumerate(it.chain.from_iterable(history)):
  497. jump = len(history)*height-1-i
  498. # move cursor, clear line, disable/reenable line wrapping
  499. sys.stdout.write('\r')
  500. if jump > 0:
  501. sys.stdout.write('\x1b[%dA' % jump)
  502. sys.stdout.write('\x1b[K')
  503. sys.stdout.write('\x1b[?7l')
  504. sys.stdout.write(row)
  505. sys.stdout.write('\x1b[?7h')
  506. if jump > 0:
  507. sys.stdout.write('\x1b[%dB' % jump)
  508. if sleep is None or (coalesce and not lines):
  509. # read/parse coalesce number of operations
  510. try:
  511. while True:
  512. with openio(path) as f:
  513. changes = 0
  514. for line in f:
  515. change = parse_line(line)
  516. changes += change
  517. if change and changes % (coalesce or 1) == 0:
  518. push_line()
  519. print_line()
  520. # sleep between coalesced lines?
  521. if sleep is not None:
  522. time.sleep(sleep)
  523. if not keep_open:
  524. break
  525. # don't just flood open calls
  526. time.sleep(sleep or 0.1)
  527. except KeyboardInterrupt:
  528. pass
  529. else:
  530. # read/parse in a background thread
  531. def parse():
  532. nonlocal done
  533. while True:
  534. with openio(path) as f:
  535. changes = 0
  536. for line in f:
  537. change = parse_line(line)
  538. changes += change
  539. if change and changes % (coalesce or 1) == 0:
  540. if coalesce:
  541. push_line()
  542. event.set()
  543. if not keep_open:
  544. break
  545. # don't just flood open calls
  546. time.sleep(sleep or 0.1)
  547. done = True
  548. th.Thread(target=parse, daemon=True).start()
  549. try:
  550. while not done:
  551. time.sleep(sleep)
  552. event.wait()
  553. event.clear()
  554. if not coalesce:
  555. push_line()
  556. print_line()
  557. except KeyboardInterrupt:
  558. pass
  559. if lines:
  560. sys.stdout.write('\n')
  561. if __name__ == "__main__":
  562. import sys
  563. import argparse
  564. parser = argparse.ArgumentParser(
  565. description="Display operations on block devices based on "
  566. "trace output.")
  567. parser.add_argument(
  568. 'path',
  569. nargs='?',
  570. help="Path to read from.")
  571. parser.add_argument(
  572. '-r',
  573. '--read',
  574. action='store_true',
  575. help="Render reads.")
  576. parser.add_argument(
  577. '-p',
  578. '--prog',
  579. action='store_true',
  580. help="Render progs.")
  581. parser.add_argument(
  582. '-e',
  583. '--erase',
  584. action='store_true',
  585. help="Render erases.")
  586. parser.add_argument(
  587. '-w',
  588. '--wear',
  589. action='store_true',
  590. help="Render wear.")
  591. parser.add_argument(
  592. '--subscripts',
  593. help="Use unicode subscripts for showing wear.")
  594. parser.add_argument(
  595. '--chars',
  596. help="Characters to use for noop, read, prog, erase operations.")
  597. parser.add_argument(
  598. '--wear-chars',
  599. help="Characters to use to show wear.")
  600. parser.add_argument(
  601. '--color',
  602. choices=['never', 'always', 'auto'],
  603. default='auto',
  604. help="When to use terminal colors. Defaults to 'auto'.")
  605. parser.add_argument(
  606. '-b',
  607. '--block',
  608. type=lambda x: tuple(int(x,0) if x else None for x in x.split(',',1)),
  609. help="Show a specific block or range of blocks.")
  610. parser.add_argument(
  611. '-i',
  612. '--off',
  613. type=lambda x: tuple(int(x,0) if x else None for x in x.split(',',1)),
  614. help="Show a specific offset or range of offsets.")
  615. parser.add_argument(
  616. '-B',
  617. '--block-size',
  618. type=lambda x: int(x, 0),
  619. help="Assume a specific block size.")
  620. parser.add_argument(
  621. '--block-count',
  622. type=lambda x: int(x, 0),
  623. help="Assume a specific block count.")
  624. parser.add_argument(
  625. '-C',
  626. '--block-cycles',
  627. type=lambda x: int(x, 0),
  628. help="Assumed maximum number of erase cycles when measuring wear.")
  629. parser.add_argument(
  630. '-R',
  631. '--reset',
  632. action='store_true',
  633. help="Reset wear on block device initialization.")
  634. parser.add_argument(
  635. '-W',
  636. '--width',
  637. type=lambda x: int(x, 0),
  638. help="Width in columns. A width of 0 indicates no limit. Defaults "
  639. "to terminal width or 80.")
  640. parser.add_argument(
  641. '-H',
  642. '--height',
  643. type=lambda x: int(x, 0),
  644. help="Height in rows. Defaults to 1.")
  645. parser.add_argument(
  646. '-x',
  647. '--scale',
  648. type=float,
  649. help="Number of characters per block, ignores --width if set.")
  650. parser.add_argument(
  651. '-n',
  652. '--lines',
  653. type=lambda x: int(x, 0),
  654. help="Number of lines to show.")
  655. parser.add_argument(
  656. '-c',
  657. '--coalesce',
  658. type=lambda x: int(x, 0),
  659. help="Number of operations to coalesce together.")
  660. parser.add_argument(
  661. '-s',
  662. '--sleep',
  663. type=float,
  664. help="Time in seconds to sleep between reads, while coalescing "
  665. "operations.")
  666. parser.add_argument(
  667. '-I',
  668. '--hilbert',
  669. action='store_true',
  670. help="Render as a space-filling Hilbert curve.")
  671. parser.add_argument(
  672. '-Z',
  673. '--lebesgue',
  674. action='store_true',
  675. help="Render as a space-filling Z-curve.")
  676. parser.add_argument(
  677. '-k',
  678. '--keep-open',
  679. action='store_true',
  680. help="Reopen the pipe on EOF, useful when multiple "
  681. "processes are writing.")
  682. sys.exit(main(**{k: v
  683. for k, v in vars(parser.parse_intermixed_args()).items()
  684. if v is not None}))