Skip to contentSkip to conversation

FUTEX_WAKE_OP (pthread_cond_signal speedup)

PATCH In review Jakub Jelinek 2 participants 3 messages Aug 23, 2005 Futex

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 FUTEX_WAKE_OP (pthread_cond_signal speedup)

Hi!

ATM pthread_cond_signal is unnecessarily slow, because it wakes one waiter (which at least on UP usually means an immediate context switch to one of the waiter threads). This waiter wakes up and after a few instructions it attempts to acquire the cv internal lock, but that lock is still held by the thread calling pthread_cond_signal. So it goes to sleep and eventually the signalling thread is scheduled in, unlocks the internal lock and wakes the waiter again.

Now, before 2003-09-21 NPTL was using FUTEX_REQUEUE in pthread_cond_signal to avoid this performance issue, but it was removed when locks were redesigned to the 3 state scheme (unlocked, locked uncontended, locked contended).

Following scenario shows why simply using FUTEX_REQUEUE in pthread_cond_signal together with using lll_mutex_unlock_force in place of lll_mutex_unlock is not enough and probably why it has been disabled at that time:

The number is value in cv->__data.__lock.
thr1 thr2 thr3

0       pthread_cond_wait
1       lll_mutex_lock (cv->__data.__lock)
0       lll_mutex_unlock (cv->__data.__lock)
0       lll_futex_wait (&cv->__data.__futex, futexval)

0 pthread_cond_signal
1 lll_mutex_lock (cv->__data.__lock)
1 pthread_cond_signal
2 lll_mutex_lock (cv->__data.__lock)
2 lll_futex_wait (&cv->__data.__lock, 2)
2 lll_futex_requeue (&cv->__data.__futex, 0, 1, &cv->__data.__lock)
# FUTEX_REQUEUE, not FUTEX_CMP_REQUEUE
2 lll_mutex_unlock_force (cv->__data.__lock)
0 cv->__data.__lock = 0
0 lll_futex_wake (&cv->__data.__lock, 1)
1 lll_mutex_lock (cv->__data.__lock)
0 lll_mutex_unlock (cv->__data.__lock)
# Here, lll_mutex_unlock doesn't know there are threads waiting # on the internal cv's lock

Now, I believe it is possible to use FUTEX_REQUEUE in pthread_cond_signal, but it will cost us not one, but 2 extra syscalls and, what's worse, one of these extra syscalls will be done for every single waiting loop in pthread_cond_*wait.
We would need to use lll_mutex_unlock_force in pthread_cond_signal after requeue and lll_mutex_cond_lock in pthread_cond_*wait after lll_futex_wait.

