]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | #include "cpu-set-util.h" | |
24 | #include "extract-word.h" | |
25 | #include "parse-util.h" | |
26 | #include "util.h" | |
27 | ||
28 | cpu_set_t* cpu_set_malloc(unsigned *ncpus) { | |
29 | cpu_set_t *c; | |
30 | unsigned n = 1024; | |
31 | ||
32 | /* Allocates the cpuset in the right size */ | |
33 | ||
34 | for (;;) { | |
35 | c = CPU_ALLOC(n); | |
36 | if (!c) | |
37 | return NULL; | |
38 | ||
39 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) { | |
40 | CPU_ZERO_S(CPU_ALLOC_SIZE(n), c); | |
41 | ||
42 | if (ncpus) | |
43 | *ncpus = n; | |
44 | ||
45 | return c; | |
46 | } | |
47 | ||
48 | CPU_FREE(c); | |
49 | ||
50 | if (errno != EINVAL) | |
51 | return NULL; | |
52 | ||
53 | n *= 2; | |
54 | } | |
55 | } | |
56 | ||
57 | int parse_cpu_set_and_warn( | |
58 | const char *rvalue, | |
59 | cpu_set_t **cpu_set, | |
60 | const char *unit, | |
61 | const char *filename, | |
62 | unsigned line, | |
63 | const char *lvalue) { | |
64 | ||
65 | const char *whole_rvalue = rvalue; | |
66 | _cleanup_cpu_free_ cpu_set_t *c = NULL; | |
67 | unsigned ncpus = 0; | |
68 | ||
69 | assert(lvalue); | |
70 | assert(rvalue); | |
71 | ||
72 | for (;;) { | |
73 | _cleanup_free_ char *word = NULL; | |
74 | unsigned cpu; | |
75 | int r; | |
76 | ||
77 | r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES); | |
78 | if (r < 0) { | |
79 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue); | |
80 | return r; | |
81 | } | |
82 | if (r == 0) | |
83 | break; | |
84 | ||
85 | if (!c) { | |
86 | c = cpu_set_malloc(&ncpus); | |
87 | if (!c) | |
88 | return log_oom(); | |
89 | } | |
90 | ||
91 | r = safe_atou(word, &cpu); | |
92 | if (r < 0 || cpu >= ncpus) { | |
93 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", rvalue); | |
94 | return -EINVAL; | |
95 | } | |
96 | ||
97 | CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c); | |
98 | } | |
99 | ||
100 | /* On success, sets *cpu_set and returns ncpus for the system. */ | |
101 | if (c) { | |
102 | *cpu_set = c; | |
103 | c = NULL; | |
104 | } | |
105 | ||
106 | return (int) ncpus; | |
107 | } |