Skip to contentSkip to conversation

tb hash improvements

PATCH In review Emilio G. Cota 5 participants 28 messages Apr 7, 2016 – Apr 8, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

v2 cover tb hash improvements

See v1 for context:
https://lists.gnu.org/archive/html/qemu-devel/2016-04/msg00587.html

All patches in v2 are checkpatch-clean, except 05 (checkpatch should be ignored for this one) and 06, which I took unmodified (later patches fix those warnings while doing other things, anyway).

Note that patch 01 has already been picked up by Paolo; I left it here for completeness.

Another patch that is related to this series is the transition of tb->flags to uint32_t; this has been sent as a separate patch since it touches all targets. It shouldn't conflict with this patchset.

Changes from v1:
- Drop QEMU_CACHELINE, define QEMU_ALIGNED()
+ Remove excessive caution about Windows: it supports it.
+ define QHT_BUCKET_ALIGN as 64
- Add some reviewed-by tags from Alex.
- Drop POSIX spinlock wrapper; use the one pointed out by Paolo
+ Add a couple of fixes over this spinlock implementation:
* define cpu_relax() for some architectures
* Optimize spin_lock() for uncontended cases
- Add tb_hash_func5, a version of xxhash32 customized for tb-hash, so that the input values do not have to be contiguous in memory.
+ Drop xxhash.h, add only the customized version at exec/tb-hash-xx.h
- qht
+ use size_t for counts in qht
* Drop some 2**32 size checks in there; it's not really a problem in practice.
+ s/__func/func__locked/
+ Test program: hash with tb_hash_func5

Thanks,

Emilio

#1 lore raw

PATCH v2 2/13 compiler.h: add QEMU_ALIGNED() to enforce struct alignment

---
 include/qemu/compiler.h | 2 ++
 1 file changed, 2 insertions(+)
include/qemu/compiler.h +2 -0
index 8f1cc7b..1978ddc 100644
@@ -41,6 +41,8 @@
 # define QEMU_PACKED __attribute__((packed))
 #endif
 
+#define QEMU_ALIGNED(B) __attribute__((aligned(B)))
+
 #ifndef glue
 #define xglue(x, y) x ## y
 #define glue(x, y) xglue(x, y)
Signature · 1 line
2.5.0
#3 lore raw

PATCH v2 3/13 seqlock: remove optional mutex

In reply to Emilio G. Cota

This option is unused; besides, it bloats the struct when not needed. Let's just let writers define their own locks elsewhere.

---
 cpus.c                 |  2 +-
 include/qemu/seqlock.h | 10 +---------
 2 files changed, 2 insertions(+), 10 deletions(-)
cpus.c +1 -1
index 8ae4777..7f550b9 100644
@@ -611,7 +611,7 @@ int cpu_throttle_get_percentage(void)
 
 void cpu_ticks_init(void)
 {
-    seqlock_init(&timers_state.vm_clock_seqlock, NULL);
+    seqlock_init(&timers_state.vm_clock_seqlock);
     vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
     throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
                                            cpu_throttle_timer_tick, NULL);
include/qemu/seqlock.h +1 -9
index 70b01fd..e673482 100644
@@ -19,22 +19,17 @@
 typedef struct QemuSeqLock QemuSeqLock;
 
 struct QemuSeqLock {
-    QemuMutex *mutex;
     unsigned sequence;
 };
 
-static inline void seqlock_init(QemuSeqLock *sl, QemuMutex *mutex)
+static inline void seqlock_init(QemuSeqLock *sl)
 {
-    sl->mutex = mutex;
     sl->sequence = 0;
 }
 
 /* Lock out other writers and update the count.  */
 static inline void seqlock_write_lock(QemuSeqLock *sl)
 {
-    if (sl->mutex) {
-        qemu_mutex_lock(sl->mutex);
-    }
     ++sl->sequence;
 
     /* Write sequence before updating other fields.  */
@@ -47,9 +42,6 @@ static inline void seqlock_write_unlock(QemuSeqLock *sl)
     smp_wmb();
 
     ++sl->sequence;
-    if (sl->mutex) {
-        qemu_mutex_unlock(sl->mutex);
-    }
 }
 
 static inline unsigned seqlock_read_begin(QemuSeqLock *sl)
Signature · 1 line
2.5.0
Reviewed-by on v2 3/13
#4 lore raw

Re: [Qemu-devel] [PATCH v2 03/13] seqlock: remove optional mutex

Quoted 9 lines · Emilio G. Cota

This option is unused; besides, it bloats the struct when not needed. Let's just let writers define their own locks elsewhere.

---
 cpus.c                 |  2 +-
 include/qemu/seqlock.h | 10 +---------
 2 files changed, 2 insertions(+), 10 deletions(-)

