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