Blame


1 ab2f42e7 2019-11-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 ab2f42e7 2019-11-10 stsp *
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.
7 ab2f42e7 2019-11-10 stsp *
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.
15 ab2f42e7 2019-11-10 stsp */
16 ab2f42e7 2019-11-10 stsp
17 ab2f42e7 2019-11-10 stsp #include <sys/queue.h>
18 ab2f42e7 2019-11-10 stsp
19 ab2f42e7 2019-11-10 stsp #include <stdlib.h>
20 ab2f42e7 2019-11-10 stsp #include <string.h>
21 d89a2828 2022-07-05 stsp #include <stdint.h>
22 ab2f42e7 2019-11-10 stsp #include <sha1.h>
23 ab2f42e7 2019-11-10 stsp #include <stdio.h>
24 ab2f42e7 2019-11-10 stsp #include <zlib.h>
25 ab2f42e7 2019-11-10 stsp #include <limits.h>
26 ab2f42e7 2019-11-10 stsp #include <time.h>
27 dac5c75e 2022-06-04 stsp #include <errno.h>
28 dac5c75e 2022-06-04 stsp #include <siphash.h>
29 ab2f42e7 2019-11-10 stsp
30 ab2f42e7 2019-11-10 stsp #include "got_object.h"
31 ab2f42e7 2019-11-10 stsp #include "got_error.h"
32 ab2f42e7 2019-11-10 stsp
33 ab2f42e7 2019-11-10 stsp #include "got_lib_delta.h"
34 ab2f42e7 2019-11-10 stsp #include "got_lib_inflate.h"
35 ab2f42e7 2019-11-10 stsp #include "got_lib_object.h"
36 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
37 ab2f42e7 2019-11-10 stsp
38 ab2f42e7 2019-11-10 stsp #ifndef nitems
39 ab2f42e7 2019-11-10 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 ab2f42e7 2019-11-10 stsp #endif
41 ab2f42e7 2019-11-10 stsp
42 dac5c75e 2022-06-04 stsp #define GOT_DELTA_CACHE_MIN_BUCKETS 64
43 dac5c75e 2022-06-04 stsp #define GOT_DELTA_CACHE_MAX_BUCKETS 16384
44 dac5c75e 2022-06-04 stsp #define GOT_DELTA_CACHE_MAX_CHAIN 2
45 dac5c75e 2022-06-04 stsp #define GOT_DELTA_CACHE_MAX_DELTA_SIZE 2048
46 dac5c75e 2022-06-04 stsp
47 dac5c75e 2022-06-04 stsp struct got_cached_delta {
48 dac5c75e 2022-06-04 stsp off_t offset;
49 dac5c75e 2022-06-04 stsp uint8_t *data;
50 dac5c75e 2022-06-04 stsp size_t len;
51 ab2f42e7 2019-11-10 stsp };
52 ab2f42e7 2019-11-10 stsp
53 dac5c75e 2022-06-04 stsp struct got_delta_cache_head {
54 dac5c75e 2022-06-04 stsp struct got_cached_delta entries[GOT_DELTA_CACHE_MAX_CHAIN];
55 dac5c75e 2022-06-04 stsp unsigned int nchain;
56 dac5c75e 2022-06-04 stsp };
57 ab2f42e7 2019-11-10 stsp
58 ab2f42e7 2019-11-10 stsp struct got_delta_cache {
59 dac5c75e 2022-06-04 stsp struct got_delta_cache_head *buckets;
60 dac5c75e 2022-06-04 stsp unsigned int nbuckets;
61 dac5c75e 2022-06-04 stsp unsigned int totelem;
62 c3b318d0 2019-11-10 stsp int cache_search;
63 c3b318d0 2019-11-10 stsp int cache_hit;
64 c3b318d0 2019-11-10 stsp int cache_miss;
65 c3b318d0 2019-11-10 stsp int cache_evict;
66 c3b318d0 2019-11-10 stsp int cache_toolarge;
67 dac5c75e 2022-06-04 stsp unsigned int flags;
68 dac5c75e 2022-06-04 stsp #define GOT_DELTA_CACHE_F_NOMEM 0x01
69 dac5c75e 2022-06-04 stsp SIPHASH_KEY key;
70 ab2f42e7 2019-11-10 stsp };
71 ab2f42e7 2019-11-10 stsp
72 dac5c75e 2022-06-04 stsp const struct got_error *
73 dac5c75e 2022-06-04 stsp got_delta_cache_alloc(struct got_delta_cache **new)
74 ab2f42e7 2019-11-10 stsp {
75 dac5c75e 2022-06-04 stsp const struct got_error *err;
76 ab2f42e7 2019-11-10 stsp struct got_delta_cache *cache;
77 ab2f42e7 2019-11-10 stsp
78 dac5c75e 2022-06-04 stsp *new = NULL;
79 dac5c75e 2022-06-04 stsp
80 ab2f42e7 2019-11-10 stsp cache = calloc(1, sizeof(*cache));
81 ab2f42e7 2019-11-10 stsp if (cache == NULL)
82 dac5c75e 2022-06-04 stsp return got_error_from_errno("calloc");
83 20282b02 2022-06-16 tracey
84 dac5c75e 2022-06-04 stsp cache->buckets = calloc(GOT_DELTA_CACHE_MIN_BUCKETS,
85 dac5c75e 2022-06-04 stsp sizeof(cache->buckets[0]));
86 dac5c75e 2022-06-04 stsp if (cache->buckets == NULL) {
87 dac5c75e 2022-06-04 stsp err = got_error_from_errno("calloc");
88 dac5c75e 2022-06-04 stsp free(cache);
89 dac5c75e 2022-06-04 stsp return err;
90 dac5c75e 2022-06-04 stsp }
91 dac5c75e 2022-06-04 stsp cache->nbuckets = GOT_DELTA_CACHE_MIN_BUCKETS;
92 ab2f42e7 2019-11-10 stsp
93 dac5c75e 2022-06-04 stsp arc4random_buf(&cache->key, sizeof(cache->key));
94 dac5c75e 2022-06-04 stsp *new = cache;
95 dac5c75e 2022-06-04 stsp return NULL;
96 ab2f42e7 2019-11-10 stsp }
97 ab2f42e7 2019-11-10 stsp
98 ab2f42e7 2019-11-10 stsp void
99 ab2f42e7 2019-11-10 stsp got_delta_cache_free(struct got_delta_cache *cache)
100 ab2f42e7 2019-11-10 stsp {
101 dac5c75e 2022-06-04 stsp struct got_cached_delta *delta;
102 dac5c75e 2022-06-04 stsp unsigned int i;
103 ab2f42e7 2019-11-10 stsp
104 c3b318d0 2019-11-10 stsp #ifdef GOT_OBJ_CACHE_DEBUG
105 dac5c75e 2022-06-04 stsp fprintf(stderr, "%s: delta cache: %u elements, %d searches, %d hits, "
106 dac5c75e 2022-06-04 stsp "%d missed, %d evicted, %d too large\n", getprogname(),
107 dac5c75e 2022-06-04 stsp cache->totelem, cache->cache_search, cache->cache_hit,
108 dac5c75e 2022-06-04 stsp cache->cache_miss, cache->cache_evict, cache->cache_toolarge);
109 c3b318d0 2019-11-10 stsp #endif
110 dac5c75e 2022-06-04 stsp for (i = 0; i < cache->nbuckets; i++) {
111 dac5c75e 2022-06-04 stsp struct got_delta_cache_head *head;
112 dac5c75e 2022-06-04 stsp int j;
113 dac5c75e 2022-06-04 stsp head = &cache->buckets[i];
114 dac5c75e 2022-06-04 stsp for (j = 0; j < head->nchain; j++) {
115 dac5c75e 2022-06-04 stsp delta = &head->entries[j];
116 dac5c75e 2022-06-04 stsp free(delta->data);
117 dac5c75e 2022-06-04 stsp }
118 ab2f42e7 2019-11-10 stsp }
119 dac5c75e 2022-06-04 stsp free(cache->buckets);
120 ab2f42e7 2019-11-10 stsp free(cache);
121 ab2f42e7 2019-11-10 stsp }
122 ab2f42e7 2019-11-10 stsp
123 dac5c75e 2022-06-04 stsp static uint64_t
124 dac5c75e 2022-06-04 stsp delta_cache_hash(struct got_delta_cache *cache, off_t delta_offset)
125 ab2f42e7 2019-11-10 stsp {
126 dac5c75e 2022-06-04 stsp return SipHash24(&cache->key, &delta_offset, sizeof(delta_offset));
127 dac5c75e 2022-06-04 stsp }
128 ab2f42e7 2019-11-10 stsp
129 20282b02 2022-06-16 tracey #ifndef GOT_NO_OBJ_CACHE
130 dac5c75e 2022-06-04 stsp static const struct got_error *
131 dac5c75e 2022-06-04 stsp delta_cache_resize(struct got_delta_cache *cache, unsigned int nbuckets)
132 dac5c75e 2022-06-04 stsp {
133 dac5c75e 2022-06-04 stsp struct got_delta_cache_head *buckets;
134 dac5c75e 2022-06-04 stsp size_t i;
135 ab2f42e7 2019-11-10 stsp
136 dac5c75e 2022-06-04 stsp buckets = calloc(nbuckets, sizeof(buckets[0]));
137 dac5c75e 2022-06-04 stsp if (buckets == NULL) {
138 dac5c75e 2022-06-04 stsp if (errno != ENOMEM)
139 dac5c75e 2022-06-04 stsp return got_error_from_errno("calloc");
140 dac5c75e 2022-06-04 stsp /* Proceed with our current amount of hash buckets. */
141 dac5c75e 2022-06-04 stsp cache->flags |= GOT_DELTA_CACHE_F_NOMEM;
142 dac5c75e 2022-06-04 stsp return NULL;
143 dac5c75e 2022-06-04 stsp }
144 dac5c75e 2022-06-04 stsp
145 dac5c75e 2022-06-04 stsp arc4random_buf(&cache->key, sizeof(cache->key));
146 dac5c75e 2022-06-04 stsp
147 dac5c75e 2022-06-04 stsp for (i = 0; i < cache->nbuckets; i++) {
148 dac5c75e 2022-06-04 stsp unsigned int j;
149 dac5c75e 2022-06-04 stsp for (j = 0; j < cache->buckets[i].nchain; j++) {
150 dac5c75e 2022-06-04 stsp struct got_delta_cache_head *head;
151 dac5c75e 2022-06-04 stsp struct got_cached_delta *delta;
152 dac5c75e 2022-06-04 stsp uint64_t idx;
153 dac5c75e 2022-06-04 stsp delta = &cache->buckets[i].entries[j];
154 dac5c75e 2022-06-04 stsp idx = delta_cache_hash(cache, delta->offset) % nbuckets;
155 dac5c75e 2022-06-04 stsp head = &buckets[idx];
156 dac5c75e 2022-06-04 stsp if (head->nchain < nitems(head->entries)) {
157 dac5c75e 2022-06-04 stsp struct got_cached_delta *new_delta;
158 dac5c75e 2022-06-04 stsp new_delta = &head->entries[head->nchain];
159 dac5c75e 2022-06-04 stsp memcpy(new_delta, delta, sizeof(*new_delta));
160 dac5c75e 2022-06-04 stsp head->nchain++;
161 dac5c75e 2022-06-04 stsp } else
162 dac5c75e 2022-06-04 stsp free(delta->data);
163 dac5c75e 2022-06-04 stsp }
164 dac5c75e 2022-06-04 stsp }
165 dac5c75e 2022-06-04 stsp
166 dac5c75e 2022-06-04 stsp free(cache->buckets);
167 dac5c75e 2022-06-04 stsp cache->buckets = buckets;
168 dac5c75e 2022-06-04 stsp cache->nbuckets = nbuckets;
169 dac5c75e 2022-06-04 stsp return NULL;
170 ab2f42e7 2019-11-10 stsp }
171 ab2f42e7 2019-11-10 stsp
172 dac5c75e 2022-06-04 stsp static const struct got_error *
173 dac5c75e 2022-06-04 stsp delta_cache_grow(struct got_delta_cache *cache)
174 dac5c75e 2022-06-04 stsp {
175 dac5c75e 2022-06-04 stsp unsigned int nbuckets;
176 dac5c75e 2022-06-04 stsp
177 dac5c75e 2022-06-04 stsp if ((cache->flags & GOT_DELTA_CACHE_F_NOMEM) ||
178 dac5c75e 2022-06-04 stsp cache->nbuckets == GOT_DELTA_CACHE_MAX_BUCKETS)
179 dac5c75e 2022-06-04 stsp return NULL;
180 dac5c75e 2022-06-04 stsp
181 dac5c75e 2022-06-04 stsp if (cache->nbuckets >= GOT_DELTA_CACHE_MAX_BUCKETS / 2)
182 dac5c75e 2022-06-04 stsp nbuckets = GOT_DELTA_CACHE_MAX_BUCKETS;
183 dac5c75e 2022-06-04 stsp else
184 dac5c75e 2022-06-04 stsp nbuckets = cache->nbuckets * 2;
185 dac5c75e 2022-06-04 stsp
186 dac5c75e 2022-06-04 stsp return delta_cache_resize(cache, nbuckets);
187 dac5c75e 2022-06-04 stsp }
188 20282b02 2022-06-16 tracey #endif
189 dac5c75e 2022-06-04 stsp
190 ab2f42e7 2019-11-10 stsp const struct got_error *
191 ab2f42e7 2019-11-10 stsp got_delta_cache_add(struct got_delta_cache *cache,
192 ab2f42e7 2019-11-10 stsp off_t delta_data_offset, uint8_t *delta_data, size_t delta_len)
193 ab2f42e7 2019-11-10 stsp {
194 fa7a529e 2020-01-06 stsp #ifdef GOT_NO_OBJ_CACHE
195 fa7a529e 2020-01-06 stsp return got_error(GOT_ERR_NO_SPACE);
196 fa7a529e 2020-01-06 stsp #else
197 dac5c75e 2022-06-04 stsp const struct got_error *err = NULL;
198 dac5c75e 2022-06-04 stsp struct got_cached_delta *delta;
199 dac5c75e 2022-06-04 stsp struct got_delta_cache_head *head;
200 dac5c75e 2022-06-04 stsp uint64_t idx;
201 ab2f42e7 2019-11-10 stsp
202 4264b841 2022-06-04 stsp if (delta_len > GOT_DELTA_CACHE_MAX_DELTA_SIZE) {
203 c3b318d0 2019-11-10 stsp cache->cache_toolarge++;
204 ab2f42e7 2019-11-10 stsp return got_error(GOT_ERR_NO_SPACE);
205 c3b318d0 2019-11-10 stsp }
206 ab2f42e7 2019-11-10 stsp
207 dac5c75e 2022-06-04 stsp if (cache->nbuckets * 3 < cache->totelem * 4) {
208 dac5c75e 2022-06-04 stsp err = delta_cache_grow(cache);
209 dac5c75e 2022-06-04 stsp if (err)
210 dac5c75e 2022-06-04 stsp return err;
211 dac5c75e 2022-06-04 stsp }
212 ab2f42e7 2019-11-10 stsp
213 dac5c75e 2022-06-04 stsp idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
214 dac5c75e 2022-06-04 stsp head = &cache->buckets[idx];
215 dac5c75e 2022-06-04 stsp if (head->nchain >= nitems(head->entries)) {
216 dac5c75e 2022-06-04 stsp delta = &head->entries[head->nchain - 1];
217 dac5c75e 2022-06-04 stsp free(delta->data);
218 dac5c75e 2022-06-04 stsp memset(delta, 0, sizeof(*delta));
219 dac5c75e 2022-06-04 stsp head->nchain--;
220 dac5c75e 2022-06-04 stsp }
221 ab2f42e7 2019-11-10 stsp
222 dac5c75e 2022-06-04 stsp delta = &head->entries[head->nchain];
223 dac5c75e 2022-06-04 stsp delta->offset = delta_data_offset;
224 dac5c75e 2022-06-04 stsp delta->data = delta_data;
225 dac5c75e 2022-06-04 stsp delta->len = delta_len;
226 dac5c75e 2022-06-04 stsp head->nchain++;
227 dac5c75e 2022-06-04 stsp cache->totelem++;
228 ab2f42e7 2019-11-10 stsp
229 ab2f42e7 2019-11-10 stsp return NULL;
230 fa7a529e 2020-01-06 stsp #endif
231 ab2f42e7 2019-11-10 stsp }
232 ab2f42e7 2019-11-10 stsp
233 ab2f42e7 2019-11-10 stsp void
234 ab2f42e7 2019-11-10 stsp got_delta_cache_get(uint8_t **delta_data, size_t *delta_len,
235 ab2f42e7 2019-11-10 stsp struct got_delta_cache *cache, off_t delta_data_offset)
236 ab2f42e7 2019-11-10 stsp {
237 dac5c75e 2022-06-04 stsp uint64_t idx;
238 dac5c75e 2022-06-04 stsp struct got_delta_cache_head *head;
239 dac5c75e 2022-06-04 stsp struct got_cached_delta *delta;
240 dac5c75e 2022-06-04 stsp int i;
241 ab2f42e7 2019-11-10 stsp
242 dac5c75e 2022-06-04 stsp idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
243 dac5c75e 2022-06-04 stsp head = &cache->buckets[idx];
244 dac5c75e 2022-06-04 stsp
245 c3b318d0 2019-11-10 stsp cache->cache_search++;
246 ab2f42e7 2019-11-10 stsp *delta_data = NULL;
247 ab2f42e7 2019-11-10 stsp *delta_len = 0;
248 dac5c75e 2022-06-04 stsp for (i = 0; i < head->nchain; i++) {
249 dac5c75e 2022-06-04 stsp delta = &head->entries[i];
250 dac5c75e 2022-06-04 stsp if (delta->offset != delta_data_offset)
251 ab2f42e7 2019-11-10 stsp continue;
252 c3b318d0 2019-11-10 stsp cache->cache_hit++;
253 dac5c75e 2022-06-04 stsp if (i > 0) {
254 dac5c75e 2022-06-04 stsp struct got_cached_delta tmp;
255 dac5c75e 2022-06-04 stsp memcpy(&tmp, &head->entries[0], sizeof(tmp));
256 dac5c75e 2022-06-04 stsp memcpy(&head->entries[0], &head->entries[i],
257 dac5c75e 2022-06-04 stsp sizeof(head->entries[0]));
258 dac5c75e 2022-06-04 stsp memcpy(&head->entries[i], &tmp,
259 dac5c75e 2022-06-04 stsp sizeof(head->entries[i]));
260 dac5c75e 2022-06-04 stsp delta = &head->entries[0];
261 ab2f42e7 2019-11-10 stsp }
262 dac5c75e 2022-06-04 stsp *delta_data = delta->data;
263 dac5c75e 2022-06-04 stsp *delta_len = delta->len;
264 ab2f42e7 2019-11-10 stsp return;
265 ab2f42e7 2019-11-10 stsp }
266 c3b318d0 2019-11-10 stsp
267 c3b318d0 2019-11-10 stsp cache->cache_miss++;
268 ab2f42e7 2019-11-10 stsp }