3535#include < cstdlib>
3636#include < unistd.h>
3737
38+ #if defined(__linux__)
39+ #include < sys/resource.h>
40+ #endif
41+
3842class DataHeader ;
3943class FairMQUnmanagedRegion ;
4044
@@ -43,6 +47,7 @@ namespace o2
4347namespace DataDistribution
4448{
4549
50+ static constexpr const char *ENV_NOLOCK = " DATADIST_NO_MLOCK" ;
4651static constexpr const char *ENV_SHM_PATH = " DATADIST_SHM_PATH" ;
4752static constexpr const char *ENV_SHM_DELAY = " DATADIST_SHM_DELAY" ;
4853
@@ -65,16 +70,38 @@ class RegionAllocatorResource
6570 int lMapFlags = 0 ;
6671 std::string lSegmentRoot = " " ;
6772
68- // don't reserve swap space and try to lock the region
69- #if defined(MAP_NORESERVE) && defined(MAP_LOCKED)
70- lMapFlags = MAP_NORESERVE | MAP_LOCKED;
73+ // don't reserve swap space
74+ #if defined(MAP_NORESERVE)
75+ lMapFlags |= MAP_NORESERVE;
76+ #endif
77+
78+ // and try to lock the memory
79+ #if defined(MAP_LOCKED) && defined(__linux__)
80+ {
81+ struct rlimit lMyLimits;
82+ getrlimit (RLIMIT_MEMLOCK, &lMyLimits);
83+
84+ if (lMyLimits.rlim_cur >= pSize) {
85+ lMapFlags |= MAP_LOCKED;
86+ } else {
87+ if (std::getenv (ENV_NOLOCK)) {
88+ WDDLOG (" MemoryResource: Memory locking disabled via {} env variable. Not suitable for production." ,
89+ ENV_NOLOCK);
90+ } else {
91+ EDDLOG (" MemoryResource: Failed to lock the memory region. Increase your memory lock limits (ulimit -l)." );
92+ EDDLOG (" MemoryResource: To run without memory locking define {} env variable. Not suitable for production." ,
93+ ENV_NOLOCK);
94+ throw std::bad_alloc ();
95+ }
96+ }
97+ }
7198#endif
99+
72100 // populate the mapping
73101#if defined(MAP_POPULATE)
74102 lMapFlags |= MAP_POPULATE;
75103#endif
76104
77-
78105 // try to use different file mapping (hugetlbfs)
79106 const auto lHugetlbfsPath = std::getenv (ENV_SHM_PATH);
80107 if (lHugetlbfsPath) {
0 commit comments