Skip to contentSkip to conversation

posix-aio-compat: Fix idle_threads counter

PATCH In review Kevin Wolf 2 participants 4 messages May 3, 2011 – May 4, 2011

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 posix-aio-compat: Fix idle_threads counter

A thread should only be counted as idle when it really is waiting for new requests. Without this patch, sometimes too few threads are started as busy threads are counted as idle.

Not sure if it makes a difference in practice outside some artificial qemu-io/qemu-img tests, but I think the change makes sense in any case.

---
 posix-aio-compat.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
posix-aio-compat.c +2 -4
index 6d4df9d..f3cc868 100644
@@ -322,7 +322,9 @@ static void *aio_thread(void *unused)
 
         while (QTAILQ_EMPTY(&request_list) &&
                !(ret == ETIMEDOUT)) {
+            idle_threads++;
             ret = cond_timedwait(&cond, &lock, &ts);
+            idle_threads--;
         }
 
         if (QTAILQ_EMPTY(&request_list))
@@ -331,7 +333,6 @@ static void *aio_thread(void *unused)
         aiocb = QTAILQ_FIRST(&request_list);
         QTAILQ_REMOVE(&request_list, aiocb, node);
         aiocb->active = 1;
-        idle_threads--;
         mutex_unlock(&lock);
 
         switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
@@ -353,13 +354,11 @@ static void *aio_thread(void *unused)
 
         mutex_lock(&lock);
         aiocb->ret = ret;
-        idle_threads++;
         mutex_unlock(&lock);
 
         if (kill(pid, aiocb->ev_signo)) die("kill failed");
     }
 
-    idle_threads--;
     cur_threads--;
     mutex_unlock(&lock);
 
@@ -371,7 +370,6 @@ static void spawn_thread(void)
     sigset_t set, oldset;
 
     cur_threads++;
-    idle_threads++;
 
     /* block all signals */
     if (sigfillset(&set)) die("sigfillset");
Signature · 1 line
1.7.2.3
#1 lore raw
Quoted 11 lines · Kevin Wolf

A thread should only be counted as idle when it really is waiting for new requests. Without this patch, sometimes too few threads are started as busy threads are counted as idle.

Not sure if it makes a difference in practice outside some artificial qemu-io/qemu-img tests, but I think the change makes sense in any case.

---
 posix-aio-compat.c |    6 ++----

 1 files changed, 2 insertions(+), 4 deletions(-)

I think the critical change here is that idle_threads is not being incremented by spawn_thread(). This means that we will keep spawning threads as new requests come in and until the first thread goes idle.

Previously you could imagine a scenario where we spawn a thread but don't schedule it yet. Then we immediately submit more I/O and since idle_threads was incremented we don't spawn additional threads to handle the requests.

Are these the cases you were thinking about?

I like your patch because it reduces the number of places where we mess with idle_threads. I'd move the increment/decrement out of the loop since the mutex is held here anyway so no one will test idle_threads between loop iterations, but it's up to you if you want to send a v2 or not.

Stefan

author
#2 lore raw

Am 03.05.2011 17:56, schrieb Stefan Hajnoczi:

Quoted 17 lines · Kevin Wolf
Quoted 11 lines

A thread should only be counted as idle when it really is waiting for new requests. Without this patch, sometimes too few threads are started as busy threads are counted as idle.

Not sure if it makes a difference in practice outside some artificial qemu-io/qemu-img tests, but I think the change makes sense in any case.

---
 posix-aio-compat.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)

I think the critical change here is that idle_threads is not being incremented by spawn_thread(). This means that we will keep spawning threads as new requests come in and until the first thread goes idle.

Quoted 6 lines

Previously you could imagine a scenario where we spawn a thread but don't schedule it yet. Then we immediately submit more I/O and since idle_threads was incremented we don't spawn additional threads to handle the requests.

Are these the cases you were thinking about?

Yes, this is the case that I noticed.

However, I'm not sure if this is really the critical change. In this case, it would take a bit longer until you get your full 64 threads, but you should get there eventually and then it shouldn't impact performance any more.

However, what I saw in my test case (qemu-img always running 16 sequential read requests in parallel) was that I only got 16 threads. This sounds logical, but in fact you seem to need always one thread more for good performance (I don't fully understand this yet). And with this patch, you actually get 17 threads. The difference was like 8s vs. 22s for the same requests, and using more than 17 threads doesn't further improve it.

This is what makes me a bit nervous about it, I don't really understand what's going on here. The observation suggests that the race doesn't only affect thread creation, but also operations afterwards.

Quoted 5 lines

I like your patch because it reduces the number of places where we mess with idle_threads. I'd move the increment/decrement out of the loop since the mutex is held here anyway so no one will test idle_threads between loop iterations, but it's up to you if you want to send a v2 or not.

Probably doesn't really make a difference.

Kevin

#3 lore raw
Quoted 32 lines · Kevin Wolf

Am 03.05.2011 17:56, schrieb Stefan Hajnoczi:

Quoted 17 lines · Kevin Wolf
Quoted 11 lines

A thread should only be counted as idle when it really is waiting for new requests. Without this patch, sometimes too few threads are started as busy threads are counted as idle.

Not sure if it makes a difference in practice outside some artificial qemu-io/qemu-img tests, but I think the change makes sense in any case.

---
 posix-aio-compat.c |    6 ++----

 1 files changed, 2 insertions(+), 4 deletions(-)

I think the critical change here is that idle_threads is not being incremented by spawn_thread().  This means that we will keep spawning threads as new requests come in and until the first thread goes idle.

Quoted 6 lines

Previously you could imagine a scenario where we spawn a thread but don't schedule it yet.  Then we immediately submit more I/O and since idle_threads was incremented we don't spawn additional threads to handle the requests.

Are these the cases you were thinking about?

Yes, this is the case that I noticed.

However, I'm not sure if this is really the critical change. In this case, it would take a bit longer until you get your full 64 threads, but you should get there eventually and then it shouldn't impact performance any more.

Quoted 7 lines

However, what I saw in my test case (qemu-img always running 16 sequential read requests in parallel) was that I only got 16 threads. This sounds logical, but in fact you seem to need always one thread more for good performance (I don't fully understand this yet). And with this patch, you actually get 17 threads. The difference was like 8s vs. 22s for the same requests, and using more than 17 threads doesn't further improve it.

Wow, 8s vs 22s is a big difference. Did you run any guest benchmarks?

Stefan