mwindow.c 9.33 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8
 */

#include "mwindow.h"
9

10 11 12
#include "vector.h"
#include "fileops.h"
#include "map.h"
13
#include "global.h"
14 15 16
#include "strmap.h"
#include "pack.h"

17 18
#define DEFAULT_WINDOW_SIZE \
	(sizeof(void*) >= 8 \
Vicent Marti committed
19
		? 1 * 1024 * 1024 * 1024 \
20 21 22
		: 32 * 1024 * 1024)

#define DEFAULT_MAPPED_LIMIT \
23
	((1024 * 1024) * (sizeof(void*) >= 8 ? 8192ULL : 256UL))
24

Vicent Marti committed
25 26
size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE;
size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT;
27

28 29 30
/* Whenever you want to read or modify this, grab git__mwindow_mutex */
static git_mwindow_ctl mem_ctl;

31 32 33
/* Global list of mwindow files, to open packs once across repos */
git_strmap *git__pack_cache = NULL;

34
static void git_mwindow_files_free(void)
35
{
36
	git_strmap *tmp = git__pack_cache;
37

38 39
	git__pack_cache = NULL;
	git_strmap_free(tmp);
40 41
}

42
int git_mwindow_global_init(void)
43
{
44
	assert(!git__pack_cache);
45

46 47
	git__on_shutdown(git_mwindow_files_free);
	return git_strmap_alloc(&git__pack_cache);
48 49 50 51 52 53 54 55 56 57 58 59
}

int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
{
	int error;
	char *packname;
	git_strmap_iter pos;
	struct git_pack_file *pack;

	if ((error = git_packfile__name(&packname, path)) < 0)
		return error;

60 61
	if (git_mutex_lock(&git__mwindow_mutex) < 0) {
		giterr_set(GITERR_OS, "failed to lock mwindow mutex");
62
		return -1;
63
	}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	pos = git_strmap_lookup_index(git__pack_cache, packname);
	git__free(packname);

	if (git_strmap_valid_index(git__pack_cache, pos)) {
		pack = git_strmap_value_at(git__pack_cache, pos);
		git_atomic_inc(&pack->refcount);

		git_mutex_unlock(&git__mwindow_mutex);
		*out = pack;
		return 0;
	}

	/* If we didn't find it, we need to create it */
	if ((error = git_packfile_alloc(&pack, path)) < 0) {
		git_mutex_unlock(&git__mwindow_mutex);
		return error;
	}

	git_atomic_inc(&pack->refcount);

85
	git_strmap_insert(git__pack_cache, pack->pack_name, pack, &error);
86 87
	git_mutex_unlock(&git__mwindow_mutex);

88 89
	if (error < 0) {
		git_packfile_free(pack);
90
		return -1;
91
	}
92

93 94 95 96
	*out = pack;
	return 0;
}

97
void git_mwindow_put_pack(struct git_pack_file *pack)
98 99 100 101 102
{
	int count;
	git_strmap_iter pos;

	if (git_mutex_lock(&git__mwindow_mutex) < 0)
103
		return;
104

105 106
	/* put before get would be a corrupted state */
	assert(git__pack_cache);
107 108

	pos = git_strmap_lookup_index(git__pack_cache, pack->pack_name);
109 110
	/* if we cannot find it, the state is corrupted */
	assert(git_strmap_valid_index(git__pack_cache, pos));
111 112 113 114 115 116 117 118

	count = git_atomic_dec(&pack->refcount);
	if (count == 0) {
		git_strmap_delete_at(git__pack_cache, pos);
		git_packfile_free(pack);
	}

	git_mutex_unlock(&git__mwindow_mutex);
119
	return;
120 121 122 123
}

void git_mwindow_free_all(git_mwindow_file *mwf)
{
124 125 126 127
	if (git_mutex_lock(&git__mwindow_mutex)) {
		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
		return;
	}
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142
	git_mwindow_free_all_locked(mwf);

	git_mutex_unlock(&git__mwindow_mutex);
}

/*
 * Free all the windows in a sequence, typically because we're done
 * with the file
 */
