~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Storage.File/WindowCacheConfig.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
using System;
 
45
using NGit;
 
46
using NGit.Storage.Pack;
 
47
using Sharpen;
 
48
 
 
49
namespace NGit.Storage.File
 
50
{
 
51
        /// <summary>
 
52
        /// Configuration parameters for
 
53
        /// <see cref="WindowCache">WindowCache</see>
 
54
        /// .
 
55
        /// </summary>
 
56
        public class WindowCacheConfig
 
57
        {
 
58
                /// <summary>1024 (number of bytes in one kibibyte/kilobyte)</summary>
 
59
                public const int KB = 1024;
 
60
 
 
61
                /// <summary>
 
62
                /// 1024
 
63
                /// <see cref="KB">KB</see>
 
64
                /// (number of bytes in one mebibyte/megabyte)
 
65
                /// </summary>
 
66
                public const int MB = 1024 * KB;
 
67
 
 
68
                private int packedGitOpenFiles;
 
69
 
 
70
                private long packedGitLimit;
 
71
 
 
72
                private int packedGitWindowSize;
 
73
 
 
74
                private bool packedGitMMAP;
 
75
 
 
76
                private int deltaBaseCacheLimit;
 
77
 
 
78
                private int streamFileThreshold;
 
79
 
 
80
                /// <summary>Create a default configuration.</summary>
 
81
                /// <remarks>Create a default configuration.</remarks>
 
82
                public WindowCacheConfig()
 
83
                {
 
84
                        packedGitOpenFiles = 128;
 
85
                        packedGitLimit = 10 * MB;
 
86
                        packedGitWindowSize = 8 * KB;
 
87
                        packedGitMMAP = false;
 
88
                        deltaBaseCacheLimit = 10 * MB;
 
89
                        streamFileThreshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD;
 
90
                }
 
91
 
 
92
                /// <returns>
 
93
                /// maximum number of streams to open at a time. Open packs count
 
94
                /// against the process limits. <b>Default is 128.</b>
 
95
                /// </returns>
 
96
                public virtual int GetPackedGitOpenFiles()
 
97
                {
 
98
                        return packedGitOpenFiles;
 
99
                }
 
100
 
 
101
                /// <param name="fdLimit">
 
102
                /// maximum number of streams to open at a time. Open packs count
 
103
                /// against the process limits
 
104
                /// </param>
 
105
                public virtual void SetPackedGitOpenFiles(int fdLimit)
 
106
                {
 
107
                        packedGitOpenFiles = fdLimit;
 
108
                }
 
109
 
 
110
                /// <returns>
 
111
                /// maximum number bytes of heap memory to dedicate to caching pack
 
112
                /// file data. <b>Default is 10 MB.</b>
 
113
                /// </returns>
 
114
                public virtual long GetPackedGitLimit()
 
115
                {
 
116
                        return packedGitLimit;
 
117
                }
 
118
 
 
119
                /// <param name="newLimit">
 
120
                /// maximum number bytes of heap memory to dedicate to caching
 
121
                /// pack file data.
 
122
                /// </param>
 
123
                public virtual void SetPackedGitLimit(long newLimit)
 
124
                {
 
125
                        packedGitLimit = newLimit;
 
126
                }
 
127
 
 
128
                /// <returns>
 
129
                /// size in bytes of a single window mapped or read in from the pack
 
130
                /// file. <b>Default is 8 KB.</b>
 
131
                /// </returns>
 
132
                public virtual int GetPackedGitWindowSize()
 
133
                {
 
134
                        return packedGitWindowSize;
 
135
                }
 
136
 
 
137
                /// <param name="newSize">size in bytes of a single window read in from the pack file.
 
138
                ///     </param>
 
139
                public virtual void SetPackedGitWindowSize(int newSize)
 
140
                {
 
141
                        packedGitWindowSize = newSize;
 
142
                }
 
143
 
 
144
                /// <returns>
 
145
                /// true enables use of Java NIO virtual memory mapping for windows;
 
146
                /// false reads entire window into a byte[] with standard read calls.
 
147
                /// <b>Default false.</b>
 
148
                /// </returns>
 
149
                public virtual bool IsPackedGitMMAP()
 
150
                {
 
151
                        return packedGitMMAP;
 
152
                }
 
153
 
 
154
                /// <param name="usemmap">
 
155
                /// true enables use of Java NIO virtual memory mapping for
 
156
                /// windows; false reads entire window into a byte[] with standard
 
157
                /// read calls.
 
158
                /// </param>
 
159
                public virtual void SetPackedGitMMAP(bool usemmap)
 
160
                {
 
161
                        packedGitMMAP = usemmap;
 
162
                }
 
163
 
 
164
                /// <returns>
 
165
                /// maximum number of bytes to cache in
 
166
                /// <see cref="DeltaBaseCache">DeltaBaseCache</see>
 
167
                /// for inflated, recently accessed objects, without delta chains.
 
168
                /// <b>Default 10 MB.</b>
 
169
                /// </returns>
 
170
                public virtual int GetDeltaBaseCacheLimit()
 
171
                {
 
172
                        return deltaBaseCacheLimit;
 
173
                }
 
174
 
 
175
                /// <param name="newLimit">
 
176
                /// maximum number of bytes to cache in
 
177
                /// <see cref="DeltaBaseCache">DeltaBaseCache</see>
 
178
                /// for inflated, recently accessed
 
179
                /// objects, without delta chains.
 
180
                /// </param>
 
181
                public virtual void SetDeltaBaseCacheLimit(int newLimit)
 
182
                {
 
183
                        deltaBaseCacheLimit = newLimit;
 
184
                }
 
185
 
 
186
                /// <returns>the size threshold beyond which objects must be streamed.</returns>
 
187
                public virtual int GetStreamFileThreshold()
 
188
                {
 
189
                        return streamFileThreshold;
 
190
                }
 
191
 
 
192
                /// <param name="newLimit">
 
193
                /// new byte limit for objects that must be streamed. Objects
 
194
                /// smaller than this size can be obtained as a contiguous byte
 
195
                /// array, while objects bigger than this size require using an
 
196
                /// <see cref="NGit.ObjectStream">NGit.ObjectStream</see>
 
197
                /// .
 
198
                /// </param>
 
199
                public virtual void SetStreamFileThreshold(int newLimit)
 
200
                {
 
201
                        streamFileThreshold = newLimit;
 
202
                }
 
203
 
 
204
                /// <summary>Update properties by setting fields from the configuration.</summary>
 
205
                /// <remarks>
 
206
                /// Update properties by setting fields from the configuration.
 
207
                /// <p>
 
208
                /// If a property is not defined in the configuration, then it is left
 
209
                /// unmodified.
 
210
                /// </remarks>
 
211
                /// <param name="rc">configuration to read properties from.</param>
 
212
                public virtual void FromConfig(Config rc)
 
213
                {
 
214
                        SetPackedGitOpenFiles(rc.GetInt("core", null, "packedgitopenfiles", GetPackedGitOpenFiles
 
215
                                ()));
 
216
                        SetPackedGitLimit(rc.GetLong("core", null, "packedgitlimit", GetPackedGitLimit())
 
217
                                );
 
218
                        SetPackedGitWindowSize(rc.GetInt("core", null, "packedgitwindowsize", GetPackedGitWindowSize
 
219
                                ()));
 
220
                        SetPackedGitMMAP(rc.GetBoolean("core", null, "packedgitmmap", IsPackedGitMMAP()));
 
221
                        SetDeltaBaseCacheLimit(rc.GetInt("core", null, "deltabasecachelimit", GetDeltaBaseCacheLimit
 
222
                                ()));
 
223
                        long maxMem = Runtime.GetRuntime().MaxMemory();
 
224
                        long sft = rc.GetLong("core", null, "streamfilethreshold", GetStreamFileThreshold
 
225
                                ());
 
226
                        sft = Math.Min(sft, maxMem / 4);
 
227
                        // don't use more than 1/4 of the heap
 
228
                        sft = Math.Min(sft, int.MaxValue);
 
229
                        // cannot exceed array length
 
230
                        SetStreamFileThreshold((int)sft);
 
231
                }
 
232
        }
 
233
}