Skip to content

runtime: improve free_memory implementation for OpenBSD - #24431

Merged
spytheman merged 1 commit into
vlang:masterfrom
lcheylus:openbsd-free_memory
May 8, 2025
Merged

spytheman merged 1 commit into
vlang:masterfrom
lcheylus:openbsd-free_memory

Conversation

@lcheylus

@lcheylus lcheylus commented May 7, 2025 •

Copy link
Copy Markdown
Contributor

It's more accurate to use sysctl with uvmexp struct (statistics about the UVM memory management system) to get pagesize and free pages, than using calls to C.sysconf.

See OpenBSD uvm/uvmexp.h include file for definition of uvmexp struct.

Tests OK on OpenBSD current/amd64

get_free_memory.v:

// Get free memory (vlib/runtime/free_memory_impl*.c.v)

import os
import runtime

fn main() {
        println('OS: ${os.user_os()}')

        total := runtime.total_memory()
        free := runtime.free_memory()

        println('total memory: ${total}')
        println('free  memory: ${free}')
}
$ ./v run ~/tmp/get_free_memory.v 
OS: openbsd
total memory: 4278042624
free  memory: 1475702784

It's more accurate to use sysctl with uvmexp struct (statistics about
the UVM memory management system) to get pagesize and free pages, than
using calls to C.sysconf.

Signed-off-by: Laurent Cheylus <foxy@free.fr>
@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-22802

@spytheman

Copy link
Copy Markdown
Contributor

Is there a difference in the reported sizes before/after the patch?

@spytheman
spytheman merged commit 656afa9 into vlang:master May 8, 2025
@lcheylus
lcheylus deleted the openbsd-free_memory branch May 8, 2025 10:31
@lcheylus

lcheylus commented May 8, 2025

Copy link
Copy Markdown
Contributor Author

Is there a difference in the reported sizes before/after the patch?

Yes, there is a difference in the reported number of free pages between direct call to sysctl and C.sysconf for _SC_AVPHYS_PAGES => call via libc with some additional memory allocations.

See https://github.com/openbsd/src/blob/987e6c5a8530c1fc5f890c92b40b9e16b89c7264/lib/libc/gen/sysconf.c#L451-L460

@spytheman

Copy link
Copy Markdown
Contributor

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants