Salome HOME
690490c04046d23a7ea9b3897c48201b408b36d0
[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 <cerrno>
42 #include <cstring>
43 #include <sys/types.h>
44 #include <signal.h>
45 #endif
46
47 static std::string _out_filename;
48 #ifndef WIN32
49 static pid_t pid_of_subprocess = 0;
50 #endif
51
52 #ifndef WIN32
53 static void LaunchMonitoringLinux(const std::string& pyScriptToEvaluate, const std::string& outFileName)
54 {
55   constexpr char PYTHON_EXEC[] = "python3";
56   pid_t pid = fork();
57   if (pid == -1)
58   {
59     throw std::runtime_error("LaunchMonitoring : Error at creation of sub process !");
60   }
61   else if( pid == 0)
62   {
63     execlp(PYTHON_EXEC,PYTHON_EXEC,pyScriptToEvaluate.c_str(),nullptr);
64     char buff[1024];
65     std::ostringstream oss; oss << "LaunchMonitoring : Error during exe : " << strerror_r(errno, buff, sizeof(buff));
66     throw std::runtime_error( oss.str() );
67   }
68   else
69   {
70     pid_of_subprocess = pid;
71   }
72 }
73 #endif
74
75 void SALOME::LaunchMonitoring(const std::string& pyScriptToEvaluate, const std::string& outFileName)
76 {
77   _out_filename = outFileName;
78 #ifndef WIN32
79   LaunchMonitoringLinux(pyScriptToEvaluate,outFileName);
80 #else
81   throw std::runtime_error("LaunchMonitoring not implemented for Windows !");
82 #endif
83 }
84
85 std::vector<double> SALOME::ReadFloatsInFile(const std::string& fileName)
86 {
87   std::ifstream inputFile( fileName );
88
89   if(!inputFile.is_open())
90   {
91     std::ostringstream oss; oss << "Impossible to open file \"" << _out_filename<< "\" !";
92     throw std::runtime_error( oss.str() );
93   }
94   std::vector<double> ret;
95   std::string line;
96   try
97   {
98       while (std::getline(inputFile, line))
99       {
100         std::istringstream iss(line);
101         double floatValue;
102         if( !(iss >> floatValue) )
103           throw std::invalid_argument("Conversion into FP failed !");
104         if( !iss.eof() )
105           throw std::invalid_argument("Conversion into FP failed !");
106         ret.push_back(floatValue);
107       }
108       inputFile.close();
109   }
110   catch (const std::exception& e)
111   {
112   }
113   return ret;
114 }
115
116 #ifndef WIN32
117 static std::vector<double> StopMonitoringLinux()
118 {
119   kill( pid_of_subprocess, SIGTERM );
120   std::vector<double> ret;
121   try
122   {
123     ret = SALOME::ReadFloatsInFile( _out_filename );
124   }
125   catch(std::exception& e) { }
126   pid_of_subprocess = 0;
127   _out_filename.clear();
128   return ret;
129 }
130 #endif
131
132 std::vector<double> SALOME::StopMonitoring()
133 {
134 #ifndef WIN32
135   return StopMonitoringLinux();
136 #else
137   throw std::runtime_error("StopMonitoring not implemented for Windows !");
138 #endif
139 }