RE: [PATCH v6 5/5] KVM: selftests: Allowing running dirty_log_perf_test on specific CPUs

From: Wang, Wei W
Date: Tue Oct 25 2022 - 22:34:53 EST


On Saturday, October 22, 2022 5:18 AM, Vipin Sharma wrote:
> +static void pin_this_task_to_pcpu(uint32_t pcpu) {
> + cpu_set_t mask;
> + int r;
> +
> + CPU_ZERO(&mask);
> + CPU_SET(pcpu, &mask);
> + r = sched_setaffinity(0, sizeof(mask), &mask);
> + TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.\n", pcpu);
> +}
> +
> static void *vcpu_thread_main(void *data) {
> + struct perf_test_vcpu_args *vcpu_args;
> struct vcpu_thread *vcpu = data;
>
> + vcpu_args = &perf_test_args.vcpu_args[vcpu->vcpu_idx];
> +
> + if (perf_test_args.pin_vcpus)
> + pin_this_task_to_pcpu(vcpu_args->pcpu);
> +

I think it would be better to do the thread pinning at the time when the
thread is created by providing a pthread_attr_t attr, e.g. :

pthread_attr_t attr;

CPU_SET(vcpu->pcpu, &cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_set);
pthread_create(thread, attr,...);

Also, pinning a vCPU thread to a pCPU is a general operation
which other users would need. I think we could make it more general and
put it to kvm_util, e.g. adding it to the helper function that I'm trying to create

+ * Create a vcpu thread with user provided attribute and the name in
+ * "vcpu-##id" format.
+ */
+void __vcpu_thread_create(struct kvm_vcpu *vcpu, const pthread_attr_t *attr,
+ void *(*start_routine)(void *), uint32_t private_data_size)

(https://lore.kernel.org/kvm/20221024113445.1022147-1-wei.w.wang@xxxxxxxxx/T/#m0ceed820278a9deb199871ee6da7d6ec54d065f4)