File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/profile.cpython-310.pyc
o
�N�g� � @ s� d Z ddlZddlZddlZddlZddlZddlZddlZddl Zddl
Ze�e�Z
zddlZdZW n ey> dZY nw ddd�Zd
dd�Zdd
d�ZdS )z8
Decorator and functions to profile Salt using cProfile
� NTFc s � fdd�}|S )zE
Decorator for adding profiling to a nested function in Salt
c s �� fdd�}|S )Nc sl t �d�j� zt�� }|j�g| �R i |��}|�� p!�j� d�� W |S ty5 t �d� � Y |S w )NzProfiling function %sz
_func.profilezCould not open profile file %s) �logging�info�__name__�cProfile�ProfileZruncall�
dump_stats�OSError� exception)�args�kwargsZprofiler�retval)�filename�fun� �F/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/profile.py�
profiled_func s ��z5profile_func.<locals>.proffunc.<locals>.profiled_funcr )r r �r
)r r �proffunc s zprofile_func.<locals>.proffuncr )r
r r r r �profile_func s r c C s. d }| rt rt�� }|�� |S t�d� |S )Nz*cProfile is not available on your platform)�HAS_CPROFILEr r �enable�log�error)�test�prr r r �activate_profile0 s
�r �
/tmp/statsc
C s | d ur�t r�z�| �� tj�|�st�|� tj�� �� }|d u r)t j
jjdd�}tj�
||� d|� d��}tj�
||� d|� d��}tj�
||� d|� d��}tj�|�s}| �|� t j
j�|d��}tj| |d��d � W d � n1 sxw Y t�d
|� t�d|� t j
j�d�} | d
|d|g}
| r�d}z
tj|
dtjtjd�}W n ty� d}Y nw |jr�d}|r�t�d� nt�d|� t�d� t�t j
j� |j!�"� ��#� t j
j� |j$�"� ��#� � nt�d|
� W |s�| �%� | S |s�| �%� w w | S )N� )�size�.z.pstatsz.dotz.stats�w)�streamZ
cumulativezPROFILING: %s generatedz$PROFILING (cumulative): %s generatedZpyprof2calltreez-iz-oF)�shell�stdout�stderrTzPROFILING (dot problemzPROFILING (dot): %s generatedzpyprof2calltree output:z$You can run %s for additional stats.)&r �disable�os�path�isdir�makedirs�datetime�now� isoformat�salt�utilsZ hashutilsZrandom_hash�join�existsr �filesZfopen�pstatsZStatsZ
sort_statsr r �which�
subprocess�Popen�PIPEr �
returncoder ZtraceZstringutilsZto_strr# �read�stripr$ r )
r Z
stats_path�stopZid_�dateZficpZficoZficnZficZpyprof�cmdZfailedZpror r r �output_profile; sb
�
��
����
�r= )N)T)r FN)�__doc__r* r r&