From 15292e2b25cfc977202dfcb75e90ec938d2ab55a Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sat, 14 Nov 2026 14:03:21 +0100 Subject: [PATCH] test(subprocess): sample one process tree idle and loaded subprocess_tree_rss_measures_contained_descendants compared two independently spawned process trees, one idle and one whose descendant touched 128 MiB, and required the loaded total to be at least 96 MiB above the idle total. Two separate trees need not start from the same footprint. On the hosted Windows CLANG64 leg they did not: the loaded tree measured only 78 MiB above the idle one (167 MiB vs 245 MiB), and the test failed on two unrelated PRs within two days (#2965 on 889170ca, #2975 run 43499140720). The diagnosis that the two separate trees are the cause comes from #2965. The test now samples ONE tree twice, and every wait in the handshake is a blocking pipe read instead of a poll of a ready file: - The test creates two pipes, "up" (tree to test) and "down" (test to tree): FIFOs in its private temporary directory on POSIX, named pipes named after that directory's unique leaf on Windows. The __cbm_rss_tree_root role connects both as its first action and starts one __cbm_rss_tree_hold descendant that inherits the connected ends (fork on POSIX, an explicit two-handle inherit list on Windows); the root then drops its own copies. - The descendant writes 'I'; the test takes the idle sample and writes 'L'; the descendant touches every page of a 192 MiB load and writes 'R'; the test takes the loaded sample and closes its ends, which releases the descendant to free the load and exit. - A descendant that dies closes its ends, so the waiting read returns end-of-file instead of blocking. No handshake step has a sleep, a poll interval or a deadline. The floor stays at 96 MiB and is now half the load: the root does not grow between the two samples, so a probe that counts only the root misses the floor by 96 MiB, and a probe that counts the descendant clears it with 96 MiB to spare. The two member-fault tests use the same handshake and keep their assertions. Production code is unchanged. Evidence: - macOS arm64, ASan: subprocess suite 60 passed, 2 skipped (the Windows-only Job Object probes); the rewritten test passed 20 of 20 consecutive runs, idle 43 MiB and loaded 259 MiB in every run. - Binding: with the macOS probe temporarily limited to the root process, the test fails at the floor assertion (idle 33 MiB, loaded 33 MiB); restoring the probe returns the suite to green. - Linux arm64 container, gcc ASan: subprocess suite 60 passed, 2 skipped; the rewritten test passed 20 of 20 consecutive runs, idle 462 MiB and loaded 654 MiB in every run. - Windows: both changed files compile with -fsyntax-only -Wall -Wextra -Werror under llvm-mingw for x86_64 and aarch64. Signed-off-by: Martin Vogel --- tests/test_main.c | 230 +++++++++++++++++++++++++------- tests/test_subprocess.c | 286 +++++++++++++++++++++++++++++++--------- 2 files changed, 407 insertions(+), 109 deletions(-) diff --git a/tests/test_main.c b/tests/test_main.c index 4b3716c50c..189ad8e930 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -44,6 +44,8 @@ int tf_deselected_count = 0; #include #include "foundation/win_utf8.h" /* cbm_module_path_utf8 — the runner's own image */ #else +#include /* open: the process-tree probe's handshake FIFOs */ +#include /* INT_MAX: a handed-down descriptor */ #include /* waitpid: the process-tree probe root */ #include #ifdef __APPLE__ @@ -1975,79 +1977,216 @@ static int tf_role_env_isolation(int argc, char **argv) { return 0; } -/* Process-tree memory probe coverage (test_subprocess.c). The root starts ONE - * descendant that charges a controlled amount of memory, so a tree total can - * be checked against an allocation only a descendant holds. The descendant - * publishes its pid by an atomic rename once every page is touched, then - * blocks until it is killed. The root exits when its descendant dies, so a - * failed setup ends the tree instead of leaving the test waiting. */ -static int tf_rss_tree_hold(const char *ready_path, const char *mib_text) { +/* Process-tree memory probe coverage (test_subprocess.c). The test creates + * two pipes, "up" (tree -> test) and "down" (test -> tree), and starts the + * root with their names. The root connects both FIRST -- the test is blocked + * until it has -- then starts ONE descendant that inherits the connected ends + * and drops its own copies, so the descendant alone holds the tree's side. + * The descendant and the test then move in lock step, each blocked in a pipe + * read until the other side writes: + * + * descendant: 'I' up started, nothing allocated (test: idle sample) + * test: 'L' down allocate the load + * descendant: 'R' up every page of the load is touched (test: loaded sample) + * test: closes the descendant frees the load and exits, then the root + * + * A read whose writer died returns end-of-file, so a failed descendant ends + * the wait on the other side instead of leaving it blocked. */ +#ifdef _WIN32 +typedef HANDLE tf_rss_pipe_t; +#define TF_RSS_NO_PIPE INVALID_HANDLE_VALUE +#else +typedef int tf_rss_pipe_t; +#define TF_RSS_NO_PIPE (-1) +#endif + +static bool tf_rss_tree_say(tf_rss_pipe_t channel, char byte) { +#ifdef _WIN32 + DWORD written = 0; + return WriteFile(channel, &byte, 1, &written, NULL) && written == 1; +#else + ssize_t count; + do { + count = write(channel, &byte, 1); + } while (count < 0 && errno == EINTR); + return count == 1; +#endif +} + +/* Blocks until the test writes; false once it closed its end. */ +static bool tf_rss_tree_hear(tf_rss_pipe_t channel, char *byte) { +#ifdef _WIN32 + DWORD got = 0; + return ReadFile(channel, byte, 1, &got, NULL) && got == 1; +#else + ssize_t count; + do { + count = read(channel, byte, 1); + } while (count < 0 && errno == EINTR); + return count == 1; +#endif +} + +static int tf_rss_tree_hold(tf_rss_pipe_t up, tf_rss_pipe_t down, const char *mib_text) { char *end = NULL; long mib = strtol(mib_text, &end, 10); if (!end || *end != '\0' || mib < 0 || mib > 1024) { return 81; } + if (!tf_rss_tree_say(up, 'I')) { + return 83; + } + char heard = 0; + if (!tf_rss_tree_hear(down, &heard)) { + return 0; /* released before the load */ + } + if (heard != 'L') { + return 84; + } size_t bytes = (size_t)mib * 1024U * 1024U; unsigned char *block = bytes > 0 ? malloc(bytes) : NULL; if (bytes > 0 && !block) { return 82; } - if (block) { - memset(block, 0xA5, bytes); + /* One store per 4 KiB reaches every page at every page size in use (4 KiB, + * 16 KiB on Apple silicon). volatile keeps the compiler from dropping + * stores to memory nothing reads back. */ + volatile unsigned char *page = block; + for (size_t offset = 0; offset < bytes; offset += 4096U) { + page[offset] = 0xA5; } - char staged[1024]; - int length = snprintf(staged, sizeof(staged), "%s.tmp", ready_path); - FILE *ready = length > 0 && (size_t)length < sizeof(staged) ? cbm_fopen(staged, "wb") : NULL; - if (!ready) { - return 83; + int code = tf_rss_tree_say(up, 'R') ? 0 : 83; + if (code == 0) { + (void)tf_rss_tree_hear(down, &heard); /* hold the load until released */ + } + free(block); + return code; +} + +static bool tf_rss_tree_parse_pipe(const char *text, tf_rss_pipe_t *channel) { + char *end = NULL; + errno = 0; + unsigned long long value = strtoull(text, &end, 10); + if (!text[0] || !end || *end != '\0' || errno != 0) { + return false; } #ifdef _WIN32 - (void)fprintf(ready, "%lu\n", (unsigned long)GetCurrentProcessId()); + *channel = (HANDLE)(uintptr_t)value; #else - (void)fprintf(ready, "%ld\n", (long)getpid()); + if (value > (unsigned long long)INT_MAX) { + return false; + } + *channel = (int)value; #endif - if (fclose(ready) != 0 || rename(staged, ready_path) != 0) { - return 84; + return true; +} + +/* The descendant as its own image (Windows): argv names the inherited ends. */ +static int tf_role_rss_tree_hold(int argc, char **argv) { + if (argc != 5) { + return -1; } - volatile unsigned char keep = block ? block[bytes - 1] : 0; - (void)keep; - for (;;) { + tf_rss_pipe_t up = TF_RSS_NO_PIPE; + tf_rss_pipe_t down = TF_RSS_NO_PIPE; + if (!tf_rss_tree_parse_pipe(argv[2], &up) || !tf_rss_tree_parse_pipe(argv[3], &down)) { + return 80; + } + return tf_rss_tree_hold(up, down, argv[4]); +} + +static tf_rss_pipe_t tf_rss_tree_connect(const char *name, bool writer) { #ifdef _WIN32 - Sleep(INFINITE); + /* Inheritable: the descendant receives this end through the handle list. */ + SECURITY_ATTRIBUTES inheritable; + ZeroMemory(&inheritable, sizeof(inheritable)); + inheritable.nLength = sizeof(inheritable); + inheritable.bInheritHandle = TRUE; + wchar_t *wide = cbm_utf8_to_wide(name); + HANDLE channel = wide ? CreateFileW(wide, writer ? GENERIC_WRITE : GENERIC_READ, 0, + &inheritable, OPEN_EXISTING, 0, NULL) + : INVALID_HANDLE_VALUE; + free(wide); + return channel; #else - (void)pause(); + int channel; + do { + channel = open(name, writer ? O_WRONLY : O_RDONLY); + } while (channel < 0 && errno == EINTR); + return channel; #endif - } } -static int tf_role_rss_tree_hold(int argc, char **argv) { - return argc == 4 ? tf_rss_tree_hold(argv[2], argv[3]) : -1; +static void tf_rss_tree_drop(tf_rss_pipe_t channel) { + if (channel == TF_RSS_NO_PIPE) { + return; + } +#ifdef _WIN32 + (void)CloseHandle(channel); +#else + (void)close(channel); +#endif } -static int tf_role_rss_tree_root(int argc, char **argv) { - if (argc != 4) { - return -1; - } #ifdef _WIN32 +/* Start the descendant with exactly the two pipe ends inherited. The child + * joins this process's Job Object by default, which is the tree the probe + * under test enumerates. */ +static bool tf_rss_tree_start_descendant(HANDLE up, HANDLE down, const char *mib_text, + PROCESS_INFORMATION *child) { + char up_text[32]; + char down_text[32]; + (void)snprintf(up_text, sizeof(up_text), "%llu", (unsigned long long)(uintptr_t)up); + (void)snprintf(down_text, sizeof(down_text), "%llu", (unsigned long long)(uintptr_t)down); char *self = cbm_module_path_utf8(); - const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], NULL}; + const char *child_argv[] = {self, "__cbm_rss_tree_hold", up_text, down_text, mib_text, NULL}; char command_line[8192]; bool built = self && cbm_build_win_cmdline(command_line, sizeof(command_line), child_argv); wchar_t *wide_self = built ? cbm_utf8_to_wide(self) : NULL; wchar_t *wide_command = built ? cbm_utf8_to_wide(command_line) : NULL; - STARTUPINFOW startup; + HANDLE inherit[2] = {up, down}; + SIZE_T attrs_size = 0; + (void)InitializeProcThreadAttributeList(NULL, 1, 0, &attrs_size); + LPPROC_THREAD_ATTRIBUTE_LIST attrs = (LPPROC_THREAD_ATTRIBUTE_LIST)malloc(attrs_size); + bool attrs_init = attrs && InitializeProcThreadAttributeList(attrs, 1, 0, &attrs_size); + bool attrs_ready = + attrs_init && UpdateProcThreadAttribute(attrs, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, + inherit, sizeof(inherit), NULL, NULL); + STARTUPINFOEXW startup; ZeroMemory(&startup, sizeof(startup)); - startup.cb = sizeof(startup); - PROCESS_INFORMATION child; - ZeroMemory(&child, sizeof(child)); - /* The child joins this process's Job Object by default, which is the tree - * the probe under test enumerates. */ - BOOL started = wide_self && wide_command && - CreateProcessW(wide_self, wide_command, NULL, NULL, FALSE, 0, NULL, NULL, - &startup, &child); + startup.StartupInfo.cb = sizeof(startup); + startup.lpAttributeList = attrs; + ZeroMemory(child, sizeof(*child)); + BOOL started = + wide_self && wide_command && attrs_ready && + CreateProcessW(wide_self, wide_command, NULL, NULL, TRUE, EXTENDED_STARTUPINFO_PRESENT, + NULL, NULL, &startup.StartupInfo, child); + if (attrs_init) { + DeleteProcThreadAttributeList(attrs); + } + free(attrs); free(wide_self); free(wide_command); free(self); + return started != FALSE; +} +#endif + +static int tf_role_rss_tree_root(int argc, char **argv) { + if (argc != 5) { + return -1; + } + /* Connect before anything else: the test's first blocking step waits for + * these two ends, in this order. */ + tf_rss_pipe_t up = tf_rss_tree_connect(argv[2], true); + tf_rss_pipe_t down = + up != TF_RSS_NO_PIPE ? tf_rss_tree_connect(argv[3], false) : TF_RSS_NO_PIPE; + bool connected = up != TF_RSS_NO_PIPE && down != TF_RSS_NO_PIPE; +#ifdef _WIN32 + PROCESS_INFORMATION child; + bool started = connected && tf_rss_tree_start_descendant(up, down, argv[4], &child); + /* From here the descendant holds the tree's only copies of both ends. */ + tf_rss_tree_drop(up); + tf_rss_tree_drop(down); if (!started) { return 85; } @@ -2055,13 +2194,16 @@ static int tf_role_rss_tree_root(int argc, char **argv) { (void)WaitForSingleObject(child.hProcess, INFINITE); CloseHandle(child.hProcess); #else - pid_t child = fork(); + pid_t child = connected ? fork() : -1; + if (child == 0) { + _exit(tf_rss_tree_hold(up, down, argv[4])); + } + /* From here the descendant holds the tree's only copies of both ends. */ + tf_rss_tree_drop(up); + tf_rss_tree_drop(down); if (child < 0) { return 85; } - if (child == 0) { - _exit(tf_rss_tree_hold(argv[2], argv[3])); - } int status = 0; while (waitpid(child, &status, 0) < 0 && errno == EINTR) { } diff --git a/tests/test_subprocess.c b/tests/test_subprocess.c index e214a2c36f..50a7b4a274 100644 --- a/tests/test_subprocess.c +++ b/tests/test_subprocess.c @@ -808,10 +808,16 @@ TEST(subprocess_spawn_returns_while_child_is_running) { } /* ── Process-tree memory probe (every platform) ───────────────────────────── - * The test runner's __cbm_rss_tree_root role starts one __cbm_rss_tree_hold - * descendant that touches a controlled allocation and then publishes its pid. - * Waits below have no deadline: they end when the descendant is ready or the - * tree stops running, so the outcome, not a timer, decides the verdict. */ + * One tree per sample. The test creates two pipes, "up" (tree -> test) and + * "down" (test -> tree); the runner's __cbm_rss_tree_root role connects both + * before anything else, then starts ONE __cbm_rss_tree_hold descendant that + * alone holds the tree's ends (protocol: tf_rss_tree_hold in test_main.c). + * The test takes the idle sample, lets the descendant allocate, and takes the + * loaded sample of the SAME processes, each step blocked in a pipe read until + * the other side has written. A descendant that dies closes its ends, so the + * read waiting on it returns end-of-file: no step has a deadline, a sleep or a + * poll interval. Connecting is the one wait end-of-file cannot cover; the + * root does it as its first action. */ #ifdef _WIN32 #define RSS_TREE_FAULT_GONE ERROR_INVALID_PARAMETER /* no process with that id any more */ @@ -826,9 +832,27 @@ TEST(subprocess_spawn_returns_while_child_is_running) { #define RSS_TREE_MIB UINT64_C(1048576) +#ifdef _WIN32 +typedef HANDLE rss_tree_pipe_t; +#define RSS_TREE_NO_PIPE INVALID_HANDLE_VALUE +#else +typedef int rss_tree_pipe_t; +#define RSS_TREE_NO_PIPE (-1) +#endif + +typedef struct { + char up_name[300]; /* the tree writes, the test reads */ + char down_name[300]; /* the test writes, the tree reads */ + rss_tree_pipe_t up; + rss_tree_pipe_t down; +} rss_tree_pipes_t; + typedef struct { bool spawned; - bool ready; + bool idle_ready; /* the descendant said 'I': started, nothing allocated */ + cbm_proc_tree_rss_status_t idle_status; + uint64_t idle_rss; + bool ready; /* the descendant said 'R': every page of the load touched */ cbm_proc_tree_rss_status_t status; uint64_t rss; bool quiesced; @@ -840,25 +864,138 @@ static void rss_tree_pause(void) { cbm_usleep(10000); /* poll interval only; nothing here is a deadline */ } -static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *process) { - for (;;) { - FILE *ready = cbm_fopen(ready_path, "rb"); - if (ready) { - long pid = 0; - int fields = fscanf(ready, "%ld", &pid); - (void)fclose(ready); - if (fields == 1 && pid > 0) { - return true; - } +#ifdef _WIN32 +static HANDLE rss_tree_pipe_serve(const char *name, DWORD direction) { + wchar_t *wide = cbm_utf8_to_wide(name); + HANDLE channel = wide ? CreateNamedPipeW(wide, direction | FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + 1, 64, 64, 0, NULL) + : INVALID_HANDLE_VALUE; + free(wide); + return channel; +} + +/* The client may connect before this call (ERROR_PIPE_CONNECTED) or connect + * and close again (ERROR_NO_DATA). Both are connected; the next read or write + * reports whether the other side is still there. */ +static bool rss_tree_pipe_accept(HANDLE channel) { + if (ConnectNamedPipe(channel, NULL)) { + return true; + } + DWORD error = GetLastError(); + return error == ERROR_PIPE_CONNECTED || error == ERROR_NO_DATA; +} +#else +static int rss_tree_fifo_open(const char *name, int mode) { + int channel; + do { + channel = open(name, mode | O_CLOEXEC); + } while (channel < 0 && errno == EINTR); + return channel; +} +#endif + +/* Pipes named after the private temporary directory, so concurrent runners + * never meet: FIFOs inside it on POSIX, named pipes after its unique leaf on + * Windows, created before the spawn so the root's connect finds them. */ +static bool rss_tree_pipes_create(rss_tree_pipes_t *pipes, const char *dir) { + pipes->up = RSS_TREE_NO_PIPE; + pipes->down = RSS_TREE_NO_PIPE; +#ifdef _WIN32 + const char *leaf = dir; + for (const char *cursor = dir; *cursor; cursor++) { + if (*cursor == '\\' || *cursor == '/') { + leaf = cursor + 1; } - cbm_proc_result_t ignored; - if (cbm_subprocess_poll(process, &ignored) != CBM_PROC_POLL_RUNNING) { - return false; + } + int up_length = snprintf(pipes->up_name, sizeof(pipes->up_name), "\\\\.\\pipe\\%s-up", leaf); + int down_length = + snprintf(pipes->down_name, sizeof(pipes->down_name), "\\\\.\\pipe\\%s-down", leaf); +#else + int up_length = snprintf(pipes->up_name, sizeof(pipes->up_name), "%s/up", dir); + int down_length = snprintf(pipes->down_name, sizeof(pipes->down_name), "%s/down", dir); +#endif + if (up_length <= 0 || (size_t)up_length >= sizeof(pipes->up_name) || down_length <= 0 || + (size_t)down_length >= sizeof(pipes->down_name)) { + return false; + } +#ifdef _WIN32 + pipes->up = rss_tree_pipe_serve(pipes->up_name, PIPE_ACCESS_INBOUND); + pipes->down = rss_tree_pipe_serve(pipes->down_name, PIPE_ACCESS_OUTBOUND); + return pipes->up != INVALID_HANDLE_VALUE && pipes->down != INVALID_HANDLE_VALUE; +#else + return mkfifo(pipes->up_name, 0600) == 0 && mkfifo(pipes->down_name, 0600) == 0; +#endif +} + +/* Blocks until the root has connected both ends, in the order it connects + * them (up, then down). */ +static bool rss_tree_pipes_connect(rss_tree_pipes_t *pipes) { +#ifdef _WIN32 + return rss_tree_pipe_accept(pipes->up) && rss_tree_pipe_accept(pipes->down); +#else + pipes->up = rss_tree_fifo_open(pipes->up_name, O_RDONLY); + pipes->down = pipes->up >= 0 ? rss_tree_fifo_open(pipes->down_name, O_WRONLY) : -1; + return pipes->up >= 0 && pipes->down >= 0; +#endif +} + +/* Closing the test's ends is what releases the descendant to exit. */ +static void rss_tree_pipes_close(rss_tree_pipes_t *pipes) { + rss_tree_pipe_t *ends[] = {&pipes->up, &pipes->down}; + for (size_t index = 0; index < sizeof(ends) / sizeof(ends[0]); index++) { + if (*ends[index] == RSS_TREE_NO_PIPE) { + continue; } - rss_tree_pause(); +#ifdef _WIN32 + (void)CloseHandle(*ends[index]); +#else + (void)close(*ends[index]); +#endif + *ends[index] = RSS_TREE_NO_PIPE; } } +/* Blocks until the descendant writes; false on end-of-file (it is gone) or + * an unexpected byte. */ +static bool rss_tree_hear(rss_tree_pipe_t channel, char expected) { + char byte = 0; +#ifdef _WIN32 + DWORD got = 0; + bool heard = ReadFile(channel, &byte, 1, &got, NULL) && got == 1; +#else + ssize_t count; + do { + count = read(channel, &byte, 1); + } while (count < 0 && errno == EINTR); + bool heard = count == 1; +#endif + return heard && byte == expected; +} + +static bool rss_tree_say(rss_tree_pipe_t channel, char byte) { +#ifdef _WIN32 + DWORD written = 0; + return WriteFile(channel, &byte, 1, &written, NULL) && written == 1; +#else + /* A descendant that already died leaves no reader: report that instead + * of taking SIGPIPE. */ + struct sigaction ignore = {.sa_handler = SIG_IGN}; + struct sigaction previous; + (void)sigemptyset(&ignore.sa_mask); + if (sigaction(SIGPIPE, &ignore, &previous) != 0) { + return false; + } + ssize_t count; + do { + count = write(channel, &byte, 1); + } while (count < 0 && errno == EINTR); + bool restored = sigaction(SIGPIPE, &previous, NULL) == 0; + return count == 1 && restored; +#endif +} + static bool rss_tree_finish(cbm_subprocess_t *process, rss_tree_sample_t *sample) { (void)cbm_subprocess_request_cancel(process); cbm_proc_result_t result; @@ -877,66 +1014,85 @@ static bool rss_tree_finish(cbm_subprocess_t *process, rss_tree_sample_t *sample return result.tree_quiesced; } -/* Measure one tree whose descendant holds `mib` MiB, with every non-root - * member's measurement failing with `member_fault` (0 = real calls). */ +/* Sample one tree idle, then again once its descendant has touched `mib` MiB. + * In the loaded sample every non-root member's measurement fails with + * `member_fault` (0 = real calls). */ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { - rss_tree_sample_t sample = {.status = CBM_PROC_TREE_RSS_ERROR, + rss_tree_sample_t sample = {.idle_status = CBM_PROC_TREE_RSS_ERROR, + .status = CBM_PROC_TREE_RSS_ERROR, .final_status = CBM_PROC_TREE_RSS_ERROR, .final_rss = UINT64_MAX}; const char *made = th_mktempdir("cbm_rss_tree"); char dir[256]; - char ready_path[300]; - char mib_text[16]; - if (!made || snprintf(dir, sizeof(dir), "%s", made) >= (int)sizeof(dir) || - snprintf(ready_path, sizeof(ready_path), "%s/ready", dir) >= (int)sizeof(ready_path)) { + if (!made || snprintf(dir, sizeof(dir), "%s", made) >= (int)sizeof(dir)) { return sample; } - (void)snprintf(mib_text, sizeof(mib_text), "%d", mib); - const char *argv[] = {cbm_test_program_path, "__cbm_rss_tree_root", ready_path, mib_text, - NULL}; - cbm_proc_opts_t opts = {0}; - opts.bin = cbm_test_program_path; - opts.argv = argv; - opts.cancel_grace_ms = 100; - cbm_subprocess_t *process = NULL; - sample.spawned = cbm_subprocess_spawn(&opts, &process) == 0; - if (sample.spawned) { - sample.ready = rss_tree_wait_ready(ready_path, process); - if (sample.ready) { - cbm_subprocess_tree_rss_fault_members_for_testing(member_fault); - sample.status = cbm_subprocess_tree_rss_bytes(process, &sample.rss); - cbm_subprocess_tree_rss_fault_members_for_testing(0); + rss_tree_pipes_t pipes; + if (rss_tree_pipes_create(&pipes, dir)) { + char mib_text[16]; + (void)snprintf(mib_text, sizeof(mib_text), "%d", mib); + const char *argv[] = {cbm_test_program_path, + "__cbm_rss_tree_root", + pipes.up_name, + pipes.down_name, + mib_text, + NULL}; + cbm_proc_opts_t opts = {0}; + opts.bin = cbm_test_program_path; + opts.argv = argv; + opts.cancel_grace_ms = 100; + cbm_subprocess_t *process = NULL; + sample.spawned = cbm_subprocess_spawn(&opts, &process) == 0; + if (sample.spawned) { + sample.idle_ready = rss_tree_pipes_connect(&pipes) && rss_tree_hear(pipes.up, 'I'); + if (sample.idle_ready) { + sample.idle_status = cbm_subprocess_tree_rss_bytes(process, &sample.idle_rss); + } + sample.ready = + sample.idle_ready && rss_tree_say(pipes.down, 'L') && rss_tree_hear(pipes.up, 'R'); + if (sample.ready) { + cbm_subprocess_tree_rss_fault_members_for_testing(member_fault); + sample.status = cbm_subprocess_tree_rss_bytes(process, &sample.rss); + cbm_subprocess_tree_rss_fault_members_for_testing(0); + } + rss_tree_pipes_close(&pipes); + sample.quiesced = rss_tree_finish(process, &sample); } - sample.quiesced = rss_tree_finish(process, &sample); } + rss_tree_pipes_close(&pipes); th_cleanup(dir); return sample; } -/* Only the DESCENDANT allocates, so the difference between two otherwise - * identical trees is memory the probe can only have counted by measuring past - * the root. 128 MiB touched against a 96 MiB floor leaves room for allocator - * and page-rounding noise and none for a root-only total. */ +/* Only the DESCENDANT allocates, and only between two samples of the SAME + * tree while the root sits blocked in its wait, so the growth is memory the + * probe can only have counted by measuring past the root. (Two separately + * spawned trees need not start from the same footprint, so comparing them + * proves nothing.) The floor is half the touched load: a root-only total, + * which does not grow, misses it by the full 96 MiB, and a total that counts + * the descendant clears it with 96 MiB to spare for page rounding, allocator + * bookkeeping, or a working-set trim between the two samples. */ +#define RSS_TREE_LOAD_MIB 192 +#define RSS_TREE_FLOOR_MIB 96 + TEST(subprocess_tree_rss_measures_contained_descendants) { - rss_tree_sample_t idle = rss_tree_sample(0, 0); - rss_tree_sample_t loaded = rss_tree_sample(128, 0); - if (loaded.status == CBM_PROC_TREE_RSS_OK && idle.status == CBM_PROC_TREE_RSS_OK) { - printf(" tree rss: idle=%llu MiB loaded=%llu MiB\n", - (unsigned long long)(idle.rss / RSS_TREE_MIB), - (unsigned long long)(loaded.rss / RSS_TREE_MIB)); - } - ASSERT_TRUE(idle.spawned); - ASSERT_TRUE(idle.ready); - ASSERT_EQ(idle.status, CBM_PROC_TREE_RSS_OK); - ASSERT_TRUE(idle.rss > 0); - ASSERT_TRUE(loaded.spawned); - ASSERT_TRUE(loaded.ready); - ASSERT_EQ(loaded.status, CBM_PROC_TREE_RSS_OK); - ASSERT_TRUE(loaded.rss >= idle.rss + 96 * RSS_TREE_MIB); - ASSERT_TRUE(idle.quiesced); - ASSERT_TRUE(loaded.quiesced); - ASSERT_EQ(loaded.final_status, CBM_PROC_TREE_RSS_EMPTY); - ASSERT_EQ(loaded.final_rss, 0); + rss_tree_sample_t tree = rss_tree_sample(RSS_TREE_LOAD_MIB, 0); + if (tree.idle_status == CBM_PROC_TREE_RSS_OK && tree.status == CBM_PROC_TREE_RSS_OK) { + printf(" tree rss: idle=%llu MiB loaded=%llu MiB (load %d MiB, floor +%d MiB)\n", + (unsigned long long)(tree.idle_rss / RSS_TREE_MIB), + (unsigned long long)(tree.rss / RSS_TREE_MIB), RSS_TREE_LOAD_MIB, + RSS_TREE_FLOOR_MIB); + } + ASSERT_TRUE(tree.spawned); + ASSERT_TRUE(tree.idle_ready); + ASSERT_EQ(tree.idle_status, CBM_PROC_TREE_RSS_OK); + ASSERT_TRUE(tree.idle_rss > 0); + ASSERT_TRUE(tree.ready); + ASSERT_EQ(tree.status, CBM_PROC_TREE_RSS_OK); + ASSERT_TRUE(tree.rss >= tree.idle_rss + RSS_TREE_FLOOR_MIB * RSS_TREE_MIB); + ASSERT_TRUE(tree.quiesced); + ASSERT_EQ(tree.final_status, CBM_PROC_TREE_RSS_EMPTY); + ASSERT_EQ(tree.final_rss, 0); PASS(); }