Guard madvise ENOMEM and stabilize pool/tiny front v3

This commit is contained in:
Moe Charm (CI)
2025-12-09 21:50:15 +09:00
parent e274d5f6a9
commit a905e0ffdd
45 changed files with 3154 additions and 242 deletions

View File

@ -11,6 +11,7 @@
#include "hakmem_sys.h" // Phase 6.11.1: Syscall wrappers with timing
#include "hakmem_whale.h" // Phase 6.11.1: Whale fast-path cache
#include "hakmem_env_cache.h" // Priority-2: ENV cache
#include "box/ss_os_acquire_box.h" // madvise guard
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -117,12 +118,17 @@ void hak_batch_flush(void) {
size_t size = snap.sizes[i];
// Step 1: MADV_FREE to release physical pages (fast, low TLB cost)
int ret = madvise(ptr, size, MADV_FREE);
int ret = ss_os_madvise_guarded(ptr, size, MADV_FREE, "batch_free");
if (ret != 0) {
if (HAK_ENV_SS_MADVISE_STRICT() && errno == EINVAL) {
fprintf(stderr, "[Batch] madvise(MADV_FREE) EINVAL (STRICT). Aborting.\n");
abort();
}
// Fallback to MADV_DONTNEED if MADV_FREE not supported
ret = madvise(ptr, size, MADV_DONTNEED);
if (ret != 0) {
fprintf(stderr, "[Batch] Warning: madvise failed for block %p (size %zu)\n", ptr, size);
ret = ss_os_madvise_guarded(ptr, size, MADV_DONTNEED, "batch_dontneed");
if (ret != 0 && HAK_ENV_SS_MADVISE_STRICT() && errno == EINVAL) {
fprintf(stderr, "[Batch] madvise(MADV_DONTNEED) EINVAL (STRICT). Aborting.\n");
abort();
}
}