Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Unified Diff: athena/resource_manager/memory_pressure_notifier.cc

Issue 513523002: Adding Baseframework of the ResourceManager (all hooks and observers) are being put in place with a… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self nits Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: athena/resource_manager/memory_pressure_notifier.cc
diff --git a/athena/resource_manager/memory_pressure_notifier.cc b/athena/resource_manager/memory_pressure_notifier.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4df752ab2e3f611d6476aa3c50e99684a507c4f7
--- /dev/null
+++ b/athena/resource_manager/memory_pressure_notifier.cc
@@ -0,0 +1,118 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "athena/resource_manager/memory_pressure_notifier.h"
+
+#include "athena/resource_manager/public/resource_manager_delegate.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
+#include "base/timer/timer.h"
+
+
+namespace athena {
+
+////////////////////////////////////////////////////////////////////////////////
+// MemoryPressureNotifierImpl
+//
+// Does the actual work of observing. Ideally we would need to have at least one
+// event driven margin window which can monitor the memory size. However, this
+// functionality does not yet exist and for the time being we poll instead the
+// available memory amount. Note that on the bright side this will also work on
+// a non ChromeOS kernel.
+// Note: Any calls to the resource manager will take some time to show effect.
+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
+ : public base::RefCountedThreadSafe<MemoryPressureNotifierImpl> {
+ public:
+ explicit MemoryPressureNotifierImpl(MemoryPressureObserver* listener)
+ : listener_(listener),
+ current_pressure_(MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN) {
+ StartObserving();
+ }
+
+ private:
+ friend class base::RefCountedThreadSafe<MemoryPressureNotifierImpl>;
+
+ virtual ~MemoryPressureNotifierImpl() {
+ StopObserving();
+ }
+
+ // Starts observing the memory fill level.
+ // Calls to StartObserving should always be matched with calls to
+ // StopObserving.
+ void StartObserving();
+
+ // Stop observing the memory fill level.
+ // May be safely called if StartObserving has not been called.
+ void StopObserving();
+
+ // The function which gets periodically be called to check any changes in the
+ // memory pressure.
+ void CheckMemoryPressure();
+
+ // Converts free percent of memory into a memory pressure value.
+ MemoryPressureObserver::MemoryPressure GetMemoryPressureLevelFromFillLevel(
+ int memory_fill_level);
+
+ base::RepeatingTimer<MemoryPressureNotifierImpl> timer_;
+
+ // The listener which needs to be informed about memory pressure.
+ MemoryPressureObserver* listener_;
+
+ // Our current memory pressure.
+ MemoryPressureObserver::MemoryPressure current_pressure_;
+
+ DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifierImpl);
+};
+
+void MemoryPressureNotifierImpl::StartObserving() {
+ int time_in_ms = listener_->GetDelegate()->MemoryPressureIntervalInMS();
+ timer_.Start(FROM_HERE,
+ base::TimeDelta::FromMilliseconds(time_in_ms),
+ base::Bind(&MemoryPressureNotifierImpl::CheckMemoryPressure,
+ base::Unretained(this)));
+}
+
+void MemoryPressureNotifierImpl::StopObserving() {
+ // If StartObserving failed, StopObserving will still get called.
+ timer_.Stop();
+}
+
+void MemoryPressureNotifierImpl::CheckMemoryPressure() {
+ MemoryPressureObserver::MemoryPressure pressure =
+ GetMemoryPressureLevelFromFillLevel(
+ listener_->GetDelegate()->GetUsedMemoryInPercent());
+ if (current_pressure_ != pressure ||
+ (pressure != MemoryPressureObserver::MEMORY_PRESSURE_LOW &&
+ pressure != MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN)) {
+ // If we are anything worse than |MEMORY_PRESSURE_LOW|, we notify the
+ // listener.
+ current_pressure_ = pressure;
+ listener_->OnMemoryPressure(current_pressure_);
+ }
+}
+
+MemoryPressureObserver::MemoryPressure
+MemoryPressureNotifierImpl::GetMemoryPressureLevelFromFillLevel(
+ int memory_fill_level) {
+ if (memory_fill_level == 0)
+ return MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN;
+ if (memory_fill_level < 50)
+ return MemoryPressureObserver::MEMORY_PRESSURE_LOW;
+ if (memory_fill_level < 75)
+ return MemoryPressureObserver::MEMORY_PRESSURE_MODERATE;
+ if (memory_fill_level < 90)
+ return MemoryPressureObserver::MEMORY_PRESSURE_HIGH;
+ return MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// MemoryPressureNotifier
+
+MemoryPressureNotifier::MemoryPressureNotifier(MemoryPressureObserver* listener)
+ : observer_(new MemoryPressureNotifierImpl(listener)) {
+}
+
+MemoryPressureNotifier::~MemoryPressureNotifier() {}
+
+} // namespace athena

Powered by Google App Engine
This is Rietveld 408576698