REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestBenchMarkProcess.cxx
1 #include "TRestBenchMarkProcess.h"
2 
3 #include "TRestManager.h"
4 #include "TRestProcessRunner.h"
5 #ifndef __APPLE__
6 #include "sys/sysinfo.h"
7 #else
8 #include <unistd.h>
9 #endif
10 #include "sys/wait.h"
11 
12 using namespace std;
13 
14 ClassImp(TRestBenchMarkProcess);
15 
16 thread* TRestBenchMarkProcess::fMonitorThread = nullptr;
17 int TRestBenchMarkProcess::fMonitorFlag = 0;
19 float TRestBenchMarkProcess::fMemUsageInMB = 0;
20 float TRestBenchMarkProcess::fReadingInMBs = 0;
21 float TRestBenchMarkProcess::fProcessSpeedInHz = 0;
22 int TRestBenchMarkProcess::fLastEventNumber = 0;
23 ULong64_t TRestBenchMarkProcess::fStartTime = 0;
24 
25 TRestBenchMarkProcess::TRestBenchMarkProcess() { Initialize(); }
26 
28  fEvent = nullptr;
29 #ifndef __APPLE__
30  fCPUNumber = get_nprocs_conf();
31  fMemNumber = get_phys_pages();
32 #else
33  fCPUNumber = 0;
34  fMemNumber = 0;
35  RESTError << "TRestBenchMarkProcess is not available yet on MACOS!!" << RESTendl;
36 #endif
37  fPid = getpid();
38  fRefreshRate = 10;
39 }
40 
41 void TRestBenchMarkProcess::SysMonitorFunc(int pid, double refreshRate) {
42  while (fMonitorFlag == 1) {
43  string topOutput = TRestTools::Execute(Form("top -bn 1 -p %i | grep %i", pid, pid));
44  auto topItems = Split(topOutput, " ", false, true);
45 
46  fCPUUsageInPct = StringToDouble(topItems[8]);
47  fMemUsageInMB =
48  StringToInteger(topItems[4]) + StringToInteger(topItems[5]) + StringToInteger(topItems[6]);
49  fMemUsageInMB /= 1000; // convert kB to MB
50  fReadingInMBs = fHostmgr->GetProcessRunner()->GetReadingSpeed() / 1024 / 1024; // convert byte to MB
51  int Neve = fHostmgr->GetProcessRunner()->GetNProcessedEvents();
52  fProcessSpeedInHz = (Neve - fLastEventNumber) * refreshRate;
53 
54  usleep(1e6 / refreshRate);
55  }
56 }
57 
59  if (fHostmgr == nullptr || fHostmgr->GetProcessRunner() == nullptr) {
60  RESTError << "TRestBenchMarkProcess: the process is not hosted by TRestProcessRunner!" << RESTendl;
61  exit(1);
62  }
63 
64  // init external thread for system info service
65  if (fMonitorThread == nullptr) {
66  fMonitorFlag = 1;
67  // SysMonitorFunc(fPid, fRefreshRate);
68  fMonitorThread = new thread(&TRestBenchMarkProcess::SysMonitorFunc, this, fPid, fRefreshRate);
69  fMonitorThread->detach();
70  }
71 
72  fStartTime = chrono::high_resolution_clock::now().time_since_epoch().count();
73 }
74 
76  fEvent = inputEvent;
77 
78  ULong64_t time = chrono::high_resolution_clock::now().time_since_epoch().count();
79  SetObservableValue("RunningTime", (time - fStartTime) / 1e9);
80  SetObservableValue("EventPerSecond", fProcessSpeedInHz);
81  SetObservableValue("ReadingSpeedMBs", fReadingInMBs);
82  SetObservableValue("CPUPrecentage", fCPUUsageInPct);
83  SetObservableValue("MemoryUsedMB", fMemUsageInMB);
84 
85  return fEvent;
86 }
87 
89  if (fMonitorThread != nullptr) {
90  fMonitorFlag = 0;
91  usleep(1e6 / fRefreshRate * 2);
92  // fMonitorThread->join();
93  delete fMonitorThread;
94  fMonitorThread = nullptr;
95  }
96 }
97 
99  BeginPrintProcess();
100 
101  RESTMetadata << "REST pid: " << fPid << RESTendl;
102  RESTMetadata << "Number of CPUs: " << fCPUNumber << RESTendl;
103  RESTMetadata << "Total Memory: " << round((double)fMemNumber / 1024 / 1024 * 10) / 10 << " GB"
104  << RESTendl;
105  RESTMetadata << "System information refresh rate: " << fRefreshRate << " Hz" << RESTendl;
106  RESTMetadata << "Monitoring thread: " << fMonitorThread
107  << ", status: " << (fMonitorFlag == 0 ? "stopped" : "running") << RESTendl;
108 
109  EndPrintProcess();
110 }
A system performance monitor process for event flow rate, reading speed, cpu stress,...
static float fCPUUsageInPct
//0: return, 1: run
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
void Initialize() override
Making default settings.
void EndProcess() override
To be executed at the end of the run (outside event loop)
A base class for any REST event.
Definition: TRestEvent.h:38
static std::string Execute(std::string cmd)
Executes a shell command and returns its output in a string.
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.
Double_t StringToDouble(std::string in)
Gets a double from a string.
Int_t StringToInteger(std::string in)
Gets an integer from a string.