You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WindowCacheConfig.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.file;
  44. import org.eclipse.jgit.internal.storage.file.WindowCache;
  45. import org.eclipse.jgit.lib.Config;
  46. import org.eclipse.jgit.storage.pack.PackConfig;
  47. /** Configuration parameters for JVM-wide buffer cache used by JGit. */
  48. public class WindowCacheConfig {
  49. /** 1024 (number of bytes in one kibibyte/kilobyte) */
  50. public static final int KB = 1024;
  51. /** 1024 {@link #KB} (number of bytes in one mebibyte/megabyte) */
  52. public static final int MB = 1024 * KB;
  53. private int packedGitOpenFiles;
  54. private long packedGitLimit;
  55. private int packedGitWindowSize;
  56. private boolean packedGitMMAP;
  57. private int deltaBaseCacheLimit;
  58. private int streamFileThreshold;
  59. /** Create a default configuration. */
  60. public WindowCacheConfig() {
  61. packedGitOpenFiles = 128;
  62. packedGitLimit = 10 * MB;
  63. packedGitWindowSize = 8 * KB;
  64. packedGitMMAP = false;
  65. deltaBaseCacheLimit = 10 * MB;
  66. streamFileThreshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD;
  67. }
  68. /**
  69. * @return maximum number of streams to open at a time. Open packs count
  70. * against the process limits. <b>Default is 128.</b>
  71. */
  72. public int getPackedGitOpenFiles() {
  73. return packedGitOpenFiles;
  74. }
  75. /**
  76. * @param fdLimit
  77. * maximum number of streams to open at a time. Open packs count
  78. * against the process limits
  79. */
  80. public void setPackedGitOpenFiles(final int fdLimit) {
  81. packedGitOpenFiles = fdLimit;
  82. }
  83. /**
  84. * @return maximum number bytes of heap memory to dedicate to caching pack
  85. * file data. <b>Default is 10 MB.</b>
  86. */
  87. public long getPackedGitLimit() {
  88. return packedGitLimit;
  89. }
  90. /**
  91. * @param newLimit
  92. * maximum number bytes of heap memory to dedicate to caching
  93. * pack file data.
  94. */
  95. public void setPackedGitLimit(final long newLimit) {
  96. packedGitLimit = newLimit;
  97. }
  98. /**
  99. * @return size in bytes of a single window mapped or read in from the pack
  100. * file. <b>Default is 8 KB.</b>
  101. */
  102. public int getPackedGitWindowSize() {
  103. return packedGitWindowSize;
  104. }
  105. /**
  106. * @param newSize
  107. * size in bytes of a single window read in from the pack file.
  108. */
  109. public void setPackedGitWindowSize(final int newSize) {
  110. packedGitWindowSize = newSize;
  111. }
  112. /**
  113. * @return true enables use of Java NIO virtual memory mapping for windows;
  114. * false reads entire window into a byte[] with standard read calls.
  115. * <b>Default false.</b>
  116. */
  117. public boolean isPackedGitMMAP() {
  118. return packedGitMMAP;
  119. }
  120. /**
  121. * @param usemmap
  122. * true enables use of Java NIO virtual memory mapping for
  123. * windows; false reads entire window into a byte[] with standard
  124. * read calls.
  125. */
  126. public void setPackedGitMMAP(final boolean usemmap) {
  127. packedGitMMAP = usemmap;
  128. }
  129. /**
  130. * @return maximum number of bytes to cache in delta base cache for
  131. * inflated, recently accessed objects, without delta chains.
  132. * <b>Default 10 MB.</b>
  133. */
  134. public int getDeltaBaseCacheLimit() {
  135. return deltaBaseCacheLimit;
  136. }
  137. /**
  138. * @param newLimit
  139. * maximum number of bytes to cache in delta base cache for
  140. * inflated, recently accessed objects, without delta chains.
  141. */
  142. public void setDeltaBaseCacheLimit(final int newLimit) {
  143. deltaBaseCacheLimit = newLimit;
  144. }
  145. /** @return the size threshold beyond which objects must be streamed. */
  146. public int getStreamFileThreshold() {
  147. return streamFileThreshold;
  148. }
  149. /**
  150. * @param newLimit
  151. * new byte limit for objects that must be streamed. Objects
  152. * smaller than this size can be obtained as a contiguous byte
  153. * array, while objects bigger than this size require using an
  154. * {@link org.eclipse.jgit.lib.ObjectStream}.
  155. */
  156. public void setStreamFileThreshold(final int newLimit) {
  157. streamFileThreshold = newLimit;
  158. }
  159. /**
  160. * Update properties by setting fields from the configuration.
  161. * <p>
  162. * If a property is not defined in the configuration, then it is left
  163. * unmodified.
  164. *
  165. * @param rc
  166. * configuration to read properties from.
  167. * @return {@code this}.
  168. * @since 3.0
  169. */
  170. public WindowCacheConfig fromConfig(final Config rc) {
  171. setPackedGitOpenFiles(rc.getInt(
  172. "core", null, "packedgitopenfiles", getPackedGitOpenFiles())); //$NON-NLS-1$ //$NON-NLS-2$
  173. setPackedGitLimit(rc.getLong(
  174. "core", null, "packedgitlimit", getPackedGitLimit())); //$NON-NLS-1$ //$NON-NLS-2$
  175. setPackedGitWindowSize(rc.getInt(
  176. "core", null, "packedgitwindowsize", getPackedGitWindowSize())); //$NON-NLS-1$ //$NON-NLS-2$
  177. setPackedGitMMAP(rc.getBoolean(
  178. "core", null, "packedgitmmap", isPackedGitMMAP())); //$NON-NLS-1$ //$NON-NLS-2$
  179. setDeltaBaseCacheLimit(rc.getInt(
  180. "core", null, "deltabasecachelimit", getDeltaBaseCacheLimit())); //$NON-NLS-1$ //$NON-NLS-2$
  181. long maxMem = Runtime.getRuntime().maxMemory();
  182. long sft = rc.getLong(
  183. "core", null, "streamfilethreshold", getStreamFileThreshold()); //$NON-NLS-1$ //$NON-NLS-2$
  184. sft = Math.min(sft, maxMem / 4); // don't use more than 1/4 of the heap
  185. sft = Math.min(sft, Integer.MAX_VALUE); // cannot exceed array length
  186. setStreamFileThreshold((int) sft);
  187. return this;
  188. }
  189. /**
  190. * Install this configuration as the live settings.
  191. * <p>
  192. * The new configuration is applied immediately. If the new limits are
  193. * smaller than what what is currently cached, older entries will be purged
  194. * as soon as possible to allow the cache to meet the new limit.
  195. *
  196. * @since 3.0
  197. */
  198. @SuppressWarnings("deprecation")
  199. public void install() {
  200. WindowCache.reconfigure(this);
  201. }
  202. }