Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
66846ca
feat(memory): add shared budget and RSS admission foundations
iaojnh Nov 16, 2026
f0a162f
test(memory): focus shared budget and RSS foundation coverage
iaojnh Nov 16, 2026
b079ee6
fix(memory): gate retained slots and avoid reclaimer reset deadlocks
iaojnh Nov 16, 2026
7d20eff
fix(memory): preserve permanent exclusions and workspace ownership
iaojnh Nov 16, 2026
e95c363
fix(memory): preserve slab header quota and complete cleanup scans
iaojnh Nov 16, 2026
2203998
fix(memory): exclude permanent backing from resident overlap proofs
iaojnh Nov 16, 2026
308ae4f
fix(memory): retain untouched backing across RSS observations
iaojnh Nov 16, 2026
0c9f642
fix(memory): preserve RSS backing history across disabled admission
iaojnh Nov 16, 2026
ee5be85
fix(memory): retain unobserved backing before initial RSS samples
iaojnh Nov 16, 2026
9013cac
fix(ailego): reconcile RSS residency and materialization ledgers
iaojnh Nov 16, 2026
0204cbf
fix(memory): reconcile RSS credits with current ownership
iaojnh Nov 16, 2026
9464bd6
fix(memory): share worker identity across component images
iaojnh Nov 16, 2026
b370cf2
fix(memory): align RSS proof and queue mapping lifetimes
iaojnh Nov 16, 2026
eacf9cd
fix(memory): reconcile RSS ownership with BP native allowances
iaojnh Nov 16, 2026
9afbab9
fix(memory): wait for complete materialization headroom
iaojnh Nov 16, 2026
7aee126
refactor(memory): unify admission and bounded waits
iaojnh Nov 16, 2026
41479c5
fix(memory): reclaim idle backing during maintenance admission
iaojnh Nov 16, 2026
6339e38
test(memory): simplify RSS admission coverage
iaojnh Nov 16, 2026
cfcd9c2
fix(ci): account for slab headers and repair Windows builds
iaojnh Nov 16, 2026
da6c1d7
fix(ci): stabilize tiny-pool background reclaim tests
iaojnh Nov 17, 2026
6f09fb7
fix(ci): exercise the enabled shared-cache reserve
iaojnh Nov 17, 2026
29358f8
Merge upstream main and retain its Snowball workaround
iaojnh Nov 17, 2026
3108a19
fix(memory): admit complete workspaces and return resize status
iaojnh Nov 17, 2026
307027f
fix(buffer): quiesce eviction before pool teardown
iaojnh Nov 17, 2026
f117e46
fix(memory): gate optional workspaces on pressure and waiters
iaojnh Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,545 changes: 2,372 additions & 173 deletions src/ailego/buffer/block_eviction_queue.cc

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/ailego/buffer/vector_page_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ constexpr size_t kBlockingAioBatchSize = VecBufferPool::kWritebackBatchPages;
}

