summaryrefslogtreecommitdiffstats
path: root/chromium/base/mac/memory_pressure_monitor.cc
blob: 5667aa6661c08cd2a89955ca4b58b9ee6ece3fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2015 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 "base/mac/memory_pressure_monitor.h"

#include <dlfcn.h>
#include <sys/sysctl.h>

#include "base/mac/mac_util.h"

namespace base {
namespace mac {

MemoryPressureListener::MemoryPressureLevel
MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure(
    int mac_memory_pressure) {
  switch (mac_memory_pressure) {
    case DISPATCH_MEMORYPRESSURE_NORMAL:
      return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
    case DISPATCH_MEMORYPRESSURE_WARN:
      return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
    case DISPATCH_MEMORYPRESSURE_CRITICAL:
      return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
  }
  return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
}

void MemoryPressureMonitor::NotifyMemoryPressureChanged(
    dispatch_source_s* event_source) {
  int mac_memory_pressure = dispatch_source_get_data(event_source);
  MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
      MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
  MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level);
}

MemoryPressureMonitor::MemoryPressureMonitor()
  : memory_level_event_source_(nullptr) {
  // _dispatch_source_type_memorypressure is not available prior to 10.9.
  dispatch_source_type_t dispatch_source_memorypressure =
      static_cast<dispatch_source_type_t>
          (dlsym(RTLD_NEXT, "_dispatch_source_type_memorypressure"));
  if (dispatch_source_memorypressure) {
    // The MemoryPressureListener doesn't want to know about transitions to
    // MEMORY_PRESSURE_LEVEL_NONE so don't watch for
    // DISPATCH_MEMORYPRESSURE_NORMAL notifications.
    memory_level_event_source_.reset(
        dispatch_source_create(dispatch_source_memorypressure, 0,
                               DISPATCH_MEMORYPRESSURE_WARN |
                                   DISPATCH_MEMORYPRESSURE_CRITICAL,
                               dispatch_get_global_queue(
                                   DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)));

    dispatch_source_set_event_handler(memory_level_event_source_.get(), ^{
        NotifyMemoryPressureChanged(memory_level_event_source_.get());
    });
    dispatch_retain(memory_level_event_source_.get());
    dispatch_resume(memory_level_event_source_.get());
  }
}

MemoryPressureMonitor::~MemoryPressureMonitor() {
}

MemoryPressureListener::MemoryPressureLevel
MemoryPressureMonitor::GetCurrentPressureLevel() const {
  if (base::mac::IsOSMountainLionOrEarlier()) {
    return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
  }
  int mac_memory_pressure;
  size_t length = sizeof(int);
  sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
               &length, nullptr, 0);
  return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
}

}  // namespace mac
}  // namespace base