# Linux NUMA system calls ` numaif.h `
Experimented by ` Julian Fang ` [JulianATA]()
## `numaif.h`
:::info
should be linked with ` -lnuma ` while building.
:::
` numaif.h ` is a wrapper for basic system calls related to NUMA management which are not included in ` libc `
There are 2 parts of syscall in this header:
* memory allocation policy setting:
* get_mempolicy();
* mbind();
* set_mempolicy();
* memory migrations:
* migrate_pages();
* move_pages();
Comparing with `numa.h`, it provides ` low level ` syscall to build the NUMA management functions, while ` numa.h ` provides ` high level ` functions.
If our purpose is to build a function
## Experiments
:::info
Tested on 2 nodes (0, 2) of a 4 nodes machine, which have 64 cores.
:::
``` bash
$uname -a
Linux 4.15.0-62-generic
```
---
### Memory Policies
* MPOL_DEFAULT
* Remove all other policies. Set the memory policy falls back to the system default policy.
* Default policy is `local allocation`:
* allocate memory on the node of the CPU that triggered the allocation.
* If the "local node" contains no free memory, the system will attempt to allocate memory from a "near by" node.
* nodemask must be specified as NULL.
* MPOL_PREFERRED
* This mode sets the preferred node for allocation based on `nodemask`.
* `nodemask` can specify more than one node, the first node will be selected as the preferred node/
* If the `nodemask` and `maxnode` are empty, the policy specifies ` local allocation `(default).
* If the node is nearly full, it'll fall back to "near by" nodes.
* MPOL_BIND
* Strict policy!
* restricts memory allocation to the nodes specified in nodemask.
* Allocate memories from lowest node to high node ID node.
* MPOL_INTERLEAVE
* Optimize for bandwidth instead of latency.
* allocations across the nodes specified in nodemask in numeric node ID order.
* MPOL_LOCAL
* MPOL_MAX
---
### set_mempolicy();
```c
long set_mempolicy(
int mode,
const unsigned long *nodemask,
unsigned long maxnode
);
```
Description:
* Sets the NUMA memory policy of the ` calling thread `, which consists of a ` policy mode` and `zero or more nodes`, to the values specified by the mode, nodemask and maxnode arguments.
For short:
* We can set the NUMA memory policy of a ` thread ` based on
* policy mode
* nodes
* Described by
* mode (policy mode)
* nodemask (nodes)
* maxnode (limitation of nodes)
### get_mempolicy();
``` c
long get_mempolicy(
int *mode,
unsigned long *nodemask,
unsigned long maxnode,
void *addr,
unsigned long flags
);
```
Description:
* Retrieves the NUMA policy of the calling thread or of a memory address, depending on the setting of flags.
###### tags: `NUMA`