Skip to content

Commit

Permalink
Added read-around cache layer to ROFS
Browse files Browse the repository at this point in the history
This patch adda simple "read-around" cache layer logic to ROFS. In cache mode
ROFS reads more data from disk than needed per uio and stores it in memory
in anticipation there will be more subsequent contiguous reads of data.
With this change by default ROFS will operate in cache-mode but can be
changed to non-cache by passing '--disable_rofs_cache' boot option.

Specifically ROFS cache mode implements simple "read-around" strategy by
dividing a file into same size (32K) segments and reading entire segment
into memory when corresponding offset of file is requested. Files smaller
than 32K are loaded in full on first read. This simple read-around strategy
can achieve 80-90% cache hit ratio in many conducted measurements. Also it can
deliver 2-3 increase of read speed over non-cache mode at some cost of
too much unneeded data read (15-20%). Lastly the loaded data stays
in memory forever as there is no LRU logic implemented that could limit
memory used.

Signed-off-by: Waldemar Kozaczuk <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
wkozaczuk authored and nyh committed Mar 22, 2018
1 parent cd44966 commit 1f122ae
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,7 @@ fs_objs += devfs/devfs_vnops.o \

fs_objs += rofs/rofs_vfsops.o \
rofs/rofs_vnops.o \
rofs/rofs_cache.o \
rofs/rofs_common.o

fs_objs += procfs/procfs_vnops.o
Expand Down
29 changes: 24 additions & 5 deletions fs/rofs/rofs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@
// write to local disk. The ROFS is inspired and shares some ideas
// from the original MFS implementation by James Root from 2015.
//
// This initial version of ROFS operates without cache. It reads as much data
// from disk as requested per uio passed in to the read function and it does
// not retain/cache it for any subsequent read of the same data.
// The ROFS can operate in two modes - without cache and with cache.
// Without cache ROFS reads as much data from disk as requested
// per uio passed in to the read function but it does not retain/cache it
// for any subsequent read of the same data. Conversely in cache mode
// ROFS reads more data from disk than needed per uio and stores it in memory
// in anticipation there will be more subsequent contiguous reads of data.
// By default ROFS operates in cache-mode but can be changed to non-cache
// by passing '--disable_rofs_cache' boot option.
//
// Specifically ROFS cache mode implements simple "read-around" strategy by
// dividing a file into same size (32K) segments and reading entire segment
// into memory when corresponding offset of file is requested. Files smaller
// than 32K are loaded in full on first read. This simple read-around strategy
// can achieve 80-90% cache hit ratio in many conducted measurements. Also it can
// deliver 2-3 increase of read speed over non-cache mode at some cost of
// too much unneeded data read (15-20%). Lastly the loaded data stays
// in memory forever as there is no LRU logic implemented that could limit
// memory used.
//
// The structure of the data on disk is explained in scripts/gen-rofs-img.py

Expand Down Expand Up @@ -110,8 +125,12 @@ struct rofs_info {
struct rofs_inode *inodes;
};

int rofs_read_blocks(struct device *device, uint64_t starting_block, uint64_t blocks_count, void *buf);
namespace rofs {
int
cache_read(struct rofs_inode *inode, struct device *device, struct rofs_super_block *sb, struct uio *uio);
}

void rofs_set_vnode(struct vnode *vnode, struct rofs_inode *inode);
int rofs_read_blocks(struct device *device, uint64_t starting_block, uint64_t blocks_count, void* buf);
void rofs_set_vnode(struct vnode* vnode, struct rofs_inode *inode);

#endif
274 changes: 274 additions & 0 deletions fs/rofs/rofs_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/*
* Copyright (C) 2017 Waldemar Kozaczuk
* Inspired by original MFS implementation by James Root from 2015
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/

#include "rofs.hh"
#include <list>
#include <unordered_map>
#include <include/osv/uio.h>
#include <osv/debug.h>
#include <osv/sched.hh>

/*
* From cache perspective let us divide each file into sequence of contiguous 32K segments.
* The files smaller or equal than 32K get loaded in one read, others get loaded
* segment by segment.
**/
//
//TODO These 2 values can be made configurable
#define CACHE_SEGMENT_SIZE_IN_BLOCKS 64 // 32K
#define CACHE_SEGMENT_INDEX(offset) (offset >> 15)

#if defined(ROFS_DIAGNOSTICS_ENABLED)
extern std::atomic<long> rofs_block_allocated;
extern std::atomic<long> rofs_cache_reads;
extern std::atomic<long> rofs_cache_misses;
#endif

