vitastor/test_blockstore.cpp

118 lines
3.1 KiB
C++
Raw Normal View History

2019-11-17 17:45:39 +03:00
#include <sys/timerfd.h>
#include <sys/poll.h>
#include <iostream>
#include "blockstore.h"
2019-11-17 17:45:39 +03:00
class timerfd_interval
{
int wait_state;
int timerfd;
int status;
ring_loop_t *ringloop;
ring_consumer_t consumer;
2019-11-18 02:36:53 +03:00
std::function<void(void)> callback;
2019-11-17 17:45:39 +03:00
public:
2019-11-18 02:36:53 +03:00
timerfd_interval(ring_loop_t *ringloop, int seconds, std::function<void(void)> cb)
2019-11-17 17:45:39 +03:00
{
wait_state = 0;
timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (timerfd < 0)
{
throw std::runtime_error(std::string("timerfd_create: ") + strerror(errno));
}
struct itimerspec exp = {
.it_interval = { seconds, 0 },
.it_value = { seconds, 0 },
};
if (timerfd_settime(timerfd, 0, &exp, NULL))
{
throw std::runtime_error(std::string("timerfd_settime: ") + strerror(errno));
}
consumer.loop = [this]() { loop(); };
ringloop->register_consumer(consumer);
this->ringloop = ringloop;
2019-11-18 02:36:53 +03:00
this->callback = cb;
2019-11-17 17:45:39 +03:00
}
~timerfd_interval()
{
2019-11-18 14:08:11 +03:00
ringloop->unregister_consumer(consumer);
2019-11-17 17:45:39 +03:00
close(timerfd);
}
void loop()
{
if (wait_state == 1)
{
return;
}
struct io_uring_sqe *sqe = ringloop->get_sqe();
if (!sqe)
{
wait_state = 0;
return;
}
struct ring_data_t *data = ((ring_data_t*)sqe->user_data);
my_uring_prep_poll_add(sqe, timerfd, POLLIN);
2019-11-17 17:45:39 +03:00
data->callback = [&](ring_data_t *data)
{
if (data->res < 0)
{
throw std::runtime_error(std::string("waiting for timer failed: ") + strerror(-data->res));
}
uint64_t n;
read(timerfd, &n, 8);
2019-11-17 17:45:39 +03:00
wait_state = 0;
2019-11-18 02:36:53 +03:00
callback();
2019-11-17 17:45:39 +03:00
};
wait_state = 1;
ringloop->submit();
}
};
int main(int narg, char *args[])
{
spp::sparse_hash_map<std::string, std::string> config;
config["meta_device"] = "./test_meta.bin";
config["journal_device"] = "./test_journal.bin";
config["data_device"] = "./test_data.bin";
ring_loop_t *ringloop = new ring_loop_t(512);
2019-11-17 22:26:55 +03:00
blockstore *bs = new blockstore(config, ringloop);
2019-11-18 02:36:53 +03:00
timerfd_interval tick_tfd(ringloop, 1, []()
{
printf("tick 1s\n");
});
blockstore_operation op;
op.flags = OP_WRITE;
op.oid = { .inode = 1, .stripe = 0 };
op.version = 0;
op.offset = 4096;
op.len = 4096;
op.buf = (uint8_t*)memalign(512, 4096);
memset(op.buf, 0xaa, 4096);
op.callback = [](blockstore_operation *op)
{
printf("completed %d\n", op->retval);
};
2019-11-18 14:08:11 +03:00
ring_consumer_t main_cons;
2019-11-18 02:36:53 +03:00
bool bs_was_done = false;
2019-11-18 14:08:11 +03:00
main_cons.loop = [&]()
2019-11-17 17:45:39 +03:00
{
2019-11-18 02:36:53 +03:00
bool bs_done = bs->is_started();
if (bs_done && !bs_was_done)
{
2019-11-18 14:08:11 +03:00
printf("init completed\n");
2019-11-18 02:36:53 +03:00
bs->enqueue_op(&op);
bs_was_done = true;
}
2019-11-18 14:08:11 +03:00
};
ringloop->register_consumer(main_cons);
while (true)
{
ringloop->loop();
2019-11-17 17:45:39 +03:00
}
2019-11-17 22:26:55 +03:00
delete bs;
delete ringloop;
return 0;
}