2016-03-17 20:45:52 +05:30
|
|
|
#include "block-cache/copier.h"
|
|
|
|
|
2016-04-14 13:24:32 +05:30
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace bcache;
|
|
|
|
using namespace boost;
|
|
|
|
using namespace std;
|
|
|
|
|
2016-03-17 20:45:52 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
copier::copier(io_engine &engine,
|
|
|
|
string const &src, string const &dest,
|
2021-01-17 22:41:01 +05:30
|
|
|
sector_t block_size, size_t mem,
|
|
|
|
sector_t src_offset, sector_t dest_offset)
|
2016-06-14 20:57:17 +05:30
|
|
|
: pool_(block_size * 512, mem, PAGE_SIZE),
|
2016-04-14 13:24:32 +05:30
|
|
|
block_size_(block_size),
|
2016-06-07 15:42:27 +05:30
|
|
|
engine_(engine),
|
2016-06-14 20:57:17 +05:30
|
|
|
src_handle_(engine_.open_file(src, io_engine::M_READ_ONLY)),
|
|
|
|
dest_handle_(engine_.open_file(dest, io_engine::M_READ_WRITE)),
|
2021-01-17 22:41:01 +05:30
|
|
|
src_offset_(src_offset), dest_offset_(dest_offset),
|
2016-04-14 13:24:32 +05:30
|
|
|
genkey_count_(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
copier::~copier()
|
|
|
|
{
|
|
|
|
engine_.close_file(src_handle_);
|
|
|
|
engine_.close_file(dest_handle_);
|
|
|
|
}
|
|
|
|
|
2016-04-14 13:24:32 +05:30
|
|
|
void
|
|
|
|
copier::issue(copy_op const &op)
|
|
|
|
{
|
2016-06-14 20:57:17 +05:30
|
|
|
void *data;
|
|
|
|
|
|
|
|
while (!(data = pool_.alloc())) {
|
2016-04-14 13:24:32 +05:30
|
|
|
wait_();
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
// data may still not be present because the wait_ could
|
|
|
|
// have completed a read and issued the corresponding
|
|
|
|
// write.
|
2016-04-14 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
2016-06-01 19:16:27 +05:30
|
|
|
copy_job job(op, data);
|
2016-04-14 13:24:32 +05:30
|
|
|
job.op.read_complete = job.op.write_complete = false;
|
|
|
|
unsigned key = genkey(); // used as context for the io_engine
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
auto r = engine_.issue_io(src_handle_,
|
2016-06-14 20:57:17 +05:30
|
|
|
io_engine::D_READ,
|
2021-01-17 22:41:01 +05:30
|
|
|
to_src_sector(op.src_b),
|
|
|
|
to_src_sector(op.src_e),
|
2016-06-07 15:42:27 +05:30
|
|
|
data,
|
|
|
|
key);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
jobs_.insert(make_pair(key, job));
|
2016-06-14 20:57:17 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
else
|
|
|
|
complete(job);
|
2016-04-14 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
copier::nr_pending() const
|
|
|
|
{
|
|
|
|
return jobs_.size() + complete_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<copy_op>
|
|
|
|
copier::wait()
|
|
|
|
{
|
2016-06-14 20:57:17 +05:30
|
|
|
if (complete_.empty())
|
2016-04-14 13:24:32 +05:30
|
|
|
wait_();
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
return wait_complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<copy_op>
|
|
|
|
copier::wait(unsigned µ)
|
|
|
|
{
|
2016-04-14 13:24:32 +05:30
|
|
|
if (complete_.empty())
|
2016-06-14 20:57:17 +05:30
|
|
|
wait_(micro);
|
|
|
|
return wait_complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
copier::pending() const
|
|
|
|
{
|
|
|
|
return !jobs_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<copy_op>
|
|
|
|
copier::wait_complete()
|
|
|
|
{
|
|
|
|
if (complete_.empty()) {
|
2016-04-14 13:24:32 +05:30
|
|
|
return optional<copy_op>();
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
} else {
|
2016-04-14 13:24:32 +05:30
|
|
|
auto op = complete_.front();
|
|
|
|
complete_.pop_front();
|
|
|
|
return optional<copy_op>(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
void
|
|
|
|
copier::wait_(unsigned µ)
|
|
|
|
{
|
|
|
|
optional<io_engine::wait_result> mp;
|
|
|
|
|
|
|
|
if (!pending())
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
bool completed = false;
|
|
|
|
while (pending() && !completed) {
|
|
|
|
mp = engine_.wait(micro);
|
|
|
|
if (mp)
|
|
|
|
completed = wait_successful(*mp);
|
|
|
|
|
|
|
|
if (!micro)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 13:24:32 +05:30
|
|
|
void
|
|
|
|
copier::wait_()
|
|
|
|
{
|
2016-06-14 20:57:17 +05:30
|
|
|
bool completed = false;
|
|
|
|
|
|
|
|
while (pending() && !completed) {
|
|
|
|
auto mp = engine_.wait();
|
|
|
|
if (mp)
|
|
|
|
completed = wait_successful(*mp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
copier::wait_successful(io_engine::wait_result const &p)
|
|
|
|
{
|
2016-04-14 13:24:32 +05:30
|
|
|
auto it = jobs_.find(p.second);
|
|
|
|
if (it == jobs_.end())
|
|
|
|
throw runtime_error("Internal error. Lost track of copy job.");
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
copy_job &j = it->second;
|
2016-04-14 13:24:32 +05:30
|
|
|
if (!p.first) {
|
|
|
|
// IO was unsuccessful
|
|
|
|
complete(j);
|
2016-06-07 15:42:27 +05:30
|
|
|
jobs_.erase(it);
|
2016-06-14 20:57:17 +05:30
|
|
|
return true;
|
2016-04-14 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IO was successful
|
|
|
|
if (!j.op.read_complete) {
|
|
|
|
j.op.read_complete = true;
|
2016-06-07 15:42:27 +05:30
|
|
|
if (!engine_.issue_io(dest_handle_,
|
2016-06-14 20:57:17 +05:30
|
|
|
io_engine::D_WRITE,
|
2021-01-17 22:41:01 +05:30
|
|
|
to_dest_sector(j.op.dest_b),
|
|
|
|
to_dest_sector(j.op.dest_b + (j.op.src_e - j.op.src_b)),
|
2016-06-07 15:42:27 +05:30
|
|
|
j.data,
|
|
|
|
it->first)) {
|
|
|
|
complete(j);
|
|
|
|
jobs_.erase(it);
|
2016-06-14 20:57:17 +05:30
|
|
|
return true;
|
2016-06-07 15:42:27 +05:30
|
|
|
}
|
2016-06-14 20:57:17 +05:30
|
|
|
return false;
|
2016-04-14 13:24:32 +05:30
|
|
|
|
|
|
|
} else {
|
|
|
|
j.op.write_complete = true;
|
|
|
|
complete(j);
|
2016-06-07 15:42:27 +05:30
|
|
|
jobs_.erase(it);
|
2016-06-14 20:57:17 +05:30
|
|
|
return true;
|
2016-04-14 13:24:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
copier::complete(copy_job const &j)
|
|
|
|
{
|
|
|
|
pool_.free(j.data);
|
|
|
|
complete_.push_back(j.op);
|
|
|
|
}
|
|
|
|
|
|
|
|
sector_t
|
2021-01-17 22:41:01 +05:30
|
|
|
copier::to_src_sector(block_address b) const
|
2016-04-14 13:24:32 +05:30
|
|
|
{
|
2021-01-17 22:41:01 +05:30
|
|
|
return src_offset_ + b * block_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
sector_t
|
|
|
|
copier::to_dest_sector(block_address b) const
|
|
|
|
{
|
|
|
|
return dest_offset_ + b * block_size_;
|
2016-04-14 13:24:32 +05:30
|
|
|
}
|
2016-03-17 20:45:52 +05:30
|
|
|
|
2016-04-14 13:24:32 +05:30
|
|
|
unsigned
|
|
|
|
copier::genkey()
|
|
|
|
{
|
|
|
|
return genkey_count_++;
|
|
|
|
}
|
2016-03-17 20:45:52 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|