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

Added method to set thread affinity for a certain core #140

Closed
wants to merge 6 commits into from
Closed
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ if(BUILD_TESTING)
find_package(rclcpp_lifecycle REQUIRED)
find_package(test_msgs REQUIRED)

ament_add_gmock(thread_priority_tests test/thread_priority_tests.cpp)
target_link_libraries(thread_priority_tests thread_priority)

ament_add_gmock(realtime_box_tests test/realtime_box_tests.cpp)
target_link_libraries(realtime_box_tests realtime_tools)

Expand Down
12 changes: 12 additions & 0 deletions include/realtime_tools/thread_priority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ bool has_realtime_kernel();
*/
bool configure_sched_fifo(int priority);

/**
* Configure the caller thread affinity - Tell the scheduler to prefer a certain core for the current thread
* \param[in] core - the number of the core
* \returns true if configuring the scheduler succeeded
*/
bool set_preferred_core(const int core = -1);
Comment on lines +49 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Configure the caller thread affinity - Tell the scheduler to prefer a certain core for the current thread
* \param[in] core - the number of the core
* \returns true if configuring the scheduler succeeded
*/
bool set_preferred_core(const int core = -1);
/**
* Configure the caller thread affinity - Tell the scheduler to prefer a certain core for the current thread
* \param[in] core - the number of the core
* When parsed -1, the CPU affinity is reset to all CPUs
* \returns true if configuring the scheduler succeeded
*/
bool set_cpu_affinity(const int core);

I would rather make the naming more standard as known to everyone


/**
* \returns The amount of available cpu cores
*/
int get_core_count();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int get_core_count();
int get_available_cpu_count();


/**
* 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
Expand Down
54 changes: 54 additions & 0 deletions src/thread_priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
#include <sys/mman.h>
#endif

#include <unistd.h>
#include <cstring>
#include <fstream>
#include <iostream>

namespace realtime_tools
{
Expand All @@ -64,6 +66,58 @@ bool configure_sched_fifo(int priority)
#endif
}

static void print_error(const int errc)
{
switch (errc) {
case 0:
return;
case EFAULT:
std::cerr << "Call of sched_setaffinity with invalid cpuset" << std::endl;
return;
case EINVAL:
std::cerr << "Call of sched_setaffinity with an invalid cpu core" << std::endl;
return;
case ESRCH:
std::cerr << "Call of sched_setaffinity with and invalid thread id/process id" << std::endl;
return;
default:
std::cerr << "Error code: " << errc << ": " << std::string(strerror(errc)) << std::endl;
}
}

bool set_preferred_core(const int core)
{
// Allow attaching the thread/process to a certain cpu core
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
// Obtain amount of cores/
const auto number_of_cores = get_core_count();

// Reset affinity by setting it to all cores
if (core < 0) {
for (int i{0}; i < number_of_cores; i++) {
CPU_SET(i, &cpuset);
}
// And actually tell the schedular to set the affinity of the currently calling thread
const auto result = sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
print_error(result);
return result == 0;
}

if (core < number_of_cores) {
// Set the passed core to the cpu set
CPU_SET(core, &cpuset);
// And actually tell the schedular to set the affinity of the currently calling thread
const auto result = sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
print_error(result);
return result == 0;
}
// Invalid core number passed
return false;
}

int get_core_count() { return sysconf(_SC_NPROCESSORS_ONLN); }

bool lock_memory(std::string & message)
{
#ifdef _WIN32
Expand Down
60 changes: 60 additions & 0 deletions test/thread_priority_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2024, Lennart Nachtigall
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// Author: Lennart Nachtigall

#include <gmock/gmock.h>
#include <thread>

#include <realtime_tools/thread_priority.hpp>

TEST(thread_priority, get_core_count)
{
const auto count = realtime_tools::get_core_count();

EXPECT_EQ(count, std::thread::hardware_concurrency());
}

TEST(thread_priority, set_preferred_core_valid)
{
// We should always have at least one core
EXPECT_TRUE(realtime_tools::set_preferred_core(0));
}

TEST(thread_priority, set_preferred_core_invalid_too_many_cores)
{
const auto count = realtime_tools::get_core_count();
// We should always have at least one core
EXPECT_FALSE(realtime_tools::set_preferred_core(count + 10));
}

TEST(thread_priority, set_preferred_core_valid_reset)
{
// Reset core affinity
EXPECT_TRUE(realtime_tools::set_preferred_core(-1));
}
Loading