3 ̪e:@sdZddlmZddlZddlZddlZddlZddlZddlZddlm Z ddlm Z ddlm Z ddlm Z ddlm Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ej'j(Z(ej)Z*d Z+d!d"Z,Gd#d$d$eZ-Gd%d&d&e-Z.ej/e d'Gd(d)d)eZ0Gd*d+d+eZ1e2d,krdd-l3m4Z4e4e5dS).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)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. cstjfdd}|S)Nc sFtr2|jj}zt|j_|f||S||j_Xn|f||SdS)N)r __class__times FEW_TIMES)selfargskwargsbefore)funF/usr/local/lib64/python3.6/site-packages/psutil/tests/test_memleaks.pywrapperCs z5fewtimes_if_linux..decorator..wrapper) functoolswraps)r'r*r()r'r) decoratorBs z$fewtimes_if_linux..decoratorr()r-r(r(r)fewtimes_if_linux=sr.c@seZdZdZeZddZeddZeddZ edd Z ed d Z e j e d ed dZe j e d eddZeddZddZddZe j e dddZe j e dddZe j e deddZe j eddd Zed!d"Zeeed#d$d%Ze j e d&d'd(Ze j e d ed)d*Z ed+d,Z!eeed#d-d.Z"ed/d0Z#ee j e$ dd1d2Z%ed3d4Z&ed5d6Z'e j e d ed7d8Z(d9d:Z)ed;d<Z*e j e+ dd=d>Z,e j e+ dd?d@Z-edAdBZ.e j e/ dedCdDZ0e j e1 dEe j e2 ddFdGZ3e j e1 dEe j e2 ddHdIZ4ee j edJdKdLZ5e j e6 ddMdNZ7e j e d&dOdPZ8dQS)RTestProcessObjectLeaksz$Test leaks of Process class methods.cCs td}|j||j|jdS)N)rtest_class_coverageZgettersZsetters)r#nsr(r(r) test_coverage^sz$TestProcessObjectLeaks.test_coveragecCs|j|jjdS)N)executeprocname)r#r(r(r) test_namebsz TestProcessObjectLeaks.test_namecCs|j|jjdS)N)r3r4cmdline)r#r(r(r) test_cmdlinefsz#TestProcessObjectLeaks.test_cmdlinecCs|j|jjdS)N)r3r4Zexe)r#r(r(r)test_exejszTestProcessObjectLeaks.test_execCs|j|jjdS)N)r3r4ppid)r#r(r(r) test_ppidnsz TestProcessObjectLeaks.test_ppidz POSIX onlycCs|j|jjdS)N)r3r4Zuids)r#r(r(r) test_uidsrsz TestProcessObjectLeaks.test_uidscCs|j|jjdS)N)r3r4Zgids)r#r(r(r) test_gidswsz TestProcessObjectLeaks.test_gidscCs|j|jjdS)N)r3r4status)r#r(r(r) test_status|sz"TestProcessObjectLeaks.test_statuscCs|j|jjdS)N)r3r4nice)r#r(r(r) test_nicesz TestProcessObjectLeaks.test_nicecs tjjfdddS)Ncs jjS)N)r4r@r()nicenessr#r(r)sz6TestProcessObjectLeaks.test_nice_set..)thisprocr@r3)r#r()rBr#r) test_nice_setsz$TestProcessObjectLeaks.test_nice_setz not supportedcCs|j|jjdS)N)r3r4ionice)r#r(r(r) test_ionicesz"TestProcessObjectLeaks.test_ionicecsZtr"tjjfddn4jfddtjtjtj dd}j t |dS)Ncs jjS)N)r4rFr()r#valuer(r)rCsz8TestProcessObjectLeaks.test_ionice_set..csjjtjS)N)r4rFpsutilZIOPRIO_CLASS_NONEr()r#r(r)rCsr) rrDrFr3r+partialcextZproc_ioprio_setosgetpid execute_w_excOSError)r#r'r()r#rHr)test_ionice_sets z&TestProcessObjectLeaks.test_ionice_setcCs|j|jjdS)N)r3r4Z io_counters)r#r(r(r)test_io_counterssz'TestProcessObjectLeaks.test_io_counterszworthless on POSIXcCstjj|j|jjdS)N)rIProcessusernamer3r4)r#r(r(r) test_usernames z$TestProcessObjectLeaks.test_usernamecCs|j|jjdS)N)r3r4Z create_time)r#r(r(r)test_create_timesz'TestProcessObjectLeaks.test_create_time)Zonly_ifcCs|j|jjdS)N)r3r4 num_threads)r#r(r(r)test_num_threadssz'TestProcessObjectLeaks.test_num_threadsz WINDOWS onlycCs|j|jjdS)N)r3r4Z num_handles)r#r(r(r)test_num_handlessz'TestProcessObjectLeaks.test_num_handlescCs|j|jjdS)N)r3r4Znum_fds)r#r(r(r) test_num_fdssz#TestProcessObjectLeaks.test_num_fdscCs|j|jjdS)N)r3r4Znum_ctx_switches)r#r(r(r)test_num_ctx_switchessz,TestProcessObjectLeaks.test_num_ctx_switchescCs|j|jjdS)N)r3r4threads)r#r(r(r) test_threadssz#TestProcessObjectLeaks.test_threadscCs|j|jjdS)N)r3r4 cpu_times)r#r(r(r)test_cpu_timessz%TestProcessObjectLeaks.test_cpu_timescCs|j|jjdS)N)r3r4Zcpu_num)r#r(r(r) test_cpu_numsz#TestProcessObjectLeaks.test_cpu_numcCs|j|jjdS)N)r3r4Z memory_info)r#r(r(r)test_memory_infosz'TestProcessObjectLeaks.test_memory_infocCs|j|jjdS)N)r3r4Zmemory_full_info)r#r(r(r)test_memory_full_infosz,TestProcessObjectLeaks.test_memory_full_infocCs|j|jjdS)N)r3r4terminal)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|j|jjdS)N)r3r4cwd)r#r(r(r)test_cwdszTestProcessObjectLeaks.test_cwdcCs|j|jjdS)N)r3r4 cpu_affinity)r#r(r(r)test_cpu_affinitysz(TestProcessObjectLeaks.test_cpu_affinitycs4tjjfddjtfdddS)Ncs jjS)N)r4rjr()affinityr#r(r)rCsz>TestProcessObjectLeaks.test_cpu_affinity_set..csjjdgS)NrJrK)r4rjr()r#r(r)rCs)rDrjr3rP ValueError)r#r()rlr#r)test_cpu_affinity_setsz,TestProcessObjectLeaks.test_cpu_affinity_setc Cs*ttd|j|jjWdQRXdS)Nw)openrr3r4Z open_files)r#r(r(r)test_open_filessz&TestProcessObjectLeaks.test_open_filescCs|j|jjdS)N)r3r4Z memory_maps)r#r(r(r)test_memory_mapssz'TestProcessObjectLeaks.test_memory_mapsz LINUX onlycsjfdddS)NcsjjtjS)N)r4rlimitrI RLIMIT_NOFILEr()r#r(r)rCsz4TestProcessObjectLeaks.test_rlimit..)r3)r#r()r#r) test_rlimitsz"TestProcessObjectLeaks.test_rlimitcs<tjtjjfddjttffdddS)NcsjjtjS)N)r4rsrIrtr()limitr#r(r)rCsz8TestProcessObjectLeaks.test_rlimit_set..cs jjdS)NrJrK)r4rsr()r#r(r)rCs)rDrsrIrtr3rPrQrm)r#r()rvr#r)test_rlimit_sets z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc s6t&trdndjfddWdQRXdS)Ninetallcs jjS)N)r4Z connectionsr()kindr#r(r)rCsz9TestProcessObjectLeaks.test_connections..)rrr3)r#r()rzr#r)test_connectionss z'TestProcessObjectLeaks.test_connectionscCs|j|jjdS)N)r3r4environ)r#r(r(r) test_environsz#TestProcessObjectLeaks.test_environcCs|jdddS)NcSstjtjS)N)rM proc_inforNrOr(r(r(r)rC sz7TestProcessObjectLeaks.test_proc_info..)r3)r#r(r(r)test_proc_info sz%TestProcessObjectLeaks.test_proc_infoN)9__name__ __module__ __qualname____doc__rDr4r2r.r6r8r9r;unittestskipIfrr<r=r?rArErrGrRrrSrVrWrrrYrrZr[r\r^r`rrarbrcrergrir rkrnrqrrrrrrurwr{r r}rr(r(r(r)r/Ysd         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:tjt|_tj|jj|_|jj|jj dS)N) r setUpClassrsubprIrTpidr4killwait)cls)r r(r)rs   z%TestTerminatedProcessLeaks.setUpClasscstjt|jdS)N)r tearDownClassrr)r)r r(r)rs z(TestTerminatedProcessLeaks.tearDownClassc Cs&y |Wntjk r YnXdS)N)rI NoSuchProcess)r#r'r(r(r)call"s zTestTerminatedProcessLeaks.callcCs|j|jjdS)N)r3r4r)r#r(r(r) test_kill*sz$TestTerminatedProcessLeaks.test_killcCs|j|jjdS)N)r3r4r)r#r(r(r)test_terminate-sz)TestTerminatedProcessLeaks.test_terminatecCs|j|jjdS)N)r3r4Zsuspend)r#r(r(r) test_suspend0sz'TestTerminatedProcessLeaks.test_suspendcCs|j|jjdS)N)r3r4rf)r#r(r(r)rg3sz&TestTerminatedProcessLeaks.test_resumecCs|j|jjdS)N)r3r4r)r#r(r(r) test_wait6sz$TestTerminatedProcessLeaks.test_waitcsfdd}j|dS)Nc s(ytjjjStk r"YnXdS)N)rMr~r4rr r()r#r(r)r;sz7TestTerminatedProcessLeaks.test_proc_info..call)r3)r#rr()r#r)r9s z)TestTerminatedProcessLeaks.test_proc_info)rrrr classmethodrrrrrrrrgrr __classcell__r(r()r r)rsrz WINDOWS onlyc@seZdZddZddZdS)TestProcessDualImplementationcCs|jdddS)NcSstjtjddS)NT)use_peb)rM proc_cmdlinerNrOr(r(r(r)rCGszETestProcessDualImplementation.test_cmdline_peb_true..)r3)r#r(r(r)test_cmdline_peb_trueFsz3TestProcessDualImplementation.test_cmdline_peb_truecCs|jdddS)NcSstjtjddS)NF)r)rMrrNrOr(r(r(r)rCJszFTestProcessDualImplementation.test_cmdline_peb_false..)r3)r#r(r(r)test_cmdline_peb_falseIsz4TestProcessDualImplementation.test_cmdline_peb_falseN)rrrrrr(r(r(r)rDsrc@seZdZdZddZeddZeddZedd Zed d Z ed d Z ee j e opejdkde j e dddZe j e dddZddZe j edddZddZddZdd Ze j eoejjd! d"ed#d$Zed%d&Zee j e dd'd(Z!ee j e oBej"d)kd*d+d,Z#d-d.Z$d/d0Z%ee j e& dd1d2Z'ee j e( dd3d4Z)ee j e* dd5d6Z+ed7d8Z,d9d:Z-d;d<Z.erd=d>Z/d?d@Z0dAdBZ1dCdDZ2dEdFZ3dGS)HTestModuleFunctionsLeaksz&Test leaks of psutil module functions.cCst}|j||jdS)N)rr0ry)r#r1r(r(r)r2Usz&TestModuleFunctionsLeaks.test_coveragecCs|jdddS)NcSs tjddS)NT)logical)rI cpu_countr(r(r(r)rC]sz9TestModuleFunctionsLeaks.test_cpu_count..)r3)r#r(r(r)test_cpu_count[sz'TestModuleFunctionsLeaks.test_cpu_countcCs|jdddS)NcSs tjddS)NF)r)rIrr(r(r(r)rCasz?TestModuleFunctionsLeaks.test_cpu_count_cores..)r3)r#r(r(r)test_cpu_count_cores_sz-TestModuleFunctionsLeaks.test_cpu_count_corescCs|jtjdS)N)r3rIr_)r#r(r(r)r`csz'TestModuleFunctionsLeaks.test_cpu_timescCs|jdddS)NcSs tjddS)NT)Zpercpu)rIr_r(r(r(r)rCisz=TestModuleFunctionsLeaks.test_per_cpu_times..)r3)r#r(r(r)test_per_cpu_timesgsz+TestModuleFunctionsLeaks.test_per_cpu_timescCs|jtjdS)N)r3rIZ cpu_stats)r#r(r(r)test_cpu_statsksz'TestModuleFunctionsLeaks.test_cpu_statsarm64zskipped due to #1892z not supportedcCs|jtjdS)N)r3rIZcpu_freq)r#r(r(r) test_cpu_freqosz&TestModuleFunctionsLeaks.test_cpu_freqz WINDOWS onlycCstj|jtjdS)N)rI getloadavgr3)r#r(r(r)test_getloadavgxsz(TestModuleFunctionsLeaks.test_getloadavgcCs|jtjdS)N)r3rIZvirtual_memory)r#r(r(r)test_virtual_memorysz,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)cCs|jtjdS)N)r3rIZ swap_memory)r#r(r(r)test_swap_memorysz)TestModuleFunctionsLeaks.test_swap_memorycCs$trtn|j}|jdd|ddS)NcSstjtjS)N)rIZ pid_existsrNrOr(r(r(r)rCsz: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 tjdS)N.)rI disk_usager(r(r(r)rCsz:TestModuleFunctionsLeaks.test_disk_usage..)r!)rr"r!r3)r#r!r(r(r)test_disk_usagesz(TestModuleFunctionsLeaks.test_disk_usagecCs|jtjdS)N)r3rIZdisk_partitions)r#r(r(r)test_disk_partitionssz-TestModuleFunctionsLeaks.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this Linux versioncCs|jdddS)NcSs tjddS)NF)nowrap)rIZdisk_io_countersr(r(r(r)rCsz@TestModuleFunctionsLeaks.test_disk_io_counters..)r3)r#r(r(r)test_disk_io_counterssz.TestModuleFunctionsLeaks.test_disk_io_counterscCs|jtjdS)N)r3rIZpids)r#r(r(r) test_pidssz"TestModuleFunctionsLeaks.test_pidscCs|jdddS)NcSs tjddS)NF)r)rIZnet_io_countersr(r(r(r)rCsz?TestModuleFunctionsLeaks.test_net_io_counters..)r3)r#r(r(r)test_net_io_counterssz-TestModuleFunctionsLeaks.test_net_io_countersrzneed root accessc Cs0tjddt|jddWdQRXdS)Nry)rzcSs tjddS)Nry)rz)rInet_connectionsr(r(r(r)rCsz?TestModuleFunctionsLeaks.test_net_connections..)rIrrr3)r#r(r(r)test_net_connectionss z-TestModuleFunctionsLeaks.test_net_connectionscCs"trdn|j}|jtj|ddS)NPi) tolerancei@)rrr3rIZ net_if_addrs)r#rr(r(r)test_net_if_addrssz*TestModuleFunctionsLeaks.test_net_if_addrscCs|jtjdS)N)r3rIZ net_if_stats)r#r(r(r)test_net_if_statssz*TestModuleFunctionsLeaks.test_net_if_statscCs|jtjdS)N)r3rIZsensors_battery)r#r(r(r)test_sensors_batterysz-TestModuleFunctionsLeaks.test_sensors_batterycCs|jtjdS)N)r3rIZsensors_temperatures)r#r(r(r)test_sensors_temperaturessz2TestModuleFunctionsLeaks.test_sensors_temperaturescCs|jtjdS)N)r3rIZ sensors_fans)r#r(r(r)test_sensors_fanssz*TestModuleFunctionsLeaks.test_sensors_fanscCs|jtjdS)N)r3rIZ boot_time)r#r(r(r)test_boot_timesz'TestModuleFunctionsLeaks.test_boot_timecCs|jtjdS)N)r3rIZusers)r#r(r(r) test_userssz#TestModuleFunctionsLeaks.test_userscCs|jdddS)NcSs tjdS)NF)rIZ _set_debugr(r(r(r)rCsz9TestModuleFunctionsLeaks.test_set_debug..)r3)r#r(r(r)test_set_debugsz'TestModuleFunctionsLeaks.test_set_debugcCs|jtjdS)N)r3rMZwinservice_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&ttjj|jfdddS)Ncs tjS)N)rMZwinservice_query_configr()r5r(r)rCszFTestModuleFunctionsLeaks.test_win_service_get_config..)nextrIwin_service_iterr5r3)r#r()r5r)test_win_service_get_configsz4TestModuleFunctionsLeaks.test_win_service_get_configcs&ttjj|jfdddS)Ncs tjS)N)rMZwinservice_query_statusr()r5r(r)rCszFTestModuleFunctionsLeaks.test_win_service_get_status..)rrIrr5r3)r#r()r5r)test_win_service_get_statussz4TestModuleFunctionsLeaks.test_win_service_get_statuscs&ttjj|jfdddS)Ncs tjS)N)rMZwinservice_query_descrr()r5r(r)rCszKTestModuleFunctionsLeaks.test_win_service_get_description..)rrIrr5r3)r#r()r5r) test_win_service_get_descriptionsz9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)4rrrrr2r.rrr`rrrrrplatformmachiner rrrrrrrrrrrNpathexistsrrrrgetuidrrrrrrrrrrrrrrrrrr(r(r(r)rRsV$r__main__) run_from_name)6r __future__rr+rNrrrIZpsutil._commonrrrrrrZpsutil._compatr r Z psutil.testsr r r rrrrrrrrrrrrrrrrrZ _psplatformrMrTrDr"r.r/rrrrrZpsutil.tests.runnerr__file__r(r(r(r)sb                             66