How to allocate a stack on a specific NUMA node?

Genie86

Junior Member
May 16, 2012
2
0
0
I would like to know if there is a way to create the stack of a thread on a specific NUMA node. I have written this code but i'm not sure if it does the trick or not.

Code:
pthread_t thread1;

int main(int argc, char**argv) {        
  pthread_attr_t attr;
  pthread_attr_init(&attr);

  char** stackarray;
  int numanode = 1;

  stackarray = (char**) numa_alloc_onnode(sizeof(char*), numanode);
  // considering that the newly 
  // created thread will be running on a core on node1

  pthread_attr_setstack(&attr, stackarray[0], 1000000);
  pthread create(&thread1, NULL, function, (void*)0);

  ...
  ...
}
Thanks
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
1. You probably want to allocate more than one pointer's worth of memory for a stack, say, 64KB instead. You're also telling pthread_attr_setstack the stack size is 1 million bytes, but you're only allocating ~8. (though numa_alloc_onnode rounds up to the page size).

2. No need to indirect though char**:
Code:
void* stack_location = numa_alloc_onnode(64*1024, numa_node);
if (NULL == stack_location) {
  ... // failure
}
if (0 == pthread_attr_setstack(&attr, stack_location, 64*1024)) {
  ... // success
} else {
  ... // failure
}