2019-02-24 09:39:41 +01:00
|
|
|
/** Path destination
|
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2019-02-24 09:39:41 +01:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/node.hpp>
|
|
|
|
#include <villas/path.hpp>
|
2020-09-10 17:36:08 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/path_destination.hpp>
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2020-09-10 17:36:08 +02:00
|
|
|
using namespace villas;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
2020-09-10 17:36:08 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
PathDestination::PathDestination(Path *p, Node *n) :
|
|
|
|
node(n),
|
|
|
|
path(p)
|
2020-08-28 09:37:06 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
queue.state = State::DESTROYED;
|
2020-08-28 09:37:06 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
PathDestination::~PathDestination()
|
2019-02-24 09:39:41 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
int ret __attribute__((unused));
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = queue_destroy(&queue);
|
2019-02-24 09:39:41 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int PathDestination::prepare(int queuelen)
|
2019-02-24 09:39:41 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = queue_init(&queue, queuelen);
|
2019-02-24 09:39:41 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void PathDestination::enqueueAll(Path *p, const struct Sample * const smps[], unsigned cnt)
|
2019-02-24 09:39:41 +01:00
|
|
|
{
|
|
|
|
unsigned enqueued, cloned;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample *clones[cnt];
|
2019-02-24 09:39:41 +01:00
|
|
|
|
|
|
|
cloned = sample_clone_many(clones, smps, cnt);
|
|
|
|
if (cloned < cnt)
|
2023-01-10 15:25:27 +01:00
|
|
|
p->logger->warn("Pool underrun in path {}", p->toString());
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto pd : p->destinations) {
|
2019-02-24 09:39:41 +01:00
|
|
|
enqueued = queue_push_many(&pd->queue, (void **) clones, cloned);
|
|
|
|
if (enqueued != cnt)
|
2023-01-10 15:25:27 +01:00
|
|
|
p->logger->warn("Queue overrun for path {}", p->toString());
|
2019-02-24 09:39:41 +01:00
|
|
|
|
|
|
|
/* Increase reference counter of these samples as they are now also owned by the queue. */
|
|
|
|
sample_incref_many(clones, cloned);
|
|
|
|
|
2023-01-10 15:25:27 +01:00
|
|
|
p->logger->debug("Enqueued {} samples to destination {} of path {}", enqueued, pd->node->getName(), p->toString());
|
2019-02-24 09:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sample_decref_many(clones, cloned);
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void PathDestination::write()
|
2019-02-24 09:39:41 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
int cnt = node->out.vectorize;
|
2019-02-24 09:39:41 +01:00
|
|
|
int sent;
|
|
|
|
int allocated;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample *smps[cnt];
|
2019-02-24 09:39:41 +01:00
|
|
|
|
|
|
|
/* As long as there are still samples in the queue */
|
2021-08-10 10:12:48 -04:00
|
|
|
while (true) {
|
|
|
|
allocated = queue_pull_many(&queue, (void **) smps, cnt);
|
2019-02-24 09:39:41 +01:00
|
|
|
if (allocated == 0)
|
|
|
|
break;
|
|
|
|
else if (allocated < cnt)
|
2023-01-10 15:25:27 +01:00
|
|
|
path->logger->debug("Queue underrun for path {}: allocated={} expected={}", path->toString(), allocated, cnt);
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2023-01-10 15:25:27 +01:00
|
|
|
path->logger->debug("Dequeued {} samples from queue of node {} which is part of path {}", allocated, node->getName(), path->toString());
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
sent = node->write(smps, allocated);
|
2019-04-07 16:16:58 +02:00
|
|
|
if (sent < 0) {
|
2023-01-10 15:25:27 +01:00
|
|
|
path->logger->error("Failed to sent {} samples to node {}: reason={}", cnt, node->getName(), sent);
|
2019-04-07 16:16:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-02-24 09:39:41 +01:00
|
|
|
else if (sent < allocated)
|
2023-01-10 15:25:27 +01:00
|
|
|
path->logger->debug("Partial write to node {}: written={}, expected={}", node->getName(), sent, allocated);
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int released = sample_decref_many(smps, allocated);
|
2019-02-24 09:39:41 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
path->logger->debug("Released {} samples back to memory pool", released);
|
2019-02-24 09:39:41 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 17:36:08 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void PathDestination::check()
|
2020-09-10 17:36:08 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!node->isEnabled())
|
2023-01-10 15:25:27 +01:00
|
|
|
throw RuntimeError("Destination {} is not enabled", node->getName());
|
2020-09-10 17:36:08 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!(node->getFactory()->getFlags() & (int) NodeFactory::Flags::SUPPORTS_WRITE))
|
2023-01-10 15:25:27 +01:00
|
|
|
throw RuntimeError("Destination node {} is not supported as a sink for path ", node->getName());
|
2021-05-10 00:12:30 +02:00
|
|
|
}
|