From dc3030cc80b570e2db67d9f07b6aa325b6c8e14e Mon Sep 17 00:00:00 2001 From: Andrea Bolognani Date: Fri, 23 Feb 2024 01:37:22 +0100 Subject: [PATCH] util: Prefer cpuset_{get,set}affinity() on BSD FreeBSD 14 implements sched_{get,set}affinity() for compatibility with Linux, but we should still use the native syscalls instead. Signed-off-by: Andrea Bolognani Reviewed-by: Martin Kletzander --- src/util/virprocess.c | 110 +++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/src/util/virprocess.c b/src/util/virprocess.c index 8384bd1378..426d56c1ce 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -444,7 +444,61 @@ int virProcessKillPainfully(pid_t pid, bool force) return virProcessKillPainfullyDelay(pid, force, 0, false); } -#if WITH_DECL_CPU_SET_T +#if defined(WITH_BSD_CPU_AFFINITY) + +int virProcessSetAffinity(pid_t pid, + virBitmap *map, + bool quiet) +{ + size_t i; + cpuset_t mask; + + CPU_ZERO(&mask); + for (i = 0; i < virBitmapSize(map); i++) { + if (virBitmapIsBitSet(map, i)) + CPU_SET(i, &mask); + } + + if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID, pid, + sizeof(mask), &mask) != 0) { + if (quiet) { + VIR_DEBUG("cannot set CPU affinity on process %d: %s", + pid, g_strerror(errno)); + } else { + virReportSystemError(errno, + _("cannot set CPU affinity on process %1$d"), pid); + return -1; + } + } + + return 0; +} + +virBitmap * +virProcessGetAffinity(pid_t pid) +{ + size_t i; + cpuset_t mask; + virBitmap *ret = NULL; + + CPU_ZERO(&mask); + if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID, pid, + sizeof(mask), &mask) != 0) { + virReportSystemError(errno, + _("cannot get CPU affinity of process %1$d"), pid); + return NULL; + } + + ret = virBitmapNew(sizeof(mask) * 8); + + for (i = 0; i < sizeof(mask) * 8; i++) + if (CPU_ISSET(i, &mask)) + ignore_value(virBitmapSetBit(ret, i)); + + return ret; +} + +#elif WITH_DECL_CPU_SET_T int virProcessSetAffinity(pid_t pid, virBitmap *map, bool quiet) { @@ -536,60 +590,6 @@ virProcessGetAffinity(pid_t pid) return ret; } -#elif defined(WITH_BSD_CPU_AFFINITY) - -int virProcessSetAffinity(pid_t pid, - virBitmap *map, - bool quiet) -{ - size_t i; - cpuset_t mask; - - CPU_ZERO(&mask); - for (i = 0; i < virBitmapSize(map); i++) { - if (virBitmapIsBitSet(map, i)) - CPU_SET(i, &mask); - } - - if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID, pid, - sizeof(mask), &mask) != 0) { - if (quiet) { - VIR_DEBUG("cannot set CPU affinity on process %d: %s", - pid, g_strerror(errno)); - } else { - virReportSystemError(errno, - _("cannot set CPU affinity on process %1$d"), pid); - return -1; - } - } - - return 0; -} - -virBitmap * -virProcessGetAffinity(pid_t pid) -{ - size_t i; - cpuset_t mask; - virBitmap *ret = NULL; - - CPU_ZERO(&mask); - if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID, pid, - sizeof(mask), &mask) != 0) { - virReportSystemError(errno, - _("cannot get CPU affinity of process %1$d"), pid); - return NULL; - } - - ret = virBitmapNew(sizeof(mask) * 8); - - for (i = 0; i < sizeof(mask) * 8; i++) - if (CPU_ISSET(i, &mask)) - ignore_value(virBitmapSetBit(ret, i)); - - return ret; -} - #else /* WITH_DECL_CPU_SET_T */ int virProcessSetAffinity(pid_t pid G_GNUC_UNUSED,