From e6235910b478fb0a42b43d469e485790ee2f49eb Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 23 Apr 2024 09:23:56 +0200 Subject: [PATCH] [EDF29138] : measure CPU/Mem even in OutOfProcess mode --- src/Container/SALOME_PyNode.py | 13 +++++-- src/Launcher/Test/testCrashProofContainer.py | 37 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Container/SALOME_PyNode.py b/src/Container/SALOME_PyNode.py index ed19bb306..e7f3cb625 100644 --- a/src/Container/SALOME_PyNode.py +++ b/src/Container/SALOME_PyNode.py @@ -528,12 +528,21 @@ def CPUMemoryMonitoring( intervalInMs, outFileName = None ): f.write("""import psutil pid = {} process = psutil.Process( pid ) +def getChargeOf( p ): + a,b = p.cpu_percent(), p.memory_info().rss + try: + for c in p.children(): + a += c.cpu_percent(interval=0.01) ; b += c.memory_info().rss + except: + pass + return a,b import time with open("{}","a") as f: f.write( "{{}}\\n".format( "{}" ) ) while True: - f.write( "{{}}\\n".format( str( process.cpu_percent() ) ) ) - f.write( "{{}}\\n".format( str( process.memory_info().rss ) ) ) + cpu,mem_rss = getChargeOf( process ) + f.write( "{{}}\\n".format( str( cpu ) ) ) + f.write( "{{}}\\n".format( str( mem_rss ) ) ) f.flush() time.sleep( {} / 1000.0 ) """.format(pid, tempOutFile, intervalInMs, intervalInMs)) diff --git a/src/Launcher/Test/testCrashProofContainer.py b/src/Launcher/Test/testCrashProofContainer.py index 027cf7904..7bad47407 100644 --- a/src/Launcher/Test/testCrashProofContainer.py +++ b/src/Launcher/Test/testCrashProofContainer.py @@ -47,6 +47,12 @@ j = 8 * i my_log_4_this_session.addFreestyleAndFlush( ("a",777) ) # to check that hidden var is still accessible """ +heatCPUCode = """import KernelBasis +nbcore = 3 +cst = KernelBasis.GetTimeAdjustmentCst() +KernelBasis.HeatMarcel(5 * nbcore * cst,nbcore) +j = 8*i""" + class testPerfLogManager1(unittest.TestCase): def test0(self): """ @@ -116,6 +122,37 @@ class testPerfLogManager1(unittest.TestCase): os.unlink( zeFile ) cont.Shutdown() + def test2(self): + """ + Aim of test is to check that CPU/mem retrieved in log is OK even in OutOfProcessNoReplay mode. + """ + salome.salome_init() + salome.logm.clear() + assert(isinstance(KernelBasis.GetAllPyExecutionModes(),tuple)) + KernelBasis.SetPyExecutionMode("OutOfProcessNoReplay") # the aim of test is here + hostname = "localhost" + PROXY_THRES = "-1" + # + salome.cm.SetOverrideEnvForContainersSimple(env = [("SALOME_BIG_OBJ_ON_DISK_THRES",PROXY_THRES)]) + salome.cm.SetDeltaTimeBetweenCPUMemMeasureInMilliSecond( 250 ) + cp = pylauncher.GetRequestForGiveContainer(hostname,"container_cpu_mem_out_process_test") + cont = salome.cm.GiveContainer(cp) + poa = salome.orb.resolve_initial_references("RootPOA") + obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( (["i"],{"i": 3} ) )) ; id_o = poa.activate_object(obj) ; refPtr = poa.id_to_reference(id_o) + pyscript2 = cont.createPyScriptNode("testScript3",heatCPUCode) + pyscript2.executeFirst(refPtr) + ret = pyscript2.executeSecond(["j"]) + ret = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret[0]).data() ) + self.assertEqual(ret,24) + pyscript2.UnRegister() + a = salome.logm.NaiveFetch() + cpu_mem_to_test = a[0][1][0].get()._cpu_mem_during_exec.data # normally even in OutOfProcessNoReplay mode the CPU must report somehing greater than 100 + self.assertGreater(len(cpu_mem_to_test),10) # 5 second of run 250 ms of interval between measures -> 20. In case of problem in HeatMarcel -> 10 + logging.debug("CPU mem measured (even in OutOfProcessNoReplay) : {}".format(cpu_mem_to_test)) + greater_than_100 = [a for a,b in cpu_mem_to_test if a > 100] + self.assertGreater(len(greater_than_100),1) # At minimum one measure must report CPU load > 100% + cont.Shutdown() + if __name__ == '__main__': from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose salome.standalone() -- 2.39.2