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;
69 #define GOT_DELTA_CACHE_F_NOMEM 0x01
73 const struct got_error *
74 got_delta_cache_alloc(struct got_delta_cache **new)
76 const struct got_error *err;
77 struct got_delta_cache *cache;
81 cache = calloc(1, sizeof(*cache));
83 return got_error_from_errno("calloc");
85 cache->buckets = calloc(GOT_DELTA_CACHE_MIN_BUCKETS,
86 sizeof(cache->buckets[0]));
87 if (cache->buckets == NULL) {
88 err = got_error_from_errno("calloc");
92 cache->nbuckets = GOT_DELTA_CACHE_MIN_BUCKETS;
94 arc4random_buf(&cache->key, sizeof(cache->key));
100 got_delta_cache_free(struct got_delta_cache *cache)
102 struct got_cached_delta *delta;
105 #ifdef GOT_DELTA_CACHE_DEBUG
106 fprintf(stderr, "%s: delta cache: %u elements, %d searches, %d hits, "
107 "%d missed, %d evicted, %d too large\n", getprogname(),
108 cache->totelem, cache->cache_search, cache->cache_hit,
109 cache->cache_miss, cache->cache_evict, cache->cache_toolarge);
111 for (i = 0; i < cache->nbuckets; i++) {
112 struct got_delta_cache_head *head;
114 head = &cache->buckets[i];
115 for (j = 0; j < head->nchain; j++) {
116 delta = &head->entries[j];
120 free(cache->buckets);
125 delta_cache_hash(struct got_delta_cache *cache, off_t delta_offset)
127 return SipHash24(&cache->key, &delta_offset, sizeof(delta_offset));
130 #ifndef GOT_NO_DELTA_CACHE
131 static const struct got_error *
132 delta_cache_resize(struct got_delta_cache *cache, unsigned int nbuckets)
134 struct got_delta_cache_head *buckets;
137 buckets = calloc(nbuckets, sizeof(buckets[0]));
138 if (buckets == NULL) {
140 return got_error_from_errno("calloc");
141 /* Proceed with our current amount of hash buckets. */
142 cache->flags |= GOT_DELTA_CACHE_F_NOMEM;
146 arc4random_buf(&cache->key, sizeof(cache->key));
148 for (i = 0; i < cache->nbuckets; i++) {
150 for (j = 0; j < cache->buckets[i].nchain; j++) {
151 struct got_delta_cache_head *head;
152 struct got_cached_delta *delta;
154 delta = &cache->buckets[i].entries[j];
155 idx = delta_cache_hash(cache, delta->offset) % nbuckets;
156 head = &buckets[idx];
157 if (head->nchain < nitems(head->entries)) {
158 struct got_cached_delta *new_delta;
159 new_delta = &head->entries[head->nchain];
160 memcpy(new_delta, delta, sizeof(*new_delta));
169 free(cache->buckets);
170 cache->buckets = buckets;
171 cache->nbuckets = nbuckets;
175 static const struct got_error *
176 delta_cache_grow(struct got_delta_cache *cache)
178 unsigned int nbuckets;
180 if ((cache->flags & GOT_DELTA_CACHE_F_NOMEM) ||
181 cache->nbuckets == GOT_DELTA_CACHE_MAX_BUCKETS)
184 if (cache->nbuckets >= GOT_DELTA_CACHE_MAX_BUCKETS / 2)
185 nbuckets = GOT_DELTA_CACHE_MAX_BUCKETS;
187 nbuckets = cache->nbuckets * 2;
189 return delta_cache_resize(cache, nbuckets);
193 const struct got_error *
194 got_delta_cache_add(struct got_delta_cache *cache,
195 off_t delta_data_offset, uint8_t *delta_data, size_t delta_len)
197 #ifdef GOT_NO_DELTA_CACHE
198 return got_error(GOT_ERR_NO_SPACE);
200 const struct got_error *err = NULL;
201 struct got_cached_delta *delta;
202 struct got_delta_cache_head *head;
205 if (delta_len > GOT_DELTA_CACHE_MAX_DELTA_SIZE) {
206 cache->cache_toolarge++;
207 return got_error(GOT_ERR_NO_SPACE);
210 if (cache->nbuckets * 3 < cache->totelem * 4) {
211 err = delta_cache_grow(cache);
216 idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
217 head = &cache->buckets[idx];
218 if (head->nchain >= nitems(head->entries)) {
219 delta = &head->entries[head->nchain - 1];
221 memset(delta, 0, sizeof(*delta));
224 cache->cache_evict++;
227 delta = &head->entries[head->nchain];
228 delta->offset = delta_data_offset;
229 delta->data = delta_data;
230 delta->len = delta_len;
239 got_delta_cache_get(uint8_t **delta_data, size_t *delta_len,
240 struct got_delta_cache *cache, off_t delta_data_offset)
243 struct got_delta_cache_head *head;
244 struct got_cached_delta *delta;
247 idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
248 head = &cache->buckets[idx];
250 cache->cache_search++;
253 for (i = 0; i < head->nchain; i++) {
254 delta = &head->entries[i];
255 if (delta->offset != delta_data_offset)
259 struct got_cached_delta tmp;
260 memcpy(&tmp, &head->entries[0], sizeof(tmp));
261 memcpy(&head->entries[0], &head->entries[i],
262 sizeof(head->entries[0]));
263 memcpy(&head->entries[i], &tmp,
264 sizeof(head->entries[i]));
265 delta = &head->entries[0];
267 *delta_data = delta->data;
268 *delta_len = delta->len;