fio engine starts now

This commit is contained in:
Vitaliy Filippov
2019-11-26 02:18:42 +03:00
parent a7dc759f74
commit a8f250c68f
7 changed files with 106 additions and 81 deletions
+2 -69
View File
@@ -1,75 +1,7 @@
#include <sys/timerfd.h>
#include <sys/poll.h>
#include <iostream>
#include "timerfd_interval.h"
#include "blockstore.h"
class timerfd_interval
{
int wait_state;
int timerfd;
int status;
ring_loop_t *ringloop;
ring_consumer_t consumer;
std::function<void(void)> callback;
public:
timerfd_interval(ring_loop_t *ringloop, int seconds, std::function<void(void)> cb)
{
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;
this->callback = cb;
}
~timerfd_interval()
{
ringloop->unregister_consumer(consumer);
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);
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);
wait_state = 0;
callback();
};
wait_state = 1;
ringloop->submit();
}
};
int main(int narg, char *args[])
{
spp::sparse_hash_map<std::string, std::string> config;
@@ -141,6 +73,7 @@ int main(int narg, char *args[])
while (1)
{
ringloop->loop();
ringloop->wait();
}
delete bs;
delete ringloop;