2 5aa81393 2020-01-06 stsp * Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 ab2f42e7 2019-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 ab2f42e7 2019-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 ab2f42e7 2019-11-10 stsp * copyright notice and this permission notice appear in all copies.
8 ab2f42e7 2019-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ab2f42e7 2019-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ab2f42e7 2019-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ab2f42e7 2019-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ab2f42e7 2019-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ab2f42e7 2019-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ab2f42e7 2019-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ab2f42e7 2019-11-10 stsp #include <stdlib.h>
19 ab2f42e7 2019-11-10 stsp #include <string.h>
20 ab2f42e7 2019-11-10 stsp #include <stdio.h>
21 ab2f42e7 2019-11-10 stsp #include <zlib.h>
22 ab2f42e7 2019-11-10 stsp #include <limits.h>
23 ab2f42e7 2019-11-10 stsp #include <time.h>
24 a5061f77 2022-06-13 thomas #include <errno.h>
25 a5061f77 2022-06-13 thomas #include <siphash.h>
27 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
29 ab2f42e7 2019-11-10 stsp #include "got_object.h"
30 ab2f42e7 2019-11-10 stsp #include "got_error.h"
32 ab2f42e7 2019-11-10 stsp #include "got_lib_delta.h"
33 ab2f42e7 2019-11-10 stsp #include "got_lib_inflate.h"
34 ab2f42e7 2019-11-10 stsp #include "got_lib_object.h"
35 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
37 ab2f42e7 2019-11-10 stsp #ifndef nitems
38 ab2f42e7 2019-11-10 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 a5061f77 2022-06-13 thomas #define GOT_DELTA_CACHE_MIN_BUCKETS 64
42 a5061f77 2022-06-13 thomas #define GOT_DELTA_CACHE_MAX_BUCKETS 16384
43 a5061f77 2022-06-13 thomas #define GOT_DELTA_CACHE_MAX_CHAIN 2
44 a5061f77 2022-06-13 thomas #define GOT_DELTA_CACHE_MAX_DELTA_SIZE 2048
46 a5061f77 2022-06-13 thomas struct got_cached_delta {
47 a5061f77 2022-06-13 thomas off_t offset;
48 a5061f77 2022-06-13 thomas uint8_t *data;
49 a5061f77 2022-06-13 thomas size_t len;
52 a5061f77 2022-06-13 thomas struct got_delta_cache_head {
53 a5061f77 2022-06-13 thomas struct got_cached_delta entries[GOT_DELTA_CACHE_MAX_CHAIN];
54 a5061f77 2022-06-13 thomas unsigned int nchain;
57 ab2f42e7 2019-11-10 stsp struct got_delta_cache {
58 a5061f77 2022-06-13 thomas struct got_delta_cache_head *buckets;
59 a5061f77 2022-06-13 thomas unsigned int nbuckets;
60 a5061f77 2022-06-13 thomas unsigned int totelem;
61 c3b318d0 2019-11-10 stsp int cache_search;
62 c3b318d0 2019-11-10 stsp int cache_hit;
63 c3b318d0 2019-11-10 stsp int cache_miss;
64 c3b318d0 2019-11-10 stsp int cache_evict;
65 c3b318d0 2019-11-10 stsp int cache_toolarge;
66 a5061f77 2022-06-13 thomas unsigned int flags;
67 a5061f77 2022-06-13 thomas #define GOT_DELTA_CACHE_F_NOMEM 0x01
68 a5061f77 2022-06-13 thomas SIPHASH_KEY key;
71 a5061f77 2022-06-13 thomas const struct got_error *
72 a5061f77 2022-06-13 thomas got_delta_cache_alloc(struct got_delta_cache **new)
74 a5061f77 2022-06-13 thomas const struct got_error *err;
75 ab2f42e7 2019-11-10 stsp struct got_delta_cache *cache;
77 a5061f77 2022-06-13 thomas *new = NULL;
79 ab2f42e7 2019-11-10 stsp cache = calloc(1, sizeof(*cache));
80 ab2f42e7 2019-11-10 stsp if (cache == NULL)
81 a5061f77 2022-06-13 thomas return got_error_from_errno("calloc");
83 a5061f77 2022-06-13 thomas cache->buckets = calloc(GOT_DELTA_CACHE_MIN_BUCKETS,
84 a5061f77 2022-06-13 thomas sizeof(cache->buckets[0]));
85 a5061f77 2022-06-13 thomas if (cache->buckets == NULL) {
86 a5061f77 2022-06-13 thomas err = got_error_from_errno("calloc");
87 a5061f77 2022-06-13 thomas free(cache);
88 a5061f77 2022-06-13 thomas return err;
90 a5061f77 2022-06-13 thomas cache->nbuckets = GOT_DELTA_CACHE_MIN_BUCKETS;
92 a5061f77 2022-06-13 thomas arc4random_buf(&cache->key, sizeof(cache->key));
93 a5061f77 2022-06-13 thomas *new = cache;
94 a5061f77 2022-06-13 thomas return NULL;
98 ab2f42e7 2019-11-10 stsp got_delta_cache_free(struct got_delta_cache *cache)
100 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
101 a5061f77 2022-06-13 thomas unsigned int i;
103 c3b318d0 2019-11-10 stsp #ifdef GOT_OBJ_CACHE_DEBUG
104 a5061f77 2022-06-13 thomas fprintf(stderr, "%s: delta cache: %u elements, %d searches, %d hits, "
105 a5061f77 2022-06-13 thomas "%d missed, %d evicted, %d too large\n", getprogname(),
106 a5061f77 2022-06-13 thomas cache->totelem, cache->cache_search, cache->cache_hit,
107 a5061f77 2022-06-13 thomas cache->cache_miss, cache->cache_evict, cache->cache_toolarge);
109 a5061f77 2022-06-13 thomas for (i = 0; i < cache->nbuckets; i++) {
110 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
112 a5061f77 2022-06-13 thomas head = &cache->buckets[i];
113 a5061f77 2022-06-13 thomas for (j = 0; j < head->nchain; j++) {
114 a5061f77 2022-06-13 thomas delta = &head->entries[j];
115 a5061f77 2022-06-13 thomas free(delta->data);
118 a5061f77 2022-06-13 thomas free(cache->buckets);
119 ab2f42e7 2019-11-10 stsp free(cache);
122 a5061f77 2022-06-13 thomas static uint64_t
123 a5061f77 2022-06-13 thomas delta_cache_hash(struct got_delta_cache *cache, off_t delta_offset)
125 a5061f77 2022-06-13 thomas return SipHash24(&cache->key, &delta_offset, sizeof(delta_offset));
128 a5061f77 2022-06-13 thomas static const struct got_error *
129 a5061f77 2022-06-13 thomas delta_cache_resize(struct got_delta_cache *cache, unsigned int nbuckets)
131 a5061f77 2022-06-13 thomas struct got_delta_cache_head *buckets;
132 a5061f77 2022-06-13 thomas size_t i;
134 a5061f77 2022-06-13 thomas buckets = calloc(nbuckets, sizeof(buckets[0]));
135 a5061f77 2022-06-13 thomas if (buckets == NULL) {
136 a5061f77 2022-06-13 thomas if (errno != ENOMEM)
137 a5061f77 2022-06-13 thomas return got_error_from_errno("calloc");
138 a5061f77 2022-06-13 thomas /* Proceed with our current amount of hash buckets. */
139 a5061f77 2022-06-13 thomas cache->flags |= GOT_DELTA_CACHE_F_NOMEM;
140 a5061f77 2022-06-13 thomas return NULL;
143 a5061f77 2022-06-13 thomas arc4random_buf(&cache->key, sizeof(cache->key));
145 a5061f77 2022-06-13 thomas for (i = 0; i < cache->nbuckets; i++) {
146 a5061f77 2022-06-13 thomas unsigned int j;
147 a5061f77 2022-06-13 thomas for (j = 0; j < cache->buckets[i].nchain; j++) {
148 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
149 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
150 a5061f77 2022-06-13 thomas uint64_t idx;
151 a5061f77 2022-06-13 thomas delta = &cache->buckets[i].entries[j];
152 a5061f77 2022-06-13 thomas idx = delta_cache_hash(cache, delta->offset) % nbuckets;
153 a5061f77 2022-06-13 thomas head = &buckets[idx];
154 a5061f77 2022-06-13 thomas if (head->nchain < nitems(head->entries)) {
155 a5061f77 2022-06-13 thomas struct got_cached_delta *new_delta;
156 a5061f77 2022-06-13 thomas new_delta = &head->entries[head->nchain];
157 a5061f77 2022-06-13 thomas memcpy(new_delta, delta, sizeof(*new_delta));
158 a5061f77 2022-06-13 thomas head->nchain++;
160 a5061f77 2022-06-13 thomas free(delta->data);
164 a5061f77 2022-06-13 thomas free(cache->buckets);
165 a5061f77 2022-06-13 thomas cache->buckets = buckets;
166 a5061f77 2022-06-13 thomas cache->nbuckets = nbuckets;
167 a5061f77 2022-06-13 thomas return NULL;
170 a5061f77 2022-06-13 thomas static const struct got_error *
171 a5061f77 2022-06-13 thomas delta_cache_grow(struct got_delta_cache *cache)
173 a5061f77 2022-06-13 thomas unsigned int nbuckets;
175 a5061f77 2022-06-13 thomas if ((cache->flags & GOT_DELTA_CACHE_F_NOMEM) ||
176 a5061f77 2022-06-13 thomas cache->nbuckets == GOT_DELTA_CACHE_MAX_BUCKETS)
177 a5061f77 2022-06-13 thomas return NULL;
179 a5061f77 2022-06-13 thomas if (cache->nbuckets >= GOT_DELTA_CACHE_MAX_BUCKETS / 2)
180 a5061f77 2022-06-13 thomas nbuckets = GOT_DELTA_CACHE_MAX_BUCKETS;
182 a5061f77 2022-06-13 thomas nbuckets = cache->nbuckets * 2;
184 a5061f77 2022-06-13 thomas return delta_cache_resize(cache, nbuckets);
187 ab2f42e7 2019-11-10 stsp const struct got_error *
188 ab2f42e7 2019-11-10 stsp got_delta_cache_add(struct got_delta_cache *cache,
189 ab2f42e7 2019-11-10 stsp off_t delta_data_offset, uint8_t *delta_data, size_t delta_len)
191 fa7a529e 2020-01-06 stsp #ifdef GOT_NO_OBJ_CACHE
192 fa7a529e 2020-01-06 stsp return got_error(GOT_ERR_NO_SPACE);
194 a5061f77 2022-06-13 thomas const struct got_error *err = NULL;
195 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
196 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
197 a5061f77 2022-06-13 thomas uint64_t idx;
199 88b1b490 2022-06-13 thomas if (delta_len > GOT_DELTA_CACHE_MAX_DELTA_SIZE) {
200 c3b318d0 2019-11-10 stsp cache->cache_toolarge++;
201 ab2f42e7 2019-11-10 stsp return got_error(GOT_ERR_NO_SPACE);
204 a5061f77 2022-06-13 thomas if (cache->nbuckets * 3 < cache->totelem * 4) {
205 a5061f77 2022-06-13 thomas err = delta_cache_grow(cache);
207 a5061f77 2022-06-13 thomas return err;
210 a5061f77 2022-06-13 thomas idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
211 a5061f77 2022-06-13 thomas head = &cache->buckets[idx];
212 a5061f77 2022-06-13 thomas if (head->nchain >= nitems(head->entries)) {
213 a5061f77 2022-06-13 thomas delta = &head->entries[head->nchain - 1];
214 a5061f77 2022-06-13 thomas free(delta->data);
215 a5061f77 2022-06-13 thomas memset(delta, 0, sizeof(*delta));
216 a5061f77 2022-06-13 thomas head->nchain--;
219 a5061f77 2022-06-13 thomas delta = &head->entries[head->nchain];
220 a5061f77 2022-06-13 thomas delta->offset = delta_data_offset;
221 a5061f77 2022-06-13 thomas delta->data = delta_data;
222 a5061f77 2022-06-13 thomas delta->len = delta_len;
223 a5061f77 2022-06-13 thomas head->nchain++;
224 a5061f77 2022-06-13 thomas cache->totelem++;
226 ab2f42e7 2019-11-10 stsp return NULL;
231 ab2f42e7 2019-11-10 stsp got_delta_cache_get(uint8_t **delta_data, size_t *delta_len,
232 ab2f42e7 2019-11-10 stsp struct got_delta_cache *cache, off_t delta_data_offset)
234 a5061f77 2022-06-13 thomas uint64_t idx;
235 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
236 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
239 a5061f77 2022-06-13 thomas idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
240 a5061f77 2022-06-13 thomas head = &cache->buckets[idx];
242 c3b318d0 2019-11-10 stsp cache->cache_search++;
243 ab2f42e7 2019-11-10 stsp *delta_data = NULL;
244 ab2f42e7 2019-11-10 stsp *delta_len = 0;
245 a5061f77 2022-06-13 thomas for (i = 0; i < head->nchain; i++) {
246 a5061f77 2022-06-13 thomas delta = &head->entries[i];
247 a5061f77 2022-06-13 thomas if (delta->offset != delta_data_offset)
249 c3b318d0 2019-11-10 stsp cache->cache_hit++;
250 a5061f77 2022-06-13 thomas if (i > 0) {
251 a5061f77 2022-06-13 thomas struct got_cached_delta tmp;
252 a5061f77 2022-06-13 thomas memcpy(&tmp, &head->entries[0], sizeof(tmp));
253 a5061f77 2022-06-13 thomas memcpy(&head->entries[0], &head->entries[i],
254 a5061f77 2022-06-13 thomas sizeof(head->entries[0]));
255 a5061f77 2022-06-13 thomas memcpy(&head->entries[i], &tmp,
256 a5061f77 2022-06-13 thomas sizeof(head->entries[i]));
257 a5061f77 2022-06-13 thomas delta = &head->entries[0];
259 a5061f77 2022-06-13 thomas *delta_data = delta->data;
260 a5061f77 2022-06-13 thomas *delta_len = delta->len;
264 c3b318d0 2019-11-10 stsp cache->cache_miss++;