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 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 size_t size;
45 memset(cache, 0, sizeof(*cache));
47 switch (type) {
48 case GOT_OBJECT_CACHE_TYPE_OBJ:
49 size = GOT_OBJECT_CACHE_SIZE_OBJ;
50 break;
51 case GOT_OBJECT_CACHE_TYPE_TREE:
52 size = GOT_OBJECT_CACHE_SIZE_TREE;
53 break;
54 case GOT_OBJECT_CACHE_TYPE_COMMIT:
55 size = GOT_OBJECT_CACHE_SIZE_COMMIT;
56 break;
57 }
59 cache->idcache = got_object_idcache_alloc(size);
60 if (cache->idcache == NULL)
61 return got_error_from_errno();
62 cache->type = type;
63 cache->size = size;
64 return NULL;
65 }
67 const struct got_error *
68 got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
69 {
70 const struct got_error *err = NULL;
71 struct got_object_cache_entry *ce;
72 int nelem;
74 nelem = got_object_idcache_num_elements(cache->idcache);
75 if (nelem >= cache->size) {
76 err = got_object_idcache_remove_least_used((void **)&ce,
77 cache->idcache);
78 if (err)
79 return err;
80 switch (cache->type) {
81 case GOT_OBJECT_CACHE_TYPE_OBJ:
82 got_object_close(ce->data.obj);
83 break;
84 case GOT_OBJECT_CACHE_TYPE_TREE:
85 got_object_tree_close(ce->data.tree);
86 break;
87 case GOT_OBJECT_CACHE_TYPE_COMMIT:
88 got_object_commit_close(ce->data.commit);
89 break;
90 }
91 free(ce);
92 cache->cache_evict++;
93 }
95 ce = calloc(1, sizeof(*ce));
96 if (ce == NULL)
97 return got_error_from_errno();
98 memcpy(&ce->id, id, sizeof(ce->id));
99 switch (cache->type) {
100 case GOT_OBJECT_CACHE_TYPE_OBJ:
101 ce->data.obj = (struct got_object *)item;
102 break;
103 case GOT_OBJECT_CACHE_TYPE_TREE:
104 ce->data.tree = (struct got_tree_object *)item;
105 break;
106 case GOT_OBJECT_CACHE_TYPE_COMMIT:
107 ce->data.commit = (struct got_commit_object *)item;
108 break;
111 err = got_object_idcache_add(cache->idcache, id, ce);
112 if (err) {
113 if (err->code == GOT_ERR_OBJ_EXISTS) {
114 free(ce);
115 err = NULL;
118 return err;
121 void *
122 got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
124 struct got_object_cache_entry *ce;
126 cache->cache_searches++;
127 ce = got_object_idcache_get(cache->idcache, id);
128 if (ce) {
129 cache->cache_hit++;
130 switch (cache->type) {
131 case GOT_OBJECT_CACHE_TYPE_OBJ:
132 return ce->data.obj;
133 case GOT_OBJECT_CACHE_TYPE_TREE:
134 return ce->data.tree;
135 case GOT_OBJECT_CACHE_TYPE_COMMIT:
136 return ce->data.commit;
140 cache->cache_miss++;
141 return NULL;
144 #ifdef GOT_OBJ_CACHE_DEBUG
145 static void
146 print_cache_stats(struct got_object_cache *cache, const char *name)
148 fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
149 "%d missed, %d evicted\n", getprogname(), name,
150 got_object_idcache_num_elements(cache->idcache),
151 cache->cache_searches, cache->cache_hit,
152 cache->cache_miss, cache->cache_evict);
155 void check_refcount(struct got_object_id *id, void *data, void *arg)
157 struct got_object_cache *cache = arg;
158 struct got_object_cache_entry *ce = data;
159 struct got_object *obj;
160 struct got_tree_object *tree;
161 struct got_commit_object *commit;
162 char *id_str;
164 if (got_object_id_str(&id_str, id) != NULL)
165 return;
167 switch (cache->type) {
168 case GOT_OBJECT_CACHE_TYPE_OBJ:
169 obj = ce->data.obj;
170 if (obj->refcnt == 1)
171 break;
172 fprintf(stderr, "object %s has %d unclaimed references\n",
173 id_str, obj->refcnt - 1);
174 break;
175 case GOT_OBJECT_CACHE_TYPE_TREE:
176 tree = ce->data.tree;
177 if (tree->refcnt == 1)
178 break;
179 fprintf(stderr, "tree %s has %d unclaimed references\n",
180 id_str, tree->refcnt - 1);
181 break;
182 case GOT_OBJECT_CACHE_TYPE_COMMIT:
183 commit = ce->data.commit;
184 if (commit->refcnt == 1)
185 break;
186 fprintf(stderr, "commit %s has %d unclaimed references\n",
187 id_str, commit->refcnt - 1);
188 break;
190 free(id_str);
192 #endif
194 void
195 got_object_cache_close(struct got_object_cache *cache)
197 #ifdef GOT_OBJ_CACHE_DEBUG
198 switch (cache->type) {
199 case GOT_OBJECT_CACHE_TYPE_OBJ:
200 print_cache_stats(cache, "object");
201 break;
202 case GOT_OBJECT_CACHE_TYPE_TREE:
203 print_cache_stats(cache, "tree");
204 break;
205 case GOT_OBJECT_CACHE_TYPE_COMMIT:
206 print_cache_stats(cache, "commit");
207 break;
210 got_object_idcache_for_each(cache->idcache, check_refcount, cache);
211 #endif
213 if (cache->idcache) {
214 got_object_idcache_free(cache->idcache);
215 cache->idcache = NULL;
217 cache->size = 0;