
    w&b&>                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	 \
" \	S5      (       d  S SKJr  \\	l        \R                  R                  S5      r S SKJr  \" 5       rSr\(       a  \(       d@  \" S	5        \" S
5        \(       d  \" S5        \(       d  \" S5        \R.                  " S5        S SKJrJr  S SKJrJrJrJr  S SK J!r!J"r"   " S S\#5      r$ " S S\$5      r%Sr&Sr'Sr(Sr)Sr* " S S\#5      r+S r,S r-S r. " S S\$5      r/ " S S\$5      r0 " S  S!\$5      r1S" r2g! \\4 a    \	R                  \	l         GN
f = f! \ a    Sr Nf = f)#    )print_functionN	monotonic)r   z/proc/self/io)VmStatTFz<Could not run iotop as some of the requirements are not met:z- Linux >= 2.6.20 withz~  - I/O accounting support (CONFIG_TASKSTATS, CONFIG_TASK_DELAY_ACCT, CONFIG_TASK_IO_ACCOUNTING, kernel.task_delayacct sysctl)z0  - VM event counters (CONFIG_VM_EVENT_COUNTERS)   )iopriovmstat)
ConnectionNETLINK_GENERICU32AttrNLM_F_REQUEST)
ControllerGeNlMessagec                       \ rS rSrSrS rSrg)DumpableObjectI   zBBase class for objects that allows easy introspection when printedc                 |    [        [        U 5      5      S S < S[        R                  " U R                  5      < S3$ )Nz: >)strtypepprintpformat__dict__selfs    ,/usr/lib/python3/dist-packages/iotop/data.py__repr__DumpableObject.__repr__K   s.    T
OCR0"NN4==9; 	;     N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r    r   r   r   r   I   s
    L;r   r   c                   L    \ rS rSr/ SQrSrS rSS jrS rS r	\
S 5       rS	rg
)StatsT   ))blkio_delay_total(   )swapin_delay_total8   )
read_bytes   )write_bytes   )cancelled_write_bytesi  Fc                     U R                   n[        R                   H(  u  p4XUS-    n[        R                  " SU5      S   X#'   M*     [        R
                  (       d  U R                  S:g  [        l        g g )N   Qr   )r   r(   members_offsetsstructunpackhas_blkio_delay_totalr*   )r   task_stats_buffersdnameoffsetdatas         r   __init__Stats.__init___   se    ]]!11LD$FQJ7D}}S$/2BH 2 ***.*@*@A*EE' +r   c                     UR                   nU R                   nUR                   n[        R                   H  u  pxXW   X6U   -  -   XG'   M     g)z3Update destination from operator(self, other_stats)Nr   r(   r6   )	r   other_statsdestinationcoeffddr;   odmemberr=   s	            r   
accumulateStats.accumulatej   sH    !!]]!!#33NFej&88BJ 4r   c                 "    U R                  XSS9$ )z*Update destination with self - other_statsr   )rE   )rI   )r   rC   rD   s      r   deltaStats.deltar   s    {rBBr   c                 b    U R                   n[        R                   H  u  p#X   S:w  d  M    g   g)Nr   FTrB   )r   r;   r<   r=   s       r   is_all_zeroStats.is_all_zerov   s.    ]]!11LDx1} 2 r   c                      [         R                  [         5      n U R                  n[         R                   H	  u  p#SX'   M     U $ )Nr   )r(   __new__r   r6   )statsstdr<   r=   s       r   build_all_zeroStats.build_all_zero}   s7    e$nn!11LDCI 2r   r    N)r   )r!   r"   r#   r$   r6   r9   r?   rI   rL   rO   staticmethodrU   r&   r    r   r   r(   r(   T   s;    O "	F9C  r   r(         c                   &    \ rS rSrS rS rS rSrg)TaskStatsNetlink   c                     Xl         [        [        5      U l        [	        U R                  5      nUR                  S5      U l        g )N	TASKSTATS)optionsr	   r
   
