Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.IOException;
  46. import java.lang.ref.ReferenceQueue;
  47. import java.util.concurrent.atomic.AtomicInteger;
  48. import java.util.concurrent.atomic.AtomicLong;
  49. /**
  50. * Caches slices of a {@link PackFile} in memory for faster read access.
  51. * <p>
  52. * The WindowCache serves as a Java based "buffer cache", loading segments of a
  53. * PackFile into the JVM heap prior to use. As JGit often wants to do reads of
  54. * only tiny slices of a file, the WindowCache tries to smooth out these tiny
  55. * reads into larger block-sized IO operations.
  56. */
  57. public class WindowCache extends OffsetCache<ByteWindow, WindowCache.WindowRef> {
  58. private static final int bits(int newSize) {
  59. if (newSize < 4096)
  60. throw new IllegalArgumentException("Invalid window size");
  61. if (Integer.bitCount(newSize) != 1)
  62. throw new IllegalArgumentException("Window size must be power of 2");
  63. return Integer.numberOfTrailingZeros(newSize);
  64. }
  65. private static volatile WindowCache cache;
  66. static {
  67. reconfigure(new WindowCacheConfig());
  68. }
  69. /**
  70. * Modify the configuration of the window cache.
  71. * <p>
  72. * The new configuration is applied immediately. If the new limits are
  73. * smaller than what what is currently cached, older entries will be purged
  74. * as soon as possible to allow the cache to meet the new limit.
  75. *
  76. * @param packedGitLimit
  77. * maximum number of bytes to hold within this instance.
  78. * @param packedGitWindowSize
  79. * number of bytes per window within the cache.
  80. * @param packedGitMMAP
  81. * true to enable use of mmap when creating windows.
  82. * @param deltaBaseCacheLimit
  83. * number of bytes to hold in the delta base cache.
  84. * @deprecated Use {@link WindowCacheConfig} instead.
  85. */
  86. public static void reconfigure(final int packedGitLimit,
  87. final int packedGitWindowSize, final boolean packedGitMMAP,
  88. final int deltaBaseCacheLimit) {
  89. final WindowCacheConfig c = new WindowCacheConfig();
  90. c.setPackedGitLimit(packedGitLimit);
  91. c.setPackedGitWindowSize(packedGitWindowSize);
  92. c.setPackedGitMMAP(packedGitMMAP);
  93. c.setDeltaBaseCacheLimit(deltaBaseCacheLimit);
  94. reconfigure(c);
  95. }
  96. /**
  97. * Modify the configuration of the window cache.
  98. * <p>
  99. * The new configuration is applied immediately. If the new limits are
  100. * smaller than what what is currently cached, older entries will be purged
  101. * as soon as possible to allow the cache to meet the new limit.
  102. *
  103. * @param cfg
  104. * the new window cache configuration.
  105. * @throws IllegalArgumentException
  106. * the cache configuration contains one or more invalid
  107. * settings, usually too low of a limit.
  108. */
  109. public static void reconfigure(final WindowCacheConfig cfg) {
  110. final WindowCache nc = new WindowCache(cfg);
  111. final WindowCache oc = cache;
  112. if (oc != null)
  113. oc.removeAll();
  114. cache = nc;
  115. UnpackedObjectCache.reconfigure(cfg);
  116. }
  117. static WindowCache getInstance() {
  118. return cache;
  119. }
  120. static final ByteWindow get(final PackFile pack, final long offset)
  121. throws IOException {
  122. final WindowCache c = cache;
  123. final ByteWindow r = c.getOrLoad(pack, c.toStart(offset));
  124. if (c != cache) {
  125. // The cache was reconfigured while we were using the old one
  126. // to load this window. The window is still valid, but our
  127. // cache may think its still live. Ensure the window is removed
  128. // from the old cache so resources can be released.
  129. //
  130. c.removeAll();
  131. }
  132. return r;
  133. }
  134. static final void purge(final PackFile pack) {
  135. cache.removeAll(pack);
  136. }
  137. private final int maxFiles;
  138. private final long maxBytes;
  139. private final boolean mmap;
  140. private final int windowSizeShift;
  141. private final int windowSize;
  142. private final AtomicInteger openFiles;
  143. private final AtomicLong openBytes;
  144. private WindowCache(final WindowCacheConfig cfg) {
  145. super(tableSize(cfg), lockCount(cfg));
  146. maxFiles = cfg.getPackedGitOpenFiles();
  147. maxBytes = cfg.getPackedGitLimit();
  148. mmap = cfg.isPackedGitMMAP();
  149. windowSizeShift = bits(cfg.getPackedGitWindowSize());
  150. windowSize = 1 << windowSizeShift;
  151. openFiles = new AtomicInteger();
  152. openBytes = new AtomicLong();
  153. if (maxFiles < 1)
  154. throw new IllegalArgumentException("Open files must be >= 1");
  155. if (maxBytes < windowSize)
  156. throw new IllegalArgumentException("Window size must be < limit");
  157. }
  158. int getOpenFiles() {
  159. return openFiles.get();
  160. }
  161. long getOpenBytes() {
  162. return openBytes.get();
  163. }
  164. @Override
  165. protected int hash(final int packHash, final long off) {
  166. return packHash + (int) (off >>> windowSizeShift);
  167. }
  168. @Override
  169. protected ByteWindow load(final PackFile pack, final long offset)
  170. throws IOException {
  171. if (pack.beginWindowCache())
  172. openFiles.incrementAndGet();
  173. try {
  174. if (mmap)
  175. return pack.mmap(offset, windowSize);
  176. return pack.read(offset, windowSize);
  177. } catch (IOException e) {
  178. close(pack);
  179. throw e;
  180. } catch (RuntimeException e) {
  181. close(pack);
  182. throw e;
  183. } catch (Error e) {
  184. close(pack);
  185. throw e;
  186. }
  187. }
  188. @Override
  189. protected WindowRef createRef(final PackFile p, final long o,
  190. final ByteWindow v) {
  191. final WindowRef ref = new WindowRef(p, o, v, queue);
  192. openBytes.addAndGet(ref.size);
  193. return ref;
  194. }
  195. @Override
  196. protected void clear(final WindowRef ref) {
  197. openBytes.addAndGet(-ref.size);
  198. close(ref.pack);
  199. }
  200. private void close(final PackFile pack) {
  201. if (pack.endWindowCache())
  202. openFiles.decrementAndGet();
  203. }
  204. @Override
  205. protected boolean isFull() {
  206. return maxFiles < openFiles.get() || maxBytes < openBytes.get();
  207. }
  208. private long toStart(final long offset) {
  209. return (offset >>> windowSizeShift) << windowSizeShift;
  210. }
  211. private static int tableSize(final WindowCacheConfig cfg) {
  212. final int wsz = cfg.getPackedGitWindowSize();
  213. final long limit = cfg.getPackedGitLimit();
  214. if (wsz <= 0)
  215. throw new IllegalArgumentException("Invalid window size");
  216. if (limit < wsz)
  217. throw new IllegalArgumentException("Window size must be < limit");
  218. return (int) Math.min(5 * (limit / wsz) / 2, 2000000000);
  219. }
  220. private static int lockCount(final WindowCacheConfig cfg) {
  221. return Math.max(cfg.getPackedGitOpenFiles(), 32);
  222. }
  223. static class WindowRef extends OffsetCache.Ref<ByteWindow> {
  224. final int size;
  225. WindowRef(final PackFile pack, final long position, final ByteWindow v,
  226. final ReferenceQueue<ByteWindow> queue) {
  227. super(pack, position, v, queue);
  228. size = v.size();
  229. }
  230. }
  231. }