a
    –¥»a‘  ã                   @   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   úR/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/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rœtdƒ t|ƒ t d¡ g }| 	d¡D ]D}|rª| 	d¡}	t
|	ƒdk rÊqª| t|	d ƒd |	dd… ¡g¡ qªtdƒ}
|D ]d\}}|
}|D ]R}||jv�r0t|j| ƒ|j|< |j| }| j|7  _t
|jƒ|k�r qü�qqütdd„ |
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   ó    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%   sJ    	þ

þ


&

r8   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
    
rA   c                    sú   t ˆ jƒ|ksˆ j ¡ sDˆ j|krötd |tˆ j|ƒtˆ jƒ¡ƒ n²t ˆ j ¡ ƒdkrrt	ˆ j 
¡ d ||||ƒ n„ˆ j|krötd |tˆ j|ƒtˆ jƒ¡ƒ ‡ fdd„ˆ j ¡ D ƒ}|jdd„ d� |d	d	d
… D ]"\}}t	ˆ j| |d |||ƒ qÒ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>�   r   zPrintTrie.<locals>.<lambda>)ÚkeyNéÿÿÿÿz |)r&   r   r   Úkeysr   r"   r9   r<   rA   Ú	PrintTrier+   Úsort)rD   Úprefixr.   Úmin_sizer;   Zkeys_with_sizesrB   r0   r   rC   r   rK   j   s2    

ýÿ

ý
ÿÿ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
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.)Ú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,   r8   r-   r.   rK   rN   r;   )ÚargvÚparserÚargsr5   r   r   r   Úmain‡   s@    ÿþþþþþ
ÿrc   Ú__main__r   )r   r[   r   r#   Úobjectr   r8   r<   rA   rK   rc   r
   r`   r   r   r   r   Ú<module>   s   
1	