diff --git a/platforms/posix/src/px4/common/main.cpp b/platforms/posix/src/px4/common/main.cpp index 3e51a48ddc8..f277a6b9969 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{};