patch 4.0
This commit is contained in:
205
deps/oblib/unittest/lib/alloc/test_object_mgr.cpp
vendored
205
deps/oblib/unittest/lib/alloc/test_object_mgr.cpp
vendored
@ -10,8 +10,12 @@
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#include "lib/allocator/ob_malloc.h"
|
||||
#define private public
|
||||
#include "lib/resource/achunk_mgr.h"
|
||||
#include "lib/resource/ob_resource_mgr.h"
|
||||
#include "lib/alloc/object_mgr.h"
|
||||
#undef private
|
||||
#include "lib/allocator/ob_malloc.h"
|
||||
#include "lib/utility/ob_test_util.h"
|
||||
#include "lib/coro/testing.h"
|
||||
#include <gtest/gtest.h>
|
||||
@ -20,15 +24,17 @@ using namespace oceanbase::lib;
|
||||
using namespace oceanbase::common;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
class TestObjectMgr : public ::testing::Test {
|
||||
class TestObjectMgr
|
||||
: public ::testing::Test
|
||||
{
|
||||
public:
|
||||
void* Malloc(uint64_t size)
|
||||
void *Malloc(uint64_t size)
|
||||
{
|
||||
// void *p = NULL;
|
||||
// AObject *obj = os_.alloc_object(size);
|
||||
@ -39,7 +45,7 @@ public:
|
||||
return oceanbase::common::ob_malloc(size);
|
||||
}
|
||||
|
||||
void Free(void* ptr)
|
||||
void Free(void *ptr)
|
||||
{
|
||||
// AObject *obj = reinterpret_cast<AObject*>(
|
||||
// (char*)ptr - AOBJECT_HEADER_SIZE);
|
||||
@ -47,51 +53,48 @@ public:
|
||||
return oceanbase::common::ob_free(ptr);
|
||||
}
|
||||
|
||||
// protected:
|
||||
// ObjectMgr<1> om_;
|
||||
// protected:
|
||||
// ObjectMgr<1> om_;
|
||||
};
|
||||
|
||||
TEST_F(TestObjectMgr, Basic2)
|
||||
{
|
||||
cotesting::FlexPool(
|
||||
[] {
|
||||
void* p[128] = {};
|
||||
int64_t cnt = 1L << 18;
|
||||
uint64_t sz = 1L << 4;
|
||||
cotesting::FlexPool([] {
|
||||
void *p[128] = {};
|
||||
int64_t cnt = 1L << 18;
|
||||
uint64_t sz = 1L << 4;
|
||||
|
||||
while (cnt--) {
|
||||
int i = 0;
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
while (i--) {
|
||||
ob_free(p[i]);
|
||||
}
|
||||
// sz = ((sz | reinterpret_cast<size_t>(p[0])) & ((1<<13) - 1));
|
||||
}
|
||||
while (cnt--) {
|
||||
int i = 0;
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
p[i++] = ob_malloc(sz);
|
||||
while (i--) {
|
||||
ob_free(p[i]);
|
||||
}
|
||||
// sz = ((sz | reinterpret_cast<size_t>(p[0])) & ((1<<13) - 1));
|
||||
}
|
||||
|
||||
cout << "done" << endl;
|
||||
},
|
||||
4)
|
||||
.start();
|
||||
cout << "done" << endl;
|
||||
}, 4).start();
|
||||
}
|
||||
|
||||
TEST_F(TestObjectMgr, TestName)
|
||||
{
|
||||
void* p[128];
|
||||
void *p[128];
|
||||
|
||||
Malloc(327800);
|
||||
Malloc(65536);
|
||||
@ -155,3 +158,125 @@ TEST_F(TestObjectMgr, TestName)
|
||||
Free(p[19]);
|
||||
Malloc(96);
|
||||
}
|
||||
|
||||
struct Record
|
||||
{
|
||||
int32_t size_;
|
||||
int64_t addr_;
|
||||
};
|
||||
|
||||
AChunk *chunk(void *ptr)
|
||||
{
|
||||
auto *obj = (AObject*)((char*)ptr - AOBJECT_HEADER_SIZE);
|
||||
auto *chunk = obj->block()->chunk();
|
||||
return chunk;
|
||||
}
|
||||
|
||||
TEST_F(TestObjectMgr, TestFragmentWash)
|
||||
{
|
||||
ObTenantResourceMgrHandle resource_handle;
|
||||
ObResourceMgr::get_instance().get_tenant_resource_mgr(
|
||||
OB_SERVER_TENANT_ID, resource_handle);
|
||||
auto ta = ObMallocAllocator::get_instance()->get_tenant_ctx_allocator(
|
||||
OB_SERVER_TENANT_ID, ObCtxIds::DEFAULT_CTX_ID);
|
||||
ta->sync_wash(INT64_MAX);
|
||||
int washed_size = ta->sync_wash(INT64_MAX);
|
||||
ASSERT_EQ(washed_size, 0);
|
||||
|
||||
vector<pair<AChunk*, vector<void*> > > chunk_ptrs;
|
||||
int chunk_cnt = 1000;
|
||||
int alloc_size = ABLOCK_SIZE - 256;
|
||||
do {
|
||||
void *ptr = ob_malloc(alloc_size, "mod");
|
||||
abort_unless(ptr != nullptr);
|
||||
AChunk *cur_chunk = chunk(ptr);
|
||||
vector<void*> ptrs{ptr};
|
||||
do {
|
||||
void *ptr = ob_malloc(alloc_size, "mod");
|
||||
abort_unless(ptr != nullptr);
|
||||
if (chunk(ptr) != cur_chunk) {
|
||||
ob_free(ptr);
|
||||
break;
|
||||
}
|
||||
ptrs.push_back(ptr);
|
||||
} while (true);
|
||||
chunk_ptrs.push_back(make_pair(cur_chunk, ptrs));
|
||||
} while (chunk_cnt--);
|
||||
|
||||
washed_size = ta->sync_wash(INT64_MAX);
|
||||
ASSERT_EQ(washed_size, 0);
|
||||
|
||||
int64_t chunk_mgr_hold_before = AChunkMgr::instance().get_hold();
|
||||
int64_t resource_mgr_hold_before = resource_handle.get_memory_mgr()->get_sum_hold();
|
||||
int free_size = 0;
|
||||
for (int i =0; i< chunk_ptrs.size(); i++) {
|
||||
// keep first ptr
|
||||
for (int j =1; j< chunk_ptrs[i].second.size(); j++) {
|
||||
ob_free(chunk_ptrs[i].second[j]);
|
||||
free_size += alloc_size;
|
||||
}
|
||||
}
|
||||
int64_t rss_size_orig = 0;
|
||||
int64_t rss_size = 0;
|
||||
get_virtual_memory_used(&rss_size_orig);
|
||||
washed_size = ta->sync_wash(INT64_MAX);
|
||||
get_virtual_memory_used(&rss_size);
|
||||
ASSERT_GT(washed_size, free_size);
|
||||
ASSERT_TRUE(rss_size < rss_size_orig &&
|
||||
std::abs(std::abs(rss_size - rss_size_orig) - washed_size) * 1.0 / washed_size < 0.1);
|
||||
|
||||
int64_t chunk_mgr_hold = AChunkMgr::instance().get_hold();
|
||||
int64_t resource_mgr_hold = resource_handle.get_memory_mgr()->get_sum_hold();
|
||||
ASSERT_EQ(resource_mgr_hold_before - washed_size, resource_mgr_hold);
|
||||
ASSERT_EQ(chunk_mgr_hold_before - washed_size, chunk_mgr_hold);
|
||||
|
||||
for (int i =0; i< chunk_ptrs.size(); i++) {
|
||||
ob_free(chunk_ptrs[i].second[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestObjectMgr, TestSubObjectMgr)
|
||||
{
|
||||
AChunkMgr::instance().set_max_chunk_cache_cnt(0);
|
||||
oceanbase::lib::set_memory_limit(20LL<<30);
|
||||
int fd = open("alloc_flow_records", O_RDONLY, S_IRWXU | S_IRGRP);
|
||||
abort_unless(fd > 0);
|
||||
struct stat fileInfo;
|
||||
bzero(&fileInfo, sizeof(fileInfo));
|
||||
int rc = fstat(fd, &fileInfo);
|
||||
abort_unless(rc != -1);
|
||||
int64_t total_size = fileInfo.st_size;
|
||||
void *ptr = ::mmap(0, total_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
abort_unless(ptr != MAP_FAILED);
|
||||
SubObjectMgr som(false);
|
||||
ObMemAttr attr;
|
||||
som.set_tenant_ctx_allocator(*ObMallocAllocator::get_instance()->get_tenant_ctx_allocator(
|
||||
OB_SERVER_TENANT_ID, ObCtxIds::DEFAULT_CTX_ID), attr);
|
||||
ObTenantResourceMgrHandle resource_handle;
|
||||
ObResourceMgr::get_instance().get_tenant_resource_mgr(
|
||||
OB_SERVER_TENANT_ID, resource_handle);
|
||||
map<int64_t, AObject*> allocs;
|
||||
int i = total_size/sizeof(Record);
|
||||
auto *rec = (Record*)ptr;
|
||||
while (i--) {
|
||||
int32_t size = rec->size_;
|
||||
int64_t addr = rec->addr_;
|
||||
if (size != 0) {
|
||||
auto *object = som.alloc_object(size, attr);
|
||||
abort_unless(object != nullptr);
|
||||
allocs.insert(pair<int64_t, AObject*>(addr, object));
|
||||
memset(object->data_, 0xAA, size);
|
||||
} else {
|
||||
auto it = allocs.find(addr);
|
||||
abort_unless(it != allocs.end());
|
||||
AObject *obj = it->second;
|
||||
ABlock *block = obj->block();
|
||||
abort_unless(block->is_valid());
|
||||
ObjectSet *set = block->obj_set_;
|
||||
set->free_object(obj);
|
||||
allocs.erase(it->first);
|
||||
}
|
||||
rec++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user