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

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

Issue 2434103003: Add Mac memory pressure statistic reporting and consolidate platform code (Closed)
Patch Set: Created 4 years, 2 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
(...skipping 15 matching lines...) Expand all
26 case DISPATCH_MEMORYPRESSURE_NORMAL: 26 case DISPATCH_MEMORYPRESSURE_NORMAL:
27 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 27 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
28 case DISPATCH_MEMORYPRESSURE_WARN: 28 case DISPATCH_MEMORYPRESSURE_WARN:
29 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; 29 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
30 case DISPATCH_MEMORYPRESSURE_CRITICAL: 30 case DISPATCH_MEMORYPRESSURE_CRITICAL:
31 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 31 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
32 } 32 }
33 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; 33 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
34 } 34 }
35 35
36 void MemoryPressureMonitor::NotifyMemoryPressureChanged(
37 dispatch_source_s* event_source,
38 const MemoryPressureMonitor::DispatchCallback& dispatch_callback) {
39 int mac_memory_pressure = dispatch_source_get_data(event_source);
40 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
41 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
42 dispatch_callback.Run(memory_pressure_level);
43 }
44
45 MemoryPressureMonitor::MemoryPressureMonitor() 36 MemoryPressureMonitor::MemoryPressureMonitor()
46 // The MemoryPressureListener doesn't want to know about transitions to
47 // MEMORY_PRESSURE_LEVEL_NONE so don't watch for
48 // DISPATCH_MEMORYPRESSURE_NORMAL notifications.
49 : memory_level_event_source_(dispatch_source_create( 37 : memory_level_event_source_(dispatch_source_create(
50 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 38 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,
51 0, 39 0,
52 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, 40 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL |
41 DISPATCH_MEMORYPRESSURE_NORMAL,
53 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))), 42 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))),
54 dispatch_callback_( 43 dispatch_callback_(
55 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)) { 44 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)),
shrike 2016/10/20 21:04:31 I think MemoryPressureListener::NotifyMemoryPressu
lgrey 2016/10/21 14:15:04 This threw me for a loop for a bit, then I realize
chrisha 2016/10/21 18:18:58 Yeah, the rename might help. Maybe "OnMemoryPressu
lgrey 2016/10/21 18:39:35 Done.
45 last_pressure_change_(CFAbsoluteTimeGetCurrent()) {
46 last_pressure_level_ = GetCurrentPressureLevel();
56 dispatch_source_set_event_handler(memory_level_event_source_, ^{ 47 dispatch_source_set_event_handler(memory_level_event_source_, ^{
57 NotifyMemoryPressureChanged(memory_level_event_source_.get(), 48 NotifyMemoryPressureChanged(memory_level_event_source_.get(),
58 dispatch_callback_); 49 dispatch_callback_);
59 }); 50 });
60 dispatch_resume(memory_level_event_source_); 51 dispatch_resume(memory_level_event_source_);
61 } 52 }
62 53
63 MemoryPressureMonitor::~MemoryPressureMonitor() { 54 MemoryPressureMonitor::~MemoryPressureMonitor() {
64 dispatch_source_cancel(memory_level_event_source_); 55 dispatch_source_cancel(memory_level_event_source_);
65 } 56 }
66 57
67 MemoryPressureListener::MemoryPressureLevel 58 MemoryPressureListener::MemoryPressureLevel
68 MemoryPressureMonitor::GetCurrentPressureLevel() const { 59 MemoryPressureMonitor::GetCurrentPressureLevel() const {
69 int mac_memory_pressure; 60 int mac_memory_pressure;
70 size_t length = sizeof(int); 61 size_t length = sizeof(int);
71 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, 62 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
72 &length, nullptr, 0); 63 &length, nullptr, 0);
73 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 64 return MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
74 } 65 }
66 void MemoryPressureMonitor::NotifyMemoryPressureChanged(
67 dispatch_source_s* event_source,
68 const MemoryPressureMonitor::DispatchCallback& dispatch_callback) {
69 int mac_memory_pressure = dispatch_source_get_data(event_source);
70 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
71 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
72 CFTimeInterval now = CFAbsoluteTimeGetCurrent();
73 CFTimeInterval since_last_change = now - last_pressure_change_;
74 last_pressure_change_ = now;
75
76 // TODO(lgrey): Two questions:
77 // 1) Is it OK to give up precision by flooring this?
78 // 2) Is there a better way to send multiple events?
79 int last_level_seconds = static_cast<int>(since_last_change);
80 for (int i = 0; i < last_level_seconds; i++) {
shrike 2016/10/20 21:04:30 This is unfortunate, because last_level_seconds co
lgrey 2016/10/21 14:15:04 asvitkine@ what do you think? For context, this p
chrisha 2016/10/21 18:18:58 We had talked about switching to polling on Mac, a
lgrey 2016/10/21 18:39:35 My mistake, it looks like Chrome polls once a seco
81 RecordMemoryPressure(last_pressure_level_);
82 }
83 last_pressure_level_ = memory_pressure_level;
84 if (memory_pressure_level !=
85 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE)
86 dispatch_callback.Run(memory_pressure_level);
87 }
75 88
76 void MemoryPressureMonitor::SetDispatchCallback( 89 void MemoryPressureMonitor::SetDispatchCallback(
77 const DispatchCallback& callback) { 90 const DispatchCallback& callback) {
78 dispatch_callback_ = callback; 91 dispatch_callback_ = callback;
79 } 92 }
80 93
81 } // namespace mac 94 } // namespace mac
82 } // namespace base 95 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/memory_pressure_monitor_mac.h ('k') | base/memory/memory_pressure_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698