Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTop.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 #include <fstream>
5 #include <iomanip>
6 #include <vector>
7 #include <map>
8 #include <chrono>
9 #include <unistd.h>
10 
11 #include "JSystem/JShell.hh"
12 
13 #include "Jeep/JPrint.hh"
14 #include "Jeep/JParser.hh"
15 #include "Jeep/JMessage.hh"
16 
17 namespace {
18  struct zbuf : std::filebuf
19  {
20  void rewind() { std::filebuf::seekpos(0, std::ios_base::out); }
21  };
22 }
23 
24 /**
25  * \file
26  *
27  * Auxiliary program to continually monitor processes.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  vector<string> process;
36  string outputFile;
37  unsigned int T_us;
38  long int prescale;
39  string master;
40  int debug;
41 
42  try {
43 
44  JParser<> zap("Auxiliary program to continually monitor processes.");
45 
46  zap['P'] = make_field(process, "name of process");
47  zap['o'] = make_field(outputFile, "output file") = "";
48  zap['T'] = make_field(T_us, "interval time [us]") = 1000;
49  zap['n'] = make_field(prescale, "pre-scale for logging") = 1000;
50  zap['M'] = make_field(master, "stop when master does") = "";
51  zap['d'] = make_field(debug) = 1;
52 
53  zap(argc, argv);
54  }
55  catch(const exception &error) {
56  FATAL(error.what() << endl);
57  }
58 
59  if (process.empty()) {
60  FATAL("No process." << endl);
61  }
62 
63  JShell shell;
64  string ps;
65  {
66  ostringstream os;
67 
68  os << "ps -o comm= -o pid=";
69 
70  for (vector<string>::const_iterator i = process.begin(); i != process.end(); ++i) {
71  os << " -C " << *i;
72  }
73 
74  if (master != "") {
75  os << " -C " << master;
76  }
77 
78  ps = os.str();
79 
80  DEBUG(ps << endl);
81  }
82 
83  zbuf buffer;
84 
85  if (outputFile != "") {
86  buffer.open(outputFile.c_str(), ios::out);
87  }
88 
89  ostream os(outputFile != "" ? &buffer : cout.rdbuf());
90 
91  enum {
92  UNDEFINED = 0,
93  STOPPED = +1,
94  RUNNING = +2
95  };
96 
97  int monitor = (master != "" ? UNDEFINED : RUNNING);
98 
99  const auto start = chrono::system_clock::now();
100 
102 
103  for (long int count = 1; monitor != STOPPED; usleep(T_us)) {
104 
105  if (master != "" && monitor == RUNNING) {
106  monitor = STOPPED;
107  }
108 
109  shell << ps << endl;
110 
111  for (string buffer; shell.getline(buffer); ) {
112 
113  string command;
114  int pid;
115 
116  istringstream is(buffer);
117 
118  if (is >> command >> pid) {
119 
120  top[command] += 1;
121 
122  if (command == master) {
123  monitor = RUNNING;
124  }
125 
126  } else {
127 
128  ERROR("Error reading \"" << buffer << "\"" << endl);
129  }
130  }
131 
132  if (monitor == RUNNING) {
133  ++count;
134  }
135 
136  if (count%prescale == 0) {
137 
138  buffer.rewind();
139 
140  os << "top " << FIXED(9,1) << chrono::duration<double>(chrono::system_clock::now() - start).count() << " [s]" << endl;
141 
142  long int total = 0;
143 
144  for (vector<string>::const_iterator i = process.begin(); i != process.end(); ++i) {
145 
146  const long int value = top[*i];
147 
148  os << setw(24) << left << *i << ' ' << FIXED(5,3) << (double) value / (double) count << endl;
149 
150  total += value;
151  }
152  {
153  os << setw(24) << left << "Total" << ' ' << FIXED(5,3) << (double) total / (double) count << endl;
154  }
155  }
156  }
157 
158  buffer.close();
159 }
Utility class to parse command line options.
Definition: JParser.hh:1514
then echo Test string reversed by master(hit< return > to continue)." $DIR/JProcess -c "$DIR/JEcho" -rC fi if (( 1 ))
int main(int argc, char *argv[])
Definition: Main.cc:15
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
is
Definition: JDAQCHSM.chsm:167
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JManip.hh:222
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define ERROR(A)
Definition: JMessage.hh:66
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
Shell interaction via I/O streams.
static void monitor()
Definition: DataQueue.cc:32
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62