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

Side by Side Diff: base/memory/memory_pressure_monitor_mac.cc

Issue 2097753002: Make memory pressure notifier configurable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 4 years, 5 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/memory_pressure_monitor_mac.h" 5 #include "base/memory/memory_pressure_monitor_mac.h"
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <sys/sysctl.h> 9 #include <sys/sysctl.h>
10 10
11 #include "base/logging.h"
11 #include "base/mac/mac_util.h" 12 #include "base/mac/mac_util.h"
12 13
13 // Redeclare for partial 10.9 availability. 14 // Redeclare for partial 10.9 availability.
14 DISPATCH_EXPORT const struct dispatch_source_type_s 15 DISPATCH_EXPORT const struct dispatch_source_type_s
15 _dispatch_source_type_memorypressure; 16 _dispatch_source_type_memorypressure;
16 17
17 namespace base { 18 namespace base {
18 namespace mac { 19 namespace mac {
19 20
20 MemoryPressureListener::MemoryPressureLevel 21 MemoryPressureListener::MemoryPressureLevel
21 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure( 22 MemoryPressureMonitor::MemoryPressureLevelForMacMemoryPressure(
22 int mac_memory_pressure) { 23 int mac_memory_pressure) {
23 switch (mac_memory_pressure) { 24 switch (mac_memory_pressure) {
24 case DISPATCH_MEMORYPRESSURE_NORMAL: 25 case DISPATCH_MEMORYPRESSURE_NORMAL:
25 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 26 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
26 case DISPATCH_MEMORYPRESSURE_WARN: 27 case DISPATCH_MEMORYPRESSURE_WARN:
27 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; 28 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
28 case DISPATCH_MEMORYPRESSURE_CRITICAL: 29 case DISPATCH_MEMORYPRESSURE_CRITICAL:
29 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 30 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
30 } 31 }
31 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 32 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
32 } 33 }
33 34
34 void MemoryPressureMonitor::NotifyMemoryPressureChanged( 35 void MemoryPressureMonitor::NotifyMemoryPressureChanged(
35 dispatch_source_s* event_source) { 36 dispatch_source_s* event_source,
37 MemoryPressureMonitor::DispatchCallback dispatch_callback) {
36 int mac_memory_pressure = dispatch_source_get_data(event_source); 38 int mac_memory_pressure = dispatch_source_get_data(event_source);
37 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = 39 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
38 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 40 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
39 MemoryPressureListener::NotifyMemoryPressure(memory_pressure_level); 41 dispatch_callback(memory_pressure_level);
40 } 42 }
41 43
42 MemoryPressureMonitor::MemoryPressureMonitor() 44 MemoryPressureMonitor::MemoryPressureMonitor()
43 // The MemoryPressureListener doesn't want to know about transitions to 45 // The MemoryPressureListener doesn't want to know about transitions to
44 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for 46 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for
45 // DISPATCH_MEMORYPRESSURE_NORMAL notifications. 47 // DISPATCH_MEMORYPRESSURE_NORMAL notifications.
46 : memory_level_event_source_(dispatch_source_create( 48 : memory_level_event_source_(dispatch_source_create(
47 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 49 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,
48 0, 50 0,
49 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, 51 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL,
50 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))) { 52 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))) {
51 dispatch_source_set_event_handler(memory_level_event_source_, ^{ 53 dispatch_source_set_event_handler(memory_level_event_source_, ^{
52 NotifyMemoryPressureChanged(memory_level_event_source_.get()); 54 NotifyMemoryPressureChanged(memory_level_event_source_.get(),
55 dispatch_callback_);
danakj 2016/07/08 00:03:53 does this not bind the current callback forever? m
bashi 2016/07/08 00:13:05 Not sure how blocks work too but seems a bug. I'll
53 }); 56 });
54 dispatch_resume(memory_level_event_source_); 57 dispatch_resume(memory_level_event_source_);
55 } 58 }
56 59
57 MemoryPressureMonitor::~MemoryPressureMonitor() { 60 MemoryPressureMonitor::~MemoryPressureMonitor() {
58 dispatch_source_cancel(memory_level_event_source_); 61 dispatch_source_cancel(memory_level_event_source_);
59 } 62 }
60 63
61 MemoryPressureListener::MemoryPressureLevel 64 MemoryPressureListener::MemoryPressureLevel
62 MemoryPressureMonitor::GetCurrentPressureLevel() const { 65 MemoryPressureMonitor::GetCurrentPressureLevel() const {
63 int mac_memory_pressure; 66 int mac_memory_pressure;
64 size_t length = sizeof(int); 67 size_t length = sizeof(int);
65 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, 68 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
66 &length, nullptr, 0); 69 &length, nullptr, 0);
67 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 70 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
68 } 71 }
69 72
73 void MemoryPressureMonitor::SetDispatchCallback(DispatchCallback* callback) {
74 dispatch_callback_ = callback;
75 }
76
70 } // namespace mac 77 } // namespace mac
71 } // namespace base 78 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698