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>
19 #include <sys/resource.h>
28 #include "got_compat.h"
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_inflate.h"
34 #include "got_lib_object.h"
35 #include "got_lib_object_idset.h"
36 #include "got_lib_object_cache.h"
39 * XXX This should be reworked to track cache size and usage in bytes,
40 * rather than tracking N elements capped to a maximum element size.
42 #define GOT_OBJECT_CACHE_SIZE_OBJ 256
43 #define GOT_OBJECT_CACHE_SIZE_TREE 256
44 #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
45 #define GOT_OBJECT_CACHE_SIZE_TAG 256
46 #define GOT_OBJECT_CACHE_SIZE_RAW 16
47 #define GOT_OBJECT_CACHE_MAX_ELEM_SIZE 1048576 /* 1 MB */
49 const struct got_error
*
50 got_object_cache_init(struct got_object_cache
*cache
,
51 enum got_object_cache_type type
)
55 memset(cache
, 0, sizeof(*cache
));
57 cache
->idset
= got_object_idset_alloc();
58 if (cache
->idset
== NULL
)
59 return got_error_from_errno("got_object_idset_alloc");
63 case GOT_OBJECT_CACHE_TYPE_OBJ
:
64 cache
->size
= GOT_OBJECT_CACHE_SIZE_OBJ
;
66 case GOT_OBJECT_CACHE_TYPE_TREE
:
67 cache
->size
= GOT_OBJECT_CACHE_SIZE_TREE
;
69 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
70 cache
->size
= GOT_OBJECT_CACHE_SIZE_COMMIT
;
72 case GOT_OBJECT_CACHE_TYPE_TAG
:
73 cache
->size
= GOT_OBJECT_CACHE_SIZE_TAG
;
75 case GOT_OBJECT_CACHE_TYPE_RAW
:
76 if (getrlimit(RLIMIT_NOFILE
, &rl
) == -1)
77 return got_error_from_errno("getrlimit");
78 cache
->size
= GOT_OBJECT_CACHE_SIZE_RAW
;
79 if (cache
->size
> rl
.rlim_cur
/ 16)
80 cache
->size
= rl
.rlim_cur
/ 16;
87 get_size_obj(struct got_object
*obj
)
89 size_t size
= sizeof(*obj
);
90 struct got_delta
*delta
;
92 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0)
95 STAILQ_FOREACH(delta
, &obj
->deltas
.entries
, entry
) {
96 if (SIZE_MAX
- sizeof(*delta
) < size
)
98 size
+= sizeof(*delta
);
105 get_size_tree(struct got_tree_object
*tree
)
107 size_t size
= sizeof(*tree
);
109 size
+= sizeof(struct got_tree_entry
) * tree
->nentries
;
114 get_size_commit(struct got_commit_object
*commit
)
116 size_t size
= sizeof(*commit
);
117 struct got_object_qid
*qid
;
119 size
+= sizeof(*commit
->tree_id
);
120 size
+= strlen(commit
->author
);
121 size
+= strlen(commit
->committer
);
122 size
+= strlen(commit
->logmsg
);
124 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
)
125 size
+= sizeof(*qid
) + sizeof(qid
->id
);
131 get_size_tag(struct got_tag_object
*tag
)
133 size_t size
= sizeof(*tag
);
135 size
+= strlen(tag
->tag
);
136 size
+= strlen(tag
->tagger
);
137 size
+= strlen(tag
->tagmsg
);
143 get_size_raw(struct got_raw_object
*raw
)
148 const struct got_error
*
149 got_object_cache_add(struct got_object_cache
*cache
, struct got_object_id
*id
,
152 const struct got_error
*err
= NULL
;
153 struct got_object_cache_entry
*ce
;
157 switch (cache
->type
) {
158 case GOT_OBJECT_CACHE_TYPE_OBJ
:
159 size
= get_size_obj((struct got_object
*)item
);
161 case GOT_OBJECT_CACHE_TYPE_TREE
:
162 size
= get_size_tree((struct got_tree_object
*)item
);
164 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
165 size
= get_size_commit((struct got_commit_object
*)item
);
167 case GOT_OBJECT_CACHE_TYPE_TAG
:
168 size
= get_size_tag((struct got_tag_object
*)item
);
170 case GOT_OBJECT_CACHE_TYPE_RAW
:
171 size
= get_size_raw((struct got_raw_object
*)item
);
174 return got_error(GOT_ERR_OBJ_TYPE
);
177 if (size
> GOT_OBJECT_CACHE_MAX_ELEM_SIZE
) {
178 #ifdef GOT_OBJ_CACHE_DEBUG
180 if (got_object_id_str(&id_str
, id
) != NULL
)
181 return got_error_from_errno("got_object_id_str");
182 fprintf(stderr
, "%s: not caching ", getprogname());
183 switch (cache
->type
) {
184 case GOT_OBJECT_CACHE_TYPE_OBJ
:
185 fprintf(stderr
, "object");
187 case GOT_OBJECT_CACHE_TYPE_TREE
:
188 fprintf(stderr
, "tree");
190 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
191 fprintf(stderr
, "commit");
193 case GOT_OBJECT_CACHE_TYPE_TAG
:
194 fprintf(stderr
, "tag");
196 case GOT_OBJECT_CACHE_TYPE_RAW
:
197 fprintf(stderr
, "raw");
200 fprintf(stderr
, " %s (%zd bytes; %zd MB)\n", id_str
, size
,
204 cache
->cache_toolarge
++;
205 return got_error(GOT_ERR_OBJ_TOO_LARGE
);
208 nelem
= got_object_idset_num_elements(cache
->idset
);
209 if (nelem
>= cache
->size
) {
210 err
= got_object_idset_remove((void **)&ce
,
214 switch (cache
->type
) {
215 case GOT_OBJECT_CACHE_TYPE_OBJ
:
216 got_object_close(ce
->data
.obj
);
218 case GOT_OBJECT_CACHE_TYPE_TREE
:
219 got_object_tree_close(ce
->data
.tree
);
221 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
222 got_object_commit_close(ce
->data
.commit
);
224 case GOT_OBJECT_CACHE_TYPE_TAG
:
225 got_object_tag_close(ce
->data
.tag
);
227 case GOT_OBJECT_CACHE_TYPE_RAW
:
228 got_object_raw_close(ce
->data
.raw
);
231 memset(ce
, 0, sizeof(*ce
));
232 cache
->cache_evict
++;
234 ce
= malloc(sizeof(*ce
));
236 return got_error_from_errno("malloc");
239 memcpy(&ce
->id
, id
, sizeof(ce
->id
));
240 switch (cache
->type
) {
241 case GOT_OBJECT_CACHE_TYPE_OBJ
:
242 ce
->data
.obj
= (struct got_object
*)item
;
244 case GOT_OBJECT_CACHE_TYPE_TREE
:
245 ce
->data
.tree
= (struct got_tree_object
*)item
;
247 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
248 ce
->data
.commit
= (struct got_commit_object
*)item
;
250 case GOT_OBJECT_CACHE_TYPE_TAG
:
251 ce
->data
.tag
= (struct got_tag_object
*)item
;
253 case GOT_OBJECT_CACHE_TYPE_RAW
:
254 ce
->data
.raw
= (struct got_raw_object
*)item
;
258 err
= got_object_idset_add(cache
->idset
, id
, ce
);
261 else if (size
> cache
->max_cached_size
)
262 cache
->max_cached_size
= size
;
267 got_object_cache_get(struct got_object_cache
*cache
, struct got_object_id
*id
)
269 struct got_object_cache_entry
*ce
;
271 cache
->cache_searches
++;
272 ce
= got_object_idset_get(cache
->idset
, id
);
275 switch (cache
->type
) {
276 case GOT_OBJECT_CACHE_TYPE_OBJ
:
278 case GOT_OBJECT_CACHE_TYPE_TREE
:
279 return ce
->data
.tree
;
280 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
281 return ce
->data
.commit
;
282 case GOT_OBJECT_CACHE_TYPE_TAG
:
284 case GOT_OBJECT_CACHE_TYPE_RAW
:
293 #ifdef GOT_OBJ_CACHE_DEBUG
295 print_cache_stats(struct got_object_cache
*cache
, const char *name
)
297 fprintf(stderr
, "%s: %s cache: %d elements, %d searches, %d hits, "
298 "%d missed, %d evicted, %d too large, max cached %zd bytes\n",
300 cache
->idset
? got_object_idset_num_elements(cache
->idset
) : -1,
301 cache
->cache_searches
, cache
->cache_hit
,
302 cache
->cache_miss
, cache
->cache_evict
, cache
->cache_toolarge
,
303 cache
->max_cached_size
);
306 static const struct got_error
*
307 check_refcount(struct got_object_id
*id
, void *data
, void *arg
)
309 struct got_object_cache
*cache
= arg
;
310 struct got_object_cache_entry
*ce
= data
;
311 struct got_object
*obj
;
312 struct got_tree_object
*tree
;
313 struct got_commit_object
*commit
;
314 struct got_tag_object
*tag
;
315 struct got_raw_object
*raw
;
318 if (got_object_id_str(&id_str
, id
) != NULL
)
321 switch (cache
->type
) {
322 case GOT_OBJECT_CACHE_TYPE_OBJ
:
324 if (obj
->refcnt
== 1)
326 fprintf(stderr
, "object %s has %d unclaimed references\n",
327 id_str
, obj
->refcnt
- 1);
329 case GOT_OBJECT_CACHE_TYPE_TREE
:
330 tree
= ce
->data
.tree
;
331 if (tree
->refcnt
== 1)
333 fprintf(stderr
, "tree %s has %d unclaimed references\n",
334 id_str
, tree
->refcnt
- 1);
336 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
337 commit
= ce
->data
.commit
;
338 if (commit
->refcnt
== 1)
340 fprintf(stderr
, "commit %s has %d unclaimed references\n",
341 id_str
, commit
->refcnt
- 1);
343 case GOT_OBJECT_CACHE_TYPE_TAG
:
345 if (tag
->refcnt
== 1)
347 fprintf(stderr
, "tag %s has %d unclaimed references\n",
348 id_str
, tag
->refcnt
- 1);
350 case GOT_OBJECT_CACHE_TYPE_RAW
:
352 if (raw
->refcnt
== 1)
354 fprintf(stderr
, "raw %s has %d unclaimed references\n",
355 id_str
, raw
->refcnt
- 1);
363 static const struct got_error
*
364 free_entry(struct got_object_id
*id
, void *data
, void *arg
)
366 struct got_object_cache
*cache
= arg
;
367 struct got_object_cache_entry
*ce
= data
;
369 switch (cache
->type
) {
370 case GOT_OBJECT_CACHE_TYPE_OBJ
:
371 got_object_close(ce
->data
.obj
);
373 case GOT_OBJECT_CACHE_TYPE_TREE
:
374 got_object_tree_close(ce
->data
.tree
);
376 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
377 got_object_commit_close(ce
->data
.commit
);
379 case GOT_OBJECT_CACHE_TYPE_TAG
:
380 got_object_tag_close(ce
->data
.tag
);
382 case GOT_OBJECT_CACHE_TYPE_RAW
:
383 got_object_raw_close(ce
->data
.raw
);
393 got_object_cache_close(struct got_object_cache
*cache
)
395 #ifdef GOT_OBJ_CACHE_DEBUG
396 switch (cache
->type
) {
397 case GOT_OBJECT_CACHE_TYPE_OBJ
:
398 print_cache_stats(cache
, "object");
400 case GOT_OBJECT_CACHE_TYPE_TREE
:
401 print_cache_stats(cache
, "tree");
403 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
404 print_cache_stats(cache
, "commit");
406 case GOT_OBJECT_CACHE_TYPE_TAG
:
407 print_cache_stats(cache
, "tag");
409 case GOT_OBJECT_CACHE_TYPE_RAW
:
410 print_cache_stats(cache
, "raw");
415 got_object_idset_for_each(cache
->idset
, check_refcount
, cache
);
419 got_object_idset_for_each(cache
->idset
, free_entry
, cache
);
420 got_object_idset_free(cache
->idset
);