Skip to contentSkip to conversation

Release SMP restriction on Windows host

PATCH In review Fabien Chouteau 4 participants 8 messages Apr 9, 2013 – Apr 12, 2013

lore
Share & tools
lore
#0 lore raw

cover Release SMP restriction on Windows host

As I already explained to this mailling list, we (AdaCore) use QEMU to run a lot of short executable (~2 secs) on many different targets. In that configuration the SMP restriction on Windows (QEMU runs only on 1 core) was not efficient on our 16 cores servers.

Few months ago I asked on this mailing list if the restriction could be removed. There was no clear answer because nobody was able to tell if it would work. We decided to give it a try.

During the last months we only found one problem, it was a dead lock between the TCG and IO thread, this is fixed by the second patch.

We also found that suspendThread() and resumeThread() were not synchronous on SMP systems, so we added a busy loop to wait for effective suspension.

Since then, we run thousands of QEMU on our SMP Windows servers each day without any problem. And of course we greatly improve the execution time of our test-suites.

Fabien Chouteau (1):
Release SMP restriction on Windows

Olivier Hainque (2):
Check effective suspension of TCG thread Ensure good ordering of memory instruction in cpu_exec

 cpu-exec.c |    8 ++++++++
 cpus.c     |   24 ++++++++++++++++++++++--
 os-win32.c |   18 ------------------
 3 files changed, 30 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/3 Check effective suspension of TCG thread

From: Olivier Hainque <[email protected]>

On multi-core systems, SuspendThread does not guaranty immediate thread suspension. We add busy loop to wait for effective thread suspension after call to ThreadSuspend().

---
 cpus.c |   24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
cpus.c +22 -2
index e919dd7..97e9ab4 100644
@@ -862,9 +862,29 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
 #else /* _WIN32 */
     if (!qemu_cpu_is_self(cpu)) {
-        SuspendThread(cpu->hThread);
+        CONTEXT tcgContext;
+
+        if (SuspendThread(cpu->hThread) == (DWORD)-1) {
+            fprintf(stderr, "qemu:%s: GetLastError:%d\n", __func__,
+                    GetLastError());
+            exit(1);
+        }
+
+        /* On multi-core systems, we are not sure that the thread is actually
+         * suspended until we can get the context.
+         */
+        tcgContext.ContextFlags = CONTEXT_CONTROL;
+        while (GetThreadContext(cpu->hThread, &tcgContext) != 0) {
+            continue;
+        }
+
         cpu_signal(0);
-        ResumeThread(cpu->hThread);
+
+        if (ResumeThread(cpu->hThread) == (DWORD)-1) {
+            fprintf(stderr, "qemu:%s: GetLastError:%d\n", __func__,
+                    GetLastError());
+            exit(1);
+        }
     }
 #endif
 }
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/3 Ensure good ordering of memory instruction in cpu_exec

In reply to Fabien Chouteau

From: Olivier Hainque <[email protected]>

The IO thread, when it senses cpu_single_env == 0, expects exit_request to be checked later on. A compiler scheduling constraint is not strong enough to ensure this on modern architecture. A memory fence is needed as well.

---
 cpu-exec.c |    8 ++++++++
 1 file changed, 8 insertions(+)
cpu-exec.c +8 -0
index e74e556..aa8fa89 100644
@@ -217,6 +217,14 @@ int cpu_exec(CPUArchState *env)
 
     cpu_single_env = env;
 
+    /* As long as cpu_single_env is null, up to the assignment just above,
+     * requests by other threads to exit the execution loop are expected to
+     * be issued using the exit_request global. We must make sure that our
+     * evaluation of the global value is performed past the cpu_single_env
+     * value transition point, which requires a memory barrier as well as
+     * an instruction scheduling constraint on modern architectures.  */
+    smp_mb();
+
     if (unlikely(exit_request)) {
         cpu->exit_request = 1;
     }
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH 3/3 Release SMP restriction on Windows

In reply to Fabien Chouteau

The previous patches make QEMU SMP safe on Windows, we can now release the restriction.

---
 os-win32.c |   18 ------------------
 1 file changed, 18 deletions(-)
os-win32.c +0 -18
index 9673a81..c7f6b5c 100644
@@ -69,25 +69,7 @@ static BOOL WINAPI qemu_ctrl_handler(DWORD type)
 
 void os_setup_early_signal_handling(void)
 {
-    /* Note: cpu_interrupt() is currently not SMP safe, so we force
-       QEMU to run on a single CPU */
-    HANDLE h;
-    DWORD_PTR mask, smask;
-    int i;
-
     SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
-
-    h = GetCurrentProcess();
-    if (GetProcessAffinityMask(h, &mask, &smask)) {
-        for(i = 0; i < 32; i++) {
-            if (mask & (1 << i))
-                break;
-        }
-        if (i != 32) {
-            mask = 1 << i;
-            SetProcessAffinityMask(h, mask);
-        }
-    }
 }
 
 /* Look for support files in the same directory as the executable.  */
Signature · 1 line
1.7.9.5
Reviewed-by on the v1 series
#4 lore raw
In reply to Fabien Chouteau

Il 09/04/2013 18:06, Fabien Chouteau ha scritto:

Quoted 12 lines

As I already explained to this mailling list, we (AdaCore) use QEMU to run a lot of short executable (~2 secs) on many different targets. In that configuration the SMP restriction on Windows (QEMU runs only on 1 core) was not efficient on our 16 cores servers.

Few months ago I asked on this mailing list if the restriction could be removed. There was no clear answer because nobody was able to tell if it would work. We decided to give it a try.

During the last months we only found one problem, it was a dead lock between the TCG and IO thread, this is fixed by the second patch.

Quoted 6 lines

We also found that suspendThread() and resumeThread() were not synchronous on SMP systems, so we added a busy loop to wait for effective suspension.

Since then, we run thousands of QEMU on our SMP Windows servers each day without any problem. And of course we greatly improve the execution time of our test-suites.

Awesome! Thanks guys.

Paolo

#5 lore raw
Quoted 1 line · Paolo Bonzini

Do we need an additional Signed-off from the author (Olivier Hainque)? Currently, only Fabien signed these patches.

I'm preparing a pull request and was not sure about this point.

Regards,

Stefan W.

#6 lore raw

Il 11/04/2013 20:57, Stefan Weil ha scritto:

Quoted 5 lines · Paolo Bonzini
Quoted 1 line

Do we need an additional Signed-off from the author (Olivier Hainque)? Currently, only Fabien signed these patches.

In theory yes, in practice they are colleagues so I think it's fine.

Paolo

#7 lore raw
Quoted 4 lines · Paolo Bonzini
Quoted 2 lines

Do we need an additional Signed-off from the author (Olivier Hainque)? Currently, only Fabien signed these patches.

In theory yes, in practice they are colleagues so I think it's fine.

We are colleagues indeed and there's no problem having the patches signed by Fabien only.

[Qemu-devel] [PATCH 1/3] Check effective suspension of TCG thread

Was essentially written by Fabien anyway :)

If you'd rather have a distinct signoff for

[Qemu-devel] [PATCH 2/3] Ensure good ordering of memory instruction cpu_exec

... fine with me of course:

Thanks!

Olivier