From eaff62fca23f72ccf89b2debdd18ae69da6d03c2 Mon Sep 17 00:00:00 2001 From: Roman Tsisyk <roman@tsisyk.com> Date: Wed, 21 Oct 2015 19:01:50 +0300 Subject: [PATCH] Move src/lib/small into a separate repository --- .gitmodules | 3 + src/lib/CMakeLists.txt | 1 + src/lib/small | 1 + test/unit/CMakeLists.txt | 22 +-- test/unit/arena_mt.c | 82 ----------- test/unit/arena_mt.result | 1 - test/unit/ibuf.c | 50 ------- test/unit/ibuf.result | 2 - test/unit/lf_lifo.c | 69 --------- test/unit/lf_lifo.result | 1 - test/unit/matras.cc | 272 ----------------------------------- test/unit/matras.result | 4 - test/unit/mempool.c | 136 ------------------ test/unit/mempool.result | 4 - test/unit/obuf.c | 82 ----------- test/unit/obuf.result | 2 - test/unit/quota.cc | 100 ------------- test/unit/quota.result | 6 - test/unit/region.c | 89 ------------ test/unit/region.result | 7 - test/unit/slab_arena.c | 45 ------ test/unit/slab_arena.result | 25 ---- test/unit/slab_cache.c | 41 ------ test/unit/slab_cache.result | 0 test/unit/small_alloc.c | 106 -------------- test/unit/small_alloc.result | 2 - 26 files changed, 6 insertions(+), 1147 deletions(-) create mode 160000 src/lib/small delete mode 100644 test/unit/arena_mt.c delete mode 100644 test/unit/arena_mt.result delete mode 100644 test/unit/ibuf.c delete mode 100644 test/unit/ibuf.result delete mode 100644 test/unit/lf_lifo.c delete mode 100644 test/unit/lf_lifo.result delete mode 100644 test/unit/matras.cc delete mode 100644 test/unit/matras.result delete mode 100644 test/unit/mempool.c delete mode 100644 test/unit/mempool.result delete mode 100644 test/unit/obuf.c delete mode 100644 test/unit/obuf.result delete mode 100644 test/unit/quota.cc delete mode 100644 test/unit/quota.result delete mode 100644 test/unit/region.c delete mode 100644 test/unit/region.result delete mode 100644 test/unit/slab_arena.c delete mode 100644 test/unit/slab_arena.result delete mode 100644 test/unit/slab_cache.c delete mode 100644 test/unit/slab_cache.result delete mode 100644 test/unit/small_alloc.c delete mode 100644 test/unit/small_alloc.result diff --git a/.gitmodules b/.gitmodules index ea4c4507db..4a28f773cd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,6 @@ [submodule "test-run"] path = test-run url = https://github.com/tarantool/test-run.git +[submodule "src/lib/small"] + path = src/lib/small + url = https://github.com/tarantool/small.git diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 352602bbc7..6215544b83 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(bit) add_subdirectory(bitset) +set(SMALL_EMBEDDED ON) add_subdirectory(small) add_subdirectory(salad) add_subdirectory(csv) diff --git a/src/lib/small b/src/lib/small new file mode 160000 index 0000000000..2d69d5d0f0 --- /dev/null +++ b/src/lib/small @@ -0,0 +1 @@ +Subproject commit 2d69d5d0f010cade8a8d08dbcb3586f782e4f940 diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 3badd231a3..d600984e3b 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -26,23 +26,7 @@ target_link_libraries(bitset_iterator.test bitset) add_executable(bitset_index.test bitset_index.c) target_link_libraries(bitset_index.test bitset) add_executable(base64.test base64.c ${CMAKE_SOURCE_DIR}/third_party/base64.c) -add_executable(slab_cache.test slab_cache.c) -target_link_libraries(slab_cache.test small) -add_executable(region.test region.c) -target_link_libraries(region.test small) -add_executable(ibuf.test ibuf.c) -target_link_libraries(ibuf.test small) -add_executable(obuf.test obuf.c) -target_link_libraries(obuf.test small) -add_executable(mempool.test mempool.c) -target_link_libraries(mempool.test small) -add_executable(small_alloc.test small_alloc.c) -target_link_libraries(small_alloc.test small) -add_executable(lf_lifo.test lf_lifo.c) -add_executable(slab_arena.test slab_arena.c) -target_link_libraries(slab_arena.test small) -add_executable(arena_mt.test arena_mt.c) -target_link_libraries(arena_mt.test small pthread) + add_executable(bps_tree.test bps_tree.cc) target_link_libraries(bps_tree.test small misc) add_executable(bps_tree_itr.test bps_tree_itr.cc) @@ -53,8 +37,6 @@ add_executable(rtree_itr.test rtree_itr.cc) target_link_libraries(rtree_itr.test salad small) add_executable(rtree_multidim.test rtree_multidim.cc) target_link_libraries(rtree_multidim.test salad small) -add_executable(matras.test matras.cc) -target_link_libraries(matras.test small) add_executable(light.test light.cc) target_link_libraries(light.test small) add_executable(vclock.test vclock.cc unit.c @@ -70,8 +52,6 @@ add_executable(xrow.test xrow.cc unit.c ${CMAKE_SOURCE_DIR}/src/box/errcode.c ${CMAKE_SOURCE_DIR}/src/box/error.cc) target_link_libraries(xrow.test server misc msgpuck) -add_executable(quota.test quota.cc unit.c) -target_link_libraries(quota.test pthread) add_executable(fiber.test fiber.cc unit.c) target_link_libraries(fiber.test core) diff --git a/test/unit/arena_mt.c b/test/unit/arena_mt.c deleted file mode 100644 index baae0941c6..0000000000 --- a/test/unit/arena_mt.c +++ /dev/null @@ -1,82 +0,0 @@ -#include "small/slab_arena.h" -#include "small/quota.h" -#include <stdio.h> -#include <limits.h> -#include <stdlib.h> -#include <time.h> -#include "unit.h" -#include <trivia/util.h> -#include <pthread.h> -#ifdef TARGET_OS_FREEBSD -#include <pthread_np.h> -#endif - -struct slab_arena arena; -struct quota quota; - -int THREADS = 8; -int ITERATIONS = 1009 /* 100003 */; -int OSCILLATION = 137; -int FILL = SLAB_MIN_SIZE/sizeof(pthread_t); - -void * -run(void *p __attribute__((unused))) -{ -#ifdef TARGET_OS_FREEBSD - unsigned int seed = pthread_getthreadid_np(); -#else - unsigned int seed = (unsigned int) pthread_self(); -#endif - int iterations = rand_r(&seed) % ITERATIONS; - pthread_t **slabs = slab_map(&arena); - for (int i = 0; i < iterations; i++) { - int oscillation = rand_r(&seed) % OSCILLATION; - for (int osc = 0; osc < oscillation; osc++) { - slabs[osc] = (pthread_t *) slab_map(&arena); - for (int fill = 0; fill < FILL; fill += 100) { - slabs[osc][fill] = pthread_self(); - } - } - sched_yield(); - for (int osc = 0; osc < oscillation; osc++) { - for (int fill = 0; fill < FILL; fill+= 100) { - fail_unless(slabs[osc][fill] == - pthread_self()); - } - slab_unmap(&arena, slabs[osc]); - } - } - slab_unmap(&arena, slabs); - return 0; -} - -void -bench(int count) -{ - pthread_attr_t attr; - pthread_attr_init(&attr); - - pthread_t *threads = (pthread_t *) malloc(sizeof(*threads)*count); - - int i; - for (i = 0; i < count; i++) { - pthread_create(&threads[i], &attr, run, NULL); - } - for (i = 0; i < count; i++) { - pthread_t *thread = &threads[i]; - pthread_join(*thread, NULL); - } - free(threads); -} - -int -main() -{ - size_t maxalloc = THREADS * (OSCILLATION + 1) * SLAB_MIN_SIZE; - quota_init("a, maxalloc); - slab_arena_create(&arena, "a, maxalloc/8, - SLAB_MIN_SIZE, MAP_PRIVATE); - bench(THREADS); - slab_arena_destroy(&arena); - printf("ok\n"); -} diff --git a/test/unit/arena_mt.result b/test/unit/arena_mt.result deleted file mode 100644 index 9766475a41..0000000000 --- a/test/unit/arena_mt.result +++ /dev/null @@ -1 +0,0 @@ -ok diff --git a/test/unit/ibuf.c b/test/unit/ibuf.c deleted file mode 100644 index 46bb04e46f..0000000000 --- a/test/unit/ibuf.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "small/quota.h" -#include "small/ibuf.h" -#include "small/slab_cache.h" -#include "unit.h" -#include <stdio.h> - -struct slab_cache cache; -struct slab_arena arena; -struct quota quota; - -void -ibuf_basic() -{ - header(); - - struct ibuf ibuf; - - ibuf_create(&ibuf, &cache, 16320); - - fail_unless(ibuf_used(&ibuf) == 0); - - void *ptr = ibuf_alloc_nothrow(&ibuf, 10); - - fail_unless(ptr); - - fail_unless(ibuf_used(&ibuf) == 10); - - ptr = ibuf_alloc_nothrow(&ibuf, 1000000); - fail_unless(ptr); - - fail_unless(ibuf_used(&ibuf) == 1000010); - - ibuf_reset(&ibuf); - - fail_unless(ibuf_used(&ibuf) == 0); - - footer(); -} - -int main() -{ - quota_init("a, UINT_MAX); - slab_arena_create(&arena, "a, 0, - 4000000, MAP_PRIVATE); - slab_cache_create(&cache, &arena); - - ibuf_basic(); - - slab_cache_destroy(&cache); -} diff --git a/test/unit/ibuf.result b/test/unit/ibuf.result deleted file mode 100644 index 5bcde7d88a..0000000000 --- a/test/unit/ibuf.result +++ /dev/null @@ -1,2 +0,0 @@ - *** ibuf_basic *** - *** ibuf_basic: done *** diff --git a/test/unit/lf_lifo.c b/test/unit/lf_lifo.c deleted file mode 100644 index 1c959ee37b..0000000000 --- a/test/unit/lf_lifo.c +++ /dev/null @@ -1,69 +0,0 @@ -#include "small/lf_lifo.h" -#include "unit.h" -#include <sys/mman.h> - -#if !defined(MAP_ANONYMOUS) -#define MAP_ANONYMOUS MAP_ANON -#endif - -static void * -mmap_aligned(size_t size) -{ - assert((size & (size - 1)) == 0); - void *map = mmap(NULL, 2 * size, - PROT_READ | PROT_WRITE, MAP_PRIVATE | - MAP_ANONYMOUS, -1, 0); - - /* Align the mapped address around slab size. */ - size_t offset = (intptr_t) map & (size - 1); - - if (offset != 0) { - munmap(map, size - offset); - map += size - offset; - munmap(map + size, offset); - } else { - /* The address is returned aligned. */ - munmap(map + size, size); - } - return map; -} - -#define MAP_SIZE 0x10000 - -int main() -{ - struct lf_lifo head; - void *val1 = mmap_aligned(MAP_SIZE); - void *val2 = mmap_aligned(MAP_SIZE); - void *val3 = mmap_aligned(MAP_SIZE); - lf_lifo_init(&head); - - fail_unless(lf_lifo_pop(&head) == NULL); - fail_unless(lf_lifo_pop(lf_lifo_push(&head, val1)) == val1); - fail_unless(lf_lifo_pop(lf_lifo_push(&head, val1)) == val1); - lf_lifo_push(lf_lifo_push(lf_lifo_push(&head, val1), val2), val3); - fail_unless(lf_lifo_pop(&head) == val3); - fail_unless(lf_lifo_pop(&head) == val2); - fail_unless(lf_lifo_pop(&head) == val1); - fail_unless(lf_lifo_pop(&head) == NULL); - - lf_lifo_init(&head); - - /* Test overflow of ABA counter. */ - - int i = 0; - do { - lf_lifo_push(&head, val1); - fail_unless(lf_lifo_pop(&head) == val1); - fail_unless(lf_lifo_pop(&head) == NULL); - i++; - } while (head.next != 0); - - munmap(val1, MAP_SIZE); - munmap(val2, MAP_SIZE); - munmap(val3, MAP_SIZE); - - printf("success\n"); - - return 0; -} diff --git a/test/unit/lf_lifo.result b/test/unit/lf_lifo.result deleted file mode 100644 index 2e9ba477f8..0000000000 --- a/test/unit/lf_lifo.result +++ /dev/null @@ -1 +0,0 @@ -success diff --git a/test/unit/matras.cc b/test/unit/matras.cc deleted file mode 100644 index 6f80d00f4c..0000000000 --- a/test/unit/matras.cc +++ /dev/null @@ -1,272 +0,0 @@ -#include "small/matras.h" -#include <set> -#include <vector> -#include <assert.h> -#include <stdlib.h> -#include <stdint.h> -#include <stddef.h> -#include <iostream> - -static void * -pta_alloc(); -static void -pta_free(void *p); - -#define PROV_BLOCK_SIZE 16 -#define PROV_EXTENT_SIZE 64 - -static size_t AllocatedCount = 0; -static std::set<void*> AllocatedBlocks; -static std::set<void*> AllocatedItems; - -static void -check_file_line(bool expr, const char *err_message, const char *file, int line) -{ - if (!expr) { - std::cout << " ****************************************\n" - << " * " << file << ":" << line - << " ERROR: " << err_message << "\n"; - } - assert(expr); - if (!expr) { - throw err_message; - } -} - -#define check(e, m) check_file_line(e, m, __FILE__, __LINE__) - -bool alloc_err_inj_enabled = false; -unsigned int alloc_err_inj_countdown = 0; - -#define MATRAS_VERSION_COUNT 8 - -static void * -pta_alloc() -{ - if (alloc_err_inj_enabled) { - if (alloc_err_inj_countdown == 0) - return 0; - alloc_err_inj_countdown--; - } - void *p = new char[PROV_EXTENT_SIZE]; - AllocatedCount++; - AllocatedBlocks.insert(p); - return p; -} -static void -pta_free(void *p) -{ - check(AllocatedBlocks.find(p) != AllocatedBlocks.end(), "Bad free"); - AllocatedBlocks.erase(p); - delete [] static_cast<char *>(p); - AllocatedCount--; -} - -void matras_alloc_test() -{ - std::cout << "Testing matras_alloc..." << std::endl; - unsigned int maxCapacity = PROV_EXTENT_SIZE / PROV_BLOCK_SIZE; - maxCapacity *= PROV_EXTENT_SIZE / sizeof(void *); - maxCapacity *= PROV_EXTENT_SIZE / sizeof(void *); - - struct matras mat; - - alloc_err_inj_enabled = false; - for (unsigned int i = 0; i <= maxCapacity; i++) { - matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free); - check(1u << mat.log2_capacity == maxCapacity, "Wrong capacity!"); - AllocatedItems.clear(); - for (unsigned int j = 0; j < i; j++) { - unsigned int res = 0; - void *data = matras_alloc(&mat, &res); - check(data, "Alloc returned NULL"); - void *test_data = matras_get(&mat, res); - check(data == test_data, "Alloc and Get mismatch"); - size_t provConsumedMemory = (size_t)matras_extent_count(&mat) * PROV_EXTENT_SIZE; - check(provConsumedMemory == AllocatedCount * PROV_EXTENT_SIZE, "ConsumedMemory counter failed (1)"); - check(res == j, "Index mismatch"); - { - check(!AllocatedBlocks.empty(), "Alloc w/o alloc!"); - std::set<void*>::iterator itr = AllocatedBlocks.lower_bound(data); - if (itr == AllocatedBlocks.end() || *itr != data) { - check(itr != AllocatedBlocks.begin(), "Pointer to not allocatead region! (1)"); - --itr; - } - check (itr != AllocatedBlocks.end(), "Pointer to not allocatead region! (2)"); - check(data <= (void*)( ((char*)(*itr)) + PROV_EXTENT_SIZE - PROV_BLOCK_SIZE), "Pointer to not allocatead region! (3)"); - } - { - if (!AllocatedItems.empty()) { - std::set<void*>::iterator itr = AllocatedItems.lower_bound(data); - if (itr != AllocatedItems.end()) { - check(*itr >= (void*)(((char*)data) + PROV_BLOCK_SIZE), "Data regions overlaps! (1)"); - } - if (itr != AllocatedItems.begin()) { - --itr; - check(data >= (void*)(((char*)(*itr)) + PROV_BLOCK_SIZE), "Data regions overlaps! (2)"); - } - } - } - AllocatedItems.insert(data); - } - size_t provConsumedMemory = (size_t)matras_extent_count(&mat) * PROV_EXTENT_SIZE; - check(provConsumedMemory == AllocatedCount * PROV_EXTENT_SIZE, "ConsumedMemory counter failed (2)"); - matras_destroy(&mat); - check(AllocatedCount == 0, "Not all memory freed (1)"); - } - - for (unsigned int i = 0; i <= maxCapacity; i++) { - matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free); - for (unsigned int j = 0; j < i; j++) { - unsigned int res = 0; - void *data = matras_alloc(&mat, &res); - } - for (unsigned int j = 0; j < i; j++) { - matras_dealloc(&mat); - size_t provConsumedMemory = (size_t)matras_extent_count(&mat) * PROV_EXTENT_SIZE; - check(provConsumedMemory == AllocatedCount * PROV_EXTENT_SIZE, "ConsumedMemory counter failed (3)"); - } - check(AllocatedCount == 0, "Not all memory freed (2)"); - matras_destroy(&mat); - } - - alloc_err_inj_enabled = true; - for (unsigned int i = 0; i <= maxCapacity; i++) { - matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free); - - alloc_err_inj_countdown = i; - - for (unsigned int j = 0; j < maxCapacity; j++) { - unsigned int res = 0; - unsigned int prev_block_count = mat.head.block_count; - void *data = matras_alloc(&mat, &res); - if (!data) { - check(prev_block_count == mat.head.block_count, "Created count changed during memory fail!"); - break; - } - } - matras_destroy(&mat); - check(AllocatedCount == 0, "Not all memory freed after memory fail!"); - } - - std::cout << "Testing matras_alloc successfully finished" << std::endl; -} - -typedef uint64_t type_t; -const size_t VER_EXTENT_SIZE = 512; -int extents_in_use = 0; - -void *all() -{ - extents_in_use++; - return malloc(VER_EXTENT_SIZE); -} - -void dea(void *p) -{ - extents_in_use--; - free(p); -} - -struct matras_view views[MATRAS_VERSION_COUNT]; -int vermask = 1; - -int reg_view_id() -{ - int id = __builtin_ctz(~vermask); - vermask |= 1 << id; - return id; -} - -void unreg_view_id(int id) -{ - vermask &=~ (1 << id); -} - -void -matras_vers_test() -{ - std::cout << "Testing matras versions..." << std::endl; - - std::vector<type_t> comps[MATRAS_VERSION_COUNT]; - int use_mask = 1; - int cur_num_or_ver = 1; - struct matras local; - matras_create(&local, VER_EXTENT_SIZE, sizeof(type_t), all, dea); - type_t val = 0; - for (int s = 10; s < 8000; s = int(s * 1.5)) { - for (int k = 0; k < 800; k++) { - bool check_me = false; - if (rand() % 16 == 0) { - bool add_ver; - if (cur_num_or_ver == 1) - add_ver = true; - else if (cur_num_or_ver == MATRAS_VERSION_COUNT) - add_ver = false; - else - add_ver = rand() % 2 == 0; - if (add_ver) { - cur_num_or_ver++; - matras_id_t new_ver = reg_view_id(); - matras_create_read_view(&local, views + new_ver); - check(new_ver > 0, "create read view failed"); - use_mask |= (1 << new_ver); - comps[new_ver] = comps[0]; - } else { - cur_num_or_ver--; - int del_ver; - do { - del_ver = 1 + rand() % (MATRAS_VERSION_COUNT - 1); - } while ((use_mask & (1 << del_ver)) == 0); - matras_destroy_read_view(&local, views + del_ver); - unreg_view_id(del_ver); - comps[del_ver].clear(); - use_mask &= ~(1 << del_ver); - } - check_me = true; - } else { - check_me = rand() % 16 == 0; - if (rand() % 8 == 0 && comps[0].size() > 0) { - matras_dealloc(&local); - comps[0].pop_back(); - } - int p = rand() % s; - int mod = 0; - while (p >= comps[0].size()) { - comps[0].push_back(val * 10000 + mod); - matras_id_t tmp; - type_t *ptrval = (type_t *)matras_alloc(&local, &tmp); - *ptrval = val * 10000 + mod; - mod++; - } - val++; - comps[0][p] = val; - matras_touch(&local, p); - *(type_t *)matras_get(&local, p) = val; - } - views[0] = local.head; - - for (int i = 0; i < MATRAS_VERSION_COUNT; i++) { - if ((use_mask & (1 << i)) == 0) - continue; - check(comps[i].size() == views[i].block_count, "size mismatch"); - for (int j = 0; j < comps[i].size(); j++) { - type_t val1 = comps[i][j]; - type_t val2 = *(type_t *)matras_view_get(&local, views + i, j); - check(val1 == val2, "data mismatch"); - } - } - } - } - matras_destroy(&local); - check(extents_in_use == 0, "memory leak"); - - std::cout << "Testing matras_version successfully finished" << std::endl; -} - -int -main(int, const char **) -{ - matras_alloc_test(); - matras_vers_test(); -} diff --git a/test/unit/matras.result b/test/unit/matras.result deleted file mode 100644 index cf05e6434a..0000000000 --- a/test/unit/matras.result +++ /dev/null @@ -1,4 +0,0 @@ -Testing matras_alloc... -Testing matras_alloc successfully finished -Testing matras versions... -Testing matras_version successfully finished diff --git a/test/unit/mempool.c b/test/unit/mempool.c deleted file mode 100644 index f37b691d39..0000000000 --- a/test/unit/mempool.c +++ /dev/null @@ -1,136 +0,0 @@ -#include "small/mempool.h" -#include "small/quota.h" -#include "unit.h" -#include <stdio.h> -#include <stdlib.h> -#include <time.h> - -enum { - OBJSIZE_MIN = 2 * sizeof(int), - OBJSIZE_MAX = 4096, - OBJECTS_MAX = 10000, - OSCILLATION_MAX = 1024, - ITERATIONS_MAX = 500, -}; - -struct slab_arena arena; -struct slab_cache cache; -struct quota quota; -struct mempool pool; -int objsize; -size_t used; -/* Streak type - allocating or freeing */ -bool allocating = true; -/** Keep global to easily inspect the core. */ -long seed; - -static int *ptrs[OBJECTS_MAX]; - -static inline void -free_checked(int *ptr) -{ - fail_unless(ptr[0] < OBJECTS_MAX && - ptr[objsize/sizeof(int)-1] == ptr[0]); - int pos = ptr[0]; - fail_unless(ptrs[pos] == ptr); - fail_unless(mempool_used(&pool) == used); - ptrs[pos][0] = ptrs[pos][objsize/sizeof(int)-1] = INT_MAX; - mempool_free(&pool, ptrs[pos]); - ptrs[pos] = NULL; - used -= objsize; -} - -static inline void * -alloc_checked() -{ - int pos = rand() % OBJECTS_MAX; - if (ptrs[pos]) { - assert(ptrs[pos][0] == pos); - free_checked(ptrs[pos]); - ptrs[pos] = 0; - } - if (! allocating) - return NULL; - fail_unless(mempool_used(&pool) == used); - used += objsize; - ptrs[pos] = mempool_alloc_nothrow(&pool); - ptrs[pos][0] = pos; - ptrs[pos][objsize/sizeof(int)-1] = pos; - return ptrs[pos]; -} - - -static void -basic_alloc_streak() -{ - int oscillation = rand() % OSCILLATION_MAX; - int i; - - for (i = 0; i < oscillation; ++i) { - alloc_checked(); - } -} - -void -mempool_basic() -{ - int i; - header(); - - mempool_create(&pool, &cache, objsize); - - for (i = 0; i < ITERATIONS_MAX; i++) { - basic_alloc_streak(); - allocating = ! allocating; -#if 0 - printf("%zu %zu\n", mempool_used(&pool), - mempool_total(&pool)); -#endif - } - - mempool_destroy(&pool); - - footer(); -} - -void -mempool_align() -{ - header(); - - for (uint32_t size = OBJSIZE_MIN; size < OBJSIZE_MAX; size <<= 1) { - mempool_create(&pool, &cache, size); - for (uint32_t i = 0; i < 32; i++) { - void *ptr = mempool_alloc_nothrow(&pool); - uintptr_t addr = (uintptr_t)ptr; - if (addr % size) - fail("aligment", "wrong"); - } - mempool_destroy(&pool); - } - - footer(); -} - -int main() -{ - seed = time(0); - - srand(seed); - - objsize = rand() % OBJSIZE_MAX; - if (objsize < OBJSIZE_MIN) - objsize = OBJSIZE_MIN; - - quota_init("a, UINT_MAX); - - slab_arena_create(&arena, "a, 0, - 4000000, MAP_PRIVATE); - slab_cache_create(&cache, &arena); - - mempool_basic(); - - mempool_align(); - - slab_cache_destroy(&cache); -} diff --git a/test/unit/mempool.result b/test/unit/mempool.result deleted file mode 100644 index 6cfdd259ab..0000000000 --- a/test/unit/mempool.result +++ /dev/null @@ -1,4 +0,0 @@ - *** mempool_basic *** - *** mempool_basic: done *** - *** mempool_align *** - *** mempool_align: done *** diff --git a/test/unit/obuf.c b/test/unit/obuf.c deleted file mode 100644 index 9b091569db..0000000000 --- a/test/unit/obuf.c +++ /dev/null @@ -1,82 +0,0 @@ -#include "small/quota.h" -#include "small/obuf.h" -#include "small/slab_cache.h" -#include "unit.h" -#include <stdio.h> -#include <stdlib.h> -#include <time.h> - -enum { - OBJSIZE_MIN = sizeof(int), - OBJSIZE_MAX = 5000, - OBJECTS_MAX = 1000, - OSCILLATION_MAX = 1024, - ITERATIONS_MAX = 5000, -}; - -/** Keep global to easily inspect the core. */ -long seed; - -void -alloc_checked(struct obuf *buf) -{ - int pos = rand() % OBJECTS_MAX; - int size = rand() % OBJSIZE_MAX; - if (size < OBJSIZE_MIN || size > OBJSIZE_MAX) - size = OBJSIZE_MIN; - - obuf_alloc_nothrow(buf, size); -} - -static void -basic_alloc_streak(struct obuf *buf) -{ - int oscillation = rand() % OSCILLATION_MAX; - int i; - - for (i = 0; i < oscillation; ++i) - alloc_checked(buf); -} - -void -obuf_basic(struct slab_cache *slabc) -{ - int i; - header(); - - struct obuf buf; - obuf_create(&buf, slabc, 16320); - - for (i = 0; i < ITERATIONS_MAX; i++) { - basic_alloc_streak(&buf); - obuf_reset(&buf); - fail_unless(obuf_used(&buf) == 0); - } - obuf_destroy(&buf); - fail_unless(slab_cache_used(slabc) == 0); - slab_cache_check(slabc); - - footer(); -} - -int main() -{ - struct slab_cache cache; - struct slab_arena arena; - struct quota quota; - - seed = time(0); - - srand(seed); - - quota_init("a, UINT_MAX); - - slab_arena_create(&arena, "a, 0, 4000000, - MAP_PRIVATE); - slab_cache_create(&cache, &arena); - - obuf_basic(&cache); - - slab_cache_destroy(&cache); -} - diff --git a/test/unit/obuf.result b/test/unit/obuf.result deleted file mode 100644 index e04e27a1d5..0000000000 --- a/test/unit/obuf.result +++ /dev/null @@ -1,2 +0,0 @@ - *** obuf_basic *** - *** obuf_basic: done *** diff --git a/test/unit/quota.cc b/test/unit/quota.cc deleted file mode 100644 index 31c6d0a2de..0000000000 --- a/test/unit/quota.cc +++ /dev/null @@ -1,100 +0,0 @@ -#include "small/quota.h" - -#include <pthread.h> -#include <sched.h> - -#include "unit.h" - -struct quota quota; - -const size_t THREAD_CNT = 10; -const size_t RUN_CNT = 128 * 1024; - -struct thread_data { - size_t use_change; - size_t last_lim_set; - long use_change_success; - long lim_change_success; -}; - -pthread_t threads[THREAD_CNT]; -thread_data datum[THREAD_CNT]; - -void *thread_routine(void *vparam) -{ - struct thread_data *data = (struct thread_data *)vparam; - size_t check_fail_count = 0; - ssize_t allocated_size = 0; - for (size_t i = 0; i < RUN_CNT; i++) { - { - size_t total, used; - quota_get_total_and_used("a, &total, &used); - if (used > total) - check_fail_count++; - } - ssize_t max = rand() % QUOTA_MAX; - max = quota_set("a, max); - sched_yield(); - if (max > 0) { - data->last_lim_set = max; - data->lim_change_success++; - } - if (allocated_size > 0) { - quota_release("a, allocated_size); - allocated_size = -1; - data->use_change = 0; - data->use_change_success++; - sched_yield(); - } else { - allocated_size = rand() % max + 1; - allocated_size = quota_use("a, allocated_size); - if (allocated_size > 0) { - data->use_change = allocated_size; - data->use_change_success++; - } - sched_yield(); - } - } - return (void *)check_fail_count; -} - -int -main(int n, char **a) -{ - (void)n; - (void)a; - quota_init("a, 0); - srand(time(0)); - - plan(5); - - for (size_t i = 0; i < THREAD_CNT; i++) { - pthread_create(threads + i, 0, thread_routine, (void *)(datum + i)); - } - - size_t check_fail_count = 0; - for (size_t i = 0; i < THREAD_CNT; i++) { - void *ret; - check_fail_count += (size_t)pthread_join(threads[i], &ret); - } - - bool one_set_successed = false; - size_t total_alloc = 0; - long set_success_count = 0; - long use_success_count = 0; - for (size_t i = 0; i < THREAD_CNT; i++) { - if (datum[i].last_lim_set == quota_total("a)) - one_set_successed = true; - total_alloc += datum[i].use_change; - use_success_count += datum[i].use_change_success; - set_success_count += datum[i].lim_change_success; - } - - ok(check_fail_count == 0, "no fails detected"); - ok(one_set_successed, "one of thread limit set is final"); - ok(total_alloc == quota_used("a), "total alloc match"); - ok(use_success_count > THREAD_CNT * RUN_CNT * .1, "uses are mosly successful"); - ok(set_success_count > THREAD_CNT * RUN_CNT * .1, "sets are mosly successful"); - - return check_plan(); -} diff --git a/test/unit/quota.result b/test/unit/quota.result deleted file mode 100644 index 84fddb5dab..0000000000 --- a/test/unit/quota.result +++ /dev/null @@ -1,6 +0,0 @@ -1..5 -ok 1 - no fails detected -ok 2 - one of thread limit set is final -ok 3 - total alloc match -ok 4 - uses are mosly successful -ok 5 - sets are mosly successful diff --git a/test/unit/region.c b/test/unit/region.c deleted file mode 100644 index ac8dd2005e..0000000000 --- a/test/unit/region.c +++ /dev/null @@ -1,89 +0,0 @@ -#include "small/region.h" -#include "small/quota.h" -#include "unit.h" -#include <stdio.h> - -struct slab_cache cache; -struct slab_arena arena; -struct quota quota; - -void -region_basic() -{ - header(); - - struct region region; - - region_create(®ion, &cache); - - fail_unless(region_used(®ion) == 0); - - void *ptr = region_alloc_nothrow(®ion, 10); - - fail_unless(ptr); - - fail_unless(region_used(®ion) == 10); - - ptr = region_alloc_nothrow(®ion, 10000000); - fail_unless(ptr); - - fail_unless(region_used(®ion) == 10000010); - - region_free(®ion); - - fail_unless(region_used(®ion) == 0); - - printf("name of a new region: %s.\n", region_name(®ion)); - - region_set_name(®ion, "region"); - - printf("set new region name: %s.\n", region_name(®ion)); - - region_set_name(®ion, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); - - printf("region name is truncated: %s.\n", region_name(®ion)); - - footer(); -} - -void -region_test_truncate() -{ - header(); - - struct region region; - - region_create(®ion, &cache); - - void *ptr = region_alloc_nothrow(®ion, 10); - - fail_unless(ptr); - - size_t used = region_used(®ion); - - region_alloc_nothrow(®ion, 10000); - region_alloc_nothrow(®ion, 10000000); - - region_truncate(®ion, used); - - fail_unless(region_used(®ion) == used); - - region_free(®ion); - - footer(); -} - -int main() -{ - quota_init("a, UINT_MAX); - slab_arena_create(&arena, "a, 0, - 4000000, MAP_PRIVATE); - slab_cache_create(&cache, &arena); - - region_basic(); - region_test_truncate(); - - slab_cache_destroy(&cache); -} diff --git a/test/unit/region.result b/test/unit/region.result deleted file mode 100644 index 469d346e48..0000000000 --- a/test/unit/region.result +++ /dev/null @@ -1,7 +0,0 @@ - *** region_basic *** -name of a new region: . -set new region name: region. -region name is truncated: aaaaaaaaaaaaaaaaaaaaaaaaaaaaa. - *** region_basic: done *** - *** region_test_truncate *** - *** region_test_truncate: done *** diff --git a/test/unit/slab_arena.c b/test/unit/slab_arena.c deleted file mode 100644 index 2f7a5b48d0..0000000000 --- a/test/unit/slab_arena.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "small/slab_arena.h" -#include "small/quota.h" -#include <stdio.h> -#include <limits.h> -#include <stdlib.h> -#include <time.h> -#include "unit.h" - -void -slab_arena_print(struct slab_arena *arena) -{ - printf("arena->prealloc = %zu\narena->maxalloc = %zu\n" - "arena->used = %zu\narena->slab_size = %u\n", - arena->prealloc, quota_total(arena->quota), - arena->used, arena->slab_size); -} - -int main() -{ - struct quota quota; - struct slab_arena arena; - - quota_init("a, 0); - slab_arena_create(&arena, "a, 0, 0, MAP_PRIVATE); - slab_arena_print(&arena); - slab_arena_destroy(&arena); - - quota_init("a, SLAB_MIN_SIZE); - slab_arena_create(&arena, "a, 1, 1, MAP_PRIVATE); - slab_arena_print(&arena); - void *ptr = slab_map(&arena); - slab_arena_print(&arena); - void *ptr1 = slab_map(&arena); - printf("going beyond the limit: %s\n", ptr1 ? "(ptr)" : "(nil)"); - slab_arena_print(&arena); - slab_unmap(&arena, ptr); - slab_unmap(&arena, ptr1); - slab_arena_print(&arena); - slab_arena_destroy(&arena); - - quota_init("a, 2000000); - slab_arena_create(&arena, "a, 3000000, 1, MAP_PRIVATE); - slab_arena_print(&arena); - slab_arena_destroy(&arena); -} diff --git a/test/unit/slab_arena.result b/test/unit/slab_arena.result deleted file mode 100644 index fb73025acb..0000000000 --- a/test/unit/slab_arena.result +++ /dev/null @@ -1,25 +0,0 @@ -arena->prealloc = 0 -arena->maxalloc = 0 -arena->used = 0 -arena->slab_size = 65536 -arena->prealloc = 65536 -arena->maxalloc = 65536 -arena->used = 0 -arena->slab_size = 65536 -arena->prealloc = 65536 -arena->maxalloc = 65536 -arena->used = 65536 -arena->slab_size = 65536 -going beyond the limit: (nil) -arena->prealloc = 65536 -arena->maxalloc = 65536 -arena->used = 65536 -arena->slab_size = 65536 -arena->prealloc = 65536 -arena->maxalloc = 65536 -arena->used = 65536 -arena->slab_size = 65536 -arena->prealloc = 2031616 -arena->maxalloc = 2000896 -arena->used = 0 -arena->slab_size = 65536 diff --git a/test/unit/slab_cache.c b/test/unit/slab_cache.c deleted file mode 100644 index a52c09ff9d..0000000000 --- a/test/unit/slab_cache.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "small/slab_cache.h" -#include "small/quota.h" -#include <stdio.h> -#include <limits.h> -#include <stdlib.h> -#include <time.h> -#include "unit.h" - - -enum { NRUNS = 25, ITERATIONS = 1000, MAX_ALLOC = 5000000 }; -static struct slab *runs[NRUNS]; - -int main() -{ - srand(time(0)); - - struct quota quota; - struct slab_arena arena; - struct slab_cache cache; - - quota_init("a, UINT_MAX); - - slab_arena_create(&arena, "a, 0, 4000000, MAP_PRIVATE); - slab_cache_create(&cache, &arena); - - int i = 0; - - while (i < ITERATIONS) { - int run = random() % NRUNS; - int size = random() % MAX_ALLOC; - if (runs[run]) { - slab_put(&cache, runs[run]); - } - runs[run] = slab_get(&cache, size); - fail_unless(runs[run]); - slab_cache_check(&cache); - i++; - } - - slab_cache_destroy(&cache); -} diff --git a/test/unit/slab_cache.result b/test/unit/slab_cache.result deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/test/unit/small_alloc.c b/test/unit/small_alloc.c deleted file mode 100644 index 9761da701f..0000000000 --- a/test/unit/small_alloc.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "small/small.h" -#include "small/quota.h" -#include "unit.h" -#include <stdio.h> -#include <stdlib.h> -#include <time.h> - -enum { - OBJSIZE_MIN = 3 * sizeof(int), - OBJSIZE_MAX = 5000, - OBJECTS_MAX = 1000, - OSCILLATION_MAX = 1024, - ITERATIONS_MAX = 5000, -}; - -struct slab_arena arena; -struct slab_cache cache; -struct small_alloc alloc; -struct quota quota; -/* Streak type - allocating or freeing */ -bool allocating = true; -/** Keep global to easily inspect the core. */ -long seed; - -static int *ptrs[OBJECTS_MAX]; - -static inline void -free_checked(int *ptr) -{ - fail_unless(ptr[0] < OBJECTS_MAX && - ptr[ptr[1]/sizeof(int)-1] == ptr[0]); - int pos = ptr[0]; - fail_unless(ptrs[pos] == ptr); - ptrs[pos][0] = ptrs[pos][ptr[1]/sizeof(int)-1] = INT_MAX; - smfree(&alloc, ptrs[pos], ptrs[pos][1]); - ptrs[pos] = NULL; -} - -static inline void * -alloc_checked() -{ - int pos = rand() % OBJECTS_MAX; - int size = rand() % OBJSIZE_MAX; - if (size < OBJSIZE_MIN || size > OBJSIZE_MAX) - size = OBJSIZE_MIN; - - if (ptrs[pos]) { - assert(ptrs[pos][0] == pos); - free_checked(ptrs[pos]); - } - if (! allocating) - return NULL; - ptrs[pos] = smalloc_nothrow(&alloc, size); - ptrs[pos][0] = pos; - ptrs[pos][1] = size; - ptrs[pos][size/sizeof(int)-1] = pos; -// printf("size: %d\n", size); - return ptrs[pos]; -} - - -static void -basic_alloc_streak() -{ - int oscillation = rand() % OSCILLATION_MAX; - int i; - - for (i = 0; i < oscillation; ++i) { - alloc_checked(); - } -} - -void -small_alloc_basic() -{ - int i; - header(); - - small_alloc_create(&alloc, &cache, OBJSIZE_MIN, 1.3); - - for (i = 0; i < ITERATIONS_MAX; i++) { - basic_alloc_streak(); - allocating = ! allocating; - } - - small_alloc_destroy(&alloc); - - footer(); -} - -int main() -{ - seed = time(0); - - srand(seed); - - quota_init("a, UINT_MAX); - - slab_arena_create(&arena, "a, 0, 4000000, - MAP_PRIVATE); - slab_cache_create(&cache, &arena); - - small_alloc_basic(); - - slab_cache_destroy(&cache); -} diff --git a/test/unit/small_alloc.result b/test/unit/small_alloc.result deleted file mode 100644 index 362ca413a4..0000000000 --- a/test/unit/small_alloc.result +++ /dev/null @@ -1,2 +0,0 @@ - *** small_alloc_basic *** - *** small_alloc_basic: done *** -- GitLab