Basics_DirUtils.cxx
KernelBasis.cxx
HeatMarcel.cxx
+ Monitoring.cxx
)
ADD_LIBRARY(SALOMELog ${SALOMELog_SOURCES})
#include "KernelBasis.hxx"
#include "HeatMarcel.hxx"
#include "libSALOMELog.hxx"
+#include "Monitoring.hxx"
using namespace SALOME;
%}
%include std_string.i
%include std_set.i
%include std_except.i
+%include std_vector.i
+
+%template(dvec) std::vector<double>;
%exception {
try
double GetTimeAdjustmentCst();
+void LaunchMonitoring(const std::string& pyScriptToEvaluate, const std::string& outFileName);
+
+std::vector<double> StopMonitoring();
+
bool VerbosityActivated();
void SetVerbosityActivated(bool flag);
--- /dev/null
+// Copyright (C) 2023 CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#include "Monitoring.hxx"
+
+#include "baseutilities.h"
+
+#include <cstdint>
+#include <cmath>
+#include <vector>
+#include <numeric>
+#include <iomanip>
+#include <iostream>
+
+#include <sstream>
+#include <array>
+#include <stdexcept>
+#include <thread>
+#include <stdio.h>
+#include <chrono>
+#include <fstream>
+
+#ifndef WIN32
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <signal.h>
+#endif
+
+static std::string _out_filename;
+#ifndef WIN32
+static pid_t pid_of_subprocess = 0;
+#endif
+
+#ifndef WIN32
+static void LaunchMonitoringLinux(const std::string& pyScriptToEvaluate, const std::string& outFileName)
+{
+ constexpr char PYTHON_EXEC[] = "python3";
+ pid_t pid = fork();
+ if (pid == -1)
+ {
+ throw std::runtime_error("LaunchMonitoring : Error at creation of sub process !");
+ }
+ else if( pid == 0)
+ {
+ execlp(PYTHON_EXEC,PYTHON_EXEC,pyScriptToEvaluate.c_str(),nullptr);
+ std::ostringstream oss; oss << "LaunchMonitoring : Error during exe : " << sys_errlist[errno];
+ throw std::runtime_error( oss.str() );
+ }
+ else
+ {
+ pid_of_subprocess = pid;
+ }
+}
+#endif
+
+void SALOME::LaunchMonitoring(const std::string& pyScriptToEvaluate, const std::string& outFileName)
+{
+ _out_filename = outFileName;
+#ifndef WIN32
+ LaunchMonitoringLinux(pyScriptToEvaluate,outFileName);
+#else
+ throw std::runtime_error("LaunchMonitoring not implemented for Windows !");
+#endif
+}
+
+std::vector<double> SALOME::ReadFloatsInFile(const std::string& fileName)
+{
+ std::ifstream inputFile( fileName );
+
+ if(!inputFile.is_open())
+ {
+ std::ostringstream oss; oss << "Impossible to open file \"" << _out_filename<< "\" !";
+ throw std::runtime_error( oss.str() );
+ }
+ std::vector<double> ret;
+ std::string line;
+ try
+ {
+ while (std::getline(inputFile, line))
+ {
+ std::istringstream iss(line);
+ double floatValue;
+ if( !(iss >> floatValue) )
+ throw std::invalid_argument("Conversion into FP failed !");
+ if( !iss.eof() )
+ throw std::invalid_argument("Conversion into FP failed !");
+ ret.push_back(floatValue);
+ }
+ inputFile.close();
+ }
+ catch (const std::exception& e)
+ {
+ }
+ return ret;
+}
+
+#ifndef WIN32
+static std::vector<double> StopMonitoringLinux()
+{
+ kill( pid_of_subprocess, SIGTERM );
+ std::vector<double> ret;
+ try
+ {
+ ret = SALOME::ReadFloatsInFile( _out_filename );
+ }
+ catch(std::exception& e) { }
+ pid_of_subprocess = 0;
+ _out_filename.clear();
+ return ret;
+}
+#endif
+
+std::vector<double> SALOME::StopMonitoring()
+{
+#ifndef WIN32
+ return StopMonitoringLinux();
+#else
+ throw std::runtime_error("StopMonitoring not implemented for Windows !");
+#endif
+}
--- /dev/null
+// Copyright (C) 2023 CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#pragma once
+
+#include "SALOME_Basics.hxx"
+
+#include <string>
+#include <vector>
+
+namespace SALOME
+{
+ void BASICS_EXPORT LaunchMonitoring(const std::string& pyScriptToEvaluate, const std::string& outFileName);
+
+ std::vector<double> BASICS_EXPORT ReadFloatsInFile(const std::string& fileName);
+
+ std::vector<double> BASICS_EXPORT StopMonitoring();
+}
import SALOME__POA
import SALOME
import logging
+import os
MY_CONTAINER_ENTRY_IN_GLBS = "my_container"
return retObj
else:
return obj
-
+
+class FileDeleter:
+ def __init__(self, fileName):
+ self._filename = fileName
+ @property
+ def filename(self):
+ return self._filename
+ def __del__(self):
+ import os
+ if os.path.exists( self._filename ):
+ os.unlink( self._filename )
+
+def LaunchMonitoring( intervalInMs ):
+ """
+ Launch a subprocess monitoring self process.
+ This monitoring subprocess is a python process lauching every intervalInMs ms evaluation of
+ CPU usage and RSS memory.
+ Communication between subprocess and self is done by file.
+ """
+ import KernelBasis
+ def BuildPythonFileForCPUPercent( intervalInMs ):
+ import os
+ import tempfile
+ with tempfile.NamedTemporaryFile(prefix="htop_",suffix=".py") as f:
+ tempPyFile = f.name
+ tempOutFile = "{}.txt".format( os.path.splitext( tempPyFile )[0] )
+ pid = os.getpid()
+ with open(tempPyFile,"w") as f:
+ f.write("""import psutil
+pid = {}
+process = psutil.Process( pid )
+import time
+with open("{}","a") as f:
+ while True:
+ f.write( "{{}}\\n".format( str( process.cpu_percent() ) ) )
+ f.write( "{{}}\\n".format( str( process.memory_info().rss ) ) )
+ f.flush()
+ time.sleep( {} / 1000.0 )
+""".format(pid, tempOutFile, intervalInMs))
+ return FileDeleter(tempPyFile), FileDeleter(tempOutFile)
+ pyFileName, outFileName = BuildPythonFileForCPUPercent( intervalInMs )
+ KernelBasis.LaunchMonitoring(pyFileName.filename,outFileName.filename)
+ return pyFileName, outFileName
+
+def StopMonitoring( ):
+ """
+ Retrieve data of monitoring and kill monitoring subprocess.
+
+ Returns
+ -------
+ list<float,str> : list of pairs. First param of pair is CPU usage. Second param of pair is rss memory usage
+ """
+ import KernelBasis
+ from SALOME_ContainerHelper import ScriptExecInfo
+ ret = KernelBasis.StopMonitoring()
+ cpu = ret[::2]
+ mem_rss = [ScriptExecInfo.MemRepr( int(elt) ) for elt in ret[1::2]]
+ return [(a,b) for a,b in zip(cpu,mem_rss)]
+
class SeqByteReceiver:
# 2GB limit to trigger split into chunks
CHUNK_SIZE = 2000000000
import sys
try:
self.my_container_py.addTimeInfoOnLevel2(self.getIDInContainer(),self._current_exec,"startExecTime")
+ pyfile = BuildPythonFileForCPUPercent()
+
exec(self.ccode, self.context)
self.my_container_py.addTimeInfoOnLevel2(self.getIDInContainer(),self._current_exec,"endExecTime")
self.my_container_py.addTimeInfoOnLevel2(self.getIDInContainer(),self._current_exec,"startOutputTime")