File: object_cache.c

package info (click to toggle)
got 0.119-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,448 kB
  • sloc: ansic: 124,378; sh: 50,814; yacc: 4,353; makefile: 2,241; perl: 357
file content (424 lines) | stat: -rw-r--r-- 10,733 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/time.h>
#include <sys/queue.h>
#include <sys/resource.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <zlib.h>

#include "got_compat.h"
#include "got_error.h"
#include "got_object.h"

#include "got_lib_delta.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
#include "got_lib_object_idset.h"
#include "got_lib_object_cache.h"

/*
 * XXX This should be reworked to track cache size and usage in bytes,
 * rather than tracking N elements capped to a maximum element size.
 */
#define GOT_OBJECT_CACHE_SIZE_OBJ	256
#define GOT_OBJECT_CACHE_SIZE_TREE	256
#define GOT_OBJECT_CACHE_SIZE_COMMIT	64
#define GOT_OBJECT_CACHE_SIZE_TAG	256
#define GOT_OBJECT_CACHE_SIZE_RAW	16
#define GOT_OBJECT_CACHE_MAX_ELEM_SIZE	1048576	/* 1 MB */

const struct got_error *
got_object_cache_init(struct got_object_cache *cache,
    enum got_object_cache_type type)
{
	struct rlimit rl;

	memset(cache, 0, sizeof(*cache));

	cache->idset = got_object_idset_alloc();
	if (cache->idset == NULL)
		return got_error_from_errno("got_object_idset_alloc");

	cache->type = type;
	switch (type) {
	case GOT_OBJECT_CACHE_TYPE_OBJ:
		cache->size = GOT_OBJECT_CACHE_SIZE_OBJ;
		break;
	case GOT_OBJECT_CACHE_TYPE_TREE:
		cache->size = GOT_OBJECT_CACHE_SIZE_TREE;
		break;
	case GOT_OBJECT_CACHE_TYPE_COMMIT:
		cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
		break;
	case GOT_OBJECT_CACHE_TYPE_TAG:
		cache->size = GOT_OBJECT_CACHE_SIZE_TAG;
		break;
	case GOT_OBJECT_CACHE_TYPE_RAW:
		if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
			return got_error_from_errno("getrlimit");
		cache->size = GOT_OBJECT_CACHE_SIZE_RAW;
		if (cache->size > rl.rlim_cur / 16)
			cache->size = rl.rlim_cur / 16;
		break;
	}
	return NULL;
}

static size_t
get_size_obj(struct got_object *obj)
{
	size_t size = sizeof(*obj);
	struct got_delta *delta;

	if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
		return size;

	STAILQ_FOREACH(delta, &obj->deltas.entries, entry) {
		if (SIZE_MAX - sizeof(*delta) < size)
			return SIZE_MAX;
		size += sizeof(*delta);
	}

	return size;
}

static size_t
get_size_tree(struct got_tree_object *tree)
{
	size_t size = sizeof(*tree);

	size += sizeof(struct got_tree_entry) * tree->nentries;
	return size;
}

static size_t
get_size_commit(struct got_commit_object *commit)
{
	size_t size = sizeof(*commit);
	struct got_object_qid *qid;

	size += sizeof(*commit->tree_id);
	size += strlen(commit->author);
	size += strlen(commit->committer);
	size += strlen(commit->logmsg);

	STAILQ_FOREACH(qid, &commit->parent_ids, entry)
		size += sizeof(*qid) + sizeof(qid->id);

	return size;
}

static size_t
get_size_tag(struct got_tag_object *tag)
{
	size_t size = sizeof(*tag);

	size += strlen(tag->tag);
	size += strlen(tag->tagger);
	size += strlen(tag->tagmsg);

	return size;
}

static size_t
get_size_raw(struct got_raw_object *raw)
{
	return sizeof(*raw);
}

