Skip to content

Commit 433b3de

Browse files
authored
Update memory reclaimer code (#430)
1 parent 80988e4 commit 433b3de

14 files changed

+52
-96
lines changed

velox/common/memory/Memory.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ std::shared_ptr<MemoryPool> MemoryManager::addLeafPool(
149149
return defaultRoot_->addLeafChild(poolName, threadSafe, nullptr);
150150
}
151151

152-
uint64_t MemoryManager::shrinkPool(MemoryPool* pool, uint64_t decrementBytes) {
153-
VELOX_CHECK_NOT_NULL(pool);
154-
if (arbitrator_ == nullptr) {
155-
return pool->shrink(decrementBytes);
156-
}
157-
return arbitrator_->releaseMemory(pool, decrementBytes);
158-
}
159-
160152
bool MemoryManager::growPool(MemoryPool* pool, uint64_t incrementBytes) {
161153
VELOX_CHECK_NOT_NULL(pool);
162154
VELOX_CHECK_NE(pool->capacity(), kMaxMemory);
@@ -175,7 +167,7 @@ void MemoryManager::dropPool(MemoryPool* pool) {
175167
VELOX_FAIL("The dropped memory pool {} not found", pool->name());
176168
}
177169
pools_.erase(it);
178-
arbitrator_->releaseMemory(pool, 0);
170+
arbitrator_->releaseMemory(pool);
179171
}
180172

181173
MemoryPool& MemoryManager::deprecatedSharedLeafPool() {

velox/common/memory/Memory.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ class MemoryManager {
160160
const std::string& name = "",
161161
bool threadSafe = true);
162162

163-
/// Invoked to shrink a memory pool's free capacity with up to
164-
/// 'decrementBytes'.
165-
uint64_t shrinkPool(MemoryPool* pool, uint64_t decrementBytes);
166-
167163
/// Invoked to grows a memory pool's free capacity with at least
168164
/// 'incrementBytes'. The function returns true on success, otherwise false.
169165
bool growPool(MemoryPool* pool, uint64_t incrementBytes);

velox/common/memory/MemoryArbitrator.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ class NoopArbitrator : public MemoryArbitrator {
9595

9696
// Noop arbitrator has no memory capacity limit so no operation needed for
9797
// memory pool capacity release.
98-
uint64_t releaseMemory(MemoryPool* /*unused*/, uint64_t /*unused*/) override {
98+
void releaseMemory(MemoryPool* /*unused*/) override {
9999
// No-op
100-
return 0ULL;
101100
}
102101

103102
// Noop arbitrator has no memory capacity limit so no operation needed for
@@ -154,10 +153,6 @@ void MemoryArbitrator::unregisterFactory(const std::string& kind) {
154153
arbitratorFactories().unregisterFactory(kind);
155154
}
156155

157-
uint64_t MemoryArbitrator::capacity() {
158-
return capacity_;
159-
}
160-
161156
std::unique_ptr<MemoryReclaimer> MemoryReclaimer::create() {
162157
return std::unique_ptr<MemoryReclaimer>(new MemoryReclaimer());
163158
}

velox/common/memory/MemoryArbitrator.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ class MemoryArbitrator {
124124
/// the memory arbitration on demand when actual memory allocation happens.
125125
virtual void reserveMemory(MemoryPool* pool, uint64_t bytes) = 0;
126126

127-
/// Invoked by the memory manager to return back the specified amount of
128-
/// reserved memory capacity of a destroying memory pool. If 0 is specified,
129-
/// release all reserve memory. Returns the actually released amount of bytes.
130-
virtual uint64_t releaseMemory(MemoryPool* pool, uint64_t bytes) = 0;
127+
/// Invoked by the memory manager to return back all the reserved memory
128+
/// capacity of a destroying memory pool.
129+
virtual void releaseMemory(MemoryPool* pool) = 0;
131130

132131
/// Invoked by the memory manager to grow a memory pool's capacity.
133132
/// 'pool' is the memory pool to request to grow. 'candidates' is a list
@@ -153,7 +152,6 @@ class MemoryArbitrator {
153152
const std::vector<std::shared_ptr<MemoryPool>>& pools,
154153
uint64_t targetBytes) = 0;
155154

156-
uint64_t capacity();
157155
/// The internal execution stats of the memory arbitrator.
158156
struct Stats {
159157
/// The number of arbitration requests.

velox/common/memory/MemoryPool.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -759,16 +759,6 @@ bool MemoryPoolImpl::incrementReservationThreadSafe(
759759
treeMemoryUsage()));
760760
}
761761

762-
uint64_t MemoryPoolImpl::shrinkManaged(
763-
MemoryPool* requestor,
764-
uint64_t targetBytes) {
765-
if (parent_ != nullptr) {
766-
return parent_->shrinkManaged(requestor, targetBytes);
767-
}
768-
VELOX_CHECK_NULL(parent_);
769-
return manager_->shrinkPool(requestor, targetBytes);
770-
};
771-
772762
bool MemoryPoolImpl::maybeIncrementReservation(uint64_t size) {
773763
std::lock_guard<std::mutex> l(mutex_);
774764
return maybeIncrementReservationLocked(size);

velox/common/memory/MemoryPool.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,6 @@ class MemoryPool : public std::enable_shared_from_this<MemoryPool> {
366366
/// without actually freeing the used memory.
367367
virtual uint64_t freeBytes() const = 0;
368368

369-
/// Try shrinking up to the specified amount of free memory via memory
370-
/// manager.
371-
virtual uint64_t shrinkManaged(
372-
MemoryPool* requestor,
373-
uint64_t targetBytes = 0) = 0;
374-
375369
/// Invoked to free up to the specified amount of free memory by reducing
376370
/// this memory pool's capacity without actually freeing any used memory. The
377371
/// function returns the actually freed memory capacity in bytes. If
@@ -638,8 +632,6 @@ class MemoryPoolImpl : public MemoryPool {
638632
uint64_t reclaim(uint64_t targetBytes, memory::MemoryReclaimer::Stats& stats)
639633
override;
640634

641-
uint64_t shrinkManaged(MemoryPool* requestor, uint64_t targetBytes) override;
642-
643635
uint64_t shrink(uint64_t targetBytes = 0) override;
644636

645637
uint64_t grow(uint64_t bytes) noexcept override;

velox/common/memory/tests/MemoryArbitratorTest.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ class FakeTestArbitrator : public MemoryArbitrator {
163163
.memoryPoolTransferCapacity = config.memoryPoolTransferCapacity}) {
164164
}
165165

166-
void reserveMemory(MemoryPool* pool, uint64_t bytes) override{VELOX_NYI()}
166+
void reserveMemory(MemoryPool* pool, uint64_t bytes) override {
167+
VELOX_NYI();
168+
}
167169

168-
uint64_t releaseMemory(MemoryPool* pool, uint64_t bytes) override{VELOX_NYI()}
170+
void releaseMemory(MemoryPool* pool) override {
171+
VELOX_NYI();
172+
}
169173

170174
std::string kind() const override {
171175
return "USER";

velox/common/memory/tests/MemoryManagerTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class FakeTestArbitrator : public MemoryArbitrator {
127127
VELOX_NYI();
128128
}
129129

130-
uint64_t releaseMemory(MemoryPool* pool, uint64_t bytes) override {
130+
void releaseMemory(MemoryPool* pool) override {
131131
VELOX_NYI();
132132
}
133133

@@ -211,6 +211,9 @@ TEST_F(MemoryManagerTest, addPoolWithArbitrator) {
211211
options.allocator = allocator.get();
212212
options.capacity = kCapacity;
213213
options.arbitratorKind = arbitratorKind_;
214+
// The arbitrator capacity will be overridden by the memory manager's
215+
// capacity.
216+
options.capacity = options.capacity;
214217
const uint64_t initialPoolCapacity = options.capacity / 32;
215218
options.memoryPoolInitCapacity = initialPoolCapacity;
216219
MemoryManager manager{options};

velox/common/memory/tests/MockSharedArbitratorTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class MockMemoryOperator {
308308
for (const auto& allocation : allocationsToFree) {
309309
pool_->free(allocation.buffer, allocation.size);
310310
}
311-
return pool_->shrinkManaged(pool, targetBytes);
311+
return pool_->shrink(targetBytes);
312312
}
313313

314314
void abort(MemoryPool* pool) {

velox/dwio/dwrf/test/WriterFlushTest.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ class MockMemoryPool : public velox::memory::MemoryPool {
206206
VELOX_UNSUPPORTED("{} unsupported", __FUNCTION__);
207207
}
208208

209-
uint64_t shrinkManaged(MemoryPool* /*unused*/, uint64_t /*unused*/) override {
210-
VELOX_UNSUPPORTED("{} unsupported", __FUNCTION__);
211-
}
212-
213209
uint64_t grow(uint64_t /*unused*/) noexcept override {
214210
VELOX_UNSUPPORTED("{} unsupported", __FUNCTION__);
215211
}

0 commit comments

Comments
 (0)