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 #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_MAC_H_ | 5 #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_MAC_H_ |
| 6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_MAC_H_ | 6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_MAC_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CFDate.h> | 8 #include <CoreFoundation/CFDate.h> |
| 9 #include <dispatch/dispatch.h> | 9 #include <dispatch/dispatch.h> |
| 10 | 10 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/mac/scoped_cftyperef.h" | |
| 12 #include "base/mac/scoped_dispatch_object.h" | 13 #include "base/mac/scoped_dispatch_object.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/memory_pressure_listener.h" | 15 #include "base/memory/memory_pressure_listener.h" |
| 15 #include "base/memory/memory_pressure_monitor.h" | 16 #include "base/memory/memory_pressure_monitor.h" |
| 17 #include "base/message_loop/message_pump_mac.h" | |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 namespace mac { | 20 namespace mac { |
| 19 | 21 |
| 20 class TestMemoryPressureMonitor; | 22 class TestMemoryPressureMonitor; |
| 21 | 23 |
| 22 // Declares the interface for the Mac MemoryPressureMonitor, which reports | 24 // Declares the interface for the Mac MemoryPressureMonitor, which reports |
| 23 // memory pressure events and status. | 25 // memory pressure events and status. |
| 24 class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { | 26 class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { |
| 25 public: | 27 public: |
| 26 MemoryPressureMonitor(); | 28 MemoryPressureMonitor(); |
| 27 ~MemoryPressureMonitor() override; | 29 ~MemoryPressureMonitor() override; |
| 28 | 30 |
| 29 // Returns the currently-observed memory pressure. | 31 // Returns the currently-observed memory pressure. |
| 30 MemoryPressureLevel GetCurrentPressureLevel() override; | 32 MemoryPressureLevel GetCurrentPressureLevel() override; |
| 31 | 33 |
| 32 void SetDispatchCallback(const DispatchCallback& callback) override; | 34 void SetDispatchCallback(const DispatchCallback& callback) override; |
| 33 | 35 |
| 34 private: | 36 private: |
| 35 friend TestMemoryPressureMonitor; | 37 friend TestMemoryPressureMonitor; |
| 36 | 38 |
| 37 static MemoryPressureLevel | 39 static MemoryPressureLevel MemoryPressureLevelForMacMemoryPressureLevel( |
| 38 MemoryPressureLevelForMacMemoryPressure(int mac_memory_pressure); | 40 int mac_memory_pressure_level); |
| 41 static void OnRunLoopExit(CFRunLoopObserverRef observer, | |
| 42 CFRunLoopActivity activity, | |
| 43 void* info); | |
| 44 // Returns the raw memory pressure level from the macOS. Exposed for | |
| 45 // unit testing. | |
| 46 virtual int GetMacMemoryPressureLevel(); | |
| 47 | |
| 48 // Updates |last_pressure_level_| with the current memory pressure level. | |
| 49 void UpdatePressureLevel(); | |
| 50 | |
| 51 // Updates |last_pressure_level_| at the end of every run loop pass (modulo | |
| 52 // some numebr of seconds). | |
|
lgrey
2017/02/22 21:33:40
Nit: number
shrike
2017/02/22 23:56:25
Done.
| |
| 53 void UpdatePressureLevelOnRunLoopExit(); | |
| 54 | |
| 55 // Run |dispatch_callback| on memory pressure notifications from the OS. | |
| 39 void OnMemoryPressureChanged(dispatch_source_s* event_source, | 56 void OnMemoryPressureChanged(dispatch_source_s* event_source, |
| 40 const DispatchCallback& dispatch_callback); | 57 const DispatchCallback& dispatch_callback); |
| 41 void SendStatisticsIfNecessary(bool pressure_level_changed); | |
| 42 | 58 |
| 59 // Returns the number of seconds per UMA tick (for statistics recording). | |
| 60 // Exposed for testing. | |
| 61 static int GetSecondsPerUMATick(); | |
| 62 | |
| 63 // The dispatch source that generates memory pressure change notifications. | |
| 43 ScopedDispatchObject<dispatch_source_t> memory_level_event_source_; | 64 ScopedDispatchObject<dispatch_source_t> memory_level_event_source_; |
| 44 | 65 |
| 66 // The callback to call upon receiving a memory pressure change notification. | |
| 45 DispatchCallback dispatch_callback_; | 67 DispatchCallback dispatch_callback_; |
| 46 | 68 |
| 47 CFTimeInterval last_statistic_report_; | 69 // Last UMA report time. |
| 70 CFTimeInterval last_statistic_report_time_; | |
| 48 | 71 |
| 72 // Most-recent memory pressure level. | |
| 49 MemoryPressureLevel last_pressure_level_; | 73 MemoryPressureLevel last_pressure_level_; |
| 50 | 74 |
| 51 // The UMA statistic is recorded in 5 second increments. This | 75 // Observer that tracks exits from the main run loop. |
| 52 // accumulates the remaining time to be rolled into the next | 76 ScopedCFTypeRef<CFRunLoopObserverRef> exit_observer_; |
| 53 // call. | 77 |
| 54 CFTimeInterval reporting_error_; | 78 // Next time to update the memory pressure level when exiting the run loop. |
| 79 CFTimeInterval next_run_loop_update_time_; | |
| 80 | |
| 81 // Seconds left over from the last UMA tick calculation (to be added to the | |
| 82 // next calculation). | |
| 83 CFTimeInterval subtick_seconds_; | |
| 55 | 84 |
| 56 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); | 85 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); |
| 57 }; | 86 }; |
| 58 | 87 |
| 59 } // namespace mac | 88 } // namespace mac |
| 60 } // namespace base | 89 } // namespace base |
| 61 | 90 |
| 62 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_MAC_H_ | 91 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_MAC_H_ |
| OLD | NEW |