Make threads faster and more reliable

This change doubles the performance of thread spawning. That's thanks to
our new stack manager, which allows us to avoid zeroing stacks. It gives
us 15µs spawns rather than 30µs spawns on Linux. Also, pthread_exit() is
faster now, since it doesn't need to acquire the pthread GIL. On NetBSD,
that helps us avoid allocating too many semaphores. Even if that happens
we're now able to survive semaphores running out and even memory running
out, when allocating *NSYNC waiter objects. I found a lot more rare bugs
in the POSIX threads runtime that could cause things to crash, if you've
got dozens of threads all spawning and joining dozens of threads. I want
cosmo to be world class production worthy for 2025 so happy holidays all
This commit is contained in:
Justine Tunney 2024-12-18 04:59:02 -08:00
parent 906bd06a5a
commit 624573207e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
51 changed files with 1006 additions and 321 deletions

View file

@ -33,6 +33,7 @@ TEST_LIBC_SYSTEM_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSTEM \
LIBC_SYSV \
LIBC_TESTLIB \
@ -40,6 +41,7 @@ TEST_LIBC_SYSTEM_DIRECTDEPS = \
LIBC_X \
THIRD_PARTY_MUSL \
THIRD_PARTY_TR \
THIRD_PARTY_TZ \
TEST_LIBC_SYSTEM_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_SYSTEM_DIRECTDEPS),$($(x))))

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/itimerval.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/dce.h"
#include "libc/errno.h"
@ -31,15 +32,40 @@
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/itimer.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "libc/time.h"
FILE *f;
char buf[32];
void OnAlarm(int sig) {
}
void *LolThread(void *arg) {
return 0;
}
void SetUpOnce(void) {
testlib_enable_tmp_setup_teardown();
// give deadlock detector more information
int64_t t = 0x5cd04d0e;
localtime(&t);
pthread_t th;
pthread_create(&th, 0, LolThread, 0);
pthread_join(th, 0);
char buf[32];
sprintf(buf, "%g", 3.14);
atexit((void *)LolThread);
FILE *f = fopen("/zip/.cosmo", "r");
fgetc(f);
fclose(f);
signal(SIGALRM, OnAlarm);
struct itimerval it = {{0, 1000}, {0, 1}};
setitimer(ITIMER_REAL, &it, 0);
}
void CheckForFdLeaks(void) {

View file

@ -22,6 +22,8 @@
#include "libc/calls/struct/sched_param.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/struct/sigset.h"
#include "libc/cosmo.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
@ -40,7 +42,9 @@
#include "libc/sysv/consts/sched.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/ss.h"
#include "libc/testlib/benchmark.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/manystack.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/posixthread.internal.h"
@ -50,6 +54,10 @@
void OnUsr1(int sig, siginfo_t *si, void *vctx) {
}
void SetUpOnce(void) {
cosmo_stack_setmaxstacks((_rand64() & 7) - 1);
}
void SetUp(void) {
struct sigaction sig = {.sa_sigaction = OnUsr1, .sa_flags = SA_SIGINFO};
sigaction(SIGUSR1, &sig, 0);
@ -280,10 +288,60 @@ static void CreateDetached(void) {
ASSERT_EQ(0, pthread_attr_destroy(&attr));
}
#define LAUNCHES 10
#define LAUNCHERS 10
errno_t pthread_create2(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
for (int i = 1;; i <<= 1) {
errno_t err = pthread_create(thread, attr, start_routine, arg);
if (err != EAGAIN)
return err;
usleep(i);
}
}
static void *CreateDetachedParallelThreads(void *arg) {
for (int i = 0; i < LAUNCHES; ++i)
CreateDetached();
return 0;
}
static void CreateDetachedParallel(void) {
pthread_t th[LAUNCHERS];
for (int i = 0; i < LAUNCHERS; ++i)
ASSERT_EQ(0, pthread_create2(&th[i], 0, CreateDetachedParallelThreads, 0));
for (int i = 0; i < LAUNCHERS; ++i)
ASSERT_EQ(0, pthread_join(th[i], 0));
}
static void *CreateJoinParallelThreads(void *arg) {
for (int i = 0; i < LAUNCHES; ++i)
CreateJoin();
return 0;
}
static void CreateJoinParallel(void) {
pthread_t th[LAUNCHERS];
for (int i = 0; i < LAUNCHERS; ++i)
ASSERT_EQ(0, pthread_create2(&th[i], 0, CreateJoinParallelThreads, 0));
for (int i = 0; i < LAUNCHERS; ++i)
ASSERT_EQ(0, pthread_join(th[i], 0));
}
TEST(pthread_create, bench) {
EZBENCH2("CreateJoin", donothing, CreateJoin());
EZBENCH2("CreateDetach", donothing, CreateDetach());
EZBENCH2("CreateDetached", donothing, CreateDetached());
kprintf("cosmo_stack_getmaxstacks() = %d\n", cosmo_stack_getmaxstacks());
pthread_t msh = manystack_start();
BENCHMARK(100, 1, CreateJoin());
BENCHMARK(100, 1, CreateDetach());
usleep(10000);
pthread_decimate_np();
BENCHMARK(100, 1, CreateDetached());
usleep(10000);
pthread_decimate_np();
BENCHMARK(1, LAUNCHERS + LAUNCHERS * LAUNCHES, CreateJoinParallel());
BENCHMARK(1, LAUNCHERS + LAUNCHERS * LAUNCHES, CreateDetachedParallel());
manystack_stop(msh);
while (!pthread_orphan_np())
pthread_decimate_np();
}