U
    k&p[R  ã                   @   s<   d dl Z d dlZd dlZd dlZd dlZG dd„ deƒZdS )é    Nc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚMemoryProfileDumperc                 C   s    || _ d| _d| _t ¡ | _d S )Nr   )ÚsettingsÚlast_memory_dumpZmemory_dump_counterÚtimeÚstarted)Úselfr   © r   ú-/usr/lib/python3/dist-packages/cliapp/util.pyÚ__init__   s    zMemoryProfileDumper.__init__c                 C   sê   | j d }| j d }|dkr dS t ¡ }| j| |kr:dS || _t ¡ \}}}}}	|	| j }
t d|
¡ t d|¡ t d|¡ t d|¡ t d	|¡ t d
|¡ t d|  ¡ ¡ |dkrÀdS t dt	t
 ¡ ƒ¡ t dt	t
jƒ¡ dS )a  Log memory profiling information.

        Get the memory profiling method from the dump-memory-profile
        setting, and log the results at DEBUG level. ``msg`` is a
        message the caller provides to identify at what point the profiling
        happens.

        zdump-memory-profilezmemory-dump-intervalZnoneNzprocess duration: %s szCPU time, in process: %s szCPU time, in system: %s szCPU time, in children: %s sz&CPU time, in system for children: %s sz!dumping memory profiling data: %szVmRSS: %s KiBZsimplez# objects: %dz# garbage: %d)r   r   r   ÚosÚtimesr   ÚloggingÚdebugÚ_vmrssÚlenÚgcZget_objectsZgarbage)r   ÚmsgZkindZintervalZnowÚutimeZstimeZcutimeZcstimeZelapsed_timeZdurationr   r   r	   Údump_memory_profile"   s*    



z'MemoryProfileDumper.dump_memory_profilec                 C   sd   t  ¡ dkrdS ztdƒ}W n tk
r2   Y dS X d}|D ]}| d¡r<| ¡ d }q<| ¡  |S )z+Return current resident memory use, in KiB.ZLinuxr   z/proc/self/statusZVmRSSé   )ÚplatformÚsystemÚopenÚIOErrorÚ
startswithÚsplitÚclose)r   ÚfZrssÚliner   r   r	   r   K   s    
zMemoryProfileDumper._vmrssN)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r   r   r   r	   r      s   )r   )r   r   r   r   r   Úobjectr   r   r   r   r	   Ú<module>   s
   