From 3a7af08799e3188c486cabf44723085fb1bbc916 Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Fri, 7 Apr 2017 12:57:40 +0200 Subject: [PATCH] move "external" part of shmem to separate files --- include/villas/kernel/kernel.h | 3 ++- include/villas/nodes/shmem.h | 21 +----------------- include/villas/shmem.h | 23 ++++++++++++++++++++ lib/Makefile.inc | 2 +- lib/nodes/shmem.c | 31 +-------------------------- lib/shmem.c | 39 ++++++++++++++++++++++++++++++++++ src/shmem.c | 2 ++ 7 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 include/villas/shmem.h create mode 100644 lib/shmem.c diff --git a/include/villas/kernel/kernel.h b/include/villas/kernel/kernel.h index 5956c01b3..81ce89539 100644 --- a/include/villas/kernel/kernel.h +++ b/include/villas/kernel/kernel.h @@ -9,6 +9,7 @@ #pragma once +#include #include /* Forward declarations */ @@ -70,4 +71,4 @@ int kernel_get_hugepage_size(); /** Set SMP affinity of IRQ */ int kernel_irq_setaffinity(unsigned irq, uintmax_t new, uintmax_t *old); -/** @} */ \ No newline at end of file +/** @} */ diff --git a/include/villas/nodes/shmem.h b/include/villas/nodes/shmem.h index b56e3afee..10dfeae2f 100644 --- a/include/villas/nodes/shmem.h +++ b/include/villas/nodes/shmem.h @@ -5,22 +5,10 @@ #include "memory.h" #include "pool.h" #include "queue.h" -#include "queue_signalled.h" +#include "shmem.h" #define DEFAULT_SHMEM_QUEUESIZE 512 -union shmem_queue { - struct queue q; - struct queue_signalled qs; -}; - -/** The structure that actually resides in the shared memory. TODO better name?*/ -struct shmem_shared { - union shmem_queue in; /**< Queue for samples passed from external program to node.*/ - union shmem_queue out; /**< Queue for samples passed from node to external program.*/ - struct pool pool; /**< Pool for the samples in the queues. */ -}; - struct shmem { const char* name; /**< Name of the shm object. */ int sample_size; /**< Number of data entries for each sample. */ @@ -46,11 +34,4 @@ int shmem_close(struct node *n); int shmem_read(struct node *n, struct sample *smps[], unsigned cnt); int shmem_write(struct node *n, struct sample *smps[], unsigned cnt); - -/* The interface functions that the external program should use. TODO put this - * in another file? */ - -struct shmem_shared * shmem_int_open(const char* name, size_t len); - -size_t shmem_total_size(int insize, int outsize, int sample_size); #endif /* _SHMEM_H_ */ diff --git a/include/villas/shmem.h b/include/villas/shmem.h new file mode 100644 index 000000000..442d05174 --- /dev/null +++ b/include/villas/shmem.h @@ -0,0 +1,23 @@ +#pragma once + +/* The interface functions that the external program should use. */ + +#include "pool.h" +#include "queue.h" +#include "queue_signalled.h" + +union shmem_queue { + struct queue q; + struct queue_signalled qs; +}; + +/** The structure that actually resides in the shared memory. TODO better name?*/ +struct shmem_shared { + union shmem_queue in; /**< Queue for samples passed from external program to node.*/ + union shmem_queue out; /**< Queue for samples passed from node to external program.*/ + struct pool pool; /**< Pool for the samples in the queues. */ +}; + +struct shmem_shared * shmem_int_open(const char* name, size_t len); + +size_t shmem_total_size(int insize, int outsize, int sample_size); diff --git a/lib/Makefile.inc b/lib/Makefile.inc index b20543b9e..79307906e 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -9,7 +9,7 @@ LIB_SRCS = $(addprefix lib/nodes/, file.c cbuilder.c shmem.c) \ $(addprefix lib/, sample.c path.c node.c hook.c \ log.c utils.c super_node.c hist.c timing.c pool.c \ list.c queue.c queue_signalled.c memory.c advio.c web.c api.c \ - plugin.c node_type.c stats.c mapping.c sample_io.c\ + plugin.c node_type.c stats.c mapping.c sample_io.c shmem.c \ ) LIB_CFLAGS = $(CFLAGS) -fPIC diff --git a/lib/nodes/shmem.c b/lib/nodes/shmem.c index 7444b2f02..e1bdc6181 100644 --- a/lib/nodes/shmem.c +++ b/lib/nodes/shmem.c @@ -9,6 +9,7 @@ #include "log.h" #include "nodes/shmem.h" #include "plugin.h" +#include "shmem.h" #include "utils.h" int shmem_parse(struct node *n, config_setting_t *cfg) { @@ -152,33 +153,3 @@ static struct plugin p = { }; REGISTER_PLUGIN(&p) - -size_t shmem_total_size(int insize, int outsize, int sample_size) -{ - // we have the constant const of the memtype header - return sizeof(struct memtype) - // and the shared struct itself - + sizeof(struct shmem_shared) - // the size of the 2 queues and the queue for the pool - + (insize + outsize) * (2*sizeof(struct queue_cell)) - // the size of the pool - + (insize + outsize) * kernel_get_cacheline_size() * CEIL(SAMPLE_LEN(sample_size), kernel_get_cacheline_size()) - // a memblock for each allocation (1 shmem_shared, 3 queues, 1 pool) - + 5 * sizeof(struct memblock) - // and some extra buffer for alignment - + 1024; -} - -struct shmem_shared* shmem_int_open(const char *name, size_t len) -{ - int fd = shm_open(name, O_RDWR, 0); - if (fd < 0) - return NULL; - void *base = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if (base == MAP_FAILED) - return NULL; - /* This relies on the behaviour of the node and the allocator; it assumes - * that memtype_managed is used and the shmem_shared is the first allocated object */ - char *cptr = (char *) base + sizeof(struct memtype) + sizeof(struct memblock); - return (struct shmem_shared *) cptr; -} diff --git a/lib/shmem.c b/lib/shmem.c new file mode 100644 index 000000000..89d4bc3d1 --- /dev/null +++ b/lib/shmem.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "kernel/kernel.h" +#include "memory.h" +#include "utils.h" +#include "sample.h" +#include "shmem.h" + +size_t shmem_total_size(int insize, int outsize, int sample_size) +{ + // we have the constant const of the memtype header + return sizeof(struct memtype) + // and the shared struct itself + + sizeof(struct shmem_shared) + // the size of the 2 queues and the queue for the pool + + (insize + outsize) * (2*sizeof(struct queue_cell)) + // the size of the pool + + (insize + outsize) * kernel_get_cacheline_size() * CEIL(SAMPLE_LEN(sample_size), kernel_get_cacheline_size()) + // a memblock for each allocation (1 shmem_shared, 3 queues, 1 pool) + + 5 * sizeof(struct memblock) + // and some extra buffer for alignment + + 1024; +} + +struct shmem_shared* shmem_int_open(const char *name, size_t len) +{ + int fd = shm_open(name, O_RDWR, 0); + if (fd < 0) + return NULL; + void *base = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (base == MAP_FAILED) + return NULL; + /* This relies on the behaviour of the node and the allocator; it assumes + * that memtype_managed is used and the shmem_shared is the first allocated object */ + char *cptr = (char *) base + sizeof(struct memtype) + sizeof(struct memblock); + return (struct shmem_shared *) cptr; +} diff --git a/src/shmem.c b/src/shmem.c index 0d55fdc72..dc9e7fb71 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -4,11 +4,13 @@ #include "config.h" #include "log.h" +#include "node.h" #include "nodes/shmem.h" #include "pool.h" #include "queue.h" #include "sample.h" #include "sample_io.h" +#include "shmem.h" #include "super_node.h" #include "utils.h"