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