Skip to content

Commit

Permalink
Make mmap() scalable
Browse files Browse the repository at this point in the history
It's now possible to create thousands of thousands of sparse independent
memory mappings, without any slowdown. The memory manager is better with
tracking memory protection now, particularly on Windows in a precise way
that can be restored during fork(). You now have the highest quality mem
manager possible. It's even better than some OSes like XNU, where mmap()
is implemented as an O(n) operation which means sadly things aren't much
improved over there. With this change the llamafile HTTP server endpoint
at /tokenize with a prompt of 50 tokens is now able to handle 2.6m r/sec
  • Loading branch information
jart committed Jul 6, 2024
1 parent 3756870 commit 8c645fa
Show file tree
Hide file tree
Showing 59 changed files with 1,222 additions and 1,051 deletions.
2 changes: 1 addition & 1 deletion libc/calls/setrlimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int setrlimit(int resource, const struct rlimit *rlim) {
rc = efault();
} else if (IsXnuSilicon()) {
rc = _sysret(__syslib->__setrlimit(resource, rlim));
} else if (!IsWindows()) {
} else if (!IsWindows() && !(IsNetbsd() && resource == RLIMIT_AS)) {
rc = sys_setrlimit(resource, rlim);
} else if (resource == RLIMIT_STACK) {
rc = enotsup();
Expand Down
4 changes: 4 additions & 0 deletions libc/intrin/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ o/$(MODE)/libc/intrin/kprintf.o: private \
-Wframe-larger-than=128 \
-Walloca-larger-than=128

o/$(MODE)/libc/intrin/tree.o: private \
CFLAGS += \
-ffunction-sections

o//libc/intrin/memmove.o: private \
CFLAGS += \
-fno-toplevel-reorder
Expand Down
4 changes: 2 additions & 2 deletions libc/intrin/describemapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
static char DescribeMapType(int flags) {
switch (flags & MAP_TYPE) {
case MAP_FILE:
return 'f';
return '-';
case MAP_PRIVATE:
return 'p';
case MAP_SHARED:
Expand All @@ -47,7 +47,7 @@ const char *(DescribeMapping)(char p[8], int prot, int flags) {
DescribeProt(p, prot);
p[3] = DescribeMapType(flags);
p[4] = (flags & MAP_ANONYMOUS) ? 'a' : '-';
p[5] = (flags & MAP_FIXED) ? 'F' : '-';
p[5] = (flags & MAP_FIXED) ? 'f' : '-';
p[6] = 0;
return p;
}
12 changes: 6 additions & 6 deletions libc/intrin/describeprotflags.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#include "libc/macros.internal.h"
#include "libc/sysv/consts/prot.h"

static const struct DescribeFlags kProtFlags[] = {
{PROT_READ, "READ"}, //
{PROT_WRITE, "WRITE"}, //
{PROT_EXEC, "EXEC"}, //
};

const char *(DescribeProtFlags)(char buf[48], int x) {
const struct DescribeFlags kProtFlags[] = {
{PROT_READ, "READ"}, //
{PROT_WRITE, "WRITE"}, //
{PROT_EXEC, "EXEC"}, //
{PROT_GUARD, "GUARD"}, //
};
return DescribeFlags(buf, 48, kProtFlags, ARRAYLEN(kProtFlags), "PROT_", x);
}
16 changes: 15 additions & 1 deletion libc/intrin/describerlimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/struct/rlimit.h"
#include "libc/dce.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/rlim.h"

const char *DescribeRlimit(char buf[64], int rc, const struct rlimit *rlim) {
if (rc == -1)
Expand All @@ -29,7 +32,18 @@ const char *DescribeRlimit(char buf[64], int rc, const struct rlimit *rlim) {
if (kisdangerous(rlim)) {
ksnprintf(buf, 64, "%p", rlim);
} else {
ksnprintf(buf, 64, "{%'ld, %'ld}", rlim->rlim_cur, rlim->rlim_max);
char str[2][21];
if (rlim->rlim_cur == RLIM_INFINITY) {
strcpy(str[0], "RLIM_INFINITY");
} else {
FormatInt64(str[0], rlim->rlim_cur);
}
if (rlim->rlim_max == RLIM_INFINITY) {
strcpy(str[1], "RLIM_INFINITY");
} else {
FormatInt64(str[1], rlim->rlim_max);
}
ksnprintf(buf, 64, "{%s, %s}", str[0], str[1]);
}
return buf;
}
3 changes: 1 addition & 2 deletions libc/intrin/directmap-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ textwindows struct DirectMap sys_mmap_nt(void *addr, size_t size, int prot,
if ((dm.addr = MapViewOfFileEx(dm.maphandle, fl.flags2, off >> 32, off,
size, addr))) {
uint32_t oldprot;
if (VirtualProtect(dm.addr, size, __prot2nt(prot, iscow), &oldprot)) {
if (VirtualProtect(dm.addr, size, __prot2nt(prot, iscow), &oldprot))
return dm;
}
UnmapViewOfFile(dm.addr);
}
CloseHandle(dm.maphandle);
Expand Down
8 changes: 4 additions & 4 deletions libc/intrin/dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* It's required that `elem` and `succ` aren't part of the same list.
*/
privileged void dll_splice_after(struct Dll *elem, struct Dll *succ) {
void dll_splice_after(struct Dll *elem, struct Dll *succ) {
struct Dll *tmp1, *tmp2;
tmp1 = elem->next;
tmp2 = succ->prev;
Expand All @@ -43,7 +43,7 @@ privileged void dll_splice_after(struct Dll *elem, struct Dll *succ) {
*
* @param list is a doubly-linked list, where `!*list` means empty
*/
privileged void dll_remove(struct Dll **list, struct Dll *elem) {
void dll_remove(struct Dll **list, struct Dll *elem) {
if (*list == elem) {
if ((*list)->prev == *list) {
*list = 0;
Expand All @@ -66,7 +66,7 @@ privileged void dll_remove(struct Dll **list, struct Dll *elem) {
* @param list is a doubly-linked list, where `!*list` means empty
* @param elem must not be a member of `list`, or null for no-op
*/
privileged void dll_make_first(struct Dll **list, struct Dll *elem) {
void dll_make_first(struct Dll **list, struct Dll *elem) {
if (elem) {
if (!*list) {
*list = elem->prev;
Expand All @@ -85,7 +85,7 @@ privileged void dll_make_first(struct Dll **list, struct Dll *elem) {
* @param list is a doubly-linked list, where `!*list` means empty
* @param elem must not be a member of `list`, or null for no-op
*/
privileged void dll_make_last(struct Dll **list, struct Dll *elem) {
void dll_make_last(struct Dll **list, struct Dll *elem) {
if (elem) {
dll_make_first(list, elem->next);
*list = elem;
Expand Down
30 changes: 10 additions & 20 deletions libc/intrin/kprintf.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/intrin/asmflag.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/getenv.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/likely.h"
Expand Down Expand Up @@ -154,27 +153,18 @@ __funline bool kischarmisaligned(const char *p, signed char t) {
return false;
}

privileged static bool32 kisdangerous_unlocked(const char *addr) {
struct Dll *e;
if ((e = dll_first(__maps.used))) {
do {
struct Map *map = MAP_CONTAINER(e);
if (map->addr <= addr && addr < map->addr + map->size) {
dll_remove(&__maps.used, e);
dll_make_first(&__maps.used, e);
return !(map->prot & PROT_READ);
}
} while ((e = dll_next(__maps.used, e)));
return true;
} else {
return false;
}
}

privileged bool32 kisdangerous(const void *addr) {
bool32 res;
bool32 res = true;
__maps_lock();
res = kisdangerous_unlocked(addr);
if (__maps.maps) {
struct Map *map;
if ((map = __maps_floor(addr)))
if ((const char *)addr >= map->addr &&
(const char *)addr < map->addr + map->size)
res = false;
} else {
res = false;
}
__maps_unlock();
return res;
}
Expand Down
4 changes: 2 additions & 2 deletions libc/intrin/maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ape/sections.internal.h"
#include "libc/dce.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/maps.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/sysv/consts/auxv.h"
Expand All @@ -32,8 +33,7 @@ __static_yoink("_init_maps");
struct Maps __maps;

void __maps_add(struct Map *map) {
dll_init(&map->elem);
dll_make_first(&__maps.used, &map->elem);
tree_insert(&__maps.maps, &map->tree, __maps_compare);
++__maps.count;
}

Expand Down
54 changes: 43 additions & 11 deletions libc/intrin/maps.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_MAPS_H_
#define COSMOPOLITAN_LIBC_RUNTIME_MAPS_H_
#ifndef COSMOPOLITAN_MAPS_H_
#define COSMOPOLITAN_MAPS_H_
#include "libc/intrin/atomic.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/tree.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/tls2.internal.h"
COSMOPOLITAN_C_START_

#define MAP_CONTAINER(e) DLL_CONTAINER(struct Map, elem, e)
#define MAP_TREE_CONTAINER(e) TREE_CONTAINER(struct Map, tree, e)
#define MAP_FREE_CONTAINER(e) DLL_CONTAINER(struct Map, free, e)

struct Map {
char *addr; /* granule aligned */
size_t size; /* must be nonzero */
struct Dll elem; /* for __maps.free */
int64_t off; /* -1 if anonymous */
int64_t off; /* ignore for anon */
int prot; /* memory protects */
int flags; /* memory map flag */
bool iscow; /* windows nt only */
bool readonlyfile; /* windows nt only */
unsigned visited; /* used for checks */
unsigned oldprot; /* in windows fork */
intptr_t hand; /* windows nt only */
union {
struct Tree tree;
struct Dll free;
};
};

struct Maps {
unsigned mono;
atomic_int lock;
struct Tree *maps;
struct Dll *free;
struct Dll *used;
size_t count;
size_t pages;
atomic_ulong rollo;
struct Map stack;
struct Map guard;
bool once;
atomic_ulong rollo;
};

struct AddrSize {
Expand All @@ -45,10 +50,37 @@ bool __maps_lock(void);
void __maps_check(void);
void __maps_unlock(void);
void __maps_add(struct Map *);
struct Map *__maps_alloc(void);
void __maps_free(struct Map *);
struct Map *__maps_alloc(void);
struct Map *__maps_floor(const char *);
void __maps_stack(char *, int, int, size_t, int, intptr_t);
int __maps_compare(const struct Tree *, const struct Tree *);
struct AddrSize __get_main_stack(void);

forceinline optimizespeed int __maps_search(const void *key,
const struct Tree *node) {
const char *addr = (const char *)key;
const struct Map *map = (const struct Map *)MAP_TREE_CONTAINER(node);
if (addr < map->addr)
return +1;
if (addr >= map->addr + map->size)
return -1;
return 0;
}

static struct Map *__maps_next(struct Map *map) {
struct Tree *node;
if ((node = tree_next(&map->tree)))
return MAP_TREE_CONTAINER(node);
return 0;
}

static struct Map *__maps_first(void) {
struct Tree *node;
if ((node = tree_first(__maps.maps)))
return MAP_TREE_CONTAINER(node);
return 0;
}

COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_RUNTIME_MAPS_H_ */
#endif /* COSMOPOLITAN_MAPS_H_ */
Loading

0 comments on commit 8c645fa

Please sign in to comment.