2014-07-22 21:13:44 +05:30
|
|
|
#ifndef BLOCK_CACHE_H
|
|
|
|
#define BLOCK_CACHE_H
|
|
|
|
|
2016-02-24 20:01:51 +05:30
|
|
|
#include "base/container_of.h"
|
2020-02-22 15:07:22 +05:30
|
|
|
#include "base/file_utils.h"
|
2020-04-30 19:00:01 +05:30
|
|
|
#include "block-cache/io_engine.h"
|
2014-07-22 21:13:44 +05:30
|
|
|
|
2016-02-04 14:27:41 +05:30
|
|
|
#include <boost/intrusive/list.hpp>
|
|
|
|
#include <boost/intrusive/set.hpp>
|
2014-07-25 15:05:04 +05:30
|
|
|
#include <boost/noncopyable.hpp>
|
2016-02-04 14:27:41 +05:30
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
2014-07-25 15:05:04 +05:30
|
|
|
#include <libaio.h>
|
|
|
|
#include <memory>
|
2016-02-04 14:27:41 +05:30
|
|
|
#include <stdexcept>
|
2014-07-25 15:05:04 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <vector>
|
2015-08-24 15:48:31 +05:30
|
|
|
#include <iostream>
|
2014-07-22 21:13:44 +05:30
|
|
|
|
2016-02-04 14:27:41 +05:30
|
|
|
namespace bi = boost::intrusive;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
namespace bcache {
|
2014-07-25 19:16:51 +05:30
|
|
|
typedef uint64_t block_address;
|
|
|
|
typedef uint64_t sector_t;
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
class validator {
|
|
|
|
public:
|
2020-04-30 19:32:43 +05:30
|
|
|
typedef std::shared_ptr<validator> ptr;
|
2014-07-22 21:13:44 +05:30
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
virtual ~validator() {}
|
2014-07-22 21:13:44 +05:30
|
|
|
|
2014-07-25 19:16:51 +05:30
|
|
|
virtual void check(void const *data, block_address location) const = 0;
|
2016-02-27 12:51:13 +05:30
|
|
|
virtual bool check_raw(void const *data) const = 0;
|
2014-07-25 19:16:51 +05:30
|
|
|
virtual void prepare(void *data, block_address location) const = 0;
|
2014-07-25 15:05:04 +05:30
|
|
|
};
|
2014-07-22 21:13:44 +05:30
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
class noop_validator : public validator {
|
|
|
|
public:
|
2014-07-25 19:16:51 +05:30
|
|
|
void check(void const *data, block_address location) const {}
|
2016-02-27 12:51:13 +05:30
|
|
|
bool check_raw(void const *data) const {return true;}
|
2014-07-25 19:16:51 +05:30
|
|
|
void prepare(void *data, block_address location) const {}
|
2014-07-25 15:05:04 +05:30
|
|
|
};
|
2014-07-25 19:16:51 +05:30
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
class block_cache : private boost::noncopyable {
|
|
|
|
public:
|
|
|
|
enum block_flags {
|
2014-07-28 18:43:28 +05:30
|
|
|
BF_IO_PENDING = (1 << 0),
|
|
|
|
BF_DIRTY = (1 << 1),
|
|
|
|
BF_FLUSH = (1 << 2),
|
2014-07-25 15:05:04 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
class block : private boost::noncopyable {
|
|
|
|
public:
|
2014-07-25 19:16:51 +05:30
|
|
|
block()
|
|
|
|
: v_() {
|
2016-02-04 14:27:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool operator <(block const &rhs) const {
|
|
|
|
return index_ > rhs.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(block const &rhs) const {
|
|
|
|
return index_ == rhs.index_;
|
2014-07-25 19:16:51 +05:30
|
|
|
}
|
|
|
|
|
2014-07-28 19:02:33 +05:30
|
|
|
// Do not give this class a destructor, it wont get
|
|
|
|
// called because we manage allocation ourselves.
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
uint64_t get_index() const {
|
|
|
|
return index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *get_data() const {
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
2014-07-28 18:43:28 +05:30
|
|
|
void mark_dirty() {
|
2014-07-28 19:02:33 +05:30
|
|
|
set_flags(BF_DIRTY);
|
2014-07-28 18:43:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void set_flags(unsigned flags) {
|
|
|
|
flags_ |= flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned test_flags(unsigned flags) const {
|
|
|
|
return flags_ & flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_flags(unsigned flags) {
|
|
|
|
flags_ &= ~flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get() {
|
|
|
|
ref_count_++;
|
|
|
|
};
|
|
|
|
|
|
|
|
void put() {
|
|
|
|
if (!ref_count_)
|
|
|
|
throw std::runtime_error("bad put");
|
|
|
|
|
|
|
|
if (!--ref_count_)
|
|
|
|
bc_->release(*this);
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:32:42 +05:30
|
|
|
void unlink_set() {
|
|
|
|
set_hook_.unlink();
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:27:41 +05:30
|
|
|
void unlink() {
|
|
|
|
list_hook_.unlink();
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
private:
|
|
|
|
friend class block_cache;
|
2016-02-04 14:27:41 +05:30
|
|
|
friend class cmp_index;
|
2014-07-25 15:05:04 +05:30
|
|
|
|
2014-07-28 18:43:28 +05:30
|
|
|
block_cache *bc_;
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
uint64_t index_;
|
|
|
|
void *data_;
|
|
|
|
|
2016-02-04 14:27:41 +05:30
|
|
|
bi::list_member_hook<bi::link_mode<bi::auto_unlink>> list_hook_;
|
2016-02-04 14:32:42 +05:30
|
|
|
bi::set_member_hook<bi::link_mode<bi::auto_unlink>> set_hook_;
|
2014-07-25 15:05:04 +05:30
|
|
|
|
|
|
|
unsigned ref_count_;
|
|
|
|
|
|
|
|
int error_;
|
|
|
|
unsigned flags_;
|
|
|
|
|
|
|
|
iocb control_block_;
|
2014-07-25 19:16:51 +05:30
|
|
|
validator::ptr v_;
|
2014-07-25 15:05:04 +05:30
|
|
|
};
|
|
|
|
|
2016-02-04 14:27:41 +05:30
|
|
|
struct cmp_index {
|
|
|
|
bool operator()(block_address index, block const &b) const {
|
|
|
|
return index > b.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(block const &b, block_address index) const {
|
|
|
|
return b.index_ > index;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class auto_block {
|
|
|
|
public:
|
|
|
|
auto_block()
|
|
|
|
: b_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_block(block &b)
|
|
|
|
: b_(&b) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~auto_block() {
|
|
|
|
put();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_block &operator =(block &b) {
|
|
|
|
put();
|
|
|
|
b_ = &b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *get_data() const {
|
|
|
|
if (b_)
|
|
|
|
return b_->get_data();
|
|
|
|
|
|
|
|
throw std::runtime_error("auto_block not set");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void put() {
|
|
|
|
if (b_) {
|
|
|
|
b_->put();
|
|
|
|
b_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block *b_;
|
|
|
|
};
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
2020-02-22 15:07:22 +05:30
|
|
|
block_cache(file_utils::file_descriptor &fd, sector_t block_size,
|
2014-07-25 15:05:04 +05:30
|
|
|
uint64_t max_nr_blocks, size_t mem);
|
|
|
|
~block_cache();
|
|
|
|
|
|
|
|
uint64_t get_nr_blocks() const;
|
2014-08-21 16:48:05 +05:30
|
|
|
uint64_t get_nr_locked() const;
|
2014-07-25 15:05:04 +05:30
|
|
|
|
|
|
|
enum get_flags {
|
|
|
|
GF_ZERO = (1 << 0),
|
2014-07-28 18:43:28 +05:30
|
|
|
GF_DIRTY = (1 << 1),
|
2014-07-29 16:04:26 +05:30
|
|
|
GF_BARRIER = (1 << 2)
|
2014-07-25 15:05:04 +05:30
|
|
|
};
|
|
|
|
|
2014-07-25 19:16:51 +05:30
|
|
|
block_cache::block &get(block_address index, unsigned flags, validator::ptr v);
|
2014-07-25 15:05:04 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush can fail if an earlier write failed. You do not know which block
|
|
|
|
* failed. Make sure you build your recovery with this in mind.
|
|
|
|
*/
|
|
|
|
int flush();
|
2014-07-25 19:16:51 +05:30
|
|
|
void prefetch(block_address index);
|
2014-07-25 15:05:04 +05:30
|
|
|
|
|
|
|
private:
|
2016-02-04 14:27:41 +05:30
|
|
|
typedef bi::member_hook<block,
|
|
|
|
bi::list_member_hook<bi::link_mode<bi::auto_unlink>>,
|
|
|
|
&block::list_hook_> list_hook_option;
|
|
|
|
typedef bi::list<block, list_hook_option,
|
|
|
|
bi::constant_time_size<false>> block_list;
|
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
int init_free_list(unsigned count);
|
|
|
|
block *__alloc_block();
|
|
|
|
void complete_io(block &b, int result);
|
2014-07-29 18:11:45 +05:30
|
|
|
void issue_low_level(block &b, enum io_iocb_cmd opcode, const char *desc);
|
|
|
|
void issue_read(block &b);
|
|
|
|
void issue_write(block &b);
|
2014-07-25 15:05:04 +05:30
|
|
|
void wait_io();
|
2017-08-14 18:14:21 +05:30
|
|
|
void unlink_block(block &b);
|
|
|
|
void link_block(block &b);
|
2017-08-14 18:28:12 +05:30
|
|
|
void relink(block &b);
|
2014-07-25 15:05:04 +05:30
|
|
|
void wait_all();
|
|
|
|
void wait_specific(block &b);
|
|
|
|
unsigned writeback(unsigned count);
|
|
|
|
void setup_control_block(block &b);
|
2014-07-29 18:11:45 +05:30
|
|
|
block *find_unused_clean_block();
|
2014-07-25 19:16:51 +05:30
|
|
|
block *new_block(block_address index);
|
2014-07-25 15:05:04 +05:30
|
|
|
void mark_dirty(block &b);
|
|
|
|
unsigned calc_nr_cache_blocks(size_t mem, sector_t block_size);
|
|
|
|
unsigned calc_nr_buckets(unsigned nr_blocks);
|
|
|
|
void zero_block(block &b);
|
2014-07-25 19:16:51 +05:30
|
|
|
block *lookup_or_read_block(block_address index, unsigned flags, validator::ptr v);
|
2016-02-04 14:27:41 +05:30
|
|
|
void exit_free_list();
|
2014-07-28 18:43:28 +05:30
|
|
|
|
|
|
|
void preemptive_writeback();
|
2017-08-14 18:14:21 +05:30
|
|
|
bool maybe_flush(block_cache::block &b);
|
2014-07-28 18:43:28 +05:30
|
|
|
void release(block_cache::block &block);
|
|
|
|
void check_index(block_address index) const;
|
2014-07-25 15:05:04 +05:30
|
|
|
|
2017-08-14 18:28:12 +05:30
|
|
|
void hit(block &b, unsigned flags);
|
|
|
|
void miss(unsigned flags);
|
2014-07-31 16:48:01 +05:30
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
2020-02-22 15:07:22 +05:30
|
|
|
file_utils::file_descriptor &fd_;
|
2014-07-25 15:05:04 +05:30
|
|
|
sector_t block_size_;
|
|
|
|
uint64_t nr_data_blocks_;
|
|
|
|
uint64_t nr_cache_blocks_;
|
|
|
|
|
2016-02-04 14:27:41 +05:30
|
|
|
std::unique_ptr<std::vector<block>> blocks_memory_;
|
|
|
|
unsigned char *blocks_data_;
|
2014-07-25 15:05:04 +05:30
|
|
|
|
|
|
|
io_context_t aio_context_;
|
|
|
|
std::vector<io_event> events_;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Blocks on the free list are not initialised, apart from the
|
|
|
|
* b.data field.
|
|
|
|
*/
|
2016-02-04 14:27:41 +05:30
|
|
|
block_list free_;
|
|
|
|
block_list errored_;
|
|
|
|
block_list dirty_;
|
|
|
|
block_list clean_;
|
2014-07-25 15:05:04 +05:30
|
|
|
|
2017-08-14 14:47:56 +05:30
|
|
|
// Because the block_list type doesn't have a constant time
|
|
|
|
// size() method, we have to manually keep track of the list
|
|
|
|
// sizes (tedious and error prone).
|
2014-07-29 16:04:26 +05:30
|
|
|
unsigned nr_locked_;
|
2017-08-14 14:43:32 +05:30
|
|
|
unsigned nr_dirty_;
|
2014-07-25 19:16:51 +05:30
|
|
|
|
2017-08-14 14:43:32 +05:30
|
|
|
unsigned nr_io_pending_;
|
2016-02-04 14:27:41 +05:30
|
|
|
block_list io_pending_;
|
|
|
|
|
|
|
|
typedef bi::member_hook<block,
|
2016-02-04 14:32:42 +05:30
|
|
|
bi::set_member_hook<bi::link_mode<bi::auto_unlink>>,
|
2016-02-04 14:27:41 +05:30
|
|
|
&block::set_hook_> block_option;
|
|
|
|
typedef bi::set<block, block_option,
|
|
|
|
bi::constant_time_size<false>> block_set;
|
|
|
|
block_set block_set_;
|
2014-07-31 16:48:01 +05:30
|
|
|
|
|
|
|
// Stats
|
|
|
|
unsigned read_hits_;
|
|
|
|
unsigned read_misses_;
|
|
|
|
unsigned write_zeroes_;
|
|
|
|
unsigned write_hits_;
|
|
|
|
unsigned write_misses_;
|
|
|
|
unsigned prefetches_;
|
2014-08-21 18:50:36 +05:30
|
|
|
|
|
|
|
validator::ptr noop_validator_;
|
2014-07-25 15:05:04 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
2014-07-22 21:13:44 +05:30
|
|
|
|
|
|
|
#endif
|