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

Side by Side Diff: content/browser/memory/memory_monitor_mac.cc

Issue 2360433002: Memory-monitor for Mac. (Closed)
Patch Set: Created 4 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/memory/memory_monitor_mac.h"
6
7 #include <sys/sysctl.h>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/process/process_metrics.h"
11
12 namespace content {
13
14 namespace {
15
16 // The number of bits to shift to convert KiB to MiB.
17 const int kShiftKiBtoMiB = 10;
18
19 } // namespace
20
21 MemoryMonitorMac::MemoryMonitorMac(MemoryMonitorDelegate* delegate)
22 : delegate_(delegate), critical_free_mb_(0) {}
23
24 MemoryMonitorMac::~MemoryMonitorMac() {}
25
26 int MemoryMonitorMac::GetFreeMemoryUntilCriticalMB() {
27 // Get the amount of free memory in the system. The "free" amount is a poor
28 // indication because it doesn't take into consideration memory that is
29 // in use but can be easily freed, such as disk buffers.
30 base::SystemMemoryInfoKB mem_info = {};
31 delegate_->GetSystemMemoryInfo(&mem_info);
32 int free_mem = mem_info.free >> kShiftKiBtoMiB;
33
34 // Get the current system memory pressure.
35 int mac_memory_pressure;
36 bool success = delegate_->GetSystemMemoryPressure(&mac_memory_pressure);
37 DCHECK(success);
38
39 // Adjust the "critical threshold" according to current memory pressure.
40 switch (mac_memory_pressure) {
41 case DISPATCH_MEMORYPRESSURE_NORMAL:
42 // Under normal operation, allow more and more memory to be used.
43 if (critical_free_mb_ > 0)
44 --critical_free_mb_;
45 break;
46 case DISPATCH_MEMORYPRESSURE_WARN: {
47 // Once free memory starts to get low, gradually reduce the amount
48 // of memory that con be used.
shrike 2016/09/29 22:48:43 "con" should be "can"
49 int half_free = free_mem >> 1;
50 if (critical_free_mb_ < half_free)
51 critical_free_mb_ = half_free;
52 else
53 ++critical_free_mb_;
54 } break;
55 case DISPATCH_MEMORYPRESSURE_CRITICAL:
56 // When memory pressure is critical, assume that system is about to swap.
57 critical_free_mb_ = free_mem;
58 break;
59 }
60
61 // Return the amount of memory until critical.
62 return free_mem - critical_free_mb_;
63 }
64
65 // static
66 std::unique_ptr<MemoryMonitorMac> MemoryMonitorMac::Create(
67 MemoryMonitorDelegate* delegate) {
68 return base::MakeUnique<MemoryMonitorMac>(delegate);
69 }
70
71 // Implementation of factory function defined in memory_monitor.h.
72 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() {
73 return MemoryMonitorMac::Create(MemoryMonitorMacDelegate::GetInstance());
74 }
75
76 MemoryMonitorMacDelegate* MemoryMonitorMacDelegate::GetInstance() {
77 return base::Singleton<
78 MemoryMonitorMacDelegate,
79 base::LeakySingletonTraits<MemoryMonitorMacDelegate>>::get();
80 }
81
82 bool MemoryMonitorMacDelegate::GetSystemMemoryPressure(int* mem_pressure) {
83 size_t length = sizeof(*mem_pressure);
84 sysctlbyname("kern.memorystatus_vm_pressure_level", mem_pressure, &length,
85 nullptr, 0);
86 return true;
87 }
88
89 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_monitor_mac.h ('k') | content/browser/memory/memory_monitor_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698