| 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 <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
| 8 | 8 |
| 9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 dispatch_source_cancel(memory_level_event_source_); | 97 dispatch_source_cancel(memory_level_event_source_); |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 int MemoryPressureMonitor::GetMacMemoryPressureLevel() { | 101 int MemoryPressureMonitor::GetMacMemoryPressureLevel() { |
| 102 // Get the raw memory pressure level from macOS. | 102 // Get the raw memory pressure level from macOS. |
| 103 int mac_memory_pressure_level; | 103 int mac_memory_pressure_level; |
| 104 size_t length = sizeof(int); | 104 size_t length = sizeof(int); |
| 105 sysctlbyname("kern.memorystatus_vm_pressure_level", | 105 sysctlbyname("kern.memorystatus_vm_pressure_level", |
| 106 &mac_memory_pressure_level, &length, nullptr, 0); | 106 &mac_memory_pressure_level, &length, nullptr, 0); |
| 107 | 107 printf("kern memory pressure = %d\n", mac_memory_pressure_level); |
| 108 |
| 108 return mac_memory_pressure_level; | 109 return mac_memory_pressure_level; |
| 109 } | 110 } |
| 110 | 111 |
| 111 void MemoryPressureMonitor::UpdatePressureLevel() { | 112 void MemoryPressureMonitor::UpdatePressureLevel() { |
| 112 // Get the current macOS pressure level and convert to the corresponding | 113 // Get the current macOS pressure level and convert to the corresponding |
| 113 // Chrome pressure level. | 114 // Chrome pressure level. |
| 114 int mac_memory_pressure_level = GetMacMemoryPressureLevel(); | 115 int mac_memory_pressure_level = GetMacMemoryPressureLevel(); |
| 116 printf("updating pressure level to %d\n", mac_memory_pressure_level); |
| 115 MemoryPressureListener::MemoryPressureLevel new_pressure_level = | 117 MemoryPressureListener::MemoryPressureLevel new_pressure_level = |
| 116 MemoryPressureLevelForMacMemoryPressureLevel(mac_memory_pressure_level); | 118 MemoryPressureLevelForMacMemoryPressureLevel(mac_memory_pressure_level); |
| 117 | 119 |
| 118 // Compute the number of "ticks" spent at |last_pressure_level_| (since the | 120 // Compute the number of "ticks" spent at |last_pressure_level_| (since the |
| 119 // last report sent to UMA). | 121 // last report sent to UMA). |
| 120 CFTimeInterval now = CFAbsoluteTimeGetCurrent(); | 122 CFTimeInterval now = CFAbsoluteTimeGetCurrent(); |
| 121 CFTimeInterval time_since_last_report = now - last_statistic_report_time_; | 123 CFTimeInterval time_since_last_report = now - last_statistic_report_time_; |
| 122 last_statistic_report_time_ = now; | 124 last_statistic_report_time_ = now; |
| 123 | 125 |
| 124 double accumulated_time = time_since_last_report + subtick_seconds_; | 126 double accumulated_time = time_since_last_report + subtick_seconds_; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 155 } | 157 } |
| 156 } | 158 } |
| 157 | 159 |
| 158 // Static. | 160 // Static. |
| 159 int MemoryPressureMonitor::GetSecondsPerUMATick() { | 161 int MemoryPressureMonitor::GetSecondsPerUMATick() { |
| 160 return kUMATickSize; | 162 return kUMATickSize; |
| 161 } | 163 } |
| 162 | 164 |
| 163 MemoryPressureListener::MemoryPressureLevel | 165 MemoryPressureListener::MemoryPressureLevel |
| 164 MemoryPressureMonitor::GetCurrentPressureLevel() { | 166 MemoryPressureMonitor::GetCurrentPressureLevel() { |
| 165 return last_pressure_level_; | 167 MemoryPressureListener::MemoryPressureLevel level = MemoryPressureLevelForMacM
emoryPressureLevel(GetMacMemoryPressureLevel()); |
| 168 printf(" GetCurrentPressureLevel = %d\n", level); |
| 169 return level; |
| 170 |
| 171 |
| 172 //return last_pressure_level_; |
| 166 } | 173 } |
| 167 | 174 |
| 168 void MemoryPressureMonitor::OnMemoryPressureChanged( | 175 void MemoryPressureMonitor::OnMemoryPressureChanged( |
| 169 dispatch_source_s* event_source, | 176 dispatch_source_s* event_source, |
| 170 const MemoryPressureMonitor::DispatchCallback& dispatch_callback) { | 177 const MemoryPressureMonitor::DispatchCallback& dispatch_callback) { |
| 171 // Get the Chrome-equvialent memory pressure level. | 178 // Get the Chrome-equvialent memory pressure level. |
| 172 int mac_memory_pressure_level = dispatch_source_get_data(event_source); | 179 int mac_memory_pressure_level = dispatch_source_get_data(event_source); |
| 180 printf("OnChanged to %d\n", mac_memory_pressure_level); |
| 173 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = | 181 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = |
| 174 MemoryPressureLevelForMacMemoryPressureLevel(mac_memory_pressure_level); | 182 MemoryPressureLevelForMacMemoryPressureLevel(mac_memory_pressure_level); |
| 175 | 183 |
| 176 // Run the callback that's waiting on memory pressure change notifications. | 184 // Run the callback that's waiting on memory pressure change notifications. |
| 177 // Note that we don't bother with updating |last_pressure_level_| or | 185 // Note that we don't bother with updating |last_pressure_level_| or |
| 178 // causing memory pressure stats to be sent to UMA. Memory pressure change | 186 // causing memory pressure stats to be sent to UMA. Memory pressure change |
| 179 // notifications are delayed on the Mac, so the current actual memory pressure | 187 // notifications are delayed on the Mac, so the current actual memory pressure |
| 180 // level may be different than the incoming pressure level from the event. | 188 // level may be different than the incoming pressure level from the event. |
| 181 // | 189 // |
| 182 // In general we don't want to take action (such as freeing memory) on | 190 // In general we don't want to take action (such as freeing memory) on |
| 183 // memory pressure change events, but that's how the current system is | 191 // memory pressure change events, but that's how the current system is |
| 184 // designed. Given that it's incorrect to act on either stale or current | 192 // designed. Given that it's incorrect to act on either stale or current |
| 185 // pressure level info, it's not clear which level is better to send. For | 193 // pressure level info, it's not clear which level is better to send. For |
| 186 // now stick with how it's been implemented to date, which is to send the | 194 // now stick with how it's been implemented to date, which is to send the |
| 187 // stale value. | 195 // stale value. |
| 188 if (memory_pressure_level != | 196 if (memory_pressure_level != |
| 189 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) | 197 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) |
| 190 dispatch_callback.Run(memory_pressure_level); | 198 dispatch_callback.Run(memory_pressure_level); |
| 191 } | 199 } |
| 192 | 200 |
| 193 void MemoryPressureMonitor::SetDispatchCallback( | 201 void MemoryPressureMonitor::SetDispatchCallback( |
| 194 const DispatchCallback& callback) { | 202 const DispatchCallback& callback) { |
| 195 dispatch_callback_ = callback; | 203 dispatch_callback_ = callback; |
| 196 } | 204 } |
| 197 | 205 |
| 198 } // namespace mac | 206 } // namespace mac |
| 199 } // namespace base | 207 } // namespace base |
| OLD | NEW |