B =_9@sdZddlmZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z dd lm Z dd l mZdd l mZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm 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,Z-e%.e d(Gd)d*d*e$Z/Gd+d,d,e$Z0e1d-krdd.l2m3Z3e3e4dS)/a Tests 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)create_sockets) get_testfn)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)process_namespace)skip_on_access_denied)spawn_testproc)system_namespace) terminate)TestMemoryLeak)unittestcCs dd}|S)zsDecorator for those Linux functions which are implemented in pure Python, and which we want to run faster. cstfdd}|S)NcsFtr2|jj}zt|j_|f||S||j_Xn|f||SdS)N)r __class__times FEW_TIMES)selfargskwargsZbefore)funM/opt/alt/python37/lib64/python3.7/site-packages/psutil/tests/test_memleaks.pywrapperAs z5fewtimes_if_linux..decorator..wrapper) functoolswraps)r'r*r()r'r) decorator@s z$fewtimes_if_linux..decoratorr()r-r(r(r)fewtimes_if_linux<s r.c@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|jdS)N)rtest_class_coverageZgettersZsetters)r$nsr(r(r) test_coverageZsz$TestProcessObjectLeaks.test_coveragecCs||jjdS)N)executeprocname)r$r(r(r) test_name^sz TestProcessObjectLeaks.test_namecCs||jjdS)N)r3r4Zcmdline)r$r(r(r) test_cmdlinebsz#TestProcessObjectLeaks.test_cmdlinecCs||jjdS)N)r3r4Zexe)r$r(r(r)test_exefszTestProcessObjectLeaks.test_execCs||jjdS)N)r3r4Zppid)r$r(r(r) test_ppidjsz TestProcessObjectLeaks.test_ppidz POSIX onlycCs||jjdS)N)r3r4Zuids)r$r(r(r) test_uidsnsz TestProcessObjectLeaks.test_uidscCs||jjdS)N)r3r4Zgids)r$r(r(r) test_gidsssz TestProcessObjectLeaks.test_gidscCs||jjdS)N)r3r4Zstatus)r$r(r(r) test_statusxsz"TestProcessObjectLeaks.test_statuscCs||jjdS)N)r3r4nice)r$r(r(r) test_nice|sz TestProcessObjectLeaks.test_nicecs tfdddS)Ncs jS)N)r4r=r()nicenessr$r(r)z6TestProcessObjectLeaks.test_nice_set..)thisprocr=r3)r$r()r?r$r) test_nice_setsz$TestProcessObjectLeaks.test_nice_setz not supportedcCs||jjdS)N)r3r4ionice)r$r(r(r) test_ionicesz"TestProcessObjectLeaks.test_ionicecsZtr"tfddn4fddttjt dd} t |dS)Ncs jS)N)r4rDr()r$valuer(r)r@rAz8TestProcessObjectLeaks.test_ionice_set..csjtjS)N)r4rDpsutilZIOPRIO_CLASS_NONEr()r$r(r)r@rAr) rrBrDr3r+partialcextZproc_ioprio_setosgetpid execute_w_excOSError)r$r'r()r$rFr)test_ionice_sets z&TestProcessObjectLeaks.test_ionice_setcCs||jjdS)N)r3r4Z io_counters)r$r(r(r)test_io_counterssz'TestProcessObjectLeaks.test_io_counterszworthless on POSIXcCst||jjdS)N)rGProcessZusernamer3r4)r$r(r(r) test_usernames z$TestProcessObjectLeaks.test_usernamecCs||jjdS)N)r3r4Z create_time)r$r(r(r)test_create_timesz'TestProcessObjectLeaks.test_create_time)Zonly_ifcCs||jjdS)N)r3r4Z num_threads)r$r(r(r)test_num_threadssz'TestProcessObjectLeaks.test_num_threadsz WINDOWS onlycCs||jjdS)N)r3r4Z num_handles)r$r(r(r)test_num_handlessz'TestProcessObjectLeaks.test_num_handlescCs||jjdS)N)r3r4Znum_fds)r$r(r(r) test_num_fdssz#TestProcessObjectLeaks.test_num_fdscCs||jjdS)N)r3r4Znum_ctx_switches)r$r(r(r)test_num_ctx_switchessz,TestProcessObjectLeaks.test_num_ctx_switchescCs||jjdS)N)r3r4Zthreads)r$r(r(r) test_threadssz#TestProcessObjectLeaks.test_threadscCs||jjdS)N)r3r4 cpu_times)r$r(r(r)test_cpu_timessz%TestProcessObjectLeaks.test_cpu_timescCs||jjdS)N)r3r4Zcpu_num)r$r(r(r) test_cpu_numsz#TestProcessObjectLeaks.test_cpu_numcCs||jjdS)N)r3r4Z memory_info)r$r(r(r)test_memory_infosz'TestProcessObjectLeaks.test_memory_infocCs||jjdS)N)r3r4Zmemory_full_info)r$r(r(r)test_memory_full_infosz,TestProcessObjectLeaks.test_memory_full_infocCs||jjdS)N)r3r4Zterminal)r$r(r(r) test_terminalsz$TestProcessObjectLeaks.test_terminalcCs$trtn|j}|j|jj|ddS)N)r")rr#r"r3r4resume)r$r"r(r(r) test_resumesz"TestProcessObjectLeaks.test_resumecCs||jjdS)N)r3r4cwd)r$r(r(r)test_cwdszTestProcessObjectLeaks.test_cwdcCs||jjdS)N)r3r4 cpu_affinity)r$r(r(r)test_cpu_affinitysz(TestProcessObjectLeaks.test_cpu_affinitycs4tfddtfdddS)Ncs jS)N)r4rcr()affinityr$r(r)r@rAz>TestProcessObjectLeaks.test_cpu_affinity_set..csjdgS)NrH)r4rcr()r$r(r)r@rA)rBrcr3rM ValueError)r$r()rer$r)test_cpu_affinity_setsz,TestProcessObjectLeaks.test_cpu_affinity_setc Cs*ttd||jjWdQRXdS)Nw)openr r3r4Z open_files)r$r(r(r)test_open_filessz&TestProcessObjectLeaks.test_open_filescCs||jjdS)N)r3r4Z memory_maps)r$r(r(r)test_memory_mapssz'TestProcessObjectLeaks.test_memory_mapsz LINUX onlycsfdddS)NcsjtjS)N)r4rlimitrG RLIMIT_NOFILEr()r$r(r)r@rAz4TestProcessObjectLeaks.test_rlimit..)r3)r$r()r$r) test_rlimitsz"TestProcessObjectLeaks.test_rlimitcs<ttjfddttffdddS)NcsjtjS)N)r4rlrGrmr()limitr$r(r)r@rAz8TestProcessObjectLeaks.test_rlimit_set..cs jdS)NrH)r4rlr()r$r(r)r@rA)rBrlrGrmr3rMrNrf)r$r()ror$r)test_rlimit_sets z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc s6t&trdndfddWdQRXdS)NZinetallcs jS)N)r4Z connectionsr()kindr$r(r)r@rAz9TestProcessObjectLeaks.test_connections..)r rr3)r$r()rrr$r)test_connectionss z'TestProcessObjectLeaks.test_connectionscCs||jjdS)N)r3r4environ)r$r(r(r) test_environsz#TestProcessObjectLeaks.test_environcCs|dddS)NcSsttS)N)rJ proc_inforKrLr(r(r(r)r@rAz7TestProcessObjectLeaks.test_proc_info..)r3)r$r(r(r)test_proc_infosz%TestProcessObjectLeaks.test_proc_infoN)9__name__ __module__ __qualname____doc__rBr4r2r.r6r7r8r9rskipIfrr:r;r<r>rCrrErOrrPrRrSrrrTrrUrVrWrXrZrr[r\r]r^r`rbr rdrgrjrrkrrrnrprsrrurwr(r(r(r)r/Usd         r/cspeZdZdZefddZefddZddZerhdd 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 dS)N) r setUpClassrsubprGrQpidr4killwait)cls)r!r(r)r~s   z%TestTerminatedProcessLeaks.setUpClasscstt|jdS)N)r tearDownClassrr)r)r!r(r)rs z(TestTerminatedProcessLeaks.tearDownClasscCs&y |Wntjk r YnXdS)N)rGZ NoSuchProcess)r$r'r(r(r)calls zTestTerminatedProcessLeaks.callcCs||jjdS)N)r3r4r)r$r(r(r) test_kill'sz$TestTerminatedProcessLeaks.test_killcCs||jjdS)N)r3r4r)r$r(r(r)test_terminate*sz)TestTerminatedProcessLeaks.test_terminatecCs||jjdS)N)r3r4Zsuspend)r$r(r(r) test_suspend-sz'TestTerminatedProcessLeaks.test_suspendcCs||jjdS)N)r3r4r_)r$r(r(r)r`0sz&TestTerminatedProcessLeaks.test_resumecCs||jjdS)N)r3r4r)r$r(r(r) test_wait3sz$TestTerminatedProcessLeaks.test_waitcsfdd}|dS)Ncs(ytjjStk r"YnXdS)N)rJrvr4rr r()r$r(r)r8sz7TestTerminatedProcessLeaks.test_proc_info..call)r3)r$rr()r$r)rw6s z)TestTerminatedProcessLeaks.test_proc_info)rxryrzr{ classmethodr~rrrrrrr`rrw __classcell__r(r()r!r)r} sr}z WINDOWS onlyc@seZdZddZddZdS)TestProcessDualImplementationcCs|dddS)NcSstjtddS)NT)use_peb)rJ proc_cmdlinerKrLr(r(r(r)r@ErAzETestProcessDualImplementation.test_cmdline_peb_true..)r3)r$r(r(r)test_cmdline_peb_trueDsz3TestProcessDualImplementation.test_cmdline_peb_truecCs|dddS)NcSstjtddS)NF)r)rJrrKrLr(r(r(r)r@HrAzFTestProcessDualImplementation.test_cmdline_peb_false..)r3)r$r(r(r)test_cmdline_peb_falseGsz4TestProcessDualImplementation.test_cmdline_peb_falseN)rxryrzrrr(r(r(r)rAsrc@seZdZdZddZeddZeddZedd Zed d Z ed d Z ee e dddZe e dddZddZe edddZddZddZddZe eoejd d ed!d"Zed#d$Zee e dd%d&Zee eo*e d'kd(d)d*Z!d+d,Z"d-d.Z#ee e$ dd/d0Z%ee e& dd1d2Z'ee e( dd3d4Z)ed5d6Z*d7d8Z+erd9d:Z,d;d<Z-d=d>Z.d?d@Z/dAdBZ0dCS)DTestModuleFunctionsLeaksz&Test leaks of psutil module functions.cCst}|||jdS)N)rr0rq)r$r1r(r(r)r2Ssz&TestModuleFunctionsLeaks.test_coveragecCs|dddS)NcSs tjddS)NT)logical)rG cpu_countr(r(r(r)r@[rAz9TestModuleFunctionsLeaks.test_cpu_count..)r3)r$r(r(r)test_cpu_countYsz'TestModuleFunctionsLeaks.test_cpu_countcCs|dddS)NcSs tjddS)NF)r)rGrr(r(r(r)r@_rAzBTestModuleFunctionsLeaks.test_cpu_count_physical..)r3)r$r(r(r)test_cpu_count_physical]sz0TestModuleFunctionsLeaks.test_cpu_count_physicalcCs|tjdS)N)r3rGrY)r$r(r(r)rZasz'TestModuleFunctionsLeaks.test_cpu_timescCs|dddS)NcSs tjddS)NT)Zpercpu)rGrYr(r(r(r)r@grAz=TestModuleFunctionsLeaks.test_per_cpu_times..)r3)r$r(r(r)test_per_cpu_timesesz+TestModuleFunctionsLeaks.test_per_cpu_timescCs|tjdS)N)r3rGZ cpu_stats)r$r(r(r)test_cpu_statsisz'TestModuleFunctionsLeaks.test_cpu_statsz not supportedcCs|tjdS)N)r3rGZcpu_freq)r$r(r(r) test_cpu_freqmsz&TestModuleFunctionsLeaks.test_cpu_freqz WINDOWS onlycCst|tjdS)N)rG getloadavgr3)r$r(r(r)test_getloadavgrsz(TestModuleFunctionsLeaks.test_getloadavgcCs|tjdS)N)r3rGZvirtual_memory)r$r(r(r)test_virtual_memoryysz,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)cCs|tjdS)N)r3rGZ swap_memory)r$r(r(r)test_swap_memory}sz)TestModuleFunctionsLeaks.test_swap_memorycCs$trtn|j}|jdd|ddS)NcSsttS)N)rGZ pid_existsrKrLr(r(r(r)r@rAz:TestModuleFunctionsLeaks.test_pid_exists..)r")rr#r"r3)r$r"r(r(r)test_pid_existssz(TestModuleFunctionsLeaks.test_pid_existscCs$trtn|j}|jdd|ddS)NcSs tdS)N.)rGZ disk_usager(r(r(r)r@rAz:TestModuleFunctionsLeaks.test_disk_usage..)r")rr#r"r3)r$r"r(r(r)test_disk_usagesz(TestModuleFunctionsLeaks.test_disk_usagecCs|tjdS)N)r3rGZdisk_partitions)r$r(r(r)test_disk_partitionssz-TestModuleFunctionsLeaks.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this Linux versioncCs|dddS)NcSs tjddS)NF)nowrap)rGZdisk_io_countersr(r(r(r)r@rAz@TestModuleFunctionsLeaks.test_disk_io_counters..)r3)r$r(r(r)test_disk_io_counterssz.TestModuleFunctionsLeaks.test_disk_io_counterscCs|tjdS)N)r3rGZpids)r$r(r(r) test_pidssz"TestModuleFunctionsLeaks.test_pidscCs|dddS)NcSs tjddS)NF)r)rGZnet_io_countersr(r(r(r)r@rAz?TestModuleFunctionsLeaks.test_net_io_counters..)r3)r$r(r(r)test_net_io_counterssz-TestModuleFunctionsLeaks.test_net_io_countersrzneed root accessc Cs0tjddt|ddWdQRXdS)Nrq)rrcSs tjddS)Nrq)rr)rGnet_connectionsr(r(r(r)r@rAz?TestModuleFunctionsLeaks.test_net_connections..)rGrr r3)r$r(r(r)test_net_connectionss z-TestModuleFunctionsLeaks.test_net_connectionscCs"trdn|j}|jtj|ddS)Ni@) tolerance)rrr3rGZ net_if_addrs)r$rr(r(r)test_net_if_addrssz*TestModuleFunctionsLeaks.test_net_if_addrscCs|tjdS)N)r3rGZ net_if_stats)r$r(r(r)test_net_if_statssz*TestModuleFunctionsLeaks.test_net_if_statscCs|tjdS)N)r3rGZsensors_battery)r$r(r(r)test_sensors_batterysz-TestModuleFunctionsLeaks.test_sensors_batterycCs|tjdS)N)r3rGZsensors_temperatures)r$r(r(r)test_sensors_temperaturessz2TestModuleFunctionsLeaks.test_sensors_temperaturescCs|tjdS)N)r3rGZ sensors_fans)r$r(r(r)test_sensors_fanssz*TestModuleFunctionsLeaks.test_sensors_fanscCs|tjdS)N)r3rGZ boot_time)r$r(r(r)test_boot_timesz'TestModuleFunctionsLeaks.test_boot_timecCs|tjdS)N)r3rGZusers)r$r(r(r) test_userssz#TestModuleFunctionsLeaks.test_userscCs|tjdS)N)r3rJZwinservice_enumerate)r$r(r(r)test_win_service_itersz.TestModuleFunctionsLeaks.test_win_service_itercCsdS)Nr()r$r(r(r)test_win_service_getsz-TestModuleFunctionsLeaks.test_win_service_getcs&tt|fdddS)Ncs tS)N)rJZwinservice_query_configr()r5r(r)r@rAzFTestModuleFunctionsLeaks.test_win_service_get_config..)nextrGwin_service_iterr5r3)r$r()r5r)test_win_service_get_configsz4TestModuleFunctionsLeaks.test_win_service_get_configcs&tt|fdddS)Ncs tS)N)rJZwinservice_query_statusr()r5r(r)r@rAzFTestModuleFunctionsLeaks.test_win_service_get_status..)rrGrr5r3)r$r()r5r)test_win_service_get_statussz4TestModuleFunctionsLeaks.test_win_service_get_statuscs&tt|fdddS)Ncs tS)N)rJZwinservice_query_descrr()r5r(r)r@rAzKTestModuleFunctionsLeaks.test_win_service_get_description..)rrGrr5r3)r$r()r5r) test_win_service_get_descriptionsz9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)1rxryrzr{r2r.rrrZrrrr|rrrrrrrrrrrrKpathexistsrrrrrgetuidrrrrrrrrrrrrrrrrr(r(r(r)rPsN$r__main__) run_from_name)5r{Z __future__rr+rKrGZpsutil._commonrrrrrrZpsutil._compatr r Z psutil.testsr r r rrrrrrrrrrrrrrrrrrZ _psplatformrJrQrBr#r.r/r}r|rrrxZpsutil.tests.runnerr__file__r(r(r(r)s`                              76