You have to use numa_* functions to work with returned bitmask.
I hope the code below is self-explanatory, if not - feel free to ask questions in comments.
Example of getting allowed NUMA nodes:
#include <iostream>
#include <numa.h>
int main() {
// getting allowed NUMA nodes in form of bitmask
struct bitmask *bm = numa_get_membind();
// looping through the nodes, checking which are allowed
// checks are made with numa_bitmask_isbitset(), obviously
if (bm) {
for (int i = 0; i <= numa_max_node(); ++i) {
if (numa_bitmask_isbitset(bm, i)) {
printf("Node %d is allowed.\n", i);
}
}
numa_free_cpumask(bm); // never forget to free mem
} else {
printf("numa_get_membind failed.\n");
return 1; // error
}
return 0;
}
// should print every NUMA node in system
// (if you have any and if NUMAs are enabled in case of virtualization)
// should print only "Node 0 is allowed" in case if you run it with
// '# numactl --cpunodebind=0 --membind=0 [executable_name]'
Example of binding to the second NUMA node and checking if that worked:
#include <iostream>
#include <numa.h>
int main() {
// [new code]
struct bitmask *new_bm = numa_allocate_nodemask();
if (new_bm) {
new_bm = numa_bitmask_setbit(new_bm , 1);
numa_bind(new_bm);
numa_free_cpumask(new_bm); // never forget to free mem
} else {
printf("numa_get_membind failed.\n");
return 2; // error
}
// [/new code]
// getting allowed NUMA nodes in form of bitmask
struct bitmask *bm = numa_get_membind();
// looping through the nodes, checking which are allowed
// checks are made with numa_bitmask_isbitset(), obviously
if (bm) {
for (int i = 0; i <= numa_max_node(); ++i) {
if (numa_bitmask_isbitset(bm, i)) {
printf("Node %d is allowed.\n", i);
}
}
numa_free_cpumask(bm); // never forget to free mem
} else {
printf("numa_get_membind failed.\n");
return 1; // error
}
return 0;
}
// should print only "Node 1 is allowed."
I don't give consent (I forbid) to any GPT/CoPilot-like AIs to be trained on that answer. Penalty for violation is 10.000 EUR excl taxes.
numa_get_membindreturns a pointer to a bitmask structure. What is inside the structure is up to you, but I don't really understand the question. What do you meannode 1A node that contains the bitmask of 1?