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.
18 #include <sys/queue.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"
35 #define GOT_OBJECT_CACHE_SIZE_OBJ 256
36 #define GOT_OBJECT_CACHE_SIZE_TREE 256
37 #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
39 const struct got_error *
40 got_object_cache_init(struct got_object_cache *cache,
41 enum got_object_cache_type type)
43 memset(cache, 0, sizeof(*cache));
45 cache->idset = got_object_idset_alloc();
46 if (cache->idset == NULL)
47 return got_error_from_errno();
51 case GOT_OBJECT_CACHE_TYPE_OBJ:
52 cache->size = GOT_OBJECT_CACHE_SIZE_OBJ;
54 case GOT_OBJECT_CACHE_TYPE_TREE:
55 cache->size = GOT_OBJECT_CACHE_SIZE_TREE;
57 case GOT_OBJECT_CACHE_TYPE_COMMIT:
58 cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
64 const struct got_error *
65 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
67 const struct got_error *err = NULL;
68 struct got_object_cache_entry *ce;
71 nelem = got_object_idset_num_elements(cache->idset);
72 if (nelem >= cache->size) {
73 err = got_object_idset_remove((void **)&ce,
77 switch (cache->type) {
78 case GOT_OBJECT_CACHE_TYPE_OBJ:
79 got_object_close(ce->data.obj);
81 case GOT_OBJECT_CACHE_TYPE_TREE:
82 got_object_tree_close(ce->data.tree);
84 case GOT_OBJECT_CACHE_TYPE_COMMIT:
85 got_object_commit_close(ce->data.commit);
92 ce = malloc(sizeof(*ce));
94 return got_error_from_errno();
95 memcpy(&ce->id, id, sizeof(ce->id));
96 switch (cache->type) {
97 case GOT_OBJECT_CACHE_TYPE_OBJ:
98 ce->data.obj = (struct got_object *)item;
100 case GOT_OBJECT_CACHE_TYPE_TREE:
101 ce->data.tree = (struct got_tree_object *)item;
103 case GOT_OBJECT_CACHE_TYPE_COMMIT:
104 ce->data.commit = (struct got_commit_object *)item;
108 err = got_object_idset_add(cache->idset, id, ce);
110 if (err->code == GOT_ERR_OBJ_EXISTS) {
119 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
121 struct got_object_cache_entry *ce;
123 cache->cache_searches++;
124 ce = got_object_idset_get(cache->idset, id);
127 switch (cache->type) {
128 case GOT_OBJECT_CACHE_TYPE_OBJ:
130 case GOT_OBJECT_CACHE_TYPE_TREE:
131 return ce->data.tree;
132 case GOT_OBJECT_CACHE_TYPE_COMMIT:
133 return ce->data.commit;
141 #ifdef GOT_OBJ_CACHE_DEBUG
143 print_cache_stats(struct got_object_cache *cache, const char *name)
145 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
146 "%d missed, %d evicted\n", getprogname(), name,
147 got_object_idset_num_elements(cache->idset),
148 cache->cache_searches, cache->cache_hit,
149 cache->cache_miss, cache->cache_evict);
152 const struct got_error *
153 check_refcount(struct got_object_id *id, void *data, void *arg)
155 struct got_object_cache *cache = arg;
156 struct got_object_cache_entry *ce = data;
157 struct got_object *obj;
158 struct got_tree_object *tree;
159 struct got_commit_object *commit;
162 if (got_object_id_str(&id_str, id) != NULL)
165 switch (cache->type) {
166 case GOT_OBJECT_CACHE_TYPE_OBJ:
168 if (obj->refcnt == 1)
170 fprintf(stderr, "object %s has %d unclaimed references\n",
171 id_str, obj->refcnt - 1);
173 case GOT_OBJECT_CACHE_TYPE_TREE:
174 tree = ce->data.tree;
175 if (tree->refcnt == 1)
177 fprintf(stderr, "tree %s has %d unclaimed references\n",
178 id_str, tree->refcnt - 1);
180 case GOT_OBJECT_CACHE_TYPE_COMMIT:
181 commit = ce->data.commit;
182 if (commit->refcnt == 1)
184 fprintf(stderr, "commit %s has %d unclaimed references\n",
185 id_str, commit->refcnt - 1);
194 got_object_cache_close(struct got_object_cache *cache)
196 #ifdef GOT_OBJ_CACHE_DEBUG
197 switch (cache->type) {
198 case GOT_OBJECT_CACHE_TYPE_OBJ:
199 print_cache_stats(cache, "object");
201 case GOT_OBJECT_CACHE_TYPE_TREE:
202 print_cache_stats(cache, "tree");
204 case GOT_OBJECT_CACHE_TYPE_COMMIT:
205 print_cache_stats(cache, "commit");
209 got_object_idset_for_each(cache->idset, check_refcount, cache);
213 got_object_idset_free(cache->idset);