1

I have allocated an array in C as follows:

void *mem = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);

Imagine this array is initialized and now I need to migrate (pin) it to a NUMA node. I know I can use numa_alloc_on_node() function to directly allocate on a node. However, please note that the concern here is to migrate the already allocated memory to a specified node. Is there any way to do this in C?

Thanks

1
  • 1
    please don't tag C questions with C++. Commented Aug 15, 2022 at 20:10

2 Answers 2

2

It seems numa_move_pages from numalib does the job. Here one can find its usage: https://github.com/numactl/numactl/blob/master/test/migrate_pages.c

These examples are also useful: https://cpp.hotexamples.com/examples/-/-/numa_move_pages/cpp-numa_move_pages-function-examples.html

There is also another function numa_migrate_page which migrates the entire pages that belongs to the PID.

Hope this helps someone in the future.

EDIT

numa_tonode_memory() from libnuma is probably the easiest way to achieve this without getting involved with extra parametes.

Sign up to request clarification or add additional context in comments.

1 Comment

Well found! It appears that my supposition above is correct. The actual system call that numa_move_pages uses is detailed in the Linux source code here github.com/torvalds/linux/blob/…, and indeed step 12) copies the content of one set of pages into the new set of pages. The system call may save time if you already have allocated the new pages, but otherwise that would have to be taken into account too in the overall runtime needed.
1

I don't think there is, as such.

It's worth considering what would be involved in doing so; it would involve copying data from where it currently is in memory, to another place in memory. One could achieve what you want by using numa_alloc_on_node() for where you want the data to got to, and then memcpy() to put the data there.

If there were an all-in-one solution (library function, or op-code, or whatever), it'd have to do basically those operations anyway; there's no magic short cut that could be taken by the CPU's electronics to achieve the same end result.

So I'm fairly sure that you won't be missing out if you do do this with a call to numa_alloc_on_node() followed by a memcpy(). It's worth noting that with today's CPUs (some support internal DMAs) a good memcpy implementation will be moving data as fast as the electronics can achieve anyway.

The pointer to the data changes, of course.

2 Comments

Thanks for your answer. I'm afraid there should be a system call for that purpose. In the end, the kernel does this frequently, and tools such as migratepages from numactl does this too but in kind of different context.
numa_set_membind does this too but for the whole program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.