Salome HOME
[EDF29138] : measure CPU/Mem even in OutOfProcess mode
[modules/kernel.git] / src / Container / SALOME_PyNode.py
index 26493bf8145eccf234e0c7d1f425e49be5bb728b..e7f3cb62587864d177b96e5cb75e0e0f607d1c4b 100644 (file)
@@ -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))
@@ -728,6 +737,7 @@ outputsKeys = {}
 exec( "{{}} = LogOfCurrentExecutionSession( orb.string_to_object( \\"{}\\" ) )".format(MY_PERFORMANCE_LOG_ENTRY_IN_GLBS) )
 with open(inputFileName,"rb") as f:
   context = pickle.load( f )
+context[MY_PERFORMANCE_LOG_ENTRY_IN_GLBS] = eval( MY_PERFORMANCE_LOG_ENTRY_IN_GLBS )
 with open(codeFileName,"r") as f:
   code = f.read()
 # go for execution
@@ -873,21 +883,38 @@ def ExecLocal( code, context, outargsname, containerRef, instanceOfLogOfCurrentS
   exec( code, context )
   return instanceOfLogOfCurrentSession._current_instance
 
-class LogOfCurrentExecutionSession:
+class LogOfCurrentExecutionSessionAbs(abc.ABC):
+  def __init__(self):
+    self._current_instance = ScriptExecInfo()
+
+  def addInfoOnLevel2(self, key, value):
+    setattr(self._current_instance,key,value)
+
+  @abc.abstractmethod
+  def addFreestyleAndFlush(self, value):
+    raise RuntimeError("Must be overloaded")
+
+class LogOfCurrentExecutionSession(LogOfCurrentExecutionSessionAbs):
   def __init__(self, handleToCentralizedInst):
+    super().__init__()
     self._remote_handle = handleToCentralizedInst
-    self._current_instance = ScriptExecInfo()
 
   def addFreestyleAndFlush(self, value):
     self._current_instance.freestyle = value
     self.finalizeAndPushToMaster()
 
-  def addInfoOnLevel2(self, key, value):
-    setattr(self._current_instance,key,value)
-
   def finalizeAndPushToMaster(self):
     self._remote_handle.assign( pickle.dumps( self._current_instance ) )
 
+class LogOfCurrentExecutionSessionStub(LogOfCurrentExecutionSessionAbs):
+  """
+  This class is to stub LogOfCurrentExecutionSession in context of replay where the server (handleToCentralizedInst) has vanished
+  """
+  def __init__(self, handleToCentralizedInst = None):
+    super().__init__()
+  def addFreestyleAndFlush(self, value):
+    pass
+
 class PyScriptNode_Abstract_i(Engines__POA.PyScriptNode,Generic,abc.ABC):
   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
   def __init__(self, nodeName, code, poa, my_container, logscript):