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_idcache.h"
33 #include "got_lib_object_cache.h"
35 #define GOT_OBJECT_CACHE_SIZE_OBJ 1024
36 #define GOT_OBJECT_CACHE_SIZE_TREE 2048
37 #define GOT_OBJECT_CACHE_SIZE_COMMIT 512
39 const struct got_error *
40 got_object_cache_init(struct got_object_cache *cache,
41 enum got_object_cache_type type)
46 case GOT_OBJECT_CACHE_TYPE_OBJ:
47 size = GOT_OBJECT_CACHE_SIZE_OBJ;
49 case GOT_OBJECT_CACHE_TYPE_TREE:
50 size = GOT_OBJECT_CACHE_SIZE_TREE;
52 case GOT_OBJECT_CACHE_TYPE_COMMIT:
53 size = GOT_OBJECT_CACHE_SIZE_COMMIT;
57 cache->idcache = got_object_idcache_alloc(size);
58 if (cache->idcache == NULL)
59 return got_error_from_errno();
65 const struct got_error *
66 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
68 const struct got_error *err = NULL;
69 struct got_object_cache_entry *ce;
72 nelem = got_object_idcache_num_elements(cache->idcache);
73 if (nelem >= cache->size) {
74 err = got_object_idcache_remove_least_used((void **)&ce,
78 switch (cache->type) {
79 case GOT_OBJECT_CACHE_TYPE_OBJ:
80 got_object_close(ce->data.obj);
82 case GOT_OBJECT_CACHE_TYPE_TREE:
83 got_object_tree_close(ce->data.tree);
85 case GOT_OBJECT_CACHE_TYPE_COMMIT:
86 got_object_commit_close(ce->data.commit);
93 ce = calloc(1, sizeof(*ce));
95 return got_error_from_errno();
96 memcpy(&ce->id, id, sizeof(ce->id));
97 switch (cache->type) {
98 case GOT_OBJECT_CACHE_TYPE_OBJ:
99 ce->data.obj = (struct got_object *)item;
101 case GOT_OBJECT_CACHE_TYPE_TREE:
102 ce->data.tree = (struct got_tree_object *)item;
104 case GOT_OBJECT_CACHE_TYPE_COMMIT:
105 ce->data.commit = (struct got_commit_object *)item;
109 err = got_object_idcache_add(cache->idcache, id, ce);
111 if (err->code == GOT_ERR_OBJ_EXISTS) {
120 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
122 struct got_object_cache_entry *ce;
124 cache->cache_searches++;
125 ce = got_object_idcache_get(cache->idcache, id);
128 switch (cache->type) {
129 case GOT_OBJECT_CACHE_TYPE_OBJ:
131 case GOT_OBJECT_CACHE_TYPE_TREE:
132 return ce->data.tree;
133 case GOT_OBJECT_CACHE_TYPE_COMMIT:
134 return ce->data.commit;
142 #ifdef GOT_OBJ_CACHE_DEBUG
144 print_cache_stats(struct got_object_cache *cache, const char *name)
146 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
147 "%d missed, %d evicted\n", getprogname(), name,
148 got_object_idcache_num_elements(cache->idcache),
149 cache->cache_searches, cache->cache_hit,
150 cache->cache_miss, cache->cache_evict);
153 void 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);
193 got_object_cache_close(struct got_object_cache *cache)
195 #ifdef GOT_OBJ_CACHE_DEBUG
196 switch (cache->type) {
197 case GOT_OBJECT_CACHE_TYPE_OBJ:
198 print_cache_stats(cache, "object");
200 case GOT_OBJECT_CACHE_TYPE_TREE:
201 print_cache_stats(cache, "tree");
203 case GOT_OBJECT_CACHE_TYPE_COMMIT:
204 print_cache_stats(cache, "commit");
208 got_object_idcache_for_each(cache->idcache, check_refcount, cache);
211 if (cache->idcache) {
212 got_object_idcache_free(cache->idcache);
213 cache->idcache = NULL;