Skip to content

Commit

Permalink
Merge pull request #51 from AntelopeIO/reclaimable_memory
Browse files Browse the repository at this point in the history
expose amount of memory in `chainbase_node_allocator`'s free list
  • Loading branch information
spoonincode authored Aug 13, 2024
2 parents 5275ccc + ce64753 commit 60757ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions include/chainbase/chainbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ namespace chainbase {
virtual void undo_all()const = 0;
virtual uint32_t type_id()const = 0;
virtual uint64_t row_count()const = 0;
virtual size_t freelist_memory_usage()const = 0;
virtual const std::string& type_name()const = 0;
virtual std::pair<uint64_t, uint64_t> undo_stack_revision_range()const = 0;

Expand Down Expand Up @@ -190,6 +191,7 @@ namespace chainbase {
virtual void undo_all() const override {_base.undo_all(); }
virtual uint32_t type_id()const override { return BaseIndex::value_type::type_id; }
virtual uint64_t row_count()const override { return _base.indices().size(); }
virtual size_t freelist_memory_usage() const override { return _base.freelist_memory_usage(); }
virtual const std::string& type_name() const override { return BaseIndex_name; }
virtual std::pair<uint64_t, uint64_t> undo_stack_revision_range()const override { return _base.undo_stack_revision_range(); }

Expand Down Expand Up @@ -398,6 +400,16 @@ namespace chainbase {
return _db_file.get_segment_manager()->get_free_memory();
}

size_t get_reclaimable_memory() const {
size_t ret = 0;
for(const unique_ptr<abstract_index>& ai_ptr : _index_map) {
if(!ai_ptr)
continue;
ret += ai_ptr->freelist_memory_usage();
}
return ret;
}

template<typename MultiIndexType>
const generic_index<MultiIndexType>& get_index()const
{
Expand Down
10 changes: 8 additions & 2 deletions include/chainbase/chainbase_node_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace chainbase {
list_item* result = &*_freelist;
_freelist = _freelist->_next;
result->~list_item();
--_freelist_size;
return pointer{(T*)result};
} else {
return pointer{(T*)_manager->allocate(num*sizeof(T))};
Expand All @@ -34,22 +35,26 @@ namespace chainbase {
void deallocate(const pointer& p, std::size_t num) {
if (num == 1) {
_freelist = new (&*p) list_item{_freelist};
++_freelist_size;
} else {
_manager->deallocate(&*p);
}
}
bool operator==(const chainbase_node_allocator& other) const { return this == &other; }
bool operator!=(const chainbase_node_allocator& other) const { return this != &other; }
segment_manager* get_segment_manager() const { return _manager.get(); }
size_t freelist_memory_usage() const { return _freelist_size * sizeof(T); }
private:
template<typename T2, typename S2>
friend class chainbase_node_allocator;
void get_some() {
static_assert(sizeof(T) >= sizeof(list_item), "Too small for free list");
static_assert(sizeof(T) % alignof(list_item) == 0, "Bad alignment for free list");
char* result = (char*)_manager->allocate(sizeof(T) * 64);
const unsigned allocation_batch_size = 64;
char* result = (char*)_manager->allocate(sizeof(T) * allocation_batch_size);
_freelist_size += allocation_batch_size;
_freelist = bip::offset_ptr<list_item>{(list_item*)result};
for(int i = 0; i < 63; ++i) {
for(int i = 0; i < allocation_batch_size-1; ++i) {
char* next = result + sizeof(T);
new(result) list_item{bip::offset_ptr<list_item>{(list_item*)next}};
result = next;
Expand All @@ -59,6 +64,7 @@ namespace chainbase {
struct list_item { bip::offset_ptr<list_item> _next; };
bip::offset_ptr<segment_manager> _manager;
bip::offset_ptr<list_item> _freelist{};
size_t _freelist_size = 0;
};

} // namepsace chainbase
4 changes: 4 additions & 0 deletions include/chainbase/undo_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ namespace chainbase {
compress_impl(_undo_stack.back());
}

size_t freelist_memory_usage() const {
return _allocator.freelist_memory_usage() + _old_values_allocator.freelist_memory_usage();
}

private:

// Removes elements of the last undo session that would be redundant
Expand Down

0 comments on commit 60757ff

Please sign in to comment.