]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/subversion/subversion/libsvn_subr/cache_config.c
MFC r275385 (by bapt):
[FreeBSD/stable/10.git] / contrib / subversion / subversion / libsvn_subr / cache_config.c
1 /* svn_cache_config.c : configuration of internal caches
2  *
3  * ====================================================================
4  *    Licensed to the Apache Software Foundation (ASF) under one
5  *    or more contributor license agreements.  See the NOTICE file
6  *    distributed with this work for additional information
7  *    regarding copyright ownership.  The ASF licenses this file
8  *    to you under the Apache License, Version 2.0 (the
9  *    "License"); you may not use this file except in compliance
10  *    with the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *    Unless required by applicable law or agreed to in writing,
15  *    software distributed under the License is distributed on an
16  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  *    KIND, either express or implied.  See the License for the
18  *    specific language governing permissions and limitations
19  *    under the License.
20  * ====================================================================
21  */
22
23 #include <apr_atomic.h>
24
25 #include "svn_cache_config.h"
26 #include "private/svn_atomic.h"
27 #include "private/svn_cache.h"
28
29 #include "svn_pools.h"
30 #include "svn_sorts.h"
31
32 /* The cache settings as a process-wide singleton.
33  */
34 static svn_cache_config_t cache_settings =
35   {
36     /* default configuration:
37      *
38      * Please note that the resources listed below will be allocated
39      * PER PROCESS. Thus, the defaults chosen here are kept deliberately
40      * low to still make a difference yet to ensure that pre-fork servers
41      * on machines with small amounts of RAM aren't severely impacted.
42      */
43     0x1000000,   /* 16 MB for caches.
44                   * If you are running a single server process,
45                   * you may easily increase that to 50+% of your RAM
46                   * using svn_fs_set_cache_config().
47                   */
48     16,          /* up to 16 files kept open.
49                   * Most OS restrict the number of open file handles to
50                   * about 1000. To minimize I/O and OS overhead, values
51                   * of 500+ can be beneficial (use svn_fs_set_cache_config()
52                   * to change the configuration).
53                   * When running with a huge in-process cache, this number
54                   * has little impact on performance and a more modest
55                   * value (< 100) may be more suitable.
56                   */
57 #if APR_HAS_THREADS
58     FALSE        /* assume multi-threaded operation.
59                   * Because this simply activates proper synchronization
60                   * between threads, it is a safe default.
61                   */
62 #else
63     TRUE         /* single-threaded is the only supported mode of operation */
64 #endif
65 };
66
67 /* Get the current FSFS cache configuration. */
68 const svn_cache_config_t *
69 svn_cache_config_get(void)
70 {
71   return &cache_settings;
72 }
73
74 /* Initializer function as required by svn_atomic__init_once.  Allocate
75  * the process-global (singleton) membuffer cache and return it in the
76  * svn_membuffer_t * in *BATON.  UNUSED_POOL is unused and should be NULL.
77  */
78 static svn_error_t *
79 initialize_cache(void *baton, apr_pool_t *unused_pool)
80 {
81   svn_membuffer_t **cache_p = baton;
82   svn_membuffer_t *cache = NULL;
83
84   /* Limit the cache size to about half the available address space
85    * (typ. 1G under 32 bits).
86    */
87   apr_uint64_t cache_size = MIN(cache_settings.cache_size,
88                                 (apr_uint64_t)SVN_MAX_OBJECT_SIZE / 2);
89
90   /* Create caches at all? */
91   if (cache_size)
92     {
93       svn_error_t *err;
94
95       /* auto-allocate cache */
96       apr_allocator_t *allocator = NULL;
97       apr_pool_t *pool = NULL;
98
99       if (apr_allocator_create(&allocator))
100         return SVN_NO_ERROR;
101
102       /* Ensure that we free partially allocated data if we run OOM
103        * before the cache is complete: If the cache cannot be allocated
104        * in its full size, the create() function will clear the pool
105        * explicitly. The allocator will make sure that any memory no
106        * longer used by the pool will actually be returned to the OS.
107        *
108        * Please note that this pool and allocator is used *only* to
109        * allocate the large membuffer. All later dynamic allocations
110        * come from other, temporary pools and allocators.
111        */
112       apr_allocator_max_free_set(allocator, 1);
113
114       /* don't terminate upon OOM but make pool return a NULL pointer
115        * instead so we can disable caching gracefully and continue
116        * operation without membuffer caches.
117        */
118       apr_pool_create_ex(&pool, NULL, NULL, allocator);
119       if (pool == NULL)
120         return SVN_NO_ERROR;
121       apr_allocator_owner_set(allocator, pool);
122
123       err = svn_cache__membuffer_cache_create(
124           &cache,
125           (apr_size_t)cache_size,
126           (apr_size_t)(cache_size / 5),
127           0,
128           ! svn_cache_config_get()->single_threaded,
129           FALSE,
130           pool);
131
132       /* Some error occurred. Most likely it's an OOM error but we don't
133        * really care. Simply release all cache memory and disable caching
134        */
135       if (err)
136         {
137           /* Memory cleanup */
138           svn_pool_destroy(pool);
139
140           /* Document that we actually don't have a cache. */
141           cache_settings.cache_size = 0;
142
143           return svn_error_trace(err);
144         }
145
146       /* done */
147       *cache_p = cache;
148     }
149
150   return SVN_NO_ERROR;
151 }
152
153 /* Access the process-global (singleton) membuffer cache. The first call
154  * will automatically allocate the cache using the current cache config.
155  * NULL will be returned if the desired cache size is 0 or if the cache
156  * could not be created for some reason.
157  */
158 svn_membuffer_t *
159 svn_cache__get_global_membuffer_cache(void)
160 {
161   static svn_membuffer_t *cache = NULL;
162   static svn_atomic_t initialized = 0;
163
164   svn_error_t *err
165     = svn_atomic__init_once(&initialized, initialize_cache, &cache, NULL);
166   if (err)
167     {
168       /* no caches today ... */
169       svn_error_clear(err);
170       return NULL;
171     }
172
173   return cache;
174 }
175
176 void
177 svn_cache_config_set(const svn_cache_config_t *settings)
178 {
179   cache_settings = *settings;
180 }
181