diff options
Diffstat (limited to 'include/llfio/v2.0/detail/impl/windows')
-rw-r--r-- | include/llfio/v2.0/detail/impl/windows/import.hpp | 28 | ||||
-rw-r--r-- | include/llfio/v2.0/detail/impl/windows/utils.ipp | 61 |
2 files changed, 67 insertions, 22 deletions
diff --git a/include/llfio/v2.0/detail/impl/windows/import.hpp b/include/llfio/v2.0/detail/impl/windows/import.hpp index 71001ce6..dd4b0ac2 100644 --- a/include/llfio/v2.0/detail/impl/windows/import.hpp +++ b/include/llfio/v2.0/detail/impl/windows/import.hpp @@ -539,6 +539,26 @@ namespace windows_nt_kernel using DiscardVirtualMemory_t = BOOL(NTAPI *)(_In_ PVOID VirtualAddress, _In_ SIZE_T Size); + typedef struct _PERFORMANCE_INFORMATION + { + DWORD cb; + SIZE_T CommitTotal; + SIZE_T CommitLimit; + SIZE_T CommitPeak; + SIZE_T PhysicalTotal; + SIZE_T PhysicalAvailable; + SIZE_T SystemCache; + SIZE_T KernelTotal; + SIZE_T KernelPaged; + SIZE_T KernelNonpaged; + SIZE_T PageSize; + DWORD HandleCount; + DWORD ProcessCount; + DWORD ThreadCount; + } PERFORMANCE_INFORMATION, *PPERFORMANCE_INFORMATION, PERFORMACE_INFORMATION, *PPERFORMACE_INFORMATION; + + using GetPerformanceInfo_t = BOOL(NTAPI*)(PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb); + using RtlCaptureStackBackTrace_t = USHORT(NTAPI *)(_In_ ULONG FramesToSkip, _In_ ULONG FramesToCapture, _Out_ PVOID *BackTrace, _Out_opt_ PULONG BackTraceHash); @@ -965,6 +985,7 @@ namespace windows_nt_kernel static AdjustTokenPrivileges_t AdjustTokenPrivileges; static PrefetchVirtualMemory_t PrefetchVirtualMemory_; static DiscardVirtualMemory_t DiscardVirtualMemory_; + static GetPerformanceInfo_t GetPerformanceInfo; static SymInitialize_t SymInitialize; static SymGetLineFromAddr64_t SymGetLineFromAddr64; static RtlCaptureStackBackTrace_t RtlCaptureStackBackTrace; @@ -1300,6 +1321,13 @@ namespace windows_nt_kernel { DiscardVirtualMemory_ = reinterpret_cast<DiscardVirtualMemory_t>(GetProcAddress(kernel32, "DiscardVirtualMemory")); } + if(GetPerformanceInfo == nullptr) + { + if((GetPerformanceInfo = reinterpret_cast<GetPerformanceInfo_t>(GetProcAddress(kernel32, "K32GetPerformanceInfo"))) == nullptr) + { + abort(); + } + } #ifdef LLFIO_OP_STACKBACKTRACEDEPTH if(dbghelp) { diff --git a/include/llfio/v2.0/detail/impl/windows/utils.ipp b/include/llfio/v2.0/detail/impl/windows/utils.ipp index e12db462..4557e567 100644 --- a/include/llfio/v2.0/detail/impl/windows/utils.ipp +++ b/include/llfio/v2.0/detail/impl/windows/utils.ipp @@ -207,38 +207,55 @@ namespace utils return success(); } - result<process_memory_usage> current_process_memory_usage(process_memory_usage::want /*unused*/) noexcept + result<process_memory_usage> current_process_memory_usage(process_memory_usage::want wanted) noexcept { // Amazingly Win32 doesn't expose private working set, so to avoid having // to iterate all the pages in the process and calculate, use a hidden // NT kernel call windows_nt_kernel::init(); using namespace windows_nt_kernel; - ULONG written = 0; - _VM_COUNTERS_EX2 vmc; - memset(&vmc, 0, sizeof(vmc)); - NTSTATUS ntstat = NtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &vmc, sizeof(vmc), &written); - if(ntstat < 0) - { - return ntkernel_error(ntstat); - } process_memory_usage ret; - /* Notes: + if(!!(wanted & process_memory_usage::want::this_process)) + { + ULONG written = 0; + _VM_COUNTERS_EX2 vmc; + memset(&vmc, 0, sizeof(vmc)); + NTSTATUS ntstat = NtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &vmc, sizeof(vmc), &written); + if(ntstat < 0) + { + return ntkernel_error(ntstat); + } + /* Notes: - Apparently PrivateUsage is the commit charge on Windows. It always equals PagefileUsage. - It is the total amount of private anonymous pages committed. - SharedCommitUsage is amount of non-binary Shared memory committed. - Therefore total non-binary commit = PrivateUsage + SharedCommitUsage + Apparently PrivateUsage is the commit charge on Windows. It always equals PagefileUsage. + It is the total amount of private anonymous pages committed. + SharedCommitUsage is amount of non-binary Shared memory committed. + Therefore total non-binary commit = PrivateUsage + SharedCommitUsage - WorkingSetSize is the total amount of program binaries, non-binary shared memory, and anonymous pages faulted in. - PrivateWorkingSetSize is the amount of private anonymous pages faulted into the process. - Therefore remainder is all shared working set faulted into the process. - */ - ret.total_address_space_in_use = vmc.VirtualSize; - ret.total_address_space_paged_in = vmc.WorkingSetSize; + WorkingSetSize is the total amount of program binaries, non-binary shared memory, and anonymous pages faulted in. + PrivateWorkingSetSize is the amount of private anonymous pages faulted into the process. + Therefore remainder is all shared working set faulted into the process. + */ + ret.total_address_space_in_use = vmc.VirtualSize; + ret.total_address_space_paged_in = vmc.WorkingSetSize; - ret.private_committed = vmc.PrivateUsage; - ret.private_paged_in = vmc.PrivateWorkingSetSize; + ret.private_committed = vmc.PrivateUsage; + ret.private_paged_in = vmc.PrivateWorkingSetSize; + } + if(!!(wanted & process_memory_usage::want::this_system)) + { + PERFORMANCE_INFORMATION pi; + memset(&pi, 0, sizeof(pi)); + pi.cb = sizeof(pi); + if(!GetPerformanceInfo(&pi, sizeof(pi))) + { + return win32_error(); + } + ret.system_physical_memory_total = (uint64_t) pi.PhysicalTotal * pi.PageSize; + ret.system_physical_memory_available = (uint64_t) pi.PhysicalAvailable * pi.PageSize; + ret.system_commit_charge_maximum = (uint64_t) pi.CommitLimit * pi.PageSize; + ret.system_commit_charge_available = (uint64_t)(pi.CommitLimit - pi.CommitTotal) * pi.PageSize; + } return ret; } |