Use a more clever formula for available_mem#34
Use a more clever formula for available_mem#34gbaraldi wants to merge 1 commit intojulia-uv2-1.44.2from
Conversation
|
|
||
| if (host_statistics64(mach_host_self(), HOST_VM_INFO64, | ||
| (host_info64_t)&info, &count) != KERN_SUCCESS) { | ||
| return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */ |
There was a problem hiding this comment.
Ben Noordhuis, the libuv maintainer
| // Based on https://github.com/exelban/stats/blob/6c991de101957065f579a550ac2ae358d733d0c0/Modules/RAM/readers.swift#L47-L57 | ||
| uint64_t used = info.active_count + info.inactive_count + info.compressor_page_count +info.speculative_count + info.wire_count; | ||
| uint64_t not_used = info.purgeable_count + info.external_page_count; | ||
| uint64_t available = uv_get_total_memory() - (used - not_used) * sysconf(_SC_PAGESIZE); |
There was a problem hiding this comment.
I don't think speculative_count should be in used; since the kernel counts it as part of free: https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/osfmk/kern/host.c#L815
I think there's an easier way to figure out "available" memory:
uint64_t available = (info.free_count + info.inactive_count) * sysconf(_SC_PAGESIZE);
You could arguably include purgeable_count into that. This completely ignores the external page count (which I believe represents pages which have been swapped to disk), and I think that.... is probably the right thing to do? If macOS pages things out to make room for us, that's fine, and we can continue to use more physical memory.
There was a problem hiding this comment.
I'm not sure if external is swapped pages, or pages that are on disk by default, but the os is caching on RAM.
There was a problem hiding this comment.
But your code seems to give very similar results while looking cleaner.
|
In case anyone needs to calculate this in Julia, here's a hacked up snippet: |
We might want to use a different combination of values from vm_info64, I chose this because that's what Stats uses but I'm not too inclined to it or against it.
@staticfloat
Fixes JuliaLang/julia#48473