Skip to contentSkip to conversation

posix-aio-compat: fix latency issues

PATCH Applied Avi Kivity 5 participants 9 messages Aug 14, 2011 – Aug 28, 2011 KVM

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH v2 1/1 posix-aio-compat: fix latency issues

In certain circumstances, posix-aio-compat can incur a lot of latency:
- threads are created by vcpu threads, so if vcpu affinity is set, aio threads inherit vcpu affinity. This can cause many aio threads to compete for one cpu.
- we can create up to max_threads (64) aio threads in one go; since a pthread_create can take around 30μs, we have up to 2ms of cpu time under a global lock.

Fix by:
- moving thread creation to the main thread, so we inherit the main thread's affinity instead of the vcpu thread's affinity.
- if a thread is currently being created, and we need to create yet another thread, let thread being born create the new thread, reducing the amount of time we spend under the main thread.
- drop the local lock while creating a thread (we may still hold the global mutex, though)

Note this doesn't eliminate latency completely; scheduler artifacts or lack of host cpu resources can still cause it. We may want pre-allocated threads when this cannot be tolerated.

Thanks to Uli Obergfell of Red Hat for his excellent analysis and suggestions.

---
v2: simplify do_spawn_thread() locking

 posix-aio-compat.c |   44 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 42 insertions(+), 2 deletions(-)
posix-aio-compat.c +42 -2
index 8dc00cb..c3febfb 100644
@@ -30,6 +30,7 @@
 
 #include "block/raw-posix-aio.h"
 