VecBufferPool::~VecBufferPool() {
// Retire and wait for in-flight eviction callbacks before teardown. A
// callback can retain a page after force_evict_all_loaded() skips its
// evicting state, leaking that page when the table is destroyed.
BlockEvictionQueue::get_instance().set_invalid(&page_table_);
// Finish queued writes before page buffers, latches, and descriptors go
// away. The synchronous pass below catches any prior writeback error.
stop_writeback();
Expand Down
26 changes: 14 additions & 12 deletions src/ailego/io/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,11 @@ void *File::MemoryRemap(void *oldptr, size_t oldsize, void *newptr,
}

void File::MemoryUnmap(void *addr, size_t len) {
ailego_return_if_false(addr);
munmap(addr, len);
TryMemoryUnmap(addr, len);
}

bool File::TryMemoryUnmap(void *addr, size_t len) {
return addr && ::munmap(addr, len) == 0;
}

bool File::MemoryFlush(void *addr, size_t len) {
Expand Down Expand Up @@ -733,16 +736,15 @@ void *File::MemoryRemap(void *, size_t, void *, size_t) {
return nullptr;
}

void File::MemoryUnmap(void *addr, size_t /*len*/) {
ailego_return_if_false(addr);
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery(addr, &mbi, sizeof(mbi))) {
if (mbi.Type == MEM_MAPPED) {
UnmapViewOfFile(mbi.AllocationBase);
} else {
VirtualFree(mbi.AllocationBase, 0, MEM_RELEASE);
}
}
void File::MemoryUnmap(void *addr, size_t len) {
TryMemoryUnmap(addr, len);
}

bool File::TryMemoryUnmap(void *addr, size_t /*len*/) {
MEMORY_BASIC_INFORMATION mbi{};
if (!addr || !::VirtualQuery(addr, &mbi, sizeof(mbi))) return false;
if (mbi.Type == MEM_MAPPED) return ::UnmapViewOfFile(mbi.AllocationBase) != 0;
return ::VirtualFree(mbi.AllocationBase, 0, MEM_RELEASE) != 0;
}

bool File::MemoryFlush(void *addr, size_t /*len*/) {
Expand Down
111 changes: 74 additions & 37 deletions src/ailego/utility/memory_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
// limitations under the License.

#include "memory_helper.h"
#include <algorithm>
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <limits>
#include <zvec/ailego/utility/file_helper.h>
#include <zvec/ailego/utility/string_helper.h>

Expand All @@ -26,6 +30,7 @@
#else
#if defined(__linux__) || defined(__linux)
#include <sys/resource.h>
#include <fcntl.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#include <sys/sysctl.h>
Expand All @@ -44,41 +49,70 @@ bool MemoryHelper::SelfUsage(size_t *vsz, size_t *rss) {
return false;
}

if (fscanf(fp, "%zd %zd", vsz, rss) == EOF) {
fclose(fp);
return false;
}
size_t virtual_pages = 0;
size_t resident_pages = 0;
const int fields = fscanf(fp, "%zu %zu", &virtual_pages, &resident_pages);
fclose(fp);

long pagesz = sysconf(_SC_PAGESIZE);
*vsz *= (size_t)pagesz;
*rss *= (size_t)pagesz;
const long pagesz = sysconf(_SC_PAGESIZE);
if (fields != 2 || pagesz <= 0 ||
std::max(virtual_pages, resident_pages) >
std::numeric_limits<size_t>::max() / static_cast<size_t>(pagesz)) {
return false;
}
*vsz = virtual_pages * static_cast<size_t>(pagesz);
*rss = resident_pages * static_cast<size_t>(pagesz);
return true;
}

size_t MemoryHelper::SelfRSS() {
FILE *fp = fopen("/proc/self/statm", "r");
if (!fp) {
const int fd = ::open("/proc/self/statm", O_RDONLY | O_CLOEXEC);
if (fd < 0) return 0;

// Sampling may run under the RSS accounting mutex. Use stack storage for
// the kernel's page counts so the sampler does not allocate heap memory.
char statm[128]{};
ssize_t len;
do {
len = ::read(fd, statm, sizeof(statm) - 1);
} while (len < 0 && errno == EINTR);
::close(fd);
if (len <= 0) return 0;

const char *resident = std::strchr(statm, ' ');
if (!resident) return 0;
++resident;
char *end = nullptr;
errno = 0;
const auto resident_pages = std::strtoull(resident, &end, 10);
const long page = sysconf(_SC_PAGESIZE);
if (end == resident || *end != ' ' || errno == ERANGE || page <= 0 ||
resident_pages >
std::numeric_limits<size_t>::max() / static_cast<size_t>(page)) {
return 0;
}

size_t rss = 0;
if (fscanf(fp, "%*d %zd %*d", &rss) == EOF) {
fclose(fp);
return 0;
}
fclose(fp);
return (rss * sysconf(_SC_PAGESIZE));
return static_cast<size_t>(resident_pages) * static_cast<size_t>(page);
}

size_t MemoryHelper::SelfPeakRSS() {
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
return (size_t)(rusage.ru_maxrss * 1024);
struct rusage usage {};
if (getrusage(RUSAGE_SELF, &usage) != 0 || usage.ru_maxrss < 0 ||
static_cast<unsigned long>(usage.ru_maxrss) >
std::numeric_limits<size_t>::max() / 1024) {
return 0;
}
return static_cast<size_t>(usage.ru_maxrss) * 1024;
}

size_t MemoryHelper::TotalRamSize() {
return (sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE));
const long pages = sysconf(_SC_PHYS_PAGES);
const long page = sysconf(_SC_PAGESIZE);
if (pages <= 0 || page <= 0) return 0;
if (static_cast<size_t>(pages) >
std::numeric_limits<size_t>::max() / static_cast<size_t>(page)) {
return std::numeric_limits<size_t>::max();
}
return static_cast<size_t>(pages) * static_cast<size_t>(page);
}

size_t MemoryHelper::AvailableRamSize() {
Expand Down Expand Up @@ -217,11 +251,12 @@ size_t MemoryHelper::ContainerAwareTotalRamSize() {

#elif defined(__APPLE__) && defined(__MACH__)
bool MemoryHelper::SelfUsage(size_t *vsz, size_t *rss) {
struct mach_task_basic_info info;
struct mach_task_basic_info info {};
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;

if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info,
&count) != KERN_SUCCESS) {
&count) != KERN_SUCCESS ||
count < MACH_TASK_BASIC_INFO_COUNT) {
return false;
}
*vsz = info.virtual_size;
Expand All @@ -230,22 +265,24 @@ bool MemoryHelper::SelfUsage(size_t *vsz, size_t *rss) {
}

size_t MemoryHelper::SelfRSS() {
struct mach_task_basic_info info;
struct mach_task_basic_info info {};
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;

if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info,
&count) != KERN_SUCCESS) {
&count) != KERN_SUCCESS ||
count < MACH_TASK_BASIC_INFO_COUNT) {
return 0;
}
return info.resident_size;
}

