2017-04-15 21:22:19 +02:00
|
|
|
/** Shared-memory interface: The interface functions that the external program should use.
|
|
|
|
*
|
|
|
|
* @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-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
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cerrno>
|
2017-04-07 12:57:40 +02:00
|
|
|
#include <fcntl.h>
|
2017-06-14 13:00:43 +02:00
|
|
|
#include <semaphore.h>
|
2017-04-07 12:57:40 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/memory.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/shmem.h>
|
2017-04-07 12:57:40 +02:00
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
|
|
|
|
2017-07-28 12:17:24 +02:00
|
|
|
size_t shmem_total_size(int queuelen, int samplelen)
|
2017-04-07 12:57:40 +02:00
|
|
|
{
|
2018-07-02 14:17:50 +02:00
|
|
|
/* We have the constant const of the memory_type header */
|
|
|
|
return sizeof(struct memory_type)
|
2017-04-15 18:59:22 +02:00
|
|
|
/* and the shared struct itself */
|
2017-04-07 12:57:40 +02:00
|
|
|
+ sizeof(struct shmem_shared)
|
2017-07-28 12:17:24 +02:00
|
|
|
/* the size of the actual queue and the queue for the pool */
|
|
|
|
+ queuelen * (2 * sizeof(struct queue_cell))
|
2017-04-15 18:59:22 +02:00
|
|
|
/* the size of the pool */
|
2021-05-20 06:21:33 -04:00
|
|
|
+ queuelen * kernel::getCachelineSize() * CEIL(SAMPLE_LENGTH(samplelen), kernel::getCachelineSize())
|
2017-07-28 12:17:24 +02:00
|
|
|
/* a memblock for each allocation (1 shmem_shared, 2 queues, 1 pool) */
|
2018-07-02 19:00:55 +02:00
|
|
|
+ 4 * sizeof(struct memory_block)
|
2017-04-15 18:59:22 +02:00
|
|
|
/* and some extra buffer for alignment */
|
2017-04-07 12:57:40 +02:00
|
|
|
+ 1024;
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
int shmem_int_open(const char *wname, const char* rname, struct shmem_int *shm, struct shmem_conf *conf)
|
2017-04-07 12:57:40 +02:00
|
|
|
{
|
2017-06-14 13:00:43 +02:00
|
|
|
char *cptr;
|
2017-07-28 12:17:24 +02:00
|
|
|
int fd, ret;
|
2017-06-08 12:43:24 +02:00
|
|
|
size_t len;
|
2017-04-15 18:59:22 +02:00
|
|
|
void *base;
|
2018-07-02 14:17:50 +02:00
|
|
|
struct memory_type *manager;
|
2017-06-14 13:00:43 +02:00
|
|
|
struct shmem_shared *shared;
|
|
|
|
struct stat stat_buf;
|
|
|
|
sem_t *sem_own, *sem_other;
|
|
|
|
|
|
|
|
/* Ensure both semaphores exist */
|
|
|
|
sem_own = sem_open(wname, O_CREAT, 0600, 0);
|
|
|
|
if (sem_own == SEM_FAILED)
|
2017-06-08 12:43:24 +02:00
|
|
|
return -1;
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
sem_other = sem_open(rname, O_CREAT, 0600, 0);
|
|
|
|
if (sem_other == SEM_FAILED)
|
2018-05-07 15:54:27 +02:00
|
|
|
return -2;
|
2017-06-08 12:43:24 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
/* Open and initialize the shared region for the output queue */
|
2018-05-07 18:03:00 +02:00
|
|
|
retry: fd = shm_open(wname, O_RDWR|O_CREAT|O_EXCL, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
ret = shm_unlink(wname);
|
|
|
|
if (ret)
|
|
|
|
return -12;
|
2019-01-30 01:54:03 +01:00
|
|
|
|
2018-05-07 18:03:00 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:54:27 +02:00
|
|
|
return -3;
|
2018-05-07 18:03:00 +02:00
|
|
|
}
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-07-28 12:17:24 +02:00
|
|
|
len = shmem_total_size(conf->queuelen, conf->samplelen);
|
2017-06-08 12:43:24 +02:00
|
|
|
if (ftruncate(fd, len) < 0)
|
|
|
|
return -1;
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
base = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2017-04-07 13:27:10 +02:00
|
|
|
if (base == MAP_FAILED)
|
2018-05-07 15:54:27 +02:00
|
|
|
return -4;
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
close(fd);
|
2017-06-08 12:43:24 +02:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
manager = memory_managed(base, len);
|
2019-10-26 13:07:02 +02:00
|
|
|
shared = (struct shmem_shared *) memory_alloc(sizeof(struct shmem_shared), manager);
|
2017-06-08 12:43:24 +02:00
|
|
|
if (!shared) {
|
|
|
|
errno = ENOMEM;
|
2018-05-07 15:54:27 +02:00
|
|
|
return -5;
|
2017-06-08 12:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared->polling = conf->polling;
|
2017-10-16 23:08:46 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
int flags = (int) QueueSignalledFlags::PROCESS_SHARED;
|
|
|
|
enum QueueSignalledMode mode = conf->polling
|
|
|
|
? QueueSignalledMode::POLLING
|
|
|
|
: QueueSignalledMode::PTHREAD;
|
|
|
|
|
|
|
|
ret = queue_signalled_init(&shared->queue, conf->queuelen, manager, mode, flags);
|
2017-06-08 12:43:24 +02:00
|
|
|
if (ret) {
|
|
|
|
errno = ENOMEM;
|
2018-05-07 15:54:27 +02:00
|
|
|
return -6;
|
2017-06-08 12:43:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 10:38:54 +02:00
|
|
|
ret = pool_init(&shared->pool, conf->queuelen, SAMPLE_LENGTH(conf->samplelen), manager);
|
2017-06-08 12:43:24 +02:00
|
|
|
if (ret) {
|
|
|
|
errno = ENOMEM;
|
2018-05-07 15:54:27 +02:00
|
|
|
return -7;
|
2017-06-08 12:43:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
shm->write.base = base;
|
|
|
|
shm->write.name = wname;
|
|
|
|
shm->write.len = len;
|
|
|
|
shm->write.shared = shared;
|
|
|
|
|
|
|
|
/* Post own semaphore and wait on the other one, so both processes know that
|
|
|
|
* both regions are initialized */
|
|
|
|
sem_post(sem_own);
|
|
|
|
sem_wait(sem_other);
|
2017-06-08 13:07:20 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
/* Open and map the other region */
|
|
|
|
fd = shm_open(rname, O_RDWR, 0);
|
|
|
|
if (fd < 0)
|
2018-05-07 15:54:27 +02:00
|
|
|
return -8;
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
if (fstat(fd, &stat_buf) < 0)
|
2018-05-07 15:54:27 +02:00
|
|
|
return -9;
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
len = stat_buf.st_size;
|
2019-04-07 15:13:40 +02:00
|
|
|
base = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
if (base == MAP_FAILED)
|
2018-05-07 15:54:27 +02:00
|
|
|
return -10;
|
2017-06-08 13:07:20 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
cptr = (char *) base + sizeof(struct memory_type) + sizeof(struct memory_block);
|
2017-06-14 13:00:43 +02:00
|
|
|
shared = (struct shmem_shared *) cptr;
|
|
|
|
shm->read.base = base;
|
|
|
|
shm->read.name = rname;
|
|
|
|
shm->read.len = len;
|
|
|
|
shm->read.shared = shared;
|
2017-06-08 13:07:20 +02:00
|
|
|
|
2017-07-28 12:17:37 +02:00
|
|
|
shm->readers = 0;
|
|
|
|
shm->writers = 0;
|
|
|
|
shm->closed = 0;
|
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
/* Unlink the semaphores; we don't need them anymore */
|
|
|
|
sem_unlink(wname);
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
return 0;
|
2017-04-07 12:57:40 +02:00
|
|
|
}
|
2017-04-07 13:27:10 +02:00
|
|
|
|
2017-06-08 12:43:24 +02:00
|
|
|
int shmem_int_close(struct shmem_int *shm)
|
2017-04-12 14:38:18 +02:00
|
|
|
{
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-07-28 12:17:37 +02:00
|
|
|
atomic_store(&shm->closed, 1);
|
2017-10-16 23:08:46 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = queue_signalled_close(&shm->write.shared->queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
shm_unlink(shm->write.name);
|
2020-09-10 11:11:42 +02:00
|
|
|
|
2017-07-28 12:17:37 +02:00
|
|
|
if (atomic_load(&shm->readers) == 0)
|
|
|
|
munmap(shm->read.base, shm->read.len);
|
2020-09-10 11:11:42 +02:00
|
|
|
|
2017-07-28 12:17:37 +02:00
|
|
|
if (atomic_load(&shm->writers) == 0)
|
|
|
|
munmap(shm->write.base, shm->write.len);
|
2017-06-15 15:07:19 +02:00
|
|
|
|
2017-06-14 13:00:43 +02:00
|
|
|
return 0;
|
2017-04-12 14:38:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int shmem_int_read(struct shmem_int *shm, struct sample * const smps[], unsigned cnt)
|
2017-04-12 14:38:18 +02:00
|
|
|
{
|
2017-07-28 12:17:37 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
atomic_fetch_add(&shm->readers, 1);
|
|
|
|
|
2017-08-30 12:37:09 +02:00
|
|
|
ret = queue_signalled_pull_many(&shm->read.shared->queue, (void **) smps, cnt);
|
2017-07-28 12:17:37 +02:00
|
|
|
|
|
|
|
if (atomic_fetch_sub(&shm->readers, 1) == 1 && atomic_load(&shm->closed) == 1)
|
|
|
|
munmap(shm->read.base, shm->read.len);
|
|
|
|
|
|
|
|
return ret;
|
2017-04-07 13:27:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int shmem_int_write(struct shmem_int *shm, const struct sample * const smps[], unsigned cnt)
|
2017-04-12 14:38:18 +02:00
|
|
|
{
|
2017-07-28 12:17:37 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
atomic_fetch_add(&shm->writers, 1);
|
|
|
|
|
2017-08-30 12:37:09 +02:00
|
|
|
ret = queue_signalled_push_many(&shm->write.shared->queue, (void **) smps, cnt);
|
2017-07-28 12:17:37 +02:00
|
|
|
|
|
|
|
if (atomic_fetch_sub(&shm->writers, 1) == 1 && atomic_load(&shm->closed) == 1)
|
|
|
|
munmap(shm->write.base, shm->write.len);
|
|
|
|
|
|
|
|
return ret;
|
2017-04-07 13:27:10 +02:00
|
|
|
}
|
2017-06-17 03:15:35 +02:00
|
|
|
|
|
|
|
int shmem_int_alloc(struct shmem_int *shm, struct sample *smps[], unsigned cnt)
|
|
|
|
{
|
2017-10-16 08:08:17 +02:00
|
|
|
return sample_alloc_many(&shm->write.shared->pool, smps, cnt);
|
2017-06-17 03:15:35 +02:00
|
|
|
}
|