Skip to content

Commit 23d1049

Browse files
committed
stats functions for memory available
1 parent 0fab786 commit 23d1049

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/ReadoutUtils.cxx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sstream>
1515
#include <stdio.h>
1616
#include <unistd.h>
17+
#include <filesystem>
1718

1819
#include "RAWDataHeader.h"
1920

@@ -179,3 +180,47 @@ int getIntegerListFromString(const std::string& input, std::vector<int>& output)
179180
}
180181
return 0;
181182
}
183+
184+
int getStatsFreeMemory(unsigned long long &freeBytes) {
185+
FILE *fp;
186+
const int lineBufSz = 128;
187+
char lineBuf[lineBufSz];
188+
long long value;
189+
int success = 0;
190+
freeBytes = 0;
191+
192+
if ((fp = fopen("/proc/meminfo", "r")) != NULL) {
193+
194+
while (fgets(lineBuf, lineBufSz, fp) != NULL) {
195+
if ( sscanf(lineBuf, "MemFree: %lld kB", &value) == 1) {
196+
freeBytes = ((unsigned long long)value) * 1024;
197+
success = 1;
198+
break;
199+
}
200+
}
201+
202+
fclose(fp);
203+
}
204+
205+
if (!success) {
206+
return -1;
207+
}
208+
return 0;
209+
}
210+
211+
int getStatsFreeSHM(unsigned long long &freeBytes) {
212+
int success = 0;
213+
freeBytes = 0;
214+
215+
try {
216+
freeBytes = (unsigned long long) (std::filesystem::space("/dev/shm")).free;
217+
success = 1;
218+
}
219+
catch (...) {
220+
}
221+
222+
if (!success) {
223+
return -1;
224+
}
225+
return 0;
226+
}

src/ReadoutUtils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,15 @@ int getProcessStats(double& uTime, double& sTime);
5656
typedef uint32_t tRunNumber;
5757
typedef uint32_t tTimeframeId;
5858

59+
// function to retrieve amount of free memory on the system
60+
// Works only when /proc/meminfo available
61+
// returns 0 on success, -1 on error
62+
int getStatsFreeMemory(unsigned long long &freeBytes);
63+
64+
// function to retrieve amount of free memory on the system
65+
// Works only when /dev/shm available
66+
// returns 0 on success, -1 on error
67+
int getStatsFreeSHM(unsigned long long &freeBytes);
68+
5969
// end of _READOUTUTILS_H
6070
#endif

0 commit comments

Comments
 (0)