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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

223 lines
5.7 KiB
C++
Raw Permalink Normal View History

/* Node-type for shared memory communication.
*
* Author: Georg Martin Reinke <georg.reinke@rwth-aachen.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstring>
2017-04-05 12:52:21 +02:00
#include <fcntl.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <villas/exceptions.hpp>
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/node_compat.hpp>
#include <villas/nodes/shmem.hpp>
#include <villas/shmem.hpp>
#include <villas/timing.hpp>
#include <villas/utils.hpp>
2017-04-05 12:52:21 +02:00
using namespace villas;
2021-05-10 00:12:30 +02:00
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
int villas::node::shmem_init(NodeCompat *n) {
auto *shm = n->getData<struct shmem>();
// Default values
shm->conf.queuelen = -1;
shm->conf.samplelen = -1;
shm->conf.polling = false;
shm->exec = nullptr;
return 0;
}
int villas::node::shmem_parse(NodeCompat *n, json_t *json) {
auto *shm = n->getData<struct shmem>();
2019-04-22 23:45:38 +02:00
const char *val, *mode_str = nullptr;
int ret;
2019-04-22 23:45:38 +02:00
json_t *json_exec = nullptr;
json_error_t err;
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;
}
int villas::node::shmem_prepare(NodeCompat *n) {
auto *shm = n->getData<struct shmem>();
if (shm->conf.queuelen < 0)
shm->conf.queuelen = MAX(DEFAULT_SHMEM_QUEUELEN, n->in.vectorize);
if (shm->conf.samplelen < 0) {
auto input_sigs = n->getInputSignals(false)->size();
auto output_sigs = 0U;
if (n->getOutputSignals(true))
output_sigs = n->getOutputSignals(true)->size();
shm->conf.samplelen = MAX(input_sigs, output_sigs);
}
return 0;
}
int villas::node::shmem_start(NodeCompat *n) {
auto *shm = n->getData<struct shmem>();
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;
}
int villas::node::shmem_stop(NodeCompat *n) {
auto *shm = n->getData<struct shmem>();
return shmem_int_close(&shm->intf);
2017-04-05 12:52:21 +02:00
}
int villas::node::shmem_read(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
auto *shm = n->getData<struct shmem>();
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.");
n->setState(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++)
smps[i]->signals = n->getInputSignals(false);
return recv;
2017-04-05 12:52:21 +02:00
}
int villas::node::shmem_write(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
auto *shm = n->getData<struct shmem>();
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;
}
char *villas::node::shmem_print(NodeCompat *n) {
auto *shm = n->getData<struct shmem>();
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 NodeCompatType p;
__attribute__((constructor(110))) static void register_plugin() {
2021-06-21 16:11:42 -04:00
p.name = "shmem";
p.description = "POSIX shared memory interface with external processes";
p.vectorize = 0;
p.size = sizeof(struct shmem);
p.parse = shmem_parse;
p.print = shmem_print;
p.start = shmem_start;
p.stop = shmem_stop;
p.read = shmem_read;
p.write = shmem_write;
p.prepare = shmem_prepare;
p.init = shmem_init;
static NodeCompatFactory ncp(&p);
}