3 JZ=G@s<dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddl m Z ddl mZddl mZddl mZdd l mZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(dZ)d Z*d!Z+e'rd"nd#Z,e j-j.Z.e j/Z0e'rd"nd#Z,d$d%Z1d&d'Z2Gd(d)d)e(j3Z4Gd*d+d+e4Z5Gd,d-d-e5Z6Gd.d/d/e4Z7e8d0kr8e#e9dS)1a) 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). )print_functionN)LINUX)OPENBSD)OSX)POSIX)SUNOS)WINDOWS)xrange)create_sockets)get_test_subprocess)HAS_CPU_AFFINITY) HAS_CPU_FREQ) HAS_ENVIRON) HAS_IONICE)HAS_MEMORY_MAPS)HAS_PROC_CPU_NUM)HAS_PROC_IO_COUNTERS) HAS_RLIMIT)HAS_SENSORS_BATTERY)HAS_SENSORS_FANS)HAS_SENSORS_TEMPERATURES) reap_children)run_test_module_by_name) safe_rmpath)skip_on_access_denied)TESTFN)TRAVIS)unittestiiTFcCstjto tdS)Nz worthless on LINUX (pure python))rskipIfrSKIP_PYTHON_IMPLr!r!)/usr/lib64/python3.6/test_memory_leaks.py skip_if_linuxFs r#cCstd }i}x(t|D]\}}d |d d >||<qWx8t|D],}|||kr>> bytes2human(10000) '9.8K' >>> bytes2human(100001221) '95.4M' KMGTPEZY z%.2f%sz%sB)r$r%r&r'r(r)r*r+) enumeratereversedfloat)nZsymbolsprefixisvaluer!r!r" bytes2humanKs r6c@sLeZdZdZeZeZeZ ddZ ddZ ddZ e dd Ze d d Zd S) TestMemLeakzBase framework class which calls a function many times and produces a failure if process memory usage keeps increasing between calls or over time. cCs tjdS)N)gccollect)selfr!r!r"setUpgszTestMemLeak.setUpcs~fdd}jddp$j}jddp6jjddpHj}x"tdD]}jfqTWjtjgjt j djt j g|j }|j } | |} | |krzd } tj|} x,tj| krjf| d7} qW~ tjj } | | }| | krzt| |}td |tjd d }|d 7}|t| t|| |f}j|dS)zTest a callable.cs2x"tD]}jfq W~tjdS)N)r _callr8r9)x)argsfunkwargsloopsr:r!r"call_many_timeslsz,TestMemLeak.execute..call_many_times tolerance_NZloops_Z retry_for_r-r,rzexta proc mem: %s)filez0+%s after %s calls, +%s after another %s calls, z+%s extra proc mem)pop tolerancerA retry_forranger<Z assertEqualr8Zgarbage threadingZ active_countthisprocchildren_get_memtimer9r6printsysstderrZfail)r:r?r>r@rBrFrGr=Zmem1Zmem2Zdiff1ZncallsZstop_atZmem3Zdiff2Zextra_proc_memmsgr!)r>r?r@rAr:r"executejsB     zTestMemLeak.executecs"fdd}j|dS)zRConvenience function which tests a callable raising an exception. csjfdS)N)Z assertRaisesr!)r>excr?r@r:r!r"callsz'TestMemLeak.execute_w_exc..callN)rR)r:rSr?r>r@rTr!)r>rSr?r@r:r" execute_w_excszTestMemLeak.execute_w_exccCs$ts ts trtjjStjjSdS)N)rrrrJmemory_full_infoZuss memory_infoZrssr!r!r!r"rLs  zTestMemLeak._get_memcOs|||dS)Nr!)r?r>r@r!r!r"r<szTestMemLeak._callN)__name__ __module__ __qualname____doc__MEMORY_TOLERANCErFLOOPSrA RETRY_FORrGr;rRrU staticmethodrLr<r!r!r!r"r7^s; r7c@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(e j eoe)d9d:d;Z*edd?Z-e j e, dd@dAZ.edBdCZ/e j e0dDe j e1 dedEdFZ2e j e3 dGe j e4 ddHdIZ5e j e3 dGe j e4 ddJdKZ6ee j edLdMdNZ7e j e8 ddOdPZ9e j e d&dQdRZ:dSS)TTestProcessObjectLeaksz$Test leaks of Process class methods.cCsNtd}x@ttjD]2}|jdr$q||kr.q|jt|d||dqWdS)Npidas_dictrK cpu_affinity cpu_percentionice is_runningkillmemory_info_exmemory_percentniceoneshotparentrlimit send_signalsuspend terminatewait_test_)rQ)rarbrKrcrdrerfrgrhrirjrkrlrmrnrorprq)setdirpsutilProcess startswith assertTruehasattr)r:skipnamer!r!r" test_coverages z$TestProcessObjectLeaks.test_coveragecCs|j|jjdS)N)rRprocr|)r:r!r!r" test_namesz TestProcessObjectLeaks.test_namecCs|j|jjdS)N)rRr~Zcmdline)r:r!r!r" test_cmdlinesz#TestProcessObjectLeaks.test_cmdlinecCs|j|jjdS)N)rRr~Zexe)r:r!r!r"test_exeszTestProcessObjectLeaks.test_execCs|j|jjdS)N)rRr~Zppid)r:r!r!r" test_ppidsz TestProcessObjectLeaks.test_ppidz POSIX onlycCs|j|jjdS)N)rRr~Zuids)r:r!r!r" test_uidssz TestProcessObjectLeaks.test_uidscCs|j|jjdS)N)rRr~Zgids)r:r!r!r" test_gidssz TestProcessObjectLeaks.test_gidscCs|j|jjdS)N)rRr~Zstatus)r:r!r!r" test_statussz"TestProcessObjectLeaks.test_statuscCs|j|jjdS)N)rRr~rj)r:r!r!r" test_nice_getsz$TestProcessObjectLeaks.test_nice_getcCstj}|j|jj|dS)N)rJrjrRr~)r:Znicenessr!r!r" test_nice_setsz$TestProcessObjectLeaks.test_nice_setz not supportedcCs|j|jjdS)N)rRr~re)r:r!r!r"test_ionice_getsz&TestProcessObjectLeaks.test_ionice_getcCsVtrtj}|j|jj|n4|j|jjtjtjt j t j dd}|j t|dS)Nr,r)rrJrerRr~rvZIOPRIO_CLASS_NONE functoolspartialcextZproc_ioprio_setosgetpidrUOSError)r:r5r?r!r!r"test_ionice_sets z&TestProcessObjectLeaks.test_ionice_setcCs|j|jjdS)N)rRr~Z io_counters)r:r!r!r"test_io_counterssz'TestProcessObjectLeaks.test_io_counterszworthless on POSIXcCs|j|jjdS)N)rRr~Zusername)r:r!r!r" test_username sz$TestProcessObjectLeaks.test_usernamecCs|j|jjdS)N)rRr~Z create_time)r:r!r!r"test_create_timesz'TestProcessObjectLeaks.test_create_time)Zonly_ifcCs|j|jjdS)N)rRr~Z num_threads)r:r!r!r"test_num_threadssz'TestProcessObjectLeaks.test_num_threadsz WINDOWS onlycCs|j|jjdS)N)rRr~Z num_handles)r:r!r!r"test_num_handlessz'TestProcessObjectLeaks.test_num_handlescCs|j|jjdS)N)rRr~Znum_fds)r:r!r!r" test_num_fdssz#TestProcessObjectLeaks.test_num_fdscCs|j|jjdS)N)rRr~Znum_ctx_switches)r:r!r!r"test_num_ctx_switches!sz,TestProcessObjectLeaks.test_num_ctx_switchescCs|j|jjdS)N)rRr~Zthreads)r:r!r!r" test_threads%sz#TestProcessObjectLeaks.test_threadscCs|j|jjdS)N)rRr~ cpu_times)r:r!r!r"test_cpu_times*sz%TestProcessObjectLeaks.test_cpu_timescCs|j|jjdS)N)rRr~Zcpu_num)r:r!r!r" test_cpu_num.sz#TestProcessObjectLeaks.test_cpu_numcCs|j|jjdS)N)rRr~rW)r:r!r!r"test_memory_info3sz'TestProcessObjectLeaks.test_memory_infocCs|j|jjdS)N)rRr~rV)r:r!r!r"test_memory_full_info7sz,TestProcessObjectLeaks.test_memory_full_infocCs|j|jjdS)N)rRr~Zterminal)r:r!r!r" test_terminal;sz$TestProcessObjectLeaks.test_terminalz worthless on POSIX (pure python)cCs|j|jjdS)N)rRr~resume)r:r!r!r" test_resume@sz"TestProcessObjectLeaks.test_resumecCs|j|jjdS)N)rRr~cwd)r:r!r!r"test_cwdEszTestProcessObjectLeaks.test_cwdcCs|j|jjdS)N)rRr~rc)r:r!r!r"test_cpu_affinity_getIsz,TestProcessObjectLeaks.test_cpu_affinity_getcCs4tj}|j|jj|ts0|jt|jjdgdS)Nr,r)rJrcrRr~rrU ValueError)r:Zaffinityr!r!r"test_cpu_affinity_setMsz,TestProcessObjectLeaks.test_cpu_affinity_setc Cs0ttttd|j|jjWdQRXdS)Nw)rropenrRr~Z open_files)r:r!r!r"test_open_filesTs z&TestProcessObjectLeaks.test_open_filesztoo slow on OSXcCs|j|jjdS)N)rRr~Z memory_maps)r:r!r!r"test_memory_maps[sz'TestProcessObjectLeaks.test_memory_mapsz LINUX onlycCs|j|jjtjdS)N)rRr~rmrv RLIMIT_NOFILE)r:r!r!r"test_rlimit_getasz&TestProcessObjectLeaks.test_rlimit_getcCs6tjtj}|j|jjtj||jt|jjddS)Nr,r)rJrmrvrrRr~rUr)r:limitr!r!r"test_rlimit_setfs z&TestProcessObjectLeaks.test_rlimit_setzworthless on WINDOWSc Cs2t"trdnd}|j|jj|WdQRXdS)NZinetall)r rrRr~Z connections)r:Zkindr!r!r"test_connectionsms z'TestProcessObjectLeaks.test_connectionscCs|j|jjdS)N)rRr~environ)r:r!r!r" test_environysz#TestProcessObjectLeaks.test_environcCs|jtjtjdS)N)rRr proc_inforr)r:r!r!r"test_proc_info}sz%TestProcessObjectLeaks.test_proc_infoN);rXrYrZr[rJr~r}r#rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrr rrrrrrrrrrrrrrr!r!r!r"r`sh            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|jt}tj|j|_|jj|jj dS)N) superr setUpClassr rvrwrar~rgrq)clsp) __class__r!r"rs  z%TestTerminatedProcessLeaks.setUpClasscstt|jtdS)N)rr tearDownClassr)r)rr!r"rsz(TestTerminatedProcessLeaks.tearDownClassc Os*y|||Wntjk r$YnXdS)N)rvZ NoSuchProcess)r:r?r>r@r!r!r"r<sz TestTerminatedProcessLeaks._callcCs|j|jjdS)N)rRr~rg)r:r!r!r" test_killsz$TestTerminatedProcessLeaks.test_killcCs|j|jjdS)N)rRr~rp)r:r!r!r"test_terminatesz)TestTerminatedProcessLeaks.test_terminatecCs|j|jjdS)N)rRr~ro)r:r!r!r" test_suspendsz'TestTerminatedProcessLeaks.test_suspendcCs|j|jjdS)N)rRr~r)r:r!r!r"rsz&TestTerminatedProcessLeaks.test_resumecCs|j|jjdS)N)rRr~rq)r:r!r!r" test_waitsz$TestTerminatedProcessLeaks.test_waitcsfdd}j|dS)NcsDytjjjStk r>}z|jtjkr.WYdd}~XnXdS)N)rrr~rarerrnoZESRCH)err)r:r!r"rTs  z7TestTerminatedProcessLeaks.test_proc_info..call)rR)r:rTr!)r:r"rs z)TestTerminatedProcessLeaks.test_proc_info)rXrYrZr[ classmethodrrr<rrrrrrr __classcell__r!r!)rr"rsrc@seZdZdZddZeddZeddZedd Zed d Z d d Z ee j e dddZddZe j edddZe j eoedddZe j eoedddZddZe j eoejjd dedd Zed!d"Zed#d$Ze j ed%e j eo&ejd&kd'd(d)Z d*d+Z!e j e"d,d-d.Z#ee j e$ dd/d0Z%ee j e& dd1d2Z'ee j e( dd3d4Z)ed5d6Z*e j e+d7d8d9Z,e+rd:d;Z-dd?Z/d@dAZ0dBdCZ1dDS)ETestModuleFunctionsLeaksz&Test leaks of psutil module functions.cCsHtd }x:tjD]0}|jsq||kr(q|jt|d||d qWdS) N version_info __version__ process_iter wait_procsrdcpu_times_percent cpu_countrs)rQ)rrrrrdrr)rtrv__all__islowerryrz)r:r{r|r!r!r"r}s z&TestModuleFunctionsLeaks.test_coveragecCs|jtjdddS)NT)logical)rRrvr)r:r!r!r"test_cpu_count_logicalsz/TestModuleFunctionsLeaks.test_cpu_count_logicalcCs|jtjdddS)NF)r)rRrvr)r:r!r!r"test_cpu_count_physicalsz0TestModuleFunctionsLeaks.test_cpu_count_physicalcCs|jtjdS)N)rRrvr)r:r!r!r"rsz'TestModuleFunctionsLeaks.test_cpu_timescCs|jtjdddS)NT)Zpercpu)rRrvr)r:r!r!r"test_per_cpu_timessz+TestModuleFunctionsLeaks.test_per_cpu_timescCs|jtjdS)N)rRrvZ cpu_stats)r:r!r!r"test_cpu_statssz'TestModuleFunctionsLeaks.test_cpu_statsz not supportedcCs|jtjdS)N)rRrvZcpu_freq)r:r!r!r" test_cpu_freqsz&TestModuleFunctionsLeaks.test_cpu_freqcCs|jtjdS)N)rRrvZvirtual_memory)r:r!r!r"test_virtual_memorysz,TestModuleFunctionsLeaks.test_virtual_memoryz&worthless on SUNOS (uses a subprocess)cCs|jtjdS)N)rRrvZ swap_memory)r:r!r!r"test_swap_memorysz)TestModuleFunctionsLeaks.test_swap_memoryz worthless on POSIX (pure python)cCs|jtjtjdS)N)rRrvZ pid_existsrr)r:r!r!r"test_pid_existssz(TestModuleFunctionsLeaks.test_pid_existscCs|jtjddS)N.)rRrvZ disk_usage)r:r!r!r"test_disk_usagesz(TestModuleFunctionsLeaks.test_disk_usagecCs|jtjdS)N)rRrvZdisk_partitions)r:r!r!r"test_disk_partitionssz-TestModuleFunctionsLeaks.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this Linux versioncCs|jtjdddS)NF)nowrap)rRrvZdisk_io_counters)r:r!r!r"test_disk_io_counterssz.TestModuleFunctionsLeaks.test_disk_io_counterscCs|jtjdS)N)rRrvZpids)r:r!r!r" test_pidssz"TestModuleFunctionsLeaks.test_pidscCs|jtjdddS)NF)r)rRrvZnet_io_counters)r:r!r!r"test_net_io_counterssz-TestModuleFunctionsLeaks.test_net_io_countersz worthless on Linux (pure python)rzneed root accessc Cs"t|jtjWdQRXdS)N)r rRrvZnet_connections)r:r!r!r"test_net_connectionssz-TestModuleFunctionsLeaks.test_net_connectionscCs|jtjtrdndddS)NPi)rCi@)rRrvZ net_if_addrsr)r:r!r!r"test_net_if_addrssz*TestModuleFunctionsLeaks.test_net_if_addrszEPERM on traviscCs|jtjdS)N)rRrvZ net_if_stats)r:r!r!r"test_net_if_statssz*TestModuleFunctionsLeaks.test_net_if_statscCs|jtjdS)N)rRrvZsensors_battery)r:r!r!r"test_sensors_battery$sz-TestModuleFunctionsLeaks.test_sensors_batterycCs|jtjdS)N)rRrvZsensors_temperatures)r:r!r!r"test_sensors_temperatures)sz2TestModuleFunctionsLeaks.test_sensors_temperaturescCs|jtjdS)N)rRrvZ sensors_fans)r:r!r!r"test_sensors_fans.sz*TestModuleFunctionsLeaks.test_sensors_fanscCs|jtjdS)N)rRrvZ boot_time)r:r!r!r"test_boot_time5sz'TestModuleFunctionsLeaks.test_boot_timez(XXX produces a false positive on WindowscCs|jtjdS)N)rRrvZusers)r:r!r!r" test_users:sz#TestModuleFunctionsLeaks.test_userscCs|jtjdS)N)rRrZwinservice_enumerate)r:r!r!r"test_win_service_iterBsz.TestModuleFunctionsLeaks.test_win_service_itercCsdS)Nr!)r:r!r!r"test_win_service_getEsz-TestModuleFunctionsLeaks.test_win_service_getcCs"ttjj}|jtj|dS)N)nextrvwin_service_iterr|rRrZwinservice_query_config)r:r|r!r!r"test_win_service_get_configHsz4TestModuleFunctionsLeaks.test_win_service_get_configcCs"ttjj}|jtj|dS)N)rrvrr|rRrZwinservice_query_status)r:r|r!r!r"test_win_service_get_statusLsz4TestModuleFunctionsLeaks.test_win_service_get_statuscCs"ttjj}|jtj|dS)N)rrvrr|rRrZwinservice_query_descr)r:r|r!r!r" test_win_service_get_descriptionPsz9TestModuleFunctionsLeaks.test_win_service_get_descriptionN)2rXrYrZr[r}r#rrrrrrrr rrrrrr rrrrrpathexistsrrrrgetuidrrrrrrrrrrrrrrrrrrr!r!r!r"rsR   $r__main__):r[Z __future__rrrr8rrOrIrMrvZpsutil._commonrrrrrrZpsutil._compatr Z psutil.testsr r r r rrrrrrrrrrrrrrrrr]r\r^r Z _psplatformrrwrJr#r6ZTestCaser7r`rrrX__file__r!r!r!r" sl                            cB<