@cryptotaxi247 / netdata-1 / commits / d69dee73e

fix aral memory accounting (#19308)

fix aral memory accountig

Costa Tsaousis committed Jan 1, 2025 at 18:50 UTC d69dee73eb78fe738123c2919d028d126739be7f
2 files changed +17 -14
src/daemon/pulse/pulse-daemon-memory.c
+1 -1
@@ -131,7 +131,7 @@ void pulse_daemon_memory_do(bool extended) {
131 (collected_number)dictionary_stats_memory_total(dictionary_stats_category_replication) + (collected_number)replication_sender_allocated_memory());
132 #else
133 uint64_t metadata =
134 - aral_by_size_structures_bytes() + aral_by_size_used_bytes() +
134 + aral_by_size_used_bytes() +
135 dictionary_stats_category_rrdhost.memory.dict + dictionary_stats_category_rrdhost.memory.index +
136 dictionary_stats_category_rrdset.memory.dict + dictionary_stats_category_rrdset.memory.index +
137 dictionary_stats_category_rrddim.memory.dict + dictionary_stats_category_rrddim.memory.index +
src/libnetdata/aral/aral.c
+16 -13
@@ -137,6 +137,20 @@ const char *aral_name(ARAL *ar) {
137 return ar->config.name;
138 }
139
140 +static inline void aral_element_given(ARAL *ar, ARAL_PAGE *page) {
141 + if(ar->config.mmap.enabled || page->mapped)
142 + __atomic_add_fetch(&ar->stats->mmap.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
143 + else
144 + __atomic_add_fetch(&ar->stats->malloc.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
145 +}
146 +
147 +static inline void aral_element_returned(ARAL *ar, ARAL_PAGE *page) {
148 + if(ar->config.mmap.enabled || page->mapped)
149 + __atomic_sub_fetch(&ar->stats->mmap.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
150 + else
151 + __atomic_sub_fetch(&ar->stats->malloc.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
152 +}
153 +
154 size_t aral_structures_bytes_from_stats(struct aral_statistics *stats) {
155 if(!stats) return 0;
156 return __atomic_load_n(&stats->structures.allocated_bytes, __ATOMIC_RELAXED);
@@ -800,10 +814,7 @@ static void *aral_get_free_slot___no_lock_required(ARAL *ar, ARAL_PAGE *page, bo
814 // put the page pointer after the element
815 aral_set_page_pointer_after_element___do_NOT_have_aral_lock(ar, page, found_fr, marked);
816
803 - if(unlikely(ar->config.mmap.enabled || page->mapped))
804 - __atomic_add_fetch(&ar->stats->mmap.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
805 - else
806 - __atomic_add_fetch(&ar->stats->malloc.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
817 + aral_element_given(ar, page);
818
819 return found_fr;
820 }
@@ -816,11 +827,6 @@ static inline void aral_add_free_slot___no_lock_required(ARAL *ar, ARAL_PAGE *pa
827 fr->next = page->incoming.list;
828 page->incoming.list = fr;
829 aral_page_incoming_unlock(ar, page);
819 -
820 - if(unlikely(ar->config.mmap.enabled || page->mapped))
821 - __atomic_sub_fetch(&ar->stats->mmap.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
822 - else
823 - __atomic_sub_fetch(&ar->stats->malloc.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
830 }
831
832 void *aral_callocz_internal(ARAL *ar, bool marked TRACE_ALLOCATIONS_FUNCTION_DEFINITION_PARAMS) {
@@ -905,10 +911,7 @@ void aral_freez_internal(ARAL *ar, void *ptr TRACE_ALLOCATIONS_FUNCTION_DEFINITI
911 size_t idx = mark_to_idx(marked);
912 __atomic_add_fetch(&ar->ops[idx].atomic.deallocators, 1, __ATOMIC_RELAXED);
913
908 - if(unlikely(ar->config.mmap.enabled || page->mapped))
909 - __atomic_sub_fetch(&ar->stats->mmap.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
910 - else
911 - __atomic_sub_fetch(&ar->stats->malloc.used_bytes, ar->config.requested_element_size, __ATOMIC_RELAXED);
914 + aral_element_returned(ar, page);
915
916 // make this element available
917 aral_add_free_slot___no_lock_required(ar, page, ptr);