| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // A HistogramTester for verifying correct UMA stat generation. | 21 // A HistogramTester for verifying correct UMA stat generation. |
| 22 base::HistogramTester tester; | 22 base::HistogramTester tester; |
| 23 | 23 |
| 24 TestMemoryPressureMonitor() { } | 24 TestMemoryPressureMonitor() { } |
| 25 | 25 |
| 26 // Clears the next run loop update time so that the next pass of the run | 26 // Clears the next run loop update time so that the next pass of the run |
| 27 // loop checks the memory pressure level immediately. Normally there's a | 27 // loop checks the memory pressure level immediately. Normally there's a |
| 28 // 5 second delay between pressure readings. | 28 // 5 second delay between pressure readings. |
| 29 void ResetRunLoopUpdateTime() { next_run_loop_update_time_ = 0; } | 29 void ResetRunLoopUpdateTime() { next_run_loop_update_time_ = 0; } |
| 30 | 30 |
| 31 // Access to the last-recorded memory pressure level. | |
| 32 MemoryPressureListener::MemoryPressureLevel LastPressureLevel() { | |
| 33 return last_pressure_level_; | |
| 34 } | |
| 35 | |
| 36 // Sets the last UMA stat report time. Time spent in memory pressure is | 31 // Sets the last UMA stat report time. Time spent in memory pressure is |
| 37 // recorded in 5-second "ticks" from the last time statistics were recorded. | 32 // recorded in 5-second "ticks" from the last time statistics were recorded. |
| 38 void SetLastStatisticReportTime(CFTimeInterval time) { | 33 void SetLastStatisticReportTime(CFTimeInterval time) { |
| 39 last_statistic_report_time_ = time; | 34 last_statistic_report_time_ = time; |
| 40 } | 35 } |
| 41 | 36 |
| 42 // Sets the raw macOS memory pressure level read by the memory pressure | 37 // Sets the raw macOS memory pressure level read by the memory pressure |
| 43 // monitor. | 38 // monitor. |
| 44 int macos_pressure_level_for_testing_; | 39 int macos_pressure_level_for_testing_; |
| 45 | 40 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 memory_pressure == | 99 memory_pressure == |
| 105 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE || | 100 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE || |
| 106 memory_pressure == | 101 memory_pressure == |
| 107 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | 102 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 108 } | 103 } |
| 109 | 104 |
| 110 TEST(MacMemoryPressureMonitorTest, MemoryPressureConversion) { | 105 TEST(MacMemoryPressureMonitorTest, MemoryPressureConversion) { |
| 111 TestMemoryPressureMonitor monitor; | 106 TestMemoryPressureMonitor monitor; |
| 112 | 107 |
| 113 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_NORMAL; | 108 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_NORMAL; |
| 109 monitor.UpdatePressureLevel(); |
| 114 MemoryPressureListener::MemoryPressureLevel memory_pressure = | 110 MemoryPressureListener::MemoryPressureLevel memory_pressure = |
| 115 monitor.GetCurrentPressureLevel(); | 111 monitor.GetCurrentPressureLevel(); |
| 116 EXPECT_EQ(memory_pressure, | 112 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
| 117 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | 113 memory_pressure); |
| 118 | 114 |
| 119 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_WARN; | 115 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_WARN; |
| 116 monitor.UpdatePressureLevel(); |
| 120 memory_pressure = monitor.GetCurrentPressureLevel(); | 117 memory_pressure = monitor.GetCurrentPressureLevel(); |
| 121 EXPECT_EQ(memory_pressure, | 118 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
| 122 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 119 memory_pressure); |
| 123 | 120 |
| 124 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_CRITICAL; | 121 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_CRITICAL; |
| 122 monitor.UpdatePressureLevel(); |
| 125 memory_pressure = monitor.GetCurrentPressureLevel(); | 123 memory_pressure = monitor.GetCurrentPressureLevel(); |
| 126 EXPECT_EQ(memory_pressure, | 124 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
| 127 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | 125 memory_pressure); |
| 128 } | 126 } |
| 129 | 127 |
| 130 TEST(MacMemoryPressureMonitorTest, MemoryPressureRunLoopChecking) { | 128 TEST(MacMemoryPressureMonitorTest, MemoryPressureRunLoopChecking) { |
| 131 TestMemoryPressureMonitor monitor; | 129 TestMemoryPressureMonitor monitor; |
| 132 | 130 |
| 133 // To test grabbing the memory presure at the end of the run loop, we have to | 131 // To test grabbing the memory presure at the end of the run loop, we have to |
| 134 // run the run loop, but to do that the run loop needs a run loop source. Add | 132 // run the run loop, but to do that the run loop needs a run loop source. Add |
| 135 // a timer as the source. We know that the exit observer is attached to | 133 // a timer as the source. We know that the exit observer is attached to |
| 136 // the kMessageLoopExclusiveRunLoopMode mode, so use that mode. | 134 // the kMessageLoopExclusiveRunLoopMode mode, so use that mode. |
| 137 ScopedCFTypeRef<CFRunLoopTimerRef> timer_ref(CFRunLoopTimerCreate( | 135 ScopedCFTypeRef<CFRunLoopTimerRef> timer_ref(CFRunLoopTimerCreate( |
| 138 NULL, CFAbsoluteTimeGetCurrent() + 10, 0, 0, 0, nullptr, nullptr)); | 136 NULL, CFAbsoluteTimeGetCurrent() + 10, 0, 0, 0, nullptr, nullptr)); |
| 139 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer_ref, | 137 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer_ref, |
| 140 kMessageLoopExclusiveRunLoopMode); | 138 kMessageLoopExclusiveRunLoopMode); |
| 141 | 139 |
| 142 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_WARN; | 140 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_WARN; |
| 143 monitor.ResetRunLoopUpdateTime(); | 141 monitor.ResetRunLoopUpdateTime(); |
| 144 CFRunLoopRunInMode(kMessageLoopExclusiveRunLoopMode, 0, true); | 142 CFRunLoopRunInMode(kMessageLoopExclusiveRunLoopMode, 0, true); |
| 145 EXPECT_EQ(monitor.LastPressureLevel(), | 143 EXPECT_EQ(monitor.GetCurrentPressureLevel(), |
| 146 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 144 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 147 | 145 |
| 148 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_CRITICAL; | 146 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_CRITICAL; |
| 149 monitor.ResetRunLoopUpdateTime(); | 147 monitor.ResetRunLoopUpdateTime(); |
| 150 CFRunLoopRunInMode(kMessageLoopExclusiveRunLoopMode, 0, true); | 148 CFRunLoopRunInMode(kMessageLoopExclusiveRunLoopMode, 0, true); |
| 151 EXPECT_EQ(monitor.LastPressureLevel(), | 149 EXPECT_EQ(monitor.GetCurrentPressureLevel(), |
| 152 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | 150 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 153 | 151 |
| 154 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_NORMAL; | 152 monitor.macos_pressure_level_for_testing_ = DISPATCH_MEMORYPRESSURE_NORMAL; |
| 155 monitor.ResetRunLoopUpdateTime(); | 153 monitor.ResetRunLoopUpdateTime(); |
| 156 CFRunLoopRunInMode(kMessageLoopExclusiveRunLoopMode, 0, true); | 154 CFRunLoopRunInMode(kMessageLoopExclusiveRunLoopMode, 0, true); |
| 157 EXPECT_EQ(monitor.LastPressureLevel(), | 155 EXPECT_EQ(monitor.GetCurrentPressureLevel(), |
| 158 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); | 156 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE); |
| 159 | 157 |
| 160 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timer_ref, | 158 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timer_ref, |
| 161 kMessageLoopExclusiveRunLoopMode); | 159 kMessageLoopExclusiveRunLoopMode); |
| 162 } | 160 } |
| 163 | 161 |
| 164 TEST(MacMemoryPressureMonitorTest, RecordMemoryPressureStats) { | 162 TEST(MacMemoryPressureMonitorTest, RecordMemoryPressureStats) { |
| 165 TestMemoryPressureMonitor monitor; | 163 TestMemoryPressureMonitor monitor; |
| 166 const char* kHistogram = "Memory.PressureLevel"; | 164 const char* kHistogram = "Memory.PressureLevel"; |
| 167 CFTimeInterval now = CFAbsoluteTimeGetCurrent(); | 165 CFTimeInterval now = CFAbsoluteTimeGetCurrent(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 monitor.tester.ExpectTotalCount(kHistogram, 28); | 219 monitor.tester.ExpectTotalCount(kHistogram, 28); |
| 222 monitor.tester.ExpectBucketCount(kHistogram, 0, 7); | 220 monitor.tester.ExpectBucketCount(kHistogram, 0, 7); |
| 223 monitor.tester.ExpectBucketCount(kHistogram, 1, 20); | 221 monitor.tester.ExpectBucketCount(kHistogram, 1, 20); |
| 224 monitor.tester.ExpectBucketCount(kHistogram, 2, 1); | 222 monitor.tester.ExpectBucketCount(kHistogram, 2, 1); |
| 225 // When less than 1 tick of time has elapsed but the pressure level changed, | 223 // When less than 1 tick of time has elapsed but the pressure level changed, |
| 226 // the subtick remainder gets zeroed out. | 224 // the subtick remainder gets zeroed out. |
| 227 EXPECT_EQ(0, monitor.SubTickSeconds()); | 225 EXPECT_EQ(0, monitor.SubTickSeconds()); |
| 228 } | 226 } |
| 229 } // namespace mac | 227 } // namespace mac |
| 230 } // namespace base | 228 } // namespace base |
| OLD | NEW |