From: Mark Kanda <mark.kanda@oracle.com>
To: qemu-devel@nongnu.org
Cc: david@redhat.com, pbonzini@redhat.com, mark.kanda@oracle.com
Subject: [PATCH v1 2/2] oslib-posix: initialize backend memory objects in parallel
Date: Mon, 8 Jan 2024 09:10:41 -0600 [thread overview]
Message-ID: <20240108151041.529716-3-mark.kanda@oracle.com> (raw)
In-Reply-To: <20240108151041.529716-1-mark.kanda@oracle.com>
QEMU initializes preallocated backend memory as the objects are parsed from
the command line. This is not optimal in some cases (e.g. memory spanning
multiple numa nodes) because the memory objects are initialized in series.
Allow the initialization to occur in parallel. The performance increase is
significant and scales with the number of objects. On a 2 socket Skylake VM
with 128GB and 2 init threads per socket (256GB total), the memory init time
decreases from ~27 seconds to ~14 seconds.
Signed-off-by: Mark Kanda <mark.kanda@oracle.com>
---
include/qemu/osdep.h | 6 ++++++
system/vl.c | 2 ++
util/oslib-posix.c | 46 +++++++++++++++++++++++++++++++++-----------
util/oslib-win32.c | 5 +++++
4 files changed, 48 insertions(+), 11 deletions(-)
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index d30ba73eda..57185e6309 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -682,6 +682,12 @@ typedef struct ThreadContext ThreadContext;
void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
ThreadContext *tc, Error **errp);
+/**
+ * Wait for any outstanding memory prealloc initialization
+ * to complete.
+ */
+void wait_mem_prealloc_init(void);
+
/**
* qemu_get_pid_name:
* @pid: pid of a process
diff --git a/system/vl.c b/system/vl.c
index 6b87bfa32c..9e04acbb2c 100644
--- a/system/vl.c
+++ b/system/vl.c
@@ -2010,6 +2010,8 @@ static void qemu_create_late_backends(void)
object_option_foreach_add(object_create_late);
+ wait_mem_prealloc_init();
+
if (tpm_init() < 0) {
exit(1);
}
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 293297ac6c..667d2d960c 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -91,6 +91,7 @@ static QemuMutex sigbus_mutex;
static QemuMutex page_mutex;
static QemuCond page_cond;
+static bool prealloc_init;
int qemu_get_thread_id(void)
{
@@ -487,6 +488,12 @@ static int wait_mem_prealloc(void)
{
int i, ret = 0;
MemsetContext *context, *next_context;
+
+ /* Return if memory prealloc isn't enabled or active */
+ if (QLIST_EMPTY(&memset_contexts) || !prealloc_init) {
+ return 0;
+ }
+
qemu_mutex_lock(&page_mutex);
QLIST_FOREACH(context, &memset_contexts, next) {
context->all_threads_created = true;
@@ -553,21 +560,23 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
}
qemu_mutex_lock(&sigbus_mutex);
- memset(&act, 0, sizeof(act));
+ if (!sigbus_oldact.sa_handler) {
+ memset(&act, 0, sizeof(act));
#ifdef CONFIG_LINUX
- act.sa_sigaction = &sigbus_handler;
- act.sa_flags = SA_SIGINFO;
+ act.sa_sigaction = &sigbus_handler;
+ act.sa_flags = SA_SIGINFO;
#else /* CONFIG_LINUX */
- act.sa_handler = &sigbus_handler;
- act.sa_flags = 0;
+ act.sa_handler = &sigbus_handler;
+ act.sa_flags = 0;
#endif /* CONFIG_LINUX */
- ret = sigaction(SIGBUS, &act, &sigbus_oldact);
- if (ret) {
- qemu_mutex_unlock(&sigbus_mutex);
- error_setg_errno(errp, errno,
- "qemu_prealloc_mem: failed to install signal handler");
- return;
+ ret = sigaction(SIGBUS, &act, &sigbus_oldact);
+ if (ret) {
+ qemu_mutex_unlock(&sigbus_mutex);
+ error_setg_errno(errp, errno,
+ "qemu_prealloc_mem: failed to install signal handler");
+ return;
+ }
}
}
@@ -589,6 +598,21 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
}
}
+void wait_mem_prealloc_init(void)
+{
+ /*
+ * Set prealloc_init true to make wait_mem_prealloc() wait for the
+ * initialization to complete.
+ */
+ prealloc_init = true;
+
+ /* Wait for any outstanding init to complete */
+ if (wait_mem_prealloc()) {
+ perror("wait_mem_prealloc_init: failed waiting for memory prealloc");
+ exit(1);
+ }
+}
+
char *qemu_get_pid_name(pid_t pid)
{
char *name = NULL;
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 55b0189dc3..72e050bee1 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -276,6 +276,11 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
}
}
+void wait_mem_prealloc_init(void)
+{
+ /* not supported */
+}
+
char *qemu_get_pid_name(pid_t pid)
{
/* XXX Implement me */
--
2.39.3
prev parent reply other threads:[~2024-01-08 15:11 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-08 15:10 [PATCH v1 0/2] Initialize backend memory objects in parallel Mark Kanda
2024-01-08 15:10 ` [PATCH v1 1/2] oslib-posix: refactor memory prealloc threads Mark Kanda
2024-01-08 15:40 ` David Hildenbrand
2024-01-08 18:40 ` Mark Kanda
2024-01-09 14:02 ` David Hildenbrand
2024-01-09 14:15 ` Daniel P. Berrangé
2024-01-09 14:25 ` David Hildenbrand
2024-01-09 18:42 ` Mark Kanda
2024-01-17 14:44 ` Mark Kanda
2024-01-17 14:51 ` David Hildenbrand
2024-01-08 15:10 ` Mark Kanda [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240108151041.529716-3-mark.kanda@oracle.com \
--to=mark.kanda@oracle.com \
--cc=david@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).