Skip to content

Commit

Permalink
Make memory pressure notifier configurable
Browse files Browse the repository at this point in the history
In memory coordinator v0 we try to replace MemoryPressureListener
with MemoryCoordinatorClient as described in [1]. This CL adds
SetNotifier() method which takes a callback for dispatching
memory pressure notifications. This allows us to replace
MemoryPressureListener::NotifyMemoryPressure() with an arbitrary
function.

[1] https://docs.google.com/document/d/1rGeUzhfzIKwmBzUcqUEwxEpLQQRq3PulRuHwFBlVIYI/edit#

BUG=617492

Review-Url: https://codereview.chromium.org/2097753002
Cr-Commit-Position: refs/heads/master@{#404327}
  • Loading branch information
bashi authored and Commit bot committed Jul 8, 2016
1 parent 969a37d commit 456eef4
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 8 deletions.
6 changes: 6 additions & 0 deletions base/memory/memory_pressure_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_

#include "base/base_export.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/memory_pressure_listener.h"

Expand All @@ -23,6 +24,7 @@ namespace base {
class BASE_EXPORT MemoryPressureMonitor {
public:
using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
using DispatchCallback = base::Callback<void(MemoryPressureLevel level)>;

virtual ~MemoryPressureMonitor();

Expand All @@ -32,6 +34,10 @@ class BASE_EXPORT MemoryPressureMonitor {
// Returns the currently observed memory pressure.
virtual MemoryPressureLevel GetCurrentPressureLevel() const = 0;

// Sets a notification callback. The default callback invokes
// base::MemoryPressureListener::NotifyMemoryPressure.
virtual void SetDispatchCallback(const DispatchCallback& callback) = 0;

protected:
MemoryPressureMonitor();

Expand Down
9 changes: 8 additions & 1 deletion base/memory/memory_pressure_monitor_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ MemoryPressureMonitor::MemoryPressureMonitor(
critical_pressure_threshold_percent_(
GetCriticalMemoryThresholdInPercent(thresholds)),
low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))),
dispatch_callback_(
base::Bind(&MemoryPressureListener::NotifyMemoryPressure)),
weak_ptr_factory_(this) {
StartObserving();
LOG_IF(ERROR,
Expand Down Expand Up @@ -230,7 +232,7 @@ void MemoryPressureMonitor::CheckMemoryPressure() {
return;
}
moderate_pressure_repeat_count_ = 0;
MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_);
dispatch_callback_.Run(current_memory_pressure_level_);
}

// Gets the used ChromeOS memory in percent.
Expand Down Expand Up @@ -271,5 +273,10 @@ int MemoryPressureMonitor::GetUsedMemoryInPercent() {
return percentage;
}

void MemoryPressureMonitor::SetDispatchCallback(
const DispatchCallback& callback) {
dispatch_callback_ = callback;
}

} // namespace chromeos
} // namespace base
3 changes: 3 additions & 0 deletions base/memory/memory_pressure_monitor_chromeos.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// Get the current memory pressure level.
MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() const
override;
void SetDispatchCallback(const DispatchCallback& callback) override;

// Returns a type-casted version of the current memory pressure monitor. A
// simple wrapper to base::MemoryPressureMonitor::Get.
Expand Down Expand Up @@ -107,6 +108,8 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// File descriptor used to detect low memory condition.
ScopedFD low_mem_file_;

DispatchCallback dispatch_callback_;

base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
Expand Down
19 changes: 15 additions & 4 deletions base/memory/memory_pressure_monitor_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stddef.h>
#include <sys/sysctl.h>

#include "base/bind.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"

// Redeclare for partial 10.9 availability.
Expand All @@ -32,11 +34,12 @@ MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure(
}

void MemoryPressureMonitor::NotifyMemoryPressureChanged(
dispatch_source_s* event_source) {
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);
MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level);
dispatch_callback.Run(memory_pressure_level);
}

MemoryPressureMonitor::MemoryPressureMonitor()
Expand All @@ -47,9 +50,12 @@ MemoryPressureMonitor::MemoryPressureMonitor()
DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,
0,
DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))) {
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))),
dispatch_callback_(
base::Bind(&MemoryPressureListener::NotifyMemoryPressure)) {
dispatch_source_set_event_handler(memory_level_event_source_, ^{
NotifyMemoryPressureChanged(memory_level_event_source_.get());
NotifyMemoryPressureChanged(memory_level_event_source_.get(),
dispatch_callback_);
});
dispatch_resume(memory_level_event_source_);
}
Expand All @@ -67,5 +73,10 @@ MemoryPressureMonitor::GetCurrentPressureLevel() const {
return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
}

void MemoryPressureMonitor::SetDispatchCallback(
const DispatchCallback& callback) {
dispatch_callback_ = callback;
}

} // namespace mac
} // namespace base
8 changes: 7 additions & 1 deletion base/memory/memory_pressure_monitor_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// Returns the currently-observed memory pressure.
MemoryPressureLevel GetCurrentPressureLevel() const override;

