Index: base/memory/memory_pressure_monitor_mac.cc |
diff --git a/base/memory/memory_pressure_monitor_mac.cc b/base/memory/memory_pressure_monitor_mac.cc |
index 5ea381fd3bd4941987d7a7c471bca4b09b383723..1d59d2e0eea4fb61353f88b9bdca77f1c688d2cd 100644 |
--- a/base/memory/memory_pressure_monitor_mac.cc |
+++ b/base/memory/memory_pressure_monitor_mac.cc |
@@ -44,9 +44,9 @@ MemoryPressureMonitor::MemoryPressureMonitor() |
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))), |
dispatch_callback_( |
base::Bind(&MemoryPressureListener::NotifyMemoryPressure)), |
- last_pressure_change_(CFAbsoluteTimeGetCurrent()), |
+ last_statistic_report_(CFAbsoluteTimeGetCurrent()), |
Mark Mentovai
2016/11/14 19:12:15
Is this an OK initial value? It’s not associated w
lgrey
2016/11/14 19:23:51
I think so. Worst case scenario is the application
|
+ last_pressure_level_(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
reporting_error_(0) { |
- last_pressure_level_ = GetCurrentPressureLevel(); |
dispatch_source_set_event_handler(memory_level_event_source_, ^{ |
OnMemoryPressureChanged(memory_level_event_source_.get(), |
dispatch_callback_); |
@@ -59,41 +59,56 @@ MemoryPressureMonitor::~MemoryPressureMonitor() { |
} |
MemoryPressureListener::MemoryPressureLevel |
-MemoryPressureMonitor::GetCurrentPressureLevel() const { |
+MemoryPressureMonitor::GetCurrentPressureLevel() { |
int mac_memory_pressure; |
size_t length = sizeof(int); |
sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, |
&length, nullptr, 0); |
- return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); |
+ MemoryPressureListener::MemoryPressureLevel memory_pressure_level = |
+ MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); |
+ bool pressure_level_changed = false; |
+ if (last_pressure_level_ != memory_pressure_level) { |
+ pressure_level_changed = true; |
+ } |
+ SendStatisticsIfNecessary(pressure_level_changed); |
+ last_pressure_level_ = memory_pressure_level; |
+ return memory_pressure_level; |
} |
+ |
void MemoryPressureMonitor::OnMemoryPressureChanged( |
dispatch_source_s* event_source, |
const MemoryPressureMonitor::DispatchCallback& dispatch_callback) { |
int mac_memory_pressure = dispatch_source_get_data(event_source); |
MemoryPressureListener::MemoryPressureLevel memory_pressure_level = |
MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); |
+ bool pressure_level_changed = false; |
+ if (last_pressure_level_ != memory_pressure_level) { |
+ pressure_level_changed = true; |
+ } |
+ SendStatisticsIfNecessary(pressure_level_changed); |
+ last_pressure_level_ = memory_pressure_level; |
+ if (memory_pressure_level != |
+ MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) |
+ dispatch_callback.Run(memory_pressure_level); |
+} |
+ |
+void MemoryPressureMonitor::SendStatisticsIfNecessary( |
+ bool pressure_level_changed) { |
CFTimeInterval now = CFAbsoluteTimeGetCurrent(); |
- CFTimeInterval since_last_change = now - last_pressure_change_; |
- last_pressure_change_ = now; |
+ CFTimeInterval since_last_report = now - last_statistic_report_; |
+ last_statistic_report_ = now; |
double ticks_to_report; |
reporting_error_ = |
- modf(since_last_change + reporting_error_, &ticks_to_report); |
- |
- // Sierra fails to call the handler when pressure returns to normal, |
- // which would skew our data. For example, if pressure went to 'warn' |
- // at T0, back to 'normal' at T1, then to 'critical' at T10, we would |
- // report 10 ticks of 'warn' instead of 1 tick of 'warn' and 9 ticks |
- // of 'normal'. |
- // This is rdar://29114314 |
- if (mac::IsAtMostOS10_11()) |
+ modf(since_last_report + reporting_error_, &ticks_to_report); |
Mark Mentovai
2016/11/14 19:12:15
Good practice is to not use modf(), but instead us
lgrey
2016/11/14 19:23:51
Done.
|
+ |
+ // Round up on change to ensure we capture it |
+ if (pressure_level_changed && ticks_to_report < 0) |
+ ticks_to_report = 1; |
+ |
+ if (ticks_to_report > 0) |
RecordMemoryPressure(last_pressure_level_, |
static_cast<int>(ticks_to_report)); |
- |
- last_pressure_level_ = memory_pressure_level; |
- if (memory_pressure_level != |
- MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) |
- dispatch_callback.Run(memory_pressure_level); |
} |
void MemoryPressureMonitor::SetDispatchCallback( |