Commit efaf9d0d authored by nextime's avatar nextime

Add debug and override of pthread_setaffinity_np call

parent 862c8c30
......@@ -34,6 +34,8 @@ dotprod_mutex is just a test binary you can use for testing the library.
otherwise it will just call the "real"
pthread_create without any pinning.
* PINTHREAD_DEBUG: if set, enable debug messages on stderr.
### Known bugs:
When used in qemu-static for arm* architectures setting
......
......@@ -36,6 +36,7 @@
* otherwise it will just call the "real"
* pthread_create without any pinning.
*
* - PINTHREAD_DEBUG: if set, enable debug messages on stderr.
*/
//#include <sys/types.h>
......@@ -53,11 +54,23 @@ static char *procname;
static bool pinthread_override = false;
static unsigned int ncore;
static unsigned int setcore;
static bool pinthread_debug = false;
static int (*real_pthread_create)(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
static int (*real_pthread_setaffinity_np)(pthread_t thread, size_t cpusetsize,
const cpu_set_t *cpuset);
static void pint_debug(char *msg)
{
if(pinthread_debug)
fprintf(stderr, "D:PINTHREAD %s\n", msg);
}
static void main_pinthread(int argc, char* argv[], char* envp[])
{
char *pch;
......@@ -67,6 +80,10 @@ static void main_pinthread(int argc, char* argv[], char* envp[])
procname = basename(argv[0]);
msg = getenv("PINTHREAD_DEBUG");
if(msg != NULL)
pinthread_debug = true;
msg = getenv("PINTHREAD_PNAMES");
if(msg == NULL)
{
......@@ -85,11 +102,14 @@ static void main_pinthread(int argc, char* argv[], char* envp[])
}
}
pint_debug("pinthread_main loaded");
ncore = sysconf (_SC_NPROCESSORS_CONF);
msg = getenv("PINTHREAD_CORE");
if (msg != NULL)
{
setcore = (unsigned int) strtoul(msg, (char **)NULL, 10);
pint_debug("Setting core as configured: %u", setcore)
if(setcore >= ncore)
{
fprintf(stderr, "E:PINTHREAD wrong value for PINTHREAD_CORE: %u - using default.\n", setcore);
......@@ -97,17 +117,23 @@ static void main_pinthread(int argc, char* argv[], char* envp[])
}
} else {
setcore = sched_getcpu();
pint_debug("Setting core by sched_getcpu: %u", setcore);
}
CPU_SET(setcore, &mask);
// make sure the main thread is running on the same core:
sched_setaffinity(getpid(), sizeof(mask), &mask);
pint_debug("Affinity of main process configured");
pint_debug("Set real_pthread_create");
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");
pint_debug("Set pthread_setaffinity_np");
real_pthread_setaffinity_np = dlsym(RTLD_NEXT,"pthread_setaffinity_np");
if ((msg=dlerror())!=NULL)
fprintf(stderr, "E:PINTHREAD pthread_setaffinity_np dlsym failed : %s\n", msg);
}
......@@ -123,13 +149,37 @@ int pthread_create(pthread_t *thread,
CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
CPU_SET(setcore, &mask);
//printf("*about to call original pthread_create\n");
pint_debug("about to call original pthread_create");
ret = real_pthread_create(thread,attr,start_routine,arg);
if(pinthread_override)
pthread_setaffinity_np(*thread, sizeof(mask), &mask);
{
pint_debug("Overriding pthread_creare");
real_pthread_setaffinity_np(*thread, sizeof(mask), &mask);
}
return ret;
}
/*
* We need to override also pthread_setaffinity family calls to manage cases in which
* the running program try to change it by itself
*/
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
const cpu_set_t *cpuset);
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(setcore, &mask);
pint_debug("about to call original pthread_setaffinity_np");
if(pinthread_override)
{
pint_debug("Overriding pthread_setaffinity_np");
return real_pthread_setaffinity_np(thread, sizeof(mask), &mask);
}
return real_pthread_setaffinity_np(thread, cpusetsize, cpuset);
}
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