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.

DfsBlockCacheConfig.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2011, 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.internal.storage.dfs;
  44. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
  45. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
  46. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_LIMIT;
  47. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_SIZE;
  48. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CONCURRENCY_LEVEL;
  49. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
  50. import java.text.MessageFormat;
  51. import java.util.function.Consumer;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.lib.Config;
  54. /**
  55. * Configuration parameters for
  56. * {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache}.
  57. */
  58. public class DfsBlockCacheConfig {
  59. /** 1024 (number of bytes in one kibibyte/kilobyte) */
  60. public static final int KB = 1024;
  61. /** 1024 {@link #KB} (number of bytes in one mebibyte/megabyte) */
  62. public static final int MB = 1024 * KB;
  63. private long blockLimit;
  64. private int blockSize;
  65. private double streamRatio;
  66. private int concurrencyLevel;
  67. private Consumer<Long> refLock;
  68. /**
  69. * Create a default configuration.
  70. */
  71. public DfsBlockCacheConfig() {
  72. setBlockLimit(32 * MB);
  73. setBlockSize(64 * KB);
  74. setStreamRatio(0.30);
  75. setConcurrencyLevel(32);
  76. }
  77. /**
  78. * Get maximum number bytes of heap memory to dedicate to caching pack file
  79. * data.
  80. *
  81. * @return maximum number bytes of heap memory to dedicate to caching pack
  82. * file data. <b>Default is 32 MB.</b>
  83. */
  84. public long getBlockLimit() {
  85. return blockLimit;
  86. }
  87. /**
  88. * Set maximum number bytes of heap memory to dedicate to caching pack file
  89. * data.
  90. * <p>
  91. * It is strongly recommended to set the block limit to be an integer multiple
  92. * of the block size. This constraint is not enforced by this method (since
  93. * it may be called before {@link #setBlockSize(int)}), but it is enforced by
  94. * {@link #fromConfig(Config)}.
  95. *
  96. * @param newLimit
  97. * maximum number bytes of heap memory to dedicate to caching
  98. * pack file data; must be positive.
  99. * @return {@code this}
  100. */
  101. public DfsBlockCacheConfig setBlockLimit(long newLimit) {
  102. if (newLimit <= 0) {
  103. throw new IllegalArgumentException(MessageFormat.format(
  104. JGitText.get().blockLimitNotPositive,
  105. Long.valueOf(newLimit)));
  106. }
  107. blockLimit = newLimit;
  108. return this;
  109. }
  110. /**
  111. * Get size in bytes of a single window mapped or read in from the pack
  112. * file.
  113. *
  114. * @return size in bytes of a single window mapped or read in from the pack
  115. * file. <b>Default is 64 KB.</b>
  116. */
  117. public int getBlockSize() {
  118. return blockSize;
  119. }
  120. /**
  121. * Set size in bytes of a single window read in from the pack file.
  122. *
  123. * @param newSize
  124. * size in bytes of a single window read in from the pack file.
  125. * The value must be a power of 2.
  126. * @return {@code this}
  127. */
  128. public DfsBlockCacheConfig setBlockSize(int newSize) {
  129. int size = Math.max(512, newSize);
  130. if ((size & (size - 1)) != 0) {
  131. throw new IllegalArgumentException(
  132. JGitText.get().blockSizeNotPowerOf2);
  133. }
  134. blockSize = size;
  135. return this;
  136. }
  137. /**
  138. * Get the estimated number of threads concurrently accessing the cache.
  139. *
  140. * @return the estimated number of threads concurrently accessing the cache.
  141. * <b>Default is 32.</b>
  142. */
  143. public int getConcurrencyLevel() {
  144. return concurrencyLevel;
  145. }
  146. /**
  147. * Set the estimated number of threads concurrently accessing the cache.
  148. *
  149. * @param newConcurrencyLevel
  150. * the estimated number of threads concurrently accessing the
  151. * cache.
  152. * @return {@code this}
  153. */
  154. public DfsBlockCacheConfig setConcurrencyLevel(
  155. final int newConcurrencyLevel) {
  156. concurrencyLevel = newConcurrencyLevel;
  157. return this;
  158. }
  159. /**
  160. * Get highest percentage of {@link #getBlockLimit()} a single pack can
  161. * occupy while being copied by the pack reuse strategy.
  162. *
  163. * @return highest percentage of {@link #getBlockLimit()} a single pack can
  164. * occupy while being copied by the pack reuse strategy. <b>Default
  165. * is 0.30, or 30%</b>.
  166. */
  167. public double getStreamRatio() {
  168. return streamRatio;
  169. }
  170. /**
  171. * Set percentage of cache to occupy with a copied pack.
  172. *
  173. * @param ratio
  174. * percentage of cache to occupy with a copied pack.
  175. * @return {@code this}
  176. */
  177. public DfsBlockCacheConfig setStreamRatio(double ratio) {
  178. streamRatio = Math.max(0, Math.min(ratio, 1.0));
  179. return this;
  180. }
  181. /**
  182. * Get the consumer of the object reference lock wait time in milliseconds.
  183. *
  184. * @return consumer of wait time in milliseconds.
  185. */
  186. public Consumer<Long> getRefLockWaitTimeConsumer() {
  187. return refLock;
  188. }
  189. /**
  190. * Set the consumer for lock wait time.
  191. *
  192. * @param c
  193. * consumer of wait time in milliseconds.
  194. * @return {@code this}
  195. */
  196. public DfsBlockCacheConfig setRefLockWaitTimeConsumer(Consumer<Long> c) {
  197. refLock = c;
  198. return this;
  199. }
  200. /**
  201. * Update properties by setting fields from the configuration.
  202. * <p>
  203. * If a property is not defined in the configuration, then it is left
  204. * unmodified.
  205. * <p>
  206. * Enforces certain constraints on the combination of settings in the config,
  207. * for example that the block limit is a multiple of the block size.
  208. *
  209. * @param rc
  210. * configuration to read properties from.
  211. * @return {@code this}
  212. */
  213. public DfsBlockCacheConfig fromConfig(Config rc) {
  214. long cfgBlockLimit = rc.getLong(
  215. CONFIG_CORE_SECTION,
  216. CONFIG_DFS_SECTION,
  217. CONFIG_KEY_BLOCK_LIMIT,
  218. getBlockLimit());
  219. int cfgBlockSize = rc.getInt(
  220. CONFIG_CORE_SECTION,
  221. CONFIG_DFS_SECTION,
  222. CONFIG_KEY_BLOCK_SIZE,
  223. getBlockSize());
  224. if (cfgBlockLimit % cfgBlockSize != 0) {
  225. throw new IllegalArgumentException(MessageFormat.format(
  226. JGitText.get().blockLimitNotMultipleOfBlockSize,
  227. Long.valueOf(cfgBlockLimit),
  228. Long.valueOf(cfgBlockSize)));
  229. }
  230. setBlockLimit(cfgBlockLimit);
  231. setBlockSize(cfgBlockSize);
  232. setConcurrencyLevel(rc.getInt(
  233. CONFIG_CORE_SECTION,
  234. CONFIG_DFS_SECTION,
  235. CONFIG_KEY_CONCURRENCY_LEVEL,
  236. getConcurrencyLevel()));
  237. String v = rc.getString(
  238. CONFIG_CORE_SECTION,
  239. CONFIG_DFS_SECTION,
  240. CONFIG_KEY_STREAM_RATIO);
  241. if (v != null) {
  242. try {
  243. setStreamRatio(Double.parseDouble(v));
  244. } catch (NumberFormatException e) {
  245. throw new IllegalArgumentException(MessageFormat.format(
  246. JGitText.get().enumValueNotSupported3,
  247. CONFIG_CORE_SECTION,
  248. CONFIG_DFS_SECTION,
  249. CONFIG_KEY_STREAM_RATIO, v));
  250. }
  251. }
  252. return this;
  253. }
  254. }