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 "components/memory_coordinator/browser/memory_monitor_linux.h" | 5 #include "content/browser/memory/memory_monitor_linux.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 memory_coordinator { | 10 namespace content { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // A global static instance of the default delegate. Used by default by | |
15 // MemoryMonitorLinux. | |
16 MemoryMonitorDelegate g_memory_monitor_delegate; | |
17 | |
18 // The number of bits to shift to convert KiB to MiB. | 14 // The number of bits to shift to convert KiB to MiB. |
19 const int kShiftKiBtoMiB = 10; | 15 const int kShiftKiBtoMiB = 10; |
20 | 16 |
21 } // namespace | 17 } // namespace |
22 | 18 |
23 MemoryMonitorLinux::MemoryMonitorLinux(MemoryMonitorDelegate* delegate) | 19 MemoryMonitorLinux::MemoryMonitorLinux(MemoryMonitorDelegate* delegate) |
24 : delegate_(delegate) {} | 20 : delegate_(delegate) {} |
25 | 21 |
| 22 MemoryMonitorLinux::~MemoryMonitorLinux() {} |
| 23 |
26 int MemoryMonitorLinux::GetFreeMemoryUntilCriticalMB() { | 24 int MemoryMonitorLinux::GetFreeMemoryUntilCriticalMB() { |
27 base::SystemMemoryInfoKB mem_info = {}; | 25 base::SystemMemoryInfoKB mem_info = {}; |
28 delegate_->GetSystemMemoryInfo(&mem_info); | 26 delegate_->GetSystemMemoryInfo(&mem_info); |
29 | 27 |
30 // According to kernel commit 34e431b0ae398fc54ea69ff85ec700722c9da773, | 28 // According to kernel commit 34e431b0ae398fc54ea69ff85ec700722c9da773, |
31 // "available" is the "amount of memory that is available for a new workload | 29 // "available" is the "amount of memory that is available for a new workload |
32 // without pushing the system into swap"; return that value if it is valid. | 30 // without pushing the system into swap"; return that value if it is valid. |
33 // Old linux kernels (before 3.14) don't support "available" and show zero | 31 // Old linux kernels (before 3.14) don't support "available" and show zero |
34 // instead. | 32 // instead. |
35 if (mem_info.available > 0) | 33 if (mem_info.available > 0) |
36 return mem_info.available >> kShiftKiBtoMiB; | 34 return mem_info.available >> kShiftKiBtoMiB; |
37 | 35 |
38 // If there is no "available" value, guess at it based on free memory and | 36 // If there is no "available" value, guess at it based on free memory and |
39 // what the OS can easily discard. | 37 // what the OS can easily discard. |
40 return (mem_info.free + mem_info.buffers + mem_info.cached) >> kShiftKiBtoMiB; | 38 return (mem_info.free + mem_info.buffers + mem_info.cached) >> kShiftKiBtoMiB; |
41 } | 39 } |
42 | 40 |
43 // static | 41 // static |
44 std::unique_ptr<MemoryMonitorLinux> MemoryMonitorLinux::Create( | 42 std::unique_ptr<MemoryMonitorLinux> MemoryMonitorLinux::Create( |
45 MemoryMonitorDelegate* delegate) { | 43 MemoryMonitorDelegate* delegate) { |
46 return base::MakeUnique<MemoryMonitorLinux>(delegate); | 44 return base::MakeUnique<MemoryMonitorLinux>(delegate); |
47 } | 45 } |
48 | 46 |
49 // Implementation of factory function defined in memory_monitor.h. | 47 // Implementation of factory function defined in memory_monitor.h. |
50 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { | 48 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
51 return MemoryMonitorLinux::Create(&g_memory_monitor_delegate); | 49 return MemoryMonitorLinux::Create(MemoryMonitorDelegate::GetInstance()); |
52 } | 50 } |
53 | 51 |
54 } // namespace memory_coordinator | 52 } // namespace content |
OLD | NEW |