Blame


1 6bef87be 2018-09-11 stsp /*
2 6bef87be 2018-09-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 6bef87be 2018-09-11 stsp *
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.
7 6bef87be 2018-09-11 stsp *
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.
15 6bef87be 2018-09-11 stsp */
16 6bef87be 2018-09-11 stsp
17 6bef87be 2018-09-11 stsp #include <sys/time.h>
18 6bef87be 2018-09-11 stsp #include <sys/queue.h>
19 6bef87be 2018-09-11 stsp
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>
25 6bef87be 2018-09-11 stsp
26 6bef87be 2018-09-11 stsp #include "got_error.h"
27 6bef87be 2018-09-11 stsp #include "got_object.h"
28 6bef87be 2018-09-11 stsp
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"
34 6bef87be 2018-09-11 stsp
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
38 6bef87be 2018-09-11 stsp
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)
42 6bef87be 2018-09-11 stsp {
43 6bef87be 2018-09-11 stsp size_t size;
44 6bef87be 2018-09-11 stsp
45 dab9d9b6 2018-11-05 stsp memset(cache, 0, sizeof(*cache));
46 dab9d9b6 2018-11-05 stsp
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;
50 6bef87be 2018-09-11 stsp break;
51 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
52 6bef87be 2018-09-11 stsp size = GOT_OBJECT_CACHE_SIZE_TREE;
53 6bef87be 2018-09-11 stsp break;
54 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
55 6bef87be 2018-09-11 stsp size = GOT_OBJECT_CACHE_SIZE_COMMIT;
56 6bef87be 2018-09-11 stsp break;
57 6bef87be 2018-09-11 stsp }
58 6bef87be 2018-09-11 stsp
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;
65 6bef87be 2018-09-11 stsp }
66 6bef87be 2018-09-11 stsp
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)
69 6bef87be 2018-09-11 stsp {
70 6bef87be 2018-09-11 stsp const struct got_error *err = NULL;
71 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
72 6bef87be 2018-09-11 stsp int nelem;
73 6bef87be 2018-09-11 stsp
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);
78 6bef87be 2018-09-11 stsp if (err)
79 6bef87be 2018-09-11 stsp return err;
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);
83 6bef87be 2018-09-11 stsp break;
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);
86 6bef87be 2018-09-11 stsp break;
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);
89 6bef87be 2018-09-11 stsp break;
90 6bef87be 2018-09-11 stsp }
91 6bef87be 2018-09-11 stsp free(ce);
92 315fa2b2 2018-09-15 stsp cache->cache_evict++;
93 6bef87be 2018-09-11 stsp }
94 6bef87be 2018-09-11 stsp
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;
102 6bef87be 2018-09-11 stsp break;
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;
105 6bef87be 2018-09-11 stsp break;
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;
108 6bef87be 2018-09-11 stsp break;
109 6bef87be 2018-09-11 stsp }
110 6bef87be 2018-09-11 stsp
111 6bef87be 2018-09-11 stsp err = got_object_idcache_add(cache->idcache, id, ce);
112 6bef87be 2018-09-11 stsp if (err) {
113 6bef87be 2018-09-11 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
114 6bef87be 2018-09-11 stsp free(ce);
115 6bef87be 2018-09-11 stsp err = NULL;
116 6bef87be 2018-09-11 stsp }
117 6bef87be 2018-09-11 stsp }
118 6bef87be 2018-09-11 stsp return err;
119 6bef87be 2018-09-11 stsp }
120 6bef87be 2018-09-11 stsp
121 6bef87be 2018-09-11 stsp void *
122 6bef87be 2018-09-11 stsp got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
123 6bef87be 2018-09-11 stsp {
124 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
125 6bef87be 2018-09-11 stsp
126 221e79cd 2018-09-16 stsp cache->cache_searches++;
127 6bef87be 2018-09-11 stsp ce = got_object_idcache_get(cache->idcache, id);
128 6bef87be 2018-09-11 stsp if (ce) {
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;
137 6bef87be 2018-09-11 stsp }
138 6bef87be 2018-09-11 stsp }
139 6bef87be 2018-09-11 stsp
140 6bef87be 2018-09-11 stsp cache->cache_miss++;
141 6bef87be 2018-09-11 stsp return NULL;
142 6bef87be 2018-09-11 stsp }
143 6bef87be 2018-09-11 stsp
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)
147 6bef87be 2018-09-11 stsp {
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);
153 6bef87be 2018-09-11 stsp }
154 6bef87be 2018-09-11 stsp
155 6bef87be 2018-09-11 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
156 6bef87be 2018-09-11 stsp {
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;
163 6bef87be 2018-09-11 stsp
164 6bef87be 2018-09-11 stsp if (got_object_id_str(&id_str, id) != NULL)
165 6bef87be 2018-09-11 stsp return;
166 6bef87be 2018-09-11 stsp
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)
171 6bef87be 2018-09-11 stsp break;
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);
174 6bef87be 2018-09-11 stsp break;
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)
178 6bef87be 2018-09-11 stsp break;
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);
181 6bef87be 2018-09-11 stsp break;
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)
185 6bef87be 2018-09-11 stsp break;
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);
188 6bef87be 2018-09-11 stsp break;
189 6bef87be 2018-09-11 stsp }
190 6bef87be 2018-09-11 stsp free(id_str);
191 6bef87be 2018-09-11 stsp }
192 6bef87be 2018-09-11 stsp #endif
193 6bef87be 2018-09-11 stsp
194 6bef87be 2018-09-11 stsp void
195 6bef87be 2018-09-11 stsp got_object_cache_close(struct got_object_cache *cache)
196 6bef87be 2018-09-11 stsp {
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");
201 6bef87be 2018-09-11 stsp break;
202 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
203 6bef87be 2018-09-11 stsp print_cache_stats(cache, "tree");
204 6bef87be 2018-09-11 stsp break;
205 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
206 6bef87be 2018-09-11 stsp print_cache_stats(cache, "commit");
207 6bef87be 2018-09-11 stsp break;
208 6bef87be 2018-09-11 stsp }
209 6bef87be 2018-09-11 stsp
210 6bef87be 2018-09-11 stsp got_object_idcache_for_each(cache->idcache, check_refcount, cache);
211 6bef87be 2018-09-11 stsp #endif
212 6bef87be 2018-09-11 stsp
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;
216 6bef87be 2018-09-11 stsp }
217 6bef87be 2018-09-11 stsp cache->size = 0;
218 6bef87be 2018-09-11 stsp }