kernel.c 6.25 KB
Newer Older
1
2
3
/** Linux kernel related functions.
 *
 * @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
 * @license GNU General Public License (version 3)
 *
7
 * VILLAScommon
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * 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 <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
28
#include <inttypes.h>
29
30
31
32
33
34
35
36
37
38
39

#include <sys/types.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <villas/utils.h>
#include <villas/config.h>
#include <villas/kernel/kernel.h>

int kernel_get_cacheline_size()
{
40
#if defined(__linux__) && defined(__x86_64__)
41
	return sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
Steffen Vogel's avatar
Steffen Vogel committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#elif defined(__MACH__)
	  /* Open the command for reading. */
	FILE *fp = popen("sysctl -n machdep.cpu.cache.linesize", "r");
	if (fp == NULL)
		return -1;

	int ret, size;

	ret = fscanf(fp, "%d", &size);

	pclose(fp);

	return ret == 1 ? size : -1;
#else
56
	return CACHELINE_SIZE;
57
58
59
#endif
}

60
61
62
63
64
#if defined(__linux__) || defined(__APPLE__)
int kernel_get_page_size()
{
	return sysconf(_SC_PAGESIZE);
}
Steffen Vogel's avatar
Steffen Vogel committed
65
66
#else
  #error "Unsupported platform"
67
68
#endif

Steffen Vogel's avatar
Steffen Vogel committed
69
70
71
72
/* There is no sysconf interface to get the hugepage size */
int kernel_get_hugepage_size()
{
#ifdef __linux__
73
	char *key, *value, *unit, *line = NULL, *lasts;
Steffen Vogel's avatar
Steffen Vogel committed
74
75
76
77
78
79
80
81
82
	int sz = -1;
	size_t len = 0;
	FILE *f;

	f = fopen(PROCFS_PATH "/meminfo", "r");
	if (!f)
		return -1;

	while (getline(&line, &len, f) != -1) {
83
84
85
		key   = strtok_r(line, ": ", &lasts);
		value = strtok_r(NULL, " ", &lasts);
		unit  = strtok_r(NULL, "\n", &lasts);
Steffen Vogel's avatar
Steffen Vogel committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

		if (!strcmp(key, "Hugepagesize") && !strcmp(unit, "kB")) {
			sz = strtoul(value, NULL, 10) * 1024;
			break;
		}
	}

	free(line);
	fclose(f);

	return sz;
#elif defined(__x86_64__)
	return 1 << 21;
#elif defined(__i386__)
	return 1 << 22;
#else
  #error "Unsupported architecture"
#endif
}

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifdef __linux__

int kernel_module_set_param(const char *module, const char *param, const char *value)
{
	FILE *f;
	char fn[256];

	snprintf(fn, sizeof(fn), "%s/module/%s/parameters/%s", SYSFS_PATH, module, param);
	f = fopen(fn, "w");
	if (!f)
		serror("Failed set parameter %s for kernel module %s to %s", module, param, value);

	debug(LOG_KERNEL | 5, "Set parameter %s of kernel module %s to %s", module, param, value);
	fprintf(f, "%s", value);
	fclose(f);

	return 0;
}

int kernel_module_load(const char *module)
{
	int ret;

	ret = kernel_module_loaded(module);
	if (!ret) {
		debug(LOG_KERNEL | 5, "Kernel module %s already loaded...", module);
		return 0;
	}

	pid_t pid = fork();
	switch (pid) {
137
		case -1: /* error */
138
139
			return -1;

140
		case 0:  /* child */
141
			execlp("modprobe", "modprobe", module, (char *) 0);
142
			exit(EXIT_FAILURE);   /* exec never returns */
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

		default:
			wait(&ret);

			return kernel_module_loaded(module);
	}
}

int kernel_module_loaded(const char *module)
{
	FILE *f;
	int ret = -1;
	char *line = NULL;
	size_t len = 0;

	f = fopen(PROCFS_PATH "/modules", "r");
	if (!f)
		return -1;

	while (getline(&line, &len, f) >= 0) {
		if (strstr(line, module) == line) {
			ret = 0;
			break;
		}
	}

	free(line);
	fclose(f);

	return ret;
}

