-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathpath_source.cpp
More file actions
218 lines (179 loc) · 6.75 KB
/
Copy pathpath_source.cpp
File metadata and controls
218 lines (179 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Path source.
*
* 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
*/
#include <fmt/format.h>
#include <villas/exceptions.hpp>
#include <villas/hook_list.hpp>
#include <villas/node.hpp>
#include <villas/nodes/loopback_internal.hpp>
#include <villas/path.hpp>
#include <villas/path_destination.hpp>
#include <villas/path_source.hpp>
#include <villas/sample.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
PathSource::PathSource(Path *p, Node *n) : node(n), path(p), masked(false) {
int ret;
int pool_size = std::max(DEFAULT_QUEUE_LENGTH, 20 * node->in.vectorize);
ret = pool_init(&pool, pool_size,
SAMPLE_LENGTH(node->getInputSignalsMaxCount()),
node->getMemoryType());
if (ret)
throw RuntimeError("Failed to initialize pool");
}
PathSource::~PathSource() {
int ret __attribute__((unused));
ret = pool_destroy(&pool);
}
int PathSource::read(int i) {
int ret, recv, tomux, allocated, cnt, toenqueue, enqueued = 0,
muxed_initialized = 0;
cnt = node->in.vectorize;
struct Sample *read_smps[cnt];
struct Sample *muxed_smps[cnt];
struct Sample **tomux_smps;
// Fill smps[] free sample blocks from the pool
allocated = sample_alloc_many(&pool, read_smps, cnt);
if (allocated != cnt)
path->logger->warn("Pool underrun for path source {}", node->getName());
// Read ready samples and store them to blocks pointed by smps[]
recv = node->read(read_smps, allocated);
if (recv == 0) {
enqueued = 0;
goto read_decref_read_smps;
} else if (recv < 0) {
if (node->getState() == State::STOPPING) {
path->state = State::STOPPING;
enqueued = -1;
goto read_decref_read_smps;
} else {
path->logger->error("Failed to read samples from node {}",
node->getName());
enqueued = 0;
goto read_decref_read_smps;
}
} else if (recv < allocated)
path->logger->warn("Partial read for path {}: read={}, expected={}",
path->toString(), recv, allocated);
// Let the master path sources forward received samples to their secondaries
writeToSecondaries(read_smps, recv);
if (path->mode == Path::Mode::ANY) { // Mux all samples
tomux_smps = read_smps;
tomux = recv;
} else { // Mux only last sample and discard others
tomux_smps = read_smps + recv - 1;
tomux = 1;
}
for (int i = 0; i < tomux; i++) {
muxed_smps[i] = i == 0 ? sample_clone(path->last_sample)
: sample_clone(muxed_smps[i - 1]);
if (!muxed_smps[i]) {
path->logger->error("Pool underrun in path {}", path->toString());
muxed_initialized = i == 0 ? 0 : i - 1;
enqueued = -1;
goto read_decref_muxed_smps;
}
muxed_smps[i]->flags = tomux_smps[i]->flags;
if (path->original_sequence_no) {
muxed_smps[i]->sequence = tomux_smps[i]->sequence;
if (tomux_smps[i]->flags & (int)SampleFlags::HAS_SEQUENCE)
muxed_smps[i]->flags |= (int)SampleFlags::HAS_SEQUENCE;
else
muxed_smps[i]->flags &= ~(int)SampleFlags::HAS_SEQUENCE;
} else {
muxed_smps[i]->sequence = path->last_sequence++;
muxed_smps[i]->flags |= (int)SampleFlags::HAS_SEQUENCE;
}
/* We reset the sample length after each restart of the simulation.
* This is necessary for the test_rtt node to work properly.
*/
if (tomux_smps[i]->flags & (int)SampleFlags::NEW_SIMULATION)
muxed_smps[i]->length = 0;
muxed_smps[i]->ts = tomux_smps[i]->ts;
ret = mappings.remap(muxed_smps[i], tomux_smps[i]);
if (ret < 0) {
enqueued = ret;
muxed_initialized = i;
goto read_decref_muxed_smps;
}
if (muxed_smps[i]->length > 0)
muxed_smps[i]->flags |= (int)SampleFlags::HAS_DATA;
else
muxed_smps[i]->flags &= ~(int)SampleFlags::HAS_DATA;
}
muxed_initialized = tomux;
sample_copy(path->last_sample, muxed_smps[tomux - 1]);
#ifdef WITH_HOOKS
toenqueue = path->hooks.process(muxed_smps, tomux);
if (toenqueue == -1) {
path->logger->error(
"An error occurred during hook processing. Skipping sample");
} else if (toenqueue != tomux) {
int skipped = tomux - toenqueue;
path->logger->trace("Hooks skipped {} out of {} samples for path {}",
skipped, tomux, path->toString());
}
#else
toenqueue = tomux;
#endif
path->received.set(i);
path->logger->trace("Source nodes: received=0b{:b}, mask=0b{:b}",
path->received.to_ullong(), path->mask.to_ullong());
if (path->mask.test(i)) {
// Enqueue always
if (path->mode == Path::Mode::ANY) {
enqueued = toenqueue;
PathDestination::enqueueAll(path, muxed_smps, toenqueue);
}
// Enqueue only if received == mask bitset
else if (path->mode == Path::Mode::ALL) {
if (path->mask == path->received) {
PathDestination::enqueueAll(path, muxed_smps, toenqueue);
path->received.reset();
enqueued = toenqueue;
} else
enqueued = 0;
}
} else
enqueued = 0;
read_decref_muxed_smps:
sample_decref_many(muxed_smps, muxed_initialized);
read_decref_read_smps:
sample_decref_many(read_smps, allocated);
return enqueued;
}
void PathSource::check() {
if (!node->isEnabled())
throw RuntimeError("Source {} is not enabled", node->getName());
if (!(node->getFactory()->getFlags() &
(int)NodeFactory::Flags::SUPPORTS_READ))
throw RuntimeError("Node {} is not supported as a source for a path",
node->getName());
}
MasterPathSource::MasterPathSource(Path *p, Node *n) : PathSource(p, n) {}
void MasterPathSource::writeToSecondaries(struct Sample *smps[], unsigned cnt) {
for (auto sps : secondaries) {
int sent = sps->getNode()->write(smps, cnt);
if (sent < 0)
throw RuntimeError("Failed to write secondary path source {} of path {}",
sps->getNode()->getName(), path->toString());
else if ((unsigned)sent < cnt)
path->logger->warn("Partial write to secondary path source {} of path {}",
sps->getNode()->getName(), path->toString());
}
}
SecondaryPathSource::SecondaryPathSource(Path *p, Node *n, NodeList &nodes,
PathSource::Ptr m)
: PathSource(p, n), master(m) {
auto mps = std::dynamic_pointer_cast<MasterPathSource>(m);
node = new InternalLoopbackNode(n, mps->getSecondaries().size(),
mps->getPath()->queuelen);
if (!node)
throw RuntimeError("Failed to create internal loopback");
// Register new loopback node in node list of super node
nodes.push_back(node);
}