v / vlib / runtime / runtime_nix.c.v
35 lines · 31 sloc · 1.09 KB · 681b2f1a3fe4775b0ad04e9fe3fe95c78f592dfa
Raw
1module runtime
2
3fn C.sysconf(name i32) i64
4
5// nr_cpus returns the number of virtual CPU cores found on the system.
6pub fn nr_cpus() int {
7 mut cpus := int(C.sysconf(C._SC_NPROCESSORS_ONLN))
8 if cpus < 1 {
9 eprintln('Warning: sysconf(_SC_NPROCESSORS_ONLN) returned -1, returning CPU count as 1')
10 cpus = 1
11 }
12 return cpus
13}
14
15// total_memory returns total physical memory found on the system.
16pub fn total_memory() !usize {
17 page_size := usize(C.sysconf(C._SC_PAGESIZE))
18 c_errno_1 := C.errno
19 if page_size == usize(-1) {
20 return error('total_memory: `C.sysconf(C._SC_PAGESIZE)` return error code = ${c_errno_1}')
21 }
22 phys_pages := usize(C.sysconf(C._SC_PHYS_PAGES))
23 c_errno_2 := C.errno
24 if phys_pages == usize(-1) {
25 return error('total_memory: `C.sysconf(C._SC_PHYS_PAGES)` return error code = ${c_errno_2}')
26 }
27 return page_size * phys_pages
28}
29
30// free_memory returns free physical memory found on the system.
31// Note: implementation available only on Darwin, FreeBSD, Linux, OpenBSD and
32// Windows. Otherwise, returns 'free_memory: not implemented'.
33pub fn free_memory() !usize {
34 return free_memory_impl()!
35}
36