stack.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #!/usr/bin/env python3
  2. #
  3. # Script to find stack usage at the function level. Will detect recursion and
  4. # report as infinite stack usage.
  5. #
  6. # Example:
  7. # ./scripts/stack.py lfs.ci lfs_util.ci -Slimit
  8. #
  9. # Copyright (c) 2022, The littlefs authors.
  10. # SPDX-License-Identifier: BSD-3-Clause
  11. #
  12. import collections as co
  13. import csv
  14. import itertools as it
  15. import math as m
  16. import os
  17. import re
  18. # integer fields
  19. class Int(co.namedtuple('Int', 'x')):
  20. __slots__ = ()
  21. def __new__(cls, x=0):
  22. if isinstance(x, Int):
  23. return x
  24. if isinstance(x, str):
  25. try:
  26. x = int(x, 0)
  27. except ValueError:
  28. # also accept +-∞ and +-inf
  29. if re.match('^\s*\+?\s*(?:∞|inf)\s*$', x):
  30. x = m.inf
  31. elif re.match('^\s*-\s*(?:∞|inf)\s*$', x):
  32. x = -m.inf
  33. else:
  34. raise
  35. assert isinstance(x, int) or m.isinf(x), x
  36. return super().__new__(cls, x)
  37. def __str__(self):
  38. if self.x == m.inf:
  39. return '∞'
  40. elif self.x == -m.inf:
  41. return '-∞'
  42. else:
  43. return str(self.x)
  44. def __int__(self):
  45. assert not m.isinf(self.x)
  46. return self.x
  47. def __float__(self):
  48. return float(self.x)
  49. none = '%7s' % '-'
  50. def table(self):
  51. return '%7s' % (self,)
  52. diff_none = '%7s' % '-'
  53. diff_table = table
  54. def diff_diff(self, other):
  55. new = self.x if self else 0
  56. old = other.x if other else 0
  57. diff = new - old
  58. if diff == +m.inf:
  59. return '%7s' % '+∞'
  60. elif diff == -m.inf:
  61. return '%7s' % '-∞'
  62. else:
  63. return '%+7d' % diff
  64. def ratio(self, other):
  65. new = self.x if self else 0
  66. old = other.x if other else 0
  67. if m.isinf(new) and m.isinf(old):
  68. return 0.0
  69. elif m.isinf(new):
  70. return +m.inf
  71. elif m.isinf(old):
  72. return -m.inf
  73. elif not old and not new:
  74. return 0.0
  75. elif not old:
  76. return 1.0
  77. else:
  78. return (new-old) / old
  79. def __add__(self, other):
  80. return self.__class__(self.x + other.x)
  81. def __sub__(self, other):
  82. return self.__class__(self.x - other.x)
  83. def __mul__(self, other):
  84. return self.__class__(self.x * other.x)
  85. # size results
  86. class StackResult(co.namedtuple('StackResult', [
  87. 'file', 'function', 'frame', 'limit', 'children'])):
  88. _by = ['file', 'function']
  89. _fields = ['frame', 'limit']
  90. _types = {'frame': Int, 'limit': Int}
  91. __slots__ = ()
  92. def __new__(cls, file='', function='',
  93. frame=0, limit=0, children=set()):
  94. return super().__new__(cls, file, function,
  95. Int(frame), Int(limit),
  96. children)
  97. def __add__(self, other):
  98. return StackResult(self.file, self.function,
  99. self.frame + other.frame,
  100. max(self.limit, other.limit),
  101. self.children | other.children)
  102. def openio(path, mode='r', buffering=-1):
  103. if path == '-':
  104. if mode == 'r':
  105. return os.fdopen(os.dup(sys.stdin.fileno()), mode, buffering)
  106. else:
  107. return os.fdopen(os.dup(sys.stdout.fileno()), mode, buffering)
  108. else:
  109. return open(path, mode, buffering)
  110. def collect(ci_paths, *,
  111. sources=None,
  112. everything=False,
  113. **args):
  114. # parse the vcg format
  115. k_pattern = re.compile('([a-z]+)\s*:', re.DOTALL)
  116. v_pattern = re.compile('(?:"(.*?)"|([a-z]+))', re.DOTALL)
  117. def parse_vcg(rest):
  118. def parse_vcg(rest):
  119. node = []
  120. while True:
  121. rest = rest.lstrip()
  122. m_ = k_pattern.match(rest)
  123. if not m_:
  124. return (node, rest)
  125. k, rest = m_.group(1), rest[m_.end(0):]
  126. rest = rest.lstrip()
  127. if rest.startswith('{'):
  128. v, rest = parse_vcg(rest[1:])
  129. assert rest[0] == '}', "unexpected %r" % rest[0:1]
  130. rest = rest[1:]
  131. node.append((k, v))
  132. else:
  133. m_ = v_pattern.match(rest)
  134. assert m_, "unexpected %r" % rest[0:1]
  135. v, rest = m_.group(1) or m_.group(2), rest[m_.end(0):]
  136. node.append((k, v))
  137. node, rest = parse_vcg(rest)
  138. assert rest == '', "unexpected %r" % rest[0:1]
  139. return node
  140. # collect into functions
  141. callgraph = co.defaultdict(lambda: (None, None, 0, set()))
  142. f_pattern = re.compile(
  143. r'([^\\]*)\\n([^:]*)[^\\]*\\n([0-9]+) bytes \((.*)\)')
  144. for path in ci_paths:
  145. with open(path) as f:
  146. vcg = parse_vcg(f.read())
  147. for k, graph in vcg:
  148. if k != 'graph':
  149. continue
  150. for k, info in graph:
  151. if k == 'node':
  152. info = dict(info)
  153. m_ = f_pattern.match(info['label'])
  154. if m_:
  155. function, file, size, type = m_.groups()
  156. if (not args.get('quiet')
  157. and 'static' not in type
  158. and 'bounded' not in type):
  159. print("warning: "
  160. "found non-static stack for %s (%s, %s)" % (
  161. function, type, size))
  162. _, _, _, targets = callgraph[info['title']]
  163. callgraph[info['title']] = (
  164. file, function, int(size), targets)
  165. elif k == 'edge':
  166. info = dict(info)
  167. _, _, _, targets = callgraph[info['sourcename']]
  168. targets.add(info['targetname'])
  169. else:
  170. continue
  171. callgraph_ = co.defaultdict(lambda: (None, None, 0, set()))
  172. for source, (s_file, s_function, frame, targets) in callgraph.items():
  173. # discard internal functions
  174. if not everything and s_function.startswith('__'):
  175. continue
  176. # ignore filtered sources
  177. if sources is not None:
  178. if not any(
  179. os.path.abspath(s_file) == os.path.abspath(s)
  180. for s in sources):
  181. continue
  182. else:
  183. # default to only cwd
  184. if not everything and not os.path.commonpath([
  185. os.getcwd(),
  186. os.path.abspath(s_file)]) == os.getcwd():
  187. continue
  188. # smiplify path
  189. if os.path.commonpath([
  190. os.getcwd(),
  191. os.path.abspath(s_file)]) == os.getcwd():
  192. s_file = os.path.relpath(s_file)
  193. else:
  194. s_file = os.path.abspath(s_file)
  195. callgraph_[source] = (s_file, s_function, frame, targets)
  196. callgraph = callgraph_
  197. if not everything:
  198. callgraph_ = co.defaultdict(lambda: (None, None, 0, set()))
  199. for source, (s_file, s_function, frame, targets) in callgraph.items():
  200. # discard filtered sources
  201. if sources is not None and not any(
  202. os.path.abspath(s_file) == os.path.abspath(s)
  203. for s in sources):
  204. continue
  205. # discard internal functions
  206. if s_function.startswith('__'):
  207. continue
  208. callgraph_[source] = (s_file, s_function, frame, targets)
  209. callgraph = callgraph_
  210. # find maximum stack size recursively, this requires also detecting cycles
  211. # (in case of recursion)
  212. def find_limit(source, seen=None):
  213. seen = seen or set()
  214. if source not in callgraph:
  215. return 0
  216. _, _, frame, targets = callgraph[source]
  217. limit = 0
  218. for target in targets:
  219. if target in seen:
  220. # found a cycle
  221. return m.inf
  222. limit_ = find_limit(target, seen | {target})
  223. limit = max(limit, limit_)
  224. return frame + limit
  225. def find_children(targets):
  226. children = set()
  227. for target in targets:
  228. if target in callgraph:
  229. t_file, t_function, _, _ = callgraph[target]
  230. children.add((t_file, t_function))
  231. return children
  232. # build results
  233. results = []
  234. for source, (s_file, s_function, frame, targets) in callgraph.items():
  235. limit = find_limit(source)
  236. children = find_children(targets)
  237. results.append(StackResult(s_file, s_function, frame, limit, children))
  238. return results
  239. def fold(Result, results, *,
  240. by=None,
  241. defines=None,
  242. **_):
  243. if by is None:
  244. by = Result._by
  245. for k in it.chain(by or [], (k for k, _ in defines or [])):
  246. if k not in Result._by and k not in Result._fields:
  247. print("error: could not find field %r?" % k)
  248. sys.exit(-1)
  249. # filter by matching defines
  250. if defines is not None:
  251. results_ = []
  252. for r in results:
  253. if all(getattr(r, k) in vs for k, vs in defines):
  254. results_.append(r)
  255. results = results_
  256. # organize results into conflicts
  257. folding = co.OrderedDict()
  258. for r in results:
  259. name = tuple(getattr(r, k) for k in by)
  260. if name not in folding:
  261. folding[name] = []
  262. folding[name].append(r)
  263. # merge conflicts
  264. folded = []
  265. for name, rs in folding.items():
  266. folded.append(sum(rs[1:], start=rs[0]))
  267. return folded
  268. def table(Result, results, diff_results=None, *,
  269. by=None,
  270. fields=None,
  271. sort=None,
  272. summary=False,
  273. all=False,
  274. percent=False,
  275. tree=False,
  276. depth=1,
  277. **_):
  278. all_, all = all, __builtins__.all
  279. if by is None:
  280. by = Result._by
  281. if fields is None:
  282. fields = Result._fields
  283. types = Result._types
  284. # fold again
  285. results = fold(Result, results, by=by)
  286. if diff_results is not None:
  287. diff_results = fold(Result, diff_results, by=by)
  288. # organize by name
  289. table = {
  290. ','.join(str(getattr(r, k) or '') for k in by): r
  291. for r in results}
  292. diff_table = {
  293. ','.join(str(getattr(r, k) or '') for k in by): r
  294. for r in diff_results or []}
  295. names = list(table.keys() | diff_table.keys())
  296. # sort again, now with diff info, note that python's sort is stable
  297. names.sort()
  298. if diff_results is not None:
  299. names.sort(key=lambda n: tuple(
  300. types[k].ratio(
  301. getattr(table.get(n), k, None),
  302. getattr(diff_table.get(n), k, None))
  303. for k in fields),
  304. reverse=True)
  305. if sort:
  306. for k, reverse in reversed(sort):
  307. names.sort(key=lambda n: (getattr(table[n], k),)
  308. if getattr(table.get(n), k, None) is not None else (),
  309. reverse=reverse ^ (not k or k in Result._fields))
  310. # build up our lines
  311. lines = []
  312. # header
  313. header = []
  314. header.append('%s%s' % (
  315. ','.join(by),
  316. ' (%d added, %d removed)' % (
  317. sum(1 for n in table if n not in diff_table),
  318. sum(1 for n in diff_table if n not in table))
  319. if diff_results is not None and not percent else '')
  320. if not summary else '')
  321. if diff_results is None:
  322. for k in fields:
  323. header.append(k)
  324. elif percent:
  325. for k in fields:
  326. header.append(k)
  327. else:
  328. for k in fields:
  329. header.append('o'+k)
  330. for k in fields:
  331. header.append('n'+k)
  332. for k in fields:
  333. header.append('d'+k)
  334. header.append('')
  335. lines.append(header)
  336. def table_entry(name, r, diff_r=None, ratios=[]):
  337. entry = []
  338. entry.append(name)
  339. if diff_results is None:
  340. for k in fields:
  341. entry.append(getattr(r, k).table()
  342. if getattr(r, k, None) is not None
  343. else types[k].none)
  344. elif percent:
  345. for k in fields:
  346. entry.append(getattr(r, k).diff_table()
  347. if getattr(r, k, None) is not None
  348. else types[k].diff_none)
  349. else:
  350. for k in fields:
  351. entry.append(getattr(diff_r, k).diff_table()
  352. if getattr(diff_r, k, None) is not None
  353. else types[k].diff_none)
  354. for k in fields:
  355. entry.append(getattr(r, k).diff_table()
  356. if getattr(r, k, None) is not None
  357. else types[k].diff_none)
  358. for k in fields:
  359. entry.append(types[k].diff_diff(
  360. getattr(r, k, None),
  361. getattr(diff_r, k, None)))
  362. if diff_results is None:
  363. entry.append('')
  364. elif percent:
  365. entry.append(' (%s)' % ', '.join(
  366. '+∞%' if t == +m.inf
  367. else '-∞%' if t == -m.inf
  368. else '%+.1f%%' % (100*t)
  369. for t in ratios))
  370. else:
  371. entry.append(' (%s)' % ', '.join(
  372. '+∞%' if t == +m.inf
  373. else '-∞%' if t == -m.inf
  374. else '%+.1f%%' % (100*t)
  375. for t in ratios
  376. if t)
  377. if any(ratios) else '')
  378. return entry
  379. # entries
  380. if not summary:
  381. for name in names:
  382. r = table.get(name)
  383. if diff_results is None:
  384. diff_r = None
  385. ratios = None
  386. else:
  387. diff_r = diff_table.get(name)
  388. ratios = [
  389. types[k].ratio(
  390. getattr(r, k, None),
  391. getattr(diff_r, k, None))
  392. for k in fields]
  393. if not all_ and not any(ratios):
  394. continue
  395. lines.append(table_entry(name, r, diff_r, ratios))
  396. # total
  397. r = next(iter(fold(Result, results, by=[])), None)
  398. if diff_results is None:
  399. diff_r = None
  400. ratios = None
  401. else:
  402. diff_r = next(iter(fold(Result, diff_results, by=[])), None)
  403. ratios = [
  404. types[k].ratio(
  405. getattr(r, k, None),
  406. getattr(diff_r, k, None))
  407. for k in fields]
  408. lines.append(table_entry('TOTAL', r, diff_r, ratios))
  409. # find the best widths, note that column 0 contains the names and column -1
  410. # the ratios, so those are handled a bit differently
  411. widths = [
  412. ((max(it.chain([w], (len(l[i]) for l in lines)))+1+4-1)//4)*4-1
  413. for w, i in zip(
  414. it.chain([23], it.repeat(7)),
  415. range(len(lines[0])-1))]
  416. # adjust the name width based on the expected call depth, though
  417. # note this doesn't really work with unbounded recursion
  418. if not summary and not m.isinf(depth):
  419. widths[0] += 4*(depth-1)
  420. # print the tree recursively
  421. if not tree:
  422. print('%-*s %s%s' % (
  423. widths[0], lines[0][0],
  424. ' '.join('%*s' % (w, x)
  425. for w, x in zip(widths[1:], lines[0][1:-1])),
  426. lines[0][-1]))
  427. if not summary:
  428. line_table = {n: l for n, l in zip(names, lines[1:-1])}
  429. def recurse(names_, depth_, prefixes=('', '', '', '')):
  430. for i, name in enumerate(names_):
  431. if name not in line_table:
  432. continue
  433. line = line_table[name]
  434. is_last = (i == len(names_)-1)
  435. print('%s%-*s ' % (
  436. prefixes[0+is_last],
  437. widths[0] - (
  438. len(prefixes[0+is_last])
  439. if not m.isinf(depth) else 0),
  440. line[0]),
  441. end='')
  442. if not tree:
  443. print(' %s%s' % (
  444. ' '.join('%*s' % (w, x)
  445. for w, x in zip(widths[1:], line[1:-1])),
  446. line[-1]),
  447. end='')
  448. print()
  449. # recurse?
  450. if name in table and depth_ > 1:
  451. children = {
  452. ','.join(str(getattr(Result(*c), k) or '') for k in by)
  453. for c in table[name].children}
  454. recurse(
  455. # note we're maintaining sort order
  456. [n for n in names if n in children],
  457. depth_-1,
  458. (prefixes[2+is_last] + "|-> ",
  459. prefixes[2+is_last] + "'-> ",
  460. prefixes[2+is_last] + "| ",
  461. prefixes[2+is_last] + " "))
  462. recurse(names, depth)
  463. if not tree:
  464. print('%-*s %s%s' % (
  465. widths[0], lines[-1][0],
  466. ' '.join('%*s' % (w, x)
  467. for w, x in zip(widths[1:], lines[-1][1:-1])),
  468. lines[-1][-1]))
  469. def main(ci_paths,
  470. by=None,
  471. fields=None,
  472. defines=None,
  473. sort=None,
  474. **args):
  475. # it doesn't really make sense to not have a depth with tree,
  476. # so assume depth=inf if tree by default
  477. if args.get('depth') is None:
  478. args['depth'] = m.inf if args['tree'] else 1
  479. elif args.get('depth') == 0:
  480. args['depth'] = m.inf
  481. # find sizes
  482. if not args.get('use', None):
  483. results = collect(ci_paths, **args)
  484. else:
  485. results = []
  486. with openio(args['use']) as f:
  487. reader = csv.DictReader(f, restval='')
  488. for r in reader:
  489. try:
  490. results.append(StackResult(
  491. **{k: r[k] for k in StackResult._by
  492. if k in r and r[k].strip()},
  493. **{k: r['stack_'+k] for k in StackResult._fields
  494. if 'stack_'+k in r and r['stack_'+k].strip()}))
  495. except TypeError:
  496. pass
  497. # fold
  498. results = fold(StackResult, results, by=by, defines=defines)
  499. # sort, note that python's sort is stable
  500. results.sort()
  501. if sort:
  502. for k, reverse in reversed(sort):
  503. results.sort(key=lambda r: (getattr(r, k),)
  504. if getattr(r, k) is not None else (),
  505. reverse=reverse ^ (not k or k in StackResult._fields))
  506. # write results to CSV
  507. if args.get('output'):
  508. with openio(args['output'], 'w') as f:
  509. writer = csv.DictWriter(f,
  510. (by if by is not None else StackResult._by)
  511. + ['stack_'+k for k in StackResult._fields])
  512. writer.writeheader()
  513. for r in results:
  514. writer.writerow(
  515. {k: getattr(r, k)
  516. for k in (by if by is not None else StackResult._by)}
  517. | {'stack_'+k: getattr(r, k)
  518. for k in StackResult._fields})
  519. # find previous results?
  520. if args.get('diff'):
  521. diff_results = []
  522. try:
  523. with openio(args['diff']) as f:
  524. reader = csv.DictReader(f, restval='')
  525. for r in reader:
  526. try:
  527. diff_results.append(StackResult(
  528. **{k: r[k] for k in StackResult._by
  529. if k in r and r[k].strip()},
  530. **{k: r['stack_'+k] for k in StackResult._fields
  531. if 'stack_'+k in r and r['stack_'+k].strip()}))
  532. except TypeError:
  533. raise
  534. except FileNotFoundError:
  535. pass
  536. # fold
  537. diff_results = fold(StackResult, diff_results, by=by, defines=defines)
  538. # print table
  539. if not args.get('quiet'):
  540. table(StackResult, results,
  541. diff_results if args.get('diff') else None,
  542. by=by if by is not None else ['function'],
  543. fields=fields,
  544. sort=sort,
  545. **args)
  546. # error on recursion
  547. if args.get('error_on_recursion') and any(
  548. m.isinf(float(r.limit)) for r in results):
  549. sys.exit(2)
  550. if __name__ == "__main__":
  551. import argparse
  552. import sys
  553. parser = argparse.ArgumentParser(
  554. description="Find stack usage at the function level.",
  555. allow_abbrev=False)
  556. parser.add_argument(
  557. 'ci_paths',
  558. nargs='*',
  559. help="Input *.ci files.")
  560. parser.add_argument(
  561. '-v', '--verbose',
  562. action='store_true',
  563. help="Output commands that run behind the scenes.")
  564. parser.add_argument(
  565. '-q', '--quiet',
  566. action='store_true',
  567. help="Don't show anything, useful with -o.")
  568. parser.add_argument(
  569. '-o', '--output',
  570. help="Specify CSV file to store results.")
  571. parser.add_argument(
  572. '-u', '--use',
  573. help="Don't parse anything, use this CSV file.")
  574. parser.add_argument(
  575. '-d', '--diff',
  576. help="Specify CSV file to diff against.")
  577. parser.add_argument(
  578. '-a', '--all',
  579. action='store_true',
  580. help="Show all, not just the ones that changed.")
  581. parser.add_argument(
  582. '-p', '--percent',
  583. action='store_true',
  584. help="Only show percentage change, not a full diff.")
  585. parser.add_argument(
  586. '-b', '--by',
  587. action='append',
  588. choices=StackResult._by,
  589. help="Group by this field.")
  590. parser.add_argument(
  591. '-f', '--field',
  592. dest='fields',
  593. action='append',
  594. choices=StackResult._fields,
  595. help="Show this field.")
  596. parser.add_argument(
  597. '-D', '--define',
  598. dest='defines',
  599. action='append',
  600. type=lambda x: (lambda k,v: (k, set(v.split(','))))(*x.split('=', 1)),
  601. help="Only include results where this field is this value.")
  602. class AppendSort(argparse.Action):
  603. def __call__(self, parser, namespace, value, option):
  604. if namespace.sort is None:
  605. namespace.sort = []
  606. namespace.sort.append((value, True if option == '-S' else False))
  607. parser.add_argument(
  608. '-s', '--sort',
  609. action=AppendSort,
  610. help="Sort by this fields.")
  611. parser.add_argument(
  612. '-S', '--reverse-sort',
  613. action=AppendSort,
  614. help="Sort by this fields, but backwards.")
  615. parser.add_argument(
  616. '-Y', '--summary',
  617. action='store_true',
  618. help="Only show the total.")
  619. parser.add_argument(
  620. '-F', '--source',
  621. dest='sources',
  622. action='append',
  623. help="Only consider definitions in this file. Defaults to anything "
  624. "in the current directory.")
  625. parser.add_argument(
  626. '--everything',
  627. action='store_true',
  628. help="Include builtin and libc specific symbols.")
  629. parser.add_argument(
  630. '--tree',
  631. action='store_true',
  632. help="Only show the function call tree.")
  633. parser.add_argument(
  634. '-Z', '--depth',
  635. nargs='?',
  636. type=lambda x: int(x, 0),
  637. const=0,
  638. help="Depth of function calls to show. 0 shows all calls but may not "
  639. "terminate!")
  640. parser.add_argument(
  641. '-e', '--error-on-recursion',
  642. action='store_true',
  643. help="Error if any functions are recursive.")
  644. sys.exit(main(**{k: v
  645. for k, v in vars(parser.parse_intermixed_args()).items()
  646. if v is not None}))