From 5048ce4d29218b16770a8b316cfa226fef1b41bf Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 12 Feb 2024 11:18:10 +0100 Subject: [PATCH] [EDF29150] : Add information of interval in CVR files --- src/Container/SALOME_ContainerHelper.py | 4 +- src/Container/SALOME_PyNode.py | 67 ++++++++++++++++++++----- src/KERNEL_PY/__init__.py | 2 +- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/Container/SALOME_ContainerHelper.py b/src/Container/SALOME_ContainerHelper.py index e306aff1f..72c69008b 100644 --- a/src/Container/SALOME_ContainerHelper.py +++ b/src/Container/SALOME_ContainerHelper.py @@ -112,9 +112,7 @@ class ScriptExecInfo: @property def CPUMemDuringExecStr(self): - cpu = self._cpu_mem_during_exec[::2] - mem_rss = self._cpu_mem_during_exec[1::2] - return [(a,ScriptExecInfo.MemRepr(b)) for a,b in self._cpu_mem_during_exec] + return [(a,ScriptExecInfo.MemRepr(b)) for a,b in self._cpu_mem_during_exec.data] @property def inputMem(self): diff --git a/src/Container/SALOME_PyNode.py b/src/Container/SALOME_PyNode.py index a82c2fc89..2d7a686bf 100644 --- a/src/Container/SALOME_PyNode.py +++ b/src/Container/SALOME_PyNode.py @@ -412,8 +412,9 @@ class FileDeleter(FileHolder): os.unlink( self._filename ) class MonitoringInfo: - def __init__(self, pyFileName, outFileName, pid): + def __init__(self, pyFileName, intervalInMs, outFileName, pid): self._py_file_name = pyFileName + self._interval_in_ms = intervalInMs self._out_file_name = outFileName self._pid = pid @@ -433,6 +434,10 @@ class MonitoringInfo: def outFileName(self): return self._out_file_name + @property + def intervalInMs(self): + return self._interval_in_ms + def FileSystemMonitoring(intervalInMs, dirNameToInspect, outFileName = None): """ This method loops indefinitely every intervalInMs milliseconds to scan @@ -468,6 +473,7 @@ import time import datetime with open("{tempOutFile}","a") as f: f.write( "{{}}\\n".format( "{dirNameToInspect2}" ) ) + f.write( "{{}}\\n".format( "{intervalInMs}" ) ) while(True): nbinodes = sp.check_output("{{}} | wc -l".format( " ".join(["find","{dirNameToInspect2}"]), ), shell = True).decode().strip() szOfDirStr = re.split("[\s]+",sp.check_output(["du","-sh","{dirNameToInspect2}"]).decode())[0] @@ -483,7 +489,7 @@ with open("{tempOutFile}","a") as f: outFileName = FileDeleter( tempOutFile ) else: outFileName = FileHolder(outFileName) - return MonitoringInfo(pyFileName,outFileName,None) + return MonitoringInfo(pyFileName, intervalInMs, outFileName, None) def CPUMemoryMonitoring( intervalInMs, outFileName = None ): """ @@ -514,19 +520,20 @@ pid = {} process = psutil.Process( pid ) 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 ) ) ) f.flush() time.sleep( {} / 1000.0 ) -""".format(pid, tempOutFile, intervalInMs)) +""".format(pid, tempOutFile, intervalInMs, intervalInMs)) if outFileName is None: autoOutFile = FileDeleter(tempOutFile) else: autoOutFile = FileHolder(tempOutFile) return FileDeleter(tempPyFile),autoOutFile pyFileName, outFileName = BuildPythonFileForCPUPercent( intervalInMs, outFileName ) - return MonitoringInfo(pyFileName, outFileName, None) + return MonitoringInfo(pyFileName, intervalInMs, outFileName, None) class GenericPythonMonitoringLauncherCtxMgr: def __init__(self, monitoringParams): @@ -556,12 +563,41 @@ def StopMonitoring( monitoringInfo ): import KernelBasis KernelBasis.StopMonitoring(monitoringInfo.pid) +class CPUMemInfo: + def __init__(self, intervalInMs, cpu, mem_rss): + """ + Args: + ---- + intervalInMs (int) + cpu (list) CPU usage + mem_rss (list) rss memory usage + """ + self._interval_in_ms = intervalInMs + self._data = [(a,b) for a,b in zip(cpu,mem_rss)] + def __str__(self): + st = """Interval in ms : {self.intervalInMs} +Data : ${self.data} +""".format( **locals() ) + return st + @property + def intervalInMs(self): + return self._interval_in_ms + @property + def data(self): + """ + list of triplets. First param of pair is cpu usage + Second param of pair is memory usage + """ + return self._data + def ReadCPUMemInfoInternal( fileName ): - import KernelBasis - ret = KernelBasis.ReadFloatsInFile( fileName ) - cpu = ret[::2] - mem_rss = [ int(elt) for elt in ret[1::2]] - return [(a,b) for a,b in zip(cpu,mem_rss)] + with open(fileName, "r") as f: + coarseData = [ elt.strip() for elt in f.readlines() ] + intervalInMs = int( coarseData[0] ) + coarseData = coarseData[1:] + cpu = [float(elt) for elt in coarseData[::2]] + mem_rss = [ int(elt) for elt in coarseData[1::2]] + return CPUMemInfo(intervalInMs,cpu,mem_rss) def ReadCPUMemInfo( monitoringInfo ): """ @@ -573,12 +609,12 @@ def ReadCPUMemInfo( monitoringInfo ): Returns ------- - list : list of pairs. First param of pair is CPU usage. Second param of pair is rss memory usage + CPUMemInfo instance """ return ReadCPUMemInfoInternal( monitoringInfo.outFileName.filename ) class InodeSizeInfo: - def __init__(self, dirNameMonitored, timeStamps, nbInodes, volumeOfDir): + def __init__(self, dirNameMonitored, intervalInMs, timeStamps, nbInodes, volumeOfDir): """ Args: ---- @@ -587,9 +623,11 @@ class InodeSizeInfo: volumeOfDir (list) """ self._dir_name_monitored = dirNameMonitored + self._interval_in_ms = intervalInMs self._data = [(t,a,b) for t,a,b in zip(timeStamps,nbInodes,volumeOfDir)] def __str__(self): st = """Filename monitored : {self.dirNameMonitored} +Interval in ms : ${self.intervalInMs} Data : ${self.data} """.format( **locals() ) return st @@ -597,6 +635,9 @@ Data : ${self.data} def dirNameMonitored(self): return self._dir_name_monitored @property + def intervalInMs(self): + return self._interval_in_ms + @property def data(self): """ list of triplets. First param of triplet is datetimestruct @@ -610,11 +651,11 @@ def ReadInodeSizeInfoInternal( fileName ): import os with open(fileName, "r") as f: coarseData = [ elt.strip() for elt in f.readlines() ] - dirNameMonitored = coarseData[0] ; coarseData = coarseData[1:] + dirNameMonitored = coarseData[0] ; intervalInMs = int( coarseData[1] ) ; coarseData = coarseData[2:] tss = [ datetime.datetime.fromtimestamp( float(elt) ) for elt in coarseData[::3] ] nbInodes = [int(elt) for elt in coarseData[1::3]] volumeOfDir = coarseData[2::3] - return InodeSizeInfo(dirNameMonitored,tss,nbInodes,volumeOfDir) + return InodeSizeInfo(dirNameMonitored,intervalInMs,tss,nbInodes,volumeOfDir) def ReadInodeSizeInfo( monitoringInfo ): """ diff --git a/src/KERNEL_PY/__init__.py b/src/KERNEL_PY/__init__.py index 025878f45..4fc15319f 100644 --- a/src/KERNEL_PY/__init__.py +++ b/src/KERNEL_PY/__init__.py @@ -536,7 +536,7 @@ while(True): logging.debug( "File for monitoring dump file : {}".format(tempPyFile) ) pyFileName = SALOME_PyNode.FileDeleter( tempPyFile ) pid = KernelBasis.LaunchMonitoring( tempPyFile ) - return SALOME_PyNode.MonitoringInfo(pyFileName,None,pid) + return SALOME_PyNode.MonitoringInfo(pyFileName,intervalInMs,None,pid) def LogManagerDumpIORInFile(self, iorFileName): global logm -- 2.39.2