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_atomic.h"
27
#include "private/svn_cache.h"
29
#include "svn_pools.h"
31
/* The cache settings as a process-wide singleton.
33
static svn_cache_config_t cache_settings =
35
/* default configuration:
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.
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().
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.
57
FALSE /* assume multi-threaded operation.
58
* Because this simply activates proper synchronization
59
* between threads, it is a safe default.
62
TRUE /* single-threaded is the only supported mode of operation */
66
/* Get the current FSFS cache configuration. */
67
const svn_cache_config_t *
68
svn_cache_config_get(void)
70
return &cache_settings;
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.
78
initialize_cache(void *baton, apr_pool_t *unused_pool)
80
svn_membuffer_t **cache_p = baton;
81
svn_membuffer_t *cache = NULL;
83
apr_uint64_t cache_size = cache_settings.cache_size;
88
/* auto-allocate cache */
89
apr_allocator_t *allocator = NULL;
90
apr_pool_t *pool = NULL;
92
if (apr_allocator_create(&allocator))
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.
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.
105
apr_allocator_max_free_set(allocator, 1);
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.
111
apr_pool_create_ex(&pool, NULL, NULL, allocator);
114
apr_allocator_owner_set(allocator, pool);
116
err = svn_cache__membuffer_cache_create(
118
(apr_size_t)cache_size,
119
(apr_size_t)(cache_size / 10),
121
! svn_cache_config_get()->single_threaded,
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
131
svn_pool_destroy(pool);
133
/* Document that we actually don't have a cache. */
134
cache_settings.cache_size = 0;
136
return svn_error_trace(err);
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.
152
svn_cache__get_global_membuffer_cache(void)
154
static svn_membuffer_t *cache = NULL;
155
static svn_atomic_t initialized = 0;
158
= svn_atomic__init_once(&initialized, initialize_cache, &cache, NULL);
161
/* no caches today ... */
162
svn_error_clear(err);
170
svn_cache_config_set(const svn_cache_config_t *settings)
172
cache_settings = *settings;