r~

#5 lore raw

PATCH v2 4/13 seqlock: rename write_lock/unlock to write_begin/end

In reply to Emilio G. Cota

It is a more appropriate name, now that the mutex embedded in the seqlock is gone.

---
 cpus.c                 | 28 ++++++++++++++--------------
 include/qemu/seqlock.h |  4 ++--
 2 files changed, 16 insertions(+), 16 deletions(-)
cpus.c +14 -14
index 7f550b9..7dad2e6 100644
@@ -247,13 +247,13 @@ int64_t cpu_get_clock(void)
 void cpu_enable_ticks(void)
 {
     /* Here, the really thing protected by seqlock is cpu_clock_offset. */
-    seqlock_write_lock(&timers_state.vm_clock_seqlock);
+    seqlock_write_begin(&timers_state.vm_clock_seqlock);
     if (!timers_state.cpu_ticks_enabled) {
         timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
         timers_state.cpu_clock_offset -= get_clock();
         timers_state.cpu_ticks_enabled = 1;
     }
-    seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+    seqlock_write_end(&timers_state.vm_clock_seqlock);
 }
 
 /* disable cpu_get_ticks() : the clock is stopped. You must not call
@@ -263,13 +263,13 @@ void cpu_enable_ticks(void)
 void cpu_disable_ticks(void)
 {
     /* Here, the really thing protected by seqlock is cpu_clock_offset. */
-    seqlock_write_lock(&timers_state.vm_clock_seqlock);
+    seqlock_write_begin(&timers_state.vm_clock_seqlock);
     if (timers_state.cpu_ticks_enabled) {
         timers_state.cpu_ticks_offset += cpu_get_host_ticks();
         timers_state.cpu_clock_offset = cpu_get_clock_locked();
         timers_state.cpu_ticks_enabled = 0;
     }
-    seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+    seqlock_write_end(&timers_state.vm_clock_seqlock);
 }
 
 /* Correlation between real and virtual time is always going to be
@@ -292,7 +292,7 @@ static void icount_adjust(void)
         return;
     }
 
-    seqlock_write_lock(&timers_state.vm_clock_seqlock);
+    seqlock_write_begin(&timers_state.vm_clock_seqlock);
     cur_time = cpu_get_clock_locked();
     cur_icount = cpu_get_icount_locked();
 
@@ -313,7 +313,7 @@ static void icount_adjust(void)
     last_delta = delta;
     timers_state.qemu_icount_bias = cur_icount
                               - (timers_state.qemu_icount << icount_time_shift);
-    seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+    seqlock_write_end(&timers_state.vm_clock_seqlock);
 }
 
 static void icount_adjust_rt(void *opaque)
@@ -345,7 +345,7 @@ static void icount_warp_rt(void)
         return;
     }
 
-    seqlock_write_lock(&timers_state.vm_clock_seqlock);
+    seqlock_write_begin(&timers_state.vm_clock_seqlock);
     if (runstate_is_running()) {
         int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
                                      cpu_get_clock_locked());
@@ -364,7 +364,7 @@ static void icount_warp_rt(void)
         timers_state.qemu_icount_bias += warp_delta;
     }
     vm_clock_warp_start = -1;
-    seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+    seqlock_write_end(&timers_state.vm_clock_seqlock);
 
     if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
         qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
@@ -389,9 +389,9 @@ void qtest_clock_warp(int64_t dest)
         int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
         int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
 
-        seqlock_write_lock(&timers_state.vm_clock_seqlock);
+        seqlock_write_begin(&timers_state.vm_clock_seqlock);
         timers_state.qemu_icount_bias += warp;
-        seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+        seqlock_write_end(&timers_state.vm_clock_seqlock);
 
         qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
         timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
@@ -458,9 +458,9 @@ void qemu_start_warp_timer(void)
              * It is useful when we want a deterministic execution time,
              * isolated from host latencies.
              */
-            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            seqlock_write_begin(&timers_state.vm_clock_seqlock);
             timers_state.qemu_icount_bias += deadline;
-            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            seqlock_write_end(&timers_state.vm_clock_seqlock);
             qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
         } else {
             /*
@@ -471,11 +471,11 @@ void qemu_start_warp_timer(void)
              * you will not be sending network packets continuously instead of
              * every 100ms.
              */
-            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            seqlock_write_begin(&timers_state.vm_clock_seqlock);
             if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
                 vm_clock_warp_start = clock;
             }
-            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            seqlock_write_end(&timers_state.vm_clock_seqlock);
             timer_mod_anticipate(icount_warp_timer, clock + deadline);
         }
     } else if (deadline == 0) {
include/qemu/seqlock.h +2 -2
index e673482..4dfc055 100644
@@ -28,7 +28,7 @@ static inline void seqlock_init(QemuSeqLock *sl)
 }
 
 /* Lock out other writers and update the count.  */
