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.
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_lib_delta.h"
30 #include "got_lib_inflate.h"
31 #include "got_lib_object.h"
32 #include "got_lib_object_idset.h"
33 #include "got_lib_object_cache.h"
36 * XXX This should be reworked to track cache size and usage in bytes,
37 * rather than tracking N elements capped to a maximum element size.
39 #define GOT_OBJECT_CACHE_SIZE_OBJ 256
40 #define GOT_OBJECT_CACHE_SIZE_TREE 256
41 #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
42 #define GOT_OBJECT_CACHE_SIZE_TAG 2048
43 #define GOT_OBJECT_CACHE_MAX_ELEM_SIZE 1048576 /* 1 MB */
45 const struct got_error
*
46 got_object_cache_init(struct got_object_cache
*cache
,
47 enum got_object_cache_type type
)
49 memset(cache
, 0, sizeof(*cache
));
51 cache
->idset
= got_object_idset_alloc();
52 if (cache
->idset
== NULL
)
53 return got_error_from_errno("got_object_idset_alloc");
57 case GOT_OBJECT_CACHE_TYPE_OBJ
:
58 cache
->size
= GOT_OBJECT_CACHE_SIZE_OBJ
;
60 case GOT_OBJECT_CACHE_TYPE_TREE
:
61 cache
->size
= GOT_OBJECT_CACHE_SIZE_TREE
;
63 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
64 cache
->size
= GOT_OBJECT_CACHE_SIZE_COMMIT
;
66 case GOT_OBJECT_CACHE_TYPE_TAG
:
67 cache
->size
= GOT_OBJECT_CACHE_SIZE_TAG
;
74 get_size_obj(struct got_object
*obj
)
76 size_t size
= sizeof(*obj
);
77 struct got_delta
*delta
;
79 if ((obj
->flags
& GOT_OBJ_FLAG_DELTIFIED
) == 0)
82 STAILQ_FOREACH(delta
, &obj
->deltas
.entries
, entry
) {
83 if (SIZE_MAX
- sizeof(*delta
) < size
)
85 size
+= sizeof(*delta
);
92 get_size_tree(struct got_tree_object
*tree
)
94 size_t size
= sizeof(*tree
);
96 size
+= sizeof(struct got_tree_entry
) * tree
->nentries
;
101 get_size_commit(struct got_commit_object
*commit
)
103 size_t size
= sizeof(*commit
);
104 struct got_object_qid
*qid
;
106 size
+= sizeof(*commit
->tree_id
);
107 size
+= strlen(commit
->author
);
108 size
+= strlen(commit
->committer
);
109 size
+= strlen(commit
->logmsg
);
111 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
)
112 size
+= sizeof(*qid
) + sizeof(*qid
->id
);
118 get_size_tag(struct got_tag_object
*tag
)
120 size_t size
= sizeof(*tag
);
122 size
+= strlen(tag
->tag
);
123 size
+= strlen(tag
->tagger
);
124 size
+= strlen(tag
->tagmsg
);
129 const struct got_error
*
130 got_object_cache_add(struct got_object_cache
*cache
, struct got_object_id
*id
, void *item
)
132 const struct got_error
*err
= NULL
;
133 struct got_object_cache_entry
*ce
;
137 switch (cache
->type
) {
138 case GOT_OBJECT_CACHE_TYPE_OBJ
:
139 size
= get_size_obj((struct got_object
*)item
);
141 case GOT_OBJECT_CACHE_TYPE_TREE
:
142 size
= get_size_tree((struct got_tree_object
*)item
);
144 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
145 size
= get_size_commit((struct got_commit_object
*)item
);
147 case GOT_OBJECT_CACHE_TYPE_TAG
:
148 size
= get_size_tag((struct got_tag_object
*)item
);
151 return got_error(GOT_ERR_OBJ_TYPE
);
154 if (size
> GOT_OBJECT_CACHE_MAX_ELEM_SIZE
) {
155 #ifdef GOT_OBJ_CACHE_DEBUG
157 if (got_object_id_str(&id_str
, id
) != NULL
)
158 return got_error_from_errno("got_object_id_str");
159 fprintf(stderr
, "%s: not caching ", getprogname());
160 switch (cache
->type
) {
161 case GOT_OBJECT_CACHE_TYPE_OBJ
:
162 fprintf(stderr
, "object");
164 case GOT_OBJECT_CACHE_TYPE_TREE
:
165 fprintf(stderr
, "tree");
167 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
168 fprintf(stderr
, "commit");
170 case GOT_OBJECT_CACHE_TYPE_TAG
:
171 fprintf(stderr
, "tag");
174 fprintf(stderr
, " %s (%zd bytes; %zd MB)\n", id_str
, size
,
178 cache
->cache_toolarge
++;
179 return got_error(GOT_ERR_OBJ_TOO_LARGE
);
182 nelem
= got_object_idset_num_elements(cache
->idset
);
183 if (nelem
>= cache
->size
) {
184 err
= got_object_idset_remove((void **)&ce
,
188 switch (cache
->type
) {
189 case GOT_OBJECT_CACHE_TYPE_OBJ
:
190 got_object_close(ce
->data
.obj
);
192 case GOT_OBJECT_CACHE_TYPE_TREE
:
193 got_object_tree_close(ce
->data
.tree
);
195 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
196 got_object_commit_close(ce
->data
.commit
);
198 case GOT_OBJECT_CACHE_TYPE_TAG
:
199 got_object_tag_close(ce
->data
.tag
);
203 cache
->cache_evict
++;
206 ce
= malloc(sizeof(*ce
));
208 return got_error_from_errno("malloc");
209 memcpy(&ce
->id
, id
, sizeof(ce
->id
));
210 switch (cache
->type
) {
211 case GOT_OBJECT_CACHE_TYPE_OBJ
:
212 ce
->data
.obj
= (struct got_object
*)item
;
214 case GOT_OBJECT_CACHE_TYPE_TREE
:
215 ce
->data
.tree
= (struct got_tree_object
*)item
;
217 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
218 ce
->data
.commit
= (struct got_commit_object
*)item
;
220 case GOT_OBJECT_CACHE_TYPE_TAG
:
221 ce
->data
.tag
= (struct got_tag_object
*)item
;
225 err
= got_object_idset_add(cache
->idset
, id
, ce
);
232 got_object_cache_get(struct got_object_cache
*cache
, struct got_object_id
*id
)
234 struct got_object_cache_entry
*ce
;
236 cache
->cache_searches
++;
237 ce
= got_object_idset_get(cache
->idset
, id
);
240 switch (cache
->type
) {
241 case GOT_OBJECT_CACHE_TYPE_OBJ
:
243 case GOT_OBJECT_CACHE_TYPE_TREE
:
244 return ce
->data
.tree
;
245 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
246 return ce
->data
.commit
;
247 case GOT_OBJECT_CACHE_TYPE_TAG
:
256 #ifdef GOT_OBJ_CACHE_DEBUG
258 print_cache_stats(struct got_object_cache
*cache
, const char *name
)
260 fprintf(stderr
, "%s: %s cache: %d elements, %d searches, %d hits, "
261 "%d missed, %d evicted, %d too large\n", getprogname(), name
,
262 got_object_idset_num_elements(cache
->idset
),
263 cache
->cache_searches
, cache
->cache_hit
,
264 cache
->cache_miss
, cache
->cache_evict
, cache
->cache_toolarge
);
267 const struct got_error
*
268 check_refcount(struct got_object_id
*id
, void *data
, void *arg
)
270 struct got_object_cache
*cache
= arg
;
271 struct got_object_cache_entry
*ce
= data
;
272 struct got_object
*obj
;
273 struct got_tree_object
*tree
;
274 struct got_commit_object
*commit
;
275 struct got_tag_object
*tag
;
278 if (got_object_id_str(&id_str
, id
) != NULL
)
281 switch (cache
->type
) {
282 case GOT_OBJECT_CACHE_TYPE_OBJ
:
284 if (obj
->refcnt
== 1)
286 fprintf(stderr
, "object %s has %d unclaimed references\n",
287 id_str
, obj
->refcnt
- 1);
289 case GOT_OBJECT_CACHE_TYPE_TREE
:
290 tree
= ce
->data
.tree
;
291 if (tree
->refcnt
== 1)
293 fprintf(stderr
, "tree %s has %d unclaimed references\n",
294 id_str
, tree
->refcnt
- 1);
296 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
297 commit
= ce
->data
.commit
;
298 if (commit
->refcnt
== 1)
300 fprintf(stderr
, "commit %s has %d unclaimed references\n",
301 id_str
, commit
->refcnt
- 1);
303 case GOT_OBJECT_CACHE_TYPE_TAG
:
305 if (tag
->refcnt
== 1)
307 fprintf(stderr
, "tag %s has %d unclaimed references\n",
308 id_str
, tag
->refcnt
- 1);
317 got_object_cache_close(struct got_object_cache
*cache
)
319 #ifdef GOT_OBJ_CACHE_DEBUG
320 switch (cache
->type
) {
321 case GOT_OBJECT_CACHE_TYPE_OBJ
:
322 print_cache_stats(cache
, "object");
324 case GOT_OBJECT_CACHE_TYPE_TREE
:
325 print_cache_stats(cache
, "tree");
327 case GOT_OBJECT_CACHE_TYPE_COMMIT
:
328 print_cache_stats(cache
, "commit");
330 case GOT_OBJECT_CACHE_TYPE_TAG
:
331 print_cache_stats(cache
, "tag");
335 got_object_idset_for_each(cache
->idset
, check_refcount
, cache
);
339 got_object_idset_free(cache
->idset
);