U
    g                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 dZdZdeed  ZdZdZG d	d
 d
ZdS )    N)support)print_warningi       <      zDSOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguagec                   @   sR   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dd ZdS )WindowsLoadTrackerz
    This class asynchronously interacts with the `typeperf` command to read
    the system load on Windows. Multiprocessing and threads can't be used
    here because they interfere with the test suite's cases for those
    modules.
    c                 C   s$   g | _ d | _d| _d | _|   d S )N )_values_load_buffer_popenstartself r   0/usr/lib/python3.8/test/libregrtest/win_utils.py__init__$   s
    zWindowsLoadTracker.__init__c              
   C   s   dt t  }tj}|tjO }|tjO }t||tjdt	t	tj
tj| _t|tjdtjtjdtj}t|d}tj| jdd}|d |  }d|dt tg}tjd||tjd	| _t| d S )
Nz\\.\pipe\typeperf_output_r   r   T)
overlappedZtypeperfz-si )stdoutcwd)struuidZuuid4_winapiZPIPE_ACCESS_INBOUNDZFILE_FLAG_FIRST_PIPE_INSTANCEZFILE_FLAG_OVERLAPPEDZCreateNamedPipeZ	PIPE_WAITBUFSIZEZNMPWAIT_WAIT_FOREVERZNULLpipeZ
CreateFileZGENERIC_WRITEZOPEN_EXISTINGmsvcrtZopen_osfhandleZConnectNamedPipeGetOverlappedResult_get_counter_nameSAMPLING_INTERVAL
subprocessPopenjoinr   ZSAVEDCWDr   osclose)r   Z	pipe_nameZ	open_modeZpipe_write_endZcommand_stdoutZoverlapZcounter_nameZcommandr   r   r   r   +   s<    

           
zWindowsLoadTracker.startc              	   C   sb   t t jt}t |dd }W 5 Q R X t|}tt||}|d }|d }d| d| dS )NCounterr   2Z44z"\\")winregOpenKeyHKEY_LOCAL_MACHINECOUNTER_REGISTRY_KEYZQueryValueExiterdictzip)r   ZperfkeyZcountersZcounters_dictsystemZprocess_queue_lengthr   r   r   r   M   s    z$WindowsLoadTracker._get_counter_nameTc                 C   s6   | j d krd S d | _|r"| j   | j   d | _ d S N)r   r
   killwait)r   r2   r   r   r   r$   \   s    


zWindowsLoadTracker.closec                 C   s   |    d S r1   )r$   r   r   r   r   __del__g   s    zWindowsLoadTracker.__del__c                 C   sN   | d}t|dkrt|d }|dr6|ds:t|dd }t|S )N,   r   r(   )splitlen
ValueError
startswithendswithfloat)r   linetokensvaluer   r   r   _parse_linej   s    
zWindowsLoadTracker._parse_linec                 C   s   t | jtd\}}|d\}}|dkr.dS | }|dd}| j| }|d}z| 	|d  W n  t
k
r   |d| _Y nX d| _|S )	NTFr   r   Zoemreplacer7   r   )r   ZReadFiler   r   r   	getbufferdecoder   
splitlinesrA   r:   pop)r   r   _Z
bytes_readresoutputlinesr   r   r   _read_linesx   s    

zWindowsLoadTracker._read_linesc              	   C   s  | j d krd S | j  }|d k	r0| jdd d S z|  }W n tk
rZ   |   Y d S X |D ]}| }d|krvq`|s|q`z| |}W n$ tk
r   td|  Y q`Y nX | j	d k	r| j	t
 |dt
   | _	q`t| jtk r| j| q`t| jt| j | _	q`| j	S )NF)r2   zPDH-CSVz#Failed to parse typeperf output: %ag      ?)r   Zpollr$   rK   BrokenPipeErrorrstriprA   r:   r   r
   LOAD_FACTOR_1r9   r	   NVALUEappendsum)r   
returncoderJ   r>   Zprocessor_queue_lengthr   r   r   
getloadavg   s<    




zWindowsLoadTracker.getloadavgN)T)__name__
__module____qualname____doc__r   r   r   r$   r4   rA   rK   rS   r   r   r   r   r      s   "
r   )r   Zmathr   r#   r    r   r)   Ztestr   Ztest.libregrtest.utilsr   r   r   ZexprN   rO   r,   r   r   r   r   r   <module>   s   