diff options
| author | eunhhu <[email protected]> | 2026-09-24 17:14:54 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-09-24 17:14:54 +0100 |
| commit | b1ce1927022d374198441afafeb27199fc8a052a (patch) | |
| tree | 6021f175f4c67ed03b44240eaa2f90d846f12f6f /prototypes/connections/measure-peak-rss.c | |
| parent | d34f9426d5317a1948f12f229439946728bc342a (diff) | |
| download | pure-b1ce1927022d374198441afafeb27199fc8a052a.tar.gz pure-b1ce1927022d374198441afafeb27199fc8a052a.zip | |
Establish native micro runtime checkpoint
Diffstat (limited to 'prototypes/connections/measure-peak-rss.c')
| -rw-r--r-- | prototypes/connections/measure-peak-rss.c | 43 |
1 files changed, 43 insertions, 0 deletions
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 <fcntl.h> +#include <stdio.h> +#include <sys/resource.h> +#include <sys/wait.h> +#include <unistd.h> + +/* 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; +} |
