gf9dZddlmZddlZddlZddlZddlZddlmZddlmZddlm 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'ej(Z)d!Z*d"Z+Gd#d$e$Z,Gd%d&e,Z-e%j.e d'Gd(d)e$Z/Gd*d+e$Z0e1d,krdd-l2m3Z3e3e4dSdS).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)unittestc d}|S)zsDecorator for those Linux functions which are implemented in pure Python, and which we want to run faster. cFtjfd}|S)NctrG|jj} t|j_|g|Ri|||j_S#||j_wxYw|g|Ri|SN)r __class__times FEW_TIMES)selfargskwargsbeforefuns |/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/psutil/tests/test_memleaks.pywrapperz5fewtimes_if_linux..decorator..wrapperAs 2-2+4DN(3t5d555f55+1DN((6DN(1111s41$111&111s ?A ) functoolswraps)r-r/s` r. decoratorz$fewtimes_if_linux..decorator@s8    2 2 2 2   2)r2s r.fewtimes_if_linuxr5<s    r3ceZdZdZeZdZedZedZ edZ edZ e j e dedZe j e ded Zed Zd Zd Ze j e d dZe j e d dZe j e d edZe j eddZedZeeedZe j e ddZe j e dedZ edZ!eeedZ"edZ#ee j e$ d dZ%edZ&edZ'e j e dedZ(d Z)ed!Z*e j e+ d d"Z,e j e+ d d#Z-ed$Z.e j e/ d ed%Z0e j e1 d&e j e2 d d'Z3e j e1 d&e j e2 d d(Z4ee j ed)d*Z5e j e6 d d+Z7e j e dd,Z8d-S).TestProcessObjectLeaksz$Test leaks of Process class methods.cjtd}|||j|jzdSr%)rtest_class_coveragegetterssettersr)nss r. test_coveragez$TestProcessObjectLeaks.test_coverageZs4 t $ $ tRZ"*%<=====r3cD||jjdSr%)executeprocnamer)s r. test_namez TestProcessObjectLeaks.test_name^ TY^$$$$$r3cD||jjdSr%)r@rAcmdlinerCs r. test_cmdlinez#TestProcessObjectLeaks.test_cmdlineb TY&'''''r3cD||jjdSr%)r@rAexerCs r.test_exezTestProcessObjectLeaks.test_exef TY]#####r3cD||jjdSr%)r@rAppidrCs r. test_ppidz TestProcessObjectLeaks.test_ppidjrEr3z POSIX onlycD||jjdSr%)r@rAuidsrCs r. test_uidsz TestProcessObjectLeaks.test_uidsn  TY^$$$$$r3cD||jjdSr%)r@rAgidsrCs r. test_gidsz TestProcessObjectLeaks.test_gidssrTr3cD||jjdSr%)r@rAstatusrCs r. test_statusz"TestProcessObjectLeaks.test_statusx TY%&&&&&r3cD||jjdSr%)r@rAnicerCs r. test_nicez TestProcessObjectLeaks.test_nice|s TY^$$$$$r3cntfddS)Nc8jSr%)rAr])nicenessr)sr.z6TestProcessObjectLeaks.test_nice_set..sTY^^H55r3)thisprocr]r@)r)ras`@r. test_nice_setz$TestProcessObjectLeaks.test_nice_sets6==?? 5555566666r3 not supportedcD||jjdSr%)r@rAionicerCs r. test_ionicez"TestProcessObjectLeaks.test_ionicer[r3cJtr4tfddSfdt jt jtj dd} t|dS)Nc8jSr%)rArg)r)valuesr.rbz8TestProcessObjectLeaks.test_ionice_set..s!1!1%!8!8r3cLjtjSr%)rArgpsutilIOPRIO_CLASS_NONErCsr.rbz8TestProcessObjectLeaks.test_ionice_set..s!1!1&2J!K!Kr3r) r rcrgr@r0partialcextproc_ioprio_setosgetpid execute_w_excOSError)r)r-rks` @r.test_ionice_setz&TestProcessObjectLeaks.test_ionice_sets  -OO%%E LL88888 9 9 9 9 9 LLKKKK L L L#D$8")++r1MMC   w , , , , ,r3cD||jjdSr%)r@rA io_countersrCs r.test_io_countersz'TestProcessObjectLeaks.test_io_counters! TY*+++++r3zworthless on POSIXctj||jjdSr%)rmProcessusernamer@rArCs r. test_usernamez$TestProcessObjectLeaks.test_usernames; !!### TY'(((((r3cD||jjdSr%)r@rA create_timerCs r.test_create_timez'TestProcessObjectLeaks.test_create_time TY*+++++r3)only_ifcD||jjdSr%)r@rA num_threadsrCs r.test_num_threadsz'TestProcessObjectLeaks.test_num_threadsr{r3 WINDOWS onlycD||jjdSr%)r@rA num_handlesrCs r.test_num_handlesz'TestProcessObjectLeaks.test_num_handlesrr3cD||jjdSr%)r@rAnum_fdsrCs r. test_num_fdsz#TestProcessObjectLeaks.test_num_fds! TY&'''''r3cD||jjdSr%)r@rAnum_ctx_switchesrCs r.test_num_ctx_switchesz,TestProcessObjectLeaks.test_num_ctx_switches TY/00000r3cD||jjdSr%)r@rAthreadsrCs r. test_threadsz#TestProcessObjectLeaks.test_threadsrr3cD||jjdSr%)r@rA cpu_timesrCs r.test_cpu_timesz%TestProcessObjectLeaks.test_cpu_timess TY()))))r3cD||jjdSr%)r@rAcpu_numrCs r. test_cpu_numz#TestProcessObjectLeaks.test_cpu_numrr3cD||jjdSr%)r@rA memory_inforCs r.test_memory_infoz'TestProcessObjectLeaks.test_memory_inforr3cD||jjdSr%)r@rAmemory_full_inforCs r.test_memory_full_infoz,TestProcessObjectLeaks.test_memory_full_inforr3cD||jjdSr%)r@rAterminalrCs r. test_terminalz$TestProcessObjectLeaks.test_terminals! TY'(((((r3crtrtn|j}||jj|dS)Nr')rr(r'r@rAresumer)r's r. test_resumez"TestProcessObjectLeaks.test_resumes3"2   TY%U 33333r3cD||jjdSr%)r@rAcwdrCs r.test_cwdzTestProcessObjectLeaks.test_cwdrMr3cD||jjdSr%)r@rA cpu_affinityrCs r.test_cpu_affinityz(TestProcessObjectLeaks.test_cpu_affinitys TY+,,,,,r3ctfdtfddS)Nc8jSr%rAr)affinityr)sr.rbz>TestProcessObjectLeaks.test_cpu_affinity_set..sTY33H==r3c:jdgSNrorrCsr.rbz>TestProcessObjectLeaks.test_cpu_affinity_set..s 6 6t < <r3)rcrr@ru ValueError)r)rs`@r.test_cpu_affinity_setz,TestProcessObjectLeaks.test_cpu_affinity_setse((** =====>>>  <<<< > > > > >r3cttd5||jjddddS#1swxYwYdS)Nw)openr r@rA open_filesrCs r.test_open_filesz&TestProcessObjectLeaks.test_open_filess *,, $ $ / / LL- . . . / / / / / / / / / / / / / / / / / /s A  AAcD||jjdSr%)r@rA memory_mapsrCs r.test_memory_mapsz'TestProcessObjectLeaks.test_memory_mapsr{r3z LINUX onlyc8fddS)NcLjtjSr%rArlimitrm RLIMIT_NOFILErCsr.rbz4TestProcessObjectLeaks.test_rlimit..sTY--f.BCCr3r@rCs`r. test_rlimitz"TestProcessObjectLeaks.test_rlimits' CCCCDDDDDr3cttjfdt tffddS)NcNjtjSr%r)limitr)sr.rbz8TestProcessObjectLeaks.test_rlimit_set..sTY--f.BEJJr3c8jdSr)rArrCsr.rbz8TestProcessObjectLeaks.test_rlimit_set..s$):J:J2:N:Nr3)rcrrmrr@rurvr)r)rs`@r.test_rlimit_setz&TestProcessObjectLeaks.test_rlimit_setsd 455 JJJJJKKK GZ02N2N2N2NOOOOOr3zworthless on WINDOWSct5trdndfdddddS#1swxYwYdS)Ninetallc8jSr%)rA connections)kindr)sr.rbz9TestProcessObjectLeaks.test_connections..s!6!6t! >"-66D LL<<<<< = = = > > > > > > > > > > > > > > > > > >s%AA AcD||jjdSr%)r@rAenvironrCs r. test_environz#TestProcessObjectLeaks.test_environrIr3c2|ddS)NcXttjSr%)rq proc_inforsrtr4r3r.rbz7TestProcessObjectLeaks.test_proc_info..sT^^BIKK88r3rrCs r.test_proc_infoz%TestProcessObjectLeaks.test_proc_infos 8899999r3N)9__name__ __module__ __qualname____doc__rcrAr>r5rDrHrLrPr skipIfrrSrWrZr^rdrrhrwrrzrrrrrr rrrrrrrrrrrrrrrrrrrrrrrrrrr4r3r.r7r7Us.. D>>>%%%((($$$%%%X_Y --%%.-%X_Y --%%.-%'''%%%777X_^_55''65'X_^_55--65-X_--??,,@?,X_U011))21) ,,,7+++,,,+,X_[.11,,21,X_Y --((.-(1117+++((,+(***X_))?;;((<;(,,,111X_Y --)).-)444$$$X_))?;;--<;-X_))?;;>><;> ///X_(/::,,;:,X_Y --X_^_55EE65.-EX_Y --X_^_55PP65.-P X_W455>>65>X__o66((76(X_[.11::21:::r3r7ceZdZdZefdZefdZdZerdZ dZ dZ dZ d Z d ZxZSxZS) 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. ctt|_t j|jj|_|j|j dSr%) r setUpClassrsubprmr}pidrAkillwaitclsr&s r.rz%TestTerminatedProcessLeaks.setUpClasss] !##>#(,//    r3cptt|jdSr%)r tearDownClassrrrs r.rz(TestTerminatedProcessLeaks.tearDownClasss/ #(r3cH |dS#tj$rYdSwxYwr%)rm NoSuchProcess)r)r-s r.callzTestTerminatedProcessLeaks.calls;  CEEEEE#    DD s !!cD||jjdSr%)r@rArrCs r. test_killz$TestTerminatedProcessLeaks.test_kill' LL ( ( ( ( (r3cD||jjdSr%)r@rArrCs r.test_terminatez)TestTerminatedProcessLeaks.test_terminate*s LL, - - - - -r3cD||jjdSr%)r@rAsuspendrCs r. test_suspendz'TestTerminatedProcessLeaks.test_suspend-s LL* + + + + +r3cD||jjdSr%)r@rArrCs r.rz&TestTerminatedProcessLeaks.test_resume0s LL) * * * * *r3cD||jjdSr%)r@rArrCs r. test_waitz$TestTerminatedProcessLeaks.test_wait3rr3c<fd}|dS)Ncp tjjS#t$rYdSwxYwr%)rqrrArr rCsr.rz7TestTerminatedProcessLeaks.test_proc_info..call8s@>>$)-888)DDs #' 55r)r)rs` r.rz)TestTerminatedProcessLeaks.test_proc_info6s6      LL     r3)rrrr classmethodrrrr rrrrrr __classcell__)r&s@r.rr s [[  ) ) ) . . . , , , + + + ) ) )       #r3rrceZdZdZdZdS)TestProcessDualImplementationc2|ddS)Nc\ttjdS)NTuse_pebrq proc_cmdlinersrtr4r3r.rbzETestProcessDualImplementation.test_cmdline_peb_true..EsT..ry{{D.IIr3rrCs r.test_cmdline_peb_truez3TestProcessDualImplementation.test_cmdline_peb_trueDs IIJJJJJr3c2|ddS)Nc\ttjdS)NFrrr4r3r.rbzFTestProcessDualImplementation.test_cmdline_peb_false..HsT..ry{{E.JJr3rrCs r.test_cmdline_peb_falsez4TestProcessDualImplementation.test_cmdline_peb_falseGs JJKKKKKr3N)rrrrr r4r3r.rrAs:KKKLLLLLr3rceZdZdZdZedZedZedZedZ edZ ee j e dd Ze j e d d Zd Ze j ed dZdZdZdZe j eoejd dedZedZee j e ddZee j eoej dkddZ!dZ"dZ#ee j e$ ddZ%ee j e& ddZ'ee j e( ddZ)edZ*d Z+erd!Z,d"Z-d#Z.d$Z/d%Z0d&Sd&S)'TestModuleFunctionsLeaksz&Test leaks of psutil module functions.cXt}|||jdSr%)rr9rr<s r.r>z&TestModuleFunctionsLeaks.test_coverageSs+    tRV,,,,,r3c2|ddS)Nc,tjdS)NTlogicalrm cpu_countr4r3r.rbz9TestModuleFunctionsLeaks.test_cpu_count..[sV-d;;;r3rrCs r.test_cpu_countz'TestModuleFunctionsLeaks.test_cpu_countYs ;;<<<<._sV-e<<<r3rrCs r.test_cpu_count_physicalz0TestModuleFunctionsLeaks.test_cpu_count_physical]s <<=====r3cD|tjdSr%)r@rmrrCs r.rz'TestModuleFunctionsLeaks.test_cpu_timesa V%&&&&&r3c2|ddS)Nc,tjdS)NT)percpu)rmrr4r3r.rbz=TestModuleFunctionsLeaks.test_per_cpu_times..gsV-T:::r3rrCs r.test_per_cpu_timesz+TestModuleFunctionsLeaks.test_per_cpu_timeses ::;;;;;r3cD|tjdSr%)r@rm cpu_statsrCs r.test_cpu_statsz'TestModuleFunctionsLeaks.test_cpu_statsirr3recD|tjdSr%)r@rmcpu_freqrCs r. test_cpu_freqz&TestModuleFunctionsLeaks.test_cpu_freqms V_%%%%%r3rcjtj|tjdSr%)rm getloadavgr@rCs r.test_getloadavgz(TestModuleFunctionsLeaks.test_getloadavgrs, V&'''''r3cD|tjdSr%)r@rmvirtual_memoryrCs r.test_virtual_memoryz,TestModuleFunctionsLeaks.test_virtual_memoryys V*+++++r3z&worthless on SUNOS (uses a subprocess)cD|tjdSr%)r@rm swap_memoryrCs r.test_swap_memoryz)TestModuleFunctionsLeaks.test_swap_memory}s V'(((((r3c`trtn|j}|d|dS)NcLtjtjSr%)rm pid_existsrsrtr4r3r.rbz:TestModuleFunctionsLeaks.test_pid_exists..sV.ry{{;;r3rrr(r'r@rs r.test_pid_existsz(TestModuleFunctionsLeaks.test_pid_existss2"2   ;;5 IIIIIr3c`trtn|j}|d|dS)Nc*tjdS)N.)rm disk_usager4r3r.rbz:TestModuleFunctionsLeaks.test_disk_usage..sV.s33r3rr/rs r.test_disk_usagez(TestModuleFunctionsLeaks.test_disk_usages2"2   335 AAAAAr3cD|tjdSr%)r@rmdisk_partitionsrCs r.test_disk_partitionsz-TestModuleFunctionsLeaks.test_disk_partitionss V+,,,,,r3z/proc/diskstatsz3/proc/diskstats not available on this Linux versionc2|ddS)Nc,tjdSNF)nowrap)rmdisk_io_countersr4r3r.rbz@TestModuleFunctionsLeaks.test_disk_io_counters..sV4EBBBr3rrCs r.test_disk_io_countersz.TestModuleFunctionsLeaks.test_disk_io_counterss  BBCCCCCr3cD|tjdSr%)r@rmpidsrCs r. test_pidsz"TestModuleFunctionsLeaks.test_pidss V[!!!!!r3c2|ddS)Nc,tjdSr;)rmnet_io_countersr4r3r.rbz?TestModuleFunctionsLeaks.test_net_io_counters..sV35AAAr3rrCs r.test_net_io_countersz-TestModuleFunctionsLeaks.test_net_io_counterss  AABBBBBr3rzneed root accessctjdt5|dddddS#1swxYwYdS)Nrrc,tjdS)NrrG)rmnet_connectionsr4r3r.rbz?TestModuleFunctionsLeaks.test_net_connections..s!7U!C!C!Cr3)rmrIr r@rCs r.test_net_connectionsz-TestModuleFunctionsLeaks.test_net_connectionss E****    E E LLCC D D D E E E E E E E E E E E E E E E E E EsAA A chtrdn|j}|tj|dS)Ni@) tolerance)r rLr@rm net_if_addrs)r)rLs r.test_net_if_addrsz*TestModuleFunctionsLeaks.test_net_if_addrss1!(>>>>r3cD|tjdSr%)r@rm net_if_statsrCs r.test_net_if_statsz*TestModuleFunctionsLeaks.test_net_if_statss V()))))r3cD|tjdSr%)r@rmsensors_batteryrCs r.test_sensors_batteryz-TestModuleFunctionsLeaks.test_sensors_batterys V+,,,,,r3cD|tjdSr%)r@rmsensors_temperaturesrCs r.test_sensors_temperaturesz2TestModuleFunctionsLeaks.test_sensors_temperaturess V011111r3cD|tjdSr%)r@rm sensors_fansrCs r.test_sensors_fansz*TestModuleFunctionsLeaks.test_sensors_fanss V()))))r3cD|tjdSr%)r@rm boot_timerCs r.test_boot_timez'TestModuleFunctionsLeaks.test_boot_timerr3cD|tjdSr%)r@rmusersrCs r. test_usersz#TestModuleFunctionsLeaks.test_userss V\"""""r3cD|tjdSr%)r@rqwinservice_enumeraterCs r.test_win_service_iterz.TestModuleFunctionsLeaks.test_win_service_iters LL2 3 3 3 3 3r3cdSr%r4rCs r.test_win_service_getz-TestModuleFunctionsLeaks.test_win_service_gets Dr3cttj|fddS)Nc8tSr%)rqwinservice_query_configrBsr.rbzFTestModuleFunctionsLeaks.test_win_service_get_config..!=!=d!C!Cr3nextrmwin_service_iterrBr@r)rBs @r.test_win_service_get_configz4TestModuleFunctionsLeaks.test_win_service_get_configG/11227799D LLCCCC D D D D Dr3cttj|fddS)Nc8tSr%)rqwinservice_query_statusrisr.rbzFTestModuleFunctionsLeaks.test_win_service_get_status..rjr3rkrns @r.test_win_service_get_statusz4TestModuleFunctionsLeaks.test_win_service_get_statusrpr3cttj|fddS)Nc8tSr%)rqwinservice_query_descrrisr.rbzKTestModuleFunctionsLeaks.test_win_service_get_description..s!r5rrrrrr rrr"r r%r(rr+r0r5r8rrspathexistsr>rArrErgetuidrJrNrQrrTrrWrrZr]r`rcrerortrxr4r3r.r r Pse00--- ===>>>'''<<<'''X_%77&&87&X_[.11((21( ,,,X_UDEE))FE)JJJ BBB---X_UD27>>2C#D#DDJLLDDLLD """ X_,,o>>CC?>CX_U/yry{{a/1CDDEEEDE ??? *** X_,,o>>--?>-X_11?CC22DC2X_))?;;**<;* '''###D 4 4 4    E E E E E E D D D D D%DDr3r __main__) run_from_name)5r __future__rr0rsrmpsutil._commonrrrrrr psutil._compatr r psutil.testsr r rrrrrrrrrrrrrrrrrrr _psplatformrqr}rcr(r5r7rrrr rpsutil.tests.runnerr}__file__r4r3r.rs  &%%%%% ------ ''''''######))))))%%%%%%$$$$$$######((((((,,,,,,))))))------######,,,,,,))))))111111******......''''''))))))""""""''''''!!!!!! 6>   2s:s:s:s:s:^s:s:s:l33333!7333lWn--LLLLLNLL.-LODODODODOD~ODODODd z111111M(r3