dovecot: mmap() mailbox list index only if it's at least 32kB

dovecot at dovecot.org dovecot at dovecot.org
Thu Jul 19 04:54:27 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/6805ccf0b82e
changeset: 6101:6805ccf0b82e
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jul 19 04:47:25 2007 +0300
description:
mmap() mailbox list index only if it's at least 32kB

diffstat:

3 files changed, 24 insertions(+), 10 deletions(-)
src/lib-index/mailbox-list-index-private.h |    2 ++
src/lib-index/mailbox-list-index-sync.c    |   18 +++++++++---------
src/lib-index/mailbox-list-index.c         |   14 +++++++++++++-

diffs (125 lines):

diff -r a55ee2e3caac -r 6805ccf0b82e src/lib-index/mailbox-list-index-private.h
--- a/src/lib-index/mailbox-list-index-private.h	Thu Jul 19 04:18:30 2007 +0300
+++ b/src/lib-index/mailbox-list-index-private.h	Thu Jul 19 04:47:25 2007 +0300
@@ -9,6 +9,8 @@
 
 #define MAILBOX_LIST_COMPRESS_PERCENTAGE 10
 #define MAILBOX_LIST_COMPRESS_MIN_SIZE 1024
+
+#define MAILBOX_LIST_INDEX_MMAP_MIN_SIZE (1024*32)
 
 struct mailbox_list_index_header {
 	uint8_t major_version;
diff -r a55ee2e3caac -r 6805ccf0b82e src/lib-index/mailbox-list-index-sync.c
--- a/src/lib-index/mailbox-list-index-sync.c	Thu Jul 19 04:18:30 2007 +0300
+++ b/src/lib-index/mailbox-list-index-sync.c	Thu Jul 19 04:47:25 2007 +0300
@@ -502,7 +502,7 @@ mailbox_list_index_sync_alloc_space(stru
 	/* all allocations must be 32bit aligned */
 	pos = (pos + 3) & ~3;
 
-	if (ctx->index->mmap_disable) {
+	if (ctx->index->mmap_base == NULL) {
 		/* write the data into temporary buffer first */
 		buffer_reset(ctx->output_buf);
 
@@ -647,7 +647,7 @@ mailbox_list_index_sync_recreate_dir(str
 	i_assert(dest == nondeleted_count);
 	i_assert(name_pos == space_needed);
 
-	if (index->mmap_disable) {
+	if (index->mmap_base == NULL) {
 		file_cache_write(index->file_cache, ctx->output_buf->data,
 				 ctx->output_buf->used, ctx->output->offset);
 		o_stream_send(ctx->output, ctx->output_buf->data,
@@ -661,7 +661,7 @@ mailbox_list_index_sync_recreate_dir(str
 		/* add a link to this newly created directory. */
 		uint32_t data = mail_index_uint32_to_offset(base_offset);
 
-		if (!index->mmap_disable)  {
+		if (index->mmap_base != NULL)  {
 			uint32_t *pos;
 
 			pos = PTR_OFFSET(index->mmap_base, offset_pos);
@@ -679,7 +679,7 @@ mailbox_list_index_sync_recreate_dir(str
 		}
 	}
 
-	if (index->mmap_disable) {
+	if (index->mmap_base == NULL) {
 		/* file_cache_write() calls may have moved mmaping */
 		index->const_mmap_base = file_cache_get_map(index->file_cache,
 							    &index->mmap_size);
@@ -711,7 +711,7 @@ mailbox_list_index_sync_update_dir(struc
 	i_assert(count <= dir->count);
 	i_assert(sync_dir->seen_records_count < count);
 
-	if (!ctx->index->mmap_disable)
+	if (ctx->index->mmap_base != NULL)
 		recs = MAILBOX_LIST_RECORDS_MODIFIABLE(dir);
 	else {
 		/* @UNSAFE: copy the records into a temporary buffer that
@@ -749,7 +749,7 @@ mailbox_list_index_sync_update_dir(struc
 			i++;
 		}
 	}
-	if (ctx->index->mmap_disable) {
+	if (ctx->index->mmap_base == NULL) {
 		uoff_t offset, old_offset;
 		size_t size = sizeof(struct mailbox_list_record) * dir->count;
 
@@ -849,7 +849,7 @@ mailbox_list_index_sync_write(struct mai
 	bool partial;
 	int ret = 0;
 
-	if (ctx->index->mmap_disable) {
+	if (ctx->index->mmap_base == NULL) {
 		ctx->output = o_stream_create_file(ctx->index->fd, default_pool,
 						   0, FALSE);
 		ctx->output_buf = buffer_create_dynamic(default_pool, 4096);
@@ -870,7 +870,7 @@ mailbox_list_index_sync_write(struct mai
 
 	if (!ctx->changed) {
 		/* nothing written */
-	} else if (!ctx->index->mmap_disable) {
+	} else if (ctx->index->mmap_base != NULL) {
 		/* update header */
 		hdr = ctx->index->mmap_base;
 		if (ret == 0)
@@ -900,7 +900,7 @@ mailbox_list_index_sync_write(struct mai
 			ret = -1;
 		}
 	}
-	if (ctx->index->mmap_disable) {
+	if (ctx->index->mmap_base == NULL) {
 		o_stream_destroy(&ctx->output);
 		buffer_free(ctx->output_buf);
 	}
diff -r a55ee2e3caac -r 6805ccf0b82e src/lib-index/mailbox-list-index.c
--- a/src/lib-index/mailbox-list-index.c	Thu Jul 19 04:18:30 2007 +0300
+++ b/src/lib-index/mailbox-list-index.c	Thu Jul 19 04:47:25 2007 +0300
@@ -139,12 +139,24 @@ int mailbox_list_index_map(struct mailbo
 int mailbox_list_index_map(struct mailbox_list_index *index)
 {
 	const struct mailbox_list_index_header *hdr;
+	struct stat st;
 	ssize_t ret;
 
 	mailbox_list_index_unmap(index);
 
 	if (!index->mmap_disable) {
-		index->mmap_base = mmap_rw_file(index->fd, &index->mmap_size);
+		if (fstat(index->fd, &st) < 0) {
+			mailbox_list_index_set_syscall_error(index, "fstat()");
+			return -1;
+		}
+	}
+
+	if (!index->mmap_disable &&
+	    st.st_size >= MAILBOX_LIST_INDEX_MMAP_MIN_SIZE) {
+		index->mmap_size = st.st_size;
+		index->mmap_base = mmap(NULL, index->mmap_size,
+					PROT_READ | PROT_WRITE,
+					MAP_SHARED, index->fd, 0);
 		if (index->mmap_base == MAP_FAILED) {
 			index->mmap_base = NULL;
 			mailbox_list_index_set_syscall_error(index, "mmap()");


More information about the dovecot-cvs mailing list