linux/include/asm-ppc64
Jakub Jelinek 4732efbeb9 [PATCH] FUTEX_WAKE_OP: pthread_cond_signal() speedup
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.

Acked-by: Ingo Molnar <mingo@redhat.com>
Cc: Ulrich Drepper <drepper@redhat.com>
Cc: Jamie Lokier <jamie@shareable.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-07 16:57:17 -07:00
..
iSeries [PATCH] iSeries build with newer assemblers and compilers 2005-08-16 21:06:25 -07:00
a.out.h
abs_addr.h [PATCH] ppc64: Remove CONFIG_MSCHUNKS 2005-08-29 10:53:37 +10:00
atomic.h
bitops.h
bootinfo.h
btext.h
bug.h [PATCH] ppc64: Fix issue with gcc 4.0 compiled kernels 2005-08-26 19:37:11 -07:00
byteorder.h [PATCH] headers: enable ppc64 ___arch__swab16 and ___arch__swab32 2005-06-28 21:20:32 -07:00
cache.h
cacheflush.h
checksum.h
compat.h [PATCH] compat: introduce compat_time_t 2005-06-23 09:45:32 -07:00
cputable.h [PATCH] ppc64: Move oprofile_model into cpu feature struct 2005-09-06 16:09:21 +10:00
current.h
dbdma.h
delay.h
dma-mapping.h
dma.h [PATCH] ppc64 iSeries: allow build with no PCI 2005-06-21 18:46:31 -07:00
eeh.h
elf.h [PATCH] ppc64: Fix PER_LINUX32 behaviour 2005-06-08 16:24:15 -07:00
fcntl.h
firmware.h [PATCH] ppc64: introduce FW_FEATURE_ISERIES 2005-08-29 10:53:35 +10:00
floppy.h
futex.h [PATCH] FUTEX_WAKE_OP: pthread_cond_signal() speedup 2005-09-07 16:57:17 -07:00
hardirq.h
hvcall.h
hvconsole.h [PATCH] hvc_console: Register ops when setting up hvc_console 2005-07-07 18:23:39 -07:00
hvcserver.h
hw_irq.h
ide.h
imalloc.h [PATCH] Four level pagetables for ppc64 2005-08-29 10:53:31 +10:00
io.h
iommu.h [PATCH] ppc64: move iSeries vio iommu init 2005-08-29 10:53:32 +10:00
ipcbuf.h
irq.h
kdebug.h [PATCH] RCU: clean up a few remaining synchronize_kernel() calls 2005-06-25 16:24:38 -07:00
kexec.h [PATCH] ppc64: kexec support for ppc64 2005-06-25 16:24:51 -07:00
keylargo.h
kmap_types.h
kprobes.h [PATCH] Return probe redesign: ppc64 specific implementation 2005-06-27 15:23:53 -07:00
lmb.h [PATCH] SPARSEMEM EXTREME 2005-09-05 00:05:38 -07:00
lppaca.h [PATCH] ppc64: Add VMX save flag to VPA 2005-09-05 00:06:01 -07:00
machdep.h [PATCH] ppc64: Take udbg out of ppc_md 2005-09-06 16:07:36 +10:00
macio.h
memory.h [PATCH] FUTEX_WAKE_OP: pthread_cond_signal() speedup 2005-09-07 16:57:17 -07:00
mmu_context.h
mmu.h [PATCH] Invert sense of SLB class bit 2005-09-06 16:57:46 +10:00
mmzone.h [PATCH] ppc64: sparsemem memory model 2005-06-23 09:45:06 -07:00
msgbuf.h
naca.h [PATCH] Remove NACA fixed address constraint 2005-08-29 10:53:33 +10:00
numnodes.h
nvram.h [PATCH] ppc64: add a minimal nvram driver 2005-06-23 09:43:31 +10:00
of_device.h
oprofile_impl.h [PATCH] ppc64: Move oprofile_impl.h into include/asm-ppc64 2005-09-06 16:09:21 +10:00
paca.h [PATCH] ppc64: Simplify counting of lpevents, remove lpevent_count from paca 2005-06-30 15:16:09 +10:00
page.h [PATCH] mm: consolidate get_order 2005-09-05 00:05:39 -07:00
param.h [PATCH] ppc64: Add CONFIG_HZ 2005-08-30 13:40:02 +10:00
parport.h
pci-bridge.h
pci.h [PATCH] pci and yenta: pcibios_bus_to_resource 2005-08-04 21:32:46 -07:00
pgalloc.h [PATCH] Four level pagetables for ppc64 2005-08-29 10:53:31 +10:00
pgtable.h [PATCH] ppc64: four level pagetables fix 2005-08-30 12:08:10 +10:00
plpar_wrappers.h
pmac_feature.h
pmac_low_i2c.h
pmc.h [PATCH] ppc64: Move ppc64_enable_pmcs() logic into a ppc_md function 2005-08-29 10:53:38 +10:00
posix_types.h
ppc32.h [PATCH] compat: introduce compat_time_t 2005-06-23 09:45:32 -07:00
ppc_asm.h
ppcdebug.h
processor.h [PATCH] oprofile PVR 970MP 2005-08-30 13:38:19 +10:00
prom.h [PATCH] flattened device tree changes 2005-08-29 10:53:31 +10:00
pSeries_reconfig.h
ptrace-common.h
ptrace.h
rtas.h [PATCH] ppc64: pSeries_progress -> rtas_progress 2005-06-23 09:43:28 +10:00
rwsem.h
scatterlist.h
seccomp.h
sections.h
segment.h
semaphore.h
serial.h
setup.h
sigcontext.h
signal.h
smp.h [PATCH] ppc64: add BPA platform type 2005-06-23 09:43:37 +10:00
smu.h
sparsemem.h [PATCH] ppc64: sparsemem memory model 2005-06-23 09:45:06 -07:00
spinlock.h
sstep.h
stat.h
statfs.h
system.h [PATCH] ppc64: speedup cmpxchg 2005-09-06 16:07:53 +10:00
systemcfg.h
thread_info.h [PATCH] streamline preempt_count type across archs 2005-06-23 09:45:19 -07:00
time.h [PATCH] ppc64: consolidate calibrate_decr implementations 2005-06-23 09:43:07 +10:00
timex.h
tlb.h
tlbflush.h
topology.h [PATCH] ppc64: topology API fix 2005-08-01 21:38:01 -07:00
types.h [PATCH] sab: consolidate kmem_bufctl_t 2005-09-05 00:05:48 -07:00
uaccess.h
ucontext.h
udbg.h [PATCH] ppc64: Consolidate early console and PPCDBG code 2005-09-06 16:07:37 +10:00
uninorth.h
unistd.h [PATCH] ppc64: inotify syscalls 2005-07-30 10:14:46 -07:00
user.h
vdso.h
vga.h
vio.h [PATCH] Make MODULE_DEVICE_TABLE work for vio devices 2005-08-30 13:31:56 +10:00
xics.h [PATCH] ppc64: fix for kexec boot issue 2005-08-04 13:00:55 -07:00