+static void do_spawn_thread(void);
 
 struct qemu_paiocb {
     BlockDriverAIOCB common;
@@ -64,6 +65,9 @@ static pthread_attr_t attr;
 static int max_threads = 64;
 static int cur_threads = 0;
 static int idle_threads = 0;
+static int new_threads = 0;     /* backlog of threads we need to create */
+static int pending_threads = 0; /* threads created but not running yet */
+static QEMUBH *new_thread_bh;
 static QTAILQ_HEAD(, qemu_paiocb) request_list;
 
 #ifdef CONFIG_PREADV
@@ -311,6 +315,11 @@ static void *aio_thread(void *unused)
 
     pid = getpid();
 
+    mutex_lock(&lock);
+    pending_threads--;
+    mutex_unlock(&lock);
+    do_spawn_thread();
+
     while (1) {
         struct qemu_paiocb *aiocb;
         ssize_t ret = 0;
@@ -381,11 +390,20 @@ static void *aio_thread(void *unused)
     return NULL;
 }
 
-static void spawn_thread(void)
+static void do_spawn_thread(void)
 {
     sigset_t set, oldset;
 
-    cur_threads++;
+    mutex_lock(&lock);
+    if (!new_threads) {
+        mutex_unlock(&lock);
+        return;
+    }
+
+    new_threads--;
+    pending_threads++;
+
+    mutex_unlock(&lock);
 
     /* block all signals */
     if (sigfillset(&set)) die("sigfillset");
@@ -396,6 +414,27 @@ static void spawn_thread(void)
     if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
 }
 
+static void spawn_thread_bh_fn(void *opaque)
+{
+    do_spawn_thread();
+}
+
+static void spawn_thread(void)
+{
+    cur_threads++;
+    new_threads++;
+    /* If there are threads being created, they will spawn new workers, so
+     * we don't spend time creating many threads in a loop holding a mutex or
+     * starving the current vcpu.
+     *
+     * If there are no idle threads, ask the main thread to create one, so we
+     * inherit the correct affinity instead of the vcpu affinity.
+     */
+    if (!pending_threads) {
+        qemu_bh_schedule(new_thread_bh);
+    }
+}
+
 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
 {
     aiocb->ret = -EINPROGRESS;
@@ -665,6 +704,7 @@ int paio_init(void)
         die2(ret, "pthread_attr_setdetachstate");
 
     QTAILQ_INIT(&request_list);
+    new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL);
 
     posix_aio_state = s;
     return 0;
Signature · 1 line
1.7.5.3
Applied
#1 lore raw

Am 14.08.2011 06:04, schrieb Avi Kivity:

Quoted 21 lines

In certain circumstances, posix-aio-compat can incur a lot of latency:
- threads are created by vcpu threads, so if vcpu affinity is set, aio threads inherit vcpu affinity. This can cause many aio threads to compete for one cpu.
- we can create up to max_threads (64) aio threads in one go; since a pthread_create can take around 30μs, we have up to 2ms of cpu time under a global lock.

Fix by:
- moving thread creation to the main thread, so we inherit the main thread's affinity instead of the vcpu thread's affinity.
- if a thread is currently being created, and we need to create yet another thread, let thread being born create the new thread, reducing the amount of time we spend under the main thread.
- drop the local lock while creating a thread (we may still hold the global mutex, though)

Note this doesn't eliminate latency completely; scheduler artifacts or lack of host cpu resources can still cause it. We may want pre-allocated threads when this cannot be tolerated.

Quoted 5 lines

Thanks to Uli Obergfell of Red Hat for his excellent analysis and suggestions.

---
v2: simplify do_spawn_thread() locking

Thanks, applied to the block branch.

Kevin

#2 lore raw
In reply to Avi Kivity
Quoted 17 lines · Avi Kivity

In certain circumstances, posix-aio-compat can incur a lot of latency:
- threads are created by vcpu threads, so if vcpu affinity is set, aio threads inherit vcpu affinity. This can cause many aio threads to compete for one cpu.
- we can create up to max_threads (64) aio threads in one go; since a pthread_create can take around 30μs, we have up to 2ms of cpu time under a global lock.

Fix by:
- moving thread creation to the main thread, so we inherit the main thread's affinity instead of the vcpu thread's affinity.
- if a thread is currently being created, and we need to create yet another thread, let thread being born create the new thread, reducing the amount of time we spend under the main thread.
- drop the local lock while creating a thread (we may still hold the global mutex, though)

Quoted 5 lines

Note this doesn't eliminate latency completely; scheduler artifacts or lack of host cpu resources can still cause it. We may want pre-allocated threads when this cannot be tolerated.

Thanks to Uli Obergfell of Red Hat for his excellent analysis and suggestions.

At this chance: What is the state of getting rid of the remaining delta between upstream's version and qemu-kvm?

Jan

Signature · 17 lines
Siemens AG
Corporate Technology
CT T DE IT 1
Corporate Competence Center Embedded Linux
Otto-Hahn-Ring 6
81739 Muenchen
Tel.: +49 (89) 636-40042
Fax: +49 (89) 636-45450
mailto:[email protected]

Siemens Aktiengesellschaft: Chairman of the Supervisory Board: Gerhard
Cromme; Managing Board: Peter Loescher, Chairman, President and Chief
Executive Officer; Roland Busch, Brigitte Ederer, Klaus Helmrich, Joe
Kaeser, Barbara Kux, Hermann Requardt, Siegfried Russwurm, Peter Y.
Solmssen, Michael Suess; Registered offices: Berlin and Munich, Germany;
Commercial registries: Berlin  Charlottenburg, HRB 12300, Munich, HRB
6684; WEEE-Reg.-No. DE 23691322
#3 lore raw
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Avi Kivity

In certain circumstances, posix-aio-compat can incur a lot of latency:
 - threads are created by vcpu threads, so if vcpu affinity is set, aio threads inherit vcpu affinity.  This can cause many aio threads to compete for one cpu.
 - we can create up to max_threads (64) aio threads in one go; since a pthread_create can take around 30μs, we have up to 2ms of cpu time under a global lock.

Fix by:
 - moving thread creation to the main thread, so we inherit the main thread's affinity instead of the vcpu thread's affinity.
 - if a thread is currently being created, and we need to create yet another thread, let thread being born create the new thread, reducing the amount of time we spend under the main thread.
 - drop the local lock while creating a thread (we may still hold the global mutex, though)

Quoted 8 lines
Quoted 5 lines

Note this doesn't eliminate latency completely; scheduler artifacts or lack of host cpu resources can still cause it.  We may want pre-allocated threads when this cannot be tolerated.

Thanks to Uli Obergfell of Red Hat for his excellent analysis and suggestions.

At this chance: What is the state of getting rid of the remaining delta between upstream's version and qemu-kvm?

That would be nice. qemu-kvm.git uses a signalfd to handle I/O completion whereas qemu.git uses a signal, writes to a pipe from the signal handler, and uses qemu_notify_event() to break the vcpu. Once the force iothread patch is merged we should be able to move to qemu-kvm.git's signalfd approach.

Stefan

#4 lore raw
Quoted 25 lines · Stefan Hajnoczi
Quoted 24 lines · Jan Kiszka
Quoted 22 lines · Avi Kivity

In certain circumstances, posix-aio-compat can incur a lot of latency:
- threads are created by vcpu threads, so if vcpu affinity is set, aio threads inherit vcpu affinity. This can cause many aio threads to compete for one cpu.
- we can create up to max_threads (64) aio threads in one go; since a pthread_create can take around 30μs, we have up to 2ms of cpu time under a global lock.

Fix by:
- moving thread creation to the main thread, so we inherit the main thread's affinity instead of the vcpu thread's affinity.
- if a thread is currently being created, and we need to create yet another thread, let thread being born create the new thread, reducing the amount of time we spend under the main thread.
- drop the local lock while creating a thread (we may still hold the global mutex, though)

Note this doesn't eliminate latency completely; scheduler artifacts or lack of host cpu resources can still cause it. We may want pre-allocated threads when this cannot be tolerated.

Thanks to Uli Obergfell of Red Hat for his excellent analysis and suggestions.

Quoted 8 lines
Quoted 2 lines

At this chance: What is the state of getting rid of the remaining delta between upstream's version and qemu-kvm?

That would be nice. qemu-kvm.git uses a signalfd to handle I/O completion whereas qemu.git uses a signal, writes to a pipe from the signal handler, and uses qemu_notify_event() to break the vcpu. Once the force iothread patch is merged we should be able to move to qemu-kvm.git's signalfd approach.

No need to use a signal at all actually. The use of a signal is historic and was required to work around the TCG race that I referred to in another thread.

You should be able to just use an eventfd or pipe.

Better yet, we should look at using GThreadPool to replace posix-aio-compat.

Regards,

Anthony Liguori

Quoted 3 lines

Stefan

#5 lore raw
Quoted 35 lines · Anthony Liguori
Quoted 25 lines · Stefan Hajnoczi
Quoted 24 lines · Jan Kiszka
Quoted 22 lines · Avi Kivity

In certain circumstances, posix-aio-compat can incur a lot of latency:
- threads are created by vcpu threads, so if vcpu affinity is set, aio threads inherit vcpu affinity. This can cause many aio threads to compete for one cpu.
- we can create up to max_threads (64) aio threads in one go; since a pthread_create can take around 30μs, we have up to 2ms of cpu time under a global lock.

Fix by:
- moving thread creation to the main thread, so we inherit the main thread's affinity instead of the vcpu thread's affinity.
- if a thread is currently being created, and we need to create yet another thread, let thread being born create the new thread, reducing the amount of time we spend under the main thread.
- drop the local lock while creating a thread (we may still hold the global mutex, though)

Note this doesn't eliminate latency completely; scheduler artifacts or lack of host cpu resources can still cause it. We may want pre-allocated threads when this cannot be tolerated.

Thanks to Uli Obergfell of Red Hat for his excellent analysis and suggestions.

Quoted 8 lines
Quoted 2 lines

At this chance: What is the state of getting rid of the remaining delta between upstream's version and qemu-kvm?

That would be nice. qemu-kvm.git uses a signalfd to handle I/O completion whereas qemu.git uses a signal, writes to a pipe from the signal handler, and uses qemu_notify_event() to break the vcpu. Once the force iothread patch is merged we should be able to move to qemu-kvm.git's signalfd approach.

Quoted 7 lines

No need to use a signal at all actually. The use of a signal is historic and was required to work around the TCG race that I referred to in another thread.

You should be able to just use an eventfd or pipe.

Better yet, we should look at using GThreadPool to replace posix-aio-compat.

When interacting with the thread pool is part of some time-critical path (easily possible with a real-time Linux guest), general-purpose implementations like what glib offers are typically out of the game. They do not provide sufficient customizability, specifically control over their internal synchronization and allocation policies. That applies to the other rather primitive glib threading and locking services as well.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#6 lore raw
Quoted 5 lines · Jan Kiszka
Quoted 3 lines · Anthony Liguori

You should be able to just use an eventfd or pipe.

Better yet, we should look at using GThreadPool to replace posix-aio-compat.

Quoted 7 lines

When interacting with the thread pool is part of some time-critical path (easily possible with a real-time Linux guest), general-purpose implementations like what glib offers are typically out of the game. They do not provide sufficient customizability, specifically control over their internal synchronization and allocation policies. That applies to the other rather primitive glib threading and locking services as well.

We can certainly enhance glib. glib is a cross platform library. I don't see a compelling reason to invent a new cross platform library just for QEMU especially if the justification is future features, not current features.

Regards,

Anthony Liguori

Quoted 3 lines

Jan

#7 lore raw
Quoted 14 lines · Anthony Liguori
Quoted 5 lines · Jan Kiszka
Quoted 3 lines · Anthony Liguori

You should be able to just use an eventfd or pipe.

Better yet, we should look at using GThreadPool to replace posix-aio-compat.

Quoted 7 lines

When interacting with the thread pool is part of some time-critical path (easily possible with a real-time Linux guest), general-purpose implementations like what glib offers are typically out of the game. They do not provide sufficient customizability, specifically control over their internal synchronization and allocation policies. That applies to the other rather primitive glib threading and locking services as well.

Quoted 1 line

We can certainly enhance glib. glib is a cross platform library. I

Do you want to carry forked glib bits in QEMU?

Quoted 3 lines

don't see a compelling reason to invent a new cross platform library just for QEMU especially if the justification is future features, not current features.

Tweaking affinity of aio threads is already a current requirement.

And we already have a working threading and locking system. One that is growing beyond glib's level of support quickly (think of RCU).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#8 lore raw
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Anthony Liguori
Quoted 12 lines · Jan Kiszka
Quoted 3 lines · Anthony Liguori

You should be able to just use an eventfd or pipe.

Better yet, we should look at using GThreadPool to replace posix-aio-compat.

When interacting with the thread pool is part of some time-critical path (easily possible with a real-time Linux guest), general-purpose implementations like what glib offers are typically out of the game. They do not provide sufficient customizability, specifically control over their internal synchronization and allocation policies. That applies to the other rather primitive glib threading and locking services as well.

Quoted 3 lines
Quoted 1 line

We can certainly enhance glib. glib is a cross platform library. I

Do you want to carry forked glib bits in QEMU?

We can make real-time depend on a newer glib version.

Quoted 10 lines
Quoted 4 lines

don't see a compelling reason to invent a new cross platform library just for QEMU especially if the justification is future features, not current features.

Tweaking affinity of aio threads is already a current requirement.

And we already have a working threading and locking system. One that is growing beyond glib's level of support quickly (think of RCU).

glib will have to support RCU as well. But for this topic, I agree with you for now.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.