]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/cpu-set-util.c
basic: re-sort includes
[thirdparty/systemd.git] / src / basic / cpu-set-util.c
CommitLineData
618234a5
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010-2015 Lennart Poettering
7 Copyright 2015 Filipe Brandenburger
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
11c3a366
TA
23#include <errno.h>
24#include <stddef.h>
25#include <syslog.h>
26
b5efdb8a 27#include "alloc-util.h"
6bedfcbb 28#include "cpu-set-util.h"
84ac7bea 29#include "extract-word.h"
11c3a366
TA
30#include "log.h"
31#include "macro.h"
93cc7779 32#include "parse-util.h"
b11d6a7b 33#include "string-util.h"
618234a5
LP
34
35cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
36 cpu_set_t *c;
37 unsigned n = 1024;
38
39 /* Allocates the cpuset in the right size */
40
41 for (;;) {
42 c = CPU_ALLOC(n);
43 if (!c)
44 return NULL;
45
46 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
47 CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
48
49 if (ncpus)
50 *ncpus = n;
51
52 return c;
53 }
54
55 CPU_FREE(c);
56
57 if (errno != EINVAL)
58 return NULL;
59
60 n *= 2;
61 }
62}
63
64int parse_cpu_set_and_warn(
65 const char *rvalue,
66 cpu_set_t **cpu_set,
67 const char *unit,
68 const char *filename,
69 unsigned line,
70 const char *lvalue) {
71
72 const char *whole_rvalue = rvalue;
73 _cleanup_cpu_free_ cpu_set_t *c = NULL;
74 unsigned ncpus = 0;
75
76 assert(lvalue);
77 assert(rvalue);
78
79 for (;;) {
80 _cleanup_free_ char *word = NULL;
a26662ce 81 unsigned cpu, cpu_lower, cpu_upper;
618234a5
LP
82 int r;
83
4fc66acb 84 r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES);
a26662ce
FB
85 if (r < 0)
86 return log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
618234a5
LP
87 if (r == 0)
88 break;
89
90 if (!c) {
91 c = cpu_set_malloc(&ncpus);
92 if (!c)
93 return log_oom();
94 }
95
a26662ce
FB
96 r = parse_range(word, &cpu_lower, &cpu_upper);
97 if (r < 0)
98 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word);
99 if (cpu_lower >= ncpus || cpu_upper >= ncpus)
100 return log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus);
101
102 if (cpu_lower > cpu_upper)
103 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u", word, cpu_lower, cpu_upper);
104 else
105 for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
106 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
618234a5
LP
107 }
108
109 /* On success, sets *cpu_set and returns ncpus for the system. */
110 if (c) {
111 *cpu_set = c;
112 c = NULL;
113 }
114
115 return (int) ncpus;
116}