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