Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
test(subprocess): prove descendant memory is measured on every platform
Martin Vogel33ae8f382e20
diff --git a/tests/test_main.c b/tests/test_main.cindex e0526cf85..23b2621ee 100644--- a/tests/test_main.c+++ b/tests/test_main.c@@ -44,6 +44,7 @@ int tf_deselected_count = 0;#include <windows.h>#include "foundation/win_utf8.h" /* cbm_module_path_utf8 — the runner's own image */#else+#include <sys/wait.h> /* waitpid: the process-tree probe root */#include <unistd.h>#ifdef __APPLE__#include <fcntl.h>@@ -1968,6 +1969,100 @@ 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) {+ char *end = NULL;+ long mib = strtol(mib_text, &end, 10);+ if (!end || *end != '\0' || mib < 0 || mib > 1024) {+ return 81;+ }+ 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);+ }+ 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;+ }+#ifdef _WIN32+ (void)fprintf(ready, "%lu\n", (unsigned long)GetCurrentProcessId());+#else+ (void)fprintf(ready, "%ld\n", (long)getpid());+#endif+ if (fclose(ready) != 0 || rename(staged, ready_path) != 0) {+ return 84;+ }+ volatile unsigned char keep = block ? block[bytes - 1] : 0;+ (void)keep;+ for (;;) {+#ifdef _WIN32+ Sleep(INFINITE);+#else+ (void)pause();+#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 int tf_role_rss_tree_root(int argc, char **argv) {+ if (argc != 4) {+ return -1;+ }+#ifdef _WIN32+ char *self = cbm_module_path_utf8();+ const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], 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;+ 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);+ free(wide_self);+ free(wide_command);+ free(self);+ if (!started) {+ return 85;+ }+ CloseHandle(child.hThread);+ (void)WaitForSingleObject(child.hProcess, INFINITE);+ CloseHandle(child.hProcess);+#else+ pid_t child = fork();+ 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) {+ }+#endif+ return 86; /* the descendant ended: end the tree */+}+/* Installation tests use this executable as a structurally real candidate.* Mirror the production binary's minimal verification contract. */static int tf_role_version(int argc, char **argv) {@@ -2060,6 +2155,8 @@ typedef struct {static const tf_role_t tf_roles[] = {TF_ROLE(TF_PHASE_RAW, "__cbm_env_values", tf_role_env_values),TF_ROLE(TF_PHASE_RAW, "__cbm_env_isolation", tf_role_env_isolation),+ TF_ROLE(TF_PHASE_RAW, "__cbm_rss_tree_root", tf_role_rss_tree_root),+ TF_ROLE(TF_PHASE_RAW, "__cbm_rss_tree_hold", tf_role_rss_tree_hold),TF_ROLE(TF_PHASE_GIT_CLEAN, "__cbm_subprocess_stdin_probe", tf_role_subprocess_stdin_probe),TF_ROLE(TF_PHASE_GIT_CLEAN, "__cbm_subprocess_stdout_probe", tf_role_subprocess_stdout_probe),TF_ROLE(TF_PHASE_GIT_CLEAN, "__cbm_git_facts_diff_probe", tf_role_git_facts_diff_probe),diff --git a/tests/test_subprocess.c b/tests/test_subprocess.cindex 385293e08..1c5039e87 100644--- a/tests/test_subprocess.c+++ b/tests/test_subprocess.c@@ -807,43 +807,123 @@ TEST(subprocess_spawn_returns_while_child_is_running) {#endif}-TEST(subprocess_tree_rss_measures_contained_descendants) {-#ifdef _WIN32- SKIP_PLATFORM("native Job Object process-tree RSS runs on Windows CI");-#else- char pid_path[64];- ASSERT_TRUE(make_tree_pid_path(pid_path));- cbm_subprocess_t *process = NULL;- ASSERT_EQ(spawn_ignoring_tree(pid_path, 0, 100, &process), 0);- pid_t parent_pid = 0;- pid_t grandchild_pid = 0;- bool ready = wait_for_tree_pids(pid_path, process, &parent_pid, &grandchild_pid, 3000);- uint64_t rss_bytes = 0;- cbm_proc_tree_rss_status_t rss_status =- ready ? cbm_subprocess_tree_rss_bytes(process, &rss_bytes) : CBM_PROC_TREE_RSS_ERROR;- bool cancel_accepted = cbm_subprocess_request_cancel(process);+/* ── 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. */++#define RSS_TREE_MIB UINT64_C(1048576)++typedef struct {+ bool spawned;+ bool ready;+ cbm_proc_tree_rss_status_t status;+ uint64_t rss;+ bool quiesced;+ cbm_proc_tree_rss_status_t final_status;+ uint64_t final_rss;+} rss_tree_sample_t;++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;+ }+ }+ cbm_proc_result_t ignored;+ if (cbm_subprocess_poll(process, &ignored) != CBM_PROC_POLL_RUNNING) {+ return false;+ }+ rss_tree_pause();+ }+}++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;- bool terminal = poll_until_terminal(process, 5000, &result);- uint64_t final_rss = UINT64_MAX;- cbm_proc_tree_rss_status_t final_rss_status =- terminal ? cbm_subprocess_tree_rss_bytes(process, &final_rss) : CBM_PROC_TREE_RSS_ERROR;- if (terminal) {- cbm_subprocess_destroy(process);- } else {- force_probe_cleanup(parent_pid, grandchild_pid);+ for (;;) {+ cbm_proc_poll_t state = cbm_subprocess_poll(process, &result);+ if (state == CBM_PROC_POLL_TERMINAL) {+ break;+ }+ if (state == CBM_PROC_POLL_ERROR) {+ return false;+ }+ rss_tree_pause();}- (void)unlink(pid_path);+ sample->final_status = cbm_subprocess_tree_rss_bytes(process, &sample->final_rss);+ cbm_subprocess_destroy(process);+ return result.tree_quiesced;+}++/* Measure one tree whose descendant holds `mib` MiB. */+static rss_tree_sample_t rss_tree_sample(int mib) {+ rss_tree_sample_t sample = {.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)) {+ 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) {+ sample.status = cbm_subprocess_tree_rss_bytes(process, &sample.rss);+ }+ sample.quiesced = rss_tree_finish(process, &sample);+ }+ th_cleanup(dir);+ return sample;+}- ASSERT_TRUE(ready);- ASSERT_EQ(rss_status, CBM_PROC_TREE_RSS_OK);- ASSERT_TRUE(rss_bytes > 0);- ASSERT_TRUE(cancel_accepted);- ASSERT_TRUE(terminal);- ASSERT_TRUE(result.tree_quiesced);- ASSERT_EQ(final_rss_status, CBM_PROC_TREE_RSS_EMPTY);- ASSERT_EQ(final_rss, 0);+/* 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. */+TEST(subprocess_tree_rss_measures_contained_descendants) {+ rss_tree_sample_t idle = rss_tree_sample(0);+ rss_tree_sample_t loaded = rss_tree_sample(128);+ 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);PASS();-#endif}TEST(subprocess_natural_completion_is_cached_across_polls) {