aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHanspeter Portner <dev@open-music-kontrollers.ch>2018-05-17 22:56:16 +0200
committerHanspeter Portner <dev@open-music-kontrollers.ch>2018-05-17 22:56:16 +0200
commitfa8cf43f8c20fe62e7a70edb68fe23601df9a861 (patch)
tree0f424a69d831450b7fba2993f98c3eb7b4655a2f
parenta4f7da9eb873a8fffca3b08be3abadbb41ae1163 (diff)
downloadvarchunk-fa8cf43f8c20fe62e7a70edb68fe23601df9a861.tar.xz
some code cleanup.
-rw-r--r--.gitlab-ci.yml4
-rw-r--r--meson.build4
-rw-r--r--test_varchunk.c33
-rw-r--r--varchunk.h8
4 files changed, 36 insertions, 13 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 03aa8f1..25d396a 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -22,8 +22,10 @@ stages:
- ninja -C build
.test_template: &test_definition
- <<: *build_definition
+ <<: *common_definition
script:
+ - meson --prefix=/ --cross-file "${TOOLCHAIN_FILE}" build
+ - ninja -C build
- ninja -C build test
.universal_linux_template: &universal_linux_definition
diff --git a/meson.build b/meson.build
index e6bf7fa..a395b3e 100644
--- a/meson.build
+++ b/meson.build
@@ -20,4 +20,6 @@ test_varchunk = executable('test_varchunk',
dependencies : [rt_dep, thread_dep],
install : false)
-test('Test', test_varchunk)
+test('Test', test_varchunk,
+ args : ['1000000'],
+ timeout : 180) # seconds
diff --git a/test_varchunk.c b/test_varchunk.c
index da3f772..e66edc7 100644
--- a/test_varchunk.c
+++ b/test_varchunk.c
@@ -32,8 +32,9 @@
# define VARCHUNK_USE_SHARED_MEM
#endif
-#define ITERATIONS 10000000
+static uint64_t iterations = 10000000;
#define THRESHOLD (RAND_MAX / 256)
+#define PAD(SIZE) ( ( (size_t)(SIZE) + 7U ) & ( ~7U ) )
static const struct timespec req = {
.tv_sec = 0,
@@ -57,12 +58,14 @@ producer_main(void *arg)
size_t written;
uint64_t cnt = 0;
- while(cnt < ITERATIONS)
+ while(cnt < iterations)
{
if(rand() < THRESHOLD)
+ {
nanosleep(&req, NULL);
+ }
- written = rand() * 1024.f / RAND_MAX;
+ written = PAD(rand() * 1024.f / RAND_MAX);
size_t maximum;
if( (ptr = varchunk_write_request_max(varchunk, written, &maximum)) )
@@ -70,9 +73,11 @@ producer_main(void *arg)
assert(maximum >= written);
end = ptr + written;
for(uint8_t *src=ptr; src<end; src+=sizeof(uint64_t))
+ {
*(uint64_t *)src = cnt;
+ assert(*(uint64_t *)src == cnt);
+ }
varchunk_write_advance(varchunk, written);
- //fprintf(stdout, "P %"PRIu64" %zu %zu\n", cnt, written, maximum);
cnt++;
}
else
@@ -93,18 +98,21 @@ consumer_main(void *arg)
size_t toread;
uint64_t cnt = 0;
- while(cnt < ITERATIONS)
+ while(cnt < iterations)
{
if(rand() < THRESHOLD)
+ {
nanosleep(&req, NULL);
+ }
if( (ptr = varchunk_read_request(varchunk, &toread)) )
{
end = ptr + toread;
for(const uint8_t *src=ptr; src<end; src+=sizeof(uint64_t))
+ {
assert(*(const uint64_t *)src == cnt);
+ }
varchunk_read_advance(varchunk);
- //fprintf(stdout, "C %"PRIu64" %zu\n", cnt, toread);
cnt++;
}
else
@@ -141,7 +149,9 @@ varchunk_shm_init(varchunk_shm_t *varchunk_shm, const char *name, size_t minimum
varchunk_shm->name = strdup(name);
if(!varchunk_shm->name)
+ {
return -1;
+ }
bool is_first = true;
varchunk_shm->fd = shm_open(varchunk_shm->name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
@@ -167,7 +177,9 @@ varchunk_shm_init(varchunk_shm_t *varchunk_shm, const char *name, size_t minimum
}
if(is_first)
+ {
varchunk_init(varchunk_shm->varchunk, body_size, release_and_acquire);
+ }
return 0;
}
@@ -190,6 +202,8 @@ test_shared()
const char *name = "/varchunk_shm_test";
pid_t pid = fork();
+ assert(pid != -1);
+
if(pid == 0) // child
{
varchunk_shm_t varchunk_shm;
@@ -212,11 +226,16 @@ test_shared()
#endif
int
-main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
+main(int argc, char **argv)
{
const int seed = time(NULL);
srand(seed);
+ if(argc >= 2)
+ {
+ iterations = atoi(argv[1]);
+ }
+
assert(varchunk_is_lock_free());
test_threaded();
diff --git a/varchunk.h b/varchunk.h
index 43268fe..6fc1d5b 100644
--- a/varchunk.h
+++ b/varchunk.h
@@ -271,7 +271,7 @@ varchunk_write_advance(varchunk_t *varchunk, size_t written)
if(varchunk->gapd > 0)
{
// fill end of first buffer with gap
- varchunk_elmnt_t *elmnt = (varchunk_elmnt_t *)((uint8_t *)varchunk->buf + head);
+ varchunk_elmnt_t *elmnt = (varchunk_elmnt_t *)(varchunk->buf + head);
elmnt->size = varchunk->gapd - sizeof(varchunk_elmnt_t);
elmnt->gap = 1;
@@ -283,7 +283,7 @@ varchunk_write_advance(varchunk_t *varchunk, size_t written)
else // varchunk->gapd == 0
{
// fill written element header
- varchunk_elmnt_t *elmnt = (varchunk_elmnt_t *)((uint8_t *)varchunk->buf + head);
+ varchunk_elmnt_t *elmnt = (varchunk_elmnt_t *)(varchunk->buf + head);
elmnt->size = written;
elmnt->gap = 0;
}
@@ -332,7 +332,7 @@ varchunk_read_request(varchunk_t *varchunk, size_t *toread)
_varchunk_read_advance_raw(varchunk, tail, len1);
// second part of available buffer
- const uint8_t *buf2 = (const uint8_t *)varchunk->buf;
+ const uint8_t *buf2 = varchunk->buf;
// there will always be at least on element after a gap
elmnt = (const varchunk_elmnt_t *)buf2;
@@ -368,7 +368,7 @@ varchunk_read_advance(varchunk_t *varchunk)
assert(varchunk);
// get elmnt header from tail (for size)
const size_t tail = atomic_load_explicit(&varchunk->tail, memory_order_relaxed);
- const varchunk_elmnt_t *elmnt = (const varchunk_elmnt_t *)((const uint8_t*)varchunk->buf + tail);
+ const varchunk_elmnt_t *elmnt = (const varchunk_elmnt_t *)(varchunk->buf + tail);
// advance read tail
_varchunk_read_advance_raw(varchunk, tail,