stack.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 -S
  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 glob
  15. import itertools as it
  16. import math as m
  17. import os
  18. import re
  19. CI_PATHS = ['*.ci']
  20. # integer fields
  21. class IntField(co.namedtuple('IntField', 'x')):
  22. __slots__ = ()
  23. def __new__(cls, x):
  24. if isinstance(x, IntField):
  25. return x
  26. if isinstance(x, str):
  27. try:
  28. x = int(x, 0)
  29. except ValueError:
  30. # also accept +-∞ and +-inf
  31. if re.match('^\s*\+?\s*(?:∞|inf)\s*$', x):
  32. x = float('inf')
  33. elif re.match('^\s*-\s*(?:∞|inf)\s*$', x):
  34. x = float('-inf')
  35. else:
  36. raise
  37. return super().__new__(cls, x)
  38. def __int__(self):
  39. assert not m.isinf(self.x)
  40. return self.x
  41. def __float__(self):
  42. return float(self.x)
  43. def __str__(self):
  44. if self.x == float('inf'):
  45. return '∞'
  46. elif self.x == float('-inf'):
  47. return '-∞'
  48. else:
  49. return str(self.x)
  50. none = '%7s' % '-'
  51. def table(self):
  52. return '%7s' % (self,)
  53. diff_none = '%7s' % '-'
  54. diff_table = table
  55. def diff_diff(self, other):
  56. new = self.x if self else 0
  57. old = other.x if other else 0
  58. diff = new - old
  59. if diff == float('+inf'):
  60. return '%7s' % '+∞'
  61. elif diff == float('-inf'):
  62. return '%7s' % '-∞'
  63. else:
  64. return '%+7d' % diff
  65. def ratio(self, other):
  66. new = self.x if self else 0
  67. old = other.x if other else 0
  68. if m.isinf(new) and m.isinf(old):
  69. return 0.0
  70. elif m.isinf(new):
  71. return float('+inf')
  72. elif m.isinf(old):
  73. return float('-inf')
  74. elif not old and not new:
  75. return 0.0
  76. elif not old:
  77. return 1.0
  78. else:
  79. return (new-old) / old
  80. def __add__(self, other):
  81. return IntField(self.x + other.x)
  82. def __mul__(self, other):
  83. return IntField(self.x * other.x)
  84. def __lt__(self, other):
  85. return self.x < other.x
  86. def __gt__(self, other):
  87. return self.__class__.__lt__(other, self)
  88. def __le__(self, other):
  89. return not self.__gt__(other)
  90. def __ge__(self, other):
  91. return not self.__lt__(other)
  92. def __truediv__(self, n):
  93. if m.isinf(self.x):
  94. return self
  95. else:
  96. return IntField(round(self.x / n))
  97. # size results
  98. class StackResult(co.namedtuple('StackResult',
  99. 'file,function,stack_frame,stack_limit')):
  100. __slots__ = ()
  101. def __new__(cls, file, function, stack_frame, stack_limit):
  102. return super().__new__(cls, file, function,
  103. IntField(stack_frame), IntField(stack_limit))
  104. def __add__(self, other):
  105. return StackResult(self.file, self.function,
  106. self.stack_frame + other.stack_frame,
  107. max(self.stack_limit, other.stack_limit))
  108. def openio(path, mode='r'):
  109. if path == '-':
  110. if mode == 'r':
  111. return os.fdopen(os.dup(sys.stdin.fileno()), 'r')
  112. else:
  113. return os.fdopen(os.dup(sys.stdout.fileno()), 'w')
  114. else:
  115. return open(path, mode)
  116. def collect(paths, *,
  117. everything=False,
  118. **args):
  119. # parse the vcg format
  120. k_pattern = re.compile('([a-z]+)\s*:', re.DOTALL)
  121. v_pattern = re.compile('(?:"(.*?)"|([a-z]+))', re.DOTALL)
  122. def parse_vcg(rest):
  123. def parse_vcg(rest):
  124. node = []
  125. while True:
  126. rest = rest.lstrip()
  127. m = k_pattern.match(rest)
  128. if not m:
  129. return (node, rest)
  130. k, rest = m.group(1), rest[m.end(0):]
  131. rest = rest.lstrip()
  132. if rest.startswith('{'):
  133. v, rest = parse_vcg(rest[1:])
  134. assert rest[0] == '}', "unexpected %r" % rest[0:1]
  135. rest = rest[1:]
  136. node.append((k, v))
  137. else:
  138. m = v_pattern.match(rest)
  139. assert m, "unexpected %r" % rest[0:1]
  140. v, rest = m.group(1) or m.group(2), rest[m.end(0):]
  141. node.append((k, v))
  142. node, rest = parse_vcg(rest)
  143. assert rest == '', "unexpected %r" % rest[0:1]
  144. return node
  145. # collect into functions
  146. callgraph = co.defaultdict(lambda: (None, None, 0, set()))
  147. f_pattern = re.compile(
  148. r'([^\\]*)\\n([^:]*)[^\\]*\\n([0-9]+) bytes \((.*)\)')
  149. for path in paths:
  150. with open(path) as f:
  151. vcg = parse_vcg(f.read())
  152. for k, graph in vcg:
  153. if k != 'graph':
  154. continue
  155. for k, info in graph:
  156. if k == 'node':
  157. info = dict(info)
  158. m = f_pattern.match(info['label'])
  159. if m:
  160. function, file, size, type = m.groups()
  161. if (not args.get('quiet')
  162. and 'static' not in type
  163. and 'bounded' not in type):
  164. print('warning: found non-static stack for %s (%s)'
  165. % (function, type, size))
  166. _, _, _, targets = callgraph[info['title']]
  167. callgraph[info['title']] = (
  168. file, function, int(size), targets)
  169. elif k == 'edge':
  170. info = dict(info)
  171. _, _, _, targets = callgraph[info['sourcename']]
  172. targets.add(info['targetname'])
  173. else:
  174. continue
  175. if not everything:
  176. for source, (s_file, s_function, _, _) in list(callgraph.items()):
  177. # discard internal functions
  178. if s_file.startswith('<') or s_file.startswith('/usr/include'):
  179. del callgraph[source]
  180. # find maximum stack size recursively, this requires also detecting cycles
  181. # (in case of recursion)
  182. def find_limit(source, seen=None):
  183. seen = seen or set()
  184. if source not in callgraph:
  185. return 0
  186. _, _, frame, targets = callgraph[source]
  187. limit = 0
  188. for target in targets:
  189. if target in seen:
  190. # found a cycle
  191. return float('inf')
  192. limit_ = find_limit(target, seen | {target})
  193. limit = max(limit, limit_)
  194. return frame + limit
  195. def find_calls(targets):
  196. calls = set()
  197. for target in targets:
  198. if target in callgraph:
  199. t_file, t_function, _, _ = callgraph[target]
  200. calls.add((t_file, t_function))
  201. return calls
  202. # build results
  203. results = []
  204. calls = {}
  205. for source, (s_file, s_function, frame, targets) in callgraph.items():
  206. limit = find_limit(source)
  207. cs = find_calls(targets)
  208. results.append(StackResult(s_file, s_function, frame, limit))
  209. calls[(s_file, s_function)] = cs
  210. return results, calls
  211. def fold(results, *,
  212. by=['file', 'function'],
  213. **_):
  214. folding = co.OrderedDict()
  215. for r in results:
  216. name = tuple(getattr(r, k) for k in by)
  217. if name not in folding:
  218. folding[name] = []
  219. folding[name].append(r)
  220. folded = []
  221. for rs in folding.values():
  222. folded.append(sum(rs[1:], start=rs[0]))
  223. return folded
  224. def fold_calls(calls, *,
  225. by=['file', 'function'],
  226. **_):
  227. def by_(name):
  228. file, function = name
  229. return (((file,) if 'file' in by else ())
  230. + ((function,) if 'function' in by else ()))
  231. folded = {}
  232. for name, cs in calls.items():
  233. name = by_(name)
  234. if name not in folded:
  235. folded[name] = set()
  236. folded[name] |= {by_(c) for c in cs}
  237. return folded
  238. def table(results, calls, diff_results=None, *,
  239. by_file=False,
  240. limit_sort=False,
  241. reverse_limit_sort=False,
  242. frame_sort=False,
  243. reverse_frame_sort=False,
  244. summary=False,
  245. all=False,
  246. percent=False,
  247. tree=False,
  248. depth=None,
  249. **_):
  250. all_, all = all, __builtins__.all
  251. # tree doesn't really make sense with depth=0, assume depth=inf
  252. if depth is None:
  253. depth = float('inf') if tree else 0
  254. # fold
  255. results = fold(results, by=['file' if by_file else 'function'])
  256. calls = fold_calls(calls, by=['file' if by_file else 'function'])
  257. if diff_results is not None:
  258. diff_results = fold(diff_results,
  259. by=['file' if by_file else 'function'])
  260. table = {
  261. r.file if by_file else r.function: r
  262. for r in results}
  263. diff_table = {
  264. r.file if by_file else r.function: r
  265. for r in diff_results or []}
  266. # sort, note that python's sort is stable
  267. names = list(table.keys() | diff_table.keys())
  268. names.sort()
  269. if diff_results is not None:
  270. names.sort(key=lambda n: -IntField.ratio(
  271. table[n].stack_frame if n in table else None,
  272. diff_table[n].stack_frame if n in diff_table else None))
  273. if limit_sort:
  274. names.sort(key=lambda n: (table[n].stack_limit,) if n in table else (),
  275. reverse=True)
  276. elif reverse_limit_sort:
  277. names.sort(key=lambda n: (table[n].stack_limit,) if n in table else (),
  278. reverse=False)
  279. elif frame_sort:
  280. names.sort(key=lambda n: (table[n].stack_frame,) if n in table else (),
  281. reverse=True)
  282. elif reverse_frame_sort:
  283. names.sort(key=lambda n: (table[n].stack_frame,) if n in table else (),
  284. reverse=False)
  285. # adjust the name width based on the expected call depth, note that we
  286. # can't always find the depth due to recursion
  287. width = 36 + (4*depth if not m.isinf(depth) else 0)
  288. # print header
  289. if not tree:
  290. print('%-*s' % (width, '%s%s' % (
  291. 'file' if by_file else 'function',
  292. ' (%d added, %d removed)' % (
  293. sum(1 for n in table if n not in diff_table),
  294. sum(1 for n in diff_table if n not in table))
  295. if diff_results is not None and not percent else '')
  296. if not summary else ''),
  297. end='')
  298. if diff_results is None:
  299. print(' %s %s' % (
  300. 'frame'.rjust(len(IntField.none)),
  301. 'limit'.rjust(len(IntField.none))))
  302. elif percent:
  303. print(' %s %s' % (
  304. 'frame'.rjust(len(IntField.diff_none)),
  305. 'limit'.rjust(len(IntField.diff_none))))
  306. else:
  307. print(' %s %s %s %s %s %s' % (
  308. 'oframe'.rjust(len(IntField.diff_none)),
  309. 'olimit'.rjust(len(IntField.diff_none)),
  310. 'nframe'.rjust(len(IntField.diff_none)),
  311. 'nlimit'.rjust(len(IntField.diff_none)),
  312. 'dframe'.rjust(len(IntField.diff_none)),
  313. 'dlimit'.rjust(len(IntField.diff_none))))
  314. # print entries
  315. if not summary:
  316. # print the tree recursively
  317. def table_calls(names_, depth,
  318. prefixes=('', '', '', '')):
  319. for i, name in enumerate(names_):
  320. r = table.get(name)
  321. if diff_results is not None:
  322. diff_r = diff_table.get(name)
  323. ratio = IntField.ratio(
  324. r.stack_limit if r else None,
  325. diff_r.stack_limit if diff_r else None)
  326. if not ratio and not all_:
  327. continue
  328. is_last = (i == len(names_)-1)
  329. print('%-*s' % (width, prefixes[0+is_last] + name), end='')
  330. if tree:
  331. print()
  332. elif diff_results is None:
  333. print(' %s %s' % (
  334. r.stack_frame.table()
  335. if r else IntField.none,
  336. r.stack_limit.table()
  337. if r else IntField.none))
  338. elif percent:
  339. print(' %s %s%s' % (
  340. r.stack_frame.diff_table()
  341. if r else IntField.diff_none,
  342. r.stack_limit.diff_table()
  343. if r else IntField.diff_none,
  344. ' (%s)' % (
  345. '+∞%' if ratio == float('+inf')
  346. else '-∞%' if ratio == float('-inf')
  347. else '%+.1f%%' % (100*ratio))))
  348. else:
  349. print(' %s %s %s %s %s %s%s' % (
  350. diff_r.stack_frame.diff_table()
  351. if diff_r else IntField.diff_none,
  352. diff_r.stack_limit.diff_table()
  353. if diff_r else IntField.diff_none,
  354. r.stack_frame.diff_table()
  355. if r else IntField.diff_none,
  356. r.stack_limit.diff_table()
  357. if r else IntField.diff_none,
  358. IntField.diff_diff(
  359. r.stack_frame if r else None,
  360. diff_r.stack_frame if diff_r else None)
  361. if r or diff_r else IntField.diff_none,
  362. IntField.diff_diff(
  363. r.stack_limit if r else None,
  364. diff_r.stack_limit if diff_r else None)
  365. if r or diff_r else IntField.diff_none,
  366. ' (%s)' % (
  367. '+∞%' if ratio == float('+inf')
  368. else '-∞%' if ratio == float('-inf')
  369. else '%+.1f%%' % (100*ratio))
  370. if ratio else ''))
  371. # recurse?
  372. if depth > 0:
  373. cs = calls.get((name,), set())
  374. table_calls(
  375. [n for n in names if (n,) in cs],
  376. depth-1,
  377. ( prefixes[2+is_last] + "|-> ",
  378. prefixes[2+is_last] + "'-> ",
  379. prefixes[2+is_last] + "| ",
  380. prefixes[2+is_last] + " "))
  381. table_calls(names, depth)
  382. # print total
  383. if not tree:
  384. total = fold(results, by=[])
  385. r = total[0] if total else None
  386. if diff_results is not None:
  387. diff_total = fold(diff_results, by=[])
  388. diff_r = diff_total[0] if diff_total else None
  389. ratio = IntField.ratio(
  390. r.stack_limit if r else None,
  391. diff_r.stack_limit if diff_r else None)
  392. print('%-*s' % (width, 'TOTAL'), end='')
  393. if diff_results is None:
  394. print(' %s %s' % (
  395. r.stack_frame.table()
  396. if r else IntField.none,
  397. r.stack_limit.table()
  398. if r else IntField.none))
  399. elif percent:
  400. print(' %s %s%s' % (
  401. r.stack_frame.diff_table()
  402. if r else IntField.diff_none,
  403. r.stack_limit.diff_table()
  404. if r else IntField.diff_none,
  405. ' (%s)' % (
  406. '+∞%' if ratio == float('+inf')
  407. else '-∞%' if ratio == float('-inf')
  408. else '%+.1f%%' % (100*ratio))))
  409. else:
  410. print(' %s %s %s %s %s %s%s' % (
  411. diff_r.stack_frame.diff_table()
  412. if diff_r else IntField.diff_none,
  413. diff_r.stack_limit.diff_table()
  414. if diff_r else IntField.diff_none,
  415. r.stack_frame.diff_table()
  416. if r else IntField.diff_none,
  417. r.stack_limit.diff_table()
  418. if r else IntField.diff_none,
  419. IntField.diff_diff(
  420. r.stack_frame if r else None,
  421. diff_r.stack_frame if diff_r else None)
  422. if r or diff_r else IntField.diff_none,
  423. IntField.diff_diff(
  424. r.stack_limit if r else None,
  425. diff_r.stack_limit if diff_r else None)
  426. if r or diff_r else IntField.diff_none,
  427. ' (%s)' % (
  428. '+∞%' if ratio == float('+inf')
  429. else '-∞%' if ratio == float('-inf')
  430. else '%+.1f%%' % (100*ratio))
  431. if ratio else ''))
  432. def main(ci_paths, **args):
  433. # find sizes
  434. if not args.get('use', None):
  435. # find .ci files
  436. paths = []
  437. for path in ci_paths:
  438. if os.path.isdir(path):
  439. path = path + '/*.ci'
  440. for path in glob.glob(path):
  441. paths.append(path)
  442. if not paths:
  443. print('no .ci files found in %r?' % ci_paths)
  444. sys.exit(-1)
  445. results, calls = collect(paths, **args)
  446. else:
  447. results = []
  448. with openio(args['use']) as f:
  449. reader = csv.DictReader(f, restval='')
  450. for r in reader:
  451. try:
  452. results.append(StackResult(**{
  453. k: v for k, v in r.items()
  454. if k in StackResult._fields}))
  455. except TypeError:
  456. pass
  457. calls = {}
  458. # fold to remove duplicates
  459. results = fold(results)
  460. # sort because why not
  461. results.sort()
  462. # write results to CSV
  463. if args.get('output'):
  464. with openio(args['output'], 'w') as f:
  465. writer = csv.DictWriter(f, StackResult._fields)
  466. writer.writeheader()
  467. for r in results:
  468. writer.writerow(r._asdict())
  469. # find previous results?
  470. if args.get('diff'):
  471. diff_results = []
  472. try:
  473. with openio(args['diff']) as f:
  474. reader = csv.DictReader(f, restval='')
  475. for r in reader:
  476. try:
  477. diff_results.append(StackResult(**{
  478. k: v for k, v in r.items()
  479. if k in StackResult._fields}))
  480. except TypeError:
  481. pass
  482. except FileNotFoundError:
  483. pass
  484. # fold to remove duplicates
  485. diff_results = fold(diff_results)
  486. # print table
  487. if not args.get('quiet'):
  488. table(
  489. results,
  490. calls,
  491. diff_results if args.get('diff') else None,
  492. **args)
  493. # error on recursion
  494. if args.get('error_on_recursion') and any(
  495. m.isinf(float(r.stack_limit)) for r in results):
  496. sys.exit(2)
  497. if __name__ == "__main__":
  498. import argparse
  499. import sys
  500. parser = argparse.ArgumentParser(
  501. description="Find stack usage at the function level.")
  502. parser.add_argument(
  503. 'ci_paths',
  504. nargs='*',
  505. default=CI_PATHS,
  506. help="Description of where to find *.ci files. May be a directory "
  507. "or a list of paths. Defaults to %r." % CI_PATHS)
  508. parser.add_argument(
  509. '-v', '--verbose',
  510. action='store_true',
  511. help="Output commands that run behind the scenes.")
  512. parser.add_argument(
  513. '-q', '--quiet',
  514. action='store_true',
  515. help="Don't show anything, useful with -o.")
  516. parser.add_argument(
  517. '-o', '--output',
  518. help="Specify CSV file to store results.")
  519. parser.add_argument(
  520. '-u', '--use',
  521. help="Don't parse anything, use this CSV file.")
  522. parser.add_argument(
  523. '-d', '--diff',
  524. help="Specify CSV file to diff against.")
  525. parser.add_argument(
  526. '-a', '--all',
  527. action='store_true',
  528. help="Show all, not just the ones that changed.")
  529. parser.add_argument(
  530. '-p', '--percent',
  531. action='store_true',
  532. help="Only show percentage change, not a full diff.")
  533. parser.add_argument(
  534. '-t', '--tree',
  535. action='store_true',
  536. help="Only show the function call tree.")
  537. parser.add_argument(
  538. '-b', '--by-file',
  539. action='store_true',
  540. help="Group by file.")
  541. parser.add_argument(
  542. '-s', '--limit-sort',
  543. action='store_true',
  544. help="Sort by stack limit.")
  545. parser.add_argument(
  546. '-S', '--reverse-limit-sort',
  547. action='store_true',
  548. help="Sort by stack limit, but backwards.")
  549. parser.add_argument(
  550. '--frame-sort',
  551. action='store_true',
  552. help="Sort by stack frame.")
  553. parser.add_argument(
  554. '--reverse-frame-sort',
  555. action='store_true',
  556. help="Sort by stack frame, but backwards.")
  557. parser.add_argument(
  558. '-Y', '--summary',
  559. action='store_true',
  560. help="Only show the total size.")
  561. parser.add_argument(
  562. '-L', '--depth',
  563. nargs='?',
  564. type=lambda x: int(x, 0),
  565. const=float('inf'),
  566. help="Depth of function calls to show.")
  567. parser.add_argument(
  568. '-e', '--error-on-recursion',
  569. action='store_true',
  570. help="Error if any functions are recursive.")
  571. parser.add_argument(
  572. '-A', '--everything',
  573. action='store_true',
  574. help="Include builtin and libc specific symbols.")
  575. parser.add_argument(
  576. '--build-dir',
  577. help="Specify the relative build directory. Used to map object files "
  578. "to the correct source files.")
  579. sys.exit(main(**{k: v
  580. for k, v in vars(parser.parse_intermixed_args()).items()
  581. if v is not None}))