diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2015-01-21 23:27:31 -0200 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2015-01-21 23:27:31 -0200 |
commit | 3383d8e5561dfc6fb2b65e0a194df94ccb5e08af (patch) | |
tree | dafba704561386b31b6b8af6dafb5b9a2ba7453c /Affinity.c | |
parent | 36b78328843d0dae0d0fadbd0e814a8a1546327c (diff) |
Sorry about the mega-patch.
This is a work-in-progress, code is currently broken.
(Some actions, and notably, the header, are missing.)
Diffstat (limited to 'Affinity.c')
-rw-r--r-- | Affinity.c | 71 |
1 files changed, 70 insertions, 1 deletions
@@ -9,9 +9,18 @@ in the source distribution for its full text. #include <stdlib.h> +#ifdef HAVE_LIBHWLOC +#include <hwloc/linux.h> +#elif HAVE_NATIVE_AFFINITY +#include <sched.h> +#endif + /*{ +#include "Process.h" +#include "ProcessList.h" typedef struct Affinity_ { + ProcessList* pl; int size; int used; int* cpus; @@ -19,10 +28,11 @@ typedef struct Affinity_ { }*/ -Affinity* Affinity_new() { +Affinity* Affinity_new(ProcessList* pl) { Affinity* this = calloc(1, sizeof(Affinity)); this->size = 8; this->cpus = calloc(this->size, sizeof(int)); + this->pl = pl; return this; } @@ -40,3 +50,62 @@ void Affinity_add(Affinity* this, int id) { this->used++; } + +#ifdef HAVE_LIBHWLOC + +Affinity* Affinity_get(Process* proc, ProcessList* pl) { + hwloc_cpuset_t cpuset = hwloc_bitmap_alloc(); + bool ok = (hwloc_linux_get_tid_cpubind(pl->topology, proc->pid, cpuset) == 0); + Affinity* affinity = NULL; + if (ok) { + affinity = Affinity_new(pl); + if (hwloc_bitmap_last(cpuset) == -1) { + for (int i = 0; i < pl->cpuCount; i++) { + Affinity_add(affinity, i); + } + } else { + unsigned int id; + hwloc_bitmap_foreach_begin(id, cpuset); + Affinity_add(affinity, id); + hwloc_bitmap_foreach_end(); + } + } + hwloc_bitmap_free(cpuset); + return affinity; +} + +bool Affinity_set(Process* proc, Affinity* this) { + hwloc_cpuset_t cpuset = hwloc_bitmap_alloc(); + for (int i = 0; i < affinity->used; i++) { + hwloc_bitmap_set(cpuset, affinity->cpus[i]); + } + bool ok = (hwloc_linux_set_tid_cpubind(this->pl->topology, proc->pid, cpuset) == 0); + hwloc_bitmap_free(cpuset); + return ok; +} + +#elif HAVE_NATIVE_AFFINITY + +Affinity* Affinity_get(Process* proc, ProcessList* pl) { + cpu_set_t cpuset; + bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0); + if (!ok) return NULL; + Affinity* affinity = Affinity_new(pl); + for (int i = 0; i < pl->cpuCount; i++) { + if (CPU_ISSET(i, &cpuset)) + Affinity_add(affinity, i); + } + return affinity; +} + +bool Affinity_set(Process* proc, Affinity* this) { + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + for (int i = 0; i < this->used; i++) { + CPU_SET(this->cpus[i], &cpuset); + } + bool ok = (sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset) == 0); + return ok; +} + +#endif |