1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2024 CEA, EDF
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 DisplayEntryInCMD = "--display"
25 VerboseEntryInCMD = "--verbose"
26 VerboseLevelEntryInCMD = "--verbose-level"
27 StopOnErrorEntryInCMD = "--stop-on-error"
28 DumpOnErrorEntryInCMD = "--dump-on-error"
29 DumpEntryInCMD = "--dump"
30 KernelTraceEntryInCMD = "--kerneltrace"
31 DumpStateEntryInCMD = "--dump-final"
32 LoadStateEntryInCMD = "--load-state"
33 SaveXMLSchemaEntryInCMD = "--save-xml-schema"
34 ShutdownEntryInCMD = "--shutdown"
35 ResetEntryInCMD = "--reset"
36 InitPortEntryInCMD = "--init-port"
37 DoNotSqueezeEntryInCMD = "--donotsqueeze"
38 IOREntryInCMD = "--ior-ns"
39 CPUTimeResOfContainerEntryInCMD = "--cpu-mem-container-time-res"
40 HTOPFileEntryInCMD = "--htop-of-yacs-engine-process-file"
41 HTOPServerFileEntryInCMD = "--htop-of-servers"
42 HTOPFileTimeResEntryInCMD = "--htop-of-yacs-engine-process-time-res"
43 HTOPServerFileTimeResEntryInCMD = "--htop-of-servers-time-res"
44 MonitoringDirsEntryInCMD = "--monitoring-dirs-content"
45 MonitoringDirsResEntryInCMD = "--monitoring-dirs-content-res"
46 MonitoringDirsTimeResEntryInCMD = "--monitoring-dirs-content-time-res"
47 ReplayOnErrorEntryInCMD = "--replay-on-error"
48 ReplayDirInCMD = "--replay-dir"
49 BigObjDirInCMD = "--bigobj-dir"
50 BigObjThresInCMD = "--bigobj-thres"
51 CustomOverridesInCMD = "--activate-custom-overrides"
53 DisplayKeyInARGS = "display"
54 VerboseKeyInARGS = "verbose"
55 VerboseLevelKeyInARGS = "verbose_level"
56 StopOnErrorKeyInARGS = "stop"
57 DumpOnErrorKeyInARGS = "dumpErrorFile"
58 DumpKeyInARGS = "dump"
59 KernelTraceKeyInARGS = "kerneltrace"
60 DumpStateKeyInARGS = "finalDump"
61 LoadStateKeyInARGS = "loadState"
62 SaveXMLSchemaKeyInARGS = "saveXMLSchema"
63 ShutdownKeyInARGS = "shutdown"
64 ResetKeyInARGS = "reset"
65 InitPortKeyInARGS = "init_port"
66 DoNotSqueezeKeyInARGS = "donotsqueeze"
67 IORKeyInARGS = "iorNS"
68 CPUTimeResOfContainerKeyInARGS = "cpu_mem_container_time_res"
69 HTOPFileKeyInARGS = "htop_of_yacs_engine_process_file"
70 HTOPServerFileKeyInARGS = "htop_of_servers"
71 HTOPFileTimeResKeyInARGS = "htop_of_yacs_engine_process_time_res"
72 HTOPServerFileTimeResKeyInARGS = "htop_of_servers_time_res"
73 MonitoringDirsInARGS = "monitoring_dirs_content"
74 MonitoringDirsResInARGS = "monitoring_dirs_content_res"
75 MonitoringDirsTimeResInARGS = "monitoring_dirs_content_time_res"
76 ReplayOnErrorEntryInARGS = "replay_on_error"
77 ReplayDirInARGS = "replay_dir"
78 BigObjDirInARGS = "bigobj_dir"
79 BigObjThresInARGS = "bigobj_thres"
80 CustomOverridesInARGS = "activate_custom_overrides"
82 KeyValnARGS = [(DisplayEntryInCMD,DisplayKeyInARGS),
83 (VerboseEntryInCMD,VerboseKeyInARGS),
84 (VerboseLevelEntryInCMD,VerboseLevelKeyInARGS),
85 (StopOnErrorEntryInCMD,StopOnErrorKeyInARGS),
86 (DumpOnErrorEntryInCMD,DumpOnErrorKeyInARGS),
87 (DumpEntryInCMD,DumpKeyInARGS),
88 (KernelTraceEntryInCMD,KernelTraceKeyInARGS),
89 (DumpStateEntryInCMD,DumpStateKeyInARGS),
90 (LoadStateEntryInCMD,LoadStateKeyInARGS),
91 (SaveXMLSchemaEntryInCMD,SaveXMLSchemaKeyInARGS),
92 (ShutdownEntryInCMD,ShutdownKeyInARGS),
93 (ResetEntryInCMD,ResetKeyInARGS),
94 (InitPortEntryInCMD,InitPortKeyInARGS),
95 (DoNotSqueezeEntryInCMD,DoNotSqueezeKeyInARGS),
96 (CPUTimeResOfContainerEntryInCMD,CPUTimeResOfContainerKeyInARGS),
97 (HTOPFileEntryInCMD,HTOPFileKeyInARGS),
98 (HTOPFileTimeResEntryInCMD,HTOPFileTimeResKeyInARGS),
99 (HTOPServerFileEntryInCMD,HTOPServerFileKeyInARGS),
100 (HTOPServerFileTimeResEntryInCMD,HTOPServerFileTimeResKeyInARGS),
101 (MonitoringDirsEntryInCMD,MonitoringDirsInARGS),
102 (MonitoringDirsResEntryInCMD,MonitoringDirsResInARGS),
103 (MonitoringDirsTimeResEntryInCMD,MonitoringDirsTimeResInARGS),
104 (ReplayOnErrorEntryInCMD,ReplayOnErrorEntryInARGS),
105 (ReplayDirInCMD,ReplayDirInARGS),
106 (BigObjDirInCMD,BigObjDirInARGS),
107 (BigObjThresInCMD,BigObjThresInARGS),
108 (CustomOverridesInCMD,CustomOverridesInARGS),
109 (IOREntryInCMD,IORKeyInARGS)]
111 my_runtime_yacs = None
115 my_replay_on_error = False
119 def initializeSALOME():
122 global my_runtime_yacs,my_ior_ns,my_runtime_yacs
126 if my_replay_on_error:
127 KernelBasis.SetPyExecutionMode("OutOfProcessWithReplay")
128 KernelBasis.SetDirectoryForReplayFiles( my_replay_dir )
130 salome.naming_service.DumpIORInFile( my_ior_ns )
131 flags = SALOMERuntime.RuntimeSALOME.UsePython + SALOMERuntime.RuntimeSALOME.UseCorba + SALOMERuntime.RuntimeSALOME.UseXml + SALOMERuntime.RuntimeSALOME.UseCpp + SALOMERuntime.RuntimeSALOME.UseSalome
132 SALOMERuntime.RuntimeSALOME.setRuntime( flags )
133 my_runtime_yacs = SALOMERuntime.getSALOMERuntime()
134 anIOR = salome.orb.object_to_string ( salome.modulcat )
135 aCatalog = my_runtime_yacs.loadCatalog( "session", anIOR )
136 my_runtime_yacs.addCatalog( aCatalog )
138 def SALOMEInitializationNeeded(func):
139 def decaratedFunc(*args,**kwargs):
141 return func(*args,**kwargs)
144 @SALOMEInitializationNeeded
145 def loadGraph( xmlFileName ):
149 xmlFileName : XML file containing YACS schema
154 SALOMERuntime.SalomeProc : YACS graph instance
157 l=loader.YACSLoader()
158 p=l.load( xmlFileName )
161 def patchGraph( proc, squeezeMemory, initPorts, xmlSchema, loadStateXmlFile, reset, display):
166 proc ( SALOMERuntime.SalomeProc ) : YACS Proc instance to be evaluated
167 squeezeMemory ( bool ) : squeezememory to be activated
168 initPorts (list<string>) : list of bloc.node.port=value.
170 loadStateXmlFile (string) : file if any of state to be loaded inside proc
177 def parse_init_port(input):
183 node_port, value = input.split("=")
184 nodePortSpl = node_port.split(".")
185 port = nodePortSpl[-1]
186 node = ".".join( nodePortSpl[:-1] )
187 return node,port,value
190 logging.info("SqueezeMemory requested -> update proc")
191 allNodes = proc.getAllRecursiveNodes()
192 for node in allNodes:
193 if isinstance(node,SALOMERuntime.PythonNode):
194 node.setSqueezeStatus( True )
196 for initPort in initPorts:
197 node,port,value = parse_init_port(initPort)
198 init_state = proc.setInPortValue(node, port, value)
199 if init_state != value:
200 raise RuntimeError(f"Error on initialization of {initPort}")
203 SALOMERuntime.VisitorSaveSalomeSchemaUnsafe(proc,xmlSchema)
206 info = pilot.LinkInfo( pilot.LinkInfo.ALL_DONT_STOP )
207 proc.checkConsistency(info)
208 if info.areWarningsOrErrors():
209 raise RuntimeError( info.getGlobalRepr() )
212 loader.loadState( proc, loadStateXmlFile )
214 proc.resetState(reset)
218 proc.writeDotInFile("toto")
220 @SALOMEInitializationNeeded
221 def prepareExecution(proc, isStop, dumpErrorFile):
226 pilot.ExecutorSwig : Instance of executor
229 ex=pilot.ExecutorSwig()
231 logging.info(f"Stop has been activated with {dumpErrorFile}")
232 ex.setStopOnError( dumpErrorFile!="", dumpErrorFile )
235 @SALOMEInitializationNeeded
236 def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, shutdown, CPUMemContainerTimeRes,
237 HTopOfThisProcessFile, HTopTimeRes,
238 HTopOfAllServersFile, HTopOfAllServersTimeRes, DirectoriesToMonitor):
243 executor (pilot.ExecutorSwig) : Executor in charge of evaluation.
244 proc ( SALOMERuntime.SalomeProc ) : YACS Proc instance to be evaluated
246 dump (int) : time interval between 2 dump state
247 finalDump ( string ) : filename containing final result of graph, if any.
249 shutdown (int) : shutdown level
250 CPUMemContainerTimeRes (int) : time in second between two measures of CPU/Mem in container processes
251 HTopOfThisProcessFile (str) : file name (if not empty) containing the result of measure of current process
252 HTopTimeRes (int) : time in second between two measures of CPU/Mem of current process
253 HTopOfAllServersFile (str) : file name (if not empty) containing the result of measure of all servers
254 HTopOfAllServersTimeRes (int) : time in second between two measures of CPU/Mem of any of server
262 def __init__(self, proc, shutdown):
264 self._shutdown = shutdown
268 def __exit__(self,exctype, exc, tb):
269 if my_replay_on_error:
271 for cont in salome.get_all_containers():
272 listOfGrps += cont.getAllLogFileNameGroups()
273 print("{} {} {}".format( 100*"=", "List of replay sessions of failing usecases" , 100*"="))
274 for igrp,grp in enumerate(listOfGrps):
275 print("{} : {}".format("Group {}".format(igrp)," ".join(grp)))
278 if self._shutdown < 999:
279 self._proc.shutdown(self._shutdown)
280 salome.dsm.shutdownScopes()
281 my_runtime_yacs.fini( False )
283 class AutoDumpThread:
284 def __init__(self, proc, dump, xmlfilename):
285 self._dumpFile = "dumpState_{}".format( os.path.basename(xmlfilename) )
286 self._lockFile = "{}.lock".format( os.path.splitext( os.path.basename(xmlfilename) )[0] )
288 logging.info(f"Ready to launch thread of state dump with dumpFile = {self._dumpFile} lockFile = {self._lockFile}")
289 self._dump_thread = SALOMERuntime.ThreadDumpState(proc,dump,self._dumpFile,self._lockFile)
290 self._dump_thread.start()
291 def __exit__(self,exctype, exc, tb):
292 self._dump_thread.join()
294 def MonitoringDirectories( DirectoriesToMonitor ):
296 if len( DirectoriesToMonitor ) > 0:
297 return [ SALOME_PyNode.GenericPythonMonitoringLauncherCtxMgr( SALOME_PyNode.FileSystemMonitoring(timeRes*1000,zeDir,zeDirRes) ) for zeDir,zeDirRes,timeRes in DirectoriesToMonitor ]
301 def MonitoringThisProcess(HTopOfThisProcessFile,HTopTimeRes):
303 if HTopOfThisProcessFile:
304 return [ SALOME_PyNode.GenericPythonMonitoringLauncherCtxMgr( SALOME_PyNode.CPUMemoryMonitoring(1000*HTopTimeRes,HTopOfThisProcessFile) ) ]
308 def MonitoringAllKernelServers(HTopOfAllServersFile, HTopOfAllServersTimeRes):
309 if HTopOfAllServersFile:
310 return [ salome.LogManagerLaunchMonitoringFileCtxMgr( 1000*HTopOfAllServersTimeRes, HTopOfAllServersFile ) ]
314 salome.cm.SetDeltaTimeBetweenCPUMemMeasureInMilliSecond( 1000*CPUMemContainerTimeRes )
315 # Start part of context manager instances
316 ctxManagers = [ AutoShutdown(proc,shutdown) ] # the first one must be this one. Because orb.shutdown must be called last !
318 ctxManagers += MonitoringDirectories( DirectoriesToMonitor ) + MonitoringThisProcess(HTopOfThisProcessFile, HTopTimeRes) + MonitoringAllKernelServers(HTopOfAllServersFile, HTopOfAllServersTimeRes)
321 ctxManagers += [ AutoDumpThread(proc,dump,xmlfilename) ]
322 # end of part of context managers
323 with contextlib.ExitStack() as stack:
324 for mgr in ctxManagers:
325 stack.enter_context(mgr)
326 executor.RunPy(proc,display,isPyThread=True,fromscratch=True) # same as RunW but releasing GIL
328 if proc.getEffectiveState() != pilot.DONE:
329 raise RuntimeError( proc.getErrorReport() )
332 proc.writeDotInFile("titi")
335 logging.info(f"Final dump requested : {finalDump}")
336 SALOMERuntime.schemaSaveStateUnsafe( proc, finalDump )
338 def EntryFromCoarseEntry( entry ):
339 if entry[:2] != "--":
340 raise RuntimeError("Unexpected entry")
345 Convert argparse.Namespace to dict
347 return {EntryFromCoarseEntry(entry):getattr(args,key) for entry,key in KeyValnARGS}
349 def reprAfterArgParsing( args ):
354 args (argparse.Namespace) : instance after parsing
356 return "\n".join( [ f"{EntryFromCoarseEntry(entry)} : {args[key]}" for entry,key in KeyValnARGS ] )
358 def getArgumentParser():
361 parser = argparse.ArgumentParser()
362 parser.add_argument('xmlfilename',help = "XML file containing YACS schema to be executed")
363 parser.add_argument("-d", DisplayEntryInCMD, dest = DisplayKeyInARGS, type=int, const=1, nargs='?', default=0, help="Display dot files: 0=never to 3=very often")
364 parser.add_argument("-v", VerboseEntryInCMD, dest = VerboseKeyInARGS,help="Produce verbose output", action='store_true')
365 parser.add_argument("-vl", VerboseLevelEntryInCMD, dest = VerboseLevelKeyInARGS, type=str, choices=["ERROR", "WARNING", "INFO", "DEBUG"], default="INFO", help="Specifies level of verbosity")
366 parser.add_argument("-s",StopOnErrorEntryInCMD,dest=StopOnErrorKeyInARGS,help="Stop on first error", action='store_true')
367 parser.add_argument("-e",DumpOnErrorEntryInCMD,dest=DumpOnErrorKeyInARGS, type=str, const='dumpErrorState.xml', default="", nargs='?', help="Stop on first error and dump state")
368 parser.add_argument("-g",DumpEntryInCMD,dest=DumpKeyInARGS, type=int, const=60, default=0, nargs='?', help="dump state")
369 parser.add_argument("-kt", KernelTraceEntryInCMD, dest = KernelTraceKeyInARGS,help="Produce verbose of SALOME/KERNEL", action='store_true')
370 parser.add_argument("-f",DumpStateEntryInCMD, dest =DumpStateKeyInARGS, type=str, const='finalDumpState.xml', default="", nargs='?', help="dump final state")
371 parser.add_argument("-l",LoadStateEntryInCMD, dest=LoadStateKeyInARGS, type=str, default="", help="Load State from a previous partial execution")
372 parser.add_argument("-x",SaveXMLSchemaEntryInCMD, dest=SaveXMLSchemaKeyInARGS, type=str, const="saveSchema.xml", nargs='?', default="", help = "dump xml schema")
373 parser.add_argument("-t",ShutdownEntryInCMD, dest = ShutdownKeyInARGS, type=int , default=3, help="Shutdown the schema: 0=no shutdown to 3=full shutdown")
374 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")
375 parser.add_argument("-i",InitPortEntryInCMD, dest = InitPortKeyInARGS, type=str, default =[], action='append', help="Initialisation value of a port, specified as bloc.node.port=value. For multiple settings use comma.")
376 parser.add_argument("-z",DoNotSqueezeEntryInCMD, dest = DoNotSqueezeKeyInARGS, help = "Desactivate squeeze memory optimization.", action='store_true')
377 parser.add_argument(CPUTimeResOfContainerEntryInCMD, dest = CPUTimeResOfContainerKeyInARGS, type=int, default = 10, help="Time in second between two measures of CPU/Mem in container processes")
378 parser.add_argument(HTOPFileEntryInCMD, dest = HTOPFileKeyInARGS, type=str, default ="", help="File name (if not empty) containing the result of measure of current process")
379 parser.add_argument(HTOPFileTimeResEntryInCMD, dest = HTOPFileTimeResKeyInARGS, type=int, default = 60, help="Time in second between between two measures of CPU/Mem of current process")
380 parser.add_argument(HTOPServerFileEntryInCMD, dest = HTOPServerFileKeyInARGS, type=str, default ="", help="File name (if not empty) containing the result of measure of all server processes")
381 parser.add_argument(HTOPServerFileTimeResEntryInCMD, dest = HTOPServerFileTimeResKeyInARGS, type=int, default = 30, help="Time in second between between two measures of CPU/Mem of any server process")
382 parser.add_argument(MonitoringDirsEntryInCMD, dest = MonitoringDirsInARGS, nargs='+', type=str, default =[], help="List of directories to be monitored")
383 parser.add_argument(MonitoringDirsResEntryInCMD, dest = MonitoringDirsResInARGS, nargs='+', type=str, default =[], help=f"List of files with result of monitoring of directories to be monitored (see {MonitoringDirsInARGS}). The size of lists are expected to be the same.")
384 parser.add_argument(MonitoringDirsTimeResEntryInCMD, dest = MonitoringDirsTimeResInARGS, nargs='+', type=int, default =[], help=f"List of time resolution (in second) of monitoring of directories to be monitored (see {MonitoringDirsInARGS}). The size of lists are expected to be the same.")
385 parser.add_argument("-w",ReplayOnErrorEntryInCMD,dest=ReplayOnErrorEntryInARGS,help="Mode of execution of YACS where all python execution are wrapped into a subprocess to be able to resist against failure (such as SIGSEV)", action='store_true')
386 parser.add_argument(ReplayDirInCMD, dest=ReplayDirInARGS, type=str, default ="", help="Directory storing replay scenarii if any (see replay-on-error option.")
387 parser.add_argument(BigObjDirInCMD, dest=BigObjDirInARGS, type=str, default ="", help="Directory storing big obj files exchanged between YACS python nodes.")
388 parser.add_argument(BigObjThresInCMD, dest=BigObjThresInARGS, type=int, default = KernelBasis.GetBigObjOnDiskThreshold(), help="Objects whose size exceeds this threshold in bytes will be written inside directory")
389 parser.add_argument(CustomOverridesInCMD, dest=CustomOverridesInARGS, help="Activate custom overrides. These overrides are incarnated by yacs_driver_overrides module that is expected to loadable in current application.", action='store_true')
390 parser.add_argument(IOREntryInCMD, dest = IORKeyInARGS, type=str, default ="", help="file inside which the ior of NS will be stored")
391 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.")
394 def mainRun( args, xmlFileName):
399 args (dict) : options for treatment
402 global my_ior_ns,my_replay_on_error,my_replay_dir
403 from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose,KernelLogLevelToLogging
405 iorNS = args[IORKeyInARGS]
410 if args[ReplayOnErrorEntryInARGS]:
411 my_replay_on_error = True
412 my_replay_dir = args[ReplayDirInARGS]
414 if args[VerboseKeyInARGS]:
415 setVerbose( args[ KernelTraceKeyInARGS ] )
416 setVerboseLevel( KernelLogLevelToLogging[ args[VerboseLevelKeyInARGS] ] )
417 positionVerbosityOfLoggerRegardingState()
418 logging.info( reprAfterArgParsing(args) )
420 proc = loadGraph( xmlFileName )
421 # work around a bug in Executor::Run when there are no tasks to launch.
422 if len(proc.getChildren()) == 0 :
425 patchGraph( proc, not args[DoNotSqueezeKeyInARGS], args[InitPortKeyInARGS], args[SaveXMLSchemaKeyInARGS], args[LoadStateKeyInARGS], args[ResetKeyInARGS], args[DisplayKeyInARGS])
426 executor = prepareExecution( proc, args[StopOnErrorKeyInARGS], args[DumpOnErrorKeyInARGS])
427 # proxy parameters management
428 if args[ BigObjDirInARGS ]:
429 salome.cm.SetBigObjOnDiskDirectory( args[ BigObjDirInARGS ] )
430 salome.cm.SetBigObjOnDiskThreshold( args[ BigObjThresInARGS ] )
432 if args[ CustomOverridesInARGS ]:
434 import yacs_driver_overrides
436 allresources = pylauncher.RetrieveRMCppSingleton()# pylauncher.ResourcesManager_cpp singleton representing all resources (pylauncher.ResourceDefinition_cpp) devoted for the computation.
437 yacs_driver_overrides.customize( salome.cm, allresources )
439 raise RuntimeError("Overrides have be requested to be triggered by the user but module is not available")
441 executeGraph( executor, xmlFileName, proc, args[DumpKeyInARGS], args[DumpStateKeyInARGS], args[DisplayKeyInARGS], args[ShutdownKeyInARGS], args[CPUTimeResOfContainerKeyInARGS],
442 args[HTOPFileKeyInARGS], args[HTOPFileTimeResKeyInARGS],
443 args[HTOPServerFileKeyInARGS], args[HTOPServerFileTimeResKeyInARGS], [(dirToMonitor,resFile,timeRes) for dirToMonitor,resFile,timeRes in zip(args[MonitoringDirsInARGS],args[MonitoringDirsResInARGS],args[MonitoringDirsTimeResInARGS])] )
450 - args (dict) : dictionnary containing all args taken into account. If json, the params in json will override entries
451 - xmlFileName (str) : XML YACS schema
455 parser = getArgumentParser()
456 args = parser.parse_args()
458 xmlFileName = args.xmlfilename
459 optionFromJSon = args.options_from_json
460 args = toDict( args )
462 # in case of Json overrides
463 with open( optionFromJSon ) as f:
464 opts_from_json = json.load( f )
465 for k,v in opts_from_json.items():
466 if k != EntryFromCoarseEntry(IOREntryInCMD) or v:# for IOR if v is null -> do not override
468 # change key of args from entryCMD to KeyInARGS
469 args = {key:args[EntryFromCoarseEntry(entry)] for entry,key in KeyValnARGS}
470 return args, xmlFileName
472 if __name__ == "__main__":
473 args, xmlFileName = parseArgs()
474 mainRun( args, xmlFileName)