Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JSystemToolkit.hh
Go to the documentation of this file.
1#ifndef __JSYSTEMTOOLKIT__
2#define __JSYSTEMTOOLKIT__
3
4#include <iostream>
5#include <string>
6#include <vector>
7#include <unistd.h>
8
9#include "JLang/JException.hh"
10#include "JSystem/JShell.hh"
11#include "JSystem/JSysinfo.hh"
12
13
14/**
15 * \file
16 * System auxiliaries.
17 * \author mdejong
18 */
19
20/**
21 * Auxiliary classes and methods for operating system calls.
22 */
23namespace JSYSTEM {}
24namespace JPP { using namespace JSYSTEM; }
25
26namespace JSYSTEM {
27
29
30 /**
31 * Shell names.
32 */
33 static const char* const SH = "sh";
34 static const char* const ZSH = "zsh";
35 static const char* const KSH = "ksh";
36 static const char* const BASH = "bash";
37 static const char* const CSH = "csh";
38 static const char* const TCSH = "tcsh";
39
40 static const std::string SPECIAL_CHARACTERS = "\"' <>[](){};$#";
41
42
43
44 /**
45 * Get memory usage in percent of given process identifier.
46 *
47 * \param shell shell interface
48 * \param pid process identifier
49 * \return memory usage [%]
50 */
51 inline float getMemoryUsage(JShell& shell, const pid_t pid)
52 {
53 using namespace std;
54
55 float value = 0.0;
56
57 shell << "ps -o %mem= -p " << pid << endl;
58
59 if (shell.get(value))
60 return value;
61 else
62 THROW(JSystemException, "No process data for PID " << pid);
63 }
64
65
66 /**
67 * Get memory usage in percent of this process.
68 *
69 * \param shell shell interface
70 * \return memory usage [%]
71 */
72 inline float getMemoryUsage(JShell& shell)
73 {
74 return getMemoryUsage(shell, getpid());
75 }
76
77
78 /**
79 * Get memory usage in percent of this process.
80 *
81 * \return memory usage [%]
82 */
83 inline float getMemoryUsage()
84 {
85 return getMemoryUsage(JShell::getInstance(), getpid());
86 }
87
88
89 /**
90 * Get cpu usage in percent of given process identifier.
91 *
92 * \param shell shell interface
93 * \param pid process identifier
94 * \return cpu usage [%]
95 */
96 inline float getCpuUsage(JShell& shell, const pid_t pid)
97 {
98 using namespace std;
99
100 float value = 0.0;
101
102 shell << "ps -o %cpu= -p " << pid << endl;
103
104 if (shell.get(value))
105 return value;
106 else
107 THROW(JSystemException, "No process data for PID " << pid);
108 }
109
110
111 /**
112 * Get cpu usage in percent of this process.
113 *
114 * \param shell shell interface
115 * \return cpu usage [%]
116 */
117 inline float getCpuUsage(JShell& shell)
118 {
119 return getCpuUsage(shell, getpid());
120 }
121
122
123 /**
124 * Get cpu usage in percent of this process.
125 *
126 * \return cpu usage [%]
127 */
128 inline float getCpuUsage()
129 {
130 return getCpuUsage(JShell::getInstance(), getpid());
131 }
132
133
134 /**
135 * Get process identifier.
136 *
137 * \param shell shell interface
138 * \param process process name
139 * \return process identifier
140 */
141 inline pid_t getPID(JShell& shell, const char* process)
142 {
143 using namespace std;
144
145 pid_t pid = -1;
146
147 shell << "ps -o pid= -C " << process << endl;
148
149 if (shell.get(pid))
150 return pid;
151 else
152 THROW(JSystemException, "No process " << process);
153 }
154
155
156 /**
157 * Get process identifier.
158 *
159 * \param process process name
160 * \return process identifier
161 */
162 inline pid_t getPID(const char* process)
163 {
164 return getPID(JShell::getInstance(), process);
165 }
166
167
168 /**
169 * Get parent process identifier.
170 *
171 * \param shell shell interface
172 * \param pid process identifier
173 * \return parent identifier
174 */
175 inline pid_t getParentID(JShell& shell, pid_t pid)
176 {
177 using namespace std;
178
179 shell << "ps -o ppid= -p " << pid << endl;
180
181 if (shell.get(pid))
182 return pid;
183 else
184 THROW(JSystemException, "No parent identifier " << pid);
185 }
186
187
188 /**
189 * Get parent process identifier.
190 *
191 * \param pid process identifier
192 * \return parent identifier
193 */
194 inline pid_t getParentID(const pid_t pid)
195 {
196 return getParentID(JShell::getInstance(), getpid());
197 }
198
199
200 /**
201 * Get shell name.
202 *
203 * \param shell shell interface
204 * \return shell name
205 */
206 inline std::string getShell(JShell& shell)
207 {
208 using namespace std;
209
210 static string value = "";
211
212 if (value == "") {
213
214 pid_t pid = getppid();
215
216 shell << "ps -o ppid= -o args= -p " << pid << endl;
217
218 if (shell >> pid >> value) {
219
220 shell.flush();
221
222 if (!value.empty() && value[0] == '-') {
223 value = value.substr(1);
224 }
225
226 } else {
227
228 static_cast<istream&>(shell).clear();
229 shell.flush();
230
231 THROW(JSystemException, "No shell");
232 }
233 }
234
235 return value;
236 }
237
238
239 /**
240 * Get shell name of this process.
241 *
242 * \return shell name
243 */
244 inline std::string getShell()
245 {
247 }
248
249
250 /**
251 * Get RAM of this CPU.
252 *
253 * \return number of bytes
254 */
255 inline unsigned long long int getRAM()
256 {
257 const JSysinfo info;
258
259 return info.getTotalRAM();
260 }
261
262
263 /**
264 * Get process path.
265 *
266 * \param shell shell interface
267 * \param process process name
268 * \return path
269 */
270 inline const std::string which(JShell& shell, const char* process)
271 {
272 using namespace std;
273
274 string buffer;
275
276 shell << "which " << process << endl;
277
278 shell.getline(buffer);
279
280 shell.flush();
281
282 return buffer;
283 }
284
285
286 /**
287 * Get process path.
288 *
289 * \param process process name
290 * \return path
291 */
292 inline const std::string which(const char* process)
293 {
294 return which(JShell::getInstance(), process);
295 }
296
297
298 /**
299 * Print method.
300 *
301 * \param message message
302 */
303 inline void gprint(const std::string& message)
304 {
305 using namespace std;
306
307 istringstream is(message);
308
309 for (string buffer; getline(is, buffer); ) {
310
311 if (!buffer.empty()) {
312
313 for (string::size_type i = 0; i != buffer.size(); ++i) {
314 if (SPECIAL_CHARACTERS.find(buffer[i]) != string::npos) {
315 buffer.insert(i++, "\\");
316 }
317 }
318
319 cout << "echo " << buffer << ";" << endl;
320 }
321 }
322 }
323
324
325 /**
326 * Exit method.
327 *
328 * \param status exit status
329 * \param message optional message
330 */
331 inline int gexit(int status, const std::string& message = "")
332 {
333 using namespace std;
334
335 gprint(message);
336
337 cout << "exit " << status << ";" << endl;
338
339 return status;
340 }
341
342
343 /**
344 * Set environment variable.
345 *
346 * This method prints the shell command to set the variable with the given name to the specified value.
347 *
348 * \param name variable name
349 * \param value variable value
350 */
351 inline void set_variable(const std::string& name, const std::string& value)
352 {
353 using namespace std;
354 using namespace JSYSTEM;
355
356 static const string shell = getShell();
357
358 string buffer(value);
359
360 if (buffer.find(' ') != string::npos) {
361 buffer = "\"" + buffer + "\"";
362 }
363
364 if (shell.find(ZSH) != string::npos ||
365 shell.find(KSH) != string::npos ||
366 shell.find(BASH) != string::npos) {
367
368 cout << "export " << name << "=" << buffer << ";" << endl;
369
370 } else if (shell.find(CSH) != string::npos ||
371 shell.find(TCSH) != string::npos) {
372
373 cout << "setenv " << name << " " << buffer << ";" << endl;
374
375 } else {
376
377 THROW(JSystemException, "unknown shell " << shell);
378 }
379 }
380};
381
382#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Shell interaction via I/O streams.
System information.
Exception for system call.
The JShell clas can be used to interact with the shell via I/O streams.
Definition JShell.hh:36
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition JShell.hh:184
static JShell & getInstance()
Get reference to unique instance of this class object.
Definition JShell.hh:76
bool getline(std::string &buffer, const char eol='\n')
Get line of text.
Definition JShell.hh:137
bool get(T &value)
Get value.
Definition JShell.hh:106
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.
float getMemoryUsage()
Get memory usage in percent of this process.
static const char *const ZSH
static const char *const TCSH
unsigned long long int getRAM()
Get RAM of this CPU.
static const char *const BASH
pid_t getParentID(JShell &shell, pid_t pid)
Get parent process identifier.
static const char *const SH
Shell names.
static const std::string SPECIAL_CHARACTERS
int gexit(int status, const std::string &message="")
Exit method.
static const char *const CSH
float getCpuUsage()
Get cpu usage in percent of this process.
std::string getShell()
Get shell name of this process.
const std::string which(JShell &shell, const char *process)
Get process path.
static const char *const KSH
void gprint(const std::string &message)
Print method.
void set_variable(const std::string &name, const std::string &value)
Set environment variable.
pid_t getPID(JShell &shell, const char *process)
Get process identifier.
Auxiliary class for system information.
Definition JSysinfo.hh:38
unsigned long long int getTotalRAM() const
Get total RAM.
Definition JSysinfo.hh:64