wasmhost: WASMHOST_PROFILE=1 times each host function (encode / handler / decode)
CI / test (push) Failing after 18s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
This commit is contained in:
Claude committed 2026-11-25 19:51:28 +00:00
1 parent 9431254de0
commit 05fe70a2f6
1 file changed
+44
+44
View File
@@ -21,6 +21,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wasmtime.h>
#include "moonbit.h"
@@ -266,15 +267,52 @@ static int dec_message(moonbit_bytes_t bytes, wasmtime_component_val_t *out, siz
/* ---- host functions ---- */
/* WASMHOST_PROFILE=1: time spent per host function (encode / MoonBit handler / decode), dumped to
* stderr every 5 s. */
#define MAX_FUNCS 512
static int profiling = -1;
static char *func_names[MAX_FUNCS];
static struct { uint64_t n, enc, call, dec, bytes; } prof[MAX_FUNCS];
static uint64_t prof_last;
static uint64_t now_ns(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
}
static void prof_dump(uint64_t t) {
fprintf(stderr, "--- host calls over %.1f s (ms: enc call dec)\n", (t - prof_last) / 1e9);
for (int i = 0; i < MAX_FUNCS; i++) {
if (!prof[i].n) continue;
fprintf(stderr, "%-44s %7llu %8.1f %8.1f %8.1f %llu kB\n", func_names[i] ? func_names[i] : "?",
(unsigned long long)prof[i].n, prof[i].enc / 1e6, prof[i].call / 1e6, prof[i].dec / 1e6,
(unsigned long long)(prof[i].bytes >> 10));
}
memset(prof, 0, sizeof prof);
prof_last = t;
}
static wasmtime_error_t *host_call(void *env, wasmtime_context_t *c, const wasmtime_component_func_type_t *ty,
wasmtime_component_val_t *args, size_t nargs,
wasmtime_component_val_t *results, size_t nresults) {
(void)c; (void)ty;
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;
if (profiling) t1 = now_ns();
moonbit_bytes_t out = dispatch(id, in); /* takes `in` */
uint64_t t2 = profiling ? now_ns() : 0;
int bad = dec_message(out, results, nresults);
moonbit_decref(out);
if (profiling && id >= 0 && id < MAX_FUNCS) {
uint64_t t3 = now_ns();
prof[id].n++; prof[id].enc += t1 - t0; prof[id].call += t2 - t1; prof[id].dec += t3 - t2;
prof[id].bytes += inlen;
if (t3 - prof_last > 5000000000ull) prof_dump(t3);
}
if (bad) {
char msg[96];
snprintf(msg, sizeof msg, "host function %d returned a malformed result", id);
@@ -349,6 +387,12 @@ int32_t wt_define(moonbit_bytes_t iface, moonbit_bytes_t names, int32_t first_id
set_error(what, e);
break;
}
if (id >= 0 && id < MAX_FUNCS) {
size_t nl = i - start, il = ilen;
char *nm = malloc(il + nl + 2);
memcpy(nm, iface, il); nm[il] = '.'; memcpy(nm + il + 1, s + start, nl); nm[il + nl + 1] = 0;
func_names[id] = nm;
}
id++;
}
start = i + 1;