Commit d6f50eb3 authored by nextime's avatar nextime

Add sched_setaffinity override too

parent 48dc2dce
......@@ -56,6 +56,8 @@ static unsigned int ncore;
static unsigned int setcore;
static bool pinthread_debug = false;
static cpu_set_t mask;
static int (*real_pthread_create)(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
......@@ -69,7 +71,11 @@ static int (*real_pthread_attr_setaffinity_np)(pthread_attr_t *attr,
size_t cpusetsize, const cpu_set_t *cpuset);
static void pint_debug(char *msg)
static int (*real_sched_setaffinity)(pid_t pid, size_t cpusetsize,
const cpu_set_t *mask);
static void pint_debug(const char *msg, ...)
{
if(pinthread_debug)
fprintf(stderr, "D:PINTHREAD %s\n", msg);
......@@ -79,7 +85,6 @@ 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]);
......@@ -113,7 +118,7 @@ static void main_pinthread(int argc, char* argv[], char* envp[])
if (msg != NULL)
{
setcore = (unsigned int) strtoul(msg, (char **)NULL, 10);
pint_debug("Setting core as configured: %u", setcore)
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);
......@@ -125,8 +130,16 @@ static void main_pinthread(int argc, char* argv[], char* envp[])
}
CPU_SET(setcore, &mask);
pint_debug("Set real_sched_setaffinity");
real_sched_setaffinity = dlsym(RTLD_NEXT,"sched_setaffinity");
if ((msg=dlerror())!=NULL)
fprintf(stderr, "E:PINTHREAD sched_setaffinity dlsym failed : %s\n", msg);
// make sure the main thread is running on the same core:
sched_setaffinity(getpid(), sizeof(mask), &mask);
real_sched_setaffinity(getpid(), sizeof(mask), &mask);
pint_debug("Affinity of main process configured");
pint_debug("Set real_pthread_create");
......@@ -154,9 +167,6 @@ int pthread_create(pthread_t *thread,
void *(*start_routine) (void *), void *arg)
{
int ret;
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(setcore, &mask);
pint_debug("about to call original pthread_create");
......@@ -176,12 +186,8 @@ int pthread_create(pthread_t *thread,
*/
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
const cpu_set_t *cpuset);
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)
{
......@@ -194,9 +200,6 @@ int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
int pthread_attr_setaffinity_np(pthread_attr_t *attr,
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_attr_setaffinity_np");
if(pinthread_override)
......@@ -207,3 +210,17 @@ int pthread_attr_setaffinity_np(pthread_attr_t *attr,
return real_pthread_attr_setaffinity_np(attr, cpusetsize, cpuset);
}
int sched_setaffinity(pid_t pid, size_t cpusetsize,
const cpu_set_t *omask)
{
pint_debug("about to call original sched_setaffinity");
if(pinthread_override)
{
pint_debug("Overriding sched_setaffinity");
return real_sched_setaffinity(pid, sizeof(mask), &mask);
}
return real_sched_setaffinity(pid, cpusetsize, omask);
}
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