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

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

Issue 1250093006: Added memory pressure monitor for linux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Uncomment ::Get functionality Created 5 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.h"
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 {
14 13
15 MemoryPressureListener::MemoryPressureLevel 14 MemoryPressureListener::MemoryPressureLevel
16 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure( 15 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure(
17 int mac_memory_pressure) { 16 int mac_memory_pressure) {
18 switch (mac_memory_pressure) { 17 switch (mac_memory_pressure) {
19 case DISPATCH_MEMORYPRESSURE_NORMAL: 18 case DISPATCH_MEMORYPRESSURE_NORMAL:
20 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 19 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
21 case DISPATCH_MEMORYPRESSURE_WARN: 20 case DISPATCH_MEMORYPRESSURE_WARN:
22 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; 21 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
23 case DISPATCH_MEMORYPRESSURE_CRITICAL: 22 case DISPATCH_MEMORYPRESSURE_CRITICAL:
24 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 23 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
25 } 24 }
26 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 25 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
27 } 26 }
28 27
29 void MemoryPressureMonitor::NotifyMemoryPressureChanged( 28 void MemoryPressureMonitor::NotifyMemoryPressureChanged(
30 dispatch_source_s* event_source) { 29 dispatch_source_s* event_source) {
31 int mac_memory_pressure = dispatch_source_get_data(event_source); 30 int mac_memory_pressure = dispatch_source_get_data(event_source);
32 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = 31 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
33 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 32 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
34 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level); 33 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level);
35 } 34 }
36 35
37 MemoryPressureMonitor::MemoryPressureMonitor() 36 MemoryPressureMonitor::MemoryPressureMonitor()
38 : memory_level_event_source_(nullptr) { 37 : memory_level_event_source_(nullptr) {
38 DCHECK(!g_monitor);
39 g_monitor = this;
40
39 // _dispatch_source_type_memorypressure is not available prior to 10.9. 41 // _dispatch_source_type_memorypressure is not available prior to 10.9.
40 dispatch_source_type_t dispatch_source_memorypressure = 42 dispatch_source_type_t dispatch_source_memorypressure =
41 static_cast<dispatch_source_type_t> 43 static_cast<dispatch_source_type_t>
42 (dlsym(RTLD_NEXT, "_dispatch_source_type_memorypressure")); 44 (dlsym(RTLD_NEXT, "_dispatch_source_type_memorypressure"));
43 if (dispatch_source_memorypressure) { 45 if (dispatch_source_memorypressure) {
44 // The MemoryPressureListener doesn't want to know about transitions to 46 // The MemoryPressureListener doesn't want to know about transitions to
45 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for 47 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for
46 // DISPATCH_MEMORYPRESSURE_NORMAL notifications. 48 // DISPATCH_MEMORYPRESSURE_NORMAL notifications.
47 memory_level_event_source_.reset( 49 memory_level_event_source_.reset(
48 dispatch_source_create(dispatch_source_memorypressure, 0, 50 dispatch_source_create(dispatch_source_memorypressure, 0,
49 DISPATCH_MEMORYPRESSURE_WARN | 51 DISPATCH_MEMORYPRESSURE_WARN |
50 DISPATCH_MEMORYPRESSURE_CRITICAL, 52 DISPATCH_MEMORYPRESSURE_CRITICAL,
51 dispatch_get_global_queue( 53 dispatch_get_global_queue(
52 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))); 54 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)));
53 55
54 dispatch_source_set_event_handler(memory_level_event_source_.get(), ^{ 56 dispatch_source_set_event_handler(memory_level_event_source_.get(), ^{
55 NotifyMemoryPressureChanged(memory_level_event_source_.get()); 57 NotifyMemoryPressureChanged(memory_level_event_source_.get());
56 }); 58 });
57 dispatch_retain(memory_level_event_source_.get()); 59 dispatch_retain(memory_level_event_source_.get());
58 dispatch_resume(memory_level_event_source_.get()); 60 dispatch_resume(memory_level_event_source_.get());
59 } 61 }
60 } 62 }
61 63
62 MemoryPressureMonitor::~MemoryPressureMonitor() { 64 MemoryPressureMonitor::~MemoryPressureMonitor() {
65 DCHECK(g_monitor);
66 g_monitor = nullptr;
63 } 67 }
64 68
65 MemoryPressureListener::MemoryPressureLevel 69 MemoryPressureListener::MemoryPressureLevel
66 MemoryPressureMonitor::GetCurrentPressureLevel() const { 70 MemoryPressureMonitor::GetCurrentPressureLevel() const {
67 if (base::mac::IsOSMountainLionOrEarlier()) { 71 if (base::mac::IsOSMountainLionOrEarlier()) {
68 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 72 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
69 } 73 }
70 int mac_memory_pressure; 74 int mac_memory_pressure;
71 size_t length = sizeof(int); 75 size_t length = sizeof(int);
72 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, 76 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
73 &length, nullptr, 0); 77 &length, nullptr, 0);
74 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 78 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
75 } 79 }
76 80
77 } // namespace mac
78 } // namespace base 81 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/memory_pressure_monitor_mac.h ('k') | base/memory/memory_pressure_monitor_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698