#include #include #include #include #include #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" /** * \file * * Auxiliary program to monitor memory and CPU usage of process. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; string outputFile; string process; unsigned int T_us; bool interactive; int debug; try { JParser<> zap("Auxiliary program to monitor memory and CPU usage of process."); zap['o'] = make_field(outputFile, "ROOT output file") = "profile.root"; zap['P'] = make_field(process, "name of process"); zap['T'] = make_field(T_us, "interval time [us]") = 100000; zap['u'] = make_field(interactive, "run in interactive mode"); zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } JShell shell; JKeypress keypress(false); const char QUIT = 'q'; if (interactive) { cout << "Press '" << QUIT << "' to quit." << endl; } int pid = -1; for (int i = 0; ;++i) { try { pid = getPID(shell, process.c_str()); break; } catch(const exception& error) { if (interactive || debug >= debug_t) { cout << "No process " << setw(8) << i << endl; } if (keypress.timeout(T_us)) { if (keypress.get() == QUIT && interactive) { break; } } } } NOTICE("Process identifier " << pid << endl); if (pid == -1) { FATAL("Invalid process identifier " << pid << endl); } TH1D h0("h0", NULL, 101,-0.5, 100.5); TH1D h1("h1", NULL, 1001,-0.5, 1000.5); vector X; vector Y; vector Z; localtime_t t0 = getLocalTime(); for (int i = 0; ;++i) { try { const double t1 = double (getLocalTime() - t0) * 1.0e-6; const double mem = getMemoryUsage(shell, pid); const double cpu = getCpuUsage (shell, pid); h0.Fill(mem); h1.Fill(cpu); X.push_back(t1); Y.push_back(mem); Z.push_back(cpu); if (interactive || debug >= debug_t) { cout << setw(8) << i << ' ' << FIXED(12,3) << t1 << ' ' << FIXED(7,3) << mem << ' ' << FIXED(7,3) << cpu << endl; } if (keypress.timeout(T_us)) { if (keypress.get() == QUIT && interactive) { break; } } } catch(const exception& error) { break; } } TGraph g0(X.size(), X.data(), Y.data()); TGraph g1(X.size(), X.data(), Z.data()); g0.SetName(MAKE_CSTRING(process << '.' << "mem")); g1.SetName(MAKE_CSTRING(process << '.' << "cpu")); TFile out(outputFile.c_str(), "recreate"); out << h0 << h1; out << g0 << g1; out.Write(); out.Close(); }