Make improvements

- Every unit test now passes on Apple Silicon. The final piece of this
  puzzle was porting our POSIX threads cancelation support, since that
  works differently on ARM64 XNU vs. AMD64. Our semaphore support on
  Apple Silicon is also superior now compared to AMD64, thanks to the
  grand central dispatch library which lets *NSYNC locks go faster.

- The Cosmopolitan runtime is now more stable, particularly on Windows.
  To do this, thread local storage is mandatory at all runtime levels,
  and the innermost packages of the C library is no longer being built
  using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
  process startup phase, and then later on the runtime re-allocates it
  either statically or dynamically to support code using _Thread_local.
  fork() and execve() now do a better job cooperating with threads. We
  can now check how much stack memory is left in the process or thread
  when functions like kprintf() / execve() etc. call alloca(), so that
  ENOMEM can be raised, reduce a buffer size, or just print a warning.

- POSIX signal emulation is now implemented the same way kernels do it
  with pthread_kill() and raise(). Any thread can interrupt any other
  thread, regardless of what it's doing. If it's blocked on read/write
  then the killer thread will cancel its i/o operation so that EINTR can
  be returned in the mark thread immediately. If it's doing a tight CPU
  bound operation, then that's also interrupted by the signal delivery.
  Signal delivery works now by suspending a thread and pushing context
  data structures onto its stack, and redirecting its execution to a
  trampoline function, which calls SetThreadContext(GetCurrentThread())
  when it's done.

- We're now doing a better job managing locks and handles. On NetBSD we
  now close semaphore file descriptors in forked children. Semaphores on
  Windows can now be canceled immediately, which means mutexes/condition
  variables will now go faster. Apple Silicon semaphores can be canceled
  too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
  syscalls are now used on XNU when appropriate to ensure pthread_cancel
  requests aren't lost. The MbedTLS library has been updated to support
  POSIX thread cancelations. See tool/build/runitd.c for an example of
  how it can be used for production multi-threaded tls servers. Handles
  on Windows now leak less often across processes. All i/o operations on
  Windows are now overlapped, which means file pointers can no longer be
  inherited across dup() and fork() for the time being.

- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
  which means, for example, that posix_spawn() now goes 3x faster. POSIX
  spawn is also now more correct. Like Musl, it's now able to report the
  failure code of execve() via a pipe although our approach favors using
  shared memory to do that on systems that have a true vfork() function.

- We now spawn a thread to deliver SIGALRM to threads when setitimer()
  is used. This enables the most precise wakeups the OS makes possible.

- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
  it turned out the kernel would actually commit the PT_GNU_STACK size
  which caused RSS to be 6mb for every process. Now it's down to ~4kb.
  On Apple Silicon, we reduce the mandatory upstream thread size to the
  smallest possible size to reduce the memory overhead of Cosmo threads.
  The examples directory has a program called greenbean which can spawn
  a web server on Linux with 10,000 worker threads and have the memory
  usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
  thread-local storage is now optional; it won't be allocated until the
  pthread_setspecific/getspecific functions are called. On Windows, the
  threads that get spawned which are internal to the libc implementation
  use reserve rather than commit memory, which shaves a few hundred kb.

- sigaltstack() is now supported on Windows, however it's currently not
  able to be used to handle stack overflows, since crash signals are
  still generated by WIN32. However the crash handler will still switch
  to the alt stack, which is helpful in environments with tiny threads.

- Test binaries are now smaller. Many of the mandatory dependencies of
  the test runner have been removed. This ensures many programs can do a
  better job only linking the the thing they're testing. This caused the
  test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb

- long double is no longer used in the implementation details of libc,
  except in the APIs that define it. The old code that used long double
  for time (instead of struct timespec) has now been thoroughly removed.

- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
  backtraces itself, it'll just print a command you can run on the shell
  using our new `cosmoaddr2line` program to view the backtrace.

- Crash report signal handling now works in a much better way. Instead
  of terminating the process, it now relies on SA_RESETHAND so that the
  default SIG_IGN behavior can terminate the process if necessary.

