Commit 58c9759b authored by nextime's avatar nextime

Add PINTHREAD_CORE env var to let user choose which core to pin

parent febc3903
......@@ -7,27 +7,13 @@
//#include <sys/types.h>
//#include <unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <dlfcn.h>
#include <sched.h> //cpu_set_t , CPU_SET
typedef struct {
void *arg;
void *(*fn)(void *);
} real_args;
void *create_head(void *arg)
real_args *a = arg;
//fprintf(stderr,"PID %d TID %d\n",
// getpid(),(int)pthread_self());
return a->fn(a->arg);
static int (*real_pthread_create)(pthread_t *thread,
const pthread_attr_t *attr,
......@@ -38,18 +24,28 @@ int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
//real_args targs;
char *msg;
int ret;
unsigned int ncore=sysconf (_SC_NPROCESSORS_CONF);
unsigned int setcore=NULL;
cpu_set_t mask; /* Define your cpu_set bit mask. */
CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
//CPU_SET(sched_getcpu(), &mask); /* set the bit that represents the core. */
// XXX if we are running under qemu, sched_getcpu() will give us a unimplemented syscall.
CPU_SET(0, &mask);
//targs.arg = arg;
//targs.fn = start_routine;
msg = getenv("PINTHREAD_CORE");
if (msg!=NULL)
setcore = (unsigned int) strtoul(msg, (char **)NULL, 10);
if(setcore < ncore)
CPU_SET(setcore, &mask);
} else {
fprintf(stderr, "**PINPTHREAD wrong value for PINTHREAD_CORE: %u - using default.\n", setcore);
CPU_SET(sched_getcpu(), &mask);
} else {
CPU_SET(setcore, &mask);
if (real_pthread_create == NULL)
......@@ -58,12 +54,11 @@ int pthread_create(pthread_t *thread,
real_pthread_create = dlsym(RTLD_NEXT,"pthread_create");
if ((msg=dlerror())!=NULL)
printf("**fopen dlopen failed : %s\n", msg);
printf("*wrapping done\n");
fprintf(stderr, "**PINPTHREAD pthread_create dlsym failed : %s\n", msg);
//printf("*wrapping done\n");
printf("*about to call original pthread_create\n");
//return real_pthread_create(thread,attr,create_head,&targs);
//printf("*about to call original pthread_create\n");
ret = real_pthread_create(thread,attr,start_routine,arg);
pthread_setaffinity_np(*thread, sizeof(mask), &mask);
return ret;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment