Caffe2 - Python API
A deep learning, cross platform ML framework
binarysize.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 """A tool to inspect the binary size of a built binary file.
17 
18 This script prints out a tree of symbols and their corresponding sizes, using
19 Linux's nm functionality.
20 
21 Usage:
22 
23  python binary_size.py -- \
24  --target=/path/to/your/target/binary \
25  [--nm_command=/path/to/your/custom/nm] \
26  [--max_depth=10] [--min_size=1024] \
27  [--color] \
28 
29 To assist visualization, pass in '--color' to make the symbols color coded to
30 green, assuming that you have a xterm connection that supports color.
31 """
32 
33 from __future__ import absolute_import
34 from __future__ import division
35 from __future__ import print_function
36 from __future__ import unicode_literals
37 import argparse
38 import subprocess
39 import sys
40 
41 
42 class Trie(object):
43  """A simple class that represents a Trie."""
44 
45  def __init__(self, name):
46  """Initializes a Trie object."""
47  self.name = name
48  self.size = 0
49  self.dictionary = {}
50 
51 
52 def GetSymbolTrie(target, nm_command, max_depth):
53  """Gets a symbol trie with the passed in target.
54 
55  Args:
56  target: the target binary to inspect.
57  nm_command: the command to run nm.
58  max_depth: the maximum depth to create the trie.
59  """
60  # Run nm to get a dump on the strings.
61  proc = subprocess.Popen(
62  [nm_command, '--radix=d', '--size-sort', '--print-size', target],
63  stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
64  nm_out, _ = proc.communicate()
65  if proc.returncode != 0:
66  print('NM command failed. Output is as follows:')
67  print(nm_out)
68  sys.exit(1)
69  # Run c++filt to get proper symbols.
70  proc = subprocess.Popen(['c++filt'],
71  stdin=subprocess.PIPE, stdout=subprocess.PIPE,
72  stderr=subprocess.STDOUT)
73  out, _ = proc.communicate(input=nm_out)
74  if proc.returncode != 0:
75  print('c++filt failed. Output is as follows:')
76  print(out)
77  sys.exit(1)
78  # Splits the output to size and function name.
79  data = []
80  for line in out.split('\n'):
81  if line:
82  content = line.split(' ')
83  if len(content) < 4:
84  # This is a line not representing symbol sizes. skip.
85  continue
86  data.append([int(content[1]), ' '.join(content[3:])])
87  symbol_trie = Trie('')
88  for size, name in data:
89  curr = symbol_trie
90  for c in name:
91  if c not in curr.dictionary:
92  curr.dictionary[c] = Trie(curr.name + c)
93  curr = curr.dictionary[c]
94  curr.size += size
95  if len(curr.name) > max_depth:
96  break
97  symbol_trie.size = sum(t.size for t in symbol_trie.dictionary.values())
98  return symbol_trie
99 
100 
101 def MaybeAddColor(s, color):
102  """Wrap the input string to the xterm green color, if color is set.
103  """
104  if color:
105  return '\033[92m{0}\033[0m'.format(s)
106  else:
107  return s
108 
109 
110 def ReadableSize(num):
111  """Get a human-readable size."""
112  for unit in ['B', 'KB', 'MB', 'GB']:
113  if abs(num) <= 1024.0:
114  return '%3.2f%s' % (num, unit)
115  num /= 1024.0
116  return '%.1f TB' % (num,)
117 
118 
119 # Note(jiayq): I know, I know, this is a recursive function, but it is
120 # convenient to write.
121 def PrintTrie(trie, prefix, max_depth, min_size, color):
122  """Prints the symbol trie in a readable manner.
123  """
124  if len(trie.name) == max_depth or not trie.dictionary.keys():
125  # If we are reaching a leaf node or the maximum depth, we will print the
126  # result.
127  if trie.size > min_size:
128  print('{0}{1} {2}'.format(
129  prefix,
130  MaybeAddColor(trie.name, color),
131  ReadableSize(trie.size)))
132  elif len(trie.dictionary.keys()) == 1:
133  # There is only one child in this dictionary, so we will just delegate
134  # to the downstream trie to print stuff.
135  PrintTrie(
136  trie.dictionary.values()[0], prefix, max_depth, min_size, color)
137  elif trie.size > min_size:
138  print('{0}{1} {2}'.format(
139  prefix,
140  MaybeAddColor(trie.name, color),
141  ReadableSize(trie.size)))
142  keys_with_sizes = [
143  (k, trie.dictionary[k].size) for k in trie.dictionary.keys()]
144  keys_with_sizes.sort(key=lambda x: x[1])
145  for k, _ in keys_with_sizes[::-1]:
146  PrintTrie(
147  trie.dictionary[k], prefix + ' |', max_depth, min_size, color)
148 
149 
150 def main(argv):
151  if not sys.platform.startswith('linux'):
152  raise RuntimeError('Currently this tool only supports Linux.')
153  parser = argparse.ArgumentParser(
154  description="Tool to inspect binary size.")
155  parser.add_argument(
156  '--max_depth', type=int, default=10,
157  help='The maximum depth to print the symbol tree.')
158  parser.add_argument(
159  '--min_size', type=int, default=1024,
160  help='The mininum symbol size to print.')
161  parser.add_argument(
162  '--nm_command', type=str, default='nm',
163  help='The path to the nm command that the tool needs.')
164  parser.add_argument(
165  '--color', action='store_true',
166  help='If set, use ascii color for output.')
167  parser.add_argument(
168  '--target', type=str,
169  help='The binary target to inspect.')
170  args = parser.parse_args(argv)
171  if not args.target:
172  raise RuntimeError('You must specify a target to inspect.')
173  symbol_trie = GetSymbolTrie(
174  args.target, args.nm_command, args.max_depth)
175  PrintTrie(symbol_trie, '', args.max_depth, args.min_size, args.color)
176 
177 
178 if __name__ == '__main__':
179  main(sys.argv[1:])
def __init__(self, name)
Definition: binarysize.py:45