v / vlib / runtime
Raw file | 42 loc (35 sloc) | 391 bytes | Latest commit hash 9b78d7d21
1module runtime
2
3import os
4
5[typedef]
6struct C.SYSTEM_INFO {
7 dwNumberOfProcessors u32
8}
9
10[typedef]
11struct C.MEMORYSTATUS {
12 dwTotalPhys usize
13 dwAvailPhys usize
14}
15
16fn C.GetSystemInfo(&C.SYSTEM_INFO)
17fn C.GlobalMemoryStatus(&C.MEMORYSTATUS)
18
19// nr_cpus returns the number of virtual CPU cores found on the system.
20pub fn nr_cpus() int {
21 sinfo := C.SYSTEM_INFO{}
22 C.GetSystemInfo(&sinfo)
23 mut nr := int(sinfo.dwNumberOfProcessors)
24 if nr == 0 {
25 nr = os.getenv('NUMBER_OF_PROCESSORS').int()
26 }
27 return nr
28}
29
30// total_memory returns total physical memory found on the system.
31pub fn total_memory() usize {
32 memory_status := C.MEMORYSTATUS{}
33 C.GlobalMemoryStatus(&memory_status)
34 return memory_status.dwTotalPhys
35}
36
37// free_memory returns free physical memory found on the system.
38pub fn free_memory() usize {
39 memory_status := C.MEMORYSTATUS{}
40 C.GlobalMemoryStatus(&memory_status)
41 return memory_status.dwAvailPhys
42}