2017-04-15 21:22:19 +02:00
|
|
|
/** Node-type for shared memory communication.
|
|
|
|
*
|
2017-04-17 18:50:43 +02:00
|
|
|
* @file
|
2017-04-15 21:22:19 +02:00
|
|
|
* @author Georg Martin Reinke <georg.reinke@rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* 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 12:56:43 +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 12:56:43 +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/>.
|
2017-04-15 21:22:19 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2017-04-05 12:52:21 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/log.hpp>
|
2020-07-04 16:22:10 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/shmem.h>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/shmem.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/timing.h>
|
2019-04-23 13:15:00 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-04-05 12:52:21 +02:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int shmem_parse(struct vnode *n, json_t *json)
|
2017-04-15 18:59:22 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct shmem *shm = (struct shmem *) n->_vd;
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *val, *mode_str = nullptr;
|
2017-04-05 12:52:21 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
2019-04-22 23:45:38 +02:00
|
|
|
json_t *json_exec = nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
|
|
|
|
2019-02-06 13:11:57 +01:00
|
|
|
int len = MAX(vlist_length(&n->in.signals), vlist_length(&n->out.signals));
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
/* Default values */
|
2018-05-24 09:04:41 +02:00
|
|
|
shm->conf.queuelen = MAX(DEFAULT_SHMEM_QUEUELEN, n->in.vectorize);
|
2019-02-06 13:11:57 +01:00
|
|
|
shm->conf.samplelen = len;
|
2017-08-03 00:19:27 +02:00
|
|
|
shm->conf.polling = false;
|
2019-04-22 23:45:38 +02:00
|
|
|
shm->exec = nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: { s: s }, s: { s: s }, s?: i, s?: o, s?: s }",
|
2018-08-20 18:28:40 +02:00
|
|
|
"out",
|
|
|
|
"name", &shm->out_name,
|
|
|
|
"in",
|
|
|
|
"name", &shm->in_name,
|
2017-08-03 00:19:27 +02:00
|
|
|
"queuelen", &shm->conf.queuelen,
|
2018-10-20 17:12:39 +02:00
|
|
|
"exec", &json_exec,
|
|
|
|
"mode", &mode_str
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-shmem");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2018-10-20 17:12:39 +02:00
|
|
|
if (mode_str) {
|
|
|
|
if (!strcmp(mode_str, "polling"))
|
|
|
|
shm->conf.polling = true;
|
|
|
|
else if (!strcmp(mode_str, "pthread"))
|
|
|
|
shm->conf.polling = false;
|
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Unknown mode '{}'", mode_str);
|
2018-10-20 17:12:39 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
if (json_exec) {
|
|
|
|
if (!json_is_array(json_exec))
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Setting 'exec' must be an array of strings");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
shm->exec = new char*[json_array_size(json_exec) + 1];
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!shm->exec)
|
|
|
|
throw MemoryAllocationError();
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2018-08-07 18:40:32 +02:00
|
|
|
size_t i;
|
2017-10-16 08:08:35 +02:00
|
|
|
json_t *json_val;
|
2018-08-07 18:40:32 +02:00
|
|
|
json_array_foreach(json_exec, i, json_val) {
|
2018-05-23 14:59:05 +02:00
|
|
|
val = json_string_value(json_val);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (!val)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Setting 'exec' must be an array of strings");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2018-08-07 18:40:32 +02:00
|
|
|
shm->exec[i] = strdup(val);
|
2017-04-12 17:07:59 +02:00
|
|
|
}
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
shm->exec[i] = nullptr;
|
2017-04-12 17:07:59 +02:00
|
|
|
}
|
2017-04-05 12:52:21 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int shmem_start(struct vnode *n)
|
2017-04-15 18:59:22 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct shmem *shm = (struct shmem *) n->_vd;
|
2017-04-15 18:59:22 +02:00
|
|
|
int ret;
|
2017-04-12 23:07:30 +02:00
|
|
|
|
2017-04-15 18:59:22 +02:00
|
|
|
if (shm->exec) {
|
|
|
|
ret = spawn(shm->exec[0], shm->exec);
|
|
|
|
if (!ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Failed to spawn external program");
|
2018-05-23 14:59:05 +02:00
|
|
|
|
|
|
|
sleep(1);
|
2017-04-15 18:59:22 +02:00
|
|
|
}
|
2017-06-08 13:07:20 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
ret = shmem_int_open(shm->out_name, shm->in_name, &shm->intf, &shm->conf);
|
2017-06-08 12:43:24 +02:00
|
|
|
if (ret < 0)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Opening shared memory interface failed (ret={})", ret);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int shmem_stop(struct vnode *n)
|
2017-04-15 18:59:22 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct shmem* shm = (struct shmem *) n->_vd;
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-06-08 12:43:24 +02:00
|
|
|
return shmem_int_close(&shm->intf);
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int shmem_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-04-15 18:59:22 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct shmem *shm = (struct shmem *) n->_vd;
|
2017-05-12 13:08:34 +02:00
|
|
|
int recv;
|
2018-07-11 18:14:29 +02:00
|
|
|
struct sample *shared_smps[cnt];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-28 12:15:56 +02:00
|
|
|
do {
|
2018-07-11 18:14:29 +02:00
|
|
|
recv = shmem_int_read(&shm->intf, shared_smps, cnt);
|
2017-07-28 12:15:56 +02:00
|
|
|
} while (recv == 0);
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-07-28 12:15:56 +02:00
|
|
|
if (recv < 0) {
|
2017-06-14 13:00:43 +02:00
|
|
|
/* This can only really mean that the other process has exited, so close
|
|
|
|
* the interface to make sure the shared memory object is unlinked */
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Shared memory segment has been closed.");
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
n->state = State::STOPPING;
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2017-04-15 22:26:56 +02:00
|
|
|
return recv;
|
2017-07-28 12:15:56 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-05-12 11:36:23 +02:00
|
|
|
sample_copy_many(smps, shared_smps, recv);
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref_many(shared_smps, recv);
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2019-01-23 00:48:38 +01:00
|
|
|
/** @todo: signal descriptions are currently not shared between processes */
|
|
|
|
for (int i = 0; i < recv; i++)
|
2019-02-06 13:11:57 +01:00
|
|
|
smps[i]->signals = &n->in.signals;
|
2019-01-23 00:48:38 +01:00
|
|
|
|
2017-04-15 22:26:56 +02:00
|
|
|
return recv;
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int shmem_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-04-15 18:59:22 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct shmem *shm = (struct shmem *) n->_vd;
|
2018-07-11 18:14:29 +02:00
|
|
|
struct sample *shared_smps[cnt]; /* Samples need to be copied to the shared pool first */
|
2017-10-16 08:08:17 +02:00
|
|
|
int avail, pushed, copied;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
avail = sample_alloc_many(&shm->intf.write.shared->pool, shared_smps, cnt);
|
2019-04-22 23:43:46 +02:00
|
|
|
if (avail != (int) cnt)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Pool underrun for shmem node {}", shm->out_name);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
copied = sample_copy_many(shared_smps, smps, avail);
|
2018-08-20 18:28:40 +02:00
|
|
|
if (copied < avail)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Outgoing pool underrun");
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-08-20 18:28:40 +02:00
|
|
|
pushed = shmem_int_write(&shm->intf, shared_smps, copied);
|
2017-04-05 12:52:21 +02:00
|
|
|
if (pushed != avail)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Outgoing queue overrun for node");
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
return pushed;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * shmem_print(struct vnode *n)
|
2017-04-15 18:59:22 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct shmem *shm = (struct shmem *) n->_vd;
|
2019-04-22 23:45:38 +02:00
|
|
|
char *buf = nullptr;
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-08-31 09:31:19 +02:00
|
|
|
strcatf(&buf, "out_name=%s, in_name=%s, queuelen=%d, polling=%s",
|
|
|
|
shm->out_name, shm->in_name, shm->conf.queuelen, shm->conf.polling ? "yes" : "no");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 18:15:28 +02:00
|
|
|
if (shm->exec) {
|
|
|
|
strcatf(&buf, ", exec='");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 18:15:28 +02:00
|
|
|
for (int i = 0; shm->exec[i]; i++)
|
|
|
|
strcatf(&buf, shm->exec[i+1] ? "%s " : "%s", shm->exec[i]);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 18:15:28 +02:00
|
|
|
strcatf(&buf, "'");
|
|
|
|
}
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
return buf;
|
2017-08-30 13:30:31 +02:00
|
|
|
}
|
2017-04-05 12:52:21 +02:00
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
static struct plugin p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
|
|
|
p.name = "shmem";
|
|
|
|
p.description = "POSIX shared memory interface with external processes";
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::NODE;
|
|
|
|
p.node.instances.state = State::DESTROYED;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.vectorize = 0;
|
|
|
|
p.node.size = sizeof(struct shmem);
|
|
|
|
p.node.parse = shmem_parse;
|
|
|
|
p.node.print = shmem_print;
|
|
|
|
p.node.start = shmem_start;
|
|
|
|
p.node.stop = shmem_stop;
|
|
|
|
p.node.read = shmem_read;
|
|
|
|
p.node.write = shmem_write;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret = vlist_init(&p.node.instances);
|
|
|
|
if (!ret)
|
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|
2017-04-05 12:52:21 +02:00
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
__attribute__((destructor(110)))
|
|
|
|
static void deregister_plugin() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-23 13:15:00 +02:00
|
|
|
}
|