问题
I am trying to use mbind() in my C++ code in order to rearrange virtual pages across 4 NUMA domains, unfortunately I am new to this function:
long mbind(void *addr, unsigned long len, int mode, const unsigned long *nodemask,
unsigned long maxnode, unsigned flags);
Currently, I have something like this:
mbind(0x0,4611686018424767488,MPOL_BIND,nodemask,maxnode,MPOL_MF_MOVE);
From the specs it's still unclear to me what to put and how to put for nodemask
and maxnode
.
回答1:
nodemask
is a pointer to a bitmask of the allowed NUMA nodes. The bitmask is an array of unsigned long
elements with each array element holding as many bits as the size of unsigned long int
on the particular architecture allows. Unless your program is running on a really big NUMA system, a single unsigned long
variable should suffice.
maxnode
gives the number of significant bits in nodemask
. The kernel rounds internally the size to a multiple of sizeof(unsigned long)
but only uses maxnode
bits.
There are many examples and libraries around that allow you to create and conveniently manipulate bitmasks without having to fiddle with bit operations yourself. You can utilise libnuma
. It doesn't allow you to set the MPOL_MF_MOVE
policy but includes functions for the creation and manipulation of nodemasks.
A terrible and very unportable Linux pro-tip: The existing macros that deal with the CPU affinity masks, namely CPU_ZERO
/ CPU_SET
/ CPU_CLR
and the associated data structure cpu_set_t
can also be used for NUMA nodemasks. The reason for that is that (1) both are implemented as arrays of unsigned long
and (2) there are usually less NUMA nodes than logical CPUs, therefore cpu_set_t
should have enough bits to be able to represent all NUMA nodes on the system.
Side note: 4611686018424767488
should probably be suffixed with LL
.
来源:https://stackoverflow.com/questions/32859409/is-there-any-example-of-using-mbind-in-c-c-code