summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2017-06-19 16:26:19 +0300
committerIgor Sysoev <igor@sysoev.ru>2017-06-19 16:26:19 +0300
commitb1b9f621a44725509aa7b6eab7305542945893a3 (patch)
treec9925a19d7304f52d9d5e2cc87a5ba5cc72acd4a /test
parent7574c64992b98d3dfbc3dd101bd0f7d78bad0823 (diff)
downloadunit-b1b9f621a44725509aa7b6eab7305542945893a3.tar.gz
unit-b1b9f621a44725509aa7b6eab7305542945893a3.tar.bz2
Memory pools refactoring.
Diffstat (limited to '')
-rw-r--r--test/nxt_lib_unit_test.c9
-rw-r--r--test/nxt_lib_unit_test.h2
-rw-r--r--test/nxt_lvlhsh_unit_test.c27
-rw-r--r--test/nxt_mem_cache_pool_unit_test.c83
-rw-r--r--test/nxt_mp_unit_test.c89
5 files changed, 109 insertions, 101 deletions
diff --git a/test/nxt_lib_unit_test.c b/test/nxt_lib_unit_test.c
index 669b0228..50615828 100644
--- a/test/nxt_lib_unit_test.c
+++ b/test/nxt_lib_unit_test.c
@@ -24,6 +24,7 @@ nxt_msec_less(nxt_msec_t first, nxt_msec_t second)
int nxt_cdecl
main(int argc, char **argv)
{
+ nxt_task_t task;
nxt_thread_t *thr;
if (nxt_lib_start("lib_unit_test", argv, &environ) != NXT_OK) {
@@ -31,8 +32,10 @@ main(int argc, char **argv)
}
nxt_main_log.level = NXT_LOG_INFO;
+ task.log = &nxt_main_log;
thr = nxt_thread();
+ thr->task = &task;
#if (NXT_UNIT_TEST_RTDTSC)
@@ -96,15 +99,15 @@ main(int argc, char **argv)
return 1;
}
- if (nxt_mem_cache_pool_unit_test(thr, 100, 40000, 128 - 1) != NXT_OK) {
+ if (nxt_mp_unit_test(thr, 100, 40000, 128 - 1) != NXT_OK) {
return 1;
}
- if (nxt_mem_cache_pool_unit_test(thr, 100, 1000, 4096 - 1) != NXT_OK) {
+ if (nxt_mp_unit_test(thr, 100, 1000, 4096 - 1) != NXT_OK) {
return 1;
}
- if (nxt_mem_cache_pool_unit_test(thr, 1000, 100, 64 * 1024 - 1) != NXT_OK) {
+ if (nxt_mp_unit_test(thr, 1000, 100, 64 * 1024 - 1) != NXT_OK) {
return 1;
}
diff --git a/test/nxt_lib_unit_test.h b/test/nxt_lib_unit_test.h
index a39c7c03..d215a009 100644
--- a/test/nxt_lib_unit_test.h
+++ b/test/nxt_lib_unit_test.h
@@ -54,7 +54,7 @@ void nxt_rbtree1_mb_delete(nxt_thread_t *thr);
#endif
-nxt_int_t nxt_mem_cache_pool_unit_test(nxt_thread_t *thr, nxt_uint_t runs,
+nxt_int_t nxt_mp_unit_test(nxt_thread_t *thr, nxt_uint_t runs,
nxt_uint_t nblocks, size_t max_size);
nxt_int_t nxt_mem_zone_unit_test(nxt_thread_t *thr, nxt_uint_t runs,
nxt_uint_t nblocks, size_t max_size);
diff --git a/test/nxt_lvlhsh_unit_test.c b/test/nxt_lvlhsh_unit_test.c
index c4dac2f0..d09a34f4 100644
--- a/test/nxt_lvlhsh_unit_test.c
+++ b/test/nxt_lvlhsh_unit_test.c
@@ -21,14 +21,14 @@ nxt_lvlhsh_unit_test_key_test(nxt_lvlhsh_query_t *lhq, void *data)
static void *
nxt_lvlhsh_unit_test_pool_alloc(void *pool, size_t size, nxt_uint_t nalloc)
{
- return nxt_mem_cache_align(pool, size, size);
+ return nxt_mp_align(pool, size, size);
}
static void
nxt_lvlhsh_unit_test_pool_free(void *pool, void *p, size_t size)
{
- nxt_mem_cache_free(pool, p);
+ nxt_mp_free(pool, p);
}
@@ -132,11 +132,11 @@ nxt_int_t
nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool)
{
uintptr_t key;
+ nxt_mp_t *mp;
nxt_nsec_t start, end;
nxt_uint_t i;
nxt_lvlhsh_t lh;
nxt_lvlhsh_each_t lhe;
- nxt_mem_cache_pool_t *pool;
const nxt_lvlhsh_proto_t *proto;
const size_t min_chunk_size = 32;
@@ -148,9 +148,9 @@ nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool)
start = nxt_thread_monotonic_time(thr);
if (use_pool) {
- pool = nxt_mem_cache_pool_create(cluster_size, page_alignment,
- page_size, min_chunk_size);
- if (pool == NULL) {
+ mp = nxt_mp_create(cluster_size, page_alignment, page_size,
+ min_chunk_size);
+ if (mp == NULL) {
return NXT_ERROR;
}
@@ -162,7 +162,7 @@ nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool)
nxt_log_error(NXT_LOG_NOTICE, thr->log,
"lvlhsh unit test started: %uD malloc", n);
proto = &malloc_proto;
- pool = NULL;
+ mp = NULL;
}
nxt_memzero(&lh, sizeof(nxt_lvlhsh_t));
@@ -171,7 +171,7 @@ nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool)
for (i = 0; i < n; i++) {
key = nxt_murmur_hash2(&key, sizeof(uint32_t));
- if (nxt_lvlhsh_unit_test_add(&lh, proto, pool, key) != NXT_OK) {
+ if (nxt_lvlhsh_unit_test_add(&lh, proto, mp, key) != NXT_OK) {
nxt_log_error(NXT_LOG_NOTICE, thr->log,
"lvlhsh add unit test failed at %ui", i);
return NXT_ERROR;
@@ -206,19 +206,18 @@ nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool)
for (i = 0; i < n; i++) {
key = nxt_murmur_hash2(&key, sizeof(uint32_t));
- if (nxt_lvlhsh_unit_test_delete(&lh, proto, pool, key) != NXT_OK) {
+ if (nxt_lvlhsh_unit_test_delete(&lh, proto, mp, key) != NXT_OK) {
return NXT_ERROR;
}
}
- if (pool != NULL) {
- if (!nxt_mem_cache_pool_is_empty(pool)) {
- nxt_log_error(NXT_LOG_NOTICE, thr->log,
- "mem cache pool is not empty");
+ if (mp != NULL) {
+ if (!nxt_mp_is_empty(mp)) {
+ nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem pool is not empty");
return NXT_ERROR;
}
- nxt_mem_cache_pool_destroy(pool);
+ nxt_mp_destroy(mp);
}
nxt_thread_time_update(thr);
diff --git a/test/nxt_mem_cache_pool_unit_test.c b/test/nxt_mem_cache_pool_unit_test.c
deleted file mode 100644
index b9da5099..00000000
--- a/test/nxt_mem_cache_pool_unit_test.c
+++ /dev/null
@@ -1,83 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- * Copyright (C) NGINX, Inc.
- */
-
-#include <nxt_main.h>
-
-
-nxt_int_t
-nxt_mem_cache_pool_unit_test(nxt_thread_t *thr, nxt_uint_t runs,
- nxt_uint_t nblocks, size_t max_size)
-{
- void **blocks;
- size_t total;
- uint32_t value, size;
- nxt_uint_t i, n;
- nxt_mem_cache_pool_t *pool;
-
- const size_t min_chunk_size = 16;
- const size_t page_size = 128;
- const size_t page_alignment = 128;
- const size_t cluster_size = page_size * 8;
-
- nxt_thread_time_update(thr);
- nxt_log_error(NXT_LOG_NOTICE, thr->log,
- "mem cache pool unit test started, max:%uz", max_size);
-
- blocks = nxt_malloc(nblocks * sizeof(void *));
- if (blocks == NULL) {
- return NXT_ERROR;
- }
-
- pool = nxt_mem_cache_pool_create(cluster_size, page_alignment,
- page_size, min_chunk_size);
- if (pool == NULL) {
- return NXT_ERROR;
- }
-
- value = 0;
-
- for (i = 0; i < runs; i++) {
-
- total = 0;
-
- for (n = 0; n < nblocks; n++) {
- value = nxt_murmur_hash2(&value, sizeof(uint32_t));
-
- size = value & max_size;
-
- if (size == 0) {
- size++;
- }
-
- total += size;
- blocks[n] = nxt_mem_cache_alloc(pool, size);
-
- if (blocks[n] == NULL) {
- nxt_log_error(NXT_LOG_NOTICE, thr->log,
- "mem cache pool unit test failed: %uz", total);
- return NXT_ERROR;
- }
- }
-
- for (n = 0; n < nblocks; n++) {
- nxt_mem_cache_free(pool, blocks[n]);
- }
- }
-
- if (!nxt_mem_cache_pool_is_empty(pool)) {
- nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem cache pool is not empty");
- return NXT_ERROR;
- }
-
- nxt_mem_cache_pool_destroy(pool);
-
- nxt_free(blocks);
-
- nxt_thread_time_update(thr);
- nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem cache pool unit test passed");
-
- return NXT_OK;
-}
diff --git a/test/nxt_mp_unit_test.c b/test/nxt_mp_unit_test.c
new file mode 100644
index 00000000..c51c9152
--- /dev/null
+++ b/test/nxt_mp_unit_test.c
@@ -0,0 +1,89 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) NGINX, Inc.
+ */
+
+#include <nxt_main.h>
+
+
+nxt_int_t
+nxt_mp_unit_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks,
+ size_t max_size)
+{
+ void **blocks;
+ size_t total;
+ uint32_t value, size;
+ nxt_mp_t *mp;
+ nxt_bool_t valid;
+ nxt_uint_t i, n;
+
+ const size_t min_chunk_size = 16;
+ const size_t page_size = 128;
+ const size_t page_alignment = 128;
+ const size_t cluster_size = page_size * 8;
+
+ nxt_thread_time_update(thr);
+ nxt_log_error(NXT_LOG_NOTICE, thr->log,
+ "mem pool unit test started, max:%uz", max_size);
+
+ blocks = nxt_malloc(nblocks * sizeof(void *));
+ if (blocks == NULL) {
+ return NXT_ERROR;
+ }
+
+ valid = nxt_mp_test_sizes(cluster_size, page_alignment, page_size,
+ min_chunk_size);
+ if (!valid) {
+ return NXT_ERROR;
+ }
+
+ mp = nxt_mp_create(cluster_size, page_alignment, page_size, min_chunk_size);
+ if (mp == NULL) {
+ return NXT_ERROR;
+ }
+
+ value = 0;
+
+ for (i = 0; i < runs; i++) {
+
+ total = 0;
+
+ for (n = 0; n < nblocks; n++) {
+ value = nxt_murmur_hash2(&value, sizeof(uint32_t));
+
+ size = value & max_size;
+
+ if (size == 0) {
+ size++;
+ }
+
+ total += size;
+ blocks[n] = nxt_mp_alloc(mp, size);
+
+ if (blocks[n] == NULL) {
+ nxt_log_error(NXT_LOG_NOTICE, thr->log,
+ "mem pool unit test failed: %uz", total);
+ return NXT_ERROR;
+ }
+ }
+
+ for (n = 0; n < nblocks; n++) {
+ nxt_mp_free(mp, blocks[n]);
+ }
+ }
+
+ if (!nxt_mp_is_empty(mp)) {
+ nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem pool is not empty");
+ return NXT_ERROR;
+ }
+
+ nxt_mp_destroy(mp);
+
+ nxt_free(blocks);
+
+ nxt_thread_time_update(thr);
+ nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem pool unit test passed");
+
+ return NXT_OK;
+}