Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/time.h>
18 #include <sys/queue.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <zlib.h>
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"
35 #define GOT_OBJECT_CACHE_SIZE_OBJ 256
36 #define GOT_OBJECT_CACHE_SIZE_TREE 256
37 #define GOT_OBJECT_CACHE_SIZE_COMMIT 64
39 const struct got_error *
40 got_object_cache_init(struct got_object_cache *cache,
41 enum got_object_cache_type type)
42 {
43 memset(cache, 0, sizeof(*cache));
45 cache->idset = got_object_idset_alloc();
46 if (cache->idset == NULL)
47 return got_error_from_errno();
49 cache->type = type;
50 switch (type) {
51 case GOT_OBJECT_CACHE_TYPE_OBJ:
52 cache->size = GOT_OBJECT_CACHE_SIZE_OBJ;
53 break;
54 case GOT_OBJECT_CACHE_TYPE_TREE:
55 cache->size = GOT_OBJECT_CACHE_SIZE_TREE;
56 break;
57 case GOT_OBJECT_CACHE_TYPE_COMMIT:
58 cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
59 break;
60 }
61 return NULL;
62 }
64 const struct got_error *
65 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
66 {
67 const struct got_error *err = NULL;
68 struct got_object_cache_entry *ce;
69 int nelem;
71 nelem = got_object_idset_num_elements(cache->idset);
72 if (nelem >= cache->size) {
73 err = got_object_idset_remove((void **)&ce,
74 cache->idset, NULL);
75 if (err)
76 return err;
77 switch (cache->type) {
78 case GOT_OBJECT_CACHE_TYPE_OBJ:
79 got_object_close(ce->data.obj);
80 break;
81 case GOT_OBJECT_CACHE_TYPE_TREE:
82 got_object_tree_close(ce->data.tree);
83 break;
84 case GOT_OBJECT_CACHE_TYPE_COMMIT:
85 got_object_commit_close(ce->data.commit);
86 break;
87 }
88 free(ce);
89 cache->cache_evict++;
90 }
92 ce = malloc(sizeof(*ce));
93 if (ce == NULL)
94 return got_error_from_errno();
95 memcpy(&ce->id, id, sizeof(ce->id));
96 switch (cache->type) {
97 case GOT_OBJECT_CACHE_TYPE_OBJ:
98 ce->data.obj = (struct got_object *)item;
99 break;
100 case GOT_OBJECT_CACHE_TYPE_TREE:
101 ce->data.tree = (struct got_tree_object *)item;
102 break;
103 case GOT_OBJECT_CACHE_TYPE_COMMIT:
104 ce->data.commit = (struct got_commit_object *)item;
105 break;
108 err = got_object_idset_add(cache->idset, id, ce);
109 if (err) {
110 if (err->code == GOT_ERR_OBJ_EXISTS) {
111 free(ce);
112 err = NULL;
115 return err;
118 void *
119 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
121 struct got_object_cache_entry *ce;
123 cache->cache_searches++;
124 ce = got_object_idset_get(cache->idset, id);
125 if (ce) {
126 cache->cache_hit++;
127 switch (cache->type) {
128 case GOT_OBJECT_CACHE_TYPE_OBJ:
129 return ce->data.obj;
130 case GOT_OBJECT_CACHE_TYPE_TREE:
131 return ce->data.tree;
132 case GOT_OBJECT_CACHE_TYPE_COMMIT:
133 return ce->data.commit;
137 cache->cache_miss++;
138 return NULL;
141 #ifdef GOT_OBJ_CACHE_DEBUG
142 static void
143 print_cache_stats(struct got_object_cache *cache, const char *name)
145 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
146 "%d missed, %d evicted\n", getprogname(), name,
147 got_object_idset_num_elements(cache->idset),
148 cache->cache_searches, cache->cache_hit,
149 cache->cache_miss, cache->cache_evict);
152 const struct got_error *
153 check_refcount(struct got_object_id *id, void *data, void *arg)
155 struct got_object_cache *cache = arg;
156 struct got_object_cache_entry *ce = data;
157 struct got_object *obj;
158 struct got_tree_object *tree;
159 struct got_commit_object *commit;
160 char *id_str;
162 if (got_object_id_str(&id_str, id) != NULL)
163 return NULL;
165 switch (cache->type) {
166 case GOT_OBJECT_CACHE_TYPE_OBJ:
167 obj = ce->data.obj;
168 if (obj->refcnt == 1)
169 break;
170 fprintf(stderr, "object %s has %d unclaimed references\n",
171 id_str, obj->refcnt - 1);
172 break;
173 case GOT_OBJECT_CACHE_TYPE_TREE:
174 tree = ce->data.tree;
175 if (tree->refcnt == 1)
176 break;
177 fprintf(stderr, "tree %s has %d unclaimed references\n",
178 id_str, tree->refcnt - 1);
179 break;
180 case GOT_OBJECT_CACHE_TYPE_COMMIT:
181 commit = ce->data.commit;
182 if (commit->refcnt == 1)
183 break;
184 fprintf(stderr, "commit %s has %d unclaimed references\n",
185 id_str, commit->refcnt - 1);
186 break;
188 free(id_str);
189 return NULL;
191 #endif
193 void
194 got_object_cache_close(struct got_object_cache *cache)
196 #ifdef GOT_OBJ_CACHE_DEBUG
197 switch (cache->type) {
198 case GOT_OBJECT_CACHE_TYPE_OBJ:
199 print_cache_stats(cache, "object");
200 break;
201 case GOT_OBJECT_CACHE_TYPE_TREE:
202 print_cache_stats(cache, "tree");
203 break;
204 case GOT_OBJECT_CACHE_TYPE_COMMIT:
205 print_cache_stats(cache, "commit");
206 break;
209 got_object_idset_for_each(cache->idset, check_refcount, cache);
210 #endif
212 if (cache->idset) {
213 got_object_idset_free(cache->idset);
214 cache->idset = NULL;
216 cache->size = 0;