diff options
| author | 2026-10-15 17:03:03 +0330 | |
|---|---|---|
| committer | 2026-10-15 17:03:03 +0330 | |
| commit | 7cb983bbea98d2038b2a8015a6d1f1afd40991c9 (patch) | |
| tree | ec3d88a15801207fc6d8d3c6d142617ffec85460 /src/micro/port_linux.c | |
| parent | merge work/borrow: borrow returns before definition, borrows clause, borrow m... (diff) | |
| parent | bench: rss probe path fix (diff) | |
merge work/actors: wasm seed bootstrap (seed/MANIFEST), shared copy-on-write actor memory, top-level hold blocks on all backends and lsp
Diffstat (limited to 'src/micro/port_linux.c')
| -rw-r--r-- | src/micro/port_linux.c | 94 |
1 files changed, 85 insertions, 9 deletions
diff --git a/src/micro/port_linux.c b/src/micro/port_linux.c index 6245f4f..a39e3e8 100644 --- a/src/micro/port_linux.c +++ b/src/micro/port_linux.c @@ -38,16 +38,38 @@ #define VM_TLS #endif -/* the machine's memory: a block that grows by realloc, zero in the new part. the - base can move on a grow, which micro.c takes into account (it re-reads m). */ +/* the machine's memory. with threads (actors) on a posix host it is one large + reservation that grows in place, so that a worker's memory can be a + copy-on-write map of a snapshot of its parent (micro_mem_clone); a machine that + outgrows the reservation moves to a block that grows by realloc. without threads it + is a block that grows by realloc, zero in the new part. the base can move on a + grow, which micro.c takes into account (it re-reads m). */ static VM_TLS Cell *lmem; static VM_TLS UCell lcap; +#if defined(ZY_THREADS) && !defined(_WIN32) +#include <sys/mman.h> +#ifdef __linux__ +#include <sys/syscall.h> +#endif +#define RESW ((UCell)1 << 28) /* words reserved per machine: 2 GB of address space */ +static VM_TLS int lres; /* lmem is a reservation of RESW words */ +static Cell *res_map(void) { void *p = mmap(0, RESW * sizeof(Cell), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); return p == MAP_FAILED ? 0 : p; } +#endif Cell *micro_mem_alloc(UCell words) { Cell *n; if (words <= lcap && lmem) return lmem; if (words > ((UCell)-1) / sizeof(Cell)) return NULL; +#if defined(ZY_THREADS) && !defined(_WIN32) + if (!lmem && words <= RESW && (lmem = res_map())) { lres = 1; lcap = words; return lmem; } + if (lres && words <= RESW) { lcap = words; return lmem; } /* the reservation reads zero past lcap */ + if (lres) { + n = malloc(words * sizeof(Cell)); if (!n) return NULL; + memcpy(n, lmem, lcap * sizeof(Cell)); memset(n + lcap, 0, (words - lcap) * sizeof(Cell)); + munmap(lmem, RESW * sizeof(Cell)); lres = 0; lmem = n; lcap = words; return n; + } +#endif n = realloc(lmem, words * sizeof(Cell)); if (!n) return NULL; memset(n + lcap, 0, (words - lcap) * sizeof(Cell)); @@ -56,15 +78,62 @@ Cell *micro_mem_alloc(UCell words) return n; } +#if defined(ZY_THREADS) && !defined(_WIN32) +/* a worker's memory is a private map of an unchanging snapshot of its parent's, + plus the pages the parent changed since; see the same scheme in src/core/core.c */ +#define SNAP_PAGE 512 +static VM_TLS int snap_fd = -1; static VM_TLS Cell *snap_view; static VM_TLS UCell snap_words; +static void snap_drop(void) { if (snap_view) munmap(snap_view, snap_words * sizeof(Cell)); if (snap_fd >= 0) close(snap_fd); snap_fd = -1; snap_view = 0; snap_words = 0; } +static int snap_take(void) +{ + UCell words = (lcap + SNAP_PAGE - 1) / SNAP_PAGE * SNAP_PAGE; size_t bytes = words * sizeof(Cell), done = 0; int fd = -1; void *view; +#ifdef __linux__ +#ifdef SYS_memfd_create + fd = (int)syscall(SYS_memfd_create, "zym-snapshot", 1u); +#endif +#else + { char name[64]; snprintf(name, sizeof name, "/zym-%d-%p", (int)getpid(), (void *)&name); fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); if (fd >= 0) shm_unlink(name); } +#endif + if (fd < 0) return -1; + if (ftruncate(fd, (off_t)bytes) < 0) { close(fd); return -1; } + while (done < bytes) { ssize_t k = pwrite(fd, (char *)lmem + done, bytes - done, (off_t)done); if (k <= 0) { close(fd); return -1; } done += (size_t)k; } + view = mmap(0, bytes, PROT_READ, MAP_SHARED, fd, 0); + if (view == MAP_FAILED) { close(fd); return -1; } + if (mmap(lmem, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE, fd, 0) == MAP_FAILED) { munmap(view, bytes); close(fd); return -1; } + snap_drop(); snap_fd = fd; snap_view = view; snap_words = words; return 0; +} +Cell *micro_mem_clone(void) +{ + UCell p, dirty = 0; Cell *c; + if (!lres || !lmem) return 0; + if (snap_fd >= 0) { + for (p = 0; p < lcap; p += SNAP_PAGE) if (p >= snap_words || memcmp(lmem + p, snap_view + p, SNAP_PAGE * sizeof(Cell))) dirty++; + if (dirty * 4 > snap_words / SNAP_PAGE) snap_drop(); + } + if (snap_fd < 0 && snap_take() < 0) return 0; + c = res_map(); if (!c) return 0; + if (mmap(c, snap_words * sizeof(Cell), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE, snap_fd, 0) == MAP_FAILED) { munmap(c, RESW * sizeof(Cell)); return 0; } + for (p = 0; p < lcap; p += SNAP_PAGE) { + UCell k = lcap - p < SNAP_PAGE ? lcap - p : SNAP_PAGE; + if (p >= snap_words || memcmp(lmem + p, snap_view + p, k * sizeof(Cell))) memcpy(c + p, lmem + p, k * sizeof(Cell)); + } + return c; +} +void micro_mem_adopt(Cell *mem, UCell words) { lmem = mem; lcap = words; lres = 1; } +void micro_mem_release(Cell *mem) { if (mem) munmap(mem, RESW * sizeof(Cell)); } +#endif + #if defined(ZY_THREADS) -static VM_TLS unsigned char input_bytes[65536]; +/* allocated when the machine first reads, so a worker that never reads has none */ +static VM_TLS unsigned char *input_buffer; +#define input_bytes (input_buffer ? input_buffer : (input_buffer = malloc(65536)) ? input_buffer : (fputs("zym: out of memory\n", stderr), exit(2), input_buffer)) static VM_TLS int input_position, input_count, input_ended; static int input_fill(int wait) { if (input_position < input_count) return 1; if (input_ended) return 0; if (!wait) { struct pollfd descriptor = {0, POLLIN, 0}; if (poll(&descriptor, 1, 0) <= 0) return 0; } else fflush(stdout); - int n = (int)read(0, input_bytes, sizeof input_bytes); + int n = (int)read(0, input_bytes, 65536); if (n <= 0) { input_ended = 1; return 0; } input_position = 0; input_count = n; return 1; } @@ -392,22 +461,29 @@ int main(int argc, char **argv) { #ifdef ZY_THREADS struct PortSnapshot { - unsigned char bytes[65536]; int position, count, ended, argc, envc; char **argv, **envp; + unsigned char bytes[]; /* the input read ahead and not consumed yet */ }; -size_t micro_context_port_size(void) { return sizeof(struct PortSnapshot); } +size_t micro_context_port_size(void) { return sizeof(struct PortSnapshot) + (size_t)(input_position < input_count ? input_count - input_position : 0); } void micro_context_port_save(void *destination) { struct PortSnapshot *snapshot = destination; - memcpy(snapshot->bytes, input_bytes, sizeof input_bytes); + if (input_position < input_count) memcpy(snapshot->bytes, input_bytes + input_position, (size_t)(input_count - input_position)); snapshot->position = input_position; snapshot->count = input_count; snapshot->ended = input_ended; snapshot->argc = zy_argc; snapshot->argv = zy_argv; snapshot->envc = zy_envc; snapshot->envp = zy_envp; } void micro_context_port_restore(const void *source) { const struct PortSnapshot *snapshot = source; - memcpy(input_bytes, snapshot->bytes, sizeof input_bytes); input_position = snapshot->position; input_count = snapshot->count; input_ended = snapshot->ended; + if (input_position < input_count) memcpy(input_bytes + input_position, snapshot->bytes, (size_t)(input_count - input_position)); zy_argc = snapshot->argc; zy_argv = snapshot->argv; zy_envc = snapshot->envc; zy_envp = snapshot->envp; } -void micro_context_port_free(void) { free(lmem); lmem = 0; lcap = 0; } +void micro_context_port_free(void) { + free(input_buffer); input_buffer = 0; +#ifndef _WIN32 + if (lres) { munmap(lmem, RESW * sizeof(Cell)); lres = 0; snap_drop(); } else +#endif + free(lmem); + lmem = 0; lcap = 0; +} #endif |
