whitespace

This commit is contained in:
Joe Thornber 2014-07-30 12:27:33 +01:00
parent 74de9a1a94
commit eee9004354

View File

@ -13,6 +13,8 @@
#include <stdexcept>
#include <sstream>
using namespace bcache;
//----------------------------------------------------------------
// FIXME: get from linux headers
@ -53,10 +55,9 @@ namespace {
//----------------------------------------------------------------
namespace bcache {
int
block_cache::init_free_list(unsigned count)
{
int
block_cache::init_free_list(unsigned count)
{
size_t len;
block *blocks;
size_t block_size = block_size_ << SECTOR_SHIFT;
@ -88,11 +89,11 @@ namespace bcache {
}
return 0;
}
}
block_cache::block *
block_cache::__alloc_block()
{
block_cache::block *
block_cache::__alloc_block()
{
block *b;
if (list_empty(&free_))
@ -102,9 +103,9 @@ namespace bcache {
list_del(&b->list_);
return b;
}
}
/*----------------------------------------------------------------
/*----------------------------------------------------------------
* Low level IO handling
*
* We cannot have two concurrent writes on the same block.
@ -117,14 +118,14 @@ namespace bcache {
*
*--------------------------------------------------------------*/
/*
/*
* This can be called from the context of the aio thread. So we have a
* separate 'top half' complete function that we know is only called by the
* main cache thread.
*/
void
block_cache::complete_io(block &b, int result)
{
void
block_cache::complete_io(block &b, int result)
{
b.error_ = result;
b.clear_flags(BF_IO_PENDING);
nr_io_pending_--;
@ -139,20 +140,19 @@ namespace bcache {
list_move_tail(&b.list_, &clean_);
}
}
}
/*
/*
* |b->list| should be valid (either pointing to itself, on one of the other
* lists.
*/
// FIXME: add batch issue
void
block_cache::issue_low_level(block &b, enum io_iocb_cmd opcode, const char *desc)
{
// FIXME: add batch issue
void
block_cache::issue_low_level(block &b, enum io_iocb_cmd opcode, const char *desc)
{
int r;
iocb *control_blocks[1];
// FIXME: put this back in
assert(!b.test_flags(BF_IO_PENDING));
b.set_flags(BF_IO_PENDING);
nr_io_pending_++;
@ -173,26 +173,26 @@ namespace bcache {
out << "couldn't issue io (" << desc << ") for block " << b.index_;
throw std::runtime_error(out.str());
}
}
}
void
block_cache::issue_read(block &b)
{
void
block_cache::issue_read(block &b)
{
assert(!b.test_flags(BF_IO_PENDING));
issue_low_level(b, IO_CMD_PREAD, "read");
}
}
void
block_cache::issue_write(block &b)
{
void
block_cache::issue_write(block &b)
{
assert(!b.test_flags(BF_IO_PENDING));
b.v_->prepare(b.data_, b.index_);
issue_low_level(b, IO_CMD_PWRITE, "write");
}
}
void
block_cache::wait_io()
{
void
block_cache::wait_io()
{
int r;
unsigned i;
@ -223,51 +223,51 @@ namespace bcache {
exit(1);
}
}
}
}
/*----------------------------------------------------------------
/*----------------------------------------------------------------
* Clean/dirty list management
*--------------------------------------------------------------*/
/*
/*
* We're using lru lists atm, but I think it would be worth
* experimenting with a multiqueue approach.
*/
list_head *
block_cache::__categorise(block &b)
{
list_head *
block_cache::__categorise(block &b)
{
if (b.error_)
return &errored_;
return b.test_flags(BF_DIRTY) ? &dirty_ : &clean_;
}
}
void
block_cache::hit(block &b)
{
void
block_cache::hit(block &b)
{
list_move_tail(&b.list_, __categorise(b));
}
}
/*----------------------------------------------------------------
/*----------------------------------------------------------------
* High level IO handling
*--------------------------------------------------------------*/
void
block_cache::wait_all()
{
void
block_cache::wait_all()
{
while (!list_empty(&io_pending_))
wait_io();
}
}
void
block_cache::wait_specific(block &b)
{
void
block_cache::wait_specific(block &b)
{
while (b.test_flags(BF_IO_PENDING))
wait_io();
}
}
unsigned
block_cache::writeback(unsigned count)
{
unsigned
block_cache::writeback(unsigned count)
{
block *b, *tmp;
unsigned actual = 0, dirty_length = 0;
@ -288,18 +288,18 @@ namespace bcache {
info("writeback: requested %u, actual %u, dirty length %u\n", count, actual, dirty_length);
return actual;
}
}
/*----------------------------------------------------------------
/*----------------------------------------------------------------
* Hash table
*---------------------------------------------------------------*/
/*
/*
* |nr_buckets| must be a power of two.
*/
void
block_cache::hash_init(unsigned nr_buckets)
{
void
block_cache::hash_init(unsigned nr_buckets)
{
unsigned i;
nr_buckets_ = nr_buckets;
@ -307,18 +307,18 @@ namespace bcache {
for (i = 0; i < nr_buckets; i++)
INIT_LIST_HEAD(&buckets_[i]);
}
}
unsigned
block_cache::hash(uint64_t index)
{
unsigned
block_cache::hash(uint64_t index)
{
const unsigned BIG_PRIME = 4294967291UL;
return (((unsigned) index) * BIG_PRIME) & mask_;
}
}
block_cache::block *
block_cache::hash_lookup(block_address index)
{
block_cache::block *
block_cache::hash_lookup(block_address index)
{
block *b;
unsigned bucket = hash(index);
@ -328,27 +328,27 @@ namespace bcache {
}
return NULL;
}
}
void
block_cache::hash_insert(block &b)
{
void
block_cache::hash_insert(block &b)
{
unsigned bucket = hash(b.index_);
list_move_tail(&b.hash_list_, &buckets_[bucket]);
}
}
void
block_cache::hash_remove(block &b)
{
void
block_cache::hash_remove(block &b)
{
list_del_init(&b.hash_list_);
}
}
/*----------------------------------------------------------------
/*----------------------------------------------------------------
* High level allocation
*--------------------------------------------------------------*/
void
block_cache::setup_control_block(block &b)
{
void
block_cache::setup_control_block(block &b)
{
iocb *cb = &b.control_block_;
size_t block_size_bytes = block_size_ << SECTOR_SHIFT;
@ -358,11 +358,11 @@ namespace bcache {
cb->u.c.buf = b.data_;
cb->u.c.offset = block_size_bytes * b.index_;
cb->u.c.nbytes = block_size_bytes;
}
}
block_cache::block *
block_cache::find_unused_clean_block()
{
block_cache::block *
block_cache::find_unused_clean_block()
{
struct block *b, *tmp;
list_for_each_entry_safe (b, tmp, &clean_, list_) {
@ -375,11 +375,11 @@ namespace bcache {
}
return NULL;
}
}
block_cache::block *
block_cache::new_block(block_address index)
{
block_cache::block *
block_cache::new_block(block_address index)
{
block *b;
b = __alloc_block();
@ -410,23 +410,23 @@ namespace bcache {
}
return b;
}
}
/*----------------------------------------------------------------
/*----------------------------------------------------------------
* Block reference counting
*--------------------------------------------------------------*/
unsigned
block_cache::calc_nr_cache_blocks(size_t mem, sector_t block_size)
{
unsigned
block_cache::calc_nr_cache_blocks(size_t mem, sector_t block_size)
{
size_t space_per_block = (block_size << SECTOR_SHIFT) + sizeof(block);
unsigned r = mem / space_per_block;
return (r < MIN_BLOCKS) ? MIN_BLOCKS : r;
}
}
unsigned
block_cache::calc_nr_buckets(unsigned nr_blocks)
{
unsigned
block_cache::calc_nr_buckets(unsigned nr_blocks)
{
unsigned r = 8;
unsigned n = nr_blocks / 4;
@ -437,13 +437,13 @@ namespace bcache {
r <<= 1;
return r;
}
}
block_cache::block_cache(int fd, sector_t block_size, uint64_t on_disk_blocks, size_t mem)
block_cache::block_cache(int fd, sector_t block_size, uint64_t on_disk_blocks, size_t mem)
: nr_locked_(0),
nr_dirty_(0),
nr_io_pending_(0)
{
{
int r;
unsigned nr_cache_blocks = calc_nr_cache_blocks(mem, block_size);
unsigned nr_buckets = calc_nr_buckets(nr_cache_blocks);
@ -474,10 +474,10 @@ namespace bcache {
r = init_free_list(nr_cache_blocks);
if (r)
throw std::runtime_error("couldn't allocate blocks");
}
}
block_cache::~block_cache()
{
block_cache::~block_cache()
{
assert(!nr_locked_);
flush();
wait_all();
@ -492,25 +492,25 @@ namespace bcache {
io_destroy(aio_context_);
::close(fd_);
}
}
uint64_t
block_cache::get_nr_blocks() const
{
uint64_t
block_cache::get_nr_blocks() const
{
return nr_data_blocks_;
}
}
void
block_cache::zero_block(block &b)
{
void
block_cache::zero_block(block &b)
{
memset(b.data_, 0, block_size_ << SECTOR_SHIFT);
b.mark_dirty();
}
}
block_cache::block *
block_cache::lookup_or_read_block(block_address index, unsigned flags,
block_cache::block *
block_cache::lookup_or_read_block(block_address index, unsigned flags,
validator::ptr v)
{
{
block *b = hash_lookup(index);
if (b) {
@ -545,11 +545,11 @@ namespace bcache {
}
return (!b || b->error_) ? NULL : b;
}
}
block_cache::block &
block_cache::get(block_address index, unsigned flags, validator::ptr v)
{
block_cache::block &
block_cache::get(block_address index, unsigned flags, validator::ptr v)
{
check_index(index);
block *b = lookup_or_read_block(index, flags, v);
@ -575,20 +575,20 @@ namespace bcache {
}
throw std::runtime_error("couldn't get block");
}
}
void
block_cache::preemptive_writeback()
{
void
block_cache::preemptive_writeback()
{
unsigned nr_available = nr_cache_blocks_ - (nr_dirty_ - nr_io_pending_);
if (nr_available < (WRITEBACK_LOW_THRESHOLD_PERCENT * nr_cache_blocks_ / 100))
writeback((WRITEBACK_HIGH_THRESHOLD_PERCENT * nr_cache_blocks_ / 100) - nr_available);
}
}
void
block_cache::release(block_cache::block &b)
{
void
block_cache::release(block_cache::block &b)
{
assert(!b.ref_count_);
nr_locked_--;
@ -610,11 +610,11 @@ namespace bcache {
b.clear_flags(BF_FLUSH);
}
}
}
int
block_cache::flush()
{
int
block_cache::flush()
{
block *b, *tmp;
list_for_each_entry_safe (b, tmp, &dirty_, list_) {
@ -628,11 +628,11 @@ namespace bcache {
wait_all();
return list_empty(&errored_) ? 0 : -EIO;
}
}
void
block_cache::prefetch(block_address index)
{
void
block_cache::prefetch(block_address index)
{
check_index(index);
block *b = hash_lookup(index);
@ -642,18 +642,17 @@ namespace bcache {
if (b)
issue_read(*b);
}
}
}
void
block_cache::check_index(block_address index) const
{
void
block_cache::check_index(block_address index) const
{
if (index >= nr_data_blocks_) {
std::ostringstream out;
out << "block out of bounds ("
<< index << " >= " << nr_data_blocks_ << ")\n";
throw std::runtime_error(out.str());
}
}
}
//----------------------------------------------------------------