Salome HOME
[EDF29576] : OK for the YACS process monitoring
authorAnthony Geay <anthony.geay@edf.fr>
Wed, 28 Feb 2024 12:24:11 +0000 (13:24 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Wed, 28 Feb 2024 12:24:11 +0000 (13:24 +0100)
src/yacsloader/driver_internal.py

index 686ee32865ec07f33ad58436ddab03e072d4676d..41933e2d4af360d941de069ab98d1ee6308cccb7 100644 (file)
@@ -35,6 +35,9 @@ ResetEntryInCMD = "--reset"
 InitPortEntryInCMD = "--init-port"
 DoNotSqueezeEntryInCMD = "--donotsqueeze"
 IOREntryInCMD = "--ior-ns"
+CPUTimeResOfContainerEntryInCMD = "--cpu-mem-container-time-res"
+HTOPFileEntryInCMD = "--htop-of-yacs-engine-process-file"
+HTOPFileTimeResEntryInCMD = "--htop-of-yacs-engine-process-time-res"
 
 DisplayKeyInARGS = "display"
 VerboseKeyInARGS = "verbose"
@@ -50,6 +53,9 @@ ResetKeyInARGS = "reset"
 InitPortKeyInARGS = "init_port"
 DoNotSqueezeKeyInARGS = "donotsqueeze"
 IORKeyInARGS = "iorNS"
+CPUTimeResOfContainerKeyInARGS = "cpu_mem_container_time_res"
+HTOPFileKeyInARGS = "htop_of_yacs_engine_process_file"
+HTOPFileTimeResKeyInARGS = "htop_of_yacs_engine_process_time_res"
 
 KeyValnARGS = [(DisplayEntryInCMD,DisplayKeyInARGS),
                (VerboseEntryInCMD,VerboseKeyInARGS),
@@ -64,6 +70,9 @@ KeyValnARGS = [(DisplayEntryInCMD,DisplayKeyInARGS),
                (ResetEntryInCMD,ResetKeyInARGS),
                (InitPortEntryInCMD,InitPortKeyInARGS),
                (DoNotSqueezeEntryInCMD,DoNotSqueezeKeyInARGS),
+               (CPUTimeResOfContainerEntryInCMD,CPUTimeResOfContainerKeyInARGS),
+               (HTOPFileEntryInCMD,HTOPFileKeyInARGS),
+               (HTOPFileTimeResEntryInCMD,HTOPFileTimeResKeyInARGS),
                (IOREntryInCMD,IORKeyInARGS)]
 
 my_runtime_yacs = None
@@ -177,7 +186,7 @@ def prepareExecution(proc, isStop, dumpErrorFile):
   return ex
 
 @SALOMEInitializationNeeded
-def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, shutdown ):
+def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, CPUMemContainerTimeRes, HTopOfThisProcessFile, HTopTimeRes ):
   """
   Args:
   -----
@@ -188,20 +197,35 @@ def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, shutdow
   dump (int) : time interval between 2 dump state
   finalDump ( string ) : filename containing final result of graph, if any.
   display (int) :
-  shutdown (int) : shutdown level
+  CPUMemContainerTimeRes (int) : time in second between two measures of CPU/Mem in container processes
+  HTopOfThisProcessFile (str) : file name (if not empty) containing the result of measure of current process
+  HTopTimeRes (int) : time in second between two measures of CPU/Mem of current process
   """
   import SALOMERuntime
   dump_thread = None
   import pilot
   import os
 
+  def MonitoringThisProcess(HTopOfThisProcessFile,HTopTimeRes):
+    import SALOME_PyNode
+    import contextlib
+    if HTopOfThisProcessFile:
+      return SALOME_PyNode.GenericPythonMonitoringLauncherCtxMgr( SALOME_PyNode.CPUMemoryMonitoring(1000*HTopTimeRes,HTopOfThisProcessFile) )
+    else:
+      return contextlib.nullcontext()
+  #
   if dump != 0:
       dumpFile = "dumpState_{}".format( os.path.basename(xmlfilename) )
       lockFile = "{}.lock".format( os.path.splitext( os.path.basename(xmlfilename) )[0] )
       dump_thread = SALOMERuntime.ThreadDumpState(proc,dump,dumpFile,lockFile)
       dump_thread.start()
-
-  executor.RunPy(proc,display,isPyThread=True,fromscratch=True) # same as RunW but releasing GIL
+  #
+  salome.cm.SetDeltaTimeBetweenCPUMemMeasureInMilliSecond( 1000*CPUMemContainerTimeRes )
+  #
+  #with SALOME_PyNode.GenericPythonMonitoringLauncherCtxMgr( SALOME_PyNode.CPUMemoryMonitoring(60000,self._htop_of_yacs_engine_process) ) as monitoringParamsMaster:
+  #  with salome.LogManagerLaunchMonitoringFileCtxMgr(5000,self._monitoring_file) as monitoringParams:
+  with MonitoringThisProcess(HTopOfThisProcessFile, HTopTimeRes) as monitoringParamsMaster:
+    executor.RunPy(proc,display,isPyThread=True,fromscratch=True) # same as RunW but releasing GIL
   if proc.getEffectiveState() != pilot.DONE:
     raise RuntimeError( proc.getErrorReport() )
   #
