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

@ -174,15 +174,12 @@ static int PrintBacktrace(int fd, const struct StackFrame *bp) {
void ShowBacktrace(int fd, const struct StackFrame *bp) {
#ifdef __FNO_OMIT_FRAME_POINTER__
/* asan runtime depends on this function */
int st, ft;
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);
if (!bp) bp = __builtin_frame_address(0);
PrintBacktrace(fd, bp);
__strace = st;
g_ftrace = ft;
__atomic_fetch_add(&__strace, 1, __ATOMIC_RELAXED);
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED);
#else
(fprintf)(stderr, "ShowBacktrace() needs these flags to show C backtrace:\n"
"\t-D__FNO_OMIT_FRAME_POINTER__\n"

View file

@ -1,6 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_LOG_LOG_H_
#define COSMOPOLITAN_LIBC_LOG_LOG_H_
#include "libc/bits/likely.h"
#include "libc/bits/weaken.h"
#include "libc/calls/struct/rusage.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/winsize.h"
@ -77,23 +78,24 @@ extern unsigned __log_level; /* log level for runtime check */
// log a message with the specified log level (not checking if LOGGABLE)
#define LOGF(LEVEL, FMT, ...) \
do { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(LEVEL, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} while (0)
// die with an error message without backtrace and debugger invocation
#define DIEF(FMT, ...) \
do { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(kLogError, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
if (weaken(__die)) weaken(__die)(); \
exit(1); \
unreachable; \
} while (0)
#define FATALF(FMT, ...) \
do { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
ffatalf(kLogFatal, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
unreachable; \
} while (0)
@ -101,78 +103,78 @@ extern unsigned __log_level; /* log level for runtime check */
#define ERRORF(FMT, ...) \
do { \
if (LOGGABLE(kLogError)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(kLogError, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define WARNF(FMT, ...) \
do { \
if (LOGGABLE(kLogWarn)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(kLogWarn, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define INFOF(FMT, ...) \
do { \
if (LOGGABLE(kLogInfo)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(kLogInfo, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define VERBOSEF(FMT, ...) \
do { \
if (LOGGABLE(kLogVerbose)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
fverbosef(kLogVerbose, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define DEBUGF(FMT, ...) \
do { \
if (UNLIKELY(LOGGABLE(kLogDebug))) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
fdebugf(kLogDebug, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define NOISEF(FMT, ...) \
do { \
if (UNLIKELY(LOGGABLE(kLogNoise))) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
fnoisef(kLogNoise, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define FLOGF(F, FMT, ...) \
do { \
if (LOGGABLE(kLogInfo)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(kLogInfo, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define FWARNF(F, FMT, ...) \
do { \
if (LOGGABLE(kLogWarn)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
flogf(kLogWarn, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define FFATALF(F, FMT, ...) \
do { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
ffatalf(kLogFatal, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
unreachable; \
} while (0)
@ -180,18 +182,18 @@ extern unsigned __log_level; /* log level for runtime check */
#define FDEBUGF(F, FMT, ...) \
do { \
if (UNLIKELY(LOGGABLE(kLogDebug))) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
fdebugf(kLogDebug, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
#define FNOISEF(F, FMT, ...) \
do { \
if (UNLIKELY(LOGGABLE(kLogNoise))) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
fnoisef(kLogNoise, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
} \
} while (0)
@ -204,25 +206,25 @@ extern unsigned __log_level; /* log level for runtime check */
int e = errno; \
autotype(FORM) Ax = (FORM); \
if (UNLIKELY(Ax == (typeof(Ax))(-1)) && LOGGABLE(kLogWarn)) { \
--g_ftrace; \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
__logerrno(__FILE__, __LINE__, #FORM); \
++g_ftrace; \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
errno = e; \
} \
Ax; \
})
#define LOGIFNULL(FORM) \
({ \
int e = errno; \
autotype(FORM) Ax = (FORM); \
if (Ax == NULL && LOGGABLE(kLogWarn)) { \
--g_ftrace; \
__logerrno(__FILE__, __LINE__, #FORM); \
++g_ftrace; \
errno = e; \
} \
Ax; \
#define LOGIFNULL(FORM) \
({ \
int e = errno; \
autotype(FORM) Ax = (FORM); \
if (Ax == NULL && LOGGABLE(kLogWarn)) { \
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
__logerrno(__FILE__, __LINE__, #FORM); \
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
errno = e; \
} \
Ax; \
})
/*───────────────────────────────────────────────────────────────────────────│─╗

View file

@ -275,10 +275,10 @@ static wontreturn relegated noinstrument void __minicrash(int sig,
relegated noinstrument void __oncrash(int sig, struct siginfo *si,
ucontext_t *ctx) {
intptr_t rip;
int gdbpid, err, st, ft;
int gdbpid, err;
static bool noreentry, notpossible;
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);
if (_lockcmpxchg(&noreentry, false, true)) {
if (!__vforked) {
rip = ctx ? ctx->uc_mcontext.rip : 0;
@ -306,9 +306,7 @@ relegated noinstrument void __oncrash(int sig, struct siginfo *si,
}
} else if (sig == SIGTRAP) {
/* chances are IsDebuggerPresent() confused strace w/ gdb */
g_ftrace = ft;
__strace = st;
return;
goto ItsATrap;
} else if (_lockcmpxchg(&notpossible, false, true)) {
__minicrash(sig, si, ctx, "WHILE CRASHING");
} else {
@ -317,5 +315,7 @@ relegated noinstrument void __oncrash(int sig, struct siginfo *si,
}
}
noreentry = false;
++g_ftrace;
ItsATrap:
__atomic_fetch_add(&__strace, 1, __ATOMIC_RELAXED);
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED);
}

View file

@ -26,6 +26,7 @@
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/spinlock.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
#include "libc/math.h"
@ -40,6 +41,7 @@
#define kNontrivialSize (8 * 1000 * 1000)
static struct timespec vflogf_ts;
_Alignas(64) static char vflogf_lock;
/**
* Takes corrective action if logging is on the fritz.
@ -77,17 +79,18 @@ void vflogf_onfail(FILE *f) {
*/
void(vflogf)(unsigned level, const char *file, int line, FILE *f,
const char *fmt, va_list va) {
int bufmode;
struct tm tm;
long double t2;
int st, bufmode;
const char *prog;
bool issamesecond;
char buf32[32];
int64_t secs, nsec, dots;
if (!f) f = __log_file;
if (!f) return;
st = __strace;
__strace = 0;
_spinlock(&vflogf_lock);
__atomic_fetch_sub(&__strace, 1, __ATOMIC_RELAXED);
t2 = nowl();
secs = t2;
nsec = (t2 - secs) * 1e9L;
@ -95,11 +98,13 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
dots = issamesecond ? nsec - vflogf_ts.tv_nsec : nsec;
vflogf_ts.tv_sec = secs;
vflogf_ts.tv_nsec = nsec;
localtime_r(&secs, &tm);
strcpy(iso8601(buf32, &tm), issamesecond ? "+" : ".");
prog = basename(firstnonnull(program_invocation_name, "unknown"));
bufmode = f->bufmode;
if (bufmode == _IOLBF) f->bufmode = _IOFBF;
if ((fprintf)(f, "%r%c%s%06ld:%s:%d:%.*s:%d] ", "FEWIVDNT"[level & 7], buf32,
rem1000000int64(div1000int64(dots)), file, line,
strchrnul(prog, '.') - prog, prog, getpid()) <= 0) {
@ -111,13 +116,17 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
f->bufmode = _IOLBF;
fflush(f);
}
if (level == kLogFatal) {
__start_fatal(file, line);
strcpy(buf32, "unknown");
gethostname(buf32, sizeof(buf32));
(dprintf)(STDERR_FILENO, "fatality %s pid %d\n", buf32, getpid());
_spunlock(&vflogf_lock);
__die();
unreachable;
}
__strace = st;
__atomic_fetch_add(&__strace, 1, __ATOMIC_RELAXED);
_spunlock(&vflogf_lock);
}