| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2011, 2012 Apple Inc. All Rights Reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #import "config.h" | |
| 27 #import "MemoryPressureHandler.h" | |
| 28 | |
| 29 #import <WebCore/CSSValuePool.h> | |
| 30 #import <WebCore/GCController.h> | |
| 31 #import <WebCore/FontCache.h> | |
| 32 #import <WebCore/MemoryCache.h> | |
| 33 #import <WebCore/PageCache.h> | |
| 34 #import <WebCore/LayerPool.h> | |
| 35 #import <WebCore/ScrollingThread.h> | |
| 36 #import <WebCore/StorageThread.h> | |
| 37 #import <WebCore/WorkerThread.h> | |
| 38 #import <wtf/CurrentTime.h> | |
| 39 #import <wtf/FastMalloc.h> | |
| 40 #import <wtf/Functional.h> | |
| 41 | |
| 42 #if !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 | |
| 43 #import "WebCoreSystemInterface.h" | |
| 44 #import <notify.h> | |
| 45 #endif | |
| 46 | |
| 47 using std::max; | |
| 48 | |
| 49 namespace WebCore { | |
| 50 | |
| 51 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 | |
| 52 | |
| 53 #if !PLATFORM(IOS) | |
| 54 static dispatch_source_t _cache_event_source = 0; | |
| 55 static dispatch_source_t _timer_event_source = 0; | |
| 56 static int _notifyToken; | |
| 57 | |
| 58 // Disable memory event reception for a minimum of s_minimumHoldOffTime | |
| 59 // seconds after receiving an event. Don't let events fire any sooner than | |
| 60 // s_holdOffMultiplier times the last cleanup processing time. Effectively | |
| 61 // this is 1 / s_holdOffMultiplier percent of the time. | |
| 62 // These value seems reasonable and testing verifies that it throttles frequent | |
| 63 // low memory events, greatly reducing CPU usage. | |
| 64 static const unsigned s_minimumHoldOffTime = 5; | |
| 65 static const unsigned s_holdOffMultiplier = 20; | |
| 66 | |
| 67 void MemoryPressureHandler::install() | |
| 68 { | |
| 69 if (m_installed || _timer_event_source) | |
| 70 return; | |
| 71 | |
| 72 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 73 _cache_event_source = wkCreateVMPressureDispatchOnMainQueue(); | |
| 74 if (_cache_event_source) { | |
| 75 dispatch_set_context(_cache_event_source, this); | |
| 76 dispatch_source_set_event_handler(_cache_event_source, ^{ memoryPres
sureHandler().respondToMemoryPressure();}); | |
| 77 dispatch_resume(_cache_event_source); | |
| 78 } | |
| 79 }); | |
| 80 | |
| 81 notify_register_dispatch("org.WebKit.lowMemory", &_notifyToken, | |
| 82 dispatch_get_main_queue(), ^(int) { memoryPressureHandler().respondToMe
moryPressure();}); | |
| 83 | |
| 84 m_installed = true; | |
| 85 } | |
| 86 | |
| 87 void MemoryPressureHandler::uninstall() | |
| 88 { | |
| 89 if (!m_installed) | |
| 90 return; | |
| 91 | |
| 92 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 93 if (_cache_event_source) { | |
| 94 dispatch_source_cancel(_cache_event_source); | |
| 95 dispatch_release(_cache_event_source); | |
| 96 _cache_event_source = 0; | |
| 97 } | |
| 98 | |
| 99 if (_timer_event_source) { | |
| 100 dispatch_source_cancel(_timer_event_source); | |
| 101 dispatch_release(_timer_event_source); | |
| 102 _timer_event_source = 0; | |
| 103 } | |
| 104 }); | |
| 105 | |
| 106 m_installed = false; | |
| 107 | |
| 108 notify_cancel(_notifyToken); | |
| 109 } | |
| 110 | |
| 111 void MemoryPressureHandler::holdOff(unsigned seconds) | |
| 112 { | |
| 113 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 114 _timer_event_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, dispatch_get_main_queue()); | |
| 115 if (_timer_event_source) { | |
| 116 dispatch_set_context(_timer_event_source, this); | |
| 117 dispatch_source_set_timer(_timer_event_source, dispatch_time(DISPATC
H_TIME_NOW, seconds * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 1 * s_minimumHoldOff
Time); | |
| 118 dispatch_source_set_event_handler(_timer_event_source, ^{ | |
| 119 if (_timer_event_source) { | |
| 120 dispatch_source_cancel(_timer_event_source); | |
| 121 dispatch_release(_timer_event_source); | |
| 122 _timer_event_source = 0; | |
| 123 } | |
| 124 memoryPressureHandler().install(); | |
| 125 }); | |
| 126 dispatch_resume(_timer_event_source); | |
| 127 } | |
| 128 }); | |
| 129 } | |
| 130 | |
| 131 void MemoryPressureHandler::respondToMemoryPressure() | |
| 132 { | |
| 133 uninstall(); | |
| 134 | |
| 135 double startTime = monotonicallyIncreasingTime(); | |
| 136 | |
| 137 m_lowMemoryHandler(false); | |
| 138 | |
| 139 unsigned holdOffTime = (monotonicallyIncreasingTime() - startTime) * s_holdO
ffMultiplier; | |
| 140 | |
| 141 holdOff(max(holdOffTime, s_minimumHoldOffTime)); | |
| 142 } | |
| 143 #endif // !PLATFORM(IOS) | |
| 144 | |
| 145 void MemoryPressureHandler::releaseMemory(bool) | |
| 146 { | |
| 147 int savedPageCacheCapacity = pageCache()->capacity(); | |
| 148 pageCache()->setCapacity(0); | |
| 149 pageCache()->setCapacity(savedPageCacheCapacity); | |
| 150 | |
| 151 NSURLCache *nsurlCache = [NSURLCache sharedURLCache]; | |
| 152 NSUInteger savedNsurlCacheMemoryCapacity = [nsurlCache memoryCapacity]; | |
| 153 [nsurlCache setMemoryCapacity:0]; | |
| 154 [nsurlCache setMemoryCapacity:savedNsurlCacheMemoryCapacity]; | |
| 155 | |
| 156 fontCache()->purgeInactiveFontData(); | |
| 157 | |
| 158 memoryCache()->pruneToPercentage(0); | |
| 159 | |
| 160 LayerPool::sharedPool()->drain(); | |
| 161 | |
| 162 cssValuePool().drain(); | |
| 163 | |
| 164 gcController().discardAllCompiledCode(); | |
| 165 | |
| 166 // FastMalloc has lock-free thread specific caches that can only be cleared
from the thread itself. | |
| 167 StorageThread::releaseFastMallocFreeMemoryInAllThreads(); | |
| 168 #if ENABLE(WORKERS) | |
| 169 WorkerThread::releaseFastMallocFreeMemoryInAllThreads(); | |
| 170 #endif | |
| 171 #if ENABLE(THREADED_SCROLLING) | |
| 172 ScrollingThread::dispatch(bind(WTF::releaseFastMallocFreeMemory)); | |
| 173 #endif | |
| 174 WTF::releaseFastMallocFreeMemory(); | |
| 175 } | |
| 176 #endif | |
| 177 | |
| 178 } // namespace WebCore | |
| OLD | NEW |