https://bugs.webkit.org/show_bug.cgi?id=81963 WebProcess can get stuck in GC during...
[WebKit-https.git] / Source / WebCore / platform / mac / MemoryPressureHandlerMac.mm
1 /*
2  * Copyright (C) 2011 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/GCController.h>
30 #import <WebCore/FontCache.h>
31 #import <WebCore/MemoryCache.h>
32 #import <WebCore/PageCache.h>
33 #import <wtf/FastMalloc.h>
34
35 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD) && !PLATFORM(IOS)
36 #import "WebCoreSystemInterface.h"
37 #import <notify.h>
38 #endif
39
40 namespace WebCore {
41
42 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
43
44 #if !PLATFORM(IOS)
45 static dispatch_source_t _cache_event_source = 0;
46 static dispatch_source_t _timer_event_source = 0;
47 static int _notifyToken;
48
49 // Disable memory event reception for 5 seconds after receiving an event. 
50 // This value seems reasonable and testing verifies that it throttles frequent
51 // low memory events, greatly reducing CPU usage.
52 static const time_t s_secondsBetweenMemoryCleanup = 5;
53
54 void MemoryPressureHandler::install()
55 {
56     if (m_installed || _timer_event_source)
57         return;
58
59     dispatch_async(dispatch_get_main_queue(), ^{
60         _cache_event_source = wkCreateVMPressureDispatchOnMainQueue();
61         if (_cache_event_source) {
62             dispatch_set_context(_cache_event_source, this);
63             dispatch_source_set_event_handler(_cache_event_source, ^{ memoryPressureHandler().respondToMemoryPressure();});
64             dispatch_resume(_cache_event_source);
65         }
66     });
67
68     notify_register_dispatch("org.WebKit.lowMemory", &_notifyToken,
69          dispatch_get_main_queue(), ^(int) { memoryPressureHandler().respondToMemoryPressure();});
70
71     m_installed = true;
72 }
73
74 void MemoryPressureHandler::uninstall()
75 {
76     if (!m_installed)
77         return;
78
79     dispatch_source_cancel(_cache_event_source);
80     dispatch_release(_cache_event_source);
81     _cache_event_source = 0;
82
83     if (_timer_event_source) {
84         dispatch_source_cancel(_timer_event_source);
85         dispatch_release(_timer_event_source);
86         _timer_event_source = 0;
87     }
88
89     m_installed = false;
90     
91     notify_cancel(_notifyToken);
92 }
93
94 void MemoryPressureHandler::holdOff(unsigned seconds)
95 {
96     uninstall();
97
98     dispatch_async(dispatch_get_main_queue(), ^{
99         _timer_event_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
100         if (_timer_event_source) {
101             dispatch_set_context(_timer_event_source, this);
102             dispatch_source_set_timer(_timer_event_source, dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 1 * s_secondsBetweenMemoryCleanup);
103             dispatch_source_set_event_handler(_timer_event_source, ^{
104                 dispatch_source_cancel(_timer_event_source);
105                 dispatch_release(_timer_event_source);
106                 _timer_event_source = 0;
107                 memoryPressureHandler().install();
108             });
109             dispatch_resume(_timer_event_source);
110         }
111     });
112 }
113
114 void MemoryPressureHandler::respondToMemoryPressure()
115 {
116     holdOff(s_secondsBetweenMemoryCleanup);
117
118     releaseMemory(false);
119 }
120 #endif // !PLATFORM(IOS)
121
122 void MemoryPressureHandler::releaseMemory(bool critical)
123 {
124     int savedPageCacheCapacity = pageCache()->capacity();
125     pageCache()->setCapacity(critical ? 0 : pageCache()->pageCount() / 2);
126     pageCache()->setCapacity(savedPageCacheCapacity);
127     pageCache()->releaseAutoreleasedPagesNow();
128
129     NSURLCache *nsurlCache = [NSURLCache sharedURLCache];
130     NSUInteger savedNsurlCacheMemoryCapacity = [nsurlCache memoryCapacity];
131     [nsurlCache setMemoryCapacity:critical ? 0 : [nsurlCache currentMemoryUsage] / 2];
132     [nsurlCache setMemoryCapacity:savedNsurlCacheMemoryCapacity];
133
134     fontCache()->purgeInactiveFontData();
135
136     memoryCache()->pruneToPercentage(critical ? 0 : 0.5f);
137
138     WTF::releaseFastMallocFreeMemory();
139 }
140 #endif
141
142 } // namespace WebCore