namespace rofs {
//
// This structure holds cache information and data of specific file
struct file_cache {
std::unordered_map<uint64_t, struct file_cache_segment *> segments_by_index;
struct rofs_inode *inode;
struct rofs_super_block *sb;
};

//
// This structure holds block_count (typically CACHE_SEGMENT_SIZE_IN_BLOCKS) of 512 blocks
// of file data starting at starting_block * 512 byte offset relative to the beginning
// of the file.
class file_cache_segment {
private:
struct file_cache *cache; // Parent file cache
void *data; // Copy of data on disk
uint64_t starting_block; // This is relative to the 512-block of the inode itself
uint64_t block_count; // Length of data in 512 blocks
bool data_ready; // Has data been fully read from disk?

public:
file_cache_segment(struct file_cache *_cache, uint64_t _starting_block, uint64_t _block_count) {
this->cache = _cache;
this->starting_block = _starting_block;
this->block_count = _block_count;
this->data_ready = false; // Data has to be loaded from disk
this->data = malloc(_cache->sb->block_size * _block_count);
#if defined(ROFS_DIAGNOSTICS_ENABLED)
rofs_block_allocated += block_count;
#endif
}

~file_cache_segment() {
free(this->data);
}

uint64_t length() {
return this->block_count * this->cache->sb->block_size;
}

bool is_data_ready() {
return this->data_ready;
}

//
// Read data from memory per uio
int read(struct uio *uio, uint64_t offset_in_segment, uint64_t bytes_to_read) {
print("[rofs] [%d] -> file_cache_segment::read() i-node: %d, starting block %d, reading [%d] bytes at segment offset [%d]\n",
sched::thread::current()->id(), cache->inode->inode_no, starting_block, bytes_to_read,
offset_in_segment);
return uiomove(data + offset_in_segment, bytes_to_read, uio);
}

//
// Read all segment data from disk and copy to memory
int read_from_disk(struct device *device) {
auto block = cache->inode->data_offset + starting_block;
auto bytes_remaining = cache->inode->file_size - starting_block * cache->sb->block_size;
auto blocks_remaining = bytes_remaining / cache->sb->block_size;
if (bytes_remaining % cache->sb->block_size > 0) {
blocks_remaining++;
}
auto block_count_to_read = std::min(block_count, blocks_remaining);
print("[rofs] [%d] -> file_cache_segment::write() i-node: %d, starting block %d, reading [%d] blocks at disk offset [%d]\n",
sched::thread::current()->id(), cache->inode->inode_no, starting_block, block_count_to_read, block);
auto error = rofs_read_blocks(device, block, block_count_to_read, data);
this->data_ready = (error == 0);
if (error) {
print("!!!!! Error reading from disk\n");
}
return error;
}
};

static std::unordered_map<uint64_t, struct file_cache *> file_cache_by_node_id;
static mutex file_cache_lock;

static struct file_cache *get_or_create_file_cache(struct rofs_inode *inode, struct rofs_super_block *sb) {
// This is the only global mutex
WITH_LOCK(file_cache_lock) {
auto cache_entry = file_cache_by_node_id.find(inode->inode_no);
if (cache_entry == file_cache_by_node_id.end()) {
struct file_cache *new_cache = new file_cache();
new_cache->inode = inode;
new_cache->sb = sb;
file_cache_by_node_id.emplace(inode->inode_no, new_cache);
return new_cache;
} else {
return cache_entry->second;
}
}
}

enum CacheTransactionType {
READ_FROM_MEMORY = 1,
READ_FROM_DISK
};

// This represents an operation/transaction to read data from segment memory or/and from disk
struct cache_segment_transaction {
struct file_cache_segment *segment;
CacheTransactionType transaction_type;
uint64_t segment_offset;
uint64_t bytes_to_read;

cache_segment_transaction(file_cache_segment *_segment, uint64_t file_offset, uint64_t _bytes_to_read) {
this->segment = _segment;
if (_segment->is_data_ready()) {
this->transaction_type = CacheTransactionType::READ_FROM_MEMORY;
} else {
this->transaction_type = CacheTransactionType::READ_FROM_DISK;
}
this->segment_offset = file_offset % segment->length();
this->bytes_to_read = std::min(segment->length() - segment_offset, _bytes_to_read);
}
};

//
// This function analyzes uio against existing segments in file_cache
// and builds a vector of transactions/operation that is used by cache_read to tell it
// to either read data from memory in cache segment or read data from disk into
// new segment
static std::vector<struct cache_segment_transaction>
plan_cache_transactions(struct file_cache *cache, struct uio *uio) {

std::vector<struct cache_segment_transaction> transactions;
//
// Check if file is small enough to fit into cache segment
if (cache->segments_by_index.empty() &&
cache->inode->file_size <= (CACHE_SEGMENT_SIZE_IN_BLOCKS * cache->sb->block_size)) {
auto block_count = cache->inode->file_size / cache->sb->block_size;
if (cache->inode->file_size % cache->sb->block_size > 0) {
block_count++;
}
auto new_cache_segment = new file_cache_segment(cache, 0, block_count);
cache->segments_by_index.emplace(0, new_cache_segment);
uint64_t read_amt = std::min<uint64_t>(cache->inode->file_size - uio->uio_offset, uio->uio_resid);
transactions.push_back(cache_segment_transaction(new_cache_segment, uio->uio_offset, read_amt));
print("[rofs] [%d] -> rofs_cache_get_segment_operations i-node: %d, read FULL file of %d bytes\n",
sched::thread::current()->id(), cache->inode->inode_no, uio->uio_resid);
return transactions;
}
//
// File is larger than cache segment or previous attempt to read from disk failed
uint64_t file_offset = uio->uio_offset;
uint64_t bytes_to_read = std::min<uint64_t>(cache->inode->file_size - uio->uio_offset, uio->uio_resid);
while (bytes_to_read > 0) {
//
// Next try to see if any cache segment is hit
auto cache_segment_index = CACHE_SEGMENT_INDEX(file_offset);
auto cache_segment = cache->segments_by_index.find(cache_segment_index);
if (cache_segment != cache->segments_by_index.end()) {
print("[rofs] [%d] -> rofs_cache_get_segment_operations i-node: %d, cache segment %d HIT at file offset %d\n",
sched::thread::current()->id(), cache->inode->inode_no, cache_segment_index, file_offset);

auto transaction = cache_segment_transaction(cache_segment->second, file_offset, bytes_to_read);
file_offset += transaction.bytes_to_read;
bytes_to_read -= transaction.bytes_to_read;
transactions.push_back(transaction);
}
//
// Miss -> read from disk
else {
print("[rofs] [%d] -> rofs_cache_get_segment_operations i-node: %d, cache segment %d MISS at file offset %d\n",
sched::thread::current()->id(), cache->inode->inode_no, cache_segment_index, file_offset);
uint64_t segment_starting_block = cache_segment_index * CACHE_SEGMENT_SIZE_IN_BLOCKS;
//
// Allocate new cache segment
auto new_cache_segment = new file_cache_segment(cache, segment_starting_block,
CACHE_SEGMENT_SIZE_IN_BLOCKS);
cache->segments_by_index.emplace(cache_segment_index, new_cache_segment);

auto transaction = cache_segment_transaction(new_cache_segment, file_offset, bytes_to_read);
file_offset += transaction.bytes_to_read;;
bytes_to_read -= transaction.bytes_to_read;
transactions.push_back(transaction);
}
}

return transactions;
}

//
// This function calls plan_cache_transactions first to identify what part of uio can be
// read from memory and what needs to be read from disk
// NOTE: This function is NOT thread-safe and does not need to be because it is called only
// by rofs_read_with_cache() which in turn is called by vfs_file::read() in a critical section
// specific to given file. So effectively cache_read() is assumed to be called by one thread
// at a time and no thread synchronization is needed.
int
cache_read(struct rofs_inode *inode, struct device *device, struct rofs_super_block *sb, struct uio *uio) {
//
// Find existing one or create new file cache
struct file_cache *cache = get_or_create_file_cache(inode, sb);

//
// Prepare list of cache transactions (copy from memory
// or read from disk into cache memory and then copy into memory)
auto segment_transactions = plan_cache_transactions(cache, uio);
print("[rofs] [%d] rofs_cache_read called for i-node [%d] at %d with %d ops\n",
sched::thread::current()->id(), inode->inode_no, uio->uio_offset, segment_transactions.size());

int error = 0;

// Iterate over the list of cache operation and either copy from memory
// or read from disk into cache memory and then copy into memory
auto it = segment_transactions.begin();
for (; it != segment_transactions.end(); ++it) {
auto transaction = *it;
#if defined(ROFS_DIAGNOSTICS_ENABLED)
rofs_cache_reads += 1;
#endif
if (transaction.transaction_type == CacheTransactionType::READ_FROM_MEMORY) {
//
// Copy data from segment to target buffer
error = transaction.segment->read(uio, transaction.segment_offset, transaction.bytes_to_read);
}
// Read from disk into segment missing in cache or empty segment that was in cache but had not data because
// of failure to read
else {
error = transaction.segment->read_from_disk(device);
#if defined(ROFS_DIAGNOSTICS_ENABLED)
rofs_cache_misses += 1;
#endif
//
// Copy data from segment to target buffer
if (!error) {
error = transaction.segment->read(uio, transaction.segment_offset, transaction.bytes_to_read);
}
}

if (error) {
break;
}
}

print("[rofs] [%d] rofs_cache_read completed for i-node [%d]\n", sched::thread::current()->id(),
inode->inode_no);
return error;
}

}
7 changes: 7 additions & 0 deletions fs/rofs/rofs_vfsops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ static int rofs_unmount(struct mount *mp, int flags);
#if defined(ROFS_DIAGNOSTICS_ENABLED)
std::atomic<long> rofs_block_read_ms(0);
std::atomic<long> rofs_block_read_count(0);
std::atomic<long> rofs_block_allocated(0);
std::atomic<long> rofs_cache_reads(0);
std::atomic<long> rofs_cache_misses(0);
#endif

