U
    d                     @   sr   d Z ddlZddlZddlZG dd deZdd Zdd Zd	d
 Zdd Z	dd Z
edkrne
ejdd  dS )a  A tool to inspect the binary size of a built binary file.

This script prints out a tree of symbols and their corresponding sizes, using
Linux's nm functionality.

Usage:

    python binary_size.py --             --target=/path/to/your/target/binary             [--nm_command=/path/to/your/custom/nm]             [--max_depth=10] [--min_size=1024]             [--color] 
To assist visualization, pass in '--color' to make the symbols color coded to
green, assuming that you have a xterm connection that supports color.
    Nc                   @   s   e Zd ZdZdd ZdS )Triez&A simple class that represents a Trie.c                 C   s   || _ d| _i | _dS )zInitializes a Trie object.r   N)namesize
dictionary)selfr    r   </tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/binarysize.py__init__   s    zTrie.__init__N)__name__
__module____qualname____doc__r	   r   r   r   r   r      s   r   c              	   C   s  t j|ddd| gt jt jd}| \}}|jdkrNtd t| td t jdgt jt jt jd	}|j|d
\}}|jdkrtd t| td g }|	dD ]D}|r|	d}	t
|	dk rq|t|	d d|	dd g qtd}
|D ]d\}}|
}|D ]R}||jkr0t|j| |j|< |j| }| j|7  _t
|j|kr qqqtdd |
j D |
_|
S )zGets a symbol trie with the passed in target.

    Args:
            target: the target binary to inspect.
            nm_command: the command to run nm.
            max_depth: the maximum depth to create the trie.
    z	--radix=dz--size-sortz--print-size)stdoutstderrr   z(NM command failed. Output is as follows:   zc++filt)stdinr   r   )inputz%c++filt failed. Output is as follows:
       N c                 s   s   | ]}|j V  qd S )N)r   ).0tr   r   r   	<genexpr>R   s     z GetSymbolTrie.<locals>.<genexpr>)
subprocessPopenPIPESTDOUTcommunicate
returncodeprintsysexitsplitlenappendintjoinr   r   r   r   sumvalues)target
nm_command	max_depthprocZnm_out_outdatalinecontentsymbol_trier   r   currcr   r   r   GetSymbolTrie%   sN    	 

 


&

r7   c                 C   s   |rd | S | S dS )zEWrap the input string to the xterm green color, if color is set.
    z[92m{0}[0mN)format)scolorr   r   r   MaybeAddColorV   s    
r;   c                 C   s8   dD ](}t | dkr$d| |f   S | d } qd| f S )zGet a human-readable size.)BZKBZMBZGBg      @z%3.2f%sz%.1f TB)abs)numunitr   r   r   ReadableSize_   s
    
r@   c                    s   t  j|ks j sD j|krtd|t j|t j nt  j dkrrt	 j
 d |||| n j|krtd|t j|t j  fdd j D }|jdd d |d	d	d
 D ]"\}}t	 j| |d ||| qd	S )z1Prints the symbol trie in a readable manner.
    z
{0}{1} {2}r   r   c                    s   g | ]}| j | jfqS r   )r   r   )r   ktrier   r   
<listcomp>   s    zPrintTrie.<locals>.<listcomp>c                 S   s   | d S )Nr   r   )xr   r   r   <lambda>       zPrintTrie.<locals>.<lambda>)keyNz |)r%   r   r   keysr   r!   r8   r;   r@   	PrintTrier*   sort)rC   prefixr-   min_sizer:   Zkeys_with_sizesrA   r/   r   rB   r   rK   j   sB    

    


    rK   c                 C   s   t jdstdtjdd}|jdtddd |jd	td
dd |jdtddd |jdddd |jdtdd |	| }|j
stdt|j
|j|j}t|d|j|j|j d S )Nlinuxz(Currently this tool only supports Linux.zTool to inspect binary size.)descriptionz--max_depth
   z+The maximum depth to print the symbol tree.)typedefaulthelpz
--min_sizei   z!The mininum symbol size to print.z--nm_commandnmz/The path to the nm command that the tool needs.z--color
store_truez#If set, use ascii color for output.)actionrT   z--targetzThe binary target to inspect.)rR   rT   z%You must specify a target to inspect.r   )r"   platform
startswithRuntimeErrorargparseArgumentParseradd_argumentr'   str
parse_argsr+   r7   r,   r-   rK   rN   r:   )argvparserargsr4   r   r   r   main   sT            
  rc   __main__r   )r   r[   r   r"   objectr   r7   r;   r@   rK   rc   r
   r`   r   r   r   r   <module>   s   
1	