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 31/* The cache settings as a process-wide singleton. 32 */ 33static svn_cache_config_t cache_settings = 34 { 35 /* default configuration: 36 * 37 * Please note that the resources listed below will be allocated 38 * PER PROCESS. Thus, the defaults chosen here are kept deliberately 39 * low to still make a difference yet to ensure that pre-fork servers 40 * on machines with small amounts of RAM aren't severely impacted. 41 */ 42 0x1000000, /* 16 MB for caches. 43 * If you are running a single server process, 44 * you may easily increase that to 50+% of your RAM 45 * using svn_fs_set_cache_config(). 46 */ 47 16, /* up to 16 files kept open. 48 * Most OS restrict the number of open file handles to 49 * about 1000. To minimize I/O and OS overhead, values 50 * of 500+ can be beneficial (use svn_fs_set_cache_config() 51 * to change the configuration). 52 * When running with a huge in-process cache, this number 53 * has little impact on performance and a more modest 54 * value (< 100) may be more suitable. 55 */ 56#if APR_HAS_THREADS 57 FALSE /* assume multi-threaded operation. 58 * Because this simply activates proper synchronization 59 * between threads, it is a safe default. 60 */ 61#else 62 TRUE /* single-threaded is the only supported mode of operation */ 63#endif 64}; 65 66/* Get the current FSFS cache configuration. */ 67const svn_cache_config_t * 68svn_cache_config_get(void) 69{ 70 return &cache_settings; 71} 72 73/* Initializer function as required by svn_atomic__init_once. Allocate 74 * the process-global (singleton) membuffer cache and return it in the 75 * svn_membuffer_t * in *BATON. UNUSED_POOL is unused and should be NULL. 76 */ 77static svn_error_t * 78initialize_cache(void *baton, apr_pool_t *unused_pool) 79{ 80 svn_membuffer_t **cache_p = baton; 81 svn_membuffer_t *cache = NULL; 82 83 apr_uint64_t cache_size = cache_settings.cache_size; 84 if (cache_size) 85 { 86 svn_error_t *err; 87 88 /* auto-allocate cache */ 89 apr_allocator_t *allocator = NULL; 90 apr_pool_t *pool = NULL; 91 92 if (apr_allocator_create(&allocator)) 93 return SVN_NO_ERROR; 94 95 /* Ensure that we free partially allocated data if we run OOM 96 * before the cache is complete: If the cache cannot be allocated 97 * in its full size, the create() function will clear the pool 98 * explicitly. The allocator will make sure that any memory no 99 * longer used by the pool will actually be returned to the OS. 100 * 101 * Please note that this pool and allocator is used *only* to 102 * allocate the large membuffer. All later dynamic allocations 103 * come from other, temporary pools and allocators. 104 */ 105 apr_allocator_max_free_set(allocator, 1); 106 107 /* don't terminate upon OOM but make pool return a NULL pointer 108 * instead so we can disable caching gracefully and continue 109 * operation without membuffer caches. 110 */ 111 apr_pool_create_ex(&pool, NULL, NULL, allocator); 112 if (pool == NULL) 113 return SVN_NO_ERROR; 114 apr_allocator_owner_set(allocator, pool); 115 116 err = svn_cache__membuffer_cache_create( 117 &cache, 118 (apr_size_t)cache_size, 119 (apr_size_t)(cache_size / 10), 120 0, 121 ! svn_cache_config_get()->single_threaded, 122 FALSE, 123 pool); 124 125 /* Some error occurred. Most likely it's an OOM error but we don't 126 * really care. Simply release all cache memory and disable caching 127 */ 128 if (err) 129 { 130 /* Memory cleanup */ 131 svn_pool_destroy(pool); 132 133 /* Document that we actually don't have a cache. */ 134 cache_settings.cache_size = 0; 135 136 return svn_error_trace(err); 137 } 138 139 /* done */ 140 *cache_p = cache; 141 } 142 143 return SVN_NO_ERROR; 144} 145 146/* Access the process-global (singleton) membuffer cache. The first call 147 * will automatically allocate the cache using the current cache config. 148 * NULL will be returned if the desired cache size is 0 or if the cache 149 * could not be created for some reason. 150 */ 151svn_membuffer_t * 152svn_cache__get_global_membuffer_cache(void) 153{ 154 static svn_membuffer_t *cache = NULL; 155 static svn_atomic_t initialized = 0; 156 157 svn_error_t *err 158 = svn_atomic__init_once(&initialized, initialize_cache, &cache, NULL); 159 if (err) 160 { 161 /* no caches today ... */ 162 svn_error_clear(err); 163 return NULL; 164 } 165 166 return cache; 167} 168 169void 170svn_cache_config_set(const svn_cache_config_t *settings) 171{ 172 cache_settings = *settings; 173} 174 175