- // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
+#ifdef HAVE_SCHED
+#include <sched.h>
+#endif
Thread::Thread()
: thread_id(0),
pid(0),
ioprio_class(-1),
- ioprio_priority(-1)
+ ioprio_priority(-1),
+ cpuid(-1)
{
}
pid,
IOPRIO_PRIO_VALUE(ioprio_class, ioprio_priority));
}
+ if (pid && cpuid >= 0)
+ set_affinity(cpuid);
return entry();
}
IOPRIO_PRIO_VALUE(cls, prio));
return 0;
}
+
+int Thread::set_affinity(int id)
+{
+#ifdef HAVE_SCHED
+ if (pid && id >= 0 && id < CPU_SETSIZE) {
+ cpu_set_t cpuset;
+ CPU_ZERO(&cpuset);
+
+ CPU_SET(id, &cpuset);
+
+ if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0)
+ return -errno;
+ /* guaranteed to take effect immediately */
+ sched_yield();
+ }
+#endif
+ cpuid = id;
+ return 0;
+}
pthread_t thread_id;
pid_t pid;
int ioprio_class, ioprio_priority;
+ int cpuid;
void *entry_wrapper();
int join(void **prval = 0);
int detach();
int set_ioprio(int cls, int prio);
+ int set_affinity(int cpuid);
};
#endif