2018-08-22 11:29:39 +02:00
|
|
|
/** Run tasks periodically.
|
|
|
|
*
|
2022-12-14 17:39:07 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:05:42 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-05-19 17:40:10 +02:00
|
|
|
* @license Apache License 2.0
|
2018-08-22 11:29:39 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <cerrno>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-04-23 12:57:51 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-10-27 20:23:47 +01:00
|
|
|
#include <villas/task.hpp>
|
2021-08-11 12:40:19 -04:00
|
|
|
#include <villas/timing.hpp>
|
2019-10-27 20:23:47 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
|
|
|
|
using namespace villas;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
#include <sys/timerfd.h>
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // PERIODIC_TASK_IMPL
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
Task::Task(int clk) :
|
|
|
|
clock(clk)
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
2019-10-27 20:23:47 +01:00
|
|
|
fd = timerfd_create(clock, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
throw SystemError("Failed to create timerfd");
|
2018-08-22 11:29:39 +02:00
|
|
|
#elif PERIODIC_TASK_IMPL == RDTSC
|
2019-10-27 20:23:47 +01:00
|
|
|
int ret = tsc_init(&tsc);
|
2018-08-22 11:29:39 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // PERIODIC_TASK_IMPL
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
void Task::setTimeout(double to)
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
struct timespec now;
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
clock_gettime(clock, &now);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
struct timespec timeout = time_from_double(to);
|
|
|
|
struct timespec next = time_add(&now, &timeout);
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
setNext(&next);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
void Task::setNext(const struct timespec *nxt)
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
|
2019-04-05 03:39:32 +02:00
|
|
|
#if PERIODIC_TASK_IMPL != RDTSC
|
2019-10-27 20:23:47 +01:00
|
|
|
next = *nxt;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
int ret;
|
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = (struct timespec) { 0, 0 },
|
2019-10-27 20:23:47 +01:00
|
|
|
.it_value = next
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
ret = timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, nullptr);
|
2018-08-22 11:29:39 +02:00
|
|
|
if (ret)
|
2019-10-27 20:23:47 +01:00
|
|
|
throw SystemError("Failed to set timerfd");
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
#endif // PERIODIC_TASK_IMPL != RDTSC
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
void Task::setRate(double rate)
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == RDTSC
|
2019-10-27 20:23:47 +01:00
|
|
|
period = tsc_rate_to_cycles(&tsc, rate);
|
|
|
|
next = tsc_now(&tsc) + period;
|
2018-08-22 11:29:39 +02:00
|
|
|
#else
|
2022-12-02 17:16:44 +01:00
|
|
|
// A rate of 0 will disarm the timer
|
2019-10-27 20:23:47 +01:00
|
|
|
period = rate ? time_from_double(1.0 / rate) : (struct timespec) { 0, 0 };
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
|
|
|
|
struct timespec now, next;
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
clock_gettime(clock, &now);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
next = time_add(&now, &period);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
return setNext(&next);
|
2018-08-22 11:29:39 +02:00
|
|
|
#elif PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
int ret;
|
|
|
|
struct itimerspec its = {
|
2019-10-27 20:23:47 +01:00
|
|
|
.it_interval = period,
|
|
|
|
.it_value = period
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
ret = timerfd_settime(fd, 0, &its, nullptr);
|
2018-08-22 11:29:39 +02:00
|
|
|
if (ret)
|
2019-10-27 20:23:47 +01:00
|
|
|
throw SystemError("Failed to set timerfd");
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // PERIODIC_TASK_IMPL
|
|
|
|
#endif // PERIODIC_TASK_IMPL == RDTSC
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
Task::~Task()
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
2019-10-27 20:23:47 +01:00
|
|
|
close(fd);
|
2019-02-11 16:41:51 +01:00
|
|
|
#endif
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
uint64_t Task::wait()
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
uint64_t runs;
|
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
|
|
|
|
int ret;
|
|
|
|
struct timespec now;
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
ret = clock_gettime(clock, &now);
|
2018-10-21 20:28:47 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
for (runs = 0; time_cmp(&next, &now) < 0; runs++)
|
|
|
|
next = time_add(&next, &period);
|
2018-10-21 20:28:47 +01:00
|
|
|
|
2018-08-22 11:29:39 +02:00
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP
|
|
|
|
do {
|
2019-10-27 20:23:47 +01:00
|
|
|
ret = clock_nanosleep(clock, TIMER_ABSTIME, &next, nullptr);
|
2018-08-22 11:29:39 +02:00
|
|
|
} while (ret == EINTR);
|
|
|
|
#elif PERIODIC_TASK_IMPL == NANOSLEEP
|
2019-10-27 20:23:47 +01:00
|
|
|
struct timespec req, rem = time_diff(&now, &next);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2018-10-21 20:28:47 +01:00
|
|
|
do {
|
|
|
|
req = rem;
|
|
|
|
ret = nanosleep(&req, &rem);
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
2018-08-22 11:29:39 +02:00
|
|
|
#endif
|
2018-10-21 20:28:47 +01:00
|
|
|
if (ret)
|
2018-08-22 11:29:39 +02:00
|
|
|
return 0;
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
ret = clock_gettime(clock, &now);
|
2018-08-22 11:29:39 +02:00
|
|
|
if (ret)
|
2018-10-21 20:28:47 +01:00
|
|
|
return ret;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
for (; time_cmp(&next, &now) < 0; runs++)
|
|
|
|
next = time_add(&next, &period);
|
2018-08-22 11:29:39 +02:00
|
|
|
#elif PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
int ret;
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
ret = read(fd, &runs, sizeof(runs));
|
2018-08-22 11:29:39 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
#elif PERIODIC_TASK_IMPL == RDTSC
|
|
|
|
uint64_t now;
|
|
|
|
|
|
|
|
do {
|
2019-10-27 20:23:47 +01:00
|
|
|
now = tsc_now(&tsc);
|
|
|
|
} while (now < next);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
for (runs = 0; next < now; runs++)
|
|
|
|
next += period;
|
2018-08-22 11:29:39 +02:00
|
|
|
#else
|
|
|
|
#error "Invalid period task implementation"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return runs;
|
|
|
|
}
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
void Task::stop()
|
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
int ret;
|
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = (struct timespec) { 0, 0 },
|
|
|
|
.it_value = (struct timespec) { 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = timerfd_settime(fd, 0, &its, nullptr);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to disarm timerfd");
|
2022-12-02 17:16:44 +01:00
|
|
|
#endif // PERIODIC_TASK_IMPL == TIMERFD
|
2019-10-27 20:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int Task::getFD() const
|
2018-08-22 11:29:39 +02:00
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
2019-10-27 20:23:47 +01:00
|
|
|
return fd;
|
2018-08-22 11:29:39 +02:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|