From b2e5f45a4f2cb900c7fb815c820925fa89e1d30c Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Wed, 24 Jan 2024 08:00:08 +0100 Subject: [PATCH] Append context manager to ease and generalize monitoring of process --- src/Basics/libSALOMELog.cxx | 16 +++++++++ src/Basics/libSALOMELog.hxx | 1 + src/Container/SALOME_PyNode.py | 32 ++++++++++++++---- src/Launcher/SALOME_LogManager.cxx | 41 ++++++++++++++++++++++++ src/Launcher/SALOME_LogManager.hxx | 5 ++- src/Launcher/Test/testPerfLogManager1.py | 2 ++ src/SALOMELocalTrace/utilities.h | 2 +- 7 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/Basics/libSALOMELog.cxx b/src/Basics/libSALOMELog.cxx index a662bfef4..a9de39530 100644 --- a/src/Basics/libSALOMELog.cxx +++ b/src/Basics/libSALOMELog.cxx @@ -28,6 +28,10 @@ #include #include +#include +#include +#include + enum class VerbosityMode { undefined, nolog, withlog }; static VerbosityMode isActivated = VerbosityMode::undefined; @@ -180,4 +184,16 @@ namespace SALOME { return VerbosityLevel() >= VerbosityLevelType::error_level; } + + void AppendTimeClock(std::ostream& os) + { + auto now = std::chrono::system_clock::now(); + auto duration = now.time_since_epoch(); + auto timestamp = std::chrono::system_clock::to_time_t(now); + std::tm *local_time = std::localtime(×tamp); + auto millis = std::chrono::duration_cast(duration).count(); + os << std::setfill('0') << std::setw(2) << local_time->tm_hour << ":" + << std::setw(2) << local_time->tm_min << ":" + << std::setw(2) << local_time->tm_sec << "." << std::setw(3) << millis % 1000 << " - "; + } } diff --git a/src/Basics/libSALOMELog.hxx b/src/Basics/libSALOMELog.hxx index f4d91193e..a3c0ab3cd 100644 --- a/src/Basics/libSALOMELog.hxx +++ b/src/Basics/libSALOMELog.hxx @@ -41,6 +41,7 @@ namespace SALOME bool BASICS_EXPORT IsInfoLevel(); bool BASICS_EXPORT IsWarningLevel(); bool BASICS_EXPORT IsErrorLevel(); + void BASICS_EXPORT AppendTimeClock(std::ostream& os); VerbosityLevelType BASICS_EXPORT VerbosityLevel(); std::string BASICS_EXPORT VerbosityLevelStr(); } diff --git a/src/Container/SALOME_PyNode.py b/src/Container/SALOME_PyNode.py index 1cc03e84f..84fd6b746 100644 --- a/src/Container/SALOME_PyNode.py +++ b/src/Container/SALOME_PyNode.py @@ -419,10 +419,31 @@ class MonitoringInfo: def pid(self): return self._pid + @pid.setter + def pid(self, value): + self._pid = value + @property def outFileName(self): return self._out_file_name +class GenericPythonMonitoringLauncherCtxMgr: + def __init__(self, monitoringParams): + """ + Args: + ---- + monitoringParams (MonitoringInfo) + """ + self._monitoring_params = monitoringParams + def __enter__(self): + import KernelBasis + pid = KernelBasis.LaunchMonitoring(self._monitoring_params.pyFileName.filename) + self._monitoring_params.pid = pid + return self._monitoring_params + + def __exit__(self,exctype, exc, tb): + StopMonitoring( self._monitoring_params ) + def LaunchTimeCPUMonitoring( intervalInMs ): """ Launch a subprocess monitoring self process. @@ -452,8 +473,8 @@ with open("{}","a") as f: """.format(pid, tempOutFile, intervalInMs)) return FileDeleter(tempPyFile), FileDeleter(tempOutFile) pyFileName, outFileName = BuildPythonFileForCPUPercent( intervalInMs ) - pid = KernelBasis.LaunchMonitoring(pyFileName.filename) - return MonitoringInfo(pyFileName, outFileName, pid) + + return MonitoringInfo(pyFileName, outFileName, None) def StopMonitoring( monitoringInfo ): """ @@ -637,10 +658,9 @@ class PyScriptNode_i (Engines__POA.PyScriptNode,Generic): self.addTimeInfoOnLevel2("startExecTime") ## self.addInfoOnLevel2("measureTimeResolution",self.my_container_py.monitoringtimeresms()) - monitoringParams = LaunchTimeCPUMonitoring( self.my_container_py.monitoringtimeresms() ) - exec(self.ccode, self.context) - StopMonitoring( monitoringParams ) - cpumeminfo = ReadCPUMemInfo( monitoringParams ) + with GenericPythonMonitoringLauncherCtxMgr( LaunchTimeCPUMonitoring( self.my_container_py.monitoringtimeresms() ) ) as monitoringParams: + exec(self.ccode, self.context) + cpumeminfo = ReadCPUMemInfo( monitoringParams ) ## self.addInfoOnLevel2("CPUMemDuringExec",cpumeminfo) del monitoringParams diff --git a/src/Launcher/SALOME_LogManager.cxx b/src/Launcher/SALOME_LogManager.cxx index 54a8b90d5..6e106965e 100644 --- a/src/Launcher/SALOME_LogManager.cxx +++ b/src/Launcher/SALOME_LogManager.cxx @@ -28,6 +28,8 @@ #include #endif +#include "utilities.h" + #include #include #include @@ -58,6 +60,17 @@ static SALOME::vectorOfByte *FromVectCharToCorba(const std::vector& data) return ret._retn(); } +SALOME_ContainerScriptPerfLog::~SALOME_ContainerScriptPerfLog() +{ + for(auto execSession : _sessions) + { + PortableServer::ServantBase *serv = getPOA()->reference_to_servant(execSession); + PortableServer::ObjectId_var oid = getPOA()->reference_to_id(execSession); + getPOA()->deactivate_object(oid); + } + _sessions.clear(); +} + PortableServer::POA_var SALOME_ContainerScriptExecPerfLog::getPOA() { return father()->getPOA(); @@ -142,6 +155,7 @@ void SALOME_ContainerScriptPerfLog::accept(SALOME_VisitorContainerLog &visitor) visitor.leaveContainerScriptPerfLog( *this ); } + Engines::ContainerScriptExecPerfLog_ptr SALOME_ContainerScriptPerfLog::addExecutionSession() { SALOME_ContainerScriptExecPerfLog *execution = new SALOME_ContainerScriptExecPerfLog(this); @@ -177,6 +191,17 @@ Engines::ListOfContainerScriptExecPerfLog *SALOME_ContainerScriptPerfLog::listOf ///// +SALOME_ContainerPerfLog::~SALOME_ContainerPerfLog() +{ + for(auto script : _scripts) + { + PortableServer::ServantBase *serv = getPOA()->reference_to_servant(script); + PortableServer::ObjectId_var oid = getPOA()->reference_to_id(script); + getPOA()->deactivate_object(oid); + } + _scripts.clear(); +} + PortableServer::POA_var SALOME_ContainerPerfLog::getPOA() { return father()->getPOA(); @@ -275,6 +300,22 @@ SALOME_LogManager::SALOME_LogManager(CORBA::ORB_ptr orb, PortableServer::POA_var } } +SALOME_LogManager::~SALOME_LogManager() +{ + this->clear(); +} + +void SALOME_LogManager::clear() +{ + for(auto cont : _containers) + { + PortableServer::ServantBase *serv = getPOA()->reference_to_servant(cont); + PortableServer::ObjectId_var oid = getPOA()->reference_to_id(cont); + getPOA()->deactivate_object(oid); + } + _containers.clear(); +} + Engines::ContainerPerfLog_ptr SALOME_LogManager::declareContainer(const char *contInNS, const char *logfile) { SALOME_ContainerPerfLog *cont = new SALOME_ContainerPerfLog(this,contInNS,logfile); diff --git a/src/Launcher/SALOME_LogManager.hxx b/src/Launcher/SALOME_LogManager.hxx index 7fa108fc8..89735aa3f 100644 --- a/src/Launcher/SALOME_LogManager.hxx +++ b/src/Launcher/SALOME_LogManager.hxx @@ -79,6 +79,7 @@ class SALOMELAUNCHER_EXPORT SALOME_ContainerScriptPerfLog : public POA_Engines:: { public: SALOME_ContainerScriptPerfLog(SALOME_ContainerPerfLog *father, const std::string& name, const std::string& code):_father(father),_name(name),_code(code) { } + ~SALOME_ContainerScriptPerfLog(); SALOME_ContainerPerfLog *father() const { return _father; } void setPyObj(PyObject *obj) { _pyScriptLog.set(obj); } PyObject *pyObj() { return _pyScriptLog.get(); } @@ -106,6 +107,7 @@ class SALOMELAUNCHER_EXPORT SALOME_ContainerPerfLog : public POA_Engines::Contai { public: SALOME_ContainerPerfLog(SALOME_LogManager *father, const std::string& nameInNS, const std::string& logFile):_father(father),_name_in_ns(nameInNS),_log_file(logFile) { } + ~SALOME_ContainerPerfLog(); SALOME_LogManager *father() const { return _father; } void setPyObj(PyObject *obj) { _pyContLog.set(obj); } PyObject *pyObj() { return _pyContLog.get(); } @@ -152,7 +154,8 @@ class SALOMELAUNCHER_EXPORT SALOME_LogManager : public POA_Engines::LogManager SALOME_LogManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService_Abstract *ns = nullptr); PortableServer::POA_var getPOA() { return _poa; } PyObject *pyHelper() const { return _pyLogManager; } - virtual ~SALOME_LogManager() = default; + ~SALOME_LogManager(); + void clear() override; Engines::ContainerPerfLog_ptr declareContainer(const char *contInNS, const char *logfile) override; Engines::ListOfContainerPerfLog *listOfContainerLogs() override; SALOME::vectorOfByte *getAllStruct(bool clearMemory) override; diff --git a/src/Launcher/Test/testPerfLogManager1.py b/src/Launcher/Test/testPerfLogManager1.py index 9c57bc187..d7f64fe03 100644 --- a/src/Launcher/Test/testPerfLogManager1.py +++ b/src/Launcher/Test/testPerfLogManager1.py @@ -64,6 +64,7 @@ class testPerfLogManager1(unittest.TestCase): """ hostname = "localhost" cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test") + salome.logm.clear() #PROXY_THRES = "-1" PROXY_THRES = "1" with tempfile.TemporaryDirectory() as tmpdirnameMonitoring: @@ -188,6 +189,7 @@ time.sleep(1) """ hostname = "localhost" cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test_two") + salome.logm.clear() PROXY_THRES = "1" with tempfile.TemporaryDirectory() as tmpdirname: ior_ns_file = os.path.join(tmpdirname,"ns.ior") diff --git a/src/SALOMELocalTrace/utilities.h b/src/SALOMELocalTrace/utilities.h index 72414f9d5..5bb6ed6b5 100644 --- a/src/SALOMELocalTrace/utilities.h +++ b/src/SALOMELocalTrace/utilities.h @@ -51,7 +51,7 @@ * thread waken up is not guaranteed (no fifo or priority rules in Linux Kernel) */ -#define MESS_INIT(deb) std::ostringstream os; os<insert(NORMAL_MESS, os.str().c_str()); #define MESS_ABORT std::endl; LocalTraceBufferPool::instance()->insert(ABORT_MESS, os.str().c_str()); -- 2.39.2