Commit eb9dc275 authored by nextime's avatar nextime

Moving code in our "main" and add PINTHREAD_PNAMES env

parent 89391ad7
......@@ -14,66 +14,87 @@
#include <dlfcn.h>
#include <sched.h> //cpu_set_t , CPU_SET
#include <libgen.h>
#include <stdbool.h>
#include <string.h>
static char *procname;
static void set_procname(int argc, char* argv[], char* envp[])
{
procname = basename(argv[0]);
}
__attribute__((section(".init_array"))) void (* p_my_cool_main)(int,char*[],char*[]) = &set_procname;
static bool pinthread_override = false;
static unsigned int ncore;
static unsigned int setcore;
static int (*real_pthread_create)(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
static void main_pinthread(int argc, char* argv[], char* envp[])
{
char *pch;
char *msg;
cpu_set_t mask; /* Define your cpu_set bit mask. */
CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
procname = basename(argv[0]);
msg = getenv("PINTHREAD_PNAMES");
if(msg == NULL)
{
pinthread_override = true;
} else {
pch = strtok (msg," ");
while (pch != NULL)
{
if (!strcmp(procname,pch))
{
pinthread_override = true;
pch = NULL;
} else {
pch = strtok (NULL," ");
}
}
}
ncore = sysconf (_SC_NPROCESSORS_CONF);
msg = getenv("PINTHREAD_CORE");
if (msg != NULL)
{
setcore = (unsigned int) strtoul(msg, (char **)NULL, 10);
if(setcore >= ncore)
{
fprintf(stderr, "E:PINTHREAD wrong value for PINTHREAD_CORE: %u - using default.\n", setcore);
setcore = sched_getcpu();
}
} else {
setcore = sched_getcpu();
}
CPU_SET(setcore, &mask);
// make sure the main thread is running on the same core:
sched_setaffinity(getpid(), sizeof(mask), &mask);
real_pthread_create = dlsym(RTLD_NEXT,"pthread_create");
if ((msg=dlerror())!=NULL)
fprintf(stderr, "E:PINTHREAD pthread_create dlsym failed : %s\n", msg);
//printf("*wrapping done\n");
}
__attribute__((section(".init_array"))) void (* p_main_pinthread)(int,char*[],char*[]) = &main_pinthread;
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
{
char *msg;
int ret;
static unsigned int ncore;
static unsigned int setcore=-1;
cpu_set_t mask; /* Define your cpu_set bit mask. */
CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
if(setcore == -1)
{
ncore = sysconf (_SC_NPROCESSORS_CONF);
msg = getenv("PINTHREAD_CORE");
if (msg != NULL)
{
setcore = (unsigned int) strtoul(msg, (char **)NULL, 10);
if(setcore >= ncore)
{
fprintf(stderr, "**PINTHREAD wrong value for PINTHREAD_CORE: %u - using default.\n", setcore);
setcore = sched_getcpu();
}
} else {
setcore = sched_getcpu();
}
}
CPU_SET(setcore, &mask);
if (real_pthread_create == NULL)
{
// make sure the main thread is running on the same core:
sched_setaffinity(getpid(), sizeof(mask), &mask);
real_pthread_create = dlsym(RTLD_NEXT,"pthread_create");
if ((msg=dlerror())!=NULL)
fprintf(stderr, "**PINTHREAD pthread_create dlsym failed : %s\n", msg);
//printf("*wrapping done\n");
}
//printf("*about to call original pthread_create\n");
ret = real_pthread_create(thread,attr,start_routine,arg);
pthread_setaffinity_np(*thread, sizeof(mask), &mask);
if(pinthread_override)
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