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_idcache.h"
33 #include "got_lib_object_cache.h"
35 #define GOT_OBJECT_CACHE_SIZE_OBJ 1024
36 #define GOT_OBJECT_CACHE_SIZE_TREE 2048
37 #define GOT_OBJECT_CACHE_SIZE_COMMIT 512
39 const struct got_error *
40 got_object_cache_init(struct got_object_cache *cache,
41 enum got_object_cache_type type)
42 {
43 size_t size;
45 switch (type) {
46 case GOT_OBJECT_CACHE_TYPE_OBJ:
47 size = GOT_OBJECT_CACHE_SIZE_OBJ;
48 break;
49 case GOT_OBJECT_CACHE_TYPE_TREE:
50 size = GOT_OBJECT_CACHE_SIZE_TREE;
51 break;
52 case GOT_OBJECT_CACHE_TYPE_COMMIT:
53 size = GOT_OBJECT_CACHE_SIZE_COMMIT;
54 break;
55 }
57 cache->idcache = got_object_idcache_alloc(size);
58 if (cache->idcache == NULL)
59 return got_error_from_errno();
60 cache->type = type;
61 cache->size = size;
62 return NULL;
63 }
65 const struct got_error *
66 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
67 {
68 const struct got_error *err = NULL;
69 struct got_object_cache_entry *ce;
70 int nelem;
72 nelem = got_object_idcache_num_elements(cache->idcache);
73 if (nelem >= cache->size) {
74 err = got_object_idcache_remove_least_used((void **)&ce,
75 cache->idcache);
76 if (err)
77 return err;
78 switch (cache->type) {
79 case GOT_OBJECT_CACHE_TYPE_OBJ:
80 got_object_close(ce->data.obj);
81 break;
82 case GOT_OBJECT_CACHE_TYPE_TREE:
83 got_object_tree_close(ce->data.tree);
84 break;
85 case GOT_OBJECT_CACHE_TYPE_COMMIT:
86 got_object_commit_close(ce->data.commit);
87 break;
88 }
89 free(ce);
90 cache->cache_evict++;
91 }
93 ce = calloc(1, sizeof(*ce));
94 if (ce == NULL)
95 return got_error_from_errno();
96 memcpy(&ce->id, id, sizeof(ce->id));
97 switch (cache->type) {
98 case GOT_OBJECT_CACHE_TYPE_OBJ:
99 ce->data.obj = (struct got_object *)item;
100 break;
101 case GOT_OBJECT_CACHE_TYPE_TREE:
102 ce->data.tree = (struct got_tree_object *)item;
103 break;
104 case GOT_OBJECT_CACHE_TYPE_COMMIT:
105 ce->data.commit = (struct got_commit_object *)item;
106 break;
109 err = got_object_idcache_add(cache->idcache, id, ce);
110 if (err) {
111 if (err->code == GOT_ERR_OBJ_EXISTS) {
112 free(ce);
113 err = NULL;
116 return err;
119 void *
120 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
122 struct got_object_cache_entry *ce;
124 cache->cache_searches++;
125 ce = got_object_idcache_get(cache->idcache, id);
126 if (ce) {
127 cache->cache_hit++;
128 switch (cache->type) {
129 case GOT_OBJECT_CACHE_TYPE_OBJ:
130 return ce->data.obj;
131 case GOT_OBJECT_CACHE_TYPE_TREE:
132 return ce->data.tree;
133 case GOT_OBJECT_CACHE_TYPE_COMMIT:
134 return ce->data.commit;
138 cache->cache_miss++;
139 return NULL;
142 #ifdef GOT_OBJ_CACHE_DEBUG
143 static void
144 print_cache_stats(struct got_object_cache *cache, const char *name)
146 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
147 "%d missed, %d evicted\n", getprogname(), name,
148 got_object_idcache_num_elements(cache->idcache),
149 cache->cache_searches, cache->cache_hit,
150 cache->cache_miss, cache->cache_evict);
153 void 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;
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);
190 #endif
192 void
193 got_object_cache_close(struct got_object_cache *cache)
195 #ifdef GOT_OBJ_CACHE_DEBUG
196 switch (cache->type) {
197 case GOT_OBJECT_CACHE_TYPE_OBJ:
198 print_cache_stats(cache, "object");
199 break;
200 case GOT_OBJECT_CACHE_TYPE_TREE:
201 print_cache_stats(cache, "tree");
202 break;
203 case GOT_OBJECT_CACHE_TYPE_COMMIT:
204 print_cache_stats(cache, "commit");
205 break;
208 got_object_idcache_for_each(cache->idcache, check_refcount, cache);
209 #endif
211 if (cache->idcache) {
212 got_object_idcache_free(cache->idcache);
213 cache->idcache = NULL;
215 cache->size = 0;