Skip to content

Use a more clever formula for available_mem #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: julia-uv2-1.44.2
Choose a base branch
from
Open
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions src/unix/darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <mach-o/dyld.h> /* _NSGetExecutablePath */
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <unistd.h> /* sysconf */

static uv_once_t once = UV_ONCE_INIT;
Expand Down Expand Up @@ -130,9 +131,19 @@ uint64_t uv_get_constrained_memory(void) {
return 0; /* Memory constraints are unknown. */
}


uint64_t uv_get_available_memory(void) {
return uv_get_free_memory();
vm_statistics64_data_t info;
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);

if (host_statistics64(mach_host_self(), HOST_VM_INFO64,
(host_info64_t)&info, &count) != KERN_SUCCESS) {
return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is bnoordhuis?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if external is swapped pages, or pages that are on disk by default, but the os is caching on RAM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But your code seems to give very similar results while looking cleaner.

return available;
}


Expand Down