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;
97 ARMARX_INFO <<
"Starting low latency mode by writing " << LOW_LATENCY_MAX_RESPONSE_TIME
98 <<
" to /dev/cpu_dma_latency and keeping the file open";
102 err = stat(
"/dev/cpu_dma_latency", &
s);
105 ARMARX_WARNING <<
"stat /dev/cpu_dma_latency failed: " << strerror(errno);
108 fd_low_latency_target = open(
"/dev/cpu_dma_latency", O_RDWR);
109 if (fd_low_latency_target == -1)
111 ARMARX_WARNING <<
"open /dev/cpu_dma_latency failed: " << strerror(errno);
115 err =
static_cast<int>(
write(fd_low_latency_target,
116 &LOW_LATENCY_MAX_RESPONSE_TIME,
117 sizeof(LOW_LATENCY_MAX_RESPONSE_TIME)));
120 ARMARX_WARNING <<
"error writing " << LOW_LATENCY_MAX_RESPONSE_TIME
121 <<
" to /dev/cpu_dma_latency";
122 close(fd_low_latency_target);
126 << LOW_LATENCY_MAX_RESPONSE_TIME <<
" µs\n";
133 ARMARX_INFO <<
"Stopping low latency mode by closing file /dev/cpu_dma_latency";
134 if (fd_low_latency_target >= 0)
136 close(fd_low_latency_target);