1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/nodes/shmem.cpp

230 lines
5.9 KiB
C++
Raw Permalink Normal View History

/** Node-type for shared memory communication.
*
2017-04-17 18:50:43 +02:00
* @file
* @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-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-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-05 12:52:21 +02:00
#include <fcntl.h>
#include <pthread.h>
#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>
#include <villas/exceptions.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/shmem.h>
#include <villas/nodes/shmem.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/plugin.h>
#include <villas/timing.h>
#include <villas/utils.hpp>
2017-04-05 12:52:21 +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-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
int ret;
2019-04-22 23:45:38 +02:00
json_t *json_exec = nullptr;
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));
/* Default values */
shm->conf.queuelen = MAX(DEFAULT_SHMEM_QUEUELEN, n->in.vectorize);
2019-02-06 13:11:57 +01:00
shm->conf.samplelen = len;
shm->conf.polling = false;
2019-04-22 23:45:38 +02:00
shm->exec = nullptr;
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 }",
"out",
"name", &shm->out_name,
"in",
"name", &shm->in_name,
"queuelen", &shm->conf.queuelen,
"exec", &json_exec,
"mode", &mode_str
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-shmem");
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);
}
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");
shm->exec = new char*[json_array_size(json_exec) + 1];
if (!shm->exec)
throw MemoryAllocationError();
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) {
val = json_string_value(json_val);
if (!val)
2021-02-16 14:15:14 +01:00
throw SystemError("Setting 'exec' must be an array of strings");
2018-08-07 18:40:32 +02:00
shm->exec[i] = strdup(val);
}
2019-04-22 23:45:38 +02:00
shm->exec[i] = nullptr;
}
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-10-18 15:39:53 +02:00
struct shmem *shm = (struct shmem *) n->_vd;
int ret;
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");
sleep(1);
}
2017-06-08 13:07:20 +02:00
ret = shmem_int_open(shm->out_name, shm->in_name, &shm->intf, &shm->conf);
if (ret < 0)
2021-02-16 14:15:14 +01:00
throw SystemError("Opening shared memory interface failed (ret={})", ret);
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-10-18 15:39:53 +02:00
struct shmem* shm = (struct shmem *) n->_vd;
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-10-18 15:39:53 +02:00
struct shmem *shm = (struct shmem *) n->_vd;
int recv;
struct sample *shared_smps[cnt];
2017-07-28 12:15:56 +02:00
do {
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) {
/* This can only really mean that the other process has exited, so close
* the interface to make sure the shared memory object is unlinked */
2021-02-16 14:15:14 +01:00
n->logger->info("Shared memory segment has been closed.");
2019-06-23 16:13:23 +02:00
n->state = State::STOPPING;
return recv;
2017-07-28 12:15:56 +02:00
}
2017-05-12 11:36:23 +02:00
sample_copy_many(smps, shared_smps, recv);
sample_decref_many(shared_smps, recv);
/** @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;
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-10-18 15:39:53 +02:00
struct shmem *shm = (struct shmem *) n->_vd;
struct sample *shared_smps[cnt]; /* Samples need to be copied to the shared pool first */
int avail, pushed, copied;
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);
copied = sample_copy_many(shared_smps, smps, avail);
if (copied < avail)
2021-02-16 14:15:14 +01:00
n->logger->warn("Outgoing pool underrun");
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-05 12:52:21 +02:00
return pushed;
}
2020-08-25 21:00:52 +02:00
char * shmem_print(struct vnode *n)
{
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;
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");
if (shm->exec) {
strcatf(&buf, ", exec='");
for (int i = 0; shm->exec[i]; i++)
strcatf(&buf, shm->exec[i+1] ? "%s " : "%s", shm->exec[i]);
strcatf(&buf, "'");
}
2017-04-05 12:52:21 +02:00
return buf;
}
2017-04-05 12:52:21 +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;
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;
int ret = vlist_init(&p.node.instances);
if (!ret)
vlist_init_and_push(&plugins, &p);
}
2017-04-05 12:52:21 +02:00
__attribute__((destructor(110)))
static void deregister_plugin() {
2020-06-16 02:35:34 +02:00
vlist_remove_all(&plugins, &p);
}