mwindow.c 9.37 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 9 10 11 12
 */

#include "common.h"
#include "mwindow.h"
#include "vector.h"
#include "fileops.h"
#include "map.h"
13
#include "global.h"
14 15 16
#include "strmap.h"
#include "pack.h"

17
GIT__USE_STRMAP
18 19 20

#define DEFAULT_WINDOW_SIZE \
	(sizeof(void*) >= 8 \
Vicent Marti committed
21
		? 1 * 1024 * 1024 * 1024 \
22 23 24
		: 32 * 1024 * 1024)

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

Vicent Marti committed
27 28
size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE;
size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT;
29

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

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

36
static void git_mwindow_files_free(void)
37
{
38
	git_strmap *tmp = git__pack_cache;
39

40 41
	git__pack_cache = NULL;
	git_strmap_free(tmp);
42 43
}

44
int git_mwindow_global_init(void)
45
{
46
	assert(!git__pack_cache);
47

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

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;

62 63
	if (git_mutex_lock(&git__mwindow_mutex) < 0) {
		giterr_set(GITERR_OS, "failed to lock mwindow mutex");
64
		return -1;
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

	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);

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

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

95 96 97 98
	*out = pack;
	return 0;
}

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

	if (git_mutex_lock(&git__mwindow_mutex) < 0)
105
		return;
106

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

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

	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);
121
	return;
122 123 124 125
}

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

131 132 133 134 135 136 137 138 139 140 141 142 143 144
	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;

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

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

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

164 165
		ctl->mapped -= w->window_map.len;
		ctl->open_windows--;
166 167 168 169

		git_futils_mmap_free(&w->window_map);

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

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

/*
 * Find the least-recently-used window in a file
 */
187
static void git_mwindow_scan_lru(
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	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
212 213
 * the file descriptors need closing from time to time. Called under
 * lock from new_window.
214
 */
215
static int git_mwindow_close_lru(git_mwindow_file *mwf)
216
{
217
	git_mwindow_ctl *ctl = &mem_ctl;
218
	size_t i;
219
	git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows;
220

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

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

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

238 239
	ctl->mapped -= lru_w->window_map.len;
	git_futils_mmap_free(&lru_w->window_map);
240

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

246 247
	git__free(lru_w);
	ctl->open_windows--;
248

249
	return 0;
250 251
}

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

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

266
	if (w == NULL)
267
		return NULL;
268 269 270 271 272

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

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

276
	ctl->mapped += (size_t)len;
277

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

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

287
	if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) {
288 289 290 291 292 293 294 295 296 297 298 299
		/*
		 * 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;
		}
300
	}
301

302 303
	ctl->mmap_calls++;
	ctl->open_windows++;
304

305 306
	if (ctl->mapped > ctl->peak_mapped)
		ctl->peak_mapped = ctl->mapped;
307

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

	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
 */
318 319 320 321
unsigned char *git_mwindow_open(
	git_mwindow_file *mwf,
	git_mwindow **cursor,
	git_off_t offset,
322
	size_t extra,
323
	unsigned int *left)
324
{
325
	git_mwindow_ctl *ctl = &mem_ctl;
326 327
	git_mwindow *w = *cursor;

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

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

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

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

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

	offset -= w->offset;

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

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

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

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

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

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

394
	return ret;
395 396
}

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

403 404
	if (git_mutex_lock(&git__mwindow_mutex))
		return;
405

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

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

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