1/*
2 * Copyright (C) 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "CacheModel.h"
28
29#include <algorithm>
30#include <wtf/RAMSize.h>
31#include <wtf/Seconds.h>
32#include <wtf/StdLibExtras.h>
33
34namespace WebKit {
35
36void calculateMemoryCacheSizes(CacheModel cacheModel, unsigned& cacheTotalCapacity, unsigned& cacheMinDeadCapacity, unsigned& cacheMaxDeadCapacity, Seconds& deadDecodedDataDeletionInterval, unsigned& pageCacheCapacity)
37{
38 uint64_t memorySize = ramSize() / MB;
39
40 switch (cacheModel) {
41 case CacheModel::DocumentViewer: {
42 // Page cache capacity (in pages)
43 pageCacheCapacity = 0;
44
45 // Object cache capacities (in bytes)
46 if (memorySize >= 2048)
47 cacheTotalCapacity = 96 * MB;
48 else if (memorySize >= 1536)
49 cacheTotalCapacity = 64 * MB;
50 else if (memorySize >= 1024)
51 cacheTotalCapacity = 32 * MB;
52 else if (memorySize >= 512)
53 cacheTotalCapacity = 16 * MB;
54 else
55 cacheTotalCapacity = 8 * MB;
56
57 cacheMinDeadCapacity = 0;
58 cacheMaxDeadCapacity = 0;
59
60 break;
61 }
62 case CacheModel::DocumentBrowser: {
63 // Page cache capacity (in pages)
64 if (memorySize >= 512)
65 pageCacheCapacity = 2;
66 else if (memorySize >= 256)
67 pageCacheCapacity = 1;
68 else
69 pageCacheCapacity = 0;
70
71 // Object cache capacities (in bytes)
72 if (memorySize >= 2048)
73 cacheTotalCapacity = 96 * MB;
74 else if (memorySize >= 1536)
75 cacheTotalCapacity = 64 * MB;
76 else if (memorySize >= 1024)
77 cacheTotalCapacity = 32 * MB;
78 else if (memorySize >= 512)
79 cacheTotalCapacity = 16 * MB;
80 else
81 cacheTotalCapacity = 8 * MB;
82
83 cacheMinDeadCapacity = cacheTotalCapacity / 8;
84 cacheMaxDeadCapacity = cacheTotalCapacity / 4;
85
86 break;
87 }
88 case CacheModel::PrimaryWebBrowser: {
89 // Page cache capacity (in pages)
90 if (memorySize >= 512)
91 pageCacheCapacity = 2;
92 else if (memorySize >= 256)
93 pageCacheCapacity = 1;
94 else
95 pageCacheCapacity = 0;
96
97 // Object cache capacities (in bytes)
98 // (Testing indicates that value / MB depends heavily on content and
99 // browsing pattern. Even growth above 128MB can have substantial
100 // value / MB for some content / browsing patterns.)
101 if (memorySize >= 2048)
102 cacheTotalCapacity = 128 * MB;
103 else if (memorySize >= 1536)
104 cacheTotalCapacity = 96 * MB;
105 else if (memorySize >= 1024)
106 cacheTotalCapacity = 64 * MB;
107 else if (memorySize >= 512)
108 cacheTotalCapacity = 32 * MB;
109 else
110 cacheTotalCapacity = 16 * MB;
111
112 cacheMinDeadCapacity = cacheTotalCapacity / 4;
113 cacheMaxDeadCapacity = cacheTotalCapacity / 2;
114
115 // This code is here to avoid a PLT regression. We can remove it if we
116 // can prove that the overall system gain would justify the regression.
117 cacheMaxDeadCapacity = std::max(24u, cacheMaxDeadCapacity);
118
119 deadDecodedDataDeletionInterval = 60_s;
120
121 break;
122 }
123 default:
124 ASSERT_NOT_REACHED();
125 };
126}
127
128void calculateURLCacheSizes(CacheModel cacheModel, uint64_t diskFreeSize, unsigned& urlCacheMemoryCapacity, uint64_t& urlCacheDiskCapacity)
129{
130 switch (cacheModel) {
131 case CacheModel::DocumentViewer: {
132 // Foundation memory cache capacity (in bytes)
133 urlCacheMemoryCapacity = 0;
134
135 // Disk cache capacity (in bytes)
136 urlCacheDiskCapacity = 0;
137
138 break;
139 }
140 case CacheModel::DocumentBrowser: {
141 uint64_t memorySize = ramSize() / MB;
142
143 // Foundation memory cache capacity (in bytes)
144 if (memorySize >= 2048)
145 urlCacheMemoryCapacity = 4 * MB;
146 else if (memorySize >= 1024)
147 urlCacheMemoryCapacity = 2 * MB;
148 else if (memorySize >= 512)
149 urlCacheMemoryCapacity = 1 * MB;
150 else
151 urlCacheMemoryCapacity = 512 * KB;
152
153 // Disk cache capacity (in bytes)
154 if (diskFreeSize >= 16384)
155 urlCacheDiskCapacity = 75 * MB;
156 else if (diskFreeSize >= 8192)
157 urlCacheDiskCapacity = 40 * MB;
158 else if (diskFreeSize >= 4096)
159 urlCacheDiskCapacity = 30 * MB;
160 else
161 urlCacheDiskCapacity = 20 * MB;
162
163 break;
164 }
165 case CacheModel::PrimaryWebBrowser: {
166 uint64_t memorySize = ramSize() / MB;
167
168#if PLATFORM(IOS_FAMILY)
169 if (memorySize >= 1024)
170 urlCacheMemoryCapacity = 16 * MB;
171 else
172 urlCacheMemoryCapacity = 8 * MB;
173#else
174 // Foundation memory cache capacity (in bytes)
175 // (These values are small because WebCore does most caching itself.)
176 if (memorySize >= 1024)
177 urlCacheMemoryCapacity = 4 * MB;
178 else if (memorySize >= 512)
179 urlCacheMemoryCapacity = 2 * MB;
180 else if (memorySize >= 256)
181 urlCacheMemoryCapacity = 1 * MB;
182 else
183 urlCacheMemoryCapacity = 512 * KB;
184#endif
185
186 // Disk cache capacity (in bytes)
187 if (diskFreeSize >= 16384)
188 urlCacheDiskCapacity = 500 * MB;
189 else if (diskFreeSize >= 8192)
190 urlCacheDiskCapacity = 250 * MB;
191 else if (diskFreeSize >= 4096)
192 urlCacheDiskCapacity = 125 * MB;
193 else if (diskFreeSize >= 2048)
194 urlCacheDiskCapacity = 100 * MB;
195 else if (diskFreeSize >= 1024)
196 urlCacheDiskCapacity = 75 * MB;
197 else
198 urlCacheDiskCapacity = 50 * MB;
199
200 break;
201 }
202 default:
203 ASSERT_NOT_REACHED();
204 };
205}
206
207} // namespace WebKit
208