3
I4W^"  ã               @   s¢   d Z ddlmZ ddlmZ ddlmZ ddlm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rže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.
é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsNc               @   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   ú9/tmp/pip-build-e66j97b2/torch/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ƒ \}}|jdkrNtdƒ t|ƒ tjdƒ t jdgt jt jt jd	�}|j|d
�\}}|jdkrœtdƒ t|ƒ tjdƒ g }xR|j	dƒD ]D}|r¬|j	dƒ}	t
|	ƒdk rÌq¬|jt|	d ƒdj|	dd… ƒgƒ q¬W tdƒ}
xp|D ]h\}}|
}xX|D ]P}||jk�r8t|j| ƒ|j|< |j| }| j|7  _t
|jƒ|k�rP �qW �qW tdd„ |
jjƒ D ƒƒ|
_|
S )zÛGets 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%   sF    	





(

r;   c             C   s   |rdj | ƒ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   x,dD ]$}t | ƒdkr"d| |f S | d } qW d| f S )	zGet a human-readable size.ÚBÚKBÚMBÚGBg      �@z%3.2f%sz%.1f TB)r@   rA   rB   rC   )Úabs)ÚnumÚunitr   r   r   ÚReadableSize_   s
    
rG   c                s   t ˆ jƒ|ksˆ jjƒ  rFˆ j|krütdj|tˆ j|ƒtˆ jƒƒƒ n¶t ˆ jjƒ ƒdkrtt	ˆ jj
ƒ d ||||ƒ nˆˆ j|krütdj|tˆ j|ƒtˆ jƒƒƒ ‡ fdd„ˆ jjƒ D ƒ}|jdd„ d� x4|d	d	d… D ]"\}}t	ˆ j| |d
 |||ƒ qÖW d	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>�   s    zPrintTrie.<locals>.<lambda>)ÚkeyNz |éÿÿÿÿ)r)   r   r	   Úkeysr   r%   r<   r?   rG   Ú	PrintTrier.   Úsort)rI   Úprefixr1   Úmin_sizer>   Zkeys_with_sizesrH   r3   r   )rI   r   rP   j   s&    



rP   c             C   sº   t j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	| ƒ}|j
sŽt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.)ÚactionrY   z--targetzThe binary target to inspect.)rW   rY   z%You must specify a target to inspect.r   )r&   ÚplatformÚ
startswithÚRuntimeErrorÚargparseÚArgumentParserÚadd_argumentr+   ÚstrÚ
parse_argsr/   r;   r0   r1   rP   rS   r>   )ÚargvÚparserÚargsr8   r   r   r   Úmain‡   s2    
rh   Ú__main__r   )r   Ú
__future__r   r   r   r   r`   r   r&   Úobjectr   r;   r?   rG   rP   rh   r   re   r   r   r   r   Ú<module>   s   
1	