1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "athena/resource_manager/memory_pressure_notifier.h" 6 7 #include "athena/resource_manager/public/resource_manager_delegate.h" 8 #include "base/time/time.h" 9 10 11 namespace athena { 12 MemoryPressureNotifier(MemoryPressureObserver * listener)13MemoryPressureNotifier::MemoryPressureNotifier( 14 MemoryPressureObserver* listener) 15 : listener_(listener), 16 current_pressure_(MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN) { 17 StartObserving(); 18 } 19 ~MemoryPressureNotifier()20MemoryPressureNotifier::~MemoryPressureNotifier() { 21 StopObserving(); 22 } 23 StartObserving()24void MemoryPressureNotifier::StartObserving() { 25 int time_in_ms = listener_->GetDelegate()->MemoryPressureIntervalInMS(); 26 timer_.Start(FROM_HERE, 27 base::TimeDelta::FromMilliseconds(time_in_ms), 28 base::Bind(&MemoryPressureNotifier::CheckMemoryPressure, 29 base::Unretained(this))); 30 } 31 StopObserving()32void MemoryPressureNotifier::StopObserving() { 33 // If StartObserving failed, StopObserving will still get called. 34 timer_.Stop(); 35 } 36 CheckMemoryPressure()37void MemoryPressureNotifier::CheckMemoryPressure() { 38 MemoryPressureObserver::MemoryPressure pressure = 39 GetMemoryPressureLevelFromFillLevel( 40 listener_->GetDelegate()->GetUsedMemoryInPercent()); 41 if (current_pressure_ != pressure || 42 (pressure != MemoryPressureObserver::MEMORY_PRESSURE_LOW && 43 pressure != MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN)) { 44 // If we are anything worse than |MEMORY_PRESSURE_LOW|, we notify the 45 // listener. 46 current_pressure_ = pressure; 47 listener_->OnMemoryPressure(current_pressure_); 48 } 49 } 50 51 MemoryPressureObserver::MemoryPressure GetMemoryPressureLevelFromFillLevel(int memory_fill_level)52MemoryPressureNotifier::GetMemoryPressureLevelFromFillLevel( 53 int memory_fill_level) { 54 if (memory_fill_level == 0) 55 return MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN; 56 if (memory_fill_level < 50) 57 return MemoryPressureObserver::MEMORY_PRESSURE_LOW; 58 if (memory_fill_level < 75) 59 return MemoryPressureObserver::MEMORY_PRESSURE_MODERATE; 60 if (memory_fill_level < 90) 61 return MemoryPressureObserver::MEMORY_PRESSURE_HIGH; 62 return MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL; 63 } 64 65 } // namespace athena 66