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

@ -19,6 +19,7 @@
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/spinlock.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
@ -26,19 +27,7 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
/**
* Reads string from stream.
*
* @param s is the caller's buffer (in/out) which is extended or
* allocated automatically, also NUL-terminated is guaranteed
* @param n is the capacity of s (in/out)
* @param delim is the stop char (and NUL is implicitly too)
* @return number of bytes read >0, including delim, excluding NUL,
* or -1 w/ errno on EOF or error; see ferror() and feof()
* @note this function can't punt EINTR to caller
* @see getline(), _chomp(), gettok_r()
*/
ssize_t getdelim(char **s, size_t *n, int delim, FILE *f) {
static ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) {
char *p;
ssize_t rc;
size_t i, m;
@ -83,3 +72,23 @@ ssize_t getdelim(char **s, size_t *n, int delim, FILE *f) {
return -1;
}
}
/**
* Reads string from stream.
*
* @param s is the caller's buffer (in/out) which is extended or
* allocated automatically, also NUL-terminated is guaranteed
* @param n is the capacity of s (in/out)
* @param delim is the stop char (and NUL is implicitly too)
* @return number of bytes read >0, including delim, excluding NUL,
* or -1 w/ errno on EOF or error; see ferror() and feof()
* @note this function can't punt EINTR to caller
* @see getline(), _chomp(), gettok_r()
*/
ssize_t getdelim(char **s, size_t *n, int delim, FILE *f) {
ssize_t rc;
_spinlock(&f->lock);
rc = getdelim_unlocked(s, n, delim, f);
_spunlock(&f->lock);
return rc;
}