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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <psapi.h> 8 #include <psapi.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
11 #include <winternl.h> 11 #include <winternl.h>
12 12
13 #include <algorithm> 13 #include <algorithm>
14 14
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/process/memory.h" 17 #include "base/process/memory.h"
18 #include "base/sys_info.h" 18 #include "base/sys_info.h"
19 19
20 namespace base { 20 namespace base {
21 namespace { 21 namespace {
22 22
23 // System pagesize. This value remains constant on x86/64 architectures. 23 // System pagesize. This value remains constant on x86/64 architectures.
24 const int PAGESIZE_KB = 4; 24 const int PAGESIZE_KB = 4;
25 25
26 typedef enum _SYSTEM_INFORMATION_CLASS {
27 SystemMemoryListInformation = 80
28 } SYSTEM_INFORMATION_CLASS;
29
30 struct SYSTEM_MEMORY_LIST_INFORMATION {
31 SIZE_T ZeroPageCount;
32 SIZE_T FreePageCount;
33 SIZE_T ModifiedPageCount;
34 SIZE_T ModifiedNoWritePageCount;
35 SIZE_T BadPageCount;
36 SIZE_T PageCountByPriority[8];
37 SIZE_T RepurposedPagesByPriority[8];
38 SIZE_T ModifiedPageCountPageFile;
39 };
40
26 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)( 41 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)(
27 SYSTEM_INFORMATION_CLASS SystemInformationClass, 42 SYSTEM_INFORMATION_CLASS SystemInformationClass,
28 PVOID SystemInformation, 43 PVOID SystemInformation,
29 ULONG SystemInformationLength, 44 ULONG SystemInformationLength,
30 PULONG ReturnLength); 45 PULONG ReturnLength);
31 46
32 } // namespace 47 } // namespace
33 48
34 SystemMemoryInfoKB::SystemMemoryInfoKB() 49 SystemMemoryInfoKB::SystemMemoryInfoKB()
35 : total(0), free(0), swap_total(0), swap_free(0) {} 50 : total(0), free(0), available(0), swap_total(0), swap_free(0) {}
36 51
37 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = 52 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) =
38 default; 53 default;
39 54
40 ProcessMetrics::~ProcessMetrics() { } 55 ProcessMetrics::~ProcessMetrics() { }
41 56
42 // static 57 // static
43 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( 58 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
44 ProcessHandle process) { 59 ProcessHandle process) {
45 return WrapUnique(new ProcessMetrics(process)); 60 return WrapUnique(new ProcessMetrics(process));
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 return (info.CommitTotal * system_info.dwPageSize) / 1024; 357 return (info.CommitTotal * system_info.dwPageSize) / 1024;
343 } 358 }
344 359
345 size_t GetPageSize() { 360 size_t GetPageSize() {
346 return PAGESIZE_KB * 1024; 361 return PAGESIZE_KB * 1024;
347 } 362 }
348 363
349 // This function uses the following mapping between MEMORYSTATUSEX and 364 // This function uses the following mapping between MEMORYSTATUSEX and
350 // SystemMemoryInfoKB: 365 // SystemMemoryInfoKB:
351 // ullTotalPhys ==> total 366 // ullTotalPhys ==> total
352 // ullAvailPhys ==> free 367 // 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.
353 // ullTotalPageFile ==> swap_total 368 // ullTotalPageFile ==> swap_total
354 // ullAvailPageFile ==> swap_free 369 // ullAvailPageFile ==> swap_free
355 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { 370 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
371 // Get the function pointer.
372 static const NTQUERYSYSTEMINFORMATION query_sys_info =
373 reinterpret_cast<NTQUERYSYSTEMINFORMATION>(::GetProcAddress(
374 GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation"));
375
376 if (query_sys_info != nullptr) {
377 SYSTEM_MEMORY_LIST_INFORMATION mem_list_info = {};
378 ULONG ret_length = 0;
379 NTSTATUS status =
380 query_sys_info(SystemMemoryListInformation, &mem_list_info,
381 sizeof(mem_list_info), &ret_length);
382 if (NT_SUCCESS(status)) {
383 meminfo->free =
384 (mem_list_info.FreePageCount + mem_list_info.ZeroPageCount) *
385 PAGESIZE_KB;
386 } else {
387 NOTREACHED() << "status = " << (ULONG)status
388 << ", ret_length = " << ret_length;
389 }
390 }
391
356 MEMORYSTATUSEX mem_status; 392 MEMORYSTATUSEX mem_status;
357 mem_status.dwLength = sizeof(mem_status); 393 mem_status.dwLength = sizeof(mem_status);
358 if (!::GlobalMemoryStatusEx(&mem_status)) 394 if (!::GlobalMemoryStatusEx(&mem_status))
359 return false; 395 return false;
360 396
361 meminfo->total = mem_status.ullTotalPhys / 1024; 397 meminfo->total = mem_status.ullTotalPhys / 1024;
362 meminfo->free = mem_status.ullAvailPhys / 1024; 398 meminfo->available = mem_status.ullAvailPhys / 1024;
363 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; 399 meminfo->swap_total = mem_status.ullTotalPageFile / 1024;
364 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; 400 meminfo->swap_free = mem_status.ullAvailPageFile / 1024;
365 401
366 return true; 402 return true;
367 } 403 }
368 404
369 } // namespace base 405 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698