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