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/resource.h>
27 #include "got_error.h"
28 #include "got_object.h"
30 #include "got_lib_delta.h"
31 #include "got_lib_inflate.h"
32 #include "got_lib_object.h"
33 #include "got_lib_object_idset.h"
34 #include "got_lib_object_cache.h"
37 * XXX This should be reworked to track cache size and usage in bytes,
38 * rather than tracking N elements capped to a maximum element size.
40 #define GOT_OBJECT_CACHE_SIZE_OBJ 256
41 #define GOT_OBJECT_CACHE_SIZE_TREE 256
42 #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
43 #define GOT_OBJECT_CACHE_SIZE_TAG 2048
44 #define GOT_OBJECT_CACHE_SIZE_RAW 64
45 #define GOT_OBJECT_CACHE_MAX_ELEM_SIZE 1048576 /* 1 MB */
47 const struct got_error
*
48 got_object_cache_init(struct got_object_cache
*cache
,
49 enum got_object_cache_type type
)
53 memset(cache
, 0, sizeof(*cache
));
55 cache
->idset
= got_object_idset_alloc();
56 if (cache
->idset
== NULL
)
57 return got_error_from_errno("got_object_idset_alloc");
61 case GOT_OBJECT_CACHE_TYPE_OBJ
:
62 cache
->size
= GOT_OBJECT_CACHE_SIZE_OBJ
;
64 case GOT_OBJECT_CACHE_TYPE_TREE
:
65 cache
->size
= GOT_OBJECT_CACHE_SIZE_TREE
;
67 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
68 cache
->size
= GOT_OBJECT_CACHE_SIZE_COMMIT
;
70 case GOT_OBJECT_CACHE_TYPE_TAG
:
71 cache
->size
= GOT_OBJECT_CACHE_SIZE_TAG
;
73 case GOT_OBJECT_CACHE_TYPE_RAW
:
74 if (getrlimit(RLIMIT_NOFILE
, &rl
) == -1)
75 return got_error_from_errno("getrlimit");
76 cache
->size
= GOT_OBJECT_CACHE_SIZE_RAW
;
77 if (cache
->size
> rl
.rlim_cur
/ 16)
78 cache
->size
= rl
.rlim_cur
/ 16;
85 get_size_obj(struct got_object
*obj
)
87 size_t size
= sizeof(*obj
);
88 struct got_delta
*delta
;
90 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0)
93 STAILQ_FOREACH(delta
, &obj
->deltas
.entries
, entry
) {
94 if (SIZE_MAX
- sizeof(*delta
) < size
)
96 size
+= sizeof(*delta
);
103 get_size_tree(struct got_tree_object
*tree
)
105 size_t size
= sizeof(*tree
);
107 size
+= sizeof(struct got_tree_entry
) * tree
->nentries
;
112 get_size_commit(struct got_commit_object
*commit
)
114 size_t size
= sizeof(*commit
);
115 struct got_object_qid
*qid
;
117 size
+= sizeof(*commit
->tree_id
);
118 size
+= strlen(commit
->author
);
119 size
+= strlen(commit
->committer
);
120 size
+= strlen(commit
->logmsg
);
122 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
)
123 size
+= sizeof(*qid
) + sizeof(*qid
->id
);
129 get_size_tag(struct got_tag_object
*tag
)
131 size_t size
= sizeof(*tag
);
133 size
+= strlen(tag
->tag
);
134 size
+= strlen(tag
->tagger
);
135 size
+= strlen(tag
->tagmsg
);
141 get_size_raw(struct got_raw_object
*raw
)
146 const struct got_error
*
147 got_object_cache_add(struct got_object_cache
*cache
, struct got_object_id
*id
, void *item
)
149 const struct got_error
*err
= NULL
;
150 struct got_object_cache_entry
*ce
;
154 switch (cache
->type
) {
155 case GOT_OBJECT_CACHE_TYPE_OBJ
:
156 size
= get_size_obj((struct got_object
*)item
);
158 case GOT_OBJECT_CACHE_TYPE_TREE
:
159 size
= get_size_tree((struct got_tree_object
*)item
);
161 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
162 size
= get_size_commit((struct got_commit_object
*)item
);
164 case GOT_OBJECT_CACHE_TYPE_TAG
:
165 size
= get_size_tag((struct got_tag_object
*)item
);
167 case GOT_OBJECT_CACHE_TYPE_RAW
:
168 size
= get_size_raw((struct got_raw_object
*)item
);
171 return got_error(GOT_ERR_OBJ_TYPE
);
174 if (size
> GOT_OBJECT_CACHE_MAX_ELEM_SIZE
) {
175 #ifdef GOT_OBJ_CACHE_DEBUG
177 if (got_object_id_str(&id_str
, id
) != NULL
)
178 return got_error_from_errno("got_object_id_str");
179 fprintf(stderr
, "%s: not caching ", getprogname());
180 switch (cache
->type
) {
181 case GOT_OBJECT_CACHE_TYPE_OBJ
:
182 fprintf(stderr
, "object");
184 case GOT_OBJECT_CACHE_TYPE_TREE
:
185 fprintf(stderr
, "tree");
187 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
188 fprintf(stderr
, "commit");
190 case GOT_OBJECT_CACHE_TYPE_TAG
:
191 fprintf(stderr
, "tag");
193 case GOT_OBJECT_CACHE_TYPE_RAW
:
194 fprintf(stderr
, "raw");
197 fprintf(stderr
, " %s (%zd bytes; %zd MB)\n", id_str
, size
,
201 cache
->cache_toolarge
++;
202 return got_error(GOT_ERR_OBJ_TOO_LARGE
);
205 nelem
= got_object_idset_num_elements(cache
->idset
);
206 if (nelem
>= cache
->size
) {
207 err
= got_object_idset_remove((void **)&ce
,
211 switch (cache
->type
) {
212 case GOT_OBJECT_CACHE_TYPE_OBJ
:
213 got_object_close(ce
->data
.obj
);
215 case GOT_OBJECT_CACHE_TYPE_TREE
:
216 got_object_tree_close(ce
->data
.tree
);
218 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
219 got_object_commit_close(ce
->data
.commit
);
221 case GOT_OBJECT_CACHE_TYPE_TAG
:
222 got_object_tag_close(ce
->data
.tag
);
224 case GOT_OBJECT_CACHE_TYPE_RAW
:
225 got_object_raw_close(ce
->data
.raw
);
229 cache
->cache_evict
++;
232 ce
= malloc(sizeof(*ce
));
234 return got_error_from_errno("malloc");
235 memcpy(&ce
->id
, id
, sizeof(ce
->id
));
236 switch (cache
->type
) {
237 case GOT_OBJECT_CACHE_TYPE_OBJ
:
238 ce
->data
.obj
= (struct got_object
*)item
;
240 case GOT_OBJECT_CACHE_TYPE_TREE
:
241 ce
->data
.tree
= (struct got_tree_object
*)item
;
243 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
244 ce
->data
.commit
= (struct got_commit_object
*)item
;
246 case GOT_OBJECT_CACHE_TYPE_TAG
:
247 ce
->data
.tag
= (struct got_tag_object
*)item
;
249 case GOT_OBJECT_CACHE_TYPE_RAW
:
250 ce
->data
.raw
= (struct got_raw_object
*)item
;
254 err
= got_object_idset_add(cache
->idset
, id
, ce
);
261 got_object_cache_get(struct got_object_cache
*cache
, struct got_object_id
*id
)
263 struct got_object_cache_entry
*ce
;
265 cache
->cache_searches
++;
266 ce
= got_object_idset_get(cache
->idset
, id
);
269 switch (cache
->type
) {
270 case GOT_OBJECT_CACHE_TYPE_OBJ
:
272 case GOT_OBJECT_CACHE_TYPE_TREE
:
273 return ce
->data
.tree
;
274 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
275 return ce
->data
.commit
;
276 case GOT_OBJECT_CACHE_TYPE_TAG
:
278 case GOT_OBJECT_CACHE_TYPE_RAW
:
287 #ifdef GOT_OBJ_CACHE_DEBUG
289 print_cache_stats(struct got_object_cache
*cache
, const char *name
)
291 fprintf(stderr
, "%s: %s cache: %d elements, %d searches, %d hits, "
292 "%d missed, %d evicted, %d too large\n", getprogname(), name
,
293 got_object_idset_num_elements(cache
->idset
),
294 cache
->cache_searches
, cache
->cache_hit
,
295 cache
->cache_miss
, cache
->cache_evict
, cache
->cache_toolarge
);
298 const struct got_error
*
299 check_refcount(struct got_object_id
*id
, void *data
, void *arg
)
301 struct got_object_cache
*cache
= arg
;
302 struct got_object_cache_entry
*ce
= data
;
303 struct got_object
*obj
;
304 struct got_tree_object
*tree
;
305 struct got_commit_object
*commit
;
306 struct got_tag_object
*tag
;
307 struct got_raw_object
*raw
;
310 if (got_object_id_str(&id_str
, id
) != NULL
)
313 switch (cache
->type
) {
314 case GOT_OBJECT_CACHE_TYPE_OBJ
:
316 if (obj
->refcnt
== 1)
318 fprintf(stderr
, "object %s has %d unclaimed references\n",
319 id_str
, obj
->refcnt
- 1);
321 case GOT_OBJECT_CACHE_TYPE_TREE
:
322 tree
= ce
->data
.tree
;
323 if (tree
->refcnt
== 1)
325 fprintf(stderr
, "tree %s has %d unclaimed references\n",
326 id_str
, tree
->refcnt
- 1);
328 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
329 commit
= ce
->data
.commit
;
330 if (commit
->refcnt
== 1)
332 fprintf(stderr
, "commit %s has %d unclaimed references\n",
333 id_str
, commit
->refcnt
- 1);
335 case GOT_OBJECT_CACHE_TYPE_TAG
:
337 if (tag
->refcnt
== 1)
339 fprintf(stderr
, "tag %s has %d unclaimed references\n",
340 id_str
, tag
->refcnt
- 1);
342 case GOT_OBJECT_CACHE_TYPE_RAW
:
344 if (raw
->refcnt
== 1)
346 fprintf(stderr
, "raw %s has %d unclaimed references\n",
347 id_str
, raw
->refcnt
- 1);
356 got_object_cache_close(struct got_object_cache
*cache
)
358 #ifdef GOT_OBJ_CACHE_DEBUG
359 switch (cache
->type
) {
360 case GOT_OBJECT_CACHE_TYPE_OBJ
:
361 print_cache_stats(cache
, "object");
363 case GOT_OBJECT_CACHE_TYPE_TREE
:
364 print_cache_stats(cache
, "tree");
366 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
367 print_cache_stats(cache
, "commit");
369 case GOT_OBJECT_CACHE_TYPE_TAG
:
370 print_cache_stats(cache
, "tag");
372 case GOT_OBJECT_CACHE_TYPE_RAW
:
373 print_cache_stats(cache
, "raw");
377 got_object_idset_for_each(cache
->idset
, check_refcount
, cache
);
381 got_object_idset_free(cache
->idset
);