-static inline void seqlock_write_lock(QemuSeqLock *sl)
+static inline void seqlock_write_begin(QemuSeqLock *sl)
 {
     ++sl->sequence;
 
@@ -36,7 +36,7 @@ static inline void seqlock_write_lock(QemuSeqLock *sl)
     smp_wmb();
 }
 
-static inline void seqlock_write_unlock(QemuSeqLock *sl)
+static inline void seqlock_write_end(QemuSeqLock *sl)
 {
     /* Write other fields before finalizing sequence.  */
     smp_wmb();
Signature · 1 line
2.5.0
Reviewed-by on v2 4/13
#6 lore raw

Re: [Qemu-devel] [PATCH v2 04/13] seqlock: rename write_lock/unlock to write_begin/end

Quoted 9 lines · Emilio G. Cota

It is a more appropriate name, now that the mutex embedded in the seqlock is gone.

---
 cpus.c                 | 28 ++++++++++++++--------------
 include/qemu/seqlock.h |  4 ++--
 2 files changed, 16 insertions(+), 16 deletions(-)

r~

#7 lore raw

PATCH v2 5/13 include/processor.h: define cpu_relax()

In reply to Emilio G. Cota

Taken from the linux kernel.

---
 include/qemu/processor.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 include/qemu/processor.h
include/qemu/processor.h new file +28 -0
new file mode 100644
index 0000000..675a00a
@@ -0,0 +1,28 @@
+#ifndef QEMU_PROCESSOR_H
+#define QEMU_PROCESSOR_H
+
+#include "qemu/atomic.h"
+
+#if defined(__i386__) || defined(__x86_64__)
+#define cpu_relax() asm volatile("rep; nop" ::: "memory")
+#endif
+
+#ifdef __ia64__
+#define cpu_relax() asm volatile("hint @pause" ::: "memory")
+#endif
+
+#ifdef __aarch64__
+#define cpu_relax() asm volatile("yield" ::: "memory")
+#endif
+
+#if defined(__powerpc64__)
+/* set Hardware Multi-Threading (HMT) priority to low; then back to medium */
+#define cpu_relax() asm volatile("or 1, 1, 1;"
+                                 "or 2, 2, 2;" ::: "memory")
+#endif
+
+#ifndef cpu_relax
+#define cpu_relax() barrier()
+#endif
+
+#endif /* QEMU_PROCESSOR_H */
Signature · 1 line
2.5.0
Reviewed-by on v2 5/13
#8 lore raw

Re: [Qemu-devel] [PATCH v2 05/13] include/processor.h: define cpu_relax()

Quoted 7 lines · Emilio G. Cota

Taken from the linux kernel.

---
 include/qemu/processor.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 include/qemu/processor.h

include/qemu/processor.h line 8

Quoted 3 lines
include/qemu/processor.h new file +3 -0
+#if defined(__i386__) || defined(__x86_64__)
+#define cpu_relax() asm volatile("rep; nop" ::: "memory")
+#endif

Not that it matters much, but for the record there's an ICC inspired builtin in <xmmintrin.h> for this: _mm_pause(). But so long as we're always using gcc or clang, that's probably just an extra dependency to worry about.

r~

#9 lore raw

PATCH v2 6/13 qemu-thread: add simple test-and-set spinlock

In reply to Emilio G. Cota

From: Guillaume Delbergue <[email protected]>

---
 include/qemu/thread.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
include/qemu/thread.h +31 -0
index bdae6df..1aa843b 100644
@@ -1,6 +1,8 @@
 #ifndef __QEMU_THREAD_H
 #define __QEMU_THREAD_H 1
 
+#include <errno.h>
+#include "qemu/atomic.h"
 
 typedef struct QemuMutex QemuMutex;
 typedef struct QemuCond QemuCond;
@@ -60,4 +62,33 @@ struct Notifier;
 void qemu_thread_atexit_add(struct Notifier *notifier);
 void qemu_thread_atexit_remove(struct Notifier *notifier);
 
+typedef struct QemuSpin {
+    int value;
+} QemuSpin;
+
+static inline void qemu_spin_init(QemuSpin *spin)
+{
+    spin->value = 0;
+}
+
+static inline void qemu_spin_lock(QemuSpin *spin)
+{
+    do {
+        while (atomic_read(&spin->value));
+    } while (atomic_xchg(&spin->value, true));
+}
+
+static inline int qemu_spin_trylock(QemuSpin *spin)
+{
+    if (atomic_read(&spin->value) || atomic_xchg(&spin->value, true)) {
+        return -EBUSY;
+    }
+    return 0;
+}
+
+static inline void qemu_spin_unlock(QemuSpin *spin)
+{
+    atomic_mb_set(&spin->value, 0);
+}
+
 #endif