void SetDispatchCallback(const DispatchCallback& callback) override;

private:
friend TestMemoryPressureMonitor;

static MemoryPressureLevel
MemoryPressureLevelForMacMemoryPressure(int mac_memory_pressure);
static void NotifyMemoryPressureChanged(dispatch_source_s* event_source);
static void NotifyMemoryPressureChanged(
dispatch_source_s* event_source,
const DispatchCallback& dispatch_callback);

ScopedDispatchObject<dispatch_source_t> memory_level_event_source_;

DispatchCallback dispatch_callback_;

DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
};

Expand Down
11 changes: 10 additions & 1 deletion base/memory/memory_pressure_monitor_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ MemoryPressureMonitor::MemoryPressureMonitor()
current_memory_pressure_level_(
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
moderate_pressure_repeat_count_(0),
dispatch_callback_(
base::Bind(&MemoryPressureListener::NotifyMemoryPressure)),
weak_ptr_factory_(this) {
InferThresholds();
StartObserving();
Expand All @@ -94,6 +96,8 @@ MemoryPressureMonitor::MemoryPressureMonitor(int moderate_threshold_mb,
current_memory_pressure_level_(
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
moderate_pressure_repeat_count_(0),
dispatch_callback_(
base::Bind(&MemoryPressureListener::NotifyMemoryPressure)),
weak_ptr_factory_(this) {
DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_);
DCHECK_LE(0, critical_threshold_mb_);
Expand Down Expand Up @@ -198,7 +202,7 @@ void MemoryPressureMonitor::CheckMemoryPressure() {
// happen for moderate and critical pressure levels.
DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
current_memory_pressure_level_);
MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_);
dispatch_callback_.Run(current_memory_pressure_level_);
}

void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() {
Expand Down Expand Up @@ -250,5 +254,10 @@ bool MemoryPressureMonitor::GetSystemMemoryStatus(
return true;
}

void MemoryPressureMonitor::SetDispatchCallback(
const DispatchCallback& callback) {
dispatch_callback_ = callback;
}

} // namespace win
} // namespace base
3 changes: 3 additions & 0 deletions base/memory/memory_pressure_monitor_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {

// Get the current memory pressure level. This can be called from any thread.
MemoryPressureLevel GetCurrentPressureLevel() const override;
void SetDispatchCallback(const DispatchCallback& callback) override;

// Returns the moderate pressure level free memory threshold, in MB.
int moderate_threshold_mb() const { return moderate_threshold_mb_; }
Expand Down Expand Up @@ -132,6 +133,8 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// Ensures that this object is used from a single thread.
base::ThreadChecker thread_checker_;

DispatchCallback dispatch_callback_;

// Weak pointer factory to ourself used for scheduling calls to
// CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|.
base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;
Expand Down
9 changes: 8 additions & 1 deletion chromecast/browser/cast_memory_pressure_monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ int GetSystemReservedKb() {
CastMemoryPressureMonitor::CastMemoryPressureMonitor()
: current_level_(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
system_reserved_kb_(GetSystemReservedKb()),
dispatch_callback_(
base::Bind(&base::MemoryPressureListener::NotifyMemoryPressure)),
weak_ptr_factory_(this) {
PollPressureLevel();
}
Expand Down Expand Up @@ -111,7 +113,7 @@ void CastMemoryPressureMonitor::PollPressureLevel() {
void CastMemoryPressureMonitor::UpdateMemoryPressureLevel(
MemoryPressureLevel new_level) {
if (new_level != base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE)
base::MemoryPressureListener::NotifyMemoryPressure(new_level);
dispatch_callback_.Run(new_level);

if (new_level == current_level_)
return;
Expand All @@ -121,4 +123,9 @@ void CastMemoryPressureMonitor::UpdateMemoryPressureLevel(
"Memory.Pressure.LevelChange", new_level);
}

void CastMemoryPressureMonitor::SetDispatchCallback(
const DispatchCallback& callback) {
dispatch_callback_ = callback;
}

} // namespace chromecast
2 changes: 2 additions & 0 deletions chromecast/browser/cast_memory_pressure_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class CastMemoryPressureMonitor : public base::MemoryPressureMonitor {

// base::MemoryPressureMonitor implementation:
MemoryPressureLevel GetCurrentPressureLevel() const override;
void SetDispatchCallback(const DispatchCallback& callback) override;

private:
void PollPressureLevel();
void UpdateMemoryPressureLevel(MemoryPressureLevel new_level);

MemoryPressureLevel current_level_;
const int system_reserved_kb_;
DispatchCallback dispatch_callback_;
base::WeakPtrFactory<CastMemoryPressureMonitor> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(CastMemoryPressureMonitor);
Expand Down

0 comments on commit 456eef4

Please sign in to comment.