1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

runtime: add new functions total_memory/0 and free_memory/0 (#18499)

This commit is contained in:
kbkpbot 2023-06-24 19:15:15 +08:00 committed by GitHub
parent 2abd2e2c2a
commit 5f0ad64155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 0 deletions

View File

@ -3,5 +3,6 @@
`runtime` provides access to functions describing the current platform:
- whether it is 32bit or 64bit
- how many CPUs/cores are available
- total/free physical memory
- whether the platform is little endian or big endian
- etc.

View File

@ -0,0 +1,34 @@
module runtime
#include <mach/mach.h>
[typedef]
struct C.vm_size_t {
}
[typedef]
struct C.vm_statistics64_data_t {
free_count u32
purgeable_count u32
speculative_count u32
external_page_count u32
}
fn C.mach_host_self() C.host_t
fn C.host_page_size(host C.host_t, out_page_size &C.vm_size_t) int
fn C.host_statistics64(host C.host_t, flavor int, host_info_out &int, host_info_outCnt &u32) int
fn free_memory_impl() usize {
$if macos {
mut hs := C.vm_statistics64_data_t{}
mut vmsz := u32(C.HOST_VM_INFO64_COUNT)
mut hps := u32(0)
mut host := C.mach_host_self()
unsafe {
C.host_statistics64(host, C.HOST_VM_INFO64, &int(&hs), &vmsz)
C.host_page_size(host, &C.vm_size_t(&hps))
}
return usize(u64(hs.free_count) * u64(hps))
}
return 1
}

View File

@ -0,0 +1,5 @@
module runtime
fn free_memory_impl() usize {
return 1
}

View File

@ -0,0 +1,10 @@
module runtime
fn free_memory_impl() usize {
$if linux {
page_size := usize(C.sysconf(C._SC_PAGESIZE))
av_phys_pages := usize(C.sysconf(C._SC_AVPHYS_PAGES))
return page_size * av_phys_pages
}
return 1
}

View File

@ -6,3 +6,15 @@ fn C.sysconf(name int) i64
pub fn nr_cpus() int {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
}
// total_memory returns total physical memory found on the system.
pub fn total_memory() usize {
page_size := usize(C.sysconf(C._SC_PAGESIZE))
phys_pages := usize(C.sysconf(C._SC_PHYS_PAGES))
return page_size * phys_pages
}
// free_memory returns free physical memory found on the system.
pub fn free_memory() usize {
return free_memory_impl()
}

View File

@ -1,32 +1,46 @@
import runtime
fn test_physical_memory() {
total := runtime.total_memory()
free := runtime.free_memory()
println('total memory: ${total}')
println('free memory: ${free}')
assert total > 0 && free > 0
}
fn test_nr_cpus() {
nr_cpus := runtime.nr_cpus()
println(' nr cpus: ${nr_cpus}')
assert nr_cpus > 0
}
fn test_nr_jobs() {
nr_jobs := runtime.nr_jobs()
println(' nr jobs: ${nr_jobs}')
assert nr_jobs > 0
}
fn test_is_32bit() {
x := runtime.is_32bit().str()
println(' is_32bit: ${x}')
assert x == 'true' || x == 'false'
}
fn test_is_64bit() {
x := runtime.is_64bit().str()
println(' is_64bit: ${x}')
assert x == 'true' || x == 'false'
}
fn test_is_little_endian() {
x := runtime.is_little_endian().str()
println(' is_le: ${x}')
assert x == 'true' || x == 'false'
}
fn test_is_big_endian() {
x := runtime.is_big_endian().str()
println(' is_be: ${x}')
assert x == 'true' || x == 'false'
}

View File

@ -7,7 +7,14 @@ struct C.SYSTEM_INFO {
dwNumberOfProcessors u32
}
[typedef]
struct C.MEMORYSTATUS {
dwTotalPhys usize
dwAvailPhys usize
}
fn C.GetSystemInfo(&C.SYSTEM_INFO)
fn C.GlobalMemoryStatus(&C.MEMORYSTATUS)
// nr_cpus returns the number of virtual CPU cores found on the system.
pub fn nr_cpus() int {
@ -19,3 +26,17 @@ pub fn nr_cpus() int {
}
return nr
}
// total_memory returns total physical memory found on the system.
pub fn total_memory() usize {
memory_status := C.MEMORYSTATUS{}
C.GlobalMemoryStatus(&memory_status)
return memory_status.dwTotalPhys
}
// free_memory returns free physical memory found on the system.
pub fn free_memory() usize {
memory_status := C.MEMORYSTATUS{}
C.GlobalMemoryStatus(&memory_status)
return memory_status.dwAvailPhys
}