KVM: selftests: Pre-set threads affinity in hardware disable test when possible

When building against a libc that provides pthread_attr_setaffinity_np(),
pre-set the grandchildren threads CPU affinity in the hardware disable test
to further reduce the perceived latency of pthread_create().  On large NUMA
systems, this reduces the average runtime from ~10s to ~5s.

Link: https://patch.msgid.link/20260731195612.2697986-4-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson 2026-07-31 12:56:03 -07:00
parent 2887da61c4
commit 496779b549

View File

@ -36,7 +36,9 @@ static void *run_vcpu(void *arg)
struct kvm_vcpu *vcpu = arg;
struct kvm_run *run = vcpu->run;
#ifndef _GNU_SOURCE
kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
#endif
vcpu_run(vcpu);
@ -50,7 +52,9 @@ static void *sleeping_thread(void *arg)
{
int fd;
#ifndef _GNU_SOURCE
kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
#endif
while (true) {
fd = open("/dev/null", O_RDWR);
@ -80,22 +84,28 @@ static inline void check_join(pthread_t thread, void **retval)
static void run_test(u32 run)
{
struct kvm_vcpu *vcpu;
pthread_attr_t attr;
struct kvm_vm *vm;
pthread_t threads[VCPU_NUM];
pthread_t throw_away;
void *b;
u32 i, j;
TEST_ASSERT_EQ(pthread_attr_init(&attr), 0);
#ifdef _GNU_SOURCE
TEST_ASSERT_EQ(pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &threads_cpu_set), 0);
#endif
vm = vm_create(VCPU_NUM);
pr_debug("%s: [%d] start vcpus\n", __func__, run);
for (i = 0; i < VCPU_NUM; ++i) {
vcpu = vm_vcpu_add(vm, i, guest_code);
check_create_thread(&threads[i], NULL, run_vcpu, vcpu);
check_create_thread(&threads[i], &attr, run_vcpu, vcpu);
for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
check_create_thread(&throw_away, NULL, sleeping_thread,
check_create_thread(&throw_away, &attr, sleeping_thread,
(void *)NULL);
}
}