1 /* svn_cache_config.c : configuration of internal caches
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
20 * ====================================================================
23 #include <apr_atomic.h>
25 #include "svn_cache_config.h"
26 #include "private/svn_cache.h"
28 #include "svn_pools.h"
30 /* The cache settings as a process-wide singleton.
32 static svn_cache_config_t cache_settings =
34 /* default configuration:
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.
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().
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.
56 FALSE /* assume multi-threaded operation.
57 * Because this simply activates proper synchronization
58 * between threads, it is a safe default.
61 TRUE /* single-threaded is the only supported mode of operation */
65 /* Get the current FSFS cache configuration. */
66 const svn_cache_config_t *
67 svn_cache_config_get(void)
69 return &cache_settings;
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.
78 svn_cache__get_global_membuffer_cache(void)
80 static svn_membuffer_t * volatile cache = NULL;
82 apr_uint64_t cache_size = cache_settings.cache_size;
83 if (!cache && cache_size)
87 svn_membuffer_t *old_cache = NULL;
88 svn_membuffer_t *new_cache = NULL;
90 /* auto-allocate cache */
91 apr_allocator_t *allocator = NULL;
92 apr_pool_t *pool = NULL;
94 if (apr_allocator_create(&allocator))
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.
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.
107 apr_allocator_max_free_set(allocator, 1);
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.
113 apr_pool_create_ex(&pool, NULL, NULL, allocator);
116 apr_allocator_owner_set(allocator, pool);
118 err = svn_cache__membuffer_cache_create(
120 (apr_size_t)cache_size,
121 (apr_size_t)(cache_size / 10),
123 ! svn_cache_config_get()->single_threaded,
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
132 /* Memory and error cleanup */
133 svn_error_clear(err);
134 svn_pool_destroy(pool);
136 /* Prevent future attempts to create the cache. However, an
137 * existing cache instance (see next comment) remains valid.
139 cache_settings.cache_size = 0;
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.
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.
153 * Cast is necessary because of APR bug:
154 * https://issues.apache.org/bugzilla/show_bug.cgi?id=50731
156 old_cache = apr_atomic_casptr((volatile void **)&cache, new_cache, NULL);
157 if (old_cache != NULL)
158 svn_pool_destroy(pool);
165 svn_cache_config_set(const svn_cache_config_t *settings)
167 cache_settings = *settings;