Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JProfiler.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <unistd.h>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TH1D.h"
10 #include "TGraph.h"
11 
12 #include "JROOT/JRootToolkit.hh"
13 
14 #include "JSystem/JShell.hh"
16 #include "JSystem/JKeypress.hh"
17 #include "JSystem/JTime.hh"
18 
19 #include "Jeep/JPrint.hh"
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 
24 /**
25  * \file
26  *
27  * Auxiliary program to monitor memory and CPU usage of process.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  string outputFile;
36  string process;
37  unsigned int T_us;
38  bool interactive;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Auxiliary program to monitor memory and CPU usage of process.");
44 
45  zap['o'] = make_field(outputFile, "ROOT output file") = "profile.root";
46  zap['P'] = make_field(process, "name of process");
47  zap['T'] = make_field(T_us, "interval time [us]") = 100000;
48  zap['u'] = make_field(interactive, "run in interactive mode");
49  zap['d'] = make_field(debug) = 1;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57  JShell shell;
58  JKeypress keypress(false);
59 
60  const char QUIT = 'q';
61 
62  if (interactive) {
63  cout << "Press '" << QUIT << "' to quit." << endl;
64  }
65 
66  int pid = -1;
67 
68  for (int i = 0; ;++i) {
69 
70  try {
71 
72  pid = getPID(shell, process.c_str());
73 
74  break;
75  }
76  catch(const exception& error) {
77 
78  if (interactive || debug >= debug_t) {
79  cout << "No process " << setw(8) << i << endl;
80  }
81 
82  if (keypress.timeout(T_us)) {
83  if (keypress.get() == QUIT && interactive) {
84  break;
85  }
86  }
87  }
88  }
89 
90  NOTICE("Process identifier " << pid << endl);
91 
92  if (pid == -1) {
93  FATAL("Invalid process identifier " << pid << endl);
94  }
95 
96  TH1D h0("h0", NULL, 101,-0.5, 100.5);
97  TH1D h1("h1", NULL, 1001,-0.5, 1000.5);
98 
100  vector<double> Y;
101  vector<double> Z;
102 
103  localtime_t t0 = getLocalTime();
104 
105  for (int i = 0; ;++i) {
106 
107  try {
108 
109  const double t1 = double (getLocalTime() - t0) * 1.0e-6;
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(t1);
117  Y.push_back(mem);
118  Z.push_back(cpu);
119 
120  if (interactive || debug >= debug_t) {
121  cout << setw(8) << i << ' ' << FIXED(12,3) << t1 << ' ' << 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 }
string outputFile
Keyboard settings for unbuffered input.
General purpose messaging.
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
I/O formatting auxiliaries.
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:72
int main(int argc, char **argv)
Definition: JProfiler.cc:30
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
Shell interaction via I/O streams.
System auxiliaries.
System time information.
Utility class to parse command line options.
Definition: JParser.hh:1698
Enable unbuffered terminal input.
Definition: JKeypress.hh:32
bool timeout(JTimeval timeout)
Timeout method.
Definition: JKeypress.hh:92
char get()
Get single character.
Definition: JKeypress.hh:74
The JShell clas can be used to interact with the shell via I/O streams.
Definition: JShell.hh:36
@ debug_t
debug
Definition: JMessage.hh:29
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.
long long int localtime_t
Type definition of local time.
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.
pid_t getPID(JShell &shell, const char *process)
Get process identifier.
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448