a M_@sdZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddl m Z ddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%zddl mZ&Wnle'yZ(zRe)e(*+dre,dd krd!Z-e-d"7Z-e-d#7Z-e.e-nWYdZ([(n dZ([(00ej/d$krddl0Z0ndZ0gd%Z1d&Z2d'Z3d(ej4vZ5e0dur6d)Z6ne07d*d+d)iZ8e8j6Z6e&j9e j:e&j;e je&j?e j@e&jAe jBe&jCe jDe&jEe jFe&jGe jHe&jIe jJe&jKe jLe&jMe jNe&jOe2e&jPe jQi ZRe0durGd,d-d-e0j7ZSeTUeSjVe0durdZWdZXd.ZYd/ZZn Gd0d1d1e0j7Z[eTUe[jVe\ddd.d/d2d3d d4d5d6d7d8d9d:d;dd?d@dAdBdCZ]e dDgdEZ^e dFgdGZ_e dHgdIZ`e dJe`jadKZbe dLdMdNgZce dOdPdQdecjaZee dRgdSZfedTdUdVdWZgdXdYZhedZd[Zid\d]Zjd^d_Zke&jlZld`daZmdbdcZndddeZodfdgZpdhdiZqdjdkZrdldmZsdndoZtdpaudqdrZvddsdtZwdudvZxdwdxZydydzZzd{d|Z{da|d}d~Z}ddZ~ddZddZGdddeZe&jZe&jZe&jZddZdddZddZddZGdddeZdS)z Windows platform implementation.N) namedtuple)_common) AccessDenied) conn_tmap)conn_to_ntuple)debug)ENCODING) ENCODING_ERRS) isfile_strict)memoize)memoize_when_activated) NoSuchProcess)parse_environ_block)TimeoutExpired) usage_percent)long) lru_cache)PY3range)unicode)ABOVE_NORMAL_PRIORITY_CLASS)BELOW_NORMAL_PRIORITY_CLASS)HIGH_PRIORITY_CLASS)IDLE_PRIORITY_CLASS)NORMAL_PRIORITY_CLASS)REALTIME_PRIORITY_CLASS)_psutil_windowszdll load failedz3this Windows version is too old (< Windows Vista); z:psutil 3.4.2 is the latest version which supports Windows z2000, XP and 2003 server))win_service_iterwin_service_getrrrrrrIOPRIO_VERYLOW IOPRIO_LOW IOPRIO_NORMAL IOPRIO_HIGHCONN_DELETE_TCBAF_LINKZ DELETE_TCBi+Z__pypy__ AddressFamilyr)c@s$eZdZeZeZeZeZeZeZdS)PriorityN) __name__ __module__ __qualname__rrrrrrr0r07/usr/lib64/python3.9/site-packages/psutil/_pswindows.pyr,ms r,r c@seZdZdZdZdZdZdS) IOPriorityrrr2r N)r-r.r/r$r%r&r'r0r0r0r1r3}sr3r! ) num_handles ctx_switches user_time kernel_time create_time num_threads io_rcount io_wcount io_rbytes io_wbytesio_count_othersio_bytes_othersnum_page_faults peak_wsetwsetpeak_paged_pool paged_poolpeak_non_paged_poolnon_paged_poolpagefile peak_pagefile mem_private scputimes)usersystemidle interruptdpcsvmem)totalZ availablepercentusedfreepmem) rssvmsrPrQrRrSrTZpeak_nonpaged_poolZ nonpaged_poolrWrXZprivatepfullmem)uss pmmap_groupedpathrf pmmap_extz addr perms  pio)Z read_countZ write_count read_bytes write_bytes other_countZ other_bytesi)maxsizecCs@d|ddd}t|}|t|d}tj||S)zConvert paths using native DOS format like: "\Device\HarddiskVolume1\Windows\systemew\file.txt" into: "C:\Windows\systemew\file.txt" \Nr )joinsplitcextZwin32_QueryDosDevicelenosrk)sZrawdriveZ driveletterZ remainderr0r0r1convert_dos_paths rzcCs&tr|St|tr|S|ttSdS)zmEncode a unicode string to a byte string by using the default fs encoding + "replace" error handler. N)r isinstancestrencoder r )ryr0r0r1 py2_strencodes  r~cCstSN)rv getpagesizer0r0r0r1rsrc CsNt}|\}}}}}}|}|}|} ||} t|||dd} t||| | | S)z&System virtual memory as a namedtuple.rZround_)rv virtual_memrr`) memZtotphysZ availphysZtotpagefZ availpagefZtotvirtZfreevirtraZavailrdrcrbr0r0r1virtual_memorysrcCsBt}|d}|d}||}t||dd}t||||ddS)z=Swap system memory as a (total, used, free, sin, sout) tuple.r2r rrr)rvrrrZsswap)rrardrcrbr0r0r1 swap_memorys rcCsPtrt|tr|jtdd}t|\}}||}t||dd}t ||||S)z'Return disk usage associated with path.strict)errorsrr) rr{bytesdecoder rv disk_usagerrZ sdiskusage)rkrardrcrbr0r0r1rs rcCst|}dd|DS)zReturn disk partitions.cSsg|]}tj|qSr0)rZ sdiskpart.0xr0r0r1 z#disk_partitions..)rvdisk_partitions)allrawlistr0r0r1rs rcCs<t\}}}tddttD}t||||j|jS)z)Return system CPU times as a named tuple.cSsg|] }t|qSr0)sum)rnr0r0r1r rzcpu_times..)rv cpu_timesrZzip per_cpu_timesr^r_)r[r\r]Z percpu_summedr0r0r1rs rcCs:g}tD](\}}}}}t|||||}||q |S)z6Return system per-CPU times as a list of named tuples.)rvrrZappend)retr[r\r]r^r_itemr0r0r1r%s  rcCstS)z0Return the number of logical CPUs in the system.)rvcpu_count_logicalr0r0r0r1r.srcCstS)z6Return the number of physical CPU cores in the system.)rvZcpu_count_physr0r0r0r1cpu_count_physical3srcCs$t\}}}}d}t||||S)zReturn CPU statistics.r)rv cpu_statsrZ scpustats)rEZ interruptsZdpcsZsyscallsZsoft_interruptsr0r0r1r8s  rcCs(t\}}d}tt||t|gS)zMReturn CPU frequency. On Windows per-cpu frequency is not supported. )rvcpu_freqrZscpufreqfloat)ZcurrZmax_Zmin_r0r0r1r@s rFcCs*tstdat}tdd|DS)z~Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes respectively as a tupleTcSsg|]}t|dqS)r2)round)rloadr0r0r1rWrzgetloadavg..)_loadavg_inititializedrvZinit_loadavg_counter getloadavgtuple)Z raw_loadsr0r0r1rLs rc Cs|tvr(td|dddtDft|\}}t|||}t}|D]D}|\}}} } } } } t||| | | | t|dkr~| ndd}||qLt |S)zReturn socket connections. If pid == -1 return system-wide connections (as opposed to connections opened by one process only). z+invalid %r kind argument; choose between %sz, cSsg|] }t|qSr0)reprrr0r0r1rerz#net_connections..r*N)pid) r ValueErrorrtrvnet_connectionssetr TCP_STATUSESaddlist)kind_pidZfamiliestypesrrrfdZfamtypeZladdrZraddrstatusrntr0r0r1r_s  rcCsxi}t}|D]^\}}ts>t|ts6Jt|t|}|\}}}}tt dr^t |}t ||||||<q|S)z)Get NIC stats (isup, duplex, speed, mtu). NicDuplex) rv net_if_statsitemsrr{rrr~hasattrrrZ snicstats)rZrawdictnamerZisupZduplexZspeedZmtur0r0r1rqs   rcCst}tdd|DS)zsReturn network I/O statistics for every network interface installed on the system as a dict of raw tuples. cSsg|]\}}t||fqSr0r~)rkvr0r0r1rrz#net_io_counters..)rvnet_io_countersdictrrr0r0r1rsrcCs8g}tD]&}t|}t|d|d<||q |S)z,Return the addresses associated to each NIC.r)rv net_if_addrsrr~r)rrr0r0r1rs   rcCsdt\}}}}|dk}t|d@}t|d@}|r8dS|s@|rHtj}n|dkrVtj}t|||S)zReturn battery information.rr6Nr*)rvsensors_batteryboolrZPOWER_TIME_UNLIMITEDZPOWER_TIME_UNKNOWNZsbattery)Z acline_statusflagsrbZsecsleftZ power_pluggedZ no_batteryZchargingr0r0r1rs  rcCs,tt}t|tdkr tS|a|SdS)z:The system boot time expressed in seconds since the epoch.rN)rrv boot_timeabs _last_btimerr0r0r1rs  rcCsHg}t}|D]2}|\}}}t|}t|d||d}||q|S)z:Return currently connected users as a list of namedtuples.N)rvusersr~rZsuserr)retlistrrr[hostnameZtstamprr0r0r1rs  rccs*tD]\}}tt|t|VqdS)z*Yields a list of WindowsService instances.N)rvZwinservice_enumerateWindowsServicer~)r display_namer0r0r1r"sr"cCst|d}|d|_|S)zBOpen a Windows service and return it as a WindowsService instance.Nr)r _query_config _display_name)rZservicer0r0r1r#s r#c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ e j ddZ ddZddZddZddZddZddZddZd d!Zd"d#Zd$S)%rz(Represents an installed Windows service.cCs||_||_dSr)_namer)selfrrr0r0r1__init__szWindowsService.__init__cCs d|j|jf}d|jj|fS)Nz(name=%r, display_name=%r)z%s%s)rr __class__r-)rZdetailsr0r0r1__str__szWindowsService.__str__cCsd|t|fS)Nz <%s at %s>)ridrr0r0r1__repr__szWindowsService.__repr__cCst|tstS|j|jkSr)r{rNotImplementedrrotherr0r0r1__eq__s zWindowsService.__eq__cCs ||k Srr0rr0r0r1__ne__szWindowsService.__ne__cCs\|$t|j\}}}}Wdn1s20Ytt|t|t|t|dS)N)rbinpathusername start_type)_wrap_exceptionsrvZwinservice_query_configrrr~)rrrrrr0r0r1rs  (zWindowsService._query_configcCsP| t|j\}}Wdn1s.0Y|dkrDd}t||dS)Nr)rr)rrvZwinservice_query_statusrr)rrrr0r0r1 _query_statuss  .zWindowsService._query_statusc csz dVWnptyz}zXt|r:td|jd|jdn,|jtjtjfvrdtd|jd|jdnWYd}~n d}~00dS)z{Ctx manager which translates bare OSError and WindowsError exceptions into NoSuchProcess and AccessDenied. Nz2service %r is not querable (not enough privileges)rrmsgzservice %r does not exist)) OSErroris_permission_errrrwinerrorrvZERROR_INVALID_NAMEZERROR_SERVICE_DOES_NOT_EXISTrrerrr0r0r1r s$ zWindowsService._wrap_exceptionscCs|jS)zThe service name. This string is how a service is referenced and can be passed to win_service_get() to get a new WindowsService instance. )rrr0r0r1r!szWindowsService.namecCs|jS)z_The service display name. The value is cached when this class is instantiated. )rrr0r0r1r(szWindowsService.display_namecCs |dS)zwThe fully qualified path to the service binary/exe file as a string, including command line arguments. rrrr0r0r1r.szWindowsService.binpathcCs |dS)z,The name of the user that owns this service.rrrr0r0r1r4szWindowsService.usernamecCs |dS)zRA string which can either be "automatic", "manual" or "disabled". rrrr0r0r1r8szWindowsService.start_typecCs |dS)zzThe process PID, if any, else None. This can be passed to Process class to control the service's process. rrrr0r0r1r@szWindowsService.pidcCs |dS)zService status as a string.rrrr0r0r1rFszWindowsService.statuscCstt|S)zService long description.)r~rvZwinservice_query_descrrrr0r0r1 descriptionJszWindowsService.descriptioncCs>|}||||d<||d<||d<|S)zUUtility method retrieving all the information above as a dictionary. rrr)rupdaterrrr)rdr0r0r1as_dictPs    zWindowsService.as_dictN)r-r.r/__doc__rrrrrrr contextlibcontextmanagerrrrrrrrrrrr0r0r0r1rs&  rcCs<t|tsJ||jtjtjfvp:t|ddtjtjfvS)z*Return True if this is a permission error.rr*) r{rerrnoEPERMZEACCESgetattrrvZERROR_ACCESS_DENIEDZERROR_PRIVILEGE_NOT_HELD)excr0r0r1rs rcCsFt|tsJ|t|r&t||dS|jtjkr>t||dS|dS)z3Convert OSError into NoSuchProcess or AccessDenied.rrN)r{rrrrZESRCHr)rrrr0r0r1convert_oserrors    rcstfdd}|S)zDDecorator which converts OSError into NoSuchProcess or AccessDenied.c sTz|g|Ri|WStyN}zt||j|jdWYd}~n d}~00dS)Nr)rrrr)rargskwargsrfunr0r1wrappersz wrap_exceptions..wrapper functoolswrapsrrr0rr1wrap_exceptionssrcstfdd}|S)zWorkaround for https://github.com/giampaolo/psutil/issues/875. See: https://stackoverflow.com/questions/4457745#4457745 c sd}d}t|D]|}z|g|Ri|WSty}zB|}|jtkrtt|t|dd}WYd}~qnWYd}~qd}~00qd||f}t|j|j |ddS)N-C6?!r2{Gz?zI%s retried %s times, converted to AccessDenied as it's still returning %rr) r WindowsErrorrERROR_PARTIAL_COPYtimesleepminrrr) rrrdelaytimesr_rrrr0r1rs    z)retry_error_partial_copy..wrapperrrr0rr1retry_error_partial_copysr c@seZdZdZgdZddZddZddZed d Z d d Z e ed dZ e e ddZe e ddZddZddZe ddZe ddZddZe ddZe dd Ze dMd"d#Ze d$d%Ze d&d'Ze d(d)Ze d*d+Ze d,d-Ze d.d/Ze d0d1Ze e d2d3Ze d4d5Z e dNd7d8Z!e d9d:Z"e d;d<Z#e d=d>Z$e d?d@Z%e dAdBZ&e dCdDZ'e dEdFZ(e dGdHZ)e dIdJZ*e dKdLZ+d!S)OProcessz1Wrapper class around underlying C implementation.)rr_ppid_cachecCs||_d|_d|_dSr)rrr)rrr0r0r1rszProcess.__init__cCs|j||j|dSr) _proc_infoZcache_activateexerr0r0r1 oneshot_enters zProcess.oneshot_entercCs|j||j|dSr)rZcache_deactivaterrr0r0r1 oneshot_exits zProcess.oneshot_exitcCs$t|j}t|ttks J|S)zOReturn multiple information about this process as a raw tuple. )rvZ proc_inforrw pinfo_maprrr0r0r1rs zProcess._proc_infocCs,|jdkrdS|jdkrdStj|S)zbReturn process name, which on Windows is always the final part of the executable. rzSystem Idle Processr!ZSystem)rrxrkbasenamerrr0r0r1rs   z Process.namec Cstrbzt|j}Wqnty^}z2|jdkrHtd|t|j|jWYd}~qnd}~00n t|j}t szt |}| drt |S|S)Nz%r forced into AccessDeniedrs) PYPYrvZproc_exerrrrrrrr~ startswithrz)rrrr0r0r1rs    z Process.exec Cstjtjkrdztj|jdd}Wqtty`}z(t|rJtj|jdd}nWYd}~qtd}~00ntj|jdd}tr||Sdd|DSdS)NT)Zuse_pebFcSsg|] }t|qSr0r)rryr0r0r1rrz#Process.cmdline..)rvZWINVERZ WINDOWS_8_1Z proc_cmdlinerrrr)rrrr0r0r1cmdlines zProcess.cmdlinecCs6t|j}|r*ts*t|ts*Jt|tt|Sr) rvZ proc_environrrr{rrrr~)rZustrr0r0r1environs zProcess.environcCs4zt|jWSty.t|j|jYn0dSr)ppid_maprKeyErrorrrrr0r0r1ppid!s z Process.ppidcCszt|jWSty}zt|r|}|td|td|td|td|td|td|td|td|td |td f WYd}~SWYd}~n d}~00dS) NrPrQrRrSrTrUrVrWrXrY)rvZproc_memory_inforrrrr)rrinfor0r0r1_get_raw_meminfo's"           zProcess._get_raw_meminfocCs(|}|d}|d}t||f|S)Nr2r5)r re)rtrfrgr0r0r1 memory_info=szProcess.memory_infocCs,|}t|j}|t9}t||fSr)r"rvZproc_memory_ussrrrh)rZ basic_memrir0r0r1memory_full_infoGs  zProcess.memory_full_infoc cszt|j}Wn4tyD}zt||j|jWYd}~nFd}~00|D]6\}}}}t|}tsjt|}t |}||||fVqJdSr) rvZproc_memory_mapsrrrrrzrr~hex)rrawrZaddrZpermrkrfr0r0r1 memory_mapsNs&zProcess.memory_mapscCs t|jSr)rv proc_killrrr0r0r1kill]sz Process.killcCsT|tjkrt|jn8|ttdtttdtfvrHt|j|nt ddS)NZ CTRL_C_EVENTZCTRL_BREAK_EVENTzPonly SIGTERM, CTRL_C_EVENT and CTRL_BREAK_EVENT signals are supported on Windows) signalSIGTERMrvr'rrobjectrxr(r)rZsigr0r0r1 send_signalas  zProcess.send_signalNcCs|durtj}n t|d}ttdtj}|dur<||nd}zt|j|}Wn:tjyvt||j|jYntj yd}Yn0d}t |js|S|r||krt||j|jdt |t |dd}qdS)Ni monotonicrrr2r) rvZINFINITEintrrZ proc_waitrrrZTimeoutAbandoned pid_existsrr)rtimeoutZ cext_timeoutZtimerZstop_atZ exit_coder r0r0r1waitns$    z Process.waitcCs2|jdvrdSt|j\}}t|dt|S)Nrr!zNT AUTHORITY\SYSTEMrs)rrvZ proc_usernamer~)rdomainr[r0r0r1rs zProcess.usernamec Cshzt|j\}}}|WStyb}z2t|rL|tdWYd}~SWYd}~n d}~00dS)NrH)rv proc_timesrrrrr)rr[r\createdrr0r0r1rHszProcess.create_timecCs|tdS)NrI)rrrr0r0r1rIszProcess.num_threadscCs<t|j}g}|D]"\}}}t|||}||q|Sr)rvZ proc_threadsrrZpthreadr)rrrZ thread_idutimeZstimentupler0r0r1threadss   zProcess.threadsc Csvzt|j\}}}WnNtyd}z6t|s0|}|td}|td}WYd}~n d}~00t||ddS)NrFrGr) rvr4rrrrrrZ pcputimes)rr[r\r5rrr0r0r1rs "zProcess.cpu_timescCst|jddS)NTrvZproc_suspend_or_resumerrr0r0r1suspendszProcess.suspendcCst|jddS)NFr9rr0r0r1resumeszProcess.resumecCs4|jdvrt|j|jt|j}ttj|S)Nr2) rrrrvZproc_cwdr~rxrknormpath)rrkr0r0r1cwds  z Process.cwdcCsd|jdvrgSt}t|j}|D]6}t|}t|r$tsDt|}t |d}| |q$t |S)Nr2r*) rrrvZproc_open_filesrzr rr~rZ popenfilerr)rrZraw_file_namesZ_filer7r0r0r1 open_filess    zProcess.open_filesinetcCst||jdS)N)r)rr)rrr0r0r1 connectionsszProcess.connectionscCs t|j}tdurt|}|Sr)rvZproc_priority_getrenumr,rvaluer0r0r1nice_gets zProcess.nice_getcCst|j|Sr)rvZproc_priority_setrrBr0r0r1nice_setszProcess.nice_setcCs t|j}tdurt|}|Sr)rvZproc_io_priority_getrrAr3rr0r0r1 ionice_gets zProcess.ionice_getcCs:|r td|ttttfvr(td|t|j|dS)Nz&value argument not accepted on Windowsz%s is not a valid priority) TypeErrorr$r%r&r'rrvZproc_io_priority_setr)rZioclassrCr0r0r1 ionice_sets zProcess.ionice_setc Cszt|j}Wnvty}z^t|s*|}|td|td|td|td|td|tdf}WYd}~n d}~00t|S)NrJrKrLrMrNrO)rvZproc_io_countersrrrrrrn)rrrrr0r0r1 io_counters s      zProcess.io_counterscCs t|j}|rtjStjSdSr)rvZproc_is_suspendedrrZSTATUS_STOPPEDZSTATUS_RUNNING)rZ suspendedr0r0r1rs zProcess.statuscCsdd}t|j}||S)NcsfddtdDS)Ncsg|]}d|>@r|qS)rr0)rirr0r1r'rzBProcess.cpu_affinity_get..from_bitmask..@rrKr0rKr1 from_bitmask&sz.Process.cpu_affinity_get..from_bitmask)rvZproc_cpu_affinity_getr)rrMbitmaskr0r0r1cpu_affinity_get$s zProcess.cpu_affinity_getcCsndd}tttt}|D]4}||vrt|ttfsFtd|qtd|q||}t |j |dS)NcSs.|std|d}|D]}|d|O}q|S)Nzinvalid argument %rrr2)r)Zlsoutbr0r0r1 to_bitmask-s  z,Process.cpu_affinity_set..to_bitmaskz&invalid CPU %r; an integer is requiredzinvalid CPU %r) rrrwrr{r.rrGrrvZproc_cpu_affinity_setr)rrCrRZallcpusZcpurNr0r0r1cpu_affinity_set+s zProcess.cpu_affinity_setc Cs^zt|jWStyX}z2t|rB|tdWYd}~SWYd}~n d}~00dS)NrD)rvZproc_num_handlesrrrrrrr0r0r1rDDs zProcess.num_handlescCs|td}t|dS)NrEr)rrrZpctxsw)rrEr0r0r1num_ctx_switchesMszProcess.num_ctx_switches)N)r?),r-r.r/r __slots__rrrr rrrrr rrrr r"r#r&r(r,r1rrHrIr8rr:r;r=r>r@rDrErFrHrIrrOrSrDrTr0r0r0r1r s          *                   r )r*)NN)rrrrrxr)sysr collectionsrrrrrrr r r r r rrrrZ_compatrrrrrrrrrrrrrv ImportErrorrr|lowerrZgetwindowsversionr RuntimeError version_inforAZ__extra__all__r(rbuiltin_module_namesrr)IntEnumr+ZMIB_TCP_STATE_ESTABZCONN_ESTABLISHEDZMIB_TCP_STATE_SYN_SENTZ CONN_SYN_SENTZMIB_TCP_STATE_SYN_RCVDZ CONN_SYN_RECVZMIB_TCP_STATE_FIN_WAIT1ZCONN_FIN_WAIT1ZMIB_TCP_STATE_FIN_WAIT2ZCONN_FIN_WAIT2ZMIB_TCP_STATE_TIME_WAITZCONN_TIME_WAITZMIB_TCP_STATE_CLOSEDZ CONN_CLOSEZMIB_TCP_STATE_CLOSE_WAITZCONN_CLOSE_WAITZMIB_TCP_STATE_LAST_ACKZ CONN_LAST_ACKZMIB_TCP_STATE_LISTENZ CONN_LISTENZMIB_TCP_STATE_CLOSINGZ CONN_CLOSINGZMIB_TCP_STATE_DELETE_TCBZPSUTIL_CONN_NONEZ CONN_NONErr,globalsr __members__r$r%r&r'r3rrrZr`re_fieldsrhrjrtrlrnrzr~rrrZdisk_io_countersrrrrrrrrrrrrrrrrrrr"r#r+rZpidsr/rrrrr r r0r0r0r1s4                                             *