const struct got_error *
got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id,
    void *item)
{
	const struct got_error *err = NULL;
	struct got_object_cache_entry *ce;
	int nelem;
	size_t size;

	switch (cache->type) {
	case GOT_OBJECT_CACHE_TYPE_OBJ:
		size = get_size_obj((struct got_object *)item);
		break;
	case GOT_OBJECT_CACHE_TYPE_TREE:
		size = get_size_tree((struct got_tree_object *)item);
		break;
	case GOT_OBJECT_CACHE_TYPE_COMMIT:
		size = get_size_commit((struct got_commit_object *)item);
		break;
	case GOT_OBJECT_CACHE_TYPE_TAG:
		size = get_size_tag((struct got_tag_object *)item);
		break;
	case GOT_OBJECT_CACHE_TYPE_RAW:
		size = get_size_raw((struct got_raw_object *)item);
		break;
	default:
		return got_error(GOT_ERR_OBJ_TYPE);
	}

	if (size > GOT_OBJECT_CACHE_MAX_ELEM_SIZE) {
#ifdef GOT_OBJ_CACHE_DEBUG
		char *id_str;
		if (got_object_id_str(&id_str, id) != NULL)
			return got_error_from_errno("got_object_id_str");
		fprintf(stderr, "%s: not caching ", getprogname());
		switch (cache->type) {
		case GOT_OBJECT_CACHE_TYPE_OBJ:
			fprintf(stderr, "object");
			break;
		case GOT_OBJECT_CACHE_TYPE_TREE:
			fprintf(stderr, "tree");
			break;
		case GOT_OBJECT_CACHE_TYPE_COMMIT:
			fprintf(stderr, "commit");
			break;
		case GOT_OBJECT_CACHE_TYPE_TAG:
			fprintf(stderr, "tag");
			break;
		case GOT_OBJECT_CACHE_TYPE_RAW:
			fprintf(stderr, "raw");
			break;
		}
		fprintf(stderr, " %s (%zd bytes; %zd MB)\n", id_str, size,
		    size/1024/1024);
		free(id_str);
#endif
		cache->cache_toolarge++;
		return got_error(GOT_ERR_OBJ_TOO_LARGE);
	}

	nelem = got_object_idset_num_elements(cache->idset);
	if (nelem >= cache->size) {
		err = got_object_idset_remove((void **)&ce,
		    cache->idset, NULL);
		if (err)
			return err;
		switch (cache->type) {
		case GOT_OBJECT_CACHE_TYPE_OBJ:
			got_object_close(ce->data.obj);
			break;
		case GOT_OBJECT_CACHE_TYPE_TREE:
			got_object_tree_close(ce->data.tree);
			break;
		case GOT_OBJECT_CACHE_TYPE_COMMIT:
			got_object_commit_close(ce->data.commit);
			break;
		case GOT_OBJECT_CACHE_TYPE_TAG:
			got_object_tag_close(ce->data.tag);
			break;
		case GOT_OBJECT_CACHE_TYPE_RAW:
			got_object_raw_close(ce->data.raw);
			break;
		}
		memset(ce, 0, sizeof(*ce));
		cache->cache_evict++;
	} else {
		ce = malloc(sizeof(*ce));
		if (ce == NULL)
			return got_error_from_errno("malloc");
	}

	memcpy(&ce->id, id, sizeof(ce->id));
	switch (cache->type) {
	case GOT_OBJECT_CACHE_TYPE_OBJ:
		ce->data.obj = (struct got_object *)item;
		break;
	case GOT_OBJECT_CACHE_TYPE_TREE:
		ce->data.tree = (struct got_tree_object *)item;
		break;
	case GOT_OBJECT_CACHE_TYPE_COMMIT:
		ce->data.commit = (struct got_commit_object *)item;
		break;
	case GOT_OBJECT_CACHE_TYPE_TAG:
		ce->data.tag = (struct got_tag_object *)item;
		break;
	case GOT_OBJECT_CACHE_TYPE_RAW:
		ce->data.raw = (struct got_raw_object *)item;
		break;
	}

	err = got_object_idset_add(cache->idset, id, ce);
	if (err)
		free(ce);
	else if (size > cache->max_cached_size)
		cache->max_cached_size = size;
	return err;
}

void *
got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
{
	struct got_object_cache_entry *ce;

	cache->cache_searches++;
	ce = got_object_idset_get(cache->idset, id);
	if (ce) {
		cache->cache_hit++;
		switch (cache->type) {
		case GOT_OBJECT_CACHE_TYPE_OBJ:
			return ce->data.obj;
		case GOT_OBJECT_CACHE_TYPE_TREE:
			return ce->data.tree;
		case GOT_OBJECT_CACHE_TYPE_COMMIT:
			return ce->data.commit;
		case GOT_OBJECT_CACHE_TYPE_TAG:
			return ce->data.tag;
		case GOT_OBJECT_CACHE_TYPE_RAW:
			return ce->data.raw;
		}
	}

	cache->cache_miss++;
	return NULL;
}