int kernel_get_cmdline_param(const char *param, char *buf, size_t len)
{
	int ret;
178
	char cmdline[512], *lasts;
179
180
181
182
183
184
185
186

	FILE *f = fopen(PROCFS_PATH "/cmdline", "r");
	if (!f)
		return -1;

	if (!fgets(cmdline, sizeof(cmdline), f))
		goto out;

187
	char *tok = strtok_r(cmdline, " \t", &lasts);
188
189
190
191
192
193
194
195
196
197
198
199
	do {
		char key[128], value[128];

		ret = sscanf(tok, "%127[^=]=%127s", key, value);
		if (ret >= 1) {
			if (ret >= 2)
				debug(30, "Found kernel param: %s=%s", key, value);
			else
				debug(30, "Found kernel param: %s", key);

			if (strcmp(param, key) == 0) {
				if (ret >= 2 && buf)
200
					snprintf(buf, len, "%s", value);
201
202
203
204

				return 0; /* found */
			}
		}
205
	} while ((tok = strtok_r(NULL, " \t", &lasts)));
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

out:
	fclose(f);

	return -1; /* not found or error */
}

int kernel_get_nr_hugepages()
{
	FILE *f;
	int nr, ret;

	f = fopen(PROCFS_PATH "/sys/vm/nr_hugepages", "r");
	if (!f)
		serror("Failed to open %s", PROCFS_PATH "/sys/vm/nr_hugepages");

	ret = fscanf(f, "%d", &nr);
	if (ret != 1)
		nr = -1;

	fclose(f);

	return nr;
}

int kernel_set_nr_hugepages(int nr)
{
	FILE *f;

	f = fopen(PROCFS_PATH "/sys/vm/nr_hugepages", "w");
	if (!f)
		serror("Failed to open %s", PROCFS_PATH "/sys/vm/nr_hugepages");

	fprintf(f, "%d\n", nr);
	fclose(f);

	return 0;
}

245
int kernel_irq_setaffinity(unsigned irq, uintmax_t aff, uintmax_t *old)
246
247
248
249
250
251
252
253
254
{
	char fn[64];
	FILE *f;
	int ret = 0;

	snprintf(fn, sizeof(fn), "/proc/irq/%u/smp_affinity", irq);

	f = fopen(fn, "w+");
	if (!f)
255
		return -1; /* IRQ does not exist */
256
257
258
259

	if (old)
		ret = fscanf(f, "%jx", old);

260
	fprintf(f, "%jx", aff);
261
262
263
264
265
	fclose(f);

	return ret;
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
int kernel_get_cpu_frequency(uint64_t *freq)
{
	char *line = NULL, *sep, *end;
	size_t len = 0;
	double dfreq;
	int ret;
	FILE *f;

	/* Try to get CPU frequency from cpufreq module */
	f = fopen(SYSFS_PATH "/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
	if (!f)
		goto cpuinfo;

	ret = fscanf(f, "%" PRIu64, freq);
	fclose(f);
	if (ret != 1)
		return -1;

	/* cpufreq reports kHz */
	*freq = *freq * 1000;

	return 0;

cpuinfo:
	/* Try to read CPU frequency from /proc/cpuinfo */
	f = fopen(PROCFS_PATH "/cpuinfo", "r");
	if (!f)
		return -1; /* We give up here */

	ret = -1;
	while (getline(&line, &len, f) >= 0) {
		if (strstr(line,  "cpu MHz") == line) {
			ret = 0;
			break;
		}
	}
	if (ret)
		goto out;

	sep = strchr(line, ':');
	if (!sep) {
		ret = -1;
		goto out;
	}

	dfreq = strtod(sep+1, &end);

	if (end == sep+1) {
		ret = -1;
		goto out;
	}

	/* Frequency is given in MHz */
	*freq = dfreq * 1e6;

out:	fclose(f);
	free(line);

	return ret;
}
326
#endif /* __linux__ */