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
18 ab2f42e7 2019-11-10 stsp #include <stdlib.h>
19 ab2f42e7 2019-11-10 stsp #include <string.h>
20 26498a8b 2022-07-06 thomas #include <stdint.h>
21 ab2f42e7 2019-11-10 stsp #include <stdio.h>
22 ab2f42e7 2019-11-10 stsp #include <zlib.h>
23 ab2f42e7 2019-11-10 stsp #include <limits.h>
24 ab2f42e7 2019-11-10 stsp #include <time.h>
25 a5061f77 2022-06-13 thomas #include <errno.h>
26 ab2f42e7 2019-11-10 stsp
27 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
28 dd038bc6 2021-09-21 thomas.ad
29 ab2f42e7 2019-11-10 stsp #include "got_object.h"
30 ab2f42e7 2019-11-10 stsp #include "got_error.h"
31 ab2f42e7 2019-11-10 stsp
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"
36 ab2f42e7 2019-11-10 stsp
37 ab2f42e7 2019-11-10 stsp #ifndef nitems
38 ab2f42e7 2019-11-10 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 ab2f42e7 2019-11-10 stsp #endif
40 ab2f42e7 2019-11-10 stsp
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
45 a5061f77 2022-06-13 thomas
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;
50 ab2f42e7 2019-11-10 stsp };
51 ab2f42e7 2019-11-10 stsp
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;
55 a5061f77 2022-06-13 thomas };
56 ab2f42e7 2019-11-10 stsp
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;
69 ab2f42e7 2019-11-10 stsp };
70 ab2f42e7 2019-11-10 stsp
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)
73 ab2f42e7 2019-11-10 stsp {
74 a5061f77 2022-06-13 thomas const struct got_error *err;
75 ab2f42e7 2019-11-10 stsp struct got_delta_cache *cache;
76 ab2f42e7 2019-11-10 stsp
77 a5061f77 2022-06-13 thomas *new = NULL;
78 a5061f77 2022-06-13 thomas
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");
82 7c7a66bd 2022-06-23 thomas
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;
89 a5061f77 2022-06-13 thomas }
90 a5061f77 2022-06-13 thomas cache->nbuckets = GOT_DELTA_CACHE_MIN_BUCKETS;
91 ab2f42e7 2019-11-10 stsp
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;
95 ab2f42e7 2019-11-10 stsp }
96 ab2f42e7 2019-11-10 stsp
97 ab2f42e7 2019-11-10 stsp void
98 ab2f42e7 2019-11-10 stsp got_delta_cache_free(struct got_delta_cache *cache)
99 ab2f42e7 2019-11-10 stsp {
100 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
101 a5061f77 2022-06-13 thomas unsigned int i;
102 ab2f42e7 2019-11-10 stsp
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);
108 c3b318d0 2019-11-10 stsp #endif
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;
111 a5061f77 2022-06-13 thomas int j;
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);
116 a5061f77 2022-06-13 thomas }
117 ab2f42e7 2019-11-10 stsp }
118 a5061f77 2022-06-13 thomas free(cache->buckets);
119 ab2f42e7 2019-11-10 stsp free(cache);
120 ab2f42e7 2019-11-10 stsp }
121 ab2f42e7 2019-11-10 stsp
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)
124 ab2f42e7 2019-11-10 stsp {
125 a5061f77 2022-06-13 thomas return SipHash24(&cache->key, &delta_offset, sizeof(delta_offset));
126 a5061f77 2022-06-13 thomas }
127 ab2f42e7 2019-11-10 stsp
128 7c7a66bd 2022-06-23 thomas #ifndef GOT_NO_OBJ_CACHE
129 a5061f77 2022-06-13 thomas static const struct got_error *
130 a5061f77 2022-06-13 thomas delta_cache_resize(struct got_delta_cache *cache, unsigned int nbuckets)
131 a5061f77 2022-06-13 thomas {
132 a5061f77 2022-06-13 thomas struct got_delta_cache_head *buckets;
133 a5061f77 2022-06-13 thomas size_t i;
134 ab2f42e7 2019-11-10 stsp
135 a5061f77 2022-06-13 thomas buckets = calloc(nbuckets, sizeof(buckets[0]));
136 a5061f77 2022-06-13 thomas if (buckets == NULL) {
137 a5061f77 2022-06-13 thomas if (errno != ENOMEM)
138 a5061f77 2022-06-13 thomas return got_error_from_errno("calloc");
139 a5061f77 2022-06-13 thomas /* Proceed with our current amount of hash buckets. */
140 a5061f77 2022-06-13 thomas cache->flags |= GOT_DELTA_CACHE_F_NOMEM;
141 a5061f77 2022-06-13 thomas return NULL;
142 a5061f77 2022-06-13 thomas }
143 a5061f77 2022-06-13 thomas
144 a5061f77 2022-06-13 thomas arc4random_buf(&cache->key, sizeof(cache->key));
145 a5061f77 2022-06-13 thomas
146 a5061f77 2022-06-13 thomas for (i = 0; i < cache->nbuckets; i++) {
147 a5061f77 2022-06-13 thomas unsigned int j;
148 a5061f77 2022-06-13 thomas for (j = 0; j < cache->buckets[i].nchain; j++) {
149 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
150 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
151 a5061f77 2022-06-13 thomas uint64_t idx;
152 a5061f77 2022-06-13 thomas delta = &cache->buckets[i].entries[j];
153 a5061f77 2022-06-13 thomas idx = delta_cache_hash(cache, delta->offset) % nbuckets;
154 a5061f77 2022-06-13 thomas head = &buckets[idx];
155 a5061f77 2022-06-13 thomas if (head->nchain < nitems(head->entries)) {
156 a5061f77 2022-06-13 thomas struct got_cached_delta *new_delta;
157 a5061f77 2022-06-13 thomas new_delta = &head->entries[head->nchain];
158 a5061f77 2022-06-13 thomas memcpy(new_delta, delta, sizeof(*new_delta));
159 a5061f77 2022-06-13 thomas head->nchain++;
160 a5061f77 2022-06-13 thomas } else
161 a5061f77 2022-06-13 thomas free(delta->data);
162 a5061f77 2022-06-13 thomas }
163 a5061f77 2022-06-13 thomas }
164 a5061f77 2022-06-13 thomas
165 a5061f77 2022-06-13 thomas free(cache->buckets);
166 a5061f77 2022-06-13 thomas cache->buckets = buckets;
167 a5061f77 2022-06-13 thomas cache->nbuckets = nbuckets;
168 a5061f77 2022-06-13 thomas return NULL;
169 ab2f42e7 2019-11-10 stsp }
170 ab2f42e7 2019-11-10 stsp
171 a5061f77 2022-06-13 thomas static const struct got_error *
172 a5061f77 2022-06-13 thomas delta_cache_grow(struct got_delta_cache *cache)
173 a5061f77 2022-06-13 thomas {
174 a5061f77 2022-06-13 thomas unsigned int nbuckets;
175 a5061f77 2022-06-13 thomas
176 a5061f77 2022-06-13 thomas if ((cache->flags & GOT_DELTA_CACHE_F_NOMEM) ||
177 a5061f77 2022-06-13 thomas cache->nbuckets == GOT_DELTA_CACHE_MAX_BUCKETS)
178 a5061f77 2022-06-13 thomas return NULL;
179 a5061f77 2022-06-13 thomas
180 a5061f77 2022-06-13 thomas if (cache->nbuckets >= GOT_DELTA_CACHE_MAX_BUCKETS / 2)
181 a5061f77 2022-06-13 thomas nbuckets = GOT_DELTA_CACHE_MAX_BUCKETS;
182 a5061f77 2022-06-13 thomas else
183 a5061f77 2022-06-13 thomas nbuckets = cache->nbuckets * 2;
184 a5061f77 2022-06-13 thomas
185 a5061f77 2022-06-13 thomas return delta_cache_resize(cache, nbuckets);
186 a5061f77 2022-06-13 thomas }
187 7c7a66bd 2022-06-23 thomas #endif
188 a5061f77 2022-06-13 thomas
189 ab2f42e7 2019-11-10 stsp const struct got_error *
190 ab2f42e7 2019-11-10 stsp got_delta_cache_add(struct got_delta_cache *cache,
191 ab2f42e7 2019-11-10 stsp off_t delta_data_offset, uint8_t *delta_data, size_t delta_len)
192 ab2f42e7 2019-11-10 stsp {
193 fa7a529e 2020-01-06 stsp #ifdef GOT_NO_OBJ_CACHE
194 fa7a529e 2020-01-06 stsp return got_error(GOT_ERR_NO_SPACE);
195 fa7a529e 2020-01-06 stsp #else
196 a5061f77 2022-06-13 thomas const struct got_error *err = NULL;
197 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
198 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
199 a5061f77 2022-06-13 thomas uint64_t idx;
200 ab2f42e7 2019-11-10 stsp
201 88b1b490 2022-06-13 thomas if (delta_len > GOT_DELTA_CACHE_MAX_DELTA_SIZE) {
202 c3b318d0 2019-11-10 stsp cache->cache_toolarge++;
203 ab2f42e7 2019-11-10 stsp return got_error(GOT_ERR_NO_SPACE);
204 c3b318d0 2019-11-10 stsp }
205 ab2f42e7 2019-11-10 stsp
206 a5061f77 2022-06-13 thomas if (cache->nbuckets * 3 < cache->totelem * 4) {
207 a5061f77 2022-06-13 thomas err = delta_cache_grow(cache);
208 a5061f77 2022-06-13 thomas if (err)
209 a5061f77 2022-06-13 thomas return err;
210 a5061f77 2022-06-13 thomas }
211 ab2f42e7 2019-11-10 stsp
212 a5061f77 2022-06-13 thomas idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
213 a5061f77 2022-06-13 thomas head = &cache->buckets[idx];
214 a5061f77 2022-06-13 thomas if (head->nchain >= nitems(head->entries)) {
215 a5061f77 2022-06-13 thomas delta = &head->entries[head->nchain - 1];
216 a5061f77 2022-06-13 thomas free(delta->data);
217 a5061f77 2022-06-13 thomas memset(delta, 0, sizeof(*delta));
218 a5061f77 2022-06-13 thomas head->nchain--;
219 a5061f77 2022-06-13 thomas }
220 ab2f42e7 2019-11-10 stsp
221 a5061f77 2022-06-13 thomas delta = &head->entries[head->nchain];
222 a5061f77 2022-06-13 thomas delta->offset = delta_data_offset;
223 a5061f77 2022-06-13 thomas delta->data = delta_data;
224 a5061f77 2022-06-13 thomas delta->len = delta_len;
225 a5061f77 2022-06-13 thomas head->nchain++;
226 a5061f77 2022-06-13 thomas cache->totelem++;
227 ab2f42e7 2019-11-10 stsp
228 ab2f42e7 2019-11-10 stsp return NULL;
229 fa7a529e 2020-01-06 stsp #endif
230 ab2f42e7 2019-11-10 stsp }
231 ab2f42e7 2019-11-10 stsp
232 ab2f42e7 2019-11-10 stsp void
233 ab2f42e7 2019-11-10 stsp got_delta_cache_get(uint8_t **delta_data, size_t *delta_len,
234 ab2f42e7 2019-11-10 stsp struct got_delta_cache *cache, off_t delta_data_offset)
235 ab2f42e7 2019-11-10 stsp {
236 a5061f77 2022-06-13 thomas uint64_t idx;
237 a5061f77 2022-06-13 thomas struct got_delta_cache_head *head;
238 a5061f77 2022-06-13 thomas struct got_cached_delta *delta;
239 a5061f77 2022-06-13 thomas int i;
240 ab2f42e7 2019-11-10 stsp
241 a5061f77 2022-06-13 thomas idx = delta_cache_hash(cache, delta_data_offset) % cache->nbuckets;
242 a5061f77 2022-06-13 thomas head = &cache->buckets[idx];
243 a5061f77 2022-06-13 thomas
244 c3b318d0 2019-11-10 stsp cache->cache_search++;
245 ab2f42e7 2019-11-10 stsp *delta_data = NULL;
246 ab2f42e7 2019-11-10 stsp *delta_len = 0;
247 a5061f77 2022-06-13 thomas for (i = 0; i < head->nchain; i++) {
248 a5061f77 2022-06-13 thomas delta = &head->entries[i];
249 a5061f77 2022-06-13 thomas if (delta->offset != delta_data_offset)
250 ab2f42e7 2019-11-10 stsp continue;
251 c3b318d0 2019-11-10 stsp cache->cache_hit++;
252 a5061f77 2022-06-13 thomas if (i > 0) {
253 a5061f77 2022-06-13 thomas struct got_cached_delta tmp;
254 a5061f77 2022-06-13 thomas memcpy(&tmp, &head->entries[0], sizeof(tmp));
255 a5061f77 2022-06-13 thomas memcpy(&head->entries[0], &head->entries[i],
256 a5061f77 2022-06-13 thomas sizeof(head->entries[0]));
257 a5061f77 2022-06-13 thomas memcpy(&head->entries[i], &tmp,
258 a5061f77 2022-06-13 thomas sizeof(head->entries[i]));
259 a5061f77 2022-06-13 thomas delta = &head->entries[0];
260 ab2f42e7 2019-11-10 stsp }
261 a5061f77 2022-06-13 thomas *delta_data = delta->data;
262 a5061f77 2022-06-13 thomas *delta_len = delta->len;
263 ab2f42e7 2019-11-10 stsp return;
264 ab2f42e7 2019-11-10 stsp }
265 c3b318d0 2019-11-10 stsp
266 c3b318d0 2019-11-10 stsp cache->cache_miss++;
267 ab2f42e7 2019-11-10 stsp }