Skip to content

Commit 7f9d1c4

Browse files
Julian Myrchashahor02
authored andcommitted
o2-eve: adding maximum allowed memory option
1 parent 5c3b378 commit 7f9d1c4

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

EventVisualisation/View/include/EventVisualisationView/EventManagerFrame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class EventManagerFrame : public TGMainFrame
4141
bool inTick = false;
4242
bool setInTick(); // try set inTick, return true if set, false if already set
4343
void clearInTick(); // safely clears inTick
44+
void checkMemory(); // check memory used end exit(-1) if it is too much
4445
static TGTextButton* makeButton(TGCompositeFrame* p, const char* txt, Int_t width = 0,
4546
Int_t lo = 0, Int_t ro = 0, Int_t to = 0, Int_t bo = 0);
4647
bool CopyImage(TASImage* dst, TASImage* src, Int_t x_dst, Int_t y_dst, Int_t x_src, Int_t y_src, UInt_t w_src, UInt_t h_src);

EventVisualisation/View/include/EventVisualisationView/Options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Options
3333
std::string mFileName; // -f 'data.root'
3434
std::string mDataFolder; // -d './'
3535
std::string mSavedDataFolder; // -s './'
36+
long mMemoryLimit; // -m 1500 (MB) = 1.5GB
3637

3738
// helper methods
3839
static Options instance;
@@ -43,6 +44,7 @@ class Options
4344
mFileName = "data.root";
4445
mDataFolder = "./"; // current working directory
4546
mSavedDataFolder = ""; // not use
47+
mMemoryLimit = -1; // not use
4648
}
4749

4850
public:
@@ -58,6 +60,7 @@ class Options
5860
std::string savedDataFolder() { return this->mSavedDataFolder; }
5961
std::string fileName() { return this->mFileName; }
6062
bool randomTracks() { return this->mRandomTracks; }
63+
long memoryLimit() { return this->mMemoryLimit; }
6164
};
6265

6366
} // namespace event_visualisation

EventVisualisation/View/src/EventManagerFrame.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,35 @@ namespace o2
202202
clearInTick();
203203
}
204204

205+
void EventManagerFrame::checkMemory()
206+
{
207+
const long memoryLimit = Options::Instance()->memoryLimit();
208+
if (memoryLimit != -1) {
209+
const char* statmPath = "/proc/self/statm";
210+
long size = -1;
211+
FILE* f = fopen(statmPath, "r");
212+
if (f != nullptr) { // could not read file => no check
213+
int success = fscanf(f, "%ld", &size);
214+
fclose(f);
215+
if (success == 1) { // properly readed
216+
size = 4 * size / 1024; // in MB
217+
LOG(INFO) << "Memory used: " << size << " memory allowed: " << memoryLimit;
218+
if (size > memoryLimit) {
219+
LOG(ERROR) << "Memory used: " << size << " exceeds memory allowed: "
220+
<< memoryLimit;
221+
exit(-1);
222+
}
223+
}
224+
}
225+
}
226+
}
227+
205228
void EventManagerFrame::DoTimeTick()
206229
{
207230
if (not setInTick()) {
208231
return;
209232
}
233+
checkMemory(); // exits if memory usage too high = prevents freezing long-running machine
210234
if (mEventManager->getDataSource()->refresh()) {
211235
mEventManager->displayCurrentEvent();
212236
}

EventVisualisation/View/src/Options.cxx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ std::string Options::usage()
5858
<< "-f name name of the data file" << std::endl;
5959
ss << "\t\t"
6060
<< "-j use json files as a source" << std::endl;
61+
ss << "\t\t"
62+
<< "-m limit maximum size of the memory which do not cause exiting" << std::endl;
6163
ss << "\t\t"
6264
<< "-o use online json files as a source" << std::endl;
6365
ss << "\t\t"
@@ -79,7 +81,7 @@ bool Options::processCommandLine(int argc, char* argv[])
7981
// put ':' in the starting of the
8082
// string so that program can
8183
//distinguish between '?' and ':'
82-
while ((opt = getopt(argc, argv, ":d:f:hijop:rs:vt")) != -1) {
84+
while ((opt = getopt(argc, argv, ":d:f:hijm:op:rs:vt")) != -1) {
8385
switch (opt) {
8486
case 'd':
8587
this->mDataFolder = optarg;
@@ -93,6 +95,9 @@ bool Options::processCommandLine(int argc, char* argv[])
9395
case 'j':
9496
this->mJSON = true;
9597
break;
98+
case 'm':
99+
this->mMemoryLimit = std::stol(std::string(optarg));
100+
break;
96101
case 'o':
97102
this->mOnline = true;
98103
break;

0 commit comments

Comments
 (0)