{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvillas-pipe.cpp
More file actions
527 lines (413 loc) · 13.5 KB
/
Copy pathvillas-pipe.cpp
File metadata and controls
527 lines (413 loc) · 13.5 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* Receive messages from server snd print them on stdout.
*
* 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 <atomic>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <signal.h>
#include <unistd.h>
#include <villas/colors.hpp>
#include <villas/config_helper.hpp>
#include <villas/exceptions.hpp>
#include <villas/format.hpp>
#include <villas/kernel/rt.hpp>
#include <villas/log.hpp>
#include <villas/node.hpp>
#include <villas/node/config.hpp>
#include <villas/nodes/websocket.hpp>
#include <villas/pool.hpp>
#include <villas/super_node.hpp>
#include <villas/timing.hpp>
#include <villas/tool.hpp>
#include <villas/utils.hpp>
namespace villas {
namespace node {
namespace tools {
class Pipe;
class PipeDirection {
protected:
struct Pool pool;
Node *node;
Format *formatter;
std::thread thread;
Logger logger;
bool stop;
bool enabled;
int limit;
int count;
public:
// TODO: Here seems to be a cppcheck false positive in the CI.
// Try to remove this comment as cppcheck is being updated.
// cppcheck-suppress uninitMemberVar
PipeDirection(Node *n, Format *fmt, bool en, int lim, const std::string &name)
: node(n), formatter(fmt), stop(false), enabled(en), limit(lim),
count(0) {
auto loggerName = fmt::format("pipe:{}", name);
logger = Log::get(loggerName);
// Initialize memory
unsigned pool_size =
LOG2_CEIL(MAX(node->out.vectorize, node->in.vectorize));
int ret = pool_init(&pool, pool_size, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH),
node->getMemoryType());
if (ret < 0)
throw RuntimeError("Failed to allocate memory for pool.");
}
~PipeDirection() {
int ret __attribute__((unused));
ret = pool_destroy(&pool);
}
virtual void run() = 0;
void startThread() {
stop = false;
if (enabled)
thread = std::thread(&PipeDirection::run, this);
}
void stopThread() {
stop = true;
if (enabled) {
// We send a SIGUSR2 to the threads to unblock their blocking read() syscalls
pthread_kill(thread.native_handle(), SIGUSR2);
thread.join();
}
}
};
class PipeSendDirection : public PipeDirection {
friend Pipe;
public:
PipeSendDirection(Node *n, Format *i, bool en = true, int lim = -1)
: PipeDirection(n, i, en, lim, "send") {}
virtual void run() {
logger->debug("Send thread started");
unsigned last_sequenceno = 0;
int scanned, sent, allocated;
struct Sample *smps[node->out.vectorize];
while (!stop) {
allocated = sample_alloc_many(&pool, smps, node->out.vectorize);
if (allocated < 0)
throw RuntimeError("Failed to get {} samples out of send pool.",
node->out.vectorize);
else if (allocated < (int)node->out.vectorize)
logger->warn("Send pool underrun");
scanned = formatter->scan(stdin, smps, allocated);
if (scanned < 0) {
if (!stop)
logger->warn("Failed to read from stdin");
}
// Fill in missing sequence numbers
for (int i = 0; i < scanned; i++) {
if (smps[i]->flags & (int)SampleFlags::HAS_SEQUENCE)
last_sequenceno = smps[i]->sequence;
else
smps[i]->sequence = last_sequenceno++;
}
sent = node->write(smps, scanned);
sample_decref_many(smps, allocated);
count += sent;
if (limit > 0 && count >= limit)
goto leave_limit;
if (feof(stdin))
goto leave_eof;
}
goto leave;
leave_eof:
logger->info("Reached end-of-file.");
raise(SIGUSR1);
goto leave;
leave_limit:
logger->info("Reached send limit.");
raise(SIGUSR1);
leave:
logger->debug("Send thread stopped");
}
};
class PipeReceiveDirection : public PipeDirection {
friend Pipe;
public:
PipeReceiveDirection(Node *n, Format *i, bool en = true, int lim = -1)
: PipeDirection(n, i, en, lim, "recv") {}
virtual void run() {
logger->debug("Receive thread started");
int recv, allocated = 0;
struct Sample *smps[node->in.vectorize];
while (!stop) {
allocated = sample_alloc_many(&pool, smps, node->in.vectorize);
if (allocated < 0)
throw RuntimeError("Failed to allocate {} samples from receive pool.",
node->in.vectorize);
else if (allocated < (int)node->in.vectorize)
logger->warn("Receive pool underrun: allocated only {} of {} samples",
allocated, node->in.vectorize);
recv = node->read(smps, allocated);
if (recv < 0) {
if (node->getState() == State::STOPPING || stop) {
sample_decref_many(smps, allocated);
goto leave;
}
logger->warn("Failed to receive samples from node {}: reason={}",
node->getName(), recv);
} else
formatter->print(stdout, smps, recv);
sample_decref_many(smps, allocated);
count += recv;
if (limit > 0 && count >= limit)
goto leave_limit;
}
goto leave;
leave_limit:
logger->info("Reached receive limit.");
raise(SIGUSR1);
leave:
logger->debug("Receive thread stopped");
}
};
class Pipe : public Tool {
public:
// TODO: Here seems to be a cppcheck false positive in the CI.
// Try to remove this comment as cppcheck is being updated.
// cppcheck-suppress uninitMemberVar
Pipe(int argc, char *argv[])
: Tool(argc, argv, "pipe"), stop(false), formatter(), timeout(0),
reverse(false), format("villas.human"), dtypes("64f"),
config_cli(json_object()) {
send.enabled = true;
send.limit = -1;
recv.enabled = true;
recv.limit = -1;
int ret = memory::init(DEFAULT_NR_HUGEPAGES);
if (ret)
throw RuntimeError("Failed to initialize memory");
}
~Pipe() { json_decref(config_cli); }
protected:
std::atomic<bool> stop;
SuperNode sn; // The global configuration
Format *formatter;
int timeout;
bool reverse;
std::string format;
std::string dtypes;
std::string uri;
std::string nodestr;
json_t *config_cli;
struct {
int limit;
bool enabled;
std::unique_ptr<PipeReceiveDirection> dir;
} recv;
struct {
int limit;
bool enabled;
std::unique_ptr<PipeSendDirection> dir;
} send;
void handler(int signal, siginfo_t *sinfo, void *ctx) {
logger->debug("Received {} signal.", strsignal(signal));
switch (signal) {
case SIGALRM:
logger->info("Reached timeout.");
stop = true;
break;
case SIGUSR1:
if (recv.dir->enabled) {
if (recv.dir->limit < 0 && feof(stdin))
stop = true;
if (recv.dir->limit > 0 && recv.dir->count >= recv.dir->limit)
stop = true;
}
if (send.dir->enabled && send.dir->limit > 0) {
if (send.dir->count >= send.dir->limit)
stop = true;
}
break;
default:
stop = true;
break;
}
}
void usage() {
std::cout
<< "Usage: villas-pipe [OPTIONS] CONFIG NODE" << std::endl
<< " CONFIG path to a configuration file" << std::endl
<< " NODE the name of the node to which samples are sent and "
"received from"
<< std::endl
<< " OPTIONS are:" << std::endl
<< " -f FMT set the format" << std::endl
<< " -t DT the data-type format string" << std::endl
<< " -o OPTION=VALUE overwrite options in config file" << std::endl
<< " -x swap read / write endpoints" << std::endl
<< " -s only read data from stdin and send it to node"
<< std::endl
<< " -r only read data from node and write it to "
"stdout"
<< std::endl
<< " -T NUM terminate after NUM seconds" << std::endl
<< " -L NUM terminate after NUM samples sent" << std::endl
<< " -l NUM terminate after NUM samples received"
<< std::endl
<< " -h show this usage information" << std::endl
<< " -d set logging level" << std::endl
<< " -V show the version of the tool" << std::endl
<< std::endl;
printCopyright();
}
void parse() {
int c, ret;
char *endptr;
while ((c = getopt(argc, argv, "Vhxrsd:l:L:T:f:t:o:")) != -1) {
switch (c) {
case 'V':
printVersion();
exit(EXIT_SUCCESS);
case 'f':
format = optarg;
break;
case 't':
dtypes = optarg;
break;
case 'x':
reverse = true;
break;
case 's':
recv.enabled = false; // send only
break;
case 'r':
send.enabled = false; // receive only
break;
case 'l':
recv.limit = strtoul(optarg, &endptr, 10);
goto check;
case 'L':
send.limit = strtoul(optarg, &endptr, 10);
goto check;
case 'T':
timeout = strtoul(optarg, &endptr, 10);
goto check;
case 'o':
ret = json_object_extend_str(config_cli, optarg);
if (ret)
throw RuntimeError("Invalid option: {}", optarg);
break;
case 'd':
Log::getInstance().setLevel(optarg);
break;
case 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check:
if (optarg == endptr)
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c,
optarg);
}
if (argc != optind + 2) {
usage();
exit(EXIT_FAILURE);
}
uri = argv[optind];
nodestr = argv[optind + 1];
}
int main() {
int ret;
Node *node;
json_t *json_format;
json_error_t err;
logger->info("Logging level: {}", Log::getInstance().getLevelName());
if (!uri.empty())
sn.parse(uri);
else
logger->warn("No configuration file specified. Starting unconfigured. "
"Use the API to configure this instance.");
// Try parsing format config as JSON
json_format = json_loads(format.c_str(), 0, &err);
formatter = json_format ? FormatFactory::make(json_format)
: FormatFactory::make(format);
if (!formatter)
throw RuntimeError("Failed to initialize formatter");
formatter->start(dtypes);
node = sn.getNode(nodestr);
if (!node)
throw RuntimeError("Node {} does not exist!", nodestr);
if (recv.enabled && !(node->getFactory()->getFlags() &
(int)NodeFactory::Flags::SUPPORTS_READ))
throw RuntimeError("Node {} can not receive data. Consider using "
"send-only mode by using '-s' option",
nodestr);
if (send.enabled && !(node->getFactory()->getFlags() &
(int)NodeFactory::Flags::SUPPORTS_WRITE))
throw RuntimeError("Node {} can not send data. Consider using "
"receive-only mode by using '-r' option",
nodestr);
#if defined(WITH_NODE_WEBSOCKET) && defined(WITH_WEB)
// Only start web subsystem if villas-pipe is used with a websocket node
if (node->getFactory()->getFlags() &
(int)NodeFactory::Flags::REQUIRES_WEB) {
Web *w = sn.getWeb();
w->start();
}
#endif // WITH_NODE_WEBSOCKET
if (reverse)
node->reverse();
ret = node->getFactory()->start(&sn);
if (ret)
throw RuntimeError("Failed to intialize node type {}: reason={}",
node->getFactory()->getName(), ret);
sn.startInterfaces();
ret = node->check();
if (ret)
throw RuntimeError("Invalid node configuration");
ret = node->prepare();
if (ret)
throw RuntimeError("Failed to prepare node {}: reason={}",
node->getName(), ret);
ret = node->start();
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node->getName(),
ret);
recv.dir = std::make_unique<PipeReceiveDirection>(node, formatter,
recv.enabled, recv.limit);
send.dir = std::make_unique<PipeSendDirection>(node, formatter,
send.enabled, send.limit);
recv.dir->startThread();
send.dir->startThread();
// Arm timeout timer
alarm(timeout);
while (!stop)
usleep(0.1e6);
/* We are stopping the node here in order to unblock the receiving threads
* Node::read() call and allow it to be joined(). */
ret = node->stop();
if (ret)
throw RuntimeError("Failed to stop node {}: reason={}", node->getName(),
ret);
recv.dir->stopThread();
send.dir->stopThread();
sn.stopInterfaces();
ret = node->getFactory()->stop();
if (ret)
throw RuntimeError("Failed to stop node type {}: reason={}",
node->getFactory()->getName(), ret);
#if defined(WITH_NODE_WEBSOCKET) && defined(WITH_WEB)
// Only start web subsystem if villas-pipe is used with a websocket node
if (node->getFactory()->getFlags() &
(int)NodeFactory::Flags::REQUIRES_WEB) {
Web *w = sn.getWeb();
w->stop();
}
#endif // WITH_NODE_WEBSOCKET
delete formatter;
return 0;
}
};
} // namespace tools
} // namespace node
} // namespace villas
int main(int argc, char *argv[]) {
villas::node::tools::Pipe t(argc, argv);
return t.run();
}
You can’t perform that action at this time.
