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.
27 #include "got_compat.h"
29 #include "got_object.h"
30 #include "got_error.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_inflate.h"
34 #include "got_lib_object.h"
35 #include "got_lib_delta_cache.h"
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #define GOT_DELTA_CACHE_MIN_BUCKETS 64
42 #define GOT_DELTA_CACHE_MAX_BUCKETS 16384
43 #define GOT_DELTA_CACHE_MAX_CHAIN 2
44 #define GOT_DELTA_CACHE_MAX_DELTA_SIZE 2048
46 struct got_cached_delta {
52 struct got_delta_cache_head {
53 struct got_cached_delta entries[GOT_DELTA_CACHE_MAX_CHAIN];
57 struct got_delta_cache {
58 struct got_delta_cache_head *buckets;
59 unsigned int nbuckets;
67 #define GOT_DELTA_CACHE_F_NOMEM 0x01
71 const struct got_error *
72 got_delta_cache_alloc(struct got_delta_cache **new)
74 const struct got_error *err;
75 struct got_delta_cache *cache;
79 cache = calloc(1, sizeof(*cache));
81 return got_error_from_errno("calloc");
83 cache->buckets = calloc(GOT_DELTA_CACHE_MIN_BUCKETS,
84 sizeof(cache->buckets[0]));
85 if (cache->buckets == NULL) {
86 err = got_error_from_errno("calloc");
90 cache->nbuckets = GOT_DELTA_CACHE_MIN_BUCKETS;
92 arc4random_buf(&cache->key, sizeof(cache->key));
98 got_delta_cache_free(struct got_delta_cache *cache)
100 struct got_cached_delta *delta;
103 #ifdef GOT_OBJ_CACHE_DEBUG
104 fprintf(stderr, "%s: delta cache: %u elements, %d searches, %d hits, "
105 "%d missed, %d evicted, %d too large\n", getprogname(),
106 cache->totelem, cache->cache_search, cache->cache_hit,
107 cache->cache_miss, cache->cache_evict, cache->cache_toolarge);
109 for (i = 0; i < cache->nbuckets; i++) {
110 struct got_delta_cache_head *head;
112 head = &cache->buckets[i];
113 for (j = 0; j < head->nchain; j++) {
114 delta = &head->entries[j];
118 free(cache->buckets);
123 delta_cache_hash(struct got_delta_cache *cache, off_t delta_offset)
125 return SipHash24(&cache->key, &delta_offset, sizeof(delta_offset));
128 static const struct got_error *
129 delta_cache_resize(struct got_delta_cache *cache, unsigned int nbuckets)
131 struct got_delta_cache_head *buckets;
134 buckets = calloc(nbuckets, sizeof(buckets[0]));
135 if (buckets == NULL) {
137 return got_error_from_errno("calloc");
138 /* Proceed with our current amount of hash buckets. */
139 cache->flags |= GOT_DELTA_CACHE_F_NOMEM;
143 arc4random_buf(&cache->key, sizeof(cache->key));
145 for (i = 0; i < cache->nbuckets; i++) {
147 for (j = 0; j < cache->buckets[i].nchain; j++) {
148 struct got_delta_cache_head *head;
149 struct got_cached_delta *delta;
151 delta = &cache->buckets[i].entries[j];
152 idx = delta_cache_hash(cache, delta->offset) % nbuckets;
153 head = &buckets[idx];
154 if (head->nchain < nitems(head->entries)) {
155 struct got_cached_delta *new_delta;
156 new_delta = &head->entries[head->nchain];
157 memcpy(new_delta, delta, sizeof(*new_delta));
164 free(cache->buckets);
165 cache->buckets = buckets;
166 cache->nbuckets = nbuckets;
170 static const struct got_error *
171 delta_cache_grow(struct got_delta_cache *cache)
173 unsigned int nbuckets;
175 if ((cache->flags & GOT_DELTA_CACHE_F_NOMEM) ||
176 cache->nbuckets == GOT_DELTA_CACHE_MAX_BUCKETS)
179 if (cache->nbuckets >= GOT_DELTA_CACHE_MAX_BUCKETS / 2)
180 nbuckets = GOT_DELTA_CACHE_MAX_BUCKETS;
182 nbuckets = cache->nbuckets * 2;
184 return delta_cache_resize(cache, nbuckets);
187 const struct got_error *
188 got_delta_cache_add(struct got_delta_cache *cache,
189 off_t delta_data_offset, uint8_t *delta_data, size_t delta_len)
191 #ifdef GOT_NO_OBJ_CACHE
192 return got_error(GOT_ERR_NO_SPACE);
194 const struct got_error *err = NULL;
195 struct got_cached_delta *delta;
196 struct got_delta_cache_head *head;
199 if (delta_len > GOT_DELTA_CACHE_MAX_DELTA_SIZE) {
200 cache->cache_toolarge++;
201 return got_error(GOT_ERR_NO_SPACE);
204 if (cache->nbuckets * 3 < cache->totelem * 4) {
205 err = delta_cache_grow(cache);
210 idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
211 head = &cache->buckets[idx];
212 if (head->nchain >= nitems(head->entries)) {
213 delta = &head->entries[head->nchain - 1];
215 memset(delta, 0, sizeof(*delta));
219 delta = &head->entries[head->nchain];
220 delta->offset = delta_data_offset;
221 delta->data = delta_data;
222 delta->len = delta_len;
231 got_delta_cache_get(uint8_t **delta_data, size_t *delta_len,
232 struct got_delta_cache *cache, off_t delta_data_offset)
235 struct got_delta_cache_head *head;
236 struct got_cached_delta *delta;
239 idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
240 head = &cache->buckets[idx];
242 cache->cache_search++;
245 for (i = 0; i < head->nchain; i++) {
246 delta = &head->entries[i];
247 if (delta->offset != delta_data_offset)
251 struct got_cached_delta tmp;
252 memcpy(&tmp, &head->entries[0], sizeof(tmp));
253 memcpy(&head->entries[0], &head->entries[i],
254 sizeof(head->entries[0]));
255 memcpy(&head->entries[i], &tmp,
256 sizeof(head->entries[i]));
257 delta = &head->entries[0];
259 *delta_data = delta->data;
260 *delta_len = delta->len;