Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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

◆ main()

int main ( int argc,
char ** argv )

Definition at line 30 of file JProfiler.cc.

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
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
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
#define MAKE_CSTRING(A)
Make C-string.
Definition JPrint.hh:72
Double_t g1(const Double_t x)
Function.
Definition JQuantiles.cc:25
Utility class to parse command line options.
Definition JParser.hh:1698
Enable unbuffered terminal input.
Definition JKeypress.hh:32
The JShell clas can be used to interact with the shell via I/O streams.
Definition JShell.hh:36
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
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.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448