- Our pledge() functionality has now been fully ported to AARCH64 Linux.
This commit is contained in:
Justine Tunney 2023-09-18 20:44:45 -07:00
parent c4eb838516
commit ec480f5aa0
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
638 changed files with 7925 additions and 8282 deletions

View file

@ -33,6 +33,7 @@
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/termios.h"
#include "libc/calls/ucontext.h"
@ -42,6 +43,7 @@
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/intrin/xchg.internal.h"
#include "libc/log/check.h"
@ -88,6 +90,7 @@
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/consts/w.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/thread.h"
#include "libc/time/time.h"
#include "libc/x/xsigaction.h"
#include "third_party/getopt/getopt.internal.h"
@ -174,11 +177,11 @@ mode.\n\
*(B) = pvalloc(N); \
})
#define TIMEIT(OUT_NANOS, FORM) \
do { \
long double Start = nowl(); \
FORM; \
(OUT_NANOS) = (uint64_t)((nowl() - Start) * 1e9L); \
#define TIMEIT(OUT_NANOS, FORM) \
do { \
struct timespec Start = timespec_real(); \
FORM; \
(OUT_NANOS) = timespec_tonanos(timespec_sub(timespec_real(), Start)); \
} while (0)
typedef bool (*openspeaker_f)(void);
@ -278,9 +281,9 @@ static uint64_t t1, t2, t3, t4, t5, t6, t8;
static const char *sox_, *ffplay_, *patharg_;
static struct VtFrame vtframe_[2], *f1_, *f2_;
static struct Graphic graphic_[2], *g1_, *g2_;
static long double deadline_, dura_, starttime_;
static struct timespec deadline_, dura_, starttime_;
static bool yes_, stats_, dither_, ttymode_, istango_;
static long double decode_start_, f1_start_, f2_start_;
static struct timespec decode_start_, f1_start_, f2_start_;
static int16_t pcm_[PLM_AUDIO_SAMPLES_PER_FRAME * 2 / 8][8];
static int16_t pcmscale_[PLM_AUDIO_SAMPLES_PER_FRAME * 2 / 8][8];
static bool fullclear_, historyclear_, tuned_, yonly_, gotvideo_;
@ -308,23 +311,18 @@ static void StrikeDownCrapware(int sig) {
kill(playpid_, SIGKILL);
}
static long AsMilliseconds(long double ts) {
return rintl(ts * 1e3);
}
static long AsNanoseconds(long double ts) {
return rintl(ts * 1e9);
}
static long double GetGraceTime(void) {
return deadline_ - nowl();
static struct timespec GetGraceTime(void) {
return timespec_sub(deadline_, timespec_real());
}
static int GetNamedVector(const struct NamedVector *choices, size_t n,
const char *s) {
int i;
char name[sizeof(choices->name)];
strlcpy(name, s, sizeof(name));
#pragma GCC push_options
#pragma GCC diagnostic ignored "-Wstringop-truncation"
strncpy(name, s, sizeof(name));
#pragma GCC pop_options
strntoupper(name, sizeof(name));
for (i = 0; i < n; ++i) {
if (memcmp(choices[i].name, name, sizeof(name)) == 0) {
@ -345,7 +343,7 @@ static int GetLighting(const char *s) {
static bool CloseSpeaker(void) {
int rc, wstatus;
rc = 0;
sched_yield();
pthread_yield();
if (playfd_) {
rc |= close(playfd_);
playfd_ = -1;
@ -375,8 +373,13 @@ static void ResizeVtFrame(struct VtFrame *f, size_t yn, size_t xn) {
f->i = f->n = 0;
}
static float timespec_tofloat(struct timespec ts) {
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
static void RecordFactThatFrameWasFullyRendered(void) {
fcring_.p[fcring_.i] = nowl() - starttime_;
fcring_.p[fcring_.i] =
timespec_tofloat(timespec_sub(timespec_real(), starttime_));
fcring_.n += 1;
fcring_.i += 1;
fcring_.i &= ARRAYLEN(fcring_.p) - 1;
@ -521,7 +524,7 @@ static bool OpenSpeaker(void) {
if (ffplay_) tryspeakerfns_[i++] = TryFfplay;
if (sox_) tryspeakerfns_[i++] = TrySox;
}
snprintf(fifopath_, sizeof(fifopath_), "%s%s.%d.%d.wav", kTmpPath,
snprintf(fifopath_, sizeof(fifopath_), "%s%s.%d.%d.wav", __get_tmpdir(),
firstnonnull(program_invocation_short_name, "unknown"), getpid(),
count);
for (i = 0; i < ARRAYLEN(tryspeakerfns_); ++i) {
@ -539,7 +542,7 @@ static bool OpenSpeaker(void) {
static void OnAudio(plm_t *mpeg, plm_samples_t *samples, void *user) {
if (playfd_ != -1) {
DEBUGF("OnAudio() [grace=%,ldns]", AsNanoseconds(GetGraceTime()));
DEBUGF("OnAudio() [grace=%,ldns]", timespec_tonanos(GetGraceTime()));
CHECK_EQ(2, chans_);
CHECK_EQ(ARRAYLEN(pcm_) * 8, samples->count * chans_);
float2short(ARRAYLEN(pcm_), pcm_, (void *)samples->interleaved);
@ -549,7 +552,7 @@ static void OnAudio(plm_t *mpeg, plm_samples_t *samples, void *user) {
TryAgain:
if (WriteAudio(playfd_, pcm_, sizeof(pcm_), 1000) != -1) {
DEBUGF("WriteAudio(%d, %zu) ok [grace=%,ldns]", playfd_,
samples->count * 2, AsNanoseconds(GetGraceTime()));
samples->count * 2, timespec_tonanos(GetGraceTime()));
} else {
WARNF("WriteAudio(%d, %zu) failed: %s", playfd_, samples->count * 2,
strerror(errno));
@ -765,7 +768,7 @@ static void RasterIt(void) {
static void TranscodeVideo(plm_frame_t *pf) {
CHECK_EQ(pf->cb.width, pf->cr.width);
CHECK_EQ(pf->cb.height, pf->cr.height);
DEBUGF("TranscodeVideo() [grace=%,ldns]", AsNanoseconds(GetGraceTime()));
DEBUGF("TranscodeVideo() [grace=%,ldns]", timespec_tonanos(GetGraceTime()));
g2_ = &graphic_[1];
t5 = 0;
@ -879,8 +882,9 @@ static ssize_t WriteVideoCall(void) {
if (plm_get_audio_enabled(plm_)) {
plm_set_audio_lead_time(
plm_,
MAX(0, MIN(nowl() - f1_start_, plm_get_samplerate(plm_) /
PLM_AUDIO_SAMPLES_PER_FRAME)));
max(0,
min(timespec_tofloat(timespec_sub(timespec_real(), f1_start_)),
plm_get_samplerate(plm_) / PLM_AUDIO_SAMPLES_PER_FRAME)));
}
f1_start_ = f2_start_;
f1_->i = f1_->n = 0;
@ -904,10 +908,10 @@ static void DrainVideo(void) {
static void WriteVideo(void) {
ssize_t rc;
DEBUGF("write(tty) grace=%,ldns", AsNanoseconds(GetGraceTime()));
DEBUGF("write(tty) grace=%,ldns", timespec_tonanos(GetGraceTime()));
if ((rc = WriteVideoCall()) != -1) {
DEBUGF("write(tty) → %zd [grace=%,ldns]", rc,
AsNanoseconds(GetGraceTime()));
timespec_tonanos(GetGraceTime()));
} else if (errno == EAGAIN || errno == EINTR) {
DEBUGF("write(tty) → EINTR");
longjmp(jbi_, 1);
@ -1269,11 +1273,11 @@ static void PerformBestEffortIo(void) {
{infd_, POLLIN},
{outfd_, f1_ && f1_->n ? POLLOUT : 0},
};
pollms = MAX(0, AsMilliseconds(GetGraceTime()));
pollms = MAX(0, timespec_tomillis(GetGraceTime()));
DEBUGF("poll() ms=%,d", pollms);
if ((toto = poll(fds, ARRAYLEN(fds), pollms)) != -1) {
DEBUGF("poll() toto=%d [grace=%,ldns]", toto,
AsNanoseconds(GetGraceTime()));
timespec_tonanos(GetGraceTime()));
if (toto) {
if (fds[0].revents & (POLLIN | POLLERR)) ReadKeyboard();
if (fds[1].revents & (POLLOUT | POLLERR)) WriteVideo();
@ -1305,32 +1309,36 @@ static void HandleSignals(void) {
}
static void PrintVideo(void) {
long double decode_last, decode_end, next_tick, lag;
dura_ = MIN(MAX_FRAMERATE, 1 / plm_get_framerate(plm_));
struct timespec decode_last, decode_end, next_tick, lag;
dura_ = timespec_frommicros(min(MAX_FRAMERATE, 1 / plm_get_framerate(plm_)) *
1e6);
INFOF("framerate=%f dura=%f", plm_get_framerate(plm_), dura_);
next_tick = deadline_ = decode_last = nowl();
next_tick += dura_;
deadline_ += dura_;
next_tick = deadline_ = decode_last = timespec_real();
next_tick = timespec_add(next_tick, dura_);
deadline_ = timespec_add(deadline_, dura_);
do {
DEBUGF("plm_decode [grace=%,ldns]", AsNanoseconds(GetGraceTime()));
decode_start_ = nowl();
plm_decode(plm_, decode_start_ - decode_last);
DEBUGF("plm_decode [grace=%,ldns]", timespec_tonanos(GetGraceTime()));
decode_start_ = timespec_real();
plm_decode(plm_,
timespec_tofloat(timespec_sub(decode_start_, decode_last)));
decode_last = decode_start_;
decode_end = nowl();
lag = decode_end - decode_start_;
while (decode_end + lag > next_tick) next_tick += dura_;
deadline_ = next_tick - lag;
decode_end = timespec_real();
lag = timespec_sub(decode_end, decode_start_);
while (timespec_cmp(timespec_add(decode_end, lag), next_tick) > 0) {
next_tick = timespec_add(next_tick, dura_);
}
deadline_ = timespec_sub(next_tick, lag);
if (gotvideo_ || !plm_get_video_enabled(plm_)) {
gotvideo_ = false;
INFOF("entering printvideo event loop (lag=%,ldns, grace=%,ldns)",
AsNanoseconds(lag), AsNanoseconds(GetGraceTime()));
timespec_tonanos(lag), timespec_tonanos(GetGraceTime()));
}
do {
if (!setjmp(jbi_)) {
PerformBestEffortIo();
}
HandleSignals();
} while (AsMilliseconds(GetGraceTime()) > 0);
} while (timespec_tomillis(GetGraceTime()) > 0);
} while (plm_ && !plm_has_ended(plm_));
}
@ -1366,7 +1374,7 @@ static void PrintUsage(int rc, FILE *f) {
static void GetOpts(int argc, char *argv[]) {
int opt;
snprintf(logpath_, sizeof(logpath_), "%s%s.log", kTmpPath,
snprintf(logpath_, sizeof(logpath_), "%s%s.log", __get_tmpdir(),
firstnonnull(program_invocation_short_name, "unknown"));
while ((opt = getopt(argc, argv, "?34AGSTVYabdfhnpstxyzvL:")) != -1) {
switch (opt) {
@ -1533,6 +1541,7 @@ static void TryToOpenFrameBuffer(void) {
int main(int argc, char *argv[]) {
sigset_t wut;
const char *s;
ShowCrashReports();
gamma_ = 2.4;
volscale_ -= 2;
dither_ = true;
@ -1581,7 +1590,7 @@ int main(int argc, char *argv[]) {
if (t2 > t1) longjmp(jb_, 1);
OpenVideo();
DimensionDisplay();
starttime_ = nowl();
starttime_ = timespec_real();
PrintVideo();
}
INFOF("jb_ triggered");