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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.lib.Config;
  53. /** Configuration parameters for {@link DfsBlockCache}. */
  54. public class DfsBlockCacheConfig {
  55. /** 1024 (number of bytes in one kibibyte/kilobyte) */
  56. public static final int KB = 1024;
  57. /** 1024 {@link #KB} (number of bytes in one mebibyte/megabyte) */
  58. public static final int MB = 1024 * KB;
  59. private long blockLimit;
  60. private int blockSize;
  61. private double streamRatio;
  62. private int concurrencyLevel;
  63. /** Create a default configuration. */
  64. public DfsBlockCacheConfig() {
  65. setBlockLimit(32 * MB);
  66. setBlockSize(64 * KB);
  67. setStreamRatio(0.30);
  68. setConcurrencyLevel(32);
  69. }
  70. /**
  71. * @return maximum number bytes of heap memory to dedicate to caching pack
  72. * file data. <b>Default is 32 MB.</b>
  73. */
  74. public long getBlockLimit() {
  75. return blockLimit;
  76. }
  77. /**
  78. * @param newLimit
  79. * maximum number bytes of heap memory to dedicate to caching
  80. * pack file data.
  81. * @return {@code this}
  82. */
  83. public DfsBlockCacheConfig setBlockLimit(final long newLimit) {
  84. blockLimit = newLimit;
  85. return this;
  86. }
  87. /**
  88. * @return size in bytes of a single window mapped or read in from the pack
  89. * file. <b>Default is 64 KB.</b>
  90. */
  91. public int getBlockSize() {
  92. return blockSize;
  93. }
  94. /**
  95. * @param newSize
  96. * size in bytes of a single window read in from the pack file.
  97. * The value must be a power of 2.
  98. * @return {@code this}
  99. */
  100. public DfsBlockCacheConfig setBlockSize(final int newSize) {
  101. int size = Math.max(512, newSize);
  102. if ((size & (size - 1)) != 0) {
  103. throw new IllegalArgumentException(
  104. JGitText.get().blockSizeNotPowerOf2);
  105. }
  106. blockSize = size;
  107. return this;
  108. }
  109. /**
  110. * @return the estimated number of threads concurrently accessing the cache.
  111. * <b>Default is 32.</b>
  112. * @since 4.6
  113. */
  114. public int getConcurrencyLevel() {
  115. return concurrencyLevel;
  116. }
  117. /**
  118. * @param newConcurrencyLevel
  119. * the estimated number of threads concurrently accessing the
  120. * cache.
  121. * @return {@code this}
  122. * @since 4.6
  123. */
  124. public DfsBlockCacheConfig setConcurrencyLevel(
  125. final int newConcurrencyLevel) {
  126. concurrencyLevel = newConcurrencyLevel;
  127. return this;
  128. }
  129. /**
  130. * @return highest percentage of {@link #getBlockLimit()} a single pack can
  131. * occupy while being copied by the pack reuse strategy. <b>Default
  132. * is 0.30, or 30%</b>.
  133. * @since 4.0
  134. */
  135. public double getStreamRatio() {
  136. return streamRatio;
  137. }
  138. /**
  139. * @param ratio
  140. * percentage of cache to occupy with a copied pack.
  141. * @return {@code this}
  142. * @since 4.0
  143. */
  144. public DfsBlockCacheConfig setStreamRatio(double ratio) {
  145. streamRatio = Math.max(0, Math.min(ratio, 1.0));
  146. return this;
  147. }
  148. /**
  149. * Update properties by setting fields from the configuration.
  150. * <p>
  151. * If a property is not defined in the configuration, then it is left
  152. * unmodified.
  153. *
  154. * @param rc
  155. * configuration to read properties from.
  156. * @return {@code this}
  157. */
  158. public DfsBlockCacheConfig fromConfig(final Config rc) {
  159. setBlockLimit(rc.getLong(
  160. CONFIG_CORE_SECTION,
  161. CONFIG_DFS_SECTION,
  162. CONFIG_KEY_BLOCK_LIMIT,
  163. getBlockLimit()));
  164. setBlockSize(rc.getInt(
  165. CONFIG_CORE_SECTION,
  166. CONFIG_DFS_SECTION,
  167. CONFIG_KEY_BLOCK_SIZE,
  168. getBlockSize()));
  169. setConcurrencyLevel(rc.getInt(
  170. CONFIG_CORE_SECTION,
  171. CONFIG_DFS_SECTION,
  172. CONFIG_KEY_CONCURRENCY_LEVEL,
  173. getConcurrencyLevel()));
  174. String v = rc.getString(
  175. CONFIG_CORE_SECTION,
  176. CONFIG_DFS_SECTION,
  177. CONFIG_KEY_STREAM_RATIO);
  178. if (v != null) {
  179. try {
  180. setStreamRatio(Double.parseDouble(v));
  181. } catch (NumberFormatException e) {
  182. throw new IllegalArgumentException(MessageFormat.format(
  183. JGitText.get().enumValueNotSupported3,
  184. CONFIG_CORE_SECTION,
  185. CONFIG_DFS_SECTION,
  186. CONFIG_KEY_STREAM_RATIO, v));
  187. }
  188. }
  189. return this;
  190. }
  191. }