@@ -249,12 +273,11 @@ def reprAfterArgParsing( args ):
   """
   return "\n".join( [ f"{EntryFromCoarseEntry(entry)} : {args[key]}" for entry,key in KeyValnARGS ] )
 
-
 def getArgumentParser():
   import argparse
   parser = argparse.ArgumentParser()
   parser.add_argument('xmlfilename',help = "XML file containing YACS schema to be executed")
-  parser.add_argument("-d", DisplayEntryInCMD, dest = DisplayKeyInARGS, type=int, default=[0], nargs=1, help="Display dot files: 0=never to 3=very often")
+  parser.add_argument("-d", DisplayEntryInCMD, dest = DisplayKeyInARGS, type=int, const=1, nargs='?', default=0, help="Display dot files: 0=never to 3=very often")
   parser.add_argument("-v", VerboseEntryInCMD, dest = VerboseKeyInARGS,help="Produce verbose output", action='store_true')
   parser.add_argument("-s",StopOnErrorEntryInCMD,dest=StopOnErrorKeyInARGS,help="Stop on first error", action='store_true')
   parser.add_argument("-e",DumpOnErrorEntryInCMD,dest=DumpOnErrorKeyInARGS, type=str, const='dumpErrorState.xml', default="", nargs='?', help="Stop on first error and dump state")
@@ -267,6 +290,9 @@ def getArgumentParser():
   parser.add_argument("-r",ResetEntryInCMD, dest = ResetKeyInARGS, type=int , default = 0, help="Reset the schema before execution: 0=nothing, 1=reset error nodes to ready state")
   parser.add_argument("-i",InitPortEntryInCMD, dest = InitPortKeyInARGS, type=str, default ="", help="Initialisation value of a port, specified as bloc.node.port=value.")
   parser.add_argument("-z",DoNotSqueezeEntryInCMD, dest = DoNotSqueezeKeyInARGS, help = "Desactivate squeeze memory optimization.", action='store_true')
+  parser.add_argument(CPUTimeResOfContainerEntryInCMD, dest = CPUTimeResOfContainerKeyInARGS, type=int, default =10, help="Time in second between two measures of CPU/Mem in container processes")
+  parser.add_argument(HTOPFileEntryInCMD, dest = HTOPFileKeyInARGS, type=str, default ="", help="File name (if not empty) containing the result of measure of current process")
+  parser.add_argument(HTOPFileTimeResEntryInCMD, dest = HTOPFileTimeResKeyInARGS, type=int, default =60, help="Time in second between between two measures of CPU/Mem of current process")
   parser.add_argument(IOREntryInCMD, dest = IORKeyInARGS, type=str, default ="", help="file inside which the ior of NS will be stored")
   parser.add_argument("--options_from_json", dest = "options_from_json", type=str, default ="", help="Json file of options. If defined options in json will override those specified in command line.")
   return parser
@@ -281,7 +307,6 @@ def mainRun( args, iorNS, xmlFileName):
   """
   global my_ior_ns
   from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose
-  args[DisplayKeyInARGS] = args[DisplayKeyInARGS][0]
   #
   if iorNS:
     my_ior_ns = iorNS
@@ -298,7 +323,7 @@ def mainRun( args, iorNS, xmlFileName):
   patchGraph( proc, not args[DoNotSqueezeKeyInARGS], [elt for elt in args[InitPortKeyInARGS].split(",") if elt !=""], args[SaveXMLSchemaKeyInARGS], args[LoadStateKeyInARGS], args[ResetKeyInARGS], args[DisplayKeyInARGS])
   executor = prepareExecution( proc, args[StopOnErrorKeyInARGS], args[DumpOnErrorKeyInARGS])
   #
-  executeGraph( executor, xmlFileName, proc, args[DumpKeyInARGS], args[DumpStateKeyInARGS], args[DisplayKeyInARGS], args[ShutdownKeyInARGS])
+  executeGraph( executor, xmlFileName, proc, args[DumpKeyInARGS], args[DumpStateKeyInARGS], args[DisplayKeyInARGS], args[CPUTimeResOfContainerKeyInARGS], args[HTOPFileKeyInARGS], args[HTOPFileTimeResKeyInARGS])
   #
   destroyElementsGeneratedByExecutionOfGraph( proc, args[ShutdownKeyInARGS] )
 
@@ -313,8 +338,8 @@ if __name__ == "__main__":
   args = toDict( args )
   if optionFromJSon:
     # in case of Json overrides 
-    with open( args.options_from_json ) as f:
+    with open( optionFromJSon ) as f:
       opts_from_json = json.load( f )
-    for k,v in opts_from_json:
+    for k,v in opts_from_json.items():
       args[k] = v
   mainRun( args, iorNS, xmlFileName)