Signature · 1 line
2.5.0
#10 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

Quoted 23 lines · Emilio G. Cota

From: Guillaume Delbergue <[email protected]>

---
 include/qemu/thread.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
include/qemu/thread.h +2 -0
index bdae6df..1aa843b 100644
@@ -1,6 +1,8 @@
 #ifndef __QEMU_THREAD_H
 #define __QEMU_THREAD_H 1
 
+#include <errno.h>
+#include "qemu/atomic.h"
 
 typedef struct QemuMutex QemuMutex;
 typedef struct QemuCond QemuCond;

include/qemu/thread.h struct Notifier; · line 66

Quoted 5 lines
include/qemu/thread.h +2 -0
@@ -60,4 +62,33 @@ struct Notifier;
 void qemu_thread_atexit_add(struct Notifier *notifier);
 void qemu_thread_atexit_remove(struct Notifier *notifier);
 
+typedef struct QemuSpin {
+    int value;

If we are throwing true and false around as the only two values can we use bool here and be consistent when setting/clearing.

Quoted 18 lines
include/qemu/thread.h +18 -0
+} QemuSpin;
+
+static inline void qemu_spin_init(QemuSpin *spin)
+{
+    spin->value = 0;
+}
+
+static inline void qemu_spin_lock(QemuSpin *spin)
+{
+    do {
+        while (atomic_read(&spin->value));
+    } while (atomic_xchg(&spin->value, true));
+}
+
+static inline int qemu_spin_trylock(QemuSpin *spin)
+{
+    if (atomic_read(&spin->value) || atomic_xchg(&spin->value, true)) {
+        return -EBUSY;

include/qemu/thread.h struct Notifier; · line 94

Quoted 10 lines
include/qemu/thread.h +9 -0
+    }
+    return 0;
+}
+
+static inline void qemu_spin_unlock(QemuSpin *spin)
+{
+    atomic_mb_set(&spin->value, 0);
+}
+
 #endif

--
Alex Bennée

#11 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

Quoted 5 lines · Alex Bennée
Quoted 2 lines

include/qemu/thread.h struct Notifier; · line 66

