Win32: fix an incorrect error status being propagated to the caller in case
[svn/apache.git] / subversion / libsvn_subr / cache_config.c
blob639124e79397971e29ed3519f73b29c9d3dac47a
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
19 * under the License.
20 * ====================================================================
23 #include <apr_atomic.h>
25 #include "svn_cache_config.h"
26 #include "private/svn_atomic.h"
27 #include "private/svn_cache.h"
29 #include "svn_pools.h"
30 #include "svn_sorts.h"
32 /* The cache settings as a process-wide singleton.
34 static svn_cache_config_t cache_settings =
36 /* default configuration:
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.
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().
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.
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.
62 #else
63 TRUE /* single-threaded is the only supported mode of operation */
64 #endif
67 /* Get the current FSFS cache configuration. */
68 const svn_cache_config_t *
69 svn_cache_config_get(void)
71 return &cache_settings;
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.
78 static svn_error_t *
79 initialize_cache(void *baton, apr_pool_t *unused_pool)
81 svn_membuffer_t **cache_p = baton;
82 svn_membuffer_t *cache = NULL;
84 /* Limit the cache size to about half the available address space
85 * (typ. 1G under 32 bits).
87 apr_uint64_t cache_size = MIN(cache_settings.cache_size,
88 (apr_uint64_t)SVN_MAX_OBJECT_SIZE / 2);
90 /* Create caches at all? */
91 if (cache_size)
93 svn_error_t *err;
95 /* auto-allocate cache */
96 apr_allocator_t *allocator = NULL;
97 apr_pool_t *pool = NULL;
99 if (apr_allocator_create(&allocator))
100 return SVN_NO_ERROR;
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.
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.
112 apr_allocator_max_free_set(allocator, 1);
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.
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);
123 err = svn_cache__membuffer_cache_create(
124 &cache,
125 (apr_size_t)cache_size,
126 (apr_size_t)(cache_size / 5),
128 ! svn_cache_config_get()->single_threaded,
129 FALSE,
130 pool);
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
135 if (err)
137 /* Memory cleanup */
138 svn_pool_destroy(pool);
140 /* Document that we actually don't have a cache. */
141 cache_settings.cache_size = 0;
143 return svn_error_trace(err);
146 /* done */
147 *cache_p = cache;
150 return SVN_NO_ERROR;
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.
158 svn_membuffer_t *
159 svn_cache__get_global_membuffer_cache(void)
161 static svn_membuffer_t *cache = NULL;
162 static svn_atomic_t initialized = 0;
164 svn_error_t *err
165 = svn_atomic__init_once(&initialized, initialize_cache, &cache, NULL);
166 if (err)
168 /* no caches today ... */
169 svn_error_clear(err);
170 return NULL;
173 return cache;
176 void
177 svn_cache_config_set(const svn_cache_config_t *settings)
179 cache_settings = *settings;