diff --git a/include/villas/node/memory.hpp b/include/villas/node/memory.hpp index 4e750194b..d94efbaed 100644 --- a/include/villas/node/memory.hpp +++ b/include/villas/node/memory.hpp @@ -17,6 +17,8 @@ #include #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. diff --git a/lib/memory.cpp b/lib/memory.cpp index fb1cae4fd..f2fec0e98 100644 --- a/lib/memory.cpp +++ b/lib/memory.cpp @@ -10,12 +10,14 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -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;