2017-04-15 21:22:19 +02:00
|
|
|
/* 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
|
2017-04-15 21:22:19 +02:00
|
|
|
*/
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#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>
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#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>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/shmem.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/shmem.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/timing.hpp>
|
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;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::shmem_init(NodeCompat *n) {
|
|
|
|
auto *shm = n->getData<struct shmem>();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Default values
|
|
|
|
shm->conf.queuelen = -1;
|
|
|
|
shm->conf.samplelen = -1;
|
|
|
|
shm->conf.polling = false;
|
|
|
|
shm->exec = nullptr;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2021-08-10 10:12:48 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::shmem_parse(NodeCompat *n, json_t *json) {
|
|
|
|
auto *shm = n->getData<struct shmem>();
|
|
|
|
const char *val, *mode_str = nullptr;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
json_t *json_exec = nullptr;
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
throw SystemError("Unknown mode '{}'", mode_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_exec) {
|
|
|
|
if (!json_is_array(json_exec))
|
|
|
|
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();
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
json_t *json_val;
|
|
|
|
json_array_foreach(json_exec, i, json_val) {
|
|
|
|
val = json_string_value(json_val);
|
|
|
|
if (!val)
|
|
|
|
throw SystemError("Setting 'exec' must be an array of strings");
|
|
|
|
|
|
|
|
shm->exec[i] = strdup(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
shm->exec[i] = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::shmem_prepare(NodeCompat *n) {
|
|
|
|
auto *shm = n->getData<struct shmem>();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (shm->conf.queuelen < 0)
|
|
|
|
shm->conf.queuelen = MAX(DEFAULT_SHMEM_QUEUELEN, n->in.vectorize);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (shm->conf.samplelen < 0) {
|
|
|
|
auto input_sigs = n->getInputSignals(false)->size();
|
|
|
|
auto output_sigs = 0U;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (n->getOutputSignals(true))
|
|
|
|
output_sigs = n->getOutputSignals(true)->size();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
shm->conf.samplelen = MAX(input_sigs, output_sigs);
|
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2021-08-10 10:12:48 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::shmem_start(NodeCompat *n) {
|
|
|
|
auto *shm = n->getData<struct shmem>();
|
|
|
|
int ret;
|
2017-04-12 23:07:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (shm->exec) {
|
|
|
|
ret = spawn(shm->exec[0], shm->exec);
|
|
|
|
if (!ret)
|
|
|
|
throw SystemError("Failed to spawn external program");
|
2018-05-23 14:59:05 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
sleep(1);
|
|
|
|
}
|
2017-06-08 13:07:20 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = shmem_int_open(shm->out_name, shm->in_name, &shm->intf, &shm->conf);
|
|
|
|
if (ret < 0)
|
|
|
|
throw SystemError("Opening shared memory interface failed (ret={})", ret);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::shmem_stop(NodeCompat *n) {
|
|
|
|
auto *shm = n->getData<struct shmem>();
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return shmem_int_close(&shm->intf);
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +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-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
do {
|
|
|
|
recv = shmem_int_read(&shm->intf, shared_smps, cnt);
|
|
|
|
} while (recv == 0);
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (recv < 0) {
|
|
|
|
/* This can only really mean that the other process has exited, so close
|
2024-02-29 21:47:13 +01:00
|
|
|
* the interface to make sure the shared memory object is unlinked */
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
n->logger->info("Shared memory segment has been closed.");
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
n->setState(State::STOPPING);
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return recv;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
sample_copy_many(smps, shared_smps, recv);
|
|
|
|
sample_decref_many(shared_smps, recv);
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// TODO: signal descriptions are currently not shared between processes
|
|
|
|
for (int i = 0; i < recv; i++)
|
|
|
|
smps[i]->signals = n->getInputSignals(false);
|
2019-01-23 00:48:38 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return recv;
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
avail = sample_alloc_many(&shm->intf.write.shared->pool, shared_smps, cnt);
|
|
|
|
if (avail != (int)cnt)
|
|
|
|
n->logger->warn("Pool underrun for shmem node {}", shm->out_name);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
copied = sample_copy_many(shared_smps, smps, avail);
|
|
|
|
if (copied < avail)
|
|
|
|
n->logger->warn("Outgoing pool underrun");
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
pushed = shmem_int_write(&shm->intf, shared_smps, copied);
|
|
|
|
if (pushed != avail)
|
|
|
|
n->logger->warn("Outgoing queue overrun for node");
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return pushed;
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
char *villas::node::shmem_print(NodeCompat *n) {
|
|
|
|
auto *shm = n->getData<struct shmem>();
|
|
|
|
char *buf = nullptr;
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2023-09-07 11:46:39 +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
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (shm->exec) {
|
|
|
|
strcatf(&buf, ", exec='");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +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
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
strcatf(&buf, "'");
|
|
|
|
}
|
2017-04-15 18:15:28 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return buf;
|
2017-08-30 13:30:31 +02:00
|
|
|
}
|
2017-04-05 12:52:21 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
static NodeCompatType p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110))) static void register_plugin() {
|
|
|
|
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);
|
2019-04-23 13:15:00 +02:00
|
|
|
}
|