Home | History | Annotate | Download | only in resource_manager
      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 #ifndef ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
      6 #define ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
      7 
      8 #include "athena/athena_export.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/timer/timer.h"
     12 
     13 namespace athena {
     14 
     15 class MemoryPressureNotifierImpl;
     16 class ResourceManagerDelegate;
     17 
     18 ////////////////////////////////////////////////////////////////////////////////
     19 // MemoryPressureObserver
     20 //
     21 // This observer gets informed once about a |MEMORY_PRESSURE_LOW|. When the
     22 // pressure exceeds, the observer will get polled until |MEMORY_PRESSURE_LOW| is
     23 // reached again to counter memory consumption.
     24 class MemoryPressureObserver {
     25  public:
     26   MemoryPressureObserver() {}
     27   virtual ~MemoryPressureObserver() {}
     28 
     29   // The reported memory pressure. Note: The value is intentionally abstracted
     30   // since the real amount of free memory is only estimated (due to e.g. zram).
     31   // Note: The bigger the index of the pressure level, the more resources are
     32   // in use.
     33   enum MemoryPressure {
     34     MEMORY_PRESSURE_UNKNOWN = 0,   // The memory pressure cannot be determined.
     35     MEMORY_PRESSURE_LOW,           // Single call if fill level is below 50%.
     36     MEMORY_PRESSURE_MODERATE,      // Polled for fill level of ~50 .. 75%.
     37     MEMORY_PRESSURE_HIGH,          // Polled for fill level of ~75% .. 90%.
     38     MEMORY_PRESSURE_CRITICAL,      // Polled for fill level of above ~90%.
     39   };
     40   // The observer.
     41   virtual void OnMemoryPressure(MemoryPressure pressure) = 0;
     42 
     43   // OS system interface functions. The delegate remains owned by the Observer.
     44   virtual ResourceManagerDelegate* GetDelegate() = 0;
     45 };
     46 
     47 
     48 ////////////////////////////////////////////////////////////////////////////////
     49 // MemoryPressureNotifier
     50 //
     51 // Class to handle the observation of our free memory. It notifies the owner of
     52 // memory fill level changes, so that it can take action to reduce memory by
     53 // reducing active activities.
     54 //
     55 // The observer will use 3 different fill levels: 50% full, 75% full and 90%
     56 // full.
     57 class ATHENA_EXPORT MemoryPressureNotifier {
     58  public:
     59   // The creator gets the |listener| object. Note that the ownership of the
     60   // listener object remains with the creator.
     61   explicit MemoryPressureNotifier(MemoryPressureObserver* listener);
     62   ~MemoryPressureNotifier();
     63 
     64   // Stop observing the memory fill level.
     65   // May be safely called if StartObserving has not been called.
     66   void StopObserving();
     67 
     68  private:
     69   // Starts observing the memory fill level.
     70   // Calls to StartObserving should always be matched with calls to
     71   // StopObserving.
     72   void StartObserving();
     73 
     74   // The function which gets periodically be called to check any changes in the
     75   // memory pressure.
     76   void CheckMemoryPressure();
     77 
     78   // Converts free percent of memory into a memory pressure value.
     79   MemoryPressureObserver::MemoryPressure GetMemoryPressureLevelFromFillLevel(
     80       int memory_fill_level);
     81 
     82   base::RepeatingTimer<MemoryPressureNotifier> timer_;
     83 
     84   // The listener which needs to be informed about memory pressure.
     85   MemoryPressureObserver* listener_;
     86 
     87   // Our current memory pressure.
     88   MemoryPressureObserver::MemoryPressure current_pressure_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifier);
     91 };
     92 
     93 }  // namespace athena
     94 
     95 #endif  // ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
     96