Quoted 2 lines
include/qemu/thread.h +2 -0
+typedef struct QemuSpin {
+    int value;

If we are throwing true and false around as the only two values can we use bool here and be consistent when setting/clearing.

Except that quite a lot of hosts can only (efficiently) do atomic operations on a minimum of 4 byte quantities. I'd rather continue to use int here.

r~

#12 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

Quoted 9 lines · Richard Henderson
Quoted 5 lines · Alex Bennée
Quoted 2 lines

include/qemu/thread.h struct Notifier; · line 66

Quoted 2 lines
include/qemu/thread.h +2 -0
+typedef struct QemuSpin {
+    int value;

If we are throwing true and false around as the only two values can we use bool here and be consistent when setting/clearing.

Except that quite a lot of hosts can only (efficiently) do atomic operations on a minimum of 4 byte quantities. I'd rather continue to use int here.

I suspect bool == unsigned int underneath. But having true/false and 0/1 mixed up gets confusing even if they are equivalent.

Quoted 3 lines

r~

--
Alex Bennée

#13 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

Quoted 5 lines · Alex Bennée
Quoted 2 lines

Except that quite a lot of hosts can only (efficiently) do atomic operations on a minimum of 4 byte quantities. I'd rather continue to use int here.

I suspect bool == unsigned int underneath. But having true/false and 0/1 mixed up gets confusing even if they are equivalent.

Sometimes sizeof(bool) == 1.

Paolo

#14 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Alex Bennée
Quoted 2 lines

Except that quite a lot of hosts can only (efficiently) do atomic operations on a minimum of 4 byte quantities. I'd rather continue to use int here.

I suspect bool == unsigned int underneath. But having true/false and 0/1 mixed up gets confusing even if they are equivalent.

Sometimes sizeof(bool) == 1.

sizeof(bool) == 1 everywhere except MacOSX, where it's 4.

r~

#15 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

Quoted 10 lines · Richard Henderson
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Alex Bennée
Quoted 2 lines

Except that quite a lot of hosts can only (efficiently) do atomic operations on a minimum of 4 byte quantities. I'd rather continue to use int here.

I suspect bool == unsigned int underneath. But having true/false and 0/1 mixed up gets confusing even if they are equivalent.

Sometimes sizeof(bool) == 1.

sizeof(bool) == 1 everywhere except MacOSX, where it's 4.

Hm, that's too strange:

$ gcc a.c
a.c: In function ‘main’:
a.c:6:5: warning: format ‘%d’ expects argument of type ‘int’, but
argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("%d\n", sizeof(bool));
     ^
$ ./a.out
1

Kind regards,
Sergey

#16 lore raw

Re: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock

In reply to Emilio G. Cota
Quoted 1 line · Emilio G. Cota

+ while (atomic_read(&spin->value));

I really really don't like ; snuggled up behind loop conditions. Isn't this where you want to use pause, anyway?

r~

#17 lore raw

PATCH v2 7/13 qemu-thread: call cpu_relax() while spinning

In reply to Emilio G. Cota
---
 include/qemu/thread.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/qemu/thread.h +4 -1
index 1aa843b..599965e 100644
@@ -2,6 +2,7 @@
 #define __QEMU_THREAD_H 1
 
 #include <errno.h>
+#include "qemu/processor.h"
 #include "qemu/atomic.h"
 
 typedef struct QemuMutex QemuMutex;
@@ -74,7 +75,9 @@ static inline void qemu_spin_init(QemuSpin *spin)
 static inline void qemu_spin_lock(QemuSpin *spin)
 {
     do {
-        while (atomic_read(&spin->value));
+        while (atomic_read(&spin->value)) {
+            cpu_relax();
+        }
     } while (atomic_xchg(&spin->value, true));
 }
 
Signature · 1 line
2.5.0
#18 lore raw

Re: [Qemu-devel] [PATCH v2 07/13] qemu-thread: call cpu_relax() while spinning

include/qemu/thread.h static inline void qemu_spin_init(QemuSpin *spin) · line 80

Quoted 5 lines · Emilio G. Cota
     do {
include/qemu/thread.h +3 -1
-        while (atomic_read(&spin->value));
+        while (atomic_read(&spin->value)) {
+            cpu_relax();
+        }

Please just merge this with the previous patch.

r~

#19 lore raw

PATCH v2 8/13 qemu-thread: optimize spin_lock for uncontended locks

In reply to Emilio G. Cota

This way we can acquire the lock with xchg+test, instead of test+xchg+test. Most spinlocks should be uncontended so this should result in a ne performance gain.

Before:

  4ad957:       eb 09                   jmp    4ad962 <qht_insert+0x32>
  4ad959:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
  4ad960:       f3 90                   pause
  4ad962:       8b 03                   mov    (%rbx),%eax
  4ad964:       85 c0                   test   %eax,%eax
  4ad966:       75 f8                   jne    4ad960 <qht_insert+0x30>
  4ad968:       89 f8                   mov    %edi,%eax
  4ad96a:       87 03                   xchg   %eax,(%rbx)
  4ad96c:       85 c0                   test   %eax,%eax
  4ad96e:       75 f2                   jne    4ad962 <qht_insert+0x32>

After:

  4ad980:       89 f8                   mov    %edi,%eax
  4ad982:       87 03                   xchg   %eax,(%rbx)
  4ad984:       85 c0                   test   %eax,%eax
  4ad986:       74 12                   je     4ad99a <qht_insert+0x4a>
  4ad988:       0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
  4ad98f:       00
  4ad990:       8b 03                   mov    (%rbx),%eax
  4ad992:       85 c0                   test   %eax,%eax
  4ad994:       74 ea                   je     4ad980 <qht_insert+0x30>
  4ad996:       f3 90                   pause
  4ad998:       eb f6                   jmp    4ad990 <qht_insert+0x40>
---
 include/qemu/thread.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/qemu/thread.h +2 -2
index 599965e..e2af57c 100644
@@ -74,11 +74,11 @@ static inline void qemu_spin_init(QemuSpin *spin)
 
 static inline void qemu_spin_lock(QemuSpin *spin)
 {
-    do {
+    while (atomic_xchg(&spin->value, true)) {
         while (atomic_read(&spin->value)) {
             cpu_relax();
         }
-    } while (atomic_xchg(&spin->value, true));
+    }
 }
 
 static inline int qemu_spin_trylock(QemuSpin *spin)
Signature · 1 line
2.5.0
#20 lore raw

Re: [Qemu-devel] [PATCH v2 08/13] qemu-thread: optimize spin_lock for uncontended locks

include/qemu/thread.h static inline void qemu_spin_init(QemuSpin *spin) · line 82

Quoted 10 lines · Emilio G. Cota

static inline void qemu_spin_lock(QemuSpin *spin) {

include/qemu/thread.h +2 -2
-    do {
+    while (atomic_xchg(&spin->value, true)) {
         while (atomic_read(&spin->value)) {
             cpu_relax();
         }
-    } while (atomic_xchg(&spin->value, true));
+    }
 }

And merge this one as well, please. It's a good improvement, but there's little point to keeping these separate.

r~

#21 lore raw

PATCH v2 9/13 exec: add tb_hash_func5, derived from xxhash

In reply to Emilio G. Cota

This will be used by upcoming changes for hashing the tb hash.

Add this into a separate file to include the copyright notice from xxhash.

---
 include/exec/tb-hash-xx.h | 103 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 103 insertions(+)
 create mode 100644 include/exec/tb-hash-xx.h
include/exec/tb-hash-xx.h new file +103 -0
new file mode 100644
index 0000000..64d9865
@@ -0,0 +1,103 @@
+/*
+ * xxHash - Fast Hash algorithm
+ * Copyright (C) 2012-2016, Yann Collet
+ *
+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * + Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * + Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * You can contact the author at :
+ * - xxHash source repository : https://github.com/Cyan4973/xxHash
+ */
+#ifndef EXEC_TB_HASH_XX
+#define EXEC_TB_HASH_XX
+
+#include <stdint.h>
+
+#define PRIME32_1   2654435761U
+#define PRIME32_2   2246822519U
+#define PRIME32_3   3266489917U
+#define PRIME32_4    668265263U
+#define PRIME32_5    374761393U
+
+/*
+ * Note : although _rotl exists for minGW (GCC under windows), performance
+ * seems poor.
+ */
+#if defined(_MSC_VER)
+#  define XXH_rotl32(x, r) _rotl(x, r)
+#else
+#  define XXH_rotl32(x, r) ((x << r) | (x >> (32 - r)))
+#endif
+
+/*
+ * xxhash32, customized for input variables that are not guaranteed to be
+ * contiguous in memory.
+ */
+static inline
+uint32_t tb_hash_func5(uint64_t a0, uint64_t b0, uint32_t e, int seed)
+{
+    uint32_t v1 = seed + PRIME32_1 + PRIME32_2;
+    uint32_t v2 = seed + PRIME32_2;
+    uint32_t v3 = seed + 0;
+    uint32_t v4 = seed - PRIME32_1;
+    uint32_t a = a0 >> 31 >> 1;
+    uint32_t b = a0;
+    uint32_t c = b0 >> 31 >> 1;
+    uint32_t d = b0;
+    uint32_t h32;
+
+    v1 += a * PRIME32_2;
+    v1 = XXH_rotl32(v1, 13);
+    v1 *= PRIME32_1;
+
+    v2 += b * PRIME32_2;
+    v2 = XXH_rotl32(v2, 13);
+    v2 *= PRIME32_1;
+
+    v3 += c * PRIME32_2;
+    v3 = XXH_rotl32(v3, 13);
+    v3 *= PRIME32_1;
+
+    v4 += d * PRIME32_2;
+    v4 = XXH_rotl32(v4, 13);
+    v4 *= PRIME32_1;
+
+    h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) +
+          XXH_rotl32(v4, 18);
+    h32 += 20;
+
+    h32 += e * PRIME32_3;
+    h32  = XXH_rotl32(h32, 17) * PRIME32_4;
+
+    h32 ^= h32 >> 15;
+    h32 *= PRIME32_2;
+    h32 ^= h32 >> 13;
+    h32 *= PRIME32_3;
+    h32 ^= h32 >> 16;
+
+    return h32;
+}
+
+#endif /* EXEC_TB_HASH_XX */
Signature · 1 line
2.5.0
#22 lore raw

Re: [Qemu-devel] [PATCH v2 09/13] exec: add tb_hash_func5, derived from xxhash

Quoted 1 line · Emilio G. Cota

+#include <stdint.h>

Should already be done by <qemu/osdep.h>

include/exec/tb-hash-xx.h line 52

Quoted 9 lines
include/exec/tb-hash-xx.h new file +9 -0
+/*
+ * Note : although _rotl exists for minGW (GCC under windows), performance
+ * seems poor.
+ */
+#if defined(_MSC_VER)
+#  define XXH_rotl32(x, r) _rotl(x, r)
+#else
+#  define XXH_rotl32(x, r) ((x << r) | (x >> (32 - r)))
+#endif

Please use rol32 from <qemu/bitops.h>

include/exec/tb-hash-xx.h line 60

Quoted 3 lines
include/exec/tb-hash-xx.h new file +3 -0
+static inline
+uint32_t tb_hash_func5(uint64_t a0, uint64_t b0, uint32_t e, int seed)
+{

Is there really any point in passing in "seed" anymore? We certainly don't want different values passed in...

r~

#23 lore raw

PATCH v2 10/13 tb hash: hash phys_pc, pc, and flags with xxhash

In reply to Emilio G. Cota

For some workloads such as arm bootup, tb_phys_hash is performance-critical. The is due to the high frequency of accesses to the hash table, originated by (frequent) TLB flushes that wipe out the cpu-private tb_jmp_cache's. More info:
https://lists.nongnu.org/archive/html/qemu-devel/2016-03/msg05098.html

To dig further into this I modified an arm image booting debian jessie to immediately shut down after boot. Analysis revealed that quite a bit of time is unnecessarily spent in tb_phys_hash: the cause is poor hashing that results in very uneven loading of chains in the hash table's buckets; the longest observed chain had ~550 elements.

The appended addresses this with two changes:

1) Use xxhash as the hash table's hash function. xxhash is a fast, high-quality hashing function.

2) Feed the hashing function with not just tb_phys, but also pc and flags.

This improves performance over using just tb_phys for hashing, since that resulted in some hash buckets having many TB's, while others getting very few; with these changes, the longest observed chain on a single hash bucket is brought down from ~550 to ~40.

Tests show that the other element checked for in tb_find_physical, cs_base, is always a match when tb_phys+pc+flags are a match, so hashing cs_base is wasteful. It could be that this is an ARM-only thing, though.

BTW, after this change the hash table should not be called "tb_hash_phys" anymore; this is addressed later in this series.

This change gives consistent bootup time improvements. I tested two host machines:
- Intel Xeon E5-2690: 11.6% less time
- Intel i7-4790K: 19.2% less time

Increasing the number of hash buckets yields further improvements. However, using a larger, fixed number of buckets can degrade performance for other workloads that do not translate as many blocks (600K+ for debian-jessie arm bootup). This is dealt with later in this series.

---
 cpu-exec.c             | 2 +-
 include/exec/tb-hash.h | 8 ++++++--
 translate-all.c        | 6 +++---
 3 files changed, 10 insertions(+), 6 deletions(-)
cpu-exec.c +1 -1
index debc65c..a889cf1 100644
@@ -233,7 +233,7 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     /* find translated block using physical mappings */
     phys_pc = get_page_addr_code(env, pc);
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
-    h = tb_phys_hash_func(phys_pc);
+    h = tb_hash_func(phys_pc, pc, flags);
     ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
     for(;;) {
         tb = *ptb1;
include/exec/tb-hash.h +6 -2
index 0f4e8a0..164b6f4 100644
@@ -20,6 +20,9 @@
 #ifndef EXEC_TB_HASH
 #define EXEC_TB_HASH
 
+#include "exec/exec-all.h"
+#include "exec/tb-hash-xx.h"
+
 /* Only the bottom TB_JMP_PAGE_BITS of the jump cache hash bits vary for
    addresses on the same page.  The top bits are the same.  This allows
    TLB invalidation to quickly clear a subset of the hash table.  */
@@ -43,9 +46,10 @@ static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
            | (tmp & TB_JMP_ADDR_MASK));
 }
 
