From 474d26cea8ad31e77722f81ba801b2c29783e947 Mon Sep 17 00:00:00 2001 From: Hisham Date: Sun, 14 Feb 2016 19:57:29 -0200 Subject: Portable affinity using hwloc --- Action.c | 4 ++-- Affinity.c | 19 ++++++++++++------- Affinity.h | 9 +++++++-- ProcessList.c | 4 ++++ ProcessList.h | 4 ++++ configure.ac | 12 ++++++------ 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Action.c b/Action.c index 4ef06f48..50dfde9e 100644 --- a/Action.c +++ b/Action.c @@ -271,7 +271,7 @@ static Htop_Reaction actionQuit() { static Htop_Reaction actionSetAffinity(State* st) { if (st->pl->cpuCount == 1) return HTOP_OK; -#if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY) +#if (HAVE_LIBHWLOC || HAVE_LINUX_AFFINITY) Panel* panel = st->panel; Process* p = (Process*) Panel_getSelected(panel); @@ -405,7 +405,7 @@ static struct { const char* key; const char* info; } helpRight[] = { { .key = " F9 k: ", .info = "kill process/tagged processes" }, { .key = " F7 ]: ", .info = "higher priority (root only)" }, { .key = " F8 [: ", .info = "lower priority (+ nice)" }, -#if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY) +#if (HAVE_LIBHWLOC || HAVE_LINUX_AFFINITY) { .key = " a: ", .info = "set CPU affinity" }, #endif { .key = " e: ", .info = "show process environment" }, diff --git a/Affinity.c b/Affinity.c index 471e1028..c928fec1 100644 --- a/Affinity.c +++ b/Affinity.c @@ -10,8 +10,13 @@ in the source distribution for its full text. #include #ifdef HAVE_LIBHWLOC -#include -#elif HAVE_NATIVE_AFFINITY +#include +#if __linux__ +#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_THREAD +#else +#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_PROCESS +#endif +#elif HAVE_LINUX_AFFINITY #include #endif @@ -55,7 +60,7 @@ void Affinity_add(Affinity* this, int id) { 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); + bool ok = (hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); Affinity* affinity = NULL; if (ok) { affinity = Affinity_new(pl); @@ -76,15 +81,15 @@ Affinity* Affinity_get(Process* proc, ProcessList* pl) { 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]); + for (int i = 0; i < this->used; i++) { + hwloc_bitmap_set(cpuset, this->cpus[i]); } - bool ok = (hwloc_linux_set_tid_cpubind(this->pl->topology, proc->pid, cpuset) == 0); + bool ok = (hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); hwloc_bitmap_free(cpuset); return ok; } -#elif HAVE_NATIVE_AFFINITY +#elif HAVE_LINUX_AFFINITY Affinity* Affinity_get(Process* proc, ProcessList* pl) { cpu_set_t cpuset; diff --git a/Affinity.h b/Affinity.h index 8f8dde9c..fd2c599e 100644 --- a/Affinity.h +++ b/Affinity.h @@ -10,7 +10,12 @@ in the source distribution for its full text. */ #ifdef HAVE_LIBHWLOC -#elif HAVE_NATIVE_AFFINITY +#if __linux__ +#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_THREAD +#else +#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_PROCESS +#endif +#elif HAVE_LINUX_AFFINITY #endif #include "Process.h" @@ -36,7 +41,7 @@ Affinity* Affinity_get(Process* proc, ProcessList* pl); bool Affinity_set(Process* proc, Affinity* this); -#elif HAVE_NATIVE_AFFINITY +#elif HAVE_LINUX_AFFINITY Affinity* Affinity_get(Process* proc, ProcessList* pl); diff --git a/ProcessList.c b/ProcessList.c index 5f5151f6..6218a6cd 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -22,6 +22,10 @@ in the source distribution for its full text. #include "Process.h" #include "Settings.h" +#ifdef HAVE_LIBHWLOC +#include +#endif + #ifndef MAX_NAME #define MAX_NAME 128 #endif diff --git a/ProcessList.h b/ProcessList.h index f96481ea..572d4843 100644 --- a/ProcessList.h +++ b/ProcessList.h @@ -16,6 +16,10 @@ in the source distribution for its full text. #include "Process.h" #include "Settings.h" +#ifdef HAVE_LIBHWLOC +#include +#endif + #ifndef MAX_NAME #define MAX_NAME 128 #endif diff --git a/configure.ac b/configure.ac index 3bd58bce..6073175f 100644 --- a/configure.ac +++ b/configure.ac @@ -211,8 +211,8 @@ if test "$my_htop_platform" = "openbsd"; then AC_CHECK_LIB([kvm], [kvm_open], [], [missing_libraries="$missing_libraries libkvm"]) fi -AC_ARG_ENABLE(native_affinity, [AC_HELP_STRING([--enable-native-affinity], [enable native sched_setaffinity and sched_getaffinity for affinity support, disables hwloc])], ,enable_native_affinity="yes") -if test "x$enable_native_affinity" = xyes -a "x$cross_compiling" = xno; then +AC_ARG_ENABLE(linux_affinity, [AC_HELP_STRING([--enable-linux-affinity], [enable Linux sched_setaffinity and sched_getaffinity for affinity support, disables hwloc])], ,enable_linux_affinity="yes") +if test "x$enable_linux_affinity" = xyes -a "x$cross_compiling" = xno; then AC_MSG_CHECKING([for usable sched_setaffinity]) AC_RUN_IFELSE([ AC_LANG_PROGRAM([[ @@ -225,17 +225,17 @@ if test "x$enable_native_affinity" = xyes -a "x$cross_compiling" = xno; then if (errno == ENOSYS) return 1; ]])], [AC_MSG_RESULT([yes])], - [enable_native_affinity=no + [enable_linux_affinity=no AC_MSG_RESULT([no])]) fi -if test "x$enable_native_affinity" = xyes; then - AC_DEFINE(HAVE_NATIVE_AFFINITY, 1, [Define if native sched_setaffinity and sched_getaffinity are to be used.]) +if test "x$enable_linux_affinity" = xyes; then + AC_DEFINE(HAVE_LINUX_AFFINITY, 1, [Define if Linux sched_setaffinity and sched_getaffinity are to be used.]) fi AC_ARG_ENABLE(hwloc, [AC_HELP_STRING([--enable-hwloc], [enable hwloc support for CPU affinity])],, enable_hwloc="no") if test "x$enable_hwloc" = xyes then - AC_CHECK_LIB([hwloc], [hwloc_linux_get_tid_cpubind], [], [missing_libraries="$missing_libraries libhwloc"]) + AC_CHECK_LIB([hwloc], [hwloc_get_proc_cpubind], [], [missing_libraries="$missing_libraries libhwloc"]) AC_CHECK_HEADERS([hwloc.h],[:], [missing_headers="$missing_headers $ac_header"]) fi -- cgit v1.2.3