From e60ca4d26dc49bdeaab1c8aabc73fa8cae405e14 Mon Sep 17 00:00:00 2001 From: obdev Date: Wed, 13 Sep 2023 07:47:36 +0000 Subject: [PATCH] Fix the low utilization of SimpleAllocer --- deps/oblib/src/lib/hash/ob_hashutils.h | 2 + deps/oblib/unittest/lib/CMakeLists.txt | 1 + .../unittest/lib/hash/test_hashutils.cpp | 87 +++++++++++++++++++ src/diagnose/lua/ob_lua_api.cpp | 6 +- .../ob_all_virtual_sys_event.cpp | 2 +- .../virtual_table/ob_all_virtual_sys_stat.cpp | 2 +- .../ob_information_kvcache_table.cpp | 2 +- 7 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 deps/oblib/unittest/lib/hash/test_hashutils.cpp diff --git a/deps/oblib/src/lib/hash/ob_hashutils.h b/deps/oblib/src/lib/hash/ob_hashutils.h index 17eb9f2b69..da983f5776 100644 --- a/deps/oblib/src/lib/hash/ob_hashutils.h +++ b/deps/oblib/src/lib/hash/ob_hashutils.h @@ -1399,6 +1399,8 @@ public: void take_off_from_fl(Block *block) { if (block == block->next) { + abort_unless(block == block_free_list_); + block->prev = block->next = NULL; block_free_list_ = NULL; } else { block->prev->next = block->next; diff --git a/deps/oblib/unittest/lib/CMakeLists.txt b/deps/oblib/unittest/lib/CMakeLists.txt index 25da8ebe85..a5ed3556d3 100644 --- a/deps/oblib/unittest/lib/CMakeLists.txt +++ b/deps/oblib/unittest/lib/CMakeLists.txt @@ -51,6 +51,7 @@ oblib_addtest(hash/test_cuckoo_hashmap.cpp) oblib_addtest(hash/test_hashmap.cpp) oblib_addtest(hash/test_fnv_hash.cpp) oblib_addtest(hash/test_hashset.cpp) +oblib_addtest(hash/test_hashutils.cpp) oblib_addtest(hash/test_iteratable_hashmap.cpp) oblib_addtest(hash/test_iteratable_hashset.cpp) oblib_addtest(hash/test_link_hashmap.cpp) diff --git a/deps/oblib/unittest/lib/hash/test_hashutils.cpp b/deps/oblib/unittest/lib/hash/test_hashutils.cpp new file mode 100644 index 0000000000..76a5ca89db --- /dev/null +++ b/deps/oblib/unittest/lib/hash/test_hashutils.cpp @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2021 OceanBase + * OceanBase CE is licensed under Mulan PubL v2. + * You can use this software according to the terms and conditions of the Mulan PubL v2. + * You may obtain a copy of Mulan PubL v2 at: + * http://license.coscl.org.cn/MulanPubL-2.0 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PubL v2 for more details. + */ + +#include +#include +#include +#include +#include +#include +#define private public +#include "lib/hash/ob_hashset.h" +#include "lib/hash/ob_hashutils.h" +#undef private +#include "lib/allocator/ob_malloc.h" + +#include "gtest/gtest.h" + +using namespace oceanbase; +using namespace common; +using namespace hash; + +class TestHashUtils: public ::testing::Test +{ +public: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +struct MySimpleAllocer +{ +public: + explicit MySimpleAllocer() + { + used_cnt_ = 0; + } + void *alloc(const int64_t sz) + { + used_cnt_++; + return ob_malloc(sz, attr_); + } + void free(void *p) + { + ob_free(p); + used_cnt_--; + } + void set_attr(const ObMemAttr &attr) { attr_ = attr; } + void set_label(const lib::ObLabel &label) { attr_.label_ = label; } + int64_t used_cnt_; + ObMemAttr attr_; +}; + +TEST_F(TestHashUtils, Basic) +{ + static constexpr int NODE_NUM = 2; + using TestAlloc = hash::SimpleAllocer; + TestAlloc alloc; + int *obj = alloc.alloc(); + ASSERT_TRUE(obj != NULL); + int *obj2 = alloc.alloc(); + ASSERT_TRUE(obj2 != NULL); + alloc.free(obj2); + int *obj3 = alloc.alloc(); + ASSERT_EQ(obj3, obj2); + alloc.free(obj3); + int *obj4 = alloc.alloc(); + ASSERT_EQ(obj4, obj3); + alloc.free(obj); + alloc.free(obj4); + ASSERT_EQ(0, alloc.allocer_.used_cnt_); +} + +int main(int argc, char **argv) +{ + oceanbase::common::ObLogger::get_logger().set_log_level("INFO"); + OB_LOGGER.set_log_level("INFO"); + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/src/diagnose/lua/ob_lua_api.cpp b/src/diagnose/lua/ob_lua_api.cpp index 2f327ecd5a..d579834946 100644 --- a/src/diagnose/lua/ob_lua_api.cpp +++ b/src/diagnose/lua/ob_lua_api.cpp @@ -864,7 +864,8 @@ int select_sysstat(lua_State* L) GCTX.omt_->get_tenant_ids(ids); LuaVtableGenerator gen(L, columns); for (int64_t i = 0; i < ids.size() && !gen.is_end(); ++i) { - HEAP_VAR(ObDiagnoseTenantInfo, diag_info) { + ObArenaAllocator diag_allocator; + HEAP_VAR(ObDiagnoseTenantInfo, diag_info, &diag_allocator) { if (OB_FAIL(ObDIGlobalTenantCache::get_instance().get_the_diag_info(ids.at(i), diag_info))) { OB_LOG(ERROR, "failed to get_the_diag_info", K(ids.at(i)), K(ret)); } else if (OB_FAIL(observer::ObAllVirtualSysStat::update_all_stats(ids.at(i), diag_info.get_set_stat_stats()))) { @@ -2016,7 +2017,8 @@ int enable_system_tenant_memory_limit(lua_State* L) int get_tenant_sysstat(int64_t tenant_id, int64_t statistic, int64_t &value) { int ret = OB_SUCCESS; - HEAP_VAR(ObDiagnoseTenantInfo, diag_info) { + ObArenaAllocator diag_allocator; + HEAP_VAR(ObDiagnoseTenantInfo, diag_info, &diag_allocator) { if (statistic < 0 || statistic >= ObStatEventIds::STAT_EVENT_SET_END || ObStatEventIds::STAT_EVENT_ADD_END == statistic) { diff --git a/src/observer/virtual_table/ob_all_virtual_sys_event.cpp b/src/observer/virtual_table/ob_all_virtual_sys_event.cpp index 6074acf3ad..73608c8245 100644 --- a/src/observer/virtual_table/ob_all_virtual_sys_event.cpp +++ b/src/observer/virtual_table/ob_all_virtual_sys_event.cpp @@ -28,7 +28,7 @@ ObAllVirtualSysEvent::ObAllVirtualSysEvent() port_(0), event_iter_(0), tenant_id_(OB_INVALID_ID), - diag_info_() + diag_info_(allocator_) { } diff --git a/src/observer/virtual_table/ob_all_virtual_sys_stat.cpp b/src/observer/virtual_table/ob_all_virtual_sys_stat.cpp index 63e5e1e57c..6893a9b519 100644 --- a/src/observer/virtual_table/ob_all_virtual_sys_stat.cpp +++ b/src/observer/virtual_table/ob_all_virtual_sys_stat.cpp @@ -33,7 +33,7 @@ ObAllVirtualSysStat::ObAllVirtualSysStat() port_(0), stat_iter_(0), tenant_id_(OB_INVALID_TENANT_ID), - diag_info_() + diag_info_(allocator_) { } diff --git a/src/observer/virtual_table/ob_information_kvcache_table.cpp b/src/observer/virtual_table/ob_information_kvcache_table.cpp index dbfba3a0aa..b8b0ace992 100644 --- a/src/observer/virtual_table/ob_information_kvcache_table.cpp +++ b/src/observer/virtual_table/ob_information_kvcache_table.cpp @@ -27,7 +27,7 @@ ObInfoSchemaKvCacheTable::ObInfoSchemaKvCacheTable() cache_iter_(0), str_buf_(), arenallocator_(), - tenant_di_info_(), + tenant_di_info_(allocator_), tenant_dis_() { }