-static inline unsigned int tb_phys_hash_func(tb_page_addr_t pc)
+static inline
+uint32_t tb_hash_func(tb_page_addr_t phys_pc, target_ulong pc, int flags)
 {
-    return (pc >> 2) & (CODE_GEN_PHYS_HASH_SIZE - 1);
+    return tb_hash_func5(phys_pc, pc, flags, 1) & (CODE_GEN_PHYS_HASH_SIZE - 1);
 }
 
 #endif
translate-all.c +3 -3
index 1a8f68b..eca2f16 100644
@@ -972,7 +972,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
 
     /* remove the TB from the hash list */
     phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
-    h = tb_phys_hash_func(phys_pc);
+    h = tb_hash_func(phys_pc, tb->pc, tb->flags);
     tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
 
     /* remove the TB from the page list */
@@ -1474,8 +1474,8 @@ static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
     unsigned int h;
     TranslationBlock **ptb;
 
-    /* add in the physical hash table */
-    h = tb_phys_hash_func(phys_pc);
+    /* add in the hash table */
+    h = tb_hash_func(phys_pc, tb->pc, tb->flags);
     ptb = &tcg_ctx.tb_ctx.tb_phys_hash[h];
     tb->phys_hash_next = *ptb;
     *ptb = tb;
Signature · 1 line
2.5.0
Reviewed-by on v2 10/13
#24 lore raw

