2 * Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
31 #include "got_object.h"
32 #include "got_error.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_inflate.h"
36 #include "got_lib_object.h"
37 #include "got_lib_delta_cache.h"
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #define GOT_DELTA_CACHE_MIN_BUCKETS 64
44 #define GOT_DELTA_CACHE_MAX_BUCKETS 2048
45 #define GOT_DELTA_CACHE_MAX_CHAIN 2
46 #define GOT_DELTA_CACHE_MAX_DELTA_SIZE 1024
48 struct got_cached_delta {
54 struct got_delta_cache_head {
55 struct got_cached_delta entries[GOT_DELTA_CACHE_MAX_CHAIN];
59 struct got_delta_cache {
60 struct got_delta_cache_head *buckets;
61 unsigned int nbuckets;
68 int cache_maxtoolarge;
70 #define GOT_DELTA_CACHE_F_NOMEM 0x01
74 const struct got_error *
75 got_delta_cache_alloc(struct got_delta_cache **new)
77 const struct got_error *err;
78 struct got_delta_cache *cache;
82 cache = calloc(1, sizeof(*cache));
84 return got_error_from_errno("calloc");
86 cache->buckets = calloc(GOT_DELTA_CACHE_MIN_BUCKETS,
87 sizeof(cache->buckets[0]));
88 if (cache->buckets == NULL) {
89 err = got_error_from_errno("calloc");
93 cache->nbuckets = GOT_DELTA_CACHE_MIN_BUCKETS;
95 arc4random_buf(&cache->key, sizeof(cache->key));
101 got_delta_cache_free(struct got_delta_cache *cache)
103 struct got_cached_delta *delta;
106 #ifdef GOT_DELTA_CACHE_DEBUG
107 fprintf(stderr, "%s: delta cache: %u elements, %d searches, %d hits, "
108 "%d missed, %d evicted, %d too large (max %d)\n", getprogname(),
109 cache->totelem, cache->cache_search, cache->cache_hit,
110 cache->cache_miss, cache->cache_evict, cache->cache_toolarge,
111 cache->cache_maxtoolarge);
113 for (i = 0; i < cache->nbuckets; i++) {
114 struct got_delta_cache_head *head;
116 head = &cache->buckets[i];
117 for (j = 0; j < head->nchain; j++) {
118 delta = &head->entries[j];
122 free(cache->buckets);
127 delta_cache_hash(struct got_delta_cache *cache, off_t delta_offset)
129 return SipHash24(&cache->key, &delta_offset, sizeof(delta_offset));
132 #ifndef GOT_NO_DELTA_CACHE
133 static const struct got_error *
134 delta_cache_resize(struct got_delta_cache *cache, unsigned int nbuckets)
136 struct got_delta_cache_head *buckets;
139 buckets = calloc(nbuckets, sizeof(buckets[0]));
140 if (buckets == NULL) {
142 return got_error_from_errno("calloc");
143 /* Proceed with our current amount of hash buckets. */
144 cache->flags |= GOT_DELTA_CACHE_F_NOMEM;
148 arc4random_buf(&cache->key, sizeof(cache->key));
150 for (i = 0; i < cache->nbuckets; i++) {
152 for (j = 0; j < cache->buckets[i].nchain; j++) {
153 struct got_delta_cache_head *head;
154 struct got_cached_delta *delta;
156 delta = &cache->buckets[i].entries[j];
157 idx = delta_cache_hash(cache, delta->offset) % nbuckets;
158 head = &buckets[idx];
159 if (head->nchain < nitems(head->entries)) {
160 struct got_cached_delta *new_delta;
161 new_delta = &head->entries[head->nchain];
162 memcpy(new_delta, delta, sizeof(*new_delta));
171 free(cache->buckets);
172 cache->buckets = buckets;
173 cache->nbuckets = nbuckets;
177 static const struct got_error *
178 delta_cache_grow(struct got_delta_cache *cache)
180 unsigned int nbuckets;
182 if ((cache->flags & GOT_DELTA_CACHE_F_NOMEM) ||
183 cache->nbuckets == GOT_DELTA_CACHE_MAX_BUCKETS)
186 if (cache->nbuckets >= GOT_DELTA_CACHE_MAX_BUCKETS / 2)
187 nbuckets = GOT_DELTA_CACHE_MAX_BUCKETS;
189 nbuckets = cache->nbuckets * 2;
191 return delta_cache_resize(cache, nbuckets);
195 const struct got_error *
196 got_delta_cache_add(struct got_delta_cache *cache,
197 off_t delta_data_offset, uint8_t *delta_data, size_t delta_len)
199 #ifdef GOT_NO_DELTA_CACHE
200 return got_error(GOT_ERR_NO_SPACE);
202 const struct got_error *err = NULL;
203 struct got_cached_delta *delta;
204 struct got_delta_cache_head *head;
207 if (delta_len > GOT_DELTA_CACHE_MAX_DELTA_SIZE) {
208 cache->cache_toolarge++;
209 if (delta_len > cache->cache_maxtoolarge)
210 cache->cache_maxtoolarge = delta_len;
211 return got_error(GOT_ERR_NO_SPACE);
214 if (cache->nbuckets * 3 < cache->totelem * 4) {
215 err = delta_cache_grow(cache);
220 idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
221 head = &cache->buckets[idx];
222 if (head->nchain >= nitems(head->entries)) {
223 delta = &head->entries[head->nchain - 1];
225 memset(delta, 0, sizeof(*delta));
228 cache->cache_evict++;
231 delta = &head->entries[head->nchain];
232 delta->offset = delta_data_offset;
233 delta->data = delta_data;
234 delta->len = delta_len;
243 got_delta_cache_get(uint8_t **delta_data, size_t *delta_len,
244 struct got_delta_cache *cache, off_t delta_data_offset)
247 struct got_delta_cache_head *head;
248 struct got_cached_delta *delta;
251 idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
252 head = &cache->buckets[idx];
254 cache->cache_search++;
257 for (i = 0; i < head->nchain; i++) {
258 delta = &head->entries[i];
259 if (delta->offset != delta_data_offset)
263 struct got_cached_delta tmp;
264 memcpy(&tmp, &head->entries[0], sizeof(tmp));
265 memcpy(&head->entries[0], &head->entries[i],
266 sizeof(head->entries[0]));
267 memcpy(&head->entries[i], &tmp,
268 sizeof(head->entries[i]));
269 delta = &head->entries[0];
271 *delta_data = delta->data;
272 *delta_len = delta->len;