cpu-set-util: add cpu_set_count() helper

Let's add a minor, simplifying helper for getting number of CPUs in a
mask.
This commit is contained in:
Lennart Poettering
2026-05-14 08:12:28 +02:00
parent 95e7ead268
commit f78e7a0949
3 changed files with 11 additions and 2 deletions
+8
View 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);
}
+1 -1
View 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;
+2 -1
View 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));