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 f054b67a 2018-11-05 stsp #include "got_lib_object_idset.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 f4a881ce 2018-11-17 stsp #define GOT_OBJECT_CACHE_SIZE_TAG 32
39 6bef87be 2018-09-11 stsp
40 6bef87be 2018-09-11 stsp const struct got_error *
41 6bef87be 2018-09-11 stsp got_object_cache_init(struct got_object_cache *cache,
42 6bef87be 2018-09-11 stsp enum got_object_cache_type type)
43 6bef87be 2018-09-11 stsp {
44 dab9d9b6 2018-11-05 stsp memset(cache, 0, sizeof(*cache));
45 dab9d9b6 2018-11-05 stsp
46 f054b67a 2018-11-05 stsp cache->idset = got_object_idset_alloc();
47 f054b67a 2018-11-05 stsp if (cache->idset == NULL)
48 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_idset_alloc");
49 f054b67a 2018-11-05 stsp
50 f054b67a 2018-11-05 stsp cache->type = type;
51 6bef87be 2018-09-11 stsp switch (type) {
52 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
53 f054b67a 2018-11-05 stsp cache->size = GOT_OBJECT_CACHE_SIZE_OBJ;
54 6bef87be 2018-09-11 stsp break;
55 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
56 f054b67a 2018-11-05 stsp cache->size = GOT_OBJECT_CACHE_SIZE_TREE;
57 6bef87be 2018-09-11 stsp break;
58 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
59 f054b67a 2018-11-05 stsp cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
60 6bef87be 2018-09-11 stsp break;
61 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
62 f4a881ce 2018-11-17 stsp cache->size = GOT_OBJECT_CACHE_SIZE_TAG;
63 f4a881ce 2018-11-17 stsp break;
64 6bef87be 2018-09-11 stsp }
65 6bef87be 2018-09-11 stsp return NULL;
66 6bef87be 2018-09-11 stsp }
67 6bef87be 2018-09-11 stsp
68 6bef87be 2018-09-11 stsp const struct got_error *
69 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 {
71 6bef87be 2018-09-11 stsp const struct got_error *err = NULL;
72 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
73 6bef87be 2018-09-11 stsp int nelem;
74 6bef87be 2018-09-11 stsp
75 f054b67a 2018-11-05 stsp nelem = got_object_idset_num_elements(cache->idset);
76 6bef87be 2018-09-11 stsp if (nelem >= cache->size) {
77 f054b67a 2018-11-05 stsp err = got_object_idset_remove((void **)&ce,
78 f054b67a 2018-11-05 stsp cache->idset, NULL);
79 6bef87be 2018-09-11 stsp if (err)
80 6bef87be 2018-09-11 stsp return err;
81 6bef87be 2018-09-11 stsp switch (cache->type) {
82 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
83 6bef87be 2018-09-11 stsp got_object_close(ce->data.obj);
84 6bef87be 2018-09-11 stsp break;
85 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
86 6bef87be 2018-09-11 stsp got_object_tree_close(ce->data.tree);
87 6bef87be 2018-09-11 stsp break;
88 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
89 6bef87be 2018-09-11 stsp got_object_commit_close(ce->data.commit);
90 6bef87be 2018-09-11 stsp break;
91 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
92 f4a881ce 2018-11-17 stsp got_object_tag_close(ce->data.tag);
93 f4a881ce 2018-11-17 stsp break;
94 6bef87be 2018-09-11 stsp }
95 6bef87be 2018-09-11 stsp free(ce);
96 315fa2b2 2018-09-15 stsp cache->cache_evict++;
97 6bef87be 2018-09-11 stsp }
98 6bef87be 2018-09-11 stsp
99 507aef8f 2018-11-05 stsp ce = malloc(sizeof(*ce));
100 6bef87be 2018-09-11 stsp if (ce == NULL)
101 230a42bd 2019-05-11 jcs return got_error_prefix_errno("malloc");
102 6bef87be 2018-09-11 stsp memcpy(&ce->id, id, sizeof(ce->id));
103 6bef87be 2018-09-11 stsp switch (cache->type) {
104 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
105 6bef87be 2018-09-11 stsp ce->data.obj = (struct got_object *)item;
106 6bef87be 2018-09-11 stsp break;
107 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
108 6bef87be 2018-09-11 stsp ce->data.tree = (struct got_tree_object *)item;
109 6bef87be 2018-09-11 stsp break;
110 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
111 6bef87be 2018-09-11 stsp ce->data.commit = (struct got_commit_object *)item;
112 6bef87be 2018-09-11 stsp break;
113 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
114 f4a881ce 2018-11-17 stsp ce->data.tag = (struct got_tag_object *)item;
115 f4a881ce 2018-11-17 stsp break;
116 6bef87be 2018-09-11 stsp }
117 6bef87be 2018-09-11 stsp
118 f054b67a 2018-11-05 stsp err = got_object_idset_add(cache->idset, id, ce);
119 6bef87be 2018-09-11 stsp if (err) {
120 6bef87be 2018-09-11 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
121 6bef87be 2018-09-11 stsp free(ce);
122 6bef87be 2018-09-11 stsp err = NULL;
123 6bef87be 2018-09-11 stsp }
124 6bef87be 2018-09-11 stsp }
125 6bef87be 2018-09-11 stsp return err;
126 6bef87be 2018-09-11 stsp }
127 6bef87be 2018-09-11 stsp
128 6bef87be 2018-09-11 stsp void *
129 6bef87be 2018-09-11 stsp got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
130 6bef87be 2018-09-11 stsp {
131 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce;
132 6bef87be 2018-09-11 stsp
133 221e79cd 2018-09-16 stsp cache->cache_searches++;
134 f054b67a 2018-11-05 stsp ce = got_object_idset_get(cache->idset, id);
135 6bef87be 2018-09-11 stsp if (ce) {
136 6bef87be 2018-09-11 stsp cache->cache_hit++;
137 6bef87be 2018-09-11 stsp switch (cache->type) {
138 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
139 6bef87be 2018-09-11 stsp return ce->data.obj;
140 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
141 6bef87be 2018-09-11 stsp return ce->data.tree;
142 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
143 6bef87be 2018-09-11 stsp return ce->data.commit;
144 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
145 f4a881ce 2018-11-17 stsp return ce->data.tag;
146 6bef87be 2018-09-11 stsp }
147 6bef87be 2018-09-11 stsp }
148 6bef87be 2018-09-11 stsp
149 6bef87be 2018-09-11 stsp cache->cache_miss++;
150 6bef87be 2018-09-11 stsp return NULL;
151 6bef87be 2018-09-11 stsp }
152 6bef87be 2018-09-11 stsp
153 f4081577 2018-09-15 stsp #ifdef GOT_OBJ_CACHE_DEBUG
154 6bef87be 2018-09-11 stsp static void
155 6bef87be 2018-09-11 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
156 6bef87be 2018-09-11 stsp {
157 221e79cd 2018-09-16 stsp fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
158 221e79cd 2018-09-16 stsp "%d missed, %d evicted\n", getprogname(), name,
159 f054b67a 2018-11-05 stsp got_object_idset_num_elements(cache->idset),
160 221e79cd 2018-09-16 stsp cache->cache_searches, cache->cache_hit,
161 221e79cd 2018-09-16 stsp cache->cache_miss, cache->cache_evict);
162 6bef87be 2018-09-11 stsp }
163 6bef87be 2018-09-11 stsp
164 cb103d04 2018-11-07 stsp const struct got_error *
165 cb103d04 2018-11-07 stsp check_refcount(struct got_object_id *id, void *data, void *arg)
166 6bef87be 2018-09-11 stsp {
167 6bef87be 2018-09-11 stsp struct got_object_cache *cache = arg;
168 6bef87be 2018-09-11 stsp struct got_object_cache_entry *ce = data;
169 6bef87be 2018-09-11 stsp struct got_object *obj;
170 6bef87be 2018-09-11 stsp struct got_tree_object *tree;
171 6bef87be 2018-09-11 stsp struct got_commit_object *commit;
172 f4a881ce 2018-11-17 stsp struct got_tag_object *tag;
173 6bef87be 2018-09-11 stsp char *id_str;
174 6bef87be 2018-09-11 stsp
175 6bef87be 2018-09-11 stsp if (got_object_id_str(&id_str, id) != NULL)
176 cb103d04 2018-11-07 stsp return NULL;
177 6bef87be 2018-09-11 stsp
178 6bef87be 2018-09-11 stsp switch (cache->type) {
179 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
180 6bef87be 2018-09-11 stsp obj = ce->data.obj;
181 6bef87be 2018-09-11 stsp if (obj->refcnt == 1)
182 6bef87be 2018-09-11 stsp break;
183 6bef87be 2018-09-11 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
184 6bef87be 2018-09-11 stsp id_str, obj->refcnt - 1);
185 6bef87be 2018-09-11 stsp break;
186 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
187 6bef87be 2018-09-11 stsp tree = ce->data.tree;
188 6bef87be 2018-09-11 stsp if (tree->refcnt == 1)
189 6bef87be 2018-09-11 stsp break;
190 6bef87be 2018-09-11 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
191 6bef87be 2018-09-11 stsp id_str, tree->refcnt - 1);
192 6bef87be 2018-09-11 stsp break;
193 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
194 6bef87be 2018-09-11 stsp commit = ce->data.commit;
195 6bef87be 2018-09-11 stsp if (commit->refcnt == 1)
196 6bef87be 2018-09-11 stsp break;
197 6bef87be 2018-09-11 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
198 414611d9 2018-09-19 stsp id_str, commit->refcnt - 1);
199 6bef87be 2018-09-11 stsp break;
200 f4a881ce 2018-11-17 stsp case GOT_OBJECT_CACHE_TYPE_TAG:
201 f4a881ce 2018-11-17 stsp tag = ce->data.tag;
202 f4a881ce 2018-11-17 stsp if (tag->refcnt == 1)
203 f4a881ce 2018-11-17 stsp break;
204 f4a881ce 2018-11-17 stsp fprintf(stderr, "tag %s has %d unclaimed references\n",
205 f4a881ce 2018-11-17 stsp id_str, tag->refcnt - 1);
206 f4a881ce 2018-11-17 stsp break;
207 6bef87be 2018-09-11 stsp }
208 6bef87be 2018-09-11 stsp free(id_str);
209 cb103d04 2018-11-07 stsp return NULL;
210 6bef87be 2018-09-11 stsp }
211 6bef87be 2018-09-11 stsp #endif
212 6bef87be 2018-09-11 stsp
213 6bef87be 2018-09-11 stsp void
214 6bef87be 2018-09-11 stsp got_object_cache_close(struct got_object_cache *cache)
215 6bef87be 2018-09-11 stsp {
216 f4081577 2018-09-15 stsp #ifdef GOT_OBJ_CACHE_DEBUG
217 6bef87be 2018-09-11 stsp switch (cache->type) {
218 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
219 6bef87be 2018-09-11 stsp print_cache_stats(cache, "object");
220 6bef87be 2018-09-11 stsp break;
221 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
222 6bef87be 2018-09-11 stsp print_cache_stats(cache, "tree");
223 6bef87be 2018-09-11 stsp break;
224 6bef87be 2018-09-11 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
225 6bef87be 2018-09-11 stsp print_cache_stats(cache, "commit");
226 6bef87be 2018-09-11 stsp break;
227 6bef87be 2018-09-11 stsp }
228 6bef87be 2018-09-11 stsp
229 f054b67a 2018-11-05 stsp got_object_idset_for_each(cache->idset, check_refcount, cache);
230 6bef87be 2018-09-11 stsp #endif
231 6bef87be 2018-09-11 stsp
232 f054b67a 2018-11-05 stsp if (cache->idset) {
233 f054b67a 2018-11-05 stsp got_object_idset_free(cache->idset);
234 f054b67a 2018-11-05 stsp cache->idset = NULL;
235 6bef87be 2018-09-11 stsp }
236 6bef87be 2018-09-11 stsp cache->size = 0;
237 6bef87be 2018-09-11 stsp }