Skip to content

Commit 28710fe

Browse files
authored
Improve accuracy of alloc metrics with freelists (#12268)
1 parent 218e736 commit 28710fe

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

include/iocore/eventsystem/ProxyAllocator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ thread_alloc(CAlloc &a, ProxyAllocator &l, Args &&...args)
5757
void *v = l.freelist;
5858
l.freelist = *reinterpret_cast<void **>(l.freelist);
5959
--(l.allocated);
60+
#if TS_USE_ALLOCATOR_METRICS
61+
a.increment_for_alloc();
62+
#endif
6063
::new (v) typename CAlloc::Value_type(std::forward<Args>(args)...);
6164
return static_cast<typename CAlloc::Value_type *>(v);
6265
}
@@ -84,6 +87,12 @@ void thread_freeup(Allocator &a, ProxyAllocator &l);
8487
8588
#endif
8689

90+
#if TS_USE_ALLOCATOR_METRICS
91+
#define UPDATE_FREE_METRICS(_a) _a.increment_for_free()
92+
#else
93+
#define UPDATE_FREE_METRICS(_a)
94+
#endif
95+
8796
#define THREAD_FREE(_p, _a, _tin) \
8897
do { \
8998
::_a.destroy_if_enabled(_p); \
@@ -92,6 +101,7 @@ void thread_freeup(Allocator &a, ProxyAllocator &l);
92101
*(char **)_p = (char *)_t->_a.freelist; \
93102
_t->_a.freelist = _p; \
94103
_t->_a.allocated++; \
104+
UPDATE_FREE_METRICS(::_a); \
95105
if (thread_freelist_high_watermark > 0 && _t->_a.allocated > thread_freelist_high_watermark) \
96106
thread_freeup(::_a.raw(), _t->_a); \
97107
} else { \

include/tscore/Allocator.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,27 +227,43 @@ template <typename WrappedAllocator> class MeteredAllocator : public WrappedAllo
227227
void *
228228
alloc_void()
229229
{
230-
inuse_metric->increment(1);
231-
alloc_metric->increment(1);
230+
increment_for_alloc();
232231
return WrappedAllocator::alloc_void();
233232
}
234233

235234
void
236235
free_void(void *ptr)
237236
{
238-
inuse_metric->decrement(1);
239-
free_metric->increment(1);
237+
increment_for_free();
240238
WrappedAllocator::free_void(ptr);
241239
}
242240

243241
void
244242
free_void_bulk(void *head, void *tail, size_t num_item)
245243
{
246-
inuse_metric->decrement(num_item);
247-
free_metric->increment(num_item);
244+
// this is only currently called from ProxyAllocator to free
245+
// back to the watermark, so if this is called, it means
246+
// the ProxyAllocator free has been decrementing the free count
247+
// for items on its freelist so this function should not update metrics
248248
WrappedAllocator::free_void_bulk(head, tail, num_item);
249249
}
250250

251+
// NOTE(cmcfarlen): These metric functions are also called
252+
// by ProxyAllocator when items are added/removed from its freelist
253+
void
254+
increment_for_alloc()
255+
{
256+
inuse_metric->increment(1);
257+
alloc_metric->increment(1);
258+
}
259+
260+
void
261+
increment_for_free(int val = 1)
262+
{
263+
inuse_metric->decrement(val);
264+
free_metric->increment(val);
265+
}
266+
251267
MeteredAllocator() {}
252268

253269
MeteredAllocator(const char *name, unsigned int element_size, unsigned int chunk_size = 128, unsigned int alignment = 8,

0 commit comments

Comments
 (0)