Mold the redbean binary to minimize page faults

This change brings page faults for precompressed static asset serving
down from 27 to 20 (or fewer) after fork. This is more of an art than
science. Hopefully Blinkenlights can visualize page faults soon.
This commit is contained in:
Justine Tunney 2021-05-03 12:09:35 -07:00
parent 2d34819779
commit e56a9d0e23
14 changed files with 978 additions and 782 deletions

9
libc/dos.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_DOS_H_
#define COSMOPOLITAN_LIBC_DOS_H_
#define DOS_DATE(YEAR, MONTH_IDX1, DAY_IDX1) \
(((YEAR)-1980) << 9 | (MONTH_IDX1) << 5 | (DAY_IDX1))
#define DOS_TIME(HOUR, MINUTE, SECOND) \
((HOUR) << 11 | (MINUTE) << 5 | (SECOND) >> 1)
#endif /* COSMOPOLITAN_LIBC_DOS_H_ */

View file

@ -23,9 +23,13 @@
#include "libc/calls/internal.h"
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/repmovsb.h"
#include "libc/macros.internal.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/rdtscp.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thunk/msabi.h"
@ -49,6 +53,7 @@
void ftrace_hook(void);
static int noreentry;
static uint64_t laststamp;
static char g_buf[512];
static const char *g_lastsymbol;
static struct SymbolTable *g_symbols;
@ -70,11 +75,14 @@ static noasan int GetNestingLevel(struct StackFrame *frame) {
* according to the System Five NexGen32e ABI.
*/
privileged noasan void ftrace(void) {
size_t i, j, nesting;
char *p;
uint64_t stamp;
const char *symbol;
struct StackFrame *frame;
size_t nesting, symbolsize;
if (!cmpxchg(&noreentry, 0, 1)) return;
if (g_symbols) {
stamp = rdtsc();
frame = __builtin_frame_address(0);
frame = frame->next;
symbol =
@ -84,31 +92,30 @@ privileged noasan void ftrace(void) {
g_symbols->count,
frame->addr - g_symbols->addr_base)]
.name_rva];
if (symbol != g_lastsymbol &&
(nesting = GetNestingLevel(frame)) * 2 < ARRAYLEN(g_buf) - 4) {
i = 2;
j = 0;
while (nesting--) {
asm volatile("" : : : "memory");
g_buf[i++] = ' ';
g_buf[i++] = ' ';
if (symbol != g_lastsymbol) {
symbolsize = strlen(symbol);
nesting = GetNestingLevel(frame);
if (2 + nesting * 2 + symbolsize + 1 + 21 + 2 <= ARRAYLEN(g_buf)) {
p = g_buf;
*p++ = '+';
*p++ = ' ';
memset(p, ' ', nesting * 2);
p += nesting * 2;
p = mempcpy(p, symbol, symbolsize);
*p++ = ' ';
p += uint64toarray_radix10((stamp - laststamp) / 3.3, p);
*p++ = '\r';
*p++ = '\n';
write(2, g_buf, p - g_buf);
}
while (i < ARRAYLEN(g_buf) - 2 && symbol[j]) {
asm volatile("" : : : "memory");
g_buf[i++] = symbol[j++];
}
g_buf[i++] = '\r';
g_buf[i++] = '\n';
write(2, g_buf, i);
}
g_lastsymbol = symbol;
laststamp = X86_HAVE(RDTSCP) ? rdtscp(0) : rdtsc();
}
noreentry = 0;
}
textstartup void ftrace_install(void) {
g_buf[0] = '+';
g_buf[1] = ' ';
if ((g_symbols = OpenSymbolTable(FindDebugBinary()))) {
__hook(ftrace_hook, g_symbols);
} else {

30
libc/str/getzipcdirsize.c Normal file
View file

@ -0,0 +1,30 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/zip.h"
/**
* Returns size of zip central directory.
*/
uint64_t GetZipCdirSize(const uint8_t *eocd) {
if (READ32LE(eocd) == kZipCdir64HdrMagic) {
return ZIP_CDIR64_SIZE(eocd);
} else {
return ZIP_CDIR_SIZE(eocd);
}
}

View file

@ -30,7 +30,16 @@
void *memmem(const void *haystack, size_t haystacklen, const void *needle,
size_t needlelen) {
size_t i, j;
const char *p;
if (!needlelen) return haystack;
if (needlelen <= haystacklen) {
p = memchr(haystack, *(const char *)needle, haystacklen);
if (needlelen == 1) return p;
if (p) {
haystacklen -= p - (const char *)haystack;
haystack = p;
}
/* TODO: make not quadratic */
for (i = 0; i < haystacklen; ++i) {
for (j = 0;; ++j) {
if (j == needlelen) return (/*unconst*/ char *)haystack + i;
@ -38,5 +47,6 @@ void *memmem(const void *haystack, size_t haystacklen, const void *needle,
if (((char *)needle)[j] != ((char *)haystack)[i + j]) break;
}
}
}
return NULL;
}

View file

@ -20,7 +20,7 @@
#include "libc/bits/bits.h"
#include "libc/str/str.h"
noasan static const char *strchr_x64(const char *p, uint64_t c) {
noasan static inline const char *strchr_x64(const char *p, uint64_t c) {
unsigned a, b;
uint64_t w, x, y;
for (c *= 0x0101010101010101;; p += 8) {

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/str/str.h"
/**
@ -30,8 +29,13 @@
*/
char *strstr(const char *haystack, const char *needle) {
size_t i;
if (!*needle) return haystack;
haystack = firstnonnull(strchr(haystack, *needle), haystack);
const char *p;
if (!needle[0]) return haystack;
if (haystack == needle) return haystack;
p = strchr(haystack, needle[0]);
if (!needle[1]) return p;
if (p) haystack = p;
/* TODO: make not quadratic */
for (;;) {
for (i = 0;;) {
if (!needle[i]) return (/*unconst*/ char *)haystack;

View file

@ -185,6 +185,7 @@ int GetZipCfileMode(const uint8_t *);
uint64_t GetZipCdirOffset(const uint8_t *);
uint64_t GetZipCdirRecords(const uint8_t *);
void *GetZipCdirComment(const uint8_t *);
uint64_t GetZipCdirSize(const uint8_t *);
uint64_t GetZipCdirCommentSize(const uint8_t *);
uint64_t GetZipCfileUncompressedSize(const uint8_t *);
uint64_t GetZipCfileCompressedSize(const uint8_t *);

View file

@ -0,0 +1,29 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(memrchr, test) {
EXPECT_STREQ(".there", memrchr("yo.hi.there", '.', 11));
}
BENCH(memrchr, bench) {
EZBENCH2("memrchr", donothing, EXPROPRIATE(memrchr("yo.hi.there", '.', 11)));
}

View file

@ -21,6 +21,8 @@
#include "libc/mem/mem.h"
#include "libc/str/internal.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
#define MakeMemory(SL) memcpy(malloc(sizeof(SL) - 1), SL, sizeof(SL) - 1)
@ -60,6 +62,22 @@ TEST(memmem, testEndOfMemory) {
free(needle);
}
TEST(memmem, testOneNo) {
char *needle = MakeMemory("z");
char *haystk = MakeMemory("abc123");
EXPECT_EQ(0, memmem(haystk, 6, needle, 1));
free(haystk);
free(needle);
}
TEST(memmem, testOneYes) {
char *needle = MakeMemory("3");
char *haystk = MakeMemory("abc123");
EXPECT_EQ(&haystk[5], memmem(haystk, 6, needle, 1));
free(haystk);
free(needle);
}
TEST(memmem, testCrossesSseRegister) {
char *needle = MakeMemory("eeeeeeeeeeeeefffffffffffff");
char *haystk = MakeMemory("eeeeeeeeeeeeeeeeffffffffffffffffrrrrrrrrrrrrrrrr");
@ -113,3 +131,12 @@ TEST(memmem, testEmptyHaystackAndNeedle_returnsHaystack) {
TEST(memmem, testWut) {
ASSERT_STREQ("x", memmem("x", 1, "x", 1));
}
BENCH(memmem, bench) {
EZBENCH2("memmem", donothing,
EXPROPRIATE(memmem(kHyperion, kHyperionSize, "THE END", 7)));
EZBENCH2("memmem", donothing,
EXPROPRIATE(memmem(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
62, "aaaaaab", 7)));
}

View file

@ -46,6 +46,12 @@ TEST(strstr, test_notFound) {
free(haystack);
}
TEST(strstr, test_notFound1) {
MAKESTRING(haystack, "abc123def");
ASSERT_EQ(NULL, strstr(haystack, gc(strdup("x"))));
free(haystack);
}
TEST(strstr, test_middleOfString) {
MAKESTRING(haystack, "abc123def");
ASSERT_STREQ(&haystack[3], strstr(haystack, gc(strdup("123"))));
@ -80,8 +86,40 @@ TEST(strstr, test) {
BENCH(strstr, bench) {
EZBENCH2("strstr", donothing, EXPROPRIATE(strstr(kHyperion, "THE END")));
EZBENCH2("strstr", donothing,
EZBENCH2("strstr torture 1", donothing,
EXPROPRIATE(strstr(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"aaaaaab")));
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"b")));
EZBENCH2("strstr torture 2", donothing,
EXPROPRIATE(strstr(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"ab")));
EZBENCH2("strstr torture 4", donothing,
EXPROPRIATE(strstr(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"aaab")));
EZBENCH2("strstr torture 8", donothing,
EXPROPRIATE(strstr(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"aaaaaaab")));
EZBENCH2("strstr torture 16", donothing,
EXPROPRIATE(strstr(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"aaaaaaaaaaaaaaab")));
EZBENCH2("strstr torture 32", donothing,
EXPROPRIATE(strstr(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")));
}

View file

@ -22,6 +22,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/timespec.h"
#include "libc/dos.h"
#include "libc/elf/def.h"
#include "libc/fmt/conv.h"
#include "libc/limits.h"
@ -55,11 +56,6 @@
#define ZIP_LOCALFILE_SECTION ".zip.2."
#define ZIP_DIRECTORY_SECTION ".zip.4."
#define DOS_DATE(YEAR, MONTH_IDX1, DAY_IDX1) \
(((YEAR)-1980) << 9 | (MONTH_IDX1) << 5 | (DAY_IDX1))
#define DOS_TIME(HOUR, MINUTE, SECOND) \
((HOUR) << 11 | (MINUTE) << 5 | (SECOND) >> 1)
char *symbol_;
char *outpath_;
char *yoink_;

94
tool/net/counters.inc Normal file
View file

@ -0,0 +1,94 @@
C(accepterrors)
C(acceptinterrupts)
C(acceptresets)
C(badlengths)
C(badmessages)
C(badmethods)
C(badranges)
C(closeerrors)
C(compressedresponses)
C(connectionshandled)
C(connectsrefused)
C(continues)
C(decompressedresponses)
C(deflates)
C(dropped)
C(dynamicrequests)
C(emfiles)
C(enetdowns)
C(enfiles)
C(enobufs)
C(enomems)
C(enonets)
C(errors)
C(expectsrefused)
C(failedchildren)
C(forbiddens)
C(forkerrors)
C(frags)
C(fumbles)
C(http09)
C(http10)
C(http11)
C(http12)
C(hugepayloads)
C(identityresponses)
C(inflates)
C(listingrequests)
C(loops)
C(mapfails)
C(maps)
C(meltdowns)
C(messageshandled)
C(missinglengths)
C(netafrinic)
C(netanonymous)
C(netapnic)
C(netapple)
C(netarin)
C(netatt)
C(netcogent)
C(netcomcast)
C(netdod)
C(netford)
C(netlacnic)
C(netloopback)
C(netother)
C(netprivate)
C(netprudential)
C(netripe)
C(nettestnet)
C(netusps)
C(notfounds)
C(notmodifieds)
C(openfails)
C(partialresponses)
C(payloaddisconnects)
C(pipelinedrequests)
C(precompressedresponses)
C(readerrors)
C(readinterrupts)
C(readresets)
C(readtimeouts)
C(redirects)
C(reindexes)
C(reloads)
C(rewrites)
C(serveroptions)
C(shutdowns)
C(slowloris)
C(slurps)
C(statfails)
C(staticrequests)
C(stats)
C(statuszrequests)
C(synchronizationfailures)
C(terminatedchildren)
C(thiscorruption)
C(transfersrefused)
C(urisrefused)
C(verifies)
C(writeerrors)
C(writeinterruputs)
C(writeresets)
C(writetimeouts)

View file

@ -1 +1,2 @@
Write('hello world\r\n')
StoreAsset('/hi', 'sup')

File diff suppressed because it is too large Load diff