Salome HOME
[EDF29150] : log performance of python scripts run inside SALOME container + verbosit...
[modules/kernel.git] / src / Basics / Monitoring.cxx
1 // Copyright (C) 2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Monitoring.hxx"
21
22 #include "baseutilities.h"
23
24 #include <cstdint>
25 #include <cmath>
26 #include <vector>
27 #include <numeric>
28 #include <iomanip>
29 #include <iostream>
30
31 #include <sstream>
32 #include <array>
33 #include <stdexcept>
34 #include <thread>
35 #include <stdio.h>
36 #include <chrono>
37 #include <fstream>
38
39 #ifndef WIN32
40 #include <unistd.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <signal.h>
44 #endif
45
46 static std::string _out_filename;
47 #ifndef WIN32
48 static pid_t pid_of_subprocess = 0;
49 #endif
50
51 #ifndef WIN32
52 static void LaunchMonitoringLinux(const std::string& pyScriptToEvaluate, const std::string& outFileName)
53 {
54   constexpr char PYTHON_EXEC[] = "python3";
55   pid_t pid = fork();
56   if (pid == -1)
57   {
58     throw std::runtime_error("LaunchMonitoring : Error at creation of sub process !");
59   }
60   else if( pid == 0)
61   {
62     execlp(PYTHON_EXEC,PYTHON_EXEC,pyScriptToEvaluate.c_str(),nullptr);
63     std::ostringstream oss; oss << "LaunchMonitoring : Error during exe : " << sys_errlist[errno];
64     throw std::runtime_error( oss.str() );
65   }
66   else
67   {
68     pid_of_subprocess = pid;
69   }
70 }
71 #endif
72
73 void SALOME::LaunchMonitoring(const std::string& pyScriptToEvaluate, const std::string& outFileName)
74 {
75   _out_filename = outFileName;
76 #ifndef WIN32
77   LaunchMonitoringLinux(pyScriptToEvaluate,outFileName);
78 #else
79   throw std::runtime_error("LaunchMonitoring not implemented for Windows !");
80 #endif
81 }
82
83 std::vector<double> SALOME::ReadFloatsInFile(const std::string& fileName)
84 {
85   std::ifstream inputFile( fileName );
86
87   if(!inputFile.is_open())
88   {
89     std::ostringstream oss; oss << "Impossible to open file \"" << _out_filename<< "\" !";
90     throw std::runtime_error( oss.str() );
91   }
92   std::vector<double> ret;
93   std::string line;
94   try
95   {
96       while (std::getline(inputFile, line))
97       {
98         std::istringstream iss(line);
99         double floatValue;
100         if( !(iss >> floatValue) )
101           throw std::invalid_argument("Conversion into FP failed !");
102         if( !iss.eof() )
103           throw std::invalid_argument("Conversion into FP failed !");
104         ret.push_back(floatValue);
105       }
106       inputFile.close();
107   }
108   catch (const std::exception& e)
109   {
110   }
111   return ret;
112 }
113
114 #ifndef WIN32
115 static std::vector<double> StopMonitoringLinux()
116 {
117   kill( pid_of_subprocess, SIGTERM );
118   std::vector<double> ret;
119   try
120   {
121     ret = SALOME::ReadFloatsInFile( _out_filename );
122   }
123   catch(std::exception& e) { }
124   pid_of_subprocess = 0;
125   _out_filename.clear();
126   return ret;
127 }
128 #endif
129
130 std::vector<double> SALOME::StopMonitoring()
131 {
132 #ifndef WIN32
133   return StopMonitoringLinux();
134 #else
135   throw std::runtime_error("StopMonitoring not implemented for Windows !");
136 #endif
137 }