Skip to contentSkip to conversation

x86_64 mttcg

PATCH In review Emilio G. Cota 2 participants 8 messages Aug 14, 2018

lore
Share & tools
lore
#0 lore raw

cover x86_64 mttcg

With this series I can boot a busybox image and several Ubuntu images with various -smp's.

The speedup we get is in line with what we get with other ISAs, e.g. -smp 4 takes 34s instead of 1min with thread=single.

I've run this through the Valgrind race detectors, and the remaining races reported seem benign.

I have not tested i386, so I'm not turning mttcg on for it yet.

Patches 1-2 comply with cpu_get_ticks' documentation. I'm not too happy about effectively serializing rdtsc, since Linux guests call it very often (for each vCPU!). But as usual, correctness goes before performance or scalability.

You can fetch the series from:
https://github.com/cota/qemu/tree/x86_64-mttcg

Thanks,

Emilio

#1 lore raw

PATCH 1/4 hw/i386/pc: hold the BQL when calling cpu_get_ticks

---
 hw/i386/pc.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/i386/pc.c +12 -1
index 83a444472b..7371cd9960 100644
@@ -158,7 +158,18 @@ static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
 /* TSC handling */
 uint64_t cpu_get_tsc(CPUX86State *env)
 {
-    return cpu_get_ticks();
+    uint64_t ret;
+    bool locked;
+
+    locked = qemu_mutex_iothread_locked();
+    if (!locked) {
+        qemu_mutex_lock_iothread();
+    }
+    ret = cpu_get_ticks();
+    if (!locked) {
+        qemu_mutex_unlock_iothread();
+    }
+    return ret;
 }
 
 /* IRQ handling */
Signature · 1 line
2.17.1
#3 lore raw

PATCH 2/4 cpus: assert that the BQL is held in cpu_get_ticks

In reply to Emilio G. Cota
---
 cpus.c | 2 ++
 1 file changed, 2 insertions(+)
