3 JZ=C@sdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl m Z dd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZgZejdZejZe j e j!e j"e j#e j$e j%e j&e j'e j(e j)e j*e j+e j,e j-e j.e j/e j0e j1e j2e j3e j4e j5e j6e j7i Z8e j9e j:e j;e je j?e j@e jAe jBiZCeDdddddddddddd ZEeDddddddddd ZFed!d"d#d$d%gZGed&d'd(d)d*d+d,d-d.gZHed/d0d1d2d3gZIed4eIjJdaZKed6d7ZLed8d9d:jMeLjJZNd;d<ZOd=d>ZPd?d@ZQdAdBZRdCdDZSdEdFZTdGdHZUdIdJZVe jWZWe jXZXdbdLdMZYdNdOZZe j[Z[ej\Z\dcdQdRZ]dSdTZ^dUdVZ_dWdXZ`dYdZZae jbZbd[d\Zcejdd]d^ZeGd_d`d`efZgdS)dzOSX platform implementation.N)AF_INET) namedtuple)_common)_psposix) _psutil_osx) _psutil_posix)AF_INET6) conn_tmap) isfile_strict)memoize_when_activated)parse_environ_block)sockfam_to_enum)socktype_to_enum) usage_percent) AccessDenied) NoSuchProcess) ZombieProcess SC_PAGE_SIZE ) ppidruideuidsuidrgidegidsgidttynrctimestatusname)cpuutimecpustimerssvmspfaultspageins numthreadsvolctxsw scputimesusernicesystemidlesvmemtotalZ availablepercentusedfreeactiveinactivewiredpmemr+r,r-r.pfullmemuss pmmap_groupedz7path rss private swapped dirtied ref_count shadow_depth pmmap_extz addr perms  c CsNtj\}}}}}||}|||}t|||dd}t||||||||S)z&System virtual memory as a namedtuple.r)_round)cextZ virtual_memrr6)r7r;r<r=r:Zavailr9r8rF/usr/lib64/python3.6/_psosx.pyvirtual_memoryxs   rHcCs4tj\}}}}}t||dd}tj||||||S)z=Swap system memory as a (total, used, free, sin, sout) tuple.r)rD)rEZswap_memrrZsswap)r7r9r:ZsinZsoutr8rFrFrG swap_memorysrIcCstj\}}}}t||||S)z(Return system CPU times as a namedtuple.)rE cpu_timesr1)r2r3r4r5rFrFrGrJsrJcCs>g}x4tjD](}|\}}}}t||||}|j|qW|S)z(Return system CPU times as a named tuple)rE per_cpu_timesr1append)retZcpu_tr2r3r4r5itemrFrFrGrKs  rKcCstjS)z0Return the number of logical CPUs in the system.)rEcpu_count_logicalrFrFrFrGrOsrOcCstjS)z1Return the number of physical CPUs in the system.)rEZcpu_count_physrFrFrFrGcpu_count_physicalsrPcCs"tj\}}}}}tj||||S)N)rE cpu_statsrZ scpustats)Z ctx_switchesZ interruptsZsoft_interruptsZsyscallsZtrapsrFrFrGrQsrQcCstj\}}}tj|||gS)zReturn CPU frequency. On OSX per-cpu frequency is not supported. Also, the returned frequency never changes, see: https://arstechnica.com/civis/viewtopic.php?f=19&t=465002 )rEcpu_freqrZscpufreq)ZcurrZmin_Zmax_rFrFrGrRsrRFc Csrg}tj}x`|D]X}|\}}}}|dkr.d}|sPtjj| stjj| rPqtj||||}|j|qW|S)z8Return mounted disk partitions as a list of namedtuples.Znone) rEdisk_partitionsospathisabsexistsrZ sdiskpartrL) allretlistZ partitions partitionZdeviceZ mountpointZfstypeZoptsntuplerFrFrGrTs  rTc Csbytj\}}}Wntk r&dSX|dk}|rYntk rZ|jdYnX|S)Nr)rErbrc create_timerLrr)ZlsrFrFrGrb0s rbcstjfdd}|S)z`Decorator which translates bare OSError exceptions into NoSuchProcess and AccessDenied. csty|f||Stk rn}zB|jtjkr.wrapper) functoolswraps)rrrF)rrGwrap_exceptionsCs rccsy dVWnttfk r}zrt|ts6|jtjkry |j}Wntk rZ|YqX|tjkrzt |j |j |j qt |j |j nWYdd}~XnXdS)zThere are some poor C APIs which incorrectly raise ESRCH when the process is still alive or it's a zombie, or even RuntimeError (those who don't set errno). This is here in order to solve: https://github.com/giampaolo/psutil/issues/1044 N)rw RuntimeError isinstancerxryr'rr STATUS_ZOMBIErrgrz_ppidr)procr~r'rFrFrG catch_zombieTs    rc@sneZdZdZdddgZddZeddZed d Zd d Z d dZ e ddZ e ddZ e ddZe ddZe ddZe ddZe ddZe ddZe dd Ze d!d"Ze d#d$Ze d%d&Ze d'd(Ze d)d*Ze d+d,Ze d-d.Ze dAd0d1Ze d2d3Ze dBd5d6Ze d7d8Ze d9d:Z e d;d<Z!e d=d>Z"e d?d@Z#d4S)Crcz1Wrapper class around underlying C implementation.rgrzrcCs||_d|_d|_dS)N)rgrzr)r{rgrFrFrG__init__sszProcess.__init__cCstj|j}|S)N)rEZproc_kinfo_oneshotrg)r{rMrFrFrG_get_kinfo_procxs zProcess._get_kinfo_procc Cs$t|tj|j}WdQRX|S)N)rrEZproc_pidtaskinfo_oneshotrg)r{rMrFrFrG_get_pidtaskinfos zProcess._get_pidtaskinfocCs|jj|jjdS)N)rZcache_activater)r{rFrFrG oneshot_enters zProcess.oneshot_entercCs|jj|jjdS)N)rZcache_deactivater)r{rFrFrG oneshot_exits zProcess.oneshot_exitcCs(|jtd}|dk r|Stj|jS)Nr()rkinfo_proc_maprEZ proc_namerg)r{r(rFrFrGr(sz Process.namec Cs t|tj|jSQRXdS)N)rrEZproc_exerg)r{rFrFrGexes z Process.exec Cs t|tj|jSQRXdS)N)rrEZ proc_cmdlinerg)r{rFrFrGcmdlines zProcess.cmdlinec Cs$t|ttj|jSQRXdS)N)rr rEZ proc_environrg)r{rFrFrGenvirons zProcess.environcCs|jtd|_|jS)Nr)rrr)r{rFrFrGrsz Process.ppidc Cs t|tj|jSQRXdS)N)rrEZproc_cwdrg)r{rFrFrGcwds z Process.cwdcCs.|j}tj|td|td|tdS)Nrr r!)rrpuidsr)r{rawtuplerFrFrGuidss   z Process.uidscCs.|j}tj|td|td|tdS)Nr"r#r$)rrrr)r{rrFrFrGgidss   z Process.gidsc Cs:|jtd}tj}y||Stk r4dSXdS)Nr%)rrrZget_terminal_mapKeyError)r{Ztty_nrZtmaprFrFrGterminals zProcess.terminalcCs6|j}t|td|td|td|tdS)Nr+r,r-r.)rr>pidtaskinfo_map)r{rrFrFrG memory_infos    zProcess.memory_infocCs"|j}tj|j}t||fS)N)rrEZproc_memory_ussrgr?)r{Z basic_memr@rFrFrGmemory_full_infos zProcess.memory_full_infocCs(|j}tj|td|tdddS)Nr)r*g)rrZ pcputimesr)r{rrFrFrGrJs   zProcess.cpu_timescCs|jtdS)Nr&)rr)r{rFrFrGrvszProcess.create_timecCs|jtd}tj|dS)Nr0r)rrrZpctxsw)r{ZvolrFrFrGnum_ctx_switchesszProcess.num_ctx_switchescCs|jtdS)Nr/)rr)r{rFrFrG num_threadsszProcess.num_threadsc Csf|jdkrgSg}t|tj|j}WdQRXx.|D]&\}}t|r8tj||}|j|q8W|S)Nr)rgrrEZproc_open_filesr rZ popenfilerL)r{filesrtrVfdr\rFrFrG open_filess   zProcess.open_filesracCs|tkr(td|djddtDft|\}}t|tj|j||}WdQRXg}xz|D]r}|\}}} } } } t| } t|}t | } |t t fkr| rt j | } | rt j | } t j||| | | | } |j| qbW|S)Nz+invalid %r kind argument; choose between %sz, cSsg|] }t|qSrF)repr).0xrFrFrG sz'Process.connections..)r ValueErrorjoinrrEZproc_connectionsrg TCP_STATUSESrrrr rZaddrZpconnrL)r{rfZfamiliestypesrtrMrNrZfamtypeZladdrZraddrr'rurFrFrGrds(      zProcess.connectionsc Cs.|jdkrdSt|tj|jSQRXdS)Nr)rgrrEZ proc_num_fds)r{rFrFrGnum_fdss  zProcess.num_fdsNcCstj|j||jS)N)rZwait_pidrgrz)r{ZtimeoutrFrFrGwaitsz Process.waitc Cs t|tj|jSQRXdS)N)rrm getpriorityrg)r{rFrFrGnice_gets zProcess.nice_getc Cs"t|tj|j|SQRXdS)N)rrm setpriorityrg)r{valuerFrFrGnice_set#s zProcess.nice_setcCs|jtd}tj|dS)Nr'?)rr PROC_STATUSESget)r{coderFrFrGr'(szProcess.statusc CsTt|tj|j}WdQRXg}x*|D]"\}}}tj|||}|j|q*W|S)N)rrEZ proc_threadsrgrZpthreadrL)r{rtrZZ thread_idutimeZstimer\rFrFrGthreads.s zProcess.threadsc Cs t|tj|jSQRXdS)N)rrEZproc_memory_mapsrg)r{rFrFrG memory_maps8s zProcess.memory_maps)ra)N)$__name__ __module__ __qualname____doc__ __slots__rr rrrrrr(rrrrrrrrrrrJrvrrrrdrrrrr'rrrFrFrFrGrcnsB                       rc)r@)F)ra)hr contextlibrxrrUZsocketr collectionsrrSrrrrErrmr r r r r rrr _exceptionsrrrZ__extra__all__sysconfZPAGESIZEZAF_LINKZTCPS_ESTABLISHEDZCONN_ESTABLISHEDZ TCPS_SYN_SENTZ CONN_SYN_SENTZTCPS_SYN_RECEIVEDZ CONN_SYN_RECVZTCPS_FIN_WAIT_1ZCONN_FIN_WAIT1ZTCPS_FIN_WAIT_2ZCONN_FIN_WAIT2ZTCPS_TIME_WAITZCONN_TIME_WAITZ TCPS_CLOSEDZ CONN_CLOSEZTCPS_CLOSE_WAITZCONN_CLOSE_WAITZ TCPS_LAST_ACKZ CONN_LAST_ACKZ TCPS_LISTENZ CONN_LISTENZ TCPS_CLOSINGZ CONN_CLOSINGZPSUTIL_CONN_NONEZ CONN_NONErZSIDLZ STATUS_IDLEZSRUNZSTATUS_RUNNINGZSSLEEPZSTATUS_SLEEPINGZSSTOPZSTATUS_STOPPEDZSZOMBrrdictrrr1r6r>_fieldsr?rArrBrHrIrJrKrOrPrQrRZ disk_usageZdisk_io_countersrTr_rkZ net_if_addrsrirprqrsrbZ pid_existsrcontextmanagerrobjectrcrFrFrFrGs