cpu-set-util: add parse_cpu_set()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Aug 2017 04:42:13 +0000 (13:42 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 7 Aug 2017 14:40:40 +0000 (23:40 +0900)
src/basic/cpu-set-util.c
src/basic/cpu-set-util.h

index 95ed692..aec0edc 100644 (file)
@@ -112,3 +112,49 @@ int parse_cpu_set_and_warn(
 
         return (int) ncpus;
 }
+
+int parse_cpu_set(
+                const char *rvalue,
+                cpu_set_t **cpu_set) {
+
+        _cleanup_cpu_free_ cpu_set_t *c = NULL;
+        unsigned ncpus = 0;
+
+        assert(rvalue);
+
+        for (;;) {
+                _cleanup_free_ char *word = NULL;
+                unsigned cpu, cpu_lower, cpu_upper;
+                int r;
+
+                r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES);
+                if (r == -ENOMEM)
+                        return r;
+                if (r <= 0)
+                        break;
+
+                if (!c) {
+                        c = cpu_set_malloc(&ncpus);
+                        if (!c)
+                                return -ENOMEM;
+                }
+
+                r = parse_range(word, &cpu_lower, &cpu_upper);
+                if (r < 0)
+                        return r;
+                if (cpu_lower >= ncpus || cpu_upper >= ncpus)
+                        return -EINVAL;
+
+                if (cpu_lower <= cpu_upper)
+                        for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
+                                CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
+        }
+
+        /* On success, sets *cpu_set and returns ncpus for the system. */
+        if (c) {
+                *cpu_set = c;
+                c = NULL;
+        }
+
+        return (int) ncpus;
+}
index 6f49d9a..9b08ba0 100644 (file)
@@ -30,3 +30,4 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE);
 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
 
 int parse_cpu_set_and_warn(const char *rvalue, cpu_set_t **cpu_set, const char *unit, const char *filename, unsigned line, const char *lvalue);
+int parse_cpu_set(const char *rvalue, cpu_set_t **cpu_set);