rt.cpp 3.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/** Linux specific real-time optimizations
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
 * @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
 * @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>
#include <sys/mman.h>

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

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

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

namespace villas {
namespace kernel {
namespace rt {

static auto logger = logging.get("kernel:rt");

int init(int priority, int affinity)
48
{
Steffen Vogel's avatar
Steffen Vogel committed
49
	logger->info("Initialize sub-system");
50
51
52
53
54

#ifdef __linux__
	int is_rt;

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

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

	if (affinity)
Steffen Vogel's avatar
Steffen Vogel committed
65
		setAffinity(affinity);
66
	else
Steffen Vogel's avatar
Steffen Vogel committed
67
		logger->warn("You might want to use the 'affinity' setting to pin " PROJECT_NAME " to dedicate CPU cores");
68

Steffen Vogel's avatar
Steffen Vogel committed
69
	lockMemory();
70
#else
Steffen Vogel's avatar
Steffen Vogel committed
71
	logger->warn("This platform is not optimized for real-time execution");
Steffen Vogel's avatar
Steffen Vogel committed
72
73
	(void) affinity;
	(void) priority;
74
75
76
77
78
79
80
#endif

	return 0;
}

#ifdef __linux__

Steffen Vogel's avatar
Steffen Vogel committed
81
int lockMemory()
82
83
84
85
86
87
{
	int ret;

#ifdef _POSIX_MEMLOCK
	ret = mlockall(MCL_CURRENT | MCL_FUTURE);
	if (ret)
Steffen Vogel's avatar
Steffen Vogel committed
88
		throw new SystemError("Failed to lock memory");
89
90
91
92
93
#endif

	return 0;
}

Steffen Vogel's avatar
Steffen Vogel committed
94
int setAffinity(int affinity)
95
96
97
98
99
{
	char isolcpus[255];
	int is_isol, ret;

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

Steffen Vogel's avatar
Steffen Vogel committed
102
	cset_pin = CpuSet(affinity);
103
104
105

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

Steffen Vogel's avatar
Steffen Vogel committed
108
		cset_isol.zero();
109
110
	}
	else {
Steffen Vogel's avatar
Steffen Vogel committed
111
112
		cset_isol = CpuSet(isolcpus);
		cset_non_isol = cset_isol ^ cset_pin;
113

Steffen Vogel's avatar
Steffen Vogel committed
114
115
		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);
116
117
	}

Steffen Vogel's avatar
Steffen Vogel committed
118
	ret = sched_setaffinity(0, cset_pin.size(), cset_pin);
119
	if (ret)
Steffen Vogel's avatar
Steffen Vogel committed
120
		throw new SystemError("Failed to set CPU affinity to {}", (std::string) cset_pin);
121

Steffen Vogel's avatar
Steffen Vogel committed
122
	logger->debug("Set affinity to {}", (std::string) cset_pin);
123
124
125
126

	return 0;
}

Steffen Vogel's avatar
Steffen Vogel committed
127
int setPriority(int priority)
128
129
130
131
132
133
134
135
{
	int ret;
	struct sched_param param = {
		.sched_priority = priority
	};

	ret = sched_setscheduler(0, SCHED_FIFO, &param);
	if (ret)
Steffen Vogel's avatar
Steffen Vogel committed
136
		throw new SystemError("Failed to set real time priority");
137

Steffen Vogel's avatar
Steffen Vogel committed
138
	logger->debug("Task priority set to {}", priority);
139
140
141
142

	return 0;
}

Steffen Vogel's avatar
Steffen Vogel committed
143
int isPreemptible()
144
145
146
147
148
{
	return access(SYSFS_PATH "/kernel/realtime", R_OK);
}

#endif /* __linux__ */
Steffen Vogel's avatar
Steffen Vogel committed
149
150
151
152

} // namespace villas
} // namespace kernel
} // namespace rt