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 (cache->pretend_empty)
84 err = handle_error(cache,
85 (cache->vtable->get)(value_p,
87 cache->cache_internal,
99 svn_cache__has_key(svn_boolean_t *found,
102 apr_pool_t *scratch_pool)
106 if (cache->pretend_empty)
110 return handle_error(cache,
111 (cache->vtable->has_key)(found,
112 cache->cache_internal,
119 svn_cache__set(svn_cache__t *cache,
122 apr_pool_t *scratch_pool)
125 return handle_error(cache,
126 (cache->vtable->set)(cache->cache_internal,
135 svn_cache__iter(svn_boolean_t *completed,
137 svn_iter_apr_hash_cb_t user_cb,
139 apr_pool_t *scratch_pool)
142 if (cache->pretend_empty)
143 /* Pretend CACHE is empty. */
147 return (cache->vtable->iter)(completed,
148 cache->cache_internal,
155 svn_cache__get_partial(void **value,
156 svn_boolean_t *found,
159 svn_cache__partial_getter_func_t func,
161 apr_pool_t *result_pool)
165 /* In case any errors happen and are quelched, make sure we start
166 out with FOUND set to false. */
169 if (cache->pretend_empty)
174 err = handle_error(cache,
175 (cache->vtable->get_partial)(value,
177 cache->cache_internal,
191 svn_cache__set_partial(svn_cache__t *cache,
193 svn_cache__partial_setter_func_t func,
195 apr_pool_t *scratch_pool)
198 return handle_error(cache,
199 (cache->vtable->set_partial)(cache->cache_internal,
208 svn_cache__get_info(svn_cache__t *cache,
209 svn_cache__info_t *info,
211 apr_pool_t *result_pool)
213 /* write general statistics */
215 memset(info, 0, sizeof(*info));
216 info->gets = cache->reads;
217 info->hits = cache->hits;
218 info->sets = cache->writes;
219 info->failures = cache->failures;
221 /* Call the cache implementation for filling the blanks.
222 * It might also replace some of the general stats but
223 * this is currently not done.
225 SVN_ERR((cache->vtable->get_info)(cache->cache_internal,
230 /* reset statistics */
244 svn_cache__format_info(const svn_cache__info_t *info,
245 svn_boolean_t access_only,
246 apr_pool_t *result_pool)
248 enum { _1MB = 1024 * 1024 };
250 apr_uint64_t misses = info->gets - info->hits;
251 double hit_rate = (100.0 * (double)info->hits)
252 / (double)(info->gets ? info->gets : 1);
253 double write_rate = (100.0 * (double)info->sets)
254 / (double)(misses ? misses : 1);
255 double data_usage_rate = (100.0 * (double)info->used_size)
256 / (double)(info->data_size ? info->data_size : 1);
257 double data_entry_rate = (100.0 * (double)info->used_entries)
258 / (double)(info->total_entries ? info->total_entries : 1);
260 const char *histogram = "";
263 svn_stringbuf_t *text = svn_stringbuf_create_empty(result_pool);
266 int count = sizeof(info->histogram) / sizeof(info->histogram[0]);
267 for (i = count - 1; i >= 0; --i)
268 if (info->histogram[i] > 0 || text->len > 0)
269 text = svn_stringbuf_createf(result_pool,
271 ? "%s%12" APR_UINT64_T_FMT
272 " buckets with >%d entries\n"
273 : "%s%12" APR_UINT64_T_FMT
274 " buckets with %d entries\n",
275 text->data, info->histogram[i], i);
277 histogram = text->data;
281 ? svn_string_createf(result_pool,
283 "gets : %" APR_UINT64_T_FMT
284 ", %" APR_UINT64_T_FMT " hits (%5.2f%%)\n"
285 "sets : %" APR_UINT64_T_FMT
286 " (%5.2f%% of misses)\n",
289 info->hits, hit_rate,
290 info->sets, write_rate)
291 : svn_string_createf(result_pool,
294 "gets : %" APR_UINT64_T_FMT
295 ", %" APR_UINT64_T_FMT " hits (%5.2f%%)\n"
296 "sets : %" APR_UINT64_T_FMT
297 " (%5.2f%% of misses)\n"
298 "failures: %" APR_UINT64_T_FMT "\n"
299 "used : %" APR_UINT64_T_FMT " MB (%5.2f%%)"
300 " of %" APR_UINT64_T_FMT " MB data cache"
301 " / %" APR_UINT64_T_FMT " MB total cache memory\n"
302 " %" APR_UINT64_T_FMT " entries (%5.2f%%)"
303 " of %" APR_UINT64_T_FMT " total\n%s",
308 info->hits, hit_rate,
309 info->sets, write_rate,
312 info->used_size / _1MB, data_usage_rate,
313 info->data_size / _1MB,
314 info->total_size / _1MB,
316 info->used_entries, data_entry_rate,