Make futexes 100x better on x86 MacOS

Thanks to @autumnjolitz (in #876) the Cosmopolitan codebase is now
acquainted with Apple's outstanding ulock system calls which offer
something much closer to futexes than Grand Central Dispatch which
wasn't quite as good, since its wait function can't be interrupted
by signals (therefore necessitating a busy loop) and it also needs
semaphore objects to be created and freed. Even though ulock is an
internal Apple API, strictly speaking, the benefits of futexes are
so great that it's worth the risk for now especially since we have
the GCD implementation still as a quick escape hatch if it changes

Here's why this change is important for x86 XNU users. Cosmo has a
suboptimal polyfill when the operating system doesn't offer an API
that let's us implement futexes properly. Sadly we had to use that
on X86 XNU until now. The polyfill works using clock_nanosleep, to
poll the futex in a busy loop with exponential backoff. On XNU x86
clock_nanosleep suffers from us not being able to use a fast clock
gettime implementation, which had a compounding effect that's made
the polyfill function even more poorly. On X86 XNU we also need to
polyfill sched_yield() using select(), which made things even more
troublesome. Now that we have futexes we don't have any busy loops
anymore for both condition variables and thread joining so optimal
performance is attained. To demonstrate, consider these benchmarks

Before:

    $ ./lockscale_test.com -b
    consumed 38.8377   seconds real time and
              0.087131 seconds cpu time

After:

    $ ./lockscale_test.com -b
    consumed 0.007955 seconds real time and
             0.011515 seconds cpu time

Fixes #876
This commit is contained in:
Justine Tunney 2023-10-03 14:47:20 -07:00
parent ff250a0c10
commit 85f64f3851
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
21 changed files with 267 additions and 34 deletions

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_futex,0x0a60531c6ffff0ca,98,4095,globl,hidden
.scall sys_futex,0x0a60531c622030ca,98,515,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_futex_cp,0x8a68539c6ffff8ca,2146,4095,globl,hidden
.scall sys_futex_cp,0x8a68539c62a038ca,2146,2563,globl,hidden

View file

@ -26,6 +26,8 @@
//
// The returned value follows the Linux kernel convention ie
// errors are returned as `-errno`, rather than -1 w/ errno.
//
// This helper should not be used to do cancelation points.
__syscall2:
#ifdef __aarch64__
mov x8,x2 // syscall number (linux)

View file

@ -26,6 +26,8 @@
//
// The return value follows the Linux Kernel (System V) ABI
// where -errno is returned, rather than doing -1 w/ errno.
//
// This helper should not be used to do cancelation points.
__syscall3:
#ifdef __aarch64__
mov x8,x3 // syscall number (linux)

53
libc/sysv/syscall4.S Normal file
View file

@ -0,0 +1,53 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
// Invokes system call w/ arity of four.
//
// This function takes four params. The first four, are for
// args passed along to the system call. The 5th is for the
// the magic number, indicating which system call is called
//
// The return value follows the Linux Kernel (System V) ABI
// where -errno is returned, rather than doing -1 w/ errno.
//
// This helper should not be used to do cancelation points.
__syscall4:
#ifdef __aarch64__
mov x8,x4 // syscall number (linux)
mov x16,x4 // syscall number (xnu)
mov x9,0 // clear carry flag
adds x9,x9,0 // clear carry flag
svc 0
bcs 1f
ret
1: neg x0,x0
ret
#elif defined(__x86_64__)
mov %rcx,%r10 // avoid intel cx clobber
mov %r8d,%eax // arg5 -> syscall number
clc // linux saves carry flag
syscall // bsds set carry on errs
jnc 1f
neg %rax // normalizes to system v
1: ret
#else
#error "unsupported architecture"
#endif
.endfn __syscall4,globl

View file

@ -100,8 +100,8 @@ scall sys_killpg 0x092fff092fffffff 0xfff globl hidden
scall sys_clone 0x11fffffffffff038 0x0dc globl hidden
scall sys_tkill 0x13e0771b121480c8 0x082 globl hidden # thr_kill() on FreeBSD; _lwp_kill() on NetBSD; thrkill() on OpenBSD where arg3 should be 0 or tcb; __pthread_kill() on XNU
scall sys_tgkill 0xffffff1e1ffff0ea 0x083 globl hidden # thr_kill2() on FreeBSD
scall sys_futex 0x0a60531c6ffff0ca 0x062 globl hidden # raises SIGSYS on NetBSD; _umtx_op() on FreeBSD
scall sys_futex_cp 0x8a68539c6ffff8ca 0x862 globl hidden # intended for futex wait ops
scall sys_futex 0x0a60531c622030ca 0x062 globl hidden # raises SIGSYS on NetBSD; _umtx_op() on FreeBSD
scall sys_futex_cp 0x8a68539c62a038ca 0x862 globl hidden # intended for futex wait ops
scall sys_set_robust_list 0x0a7ffffffffff111 0x063 globl # no wrapper
scall sys_get_robust_list 0x0a8ffffffffff112 0x064 globl # no wrapper
scall sys_uname 0x0a4fff0a4ffff03f 0x0a0 globl hidden

View file

@ -40,6 +40,7 @@ LIBC_SYSV_A_FILES := \
libc/sysv/restorert.S \
libc/sysv/syscall2.S \
libc/sysv/syscall3.S \
libc/sysv/syscall4.S \
libc/sysv/systemfive.S \
libc/sysv/sysret.c \
libc/sysv/sysv.c \
@ -170,6 +171,8 @@ o/$(MODE)/libc/sysv/syscall2.o: libc/sysv/syscall2.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/syscall3.o: libc/sysv/syscall3.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/syscall4.o: libc/sysv/syscall4.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/restorert.o: libc/sysv/restorert.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/calls/%.o: libc/sysv/calls/%.S