Another alternative is to do the unlocking pthread_cond_signal needs to do (the lock can't be unlocked before lll_futex_wake, as that is racy) in the kernel.

I have implemented both variants, futex-requeue-glibc.patch is the first one and futex-wake_op{,-glibc}.patch is the unlocking inside of the kernel. The kernel interface allows userland to specify how exactly an unlocking operation should look like (some atomic arithmetic operation with optional constant argument and comparison of the previous futex value with another constant).

It has been implemented just for ppc*, x86_64 and i?86, for other architectures I'm including just a stub header which can be used as a starting point by maintainers to write support for their arches and ATM will just return -ENOSYS for FUTEX_WAKE_OP. The requeue patch has been (lightly) tested just on x86_64, the wake_op patch on ppc64 kernel running 32-bit and 64-bit NPTL and x86_64 kernel running 32-bit and 64-bit NPTL.

With the following benchmark on UP x86-64 I get:

for i in nptl-orig nptl-requeue nptl-wake_op; do echo time elf/ld.so --library-path .:$i /tmp/bench; \
for j in 1 2; do echo ( time elf/ld.so --library-path .:$i /tmp/bench ) 2>&1; done; done time elf/ld.so --library-path .:nptl-orig /tmp/bench
real 0m0.655s user 0m0.253s sys 0m0.403s
real 0m0.657s user 0m0.269s sys 0m0.388s
time elf/ld.so --library-path .:nptl-requeue /tmp/bench
real 0m0.496s user 0m0.225s sys 0m0.271s
real 0m0.531s user 0m0.242s sys 0m0.288s
time elf/ld.so --library-path .:nptl-wake_op /tmp/bench
real 0m0.380s user 0m0.176s sys 0m0.204s
real 0m0.382s user 0m0.175s sys 0m0.207s

The benchmark is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00001.txt
Older futex-requeue-glibc.patch version is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00002.txt
Older futex-wake_op-glibc.patch version is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00003.txt
Will post a new version (just x86-64 fixes so that the patch applies against pthread_cond_signal.S) to libc-hacker ml soon.

Attached is the kernel FUTEX_WAKE_OP patch as well as a simple-minded testcase that will not test the atomicity of the operation, but at least check if the threads that should have been woken up are woken up and whether the arithmetic operation in the kernel gave the expected results.

Jakub

include/linux/futex.h renamed from include/linux/futex.h.jj +31 -5
@@ -4,14 +4,40 @@
 /* Second argument to futex syscall */
 
 
-#define FUTEX_WAIT (0)
-#define FUTEX_WAKE (1)
-#define FUTEX_FD (2)
-#define FUTEX_REQUEUE (3)
-#define FUTEX_CMP_REQUEUE (4)
+#define FUTEX_WAIT		0
+#define FUTEX_WAKE		1
+#define FUTEX_FD		2
+#define FUTEX_REQUEUE		3
+#define FUTEX_CMP_REQUEUE	4
+#define FUTEX_WAKE_OP		5
 
 long do_futex(unsigned long uaddr, int op, int val,
 		unsigned long timeout, unsigned long uaddr2, int val2,
 		int val3);
 
+#define FUTEX_OP_SET		0	/* *(int *)UADDR2 = OPARG; */
+#define FUTEX_OP_ADD		1	/* *(int *)UADDR2 += OPARG; */
+#define FUTEX_OP_OR		2	/* *(int *)UADDR2 |= OPARG; */
+#define FUTEX_OP_ANDN		3	/* *(int *)UADDR2 &= ~OPARG; */
+#define FUTEX_OP_XOR		4	/* *(int *)UADDR2 ^= OPARG; */
+
+#define FUTEX_OP_OPARG_SHIFT	8	/* Use (1 << OPARG) instead of OPARG.  */
+
+#define FUTEX_OP_CMP_EQ		0	/* if (oldval == CMPARG) wake */
+#define FUTEX_OP_CMP_NE		1	/* if (oldval != CMPARG) wake */
+#define FUTEX_OP_CMP_LT		2	/* if (oldval < CMPARG) wake */
+#define FUTEX_OP_CMP_LE		3	/* if (oldval <= CMPARG) wake */
+#define FUTEX_OP_CMP_GT		4	/* if (oldval > CMPARG) wake */
+#define FUTEX_OP_CMP_GE		5	/* if (oldval >= CMPARG) wake */
+
+/* FUTEX_WAKE_OP will perform atomically
+   int oldval = *(int *)UADDR2;
+   *(int *)UADDR2 = oldval OP OPARG;
+   if (oldval CMP CMPARG)
+     wake UADDR2;  */
+
+#define FUTEX_OP(op, oparg, cmp, cmparg) \
+  (((op & 0xf) << 28) | ((cmp & 0xf) << 24)		\
+   | ((oparg & 0xfff) << 12) | (cmparg & 0xfff))
+
 #endif
kernel/futex.c renamed from kernel/futex.c.jj +116 -0

134 lines not on this page · raw message

include/asm-ppc64/futex.h renamed from include/asm-ppc64/futex.h.jj +83 -0

83 lines not on this page · raw message

include/asm-ppc64/memory.h renamed from include/asm-ppc64/memory.h.jj +2 -0
@@ -18,9 +18,11 @@
 #ifdef CONFIG_SMP
 #define EIEIO_ON_SMP	"eieio\n"
 #define ISYNC_ON_SMP	"\n\tisync"
+#define SYNC_ON_SMP	"lwsync\n\t"
 #else
 #define EIEIO_ON_SMP
 #define ISYNC_ON_SMP
+#define SYNC_ON_SMP
 #endif
 
 static inline void eieio(void)
include/asm-x86_64/futex.h renamed from include/asm-x86_64/futex.h.jj +98 -0

98 lines not on this page · raw message

include/asm-i386/futex.h renamed from include/asm-i386/futex.h.jj +108 -0

108 lines not on this page · raw message

include/asm-alpha/futex.h renamed from include/asm-alpha/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-arm/futex.h renamed from include/asm-arm/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-arm26/futex.h renamed from include/asm-arm26/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-cris/futex.h renamed from include/asm-cris/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-frv/futex.h renamed from include/asm-frv/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-h8300/futex.h renamed from include/asm-h8300/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-ia64/futex.h renamed from include/asm-ia64/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-m32r/futex.h renamed from include/asm-m32r/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-m68k/futex.h renamed from include/asm-m68k/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-m68knommu/futex.h renamed from include/asm-m68knommu/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-mips/futex.h renamed from include/asm-mips/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-parisc/futex.h renamed from include/asm-parisc/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-ppc/futex.h renamed from include/asm-ppc/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-s390/futex.h renamed from include/asm-s390/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-sh/futex.h renamed from include/asm-sh/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-sh64/futex.h renamed from include/asm-sh64/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-sparc/futex.h renamed from include/asm-sparc/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-sparc64/futex.h renamed from include/asm-sparc64/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-um/futex.h renamed from include/asm-um/futex.h.jj +53 -0

53 lines not on this page · raw message

include/asm-v850/futex.h renamed from include/asm-v850/futex.h.jj +53 -0

53 lines not on this page · raw message

#define _GNU_SOURCE 1
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#define FUTEX_WAIT 0
#define FUTEX_WAKE 1
#define FUTEX_FD 2
#define FUTEX_REQUEUE 3
#define FUTEX_CMP_REQUEUE 4
#define FUTEX_WAKE_OP 5

#define FUTEX_OP_SET 0 /* *(int *)UADDR2 = OPARG; */
#define FUTEX_OP_ADD 1 /* *(int *)UADDR2 += OPARG; */
#define FUTEX_OP_OR 2 /* *(int *)UADDR2 |= OPARG; */
#define FUTEX_OP_ANDN 3 /* *(int *)UADDR2 &= ~OPARG; */
#define FUTEX_OP_XOR 4 /* *(int *)UADDR2 ^= OPARG; */

#define FUTEX_OP_OPARG_SHIFT 8 /* Use (1 << OPARG) instead of OPARG. */

#define FUTEX_OP_CMP_EQ 0 /* if (oldval == CMPARG) wake */
#define FUTEX_OP_CMP_NE 1 /* if (oldval != CMPARG) wake */
#define FUTEX_OP_CMP_LT 2 /* if (oldval < CMPARG) wake */
#define FUTEX_OP_CMP_LE 3 /* if (oldval <= CMPARG) wake */
#define FUTEX_OP_CMP_GT 4 /* if (oldval > CMPARG) wake */
#define FUTEX_OP_CMP_GE 5 /* if (oldval >= CMPARG) wake */

/* FUTEX_WAKE_OP will perform atomically
int oldval = *(int *)UADDR2;
*(int *)UADDR2 = oldval OP OPARG;
if (oldval CMP CMPARG)
wake UADDR2; */

#define FUTEX_OP(op, oparg, cmp, cmparg) \
(((op & 0xf) << 28) | ((cmp & 0xf) << 24) \
| ((oparg & 0xfff) << 12) | (cmparg & 0xfff))

int futex;
int lock;
volatile int wake2;
pthread_barrier_t b;

#define N (5 * 2 * 6 * 20 + 1)

static void *
tf (void *arg)
{
int i;
for (i = 0; i < N; i++)

    {
      int xpect;
      pthread_barrier_wait (&b);
      xpect = arg ? lock : i;
      int ret = syscall (SYS_futex, arg ? &lock : &futex, FUTEX_WAIT,
			 xpect, 0);
      if (ret < 0 && errno == ENOSYS)
	error (EXIT_FAILURE, 0, "SYS_futex syscall FUTEX_WAIT returned ENOSYS");
      if (arg && !wake2)
	error (EXIT_FAILURE, 0, "%d: arg && !wake2", i);
      pthread_barrier_wait (&b);
      if (futex != i + 1)
	error (EXIT_FAILURE, 0, "tf%d: futex %d != i %d", arg ? 1 : 0, futex, i + 1);
    }

return NULL;
}

int
main (int argc, char **argv)
{
int verbose = (argc > 1 && strcmp (argv[1], "-v"));

  pthread_barrier_init (&b, NULL, 3);
  pthread_t th[2];
  if (pthread_create (&th[0], NULL, tf, (void *) 0L) != 0)
    error (EXIT_FAILURE, 0, "create failed");

  if (pthread_create (&th[1], NULL, tf, (void *) 1L) != 0)
    error (EXIT_FAILURE, 0, "create failed");

int i;
for (i = 0; i < N; i++)

    {
      int op, cmp, shift, cmd, cmparg, oparg, xpect;

      if (i != 0)
	{
	  cmd = i - 1;
	  op = cmd % 5;
	  cmd /= 5;
	  cmp = cmd % 6;
	  cmd /= 6;
	  shift = cmd % 2;
	  lock = (random () << 31) ^ random ();
	  oparg = (random () << 31) ^ random ();
	  cmparg = ((int) random () << 20) >> 20;
	  if (shift)
	    oparg &= 31;
	  else
	    {
	      lock = (lock << 20) >> 20;
	      oparg = (oparg << 20) >> 20;
	    }
	  cmd = FUTEX_OP (op | (shift ? FUTEX_OP_OPARG_SHIFT : 0),
			  oparg, cmp, cmparg);
	  if (shift)
	    oparg = (1 << oparg);
	  switch (cmp)
	    {
	    case FUTEX_OP_CMP_EQ: wake2 = (lock == cmparg); break;
	    case FUTEX_OP_CMP_NE: wake2 = (lock != cmparg); break;
	    case FUTEX_OP_CMP_LT: wake2 = (lock < cmparg); break;
	    case FUTEX_OP_CMP_LE: wake2 = (lock <= cmparg); break;
	    case FUTEX_OP_CMP_GT: wake2 = (lock > cmparg); break;
	    case FUTEX_OP_CMP_GE: wake2 = (lock >= cmparg); break;
	    default:
	      abort ();
	    }
	  xpect = lock;
	  switch (op)
	    {
	    case FUTEX_OP_SET: xpect = oparg; break;
	    case FUTEX_OP_ADD: xpect += oparg; break;
	    case FUTEX_OP_OR: xpect |= oparg; break;
	    case FUTEX_OP_ANDN: xpect &= ~oparg; break;
	    case FUTEX_OP_XOR: xpect ^= oparg; break;
	    default:
	      abort ();
	    }
	  if (verbose)
	    printf ("cmd %08x op %d shift %d cmp %d oparg %d cmparg %d lock %d\n",
		    (unsigned int) cmd, op, shift, cmp, oparg, cmparg, lock);
	}
      else
	{
	  xpect = lock;
	  wake2 = 0;
	}
      if (verbose)
	printf ("%d: wake2 %d\n", i, wake2);
      pthread_barrier_wait (&b);
      struct timespec ts = { 0, 10000000 };
      nanosleep (&ts, NULL);
      futex++;
      int ret;
      if (i == 0)
	ret = syscall (SYS_futex, &futex, FUTEX_WAKE, 1);
      else
	ret = syscall (SYS_futex, &futex, FUTEX_WAKE_OP, 1, 1, &lock, cmd);
      if (ret < 0 && errno == ENOSYS)
	error (EXIT_FAILURE, 0,
	       "SYS_futex syscall FUTEX_WAKE%s returned ENOSYS",
	       i ? "_OP" : "");
      if (lock != xpect)
	error (EXIT_FAILURE, 0, "%d: lock %d != xpect %d", i, lock, xpect);
      if (!wake2)
	{
	  nanosleep (&ts, NULL);
	  wake2 = 1;
	  syscall (SYS_futex, &lock, FUTEX_WAKE, 1);
	}
      pthread_barrier_wait (&b);
    }
  if (pthread_join (th[0], NULL) != 0)
    error (EXIT_FAILURE, 0, "join failed");
  if (pthread_join (th[1], NULL) != 0)
    error (EXIT_FAILURE, 0, "join failed");

return 0;
}

maintainer
#1 lore raw
Quoted 13 lines · Jakub Jelinek

Hi!

ATM pthread_cond_signal is unnecessarily slow, because it wakes one waiter (which at least on UP usually means an immediate context switch to one of the waiter threads). This waiter wakes up and after a few instructions it attempts to acquire the cv internal lock, but that lock is still held by the thread calling pthread_cond_signal. So it goes to sleep and eventually the signalling thread is scheduled in, unlocks the internal lock and wakes the waiter again.

With the following benchmark on UP x86-64 I get:

Quoted 11 lines

for i in nptl-orig nptl-requeue nptl-wake_op; do echo time elf/ld.so --library-path .:$i /tmp/bench; \
for j in 1 2; do echo ( time elf/ld.so --library-path .:$i /tmp/bench ) 2>&1; done; done time elf/ld.so --library-path .:nptl-orig /tmp/bench
real 0m0.655s user 0m0.253s sys 0m0.403s
real 0m0.657s user 0m0.269s sys 0m0.388s
time elf/ld.so --library-path .:nptl-requeue /tmp/bench
real 0m0.496s user 0m0.225s sys 0m0.271s
real 0m0.531s user 0m0.242s sys 0m0.288s
time elf/ld.so --library-path .:nptl-wake_op /tmp/bench
real 0m0.380s user 0m0.176s sys 0m0.204s
real 0m0.382s user 0m0.175s sys 0m0.207s

translation: effective thread switching is now almost twice as fast with the WAKE_OP extension of the futex interface. Cool!

a detail: many of the futex_atomic_op_inuser() seem to be duplicated across architectures. Might be worth putting into asm-generic, to avoid the duplication?

Ingo

#2 lore raw
Quoted 3 lines · Ingo Molnar

a detail: many of the futex_atomic_op_inuser() seem to be duplicated across architectures. Might be worth putting into asm-generic, to avoid the duplication?

Those are stub files waiting for arch maintainers to actually implement them, so they will be eventually different, but for the time being they just -ENOSYS, so that things compile.

Jakub