From 625d3d275a4a92fff2408a3e0a7bae6db0dbb200 Mon Sep 17 00:00:00 2001 From: Farzin Monsef Date: Sat, 2 Mar 2024 13:54:57 +0330 Subject: [PATCH] cygwin: implement uv_resident_set_memory (#4333) According to the documentation for Cygwin, the penultimate field of /proc/pid/stat corresponds to the RSS, so the method is basically the same as in the Linux version. The only difference is that getpagesize() will return wincap.allocation_granularity(), but in this mapping, RSS is calculated using wincap.page_size(), which can be accessed by sysinfo.mem_unit. --- src/unix/cygwin.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/unix/cygwin.c b/src/unix/cygwin.c index 4e541396..49131082 100644 --- a/src/unix/cygwin.c +++ b/src/unix/cygwin.c @@ -36,9 +36,45 @@ int uv_uptime(double* uptime) { } int uv_resident_set_memory(size_t* rss) { - /* FIXME: read /proc/meminfo? */ - *rss = 0; + char buf[1024]; + const char* s; + long val; + int rc; + int i; + struct sysinfo si; + + /* rss: 24th element */ + rc = uv__slurp("/proc/self/stat", buf, sizeof(buf)); + if (rc < 0) + return rc; + + /* find the last ')' */ + s = strrchr(buf, ')'); + if (s == NULL) + goto err; + + for (i = 1; i <= 22; i++) { + s = strchr(s + 1, ' '); + if (s == NULL) + goto err; + } + + errno = 0; + val = strtol(s, NULL, 10); + if (val < 0 || errno != 0) + goto err; + + do + rc = sysinfo(&si); + while (rc == -1 && errno == EINTR); + if (rc == -1) + return UV__ERR(errno); + + *rss = val * si.mem_unit; return 0; + +err: + return UV_EINVAL; } int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {