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 <abologna@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Andrea Bolognani 2024-02-23 01:37:22 +01:00
parent dc0dc848ee
commit dc3030cc80

View File

@ -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,