rt.cpp 3.5 KB
Newer Older
1
2
3
/** Linux specific real-time optimizations
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Steffen Vogel's avatar
Steffen Vogel committed
4
 * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * @license GNU General Public License (version 3)
 *
 * VILLAScommon
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <sched.h>
#include <unistd.h>

Steffen Vogel's avatar
Steffen Vogel committed
26
27
#include <villas/log.hpp>
#include <villas/cpuset.hpp>
28
29
#include <villas/config.h>
#include <villas/utils.h>
Steffen Vogel's avatar
Steffen Vogel committed
30
#include <villas/exceptions.hpp>
31
32

#include <villas/kernel/kernel.h>
Steffen Vogel's avatar
Steffen Vogel committed
33
34
#include <villas/kernel/kernel.hpp>
#include <villas/kernel/rt.hpp>
35

Steffen Vogel's avatar
Steffen Vogel committed
36
37
38
#ifdef __linux__
  using villas::utils::CpuSet;
#endif /* __linux__ */
Steffen Vogel's avatar
Steffen Vogel committed
39
40
41
42
43

namespace villas {
namespace kernel {
namespace rt {

44
void init(int priority, int affinity)
45
{
46
47
	Logger logger = logging.get("kernel:rt");

Steffen Vogel's avatar
Steffen Vogel committed
48
	logger->info("Initialize sub-system");
49
50
51
52
53

#ifdef __linux__
	int is_rt;

	/* Use FIFO scheduler with real time priority */
Steffen Vogel's avatar
Steffen Vogel committed
54
	is_rt = isPreemptible();
55
	if (!is_rt)
Steffen Vogel's avatar
Steffen Vogel committed
56
		logger->warn("We recommend to use an PREEMPT_RT patched kernel!");
57
58

	if (priority)
Steffen Vogel's avatar
Steffen Vogel committed
59
		setPriority(priority);
60
	else
Steffen Vogel's avatar
Steffen Vogel committed
61
		logger->warn("You might want to use the 'priority' setting to increase " PROJECT_NAME "'s process priority");
62
63

	if (affinity)
Steffen Vogel's avatar
Steffen Vogel committed
64
		setAffinity(affinity);
65
	else
Steffen Vogel's avatar
Steffen Vogel committed
66
		logger->warn("You might want to use the 'affinity' setting to pin " PROJECT_NAME " to dedicate CPU cores");
67
#else
Steffen Vogel's avatar
Steffen Vogel committed
68
	logger->warn("This platform is not optimized for real-time execution");
69

Steffen Vogel's avatar
Steffen Vogel committed
70
71
	(void) affinity;
	(void) priority;
72
73
74
75
#endif
}

#ifdef __linux__
76
void setAffinity(int affinity)
77
78
79
80
{
	char isolcpus[255];
	int is_isol, ret;

81
82
	Logger logger = logging.get("kernel:rt");

83
	/* Pin threads to CPUs by setting the affinity */
Steffen Vogel's avatar
Steffen Vogel committed
84
	CpuSet cset_pin, cset_isol, cset_non_isol;
85

Steffen Vogel's avatar
Steffen Vogel committed
86
	cset_pin = CpuSet(affinity);
87
88
89

	is_isol = kernel_get_cmdline_param("isolcpus", isolcpus, sizeof(isolcpus));
	if (is_isol) {
Steffen Vogel's avatar
Steffen Vogel committed
90
		logger->warn("You should reserve some cores for " PROJECT_NAME " (see 'isolcpus')");
91

Steffen Vogel's avatar
Steffen Vogel committed
92
		cset_isol.zero();
93
94
	}
	else {
Steffen Vogel's avatar
Steffen Vogel committed
95
96
		cset_isol = CpuSet(isolcpus);
		cset_non_isol = cset_isol ^ cset_pin;
97

Steffen Vogel's avatar
Steffen Vogel committed
98
99
		if (cset_non_isol.count() > 0)
			logger->warn("Affinity setting includes cores which are not isolated: affinity={} isolcpus={}", (std::string) cset_pin, (std::string) cset_isol);
100
101
	}

Steffen Vogel's avatar
Steffen Vogel committed
102
	ret = sched_setaffinity(0, cset_pin.size(), cset_pin);
103
	if (ret)
104
		throw SystemError("Failed to set CPU affinity to {}", (std::string) cset_pin);
105

Steffen Vogel's avatar
Steffen Vogel committed
106
	logger->debug("Set affinity to {}", (std::string) cset_pin);
107
108
}

109
void setPriority(int priority)
110
111
112
113
114
115
{
	int ret;
	struct sched_param param = {
		.sched_priority = priority
	};

116
117
	Logger logger = logging.get("kernel:rt");

118
119
	ret = sched_setscheduler(0, SCHED_FIFO, &param);
	if (ret)
120
		throw SystemError("Failed to set real time priority");
121

Steffen Vogel's avatar
Steffen Vogel committed
122
	logger->debug("Task priority set to {}", priority);
123
124
}

125
bool isPreemptible()
126
{
127
	return access(SYSFS_PATH "/kernel/realtime", R_OK) == 0;
128
129
130
}

#endif /* __linux__ */
Steffen Vogel's avatar
Steffen Vogel committed
131

132
133
134
} /* namespace villas */
} /* namespace kernel */
} /* namespace rt */