Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper method to lock the pages of memory in the RAM #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ target_include_directories(thread_priority PUBLIC
$<INSTALL_INTERFACE:include/realtime_tools>
)
ament_target_dependencies(thread_priority PUBLIC ${THIS_PACKAGE_INCLUDE_DEPENDS})
target_link_libraries(thread_priority PUBLIC cap)

# Unit Tests
if(BUILD_TESTING)
Expand Down
9 changes: 9 additions & 0 deletions include/realtime_tools/thread_priority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#ifndef REALTIME_TOOLS__THREAD_PRIORITY_HPP_
#define REALTIME_TOOLS__THREAD_PRIORITY_HPP_

#include <string>

namespace realtime_tools
{
/**
Expand All @@ -44,6 +46,13 @@ bool has_realtime_kernel();
*/
bool configure_sched_fifo(int priority);

/**
* Locks the memory pages of the calling thread to prevent page faults. By calling this method, the programs locks all pages mapped into the address space of the calling process and future mappings. This means that the kernel will not swap out the pages to disk i.e., the pages are guaranteed to stay in RAM until later unlocked - which is important for realtime applications.
* \param[out] message a message describing the result of the operation
* \returns true if memory locking succeeded, false otherwise
*/
bool lock_memory(std::string & message);
saikishor marked this conversation as resolved.
Show resolved Hide resolved

} // namespace realtime_tools

#endif // REALTIME_TOOLS__THREAD_PRIORITY_HPP_
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<depend>rclcpp</depend>
<depend>rclcpp_action</depend>
<build_depend>libcap-dev</build_depend>

<test_depend>ament_cmake_gmock</test_depend>
<test_depend>lifecycle_msgs</test_depend>
Expand Down
46 changes: 46 additions & 0 deletions src/thread_priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "realtime_tools/thread_priority.hpp"

#include <sched.h>
#include <sys/capability.h>
#include <sys/mman.h>

#include <cstring>
#include <fstream>
Expand All @@ -53,4 +55,48 @@ bool configure_sched_fifo(int priority)
return !sched_setscheduler(0, SCHED_FIFO, &schedp);
}

bool is_capable(cap_value_t v)
{
bool rc = false;
cap_t caps;
if ((caps = cap_get_proc()) == NULL) {
return false;
}

if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &v, CAP_SET) == -1) {
rc = false;
} else {
rc = (cap_set_proc(caps) == 0);
}
cap_free(caps);
return rc;
}

bool lock_memory(std::string & message)
{
if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
if (!is_capable(CAP_IPC_LOCK)) {
message = "No proper privileges to lock the memory!";
} else if (errno == ENOMEM) {
message =
"The caller had a nonzero RLIMIT_MEMLOCK soft resource limit, but tried to lock more "
"memory than the limit permitted. Try running the application with privileges!";
} else if (errno == EPERM) {
message =
"The caller is not privileged, but needs privilege to perform the requested operation.";
} else if (errno == EINVAL) {
message =
"The result of the addition start+len was less than start (e.g., the addition may have "
"resulted in an overflow).";
} else if (errno == EAGAIN) {
message = "Some or all of the specified address range could not be locked.";
} else {
message = "Unknown error occurred!";
}
return false;
} else {
message = "Memory locked successfully!";
return true;
}
}
} // namespace realtime_tools
Loading