portable: compat: remove unused files
[got-portable.git] / lib / object_cache.c
blobb6e53c387893d82c8683a9e3087d1c7052af32fd
1 /*
2 * Copyright (c) 2018 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/time.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <zlib.h>
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_lib_delta.h"
30 #include "got_lib_inflate.h"
31 #include "got_lib_object.h"
32 #include "got_lib_object_idset.h"
33 #include "got_lib_object_cache.h"
36 * XXX This should be reworked to track cache size and usage in bytes,
37 * rather than tracking N elements capped to a maximum element size.
39 #define GOT_OBJECT_CACHE_SIZE_OBJ 256
40 #define GOT_OBJECT_CACHE_SIZE_TREE 256
41 #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
42 #define GOT_OBJECT_CACHE_SIZE_TAG 2048
43 #define GOT_OBJECT_CACHE_MAX_ELEM_SIZE 1048576 /* 1 MB */
45 const struct got_error *
46 got_object_cache_init(struct got_object_cache *cache,
47 enum got_object_cache_type type)
49 memset(cache, 0, sizeof(*cache));
51 cache->idset = got_object_idset_alloc();
52 if (cache->idset == NULL)
53 return got_error_from_errno("got_object_idset_alloc");
55 cache->type = type;
56 switch (type) {
57 case GOT_OBJECT_CACHE_TYPE_OBJ:
58 cache->size = GOT_OBJECT_CACHE_SIZE_OBJ;
59 break;
60 case GOT_OBJECT_CACHE_TYPE_TREE:
61 cache->size = GOT_OBJECT_CACHE_SIZE_TREE;
62 break;
63 case GOT_OBJECT_CACHE_TYPE_COMMIT:
64 cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
65 break;
66 case GOT_OBJECT_CACHE_TYPE_TAG:
67 cache->size = GOT_OBJECT_CACHE_SIZE_TAG;
68 break;
70 return NULL;
73 size_t
74 get_size_obj(struct got_object *obj)
76 size_t size = sizeof(*obj);
77 struct got_delta *delta;
79 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
80 return size;
82 STAILQ_FOREACH(delta, &obj->deltas.entries, entry) {
83 if (SIZE_MAX - sizeof(*delta) < size)
84 return SIZE_MAX;
85 size += sizeof(*delta);
88 return size;
91 size_t
92 get_size_tree(struct got_tree_object *tree)
94 size_t size = sizeof(*tree);
96 size += sizeof(struct got_tree_entry) * tree->nentries;
97 return size;
100 size_t
101 get_size_commit(struct got_commit_object *commit)
103 size_t size = sizeof(*commit);
104 struct got_object_qid *qid;
106 size += sizeof(*commit->tree_id);
107 size += strlen(commit->author);
108 size += strlen(commit->committer);
109 size += strlen(commit->logmsg);
111 STAILQ_FOREACH(qid, &commit->parent_ids, entry)
112 size += sizeof(*qid) + sizeof(*qid->id);
114 return size;
117 size_t
118 get_size_tag(struct got_tag_object *tag)
120 size_t size = sizeof(*tag);
122 size += strlen(tag->tag);
123 size += strlen(tag->tagger);
124 size += strlen(tag->tagmsg);
126 return size;
129 const struct got_error *
130 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
132 const struct got_error *err = NULL;
133 struct got_object_cache_entry *ce;
134 int nelem;
135 size_t size;
137 switch (cache->type) {
138 case GOT_OBJECT_CACHE_TYPE_OBJ:
139 size = get_size_obj((struct got_object *)item);
140 break;
141 case GOT_OBJECT_CACHE_TYPE_TREE:
142 size = get_size_tree((struct got_tree_object *)item);
143 break;
144 case GOT_OBJECT_CACHE_TYPE_COMMIT:
145 size = get_size_commit((struct got_commit_object *)item);
146 break;
147 case GOT_OBJECT_CACHE_TYPE_TAG:
148 size = get_size_tag((struct got_tag_object *)item);
149 break;
150 default:
151 return got_error(GOT_ERR_OBJ_TYPE);
154 if (size > GOT_OBJECT_CACHE_MAX_ELEM_SIZE) {
155 #ifdef GOT_OBJ_CACHE_DEBUG
156 char *id_str;
157 if (got_object_id_str(&id_str, id) != NULL)
158 return got_error_from_errno("got_object_id_str");
159 fprintf(stderr, "%s: not caching ", getprogname());
160 switch (cache->type) {
161 case GOT_OBJECT_CACHE_TYPE_OBJ:
162 fprintf(stderr, "object");
163 break;
164 case GOT_OBJECT_CACHE_TYPE_TREE:
165 fprintf(stderr, "tree");
166 break;
167 case GOT_OBJECT_CACHE_TYPE_COMMIT:
168 fprintf(stderr, "commit");
169 break;
170 case GOT_OBJECT_CACHE_TYPE_TAG:
171 fprintf(stderr, "tag");
172 break;
174 fprintf(stderr, " %s (%zd bytes; %zd MB)\n", id_str, size,
175 size/1024/1024);
176 free(id_str);
177 #endif
178 cache->cache_toolarge++;
179 return got_error(GOT_ERR_OBJ_TOO_LARGE);
182 nelem = got_object_idset_num_elements(cache->idset);
183 if (nelem >= cache->size) {
184 err = got_object_idset_remove((void **)&ce,
185 cache->idset, NULL);
186 if (err)
187 return err;
188 switch (cache->type) {
189 case GOT_OBJECT_CACHE_TYPE_OBJ:
190 got_object_close(ce->data.obj);
191 break;
192 case GOT_OBJECT_CACHE_TYPE_TREE:
193 got_object_tree_close(ce->data.tree);
194 break;
195 case GOT_OBJECT_CACHE_TYPE_COMMIT:
196 got_object_commit_close(ce->data.commit);
197 break;
198 case GOT_OBJECT_CACHE_TYPE_TAG:
199 got_object_tag_close(ce->data.tag);
200 break;
202 free(ce);
203 cache->cache_evict++;
206 ce = malloc(sizeof(*ce));
207 if (ce == NULL)
208 return got_error_from_errno("malloc");
209 memcpy(&ce->id, id, sizeof(ce->id));
210 switch (cache->type) {
211 case GOT_OBJECT_CACHE_TYPE_OBJ:
212 ce->data.obj = (struct got_object *)item;
213 break;
214 case GOT_OBJECT_CACHE_TYPE_TREE:
215 ce->data.tree = (struct got_tree_object *)item;
216 break;
217 case GOT_OBJECT_CACHE_TYPE_COMMIT:
218 ce->data.commit = (struct got_commit_object *)item;
219 break;
220 case GOT_OBJECT_CACHE_TYPE_TAG:
221 ce->data.tag = (struct got_tag_object *)item;
222 break;
225 err = got_object_idset_add(cache->idset, id, ce);
226 if (err)
227 free(ce);
228 return err;
231 void *
232 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
234 struct got_object_cache_entry *ce;
236 cache->cache_searches++;
237 ce = got_object_idset_get(cache->idset, id);
238 if (ce) {
239 cache->cache_hit++;
240 switch (cache->type) {
241 case GOT_OBJECT_CACHE_TYPE_OBJ:
242 return ce->data.obj;
243 case GOT_OBJECT_CACHE_TYPE_TREE:
244 return ce->data.tree;
245 case GOT_OBJECT_CACHE_TYPE_COMMIT:
246 return ce->data.commit;
247 case GOT_OBJECT_CACHE_TYPE_TAG:
248 return ce->data.tag;
252 cache->cache_miss++;
253 return NULL;
256 #ifdef GOT_OBJ_CACHE_DEBUG
257 static void
258 print_cache_stats(struct got_object_cache *cache, const char *name)
260 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
261 "%d missed, %d evicted, %d too large\n", getprogname(), name,
262 got_object_idset_num_elements(cache->idset),
263 cache->cache_searches, cache->cache_hit,
264 cache->cache_miss, cache->cache_evict, cache->cache_toolarge);
267 const struct got_error *
268 check_refcount(struct got_object_id *id, void *data, void *arg)
270 struct got_object_cache *cache = arg;
271 struct got_object_cache_entry *ce = data;
272 struct got_object *obj;
273 struct got_tree_object *tree;
274 struct got_commit_object *commit;
275 struct got_tag_object *tag;
276 char *id_str;
278 if (got_object_id_str(&id_str, id) != NULL)
279 return NULL;
281 switch (cache->type) {
282 case GOT_OBJECT_CACHE_TYPE_OBJ:
283 obj = ce->data.obj;
284 if (obj->refcnt == 1)
285 break;
286 fprintf(stderr, "object %s has %d unclaimed references\n",
287 id_str, obj->refcnt - 1);
288 break;
289 case GOT_OBJECT_CACHE_TYPE_TREE:
290 tree = ce->data.tree;
291 if (tree->refcnt == 1)
292 break;
293 fprintf(stderr, "tree %s has %d unclaimed references\n",
294 id_str, tree->refcnt - 1);
295 break;
296 case GOT_OBJECT_CACHE_TYPE_COMMIT:
297 commit = ce->data.commit;
298 if (commit->refcnt == 1)
299 break;
300 fprintf(stderr, "commit %s has %d unclaimed references\n",
301 id_str, commit->refcnt - 1);
302 break;
303 case GOT_OBJECT_CACHE_TYPE_TAG:
304 tag = ce->data.tag;
305 if (tag->refcnt == 1)
306 break;
307 fprintf(stderr, "tag %s has %d unclaimed references\n",
308 id_str, tag->refcnt - 1);
309 break;
311 free(id_str);
312 return NULL;
314 #endif
316 void
317 got_object_cache_close(struct got_object_cache *cache)
319 #ifdef GOT_OBJ_CACHE_DEBUG
320 switch (cache->type) {
321 case GOT_OBJECT_CACHE_TYPE_OBJ:
322 print_cache_stats(cache, "object");
323 break;
324 case GOT_OBJECT_CACHE_TYPE_TREE:
325 print_cache_stats(cache, "tree");
326 break;
327 case GOT_OBJECT_CACHE_TYPE_COMMIT:
328 print_cache_stats(cache, "commit");
329 break;
330 case GOT_OBJECT_CACHE_TYPE_TAG:
331 print_cache_stats(cache, "tag");
332 break;
335 got_object_idset_for_each(cache->idset, check_refcount, cache);
336 #endif
338 if (cache->idset) {
339 got_object_idset_free(cache->idset);
340 cache->idset = NULL;
342 cache->size = 0;