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

Side by Side Diff: base/memory/memory_pressure_monitor_mac.cc

Issue 2097753002: Make memory pressure notifier configurable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 4 years, 5 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/memory/memory_pressure_monitor_mac.h" 5 #include "base/memory/memory_pressure_monitor_mac.h"
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <sys/sysctl.h> 9 #include <sys/sysctl.h>
10 10
11 #include "base/logging.h"
11 #include "base/mac/mac_util.h" 12 #include "base/mac/mac_util.h"
12 13
13 // Redeclare for partial 10.9 availability. 14 // Redeclare for partial 10.9 availability.
14 DISPATCH_EXPORT const struct dispatch_source_type_s 15 DISPATCH_EXPORT const struct dispatch_source_type_s
15 _dispatch_source_type_memorypressure; 16 _dispatch_source_type_memorypressure;
16 17
17 namespace base { 18 namespace base {
18 namespace mac { 19 namespace mac {
19 20
20 MemoryPressureListener::MemoryPressureLevel 21 MemoryPressureListener::MemoryPressureLevel
21 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure( 22 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure(
22 int mac_memory_pressure) { 23 int mac_memory_pressure) {
23 switch (mac_memory_pressure) { 24 switch (mac_memory_pressure) {
24 case DISPATCH_MEMORYPRESSURE_NORMAL: 25 case DISPATCH_MEMORYPRESSURE_NORMAL:
25 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 26 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
26 case DISPATCH_MEMORYPRESSURE_WARN: 27 case DISPATCH_MEMORYPRESSURE_WARN:
27 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; 28 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
28 case DISPATCH_MEMORYPRESSURE_CRITICAL: 29 case DISPATCH_MEMORYPRESSURE_CRITICAL:
29 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 30 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
30 } 31 }
31 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 32 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
32 } 33 }
33 34
34 void MemoryPressureMonitor::NotifyMemoryPressureChanged( 35 void MemoryPressureMonitor::NotifyMemoryPressureChanged(
35 dispatch_source_s* event_source) { 36 dispatch_source_s* event_source, MemoryPressureMonitor* monitor) {
36 int mac_memory_pressure = dispatch_source_get_data(event_source); 37 int mac_memory_pressure = dispatch_source_get_data(event_source);
37 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = 38 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
38 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 39 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
39 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level); 40 monitor->Notify(memory_pressure_level);
40 } 41 }
41 42
42 MemoryPressureMonitor::MemoryPressureMonitor() 43 MemoryPressureMonitor::MemoryPressureMonitor()
43 // The MemoryPressureListener doesn't want to know about transitions to 44 // The MemoryPressureListener doesn't want to know about transitions to
44 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for 45 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for
45 // DISPATCH_MEMORYPRESSURE_NORMAL notifications. 46 // DISPATCH_MEMORYPRESSURE_NORMAL notifications.
46 : memory_level_event_source_(dispatch_source_create( 47 : memory_level_event_source_(dispatch_source_create(
47 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 48 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,
48 0, 49 0,
49 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, 50 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL,
50 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))) { 51 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))) {
51 dispatch_source_set_event_handler(memory_level_event_source_, ^{ 52 dispatch_source_set_event_handler(memory_level_event_source_, ^{
52 NotifyMemoryPressureChanged(memory_level_event_source_.get()); 53 NotifyMemoryPressureChanged(memory_level_event_source_.get(), this);
53 }); 54 });
54 dispatch_resume(memory_level_event_source_); 55 dispatch_resume(memory_level_event_source_);
55 } 56 }
56 57
57 MemoryPressureMonitor::~MemoryPressureMonitor() { 58 MemoryPressureMonitor::~MemoryPressureMonitor() {
58 dispatch_source_cancel(memory_level_event_source_); 59 dispatch_source_cancel(memory_level_event_source_);
59 } 60 }
60 61
61 MemoryPressureListener::MemoryPressureLevel 62 MemoryPressureListener::MemoryPressureLevel
62 MemoryPressureMonitor::GetCurrentPressureLevel() const { 63 MemoryPressureMonitor::GetCurrentPressureLevel() const {
63 int mac_memory_pressure; 64 int mac_memory_pressure;
64 size_t length = sizeof(int); 65 size_t length = sizeof(int);
65 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, 66 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
66 &length, nullptr, 0); 67 &length, nullptr, 0);
67 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 68 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
68 } 69 }
69 70
71 void MemoryPressureMonitor::SetObserver(
72 MemoryPressureMonitorObserver* observer) {
73 DCHECK(!observer_);
74 observer_ = observer;
75 }
76
77 void MemoryPressureMonitor::Notify(MemoryPressureLevel level) {
78 if (observer_) {
79 observer_->OnMemoryPressure(level);
80 } else {
81 base::MemoryPressureListener::NotifyMemoryPressure(level);
82 }
83 }
84
70 } // namespace mac 85 } // namespace mac
71 } // namespace base 86 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698