void git_mwindow_free_all_locked(git_mwindow_file *mwf)
{
	git_mwindow_ctl *ctl = &mem_ctl;
	size_t i;

143 144 145
	/*
	 * Remove these windows from the global list
	 */
146 147 148
	for (i = 0; i < ctl->windowfiles.length; ++i){
		if (git_vector_get(&ctl->windowfiles, i) == mwf) {
			git_vector_remove(&ctl->windowfiles, i);
149 150 151 152
			break;
		}
	}

153 154 155
	if (ctl->windowfiles.length == 0) {
		git_vector_free(&ctl->windowfiles);
		ctl->windowfiles.contents = NULL;
156 157 158 159 160 161
	}

	while (mwf->windows) {
		git_mwindow *w = mwf->windows;
		assert(w->inuse_cnt == 0);

162 163
		ctl->mapped -= w->window_map.len;
		ctl->open_windows--;
164 165 166 167

		git_futils_mmap_free(&w->window_map);

		mwf->windows = w->next;
168
		git__free(w);
169 170 171 172 173 174
	}
}

/*
 * Check if a window 'win' contains the address 'offset'
 */
175
int git_mwindow_contains(git_mwindow *win, git_off_t offset)
176
{
177
	git_off_t win_off = win->offset;
178
	return win_off <= offset
179
		&& offset <= (git_off_t)(win_off + win->window_map.len);
180 181 182 183 184
}

/*
 * Find the least-recently-used window in a file
 */
185
static void git_mwindow_scan_lru(
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	git_mwindow_file *mwf,
	git_mwindow **lru_w,
	git_mwindow **lru_l)
{
	git_mwindow *w, *w_l;

	for (w_l = NULL, w = mwf->windows; w; w = w->next) {
		if (!w->inuse_cnt) {
			/*
			 * If the current one is more recent than the last one,
			 * store it in the output parameter. If lru_w is NULL,
			 * it's the first loop, so store it as well.
			 */
			if (!*lru_w || w->last_used < (*lru_w)->last_used) {
				*lru_w = w;
				*lru_l = w_l;
			}
		}
		w_l = w;
	}
}

/*
 * Close the least recently used window. You should check to see if
210 211
 * the file descriptors need closing from time to time. Called under
 * lock from new_window.
212
 */
213
static int git_mwindow_close_lru(git_mwindow_file *mwf)
214
{
215
	git_mwindow_ctl *ctl = &mem_ctl;
216
	size_t i;
217
	git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows;
218

219
	/* FIXME: Does this give us any advantage? */
220 221 222
	if(mwf->windows)
		git_mwindow_scan_lru(mwf, &lru_w, &lru_l);

223
	for (i = 0; i < ctl->windowfiles.length; ++i) {
224
		git_mwindow *last = lru_w;
225
		git_mwindow_file *cur = git_vector_get(&ctl->windowfiles, i);
226 227 228
		git_mwindow_scan_lru(cur, &lru_w, &lru_l);
		if (lru_w != last)
			list = &cur->windows;
229 230
	}

231
	if (!lru_w) {
232
		giterr_set(GITERR_OS, "failed to close memory window; couldn't find LRU");
233 234
		return -1;
	}
235

236 237
	ctl->mapped -= lru_w->window_map.len;
	git_futils_mmap_free(&lru_w->window_map);
238

239 240 241 242
	if (lru_l)
		lru_l->next = lru_w->next;
	else
		*list = lru_w->next;
243

244 245
	git__free(lru_w);
	ctl->open_windows--;
246

247
	return 0;
248 249
}

250
/* This gets called under lock from git_mwindow_open */
251 252 253 254 255
static git_mwindow *new_window(
	git_mwindow_file *mwf,
	git_file fd,
	git_off_t size,
	git_off_t offset)
256
{
257
	git_mwindow_ctl *ctl = &mem_ctl;
Vicent Marti committed
258
	size_t walign = git_mwindow__window_size / 2;
259
	git_off_t len;
260 261 262
	git_mwindow *w;

	w = git__malloc(sizeof(*w));
Russell Belfer committed
263

264
	if (w == NULL)
265
		return NULL;
266 267 268 269 270

	memset(w, 0x0, sizeof(*w));
	w->offset = (offset / walign) * walign;

	len = size - w->offset;
Vicent Marti committed
271 272
	if (len > (git_off_t)git_mwindow__window_size)
		len = (git_off_t)git_mwindow__window_size;
273

274
	ctl->mapped += (size_t)len;
275

Vicent Marti committed
276
	while (git_mwindow__mapped_limit < ctl->mapped &&
277
			git_mwindow_close_lru(mwf) == 0) /* nop */;
278

279
	/*
Vicent Marti committed
280
	 * We treat `mapped_limit` as a soft limit. If we can't find a
281 282 283
	 * window to close and are above the limit, we still mmap the new
	 * window.
	 */
284

285
	if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) {
286 287 288 289 290 291 292 293 294 295 296 297
		/*
		 * The first error might be down to memory fragmentation even if
		 * we're below our soft limits, so free up what we can and try again.
		 */

		while (git_mwindow_close_lru(mwf) == 0)
			/* nop */;

		if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) {
			git__free(w);
			return NULL;
		}
