Salome HOME
[EDF29138] : measure CPU/Mem even in OutOfProcess mode
[modules/kernel.git] / src / Container / SALOME_ContainerManager.cxx
index 506a44e5187808328d7f2e4b5bd7860a5eef9a99..232f47eb1ebe0e07458347a02574b40b029090ed 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -31,6 +31,7 @@
 #include "Basics_Utils.hxx"
 #include "Basics_DirUtils.hxx"
 #include "PythonCppUtils.hxx"
+#include "KernelBasis.hxx"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <signal.h>
@@ -241,9 +242,9 @@ void SALOME_ContainerManager::SetDeltaTimeBetweenCPUMemMeasureInMilliSecond(CORB
  */
 //=============================================================================
 
-void SALOME_ContainerManager::ShutdownContainers()
+void SALOME_ContainerManager::ShutdownContainersGeneric(std::function<void(Engines::Container_ptr)> funcToBeCalledOnContainer)
 {
-  MESSAGE("ShutdownContainers");
+  MESSAGE("ShutdownContainersGeneric");
   if(!_NS)
     return ;
   SALOME::Session_var session = SALOME::Session::_nil();
@@ -289,11 +290,12 @@ void SALOME_ContainerManager::ShutdownContainers()
         Engines::Container_var cont=Engines::Container::_narrow(obj);
         if(!CORBA::is_nil(cont))
         {
-          MESSAGE("ShutdownContainers: " << (*iter));
-          cont->Shutdown();
+          MESSAGE("ShutdownContainersGeneric: " << (*iter));
+          funcToBeCalledOnContainer( cont );
+          MESSAGE("ShutdownContainersGeneric: after call of shutdown" << (*iter));
         }
         else
-          MESSAGE("ShutdownContainers: no container ref for " << (*iter));
+          MESSAGE("ShutdownContainersGeneric: no container ref for " << (*iter));
       }
       catch(CORBA::SystemException& e)
       {
@@ -311,6 +313,26 @@ void SALOME_ContainerManager::ShutdownContainers()
   }
 }
 
+void SALOME_ContainerManager::ShutdownContainers()
+{
+  this->ShutdownContainersGeneric( [](Engines::Container_ptr cont) { cont->Shutdown(); } );
+}
+
+void SALOME_ContainerManager::ShutdownContainersNow()
+{
+  this->ShutdownContainersGeneric( [](Engines::Container_ptr cont)
+  {
+    try
+    {
+      cont->ShutdownNow();
+    }
+    catch(...)
+    {
+    }
+  }
+    );
+}
+
 void SALOME_ContainerManager::SetOverrideEnvForContainers(const Engines::KeyValDict& env)
 {
   this->_override_env.clear();
@@ -537,9 +559,27 @@ Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::Con
   return ret;
 }
 
+std::string SALOME_ContainerManager::GetCppBinaryOfKernelSSLContainer() const
+{
+  switch( SALOME::GetPyExecutionMode() )
+  {
+    case SALOME::PyExecutionMode::InProcess:
+      return "SALOME_Container_No_NS_Serv";
+    case SALOME::PyExecutionMode::OutOfProcessNoReplay:
+      return "SALOME_Container_No_NS_Serv_OutProcess";
+    case SALOME::PyExecutionMode::OutOfProcessWithReplay:
+      return "SALOME_Container_No_NS_Serv_OutProcess_Replay";
+    default:
+      {
+        ERROR_MESSAGE("Not manager py execution mode");
+        THROW_SALOME_EXCEPTION("GetCppBinaryOfKernelSSLContainer : Not manager py execution mode");
+      }
+  }
+}
+
 std::string SALOME_ContainerManager::GetCppBinaryOfKernelContainer() const
 {
-  std::string ret = this->_isSSL ? "SALOME_Container_No_NS_Serv" : "SALOME_Container";
+  std::string ret = this->_isSSL ?  GetCppBinaryOfKernelSSLContainer() : "SALOME_Container";
   return ret;
 }
 
@@ -950,18 +990,17 @@ std::string GetCommandFromTemplate(const std::string& theScriptName,
   // manage GIL
 
   PyObject* mod(PyImport_ImportModule(theScriptName.c_str()));
+  MESSAGE("Template name :"<< theScriptName.c_str());
+
   if (!mod)
   {
-    PyObject* sys = PyImport_ImportModule("sys");
-    PyObject* sys_path = PyObject_GetAttrString(sys, "path");
-    PyObject* folder_path = PyUnicode_FromString(getScriptTemplateFilePath().c_str());
+    AutoPyRef sys = PyImport_ImportModule("sys");
+    AutoPyRef sys_path = PyObject_GetAttrString(sys, "path");
+    AutoPyRef folder_path = PyUnicode_FromString(getScriptTemplateFilePath().c_str());
     PyList_Append(sys_path, folder_path);
 
     mod = PyImport_ImportModule(theScriptName.c_str());
 
-    Py_XDECREF(folder_path);
-    Py_XDECREF(sys_path);
-    Py_XDECREF(sys);
   }
 
   if (mod)
@@ -1006,6 +1045,10 @@ std::string GetCommandFromTemplate(const std::string& theScriptName,
       Py_XDECREF(mod);
     }
   }
+  else
+  {
+    ERROR_MESSAGE("Can not import the template script \"" << theScriptName << "\" !");
+  }
 
   MESSAGE("Command from template is ... " << command << std::endl);
   return command;
@@ -1083,6 +1126,7 @@ std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const En
   std::string ompi_uri_file = GetenvThreadSafeAsString("OMPI_URI_FILE");
   script_parameters.push(ompi_uri_file.empty() ? "NULL" : ompi_uri_file);
 
+  MESSAGE("Retrieving command from template (python module) \"" << script_name << "\"" << std::endl);
   std::string command_from_template = GetCommandFromTemplate(script_name, script_parameters);
 
   std::ostringstream o;