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