2016-04-14 13:24:32 +05:30
|
|
|
#ifndef BLOCK_CACHE_IO_ENGINE_H
|
|
|
|
#define BLOCK_CACHE_IO_ENGINE_H
|
|
|
|
|
|
|
|
#include "base/unique_handle.h"
|
|
|
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2016-06-07 15:42:27 +05:30
|
|
|
#include <libaio.h>
|
2017-03-13 18:50:27 +05:30
|
|
|
#include <vector>
|
2016-04-14 13:24:32 +05:30
|
|
|
|
2018-05-22 18:32:49 +05:30
|
|
|
#include <limits.h>
|
|
|
|
#ifndef PAGE_SIZE
|
|
|
|
#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
|
|
|
|
#endif
|
|
|
|
|
2016-04-14 13:24:32 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace bcache {
|
|
|
|
using sector_t = uint64_t;
|
|
|
|
|
2016-06-14 20:56:37 +05:30
|
|
|
unsigned const SECTOR_SHIFT = 9;
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
// Virtual base class to aid unit testing
|
|
|
|
class io_engine {
|
|
|
|
public:
|
|
|
|
enum mode {
|
2016-06-14 20:56:37 +05:30
|
|
|
M_READ_ONLY,
|
|
|
|
M_READ_WRITE
|
2016-06-07 15:42:27 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
enum dir {
|
2016-06-14 20:56:37 +05:30
|
|
|
D_READ,
|
|
|
|
D_WRITE
|
2016-06-07 15:42:27 +05:30
|
|
|
};
|
|
|
|
|
2016-06-07 18:15:27 +05:30
|
|
|
enum sharing {
|
|
|
|
EXCLUSIVE,
|
|
|
|
SHARED
|
|
|
|
};
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
io_engine() {}
|
|
|
|
virtual ~io_engine() {}
|
|
|
|
|
|
|
|
using handle = unsigned;
|
|
|
|
|
2016-06-07 18:15:27 +05:30
|
|
|
virtual handle open_file(std::string const &path, mode m, sharing s = EXCLUSIVE) = 0;
|
2016-06-07 15:42:27 +05:30
|
|
|
virtual void close_file(handle h) = 0;
|
|
|
|
|
|
|
|
// returns false if there are insufficient resources to
|
|
|
|
// queue the IO
|
|
|
|
virtual bool issue_io(handle h, dir d, sector_t b, sector_t e, void *data, unsigned context) = 0;
|
|
|
|
|
|
|
|
// returns (success, context)
|
|
|
|
using wait_result = std::pair<bool, unsigned>;
|
2016-06-14 20:56:37 +05:30
|
|
|
virtual boost::optional<wait_result> wait() = 0;
|
|
|
|
virtual boost::optional<wait_result> wait(unsigned µsec) = 0;
|
2016-06-07 15:42:27 +05:30
|
|
|
|
|
|
|
private:
|
|
|
|
io_engine(io_engine const &) = delete;
|
|
|
|
io_engine &operator =(io_engine const &) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
2016-04-14 13:24:32 +05:30
|
|
|
|
|
|
|
class control_block_set {
|
|
|
|
public:
|
|
|
|
control_block_set(unsigned nr);
|
|
|
|
|
|
|
|
iocb *alloc(unsigned context);
|
|
|
|
void free(iocb *);
|
|
|
|
|
|
|
|
unsigned context(iocb *) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct cblock {
|
|
|
|
unsigned context;
|
|
|
|
struct iocb cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::set<unsigned> free_cbs_;
|
|
|
|
std::vector<cblock> cbs_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
class aio_engine : public io_engine {
|
2016-04-14 13:24:32 +05:30
|
|
|
public:
|
|
|
|
// max_io is the maximum nr of concurrent ios expected
|
2016-06-07 15:42:27 +05:30
|
|
|
aio_engine(unsigned max_io);
|
|
|
|
~aio_engine();
|
2016-04-14 13:24:32 +05:30
|
|
|
|
|
|
|
using handle = unsigned;
|
|
|
|
|
2016-06-07 18:15:27 +05:30
|
|
|
virtual handle open_file(std::string const &path, mode m, sharing s = EXCLUSIVE);
|
2016-06-07 15:42:27 +05:30
|
|
|
virtual void close_file(handle h);
|
2016-04-14 13:24:32 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
// Returns false if queueing the io failed
|
|
|
|
virtual bool issue_io(handle h, dir d, sector_t b, sector_t e, void *data, unsigned context);
|
2016-04-14 13:24:32 +05:30
|
|
|
|
2016-06-14 20:56:37 +05:30
|
|
|
virtual boost::optional<wait_result> wait();
|
|
|
|
virtual boost::optional<wait_result> wait(unsigned µsec);
|
2016-04-14 13:24:32 +05:30
|
|
|
|
|
|
|
private:
|
2016-06-14 20:56:37 +05:30
|
|
|
static struct timespec micro_to_ts(unsigned micro);
|
|
|
|
static unsigned ts_to_micro(timespec const &ts);
|
|
|
|
boost::optional<io_engine::wait_result> wait_(timespec *ts);
|
|
|
|
|
2016-04-14 13:24:32 +05:30
|
|
|
std::list<base::unique_fd> descriptors_;
|
|
|
|
|
|
|
|
io_context_t aio_context_;
|
|
|
|
control_block_set cbs_;
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
aio_engine(io_engine const &) = delete;
|
|
|
|
aio_engine &operator =(io_engine const &) = delete;
|
2016-04-14 13:24:32 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|