Skip to content

Commit 7b2ca6a

Browse files
committed
added numa utils
1 parent e73f802 commit 7b2ca6a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/ReadoutUtils.cxx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#include <filesystem>
1919

2020
#include "RAWDataHeader.h"
21+
#include "readoutInfoLogger.h"
22+
23+
#ifdef WITH_NUMA
24+
#include <numa.h>
25+
#include <numaif.h>
26+
#endif
2127

2228
// function to convert a string to a 64-bit integer value
2329
// allowing usual "base units" in suffix (k,M,G,T)
@@ -256,3 +262,39 @@ int getStatsFilesystem(unsigned long long &freeBytes, const std::string &path) {
256262
return 0;
257263
}
258264

265+
int numaBind(int numaNode) {
266+
#ifdef WITH_NUMA
267+
struct bitmask* nodemask = nullptr;
268+
if (numaNode>=0) {
269+
nodemask = numa_allocate_nodemask();
270+
if (nodemask != nullptr) {
271+
theLog.log(LogInfoDevel, "Enforcing memory allocated on NUMA node %d", numaNode);
272+
numa_bitmask_clearall(nodemask);
273+
numa_bitmask_setbit(nodemask, numaNode);
274+
numa_bind(nodemask);
275+
numa_free_nodemask(nodemask);
276+
return 0;
277+
}
278+
} else {
279+
nodemask = numa_get_mems_allowed();
280+
numa_bind(nodemask);
281+
theLog.log(LogInfoDevel, "Releasing memory NUMA node enforcement");
282+
return 0;
283+
}
284+
//numa_set_membind(nodemask);
285+
#endif
286+
return -1;
287+
}
288+
289+
int numaGetNodeFromAddress(void *ptr, int &node) {
290+
#ifdef WITH_NUMA
291+
int err;
292+
node = -1;
293+
err = move_pages(0, 1, &ptr, NULL, &node, 0);
294+
if (!err) {
295+
return 0;
296+
}
297+
// EFAULT -> memory not mapped yet
298+
#endif
299+
return -1;
300+
}

src/ReadoutUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ int getStatsMemory(unsigned long long &freeBytes, const std::string& keyword);
7171
// returns 0 on success, -1 on error
7272
int getStatsFilesystem(unsigned long long &freeBytes, const std::string& path);
7373

74+
// function to bind CPU / MEM to specific numa node
75+
// Use numaNode = -1 to bind to any / unbind.
76+
// returns 0 on success, or an error code.
77+
int numaBind(int numaNode);
78+
79+
// function to get NUMA node of where given pointer is allocated
80+
// returns 0 on success, or an error code
81+
int numaGetNodeFromAddress(void *ptr, int &node);
82+
7483
// end of _READOUTUTILS_H
7584
#endif
7685

0 commit comments

Comments
 (0)