1 | module runtime |
2 | |
3 | import os |
4 | |
5 | [typedef] |
6 | struct C.SYSTEM_INFO { |
7 | dwNumberOfProcessors u32 |
8 | } |
9 | |
10 | [typedef] |
11 | struct C.MEMORYSTATUS { |
12 | dwTotalPhys usize |
13 | dwAvailPhys usize |
14 | } |
15 | |
16 | fn C.GetSystemInfo(&C.SYSTEM_INFO) |
17 | fn C.GlobalMemoryStatus(&C.MEMORYSTATUS) |
18 | |
19 | // nr_cpus returns the number of virtual CPU cores found on the system. |
20 | pub 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. |
31 | pub 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. |
38 | pub fn free_memory() usize { |
39 | memory_status := C.MEMORYSTATUS{} |
40 | C.GlobalMemoryStatus(&memory_status) |
41 | return memory_status.dwAvailPhys |
42 | } |