mwindow.c 6.96 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

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

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

Vicent Marti committed
23 24
size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE;
size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT;
25

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

29 30 31 32 33 34
/*
 * Free all the windows in a sequence, typically because we're done
 * with the file
 */
void git_mwindow_free_all(git_mwindow_file *mwf)
{
35
	git_mwindow_ctl *ctl = &mem_ctl;
36
	size_t i;
37

38 39 40 41
	if (git_mutex_lock(&git__mwindow_mutex)) {
		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
		return;
	}
42

43 44 45
	/*
	 * Remove these windows from the global list
	 */
46 47 48
	for (i = 0; i < ctl->windowfiles.length; ++i){
		if (git_vector_get(&ctl->windowfiles, i) == mwf) {
			git_vector_remove(&ctl->windowfiles, i);
49 50 51 52
			break;
		}
	}

53 54 55
	if (ctl->windowfiles.length == 0) {
		git_vector_free(&ctl->windowfiles);
		ctl->windowfiles.contents = NULL;
56 57 58 59 60 61
	}

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

62 63
		ctl->mapped -= w->window_map.len;
		ctl->open_windows--;
64 65 66 67

		git_futils_mmap_free(&w->window_map);

		mwf->windows = w->next;
68
		git__free(w);
69
	}
70 71

	git_mutex_unlock(&git__mwindow_mutex);
72 73 74 75 76
}

/*
 * Check if a window 'win' contains the address 'offset'
 */
77
int git_mwindow_contains(git_mwindow *win, git_off_t offset)
78
{
79
	git_off_t win_off = win->offset;
80
	return win_off <= offset
81
		&& offset <= (git_off_t)(win_off + win->window_map.len);
82 83 84 85 86
}

/*
 * Find the least-recently-used window in a file
 */
