CacheModel.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "config.h"
  26. #include "CacheModel.h"
  27. #include <algorithm>
  28. namespace WebKit {
  29. void calculateCacheSizes(CacheModel cacheModel, uint64_t memorySize, uint64_t diskFreeSize,
  30. unsigned& cacheTotalCapacity, unsigned& cacheMinDeadCapacity, unsigned& cacheMaxDeadCapacity, double& deadDecodedDataDeletionInterval,
  31. unsigned& pageCacheCapacity, unsigned long& urlCacheMemoryCapacity, unsigned long& urlCacheDiskCapacity)
  32. {
  33. switch (cacheModel) {
  34. case CacheModelDocumentViewer: {
  35. // Page cache capacity (in pages)
  36. pageCacheCapacity = 0;
  37. // Object cache capacities (in bytes)
  38. if (memorySize >= 2048)
  39. cacheTotalCapacity = 96 * 1024 * 1024;
  40. else if (memorySize >= 1536)
  41. cacheTotalCapacity = 64 * 1024 * 1024;
  42. else if (memorySize >= 1024)
  43. cacheTotalCapacity = 32 * 1024 * 1024;
  44. else if (memorySize >= 512)
  45. cacheTotalCapacity = 16 * 1024 * 1024;
  46. cacheMinDeadCapacity = 0;
  47. cacheMaxDeadCapacity = 0;
  48. // Foundation memory cache capacity (in bytes)
  49. urlCacheMemoryCapacity = 0;
  50. // Foundation disk cache capacity (in bytes)
  51. urlCacheDiskCapacity = 0;
  52. break;
  53. }
  54. case CacheModelDocumentBrowser: {
  55. // Page cache capacity (in pages)
  56. if (memorySize >= 1024)
  57. pageCacheCapacity = 3;
  58. else if (memorySize >= 512)
  59. pageCacheCapacity = 2;
  60. else if (memorySize >= 256)
  61. pageCacheCapacity = 1;
  62. else
  63. pageCacheCapacity = 0;
  64. // Object cache capacities (in bytes)
  65. if (memorySize >= 2048)
  66. cacheTotalCapacity = 96 * 1024 * 1024;
  67. else if (memorySize >= 1536)
  68. cacheTotalCapacity = 64 * 1024 * 1024;
  69. else if (memorySize >= 1024)
  70. cacheTotalCapacity = 32 * 1024 * 1024;
  71. else if (memorySize >= 512)
  72. cacheTotalCapacity = 16 * 1024 * 1024;
  73. cacheMinDeadCapacity = cacheTotalCapacity / 8;
  74. cacheMaxDeadCapacity = cacheTotalCapacity / 4;
  75. // Foundation memory cache capacity (in bytes)
  76. if (memorySize >= 2048)
  77. urlCacheMemoryCapacity = 4 * 1024 * 1024;
  78. else if (memorySize >= 1024)
  79. urlCacheMemoryCapacity = 2 * 1024 * 1024;
  80. else if (memorySize >= 512)
  81. urlCacheMemoryCapacity = 1 * 1024 * 1024;
  82. else
  83. urlCacheMemoryCapacity = 512 * 1024;
  84. // Foundation disk cache capacity (in bytes)
  85. if (diskFreeSize >= 16384)
  86. urlCacheDiskCapacity = 50 * 1024 * 1024;
  87. else if (diskFreeSize >= 8192)
  88. urlCacheDiskCapacity = 40 * 1024 * 1024;
  89. else if (diskFreeSize >= 4096)
  90. urlCacheDiskCapacity = 30 * 1024 * 1024;
  91. else
  92. urlCacheDiskCapacity = 20 * 1024 * 1024;
  93. break;
  94. }
  95. case CacheModelPrimaryWebBrowser: {
  96. // Page cache capacity (in pages)
  97. if (memorySize >= 1024)
  98. pageCacheCapacity = 3;
  99. else if (memorySize >= 512)
  100. pageCacheCapacity = 2;
  101. else if (memorySize >= 256)
  102. pageCacheCapacity = 1;
  103. else
  104. pageCacheCapacity = 0;
  105. // Object cache capacities (in bytes)
  106. // (Testing indicates that value / MB depends heavily on content and
  107. // browsing pattern. Even growth above 128MB can have substantial
  108. // value / MB for some content / browsing patterns.)
  109. if (memorySize >= 2048)
  110. cacheTotalCapacity = 128 * 1024 * 1024;
  111. else if (memorySize >= 1536)
  112. cacheTotalCapacity = 96 * 1024 * 1024;
  113. else if (memorySize >= 1024)
  114. cacheTotalCapacity = 64 * 1024 * 1024;
  115. else if (memorySize >= 512)
  116. cacheTotalCapacity = 32 * 1024 * 1024;
  117. cacheMinDeadCapacity = cacheTotalCapacity / 4;
  118. cacheMaxDeadCapacity = cacheTotalCapacity / 2;
  119. // This code is here to avoid a PLT regression. We can remove it if we
  120. // can prove that the overall system gain would justify the regression.
  121. cacheMaxDeadCapacity = std::max(24u, cacheMaxDeadCapacity);
  122. deadDecodedDataDeletionInterval = 60;
  123. // Foundation memory cache capacity (in bytes)
  124. // (These values are small because WebCore does most caching itself.)
  125. if (memorySize >= 1024)
  126. urlCacheMemoryCapacity = 4 * 1024 * 1024;
  127. else if (memorySize >= 512)
  128. urlCacheMemoryCapacity = 2 * 1024 * 1024;
  129. else if (memorySize >= 256)
  130. urlCacheMemoryCapacity = 1 * 1024 * 1024;
  131. else
  132. urlCacheMemoryCapacity = 512 * 1024;
  133. // Foundation disk cache capacity (in bytes)
  134. if (diskFreeSize >= 16384)
  135. urlCacheDiskCapacity = 175 * 1024 * 1024;
  136. else if (diskFreeSize >= 8192)
  137. urlCacheDiskCapacity = 150 * 1024 * 1024;
  138. else if (diskFreeSize >= 4096)
  139. urlCacheDiskCapacity = 125 * 1024 * 1024;
  140. else if (diskFreeSize >= 2048)
  141. urlCacheDiskCapacity = 100 * 1024 * 1024;
  142. else if (diskFreeSize >= 1024)
  143. urlCacheDiskCapacity = 75 * 1024 * 1024;
  144. else
  145. urlCacheDiskCapacity = 50 * 1024 * 1024;
  146. break;
  147. }
  148. default:
  149. ASSERT_NOT_REACHED();
  150. };
  151. }
  152. } // namespace WebKit