2 * cache.c: cache interface for Subversion
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
27 svn_cache__set_error_handler(svn_cache__t *cache,
28 svn_cache__error_handler_t handler,
30 apr_pool_t *scratch_pool)
32 cache->error_handler = handler;
33 cache->error_baton = baton;
38 svn_cache__is_cachable(svn_cache__t *cache,
41 /* having no cache means we can't cache anything */
45 return cache->vtable->is_cachable(cache->cache_internal, size);
48 /* Give the error handler callback a chance to replace or ignore the
51 handle_error(svn_cache__t *cache,
58 if (cache->error_handler)
59 err = (cache->error_handler)(err, cache->error_baton, pool);
67 svn_cache__get(void **value_p,
71 apr_pool_t *result_pool)
75 /* In case any errors happen and are quelched, make sure we start
76 out with FOUND set to false. */
79 if (getenv("SVN_X_DOES_NOT_MARK_THE_SPOT"))
84 err = handle_error(cache,
85 (cache->vtable->get)(value_p,
87 cache->cache_internal,
99 svn_cache__set(svn_cache__t *cache,
102 apr_pool_t *scratch_pool)
105 return handle_error(cache,
106 (cache->vtable->set)(cache->cache_internal,
115 svn_cache__iter(svn_boolean_t *completed,
117 svn_iter_apr_hash_cb_t user_cb,
119 apr_pool_t *scratch_pool)
122 if (getenv("SVN_X_DOES_NOT_MARK_THE_SPOT"))
123 /* Pretend CACHE is empty. */
127 return (cache->vtable->iter)(completed,
128 cache->cache_internal,
135 svn_cache__get_partial(void **value,
136 svn_boolean_t *found,
139 svn_cache__partial_getter_func_t func,
141 apr_pool_t *result_pool)
145 /* In case any errors happen and are quelched, make sure we start
146 out with FOUND set to false. */
149 if (getenv("SVN_X_DOES_NOT_MARK_THE_SPOT"))
154 err = handle_error(cache,
155 (cache->vtable->get_partial)(value,
157 cache->cache_internal,
171 svn_cache__set_partial(svn_cache__t *cache,
173 svn_cache__partial_setter_func_t func,
175 apr_pool_t *scratch_pool)
178 return handle_error(cache,
179 (cache->vtable->set_partial)(cache->cache_internal,
188 svn_cache__get_info(svn_cache__t *cache,
189 svn_cache__info_t *info,
191 apr_pool_t *result_pool)
193 /* write general statistics */
195 info->gets = cache->reads;
196 info->hits = cache->hits;
197 info->sets = cache->writes;
198 info->failures = cache->failures;
200 /* Call the cache implementation for filling the blanks.
201 * It might also replace some of the general stats but
202 * this is currently not done.
204 SVN_ERR((cache->vtable->get_info)(cache->cache_internal,
209 /* reset statistics */
223 svn_cache__format_info(const svn_cache__info_t *info,
224 apr_pool_t *result_pool)
226 enum { _1MB = 1024 * 1024 };
228 apr_uint64_t misses = info->gets - info->hits;
229 double hit_rate = (100.0 * (double)info->hits)
230 / (double)(info->gets ? info->gets : 1);
231 double write_rate = (100.0 * (double)info->sets)
232 / (double)(misses ? misses : 1);
233 double data_usage_rate = (100.0 * (double)info->used_size)
234 / (double)(info->data_size ? info->data_size : 1);
235 double data_entry_rate = (100.0 * (double)info->used_entries)
236 / (double)(info->total_entries ? info->total_entries : 1);
238 return svn_string_createf(result_pool,
241 "gets : %" APR_UINT64_T_FMT
242 ", %" APR_UINT64_T_FMT " hits (%5.2f%%)\n"
243 "sets : %" APR_UINT64_T_FMT
244 " (%5.2f%% of misses)\n"
245 "failures: %" APR_UINT64_T_FMT "\n"
246 "used : %" APR_UINT64_T_FMT " MB (%5.2f%%)"
247 " of %" APR_UINT64_T_FMT " MB data cache"
248 " / %" APR_UINT64_T_FMT " MB total cache memory\n"
249 " %" APR_UINT64_T_FMT " entries (%5.2f%%)"
250 " of %" APR_UINT64_T_FMT " total\n",
255 info->hits, hit_rate,
256 info->sets, write_rate,
259 info->used_size / _1MB, data_usage_rate,
260 info->data_size / _1MB,
261 info->total_size / _1MB,
263 info->used_entries, data_entry_rate,
264 info->total_entries);