View Javadoc
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  
44  package org.eclipse.jgit.storage.file;
45  
46  import org.eclipse.jgit.internal.storage.file.WindowCache;
47  import org.eclipse.jgit.lib.Config;
48  import org.eclipse.jgit.storage.pack.PackConfig;
49  
50  /** Configuration parameters for JVM-wide buffer cache used by JGit. */
51  public class WindowCacheConfig {
52  	/** 1024 (number of bytes in one kibibyte/kilobyte) */
53  	public static final int KB = 1024;
54  
55  	/** 1024 {@link #KB} (number of bytes in one mebibyte/megabyte) */
56  	public static final int MB = 1024 * KB;
57  
58  	private int packedGitOpenFiles;
59  
60  	private long packedGitLimit;
61  
62  	private int packedGitWindowSize;
63  
64  	private boolean packedGitMMAP;
65  
66  	private int deltaBaseCacheLimit;
67  
68  	private int streamFileThreshold;
69  
70  	/** Create a default configuration. */
71  	public WindowCacheConfig() {
72  		packedGitOpenFiles = 128;
73  		packedGitLimit = 10 * MB;
74  		packedGitWindowSize = 8 * KB;
75  		packedGitMMAP = false;
76  		deltaBaseCacheLimit = 10 * MB;
77  		streamFileThreshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD;
78  	}
79  
80  	/**
81  	 * @return maximum number of streams to open at a time. Open packs count
82  	 *         against the process limits. <b>Default is 128.</b>
83  	 */
84  	public int getPackedGitOpenFiles() {
85  		return packedGitOpenFiles;
86  	}
87  
88  	/**
89  	 * @param fdLimit
90  	 *            maximum number of streams to open at a time. Open packs count
91  	 *            against the process limits
92  	 */
93  	public void setPackedGitOpenFiles(final int fdLimit) {
94  		packedGitOpenFiles = fdLimit;
95  	}
96  
97  	/**
98  	 * @return maximum number bytes of heap memory to dedicate to caching pack
99  	 *         file data. <b>Default is 10 MB.</b>
100 	 */
101 	public long getPackedGitLimit() {
102 		return packedGitLimit;
103 	}
104 
105 	/**
106 	 * @param newLimit
107 	 *            maximum number bytes of heap memory to dedicate to caching
108 	 *            pack file data.
109 	 */
110 	public void setPackedGitLimit(final long newLimit) {
111 		packedGitLimit = newLimit;
112 	}
113 
114 	/**
115 	 * @return size in bytes of a single window mapped or read in from the pack
116 	 *         file. <b>Default is 8 KB.</b>
117 	 */
118 	public int getPackedGitWindowSize() {
119 		return packedGitWindowSize;
120 	}
121 
122 	/**
123 	 * @param newSize
124 	 *            size in bytes of a single window read in from the pack file.
125 	 */
126 	public void setPackedGitWindowSize(final int newSize) {
127 		packedGitWindowSize = newSize;
128 	}
129 
130 	/**
131 	 * @return true enables use of Java NIO virtual memory mapping for windows;
132 	 *         false reads entire window into a byte[] with standard read calls.
133 	 *         <b>Default false.</b>
134 	 */
135 	public boolean isPackedGitMMAP() {
136 		return packedGitMMAP;
137 	}
138 
139 	/**
140 	 * @param usemmap
141 	 *            true enables use of Java NIO virtual memory mapping for
142 	 *            windows; false reads entire window into a byte[] with standard
143 	 *            read calls.
144 	 */
145 	public void setPackedGitMMAP(final boolean usemmap) {
146 		packedGitMMAP = usemmap;
147 	}
148 
149 	/**
150 	 * @return maximum number of bytes to cache in delta base cache for
151 	 *         inflated, recently accessed objects, without delta chains.
152 	 *         <b>Default 10 MB.</b>
153 	 */
154 	public int getDeltaBaseCacheLimit() {
155 		return deltaBaseCacheLimit;
156 	}
157 
158 	/**
159 	 * @param newLimit
160 	 *            maximum number of bytes to cache in delta base cache for
161 	 *            inflated, recently accessed objects, without delta chains.
162 	 */
163 	public void setDeltaBaseCacheLimit(final int newLimit) {
164 		deltaBaseCacheLimit = newLimit;
165 	}
166 
167 	/** @return the size threshold beyond which objects must be streamed. */
168 	public int getStreamFileThreshold() {
169 		return streamFileThreshold;
170 	}
171 
172 	/**
173 	 * @param newLimit
174 	 *            new byte limit for objects that must be streamed. Objects
175 	 *            smaller than this size can be obtained as a contiguous byte
176 	 *            array, while objects bigger than this size require using an
177 	 *            {@link org.eclipse.jgit.lib.ObjectStream}.
178 	 */
179 	public void setStreamFileThreshold(final int newLimit) {
180 		streamFileThreshold = newLimit;
181 	}
182 
183 	/**
184 	 * Update properties by setting fields from the configuration.
185 	 * <p>
186 	 * If a property is not defined in the configuration, then it is left
187 	 * unmodified.
188 	 *
189 	 * @param rc
190 	 *            configuration to read properties from.
191 	 * @return {@code this}.
192 	 * @since 3.0
193 	 */
194 	public WindowCacheConfig fromConfig(final Config rc) {
195 		setPackedGitOpenFiles(rc.getInt(
196 				"core", null, "packedgitopenfiles", getPackedGitOpenFiles())); //$NON-NLS-1$ //$NON-NLS-2$
197 		setPackedGitLimit(rc.getLong(
198 				"core", null, "packedgitlimit", getPackedGitLimit())); //$NON-NLS-1$ //$NON-NLS-2$
199 		setPackedGitWindowSize(rc.getInt(
200 				"core", null, "packedgitwindowsize", getPackedGitWindowSize())); //$NON-NLS-1$ //$NON-NLS-2$
201 		setPackedGitMMAP(rc.getBoolean(
202 				"core", null, "packedgitmmap", isPackedGitMMAP())); //$NON-NLS-1$ //$NON-NLS-2$
203 		setDeltaBaseCacheLimit(rc.getInt(
204 				"core", null, "deltabasecachelimit", getDeltaBaseCacheLimit())); //$NON-NLS-1$ //$NON-NLS-2$
205 
206 		long maxMem = Runtime.getRuntime().maxMemory();
207 		long sft = rc.getLong(
208 				"core", null, "streamfilethreshold", getStreamFileThreshold()); //$NON-NLS-1$ //$NON-NLS-2$
209 		sft = Math.min(sft, maxMem / 4); // don't use more than 1/4 of the heap
210 		sft = Math.min(sft, Integer.MAX_VALUE); // cannot exceed array length
211 		setStreamFileThreshold((int) sft);
212 		return this;
213 	}
214 
215 	/**
216 	 * Install this configuration as the live settings.
217 	 * <p>
218 	 * The new configuration is applied immediately. If the new limits are
219 	 * smaller than what what is currently cached, older entries will be purged
220 	 * as soon as possible to allow the cache to meet the new limit.
221 	 *
222 	 * @since 3.0
223 	 */
224 	@SuppressWarnings("deprecation")
225 	public void install() {
226 		WindowCache.reconfigure(this);
227 	}
228 }