| 1 | module runtime |
| 2 | |
| 3 | #define PSAPI_VERSION 2 |
| 4 | #include <psapi.h> |
| 5 | |
| 6 | // Windows 7+ exports K32GetProcessMemoryInfo from kernel32.dll, so avoid |
| 7 | // depending on psapi.lib during bootstrap builds. |
| 8 | @[typedef] |
| 9 | struct C.PROCESS_MEMORY_COUNTERS { |
| 10 | cb u32 |
| 11 | PageFaultCount u32 |
| 12 | PeakWorkingSetSize usize |
| 13 | WorkingSetSize usize |
| 14 | QuotaPeakPagedPoolUsage usize |
| 15 | QuotaPagedPoolUsage usize |
| 16 | QuotaPeakNonPagedPoolUsage usize |
| 17 | QuotaNonPagedPoolUsage usize |
| 18 | PagefileUsage usize |
| 19 | PeakPagefileUsage usize |
| 20 | } |
| 21 | |
| 22 | fn C.K32GetProcessMemoryInfo(voidptr, &C.PROCESS_MEMORY_COUNTERS, u32) bool |
| 23 | |
| 24 | // used_memory retrieves the current physical memory usage of the process. |
| 25 | pub fn used_memory() !u64 { |
| 26 | mut pmc := C.PROCESS_MEMORY_COUNTERS{} |
| 27 | pmc.cb = u32(sizeof(pmc)) |
| 28 | if C.K32GetProcessMemoryInfo(C.GetCurrentProcess(), &pmc, pmc.cb) { |
| 29 | return u64(pmc.WorkingSetSize) |
| 30 | } |
| 31 | return 0 |
| 32 | } |
| 33 | |