View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.dfs;
12  
13  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
14  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
15  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_LIMIT;
16  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_SIZE;
17  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CONCURRENCY_LEVEL;
18  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
19  
20  import java.text.MessageFormat;
21  import java.util.Collections;
22  import java.util.Map;
23  import java.util.function.Consumer;
24  
25  import org.eclipse.jgit.internal.JGitText;
26  import org.eclipse.jgit.internal.storage.pack.PackExt;
27  import org.eclipse.jgit.lib.Config;
28  
29  /**
30   * Configuration parameters for
31   * {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache}.
32   */
33  public class DfsBlockCacheConfig {
34  	/** 1024 (number of bytes in one kibibyte/kilobyte) */
35  	public static final int KB = 1024;
36  
37  	/** 1024 {@link #KB} (number of bytes in one mebibyte/megabyte) */
38  	public static final int MB = 1024 * KB;
39  
40  	/** Default number of max cache hits. */
41  	public static final int DEFAULT_CACHE_HOT_MAX = 1;
42  
43  	private long blockLimit;
44  	private int blockSize;
45  	private double streamRatio;
46  	private int concurrencyLevel;
47  
48  	private Consumer<Long> refLock;
49  
50  	private Map<PackExt, Integer> cacheHotMap;
51  
52  	/**
53  	 * Create a default configuration.
54  	 */
55  	public DfsBlockCacheConfig() {
56  		setBlockLimit(32 * MB);
57  		setBlockSize(64 * KB);
58  		setStreamRatio(0.30);
59  		setConcurrencyLevel(32);
60  		cacheHotMap = Collections.emptyMap();
61  	}
62  
63  	/**
64  	 * Get maximum number bytes of heap memory to dedicate to caching pack file
65  	 * data.
66  	 *
67  	 * @return maximum number bytes of heap memory to dedicate to caching pack
68  	 *         file data. <b>Default is 32 MB.</b>
69  	 */
70  	public long getBlockLimit() {
71  		return blockLimit;
72  	}
73  
74  	/**
75  	 * Set maximum number bytes of heap memory to dedicate to caching pack file
76  	 * data.
77  	 * <p>
78  	 * It is strongly recommended to set the block limit to be an integer multiple
79  	 * of the block size. This constraint is not enforced by this method (since
80  	 * it may be called before {@link #setBlockSize(int)}), but it is enforced by
81  	 * {@link #fromConfig(Config)}.
82  	 *
83  	 * @param newLimit
84  	 *            maximum number bytes of heap memory to dedicate to caching
85  	 *            pack file data; must be positive.
86  	 * @return {@code this}
87  	 */
88  	public DfsBlockCacheConfig setBlockLimit(long newLimit) {
89  		if (newLimit <= 0) {
90  			throw new IllegalArgumentException(MessageFormat.format(
91  					JGitText.get().blockLimitNotPositive,
92  					Long.valueOf(newLimit)));
93  		}
94  		blockLimit = newLimit;
95  		return this;
96  	}
97  
98  	/**
99  	 * Get size in bytes of a single window mapped or read in from the pack
100 	 * file.
101 	 *
102 	 * @return size in bytes of a single window mapped or read in from the pack
103 	 *         file. <b>Default is 64 KB.</b>
104 	 */
105 	public int getBlockSize() {
106 		return blockSize;
107 	}
108 
109 	/**
110 	 * Set size in bytes of a single window read in from the pack file.
111 	 *
112 	 * @param newSize
113 	 *            size in bytes of a single window read in from the pack file.
114 	 *            The value must be a power of 2.
115 	 * @return {@code this}
116 	 */
117 	public DfsBlockCacheConfig setBlockSize(int newSize) {
118 		int size = Math.max(512, newSize);
119 		if ((size & (size - 1)) != 0) {
120 			throw new IllegalArgumentException(
121 					JGitText.get().blockSizeNotPowerOf2);
122 		}
123 		blockSize = size;
124 		return this;
125 	}
126 
127 	/**
128 	 * Get the estimated number of threads concurrently accessing the cache.
129 	 *
130 	 * @return the estimated number of threads concurrently accessing the cache.
131 	 *         <b>Default is 32.</b>
132 	 */
133 	public int getConcurrencyLevel() {
134 		return concurrencyLevel;
135 	}
136 
137 	/**
138 	 * Set the estimated number of threads concurrently accessing the cache.
139 	 *
140 	 * @param newConcurrencyLevel
141 	 *            the estimated number of threads concurrently accessing the
142 	 *            cache.
143 	 * @return {@code this}
144 	 */
145 	public DfsBlockCacheConfig setConcurrencyLevel(
146 			final int newConcurrencyLevel) {
147 		concurrencyLevel = newConcurrencyLevel;
148 		return this;
149 	}
150 
151 	/**
152 	 * Get highest percentage of {@link #getBlockLimit()} a single pack can
153 	 * occupy while being copied by the pack reuse strategy.
154 	 *
155 	 * @return highest percentage of {@link #getBlockLimit()} a single pack can
156 	 *         occupy while being copied by the pack reuse strategy. <b>Default
157 	 *         is 0.30, or 30%</b>.
158 	 */
159 	public double getStreamRatio() {
160 		return streamRatio;
161 	}
162 
163 	/**
164 	 * Set percentage of cache to occupy with a copied pack.
165 	 *
166 	 * @param ratio
167 	 *            percentage of cache to occupy with a copied pack.
168 	 * @return {@code this}
169 	 */
170 	public DfsBlockCacheConfig setStreamRatio(double ratio) {
171 		streamRatio = Math.max(0, Math.min(ratio, 1.0));
172 		return this;
173 	}
174 
175 	/**
176 	 * Get the consumer of the object reference lock wait time in milliseconds.
177 	 *
178 	 * @return consumer of wait time in milliseconds.
179 	 */
180 	public Consumer<Long> getRefLockWaitTimeConsumer() {
181 		return refLock;
182 	}
183 
184 	/**
185 	 * Set the consumer for lock wait time.
186 	 *
187 	 * @param c
188 	 *            consumer of wait time in milliseconds.
189 	 * @return {@code this}
190 	 */
191 	public DfsBlockCacheConfig setRefLockWaitTimeConsumer(Consumer<Long> c) {
192 		refLock = c;
193 		return this;
194 	}
195 
196 	/**
197 	 * Get the map of hot count per pack extension for {@code DfsBlockCache}.
198 	 *
199 	 * @return map of hot count per pack extension for {@code DfsBlockCache}.
200 	 */
201 	public Map<PackExt, Integer> getCacheHotMap() {
202 		return cacheHotMap;
203 	}
204 
205 	/**
206 	 * Set the map of hot count per pack extension for {@code DfsBlockCache}.
207 	 *
208 	 * @param cacheHotMap
209 	 *            map of hot count per pack extension for {@code DfsBlockCache}.
210 	 * @return {@code this}
211 	 */
212 	public DfsBlockCacheConfig setCacheHotMap(
213 			Map<PackExt, Integer> cacheHotMap) {
214 		this.cacheHotMap = Collections.unmodifiableMap(cacheHotMap);
215 		return this;
216 	}
217 
218 	/**
219 	 * Update properties by setting fields from the configuration.
220 	 * <p>
221 	 * If a property is not defined in the configuration, then it is left
222 	 * unmodified.
223 	 * <p>
224 	 * Enforces certain constraints on the combination of settings in the config,
225 	 * for example that the block limit is a multiple of the block size.
226 	 *
227 	 * @param rc
228 	 *            configuration to read properties from.
229 	 * @return {@code this}
230 	 */
231 	public DfsBlockCacheConfig fromConfig(Config rc) {
232 		long cfgBlockLimit = rc.getLong(
233 				CONFIG_CORE_SECTION,
234 				CONFIG_DFS_SECTION,
235 				CONFIG_KEY_BLOCK_LIMIT,
236 				getBlockLimit());
237 		int cfgBlockSize = rc.getInt(
238 				CONFIG_CORE_SECTION,
239 				CONFIG_DFS_SECTION,
240 				CONFIG_KEY_BLOCK_SIZE,
241 				getBlockSize());
242 		if (cfgBlockLimit % cfgBlockSize != 0) {
243 			throw new IllegalArgumentException(MessageFormat.format(
244 					JGitText.get().blockLimitNotMultipleOfBlockSize,
245 					Long.valueOf(cfgBlockLimit),
246 					Long.valueOf(cfgBlockSize)));
247 		}
248 
249 		setBlockLimit(cfgBlockLimit);
250 		setBlockSize(cfgBlockSize);
251 
252 		setConcurrencyLevel(rc.getInt(
253 				CONFIG_CORE_SECTION,
254 				CONFIG_DFS_SECTION,
255 				CONFIG_KEY_CONCURRENCY_LEVEL,
256 				getConcurrencyLevel()));
257 
258 		String v = rc.getString(
259 				CONFIG_CORE_SECTION,
260 				CONFIG_DFS_SECTION,
261 				CONFIG_KEY_STREAM_RATIO);
262 		if (v != null) {
263 			try {
264 				setStreamRatio(Double.parseDouble(v));
265 			} catch (NumberFormatException e) {
266 				throw new IllegalArgumentException(MessageFormat.format(
267 						JGitText.get().enumValueNotSupported3,
268 						CONFIG_CORE_SECTION,
269 						CONFIG_DFS_SECTION,
270 						CONFIG_KEY_STREAM_RATIO, v), e);
271 			}
272 		}
273 		return this;
274 	}
275 }