mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
Rewrite memory manager
Actually Portable Executable now supports Android. Cosmo's old mmap code required a 47 bit address space. The new implementation is very agnostic and supports both smaller address spaces (e.g. embedded) and even modern 56-bit PML5T paging for x86 which finally came true on Zen4 Threadripper Cosmopolitan no longer requires UNIX systems to observe the Windows 64kb granularity; i.e. sysconf(_SC_PAGE_SIZE) will now report the host native page size. This fixes a longstanding POSIX conformance issue, concerning file mappings that overlap the end of file. Other aspects of conformance have been improved too, such as the subtleties of address assignment and and the various subtleties surrounding MAP_FIXED and MAP_FIXED_NOREPLACE On Windows, mappings larger than 100 megabytes won't be broken down into thousands of independent 64kb mappings. Support for MAP_STACK is removed by this change; please use NewCosmoStack() instead. Stack overflow avoidance is now being implemented using the POSIX thread APIs. Please use GetStackBottom() and GetStackAddr(), instead of the old error-prone GetStackAddr() and HaveStackMemory() APIs which are removed.
This commit is contained in:
parent
7f6d0b8709
commit
6ffed14b9c
150 changed files with 1893 additions and 5634 deletions
|
@ -171,7 +171,6 @@ __static_yoink("blink_xnu_aarch64"); // is apple silicon
|
|||
|
||||
#define VERSION 0x020200
|
||||
#define HASH_LOAD_FACTOR /* 1. / */ 4
|
||||
#define MONITOR_MICROS 150000
|
||||
#define READ(F, P, N) readv(F, &(struct iovec){P, N}, 1)
|
||||
#define WRITE(F, P, N) writev(F, &(struct iovec){P, N}, 1)
|
||||
#define AppendCrlf(P) mempcpy(P, "\r\n", 2)
|
||||
|
@ -477,7 +476,6 @@ static int oldloglevel;
|
|||
static int messageshandled;
|
||||
static int sslticketlifetime;
|
||||
static uint32_t clientaddrsize;
|
||||
static atomic_int terminatemonitor;
|
||||
|
||||
static char *brand;
|
||||
static size_t zsize;
|
||||
|
@ -501,13 +499,11 @@ static struct pollfd *polls;
|
|||
static size_t payloadlength;
|
||||
static int64_t cacheseconds;
|
||||
static char *cachedirective;
|
||||
static const char *monitortty;
|
||||
static struct Strings stagedirs;
|
||||
static struct Strings hidepaths;
|
||||
static const char *launchbrowser;
|
||||
static const char ctIdx = 'c'; // a pseudo variable to get address of
|
||||
|
||||
static pthread_t monitorth;
|
||||
static struct Buffer inbuf_actual;
|
||||
static struct Buffer inbuf;
|
||||
static struct Buffer oldin;
|
||||
|
@ -5024,7 +5020,7 @@ static int LuaProgramTokenBucket(lua_State *L) {
|
|||
VERBOSEF("(token) please run the blackholed program; see our website!");
|
||||
}
|
||||
}
|
||||
tokenbucket.b = _mapshared(ROUNDUP(1ul << cidr, FRAMESIZE));
|
||||
tokenbucket.b = _mapshared(ROUNDUP(1ul << cidr, __granularity()));
|
||||
memset(tokenbucket.b, 127, 1ul << cidr);
|
||||
tokenbucket.cidr = cidr;
|
||||
tokenbucket.reject = reject;
|
||||
|
@ -6676,13 +6672,6 @@ static int ExitWorker(void) {
|
|||
isexitingworker = true;
|
||||
return eintr();
|
||||
}
|
||||
if (monitortty) {
|
||||
terminatemonitor = true;
|
||||
if (monitorth) {
|
||||
pthread_join(monitorth, 0);
|
||||
monitorth = 0;
|
||||
}
|
||||
}
|
||||
LuaDestroy();
|
||||
_Exit(0);
|
||||
}
|
||||
|
@ -6715,169 +6704,6 @@ static int EnableSandbox(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static void *MemoryMonitor(void *arg) {
|
||||
static struct termios oldterm;
|
||||
static int tty;
|
||||
sigset_t ss;
|
||||
bool ok;
|
||||
size_t intervals;
|
||||
struct winsize ws;
|
||||
unsigned char rez;
|
||||
struct termios term;
|
||||
char *b, *addr;
|
||||
struct MemoryInterval *mi, *mi2;
|
||||
long i, j, gen, pages;
|
||||
int rc, id, color, color2, workers;
|
||||
id = atomic_load_explicit(&shared->workers, memory_order_relaxed);
|
||||
DEBUGF("(memv) started for pid %d on tid %d", getpid(), gettid());
|
||||
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, SIGHUP);
|
||||
sigaddset(&ss, SIGINT);
|
||||
sigaddset(&ss, SIGQUIT);
|
||||
sigaddset(&ss, SIGTERM);
|
||||
sigaddset(&ss, SIGPIPE);
|
||||
sigaddset(&ss, SIGUSR1);
|
||||
sigaddset(&ss, SIGUSR2);
|
||||
sigprocmask(SIG_BLOCK, &ss, 0);
|
||||
|
||||
pthread_spin_lock(&shared->montermlock);
|
||||
if (!id) {
|
||||
if ((tty = open(monitortty, O_RDWR | O_NOCTTY)) != -1) {
|
||||
tcgetattr(tty, &oldterm);
|
||||
term = oldterm;
|
||||
term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||
term.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||
term.c_oflag |= OPOST | ONLCR;
|
||||
term.c_iflag |= IUTF8;
|
||||
term.c_cflag |= CS8;
|
||||
term.c_cc[VMIN] = 1;
|
||||
term.c_cc[VTIME] = 0;
|
||||
tcsetattr(tty, TCSANOW, &term);
|
||||
WRITE(tty, "\e[?25l", 6);
|
||||
}
|
||||
}
|
||||
pthread_spin_unlock(&shared->montermlock);
|
||||
|
||||
if (tty != -1) {
|
||||
for (gen = 0, mi = 0, b = 0; !terminatemonitor;) {
|
||||
workers = atomic_load_explicit(&shared->workers, memory_order_relaxed);
|
||||
if (id)
|
||||
id = MAX(1, MIN(id, workers));
|
||||
if (!id && workers) {
|
||||
usleep(50000);
|
||||
continue;
|
||||
}
|
||||
|
||||
++gen;
|
||||
intervals = atomic_load_explicit(&_mmi.i, memory_order_relaxed);
|
||||
if ((mi2 = realloc(mi, (intervals += 3) * sizeof(*mi)))) {
|
||||
mi = mi2;
|
||||
mi[0].x = (intptr_t)__executable_start >> 16;
|
||||
mi[0].size = _etext - __executable_start;
|
||||
mi[0].flags = 0;
|
||||
mi[1].x = (intptr_t)_etext >> 16;
|
||||
mi[1].size = _edata - _etext;
|
||||
mi[1].flags = 0;
|
||||
mi[2].x = (intptr_t)_edata >> 16;
|
||||
mi[2].size = _end - _edata;
|
||||
mi[2].flags = 0;
|
||||
__mmi_lock();
|
||||
if (_mmi.i == intervals - 3) {
|
||||
memcpy(mi + 3, _mmi.p, _mmi.i * sizeof(*mi));
|
||||
ok = true;
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
__mmi_unlock();
|
||||
if (!ok) {
|
||||
VERBOSEF("(memv) retrying due to contention on mmap table");
|
||||
continue;
|
||||
}
|
||||
|
||||
ws.ws_col = 80;
|
||||
ws.ws_row = 40;
|
||||
tcgetwinsize(tty, &ws);
|
||||
|
||||
appendr(&b, 0);
|
||||
appends(&b, "\e[H\e[1m");
|
||||
|
||||
for (i = 0; i < intervals; ++i) {
|
||||
addr = (char *)((int64_t)((uint64_t)mi[i].x << 32) >> 16);
|
||||
color = 0;
|
||||
appendf(&b, "\e[0m%lx", addr);
|
||||
int pagesz = getauxval(AT_PAGESZ);
|
||||
pages = (mi[i].size + pagesz - 1) / pagesz;
|
||||
for (j = 0; j < pages; ++j) {
|
||||
rc = mincore(addr + j * pagesz, pagesz, &rez);
|
||||
if (!rc) {
|
||||
if (rez & 1) {
|
||||
if (mi[i].flags & MAP_SHARED) {
|
||||
color2 = 105;
|
||||
} else {
|
||||
color2 = 42;
|
||||
}
|
||||
} else {
|
||||
color2 = 41;
|
||||
}
|
||||
} else {
|
||||
errno = 0;
|
||||
color2 = 0;
|
||||
}
|
||||
if (color != color2) {
|
||||
color = color2;
|
||||
appendf(&b, "\e[%dm", color);
|
||||
}
|
||||
if (mi[i].flags & MAP_ANONYMOUS) {
|
||||
appendw(&b, ' ');
|
||||
} else {
|
||||
appendw(&b, '/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
appendf(&b,
|
||||
"\e[0m ID=%d PID=%d WS=%dx%d WORKERS=%d MODE=" MODE
|
||||
" GEN=%ld\e[J",
|
||||
id, getpid(), ws.ws_col, ws.ws_row, workers, gen);
|
||||
|
||||
pthread_spin_lock(&shared->montermlock);
|
||||
WRITE(tty, b, appendz(b).i);
|
||||
appendr(&b, 0);
|
||||
usleep(MONITOR_MICROS);
|
||||
pthread_spin_unlock(&shared->montermlock);
|
||||
} else {
|
||||
// running out of memory temporarily is a real possibility here
|
||||
// the right thing to do, is stand aside and let lua try to fix
|
||||
WARNF("(memv) we require more vespene gas");
|
||||
usleep(MONITOR_MICROS);
|
||||
}
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
appendr(&b, 0);
|
||||
appends(&b, "\e[H\e[J\e[?25h");
|
||||
WRITE(tty, b, appendz(b).i);
|
||||
tcsetattr(tty, TCSANOW, &oldterm);
|
||||
}
|
||||
|
||||
DEBUGF("(memv) exiting...");
|
||||
close(tty);
|
||||
free(mi);
|
||||
free(b);
|
||||
}
|
||||
|
||||
DEBUGF("(memv) done");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void MonitorMemory(void) {
|
||||
errno_t err;
|
||||
if ((err = pthread_create(&monitorth, 0, MemoryMonitor, 0))) {
|
||||
WARNF("(memv) failed to start memory monitor %s", strerror(err));
|
||||
}
|
||||
}
|
||||
|
||||
static int HandleConnection(size_t i) {
|
||||
uint32_t ip;
|
||||
int pid, tok, rc = 0;
|
||||
|
@ -6939,9 +6765,6 @@ static int HandleConnection(size_t i) {
|
|||
} else {
|
||||
switch ((pid = fork())) {
|
||||
case 0:
|
||||
if (!IsTiny() && monitortty) {
|
||||
MonitorMemory();
|
||||
}
|
||||
meltdown = false;
|
||||
__isworker = true;
|
||||
connectionclose = false;
|
||||
|
@ -7445,7 +7268,6 @@ static void GetOpts(int argc, char *argv[]) {
|
|||
CASE('h', PrintUsage(1, EXIT_SUCCESS));
|
||||
CASE('M', ProgramMaxPayloadSize(ParseInt(optarg)));
|
||||
#if !IsTiny()
|
||||
CASE('W', monitortty = optarg);
|
||||
case 'f':
|
||||
funtrace = true;
|
||||
if (ftrace_install() == -1) {
|
||||
|
@ -7517,7 +7339,7 @@ void RedBean(int argc, char *argv[]) {
|
|||
heartbeatinterval.tv_sec = 5;
|
||||
CHECK_GT(CLK_TCK, 0);
|
||||
CHECK_NE(MAP_FAILED,
|
||||
(shared = mmap(NULL, ROUNDUP(sizeof(struct Shared), FRAMESIZE),
|
||||
(shared = mmap(NULL, ROUNDUP(sizeof(struct Shared), __granularity()),
|
||||
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,
|
||||
-1, 0)));
|
||||
if (daemonize) {
|
||||
|
@ -7578,14 +7400,6 @@ void RedBean(int argc, char *argv[]) {
|
|||
inbuf = inbuf_actual;
|
||||
isinitialized = true;
|
||||
CallSimpleHookIfDefined("OnServerStart");
|
||||
if (!IsTiny()) {
|
||||
if (monitortty && (daemonize || uniprocess)) {
|
||||
monitortty = 0;
|
||||
}
|
||||
if (monitortty) {
|
||||
MonitorMemory();
|
||||
}
|
||||
}
|
||||
#ifdef STATIC
|
||||
EventLoop(timespec_tomillis(heartbeatinterval));
|
||||
#else
|
||||
|
@ -7596,13 +7410,6 @@ void RedBean(int argc, char *argv[]) {
|
|||
}
|
||||
#endif
|
||||
if (!isexitingworker) {
|
||||
if (!IsTiny()) {
|
||||
terminatemonitor = true;
|
||||
if (monitorth) {
|
||||
pthread_join(monitorth, 0);
|
||||
monitorth = 0;
|
||||
}
|
||||
}
|
||||
HandleShutdown();
|
||||
CallSimpleHookIfDefined("OnServerStop");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue