2019-10-11 14:16:02 +03:00
|
|
|
module runtime
|
2019-11-15 16:14:28 +03:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2020-09-25 22:02:29 +03:00
|
|
|
[typedef]
|
|
|
|
struct C.SYSTEM_INFO {
|
2021-03-24 13:39:09 +03:00
|
|
|
dwNumberOfProcessors u32
|
2020-09-25 22:02:29 +03:00
|
|
|
}
|
2021-03-24 13:39:09 +03:00
|
|
|
|
2023-06-24 14:15:15 +03:00
|
|
|
[typedef]
|
|
|
|
struct C.MEMORYSTATUS {
|
|
|
|
dwTotalPhys usize
|
|
|
|
dwAvailPhys usize
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:02:29 +03:00
|
|
|
fn C.GetSystemInfo(&C.SYSTEM_INFO)
|
2023-06-24 14:15:15 +03:00
|
|
|
fn C.GlobalMemoryStatus(&C.MEMORYSTATUS)
|
2020-09-25 22:02:29 +03:00
|
|
|
|
2020-12-27 21:14:43 +03:00
|
|
|
// nr_cpus returns the number of virtual CPU cores found on the system.
|
2020-07-20 17:36:44 +03:00
|
|
|
pub fn nr_cpus() int {
|
2020-09-25 22:02:29 +03:00
|
|
|
sinfo := C.SYSTEM_INFO{}
|
|
|
|
C.GetSystemInfo(&sinfo)
|
|
|
|
mut nr := int(sinfo.dwNumberOfProcessors)
|
2019-11-15 16:14:28 +03:00
|
|
|
if nr == 0 {
|
|
|
|
nr = os.getenv('NUMBER_OF_PROCESSORS').int()
|
|
|
|
}
|
|
|
|
return nr
|
|
|
|
}
|
2023-06-24 14:15:15 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|