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 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)
45 memset(cache, 0, sizeof(*cache));
48 case GOT_OBJECT_CACHE_TYPE_OBJ:
49 size = GOT_OBJECT_CACHE_SIZE_OBJ;
51 case GOT_OBJECT_CACHE_TYPE_TREE:
52 size = GOT_OBJECT_CACHE_SIZE_TREE;
54 case GOT_OBJECT_CACHE_TYPE_COMMIT:
55 size = GOT_OBJECT_CACHE_SIZE_COMMIT;
59 cache->idcache = got_object_idcache_alloc(size);
60 if (cache->idcache == NULL)
61 return got_error_from_errno();
67 const struct got_error *
68 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
70 const struct got_error *err = NULL;
71 struct got_object_cache_entry *ce;
74 nelem = got_object_idcache_num_elements(cache->idcache);
75 if (nelem >= cache->size) {
76 err = got_object_idcache_remove_least_used((void **)&ce,
80 switch (cache->type) {
81 case GOT_OBJECT_CACHE_TYPE_OBJ:
82 got_object_close(ce->data.obj);
84 case GOT_OBJECT_CACHE_TYPE_TREE:
85 got_object_tree_close(ce->data.tree);
87 case GOT_OBJECT_CACHE_TYPE_COMMIT:
88 got_object_commit_close(ce->data.commit);
95 ce = calloc(1, sizeof(*ce));
97 return got_error_from_errno();
98 memcpy(&ce->id, id, sizeof(ce->id));
99 switch (cache->type) {
100 case GOT_OBJECT_CACHE_TYPE_OBJ:
101 ce->data.obj = (struct got_object *)item;
103 case GOT_OBJECT_CACHE_TYPE_TREE:
104 ce->data.tree = (struct got_tree_object *)item;
106 case GOT_OBJECT_CACHE_TYPE_COMMIT:
107 ce->data.commit = (struct got_commit_object *)item;
111 err = got_object_idcache_add(cache->idcache, id, ce);
113 if (err->code == GOT_ERR_OBJ_EXISTS) {
122 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
124 struct got_object_cache_entry *ce;
126 cache->cache_searches++;
127 ce = got_object_idcache_get(cache->idcache, id);
130 switch (cache->type) {
131 case GOT_OBJECT_CACHE_TYPE_OBJ:
133 case GOT_OBJECT_CACHE_TYPE_TREE:
134 return ce->data.tree;
135 case GOT_OBJECT_CACHE_TYPE_COMMIT:
136 return ce->data.commit;
144 #ifdef GOT_OBJ_CACHE_DEBUG
146 print_cache_stats(struct got_object_cache *cache, const char *name)
148 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
149 "%d missed, %d evicted\n", getprogname(), name,
150 got_object_idcache_num_elements(cache->idcache),
151 cache->cache_searches, cache->cache_hit,
152 cache->cache_miss, cache->cache_evict);
155 void check_refcount(struct got_object_id *id, void *data, void *arg)
157 struct got_object_cache *cache = arg;
158 struct got_object_cache_entry *ce = data;
159 struct got_object *obj;
160 struct got_tree_object *tree;
161 struct got_commit_object *commit;
164 if (got_object_id_str(&id_str, id) != NULL)
167 switch (cache->type) {
168 case GOT_OBJECT_CACHE_TYPE_OBJ:
170 if (obj->refcnt == 1)
172 fprintf(stderr, "object %s has %d unclaimed references\n",
173 id_str, obj->refcnt - 1);
175 case GOT_OBJECT_CACHE_TYPE_TREE:
176 tree = ce->data.tree;
177 if (tree->refcnt == 1)
179 fprintf(stderr, "tree %s has %d unclaimed references\n",
180 id_str, tree->refcnt - 1);
182 case GOT_OBJECT_CACHE_TYPE_COMMIT:
183 commit = ce->data.commit;
184 if (commit->refcnt == 1)
186 fprintf(stderr, "commit %s has %d unclaimed references\n",
187 id_str, commit->refcnt - 1);
195 got_object_cache_close(struct got_object_cache *cache)
197 #ifdef GOT_OBJ_CACHE_DEBUG
198 switch (cache->type) {
199 case GOT_OBJECT_CACHE_TYPE_OBJ:
200 print_cache_stats(cache, "object");
202 case GOT_OBJECT_CACHE_TYPE_TREE:
203 print_cache_stats(cache, "tree");
205 case GOT_OBJECT_CACHE_TYPE_COMMIT:
206 print_cache_stats(cache, "commit");
210 got_object_idcache_for_each(cache->idcache, check_refcount, cache);
213 if (cache->idcache) {
214 got_object_idcache_free(cache->idcache);
215 cache->idcache = NULL;