#6428 [Engine] Count speculative pages once in the macOS free-memory estimate
open · @macintog · 0 评论 · 去 GitHub 看
说明
## Problem and result On macOS the engine's free-memory estimate counts speculative pages (file data read ahead and not yet used) twice, so it is too high by the speculative page count. On my 128 GB M4 Max (macOS 27.3) that count was 58,497 to 174,183 pages in four readings, 0.96 to 2.85 GB at 16 KiB per page. The Metal streaming arena is sized from this estimate, rechecked against it before the fill, and filled in 1.5 GB steps, each allowed only if it leaves the estimate at or above a reserve (the larger of 7 GB and 15% of RAM, plus the n-gram table's share). The engine's other memory checks read the same estimate. It matters most on Macs under 64 GB, where the estimate usually sets the arena's size. This PR counts those pages once, in one helper that both code paths share. `host_statistics64(HOST_VM_INFO64)` already includes speculative pages in `free_count`; the SDK's `mach/vm_statistics.h` says so in `struct vm_statistics64`. `arena_avail` and `sample_host_memory` added them again: ``` free + purgeable + min(inactive + speculative, file_backed) ``` The helper computes: ``` free + purgeable + min(inactive, file_backed - min(file_backed, speculative)) ``` ## Related discussion None found. ## Approach `Window::stream_arena` and `sample_host_memory` now call `strata::platform::mac_available_pages` in `engine/include/strata/platform/mac_memory.hpp`, and `arena_avail` is removed. For the same counters the estimate is lower by `min(speculative, file_backed)` pages on every macOS run. - The planned arena is smaller by up to that amount where the estimate is the limit, and unchanged where a cap is. - The stepped fill checks the estimate on every Mac, unless `STRATA_METAL_ARENA_GB` sets the arena outright. It stops earlier, by one or two 1.5 GB steps, only when the fill was already within the double-counted amount of the reserve. - macOS callers of `sample_host_memory()` and `available_host_bytes()` see the lower figure, unless `--memory-limit-mib` is lower still. Linux and Windows builds compile the same code as before: the change in `conversation_memory.cpp` sits inside its `#elif defined(__APPLE__)` branch, and the other changed sources are Metal-only. ## Validation Branch rebased on `3142b66b9`. Apple M4 Max, 128 GB, macOS 27.3, Apple Clang 21, deployment target 15.0: ```sh cmake -S engine/metal -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_DEPLOYMENT_TARGET=15.0 \ -DMETAL_CPP_DIR=<metal-cpp> -DSTRATA_OWN_CPU_QUANT=ON -DSTRATA_BUILD_TESTS=ON cmake --build build --target strata arena_budget_test ``` **Passed:** - The Release Metal engine (`strata`) builds and links. - `arena_budget_test`: all 49 checks, including three count checks (the inactive file cache still counts, speculative pages count once, no credit when no file-backed pages are left) and a 16 GB case built from made-up counters, where the reserve is 7.5 GB: the old formula gives 9.00 GB and allows a 1.5 GB fill step, the new one gives 8.00 GB and refuses it. - `conversation_memory_test`, built from the two sources its CMake target names: 45 checks. - A probe on the same Mac agrees with the header: in three runs `vm_stat`'s "Pages free" matched `free_count` minus `speculative_count` to within 270 pages, against 174,183 speculative pages. **Failed:** `arena_budget_test` built against the old formula fails those three count checks and the 16 GB case. **Not run:** a model load, streaming run or memory-pressure test on any Mac; any Mac under 64 GB; the Linux, Windows and other GPU builds. ## Limits A larger over-estimate remains: when the inactive queue holds many anonymous pages, the formula can still credit active file-backed pages (4.06 GB in one reading on the same Mac). Kernels that report `inactive_external_count` (vm_statistics64 revision 6) give the inactive file-backed count directly, but using it needs a version check and a fallback for older systems, so I've kept it out of this fix. I can follow up separately if you want it.
本站相关内容
相关页面的快捷入口。