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

@ -16,28 +16,36 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/errno.h"
#include "libc/stdio/stdio.h"
/**
* Reads content from stream.
* Reads line from stream.
*
* This function is similar to getline() except it'll truncate lines
* exceeding size. The line ending marker is included and may be removed
* using _chomp().
*
* @param s is output buffer
* @param size is capacity of s
* @param f is non-null file oject stream pointer
* @return s on success, NULL on error, or NULL if EOF happens when
* zero characters have been read
*/
char *fgets(char *s, int size, FILE *f) {
char *fgets_unlocked(char *s, int size, FILE *f) {
int c;
char *p;
p = s;
if (size > 0) {
while (--size > 0) {
if ((c = getc(f)) == -1) {
if (ferror(f) == EINTR) continue;
break;
if ((c = fgetc_unlocked(f)) == -1) {
if (ferror_unlocked(f) == EINTR) {
continue;
} else {
break;
}
}
*p++ = c & 0xff;
*p++ = c & 255;
if (c == '\n') break;
}
*p = '\0';