Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JProfiler.cc File Reference

Auxiliary program to monitor memory and CPU usage of process. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <unistd.h>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TGraph.h"
#include "JROOT/JRootToolkit.hh"
#include "JSystem/JShell.hh"
#include "JSystem/JSystemToolkit.hh"
#include "JSystem/JKeypress.hh"
#include "JSystem/JTime.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 monitor memory and CPU usage of process.

Author
mdejong

Definition in file JProfiler.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 31 of file JProfiler.cc.

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:1514
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.
debug
Definition: JMessage.hh:29
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
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
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:1989
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.
#define NOTICE(A)
Definition: JMessage.hh:64
pid_t getPID(JShell &shell, const char *process)
Get process identifier.
long long int localtime_t
Type definition of local time.
#define FATAL(A)
Definition: JMessage.hh:67
no fit printf nominal n $STRING awk v X
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
int debug
debug level
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25