From ad4068f472e8c8416899d3437f5892f86956d126 Mon Sep 17 00:00:00 2001 From: SalimTerryLi Date: Mon, 16 Nov 2020 12:53:10 +0800 Subject: [PATCH] platforms/posix: mlockall() support * add basic mlock support to increase stability when system is under high load and RAM is almost full * mainly about minimizing or completely eliminating RAM page swap time --- platforms/posix/src/px4/common/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/platforms/posix/src/px4/common/main.cpp b/platforms/posix/src/px4/common/main.cpp index 3e51a48ddc..f277a6b996 100644 --- a/platforms/posix/src/px4/common/main.cpp +++ b/platforms/posix/src/px4/common/main.cpp @@ -62,6 +62,10 @@ #include #include #include +#include +#if (_POSIX_MEMLOCK > 0) +#include +#endif #include #include @@ -174,6 +178,22 @@ int main(int argc, char **argv) return client.process_args(argc, (const char **)argv); } else { +#if (_POSIX_MEMLOCK > 0) + + // try to lock address space into RAM, to avoid page swap delay + // TODO: Check CAP_IPC_LOCK instead of euid + if (geteuid() == 0) { // root user + if (mlockall(MCL_CURRENT) + mlockall(MCL_FUTURE)) { // check if both works + PX4_ERR("mlockall() failed! errno: %d", errno); + munlockall(); // avoid mlock limitation caused alloc failure in future + + } else { + PX4_INFO("mlockall() enabled. PX4's virtual address space is locked into RAM."); + } + } + +#endif + /* Server/daemon apps need to parse the command line arguments. */ std::string data_path{};