Skip to content

Instantly share code, notes, and snippets.

@flx42
Created December 4, 2019 18:34
Show Gist options
  • Save flx42/5378397cb00488e0c6fa5d41d8c5e63f to your computer and use it in GitHub Desktop.
Save flx42/5378397cb00488e0c6fa5d41d8c5e63f to your computer and use it in GitHub Desktop.
move_pages(2) issue
/* $ gcc move_pages_bug.c -lnuma -o move_pages_bug */
#define _DEFAULT_SOURCE
#include <sys/mman.h>
#include <numaif.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
const long node_id = 1;
const long page_size = sysconf(_SC_PAGESIZE);
const int64_t num_pages = 8;
unsigned long nodemask = 1 << node_id;
long ret = set_mempolicy(MPOL_BIND, &nodemask, sizeof(nodemask));
if (ret < 0)
return (EXIT_FAILURE);
void **pages = malloc(sizeof(void*) * num_pages);
for (int i = 0; i < num_pages; ++i) {
pages[i] = mmap(NULL, page_size, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_POPULATE | MAP_ANONYMOUS, -1, 0);
if (pages[i] == MAP_FAILED)
return (EXIT_FAILURE);
}
ret = set_mempolicy(MPOL_DEFAULT, NULL, 0);
if (ret < 0)
return (EXIT_FAILURE);
int *nodes = malloc(sizeof(int) * num_pages);
int *status = malloc(sizeof(int) * num_pages);
for (int i = 0; i < num_pages; ++i) {
nodes[i] = node_id;
status[i] = 0xd0;
}
ret = move_pages(0, num_pages, pages, nodes, status, MPOL_MF_MOVE);
printf("move_pages: %ld\n", ret);
for (int i = 0; i < num_pages; ++i)
printf("status[%d] = %d\n", i, status[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment