Skip to content

Commit d478385

Browse files
YQL-18327 Disable yellow zone of allocator on 50%. Update yellow zone on free (#4298)
1 parent 9b85674 commit d478385

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

ydb/library/yql/minikql/aligned_page_pool.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ void TAlignedPagePoolImpl<T>::ReturnBlock(void* ptr, size_t size) noexcept {
379379
Y_DEBUG_ABORT_UNLESS(ActiveBlocks.erase(ptr));
380380
}
381381
#endif
382+
UpdateMemoryYellowZone();
382383
}
383384

384385
template<typename T>
@@ -500,6 +501,19 @@ void TAlignedPagePoolImpl<T>::Free(void* ptr, size_t size) noexcept {
500501
}
501502
}
502503

504+
template<typename T>
505+
void TAlignedPagePoolImpl<T>::UpdateMemoryYellowZone() {
506+
if (IncreaseMemoryLimitCallback) return;
507+
if (Limit == 0) return;
508+
509+
ui8 usedMemoryPercent = 100 * GetUsed() / Limit;
510+
if (usedMemoryPercent >= EnableMemoryYellowZoneThreshold) {
511+
IsMemoryYellowZoneReached = true;
512+
} else if (usedMemoryPercent <= DisableMemoryYellowZoneThreshold) {
513+
IsMemoryYellowZoneReached = false;
514+
}
515+
}
516+
503517
template<typename T>
504518
bool TAlignedPagePoolImpl<T>::TryIncreaseLimit(ui64 required) {
505519
if (!IncreaseMemoryLimitCallback) {

ydb/library/yql/minikql/aligned_page_pool.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,7 @@ class TAlignedPagePoolImpl {
237237
UpdateMemoryYellowZone();
238238
}
239239

240-
void UpdateMemoryYellowZone() {
241-
if (IncreaseMemoryLimitCallback) return;
242-
243-
if (Limit != 0) {
244-
IsMemoryYellowZoneReached = (100 * GetUsed() / Limit) > MemoryYellowZoneThreshold;
245-
}
246-
}
240+
void UpdateMemoryYellowZone();
247241

248242
bool TryIncreaseLimit(ui64 required);
249243

@@ -279,8 +273,10 @@ class TAlignedPagePoolImpl {
279273
// Indicates when memory limit is almost reached.
280274
bool IsMemoryYellowZoneReached = false;
281275
// This theshold is used to determine is memory limit is almost reached.
282-
// If TIncreaseMemoryLimitCallback is set this threshold should be ignored.
283-
const ui8 MemoryYellowZoneThreshold = 80;
276+
// If TIncreaseMemoryLimitCallback is set this thresholds should be ignored.
277+
// The yellow zone turns on when memory consumption reaches 80% and turns off when consumption drops below 50%.
278+
const ui8 EnableMemoryYellowZoneThreshold = 80;
279+
const ui8 DisableMemoryYellowZoneThreshold = 50;
284280
};
285281

286282
using TAlignedPagePool = TAlignedPagePoolImpl<>;

ydb/library/yql/minikql/aligned_page_pool_ut.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,49 @@ Y_UNIT_TEST(UnalignedMmapUnalignedSize) {
131131
);
132132
}
133133

134+
Y_UNIT_TEST(YellowZoneSwitchesCorrectlyBlock) {
135+
TAlignedPagePool::ResetGlobalsUT();
136+
TAlignedPagePoolImpl alloc(__LOCATION__);
137+
138+
// choose relatively big chunk so ALLOC_AHEAD_PAGES don't affect the correctness of the test
139+
auto size = 1024 * TAlignedPagePool::POOL_PAGE_SIZE;
140+
141+
alloc.SetLimit(size * 10);
142+
143+
// 50% allocated -> no yellow zone
144+
auto block1 = alloc.GetBlock(size * 5);
145+
UNIT_ASSERT_VALUES_EQUAL(false, alloc.IsMemoryYellowZoneEnabled());
146+
147+
// 70% allocated -> no yellow zone
148+
auto block2 = alloc.GetBlock(size * 2);
149+
UNIT_ASSERT_VALUES_EQUAL(false, alloc.IsMemoryYellowZoneEnabled());
150+
151+
// 90% allocated -> yellow zone is enabled (> 80%)
152+
auto block3 = alloc.GetBlock(size * 2);
153+
UNIT_ASSERT_VALUES_EQUAL(true, alloc.IsMemoryYellowZoneEnabled());
154+
155+
// 70% allocated -> yellow zone is still enabled (> 50%)
156+
alloc.ReturnBlock(block3, size * 2);
157+
UNIT_ASSERT_VALUES_EQUAL(true, alloc.IsMemoryYellowZoneEnabled());
158+
159+
// 50% allocated -> yellow zone is disabled
160+
alloc.ReturnBlock(block2, size * 2);
161+
UNIT_ASSERT_VALUES_EQUAL(false, alloc.IsMemoryYellowZoneEnabled());
162+
163+
// 0% allocated -> yellow zone is disabled
164+
alloc.ReturnBlock(block1, size * 5);
165+
UNIT_ASSERT_VALUES_EQUAL(false, alloc.IsMemoryYellowZoneEnabled());
166+
}
167+
168+
Y_UNIT_TEST(YellowZoneZeroDivision) {
169+
TAlignedPagePool::ResetGlobalsUT();
170+
TAlignedPagePoolImpl alloc(__LOCATION__);
171+
172+
alloc.SetLimit(0);
173+
174+
UNIT_ASSERT_EQUAL(false, alloc.IsMemoryYellowZoneEnabled());
175+
}
176+
134177
} // Y_UNIT_TEST_SUITE(TAlignedPagePoolTest)
135178

136179
} // namespace NMiniKQL

0 commit comments

Comments
 (0)