struct vfsops rofs_vfsops = {
Expand Down Expand Up @@ -211,6 +214,10 @@ rofs_unmount(struct mount *mp, int flags)
#if defined(ROFS_DIAGNOSTICS_ENABLED)
debugf("ROFS: spent %.2f ms reading from disk\n", ((double) rofs_block_read_ms.load()) / 1000);
debugf("ROFS: read %d 512-byte blocks from disk\n", rofs_block_read_count.load());
debugf("ROFS: allocated %d 512-byte blocks of cache memory\n", rofs_block_allocated.load());
long total_cache_reads = rofs_cache_reads.load();
double hit_ratio = total_cache_reads > 0 ? (rofs_cache_reads.load() - rofs_cache_misses.load()) / ((double)total_cache_reads) : 0;
debugf("ROFS: hit ratio is %.2f%%\n", hit_ratio * 100);
#endif
return error;
}
21 changes: 19 additions & 2 deletions fs/rofs/rofs_vnops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int rofs_readlink(struct vnode *vnode, struct uio *uio)
//
// This function reads as much data as requested per uio in single read from the disk but
// the data does not get retained for subsequent reads
static int rofs_read(struct vnode *vnode, struct file *fp, struct uio *uio, int ioflag)
static int rofs_read_without_cache(struct vnode *vnode, struct file *fp, struct uio *uio, int ioflag)
{
struct rofs_info *rofs = (struct rofs_info *) vnode->v_mount->m_data;
struct rofs_super_block *sb = rofs->sb;
Expand Down Expand Up @@ -147,7 +147,20 @@ static int rofs_read(struct vnode *vnode, struct file *fp, struct uio *uio, int
free(buf);
return rv;
}
//
// This version of read function reads more data than needed per uio following simple
// "read-around" logic. The data gets retained in cache and retrieved from memory
// by subsequent or contiguous reads. For details look at rofs_cache.cc.
static int rofs_read_with_cache(struct vnode *vnode, struct file* fp, struct uio *uio, int ioflag) {
struct rofs_info *rofs = (struct rofs_info *) vnode->v_mount->m_data;
struct rofs_super_block *sb = rofs->sb;
struct rofs_inode *inode = (struct rofs_inode *) vnode->v_data;
struct device *device = vnode->v_mount->m_dev;

VERIFY_READ_INPUT_ARGUMENTS()

return rofs::cache_read(inode,device,sb,uio);
}
//
// This functions reads directory information (dentries) based on information in memory
// under rofs->dir_entries table
Expand Down Expand Up @@ -279,7 +292,7 @@ static int rofs_getattr(struct vnode *vnode, struct vattr *attr)
struct vnops rofs_vnops = {
rofs_open, /* open */
rofs_close, /* close */
rofs_read, /* read */
rofs_read_with_cache, /* read */
rofs_write, /* write - returns error when called */
rofs_seek, /* seek */
rofs_ioctl, /* ioctl */
Expand All @@ -301,3 +314,7 @@ struct vnops rofs_vnops = {
rofs_readlink, /* read link */
rofs_symlink /* symbolic link - returns error when called*/
};

extern "C" void rofs_disable_cache() {
rofs_vnops.vop_read = rofs_read_without_cache;
}
Loading

0 comments on commit 1f122ae

Please sign in to comment.