#ifdef GOT_OBJ_CACHE_DEBUG
static void
print_cache_stats(struct got_object_cache *cache, const char *name)
{
	fprintf(stderr, "%s: %s cache: %d elements, %d searches, %d hits, "
	    "%d missed, %d evicted, %d too large, max cached %zd bytes\n",
	    getprogname(), name,
	    cache->idset ? got_object_idset_num_elements(cache->idset) : -1,
	    cache->cache_searches, cache->cache_hit,
	    cache->cache_miss, cache->cache_evict, cache->cache_toolarge,
	    cache->max_cached_size);
}

static const struct got_error *
check_refcount(struct got_object_id *id, void *data, void *arg)
{
	struct got_object_cache *cache = arg;
	struct got_object_cache_entry *ce = data;
	struct got_object *obj;
	struct got_tree_object *tree;
	struct got_commit_object *commit;
	struct got_tag_object *tag;
	struct got_raw_object *raw;
	char *id_str;

	if (got_object_id_str(&id_str, id) != NULL)
		return NULL;

	switch (cache->type) {
	case GOT_OBJECT_CACHE_TYPE_OBJ:
		obj = ce->data.obj;
		if (obj->refcnt == 1)
			break;
		fprintf(stderr, "object %s has %d unclaimed references\n",
		    id_str, obj->refcnt - 1);
		break;
	case GOT_OBJECT_CACHE_TYPE_TREE:
		tree = ce->data.tree;
		if (tree->refcnt == 1)
			break;
		fprintf(stderr, "tree %s has %d unclaimed references\n",
		    id_str, tree->refcnt - 1);
		break;
	case GOT_OBJECT_CACHE_TYPE_COMMIT:
		commit = ce->data.commit;
		if (commit->refcnt == 1)
			break;
		fprintf(stderr, "commit %s has %d unclaimed references\n",
		    id_str, commit->refcnt - 1);
		break;
	case GOT_OBJECT_CACHE_TYPE_TAG:
		tag = ce->data.tag;
		if (tag->refcnt == 1)
			break;
		fprintf(stderr, "tag %s has %d unclaimed references\n",
		    id_str, tag->refcnt - 1);
		break;
	case GOT_OBJECT_CACHE_TYPE_RAW:
		raw = ce->data.raw;
		if (raw->refcnt == 1)
			break;
		fprintf(stderr, "raw %s has %d unclaimed references\n",
		    id_str, raw->refcnt - 1);
		break;
	}
	free(id_str);
	return NULL;
}
#endif

static const struct got_error *
free_entry(struct got_object_id *id, void *data, void *arg)
{
	struct got_object_cache *cache = arg;
	struct got_object_cache_entry *ce = data;

	switch (cache->type) {
	case GOT_OBJECT_CACHE_TYPE_OBJ:
		got_object_close(ce->data.obj);
		break;
	case GOT_OBJECT_CACHE_TYPE_TREE:
		got_object_tree_close(ce->data.tree);
		break;
	case GOT_OBJECT_CACHE_TYPE_COMMIT:
		got_object_commit_close(ce->data.commit);
		break;
	case GOT_OBJECT_CACHE_TYPE_TAG:
		got_object_tag_close(ce->data.tag);
		break;
	case GOT_OBJECT_CACHE_TYPE_RAW:
		got_object_raw_close(ce->data.raw);
		break;
	}

	free(ce);

	return NULL;
}

void
got_object_cache_close(struct got_object_cache *cache)
{
#ifdef GOT_OBJ_CACHE_DEBUG
	switch (cache->type) {
	case GOT_OBJECT_CACHE_TYPE_OBJ:
		print_cache_stats(cache, "object");
		break;
	case GOT_OBJECT_CACHE_TYPE_TREE:
		print_cache_stats(cache, "tree");
		break;
	case GOT_OBJECT_CACHE_TYPE_COMMIT:
		print_cache_stats(cache, "commit");
		break;
	case GOT_OBJECT_CACHE_TYPE_TAG:
		print_cache_stats(cache, "tag");
		break;
	case GOT_OBJECT_CACHE_TYPE_RAW:
		print_cache_stats(cache, "raw");
		break;
	}

	if (cache->idset)
		got_object_idset_for_each(cache->idset, check_refcount, cache);
#endif

	if (cache->idset) {
		got_object_idset_for_each(cache->idset, free_entry, cache);
		got_object_idset_free(cache->idset);
		cache->idset = NULL;
	}
	cache->size = 0;
}