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.c

203 lines
5.4 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>
* @copyright 2017, 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 <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "kernel/kernel.h"
#include "log.h"
#include "shmem.h"
2017-04-05 12:52:21 +02:00
#include "nodes/shmem.h"
2017-04-06 12:12:56 +02:00
#include "plugin.h"
2017-05-12 11:36:23 +02:00
#include "timing.h"
2017-04-06 12:12:56 +02:00
#include "utils.h"
2017-04-05 12:52:21 +02:00
int shmem_parse(struct node *n, config_setting_t *cfg)
{
2017-04-05 12:52:21 +02:00
struct shmem *shm = n->_vd;
if (!config_setting_lookup_string(cfg, "out_name", &shm->out_name))
cerror(cfg, "Missing shared memory output queue name");
if (!config_setting_lookup_string(cfg, "in_name", &shm->in_name))
cerror(cfg, "Missing shared memory input queue name");
if (!config_setting_lookup_int(cfg, "queuelen", &shm->conf.queuelen))
shm->conf.queuelen = DEFAULT_SHMEM_QUEUELEN;
if (!config_setting_lookup_int(cfg, "samplelen", &shm->conf.samplelen))
shm->conf.samplelen = DEFAULT_SHMEM_SAMPLELEN;
if (!config_setting_lookup_bool(cfg, "polling", &shm->conf.polling))
shm->conf.polling = false;
config_setting_t *exec_cfg = config_setting_lookup(cfg, "exec");
if (!exec_cfg)
shm->exec = NULL;
else {
if (!config_setting_is_array(exec_cfg))
cerror(exec_cfg, "Invalid format for exec");
shm->exec = alloc(sizeof(char *) * (config_setting_length(exec_cfg) + 1));
int i;
for (i = 0; i < config_setting_length(exec_cfg); i++) {
2017-04-15 18:22:47 +02:00
const char *elm = config_setting_get_string_elem(exec_cfg, i);
if (!elm)
cerror(exec_cfg, "Invalid format for exec");
2017-04-15 18:22:47 +02:00
shm->exec[i] = strdup(elm);
}
shm->exec[i] = NULL;
}
2017-04-05 12:52:21 +02:00
return 0;
}
int shmem_open(struct node *n)
{
2017-04-05 12:52:21 +02:00
struct shmem *shm = n->_vd;
int ret;
if (shm->exec) {
ret = spawn(shm->exec[0], shm->exec);
if (!ret)
serror("Failed to spawn external program");
}
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)
serror("Opening shared memory interface failed");
2017-04-05 12:52:21 +02:00
return 0;
}
int shmem_close(struct node *n)
{
2017-04-05 12:52:21 +02:00
struct shmem* shm = n->_vd;
return shmem_int_close(&shm->intf);
2017-04-05 12:52:21 +02:00
}
int shmem_read(struct node *n, struct sample *smps[], unsigned cnt)
{
2017-04-05 12:52:21 +02:00
struct shmem *shm = n->_vd;
int recv;
2017-05-12 11:36:23 +02:00
struct sample *shared_smps[cnt];
recv = shmem_int_read(&shm->intf, shared_smps, cnt);
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 */
shmem_int_close(&shm->intf);
if (recv <= 0)
return recv;
2017-05-12 11:36:23 +02:00
sample_copy_many(smps, shared_smps, recv);
sample_put_many(shared_smps, recv);
2017-05-12 11:36:23 +02:00
struct timespec ts_recv = time_now();
2017-05-12 11:36:23 +02:00
for (int i = 0; i < recv; i++)
smps[i]->ts.received = ts_recv;
return recv;
2017-04-05 12:52:21 +02:00
}
int shmem_write(struct node *n, struct sample *smps[], unsigned cnt)
{
2017-04-05 12:52:21 +02:00
struct shmem *shm = n->_vd;
2017-04-17 18:50:43 +02:00
struct sample *shared_smps[cnt]; /* Samples need to be copied to the shared pool first */
int avail, pushed, len;
avail = sample_alloc(&shm->intf.write.shared->pool, shared_smps, cnt);
2017-04-05 12:52:21 +02:00
if (avail != cnt)
warn("Pool underrun for shmem node %s", shm->out_name);
2017-04-05 12:52:21 +02:00
for (int i = 0; i < avail; i++) {
/* Since the node isn't in shared memory, the source can't be accessed */
shared_smps[i]->source = NULL;
shared_smps[i]->sequence = smps[i]->sequence;
shared_smps[i]->ts = smps[i]->ts;
len = MIN(smps[i]->length, shared_smps[i]->capacity);
2017-04-05 12:52:21 +02:00
if (len != smps[i]->length)
warn("Losing data because of sample capacity mismatch in node %s", node_name(n));
memcpy(shared_smps[i]->data, smps[i]->data, SAMPLE_DATA_LEN(len));
2017-04-05 12:52:21 +02:00
shared_smps[i]->length = len;
}
pushed = shmem_int_write(&shm->intf, shared_smps, avail);
2017-04-05 12:52:21 +02:00
if (pushed != avail)
warn("Outgoing queue overrun for node %s", node_name(n));
2017-04-05 12:52:21 +02:00
return pushed;
}
char * shmem_print(struct node *n)
{
2017-04-05 12:52:21 +02:00
struct shmem *shm = n->_vd;
char *buf = NULL;
strcatf(&buf, "out_name=%s, in_name=%s, queuelen=%d, samplelen=%d, polling=%s",
shm->out_name, shm->in_name, shm->conf.queuelen, shm->conf.samplelen, 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-06 12:12:56 +02:00
static struct plugin p = {
2017-04-05 12:52:21 +02:00
.name = "shmem",
.description = "POSIX shared memory interface with external processes",
2017-04-06 12:12:56 +02:00
.type = PLUGIN_TYPE_NODE,
.node = {
.vectorize = 0,
2017-04-06 12:12:56 +02:00
.size = sizeof(struct shmem),
.parse = shmem_parse,
.print = shmem_print,
.start = shmem_open,
.stop = shmem_close,
.read = shmem_read,
.write = shmem_write,
.instances = LIST_INIT(),
}
2017-04-05 12:52:21 +02:00
};
2017-04-06 12:12:56 +02:00
REGISTER_PLUGIN(&p)