U 1g3<@sdZddlmZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd lm&Z&dd!lm'Z'ej(j)Z)e*Z+d"Z,d#d$Z-Gd%d&d&eZ.Gd'd(d(e.Z/e#j0j1e d)d*Gd+d,d,eZ2Gd-d.d.eZ3dS)/aTests for detecting function memory leaks (typically the ones implemented in C). It does so by calling a function many times and checking whether process memory usage keeps increasing between calls or over time. Note that this may produce false positives (especially on Windows for some reason). PyPy appears to be completely unstable for this framework, probably because of how its JIT handles memory, so tests are skipped. )print_functionN)LINUX)MACOS)OPENBSD)POSIX)SUNOS)WINDOWS)ProcessLookupError)super)HAS_CPU_AFFINITY) HAS_CPU_FREQ) HAS_ENVIRON) HAS_IONICE)HAS_MEMORY_MAPS)HAS_NET_IO_COUNTERS)HAS_PROC_CPU_NUM)HAS_PROC_IO_COUNTERS) HAS_RLIMIT)HAS_SENSORS_BATTERY)HAS_SENSORS_FANS)HAS_SENSORS_TEMPERATURES) QEMU_USER)TestMemoryLeak)create_sockets) get_testfn)process_namespace)pytest)skip_on_access_denied)spawn_testproc)system_namespace) terminatecCs dd}|S)zsDecorator for those Linux functions which are implemented in pure Python, and which we want to run faster. cstfdd}|S)NcsJtr6|jj}zt|j_|f||WS||j_Xn|f||SdSN)r __class__times FEW_TIMES)selfargskwargsbeforefunJ/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_memleaks.pywrapperDs z5fewtimes_if_linux..decorator..wrapper) functoolswraps)r+r.r,r*r- decoratorCs z$fewtimes_if_linux..decoratorr,)r1r,r,r-fewtimes_if_linux>sr2c@s0eZdZdZeZddZeddZeddZ edd Z ed d Z e j je d d eddZe j je d d eddZeddZddZddZe j je dd ddZe j je dd ddZe j je dd eddZe j jedd d d!Zed"d#Zeeed$d%d&Ze j je d'd d(d)Z e j je d d ed*d+Z!ed,d-Z"eeed$d.d/Z#ed0d1Z$ee j je% dd d2d3Z&ed4d5Z'ed6d7Z(e j je d d ed8d9Z)d:d;Z*edd?Z-e j je, dd d@dAZ.edBdCZ/e j je0 dd edDdEZ1e j je2 dFd e j je3 dd dGdHZ4e j je2 dFd e j je3 dd dIdJZ5ee j jedKd dLdMZ6e j je7 dd dNdOZ8e j je d'd dPdQZ9dRS)STestProcessObjectLeaksz$Test leaks of Process class methods.cCs td}|||j|jdSr")rtest_class_coveragegettersZsettersr&nsr,r,r- test_coverage_sz$TestProcessObjectLeaks.test_coveragecCs||jjdSr")executeprocnamer&r,r,r- test_namecsz TestProcessObjectLeaks.test_namecCs||jjdSr")r9r:Zcmdliner<r,r,r- test_cmdlinegsz#TestProcessObjectLeaks.test_cmdlinecCs||jjdSr")r9r:exer<r,r,r-test_exekszTestProcessObjectLeaks.test_execCs||jjdSr")r9r:Zppidr<r,r,r- test_ppidosz TestProcessObjectLeaks.test_ppidz POSIX onlyreasoncCs||jjdSr")r9r:Zuidsr<r,r,r- test_uidsssz TestProcessObjectLeaks.test_uidscCs||jjdSr")r9r:Zgidsr<r,r,r- test_gidsxsz TestProcessObjectLeaks.test_gidscCs||jjdSr")r9r:statusr<r,r,r- test_status}sz"TestProcessObjectLeaks.test_statuscCs||jjdSr")r9r:nicer<r,r,r- test_nicesz TestProcessObjectLeaks.test_nicecs tfdddS)Ncs jSr")r:rHr,Znicenessr&r,r-z6TestProcessObjectLeaks.test_nice_set..)thisprocrHr9r<r,rJr- test_nice_setsz$TestProcessObjectLeaks.test_nice_set not supportedcCs||jjdSr")r9r:ionicer<r,r,r- test_ionicesz"TestProcessObjectLeaks.test_ionicecsZtr"tfddn4fddttjt dd} t |dS)Ncs jSr")r:rPr,r&valuer,r-rKrLz8TestProcessObjectLeaks.test_ionice_set..csjtjSr")r:rPpsutilZIOPRIO_CLASS_NONEr,r<r,r-rKrLr) rrMrPr9r/partialcextZproc_ioprio_setosgetpid execute_w_excOSErrorr&r+r,rRr-test_ionice_sets z&TestProcessObjectLeaks.test_ionice_setcCs||jjdSr")r9r:Z io_countersr<r,r,r-test_io_counterssz'TestProcessObjectLeaks.test_io_counterszworthless on POSIXcCst||jjdSr")rTProcessusernamer9r:r<r,r,r- test_usernames z$TestProcessObjectLeaks.test_usernamecCs||jjdSr")r9r:Z create_timer<r,r,r-test_create_timesz'TestProcessObjectLeaks.test_create_time)Zonly_ifcCs||jjdSr")r9r:Z num_threadsr<r,r,r-test_num_threadssz'TestProcessObjectLeaks.test_num_threads WINDOWS onlycCs||jjdSr")r9r:Z num_handlesr<r,r,r-test_num_handlessz'TestProcessObjectLeaks.test_num_handlescCs||jjdSr")r9r:Znum_fdsr<r,r,r- test_num_fdssz#TestProcessObjectLeaks.test_num_fdscCs||jjdSr")r9r:Znum_ctx_switchesr<r,r,r-test_num_ctx_switchessz,TestProcessObjectLeaks.test_num_ctx_switchescCs||jjdSr")r9r:threadsr<r,r,r- test_threadssz#TestProcessObjectLeaks.test_threadscCs||jjdSr")r9r: cpu_timesr<r,r,r-test_cpu_timessz%TestProcessObjectLeaks.test_cpu_timescCs||jjdSr")r9r:Zcpu_numr<r,r,r- test_cpu_numsz#TestProcessObjectLeaks.test_cpu_numcCs||jjdSr")r9r:Z memory_infor<r,r,r-test_memory_infosz'TestProcessObjectLeaks.test_memory_infocCs||jjdSr")r9r:Zmemory_full_infor<r,r,r-test_memory_full_infosz,TestProcessObjectLeaks.test_memory_full_infocCs||jjdSr")r9r:Zterminalr<r,r,r- test_terminalsz$TestProcessObjectLeaks.test_terminalcCs$trtn|j}|j|jj|ddS)Nr$)rr%r$r9r:resumer&r$r,r,r- test_resumesz"TestProcessObjectLeaks.test_resumecCs||jjdSr")r9r:cwdr<r,r,r-test_cwdszTestProcessObjectLeaks.test_cwdcCs||jjdSr")r9r: cpu_affinityr<r,r,r-test_cpu_affinitysz(TestProcessObjectLeaks.test_cpu_affinitycs4tfddtfdddS)Ncs jSr"r:rvr,Zaffinityr&r,r-rKrLz>TestProcessObjectLeaks.test_cpu_affinity_set..csjdgSNrUrxr,r<r,r-rKrL)rMrvr9rZ ValueErrorr<r,ryr-test_cpu_affinity_setsz,TestProcessObjectLeaks.test_cpu_affinity_setc Cs*ttd||jjW5QRXdS)Nw)openrr9r:Z open_filesr<r,r,r-test_open_filessz&TestProcessObjectLeaks.test_open_filescCs||jjdSr")r9r:Z memory_mapsr<r,r,r-test_memory_mapssz'TestProcessObjectLeaks.test_memory_mapsz LINUX onlycsfdddS)NcsjtjSr"r:rlimitrT RLIMIT_NOFILEr,r<r,r-rKrLz4TestProcessObjectLeaks.test_rlimit..r9r<r,r<r- test_rlimitsz"TestProcessObjectLeaks.test_rlimitcs<ttjfddttffdddS)NcsjtjSr"rr,limitr&r,r-rKrLz8TestProcessObjectLeaks.test_rlimit_set..cs jdSrz)r:rr,r<r,r-rKrL)rMrrTrr9rZr[r{r<r,rr-test_rlimit_sets z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc s6t&trdndfddW5QRXdS)NZinetallcs jSr")r:net_connectionsr,kindr&r,r-rKrLz=TestProcessObjectLeaks.test_net_connections..)rrr9r<r,rr-test_net_connectionss z+TestProcessObjectLeaks.test_net_connectionscCs||jjdSr")r9r:environr<r,r,r- test_environsz#TestProcessObjectLeaks.test_environcCs|dddS)NcSsttSr")rW proc_inforXrYr,r,r,r-rK rLz7TestProcessObjectLeaks.test_proc_info..rr<r,r,r-test_proc_info sz%TestProcessObjectLeaks.test_proc_infoN):__name__ __module__ __qualname____doc__rMr:r8r2r=r>r@rArmarkskipifrrDrErGrIrNrrQr]rr^rarbrrrcrrerfrgrirkrrlrmrnrorsrur rwr|rrrrrrrrr rrr,r,r,r-r3Zs                               r3cspeZdZdZefddZefddZddZerhdd Z d d Z d d Z ddZ ddZ ddZZS)TestTerminatedProcessLeakszRepeat the tests above looking for leaks occurring when dealing with terminated processes raising NoSuchProcess exception. The C functions are still invoked but will follow different code paths. We'll check those code paths. cs:tt|_t|jj|_|j|j dSr") r setUpClassrsubprTr_pidr:killwaitclsr#r,r-rs   z%TestTerminatedProcessLeaks.setUpClasscstt|jdSr")r tearDownClassr rrrr,r-rs z(TestTerminatedProcessLeaks.tearDownClasscCs&z |Wntjk r YnXdSr")rTZ NoSuchProcessr\r,r,r-call#s zTestTerminatedProcessLeaks.callcCs||jjdSr")r9r:rr<r,r,r- test_kill+sz$TestTerminatedProcessLeaks.test_killcCs||jjdSr")r9r:r r<r,r,r-test_terminate.sz)TestTerminatedProcessLeaks.test_terminatecCs||jjdSr")r9r:Zsuspendr<r,r,r- test_suspend1sz'TestTerminatedProcessLeaks.test_suspendcCs||jjdSr")r9r:rqr<r,r,r-rs4sz&TestTerminatedProcessLeaks.test_resumecCs||jjdSr")r9r:rr<r,r,r- test_wait7sz$TestTerminatedProcessLeaks.test_waitcsfdd}|dS)Ncs*ztjjWStk r$YnXdSr")rWrr:rr r,r<r,r-r<sz7TestTerminatedProcessLeaks.test_proc_info..callr)r&rr,r<r-r:s z)TestTerminatedProcessLeaks.test_proc_info)rrrr classmethodrrrrrrrrsrr __classcell__r,r,rr-rsrrdrBc@seZdZddZddZdS)TestProcessDualImplementationcCs|dddS)NcSstjtddS)NTZuse_pebrWZ proc_cmdlinerXrYr,r,r,r-rKHrLzETestProcessDualImplementation.test_cmdline_peb_true..rr<r,r,r-test_cmdline_peb_trueGsz3TestProcessDualImplementation.test_cmdline_peb_truecCs|dddS)NcSstjtddS)NFrrr,r,r,r-rKKrLzFTestProcessDualImplementation.test_cmdline_peb_false..rr<r,r,r-test_cmdline_peb_falseJsz4TestProcessDualImplementation.test_cmdline_peb_falseN)rrrrrr,r,r,r-rEsrc@sPeZdZdZddZeddZeddZedd Zed d Z ed d Z ee j j eoredkdde j j e ddddZe j j e ddddZddZe j j eddddZddZddZe j j ed dd!d"Ze j j eoejd# d$ded%d&Z ed'd(Z!ee j j e" ddd)d*Z#ee j j eone$d+kd,dd-d.Z%d/d0Z&e j j ed dd1d2Z'ee j j e( ddd3d4Z)ee j j e* ddd5d6Z+ee j j e, ddd7d8Z-ed9d:Z.d;d<Z/d=d>Z0erLd?d@Z1dAdBZ2dCdDZ3dEdFZ4dGdHZ5dIS)JTestModuleFunctionsLeaksz&Test leaks of psutil module functions.cCst}|||jdSr")rr4rr6r,r,r-r8Vsz&TestModuleFunctionsLeaks.test_coveragecCs|dddS)NcSs tjddS)NTZlogicalrT cpu_countr,r,r,r-rK^rLz9TestModuleFunctionsLeaks.test_cpu_count..rr<r,r,r-test_cpu_count\sz'TestModuleFunctionsLeaks.test_cpu_countcCs|dddS)NcSs tjddS)NFrrr,r,r,r-rKbrLz?TestModuleFunctionsLeaks.test_cpu_count_cores..rr<r,r,r-test_cpu_count_cores`sz-TestModuleFunctionsLeaks.test_cpu_count_corescCs|tjdSr")r9rTrjr<r,r,r-rkdsz'TestModuleFunctionsLeaks.test_cpu_timescCs|dddS)NcSs tjddS)NT)Zpercpu)rTrjr,r,r,r-rKjrLz=TestModuleFunctionsLeaks.test_per_cpu_times..rr<r,r,r-test_per_cpu_timeshsz+TestModuleFunctionsLeaks.test_per_cpu_timescCs|tjdSr")r9rTZ cpu_statsr<r,r,r-test_cpu_statslsz'TestModuleFunctionsLeaks.test_cpu_statsarm64zskipped due to #1892rBrOcCs|tjdSr")r9rTZcpu_freqr<r,r,r- test_cpu_freqpsz&TestModuleFunctionsLeaks.test_cpu_freqrdcCst|tjdSr")rT getloadavgr9r<r,r,r-test_getloadavgysz(TestModuleFunctionsLeaks.test_getloadavgcCs|tjdSr")r9rTZvirtual_memoryr<r,r,r-test_virtual_memorysz,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)cCs|tjdSr")r9rTZ swap_memoryr<r,r,r-test_swap_memorysz)TestModuleFunctionsLeaks.test_swap_memorycCs$trtn|j}|jdd|ddS)NcSsttSr")rTZ pid_existsrXrYr,r,r,r-rKrLz:TestModuleFunctionsLeaks.test_pid_exists..rprr%r$r9rrr,r,r-test_pid_existssz(TestModuleFunctionsLeaks.test_pid_existscCs$trtn|j}|jdd|ddS)NcSs tdS)N.)rT disk_usager,r,r,r-rKrLz:TestModuleFunctionsLeaks.test_disk_usage..rprrrr,r,r-test_disk_usagesz(TestModuleFunctionsLeaks.test_disk_usagezQEMU user not supportedcCs|tjdSr")r9rTZdisk_partitionsr<r,r,r-test_disk_partitionssz-TestModuleFunctionsLeaks.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this Linux versioncCs|dddS)NcSs tjddSNF)Znowrap)rTZdisk_io_countersr,r,r,r-rKrLz@TestModuleFunctionsLeaks.test_disk_io_counters..rr<r,r,r-test_disk_io_counterssz.TestModuleFunctionsLeaks.test_disk_io_counterscCs|tjdSr")r9rTZpidsr<r,r,r- test_pidssz"TestModuleFunctionsLeaks.test_pidscCs|dddS)NcSs tjddSr)rTZnet_io_countersr,r,r,r-rKrLz?TestModuleFunctionsLeaks.test_net_io_counters..rr<r,r,r-test_net_io_counterssz-TestModuleFunctionsLeaks.test_net_io_countersrzneed root accessc Cs0tjddt|ddW5QRXdS)NrrcSs tjddS)Nrr)rTrr,r,r,r-rKrLz?TestModuleFunctionsLeaks.test_net_connections..)rTrrr9r<r,r,r-rs z-TestModuleFunctionsLeaks.test_net_connectionscCs"trdn|j}|jtj|ddS)Ni@) tolerance)rrr9rTZ net_if_addrs)r&rr,r,r-test_net_if_addrssz*TestModuleFunctionsLeaks.test_net_if_addrscCs|tjdSr")r9rTZ net_if_statsr<r,r,r-test_net_if_statssz*TestModuleFunctionsLeaks.test_net_if_statscCs|tjdSr")r9rTZsensors_batteryr<r,r,r-test_sensors_batterysz-TestModuleFunctionsLeaks.test_sensors_batterycCs|tjdSr")r9rTZsensors_temperaturesr<r,r,r-test_sensors_temperaturessz2TestModuleFunctionsLeaks.test_sensors_temperaturescCs|tjdSr")r9rTZ sensors_fansr<r,r,r-test_sensors_fanssz*TestModuleFunctionsLeaks.test_sensors_fanscCs|tjdSr")r9rTZ boot_timer<r,r,r-test_boot_timesz'TestModuleFunctionsLeaks.test_boot_timecCs|tjdSr")r9rTZusersr<r,r,r- test_userssz#TestModuleFunctionsLeaks.test_userscCs|dddS)NcSs tdS)NF)rTZ _set_debugr,r,r,r-rKrLz9TestModuleFunctionsLeaks.test_set_debug..rr<r,r,r-test_set_debugsz'TestModuleFunctionsLeaks.test_set_debugcCs|tjdSr")r9rWZwinservice_enumerater<r,r,r-test_win_service_itersz.TestModuleFunctionsLeaks.test_win_service_itercCsdSr"r,r<r,r,r-test_win_service_getsz-TestModuleFunctionsLeaks.test_win_service_getcs&tt|fdddS)Ncs tSr")rWZwinservice_query_configr,r;r,r-rKrLzFTestModuleFunctionsLeaks.test_win_service_get_config..nextrTZwin_service_iterr;r9r<r,rr-test_win_service_get_configsz4TestModuleFunctionsLeaks.test_win_service_get_configcs&tt|fdddS)Ncs tSr")rWZwinservice_query_statusr,rr,r-rKrLzFTestModuleFunctionsLeaks.test_win_service_get_status..rr<r,rr-test_win_service_get_statussz4TestModuleFunctionsLeaks.test_win_service_get_statuscs&tt|fdddS)Ncs tSr")rWZwinservice_query_descrr,rr,r-rKrLzKTestModuleFunctionsLeaks.test_win_service_get_description..rr<r,rr- test_win_service_get_descriptionsz9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)6rrrrr8r2rrrkrrrrrrplatformmachiner rrrrrrrrrrrrXpathexistsrrrrgetuidrrrrrrrrrrrrrrrrrr,r,r,r-rSs                 r)4r __future__rr/rXrrTZpsutil._commonrrrrrrZpsutil._compatr r Z psutil.testsr r r rrrrrrrrrrrrrrrrrrr Z _psplatformrWr_rMr%r2r3rrrrrr,r,r,r-s\                               66