support to adjust fixed_count of ObSmallObjPool

This commit is contained in:
tushicheng
2024-06-24 07:16:25 +00:00
committed by ob-robot
parent 7d3b336fe2
commit 5f52ab8cbb

View File

@ -15,6 +15,7 @@
#include "lib/allocator/ob_small_allocator.h" // ObSmallAllocator #include "lib/allocator/ob_small_allocator.h" // ObSmallAllocator
#include "lib/queue/ob_fixed_queue.h" // ObFixedQueue #include "lib/queue/ob_fixed_queue.h" // ObFixedQueue
#include "lib/list/ob_atomic_list.h"
namespace oceanbase namespace oceanbase
{ {
@ -33,9 +34,11 @@ public:
SRC_ALLOC = 2, // Dynamically allocated elements, released after use SRC_ALLOC = 2, // Dynamically allocated elements, released after use
}; };
void *next_;
int8_t src_type_; int8_t src_type_;
T obj_; T obj_;
ObjItem() : src_type_(SRC_UNKNOWN), obj_() ObjItem() : src_type_(SRC_UNKNOWN), obj_()
{} {}
~ObjItem() ~ObjItem()
@ -53,11 +56,12 @@ public:
public: public:
int alloc(T *&obj); int alloc(T *&obj);
int free(T* obj); int free(T* obj);
void set_fixed_count(int64_t fixed_count) { fixed_count_ = fixed_count; }
int64_t get_free_count() const { return free_count_; } int64_t get_free_count() const { return free_count_; }
int64_t get_alloc_count() const { return alloc_count_; } int64_t get_alloc_count() const { return alloc_count_; }
int64_t get_fixed_count() const { return fixed_count_; } int64_t get_fixed_count() const { return fixed_count_; }
int64_t get_cached_total_count() const { return free_list_.get_total(); } int64_t get_cached_total_count() const { return free_count_; }
int64_t get_cached_free_count() const { return free_list_.get_free(); } int64_t get_cached_free_count() const { return max(0, fixed_count_ - free_count_); }
public: public:
int init(const int64_t fixed_count = DEFAULT_FIXED_COUNT, int init(const int64_t fixed_count = DEFAULT_FIXED_COUNT,
@ -74,7 +78,7 @@ private:
int64_t fixed_count_; int64_t fixed_count_;
int64_t free_count_; int64_t free_count_;
int64_t alloc_count_; int64_t alloc_count_;
ObFixedQueue<ObjItem> free_list_; ObAtomicList free_list_;
ObSmallAllocator allocator_; // Obj allocator ObSmallAllocator allocator_; // Obj allocator
private: private:
@ -114,7 +118,7 @@ int ObSmallObjPool<T>::init(const int64_t fixed_count,
} else if (OB_FAIL(allocator_.init(obj_size, label, tenant_id, block_size))) { } else if (OB_FAIL(allocator_.init(obj_size, label, tenant_id, block_size))) {
LIB_LOG(ERROR, "init small allocator fail", K(ret), K(obj_size), K(label), LIB_LOG(ERROR, "init small allocator fail", K(ret), K(obj_size), K(label),
K(tenant_id), K(block_size)); K(tenant_id), K(block_size));
} else if (OB_FAIL(free_list_.init(fixed_count, global_default_allocator, attr))) { } else if (OB_FAIL(free_list_.init("SmallObjPool", 0))) {
LIB_LOG(ERROR, "init free list fail", K(fixed_count)); LIB_LOG(ERROR, "init free list fail", K(fixed_count));
} else { } else {
fixed_count_ = fixed_count; fixed_count_ = fixed_count;
@ -137,19 +141,16 @@ void ObSmallObjPool<T>::destroy()
inited_ = false; inited_ = false;
ObjItem *obj = NULL; ObjItem *obj = NULL;
while (OB_SUCCESS == free_list_.pop(obj)) { while (OB_NOT_NULL(obj = (ObjItem*)free_list_.pop())) {
if (NULL != obj) { obj->~ObjItem();
obj->~ObjItem(); allocator_.free(obj);
allocator_.free(obj); obj = NULL;
obj = NULL;
}
} }
fixed_count_ = 0; fixed_count_ = 0;
free_count_ = 0; free_count_ = 0;
alloc_count_ = 0; alloc_count_ = 0;
free_list_.destroy();
(void)allocator_.destroy(); (void)allocator_.destroy();
} }
} }
@ -163,22 +164,13 @@ int ObSmallObjPool<T>::alloc(T *&obj)
if (OB_UNLIKELY(! inited_)) { if (OB_UNLIKELY(! inited_)) {
LIB_LOG(ERROR, "small obj pool has not been initialized"); LIB_LOG(ERROR, "small obj pool has not been initialized");
ret = OB_NOT_INIT; ret = OB_NOT_INIT;
} else if (OB_SUCC(free_list_.pop(obj_item))) { } else if (OB_NOT_NULL(obj_item = (ObjItem*)free_list_.pop())) {
if (OB_ISNULL(obj_item)) { obj = &obj_item->obj_;
LIB_LOG(ERROR, "pop obj item from free_list fail", K(ret), K(obj_item)); (void)ATOMIC_AAF(&free_count_, -1);
ret = OB_ERR_UNEXPECTED; } else if (OB_FAIL(alloc_obj_(obj))) {
} else { LIB_LOG(ERROR, "alloc_obj fail", K(ret));
obj = &obj_item->obj_; } else {
(void)ATOMIC_AAF(&free_count_, -1); // succ
}
} else if (OB_ENTRY_NOT_EXIST == ret) {
if (OB_FAIL(alloc_obj_(obj))) {
LIB_LOG(ERROR, "alloc_obj fail", K(ret));
} else {
// succ
}
} else { // OB_SUCCESS != ret && OB_ENTRY_NOT_EXIST != ret
LIB_LOG(ERROR, "pop object from free list fail", K(ret), K_(free_count), K_(alloc_count));
} }
return ret; return ret;
@ -198,11 +190,8 @@ int ObSmallObjPool<T>::free(T* obj)
ObjItem *obj_item = CONTAINER_OF(obj, ObjItem, obj_); ObjItem *obj_item = CONTAINER_OF(obj, ObjItem, obj_);
if (ObjItem::SRC_FIXED == obj_item->src_type_) { if (ObjItem::SRC_FIXED == obj_item->src_type_) {
if (OB_FAIL(free_list_.push(obj_item))) { free_list_.push(obj_item);
LIB_LOG(ERROR, "push obj into free list fail", K(ret), KP(obj_item)); (void)ATOMIC_AAF(&free_count_, 1);
} else {
(void)ATOMIC_AAF(&free_count_, 1);
}
} else if (ObjItem::SRC_ALLOC == obj_item->src_type_) { } else if (ObjItem::SRC_ALLOC == obj_item->src_type_) {
obj_item->~ObjItem(); obj_item->~ObjItem();
allocator_.free(obj_item); allocator_.free(obj_item);