speed up aral when a single item is allocated and freed repeateadly (#19660)
Costa Tsaousis committed
Feb 17, 2025 at 18:15 UTC
c19ae2acbda26d85b017879cb0829f71225a7e55
1 file changed
+22
-8
src/libnetdata/aral/aral.c
+22
-8
@@ -888,7 +888,9 @@ static inline void aral_add_free_slot___no_lock_required(ARAL *ar, ARAL_PAGE *pa
888
ARAL_FREE *fr = (ARAL_FREE *)ptr;
889
fr->size = ar->config.element_size;
890
891
- size_t start = gettid_cached() % ARAL_PAGE_INCOMING_PARTITIONS;
891
+ // use the slot id of the item to be freed to determine the partition number
892
+ size_t start = (((uint8_t *)ptr - page->data) / ar->config.element_size) % ARAL_PAGE_INCOMING_PARTITIONS;
893
+
894
while (true) {
895
for (size_t partition = start; partition < ARAL_PAGE_INCOMING_PARTITIONS; partition++) {
896
if (aral_page_incoming_trylock(ar, page, partition)) {
@@ -1026,19 +1028,31 @@ void aral_freez_internal(ARAL *ar, void *ptr TRACE_ALLOCATIONS_FUNCTION_DEFINITI
1028
1029
// release it
1030
if(unlikely(aral_page_release(page))) {
1029
- __atomic_sub_fetch(&ar->ops[idx].atomic.deallocators, 1, __ATOMIC_RELAXED);
1030
-
1031
internal_fatal(page->page_lock.used_elements, "page has used elements but has been acquired for deletion");
1032
internal_fatal(page->page_lock.marked_elements, "page has marked elements but not used ones");
1033
1034
aral_lock(ar);
1035
internal_fatal(!is_page_in_list(*page->aral_lock.head_ptr, page), "Page is not in this list");
1036
- DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(*page->aral_lock.head_ptr, page, aral_lock.prev, aral_lock.next);
1037
- aral_unlock(ar);
1036
1039
- aral_page_unlock(ar, page);
1040
- aral_del_page___no_lock_needed(ar, page TRACE_ALLOCATIONS_FUNCTION_CALL_PARAMS);
1041
- return;
1037
+ if(*page->aral_lock.head_ptr != page || page->aral_lock.prev != page || page->aral_lock.next != NULL) {
1038
+ // there are more pages with free items - delete it
1039
+ DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(*page->aral_lock.head_ptr, page, aral_lock.prev, aral_lock.next);
1040
+ aral_unlock(ar);
1041
+ aral_page_unlock(ar, page);
1042
+ __atomic_sub_fetch(&ar->ops[idx].atomic.deallocators, 1, __ATOMIC_RELAXED);
1043
+ aral_del_page___no_lock_needed(ar, page TRACE_ALLOCATIONS_FUNCTION_CALL_PARAMS);
1044
+ return;
1045
+ }
1046
+
1047
+ // this is the last page with free items - keep it
1048
+ page->available.list = NULL;
1049
+ page->incoming_partition_bitmap = 0;
1050
+ for(size_t p = 0; p < ARAL_PAGE_INCOMING_PARTITIONS; p++)
1051
+ page->incoming[p].list = NULL;
1052
+
1053
+ __atomic_store_n(&page->elements_segmented, 0, __ATOMIC_RELEASE);
1054
+ __atomic_store_n(&page->refcount, 0, __ATOMIC_RELAXED);
1055
+ aral_unlock(ar);
1056
}
1057
else if(unlikely(unmark)) {
1058
aral_lock(ar);