1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

add functions for stack and heap prefaulting

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
Signed-off-by: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
Co-authored-by: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
This commit is contained in:
Steffen Vogel 2022-03-15 12:30:10 -04:00 committed by Steffen Vogel
parent e8b2e9c7d5
commit 26cfc48a02
2 changed files with 39 additions and 6 deletions

View file

@ -17,6 +17,8 @@
#include <infiniband/verbs.h>
#endif // IBVERBS_FOUND
#define MAX_SAFE_STACK (512 * 1024)
namespace villas {
namespace node {
namespace memory {
@ -57,7 +59,11 @@ int init(int hugepages) __attribute__((warn_unused_result));
int lock(size_t lock);
/* Allocate \p len bytes memory of type \p m.
void prefault_heap(size_t sz);
void prefault_stack();
/** Allocate \p len bytes memory of type \p m.
*
* @retval nullptr If allocation failed.
* @retval <>0 If allocation was successful.

View file

@ -10,12 +10,14 @@
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <malloc.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <villas/exceptions.hpp>
#include <villas/kernel/kernel.hpp>
#include <villas/log.hpp>
#include <villas/node/memory.hpp>
@ -88,15 +90,19 @@ int villas::node::memory::lock(size_t sz) {
logger->debug("Increased ressource limit of locked memory to {} bytes", sz);
}
#endif // __arm__
#endif /* __arm__ */
// Disable usage of mmap() for malloc()
mallopt(M_MMAP_MAX, 0);
mallopt(M_TRIM_THRESHOLD, -1);
#ifdef _POSIX_MEMLOCK
// Lock all current and future memory allocations
/* Lock all current and future memory allocations */
ret = mlockall(MCL_CURRENT | MCL_FUTURE);
if (ret)
return -1;
#endif // _POSIX_MEMLOCK
#endif // __linux__
#endif /* _POSIX_MEMLOCK */
#endif /* __linux__ */
return 0;
}
@ -152,4 +158,25 @@ struct Allocation *villas::node::memory::get_allocation(void *ptr) {
return allocations[ptr];
}
void villas::node::memory::prefault_heap(size_t sz) {
auto pgsz = kernel::getPageSize();
char *dummy = new char[sz];
if (!dummy)
throw MemoryAllocationError();
for (size_t i = 0; i < sz; i += pgsz)
dummy[i] = 1;
delete[] dummy;
}
void villas::node::memory::prefault_stack() {
auto pgsz = kernel::getPageSize();
unsigned char dummy[MAX_SAFE_STACK] __attribute__((unused));
for (size_t i = 0; i < MAX_SAFE_STACK; i += pgsz)
dummy[i] = 1;
}
struct Type *villas::node::memory::default_type = nullptr;