10#include <sys/syscall.h>
19 long pid = syscall(SYS_gettid);
20 ARMARX_INFO <<
"Elevating priority of thread #" << pid <<
" to " << priority;
21 ARMARX_INFO <<
"Priority before: " << sched_getscheduler(
static_cast<int>(pid));
22 struct sched_param param;
23 param.sched_priority = priority;
24 if (sched_setscheduler(
static_cast<int>(pid), SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m) ==
28 ARMARX_WARNING <<
"sched_setscheduler failed: " << std::string(strerror(error));
31 if (sched_getparam(
static_cast<int>(pid), ¶m) == -1)
34 ARMARX_WARNING <<
"sched_getparam failed: " << std::string(strerror(error));
37 int new_priority = param.sched_priority;
38 if (new_priority == priority)
41 <<
" to new priority " << new_priority;
45 ARMARX_ERROR <<
"Failed to elevate priority of thread #" << pid;
54 long pid = syscall(SYS_gettid);
55 unsigned int availableCores = std::thread::hardware_concurrency();
56 if (availableCores < cpu)
58 ARMARX_ERROR <<
"Trying to pin thread #" << pid <<
" to CPU #" << cpu <<
", but only "
59 << availableCores <<
" cores are available";
63 ARMARX_INFO <<
"Pinning thread #" << pid <<
" to CPU #" << cpu;
67 int retval = sched_setaffinity(
static_cast<int>(pid),
sizeof(mask), &mask);
70 ARMARX_ERROR <<
"Failed to pin thread #" << pid <<
" to CPU #" << cpu;
76 sched_getaffinity(
static_cast<int>(pid),
sizeof(mask2), &mask2);
77 bool matches = CPU_EQUAL(&mask, &mask2);
80 ARMARX_IMPORTANT <<
"Successfully pinned thread #" << pid <<
" to CPU #" << cpu;
85 ARMARX_ERROR <<
"Failed to pin thread #" << pid <<
" to CPU #" << cpu;
90 static int fd_low_latency_target = -1;
91 static constexpr std::int32_t LOW_LATENCY_MAX_RESPONSE_TIME = 0;
96 ARMARX_INFO <<
"Starting low latency mode by writing " << LOW_LATENCY_MAX_RESPONSE_TIME
97 <<
" to /dev/cpu_dma_latency and keeping the file open";
101 err = stat(
"/dev/cpu_dma_latency", &s);
104 ARMARX_WARNING <<
"stat /dev/cpu_dma_latency failed: " << strerror(errno);
107 fd_low_latency_target = open(
"/dev/cpu_dma_latency", O_RDWR);
108 if (fd_low_latency_target == -1)
110 ARMARX_WARNING <<
"open /dev/cpu_dma_latency failed: " << strerror(errno);
114 err =
static_cast<int>(write(fd_low_latency_target,
115 &LOW_LATENCY_MAX_RESPONSE_TIME,
116 sizeof(LOW_LATENCY_MAX_RESPONSE_TIME)));
119 ARMARX_WARNING <<
"error writing " << LOW_LATENCY_MAX_RESPONSE_TIME
120 <<
" to /dev/cpu_dma_latency";
121 close(fd_low_latency_target);
125 << LOW_LATENCY_MAX_RESPONSE_TIME <<
" µs\n";
132 ARMARX_INFO <<
"Stopping low latency mode by closing file /dev/cpu_dma_latency";
133 if (fd_low_latency_target >= 0)
135 close(fd_low_latency_target);
static bool pinThreadToCPU(unsigned int cpu)
Pins the calling thread to the CPU with the given id.
static bool stopLowLatencyMode()
Deactivate low latency mode of the system.
static bool elevateThreadPriority(int priority)
Elevate the thread priority of the calling thread to the given priority.
static bool startLowLatencyMode()
Activate low latency mode of the system.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.