2016-06-26 15:27:14 +02:00
|
|
|
/** Linux specific real-time optimizations
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-26 15:27:14 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <sched.h>
|
2017-02-16 13:32:22 -03:00
|
|
|
#include <unistd.h>
|
2017-08-14 14:29:23 +02:00
|
|
|
#include <sys/mman.h>
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/config.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/super_node.h>
|
2017-02-16 09:11:49 -03:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/kernel/kernel.h>
|
|
|
|
#include <villas/kernel/rt.h>
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
int rt_init(int priority, int affinity)
|
2017-03-06 19:12:31 -04:00
|
|
|
{
|
|
|
|
info("Initialize real-time sub-system");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-16 14:42:11 +02:00
|
|
|
{
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
2017-02-16 09:11:49 -03:00
|
|
|
int is_rt;
|
2016-06-26 15:27:14 +02:00
|
|
|
|
|
|
|
/* Use FIFO scheduler with real time priority */
|
2017-02-16 09:08:29 -03:00
|
|
|
is_rt = rt_is_preemptible();
|
2016-06-26 15:27:14 +02:00
|
|
|
if (is_rt)
|
|
|
|
warn("We recommend to use an PREEMPT_RT patched kernel!");
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
if (priority)
|
|
|
|
rt_set_priority(priority);
|
2017-02-16 09:11:49 -03:00
|
|
|
else
|
|
|
|
warn("You might want to use the 'priority' setting to increase VILLASnode's process priority");
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
if (affinity)
|
|
|
|
rt_set_affinity(affinity);
|
2017-02-16 09:11:49 -03:00
|
|
|
else
|
2017-05-28 18:57:31 +02:00
|
|
|
warn("You might want to use the 'affinity' setting to pin VILLASnode to dedicate CPU cores");
|
2017-12-09 02:19:28 +08:00
|
|
|
|
2017-08-14 14:29:23 +02:00
|
|
|
rt_lock_memory();
|
2017-07-24 19:21:57 +02:00
|
|
|
#else
|
|
|
|
warn("This platform is not optimized for real-time execution");
|
|
|
|
#endif
|
2017-03-06 19:12:31 -04:00
|
|
|
}
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-07-24 19:21:57 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
2017-08-14 14:29:23 +02:00
|
|
|
int rt_lock_memory()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef _POSIX_MEMLOCK
|
|
|
|
ret = mlockall(MCL_CURRENT | MCL_FUTURE);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to lock memory");
|
|
|
|
#endif
|
2017-12-09 02:19:28 +08:00
|
|
|
|
2017-08-14 14:29:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
int rt_set_affinity(int affinity)
|
|
|
|
{
|
|
|
|
char isolcpus[255];
|
|
|
|
int is_isol, ret;
|
2016-07-11 16:03:28 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
/* Pin threads to CPUs by setting the affinity */
|
|
|
|
cpu_set_t cset_pin, cset_isol, cset_non_isol;
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
cpuset_from_integer(affinity, &cset_pin);
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
is_isol = kernel_get_cmdline_param("isolcpus", isolcpus, sizeof(isolcpus));
|
|
|
|
if (is_isol) {
|
|
|
|
warn("You should reserve some cores for VILLASnode (see 'isolcpus')");
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
CPU_ZERO(&cset_isol);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = cpulist_parse(isolcpus, &cset_isol, 0);
|
2016-07-11 11:36:23 +02:00
|
|
|
if (ret)
|
2017-02-16 09:11:49 -03:00
|
|
|
error("Invalid isolcpus cmdline parameter: %s", isolcpus);
|
|
|
|
|
|
|
|
CPU_XOR(&cset_non_isol, &cset_isol, &cset_pin);
|
|
|
|
if (CPU_COUNT(&cset_non_isol) > 0) {
|
|
|
|
char isol[128], pin[128];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
cpulist_create(isol, sizeof(isol), &cset_isol);
|
|
|
|
cpulist_create(pin, sizeof(pin), &cset_pin);
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
warn("Affinity setting includes cores which are not isolated: affinity=%s isolcpus=%s", pin, isol);
|
|
|
|
}
|
2016-07-11 11:36:23 +02:00
|
|
|
}
|
2016-06-26 15:27:14 +02:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
char list[128];
|
|
|
|
cpulist_create(list, sizeof(list), &cset_pin);
|
|
|
|
|
|
|
|
ret = sched_setaffinity(0, sizeof(cpu_set_t), &cset_pin);
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to set CPU affinity to %s", list);
|
|
|
|
|
|
|
|
debug(LOG_KERNEL | 3, "Set affinity to %s", list);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-16 09:11:49 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_set_priority(int priority)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sched_param param = {
|
|
|
|
.sched_priority = priority
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to set real time priority");
|
|
|
|
|
|
|
|
debug(LOG_KERNEL | 3, "Task priority set to %u", priority);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-26 15:27:14 +02:00
|
|
|
return 0;
|
2017-02-16 09:08:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int rt_is_preemptible()
|
|
|
|
{
|
|
|
|
return access(SYSFS_PATH "/kernel/realtime", R_OK);
|
2017-07-24 19:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __linux__ */
|