87
static void git_mwindow_scan_lru(
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	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
112 113
 * the file descriptors need closing from time to time. Called under
 * lock from new_window.
114
 */
115
static int git_mwindow_close_lru(git_mwindow_file *mwf)
116
{
117
	git_mwindow_ctl *ctl = &mem_ctl;
118
	size_t i;
119
	git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows;
120

121
	/* FIXME: Does this give us any advantage? */
122 123 124
	if(mwf->windows)
		git_mwindow_scan_lru(mwf, &lru_w, &lru_l);

125
	for (i = 0; i < ctl->windowfiles.length; ++i) {
126
		git_mwindow *last = lru_w;
127
		git_mwindow_file *cur = git_vector_get(&ctl->windowfiles, i);
128 129 130
		git_mwindow_scan_lru(cur, &lru_w, &lru_l);
		if (lru_w != last)
			list = &cur->windows;
131 132
	}

133 134 135 136
	if (!lru_w) {
		giterr_set(GITERR_OS, "Failed to close memory window. Couldn't find LRU");
		return -1;
	}
137

138 139
	ctl->mapped -= lru_w->window_map.len;
	git_futils_mmap_free(&lru_w->window_map);
140

141 142 143 144
	if (lru_l)
		lru_l->next = lru_w->next;
	else
		*list = lru_w->next;
145

146 147
	git__free(lru_w);
	ctl->open_windows--;
148

149
	return 0;
150 151
}

152
/* This gets called under lock from git_mwindow_open */
153 154 155 156 157
static git_mwindow *new_window(
	git_mwindow_file *mwf,
	git_file fd,
	git_off_t size,
	git_off_t offset)
158
{
159
	git_mwindow_ctl *ctl = &mem_ctl;
Vicent Marti committed
160
	size_t walign = git_mwindow__window_size / 2;
161
	git_off_t len;
162 163 164
	git_mwindow *w;

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

166
	if (w == NULL)
167
		return NULL;
168 169 170 171 172

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

	len = size - w->offset;
Vicent Marti committed
173 174
	if (len > (git_off_t)git_mwindow__window_size)
		len = (git_off_t)git_mwindow__window_size;
175

176
	ctl->mapped += (size_t)len;
177

Vicent Marti committed
178
	while (git_mwindow__mapped_limit < ctl->mapped &&
179
			git_mwindow_close_lru(mwf) == 0) /* nop */;
180

181
	/*
Vicent Marti committed
182
	 * We treat `mapped_limit` as a soft limit. If we can't find a
183 184 185
	 * window to close and are above the limit, we still mmap the new
	 * window.
	 */
186

187 188 189 190
	if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) {
		git__free(w);
		return NULL;
	}
191

192 193
	ctl->mmap_calls++;
	ctl->open_windows++;
194

195 196
	if (ctl->mapped > ctl->peak_mapped)
		ctl->peak_mapped = ctl->mapped;
197

198 199
	if (ctl->open_windows > ctl->peak_open_windows)
		ctl->peak_open_windows = ctl->open_windows;
200 201 202 203 204 205 206 207

	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
 */
208 209 210 211
unsigned char *git_mwindow_open(
	git_mwindow_file *mwf,
	git_mwindow **cursor,
	git_off_t offset,
212
	size_t extra,
213
	unsigned int *left)
214
{
215
	git_mwindow_ctl *ctl = &mem_ctl;
216 217
	git_mwindow *w = *cursor;

218 219 220 221 222
	if (git_mutex_lock(&git__mwindow_mutex)) {
		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
		return NULL;
	}

223
	if (!w || !(git_mwindow_contains(w, offset) && git_mwindow_contains(w, offset + extra))) {
224 225 226 227 228
		if (w) {
			w->inuse_cnt--;
		}

		for (w = mwf->windows; w; w = w->next) {
229
			if (git_mwindow_contains(w, offset) &&
Vicent Martí committed
230
				git_mwindow_contains(w, offset + extra))
231 232 233 234 235 236 237 238
				break;
		}

		/*
		 * If there isn't a suitable window, we need to create a new
		 * one.
		 */
		if (!w) {
239
			w = new_window(mwf, mwf->fd, mwf->size, offset);
240 241
			if (w == NULL) {
				git_mutex_unlock(&git__mwindow_mutex);
242
				return NULL;
243
			}
244 245 246 247 248 249 250
			w->next = mwf->windows;
			mwf->windows = w;
		}
	}

	/* If we changed w, store it in the cursor */
	if (w != *cursor) {
251
		w->last_used = ctl->used_ctr++;
252 253 254 255 256 257 258
		w->inuse_cnt++;
		*cursor = w;
	}

	offset -= w->offset;

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

261
	git_mutex_unlock(&git__mwindow_mutex);
262 263 264 265 266
	return (unsigned char *) w->window_map.data + offset;
}

int git_mwindow_file_register(git_mwindow_file *mwf)
{
267 268
	git_mwindow_ctl *ctl = &mem_ctl;
	int ret;
269

270 271 272 273 274
	if (git_mutex_lock(&git__mwindow_mutex)) {
		giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
		return -1;
	}

275
	if (ctl->windowfiles.length == 0 &&
276 277
	    git_vector_init(&ctl->windowfiles, 8, NULL) < 0) {
		git_mutex_unlock(&git__mwindow_mutex);
278
		return -1;
279 280 281 282
	}

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

284
	return ret;
285 286
}

287
void git_mwindow_file_deregister(git_mwindow_file *mwf)
288
{
289
	git_mwindow_ctl *ctl = &mem_ctl;
290
	git_mwindow_file *cur;
291
	size_t i;
292

293 294
	if (git_mutex_lock(&git__mwindow_mutex))
		return;
295

296 297 298
	git_vector_foreach(&ctl->windowfiles, i, cur) {
		if (cur == mwf) {
			git_vector_remove(&ctl->windowfiles, i);
299
			git_mutex_unlock(&git__mwindow_mutex);
300
			return;
301 302
		}
	}
303
	git_mutex_unlock(&git__mwindow_mutex);
304 305
}

306 307 308 309
void git_mwindow_close(git_mwindow **window)
{
	git_mwindow *w = *window;
	if (w) {
310 311 312 313 314
		if (git_mutex_lock(&git__mwindow_mutex)) {
			giterr_set(GITERR_THREAD, "unable to lock mwindow mutex");
			return;
		}

315
		w->inuse_cnt--;
316
		git_mutex_unlock(&git__mwindow_mutex);
317 318 319
		*window = NULL;
	}
}