From: Michael Vrable Date: Mon, 2 Aug 2010 22:50:37 +0000 (-0700) Subject: Work to allow mmap-ed log entries to be used for data blocks. X-Git-Url: https://git.vrable.net/?a=commitdiff_plain;h=a8a9f2181e2e16d24d812ea1e7a7c8af42f0d2f1;p=bluesky.git Work to allow mmap-ed log entries to be used for data blocks. --- diff --git a/bluesky/bluesky-private.h b/bluesky/bluesky-private.h index fbc765c..60c10c1 100644 --- a/bluesky/bluesky-private.h +++ b/bluesky/bluesky-private.h @@ -247,6 +247,8 @@ struct _BlueSkyLog { BlueSkyLog *bluesky_log_new(const char *log_directory); void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log); void bluesky_log_finish_all(GList *log_items); +BlueSkyRCStr *bluesky_log_map_object(BlueSkyLog *log, int log_seq, + int log_offset, int log_size); #ifdef __cplusplus } diff --git a/bluesky/bluesky.h b/bluesky/bluesky.h index 4b17bda..205da74 100644 --- a/bluesky/bluesky.h +++ b/bluesky/bluesky.h @@ -64,15 +64,25 @@ void bluesky_init(void); gchar *bluesky_lowercase(const gchar *s); /* Reference-counted blocks of memory, used for passing data in and out of - * storage backends and in other places. */ + * storage backends and in other places. This may also refer to read-only + * mmaped data. */ typedef struct { gint refcount; + const char *addr; + size_t len; +} BlueSkyMmap; + +typedef struct { + gint refcount; + BlueSkyMmap *mmap; gchar *data; gsize len; } BlueSkyRCStr; BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len); BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s); +BlueSkyRCStr *bluesky_string_new_from_mmap(BlueSkyMmap *mmap, + int offset, gsize len); void bluesky_string_ref(BlueSkyRCStr *string); void bluesky_string_unref(BlueSkyRCStr *string); BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string); diff --git a/bluesky/cache.c b/bluesky/cache.c index 587f98b..cdbc7c2 100644 --- a/bluesky/cache.c +++ b/bluesky/cache.c @@ -22,6 +22,7 @@ * - Writing batches of data to the cloud. */ +#if 0 static void writeback_complete(gpointer a, gpointer i) { BlueSkyInode *inode = (BlueSkyInode *)i; @@ -46,6 +47,7 @@ static void writeback_complete(gpointer a, gpointer i) g_mutex_unlock(inode->lock); } +#endif static void flushd_dirty_inode(BlueSkyInode *inode) { diff --git a/bluesky/cloudlog.c b/bluesky/cloudlog.c index b98c233..d7cea63 100644 --- a/bluesky/cloudlog.c +++ b/bluesky/cloudlog.c @@ -242,3 +242,20 @@ void bluesky_cloudlog_write_log(BlueSkyFS *fs) state->data = NULL; } + +/* Ensure that a cloud log item is loaded in memory, and if not read it in. + * TODO: Make asynchronous, and make this also fetch from the cloud. Right now + * we only read from the log. Log item must be locked. */ +void bluesky_cloudlog_fetch(BlueSkyCloudLog *log) +{ + if (log->data != NULL) + return; + + g_print("Re-mapping log entry %d/%d/%d...\n", + log->log_seq, log->log_offset, log->log_size); + + g_assert(log->location_flags & CLOUDLOG_JOURNAL); + + log->data = bluesky_log_map_object(log->fs->log, log->log_seq, + log->log_offset, log->log_size); +} diff --git a/bluesky/log.c b/bluesky/log.c index e9af52a..152f598 100644 --- a/bluesky/log.c +++ b/bluesky/log.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "bluesky-private.h" @@ -222,3 +223,57 @@ void bluesky_log_finish_all(GList *log_items) log_items = g_list_delete_link(log_items, log_items); } } + +/* Memory-map the given log object into memory (read-only) and return a pointer + * to it. FIXME: Use some type of cache, map entire log segments, and use + * reference counting? */ +static int page_size = 0; + +BlueSkyRCStr *bluesky_log_map_object(BlueSkyLog *log, + int log_seq, int log_offset, int log_size) +{ + if (page_size == 0) { + page_size = getpagesize(); + } + + int dirfd = open(log->log_directory, O_DIRECTORY); + if (dirfd < 0) { + fprintf(stderr, "Unable to open logging directory: %m\n"); + return NULL; + } + + char logfile[64]; + g_snprintf(logfile, sizeof(logfile), "log-%08d", log_seq); + int fd = openat(dirfd, logfile, O_RDONLY); + close(dirfd); + + if (fd < 0) { + fprintf(stderr, "Error opening logfile %s: %m\n", logfile); + return NULL; + } + + off_t off_start, off_end; + off_start = log_offset; + off_end = off_start + log_size; + off_start &= ~(page_size - 1); + off_end = (off_end + (page_size - 1)) & (page_size - 1); + + const char *ptr = (const char *)mmap(NULL, off_end - off_start, PROT_READ, + MAP_SHARED, fd, off_start); + + if (ptr == NULL) { + fprintf(stderr, "Error mapping logfile: %m\n"); + close(fd); + return NULL; + } + + close(fd); + + BlueSkyMmap *mmap = g_new0(BlueSkyMmap, 1); + mmap->addr = ptr; + mmap->len = off_end - off_start; + g_atomic_int_set(&mmap->refcount, 1); + + return bluesky_string_new_from_mmap(mmap, + log_offset - off_start, log_size); +} diff --git a/bluesky/util.c b/bluesky/util.c index 3ac6596..c4bac19 100644 --- a/bluesky/util.c +++ b/bluesky/util.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "bluesky-private.h" @@ -52,6 +53,17 @@ gboolean bluesky_inode_is_ready(BlueSkyInode *inode) /**** Reference-counted strings. ****/ +void bluesky_mmap_unref(BlueSkyMmap *mmap) +{ + if (mmap == NULL) + return; + + if (g_atomic_int_dec_and_test(&mmap->refcount)) { + munmap((void *)mmap->addr, mmap->len); + g_free(mmap); + } +} + /* Create and return a new reference-counted string. The reference count is * initially one. The newly-returned string takes ownership of the memory * pointed at by data, and will call g_free on it when the reference count @@ -59,6 +71,7 @@ gboolean bluesky_inode_is_ready(BlueSkyInode *inode) BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len) { BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1); + string->mmap = NULL; string->data = data; string->len = len; g_atomic_int_set(&string->refcount, 1); @@ -72,6 +85,19 @@ BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s) return bluesky_string_new(g_string_free(s, FALSE), len); } +/* Create a new BlueSkyRCStr from a memory-mapped buffer. */ +BlueSkyRCStr *bluesky_string_new_from_mmap(BlueSkyMmap *mmap, + int offset, gsize len) +{ + BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1); + string->mmap = mmap; + g_atomic_int_inc(&mmap->refcount); + string->data = (char *)mmap->addr + offset; + string->len = len; + g_atomic_int_set(&string->refcount, 1); + return string; +} + void bluesky_string_ref(BlueSkyRCStr *string) { if (string == NULL) @@ -86,7 +112,11 @@ void bluesky_string_unref(BlueSkyRCStr *string) return; if (g_atomic_int_dec_and_test(&string->refcount)) { - g_free(string->data); + if (string->mmap == NULL) { + g_free(string->data); + } else { + bluesky_mmap_unref(string->mmap); + } g_free(string); } } @@ -102,6 +132,14 @@ BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string) if (string == NULL) return NULL; + if (string->mmap != NULL) { + BlueSkyRCStr *s; + s = bluesky_string_new(g_memdup(string->data, string->len), + string->len); + bluesky_string_unref(string); + return s; + } + if (g_atomic_int_dec_and_test(&string->refcount)) { /* There are no other shared copies, so return this one. */ g_atomic_int_inc(&string->refcount); @@ -119,6 +157,8 @@ BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string) * if needed). */ void bluesky_string_resize(BlueSkyRCStr *string, gsize len) { + g_assert(string->mmap == NULL); + if (string->len == len) return;