2023-08-31 17:35:12 +02:00
|
|
|
/* Linux specific real-time optimizations.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-08-11 12:40:19 -04:00
|
|
|
#include <villas/config.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/cpuset.hpp>
|
2018-08-27 11:12:40 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2018-08-27 11:12:40 +02:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
|
|
|
#include <villas/kernel/rt.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2018-10-19 16:32:44 +02:00
|
|
|
#ifdef __linux__
|
2023-09-07 13:19:19 +02:00
|
|
|
using villas::utils::CpuSet;
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // __linux__
|
2018-08-27 11:12:40 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace kernel {
|
|
|
|
namespace rt {
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void init(int priority, int affinity) {
|
|
|
|
Logger logger = logging.get("kernel:rt");
|
2019-01-21 16:32:42 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
logger->info("Initialize sub-system");
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#ifdef __linux__
|
2023-09-07 13:19:19 +02:00
|
|
|
int is_rt, is_isol;
|
|
|
|
char isolcpus[255];
|
|
|
|
|
|
|
|
// Use FIFO scheduler with real time priority
|
|
|
|
is_rt = isPreemptible();
|
|
|
|
if (!is_rt)
|
|
|
|
logger->warn("We recommend to use an PREEMPT_RT patched kernel!");
|
|
|
|
|
|
|
|
if (priority)
|
|
|
|
setPriority(priority);
|
|
|
|
else
|
|
|
|
logger->warn(
|
|
|
|
"You might want to use the 'priority' setting to increase " PROJECT_NAME
|
|
|
|
"'s process priority");
|
|
|
|
|
|
|
|
if (affinity) {
|
|
|
|
is_isol = getCmdlineParam("isolcpus", isolcpus, sizeof(isolcpus));
|
|
|
|
if (is_isol)
|
|
|
|
logger->warn("You should reserve some cores for " PROJECT_NAME
|
|
|
|
" (see 'isolcpus')");
|
|
|
|
else {
|
|
|
|
CpuSet cset_pin(affinity);
|
|
|
|
CpuSet cset_isol(isolcpus);
|
|
|
|
CpuSet cset_non_isol = ~cset_isol & cset_pin;
|
|
|
|
|
|
|
|
if (cset_non_isol.count() > 0)
|
|
|
|
logger->warn("Affinity setting includes cores which are not isolated: "
|
|
|
|
"affinity={}, isolcpus={}, non_isolated={}",
|
|
|
|
(std::string)cset_pin, (std::string)cset_isol,
|
|
|
|
(std::string)cset_non_isol);
|
|
|
|
}
|
|
|
|
|
|
|
|
setProcessAffinity(affinity);
|
|
|
|
} else
|
|
|
|
logger->warn(
|
|
|
|
"You might want to use the 'affinity' setting to pin " PROJECT_NAME
|
|
|
|
" to dedicate CPU cores");
|
2018-08-22 11:29:39 +02:00
|
|
|
#else
|
2023-09-07 13:19:19 +02:00
|
|
|
logger->warn("This platform is not optimized for real-time execution");
|
2019-02-12 17:28:37 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
(void)affinity;
|
|
|
|
(void)priority;
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // __linux__
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
2023-09-07 13:19:19 +02:00
|
|
|
void setProcessAffinity(int affinity) {
|
|
|
|
int ret;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
assert(affinity != 0);
|
2020-09-13 10:59:19 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger = logging.get("kernel:rt");
|
2019-01-21 16:32:42 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Pin threads to CPUs by setting the affinity
|
|
|
|
CpuSet cset_pin(affinity);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = sched_setaffinity(0, cset_pin.size(), cset_pin);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to set CPU affinity of process");
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
logger->debug("Set affinity to {} {}",
|
|
|
|
cset_pin.count() == 1 ? "core" : "cores",
|
|
|
|
(std::string)cset_pin);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void setThreadAffinity(pthread_t thread, int affinity) {
|
|
|
|
int ret;
|
2020-09-13 08:35:27 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
assert(affinity != 0);
|
2020-09-13 10:59:19 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger = logging.get("kernel:rt");
|
2020-09-13 08:35:27 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
CpuSet cset_pin(affinity);
|
2020-09-13 08:35:27 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = pthread_setaffinity_np(thread, cset_pin.size(), cset_pin);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to set CPU affinity of thread");
|
2020-09-13 08:35:27 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
logger->debug("Set affinity of thread {} to {} {}", (long unsigned)thread,
|
|
|
|
cset_pin.count() == 1 ? "core" : "cores",
|
|
|
|
(std::string)cset_pin);
|
2020-09-13 08:35:27 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void setPriority(int priority) {
|
|
|
|
int ret;
|
|
|
|
struct sched_param param;
|
|
|
|
param.sched_priority = priority;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger = logging.get("kernel:rt");
|
2019-01-21 16:32:42 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to set real time priority");
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
logger->debug("Task priority set to {}", priority);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
bool isPreemptible() {
|
|
|
|
return access(SYSFS_PATH "/kernel/realtime", R_OK) == 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // __linux__
|
2018-08-27 11:12:40 +02:00
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace rt
|
2023-09-07 13:19:19 +02:00
|
|
|
} // namespace kernel
|
|
|
|
} // namespace villas
|