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;
|
||||
|
||||
Reference in new issue
Block a user