U |f;@sZdZddlmZddlZddlZddlZddlZddlZddlZddl Zddlm Z ddlm Z ddlm Z ddlm Z ddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'dd lm(Z(ej)j*Z*e+Z,d!Z-d"d#Z.Gd$d%d%e!Z/Gd&d'd'e/Z0e1e d(Gd)d*d*e!Z2Gd+d,d,e!Z3e4d-krVdd.l5m6Z6dd/l5m7Z7e rNe6d0e8d1e9de7e8dS)2aTests 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)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)selfargskwargsbeforefunM/opt/alt/python38/lib64/python3.8/site-packages/psutil/tests/test_memleaks.pywrapperEs z5fewtimes_if_linux..decorator..wrapper) functoolswraps)r*r-r+r)r, decoratorDs z$fewtimes_if_linux..decoratorr+)r0r+r+r,fewtimes_if_linux?sr1c@seZdZdZeZddZeddZeddZ edd Z ed d Z e e d ed dZe e d eddZeddZddZddZe e dddZe e dddZe e deddZe eddd Zed!d"Zeeed#d$d%Ze e d&d'd(Ze e d ed)d*Z ed+d,Z!eeed#d-d.Z"ed/d0Z#ee e$ dd1d2Z%ed3d4Z&ed5d6Z'e e d ed7d8Z(d9d:Z)ed;d<Z*e e+ dd=d>Z,e e+ dd?d@Z-edAdBZ.e e/ dedCdDZ0e e1 dEe e2 ddFdGZ3e e1 dEe e2 ddHdIZ4ee edJdKdLZ5e e6 ddMdNZ7e e d&dOdPZ8dQS)RTestProcessObjectLeaksz$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_namedsz TestProcessObjectLeaks.test_namecCs||jjdSr!)r8r9Zcmdliner;r+r+r, test_cmdlinehsz#TestProcessObjectLeaks.test_cmdlinecCs||jjdSr!)r8r9exer;r+r+r,test_exelszTestProcessObjectLeaks.test_execCs||jjdSr!)r8r9Zppidr;r+r+r, test_ppidpsz TestProcessObjectLeaks.test_ppidz POSIX onlycCs||jjdSr!)r8r9Zuidsr;r+r+r, test_uidstsz TestProcessObjectLeaks.test_uidscCs||jjdSr!)r8r9Zgidsr;r+r+r, test_gidsysz TestProcessObjectLeaks.test_gidscCs||jjdSr!)r8r9statusr;r+r+r, test_status~sz"TestProcessObjectLeaks.test_statuscCs||jjdSr!)r8r9nicer;r+r+r, test_nicesz TestProcessObjectLeaks.test_nicecs tfdddS)Ncs jSr!)r9rEr+Znicenessr%r+r,z6TestProcessObjectLeaks.test_nice_set..)thisprocrEr8r;r+rGr, test_nice_setsz$TestProcessObjectLeaks.test_nice_set not supportedcCs||jjdSr!)r8r9ionicer;r+r+r, test_ionicesz"TestProcessObjectLeaks.test_ionicecsZtr"tfddn4fddttjt dd} t |dS)Ncs jSr!)r9rMr+r%valuer+r,rHrIz8TestProcessObjectLeaks.test_ionice_set..csjtjSr!)r9rMpsutilZIOPRIO_CLASS_NONEr+r;r+r,rHrIr) rrJrMr8r.partialcextZproc_ioprio_setosgetpid execute_w_excOSErrorr%r*r+rOr,test_ionice_sets z&TestProcessObjectLeaks.test_ionice_setcCs||jjdSr!)r8r9Z io_countersr;r+r+r,test_io_counterssz'TestProcessObjectLeaks.test_io_counterszworthless on POSIXcCst||jjdSr!)rQProcessusernamer8r9r;r+r+r, test_usernames z$TestProcessObjectLeaks.test_usernamecCs||jjdSr!)r8r9Z create_timer;r+r+r,test_create_timesz'TestProcessObjectLeaks.test_create_time)Zonly_ifcCs||jjdSr!)r8r9Z num_threadsr;r+r+r,test_num_threadssz'TestProcessObjectLeaks.test_num_threads WINDOWS onlycCs||jjdSr!)r8r9Z num_handlesr;r+r+r,test_num_handlessz'TestProcessObjectLeaks.test_num_handlescCs||jjdSr!)r8r9Znum_fdsr;r+r+r, test_num_fdssz#TestProcessObjectLeaks.test_num_fdscCs||jjdSr!)r8r9Znum_ctx_switchesr;r+r+r,test_num_ctx_switchessz,TestProcessObjectLeaks.test_num_ctx_switchescCs||jjdSr!)r8r9threadsr;r+r+r, test_threadssz#TestProcessObjectLeaks.test_threadscCs||jjdSr!)r8r9 cpu_timesr;r+r+r,test_cpu_timessz%TestProcessObjectLeaks.test_cpu_timescCs||jjdSr!)r8r9Zcpu_numr;r+r+r, test_cpu_numsz#TestProcessObjectLeaks.test_cpu_numcCs||jjdSr!)r8r9Z memory_infor;r+r+r,test_memory_infosz'TestProcessObjectLeaks.test_memory_infocCs||jjdSr!)r8r9Zmemory_full_infor;r+r+r,test_memory_full_infosz,TestProcessObjectLeaks.test_memory_full_infocCs||jjdSr!)r8r9Zterminalr;r+r+r, test_terminalsz$TestProcessObjectLeaks.test_terminalcCs$trtn|j}|j|jj|ddS)Nr#)rr$r#r8r9resumer%r#r+r+r, test_resumesz"TestProcessObjectLeaks.test_resumecCs||jjdSr!)r8r9cwdr;r+r+r,test_cwdszTestProcessObjectLeaks.test_cwdcCs||jjdSr!)r8r9 cpu_affinityr;r+r+r,test_cpu_affinitysz(TestProcessObjectLeaks.test_cpu_affinitycs4tfddtfdddS)Ncs jSr!r9rsr+Zaffinityr%r+r,rHrIz>TestProcessObjectLeaks.test_cpu_affinity_set..csjdgSNrRrur+r;r+r,rHrI)rJrsr8rW ValueErrorr;r+rvr,test_cpu_affinity_setsz,TestProcessObjectLeaks.test_cpu_affinity_setc Cs*ttd||jjW5QRXdS)Nw)openrr8r9Z open_filesr;r+r+r,test_open_filessz&TestProcessObjectLeaks.test_open_filescCs||jjdSr!)r8r9Z memory_mapsr;r+r+r,test_memory_mapssz'TestProcessObjectLeaks.test_memory_mapsz LINUX onlycsfdddS)NcsjtjSr!r9rlimitrQ RLIMIT_NOFILEr+r;r+r,rHrIz4TestProcessObjectLeaks.test_rlimit..r8r;r+r;r, test_rlimitsz"TestProcessObjectLeaks.test_rlimitcs<ttjfddttffdddS)NcsjtjSr!r~r+limitr%r+r,rHrIz8TestProcessObjectLeaks.test_rlimit_set..cs jdSrw)r9rr+r;r+r,rHrI)rJrrQrr8rWrXrxr;r+rr,test_rlimit_sets z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc s6t&trdndfddW5QRXdS)NZinetallcs jSr!)r9net_connectionsr+kindr%r+r,rHrIz=TestProcessObjectLeaks.test_net_connections..)rrr8r;r+rr,test_net_connectionss z+TestProcessObjectLeaks.test_net_connectionscCs||jjdSr!)r8r9environr;r+r+r, test_environsz#TestProcessObjectLeaks.test_environcCs|dddS)NcSsttSr!)rT proc_inforUrVr+r+r+r,rH rIz7TestProcessObjectLeaks.test_proc_info..rr;r+r+r,test_proc_info sz%TestProcessObjectLeaks.test_proc_infoN)9__name__ __module__ __qualname____doc__rJr9r7r1r<r=r?r@unittestskipIfrrArBrDrFrKrrNrZrr[r^r_rrr`rrbrcrdrfrhrrirjrkrlrprrr rtryr|rr}rrrrrr rrr+r+r+r,r2[s                                                  r2cspeZdZdZefddZefddZddZerhdd 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 setUpClassrsubprQr\pidr9killwaitclsr"r+r,rs   z%TestTerminatedProcessLeaks.setUpClasscstt|jdSr!)r tearDownClassrrrrr+r,rs z(TestTerminatedProcessLeaks.tearDownClasscCs&z |Wntjk r YnXdSr!)rQZ NoSuchProcessrYr+r+r,call$s zTestTerminatedProcessLeaks.callcCs||jjdSr!)r8r9rr;r+r+r, test_kill,sz$TestTerminatedProcessLeaks.test_killcCs||jjdSr!)r8r9rr;r+r+r,test_terminate/sz)TestTerminatedProcessLeaks.test_terminatecCs||jjdSr!)r8r9Zsuspendr;r+r+r, test_suspend2sz'TestTerminatedProcessLeaks.test_suspendcCs||jjdSr!)r8r9rnr;r+r+r,rp5sz&TestTerminatedProcessLeaks.test_resumecCs||jjdSr!)r8r9rr;r+r+r, test_wait8sz$TestTerminatedProcessLeaks.test_waitcsfdd}|dS)Ncs*ztjjWStk r$YnXdSr!)rTrr9rr r+r;r+r,r=sz7TestTerminatedProcessLeaks.test_proc_info..callr)r%rr+r;r,r;s z)TestTerminatedProcessLeaks.test_proc_info)rrrr classmethodrrrrrrrrprr __classcell__r+r+rr,rsrrac@seZdZddZddZdS)TestProcessDualImplementationcCs|dddS)NcSstjtddS)NTZuse_pebrTZ proc_cmdlinerUrVr+r+r+r,rHIrIzETestProcessDualImplementation.test_cmdline_peb_true..rr;r+r+r,test_cmdline_peb_trueHsz3TestProcessDualImplementation.test_cmdline_peb_truecCs|dddS)NcSstjtddS)NFrrr+r+r+r,rHLrIzFTestProcessDualImplementation.test_cmdline_peb_false..rr;r+r+r,test_cmdline_peb_falseKsz4TestProcessDualImplementation.test_cmdline_peb_falseN)rrrrrr+r+r+r,rFsrc@seZdZdZddZeddZeddZedd Zed d Z ed d Z ee e opedkde e dddZe e dddZddZe edddZddZddZe edd d!Ze eoejd" d#ed$d%Zed&d'Z ee e! dd(d)Z"ee e oNe#d*kd+d,d-Z$d.d/Z%e edd0d1Z&ee e' dd2d3Z(ee e) dd4d5Z*ee e+ dd6d7Z,ed8d9Z-d:d;Z.dd?Z0d@dAZ1dBdCZ2dDdEZ3dFdGZ4dHS)ITestModuleFunctionsLeaksz&Test leaks of psutil module functions.cCst}|||jdSr!)rr3rr5r+r+r,r7Wsz&TestModuleFunctionsLeaks.test_coveragecCs|dddS)NcSs tjddS)NTZlogicalrQ cpu_countr+r+r+r,rH_rIz9TestModuleFunctionsLeaks.test_cpu_count..rr;r+r+r,test_cpu_count]sz'TestModuleFunctionsLeaks.test_cpu_countcCs|dddS)NcSs tjddS)NFrrr+r+r+r,rHcrIz?TestModuleFunctionsLeaks.test_cpu_count_cores..rr;r+r+r,test_cpu_count_coresasz-TestModuleFunctionsLeaks.test_cpu_count_corescCs|tjdSr!)r8rQrgr;r+r+r,rhesz'TestModuleFunctionsLeaks.test_cpu_timescCs|dddS)NcSs tjddS)NT)Zpercpu)rQrgr+r+r+r,rHkrIz=TestModuleFunctionsLeaks.test_per_cpu_times..rr;r+r+r,test_per_cpu_timesisz+TestModuleFunctionsLeaks.test_per_cpu_timescCs|tjdSr!)r8rQZ cpu_statsr;r+r+r,test_cpu_statsmsz'TestModuleFunctionsLeaks.test_cpu_statsarm64zskipped due to #1892rLcCs|tjdSr!)r8rQZcpu_freqr;r+r+r, test_cpu_freqqsz&TestModuleFunctionsLeaks.test_cpu_freqracCst|tjdSr!)rQ getloadavgr8r;r+r+r,test_getloadavgzsz(TestModuleFunctionsLeaks.test_getloadavgcCs|tjdSr!)r8rQZvirtual_memoryr;r+r+r,test_virtual_memorysz,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)cCs|tjdSr!)r8rQZ swap_memoryr;r+r+r,test_swap_memorysz)TestModuleFunctionsLeaks.test_swap_memorycCs$trtn|j}|jdd|ddS)NcSsttSr!)rQZ pid_existsrUrVr+r+r+r,rHrIz:TestModuleFunctionsLeaks.test_pid_exists..rmrr$r#r8ror+r+r,test_pid_existssz(TestModuleFunctionsLeaks.test_pid_existscCs$trtn|j}|jdd|ddS)NcSs tdS)N.)rQ disk_usager+r+r+r,rHrIz:TestModuleFunctionsLeaks.test_disk_usage..rmrror+r+r,test_disk_usagesz(TestModuleFunctionsLeaks.test_disk_usagezQEMU user not supportedcCs|tjdSr!)r8rQZdisk_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)rQZdisk_io_countersr+r+r+r,rHrIz@TestModuleFunctionsLeaks.test_disk_io_counters..rr;r+r+r,test_disk_io_counterssz.TestModuleFunctionsLeaks.test_disk_io_counterscCs|tjdSr!)r8rQZpidsr;r+r+r, test_pidssz"TestModuleFunctionsLeaks.test_pidscCs|dddS)NcSs tjddSr)rQZnet_io_countersr+r+r+r,rHrIz?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)rQrr+r+r+r,rHrIz?TestModuleFunctionsLeaks.test_net_connections..)rQrrr8r;r+r+r,rs z-TestModuleFunctionsLeaks.test_net_connectionscCs"trdn|j}|jtj|ddS)Ni@) tolerance)rrr8rQZ net_if_addrs)r%rr+r+r,test_net_if_addrssz*TestModuleFunctionsLeaks.test_net_if_addrscCs|tjdSr!)r8rQZ net_if_statsr;r+r+r,test_net_if_statssz*TestModuleFunctionsLeaks.test_net_if_statscCs|tjdSr!)r8rQZsensors_batteryr;r+r+r,test_sensors_batterysz-TestModuleFunctionsLeaks.test_sensors_batterycCs|tjdSr!)r8rQZsensors_temperaturesr;r+r+r,test_sensors_temperaturessz2TestModuleFunctionsLeaks.test_sensors_temperaturescCs|tjdSr!)r8rQZ sensors_fansr;r+r+r,test_sensors_fanssz*TestModuleFunctionsLeaks.test_sensors_fanscCs|tjdSr!)r8rQZ boot_timer;r+r+r,test_boot_timesz'TestModuleFunctionsLeaks.test_boot_timecCs|tjdSr!)r8rQZusersr;r+r+r, test_userssz#TestModuleFunctionsLeaks.test_userscCs|dddS)NcSs tdS)NF)rQZ _set_debugr+r+r+r,rHrIz9TestModuleFunctionsLeaks.test_set_debug..rr;r+r+r,test_set_debugsz'TestModuleFunctionsLeaks.test_set_debugcCs|tjdSr!)r8rTZwinservice_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!)rTZwinservice_query_configr+r:r+r,rHrIzFTestModuleFunctionsLeaks.test_win_service_get_config..nextrQZwin_service_iterr:r8r;r+rr,test_win_service_get_configsz4TestModuleFunctionsLeaks.test_win_service_get_configcs&tt|fdddS)Ncs tSr!)rTZwinservice_query_statusr+rr+r,rHrIzFTestModuleFunctionsLeaks.test_win_service_get_status..rr;r+rr,test_win_service_get_statussz4TestModuleFunctionsLeaks.test_win_service_get_statuscs&tt|fdddS)Ncs tSr!)rTZwinservice_query_descrr+rr+r,rHrIzKTestModuleFunctionsLeaks.test_win_service_get_description..rr;r+rr, test_win_service_get_descriptionsz9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)5rrrrr7r1rrrhrrrrrplatformmachiner rrrrrrrrrrrrUpathexistsrrrrgetuidrrrrrrrrrrrrrrrrrr+r+r+r,rTs                          r__main__)cprint) run_from_namez!skipping %s tests under QEMU_USERZbrown):r __future__rr.rUrsysrrQZpsutil._commonrrrrrrZpsutil._compatr r Z psutil.testsr r r rrrrrrrrrrrrrrrrrrZ _psplatformrTr\rJr$r1r2rrrrrZpsutil.tests.runnerrr__file__exitr+r+r+r,sn                              66