mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-26 15:13:25 +00:00
Patch add idr (#56)
* 增加了idr模块 * 增加了IDR模块,并尝试覆盖上一个错误版本. * 增加了IDR模块 * 修改了test-idr.c文件 * 进一步完善函数注释 * 更新idr文档同时修改了test-idr的错误 * 将lz4库改为使用系统的clz函数 * idr和test-idr O1 * bugfix: 修复测试用例中的移位问题 * 修正问题 Signed-off-by: guanjinquan <1666320330@qq.com> Co-authored-by: fslongjin <longjin@RinGoTek.cn>
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O1")
|
||||
#include "ktest.h"
|
||||
#include "ktest_utils.h"
|
||||
#include <common/idr.h>
|
||||
@ -8,7 +9,7 @@
|
||||
* @brief 测试idr的构建,预获取空间是否成功
|
||||
*
|
||||
* 以下函数将被测试:
|
||||
* 1. idr_pre_get
|
||||
* 1. idr_preload
|
||||
* 2. DECLARE_IDR
|
||||
* 3. idr_init
|
||||
* 4. idr_destroy
|
||||
@ -33,7 +34,7 @@ static long ktest_idr_case0(uint64_t arg0, uint64_t arg1)
|
||||
idr_init(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
|
||||
assert(idr_pre_get(&k_idr, 0) == 0);
|
||||
assert(idr_preload(&k_idr, 0) == 0);
|
||||
assert(k_idr.id_free_cnt == IDR_FREE_MAX);
|
||||
|
||||
for (uint64_t i = 1; i < 64; i++)
|
||||
@ -71,7 +72,7 @@ static long ktest_idr_case1(uint64_t arg0, uint64_t arg1)
|
||||
// 获取128个id
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
@ -87,35 +88,53 @@ static long ktest_idr_case1(uint64_t arg0, uint64_t arg1)
|
||||
// 倒序:删除64个id
|
||||
for (int i = 127; i >= 64; i--)
|
||||
{
|
||||
idr_remove(&k_idr, a[i]);
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 正序:删除64个id
|
||||
for (int i = 0; i <= 63; i++)
|
||||
{
|
||||
idr_remove(&k_idr, a[i]);
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_count(&k_idr, i) == 0);
|
||||
}
|
||||
|
||||
// 重新申请128个id, 值域范围应该仍然是[0,127]
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_count(&k_idr, i));
|
||||
}
|
||||
|
||||
// 正序:删除32个id
|
||||
for (int i = 0; i <= 31; i++)
|
||||
{
|
||||
idr_remove(&k_idr, a[i]);
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 倒序:删除32个id
|
||||
for (int i = 127; i >= 96; i--)
|
||||
{
|
||||
idr_remove(&k_idr, a[i]);
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
@ -126,7 +145,7 @@ static long ktest_idr_case1(uint64_t arg0, uint64_t arg1)
|
||||
// 获取128个id
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
@ -171,20 +190,30 @@ static long ktest_idr_case2(uint64_t arg0, uint64_t arg1)
|
||||
DECLARE_IDR(k_idr);
|
||||
|
||||
// 获取 1000‘000 个ID
|
||||
const int N = 1e7;
|
||||
const int M = 3e6;
|
||||
const int N = 1e6;
|
||||
// const int N = 1048576;
|
||||
const int M = 2e5;
|
||||
|
||||
int tmp;
|
||||
int tmp=0;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &tmp, &tmp) == 0);
|
||||
barrier();
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
barrier();
|
||||
assert(tmp == i);
|
||||
|
||||
barrier();
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
barrier();
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == i);
|
||||
}
|
||||
|
||||
barrier();
|
||||
// if (i >= 7255) kdebug("1e6 !!!!!!! : %d", i);
|
||||
assert(idr_count(&k_idr, i));
|
||||
barrier();
|
||||
}
|
||||
// kdebug("111111");
|
||||
// 正向: M 个ID
|
||||
for (int i = 0; i < M; i++)
|
||||
{
|
||||
@ -194,6 +223,7 @@ static long ktest_idr_case2(uint64_t arg0, uint64_t arg1)
|
||||
idr_remove(&k_idr, i);
|
||||
assert(idr_find(&k_idr, i) == NULL);
|
||||
}
|
||||
// kdebug("22222");
|
||||
|
||||
// 倒序: N-M 个ID
|
||||
for (int i = (N)-1; i >= M; i--)
|
||||
@ -203,11 +233,11 @@ static long ktest_idr_case2(uint64_t arg0, uint64_t arg1)
|
||||
idr_remove(&k_idr, i);
|
||||
assert(idr_find(&k_idr, i) == NULL);
|
||||
}
|
||||
|
||||
// kdebug("3333333");
|
||||
// 重新插入数据
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(tmp == i);
|
||||
assert(k_idr.top != NULL);
|
||||
|
||||
@ -215,19 +245,19 @@ static long ktest_idr_case2(uint64_t arg0, uint64_t arg1)
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == i);
|
||||
}
|
||||
|
||||
// kdebug("4444444444");
|
||||
assert(k_idr.top != NULL);
|
||||
|
||||
for (int i = 0; i < M; i++)
|
||||
{
|
||||
assert(idr_replace(&k_idr, NULL, i) == 0);
|
||||
}
|
||||
|
||||
// kdebug("555555555555555555");
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
// kdebug("666666666666");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -247,7 +277,7 @@ static long ktest_idr_case3(uint64_t arg0, uint64_t arg1)
|
||||
// 获取ID
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(tmp == i);
|
||||
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
@ -333,7 +363,7 @@ static long ktest_idr_case4(uint64_t arg0, uint64_t arg1)
|
||||
int M = N / i, T = M / 3, O = 2 * T;
|
||||
for (int j = 0; j < M; j++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(tmp == j);
|
||||
}
|
||||
|
||||
@ -362,12 +392,14 @@ static long ktest_idr_case4(uint64_t arg0, uint64_t arg1)
|
||||
}
|
||||
|
||||
assert(k_idr.top == NULL);
|
||||
assert(idr_empty(&k_idr));
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
assert(idr_empty(&k_idr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -387,7 +419,7 @@ static long ktest_idr_case5(uint64_t arg0, uint64_t arg1)
|
||||
// 获取128个id
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
@ -414,12 +446,13 @@ static long ktest_idr_case5(uint64_t arg0, uint64_t arg1)
|
||||
// destroy之后,再获取128个id
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_get_new(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(idr_empty(&k_idr));
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
@ -442,65 +475,68 @@ static long ktest_idr_case6(uint64_t arg0, uint64_t arg1)
|
||||
|
||||
DECLARE_IDA(k_ida);
|
||||
ida_init(&k_ida);
|
||||
io_sfence();
|
||||
|
||||
const int N = IDA_FULL * IDR_SIZE + 1;
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int p_id;
|
||||
assert(ida_get_new(&k_ida, &p_id) == 0);
|
||||
assert(p_id == i);
|
||||
int p_id;io_sfence();
|
||||
assert(ida_alloc(&k_ida, &p_id) == 0);io_sfence();
|
||||
assert(p_id == i);io_sfence();
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(ida_count(&k_ida, i) == 1);
|
||||
assert(ida_count(&k_ida, i) == 1);io_sfence();
|
||||
}
|
||||
|
||||
for (int i = N - 1; i >= 0; i--)
|
||||
{
|
||||
ida_remove(&k_ida, i);
|
||||
assert(ida_count(&k_ida, i) == 0);
|
||||
ida_remove(&k_ida, i);io_sfence();
|
||||
assert(ida_count(&k_ida, i) == 0);io_sfence();
|
||||
}
|
||||
|
||||
assert(k_ida.idr.top == NULL);
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int p_id;
|
||||
assert(ida_get_new(&k_ida, &p_id) == 0);
|
||||
assert(p_id == i);
|
||||
int p_id;io_sfence();
|
||||
assert(ida_alloc(&k_ida, &p_id) == 0);io_sfence();
|
||||
assert(p_id == i);io_sfence();
|
||||
}
|
||||
|
||||
assert(k_ida.idr.top != NULL);
|
||||
ida_destroy(&k_ida);
|
||||
assert(k_ida.idr.top == NULL);
|
||||
assert(k_ida.free_list == NULL);
|
||||
assert(k_ida.idr.top != NULL);io_sfence();
|
||||
ida_destroy(&k_ida);io_sfence();
|
||||
assert(k_ida.idr.top == NULL);io_sfence();
|
||||
assert(k_ida.free_list == NULL);io_sfence();
|
||||
assert(ida_empty(&k_ida));io_sfence();
|
||||
|
||||
// 测试destroy之后能否重新获取ID
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int p_id;
|
||||
assert(ida_get_new(&k_ida, &p_id) == 0);
|
||||
assert(p_id == i);
|
||||
int p_id;io_sfence();
|
||||
assert(ida_alloc(&k_ida, &p_id) == 0);io_sfence();
|
||||
assert(p_id == i);io_sfence();
|
||||
}
|
||||
|
||||
for (int i = 0; i < N / 3; i++)
|
||||
{
|
||||
ida_remove(&k_ida, i);
|
||||
assert(ida_count(&k_ida, i) == 0);
|
||||
ida_remove(&k_ida, i);io_sfence();
|
||||
assert(ida_count(&k_ida, i) == 0);io_sfence();
|
||||
}
|
||||
|
||||
for (int i = 2 * N / 3; i < N; i++)
|
||||
{
|
||||
ida_remove(&k_ida, i);
|
||||
assert(ida_count(&k_ida, i) == 0);
|
||||
ida_remove(&k_ida, i);io_sfence();
|
||||
assert(ida_count(&k_ida, i) == 0);io_sfence();
|
||||
}
|
||||
|
||||
assert(k_ida.idr.top != NULL);
|
||||
ida_destroy(&k_ida);
|
||||
assert(k_ida.idr.top == NULL);
|
||||
assert(k_ida.free_list == NULL);
|
||||
assert(k_ida.idr.top != NULL);io_sfence();
|
||||
ida_destroy(&k_ida);io_sfence();
|
||||
assert(k_ida.idr.top == NULL);io_sfence();
|
||||
assert(k_ida.free_list == NULL);io_sfence();
|
||||
assert(ida_empty(&k_ida));io_sfence();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -526,4 +562,6 @@ int ktest_test_idr(void* arg)
|
||||
}
|
||||
kTEST("idr Test done.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma GCC pop_options
|
Reference in New Issue
Block a user