Re: [Qemu-devel] [PATCH v2 10/13] tb hash: hash phys_pc, pc, and flags with xxhash

Quoted 12 lines · Emilio G. Cota

For some workloads such as arm bootup, tb_phys_hash is performance-critical. The is due to the high frequency of accesses to the hash table, originated by (frequent) TLB flushes that wipe out the cpu-private tb_jmp_cache's. More info:
https://lists.nongnu.org/archive/html/qemu-devel/2016-03/msg05098.html

To dig further into this I modified an arm image booting debian jessie to immediately shut down after boot. Analysis revealed that quite a bit of time is unnecessarily spent in tb_phys_hash: the cause is poor hashing that results in very uneven loading of chains in the hash table's buckets; the longest observed chain had ~550 elements.

Quoted 6 lines

The appended addresses this with two changes:

1) Use xxhash as the hash table's hash function. xxhash is a fast, high-quality hashing function.

2) Feed the hashing function with not just tb_phys, but also pc and flags.

r~

#25 lore raw

PATCH v2 12/13 qht: add test program

In reply to Emilio G. Cota
---
 tests/.gitignore |   1 +
 tests/Makefile   |   6 ++-
 tests/test-qht.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100644 tests/test-qht.c
tests/.gitignore +1 -0
index b7bf13e..d6d0700 100644
@@ -47,6 +47,7 @@ test-qapi-visit.[ch]
 test-qdev-global-props
 test-qemu-opts
 test-qga
