1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/common/lib/task.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

204 lines
4.5 KiB
C++
Raw Normal View History

/** Run tasks periodically.
*
2022-03-15 09:13:39 -04:00
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
2022-03-15 09:05:42 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <unistd.h>
#include <ctime>
#include <cerrno>
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;
#if PERIODIC_TASK_IMPL == TIMERFD
#include <sys/timerfd.h>
2019-04-05 03:39:32 +02:00
#endif /* PERIODIC_TASK_IMPL */
2019-10-27 20:23:47 +01:00
Task::Task(int clk) :
clock(clk)
{
#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");
#elif PERIODIC_TASK_IMPL == RDTSC
2019-10-27 20:23:47 +01:00
int ret = tsc_init(&tsc);
if (ret)
return ret;
2019-04-05 03:39:32 +02:00
#endif /* PERIODIC_TASK_IMPL */
}
2019-10-27 20:23:47 +01:00
void Task::setTimeout(double to)
{
struct timespec now;
2019-10-27 20:23:47 +01:00
clock_gettime(clock, &now);
struct timespec timeout = time_from_double(to);
struct timespec next = time_add(&now, &timeout);
2019-10-27 20:23:47 +01:00
setNext(&next);
}
2019-10-27 20:23:47 +01:00
void Task::setNext(const struct timespec *nxt)
{
2019-04-05 03:39:32 +02:00
#if PERIODIC_TASK_IMPL != RDTSC
2019-10-27 20:23:47 +01:00
next = *nxt;
#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
};
2019-10-27 20:23:47 +01:00
ret = timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, nullptr);
if (ret)
2019-10-27 20:23:47 +01:00
throw SystemError("Failed to set timerfd");
2019-04-05 03:39:32 +02:00
#endif /* PERIODIC_TASK_IMPL == TIMERFD */
#endif /* PERIODIC_TASK_IMPL != RDTSC */
}
2019-10-27 20:23:47 +01:00
void Task::setRate(double rate)
{
#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;
#else
/* 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 };
#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);
2019-10-27 20:23:47 +01:00
next = time_add(&now, &period);
2019-10-27 20:23:47 +01:00
return setNext(&next);
#elif PERIODIC_TASK_IMPL == TIMERFD
int ret;
struct itimerspec its = {
2019-10-27 20:23:47 +01:00
.it_interval = period,
.it_value = period
};
2019-10-27 20:23:47 +01:00
ret = timerfd_settime(fd, 0, &its, nullptr);
if (ret)
2019-10-27 20:23:47 +01:00
throw SystemError("Failed to set timerfd");
2019-04-05 03:39:32 +02:00
#endif /* PERIODIC_TASK_IMPL */
#endif /* PERIODIC_TASK_IMPL == RDTSC */
}
2019-10-27 20:23:47 +01:00
Task::~Task()
{
#if PERIODIC_TASK_IMPL == TIMERFD
2019-10-27 20:23:47 +01:00
close(fd);
2019-02-11 16:41:51 +01:00
#endif
}
2019-10-27 20:23:47 +01:00
uint64_t Task::wait()
{
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);
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);
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP
do {
2019-10-27 20:23:47 +01:00
ret = clock_nanosleep(clock, TIMER_ABSTIME, &next, nullptr);
} while (ret == EINTR);
#elif PERIODIC_TASK_IMPL == NANOSLEEP
2019-10-27 20:23:47 +01:00
struct timespec req, rem = time_diff(&now, &next);
do {
req = rem;
ret = nanosleep(&req, &rem);
} while (ret < 0 && errno == EINTR);
#endif
if (ret)
return 0;
2019-10-27 20:23:47 +01:00
ret = clock_gettime(clock, &now);
if (ret)
return ret;
2019-10-27 20:23:47 +01:00
for (; time_cmp(&next, &now) < 0; runs++)
next = time_add(&next, &period);
#elif PERIODIC_TASK_IMPL == TIMERFD
int ret;
2019-10-27 20:23:47 +01:00
ret = read(fd, &runs, sizeof(runs));
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);
2019-10-27 20:23:47 +01:00
for (runs = 0; next < now; runs++)
next += period;
#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");
#endif /* PERIODIC_TASK_IMPL == TIMERFD */
}
int Task::getFD() const
{
#if PERIODIC_TASK_IMPL == TIMERFD
2019-10-27 20:23:47 +01:00
return fd;
#else
return -1;
#endif
}