]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/subversion/subversion/libsvn_subr/cache_config.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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_cache.h"
27
28 #include "svn_pools.h"
29
30 /* The cache settings as a process-wide singleton.
31  */
32 static svn_cache_config_t cache_settings =
33   {
34     /* default configuration:
35      *
36      * Please note that the resources listed below will be allocated
37      * PER PROCESS. Thus, the defaults chosen here are kept deliberately
38      * low to still make a difference yet to ensure that pre-fork servers
39      * on machines with small amounts of RAM aren't severely impacted.
40      */
41     0x1000000,   /* 16 MB for caches.
42                   * If you are running a single server process,
43                   * you may easily increase that to 50+% of your RAM
44                   * using svn_fs_set_cache_config().
45                   */
46     16,          /* up to 16 files kept open.
47                   * Most OS restrict the number of open file handles to
48                   * about 1000. To minimize I/O and OS overhead, values
49                   * of 500+ can be beneficial (use svn_fs_set_cache_config()
50                   * to change the configuration).
51                   * When running with a huge in-process cache, this number
52                   * has little impact on performance and a more modest
53                   * value (< 100) may be more suitable.
54                   */
55 #if APR_HAS_THREADS
56     FALSE        /* assume multi-threaded operation.
57                   * Because this simply activates proper synchronization
58                   * between threads, it is a safe default.
59                   */
60 #else
61     TRUE         /* single-threaded is the only supported mode of operation */
62 #endif
63 };
64
65 /* Get the current FSFS cache configuration. */
66 const svn_cache_config_t *
67 svn_cache_config_get(void)
68 {
69   return &cache_settings;
70 }
71
72 /* Access the process-global (singleton) membuffer cache. The first call
73  * will automatically allocate the cache using the current cache config.
74  * NULL will be returned if the desired cache size is 0 or if the cache
75  * could not be created for some reason.
76  */
77 svn_membuffer_t *
78 svn_cache__get_global_membuffer_cache(void)
79 {
80   static svn_membuffer_t * volatile cache = NULL;
81
82   apr_uint64_t cache_size = cache_settings.cache_size;
83   if (!cache && cache_size)
84     {
85       svn_error_t *err;
86
87       svn_membuffer_t *old_cache = NULL;
88       svn_membuffer_t *new_cache = NULL;
89
90       /* auto-allocate cache */
91       apr_allocator_t *allocator = NULL;
92       apr_pool_t *pool = NULL;
93
94       if (apr_allocator_create(&allocator))
95         return NULL;
96
97       /* Ensure that we free partially allocated data if we run OOM
98        * before the cache is complete: If the cache cannot be allocated
99        * in its full size, the create() function will clear the pool
100        * explicitly. The allocator will make sure that any memory no
101        * longer used by the pool will actually be returned to the OS.
102        *
103        * Please note that this pool and allocator is used *only* to
104        * allocate the large membuffer. All later dynamic allocations
105        * come from other, temporary pools and allocators.
106        */
107       apr_allocator_max_free_set(allocator, 1);
108
109       /* don't terminate upon OOM but make pool return a NULL pointer
110        * instead so we can disable caching gracefully and continue
111        * operation without membuffer caches.
112        */
113       apr_pool_create_ex(&pool, NULL, NULL, allocator);
114       if (pool == NULL)
115         return NULL;
116       apr_allocator_owner_set(allocator, pool);
117
118       err = svn_cache__membuffer_cache_create(
119           &new_cache,
120           (apr_size_t)cache_size,
121           (apr_size_t)(cache_size / 10),
122           0,
123           ! svn_cache_config_get()->single_threaded,
124           FALSE,
125           pool);
126
127       /* Some error occurred. Most likely it's an OOM error but we don't
128        * really care. Simply release all cache memory and disable caching
129        */
130       if (err)
131         {
132           /* Memory and error cleanup */
133           svn_error_clear(err);
134           svn_pool_destroy(pool);
135
136           /* Prevent future attempts to create the cache. However, an
137            * existing cache instance (see next comment) remains valid.
138            */
139           cache_settings.cache_size = 0;
140
141           /* The current caller won't get the cache object.
142            * However, a concurrent call might have succeeded in creating
143            * the cache object. That call and all following ones will then
144            * use the successfully created cache instance.
145            */
146           return NULL;
147         }
148
149       /* Handle race condition: if we are the first to create a
150        * cache object, make it our global singleton. Otherwise,
151        * discard the new cache and keep the existing one.
152        *
153        * Cast is necessary because of APR bug:
154        * https://issues.apache.org/bugzilla/show_bug.cgi?id=50731
155        */
156       old_cache = apr_atomic_casptr((volatile void **)&cache, new_cache, NULL);
157       if (old_cache != NULL)
158         svn_pool_destroy(pool);
159     }
160
161   return cache;
162 }
163
164 void
165 svn_cache_config_set(const svn_cache_config_t *settings)
166 {
167   cache_settings = *settings;
168 }
169