mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 23:49:56 -04:00
Stop sending a request again to a new translator child after the first one gave no answer for the read timeout.
plat_child_read returned -1 both for a broken pipe and for a child that sent nothing for PLAT_CHILD_READ_MS (120 s), and svc_request retried any failure once on a fresh child. The translator is deterministic, so a program it hangs on hangs the second child too: a compute shader, which the replay thread looks up synchronously (vk_exec_draw.c shader_for), held the game for about 4 minutes instead of 2. plat_child_read now returns -2 on the timeout (win32.c and linux.c), child_exchange_raw returns 1, 0 for a broken pipe or a child that could not start, and -1 for the timeout, and svc_request stops the child and gives -1 without asking again; a dead child is still restarted once. translate_one fails such a request with its own reason, keeps its job and code for diagnosis and deletes whatever the stopped child wrote. The carry pass's child runs at idle priority, so a busy machine can starve it that long: carry_exchange does not resend after a timeout either, and carry_translate counts it as a failed input, not as an unavailable translator, so one slow program does not count towards the three that stop the pass. platform.h lets a build set PLAT_CHILD_READ_MS. Not reproduced before the change (no translator hang appears in the available logs). tests/child_timeout_check.py (new) builds the real child and request functions with a 300 ms timeout: a silent child gives -2 and costs one timeout with one child started, an exiting child is restarted once, an answering child gives its line. Every runtime file compiles, with no new warnings in gfx_shader.c and win32.c. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -653,6 +653,12 @@ A change to which translations of a previous translator are served (`runtime/gfx
|
|||||||
`gfx_shader.c` and runs them against stand-in caches: a translation whose texture pool array has the earlier length
|
`gfx_shader.c` and runs them against stand-in caches: a translation whose texture pool array has the earlier length
|
||||||
(32768) is freed and the lookup goes on, one with the current length or no pool is served. No game files, no GPU.
|
(32768) is freed and the lookup goes on, one with the current length or no pool is served. No game files, no GPU.
|
||||||
|
|
||||||
|
A change to how the translator children are read and restarted (`runtime/platform/win32.c` `plat_child_read`,
|
||||||
|
`runtime/gfx_shader.c` `child_exchange_raw`, `svc_request`) passes `python tests\child_timeout_check.py`, which builds
|
||||||
|
those functions with a 300 ms read timeout and runs `cmd.exe` children through them: one that never answers costs one
|
||||||
|
timeout and is not asked again, one that exits is restarted once, one that answers gives its line. Windows only; no
|
||||||
|
game files, no GPU.
|
||||||
|
|
||||||
A change to where the lazy write-back is on by default (`runtime/wb_lazy_choice.h`, `runtime/vk_memory.c`
|
A change to where the lazy write-back is on by default (`runtime/wb_lazy_choice.h`, `runtime/vk_memory.c`
|
||||||
`vk_mem_lazy_decide`) passes `python tests\wb_lazy_check.py`, which builds the header alone and checks the choice and
|
`vk_mem_lazy_decide`) passes `python tests\wb_lazy_check.py`, which builds the header alone and checks the choice and
|
||||||
its reason by platform, by GPU driver and by what the player set. No game files, no GPU.
|
its reason by platform, by GPU driver and by what the player set. No game files, no GPU.
|
||||||
|
|||||||
@@ -562,7 +562,9 @@ static int child_start(Svc *v) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// One line out, one line in (no newline in the reply). 0 when the pipe broke.
|
// One line out, one line in (no newline in the reply). 1 answered; 0 when the child could not start or the pipe broke;
|
||||||
|
// -1 when the child sent nothing for PLAT_CHILD_READ_MS (stuck on this request: the translator is deterministic, so
|
||||||
|
// sending it again would wait as long).
|
||||||
static int child_exchange_raw(Svc *v, const char *line, char *reply, size_t reply_size) {
|
static int child_exchange_raw(Svc *v, const char *line, char *reply, size_t reply_size) {
|
||||||
if (!v->child && !child_start(v)) return 0;
|
if (!v->child && !child_start(v)) return 0;
|
||||||
plat_inc64(&n_requests);
|
plat_inc64(&n_requests);
|
||||||
@@ -582,6 +584,7 @@ static int child_exchange_raw(Svc *v, const char *line, char *reply, size_t repl
|
|||||||
}
|
}
|
||||||
if (v->in_len == sizeof v->inbuf) v->in_len = 0; // an overlong line: drop it, wait for its end
|
if (v->in_len == sizeof v->inbuf) v->in_len = 0; // an overlong line: drop it, wait for its end
|
||||||
long got = plat_child_read(v->child, v->inbuf + v->in_len, sizeof v->inbuf - v->in_len);
|
long got = plat_child_read(v->child, v->inbuf + v->in_len, sizeof v->inbuf - v->in_len);
|
||||||
|
if (got == -2) return -1;
|
||||||
if (got <= 0) return 0;
|
if (got <= 0) return 0;
|
||||||
v->in_len += (size_t)got;
|
v->in_len += (size_t)got;
|
||||||
}
|
}
|
||||||
@@ -589,9 +592,9 @@ static int child_exchange_raw(Svc *v, const char *line, char *reply, size_t repl
|
|||||||
|
|
||||||
static int child_exchange(Svc *v, const char *line, char *reply, size_t reply_size) {
|
static int child_exchange(Svc *v, const char *line, char *reply, size_t reply_size) {
|
||||||
uint64_t p0 = perf_begin(); // NX2NAVI_TRACE=perf: render-thread time spent translating
|
uint64_t p0 = perf_begin(); // NX2NAVI_TRACE=perf: render-thread time spent translating
|
||||||
int ok = child_exchange_raw(v, line, reply, reply_size);
|
int r = child_exchange_raw(v, line, reply, reply_size);
|
||||||
perf_end(PERF_XLATE, p0);
|
perf_end(PERF_XLATE, p0);
|
||||||
return ok;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Children allowed at run time: NX2NAVI_XLATE_PROCS, default 2.
|
// Children allowed at run time: NX2NAVI_XLATE_PROCS, default 2.
|
||||||
@@ -664,13 +667,25 @@ static void svc_set_limit(int n) {
|
|||||||
plat_cond_broadcast(&S.freed);
|
plat_cond_broadcast(&S.freed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// One request on a child; a dead child is restarted once. 0 when the translator is unavailable. The caller has given
|
// One request on a child; a dead child is restarted once. 1 answered, 0 when the translator is unavailable, -1 when
|
||||||
// up its virtual core (kern_core_away): the wait for a child and the translation are host time.
|
// the child gave no answer for PLAT_CHILD_READ_MS: it is stopped and the request is not sent again, which would hold
|
||||||
|
// the caller (the replay thread for a compute shader) as long once more. The caller has given up its virtual core
|
||||||
|
// (kern_core_away): the wait for a child and the translation are host time.
|
||||||
static int svc_request(const char *line, char *reply, size_t reply_size) {
|
static int svc_request(const char *line, char *reply, size_t reply_size) {
|
||||||
Svc *v = svc_get();
|
Svc *v = svc_get();
|
||||||
int ok = 0;
|
int ok = 0;
|
||||||
for (int attempt = 0; attempt < 2 && !ok; attempt++)
|
for (int attempt = 0; attempt < 2; attempt++) {
|
||||||
if (!(ok = child_exchange(v, line, reply, reply_size))) child_stop(v);
|
int r = child_exchange(v, line, reply, reply_size);
|
||||||
|
if (r > 0) {
|
||||||
|
ok = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
child_stop(v);
|
||||||
|
if (r < 0) {
|
||||||
|
ok = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
svc_put(v);
|
svc_put(v);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@@ -2202,7 +2217,7 @@ static void zpass_kick(void) { plat_once(&Z.once, zpass_start, NULL); }
|
|||||||
int gfx_shader_raw_request(const char *line, char *reply, size_t reply_size) {
|
int gfx_shader_raw_request(const char *line, char *reply, size_t reply_size) {
|
||||||
int away = kern_core_away();
|
int away = kern_core_away();
|
||||||
Svc *v = svc_get();
|
Svc *v = svc_get();
|
||||||
int ok = child_exchange(v, line, reply, reply_size);
|
int ok = child_exchange(v, line, reply, reply_size) > 0;
|
||||||
if (!ok) child_stop(v);
|
if (!ok) child_stop(v);
|
||||||
svc_put(v);
|
svc_put(v);
|
||||||
kern_core_back(&away);
|
kern_core_back(&away);
|
||||||
@@ -2562,9 +2577,9 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co
|
|||||||
} else {
|
} else {
|
||||||
if (!write_file(code_path, code + skip, code_len)) return free(req), fail("cannot write the code file", hex);
|
if (!write_file(code_path, code + skip, code_len)) return free(req), fail("cannot write the code file", hex);
|
||||||
int ok = svc_request(req, reply, sizeof reply);
|
int ok = svc_request(req, reply, sizeof reply);
|
||||||
// Keep a failed translation's job and code for diagnosis.
|
// Keep a failed translation's job and code for diagnosis, and those of a request the translator never answered.
|
||||||
int failed = ok && strncmp(reply, "ok ", 3);
|
int failed = ok > 0 && strncmp(reply, "ok ", 3);
|
||||||
int kept = keep >= 2 || (keep && a && a->mvprev) || failed;
|
int kept = keep >= 2 || (keep && a && a->mvprev) || failed || ok < 0;
|
||||||
if (kept) {
|
if (kept) {
|
||||||
char job_path[PATH_CAP + 80];
|
char job_path[PATH_CAP + 80];
|
||||||
snprintf(job_path, sizeof job_path, "%s/%s.job", cache_dir, hex);
|
snprintf(job_path, sizeof job_path, "%s/%s.job", cache_dir, hex);
|
||||||
@@ -2572,6 +2587,13 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co
|
|||||||
}
|
}
|
||||||
free(req);
|
free(req);
|
||||||
if (!kept) plat_file_delete(code_path);
|
if (!kept) plat_file_delete(code_path);
|
||||||
|
if (ok < 0) { // whatever the stopped child wrote is not a translation
|
||||||
|
plat_file_delete(spv_path), plat_file_delete(res_path);
|
||||||
|
char why[96];
|
||||||
|
snprintf(why, sizeof why, "the translator gave no answer in %d s; its child was stopped",
|
||||||
|
PLAT_CHILD_READ_MS / 1000);
|
||||||
|
return fail(why, hex);
|
||||||
|
}
|
||||||
if (!ok) return fail("translator unavailable", hex);
|
if (!ok) return fail("translator unavailable", hex);
|
||||||
if (failed) {
|
if (failed) {
|
||||||
plat_file_delete(spv_path), plat_file_delete(res_path);
|
plat_file_delete(spv_path), plat_file_delete(res_path);
|
||||||
@@ -3724,14 +3746,18 @@ static void carry_wait(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// One request on the pass's child, started below every normal process when needed; a dead child is restarted once.
|
// One request on the pass's child, started below every normal process when needed; a dead child is restarted once.
|
||||||
|
// 1 answered, 0 unavailable, -1 no answer for PLAT_CHILD_READ_MS: the child is stopped and the request not sent again
|
||||||
|
// (at idle priority a busy machine can starve the child that long, so the next request gets a new child).
|
||||||
static int carry_exchange(const char *line, char *reply, size_t n) {
|
static int carry_exchange(const char *line, char *reply, size_t n) {
|
||||||
for (int attempt = 0; attempt < 2; attempt++) {
|
for (int attempt = 0; attempt < 2; attempt++) {
|
||||||
if (!carry_svc.child) {
|
if (!carry_svc.child) {
|
||||||
if (!child_start(&carry_svc)) return 0;
|
if (!child_start(&carry_svc)) return 0;
|
||||||
plat_child_lower(carry_svc.child);
|
plat_child_lower(carry_svc.child);
|
||||||
}
|
}
|
||||||
if (child_exchange_raw(&carry_svc, line, reply, n)) return 1;
|
int r = child_exchange_raw(&carry_svc, line, reply, n);
|
||||||
|
if (r > 0) return 1;
|
||||||
child_stop(&carry_svc);
|
child_stop(&carry_svc);
|
||||||
|
if (r < 0) return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -3739,8 +3765,8 @@ static int carry_exchange(const char *line, char *reply, size_t n) {
|
|||||||
// Translates a stored input again under the current identity and stores the files as translate_one does, leaving the
|
// Translates a stored input again under the current identity and stores the files as translate_one does, leaving the
|
||||||
// memory cache alone. The job's files have names of their own (sha256("TKSC", key)), so an on-demand translation of the
|
// memory cache alone. The job's files have names of their own (sha256("TKSC", key)), so an on-demand translation of the
|
||||||
// same key at the same time never writes the same files; each renames its own onto the key's, with the same content.
|
// same key at the same time never writes the same files; each renames its own onto the key's, with the same content.
|
||||||
// 1 stored, 0 the translator refused the program or wrote no usable translation (why says which), -1 no translator or
|
// 1 stored, 0 the translator refused the program, gave no answer or wrote no usable translation (why says which), -1 no
|
||||||
// a file error.
|
// translator or a file error.
|
||||||
static int carry_translate(const Req *r, const Inputs *in, char *why, size_t nwhy) {
|
static int carry_translate(const Req *r, const Inputs *in, char *why, size_t nwhy) {
|
||||||
uint8_t t[32], tb[36];
|
uint8_t t[32], tb[36];
|
||||||
memcpy(tb, "TKSC", 4), memcpy(tb + 4, r->key, 32);
|
memcpy(tb, "TKSC", 4), memcpy(tb + 4, r->key, 32);
|
||||||
@@ -3757,8 +3783,8 @@ static int carry_translate(const Req *r, const Inputs *in, char *why, size_t nwh
|
|||||||
char *req = malloc(cap), reply[LINE_MAX_LEN];
|
char *req = malloc(cap), reply[LINE_MAX_LEN];
|
||||||
if (!req) return plat_file_delete(code_path), snprintf(why, nwhy, "out of memory"), -1;
|
if (!req) return plat_file_delete(code_path), snprintf(why, nwhy, "out of memory"), -1;
|
||||||
build_request(req, hex, r->stage_name, code_path, r->c1, r->c1_len, &in->a, in->control, NULL);
|
build_request(req, hex, r->stage_name, code_path, r->c1, r->c1_len, &in->a, in->control, NULL);
|
||||||
int ok = carry_exchange(req, reply, sizeof reply), refused = ok && strncmp(reply, "ok ", 3);
|
int ok = carry_exchange(req, reply, sizeof reply), refused = ok > 0 && strncmp(reply, "ok ", 3);
|
||||||
if (refused) { // as translate_one: the job and its code stay, for a translator fix to be tested on
|
if (refused || ok < 0) { // as translate_one: the job and its code stay, for a translator fix to be tested on
|
||||||
char job_path[PATH_CAP + 80];
|
char job_path[PATH_CAP + 80];
|
||||||
snprintf(job_path, sizeof job_path, "%s/%s.job", cache_dir, hex);
|
snprintf(job_path, sizeof job_path, "%s/%s.job", cache_dir, hex);
|
||||||
write_file(job_path, req, strlen(req));
|
write_file(job_path, req, strlen(req));
|
||||||
@@ -3767,6 +3793,10 @@ static int carry_translate(const Req *r, const Inputs *in, char *why, size_t nwh
|
|||||||
}
|
}
|
||||||
free(req);
|
free(req);
|
||||||
if (!ok) return snprintf(why, nwhy, "translator unavailable"), -1;
|
if (!ok) return snprintf(why, nwhy, "translator unavailable"), -1;
|
||||||
|
if (ok < 0) { // this input fails; the pass goes on with the next on a new child
|
||||||
|
plat_file_delete(spv_path), plat_file_delete(res_path);
|
||||||
|
return snprintf(why, nwhy, "the translator gave no answer in %d s", PLAT_CHILD_READ_MS / 1000), 0;
|
||||||
|
}
|
||||||
GfxShader *s = refused ? NULL : disk_load_from(t, NULL, 0, 0);
|
GfxShader *s = refused ? NULL : disk_load_from(t, NULL, 0, 0);
|
||||||
if (!s || s->stage != in->stage) {
|
if (!s || s->stage != in->stage) {
|
||||||
snprintf(why, nwhy, "%s", refused ? reply : s ? "translated to another stage" : "translator output missing or malformed");
|
snprintf(why, nwhy, "%s", refused ? reply : s ? "translated to another stage" : "translator output missing or malformed");
|
||||||
|
|||||||
@@ -2594,13 +2594,14 @@ void plat_child_lower(PlatChild *c) { // a higher nice value needs no privileg
|
|||||||
// A child that died makes the write fail with EPIPE; SIGPIPE is ignored for the process (main.c), as Windows has none.
|
// A child that died makes the write fail with EPIPE; SIGPIPE is ignored for the process (main.c), as Windows has none.
|
||||||
int plat_child_write(PlatChild *c, const void *data, size_t n) { return write_all(c->in, data, n); }
|
int plat_child_write(PlatChild *c, const void *data, size_t n) { return write_all(c->in, data, n); }
|
||||||
|
|
||||||
// See win32.c plat_child_read: a child that never answers fails the read after PLAT_CHILD_READ_MS.
|
// See win32.c plat_child_read: a child that never answers fails the read with -2 after PLAT_CHILD_READ_MS.
|
||||||
long plat_child_read(PlatChild *c, void *buf, size_t cap) {
|
long plat_child_read(PlatChild *c, void *buf, size_t cap) {
|
||||||
struct pollfd pf = {.fd = c->out, .events = POLLIN};
|
struct pollfd pf = {.fd = c->out, .events = POLLIN};
|
||||||
int r;
|
int r;
|
||||||
do r = poll(&pf, 1, PLAT_CHILD_READ_MS);
|
do r = poll(&pf, 1, PLAT_CHILD_READ_MS);
|
||||||
while (r < 0 && errno == EINTR);
|
while (r < 0 && errno == EINTR);
|
||||||
if (r <= 0) return -1; // timed out or failed
|
if (!r) return -2; // timed out
|
||||||
|
if (r < 0) return -1; // failed
|
||||||
ssize_t k;
|
ssize_t k;
|
||||||
do k = read(c->out, buf, cap);
|
do k = read(c->out, buf, cap);
|
||||||
while (k < 0 && errno == EINTR);
|
while (k < 0 && errno == EINTR);
|
||||||
|
|||||||
@@ -563,8 +563,12 @@ void plat_child_stop(PlatChild *c); // kills the child and
|
|||||||
// that must not slow the game down (gfx_shader.c, the carry pass).
|
// that must not slow the game down (gfx_shader.c, the carry pass).
|
||||||
void plat_child_lower(PlatChild *c);
|
void plat_child_lower(PlatChild *c);
|
||||||
int plat_child_write(PlatChild *c, const void *data, size_t n); // 1 when all n bytes were written
|
int plat_child_write(PlatChild *c, const void *data, size_t n); // 1 when all n bytes were written
|
||||||
|
#ifndef PLAT_CHILD_READ_MS // a check may build the child functions with a shorter one
|
||||||
#define PLAT_CHILD_READ_MS 120000 // plat_child_read gives up on a child that sends nothing for this long
|
#define PLAT_CHILD_READ_MS 120000 // plat_child_read gives up on a child that sends nothing for this long
|
||||||
long plat_child_read(PlatChild *c, void *buf, size_t cap); // bytes read (at least 1); 0 or negative: pipe broke or timed out
|
#endif
|
||||||
|
// Bytes read (at least 1); 0 or -1 when the pipe broke (the child is gone); -2 when nothing came for PLAT_CHILD_READ_MS
|
||||||
|
// while the child kept its end open (a child stuck on its input: asking it again would wait as long).
|
||||||
|
long plat_child_read(PlatChild *c, void *buf, size_t cap);
|
||||||
|
|
||||||
// ---- local IPC: a stream connection to another program of this machine, one listen location per name (Discord's
|
// ---- local IPC: a stream connection to another program of this machine, one listen location per name (Discord's
|
||||||
// Rich Presence, discord_rpc.c). On Windows the named pipe \\.\pipe\<name>; on Linux the Unix domain socket
|
// Rich Presence, discord_rpc.c). On Windows the named pipe \\.\pipe\<name>; on Linux the Unix domain socket
|
||||||
|
|||||||
@@ -2384,15 +2384,16 @@ int plat_child_write(PlatChild *c, const void *data, size_t n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A child that stays alive but never answers (a translator stuck on one program) used to block the reader for good,
|
// A child that stays alive but never answers (a translator stuck on one program) used to block the reader for good,
|
||||||
// and with it the draw or dispatch waiting for that shader: after PLAT_CHILD_READ_MS without a byte the read fails,
|
// and with it the draw or dispatch waiting for that shader: after PLAT_CHILD_READ_MS without a byte the read fails
|
||||||
// and the caller stops the child (gfx_shader.c svc_request). The pipe is anonymous, so readiness is polled.
|
// with -2, apart from the -1 of a broken pipe, and the caller stops the child without sending the request again
|
||||||
|
// (gfx_shader.c svc_request). The pipe is anonymous, so readiness is polled.
|
||||||
long plat_child_read(PlatChild *c, void *buf, size_t cap) {
|
long plat_child_read(PlatChild *c, void *buf, size_t cap) {
|
||||||
ULONGLONG t0 = GetTickCount64();
|
ULONGLONG t0 = GetTickCount64();
|
||||||
for (unsigned spins = 0;; spins++) {
|
for (unsigned spins = 0;; spins++) {
|
||||||
DWORD avail = 0;
|
DWORD avail = 0;
|
||||||
if (!PeekNamedPipe(c->out, NULL, 0, NULL, &avail, NULL)) return -1; // broken: the child exited
|
if (!PeekNamedPipe(c->out, NULL, 0, NULL, &avail, NULL)) return -1; // broken: the child exited
|
||||||
if (avail) break;
|
if (avail) break;
|
||||||
if (GetTickCount64() - t0 > PLAT_CHILD_READ_MS) return -1;
|
if (GetTickCount64() - t0 > PLAT_CHILD_READ_MS) return -2;
|
||||||
Sleep(spins < 50 ? 0 : 1);
|
Sleep(spins < 50 ? 0 : 1);
|
||||||
}
|
}
|
||||||
DWORD got = 0;
|
DWORD got = 0;
|
||||||
|
|||||||
167
tests/child_timeout_check.py
Normal file
167
tests/child_timeout_check.py
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
"""Check how a translator child that stops answering is handled (runtime/platform/win32.c plat_child_read,
|
||||||
|
runtime/gfx_shader.c child_exchange_raw and svc_request). The check cuts the child functions out of win32.c and the
|
||||||
|
request functions out of gfx_shader.c, builds them with PLAT_CHILD_READ_MS at 300 ms (platform.h lets a build set it)
|
||||||
|
and runs three children through them:
|
||||||
|
|
||||||
|
- a child that keeps its pipe open and never writes: plat_child_read gives -2 after the timeout, and svc_request
|
||||||
|
gives -1 having started one child, so a stuck translator holds its caller one timeout, not two;
|
||||||
|
- a child that exits at once: the read fails with a broken pipe (0 or -1) at once, and svc_request restarts it once
|
||||||
|
and gives 0;
|
||||||
|
- a child that answers one line: svc_request gives 1 and the line without its line end.
|
||||||
|
|
||||||
|
Windows only (the child functions are win32.c's). No GPU, no game files, any Python.
|
||||||
|
|
||||||
|
Usage: child_timeout_check.py
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
||||||
|
|
||||||
|
|
||||||
|
def compiler():
|
||||||
|
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH."""
|
||||||
|
if os.environ.get('CC'):
|
||||||
|
return os.environ['CC']
|
||||||
|
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
||||||
|
return str(MSYS_BIN / 'clang.exe')
|
||||||
|
return shutil.which('clang') or 'clang'
|
||||||
|
|
||||||
|
|
||||||
|
def extract(src, head, tail=''):
|
||||||
|
"""The text from head through its brace-balanced body (plus tail)."""
|
||||||
|
i = src.index(head)
|
||||||
|
j = src.index('{', i)
|
||||||
|
depth = 0
|
||||||
|
for k in range(j, len(src)):
|
||||||
|
if src[k] == '{':
|
||||||
|
depth += 1
|
||||||
|
elif src[k] == '}':
|
||||||
|
depth -= 1
|
||||||
|
if depth == 0:
|
||||||
|
return src[i:k + 1] + tail + '\n'
|
||||||
|
raise SystemExit(f'unbalanced: {head}')
|
||||||
|
|
||||||
|
|
||||||
|
HARNESS = r'''
|
||||||
|
#include <windows.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
#define PLAT_CHILD_READ_MS 300
|
||||||
|
#include "platform/platform.h"
|
||||||
|
#define WPATH 2048
|
||||||
|
#define LINE_MAX_LEN 4096
|
||||||
|
static int widen(const char *s, wchar_t *w, int cap) { return MultiByteToWideChar(CP_UTF8, 0, s, -1, w, cap) > 0; }
|
||||||
|
static volatile int64_t n_requests;
|
||||||
|
static int starts;
|
||||||
|
static const char *child_arg;
|
||||||
|
'''
|
||||||
|
|
||||||
|
STUBS = r'''
|
||||||
|
static Svc one;
|
||||||
|
static int child_start(Svc *v) {
|
||||||
|
uint32_t err = 0;
|
||||||
|
starts++;
|
||||||
|
v->child = plat_child_start("C:\\Windows\\System32\\cmd.exe", child_arg, &err);
|
||||||
|
v->in_len = 0;
|
||||||
|
return v->child != NULL;
|
||||||
|
}
|
||||||
|
static Svc *svc_get(void) { return &one; }
|
||||||
|
static void svc_put(Svc *v) { (void)v; }
|
||||||
|
static int child_exchange(Svc *v, const char *line, char *reply, size_t reply_size) {
|
||||||
|
return child_exchange_raw(v, line, reply, reply_size);
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
MAIN = r'''
|
||||||
|
static const char *SILENT = "/c ping -n 30 127.0.0.1 >NUL", *GONE = "/c exit 0", *ANSWER = "/c echo ok 7";
|
||||||
|
|
||||||
|
static int request(const char *arg, char *reply, size_t n, ULONGLONG *ms) {
|
||||||
|
child_arg = arg, starts = 0;
|
||||||
|
ULONGLONG t0 = GetTickCount64();
|
||||||
|
int r = svc_request("job", reply, n);
|
||||||
|
*ms = GetTickCount64() - t0;
|
||||||
|
if (one.child) child_stop(&one);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
char buf[64], reply[256];
|
||||||
|
ULONGLONG ms;
|
||||||
|
|
||||||
|
/* plat_child_read alone */
|
||||||
|
PlatChild *c = plat_child_start("C:\\Windows\\System32\\cmd.exe", SILENT, NULL);
|
||||||
|
assert(c);
|
||||||
|
ULONGLONG t0 = GetTickCount64();
|
||||||
|
long got = plat_child_read(c, buf, sizeof buf);
|
||||||
|
ms = GetTickCount64() - t0;
|
||||||
|
printf("silent child: plat_child_read %ld after %llu ms\n", got, (unsigned long long)ms);
|
||||||
|
assert(got == -2 && ms >= PLAT_CHILD_READ_MS && ms < 1000);
|
||||||
|
plat_child_stop(c);
|
||||||
|
c = plat_child_start("C:\\Windows\\System32\\cmd.exe", GONE, NULL);
|
||||||
|
assert(c);
|
||||||
|
got = plat_child_read(c, buf, sizeof buf);
|
||||||
|
printf("exited child: plat_child_read %ld\n", got);
|
||||||
|
assert(got == 0 || got == -1);
|
||||||
|
plat_child_stop(c);
|
||||||
|
|
||||||
|
/* svc_request */
|
||||||
|
int r = request(SILENT, reply, sizeof reply, &ms);
|
||||||
|
printf("silent child: svc_request %d, %d children, %llu ms\n", r, starts, (unsigned long long)ms);
|
||||||
|
assert(r == -1 && starts == 1 && ms < 2 * PLAT_CHILD_READ_MS);
|
||||||
|
r = request(GONE, reply, sizeof reply, &ms);
|
||||||
|
printf("exited child: svc_request %d, %d children, %llu ms\n", r, starts, (unsigned long long)ms);
|
||||||
|
assert(r == 0 && starts == 2);
|
||||||
|
r = request(ANSWER, reply, sizeof reply, &ms);
|
||||||
|
printf("answering child: svc_request %d, %d children, reply '%s'\n", r, starts, reply);
|
||||||
|
assert(r == 1 && starts == 1 && !strcmp(reply, "ok 7"));
|
||||||
|
|
||||||
|
printf("child timeout check: a silent child costs one timeout and is not asked again; a dead one is restarted once\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if os.name != 'nt':
|
||||||
|
print('child timeout check: Windows only (win32.c); skipped')
|
||||||
|
return 0
|
||||||
|
win = (ROOT / 'runtime' / 'platform' / 'win32.c').read_text(encoding='utf-8')
|
||||||
|
gfx = (ROOT / 'runtime' / 'gfx_shader.c').read_text(encoding='utf-8')
|
||||||
|
parts = [
|
||||||
|
HARNESS,
|
||||||
|
extract(win, 'struct PlatChild {', ';'),
|
||||||
|
extract(win, 'PlatChild *plat_child_start('),
|
||||||
|
extract(win, 'void plat_child_stop('),
|
||||||
|
extract(win, 'int plat_child_write('),
|
||||||
|
extract(win, 'long plat_child_read('),
|
||||||
|
extract(gfx, 'typedef struct { // one translator child', ' Svc;'),
|
||||||
|
'static int child_start(Svc *v);\n',
|
||||||
|
extract(gfx, 'static void child_stop('),
|
||||||
|
extract(gfx, 'static int child_exchange_raw('),
|
||||||
|
STUBS,
|
||||||
|
extract(gfx, 'static int svc_request('),
|
||||||
|
MAIN,
|
||||||
|
]
|
||||||
|
work = ROOT / 'build' / 'test' / 'child_timeout'
|
||||||
|
work.mkdir(parents=True, exist_ok=True)
|
||||||
|
c = work / 'check.c'
|
||||||
|
c.write_text(''.join(parts), encoding='utf-8')
|
||||||
|
exe = work / 'check.exe'
|
||||||
|
env = dict(os.environ)
|
||||||
|
if MSYS_BIN.exists():
|
||||||
|
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
||||||
|
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-march=x86-64-v3',
|
||||||
|
'-I', str(ROOT / 'runtime'), str(c), '-o', str(exe)], env=env)
|
||||||
|
return subprocess.run([str(exe)], env=env, timeout=60).returncode
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user