]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
[EDF29150] : Base mechanism to keep track of CPU and Mem info
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 2 Jan 2024 10:24:33 +0000 (11:24 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 2 Jan 2024 10:24:33 +0000 (11:24 +0100)
src/Basics/CMakeLists.txt
src/Basics/KernelBasis.i
src/Basics/Monitoring.cxx [new file with mode: 0644]
src/Basics/Monitoring.hxx [new file with mode: 0644]
src/Container/SALOME_PyNode.py

index 721defd8b38bf6e110acd6f8583aa93a7fc822ad..b3d7815119a5899fe0fe2cd9079c938cc54cd6cd 100644 (file)
@@ -35,6 +35,7 @@ SET(SALOMEBasics_SOURCES
   Basics_DirUtils.cxx
   KernelBasis.cxx
   HeatMarcel.cxx
+  Monitoring.cxx
 )
 
 ADD_LIBRARY(SALOMELog ${SALOMELog_SOURCES})
index cf78f1d79d556bd4a0ad6d6a71e4ef5471af538c..3b733b01a99a7f69885667706bc1022777856940 100644 (file)
 #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 
@@ -58,6 +62,10 @@ void setIOROfEmbeddedNS(const std::string& ior);
 
 double GetTimeAdjustmentCst();
 
+void LaunchMonitoring(const std::string& pyScriptToEvaluate, const std::string& outFileName);
+
+std::vector<double> StopMonitoring();
+
 bool VerbosityActivated();
 
 void SetVerbosityActivated(bool flag);
diff --git a/src/Basics/Monitoring.cxx b/src/Basics/Monitoring.cxx
new file mode 100644 (file)
index 0000000..0fa97dd
--- /dev/null
@@ -0,0 +1,137 @@
+// 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
+}
diff --git a/src/Basics/Monitoring.hxx b/src/Basics/Monitoring.hxx
new file mode 100644 (file)
index 0000000..3792188
--- /dev/null
@@ -0,0 +1,34 @@
+// 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();
+}
index 7c1985d0d9ef8505eabdaeafec5d10dd9b846af1..d93751eaf4dadbff7d51a5e1674d5dda2e06ae9c 100644 (file)
@@ -30,6 +30,7 @@ import Engines__POA
 import SALOME__POA
 import SALOME
 import logging
+import os
 
 MY_CONTAINER_ENTRY_IN_GLBS = "my_container"
 
@@ -398,7 +399,65 @@ def UnProxyObjectSimpleLocal( obj ):
     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
@@ -550,6 +609,8 @@ class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
     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")