-
-
Notifications
You must be signed in to change notification settings - Fork 647
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
21 changed files
with
267 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ | ||
│vi: set net ft=c ts=2 sts=2 sw=2 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/intrin/ulock.h" | ||
#include "libc/assert.h" | ||
#include "libc/calls/calls.h" | ||
#include "libc/calls/syscall_support-sysv.internal.h" | ||
#include "libc/dce.h" | ||
#include "libc/intrin/describeflags.internal.h" | ||
#include "libc/intrin/strace.internal.h" | ||
|
||
// XNU futexes | ||
// https://opensource.apple.com/source/xnu/xnu-7195.50.7.100.1/bsd/sys/ulock.h.auto.html | ||
// https://opensource.apple.com/source/xnu/xnu-3789.41.3/bsd/kern/sys_ulock.c.auto.html | ||
|
||
int sys_ulock_wait(uint32_t operation, void *addr, uint64_t value, | ||
uint32_t timeout_micros) asm("sys_futex_cp"); | ||
|
||
// returns -1 w/ errno | ||
int ulock_wait(uint32_t operation, void *addr, uint64_t value, | ||
uint32_t timeout_micros) { | ||
int rc; | ||
operation |= ULF_WAIT_CANCEL_POINT; | ||
STRACE("ulock_wait(%#x, %p, %lx, %u) → ...", operation, addr, value, | ||
timeout_micros); | ||
rc = sys_ulock_wait(operation, addr, value, timeout_micros); | ||
STRACE("ulock_wait(%#x, %p, %lx, %u) → %d% m", operation, addr, value, | ||
timeout_micros, rc); | ||
return rc; | ||
} | ||
|
||
// returns -errno | ||
int ulock_wake(uint32_t operation, void *addr, uint64_t wake_value) { | ||
int rc; | ||
rc = __syscall3i(operation, (long)addr, wake_value, 0x2000000 | 516); | ||
STRACE("ulock_wake(%#x, %p, %lx) → %s", operation, addr, wake_value, | ||
DescribeErrno(rc)); | ||
return rc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef COSMOPOLITAN_ULOCK_H_ | ||
#define COSMOPOLITAN_ULOCK_H_ | ||
#if !(__ASSEMBLER__ + __LINKER__ + 0) | ||
COSMOPOLITAN_C_START_ | ||
|
||
/* both wake and wait take one of these */ | ||
#define UL_COMPARE_AND_WAIT 1 /* multi-thread */ | ||
#define UL_UNFAIR_LOCK 2 | ||
#define UL_COMPARE_AND_WAIT_SHARED 3 /* multi-thread/process */ | ||
#define UL_UNFAIR_LOCK64_SHARED 4 | ||
#define UL_COMPARE_AND_WAIT64 5 | ||
#define UL_COMPARE_AND_WAIT64_SHARED 6 | ||
|
||
#define ULF_WAKE_ALL 0x00000100 | ||
#define ULF_WAKE_THREAD 0x00000200 /* takes wake_value */ | ||
#define ULF_WAKE_ALLOW_NON_OWNER 0x00000400 | ||
|
||
#define ULF_WAIT_WORKQ_DATA_CONTENTION 0x00010000 | ||
#define ULF_WAIT_CANCEL_POINT 0x00020000 /* raises eintr */ | ||
#define ULF_WAIT_ADAPTIVE_SPIN 0x00040000 | ||
|
||
int ulock_wake(uint32_t, void *, uint64_t); | ||
int ulock_wait(uint32_t, void *, uint64_t, uint32_t); | ||
|
||
COSMOPOLITAN_C_END_ | ||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ | ||
#endif /* COSMOPOLITAN_ULOCK_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.