Jpp  15.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JProfiler.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <unistd.h>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TH1D.h"
11 #include "TGraph.h"
12 
13 #include "JROOT/JRootToolkit.hh"
14 
15 #include "JSystem/JShell.hh"
17 #include "JSystem/JKeypress.hh"
18 #include "JSystem/JTime.hh"
19 
20 #include "Jeep/JPrint.hh"
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 /**
26  * \file
27  *
28  * Auxiliary program to monitor memory and CPU usage of process.
29  * \author mdejong
30  */
31 int main(int argc, char **argv)
32 {
33  using namespace std;
34  using namespace JPP;
35 
36  string outputFile;
37  string process;
38  unsigned int T_us;
39  bool interactive;
40  int debug;
41 
42  try {
43 
44  JParser<> zap("Auxiliary program to monitor memory and CPU usage of process.");
45 
46  zap['o'] = make_field(outputFile, "ROOT output file") = "profile.root";
47  zap['P'] = make_field(process, "name of process");
48  zap['T'] = make_field(T_us, "interval time [us]") = 100000;
49  zap['u'] = make_field(interactive, "run in interactive mode");
50  zap['d'] = make_field(debug) = 1;
51 
52  zap(argc, argv);
53  }
54  catch(const exception &error) {
55  FATAL(error.what() << endl);
56  }
57 
58  JShell shell;
59  JKeypress keypress(false);
60 
61  const char QUIT = 'q';
62 
63  if (interactive) {
64  cout << "Press '" << QUIT << "' to quit." << endl;
65  }
66 
67  int pid = -1;
68 
69  for (int i = 0; ;++i) {
70 
71  try {
72 
73  pid = getPID(shell, process.c_str());
74 
75  break;
76  }
77  catch(const exception& error) {
78 
79  if (interactive || debug >= debug_t) {
80  cout << "No process " << setw(8) << i << endl;
81  }
82 
83  if (keypress.timeout(T_us)) {
84  if (keypress.get() == QUIT && interactive) {
85  break;
86  }
87  }
88  }
89  }
90 
91  NOTICE("Process identifier " << pid << endl);
92 
93  if (pid == -1) {
94  FATAL("Invalid process identifier " << pid << endl);
95  }
96 
97  TH1D h0("h0", NULL, 101,-0.5, 100.5);
98  TH1D h1("h1", NULL, 101,-0.5, 100.5);
99 
103 
104  localtime_t t0 = getLocalTime();
105 
106  for (int i = 0; ;++i) {
107 
108  try {
109 
110  const double mem = getMemoryUsage(shell, pid);
111  const double cpu = getCpuUsage (shell, pid);
112 
113  h0.Fill(mem);
114  h1.Fill(cpu);
115 
116  X.push_back((getLocalTime() - t0) * 1.0e-6);
117  Y.push_back(mem);
118  Z.push_back(cpu);
119 
120  if (interactive || debug >= debug_t) {
121  cout << setw(8) << i << ' ' << FIXED(7,3) << mem << ' ' << FIXED(7,3) << cpu << endl;
122  }
123 
124  if (keypress.timeout(T_us)) {
125  if (keypress.get() == QUIT && interactive) {
126  break;
127  }
128  }
129  }
130  catch(const exception& error) {
131  break;
132  }
133  }
134 
135 
136  TGraph g0(X.size(), X.data(), Y.data());
137  TGraph g1(X.size(), X.data(), Z.data());
138 
139  g0.SetName(MAKE_CSTRING(process << '.' << "mem"));
140  g1.SetName(MAKE_CSTRING(process << '.' << "cpu"));
141 
142  TFile out(outputFile.c_str(), "recreate");
143 
144  out << h0 << h1;
145  out << g0 << g1;
146 
147  out.Write();
148  out.Close();
149 }
Utility class to parse command line options.
Definition: JParser.hh:1500
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
I/O formatting auxiliaries.
Keyboard settings for unbuffered input.
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.
#define NOTICE(A)
Definition: JMessage.hh:64
then break fi done getCenter read X Y Z let X
pid_t getPID(JShell &shell, const char *process)
Get process identifier.
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
long long int localtime_t
Type definition of local time.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
Shell interaction via I/O streams.
System auxiliaries.
do set_variable MODULE getModule a $WORKDIR detector_a datx L $STRING JEditDetector a $WORKDIR detector_a datx M $MODULE setz o $WORKDIR detector_a datx JEditDetector a $WORKDIR detector_b datx M $MODULE setz o $WORKDIR detector_b datx done echo Output stored at $WORKDIR detector_a datx and $WORKDIR tripod_a txt JDrawDetector2D a $WORKDIR detector_a datx a $WORKDIR detector_b datx L BL o detector $FORMAT $BATCH JDrawDetector2D T $WORKDIR tripod_a txt T $WORKDIR tripod_b txt L BL o tripod $FORMAT $BATCH JCompareDetector a $WORKDIR detector_a datx b $WORKDIR detector_b datx o $WORKDIR abc root &dev null for KEY in X Y Z
System time information.
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25