+test-qht
 test-qmp-commands
 test-qmp-commands.h
 test-qmp-event
tests/Makefile +5 -1
index 45b9048..b5a99d6 100644
@@ -68,6 +68,8 @@ check-unit-y += tests/rcutorture$(EXESUF)
 gcov-files-rcutorture-y = util/rcu.c
 check-unit-y += tests/test-rcu-list$(EXESUF)
 gcov-files-test-rcu-list-y = util/rcu.c
+check-unit-y += tests/test-qht$(EXESUF)
+gcov-files-test-qht-y = util/qht.c
 check-unit-y += tests/test-bitops$(EXESUF)
 check-unit-$(CONFIG_HAS_GLIB_SUBPROCESS_TESTS) += tests/test-qdev-global-props$(EXESUF)
 check-unit-y += tests/check-qom-interface$(EXESUF)
@@ -387,7 +389,8 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/test-qmp-commands.o tests/test-visitor-serialization.o \
 	tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \
 	tests/test-opts-visitor.o tests/test-qmp-event.o \
-	tests/rcutorture.o tests/test-rcu-list.o
+	tests/rcutorture.o tests/test-rcu-list.o \
+	tests/test-qht.o
 
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
@@ -425,6 +428,7 @@ tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o
 tests/test-int128$(EXESUF): tests/test-int128.o
 tests/rcutorture$(EXESUF): tests/rcutorture.o $(test-util-obj-y)
 tests/test-rcu-list$(EXESUF): tests/test-rcu-list.o $(test-util-obj-y)
+tests/test-qht$(EXESUF): tests/test-qht.o $(test-util-obj-y)
 
 tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
 	hw/core/qdev.o hw/core/qdev-properties.o hw/core/hotplug.o\
tests/test-qht.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.5.0
#26 lore raw
In reply to Emilio G. Cota
Quoted 7 lines · Emilio G. Cota

See v1 for context:
https://lists.gnu.org/archive/html/qemu-devel/2016-04/msg00587.html

All patches in v2 are checkpatch-clean, except 05 (checkpatch should be ignored for this one) and 06, which I took unmodified (later patches fix those warnings while doing other things, anyway).

Quoted 6 lines

Note that patch 01 has already been picked up by Paolo; I left it here for completeness.

Another patch that is related to this series is the transition of tb->flags to uint32_t; this has been sent as a separate patch since it touches all targets. It shouldn't conflict with this patchset.

I've just gotten through v1 so I'll leave this for others to look at and pick up my review on v3. I have been benchmarking though and I like what I see.

Quoted 20 lines

Changes from v1:
- Drop QEMU_CACHELINE, define QEMU_ALIGNED()
+ Remove excessive caution about Windows: it supports it.
+ define QHT_BUCKET_ALIGN as 64
- Add some reviewed-by tags from Alex.
- Drop POSIX spinlock wrapper; use the one pointed out by Paolo
+ Add a couple of fixes over this spinlock implementation:
* define cpu_relax() for some architectures
* Optimize spin_lock() for uncontended cases
- Add tb_hash_func5, a version of xxhash32 customized for tb-hash, so that the input values do not have to be contiguous in memory.
+ Drop xxhash.h, add only the customized version at exec/tb-hash-xx.h
- qht
+ use size_t for counts in qht
* Drop some 2**32 size checks in there; it's not really a problem in practice.
+ s/__func/func__locked/
+ Test program: hash with tb_hash_func5

Quoted 3 lines

Thanks,

Emilio

--
Alex Bennée

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#28 lore raw

Re: [Qemu-devel] [PATCH v2 01/13] translate-all: add missing fold of tb_ctx into tcg_ctx

Quoted 8 lines · Emilio G. Cota

Since 5e5f07e08 "TCG: Move translation block variables to new context inside tcg_ctx: tb_ctx" on Feb 1 2013, compilation of usermode + TB_DEBUG_CHECK has been broken. Fix it.

---
 translate-all.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

r~