Return value of struct bitmask *numa_get_membind

142 Views Asked by At

I bind memory to run program on node 1. I insert some print code in the program to check current binded node. I found a function from numa.h:

struct bitmask *numa_get_membind

But I couldn't know how to interpret the return value.

What I want to get from the return value is 'node 1'.

1

There are 1 best solutions below

0
iXCray On

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.