Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/memory/scoped_ptr.h" | |
| 9 #include "base/time/time.h" | |
| 10 #include "base/timer/timer.h" | |
| 11 | |
| 12 | |
| 13 namespace athena { | |
| 14 | |
| 15 //////////////////////////////////////////////////////////////////////////////// | |
| 16 // MemoryPressureNotifierImpl | |
| 17 // | |
| 18 // Does the actual work of observing. Ideally we would need to have at least one | |
| 19 // event driven margin window which can monitor the memory size. However, this | |
| 20 // functionality does not yet exist and for the time being we poll instead the | |
| 21 // available memory amount. Note that on the bright side this will also work on | |
| 22 // a non ChromeOS kernel. | |
| 23 // Note: Any calls to the resource manager will take some time to show effect. | |
| 24 class MemoryPressureNotifierImpl | |
|
Jun Mukai
2014/08/27 01:21:18
You don't have to introduce pimpl pattern to this
Mr4D (OOO till 08-26)
2014/08/27 16:12:12
Done. (Did that because of the thread safe releas
| |
| 25 : public base::RefCountedThreadSafe<MemoryPressureNotifierImpl> { | |
| 26 public: | |
| 27 explicit MemoryPressureNotifierImpl(MemoryPressureObserver* listener) | |
| 28 : listener_(listener), | |
| 29 current_pressure_(MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN) { | |
| 30 StartObserving(); | |
| 31 } | |
| 32 | |
| 33 private: | |
| 34 friend class base::RefCountedThreadSafe<MemoryPressureNotifierImpl>; | |
| 35 | |
| 36 virtual ~MemoryPressureNotifierImpl() { | |
| 37 StopObserving(); | |
| 38 } | |
| 39 | |
| 40 // Starts observing the memory fill level. | |
| 41 // Calls to StartObserving should always be matched with calls to | |
| 42 // StopObserving. | |
| 43 void StartObserving(); | |
| 44 | |
| 45 // Stop observing the memory fill level. | |
| 46 // May be safely called if StartObserving has not been called. | |
| 47 void StopObserving(); | |
| 48 | |
| 49 // The function which gets periodically be called to check any changes in the | |
| 50 // memory pressure. | |
| 51 void CheckMemoryPressure(); | |
| 52 | |
| 53 // Converts free percent of memory into a memory pressure value. | |
| 54 MemoryPressureObserver::MemoryPressure GetMemoryPressureLevelFromFillLevel( | |
| 55 int memory_fill_level); | |
| 56 | |
| 57 base::RepeatingTimer<MemoryPressureNotifierImpl> timer_; | |
| 58 | |
| 59 // The listener which needs to be informed about memory pressure. | |
| 60 MemoryPressureObserver* listener_; | |
| 61 | |
| 62 // Our current memory pressure. | |
| 63 MemoryPressureObserver::MemoryPressure current_pressure_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifierImpl); | |
| 66 }; | |
| 67 | |
| 68 void MemoryPressureNotifierImpl::StartObserving() { | |
| 69 int time_in_ms = listener_->GetDelegate()->MemoryPressureIntervalInMS(); | |
| 70 timer_.Start(FROM_HERE, | |
| 71 base::TimeDelta::FromMilliseconds(time_in_ms), | |
| 72 base::Bind(&MemoryPressureNotifierImpl::CheckMemoryPressure, | |
| 73 base::Unretained(this))); | |
| 74 } | |
| 75 | |
| 76 void MemoryPressureNotifierImpl::StopObserving() { | |
| 77 // If StartObserving failed, StopObserving will still get called. | |
| 78 timer_.Stop(); | |
| 79 } | |
| 80 | |
| 81 void MemoryPressureNotifierImpl::CheckMemoryPressure() { | |
| 82 MemoryPressureObserver::MemoryPressure pressure = | |
| 83 GetMemoryPressureLevelFromFillLevel( | |
| 84 listener_->GetDelegate()->GetUsedMemoryInPercent()); | |
| 85 if (current_pressure_ != pressure || | |
| 86 (pressure != MemoryPressureObserver::MEMORY_PRESSURE_LOW && | |
| 87 pressure != MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN)) { | |
| 88 // If we are anything worse than |MEMORY_PRESSURE_LOW|, we notify the | |
| 89 // listener. | |
| 90 current_pressure_ = pressure; | |
| 91 listener_->OnMemoryPressure(current_pressure_); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 MemoryPressureObserver::MemoryPressure | |
| 96 MemoryPressureNotifierImpl::GetMemoryPressureLevelFromFillLevel( | |
| 97 int memory_fill_level) { | |
| 98 if (memory_fill_level == 0) | |
| 99 return MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN; | |
| 100 if (memory_fill_level < 50) | |
| 101 return MemoryPressureObserver::MEMORY_PRESSURE_LOW; | |
| 102 if (memory_fill_level < 75) | |
| 103 return MemoryPressureObserver::MEMORY_PRESSURE_MODERATE; | |
| 104 if (memory_fill_level < 90) | |
| 105 return MemoryPressureObserver::MEMORY_PRESSURE_HIGH; | |
| 106 return MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL; | |
| 107 } | |
| 108 | |
| 109 //////////////////////////////////////////////////////////////////////////////// | |
| 110 // MemoryPressureNotifier | |
| 111 | |
| 112 MemoryPressureNotifier::MemoryPressureNotifier(MemoryPressureObserver* listener) | |
| 113 : observer_(new MemoryPressureNotifierImpl(listener)) { | |
| 114 } | |
| 115 | |
| 116 MemoryPressureNotifier::~MemoryPressureNotifier() {} | |
| 117 | |
| 118 } // namespace athena | |
| OLD | NEW |