| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/mac/memory_pressure_monitor.h" | |
| 6 | |
| 7 #include <dlfcn.h> | |
| 8 #include <sys/sysctl.h> | |
| 9 | |
| 10 #include "base/mac/mac_util.h" | |
| 11 | |
| 12 namespace base { | |
| 13 namespace mac { | |
| 14 | |
| 15 MemoryPressureListener::MemoryPressureLevel | |
| 16 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure( | |
| 17 int mac_memory_pressure) { | |
| 18 switch (mac_memory_pressure) { | |
| 19 case DISPATCH_MEMORYPRESSURE_NORMAL: | |
| 20 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
| 21 case DISPATCH_MEMORYPRESSURE_WARN: | |
| 22 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | |
| 23 case DISPATCH_MEMORYPRESSURE_CRITICAL: | |
| 24 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
| 25 } | |
| 26 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
| 27 } | |
| 28 | |
| 29 void MemoryPressureMonitor::NotifyMemoryPressureChanged( | |
| 30 dispatch_source_s* event_source) { | |
| 31 int mac_memory_pressure = dispatch_source_get_data(event_source); | |
| 32 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = | |
| 33 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); | |
| 34 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level); | |
| 35 } | |
| 36 | |
| 37 MemoryPressureMonitor::MemoryPressureMonitor() | |
| 38 : memory_level_event_source_(nullptr) { | |
| 39 // _dispatch_source_type_memorypressure is not available prior to 10.9. | |
| 40 dispatch_source_type_t dispatch_source_memorypressure = | |
| 41 static_cast<dispatch_source_type_t> | |
| 42 (dlsym(RTLD_NEXT, "_dispatch_source_type_memorypressure")); | |
| 43 if (dispatch_source_memorypressure) { | |
| 44 // The MemoryPressureListener doesn't want to know about transitions to | |
| 45 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for | |
| 46 // DISPATCH_MEMORYPRESSURE_NORMAL notifications. | |
| 47 memory_level_event_source_.reset( | |
| 48 dispatch_source_create(dispatch_source_memorypressure, 0, | |
| 49 DISPATCH_MEMORYPRESSURE_WARN | | |
| 50 DISPATCH_MEMORYPRESSURE_CRITICAL, | |
| 51 dispatch_get_global_queue( | |
| 52 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))); | |
| 53 | |
| 54 dispatch_source_set_event_handler(memory_level_event_source_.get(), ^{ | |
| 55 NotifyMemoryPressureChanged(memory_level_event_source_.get()); | |
| 56 }); | |
| 57 dispatch_retain(memory_level_event_source_.get()); | |
| 58 dispatch_resume(memory_level_event_source_.get()); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 MemoryPressureMonitor::~MemoryPressureMonitor() { | |
| 63 } | |
| 64 | |
| 65 MemoryPressureListener::MemoryPressureLevel | |
| 66 MemoryPressureMonitor::GetCurrentPressureLevel() const { | |
| 67 if (base::mac::IsOSMountainLionOrEarlier()) { | |
| 68 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
| 69 } | |
| 70 int mac_memory_pressure; | |
| 71 size_t length = sizeof(int); | |
| 72 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, | |
| 73 &length, nullptr, 0); | |
| 74 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); | |
| 75 } | |
| 76 | |
| 77 } // namespace mac | |
| 78 } // namespace base | |
| OLD | NEW |