Skip to content

Instantly share code, notes, and snippets.

@akemin-dayo
Last active October 1, 2023 02:08
Show Gist options
  • Save akemin-dayo/180b0189549ffc75ef36458f6da6d91d to your computer and use it in GitHub Desktop.
Save akemin-dayo/180b0189549ffc75ef36458f6da6d91d to your computer and use it in GitHub Desktop.
A WIP patch that declares QEMU thread(s) as high-performance to the macOS QoS scheduler. Apply this patch using `git am 0001-util-qemu-thread-posix.c-Declare-QEMU-thread-s-as-hi.patch`.

This is a WIP patch that declares QEMU thread(s) as high-performance to the macOS QoS scheduler.

Apply this patch using git am 0001-util-qemu-thread-posix.c-Declare-QEMU-thread-s-as-hi.patch.

This is most useful on Apple Silicon (arm64) machines, as this pins QEMU to the faster performance cores instead of the slower efficiency cores on those systems (which use a heterogeneous core architecture).

This may possibly also result in better performance on x86_64 macOS machines, as well.

TODO: Determine if there is a better place to put this function call, as this would effectively declare every QEMU thread as high-performance, which may not be desired behaviour if QEMU spawns threads for purposes other than guest OS emulation (which I am not certain about).

TODO 2: Determine if applying this patch actually results in a measurable performance impact (as the default behaviour of the macOS scheduler may already be sufficient).

TODO 3: Write better documentation.

From 68a5829bbaadb678effc0fd8e2b2866ca2c6e960 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karen/=E3=81=82=E3=81=91=E3=81=BF?= <karen@akemi.ai>
Date: Mon, 4 Oct 2021 12:40:42 +0900
Subject: [PATCH] util/qemu-thread-posix.c: Declare QEMU thread(s) as
high-performance to the macOS QoS scheduler. [WIP]
---
util/qemu-thread-posix.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 6c5004220d..b725ce1d89 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -594,6 +594,31 @@ void qemu_thread_create(QemuThread *thread, const char *name,
qemu_thread_args->start_routine = start_routine;
qemu_thread_args->arg = arg;
+#ifdef CONFIG_DARWIN
+ /*
+ * Declare QEMU thread(s) as high-performance to the macOS QoS scheduler.
+ *
+ * This is most useful on Apple Silicon (arm64) machines, as this pins
+ * QEMU to the faster performance cores instead of the slower efficiency
+ * cores on those systems (which use a heterogeneous core architecture).
+ *
+ * This may possibly also result in better performance on x86_64 macOS
+ * machines, as well.
+ *
+ * TODO: Determine if there is a better place to put this function call,
+ * as this would effectively declare every QEMU thread as high-performance,
+ * which may not be desired behaviour if QEMU spawns threads for purposes
+ * other than guest OS emulation (which I am not certain about).
+ *
+ * TODO 2: Determine if applying this patch actually results in a
+ * measurable performance impact (as the default behaviour of the macOS
+ * scheduler may already be sufficient).
+ *
+ * TODO 3: Write better documentation.
+ */
+ pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
+#endif
+
err = pthread_create(&thread->thread, &attr,
qemu_thread_start, qemu_thread_args);
--
2.25.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment