2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-09-30 21:50:22 +02:00
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "cpu-set-util.h"
|
2015-10-22 20:12:31 +02:00
|
|
|
#include "extract-word.h"
|
2015-11-30 21:43:37 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "parse-util.h"
|
2015-11-03 12:25:29 +01:00
|
|
|
#include "string-util.h"
|
2015-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
|
|
|
|
cpu_set_t *c;
|
|
|
|
unsigned n = 1024;
|
|
|
|
|
|
|
|
/* Allocates the cpuset in the right size */
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
c = CPU_ALLOC(n);
|
|
|
|
if (!c)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
|
|
|
|
CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
|
|
|
|
|
|
|
|
if (ncpus)
|
|
|
|
*ncpus = n;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPU_FREE(c);
|
|
|
|
|
|
|
|
if (errno != EINVAL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
n *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 16:39:36 +01:00
|
|
|
int parse_cpu_set_internal(
|
2015-09-30 21:50:22 +02:00
|
|
|
const char *rvalue,
|
|
|
|
cpu_set_t **cpu_set,
|
2017-12-02 16:39:36 +01:00
|
|
|
bool warn,
|
2015-09-30 21:50:22 +02:00
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *lvalue) {
|
|
|
|
|
|
|
|
_cleanup_cpu_free_ cpu_set_t *c = NULL;
|
2017-12-02 16:39:36 +01:00
|
|
|
const char *p = rvalue;
|
2015-09-30 21:50:22 +02:00
|
|
|
unsigned ncpus = 0;
|
|
|
|
|
|
|
|
assert(rvalue);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
_cleanup_free_ char *word = NULL;
|
2015-09-25 14:23:23 +02:00
|
|
|
unsigned cpu, cpu_lower, cpu_upper;
|
2015-09-30 21:50:22 +02:00
|
|
|
int r;
|
|
|
|
|
2017-12-02 16:39:36 +01:00
|
|
|
r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES);
|
|
|
|
if (r == -ENOMEM)
|
|
|
|
return warn ? log_oom() : -ENOMEM;
|
2015-09-25 14:23:23 +02:00
|
|
|
if (r < 0)
|
2017-12-02 16:39:36 +01:00
|
|
|
return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
|
2015-09-30 21:50:22 +02:00
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!c) {
|
|
|
|
c = cpu_set_malloc(&ncpus);
|
|
|
|
if (!c)
|
2017-12-02 16:39:36 +01:00
|
|
|
return warn ? log_oom() : -ENOMEM;
|
2015-09-30 21:50:22 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 14:23:23 +02:00
|
|
|
r = parse_range(word, &cpu_lower, &cpu_upper);
|
|
|
|
if (r < 0)
|
2017-12-02 16:39:36 +01:00
|
|
|
return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
|
2015-09-25 14:23:23 +02:00
|
|
|
if (cpu_lower >= ncpus || cpu_upper >= ncpus)
|
2017-12-02 16:39:36 +01:00
|
|
|
return warn ? log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus) : -EINVAL;
|
2017-08-02 06:42:13 +02:00
|
|
|
|
2017-12-02 16:39:36 +01:00
|
|
|
if (cpu_lower > cpu_upper) {
|
|
|
|
if (warn)
|
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring", word, cpu_lower, cpu_upper);
|
|
|
|
continue;
|
2017-08-02 06:42:13 +02:00
|
|
|
}
|
|
|
|
|
2017-12-02 16:39:36 +01:00
|
|
|
for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
|
|
|
|
CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
|
2017-08-02 06:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* On success, sets *cpu_set and returns ncpus for the system. */
|
2018-04-05 07:26:26 +02:00
|
|
|
if (c)
|
|
|
|
*cpu_set = TAKE_PTR(c);
|
2017-08-02 06:42:13 +02:00
|
|
|
|
|
|
|
return (int) ncpus;
|
|
|
|
}
|