我可以从指针地址(在Linux上的C中)获取NUMA节点吗?

时间:2011-11-02 20:27:23

标签: c linux multithreading memory numa

我已经设置了我的代码,以便在我的NUMA系统上本地小心地加载和处理数据。我认为。也就是说,出于调试目的,我真的希望能够使用在许多其他函数设置的特定函数内访问的指针地址来直接识别内存指向的NUMA节点。正在居住,所以我可以检查一切都在它应该位于的位置。这可能吗?

我在msdn http://social.msdn.microsoft.com/Forums/en-US/parallelcppnative/thread/37a02e17-e160-48d9-8625-871ff6b21f72上发现了同样的请求,但答案使用的是QueryWorkingSetEx(),它似乎是Windows特有的。这可以在Linux上完成吗?我准确地说是Debian Squeeze。

感谢。

2 个答案:

答案 0 :(得分:20)

move_pages中有-lnuma个功能:http://linux.die.net/man/2/move_pages

可以将当前状态的地址(页面)报告给节点映射:

  

节点也可以是NULL,在这种情况下,move_pages()不会移动任何页面,而是返回状态数组中每个页面当前所在的节点。可能需要获取每个页面的状态以确定需要移动的页面。

所以,电话可能就像:

 void * ptr_to_check = your_address;
 /*here you should align ptr_to_check to page boundary */
 int status[1];
 int ret_code;
 status[0]=-1;
 ret_code=move_pages(0 /*self memory */, 1, &ptr_to_check,
    NULL, status, 0);
 printf("Memory at %p is at %d node (retcode %d)\n", ptr_to_check, status[0], ret_code);

答案 1 :(得分:9)

或者,get_mempolicy http://linux.die.net/man/2/get_mempolicy

中有-lnuma:个功能
If flags specifies both MPOL_F_NODE and MPOL_F_ADDR, get_mempolicy() will 
return the node ID of the node on which the address addr is allocated into 
the location pointed to by mode. If no page has yet been allocated for the 
specified address, get_mempolicy() will allocate a page as if the process had 
performed a read [load] access to that address, and return the ID of the node 
where that page was allocated. 

因此,ptr指向的页面的numa节点将被检查:

int numa_node = -1;
get_mempolicy(&numa_node, NULL, 0, (void*)ptr, MPOL_F_NODE | MPOL_F_ADDR);
return numa_node;