2016-10-22 20:43:36 -04:00
|
|
|
/** Receive messages from server snd print them on stdout.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2019-01-13 00:42:39 +01:00
|
|
|
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 13:08:17 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
2016-10-22 20:43:36 -04:00
|
|
|
*
|
2017-04-27 13:08:17 +02:00
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 13:08:17 +02:00
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 13:08:17 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2016-10-22 20:43:36 -04:00
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
2017-04-27 13:08:17 +02:00
|
|
|
*/
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2018-07-03 21:04:28 +02:00
|
|
|
#include <iostream>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <atomic>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
#include <villas/tool.hpp>
|
2017-03-20 09:03:48 -03:00
|
|
|
#include <villas/timing.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/sample.h>
|
2017-08-05 21:02:09 +02:00
|
|
|
#include <villas/io.h>
|
2019-03-26 15:33:47 +01:00
|
|
|
#include <villas/hook.hpp>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/utils.hpp>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/pool.h>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/log.hpp>
|
2019-04-23 13:11:08 +02:00
|
|
|
#include <villas/colors.hpp>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
#include <villas/plugin.hpp>
|
2017-03-05 10:06:32 -04:00
|
|
|
#include <villas/plugin.h>
|
2019-03-26 07:01:10 +01:00
|
|
|
#include <villas/config_helper.hpp>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/kernel/rt.hpp>
|
2018-08-23 17:31:01 +02:00
|
|
|
#include <villas/node/config.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
using namespace villas;
|
2019-03-26 15:33:47 +01:00
|
|
|
using namespace villas::node;
|
|
|
|
using namespace villas::plugin;
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace tools {
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
class Hook : public Tool {
|
2017-03-17 02:52:59 -03:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
public:
|
|
|
|
Hook(int argc, char *argv[]) :
|
|
|
|
Tool(argc, argv, "hook"),
|
|
|
|
stop(false),
|
|
|
|
format("villas.human"),
|
|
|
|
dtypes("64f"),
|
|
|
|
cnt(1)
|
|
|
|
{
|
|
|
|
int ret;
|
2018-07-03 21:04:28 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
ret = memory_init(DEFAULT_NR_HUGEPAGES);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize memory");
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
p.state = STATE_DESTROYED;
|
|
|
|
io.state = STATE_DESTROYED;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
cfg_cli = json_object();
|
|
|
|
}
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
~Hook()
|
|
|
|
{
|
|
|
|
json_decref(cfg_cli);
|
|
|
|
}
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
protected:
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
std::atomic<bool> stop;
|
2018-05-13 13:52:02 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
std::string hook;
|
2018-10-21 22:28:38 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
std::string format;
|
|
|
|
std::string dtypes;
|
2019-04-17 18:46:18 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
struct pool p;
|
|
|
|
struct io io;
|
|
|
|
|
|
|
|
int cnt;
|
2019-03-22 13:45:04 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
json_t *cfg_cli;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
void handler(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cout << "Usage: villas-hook [OPTIONS] NAME [[PARAM1] [PARAM2] ...]" << std::endl
|
|
|
|
<< " NAME the name of the hook function" << std::endl
|
|
|
|
<< " PARAM* a string of configuration settings for the hook" << std::endl
|
|
|
|
<< " OPTIONS is one or more of the following options:" << std::endl
|
|
|
|
<< " -f FMT the data format" << std::endl
|
|
|
|
<< " -t DT the data-type format string" << std::endl
|
|
|
|
<< " -d LVL set debug level to LVL" << std::endl
|
|
|
|
<< " -v CNT process CNT smps at once" << std::endl
|
|
|
|
<< " -h show this help" << std::endl
|
|
|
|
<< " -V show the version of the tool" << std::endl << std::endl;
|
|
|
|
|
|
|
|
#ifdef WITH_HOOKS
|
|
|
|
std::cout << "Supported hooks:" << std::endl;
|
|
|
|
for (Plugin *p : Registry::lookup<HookFactory>())
|
|
|
|
std::cout << " - " << p->getName() << ": " << p->getDescription() << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
#endif /* WITH_HOOKS */
|
|
|
|
|
|
|
|
std::cout << "Supported IO formats:" << std::endl;
|
|
|
|
plugin_dump(PLUGIN_TYPE_FORMAT);
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << "Example:" << std::endl
|
|
|
|
<< " villas-signal random | villas-hook skip_first seconds=10" << std::endl
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
printCopyright();
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse()
|
|
|
|
{
|
|
|
|
int ret;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
/* Parse optional command line arguments */
|
|
|
|
int c;
|
|
|
|
char *endptr;
|
|
|
|
while ((c = getopt(argc, argv, "Vhv:d:f:t:o:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'V':
|
2019-04-19 14:52:52 +02:00
|
|
|
printVersion();
|
2019-03-26 17:04:57 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case 'f':
|
|
|
|
format = optarg;
|
|
|
|
break;
|
2017-03-12 17:10:04 -03:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case 't':
|
|
|
|
dtypes = optarg;
|
|
|
|
break;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case 'v':
|
|
|
|
cnt = strtoul(optarg, &endptr, 0);
|
|
|
|
goto check;
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case 'd':
|
|
|
|
logging.setLevel(optarg);
|
|
|
|
break;
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case 'o':
|
|
|
|
ret = json_object_extend_str(cfg_cli, optarg);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Invalid option: {}", optarg);
|
|
|
|
break;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
check: if (optarg == endptr)
|
|
|
|
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
if (argc < optind + 1) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
hook = argv[optind];
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int ret, recv, sent;
|
|
|
|
|
|
|
|
struct format_type *ft;
|
|
|
|
struct sample **smps;
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
if (cnt < 1)
|
|
|
|
throw RuntimeError("Vectorize option must be greater than 0");
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
smps = new struct sample*[cnt];
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
ret = pool_init(&p, 10 * cnt, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), &memory_hugepage);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initilize memory pool");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
/* Initialize IO */
|
2019-04-19 14:52:52 +02:00
|
|
|
ft = format_type_lookup(format.c_str());
|
2019-03-26 17:04:57 +01:00
|
|
|
if (!ft)
|
|
|
|
throw RuntimeError("Unknown IO format '{}'", format);
|
2017-03-14 11:21:05 -03:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
ret = io_init2(&io, ft, dtypes.c_str(), SAMPLE_HAS_ALL);
|
2019-03-26 17:04:57 +01:00
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize IO");
|
2018-03-28 14:17:44 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
ret = io_check(&io);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to validate IO configuration");
|
2017-03-20 09:04:23 -03:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
ret = io_open(&io, nullptr);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to open IO");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
/* Initialize hook */
|
|
|
|
auto hf = plugin::Registry::lookup<HookFactory>(hook);
|
|
|
|
if (!hf)
|
|
|
|
throw RuntimeError("Unknown hook function '{}'", hook);
|
2019-03-16 09:05:34 +01:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
auto h = hf->make(nullptr, nullptr);
|
|
|
|
if (!h)
|
|
|
|
throw RuntimeError("Failed to initialize hook");
|
2019-03-16 09:05:34 +01:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
h->parse(cfg_cli);
|
|
|
|
h->check();
|
|
|
|
h->prepare(io.signals);
|
|
|
|
h->start();
|
2019-03-16 09:05:34 +01:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
while (!stop) {
|
|
|
|
ret = sample_alloc_many(&p, smps, cnt);
|
|
|
|
if (ret != cnt)
|
|
|
|
throw RuntimeError("Failed to allocate %d smps from pool", cnt);
|
|
|
|
|
|
|
|
recv = io_scan(&io, smps, cnt);
|
|
|
|
if (recv < 0) {
|
|
|
|
if (io_eof(&io))
|
2019-03-26 15:33:47 +01:00
|
|
|
break;
|
2019-03-16 09:05:34 +01:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
throw RuntimeError("Failed to read from stdin");
|
2019-03-16 09:05:34 +01:00
|
|
|
}
|
2019-05-24 10:51:10 +02:00
|
|
|
|
|
|
|
timespec now = time_now();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
logger->debug("Read {} smps from stdin", recv);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
unsigned send = 0;
|
|
|
|
for (int processed = 0; processed < recv; processed++) {
|
|
|
|
struct sample *smp = smps[processed];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-05-24 10:51:10 +02:00
|
|
|
if (!(smp->flags & SAMPLE_HAS_TS_RECEIVED)){
|
|
|
|
smp->ts.received = now;
|
|
|
|
smp->flags |= SAMPLE_HAS_TS_RECEIVED;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
ret = h->process(smp);
|
|
|
|
switch (ret) {
|
|
|
|
case HOOK_ERROR:
|
|
|
|
throw RuntimeError("Failed to process samples");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case HOOK_OK:
|
|
|
|
smps[send++] = smp;
|
|
|
|
break;
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case HOOK_SKIP_SAMPLE:
|
|
|
|
break;
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
case HOOK_STOP_PROCESSING:
|
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop: sent = io_print(&io, smps, send);
|
|
|
|
if (sent < 0)
|
|
|
|
throw RuntimeError("Failed to write to stdout");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
sample_free_many(smps, cnt);
|
|
|
|
}
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
h->stop();
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
delete h;
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
ret = io_close(&io);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to close IO");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
ret = io_destroy(&io);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to destroy IO");
|
2019-03-22 13:45:04 +01:00
|
|
|
|
2019-03-26 17:04:57 +01:00
|
|
|
sample_free_many(smps, cnt);
|
|
|
|
|
|
|
|
delete smps;
|
|
|
|
|
|
|
|
ret = pool_destroy(&p);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to destroy memory pool");
|
|
|
|
|
|
|
|
return 0;
|
2019-03-22 13:45:04 +01:00
|
|
|
}
|
2019-04-19 14:52:52 +02:00
|
|
|
};
|
2019-03-26 17:04:57 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
} // namespace tools
|
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-06-18 17:52:54 +02:00
|
|
|
villas::node::tools::Hook t(argc, argv);
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
return t.run();
|
2017-07-13 22:15:29 +02:00
|
|
|
}
|