cpus.c +2 -0
index b5844b7103..b23bf6fef7 100644
@@ -308,6 +308,8 @@ int64_t cpu_get_ticks(void)
 {
     int64_t ticks;
 
+    g_assert(qemu_mutex_iothread_locked());
+
     if (use_icount) {
         return cpu_get_icount();
     }
Signature · 1 line
2.17.1
#4 lore raw

PATCH 3/4 target/i386/translate: use thread-local storage in !user-mode

In reply to Emilio G. Cota

Needed for MTTCG.

---
 target/i386/translate.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
target/i386/translate.c +16 -8
index 07d185e7b6..f4c2a41f8f 100644
@@ -71,26 +71,34 @@
 
 //#define MACRO_TEST   1
 
+/* we need thread-local storage for mttcg */
+#ifdef CONFIG_USER_ONLY
+#define I386_THREAD
+#else
+#define I386_THREAD __thread
+#endif
+
 /* global register indexes */
-static TCGv cpu_A0;
-static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
+static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2;
 static TCGv_i32 cpu_cc_op;
 static TCGv cpu_regs[CPU_NB_REGS];
 static TCGv cpu_seg_base[6];
 static TCGv_i64 cpu_bndl[4];
 static TCGv_i64 cpu_bndu[4];
 /* local temps */
-static TCGv cpu_T0, cpu_T1;
+static I386_THREAD TCGv cpu_cc_srcT;
+static I386_THREAD TCGv cpu_A0;
+static I386_THREAD TCGv cpu_T0, cpu_T1;
 /* local register indexes (only used inside old micro ops) */
-static TCGv cpu_tmp0, cpu_tmp4;
-static TCGv_ptr cpu_ptr0, cpu_ptr1;
-static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
-static TCGv_i64 cpu_tmp1_i64;
+static I386_THREAD TCGv cpu_tmp0, cpu_tmp4;
+static I386_THREAD TCGv_ptr cpu_ptr0, cpu_ptr1;
+static I386_THREAD TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
+static I386_THREAD TCGv_i64 cpu_tmp1_i64;
 
 #include "exec/gen-icount.h"
 
 #ifdef TARGET_X86_64
-static int x86_64_hregs;
+static I386_THREAD int x86_64_hregs;
 #endif
 
 typedef struct DisasContext {
Signature · 1 line
2.17.1
#7 lore raw

PATCH 4/4 configure: enable mttcg for x86_64

In reply to Emilio G. Cota
---
 configure | 1 +
 1 file changed, 1 insertion(+)
configure +1 -0
index 2a7796ea80..db216bafb1 100755
@@ -6935,6 +6935,7 @@ case "$target_name" in
     TARGET_BASE_ARCH=i386
     gdb_xml_files="i386-64bit.xml i386-64bit-core.xml i386-64bit-sse.xml"
     target_compiler=$cross_cc_x86_64
+    mttcg="yes"
   ;;
   alpha)
     mttcg="yes"
Signature · 1 line
2.17.1
#5 lore raw

Re: [Qemu-devel] [PATCH 3/4] target/i386/translate: use thread-local storage in !user-mode

In reply to Emilio G. Cota
Quoted 3 lines · Emilio G. Cota

Needed for MTTCG.

Why not always use TLS, even in user-mode?

Paolo

Quoted 52 lines
---
 target/i386/translate.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
target/i386/translate.c +16 -8
index 07d185e7b6..f4c2a41f8f 100644
@@ -71,26 +71,34 @@
 
 //#define MACRO_TEST   1
 
+/* we need thread-local storage for mttcg */
+#ifdef CONFIG_USER_ONLY
+#define I386_THREAD
+#else
+#define I386_THREAD __thread
+#endif
+
 /* global register indexes */
-static TCGv cpu_A0;
-static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
+static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2;
 static TCGv_i32 cpu_cc_op;
 static TCGv cpu_regs[CPU_NB_REGS];
 static TCGv cpu_seg_base[6];
 static TCGv_i64 cpu_bndl[4];
 static TCGv_i64 cpu_bndu[4];
 /* local temps */
-static TCGv cpu_T0, cpu_T1;
+static I386_THREAD TCGv cpu_cc_srcT;
+static I386_THREAD TCGv cpu_A0;
+static I386_THREAD TCGv cpu_T0, cpu_T1;
 /* local register indexes (only used inside old micro ops) */
-static TCGv cpu_tmp0, cpu_tmp4;
-static TCGv_ptr cpu_ptr0, cpu_ptr1;
-static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
-static TCGv_i64 cpu_tmp1_i64;
+static I386_THREAD TCGv cpu_tmp0, cpu_tmp4;
+static I386_THREAD TCGv_ptr cpu_ptr0, cpu_ptr1;
+static I386_THREAD TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
+static I386_THREAD TCGv_i64 cpu_tmp1_i64;
 
 #include "exec/gen-icount.h"
 
 #ifdef TARGET_X86_64
-static int x86_64_hregs;
+static I386_THREAD int x86_64_hregs;
 #endif
 
 typedef struct DisasContext {
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] hw/i386/pc: hold the BQL when calling cpu_get_ticks

In reply to Emilio G. Cota
Quoted 20 lines · Emilio G. Cota
---
 hw/i386/pc.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/i386/pc.c +5 -1
index 83a444472b..7371cd9960 100644
@@ -158,7 +158,18 @@ static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
 /* TSC handling */
 uint64_t cpu_get_tsc(CPUX86State *env)
 {
-    return cpu_get_ticks();
+    uint64_t ret;
+    bool locked;
+
+    locked = qemu_mutex_iothread_locked();
+    if (!locked) {

hw/i386/pc.c static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size) · line 175

Quoted 10 lines
hw/i386/pc.c +7 -0
+        qemu_mutex_lock_iothread();
+    }
+    ret = cpu_get_ticks();
+    if (!locked) {
+        qemu_mutex_unlock_iothread();
+    }
+    return ret;
 }
 
 /* IRQ handling */

We could use a spinlock for the rare case of timers_state.cpu_ticks_prev

Quoted 1 line

ticks (or even, on 64-bit, a seqlock+spinlock). I'll give it a shot.

Paolo

#6 lore raw

Re: [Qemu-devel] [PATCH 3/4] target/i386/translate: use thread-local storage in !user-mode

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
Quoted 3 lines · Emilio G. Cota

Needed for MTTCG.

Why not always use TLS, even in user-mode?

To avoid TLS waste; user-mode uses a single TCGContext, so a single copy of these variables is all that's needed because code generation is serialized with a lock.

If in user-mode we just have a few threads it'd be no big deal, but apps can easily spawn thousands of threads.

		E.