298
	}
299

300 301
	ctl->mmap_calls++;
	ctl->open_windows++;
302

303 304
	if (ctl->mapped > ctl->peak_mapped)
		ctl->peak_mapped = ctl->mapped;
305

306 307
	if (ctl->open_windows > ctl->peak_open_windows)
		ctl->peak_open_windows = ctl->open_windows;
308 309 310 311 312 313 314 315

	return w;
}

/*
 * Open a new window, closing the least recenty used until we have
 * enough space. Don't forget to add it to your list
 */
316 317 318 319
unsigned char *git_mwindow_open(
	git_mwindow_file *mwf,
	git_mwindow **cursor,
	git_off_t offset,
320
	size_t extra,
321
	unsigned int *left)
322
{
323
	git_mwindow_ctl *ctl = &mem_ctl;
324 325
	git_mwindow *w = *cursor;

326 327 328 329 330
	if (git_mutex_lock(&git__mwindow_mutex)) {
		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
		return NULL;
	}

331
	if (!w || !(git_mwindow_contains(w, offset) && git_mwindow_contains(w, offset + extra))) {
332 333 334 335 336
		if (w) {
			w->inuse_cnt--;
		}

		for (w = mwf->windows; w; w = w->next) {
337
			if (git_mwindow_contains(w, offset) &&
Vicent Martí committed
338
				git_mwindow_contains(w, offset + extra))
339 340 341 342 343 344 345 346
				break;
		}

		/*
		 * If there isn't a suitable window, we need to create a new
		 * one.
		 */
		if (!w) {
347
			w = new_window(mwf, mwf->fd, mwf->size, offset);
348 349
			if (w == NULL) {
				git_mutex_unlock(&git__mwindow_mutex);
350
				return NULL;
351
			}
352 353 354 355 356 357 358
			w->next = mwf->windows;
			mwf->windows = w;
		}
	}

	/* If we changed w, store it in the cursor */
	if (w != *cursor) {
359
		w->last_used = ctl->used_ctr++;
360 361 362 363 364 365 366
		w->inuse_cnt++;
		*cursor = w;
	}

	offset -= w->offset;

	if (left)
367
		*left = (unsigned int)(w->window_map.len - offset);
368

369
	git_mutex_unlock(&git__mwindow_mutex);
370 371 372 373 374
	return (unsigned char *) w->window_map.data + offset;
}

int git_mwindow_file_register(git_mwindow_file *mwf)
{
375 376
	git_mwindow_ctl *ctl = &mem_ctl;
	int ret;
377

378 379 380 381 382
	if (git_mutex_lock(&git__mwindow_mutex)) {
		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
		return -1;
	}

383
	if (ctl->windowfiles.length == 0 &&
384 385
	    git_vector_init(&ctl->windowfiles, 8, NULL) < 0) {
		git_mutex_unlock(&git__mwindow_mutex);
386
		return -1;
387 388 389 390
	}

	ret = git_vector_insert(&ctl->windowfiles, mwf);
	git_mutex_unlock(&git__mwindow_mutex);
391

392
	return ret;
393 394
}

395
void git_mwindow_file_deregister(git_mwindow_file *mwf)
396
{
397
	git_mwindow_ctl *ctl = &mem_ctl;
398
	git_mwindow_file *cur;
399
	size_t i;
400

401 402
	if (git_mutex_lock(&git__mwindow_mutex))
		return;
403

404 405 406
	git_vector_foreach(&ctl->windowfiles, i, cur) {
		if (cur == mwf) {
			git_vector_remove(&ctl->windowfiles, i);
407
			git_mutex_unlock(&git__mwindow_mutex);
408
			return;
409 410
		}
	}
411
	git_mutex_unlock(&git__mwindow_mutex);
412 413
}

414 415 416 417
void git_mwindow_close(git_mwindow **window)
{
	git_mwindow *w = *window;
	if (w) {
418 419 420 421 422
		if (git_mutex_lock(&git__mwindow_mutex)) {
			giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
			return;
		}

423
		w->inuse_cnt--;
424
		git_mutex_unlock(&git__mwindow_mutex);
425 426 427
		*window = NULL;
	}
}