| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "content/browser/memory/memory_monitor_chromeos.h" | 5 #include "content/browser/memory/memory_monitor_chromeos.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/process/process_metrics.h" | 8 #include "base/process/process_metrics.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // The number of bits to shift to convert KiB to MiB. | 14 // The number of bits to shift to convert KiB to MiB. |
| 15 const int kShiftKiBtoMiB = 10; | 15 const int kShiftKiBtoMiB = 10; |
| 16 | 16 |
| 17 } // namespace | 17 } // namespace |
| 18 | 18 |
| 19 MemoryMonitorChromeOS::MemoryMonitorChromeOS(MemoryMonitorDelegate* delegate) | 19 MemoryMonitorChromeOS::MemoryMonitorChromeOS(MemoryMonitorDelegate* delegate) |
| 20 : delegate_(delegate) {} | 20 : delegate_(delegate) {} |
| 21 | 21 |
| 22 MemoryMonitorChromeOS::~MemoryMonitorChromeOS() {} | 22 MemoryMonitorChromeOS::~MemoryMonitorChromeOS() {} |
| 23 | 23 |
| 24 int MemoryMonitorChromeOS::GetFreeMemoryUntilCriticalMB() { | 24 int MemoryMonitorChromeOS::GetFreeMemoryUntilCriticalMB() { |
| 25 base::SystemMemoryInfoKB mem_info = {}; | 25 base::SystemMemoryInfoKB mem_info = {}; |
| 26 delegate_->GetSystemMemoryInfo(&mem_info); | 26 delegate_->GetSystemMemoryInfo(&mem_info); |
| 27 | 27 |
| 28 // The available memory consists of "real" and virtual (z)ram memory. | 28 // Use available free memory provided by the OS if the OS supports it. |
| 29 // Since swappable memory uses a non pre-deterministic compression and | 29 if (mem_info.available > 0) |
| 30 // the compression creates its own "dynamic" in the system, it gets | 30 return mem_info.available >> kShiftKiBtoMiB; |
| 31 // de-emphasized by the |kSwapWeight| factor. | |
| 32 const int kSwapWeight = 4; | |
| 33 | 31 |
| 34 // The kernel internally uses 50MB. | 32 // The kernel internally uses 50MB. |
| 35 const int kMinFileMemory = 50 * 1024; | 33 const int kMinFileMemory = 50 * 1024; |
| 36 | 34 |
| 37 // Most file memory can be easily reclaimed. | 35 // Most file memory can be easily reclaimed. |
| 38 int file_memory = mem_info.active_file + mem_info.inactive_file; | 36 int file_memory = mem_info.active_file + mem_info.inactive_file; |
| 39 // unless it is dirty or it's a minimal portion which is required. | 37 // unless it is dirty or it's a minimal portion which is required. |
| 40 file_memory -= mem_info.dirty + kMinFileMemory; | 38 file_memory -= mem_info.dirty + kMinFileMemory; |
| 41 | 39 |
| 42 // Available memory is the sum of free, swap and easy reclaimable memory. | 40 // Available memory is the sum of free and easy reclaimable memory. |
| 43 return (mem_info.free + mem_info.swap_free / kSwapWeight + file_memory) >> | 41 return (mem_info.free + file_memory) >> kShiftKiBtoMiB; |
| 44 kShiftKiBtoMiB; | |
| 45 } | 42 } |
| 46 | 43 |
| 47 // static | 44 // static |
| 48 std::unique_ptr<MemoryMonitorChromeOS> MemoryMonitorChromeOS::Create( | 45 std::unique_ptr<MemoryMonitorChromeOS> MemoryMonitorChromeOS::Create( |
| 49 MemoryMonitorDelegate* delegate) { | 46 MemoryMonitorDelegate* delegate) { |
| 50 return base::MakeUnique<MemoryMonitorChromeOS>(delegate); | 47 return base::MakeUnique<MemoryMonitorChromeOS>(delegate); |
| 51 } | 48 } |
| 52 | 49 |
| 53 // Implementation of factory function defined in memory_monitor.h. | 50 // Implementation of factory function defined in memory_monitor.h. |
| 54 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { | 51 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
| 55 return MemoryMonitorChromeOS::Create(MemoryMonitorDelegate::GetInstance()); | 52 return MemoryMonitorChromeOS::Create(MemoryMonitorDelegate::GetInstance()); |
| 56 } | 53 } |
| 57 | 54 |
| 58 } // namespace content | 55 } // namespace content |
| OLD | NEW |