]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cpu-set-util: add cpu_set_count() helper
authorLennart Poettering <lennart@amutable.com>
Wed, 13 May 2026 12:58:21 +0000 (14:58 +0200)
committerLennart Poettering <lennart@amutable.com>
Thu, 14 May 2026 06:12:28 +0000 (08:12 +0200)
Let's add a minor, simplifying helper for getting number of CPUs in a
mask.

src/shared/cpu-set-util.h
src/shared/numa-util.c
src/test/test-cpu-set-util.c

index a74e264e1267b6af250ea04024ab07d734ea129f..751e9add27f09af143fc41ffce6a3e6e1d1e9f6c 100644 (file)
@@ -38,3 +38,11 @@ int cpu_set_to_dbus(const CPUSet *c, uint8_t **ret, size_t *ret_size);
 int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *ret);
 
 int cpus_in_affinity_mask(void);
+
+static inline size_t cpu_set_count(const CPUSet *c) {
+        if (!c)
+                return 0;
+        if (c->allocated <= 0)
+                return 0;
+        return CPU_COUNT_S(c->allocated, c->set);
+}
index c011c4381d4222ce4e66dc677222b45a1e23154d..228ea7ad2d14f7f95d8595b14dd3d2114efb9363 100644 (file)
@@ -26,7 +26,7 @@ bool numa_policy_is_valid(const NUMAPolicy *policy) {
 
         if (policy->nodes.set &&
             numa_policy_get_type(policy) == MPOL_PREFERRED &&
-            CPU_COUNT_S(policy->nodes.allocated, policy->nodes.set) != 1)
+            cpu_set_count(&policy->nodes) != 1)
                 return false;
 
         return true;
index bd22e4ddb2aef8ef8bf81efad6bd8bc71d2cfce2..672451ff3557636886c98ae49179260c876327bd 100644 (file)
@@ -11,7 +11,8 @@
 #define ASSERT_CPUSET_COUNT(c, n)                                       \
         ASSERT_NOT_NULL(c.set);                                         \
         ASSERT_GE(c.allocated, CPU_ALLOC_SIZE(n));                      \
-        ASSERT_EQ(CPU_COUNT_S(c.allocated, c.set), (n))
+        ASSERT_EQ(CPU_COUNT_S(c.allocated, c.set), (n));                \
+        ASSERT_EQ(cpu_set_count(&c), (size_t) (n))
 
 #define ASSERT_CPUSET_ISSET(c, i)                               \
         ASSERT_TRUE(CPU_ISSET_S(i, c.allocated, c.set));