Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 474d26c

Browse files
committed
Portable affinity using hwloc
1 parent 3565720 commit 474d26c

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

Action.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static Htop_Reaction actionQuit() {
271271
static Htop_Reaction actionSetAffinity(State* st) {
272272
if (st->pl->cpuCount == 1)
273273
return HTOP_OK;
274-
#if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY)
274+
#if (HAVE_LIBHWLOC || HAVE_LINUX_AFFINITY)
275275
Panel* panel = st->panel;
276276

277277
Process* p = (Process*) Panel_getSelected(panel);
@@ -405,7 +405,7 @@ static struct { const char* key; const char* info; } helpRight[] = {
405405
{ .key = " F9 k: ", .info = "kill process/tagged processes" },
406406
{ .key = " F7 ]: ", .info = "higher priority (root only)" },
407407
{ .key = " F8 [: ", .info = "lower priority (+ nice)" },
408-
#if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY)
408+
#if (HAVE_LIBHWLOC || HAVE_LINUX_AFFINITY)
409409
{ .key = " a: ", .info = "set CPU affinity" },
410410
#endif
411411
{ .key = " e: ", .info = "show process environment" },

Affinity.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ in the source distribution for its full text.
1010
#include <stdlib.h>
1111

1212
#ifdef HAVE_LIBHWLOC
13-
#include <hwloc/linux.h>
14-
#elif HAVE_NATIVE_AFFINITY
13+
#include <hwloc.h>
14+
#if __linux__
15+
#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_THREAD
16+
#else
17+
#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_PROCESS
18+
#endif
19+
#elif HAVE_LINUX_AFFINITY
1520
#include <sched.h>
1621
#endif
1722

@@ -55,7 +60,7 @@ void Affinity_add(Affinity* this, int id) {
5560

5661
Affinity* Affinity_get(Process* proc, ProcessList* pl) {
5762
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
58-
bool ok = (hwloc_linux_get_tid_cpubind(pl->topology, proc->pid, cpuset) == 0);
63+
bool ok = (hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
5964
Affinity* affinity = NULL;
6065
if (ok) {
6166
affinity = Affinity_new(pl);
@@ -76,15 +81,15 @@ Affinity* Affinity_get(Process* proc, ProcessList* pl) {
7681

7782
bool Affinity_set(Process* proc, Affinity* this) {
7883
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
79-
for (int i = 0; i < affinity->used; i++) {
80-
hwloc_bitmap_set(cpuset, affinity->cpus[i]);
84+
for (int i = 0; i < this->used; i++) {
85+
hwloc_bitmap_set(cpuset, this->cpus[i]);
8186
}
82-
bool ok = (hwloc_linux_set_tid_cpubind(this->pl->topology, proc->pid, cpuset) == 0);
87+
bool ok = (hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
8388
hwloc_bitmap_free(cpuset);
8489
return ok;
8590
}
8691

87-
#elif HAVE_NATIVE_AFFINITY
92+
#elif HAVE_LINUX_AFFINITY
8893

8994
Affinity* Affinity_get(Process* proc, ProcessList* pl) {
9095
cpu_set_t cpuset;

Affinity.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ in the source distribution for its full text.
1010
*/
1111

1212
#ifdef HAVE_LIBHWLOC
13-
#elif HAVE_NATIVE_AFFINITY
13+
#if __linux__
14+
#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_THREAD
15+
#else
16+
#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_PROCESS
17+
#endif
18+
#elif HAVE_LINUX_AFFINITY
1419
#endif
1520

1621
#include "Process.h"
@@ -36,7 +41,7 @@ Affinity* Affinity_get(Process* proc, ProcessList* pl);
3641

3742
bool Affinity_set(Process* proc, Affinity* this);
3843

39-
#elif HAVE_NATIVE_AFFINITY
44+
#elif HAVE_LINUX_AFFINITY
4045

4146
Affinity* Affinity_get(Process* proc, ProcessList* pl);
4247

ProcessList.c

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ in the source distribution for its full text.
2222
#include "Process.h"
2323
#include "Settings.h"
2424
25+
#ifdef HAVE_LIBHWLOC
26+
#include <hwloc.h>
27+
#endif
28+
2529
#ifndef MAX_NAME
2630
#define MAX_NAME 128
2731
#endif

ProcessList.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ in the source distribution for its full text.
1616
#include "Process.h"
1717
#include "Settings.h"
1818

19+
#ifdef HAVE_LIBHWLOC
20+
#include <hwloc.h>
21+
#endif
22+
1923
#ifndef MAX_NAME
2024
#define MAX_NAME 128
2125
#endif

configure.ac

+6-6
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ if test "$my_htop_platform" = "openbsd"; then
211211
AC_CHECK_LIB([kvm], [kvm_open], [], [missing_libraries="$missing_libraries libkvm"])
212212
fi
213213

214-
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")
215-
if test "x$enable_native_affinity" = xyes -a "x$cross_compiling" = xno; then
214+
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")
215+
if test "x$enable_linux_affinity" = xyes -a "x$cross_compiling" = xno; then
216216
AC_MSG_CHECKING([for usable sched_setaffinity])
217217
AC_RUN_IFELSE([
218218
AC_LANG_PROGRAM([[
@@ -225,17 +225,17 @@ if test "x$enable_native_affinity" = xyes -a "x$cross_compiling" = xno; then
225225
if (errno == ENOSYS) return 1;
226226
]])],
227227
[AC_MSG_RESULT([yes])],
228-
[enable_native_affinity=no
228+
[enable_linux_affinity=no
229229
AC_MSG_RESULT([no])])
230230
fi
231-
if test "x$enable_native_affinity" = xyes; then
232-
AC_DEFINE(HAVE_NATIVE_AFFINITY, 1, [Define if native sched_setaffinity and sched_getaffinity are to be used.])
231+
if test "x$enable_linux_affinity" = xyes; then
232+
AC_DEFINE(HAVE_LINUX_AFFINITY, 1, [Define if Linux sched_setaffinity and sched_getaffinity are to be used.])
233233
fi
234234

235235
AC_ARG_ENABLE(hwloc, [AC_HELP_STRING([--enable-hwloc], [enable hwloc support for CPU affinity])],, enable_hwloc="no")
236236
if test "x$enable_hwloc" = xyes
237237
then
238-
AC_CHECK_LIB([hwloc], [hwloc_linux_get_tid_cpubind], [], [missing_libraries="$missing_libraries libhwloc"])
238+
AC_CHECK_LIB([hwloc], [hwloc_get_proc_cpubind], [], [missing_libraries="$missing_libraries libhwloc"])
239239
AC_CHECK_HEADERS([hwloc.h],[:], [missing_headers="$missing_headers $ac_header"])
240240
fi
241241

0 commit comments

Comments
 (0)