B
    u9a                 @   sV   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ZdZdZ	dZ
G dd dZdS )	    N)supporti    gi q?   z\System\Processor Queue Lengthc               @   s@   e Zd ZdZdd Z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. Mulitprocessing and threads can't be used
    here because they interfere with the test suite's cases for those
    modules.
    c             C   s   d| _ |   d S )Ng        )loadstart)self r   /usr/lib/python3.7/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tdt tg}tj||tjd| _t| d S )	Nz\\.\pipe\typeperf_output_   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COUNTER_NAMESAMPLING_INTERVAL
subprocessPopenr   ZSAVEDCWDposclose)r   Z	pipe_nameZ	open_modeZpipe_write_endZcommand_stdoutZoverlapZcommandr   r   r	   r      s     


zWindowsLoadTracker.startc             C   s,   | j d krd S | j   | j   d | _ d S )N)r   killwait)r   r   r   r	   r   ?   s
    


zWindowsLoadTracker.closec             C   s   |    d S )N)r   )r   r   r   r	   __del__F   s    zWindowsLoadTracker.__del__c             C   sB   dd l }|| jtd\}}|d\}}|dkr6d S |  S )Nr   TF)r   ZReadFiler   r   r   	getbufferdecode)r   r   r   _Z
bytes_readZresr   r   r	   read_outputI   s    zWindowsLoadTracker.read_outputc             C   s   |   }|s| jS xl| D ]`}|d}| dkst|kst|dkrLqt|d dd}| jt	 |dt	   }|| _qW | jS )N,    r   "g      ?)
r#   r   
splitlinessplitstripr   lenfloatreplaceLOAD_FACTOR_1)r   Ztypeperf_outputlineZtoksr   Znew_loadr   r   r	   
getloadavgS   s    
 
zWindowsLoadTracker.getloadavgN)
__name__
__module____qualname____doc__r
   r   r   r   r#   r0   r   r   r   r	   r      s   !
r   )r   r   r   r   r   Ztestr   r   r.   r   r   r   r   r   r   r	   <module>   s   