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