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 128
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 }
92 ce = calloc(1, 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_idcache_add(cache->idcache, 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 ce = got_object_idcache_get(cache->idcache, id);
124 if (ce) {
125 cache->cache_hit++;
126 switch (cache->type) {
127 case GOT_OBJECT_CACHE_TYPE_OBJ:
128 return ce->data.obj;
129 case GOT_OBJECT_CACHE_TYPE_TREE:
130 return ce->data.tree;
131 case GOT_OBJECT_CACHE_TYPE_COMMIT:
132 return ce->data.commit;
136 cache->cache_miss++;
137 return NULL;
140 #if 0
141 static void
142 print_cache_stats(struct got_object_cache *cache, const char *name)
144 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
145 name, got_object_idcache_num_elements(cache->idcache),
146 cache->cache_hit, cache->cache_miss);
149 void check_refcount(struct got_object_id *id, void *data, void *arg)
151 struct got_object_cache *cache = arg;
152 struct got_object_cache_entry *ce = data;
153 struct got_object *obj;
154 struct got_tree_object *tree;
155 struct got_commit_object *commit;
156 char *id_str;
158 if (got_object_id_str(&id_str, id) != NULL)
159 return;
161 switch (cache->type) {
162 case GOT_OBJECT_CACHE_TYPE_OBJ:
163 obj = ce->data.obj;
164 if (obj->refcnt == 1)
165 break;
166 fprintf(stderr, "object %s has %d unclaimed references\n",
167 id_str, obj->refcnt - 1);
168 break;
169 case GOT_OBJECT_CACHE_TYPE_TREE:
170 tree = ce->data.tree;
171 if (tree->refcnt == 1)
172 break;
173 fprintf(stderr, "tree %s has %d unclaimed references\n",
174 id_str, tree->refcnt - 1);
175 break;
176 case GOT_OBJECT_CACHE_TYPE_COMMIT:
177 commit = ce->data.commit;
178 if (commit->refcnt == 1)
179 break;
180 fprintf(stderr, "commit %s has %d unclaimed references\n",
181 id_str, commit->refcnt);
182 break;
184 free(id_str);
186 #endif
188 void
189 got_object_cache_close(struct got_object_cache *cache)
191 #if 0
192 switch (cache->type) {
193 case GOT_OBJECT_CACHE_TYPE_OBJ:
194 print_cache_stats(cache, "object");
195 break;
196 case GOT_OBJECT_CACHE_TYPE_TREE:
197 print_cache_stats(cache, "tree");
198 break;
199 case GOT_OBJECT_CACHE_TYPE_COMMIT:
200 print_cache_stats(cache, "commit");
201 break;
204 got_object_idcache_for_each(cache->idcache, check_refcount, cache);
205 #endif
207 if (cache->idcache) {
208 got_object_idcache_free(cache->idcache);
209 cache->idcache = NULL;
211 cache->size = 0;