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

Side by Side Diff: base/mac/memory_pressure_monitor.cc

Issue 1124163003: Rename OS-specific MemoryPressureMonitor implementations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile error on ChromeOS. Created 5 years, 7 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/mac/memory_pressure_monitor_mac.h" 5 #include "base/mac/memory_pressure_monitor.h"
tfarina 2015/05/20 00:38:54 why did you do that? I'm seeing this now: /Appli
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 #include <sys/sysctl.h> 8 #include <sys/sysctl.h>
9 9
10 #include "base/mac/mac_util.h" 10 #include "base/mac/mac_util.h"
11 11
12 namespace base { 12 namespace base {
13 namespace mac {
13 14
14 MemoryPressureListener::MemoryPressureLevel 15 MemoryPressureListener::MemoryPressureLevel
15 MemoryPressureMonitorMac::MemoryPressureLevelForMacMemoryPressure( 16 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure(
16 int mac_memory_pressure) { 17 int mac_memory_pressure) {
17 switch (mac_memory_pressure) { 18 switch (mac_memory_pressure) {
18 case DISPATCH_MEMORYPRESSURE_NORMAL: 19 case DISPATCH_MEMORYPRESSURE_NORMAL:
19 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 20 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
20 case DISPATCH_MEMORYPRESSURE_WARN: 21 case DISPATCH_MEMORYPRESSURE_WARN:
21 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; 22 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
22 case DISPATCH_MEMORYPRESSURE_CRITICAL: 23 case DISPATCH_MEMORYPRESSURE_CRITICAL:
23 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 24 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
24 } 25 }
25 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 26 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
26 } 27 }
27 28
28 void MemoryPressureMonitorMac::NotifyMemoryPressureChanged( 29 void MemoryPressureMonitor::NotifyMemoryPressureChanged(
29 dispatch_source_s* event_source) { 30 dispatch_source_s* event_source) {
30 int mac_memory_pressure = dispatch_source_get_data(event_source); 31 int mac_memory_pressure = dispatch_source_get_data(event_source);
31 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = 32 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
32 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 33 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
33 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level); 34 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level);
34 } 35 }
35 36
36 MemoryPressureMonitorMac::MemoryPressureMonitorMac() 37 MemoryPressureMonitor::MemoryPressureMonitor()
37 : memory_level_event_source_(nullptr) { 38 : memory_level_event_source_(nullptr) {
38 // _dispatch_source_type_memorypressure is not available prior to 10.9. 39 // _dispatch_source_type_memorypressure is not available prior to 10.9.
39 dispatch_source_type_t dispatch_source_memorypressure = 40 dispatch_source_type_t dispatch_source_memorypressure =
40 static_cast<dispatch_source_type_t> 41 static_cast<dispatch_source_type_t>
41 (dlsym(RTLD_NEXT, "_dispatch_source_type_memorypressure")); 42 (dlsym(RTLD_NEXT, "_dispatch_source_type_memorypressure"));
42 if (dispatch_source_memorypressure) { 43 if (dispatch_source_memorypressure) {
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_.reset( 47 memory_level_event_source_.reset(
47 dispatch_source_create(dispatch_source_memorypressure, 0, 48 dispatch_source_create(dispatch_source_memorypressure, 0,
48 DISPATCH_MEMORYPRESSURE_WARN | 49 DISPATCH_MEMORYPRESSURE_WARN |
49 DISPATCH_MEMORYPRESSURE_CRITICAL, 50 DISPATCH_MEMORYPRESSURE_CRITICAL,
50 dispatch_get_global_queue( 51 dispatch_get_global_queue(
51 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))); 52 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)));
52 53
53 dispatch_source_set_event_handler(memory_level_event_source_.get(), ^{ 54 dispatch_source_set_event_handler(memory_level_event_source_.get(), ^{
54 NotifyMemoryPressureChanged(memory_level_event_source_.get()); 55 NotifyMemoryPressureChanged(memory_level_event_source_.get());
55 }); 56 });
56 dispatch_retain(memory_level_event_source_.get()); 57 dispatch_retain(memory_level_event_source_.get());
57 dispatch_resume(memory_level_event_source_.get()); 58 dispatch_resume(memory_level_event_source_.get());
58 } 59 }
59 } 60 }
60 61
61 MemoryPressureMonitorMac::~MemoryPressureMonitorMac() { 62 MemoryPressureMonitor::~MemoryPressureMonitor() {
62 } 63 }
63 64
64 MemoryPressureListener::MemoryPressureLevel 65 MemoryPressureListener::MemoryPressureLevel
65 MemoryPressureMonitorMac::GetCurrentPressureLevel() const { 66 MemoryPressureMonitor::GetCurrentPressureLevel() const {
66 if (base::mac::IsOSMountainLionOrEarlier()) { 67 if (base::mac::IsOSMountainLionOrEarlier()) {
67 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 68 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
68 } 69 }
69 int mac_memory_pressure; 70 int mac_memory_pressure;
70 size_t length = sizeof(int); 71 size_t length = sizeof(int);
71 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, 72 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
72 &length, nullptr, 0); 73 &length, nullptr, 0);
73 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 74 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
74 } 75 }
75 76
77 } // namespace mac
76 } // namespace base 78 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698