Add raw memory visualization tool to redbean

This change introduces a `-W /dev/pts/1` flag to redbean. What it does
is use the mincore() system call to create a dual-screen terminal
display that lets you troubleshoot the virtual address space. This is
useful since page faults are an important thing to consider when using a
forking web server. Now we have a colorful visualization of which pages
are going to fault and which ones are resident in memory.

The memory monitor, if enabled, spawns as a thread that just outputs
ANSI codes to the second terminal in a loop. In order to make this
happen using the new clone() polyfill, stdio is now thread safe.

This change also introduces some new demo pages to redbean. It also
polishes the demos we already have, to look a bit nicer and more
presentable for the upcoming release, with better explanations too.
This commit is contained in:
Justine Tunney 2022-05-14 04:33:58 -07:00
parent 578cb21591
commit 80b211e314
106 changed files with 1483 additions and 592 deletions

View file

@ -438,7 +438,7 @@ static int CloneLinux(int (*func)(void *), char *stk, size_t stksz, int flags,
* @param flags usually has one of
* - `SIGCHLD` will delegate to fork()
* - `CLONE_VFORK|CLONE_VM|SIGCHLD` means vfork()
* - `CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND` for threads
* - `CLONE_THREAD|CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND`
* as part high bytes, and the low order byte may optionally contain
* a signal e.g. SIGCHLD, to enable parent notification on terminate
* although the signal isn't supported on non-Linux and non-NetBSD
@ -504,11 +504,11 @@ int clone(int (*func)(void *), void *stk, size_t stksz, int flags, void *arg,
// we now assume we're creating a thread
// these platforms can't do signals the way linux does
else if (!IsTiny() &&
((stksz < PAGESIZE || (stksz & (PAGESIZE - 1))) ||
(flags &
~(CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID)) !=
(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND))) {
else if (!IsTiny() && ((stksz < PAGESIZE || (stksz & (PAGESIZE - 1))) ||
(flags & ~(CLONE_SETTLS | CLONE_PARENT_SETTID |
CLONE_CHILD_SETTID)) !=
(CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES |
CLONE_SIGHAND))) {
rc = einval();
} else if (IsXnu()) {
rc = CloneXnu(func, stk, stksz, flags, arg, ptid, tls, tlssz, ctid);

View file

@ -77,6 +77,16 @@ cosmo: push %rbp
.endfn cosmo,weak
#if !IsTiny()
// Creates deterministically addressed stack we can use
//
// This helps debugging be more comprehensible, because
// when diagnosing low-level problems when error report
// isn't working, sometimes numbers are all you have to
// go on, and we can't use them if kernel hardening has
// configured that meaningful data to be randomized.
//
// Having deterministic addresses is also key to ensure
// builds, execution, and other things are reproducible
.init.start 304,_init_stack
testb IsWindows()
jnz 9f

View file

@ -137,18 +137,18 @@ textstartup void __printargs(const char *prologue) {
char **env;
sigset_t ss;
unsigned i, n;
int e, x, flags;
uintptr_t *auxp;
struct utsname uts;
struct termios termios;
int e, x, st, ft, flags;
struct AuxiliaryValue *auxinfo;
union {
char path[PATH_MAX];
struct pollfd pfds[128];
} u;
st = __strace, __strace = 0;
ft = g_ftrace, g_ftrace = 0;
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED);
__atomic_fetch_sub(&__strace, 1, __ATOMIC_RELAXED);
e = errno;
PRINT("");
@ -454,13 +454,13 @@ textstartup void __printargs(const char *prologue) {
kprintf("\n");
PRINT(" c_ispeed = %u", termios.c_ispeed);
PRINT(" c_ospeed = %u", termios.c_ospeed);
PRINT(" c_cc[VMIN] = %d", termios.c_cc[VMIN]);
PRINT(" c_cc[VTIME] = %d", termios.c_cc[VTIME]);
PRINT(" c_cc[VINTR] = CTRL-%c", CTRL(termios.c_cc[VINTR]));
PRINT(" c_cc[VQUIT] = CTRL-%c", CTRL(termios.c_cc[VQUIT]));
PRINT(" c_cc[VERASE] = CTRL-%c", CTRL(termios.c_cc[VERASE]));
PRINT(" c_cc[VKILL] = CTRL-%c", CTRL(termios.c_cc[VKILL]));
PRINT(" c_cc[VEOF] = CTRL-%c", CTRL(termios.c_cc[VEOF]));
PRINT(" c_cc[VTIME] = CTRL-%c", CTRL(termios.c_cc[VTIME]));
PRINT(" c_cc[VMIN] = CTRL-%c", CTRL(termios.c_cc[VMIN]));
PRINT(" c_cc[VSTART] = CTRL-%c", CTRL(termios.c_cc[VSTART]));
PRINT(" c_cc[VSTOP] = CTRL-%c", CTRL(termios.c_cc[VSTOP]));
PRINT(" c_cc[VSUSP] = CTRL-%c", CTRL(termios.c_cc[VSUSP]));
@ -547,7 +547,7 @@ textstartup void __printargs(const char *prologue) {
}
PRINT("");
__strace = st;
g_ftrace = ft;
__atomic_fetch_add(&__strace, 1, __ATOMIC_RELAXED);
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED);
errno = e;
}