size_t MemoryHelper::SelfPeakRSS() {
struct mach_task_basic_info info;
struct mach_task_basic_info info {};
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;

if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info,
&count) != KERN_SUCCESS) {
&count) != KERN_SUCCESS ||
count < MACH_TASK_BASIC_INFO_COUNT) {
return 0;
}
return info.resident_size_max;
Expand Down Expand Up @@ -303,7 +340,7 @@ static inline int getpagesize() {
}

bool MemoryHelper::SelfUsage(size_t *vsz, size_t *rss) {
PROCESS_MEMORY_COUNTERS info;
PROCESS_MEMORY_COUNTERS info{};
if (!GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info))) {
return false;
}
Expand All @@ -313,37 +350,37 @@ bool MemoryHelper::SelfUsage(size_t *vsz, size_t *rss) {
}

size_t MemoryHelper::SelfRSS() {
PROCESS_MEMORY_COUNTERS info;
PROCESS_MEMORY_COUNTERS info{};
if (!GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info))) {
return 0u;
}
return (size_t)info.WorkingSetSize;
}

size_t MemoryHelper::SelfPeakRSS() {
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
PROCESS_MEMORY_COUNTERS info{};
if (!GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info))) return 0;
return (size_t)info.PeakWorkingSetSize;
}

size_t MemoryHelper::TotalRamSize() {
MEMORYSTATUSEX status;
MEMORYSTATUSEX status{};
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
if (!GlobalMemoryStatusEx(&status)) return 0;
return (size_t)status.ullTotalPhys;
}

size_t MemoryHelper::AvailableRamSize() {
MEMORYSTATUSEX status;
MEMORYSTATUSEX status{};
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
if (!GlobalMemoryStatusEx(&status)) return 0;
return (size_t)status.ullAvailPhys;
}

size_t MemoryHelper::UsedRamSize() {
MEMORYSTATUSEX status;
MEMORYSTATUSEX status{};
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
if (!GlobalMemoryStatusEx(&status)) return 0;
return (size_t)(status.ullTotalPhys - status.ullAvailPhys);
}

Expand Down
41 changes: 41 additions & 0 deletions src/ailego/utility/mobile_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2025-present the zvec project
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <zvec/ailego/buffer/process_rss_budget.h>
#if defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !TARGET_OS_MACCATALYST
#include <mach/mach.h>
#include <os/proc.h>
#endif
#endif

namespace zvec::ailego {

inline ProcessRssBudget::NativeReader NativeApplicationMemoryReader() {
#if defined(__APPLE__) && TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && \
!TARGET_OS_MACCATALYST
return +[](ProcessRssBudget::NativeSample *sample) -> bool {
if (__builtin_available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)) {
task_vm_info_data_t info{};
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
if (::task_info(::mach_task_self(), TASK_VM_INFO,
reinterpret_cast<task_info_t>(&info),
&count) != KERN_SUCCESS ||
count < TASK_VM_INFO_REV1_COUNT)
return false;
sample->footprint = static_cast<size_t>(info.phys_footprint);
sample->available = ::os_proc_available_memory();
return true;
}
return false;
};
#else
// Simulator memory limits describe the host, not a device application.
// It still exercises RSS and the explicit host-pressure notification path.
return nullptr;
#endif
}

} // namespace zvec::ailego
Loading
Loading