From b1ce1927022d374198441afafeb27199fc8a052a Mon Sep 17 00:00:00 2001 From: eunhhu Date: Thu, 24 Sep 2026 17:14:54 +0100 Subject: Establish native micro runtime checkpoint --- prototypes/connections/measure-peak-rss.c | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 prototypes/connections/measure-peak-rss.c (limited to 'prototypes/connections/measure-peak-rss.c') diff --git a/prototypes/connections/measure-peak-rss.c b/prototypes/connections/measure-peak-rss.c new file mode 100644 index 0000000..1fe2e0b --- /dev/null +++ b/prototypes/connections/measure-peak-rss.c @@ -0,0 +1,43 @@ +#ifdef __APPLE__ +#define _DARWIN_C_SOURCE +#else +#define _DEFAULT_SOURCE +#endif +#include +#include +#include +#include +#include + +/* Test helper: fork from a small native process so the interpreter's pre-exec + * RSS cannot become the measured child's peak. Not shipped. */ +int main(int argc, char **argv) { + if (argc < 2) return 2; + pid_t child = fork(); + if (child < 0) return 2; + if (!child) { + int null = open("/dev/null", O_WRONLY); + if (null < 0 || dup2(null, STDOUT_FILENO) < 0 || + dup2(null, STDERR_FILENO) < 0) _exit(127); + close(null); + execvp(argv[1], argv + 1); + _exit(127); + } + struct rusage usage; + int status; + if (wait4(child, &status, 0, &usage) < 0) return 2; + if (!WIFEXITED(status)) return 2; + if (usage.ru_maxrss < 0) return 2; +#ifdef __APPLE__ + /* XNU reports ru_maxrss in bytes; Linux reports KiB. */ + long peak_kib = usage.ru_maxrss / 1024 + (usage.ru_maxrss % 1024 != 0); + const char *raw_unit = "bytes"; +#else + long peak_kib = usage.ru_maxrss; + const char *raw_unit = "KiB"; +#endif + printf("{\"exit_code\":%d,\"max_rss_kib\":%ld," + "\"raw_max_rss\":%ld,\"raw_unit\":\"%s\"}\n", + WEXITSTATUS(status), peak_kib, usage.ru_maxrss, raw_unit); + return 0; +} -- cgit v1.2.3