Skip to content

Commit

Permalink
Update: Native qemu cpu thread pinning
Browse files Browse the repository at this point in the history
Updated cpus.c file with new qemu_thread_set_affinity (1)
Removed redundant definition from already exisitng patch No. 14 (2)

Signed-off-by: Roja Eswaran <roja@zededa.com>
  • Loading branch information
roja-zededa committed Oct 22, 2024
1 parent 46fb0f1 commit 227a75a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 85 deletions.
61 changes: 0 additions & 61 deletions pkg/xen-tools/patches-4.19.0/14-qemu-Init-CPU-mask-per-VCPU.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ From: Nikolay Martyanov <ohmspectator@gmail.com>
Date: Wed, 28 Sep 2022 15:52:24 +0200
Subject: [PATCH 14/15] qemu: Init CPU mask per VCPU.

Signed-off-by: Nikolay Martyanov <ohmspectator@gmail.com>
---
tools/qemu-xen/include/hw/boards.h | 1 +
tools/qemu-xen/include/hw/core/cpu.h | 2 +
tools/qemu-xen/softmmu/cpus.c | 31 ++++++++++++
tools/qemu-xen/softmmu/vl.c | 75 ++++++++++++++++++++++++++++
4 files changed, 109 insertions(+)

diff --git a/tools/qemu-xen/include/hw/boards.h b/tools/qemu-xen/include/hw/boards.h
index b06f13e..a4b4f11 100644
--- a/tools/qemu-xen/include/hw/boards.h
Expand Down Expand Up @@ -38,59 +30,6 @@ index 397fd3a..1bf357b 100644
/* Should CPU start in powered-off state? */
bool start_powered_off;

diff --git a/tools/qemu-xen/softmmu/cpus.c b/tools/qemu-xen/softmmu/cpus.c
index 9cbc817..2867325 100644
--- a/tools/qemu-xen/softmmu/cpus.c
+++ b/tools/qemu-xen/softmmu/cpus.c
@@ -72,6 +72,25 @@ static QemuMutex qemu_global_mutex;
*/
static const AccelOpsClass *cpus_accel;

+
+static inline void cpumask_clear_bit(uint64_t *mask, uint8_t bit)
+{
+ *mask &= ~(1ul << bit);
+}
+
+static inline long cpumask_get_min_bit(uint64_t mask)
+{
+ return __builtin_ffsll(mask) - 1;
+ }
+
+static long pick_pcpu(uint64_t *cpumask)
+{
+ long ret = cpumask_get_min_bit(*cpumask);
+ cpumask_clear_bit(cpumask, ret);
+ return ret;
+}
+
+
bool cpu_is_stopped(CPUState *cpu)
{
return cpu->stopped || !runstate_is_running();
@@ -635,6 +654,21 @@ void qemu_init_vcpu(CPUState *cpu)
cpu->stopped = true;
cpu->random_seed = qemu_guest_random_seed_thread_part1();

+ cpu->pinned = ms->cpu_pin;
+ static uint64_t vm_cpumask;
+ uint64_t vcpu_cpumask;
+ if (!vm_cpumask)
+ vm_cpumask = ms->cpumask;
+ if (!cpu->pinned) {
+ /* If the CPUs are not pinned, assign the whole CPU mask to the VCPU */
+ vcpu_cpumask = vm_cpumask;
+ } else {
+ /* If the CPUs are pinned, pick only one CPU for this VCPU */
+ vcpu_cpumask = 1ull << pick_pcpu(&vm_cpumask);
+ }
+
+ cpu->cpumask = vcpu_cpumask;
+
if (!cpu->as) {
/* If the target cpu hasn't set up any address spaces itself,
* give it the default one.


diff --git a/tools/qemu-xen/softmmu/vl.c b/tools/qemu-xen/softmmu/vl.c
index ea20b23..98287f4 100644
Expand Down
24 changes: 0 additions & 24 deletions pkg/xen-tools/patches-4.19.0/16-qemu-native-thread-affinity.patch

This file was deleted.

58 changes: 58 additions & 0 deletions pkg/xen-tools/patches-4.19.0/23-qemu-native-thread-affinity.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--- a/tools/qemu-xen/softmmu/cpus.c 2023-08-16 02:43:53
+++ b/tools/qemu-xen/softmmu/cpus.c 2024-10-22 11:51:09
@@ -619,6 +619,23 @@ const AccelOpsClass *cpus_get_accel(void)
cpus_accel = ops;
}

+static inline void cpumask_clear_bit(uint64_t *mask, uint8_t bit)
+{
+ *mask &= ~(1ul << bit);
+}
+
+static inline long cpumask_get_min_bit(uint64_t mask)
+{
+ return __builtin_ffsll(mask) - 1;
+}
+
+static long pick_pcpu(uint64_t *cpumask)
+{
+ long ret = cpumask_get_min_bit(*cpumask);
+ cpumask_clear_bit(cpumask, ret);
+ return ret;
+}
+
const AccelOpsClass *cpus_get_accel(void)
{
/* broken if we call this early */
@@ -629,12 +646,30 @@ void qemu_init_vcpu(CPUState *cpu)
void qemu_init_vcpu(CPUState *cpu)
{
MachineState *ms = MACHINE(qdev_get_machine());
-
+ unsigned long *bitmap = NULL;
cpu->nr_cores = ms->smp.cores;
cpu->nr_threads = ms->smp.threads;
cpu->stopped = true;
cpu->random_seed = qemu_guest_random_seed_thread_part1();
+ cpu->pinned = ms->cpu_pin;
+ static uint64_t vm_cpumask;
+ uint64_t vcpu_cpumask;
+ if (!vm_cpumask)
+ vm_cpumask = ms->cpumask;
+ if (!cpu->pinned) {
+ vcpu_cpumask = vm_cpumask;
+ } else {
+ vcpu_cpumask = 1ull << pick_pcpu(&vm_cpumask);
+ }
+ cpu->cpumask = vcpu_cpumask;

+ if(cpu_can_run(cpu))
+ warn_report("Change a CPU affinity after the CPU may have been running for a while\n");
+ if (cpu->cpumask){
+ bitmap = bitmap_new(cpu->cpumask);
+ qemu_thread_set_affinity(cpu->thread, bitmap, cpu->cpumask);
+ }
+
if (!cpu->as) {
/* If the target cpu hasn't set up any address spaces itself,
* give it the default one.

0 comments on commit 227a75a

Please sign in to comment.