Jpp  18.2.1-ARCA-DF-PATCH
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JTop.cc File Reference

Auxiliary program to continually monitor processes. More...

#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <map>
#include <chrono>
#include <unistd.h>
#include "JSystem/JShell.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to continually monitor processes.

Author
mdejong

Definition in file JTop.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file JTop.cc.

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 ))
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
is
Definition: JDAQCHSM.chsm:167
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
static void monitor()
Definition: DataQueue.cc:32
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62