Memory pools refactoring.

This commit is contained in:
Igor Sysoev
2017-06-19 16:26:19 +03:00
parent 7574c64992
commit b1b9f621a4
17 changed files with 1133 additions and 937 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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;
}

89
test/nxt_mp_unit_test.c Normal file
View File

@@ -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;
}