add double-destroy check for MemoryContext
This commit is contained in:
81
deps/oblib/unittest/lib/rc/test_context.cpp
vendored
81
deps/oblib/unittest/lib/rc/test_context.cpp
vendored
@ -11,12 +11,12 @@
|
||||
*/
|
||||
|
||||
#define USING_LOG_PREFIX SHARE
|
||||
#include <gtest/gtest.h>
|
||||
#define private public
|
||||
#define protected public
|
||||
#include "lib/rc/context.h"
|
||||
#undef private
|
||||
#undef protected
|
||||
#include <gtest/gtest.h>
|
||||
#include "lib/alloc/ob_malloc_allocator.h"
|
||||
#include "lib/thread_local/ob_tsi_factory.h"
|
||||
#include "lib/alloc/memory_dump.h"
|
||||
@ -46,10 +46,9 @@ TEST_F(TestContext, Basic)
|
||||
// There must be a Flow pointing to root on each thread
|
||||
auto& context = Flow::current_ctx();
|
||||
auto& flow = Flow::current_flow();
|
||||
ASSERT_EQ(&MemoryContext::root(), &context);
|
||||
ASSERT_TRUE(flow.prev_ == flow.next_ && flow.prev_ == nullptr);
|
||||
ASSERT_TRUE(context.tree_node_.parent_ == context.tree_node_.child_ &&
|
||||
context.tree_node_.parent_ == context.tree_node_.next_ && context.tree_node_.parent_ == nullptr);
|
||||
ASSERT_EQ(MemoryContext::root(), context);
|
||||
ASSERT_TRUE(context->tree_node_.parent_ == context->tree_node_.child_ &&
|
||||
context->tree_node_.parent_ == context->tree_node_.next_ && context->tree_node_.parent_ == nullptr);
|
||||
uint64_t tenant_id = 1001;
|
||||
uint64_t ctx_id = ObCtxIds::WORK_AREA;
|
||||
ObMallocAllocator* ma = ObMallocAllocator::get_instance();
|
||||
@ -59,16 +58,16 @@ TEST_F(TestContext, Basic)
|
||||
ObPageManager::set_thread_local_instance(g_pm);
|
||||
g_pm.set_tenant_ctx(tenant_id, ctx_id);
|
||||
g_pm.set_max_chunk_cache_cnt(0);
|
||||
MemoryContext& root = MemoryContext::root();
|
||||
MemoryContext root = MemoryContext::root();
|
||||
ContextParam param;
|
||||
param.set_mem_attr(tenant_id, 0, ctx_id);
|
||||
ContextTLOptGuard guard(true);
|
||||
param.properties_ = USE_TL_PAGE_OPTIONAL;
|
||||
MemoryContext* mem_context = nullptr;
|
||||
int ret = root.CREATE_CONTEXT(mem_context, param);
|
||||
param.set_properties(USE_TL_PAGE_OPTIONAL);
|
||||
MemoryContext mem_context = nullptr;
|
||||
int ret = root->CREATE_CONTEXT(mem_context, param);
|
||||
ASSERT_EQ(OB_SUCCESS, ret);
|
||||
ASSERT_EQ(&mem_context->get_allocator(), &mem_context->get_arena_allocator());
|
||||
int64_t used = g_pm.used_;
|
||||
int64_t used = g_pm.get_used();
|
||||
ASSERT_EQ(0, used);
|
||||
void* ptr = nullptr;
|
||||
WITH_CONTEXT(mem_context)
|
||||
@ -76,7 +75,7 @@ TEST_F(TestContext, Basic)
|
||||
ptr = ctxalp(100);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
MEMSET(ptr, 0, 100);
|
||||
ASSERT_GT(g_pm.used_, used);
|
||||
ASSERT_GT(g_pm.get_used(), used);
|
||||
|
||||
auto& P_MCTX = CURRENT_CONTEXT;
|
||||
auto& P_MFLOW = Flow::current_flow();
|
||||
@ -91,18 +90,16 @@ TEST_F(TestContext, Basic)
|
||||
CREATE_WITH_TEMP_CONTEXT_P(true, param)
|
||||
{
|
||||
ASSERT_NE(&CURRENT_CONTEXT, &P_MCTX);
|
||||
ASSERT_EQ(Flow::current_flow().prev_, &P_MFLOW);
|
||||
ASSERT_EQ(Flow::current_flow().next_, nullptr);
|
||||
int64_t p_hold = P_MCTX.hold();
|
||||
int64_t hold = CURRENT_CONTEXT.hold();
|
||||
int64_t p_hold = P_MCTX->hold();
|
||||
int64_t hold = CURRENT_CONTEXT->hold();
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
ptr = ctxalp(100);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
}
|
||||
ASSERT_GT(g_pm.used_, used);
|
||||
ASSERT_EQ(p_hold, P_MCTX.hold());
|
||||
ASSERT_LT(hold, CURRENT_CONTEXT.hold());
|
||||
int64_t orig_pm_used = g_pm.used_;
|
||||
ASSERT_GT(g_pm.get_used(), used);
|
||||
ASSERT_EQ(p_hold, P_MCTX->hold());
|
||||
ASSERT_LT(hold, CURRENT_CONTEXT->hold());
|
||||
int64_t orig_pm_used = g_pm.get_used();
|
||||
has_unfree = false;
|
||||
CREATE_WITH_TEMP_CONTEXT(param)
|
||||
{
|
||||
@ -111,16 +108,16 @@ TEST_F(TestContext, Basic)
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
ptr = ctxalf(100);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
ObArenaAllocator& arena_alloc = CURRENT_CONTEXT.get_arena_allocator();
|
||||
ObArenaAllocator& arena_alloc = CURRENT_CONTEXT->get_arena_allocator();
|
||||
ptr = arena_alloc.alloc(1024);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
ObIAllocator& alloc = CURRENT_CONTEXT.get_malloc_allocator();
|
||||
int64_t ori_used = CURRENT_CONTEXT.used();
|
||||
ObIAllocator& alloc = CURRENT_CONTEXT->get_malloc_allocator();
|
||||
int64_t ori_used = CURRENT_CONTEXT->used();
|
||||
ptr = alloc.alloc(1024);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
ASSERT_GT(CURRENT_CONTEXT.used(), ori_used);
|
||||
ASSERT_GT(CURRENT_CONTEXT->used(), ori_used);
|
||||
alloc.free(ptr);
|
||||
ASSERT_EQ(CURRENT_CONTEXT.used(), ori_used);
|
||||
ASSERT_EQ(CURRENT_CONTEXT->used(), ori_used);
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -128,7 +125,7 @@ TEST_F(TestContext, Basic)
|
||||
ASSERT_TRUE(false);
|
||||
}
|
||||
ASSERT_TRUE(has_unfree);
|
||||
ASSERT_EQ(orig_pm_used, g_pm.used_);
|
||||
ASSERT_EQ(orig_pm_used, g_pm.get_used());
|
||||
CREATE_WITH_TEMP_CONTEXT(param)
|
||||
{
|
||||
{
|
||||
@ -140,10 +137,10 @@ TEST_F(TestContext, Basic)
|
||||
ctxfree(ptr);
|
||||
}
|
||||
int sub_cnt = 8;
|
||||
MemoryContext* subs[sub_cnt];
|
||||
MemoryContext subs[sub_cnt];
|
||||
for (int i = 0; i < sub_cnt; ++i) {
|
||||
param.properties_ = USE_TL_PAGE_OPTIONAL | RETURN_MALLOC_DEFAULT;
|
||||
ret = CURRENT_CONTEXT.CREATE_CONTEXT(subs[i], param);
|
||||
param.set_properties(USE_TL_PAGE_OPTIONAL | RETURN_MALLOC_DEFAULT);
|
||||
ret = CURRENT_CONTEXT->CREATE_CONTEXT(subs[i], param);
|
||||
ASSERT_EQ(OB_SUCCESS, ret);
|
||||
ASSERT_EQ(&subs[i]->get_allocator(), &subs[i]->get_malloc_allocator());
|
||||
ptr = subs[i]->allocp(100);
|
||||
@ -152,10 +149,10 @@ TEST_F(TestContext, Basic)
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
WITH_CONTEXT(subs[i])
|
||||
{
|
||||
ASSERT_EQ(subs[i], &CURRENT_CONTEXT);
|
||||
ASSERT_EQ(subs[i], CURRENT_CONTEXT);
|
||||
ptr = ctxalp(100);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
ptr = CURRENT_CONTEXT.get_arena_allocator().alloc(100);
|
||||
ptr = CURRENT_CONTEXT->get_arena_allocator().alloc(100);
|
||||
ASSERT_NE(ptr, nullptr);
|
||||
}
|
||||
else
|
||||
@ -163,17 +160,17 @@ TEST_F(TestContext, Basic)
|
||||
ASSERT_TRUE(false);
|
||||
}
|
||||
}
|
||||
ASSERT_GT(g_pm.used_, orig_pm_used);
|
||||
int64_t pm_used_before = g_pm.used_;
|
||||
ASSERT_GT(g_pm.get_used(), orig_pm_used);
|
||||
int64_t pm_used_before = g_pm.get_used();
|
||||
for (int i = 0; i < sub_cnt / 2; ++i) {
|
||||
DESTROY_CONTEXT(subs[i]);
|
||||
ASSERT_LT(g_pm.used_, pm_used_before);
|
||||
pm_used_before = g_pm.used_;
|
||||
ASSERT_LT(g_pm.get_used(), pm_used_before);
|
||||
pm_used_before = g_pm.get_used();
|
||||
}
|
||||
ASSERT_GT(g_pm.used_, orig_pm_used);
|
||||
ASSERT_GT(g_pm.get_used(), orig_pm_used);
|
||||
// check child num
|
||||
int child_cnt = 0;
|
||||
for (auto cur = CURRENT_CONTEXT.tree_node_.child_; cur; cur = cur->next_, child_cnt++)
|
||||
for (auto cur = CURRENT_CONTEXT->tree_node_.child_; cur; cur = cur->next_, child_cnt++)
|
||||
;
|
||||
ASSERT_EQ(child_cnt, sub_cnt / 2);
|
||||
}
|
||||
@ -181,7 +178,7 @@ TEST_F(TestContext, Basic)
|
||||
{
|
||||
ASSERT_TRUE(false);
|
||||
}
|
||||
ASSERT_EQ(g_pm.used_, orig_pm_used);
|
||||
ASSERT_EQ(g_pm.get_used(), orig_pm_used);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -192,9 +189,9 @@ TEST_F(TestContext, Basic)
|
||||
{
|
||||
ASSERT_TRUE(false);
|
||||
}
|
||||
ASSERT_GT(g_pm.used_, used);
|
||||
ASSERT_GT(g_pm.get_used(), used);
|
||||
DESTROY_CONTEXT(mem_context);
|
||||
ASSERT_EQ(g_pm.used_, used);
|
||||
ASSERT_EQ(g_pm.get_used(), used);
|
||||
|
||||
{
|
||||
get_mem_leak_checker().init();
|
||||
@ -222,7 +219,6 @@ TEST_F(TestContext, Basic)
|
||||
|
||||
// test rate
|
||||
reset_mem_leak_checker_label("test2");
|
||||
reset_mem_leak_checker_rate(10);
|
||||
int i = 20;
|
||||
while (i--) {
|
||||
ob_malloc(100, "test2");
|
||||
@ -230,15 +226,14 @@ TEST_F(TestContext, Basic)
|
||||
get_mem_leak_checker().print();
|
||||
|
||||
usleep(1000 * 1000);
|
||||
reset_mem_leak_checker_rate(20);
|
||||
i = 20;
|
||||
while (i--) {
|
||||
ob_malloc(100, "test2");
|
||||
}
|
||||
get_mem_leak_checker().print();
|
||||
|
||||
MemoryContext* ty = nullptr;
|
||||
int ret = root.CREATE_CONTEXT(ty, param);
|
||||
MemoryContext ty = nullptr;
|
||||
int ret = root->CREATE_CONTEXT(ty, param);
|
||||
reset_mem_leak_checker_label("*@7");
|
||||
ty->allocf(100, ObMemAttr(1, "test"));
|
||||
ty->allocf(103, ObMemAttr(1, 1));
|
||||
|
||||
Reference in New Issue
Block a user