|
| 1 | +/* |
| 2 | + * vmem.c — Budget-tracked virtual memory allocator. |
| 3 | + * |
| 4 | + * Allocates via mmap (POSIX) or VirtualAlloc (Windows) to bypass |
| 5 | + * ptmalloc2's per-thread arena fragmentation. All allocations are |
| 6 | + * page-aligned, zeroed by the OS, and tracked against a configurable |
| 7 | + * budget (fraction of physical RAM). |
| 8 | + * |
| 9 | + * Pressure events are logged with hysteresis to avoid log storms |
| 10 | + * near the budget boundary. |
| 11 | + */ |
| 12 | +#include "vmem.h" |
| 13 | +#include "platform.h" |
| 14 | +#include "log.h" |
| 15 | + |
| 16 | +#include <stdatomic.h> |
| 17 | +#include <stdio.h> /* snprintf */ |
| 18 | +#include <string.h> /* memset */ |
| 19 | + |
| 20 | +#ifdef _WIN32 |
| 21 | + #ifndef WIN32_LEAN_AND_MEAN |
| 22 | + #define WIN32_LEAN_AND_MEAN |
| 23 | + #endif |
| 24 | + #include <windows.h> |
| 25 | +#else |
| 26 | + #include <sys/mman.h> |
| 27 | + #include <unistd.h> /* sysconf, _SC_PAGESIZE */ |
| 28 | +#endif |
| 29 | + |
| 30 | +/* ── Static state (initialized once) ──────────────────────────── */ |
| 31 | + |
| 32 | +static atomic_size_t g_allocated = 0; /* current total allocated */ |
| 33 | +static atomic_size_t g_peak = 0; /* high-water mark */ |
| 34 | +static size_t g_budget = 0; /* budget in bytes */ |
| 35 | +static atomic_int g_was_over = 0; /* hysteresis: was over budget? */ |
| 36 | +static atomic_int g_initialized = 0; /* init guard */ |
| 37 | + |
| 38 | +/* ── Page size ─────────────────────────────────────────────────── */ |
| 39 | + |
| 40 | +static size_t page_size(void) { |
| 41 | +#ifdef _WIN32 |
| 42 | + SYSTEM_INFO si; |
| 43 | + GetSystemInfo(&si); |
| 44 | + return (size_t)si.dwPageSize; |
| 45 | +#else |
| 46 | + long ps = sysconf(_SC_PAGESIZE); |
| 47 | + return ps > 0 ? (size_t)ps : 4096; |
| 48 | +#endif |
| 49 | +} |
| 50 | + |
| 51 | +/* Round up to page boundary. */ |
| 52 | +static size_t round_to_page(size_t size) { |
| 53 | + size_t ps = page_size(); |
| 54 | + return (size + ps - 1) & ~(ps - 1); |
| 55 | +} |
| 56 | + |
| 57 | +/* ── Pressure logging ──────────────────────────────────────────── */ |
| 58 | + |
| 59 | +#define MB_DIVISOR ((size_t)(1024 * 1024)) |
| 60 | + |
| 61 | +static void check_pressure(size_t allocated) { |
| 62 | + if (g_budget == 0) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + bool over = allocated > g_budget; |
| 67 | + int was = atomic_load(&g_was_over); |
| 68 | + |
| 69 | + if (over && !was) { |
| 70 | + /* Transition: under → over */ |
| 71 | + atomic_store(&g_was_over, 1); |
| 72 | + char alloc_mb[32]; |
| 73 | + char budget_mb[32]; |
| 74 | + char pct_str[16]; |
| 75 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 76 | + snprintf(alloc_mb, sizeof(alloc_mb), "%zu", allocated / MB_DIVISOR); |
| 77 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 78 | + snprintf(budget_mb, sizeof(budget_mb), "%zu", g_budget / MB_DIVISOR); |
| 79 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 80 | + snprintf(pct_str, sizeof(pct_str), "%zu", |
| 81 | + g_budget > 0 ? (allocated * 100) / g_budget : 0); |
| 82 | + cbm_log_warn("mem.pressure.warn", "allocated_mb", alloc_mb, "budget_mb", budget_mb, "pct", |
| 83 | + pct_str); |
| 84 | + } else if (!over && was) { |
| 85 | + /* Transition: over → under */ |
| 86 | + atomic_store(&g_was_over, 0); |
| 87 | + char alloc_mb[32]; |
| 88 | + char budget_mb[32]; |
| 89 | + char pct_str[16]; |
| 90 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 91 | + snprintf(alloc_mb, sizeof(alloc_mb), "%zu", allocated / MB_DIVISOR); |
| 92 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 93 | + snprintf(budget_mb, sizeof(budget_mb), "%zu", g_budget / MB_DIVISOR); |
| 94 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 95 | + snprintf(pct_str, sizeof(pct_str), "%zu", |
| 96 | + g_budget > 0 ? (allocated * 100) / g_budget : 0); |
| 97 | + cbm_log_info("mem.pressure.ok", "allocated_mb", alloc_mb, "budget_mb", budget_mb, "pct", |
| 98 | + pct_str); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/* ── Update peak ───────────────────────────────────────────────── */ |
| 103 | + |
| 104 | +static void update_peak(size_t allocated) { |
| 105 | + size_t old_peak = atomic_load(&g_peak); |
| 106 | + while (allocated > old_peak) { |
| 107 | + if (atomic_compare_exchange_weak(&g_peak, &old_peak, allocated)) { |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/* ── Public API ────────────────────────────────────────────────── */ |
| 114 | + |
| 115 | +void cbm_vmem_init(double ram_fraction) { |
| 116 | + /* Only first call takes effect */ |
| 117 | + int expected = 0; |
| 118 | + if (!atomic_compare_exchange_strong(&g_initialized, &expected, 1)) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + if (ram_fraction <= 0.0 || ram_fraction > 1.0) { |
| 123 | + ram_fraction = 0.5; |
| 124 | + } |
| 125 | + |
| 126 | + cbm_system_info_t info = cbm_system_info(); |
| 127 | + g_budget = (size_t)((double)info.total_ram * ram_fraction); |
| 128 | + |
| 129 | + char budget_mb[32]; |
| 130 | + char ram_mb[32]; |
| 131 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 132 | + snprintf(budget_mb, sizeof(budget_mb), "%zu", g_budget / MB_DIVISOR); |
| 133 | + // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 134 | + snprintf(ram_mb, sizeof(ram_mb), "%zu", info.total_ram / MB_DIVISOR); |
| 135 | + cbm_log_info("vmem.init", "budget_mb", budget_mb, "total_ram_mb", ram_mb); |
| 136 | +} |
| 137 | + |
| 138 | +void *cbm_vmem_alloc(size_t size) { |
| 139 | + if (size == 0) { |
| 140 | + return NULL; |
| 141 | + } |
| 142 | + |
| 143 | + size_t alloc_size = round_to_page(size); |
| 144 | + |
| 145 | +#ifdef _WIN32 |
| 146 | + void *ptr = VirtualAlloc(NULL, alloc_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 147 | +#else |
| 148 | + void *ptr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 149 | + if (ptr == MAP_FAILED) { |
| 150 | + ptr = NULL; |
| 151 | + } |
| 152 | +#endif |
| 153 | + |
| 154 | + if (!ptr) { |
| 155 | + cbm_log_error("vmem.alloc.fail", "size_mb", |
| 156 | + size > MB_DIVISOR ? "large" : "small"); |
| 157 | + return NULL; |
| 158 | + } |
| 159 | + |
| 160 | + size_t new_total = atomic_fetch_add(&g_allocated, alloc_size) + alloc_size; |
| 161 | + update_peak(new_total); |
| 162 | + check_pressure(new_total); |
| 163 | + |
| 164 | + return ptr; |
| 165 | +} |
| 166 | + |
| 167 | +void cbm_vmem_free(void *ptr, size_t size) { |
| 168 | + if (!ptr || size == 0) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + size_t free_size = round_to_page(size); |
| 173 | + |
| 174 | +#ifdef _WIN32 |
| 175 | + VirtualFree(ptr, 0, MEM_RELEASE); |
| 176 | +#else |
| 177 | + munmap(ptr, free_size); |
| 178 | +#endif |
| 179 | + |
| 180 | + size_t new_total = atomic_fetch_sub(&g_allocated, free_size) - free_size; |
| 181 | + check_pressure(new_total); |
| 182 | +} |
| 183 | + |
| 184 | +size_t cbm_vmem_allocated(void) { |
| 185 | + return atomic_load(&g_allocated); |
| 186 | +} |
| 187 | + |
| 188 | +size_t cbm_vmem_peak(void) { |
| 189 | + return atomic_load(&g_peak); |
| 190 | +} |
| 191 | + |
| 192 | +size_t cbm_vmem_budget(void) { |
| 193 | + return g_budget; |
| 194 | +} |
| 195 | + |
| 196 | +bool cbm_vmem_over_budget(void) { |
| 197 | + return atomic_load(&g_allocated) > g_budget; |
| 198 | +} |
| 199 | + |
| 200 | +size_t cbm_vmem_worker_budget(int num_workers) { |
| 201 | + if (num_workers <= 0) { |
| 202 | + num_workers = 1; |
| 203 | + } |
| 204 | + return g_budget / (size_t)num_workers; |
| 205 | +} |
0 commit comments