Fix the memory leak that filled RAM and swap; launch monitor, memory log
wasmhost leaked the arguments of every host call. wt_stub.c handed them to the MoonBit dispatcher expecting it to own them, but the compiler borrowed the parameter, so nothing released them (about 600 kB/s in a test with one busy terminal; a long session reached over 16 GB resident plus swap). MoonBit now allocates the buffer and C copies into it. Also: - Unannotated pointer FFI parameters default to #owned, and none of these C functions released theirs, including the native KMS present (a whole frame each). Annotated #borrow; the wasmhost build now fails on that warning, which --warn-list -a had hidden. - Watched launches forked the whole compositor, and each monitor kept a snapshot of its heap for the app's lifetime. It now posix_spawns a fresh copy of the binary that takes over before main. - The host logs memory, window buffers and images once a minute. - chandra-session keeps the previous log as session.log.1. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hj5m35Lj6Rt9vwwLGZ
This commit is contained in:
1 parent
44688453d7
commit
ada9850b8d
11 files changed
+211
-75
No files matched your search
@@ -722,7 +722,12 @@ static void pool_maps_gc(int64_t now) {
|
||||
}
|
||||
|
||||
/* Drop pool mappings not used for a while (called with each submitted frame). */
|
||||
void rs_maps_gc(void) { pool_maps_gc(mono_ms()); }
|
||||
static void rs_mem_log(void);
|
||||
|
||||
void rs_maps_gc(void) {
|
||||
pool_maps_gc(mono_ms());
|
||||
rs_mem_log();
|
||||
}
|
||||
|
||||
/* The whole file behind fd (st_size bytes) mapped read-only, from the cache when it is there. */
|
||||
static const uint8_t *pool_map(int fd, const struct stat *st) {
|
||||
@@ -854,6 +859,48 @@ int32_t rs_blit_surface(uint32_t id, int32_t dx, int32_t dy, int32_t dw, int32_t
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ memory log */
|
||||
|
||||
/* A line of what the host holds, in the session log: once a minute (CHANDRA_MEM_LOG_SECS), so a
|
||||
* session that grows until the machine swaps leaves a record of what grew. */
|
||||
static void rs_mem_log(void) {
|
||||
static int64_t last;
|
||||
static int every_s = -1;
|
||||
if (every_s < 0) {
|
||||
const char *e = getenv("CHANDRA_MEM_LOG_SECS");
|
||||
every_s = e && atoi(e) > 0 ? atoi(e) : 60;
|
||||
}
|
||||
int64_t now = mono_ms();
|
||||
if (last && now - last < (int64_t)every_s * 1000) return;
|
||||
last = now;
|
||||
long rss = 0, swap = 0;
|
||||
FILE *f = fopen("/proc/self/status", "r");
|
||||
if (f) {
|
||||
char line[128];
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
sscanf(line, "VmRSS: %ld", &rss);
|
||||
sscanf(line, "VmSwap: %ld", &swap);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
int surf_n = 0, img_n = 0;
|
||||
size_t surf_bytes = 0, img_bytes = 0;
|
||||
for (int i = 0; i < SURF_BUCKETS; i++)
|
||||
for (Surface *s = surfaces[i]; s; s = s->next) { surf_n++; surf_bytes += (size_t)s->w * s->h * 4; }
|
||||
for (int i = 0; i < n_images; i++) {
|
||||
if (!images[i].used) continue;
|
||||
img_n++;
|
||||
if (images[i].rgba) img_bytes += (size_t)images[i].w * images[i].h * 4;
|
||||
for (Sized *s = images[i].sized; s; s = s->next) img_bytes += (size_t)s->w * s->h * 4;
|
||||
}
|
||||
time_t wall = time(NULL);
|
||||
struct tm tm;
|
||||
char when[32] = "";
|
||||
if (localtime_r(&wall, &tm)) strftime(when, sizeof when, "%F %T", &tm);
|
||||
fprintf(stderr, "%s chandra-host: memory %ld MB resident, %ld MB swapped; %d window buffers (%zu MB), %d images (%zu MB)\n",
|
||||
when, rss / 1024, swap / 1024, surf_n, surf_bytes >> 20, img_n, img_bytes >> 20);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ backdrop and output */
|
||||
|
||||
static uint8_t *backdrop;
|
||||
|
||||
+9
-3
@@ -4,7 +4,11 @@
|
||||
/// component reach the one handler given to `set_handler` as (id, arguments).
|
||||
|
||||
///|
|
||||
extern "C" fn c_set_dispatch(f : FuncRef[(Int, Bytes) -> Bytes]) = "wt_set_dispatch"
|
||||
extern "C" fn c_set_dispatch(f : FuncRef[(Int, Int) -> Bytes]) = "wt_set_dispatch"
|
||||
|
||||
///|
|
||||
#borrow(out)
|
||||
extern "C" fn c_take_args(out : Bytes) = "wt_take_args"
|
||||
|
||||
///|
|
||||
extern "C" fn c_last_error() -> Bytes = "wt_last_error"
|
||||
@@ -33,7 +37,9 @@ let handler : Ref[(Int, Array[Val]) -> Array[Val]] = Ref(fn(id, _) {
|
||||
})
|
||||
|
||||
///|
|
||||
fn dispatch(id : Int, args : Bytes) -> Bytes {
|
||||
fn dispatch(id : Int, len : Int) -> Bytes {
|
||||
let args = Bytes::make(len, 0)
|
||||
c_take_args(args)
|
||||
encode((handler.val)(id, decode(args)))
|
||||
}
|
||||
|
||||
@@ -52,7 +58,7 @@ pub fn load(path : String) -> Result[Unit, String] {
|
||||
if c_init() != 0 {
|
||||
return Err(last_error())
|
||||
}
|
||||
c_set_dispatch(fn(id, args) { dispatch(id, args) })
|
||||
c_set_dispatch(fn(id, len) { dispatch(id, len) })
|
||||
if c_load(cstr(path)) != 0 {
|
||||
return Err(last_error())
|
||||
}
|
||||
|
||||
+29
-6
@@ -27,7 +27,11 @@
|
||||
|
||||
#define KIND_BLOB 0x80
|
||||
|
||||
typedef moonbit_bytes_t (*wt_dispatch_t)(int32_t id, moonbit_bytes_t args);
|
||||
/* The dispatcher takes only the arguments' length and fetches them with wt_take_args, so no heap
|
||||
* object passes from C into MoonBit: whether a MoonBit function owns or borrows a parameter is
|
||||
* the compiler's choice (it borrowed this one, and every call's arguments leaked). What it
|
||||
* returns is ours to release. */
|
||||
typedef moonbit_bytes_t (*wt_dispatch_t)(int32_t id, int32_t len);
|
||||
|
||||
static wasm_engine_t *engine;
|
||||
static wasmtime_store_t *store;
|
||||
@@ -37,6 +41,8 @@ static wasmtime_component_t *component;
|
||||
static wasmtime_component_instance_t instance;
|
||||
static int have_instance;
|
||||
static wt_dispatch_t dispatch;
|
||||
static const uint8_t *pending_args; /* of the host call being dispatched */
|
||||
static size_t pending_len;
|
||||
static char last_error[16384]; /* wasmtime backtraces are long; the cause comes last */
|
||||
|
||||
static void set_error(const char *what, wasmtime_error_t *e) {
|
||||
@@ -139,10 +145,14 @@ static moonbit_bytes_t to_moonbit(const Buf *b) {
|
||||
return out;
|
||||
}
|
||||
|
||||
static void put_message(Buf *b, const wasmtime_component_val_t *vals, size_t n) {
|
||||
put_u32(b, (uint32_t)n);
|
||||
for (size_t i = 0; i < n; i++) enc(b, &vals[i]);
|
||||
}
|
||||
|
||||
static moonbit_bytes_t enc_message(const wasmtime_component_val_t *vals, size_t n) {
|
||||
Buf b = {0};
|
||||
put_u32(&b, (uint32_t)n);
|
||||
for (size_t i = 0; i < n; i++) enc(&b, &vals[i]);
|
||||
put_message(&b, vals, n);
|
||||
moonbit_bytes_t out = to_moonbit(&b);
|
||||
free(b.p);
|
||||
return out;
|
||||
@@ -300,10 +310,16 @@ static wasmtime_error_t *host_call(void *env, wasmtime_context_t *c, const wasmt
|
||||
int32_t id = (int32_t)(intptr_t)env;
|
||||
if (profiling < 0) { profiling = getenv("WASMHOST_PROFILE") != NULL; prof_last = now_ns(); }
|
||||
uint64_t t0 = profiling ? now_ns() : 0;
|
||||
moonbit_bytes_t in = enc_message(args, nargs);
|
||||
uint64_t t1 = 0, inlen = profiling ? (uint64_t)Moonbit_array_length(in) : 0;
|
||||
Buf in = {0};
|
||||
put_message(&in, args, nargs);
|
||||
uint64_t t1 = 0, inlen = in.len;
|
||||
if (profiling) t1 = now_ns();
|
||||
moonbit_bytes_t out = dispatch(id, in); /* takes `in` */
|
||||
pending_args = in.p;
|
||||
pending_len = in.len;
|
||||
moonbit_bytes_t out = dispatch(id, (int32_t)in.len);
|
||||
pending_args = NULL;
|
||||
pending_len = 0;
|
||||
free(in.p);
|
||||
uint64_t t2 = profiling ? now_ns() : 0;
|
||||
int bad = dec_message(out, results, nresults);
|
||||
moonbit_decref(out);
|
||||
@@ -323,6 +339,13 @@ static wasmtime_error_t *host_call(void *env, wasmtime_context_t *c, const wasmt
|
||||
|
||||
void wt_set_dispatch(wt_dispatch_t f) { dispatch = f; }
|
||||
|
||||
/* Copy the arguments of the call being dispatched into `out` (made by MoonBit, borrowed here). */
|
||||
void wt_take_args(moonbit_bytes_t out) {
|
||||
size_t n = (size_t)Moonbit_array_length(out);
|
||||
if (n > pending_len) n = pending_len;
|
||||
if (n) memcpy(out, pending_args, n);
|
||||
}
|
||||
|
||||
moonbit_bytes_t wt_last_error(void) {
|
||||
size_t n = strlen(last_error);
|
||||
moonbit_bytes_t out = moonbit_make_bytes((int32_t)n, 0);
|
||||
|
||||
Reference in new issue
Block a user