From 7712e3ea9fc6772b47f593acbe3088cae01c16d2 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sat, 19 Mar 2016 13:40:03 +0100 Subject: lib: shared memory map The shared memory map will allow passing SDU's between IPCPs efficiently. It is a single block of managed memory. SDU's are stored in a shm_du_buff datastructure, which is a list of blocks inside the shared memory region. The mechanism for passing SDU's is not yet implemented and could be either within the region or using another local IPC mechanism. The following things are configured at compile time: SHM_FILE_NAME : the name. Default is "ouroboros_du_map". SHM_DU_BUFF_BLOCK_SIZE: size of a block inside the shared memory region. Default is the pagesize of the system. SHM_DU_MAP_SIZE : the approximate size in bytes of the shared memory map. The actual filesize may differ by a small margin. Default is 4MB. --- include/ouroboros/CMakeLists.txt | 1 + include/ouroboros/shm_du_map.h | 75 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 include/ouroboros/shm_du_map.h (limited to 'include') diff --git a/include/ouroboros/CMakeLists.txt b/include/ouroboros/CMakeLists.txt index 167cf0e2..a196140b 100644 --- a/include/ouroboros/CMakeLists.txt +++ b/include/ouroboros/CMakeLists.txt @@ -14,6 +14,7 @@ set(HEADER_FILES list.h logs.h rina_name.h + shm_du_map.h sockets.h utils.h ) diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h new file mode 100644 index 00000000..948a6727 --- /dev/null +++ b/include/ouroboros/shm_du_map.h @@ -0,0 +1,75 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Shared memory map for data units + * + * Dimitri Staessens + * Sander Vrijders + * + * 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 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef OUROBOROS_SHM_DU_MAP_H +#define OUROBOROS_SHM_DU_MAP_H + +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 199506L +#endif + +#define OUROBOROS_PREFIX "shm_du_map" + +#ifndef SHM_DU_BUFF_BLOCK_SIZE +#define SHM_DU_BUFF_BLOCK_SIZE sysconf(_SC_PAGESIZE) +#endif + +#ifndef SHM_DU_MAP_FILENAME +#define SHM_DU_MAP_FILENAME "ouroboros_du_map" +#endif + +#ifndef SHM_DU_MAP_SIZE +#define SHM_DU_MAP_SIZE (1 << 22) +#endif + +#include "common.h" +#include "logs.h" + +struct shm_du_buff; + +struct shm_du_map; + +struct shm_du_map * shm_du_map_create(); +struct shm_du_map * shm_du_map_open(); +void shm_du_map_close(struct shm_du_map * dum); + +struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, + size_t size, + size_t headspace, + uint8_t * data, + size_t len); +void shm_release_du_buff(struct shm_du_map * dum, + struct shm_du_buff * sdb); + +uint8_t * shm_du_buff_head_alloc(struct shm_du_map * dum, + struct shm_du_buff * sdb, + size_t size); +uint8_t * shm_du_buff_tail_alloc(struct shm_du_map * dum, + struct shm_du_buff * sdb, + size_t size); +int shm_du_buff_head_release(struct shm_du_buff * sdb, + size_t size); +int shm_du_buff_tail_release(struct shm_du_buff * sdb, + size_t size); + +#endif /* OUROBOROS_SHM_DU_MAP_H */ -- cgit v1.2.3 From a9bd08bf09c7baa9254a4b63aacb6bbb23f85f07 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sat, 19 Mar 2016 19:07:55 +0100 Subject: lib: updated shm_du_map to be a hybrid ring buffer Elements must be created/destroyed in order, but IPCPs can access PCI while the PDU is in the ring buffer. Test updated, no more waits are needed. --- include/ouroboros/shm_du_map.h | 5 +- src/lib/shm_du_map.c | 194 +++++++++---------------- src/lib/tests/shm_du_map_test_du_buff_create.c | 6 +- src/lib/tests/shm_du_map_test_prod_cons.c | 58 ++------ 4 files changed, 89 insertions(+), 174 deletions(-) (limited to 'include') diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index 948a6727..bfccf60a 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -39,7 +39,7 @@ #endif #ifndef SHM_DU_MAP_SIZE -#define SHM_DU_MAP_SIZE (1 << 22) +#define SHM_DU_MAP_SIZE (1 << 26) #endif #include "common.h" @@ -58,8 +58,7 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t headspace, uint8_t * data, size_t len); -void shm_release_du_buff(struct shm_du_map * dum, - struct shm_du_buff * sdb); +int shm_release_du_buff(struct shm_du_map * dum); uint8_t * shm_du_buff_head_alloc(struct shm_du_map * dum, struct shm_du_buff * sdb, diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 052b8868..630cfa42 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -34,23 +34,28 @@ #define SHM_DU_BLOCK_DATA_SIZE (SHM_DU_BUFF_BLOCK_SIZE - \ - sizeof (struct shm_block)) + sizeof(struct shm_block)) #define SHM_BLOCKS_IN_MAP (SHM_DU_MAP_SIZE / SHM_DU_BUFF_BLOCK_SIZE) #define SHM_BLOCKS_SIZE (SHM_DU_BUFF_BLOCK_SIZE * SHM_BLOCKS_IN_MAP) #define SHM_BUFFS_SIZE (SHM_BLOCKS_IN_MAP * sizeof (struct shm_du_buff)) -#define SHM_MGT_MAP_SIZE (SHM_BLOCKS_IN_MAP * sizeof (uint8_t)) #define SHM_FILE_SIZE (SHM_BLOCKS_IN_MAP * (SHM_DU_BUFF_BLOCK_SIZE \ - + sizeof (struct shm_du_buff) \ - + sizeof (uint8_t)) \ + + sizeof(struct shm_du_buff) \ + + sizeof(uint8_t)) \ + + 2 * sizeof (size_t) \ + sizeof(pthread_mutex_t)) -#define idx_to_block_ptr(dum, i) ((struct shm_block *) \ +#define idx_to_block_ptr(dum, i) ((struct shm_block *) \ (dum->shm_base + i * SHM_DU_BUFF_BLOCK_SIZE)) #define idx_to_du_buff_ptr(dum, i) (dum->ptr_du_buff + i) #define du_buff_ptr_to_idx(dum, sdb) ((sdb - dum->ptr_du_buff) / sizeof *sdb) #define block_ptr_to_idx(dum, sdb) (((uint8_t *)sdb - dum->shm_base) \ / SHM_DU_BUFF_BLOCK_SIZE) +#define shm_map_used(dum) ((*(dum->ptr_head) + SHM_BLOCKS_IN_MAP - \ + *(dum->ptr_tail)) % SHM_BLOCKS_IN_MAP) + +#define shm_map_free(dum, i)(shm_map_used(dum) + i < SHM_BLOCKS_IN_MAP) + struct shm_block { size_t size; long next; @@ -64,9 +69,10 @@ struct shm_du_buff { }; struct shm_du_map { - uint8_t * shm_base; /* start of buffer blocks */ + uint8_t * shm_base; /* start of blocks */ struct shm_du_buff * ptr_du_buff; /* start of du_buff structs */ - uint8_t * ptr_map; /* start of management map */ + size_t * ptr_head; /* start of ringbuffer head */ + size_t * ptr_tail; /* start of ringbuffer tail */ pthread_mutex_t * shm_mutex; /* lock all free space in shm */ int fd; }; @@ -76,9 +82,7 @@ struct shm_du_map * shm_du_map_create() struct shm_du_map * dum; int shm_fd; uint8_t * shm_base; - uint8_t * ptr; pthread_mutexattr_t attr; - int i; dum = malloc(sizeof *dum); if (dum == NULL) { @@ -129,17 +133,19 @@ struct shm_du_map * shm_du_map_create() dum->shm_base = shm_base; dum->ptr_du_buff = (struct shm_du_buff *) ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); - dum->ptr_map = (uint8_t *) dum->ptr_du_buff + SHM_BUFFS_SIZE; + dum->ptr_head = (size_t *) + ((uint8_t *) dum->ptr_du_buff + SHM_BUFFS_SIZE); + dum->ptr_tail = (size_t *) + ((uint8_t *) dum->ptr_head + sizeof(size_t)); dum->shm_mutex = (pthread_mutex_t *) - ((uint8_t *) dum->ptr_map + SHM_MGT_MAP_SIZE); + ((uint8_t *) dum->ptr_tail + sizeof(size_t)); pthread_mutexattr_init(&attr); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(dum->shm_mutex, &attr); - ptr = dum->ptr_map; - for (i = 0; i < SHM_BLOCKS_IN_MAP; ++i) - *(ptr++) = 0; + *dum->ptr_head = 0; + *dum->ptr_tail = 0; dum->fd = shm_fd; @@ -185,9 +191,12 @@ struct shm_du_map * shm_du_map_open() dum->shm_base = shm_base; dum->ptr_du_buff = (struct shm_du_buff *) ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); - dum->ptr_map = (uint8_t *) dum->ptr_du_buff + SHM_BUFFS_SIZE; + dum->ptr_head = (size_t *) + ((uint8_t *) dum->ptr_du_buff + SHM_BUFFS_SIZE); + dum->ptr_tail = (size_t *) + ((uint8_t *) dum->ptr_head + sizeof(size_t)); dum->shm_mutex = (pthread_mutex_t *) - ((uint8_t *) dum->ptr_map + SHM_MGT_MAP_SIZE); + ((uint8_t *) dum->ptr_tail + sizeof(size_t)); return dum; } @@ -208,53 +217,6 @@ void shm_du_map_close(struct shm_du_map * dum) free(dum); } -static struct shm_du_buff * alloc_du_buff(struct shm_du_map * dum) -{ - uint8_t * ptr = dum->ptr_map; - - pthread_mutex_lock(dum->shm_mutex); - - while (*ptr == 1 && ptr < (uint8_t *) dum->shm_mutex) - ++ptr; - - if (ptr < (uint8_t *) dum->shm_mutex) { - *ptr = 1; - } else { - pthread_mutex_unlock(dum->shm_mutex); - return NULL; - } - - pthread_mutex_unlock(dum->shm_mutex); - - return dum->ptr_du_buff + (ptr - dum->ptr_map); -} - -static struct shm_block * alloc_block(struct shm_du_map * dum) -{ - - pthread_mutex_lock(dum->shm_mutex); - - uint8_t * ptr = dum->ptr_map; - - while (*ptr == 1 && ptr < (uint8_t *) dum->shm_mutex) - ++ptr; - - if (ptr < (uint8_t *) dum->shm_mutex) { - *ptr = 1; - } - else { - pthread_mutex_unlock(dum->shm_mutex); - return NULL; - } - - pthread_mutex_unlock(dum->shm_mutex); - - - return (struct shm_block *) - (dum->shm_base + ((ptr - dum->ptr_map) - * SHM_DU_BUFF_BLOCK_SIZE)); -} - struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t size, size_t headspace, @@ -262,11 +224,11 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t len) { struct shm_du_buff * sdb; - long index; long prev_index = -1; size_t remaining = size; size_t ts = size - (headspace + len); uint8_t * read_pos = data; + size_t blocks; if (dum == NULL || data == NULL) { LOG_DBGF("Bogus input, bugging out."); @@ -288,51 +250,42 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, return NULL; } - sdb = alloc_du_buff(dum); - if (sdb == NULL) { + pthread_mutex_lock(dum->shm_mutex); + + blocks = size / SHM_DU_BLOCK_DATA_SIZE; + if (size % SHM_DU_BLOCK_DATA_SIZE > 0) + ++blocks; + + if (!shm_map_free(dum, blocks)) { + pthread_mutex_unlock(dum->shm_mutex); LOG_DBGF("Allocation failed, Out of Memory."); return NULL; } - index = du_buff_ptr_to_idx(dum, sdb); + sdb = dum->ptr_du_buff + *dum->ptr_head; + sdb->size = size; sdb->du_head = headspace; sdb->du_tail = sdb->du_head + len; while (remaining > 0) { struct shm_block * shm_buf; - unsigned long bytes_to_copy = len; - uint8_t * write_pos; + long bytes_to_copy = len; + uint8_t * write_pos; - if (prev_index == -1) - shm_buf = idx_to_block_ptr(dum, index); - else - shm_buf = alloc_block(dum); - - if (shm_buf == NULL) { - shm_release_du_buff(dum, sdb); - return NULL; - } + shm_buf = idx_to_block_ptr(dum, *(dum->ptr_head)); write_pos = (uint8_t *) shm_buf + sizeof *shm_buf; - index = block_ptr_to_idx(dum, shm_buf); + shm_buf->size = remaining < SHM_DU_BLOCK_DATA_SIZE ? + remaining : SHM_DU_BLOCK_DATA_SIZE; - if (size > SHM_DU_BLOCK_DATA_SIZE - && remaining - ts <= SHM_DU_BLOCK_DATA_SIZE - && remaining != ts) { + bytes_to_copy = shm_buf->size; + + if (remaining <= SHM_DU_BLOCK_DATA_SIZE) + bytes_to_copy -= ts; + else if (remaining - ts <= SHM_DU_BLOCK_DATA_SIZE) shm_buf->size = remaining - ts; - bytes_to_copy = shm_buf->size; - } else if (size > SHM_DU_BLOCK_DATA_SIZE && remaining == ts) { - shm_buf->size = ts; - bytes_to_copy = 0; - } else { - shm_buf->size = remaining < SHM_DU_BLOCK_DATA_SIZE ? - remaining : SHM_DU_BLOCK_DATA_SIZE; - bytes_to_copy =shm_buf->size; - if (remaining == shm_buf->size) - bytes_to_copy -= ts; - } remaining -= shm_buf->size; @@ -340,16 +293,17 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, #ifdef CONFIG_OUROBOROS_DEBUG memset(write_pos, 0, sdb->du_head); #endif - write_pos += sdb->du_head; bytes_to_copy -= sdb->du_head; } if (prev_index != -1) - idx_to_block_ptr(dum, prev_index)->next = index; - - memcpy(write_pos, read_pos, bytes_to_copy); + idx_to_block_ptr(dum, prev_index)->next = + *(dum->ptr_head); + if (len > 0) { + memcpy(write_pos, read_pos, bytes_to_copy); + } read_pos += bytes_to_copy; #ifdef CONFIG_OUROBOROS_DEBUG if (remaining == 0) { @@ -360,52 +314,38 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, shm_buf->next = -1; shm_buf->prev = prev_index; - prev_index = index; + prev_index = *dum->ptr_head; + + *(dum->ptr_head) = (*dum->ptr_head + 1) % SHM_BLOCKS_IN_MAP; } + pthread_mutex_unlock(dum->shm_mutex); + return sdb; } -void shm_release_du_buff(struct shm_du_map * dum, struct shm_du_buff * sdb) +int shm_release_du_buff(struct shm_du_map * dum) { - long index = 0; - - if (sdb == NULL) { - LOG_DBGF("Bogus input, bugging out."); - return; - } - - if (sdb < dum->ptr_du_buff || sdb > dum->ptr_du_buff + SHM_BUFFS_SIZE) { - LOG_DBGF("Refused to free sdb outside of region."); - return; - } - - if ((sdb - dum->ptr_du_buff) % sizeof *sdb != 0) { - LOG_DBGF("Refused to free sdb at incorrect offset."); - return; - } - - index = du_buff_ptr_to_idx(dum, sdb); + int released = 0; pthread_mutex_lock(dum->shm_mutex); - if (dum->ptr_map[index] == 0) { - LOG_DBGF("Attempt to free unused sdb. Nothing to do."); + if (*dum->ptr_head == *dum->ptr_tail) { + LOG_DBGF("Attempt to free empty ringbuffer. Nothing to do."); pthread_mutex_unlock(dum->shm_mutex); - return; + return -1; } - /* release all blocks in the structure */ - dum->ptr_map[index] = 0; - - index = idx_to_block_ptr(dum, index)->next; - while (index != -1) { - dum->ptr_map[index] = 0; - index = idx_to_block_ptr(dum, index)->next; + while (idx_to_block_ptr(dum, *dum->ptr_tail)->next != -1) { + *(dum->ptr_tail) = (*dum->ptr_tail + 1) % SHM_BLOCKS_IN_MAP; + released++; } + *(dum->ptr_tail) = (*dum->ptr_tail + 1) % SHM_BLOCKS_IN_MAP; + pthread_mutex_unlock(dum->shm_mutex); + return 0; } uint8_t * shm_du_buff_head_alloc(struct shm_du_map * dum, diff --git a/src/lib/tests/shm_du_map_test_du_buff_create.c b/src/lib/tests/shm_du_map_test_du_buff_create.c index 9f66b20c..6787a1bc 100644 --- a/src/lib/tests/shm_du_map_test_du_buff_create.c +++ b/src/lib/tests/shm_du_map_test_du_buff_create.c @@ -54,7 +54,9 @@ int shm_du_map_test_du_buff_create(int argc, char ** argv) j_inc = MAX(1, SHM_DU_BLOCK_DATA_SIZE / 8); k_inc = MAX(1, SHM_DU_BLOCK_DATA_SIZE / 16); - for (i = SHM_DU_BUFF_BLOCK_SIZE / 4; i <= TEST_BUFF_SIZE; i += i_inc) { + for (i = SHM_DU_BLOCK_DATA_SIZE / 4; + i <= TEST_BUFF_SIZE; + i += i_inc) { for (j = 0; j < i; j += j_inc) { for (k = 0; k < i - j; k += k_inc) { if (k > SHM_DU_BLOCK_DATA_SIZE) @@ -73,7 +75,7 @@ int shm_du_map_test_du_buff_create(int argc, char ** argv) shm_du_map_close(dum); return -1; } - shm_release_du_buff(dum, dub); + shm_release_du_buff(dum); } } } diff --git a/src/lib/tests/shm_du_map_test_prod_cons.c b/src/lib/tests/shm_du_map_test_prod_cons.c index 945104c1..ff9dfb0a 100644 --- a/src/lib/tests/shm_du_map_test_prod_cons.c +++ b/src/lib/tests/shm_du_map_test_prod_cons.c @@ -27,12 +27,12 @@ #include #include "shm_du_map.c" -#define TEST_BUFF_SIZE (3 * SHM_DU_BLOCK_DATA_SIZE) +#define TEST_BUFF_SIZE (SHM_DU_BLOCK_DATA_SIZE) #define MAX(a,b) (a > b ? a : b) #define MIN(a,b) (a < b ? a : b) -int * sync; +int sync; void * produce() { @@ -46,31 +46,26 @@ void * produce() struct timespec starttime; struct timespec stoptime; double elapsed; - long overruns = 0; dum = shm_du_map_open(); if (dum == NULL) return (void *)-1; srand(time(NULL)); - clock_gettime(CLOCK_MONOTONIC, &starttime); test_values = malloc (sizeof *test_values * TEST_BUFF_SIZE); for (i = 0; i < TEST_BUFF_SIZE; i++) test_values[i] = 170; - for (i = 0; i < 4 * SHM_BLOCKS_IN_MAP; i++) { + clock_gettime(CLOCK_MONOTONIC, &starttime); + for (i = 0; i < SHM_BLOCKS_IN_MAP; i++) { struct shm_du_buff * sdb; size_t len; - struct timespec ts; - - test_buf_size = rand() % (TEST_BUFF_SIZE - 512) + 512; - headspace = MAX(4, rand() % 64); - tailspace = MAX(1, rand() % 24); + test_buf_size = TEST_BUFF_SIZE; - ts.tv_sec = 0; - ts.tv_nsec = rand() % 90000; + headspace = 32; + tailspace = 8; len = test_buf_size - (headspace + tailspace); @@ -81,23 +76,12 @@ void * produce() len); if (sdb != NULL) { - sync[i] = du_buff_ptr_to_idx(dum, sdb); bytes_written += len; } else { - i--; - ++overruns; - ts.tv_nsec = 10000; - nanosleep(&ts, NULL); - } - nanosleep(&ts, NULL); - - if (overruns > 100) { - LOG_INFO("Bugging out due to overruns."); - sync[i+1] = -2; + sync = -2; break; } - } clock_gettime(CLOCK_MONOTONIC, &stoptime); @@ -110,6 +94,9 @@ void * produce() free(test_values); shm_du_map_close(dum); + + sync = -1; + return 0; } @@ -117,24 +104,19 @@ void * consume() { struct shm_du_map * dum; - long i; - struct timespec ts; ts.tv_sec = 0; - ts.tv_nsec = 5000; + ts.tv_nsec = 1000; dum = shm_du_map_open(); if (dum == NULL) pthread_exit((void *) -1); - for (i = 0; i < 4 * SHM_BLOCKS_IN_MAP; i++) { - while (sync[i] == -1) - nanosleep(&ts, NULL); /* wait for the producer */ - if (sync[i] == -2) - break; - shm_release_du_buff(dum, idx_to_du_buff_ptr(dum, sync[i])); + while (!sync) { + while (!shm_release_du_buff(dum)); + nanosleep(&ts, NULL); } shm_du_map_close(dum); @@ -148,11 +130,8 @@ int shm_du_map_test_prod_cons(int argc, char ** argv) int res1; - int i; - pthread_t producer; pthread_t consumer; - shm_unlink(SHM_DU_MAP_FILENAME); dum = shm_du_map_create(); @@ -160,10 +139,7 @@ int shm_du_map_test_prod_cons(int argc, char ** argv) if (dum == NULL) return -1; - sync = malloc(sizeof *sync * 4 * SHM_BLOCKS_IN_MAP); - - for (i = 0; i < 4 * SHM_BLOCKS_IN_MAP; i++) - sync[i] = -1; + sync = 0; res1 = (int) pthread_create(&producer, NULL, produce, NULL); pthread_create(&consumer, NULL, consume, NULL); @@ -171,8 +147,6 @@ int shm_du_map_test_prod_cons(int argc, char ** argv) pthread_join(producer, NULL); pthread_join(consumer, NULL); - free(sync); - shm_du_map_close(dum); return res1; -- cgit v1.2.3 From b3009103bd2b86b2ee0df2a71300bb976442c6a6 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 20 Mar 2016 10:35:01 +0100 Subject: lib: optimization of ringbuffer Got rid of divisions and modulo operations. Change in the meaning of a #define. SHM_DU_MAP_SIZE: is now an exponent of 2. Default is 10, allowing for 1024 blocks in the map. --- include/ouroboros/shm_du_map.h | 2 +- src/lib/shm_du_map.c | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) (limited to 'include') diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index bfccf60a..b3533fa3 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -39,7 +39,7 @@ #endif #ifndef SHM_DU_MAP_SIZE -#define SHM_DU_MAP_SIZE (1 << 26) +#define SHM_DU_MAP_SIZE 10 #endif #include "common.h" diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 630cfa42..d071b0c3 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -35,7 +35,7 @@ #define SHM_DU_BLOCK_DATA_SIZE (SHM_DU_BUFF_BLOCK_SIZE - \ sizeof(struct shm_block)) -#define SHM_BLOCKS_IN_MAP (SHM_DU_MAP_SIZE / SHM_DU_BUFF_BLOCK_SIZE) +#define SHM_BLOCKS_IN_MAP (1 << SHM_DU_MAP_SIZE) #define SHM_BLOCKS_SIZE (SHM_DU_BUFF_BLOCK_SIZE * SHM_BLOCKS_IN_MAP) #define SHM_BUFFS_SIZE (SHM_BLOCKS_IN_MAP * sizeof (struct shm_du_buff)) #define SHM_FILE_SIZE (SHM_BLOCKS_IN_MAP * (SHM_DU_BUFF_BLOCK_SIZE \ @@ -52,9 +52,9 @@ / SHM_DU_BUFF_BLOCK_SIZE) #define shm_map_used(dum) ((*(dum->ptr_head) + SHM_BLOCKS_IN_MAP - \ - *(dum->ptr_tail)) % SHM_BLOCKS_IN_MAP) + *(dum->ptr_tail)) & (SHM_BLOCKS_IN_MAP - 1)) -#define shm_map_free(dum, i)(shm_map_used(dum) + i < SHM_BLOCKS_IN_MAP) +#define shm_map_free(dum, i)(shm_map_used(dum) + i + 1 < SHM_BLOCKS_IN_MAP) struct shm_block { size_t size; @@ -118,8 +118,6 @@ struct shm_du_map * shm_du_map_create() if (shm_base == MAP_FAILED) { LOG_ERR("Failed to map shared memory."); - if (close(shm_fd) == -1) - LOG_ERR("Failed to close invalid shm."); if (shm_unlink(SHM_DU_MAP_FILENAME) == -1) LOG_ERR("Failed to remove invalid shm."); @@ -173,9 +171,6 @@ struct shm_du_map * shm_du_map_open() if (shm_base == MAP_FAILED) { LOG_ERR("Failed to map shared memory."); - if (close(shm_fd) == -1) - LOG_ERR("Failed to close invalid shm."); - if (shm_unlink(SHM_DU_MAP_FILENAME) == -1) LOG_ERR("Failed to unlink invalid shm."); @@ -228,7 +223,8 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t remaining = size; size_t ts = size - (headspace + len); uint8_t * read_pos = data; - size_t blocks; + size_t blocks = 0; + int sz = size; if (dum == NULL || data == NULL) { LOG_DBGF("Bogus input, bugging out."); @@ -252,9 +248,10 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, pthread_mutex_lock(dum->shm_mutex); - blocks = size / SHM_DU_BLOCK_DATA_SIZE; - if (size % SHM_DU_BLOCK_DATA_SIZE > 0) - ++blocks; + while (sz > 0) { + sz -= SHM_DU_BLOCK_DATA_SIZE; + blocks++; + } if (!shm_map_free(dum, blocks)) { pthread_mutex_unlock(dum->shm_mutex); @@ -316,7 +313,8 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, prev_index = *dum->ptr_head; - *(dum->ptr_head) = (*dum->ptr_head + 1) % SHM_BLOCKS_IN_MAP; + *(dum->ptr_head) = (*dum->ptr_head + 1) + & (SHM_BLOCKS_IN_MAP - 1); } pthread_mutex_unlock(dum->shm_mutex); @@ -337,11 +335,12 @@ int shm_release_du_buff(struct shm_du_map * dum) } while (idx_to_block_ptr(dum, *dum->ptr_tail)->next != -1) { - *(dum->ptr_tail) = (*dum->ptr_tail + 1) % SHM_BLOCKS_IN_MAP; + *(dum->ptr_tail) = (*dum->ptr_tail + 1) + & (SHM_BLOCKS_IN_MAP -1); released++; } - *(dum->ptr_tail) = (*dum->ptr_tail + 1) % SHM_BLOCKS_IN_MAP; + *(dum->ptr_tail) = (*dum->ptr_tail + 1) & (SHM_BLOCKS_IN_MAP - 1); pthread_mutex_unlock(dum->shm_mutex); -- cgit v1.2.3 From 0dd4526f5c75f23aba886bee2d9850a70aa67ca8 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Thu, 24 Mar 2016 20:51:55 +0100 Subject: lib: Shared Memory Updated tests. Added code to drop a corner case (packet where the tail PCI would cross the edge of the ring buffer) because solving this very rare case is not worth the performance hit on the ringbuffer the extra code would incur. This means the ringbuffer might drop a very small percentage of packets. --- include/ouroboros/shm_du_map.h | 11 +- src/lib/CMakeLists.txt | 12 ++ src/lib/shm_du_map.c | 202 +++++++++---------------- src/lib/tests/CMakeLists.txt | 16 +- src/lib/tests/shm_du_map_test.c | 184 ++++++++++++++++++++++ src/lib/tests/shm_du_map_test_create.c | 51 ------- src/lib/tests/shm_du_map_test_du_buff_create.c | 95 ------------ src/lib/tests/shm_du_map_test_prod_cons.c | 153 ------------------- 8 files changed, 270 insertions(+), 454 deletions(-) create mode 100644 src/lib/tests/shm_du_map_test.c delete mode 100644 src/lib/tests/shm_du_map_test_create.c delete mode 100644 src/lib/tests/shm_du_map_test_du_buff_create.c delete mode 100644 src/lib/tests/shm_du_map_test_prod_cons.c (limited to 'include') diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index b3533fa3..05c49c2d 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -38,15 +38,14 @@ #define SHM_DU_MAP_FILENAME "ouroboros_du_map" #endif -#ifndef SHM_DU_MAP_SIZE -#define SHM_DU_MAP_SIZE 10 +#ifndef SHM_BLOCKS_IN_MAP +#define SHM_BLOCKS_IN_MAP (1 << 12) #endif #include "common.h" #include "logs.h" struct shm_du_buff; - struct shm_du_map; struct shm_du_map * shm_du_map_create(); @@ -60,11 +59,9 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t len); int shm_release_du_buff(struct shm_du_map * dum); -uint8_t * shm_du_buff_head_alloc(struct shm_du_map * dum, - struct shm_du_buff * sdb, +uint8_t * shm_du_buff_head_alloc(struct shm_du_buff * sdb, size_t size); -uint8_t * shm_du_buff_tail_alloc(struct shm_du_map * dum, - struct shm_du_buff * sdb, +uint8_t * shm_du_buff_tail_alloc(struct shm_du_buff * sdb, size_t size); int shm_du_buff_head_release(struct shm_du_buff * sdb, size_t size); diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index e81f3475..7ce98bf2 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -4,6 +4,16 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_SOURCE_DIR}/include) include_directories(${CMAKE_BINARY_DIR}/include) +find_library(LIBRT_LIBRARIES rt) +if(NOT LIBRT_LIBRARIES) + message(FATAL_ERROR "librt not found") +endif() + +find_library(LIBPTHREAD_LIBRARIES pthread) +if(NOT LIBPTHREAD_LIBRARIES) + message(FATAL_ERROR "libpthread not found") +endif() + set(SOURCE_FILES # Add source files here bitmap.c @@ -15,11 +25,13 @@ set(SOURCE_FILES irm.c list.c rina_name.c + shm_du_map.c sockets.c utils.c ) add_library(ouroboros SHARED ${SOURCE_FILES}) +target_link_libraries(ouroboros rt pthread) include(MacroAddCompileFlags) if (CMAKE_BUILD_TYPE MATCHES Debug) diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index d071b0c3..dc73077f 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -21,46 +21,33 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#ifndef SHM_DU_MAP_C -#define SHM_DU_MAP_C - #include - #include #include #include #include #include - -#define SHM_DU_BLOCK_DATA_SIZE (SHM_DU_BUFF_BLOCK_SIZE - \ - sizeof(struct shm_block)) -#define SHM_BLOCKS_IN_MAP (1 << SHM_DU_MAP_SIZE) -#define SHM_BLOCKS_SIZE (SHM_DU_BUFF_BLOCK_SIZE * SHM_BLOCKS_IN_MAP) -#define SHM_BUFFS_SIZE (SHM_BLOCKS_IN_MAP * sizeof (struct shm_du_buff)) -#define SHM_FILE_SIZE (SHM_BLOCKS_IN_MAP * (SHM_DU_BUFF_BLOCK_SIZE \ - + sizeof(struct shm_du_buff) \ - + sizeof(uint8_t)) \ - + 2 * sizeof (size_t) \ +#define SHM_BLOCKS_SIZE (SHM_BLOCKS_IN_MAP * SHM_DU_BUFF_BLOCK_SIZE) +#define SHM_FILE_SIZE (SHM_BLOCKS_SIZE + 2 * sizeof (size_t) \ + sizeof(pthread_mutex_t)) -#define idx_to_block_ptr(dum, i) ((struct shm_block *) \ - (dum->shm_base + i * SHM_DU_BUFF_BLOCK_SIZE)) -#define idx_to_du_buff_ptr(dum, i) (dum->ptr_du_buff + i) -#define du_buff_ptr_to_idx(dum, sdb) ((sdb - dum->ptr_du_buff) / sizeof *sdb) -#define block_ptr_to_idx(dum, sdb) (((uint8_t *)sdb - dum->shm_base) \ - / SHM_DU_BUFF_BLOCK_SIZE) +#define get_head_ptr(dum) \ +((struct shm_du_buff *)(dum->shm_base + (*dum->ptr_head * \ + SHM_DU_BUFF_BLOCK_SIZE))) -#define shm_map_used(dum) ((*(dum->ptr_head) + SHM_BLOCKS_IN_MAP - \ - *(dum->ptr_tail)) & (SHM_BLOCKS_IN_MAP - 1)) +#define get_tail_ptr(dum) \ +((struct shm_du_buff *)(dum->shm_base + (*dum->ptr_tail * \ + SHM_DU_BUFF_BLOCK_SIZE))) -#define shm_map_free(dum, i)(shm_map_used(dum) + i + 1 < SHM_BLOCKS_IN_MAP) +#define block_ptr_to_idx(dum, sdb) \ + (((uint8_t *)sdb - dum->shm_base) / SHM_DU_BUFF_BLOCK_SIZE) -struct shm_block { - size_t size; - long next; - long prev; -}; +#define shm_map_used(dum)((*dum->ptr_head + SHM_BLOCKS_IN_MAP - *dum->ptr_tail)\ + & (SHM_BLOCKS_IN_MAP - 1)) +#define shm_map_free(dum, i)(shm_map_used(dum) + i < SHM_BLOCKS_IN_MAP) + +#define MIN(a,b)(a < b ? a : b) struct shm_du_buff { size_t size; @@ -70,7 +57,6 @@ struct shm_du_buff { struct shm_du_map { uint8_t * shm_base; /* start of blocks */ - struct shm_du_buff * ptr_du_buff; /* start of du_buff structs */ size_t * ptr_head; /* start of ringbuffer head */ size_t * ptr_tail; /* start of ringbuffer tail */ pthread_mutex_t * shm_mutex; /* lock all free space in shm */ @@ -86,25 +72,25 @@ struct shm_du_map * shm_du_map_create() dum = malloc(sizeof *dum); if (dum == NULL) { - LOG_ERR("Could not allocate struct."); + LOG_DBGF("Could not allocate struct."); return NULL; } shm_fd = shm_open(SHM_DU_MAP_FILENAME, O_CREAT | O_EXCL | O_RDWR, 0666); if (shm_fd == -1) { - LOG_ERR("Failed creating shared memory map."); + LOG_DBGF("Failed creating shared memory map."); free(dum); return NULL; } - if (lseek (shm_fd,SHM_FILE_SIZE - 1, SEEK_SET) < 0) { - LOG_ERR("Failed to extend shared memory map."); + if (lseek(shm_fd, SHM_FILE_SIZE - 1, SEEK_SET) < 0) { + LOG_DBGF("Failed to extend shared memory map."); free(dum); return NULL; } - if (write (shm_fd, "", 1) != 1) { - LOG_ERR("Failed to finalise extension of shared memory map."); + if (write(shm_fd, "", 1) != 1) { + LOG_DBGF("Failed to finalise extension of shared memory map."); free(dum); return NULL; } @@ -117,22 +103,18 @@ struct shm_du_map * shm_du_map_create() 0); if (shm_base == MAP_FAILED) { - LOG_ERR("Failed to map shared memory."); + LOG_DBGF("Failed to map shared memory."); if (shm_unlink(SHM_DU_MAP_FILENAME) == -1) - LOG_ERR("Failed to remove invalid shm."); + LOG_DBGF("Failed to remove invalid shm."); free(dum); return NULL; } -#ifdef CONFIG_OUROBOROS_DEBUG - memset(shm_base, 0, SHM_FILE_SIZE); -#endif + dum->shm_base = shm_base; - dum->ptr_du_buff = (struct shm_du_buff *) - ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); dum->ptr_head = (size_t *) - ((uint8_t *) dum->ptr_du_buff + SHM_BUFFS_SIZE); + ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); dum->ptr_tail = (size_t *) ((uint8_t *) dum->ptr_head + sizeof(size_t)); dum->shm_mutex = (pthread_mutex_t *) @@ -158,7 +140,7 @@ struct shm_du_map * shm_du_map_open() shm_fd = shm_open(SHM_DU_MAP_FILENAME, O_RDWR, 0666); if (shm_fd == -1) { - LOG_ERR("Failed opening shared memory for du_buff."); + LOG_DBGF("Failed opening shared memory."); return NULL; } @@ -169,30 +151,32 @@ struct shm_du_map * shm_du_map_open() shm_fd, 0); if (shm_base == MAP_FAILED) { - LOG_ERR("Failed to map shared memory."); + LOG_DBGF("Failed to map shared memory."); + if (close(shm_fd) == -1) + LOG_DBGF("Failed to close invalid shm."); if (shm_unlink(SHM_DU_MAP_FILENAME) == -1) - LOG_ERR("Failed to unlink invalid shm."); + LOG_DBGF("Failed to unlink invalid shm."); return NULL; } dum = malloc(sizeof *dum); if (dum == NULL) { - LOG_ERR("Could not allocate struct."); + LOG_DBGF("Could not allocate struct."); return NULL; } dum->shm_base = shm_base; - dum->ptr_du_buff = (struct shm_du_buff *) - ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); dum->ptr_head = (size_t *) - ((uint8_t *) dum->ptr_du_buff + SHM_BUFFS_SIZE); + ((uint8_t *) dum->shm_base + SHM_BLOCKS_SIZE); dum->ptr_tail = (size_t *) ((uint8_t *) dum->ptr_head + sizeof(size_t)); dum->shm_mutex = (pthread_mutex_t *) ((uint8_t *) dum->ptr_tail + sizeof(size_t)); + dum->fd = shm_fd; + return dum; } @@ -219,12 +203,11 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, size_t len) { struct shm_du_buff * sdb; - long prev_index = -1; - size_t remaining = size; - size_t ts = size - (headspace + len); - uint8_t * read_pos = data; - size_t blocks = 0; - int sz = size; + long blocks = 0; + int sz = size + sizeof *sdb; + int sz2 = headspace + len + sizeof *sdb; + uint8_t * write_pos; + size_t copy_len; if (dum == NULL || data == NULL) { LOG_DBGF("Bogus input, bugging out."); @@ -241,16 +224,17 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, return NULL; } - if (headspace > SHM_DU_BLOCK_DATA_SIZE || ts > SHM_DU_BLOCK_DATA_SIZE) { - LOG_ERR("Illegal du_buff: Cannot fit PCI in DU_BUFF_BLOCK."); - return NULL; - } - pthread_mutex_lock(dum->shm_mutex); while (sz > 0) { - sz -= SHM_DU_BLOCK_DATA_SIZE; - blocks++; + sz -= SHM_DU_BUFF_BLOCK_SIZE; + sz2 -= SHM_DU_BUFF_BLOCK_SIZE; + if (sz2 < 0 && sz > 0) { + pthread_mutex_unlock(dum->shm_mutex); + LOG_DBG("Can't handle this packet now"); + return NULL; + } + ++blocks; } if (!shm_map_free(dum, blocks)) { @@ -259,62 +243,23 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, return NULL; } - sdb = dum->ptr_du_buff + *dum->ptr_head; + sdb = get_head_ptr(dum); sdb->size = size; sdb->du_head = headspace; sdb->du_tail = sdb->du_head + len; - while (remaining > 0) { - struct shm_block * shm_buf; - long bytes_to_copy = len; - uint8_t * write_pos; - - shm_buf = idx_to_block_ptr(dum, *(dum->ptr_head)); - - write_pos = (uint8_t *) shm_buf + sizeof *shm_buf; - - shm_buf->size = remaining < SHM_DU_BLOCK_DATA_SIZE ? - remaining : SHM_DU_BLOCK_DATA_SIZE; - - bytes_to_copy = shm_buf->size; - - if (remaining <= SHM_DU_BLOCK_DATA_SIZE) - bytes_to_copy -= ts; - else if (remaining - ts <= SHM_DU_BLOCK_DATA_SIZE) - shm_buf->size = remaining - ts; - - remaining -= shm_buf->size; - - if (prev_index == -1) { -#ifdef CONFIG_OUROBOROS_DEBUG - memset(write_pos, 0, sdb->du_head); -#endif - write_pos += sdb->du_head; - bytes_to_copy -= sdb->du_head; - } - - if (prev_index != -1) - idx_to_block_ptr(dum, prev_index)->next = - *(dum->ptr_head); - - if (len > 0) { - memcpy(write_pos, read_pos, bytes_to_copy); - } - read_pos += bytes_to_copy; -#ifdef CONFIG_OUROBOROS_DEBUG - if (remaining == 0) { - write_pos + = bytes_to_copy; - memset(write_pos, 0, ts); - } -#endif - shm_buf->next = -1; - shm_buf->prev = prev_index; - - prev_index = *dum->ptr_head; + copy_len = MIN(len, SHM_DU_BUFF_BLOCK_SIZE - headspace - sizeof *sdb); + write_pos = ((uint8_t *) sdb) + sizeof *sdb + headspace; + while (blocks > 0) { + memcpy(write_pos, data, copy_len); *(dum->ptr_head) = (*dum->ptr_head + 1) & (SHM_BLOCKS_IN_MAP - 1); + len -= copy_len; + copy_len = MIN(len, SHM_DU_BUFF_BLOCK_SIZE); + write_pos = (uint8_t *) get_head_ptr(dum); + --blocks; } pthread_mutex_unlock(dum->shm_mutex); @@ -324,7 +269,8 @@ struct shm_du_buff * shm_create_du_buff(struct shm_du_map * dum, int shm_release_du_buff(struct shm_du_map * dum) { - int released = 0; + long sz; + long blocks = 0; pthread_mutex_lock(dum->shm_mutex); @@ -334,25 +280,22 @@ int shm_release_du_buff(struct shm_du_map * dum) return -1; } - while (idx_to_block_ptr(dum, *dum->ptr_tail)->next != -1) { - *(dum->ptr_tail) = (*dum->ptr_tail + 1) - & (SHM_BLOCKS_IN_MAP -1); - released++; - } + sz = get_tail_ptr(dum)->size; - *(dum->ptr_tail) = (*dum->ptr_tail + 1) & (SHM_BLOCKS_IN_MAP - 1); + while (sz + (long) sizeof (struct shm_du_buff) > 0) { + sz -= SHM_DU_BUFF_BLOCK_SIZE; + ++blocks; + } + *(dum->ptr_tail) = (*dum->ptr_tail + blocks) & (SHM_BLOCKS_IN_MAP - 1); pthread_mutex_unlock(dum->shm_mutex); return 0; } -uint8_t * shm_du_buff_head_alloc(struct shm_du_map * dum, - struct shm_du_buff * sdb, +uint8_t * shm_du_buff_head_alloc(struct shm_du_buff * sdb, size_t size) { - uint8_t * ret; - if (sdb == NULL) { LOG_DBGF("Bogus input, bugging out."); return NULL; @@ -365,17 +308,12 @@ uint8_t * shm_du_buff_head_alloc(struct shm_du_map * dum, sdb->du_head -= size; - ret = (uint8_t *) idx_to_block_ptr(dum, du_buff_ptr_to_idx(dum,sdb)); - - return ret + sizeof(struct shm_block) + sdb->du_head; + return (uint8_t *) sdb + sizeof *sdb + sdb->du_head; } -uint8_t * shm_du_buff_tail_alloc(struct shm_du_map * dum, - struct shm_du_buff * sdb, +uint8_t * shm_du_buff_tail_alloc(struct shm_du_buff * sdb, size_t size) { - uint8_t * ret; - if (sdb == NULL) { LOG_DBGF("Bogus input, bugging out."); return NULL; @@ -388,9 +326,7 @@ uint8_t * shm_du_buff_tail_alloc(struct shm_du_map * dum, sdb->du_tail += size; - ret = (uint8_t *) idx_to_block_ptr(dum, du_buff_ptr_to_idx(dum,sdb)); - - return ret + sizeof(struct shm_block) + sdb->du_tail; + return (uint8_t *) sdb + sizeof *sdb + sdb->du_tail; } int shm_du_buff_head_release(struct shm_du_buff * sdb, @@ -428,5 +364,3 @@ int shm_du_buff_tail_release(struct shm_du_buff * sdb, return sdb->du_tail; } - -#endif diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index d4bcb4e4..80834df8 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -1,27 +1,15 @@ get_filename_component(tmp ".." ABSOLUTE) get_filename_component(src_folder "${tmp}" NAME) -find_library(LIBRT_LIBRARIES rt) -if(NOT LIBRT_LIBRARIES) - message(FATAL_ERROR "librt not found") -endif() - -find_library(LIBPTHREAD_LIBRARIES pthread) -if(NOT LIBPTHREAD_LIBRARIES) - message(FATAL_ERROR "libpthread not found") -endif() - create_test_sourcelist(${src_folder}_tests test_suite.c # Add new tests here bitmap_test.c du_buff_test.c - shm_du_map_test_create.c - shm_du_map_test_du_buff_create.c - shm_du_map_test_prod_cons.c + shm_du_map_test.c ) add_executable(${src_folder}_test EXCLUDE_FROM_ALL ${${src_folder}_tests}) -target_link_libraries(${src_folder}_test ouroboros rt pthread) +target_link_libraries(${src_folder}_test ouroboros) add_dependencies(check ${src_folder}_test) diff --git a/src/lib/tests/shm_du_map_test.c b/src/lib/tests/shm_du_map_test.c new file mode 100644 index 00000000..d9b44732 --- /dev/null +++ b/src/lib/tests/shm_du_map_test.c @@ -0,0 +1,184 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Test of the Shared Memory Map + * + * Dimitri Staessens + * + * 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 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include + +#define SIZE_OF_DU_BUFF 24 +#define TEST_BUFF_SIZE (SHM_DU_BUFF_BLOCK_SIZE - SIZE_OF_DU_BUFF) + +#define MAX(a,b) (a > b ? a : b) +#define MIN(a,b) (a < b ? a : b) + +int sync; + +void * produce() +{ + struct shm_du_map * dum; + long test_buf_size = 0; + uint8_t * test_values; + int headspace; + int tailspace; + long i; + long bytes_written = 0; + struct timespec starttime; + struct timespec stoptime; + double elapsed; + + dum = shm_du_map_open(); + if (dum == NULL) { + LOG_ERR("Could not open shm."); + return (void *)-1; + } + + srand(time(NULL)); + + test_values = malloc (sizeof *test_values * TEST_BUFF_SIZE); + for (i = 0; i < TEST_BUFF_SIZE; i++) + test_values[i] = 170; + + clock_gettime(CLOCK_MONOTONIC, &starttime); + for (i = 1; i < SHM_BLOCKS_IN_MAP; i++) { + struct shm_du_buff * sdb; + size_t len; + + test_buf_size = TEST_BUFF_SIZE; + + headspace = 32; + tailspace = 8; + + len = test_buf_size - (headspace + tailspace); + + sdb = shm_create_du_buff(dum, + test_buf_size, + headspace, + test_values, + len); + + if (sdb != NULL) { + bytes_written += len; + } + else { + sync = -2; + break; + } + } + + clock_gettime(CLOCK_MONOTONIC, &stoptime); + elapsed =(stoptime.tv_sec + stoptime.tv_nsec / 1000000000.0) - + (starttime.tv_sec + starttime.tv_nsec / 1000000000.0); + LOG_INFO("%ld bytes written in %.1lf ms = %lf Gb/s", + bytes_written, + elapsed * 1000.0, + bytes_written * 8 / (elapsed * 1000000000)); + + free(test_values); + + sync = -1; + + return 0; +} + +void * consume() +{ + struct shm_du_map * dum; + + struct timespec ts; + + ts.tv_sec = 0; + ts.tv_nsec = 1000000; + + dum = shm_du_map_open(); + + if (dum == NULL) { + LOG_ERR("Could not open shm."); + return (void *)-1; + } + + while (!sync) { + while (!shm_release_du_buff(dum)); + nanosleep(&ts, NULL); + } + + return 0; +} + +int shm_du_map_test(int argc, char ** argv) +{ + struct shm_du_map * dum; + int res1; + pthread_t producer; + pthread_t consumer; + + /* test 1 */ + + LOG_INFO("starting create/close test."); + + dum = shm_du_map_create(); + + if (dum == NULL) { + LOG_ERR("Could not open shm (dum)."); + return -1; + } + + shm_du_map_close(dum); + + LOG_INFO("done."); + + /* test 2 */ + + LOG_INFO("starting sequential test."); + + dum = shm_du_map_create(); + + res1 = (int) pthread_create(&producer, NULL, produce, NULL); + pthread_join(producer, NULL); + + pthread_create(&consumer, NULL, consume, NULL); + pthread_join(consumer, NULL); + + shm_du_map_close(dum); + + LOG_INFO("done."); + + /* test 3 */ + + LOG_INFO("starting concurrency test."); + + dum = shm_du_map_create(); + + res1 = (int) pthread_create(&producer, NULL, produce, NULL); + pthread_create(&consumer, NULL, consume, NULL); + + pthread_join(producer, NULL); + pthread_join(consumer, NULL); + + shm_du_map_close(dum); + + LOG_INFO("done."); + + return res1; +} diff --git a/src/lib/tests/shm_du_map_test_create.c b/src/lib/tests/shm_du_map_test_create.c deleted file mode 100644 index dcf3bd45..00000000 --- a/src/lib/tests/shm_du_map_test_create.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Test of the Shared Memory Map - * - * Dimitri Staessens - * - * 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 2 of the License, or - * (at your option) any later version. - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include -#include -#include - -int shm_du_map_test_create(int argc, char ** argv) -{ - struct shm_du_map * dum; - struct shm_du_map * dum2; - - shm_unlink(SHM_DU_MAP_FILENAME); - - dum = shm_du_map_create(); - - if (dum == NULL) - return -1; - - dum2 = shm_du_map_open(); - - if (dum2 == NULL) { - shm_du_map_close(dum); - return 1; - } - - shm_du_map_close(dum2); - - shm_du_map_close(dum); - - return 0; /* tests succeeded */ -} diff --git a/src/lib/tests/shm_du_map_test_du_buff_create.c b/src/lib/tests/shm_du_map_test_du_buff_create.c deleted file mode 100644 index 6787a1bc..00000000 --- a/src/lib/tests/shm_du_map_test_du_buff_create.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Test of the Shared Memory Map - * - * Dimitri Staessens - * - * 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 2 of the License, or - * (at your option) any later version. - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include -#include - - -#define SHM_DU_BLOCK_DATA_SIZE (SHM_DU_BUFF_BLOCK_SIZE - 3 * sizeof(long)) -#define TEST_BUFF_SIZE (16 * SHM_DU_BLOCK_DATA_SIZE) - -#define MAX(a,b) (a > b ? a : b) -#define MIN(a,b) (a < b ? a : b) - -int shm_du_map_test_du_buff_create(int argc, char ** argv) -{ - struct shm_du_map * dum; - struct shm_du_map * dum2; - - int i, j, k; - int i_inc, j_inc, k_inc; - - uint8_t bits[TEST_BUFF_SIZE]; - - shm_unlink(SHM_DU_MAP_FILENAME); - - dum = shm_du_map_create(); - - if (dum == NULL) - return -1; - - for (i = 0; i < TEST_BUFF_SIZE; i++) - bits[i] = 0; - - i_inc = MAX(1, SHM_DU_BLOCK_DATA_SIZE / 4); - j_inc = MAX(1, SHM_DU_BLOCK_DATA_SIZE / 8); - k_inc = MAX(1, SHM_DU_BLOCK_DATA_SIZE / 16); - - for (i = SHM_DU_BLOCK_DATA_SIZE / 4; - i <= TEST_BUFF_SIZE; - i += i_inc) { - for (j = 0; j < i; j += j_inc) { - for (k = 0; k < i - j; k += k_inc) { - if (k > SHM_DU_BLOCK_DATA_SIZE) - continue; - - if (i - (j + k) > SHM_DU_BLOCK_DATA_SIZE) - continue; - - struct shm_du_buff * dub = shm_create_du_buff( - dum, - i, - k, - bits, - j); - if (dub == NULL) { - shm_du_map_close(dum); - return -1; - } - shm_release_du_buff(dum); - } - } - } - - dum2 = shm_du_map_open(); - - if (dum2 == NULL) { - shm_du_map_close(dum); - return 1; - } - - shm_du_map_close(dum2); - - shm_du_map_close(dum); - - return 0; /* tests succeeded */ -} diff --git a/src/lib/tests/shm_du_map_test_prod_cons.c b/src/lib/tests/shm_du_map_test_prod_cons.c deleted file mode 100644 index ff9dfb0a..00000000 --- a/src/lib/tests/shm_du_map_test_prod_cons.c +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Test of the Shared Memory Map - * - * Dimitri Staessens - * - * 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 2 of the License, or - * (at your option) any later version. - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include -#include -#include -#include -#include -#include "shm_du_map.c" - -#define TEST_BUFF_SIZE (SHM_DU_BLOCK_DATA_SIZE) - -#define MAX(a,b) (a > b ? a : b) -#define MIN(a,b) (a < b ? a : b) - -int sync; - -void * produce() -{ - struct shm_du_map * dum; - long test_buf_size = 0; - uint8_t * test_values; - int headspace; - int tailspace; - long i; - long bytes_written = 0; - struct timespec starttime; - struct timespec stoptime; - double elapsed; - - dum = shm_du_map_open(); - if (dum == NULL) - return (void *)-1; - - srand(time(NULL)); - - test_values = malloc (sizeof *test_values * TEST_BUFF_SIZE); - for (i = 0; i < TEST_BUFF_SIZE; i++) - test_values[i] = 170; - - clock_gettime(CLOCK_MONOTONIC, &starttime); - for (i = 0; i < SHM_BLOCKS_IN_MAP; i++) { - struct shm_du_buff * sdb; - size_t len; - - test_buf_size = TEST_BUFF_SIZE; - - headspace = 32; - tailspace = 8; - - len = test_buf_size - (headspace + tailspace); - - sdb = shm_create_du_buff(dum, - test_buf_size, - headspace, - test_values, - len); - - if (sdb != NULL) { - bytes_written += len; - } - else { - sync = -2; - break; - } - } - - clock_gettime(CLOCK_MONOTONIC, &stoptime); - elapsed =(stoptime.tv_sec + stoptime.tv_nsec / 1000000000.0) - - (starttime.tv_sec + starttime.tv_nsec / 1000000000.0); - LOG_INFO("%ld bytes written in %.1lf ms = %lf Gb/s", - bytes_written, - elapsed * 1000.0, - bytes_written * 8 / (elapsed * 1000000000)); - - free(test_values); - shm_du_map_close(dum); - - sync = -1; - - return 0; -} - -void * consume() -{ - struct shm_du_map * dum; - - struct timespec ts; - - ts.tv_sec = 0; - ts.tv_nsec = 1000; - - dum = shm_du_map_open(); - - if (dum == NULL) - pthread_exit((void *) -1); - - while (!sync) { - while (!shm_release_du_buff(dum)); - nanosleep(&ts, NULL); - } - - shm_du_map_close(dum); - - return 0; -} - -int shm_du_map_test_prod_cons(int argc, char ** argv) -{ - struct shm_du_map * dum; - - int res1; - - pthread_t producer; - pthread_t consumer; - shm_unlink(SHM_DU_MAP_FILENAME); - - dum = shm_du_map_create(); - - if (dum == NULL) - return -1; - - sync = 0; - - res1 = (int) pthread_create(&producer, NULL, produce, NULL); - pthread_create(&consumer, NULL, consume, NULL); - - pthread_join(producer, NULL); - pthread_join(consumer, NULL); - - shm_du_map_close(dum); - - return res1; -} -- cgit v1.2.3