connectionr   get_family_id	family_id)r   r_   
controllers      r   r?   TaskStatsNetlink.__init__   s3    $_50
#11+>r   c                 \    [        U R                  [        [        [        U5      /[
        S9$ )N)cmdattrsflags)r   rb   TASKSTATS_CMD_GETr   TASKSTATS_CMD_ATTR_PIDr   )r   tids     r   build_requestTaskStatsNetlink.build_request   s)    4>>/@")*@#"F!G!.0 	0r   c                     UR                   R                  U R                  5         [        R                  " U R                  5      nUR                  R                  5        H!  u  pEU[        :X  d  M  UR                  5       n  O   g U[           R                  n[        U5      S:  a  g [        R                   " SUS S 5      S   nUS:  d   e[#        U5      $ ! [
         a)  nUR                  [        R                  :X  a   S nAg e S nAff = f)Ni  H   r   rX   )task_stats_requestsendr`   r   recvOSErrorerrnoESRCHrg   itemsTASKSTATS_TYPE_AGGR_PIDnestedTASKSTATS_TYPE_STATSr>   lenr7   r8   r(   )r   threadreplye	attr_type
attr_valuetaskstats_datataskstats_versions           r   get_single_task_stats&TaskStatsNetlink.get_single_task_stats   s    !!&&t7	$$T__5E &+[[%6%6%8!I33"))+ &9
 3499~$"MM#~bq/AB1E A%%%^$$#  	ww%++%		s    C 
D$DDD)r`   rb   r_   N)r!   r"   r#   r$   r?   rl   r   r&   r    r   r   r[   r[      s    ?0
%r   r[   c           	         / U l         SnU R                  =(       d    /  H3  n [        U5      nU(       a  M  U R                   R                  W5        M5     U(       a  [        R                  " S5        gg! [         aR     [        R
                  " U5      nUR                  n Nv! [         a    [        SU[        R                  S9  Sn  Nf = ff = f)zDBuild options.uids from options.users by resolving usernames to UIDsFzUnknown user:)fileTr   N)uidsusersint
ValueErrorpwdgetpwnampw_uidKeyErrorprintsysstderrappendexit)r_   erroruuidpasswds        r   	find_uidsr      s    GLE]] b 		$a&C uLL$ !    	$$a
 mm	  oqszz:	$s/   A55
C B$C$%C	CCCc                     0 n [        SU -  SS9 H*  n UR                  SS5      u  p4UR                  5       X'   M,     U$ ! [         a     M=  f = f! [         a     U$ f = f)Nz/proc/%d/statusreplace)errors:r   )opensplitr   stripIOError)pidresult_dictlinekeyvalues        r   parse_proc_pid_statusr      s    K*S0CD!ZZQ/
  %{{}K D    	  s3   A AA 
AA AA 
A A c                      U R                  S5      $ ! [         a    U R                  S5      s $ [         a    U s $ f = f)Nzutf-8string_escape)decodeUnicodeDecodeErrorencodeAttributeError)ss    r   safe_utf8_decoder      sA    xx   )xx(( s    >>>c                   0    \ rS rSrSrS rS rS rS rSr	g)	
ThreadInfo   zStats for a single threadc                     Xl         SU l        S U l        [        R	                  [        5      U l        UR                  U5      U l        g NT)rk   markstats_totalr(   rR   stats_deltarl   rq   )r   rk   taskstats_connections      r   r?   ThreadInfo.__init__   s:    	 ==/"6"D"DS"Ir   c                 B    [         R                  " U R                  5      $ N)r   getrk   r   s    r   
get_ioprioThreadInfo.get_ioprio   s    zz$((##r   c                 b    [         R                  " [         R                  U R                  X5      $ r   )r   
set_ioprioIOPRIO_WHO_PROCESSrk   )r   ioprio_classioprio_datas      r   r   ThreadInfo.set_ioprio   s&      !:!:DHH!-< 	<r   c                     U R                   (       d  Xl         UR                  U R                   U R                  5        Xl         g r   )r   rL   r   )r   rS   s     r   update_statsThreadInfo.update_stats   s1    $D$$d&6&67 r   )r   r   r   rq   rk   N)
r!   r"   r#   r$   r%   r?   r   r   r   r&   r    r   r   r   r      s    #J$<!r   r   c                   Z    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rSrg)ProcessInfor1   zStats for a single process (a single line in the output): if
options.processes is set, it is a collection of threads, otherwise a single
thread.c                     Xl         S U l        S U l        0 U l        [        R                  5       U l        [        R                  5       U l        [        R                  " 5       U l
        g r   )r   r   userthreadsr(   rU   r   stats_accumtimer   stats_accum_timestamp)r   r   s     r   r?   ProcessInfo.__init__  sM    	 //1 //1%)^^%5"r   c                     UR                   (       a,  UR                  (       d  U R                  UR                   ;  a  gUR                  (       a  U R	                  5       UR                  ;  a  ggNFT)pids	processesr   r   get_uid)r   r_   s     r   is_monitoredProcessInfo.is_monitored  sB    LL!2!2HHGLL(<<DLLN',,>r   c                    U R                   (       a  U R                   $  [        R                  " SU R                  -  5      [        R                     nXR                   :w  a  S U l        Xl         U$ ! [
         a    S n N,f = f)Nz/proc/%d)r   osstatr   ST_UIDrt   r   r   r   s     r   r   ProcessInfo.get_uid  si    8888O	''*txx/0=C ((?DIH
  	C	s   4A1 1B ?B c                    U R                  5       nUb@  U R                  (       d/   [        [        R                  " U5      R
                  5      U l        U R                  =(       d    S$ ! [        [        4 a    [        U5      U l         N7f = f)Nz{none})	r   r   r   r   getpwuidpw_namer   r   r   r   s     r   get_userProcessInfo.get_user*  sj    lln?499%,S\\#->-F-FG	 yy$H$ n- %H	%s   .A) ) BBc                     [        SU R                  -  5      nUR                  S5      n[	        U R                  5      nU(       d$  UR                  SS5      nU(       a  SU-  nU$ SnU$ Sn[        UR                  SU R                  5      5      nX`R                  :w  a:  [	        U5      R                  SS5      nUR                  SS5      nX:w  a  US	U-  -  nUR                  S
5      n	U	S   R                  S5      (       a"  U	S   R                  S5      S-   n
U	S   U
S  U	S'   SR                  U	5      R                  5       n[        X%-   5      $ ! [         a     gf = f)Nz/proc/%d/cmdlinei   z{no such process}Name z[%s]z	{no name}Tgidz [%s] r   /r    )r   r   readr   r   r   r   r   
startswithrfindjoinr   r   )r   proc_cmdlinecmdlineproc_statusproc_status_namesuffixtgid	tgid_namethread_namepartsfirst_command_chars              r   get_cmdlineProcessInfo.get_cmdline3  sa   	' 2TXX =>L"''-G ,DHH5*vr:#),<#<  $# $/ ##;??64884588-d377CI%//&"5K''K//d#8s##!&q!4q!8Qx 2 34E!H((5/'') 0111  	'&	's   )E 
E E c                     U(       a  U R                   R                  5       (       + $ U R                  R                  5        H$  nUR                  R                  5       (       a  M$    g   g)NTF)r   rO   r   valuesr   )r   accumulatedts      r   did_some_ioProcessInfo.did_some_ioR  sO    ''33555$$&A==,,.. ' r   c                     [        S U R                  R                  5        5       5      n[        U5      S:X  a  UR	                  5       $ g)Nc              3   @   #    U  H  oR                  5       v   M     g 7fr   )r   ).0r   s     r   	<genexpr>)ProcessInfo.get_ioprio.<locals>.<genexpr>[  s     G1FA1Fs   r   z?dif)setr   r   r{   pop)r   
prioritiess     r   r   ProcessInfo.get_ioprioZ  s;    G1D1D1FGG
z?a>>##r   c                 h    U R                   R                  5        H  nUR                  X5        M     g r   )r   r   r   )r   r   r   r|   s       r   r   ProcessInfo.set_ioprio`  s'    ll))+Fl8 ,r   c                 J    [         R                  " U R                  5       5      $ r   )r   sort_keyr   r   s    r   ioprio_sort_keyProcessInfo.ioprio_sort_keyd  s    t011r   c                 ~    U R                   R                  US 5      nU(       d  [        X5      nX0R                   U'   U$ r   )r   r   r   )r   rk   r   r|   s       r   
get_threadProcessInfo.get_threadg  s6    !!#t,:F &LLr   c                 x   [         R                  5       nU R                  R                  5        H4  u  p#UR                  (       a  M  UR                  UR                  U5        M6     [        U R                  R                  5        VVs/ s H  u  p#UR                  (       a  M  X#4PM     snn5      U l        [        U R                  5      nU(       d  gU=R                  U-  sl	        U=R                  U-  sl
        Xl        U R                  R                  U R                  U R                  5        gs  snnf r   )r(   rU   r   rw   r   rI   r   dictr{   r*   r,   r   )r   r   rk   r|   
nr_threadss        r   r   ProcessInfo.update_statsn  s    **,<<--/KC;;;&&v'9'9;G 0 !\\//1F1 0;s9? +c]1F G &
%%3%&&*4&&##D$4$4d6F6FGFs   	D6
$D6
)r   r   r   r   r   r   r   N)r!   r"   r#   r$   r%   r?   r   r   r   r   r   r   r   r  r  r   r&   r    r   r   r   r      s>    6
"%2>92r   r   c                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)ProcessListi  c                     0 U l         Xl        X l        [        R                  " 5       U l        [        R                  " 5       U l        U R                  5         g r   )	r   r   r_   r   r   	timestampr   r   update_process_counts)r   r   r_   s      r   r?   ProcessList.__init__  s=    $8!)mmo 	""$r   c                     U R                   R                  US5      nU(       d  [        U5      nX R                   U'   UR                  U R                  5      (       a  U$ g)zqEither get the specified PID from self.processes or build a new
ProcessInfo if we see this PID for the first timeN)r   r   r   r   r_   )r   r   processs      r   get_processProcessList.get_process  sR     ..$$S$/!#&G")NN3--N .r   c           
         U R                   R                  (       a  U R                   R                  $ [        R                  " S5      nU R                   R                  (       a2  U Vs/ s H$  nSUS   s=::  a  S::  d  M  O  M  [        U5      PM&     sn$ / nU HS  nSUS   s=::  a  S::  d  M  O  M   UR                  [        [
        [        R                  " SU-   S-   5      5      5        MU     U$ s  snf ! [         a     Mk  f = f)Nz/proc0r   9z/proc/z/task)	r_   r   r   listdirr   r   extendmaprt   )r   tgidsr   tidss       r   
list_tgidsProcessList.list_tgids  s    <<<<$$$

7#<<!!*/I%$3$q'3HS3HI3HICI%IIDd1g$$$KKC$&JJx$/H$I!K L   J  s$   'C2?C2C239C77
DDc           	      ~   U R                   R                  (       d  U/$  [        [        [        [
        R                  " SU-  5      5      5      nU R                   R                  (       a@  [        [        U R                   R                  5      R                  [        U5      5      5      nU$ ! [         a    / s $ f = f)Nz/proc/%d/task)r_   r   listr$  r   r   r"  rt   r   r  intersection)r   r   r&  s      r   	list_tidsProcessList.list_tids  s    ||%%6M	COd,B!CDED <<DLL--.;;CIFGD  	I	s   0B- -B<;B<c                 .   [         R                  " 5       nXR                  -
  U l        Xl        S=p#U R	                  5        H  nU R                  U5      nU(       d  M  U R                  U5       H  nUR                  X`R                  5      nU R                  R                  U5      nU(       d  MB  UR                  U5        UR                  n	X)R                  -  nX9R                  -  nSUl        M     M     X#4U R                  R!                  5       4$ )Nr   F)r   r   r  durationr'  r  r,  r  r   r   r   r   r.   r0   r   r   rL   )
r   new_timestamp
total_readtotal_writer   r  rk   r|   rS   rL   s
             r   r  !ProcessList.update_process_counts  s    (%6&#$$
OO%D&&t,G~~d+ ++C1J1JK11GGO5''."..E"2"22J#4#44K"'FK ,	 & ($++*;*;*===r   c                 n   U R                   R                  5        H+  nUR                  R                  5        H
  nSUl        M     M-     U R	                  5       n[        U R                   R                  5        VVs/ s H  u  pAUR                  5       (       d  M  XA4PM!     snn5      U l         U$ s  snnf r   )r   r   r   r   r  r  rw   r   )r   r  r|   total_read_and_writer   s        r   refresh_processesProcessList.refresh_processes  s    ~~,,.G!//002" 3 /  $99;#~~33575 3?#&335  .~57 8 $#	7s   <B1
B1
c                     0 U l         g r   )r   r   s    r   clearProcessList.clear  s	    r   )r/  r_   r   r   r  r   N)r!   r"   r#   r$   r?   r  r'  r,  r  r6  r9  r&   r    r   r   r  r    s%    	%	&>,$r   r  c                       [        S5       n [        [        U R                  5       R	                  5       5      5      sS S S 5        $ ! , (       d  f       g = f! [
         a     g f = f)Nz/proc/sys/kernel/task_delayacct)r   boolr   r   r   FileNotFoundError)fs    r   sysctl_task_delayacctr?    sI    34AFFHNN,-. 544 s-   A 0A	A 
AA A 
A%$A%)3
__future__r   ru   r   r   r   r   r7   r   r   hasattrr   ImportErrorRuntimeErrorpathexistsioaccountingiotop.vmstatr   vmstat_fvm_event_countersr   r   r   iotopr   r   iotop.netlinkr	   r
   r   r   iotop.genetlinkr   r   objectr   r(   ri   rj   rx   TASKSTATS_TYPE_PIDrz   r[   r   r   r   r   r   r  r?  r    r   r   <module>rO     st  ( &  	  
   
 4%%'" ww~~o.#xH ,	
HI	
"# E 	F @AHHQK   M M 3;V ;/N /j      #%v #%T*"! !.@. @F\. \~w 	\" YYDN  s#   D1 E 1EEEE