From 1d1d85f1c36bb86b4648c5c5c3afb4b41b0f7c2a Mon Sep 17 00:00:00 2001 From: Ashutosh Bapat Date: Mon, 6 Apr 2026 10:58:29 +0530 Subject: [PATCH v20260406 3/6] Add more diagnostics about shared memory segments NOT FOR FINAL COMMIT Log size, RSS and Swap of every shared memory segment mapped by the backend. This will be useful to understand the failure on CFBot where we are seeing about 10MB extra shared memory allocated that expected. --- .../modules/resizable_shmem/resizable_shmem.c | 93 +++++++++++++++++-- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/src/test/modules/resizable_shmem/resizable_shmem.c b/src/test/modules/resizable_shmem/resizable_shmem.c index 2063d05053f..66754582b32 100644 --- a/src/test/modules/resizable_shmem/resizable_shmem.c +++ b/src/test/modules/resizable_shmem/resizable_shmem.c @@ -254,8 +254,11 @@ resizable_shmem_read(PG_FUNCTION_ARGS) * backend. * * The VMA containing our resizable_shmem pointer is used to determine the main - * memory segment. RSS + Swap (in bytes) for that VMS from /proc/self/smaps is + * memory segment. RSS + Swap (in bytes) for that VMA from /proc/self/smaps is * returned. + * + * As a side effect, all shared-memory VMAs are logged with their name, RSS, + * and Swap values for diagnostic purposes. */ Datum resizable_shmem_usage(PG_FUNCTION_ARGS) @@ -268,6 +271,14 @@ resizable_shmem_usage(PG_FUNCTION_ARGS) bool in_target_vma = false; size_t result; + /* State for logging shared VMAs */ + bool in_shared_vma = false; + char vma_name[256]; + char vma_range[64]; + int64 vma_size_kb = -1; + int64 vma_rss_kb = -1; + int64 vma_swap_kb = -1; + f = fopen("/proc/self/smaps", "r"); if (f == NULL) ereport(ERROR, @@ -278,22 +289,86 @@ resizable_shmem_usage(PG_FUNCTION_ARGS) { unsigned long start; unsigned long end; + char perms[5]; + unsigned long offset; + char dev[12]; + unsigned long inode; + char pathname[256]; + int nfields; - if (sscanf(line, "%lx-%lx", &start, &end) == 2) + nfields = sscanf(line, "%lx-%lx %4s %lx %11s %lu %255[^\n]", + &start, &end, perms, &offset, dev, &inode, pathname); + + if (nfields >= 6) { + /* + * We've hit a new VMA header. First, log the previous shared VMA + * if we were tracking one. + */ + if (in_shared_vma) + elog(LOG, "shared VMA %s %s: Size=%ld kB, Rss=%ld kB, Swap=%ld kB", + vma_range, vma_name, + (long) (vma_size_kb >= 0 ? vma_size_kb : 0), + (long) (vma_rss_kb >= 0 ? vma_rss_kb : 0), + (long) (vma_swap_kb >= 0 ? vma_swap_kb : 0)); + + /* Check if this VMA is a shared mapping (has 's' in perms) */ + in_shared_vma = (perms[3] == 's'); + if (in_shared_vma) + { + snprintf(vma_range, sizeof(vma_range), "%lx-%lx", start, end); + if (nfields >= 7) + strlcpy(vma_name, pathname, sizeof(vma_name)); + else + strlcpy(vma_name, "(anonymous)", sizeof(vma_name)); + vma_size_kb = -1; + vma_rss_kb = -1; + vma_swap_kb = -1; + } + + /* Track the target VMA for our return value */ in_target_vma = (target >= start && target < end); + if (in_target_vma) + { + rss_kb = -1; + swap_kb = -1; + } } - else if (in_target_vma) + else { - if (rss_kb == -1) - sscanf(line, "Rss: %ld kB", &rss_kb); - if (swap_kb == -1) - sscanf(line, "Swap: %ld kB", &swap_kb); - if (rss_kb >= 0 && swap_kb >= 0) - break; + /* Parse detail lines for the current VMA */ + int64 val; + + if (sscanf(line, "Size: %ld kB", &val) == 1) + { + if (in_shared_vma && vma_size_kb == -1) + vma_size_kb = val; + } + else if (sscanf(line, "Rss: %ld kB", &val) == 1) + { + if (in_target_vma && rss_kb == -1) + rss_kb = val; + if (in_shared_vma && vma_rss_kb == -1) + vma_rss_kb = val; + } + else if (sscanf(line, "Swap: %ld kB", &val) == 1) + { + if (in_target_vma && swap_kb == -1) + swap_kb = val; + if (in_shared_vma && vma_swap_kb == -1) + vma_swap_kb = val; + } } } + /* Log the last shared VMA if any */ + if (in_shared_vma) + elog(LOG, "shared VMA %s %s: Size=%ld kB, Rss=%ld kB, Swap=%ld kB", + vma_range, vma_name, + (long) (vma_size_kb >= 0 ? vma_size_kb : 0), + (long) (vma_rss_kb >= 0 ? vma_rss_kb : 0), + (long) (vma_swap_kb >= 0 ? vma_swap_kb : 0)); + fclose(f); result = rss_kb >= 0 ? mul_size(rss_kb, 1024) : 0; -- 2.34.1