Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Unified Diff: base/process/process_metrics_win.cc

Issue 2558043007: Fix free memory calculation. (Closed)
Patch Set: Use real MemAvailable value, replace formulas by exact numbers in test. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/process/process_metrics_win.cc
diff --git a/base/process/process_metrics_win.cc b/base/process/process_metrics_win.cc
index d2f0c935531071a092289637b9696a744c22ea9a..7d85cb5a2aaa7feebcdc8308a3512cb41a2b2253 100644
--- a/base/process/process_metrics_win.cc
+++ b/base/process/process_metrics_win.cc
@@ -23,6 +23,21 @@ namespace {
// System pagesize. This value remains constant on x86/64 architectures.
const int PAGESIZE_KB = 4;
+typedef enum _SYSTEM_INFORMATION_CLASS {
+ SystemMemoryListInformation = 80
+} SYSTEM_INFORMATION_CLASS;
+
+struct SYSTEM_MEMORY_LIST_INFORMATION {
+ SIZE_T ZeroPageCount;
+ SIZE_T FreePageCount;
+ SIZE_T ModifiedPageCount;
+ SIZE_T ModifiedNoWritePageCount;
+ SIZE_T BadPageCount;
+ SIZE_T PageCountByPriority[8];
+ SIZE_T RepurposedPagesByPriority[8];
+ SIZE_T ModifiedPageCountPageFile;
+};
+
typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
@@ -32,7 +47,7 @@ typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)(
} // namespace
SystemMemoryInfoKB::SystemMemoryInfoKB()
- : total(0), free(0), swap_total(0), swap_free(0) {}
+ : total(0), free(0), available(0), swap_total(0), swap_free(0) {}
SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) =
default;
@@ -349,17 +364,38 @@ size_t GetPageSize() {
// This function uses the following mapping between MEMORYSTATUSEX and
// SystemMemoryInfoKB:
// ullTotalPhys ==> total
-// ullAvailPhys ==> free
+// ullAvailPhys ==> available
brucedawson 2017/03/09 19:17:52 This comment no longer mentions what maps to free
Michael K. (Yandex Team) 2017/03/10 15:53:57 Nothing maps to "free" on Win now.
// ullTotalPageFile ==> swap_total
// ullAvailPageFile ==> swap_free
bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
+ // Get the function pointer.
+ static const NTQUERYSYSTEMINFORMATION query_sys_info =
+ reinterpret_cast<NTQUERYSYSTEMINFORMATION>(::GetProcAddress(
+ GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation"));
+
+ if (query_sys_info != nullptr) {
+ SYSTEM_MEMORY_LIST_INFORMATION mem_list_info = {};
+ ULONG ret_length = 0;
+ NTSTATUS status =
+ query_sys_info(SystemMemoryListInformation, &mem_list_info,
+ sizeof(mem_list_info), &ret_length);
+ if (NT_SUCCESS(status)) {
+ meminfo->free =
+ (mem_list_info.FreePageCount + mem_list_info.ZeroPageCount) *
+ PAGESIZE_KB;
+ } else {
+ NOTREACHED() << "status = " << (ULONG)status
+ << ", ret_length = " << ret_length;
+ }
+ }
+
MEMORYSTATUSEX mem_status;
mem_status.dwLength = sizeof(mem_status);
if (!::GlobalMemoryStatusEx(&mem_status))
return false;
meminfo->total = mem_status.ullTotalPhys / 1024;
- meminfo->free = mem_status.ullAvailPhys / 1024;
+ meminfo->available = mem_status.ullAvailPhys / 1024;
meminfo->swap_total = mem_status.ullTotalPageFile / 1024;
meminfo->swap_free = mem_status.ullAvailPageFile / 1024;

Powered by Google App Engine
This is Rietveld 408576698