Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/bind.h" | |
| 12 #include "base/logging.h" | |
| 11 #include "base/mac/mac_util.h" | 13 #include "base/mac/mac_util.h" |
| 12 | 14 |
| 13 // Redeclare for partial 10.9 availability. | 15 // Redeclare for partial 10.9 availability. |
| 14 DISPATCH_EXPORT const struct dispatch_source_type_s | 16 DISPATCH_EXPORT const struct dispatch_source_type_s |
| 15 _dispatch_source_type_memorypressure; | 17 _dispatch_source_type_memorypressure; |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 namespace mac { | 20 namespace mac { |
| 19 | 21 |
| 20 MemoryPressureListener::MemoryPressureLevel | 22 MemoryPressureListener::MemoryPressureLevel |
| 21 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure( | 23 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure( |
| 22 int mac_memory_pressure) { | 24 int mac_memory_pressure) { |
| 23 switch (mac_memory_pressure) { | 25 switch (mac_memory_pressure) { |
| 24 case DISPATCH_MEMORYPRESSURE_NORMAL: | 26 case DISPATCH_MEMORYPRESSURE_NORMAL: |
| 25 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 27 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| 26 case DISPATCH_MEMORYPRESSURE_WARN: | 28 case DISPATCH_MEMORYPRESSURE_WARN: |
| 27 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | 29 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
| 28 case DISPATCH_MEMORYPRESSURE_CRITICAL: | 30 case DISPATCH_MEMORYPRESSURE_CRITICAL: |
| 29 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 31 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 30 } | 32 } |
| 31 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 33 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| 32 } | 34 } |
| 33 | 35 |
| 34 void MemoryPressureMonitor::NotifyMemoryPressureChanged( | 36 void MemoryPressureMonitor::NotifyMemoryPressureChanged( |
| 35 dispatch_source_s* event_source) { | 37 dispatch_source_s* event_source, |
| 38 const MemoryPressureMonitor::DispatchCallback& dispatch_callback) { | |
| 36 int mac_memory_pressure = dispatch_source_get_data(event_source); | 39 int mac_memory_pressure = dispatch_source_get_data(event_source); |
| 37 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = | 40 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = |
| 38 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); | 41 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); |
| 39 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level); | 42 dispatch_callback.Run(memory_pressure_level); |
| 40 } | 43 } |
| 41 | 44 |
| 42 MemoryPressureMonitor::MemoryPressureMonitor() | 45 MemoryPressureMonitor::MemoryPressureMonitor() |
| 43 // The MemoryPressureListener doesn't want to know about transitions to | 46 // The MemoryPressureListener doesn't want to know about transitions to |
| 44 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for | 47 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for |
| 45 // DISPATCH_MEMORYPRESSURE_NORMAL notifications. | 48 // DISPATCH_MEMORYPRESSURE_NORMAL notifications. |
| 46 : memory_level_event_source_(dispatch_source_create( | 49 : memory_level_event_source_(dispatch_source_create( |
| 47 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, | 50 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, |
| 48 0, | 51 0, |
| 49 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, | 52 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, |
| 50 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))) { | 53 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))), |
| 54 dispatch_callback_( | |
| 55 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)) { | |
| 51 dispatch_source_set_event_handler(memory_level_event_source_, ^{ | 56 dispatch_source_set_event_handler(memory_level_event_source_, ^{ |
|
bashi
2016/07/08 01:58:28
I read [1] and it seems that we can use member var
| |
| 52 NotifyMemoryPressureChanged(memory_level_event_source_.get()); | 57 NotifyMemoryPressureChanged(memory_level_event_source_.get(), |
| 58 dispatch_callback_); | |
| 53 }); | 59 }); |
| 54 dispatch_resume(memory_level_event_source_); | 60 dispatch_resume(memory_level_event_source_); |
| 55 } | 61 } |
| 56 | 62 |
| 57 MemoryPressureMonitor::~MemoryPressureMonitor() { | 63 MemoryPressureMonitor::~MemoryPressureMonitor() { |
| 58 dispatch_source_cancel(memory_level_event_source_); | 64 dispatch_source_cancel(memory_level_event_source_); |
| 59 } | 65 } |
| 60 | 66 |
| 61 MemoryPressureListener::MemoryPressureLevel | 67 MemoryPressureListener::MemoryPressureLevel |
| 62 MemoryPressureMonitor::GetCurrentPressureLevel() const { | 68 MemoryPressureMonitor::GetCurrentPressureLevel() const { |
| 63 int mac_memory_pressure; | 69 int mac_memory_pressure; |
| 64 size_t length = sizeof(int); | 70 size_t length = sizeof(int); |
| 65 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, | 71 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, |
| 66 &length, nullptr, 0); | 72 &length, nullptr, 0); |
| 67 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); | 73 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); |
| 68 } | 74 } |
| 69 | 75 |
| 76 void MemoryPressureMonitor::SetDispatchCallback( | |
| 77 const DispatchCallback& callback) { | |
| 78 dispatch_callback_ = callback; | |
| 79 } | |
| 80 | |
| 70 } // namespace mac | 81 } // namespace mac |
| 71 } // namespace base | 82 } // namespace base |
| OLD | NEW |