mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Pay off more technical debt
This makes breaking changes to add underscores to many non-standard function names provided by the c library. MODE=tiny is now tinier and we now use smaller locks that are better for tiny apps in this mode. Some headers have been renamed to be in the same folder as the build package, so it'll be easier to know which build dependency is needed. Certain old misguided interfaces have been removed. Intel intrinsics headers are now listed in libc/isystem (but not in the amalgamation) to help further improve open source compatibility. Header complexity has also been reduced. Lastly, more shell scripts are now available.
This commit is contained in:
parent
b69f3d2488
commit
6f7d0cb1c3
960 changed files with 4072 additions and 4873 deletions
248
test/libc/intrin/lock_test.c
Normal file
248
test/libc/intrin/lock_test.c
Normal file
|
@ -0,0 +1,248 @@
|
|||
/*-*- 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 2022 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/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/runtime/clone.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Austere Mutex Test
|
||||
*
|
||||
* 1. Tests what happens when *NSYNC isn't linked.
|
||||
* 2. Tests what happens when TLS isn't enabled.
|
||||
*
|
||||
* If either:
|
||||
*
|
||||
* 1. malloc() isn't linked, or
|
||||
* 2. we're in MODE=tiny
|
||||
*
|
||||
* Then we use Cosmopolitan's hand-rolled tiny locks. They're not
|
||||
* scalable. They use a lot of CPU if you have lots of threads. But
|
||||
* they're wicked fast and free of bloat if your app is tiny.
|
||||
*/
|
||||
|
||||
// RAW means without *NSYNC
|
||||
// TLS means gettid() is fast
|
||||
|
||||
// PTHREAD_MUTEX_NORMAL RAW TLS took 6ns
|
||||
// PTHREAD_MUTEX_RECURSIVE RAW TLS took 12ns
|
||||
// PTHREAD_MUTEX_ERRORCHECK RAW TLS took 13ns
|
||||
// PTHREAD_MUTEX_NORMAL RAW TLS contended took 16ns (!!)
|
||||
// PTHREAD_MUTEX_RECURSIVE RAW TLS contended took 205ns
|
||||
// PTHREAD_MUTEX_ERRORCHECK RAW TLS contended took 219ns
|
||||
// PTHREAD_MUTEX_NORMAL RAW took 6ns
|
||||
// PTHREAD_MUTEX_RECURSIVE RAW took 236ns
|
||||
// PTHREAD_MUTEX_ERRORCHECK RAW took 233ns
|
||||
// PTHREAD_MUTEX_NORMAL RAW contended took 20ns (!!)
|
||||
// PTHREAD_MUTEX_RECURSIVE RAW contended took 421ns
|
||||
// PTHREAD_MUTEX_ERRORCHECK RAW contended took 435ns
|
||||
|
||||
atomic_int ready;
|
||||
atomic_int counter;
|
||||
atomic_int success;
|
||||
pthread_mutex_t mu;
|
||||
|
||||
#define ASSERT_EQ(WANT, GOT) \
|
||||
do { \
|
||||
long _want = WANT, _got = GOT; \
|
||||
if (_want != _got) \
|
||||
__assert_eq_fail(__FILE__, __LINE__, #WANT, #GOT, _want, _got); \
|
||||
} while (0)
|
||||
|
||||
void __assert_eq_fail(const char *file, int line, const char *wantstr,
|
||||
const char *gotstr, long want, long got) {
|
||||
kprintf("%s:%d: %s vs. %s was %ld vs. %ld (%s)\n", file, line, wantstr,
|
||||
gotstr, want, got, !(got & ~255) ? _strerrno(got) : "n/a");
|
||||
_Exit(1);
|
||||
}
|
||||
|
||||
double time2dbl(struct timespec t) {
|
||||
return (((double)t.tv_sec) + ((double)t.tv_nsec * 1e-9));
|
||||
}
|
||||
|
||||
char *time2str(double s) {
|
||||
static char buf[32];
|
||||
static const struct {
|
||||
const char *suffix;
|
||||
double multiplier;
|
||||
} scale[] = {
|
||||
{"ns", 1.0e-9}, //
|
||||
{"us", 1e-6}, //
|
||||
{"ms", 1e-3}, //
|
||||
{"s", 1.0}, //
|
||||
{"hr", 3600.0}, //
|
||||
};
|
||||
int i = 0;
|
||||
while (i + 1 != sizeof(scale) / sizeof(scale[0]) &&
|
||||
scale[i + 1].multiplier <= s) {
|
||||
i++;
|
||||
}
|
||||
stpcpy(FormatInt32(buf, s / scale[i].multiplier), scale[i].suffix);
|
||||
return buf;
|
||||
}
|
||||
|
||||
int Worker(void *arg) {
|
||||
int i, x;
|
||||
atomic_store(&ready, 1);
|
||||
for (i = 0; i < 10000; ++i) {
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
x = atomic_load_explicit(&counter, memory_order_relaxed);
|
||||
atomic_store_explicit(&counter, x + 1, memory_order_relaxed);
|
||||
ASSERT_EQ(x + 1, atomic_load_explicit(&counter, memory_order_relaxed));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
}
|
||||
atomic_store(&success, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TestContendedLock(const char *name, int kind) {
|
||||
char *stk;
|
||||
double ns;
|
||||
int tid, x, i, n = 10000;
|
||||
struct timespec t1, t2;
|
||||
pthread_mutexattr_t attr;
|
||||
struct CosmoTib tib = {.tib_self = &tib, .tib_self2 = &tib, .tib_tid = -1};
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, kind);
|
||||
pthread_mutex_init(&mu, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
atomic_store(&ready, 0);
|
||||
atomic_store(&success, 0);
|
||||
stk = _mapstack();
|
||||
tid = clone(Worker, stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
||||
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
|
||||
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | CLONE_SETTLS,
|
||||
0, 0, &tib, &tib.tib_tid);
|
||||
if (tid == -1) {
|
||||
kprintf("clone failed: %s\n", strerror(errno));
|
||||
_Exit(1);
|
||||
}
|
||||
while (!atomic_load(&ready)) donothing;
|
||||
t1 = _timespec_real();
|
||||
for (i = 0; i < n; ++i) {
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
x = atomic_load_explicit(&counter, memory_order_relaxed);
|
||||
atomic_store_explicit(&counter, x - 1, memory_order_relaxed);
|
||||
ASSERT_EQ(x - 1, atomic_load_explicit(&counter, memory_order_relaxed));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
}
|
||||
t2 = _timespec_real();
|
||||
while (tib.tib_tid) donothing;
|
||||
ASSERT_EQ(1, atomic_load(&success));
|
||||
ASSERT_EQ(0, atomic_load(&counter));
|
||||
_freestack(stk);
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
||||
ns = time2dbl(_timespec_sub(t2, t1)) / n;
|
||||
kprintf("%s contended took %s\n", name, time2str(ns));
|
||||
}
|
||||
|
||||
void TestUncontendedLock(const char *name, int kind) {
|
||||
double ns;
|
||||
long i, n = 10000;
|
||||
struct timespec t1, t2;
|
||||
pthread_mutex_t lock;
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, kind);
|
||||
pthread_mutex_init(&lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
t1 = _timespec_real();
|
||||
for (i = 0; i < n; ++i) {
|
||||
pthread_mutex_lock(&lock);
|
||||
pthread_mutex_unlock(&lock);
|
||||
}
|
||||
t2 = _timespec_real();
|
||||
pthread_mutex_destroy(&lock);
|
||||
ns = time2dbl(_timespec_sub(t2, t1)) / n;
|
||||
kprintf("%s took %s\n", name, time2str(ns));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
pthread_mutexattr_t attr;
|
||||
|
||||
if (_weaken(nsync_mu_lock)) {
|
||||
kprintf("*NSYNC should not be linked\n");
|
||||
_Exit(1);
|
||||
}
|
||||
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
||||
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
||||
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
ASSERT_EQ(EDEADLK, pthread_mutex_lock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
ASSERT_EQ(EPERM, pthread_mutex_unlock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
||||
|
||||
ASSERT_EQ(1, __tls_enabled);
|
||||
|
||||
TestUncontendedLock("PTHREAD_MUTEX_NORMAL RAW TLS", PTHREAD_MUTEX_NORMAL);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_RECURSIVE RAW TLS",
|
||||
PTHREAD_MUTEX_RECURSIVE);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_ERRORCHECK RAW TLS",
|
||||
PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
TestContendedLock("PTHREAD_MUTEX_NORMAL RAW TLS", PTHREAD_MUTEX_NORMAL);
|
||||
TestContendedLock("PTHREAD_MUTEX_RECURSIVE RAW TLS", PTHREAD_MUTEX_RECURSIVE);
|
||||
TestContendedLock("PTHREAD_MUTEX_ERRORCHECK RAW TLS",
|
||||
PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
__tls_enabled = 0;
|
||||
|
||||
TestUncontendedLock("PTHREAD_MUTEX_NORMAL RAW", PTHREAD_MUTEX_NORMAL);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_RECURSIVE RAW", PTHREAD_MUTEX_RECURSIVE);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_ERRORCHECK RAW", PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
TestContendedLock("PTHREAD_MUTEX_NORMAL RAW", PTHREAD_MUTEX_NORMAL);
|
||||
TestContendedLock("PTHREAD_MUTEX_RECURSIVE RAW", PTHREAD_MUTEX_RECURSIVE);
|
||||
TestContendedLock("PTHREAD_MUTEX_ERRORCHECK RAW", PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
//
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue