From: Michael Vrable Date: Wed, 17 Mar 2010 20:56:26 +0000 (-0700) Subject: Track memory usage statistics for cached data. X-Git-Url: http://git.vrable.net/?a=commitdiff_plain;h=f597cd5067ed36508118d2ecd29338e7b81f47e4;p=bluesky.git Track memory usage statistics for cached data. --- diff --git a/bluesky/bluesky.h b/bluesky/bluesky.h index fdc2e39..3fc1c6a 100644 --- a/bluesky/bluesky.h +++ b/bluesky/bluesky.h @@ -116,6 +116,12 @@ typedef struct { uint64_t next_inum; /* Next available inode for allocation */ BlueSkyStore *store; + + /* Accounting for memory used for caches. Space is measured in blocks, not + * bytes. We track both total data in the caches and dirty data (total + * data includes dirty data). Updates to these variables must be made + * atomically. */ + gint cache_total, cache_dirty; } BlueSkyFS; /* Inode number of the root directory. */ diff --git a/bluesky/debug.c b/bluesky/debug.c index 023b99f..312a0e1 100644 --- a/bluesky/debug.c +++ b/bluesky/debug.c @@ -39,6 +39,9 @@ static void inode_dump(gpointer key, gpointer value, gpointer user_data) void bluesky_debug_dump(BlueSkyFS *fs) { g_print("*** DEBUG DUMP FOR FILESYSTEM %s ***\n", fs->name); + g_print("Cached blocks: %d\tDirty blocks: %d\n", + g_atomic_int_get(&fs->cache_total), + g_atomic_int_get(&fs->cache_dirty)); g_print("Cached inodes: %u\tNext inode: %"PRIu64"\n", g_hash_table_size(fs->inodes), fs->next_inum); diff --git a/bluesky/file.c b/bluesky/file.c index ee4179e..8be3ea1 100644 --- a/bluesky/file.c +++ b/bluesky/file.c @@ -44,6 +44,12 @@ void bluesky_block_touch(BlueSkyInode *inode, uint64_t i) break; } + if (block->type != BLUESKY_BLOCK_CACHED + && block->type != BLUESKY_BLOCK_DIRTY) + g_atomic_int_add(&inode->fs->cache_total, 1); + if (block->type != BLUESKY_BLOCK_DIRTY) + g_atomic_int_add(&inode->fs->cache_dirty, 1); + block->type = BLUESKY_BLOCK_DIRTY; } @@ -87,6 +93,11 @@ void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size) for (guint i = inode->blocks->len; i < blocks; i++) { BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i); g_free(b->ref); + if (b->type == BLUESKY_BLOCK_CACHED + || b->type == BLUESKY_BLOCK_DIRTY) + g_atomic_int_add(&inode->fs->cache_total, -1); + if (b->type == BLUESKY_BLOCK_DIRTY) + g_atomic_int_add(&inode->fs->cache_dirty, -1); bluesky_string_unref(b->data); } g_array_set_size(inode->blocks, blocks); @@ -232,6 +243,7 @@ void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block, bluesky_store_async_wait(async); bluesky_store_async_unref(async); + g_atomic_int_add(&fs->cache_total, 1); } /* Write the given block to cloud-backed storage and mark it clean. */ @@ -263,6 +275,7 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block, block->ref = name; block->type = BLUESKY_BLOCK_CACHED; + g_atomic_int_add(&fs->cache_dirty, -1); g_checksum_free(csum); } @@ -290,9 +303,12 @@ void bluesky_file_drop_cached(BlueSkyInode *inode) "Dropping block %d of inode %"PRIu64" from cache", i, inode->inum); + g_log("bluesky/cache", G_LOG_LEVEL_DEBUG, + " (reference count was %d)", b->data->refcount); bluesky_string_unref(b->data); b->data = NULL; b->type = BLUESKY_BLOCK_REF; + g_atomic_int_add(&inode->fs->cache_total, -1); } } }