2 6bef87be 2018-09-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 6bef87be 2018-09-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 6bef87be 2018-09-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 6bef87be 2018-09-11 stsp * copyright notice and this permission notice appear in all copies.
8 6bef87be 2018-09-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 6bef87be 2018-09-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 6bef87be 2018-09-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 6bef87be 2018-09-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 6bef87be 2018-09-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 6bef87be 2018-09-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 6bef87be 2018-09-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 6bef87be 2018-09-11 stsp #include <sys/time.h>
18 6bef87be 2018-09-11 stsp #include <sys/queue.h>
20 6bef87be 2018-09-11 stsp #include <stdio.h>
21 6bef87be 2018-09-11 stsp #include <stdlib.h>
22 6bef87be 2018-09-11 stsp #include <string.h>
23 6bef87be 2018-09-11 stsp #include <sha1.h>
24 6bef87be 2018-09-11 stsp #include <zlib.h>
26 6bef87be 2018-09-11 stsp #include "got_error.h"
27 6bef87be 2018-09-11 stsp #include "got_object.h"
29 6bef87be 2018-09-11 stsp #include "got_lib_delta.h"
30 6bef87be 2018-09-11 stsp #include "got_lib_inflate.h"
31 6bef87be 2018-09-11 stsp #include "got_lib_object.h"
32 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
33 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
35 9bccfa63 2018-11-05 stsp #define GOT_OBJECT_CACHE_SIZE_OBJ 256
36 9185b863 2018-11-05 stsp #define GOT_OBJECT_CACHE_SIZE_TREE 256
37 9bccfa63 2018-11-05 stsp #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
39 6bef87be 2018-09-11 stsp const struct got_error *
40 6bef87be 2018-09-11 stsp got_object_cache_init(struct got_object_cache *cache,
41 6bef87be 2018-09-11 stsp enum got_object_cache_type type)
43 6bef87be 2018-09-11 stsp size_t size;
45 dab9d9b6 2018-11-05 stsp memset(cache, 0, sizeof(*cache));
47 6bef87be 2018-09-11 stsp switch (type) {
48 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
49 6bef87be 2018-09-11 stsp size = GOT_OBJECT_CACHE_SIZE_OBJ;
51 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
52 6bef87be 2018-09-11 stsp size = GOT_OBJECT_CACHE_SIZE_TREE;
54 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
55 6bef87be 2018-09-11 stsp size = GOT_OBJECT_CACHE_SIZE_COMMIT;
59 6bef87be 2018-09-11 stsp cache->idcache = got_object_idcache_alloc(size);
60 6bef87be 2018-09-11 stsp if (cache->idcache == NULL)
61 6bef87be 2018-09-11 stsp return got_error_from_errno();
62 6bef87be 2018-09-11 stsp cache->type = type;
63 6bef87be 2018-09-11 stsp cache->size = size;
64 6bef87be 2018-09-11 stsp return NULL;
67 6bef87be 2018-09-11 stsp const struct got_error *
68 6bef87be 2018-09-11 stsp got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
70 6bef87be 2018-09-11 stsp const struct got_error *err = NULL;
71 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
74 6bef87be 2018-09-11 stsp nelem = got_object_idcache_num_elements(cache->idcache);
75 6bef87be 2018-09-11 stsp if (nelem >= cache->size) {
76 6bef87be 2018-09-11 stsp err = got_object_idcache_remove_least_used((void **)&ce,
77 6bef87be 2018-09-11 stsp cache->idcache);
80 6bef87be 2018-09-11 stsp switch (cache->type) {
81 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
82 6bef87be 2018-09-11 stsp got_object_close(ce->data.obj);
84 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
85 6bef87be 2018-09-11 stsp got_object_tree_close(ce->data.tree);
87 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
88 6bef87be 2018-09-11 stsp got_object_commit_close(ce->data.commit);
92 315fa2b2 2018-09-15 stsp cache->cache_evict++;
95 6bef87be 2018-09-11 stsp ce = calloc(1, sizeof(*ce));
96 6bef87be 2018-09-11 stsp if (ce == NULL)
97 6bef87be 2018-09-11 stsp return got_error_from_errno();
98 6bef87be 2018-09-11 stsp memcpy(&ce->id, id, sizeof(ce->id));
99 6bef87be 2018-09-11 stsp switch (cache->type) {
100 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
101 6bef87be 2018-09-11 stsp ce->data.obj = (struct got_object *)item;
103 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
104 6bef87be 2018-09-11 stsp ce->data.tree = (struct got_tree_object *)item;
106 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
107 6bef87be 2018-09-11 stsp ce->data.commit = (struct got_commit_object *)item;
111 6bef87be 2018-09-11 stsp err = got_object_idcache_add(cache->idcache, id, ce);
113 6bef87be 2018-09-11 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
115 6bef87be 2018-09-11 stsp err = NULL;
118 6bef87be 2018-09-11 stsp return err;
122 6bef87be 2018-09-11 stsp got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
124 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
126 221e79cd 2018-09-16 stsp cache->cache_searches++;
127 6bef87be 2018-09-11 stsp ce = got_object_idcache_get(cache->idcache, id);
129 6bef87be 2018-09-11 stsp cache->cache_hit++;
130 6bef87be 2018-09-11 stsp switch (cache->type) {
131 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
132 6bef87be 2018-09-11 stsp return ce->data.obj;
133 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
134 6bef87be 2018-09-11 stsp return ce->data.tree;
135 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
136 6bef87be 2018-09-11 stsp return ce->data.commit;
140 6bef87be 2018-09-11 stsp cache->cache_miss++;
141 6bef87be 2018-09-11 stsp return NULL;
144 f4081577 2018-09-15 stsp #ifdef GOT_OBJ_CACHE_DEBUG
145 6bef87be 2018-09-11 stsp static void
146 6bef87be 2018-09-11 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
148 221e79cd 2018-09-16 stsp fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
149 221e79cd 2018-09-16 stsp "%d missed, %d evicted\n", getprogname(), name,
150 7cf5e9c2 2018-09-15 stsp got_object_idcache_num_elements(cache->idcache),
151 221e79cd 2018-09-16 stsp cache->cache_searches, cache->cache_hit,
152 221e79cd 2018-09-16 stsp cache->cache_miss, cache->cache_evict);
155 6bef87be 2018-09-11 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
157 6bef87be 2018-09-11 stsp struct got_object_cache *cache = arg;
158 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce = data;
159 6bef87be 2018-09-11 stsp struct got_object *obj;
160 6bef87be 2018-09-11 stsp struct got_tree_object *tree;
161 6bef87be 2018-09-11 stsp struct got_commit_object *commit;
162 6bef87be 2018-09-11 stsp char *id_str;
164 6bef87be 2018-09-11 stsp if (got_object_id_str(&id_str, id) != NULL)
167 6bef87be 2018-09-11 stsp switch (cache->type) {
168 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
169 6bef87be 2018-09-11 stsp obj = ce->data.obj;
170 6bef87be 2018-09-11 stsp if (obj->refcnt == 1)
172 6bef87be 2018-09-11 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
173 6bef87be 2018-09-11 stsp id_str, obj->refcnt - 1);
175 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
176 6bef87be 2018-09-11 stsp tree = ce->data.tree;
177 6bef87be 2018-09-11 stsp if (tree->refcnt == 1)
179 6bef87be 2018-09-11 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
180 6bef87be 2018-09-11 stsp id_str, tree->refcnt - 1);
182 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
183 6bef87be 2018-09-11 stsp commit = ce->data.commit;
184 6bef87be 2018-09-11 stsp if (commit->refcnt == 1)
186 6bef87be 2018-09-11 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
187 414611d9 2018-09-19 stsp id_str, commit->refcnt - 1);
190 6bef87be 2018-09-11 stsp free(id_str);
195 6bef87be 2018-09-11 stsp got_object_cache_close(struct got_object_cache *cache)
197 f4081577 2018-09-15 stsp #ifdef GOT_OBJ_CACHE_DEBUG
198 6bef87be 2018-09-11 stsp switch (cache->type) {
199 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
200 6bef87be 2018-09-11 stsp print_cache_stats(cache, "object");
202 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
203 6bef87be 2018-09-11 stsp print_cache_stats(cache, "tree");
205 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
206 6bef87be 2018-09-11 stsp print_cache_stats(cache, "commit");
210 6bef87be 2018-09-11 stsp got_object_idcache_for_each(cache->idcache, check_refcount, cache);
213 6bef87be 2018-09-11 stsp if (cache->idcache) {
214 6bef87be 2018-09-11 stsp got_object_idcache_free(cache->idcache);
215 6bef87be 2018-09-11 stsp cache->idcache = NULL;
217 6bef87be 2018-09-11 stsp cache->size = 0;