Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
zhztheplayer committed Jul 24, 2024
1 parent 8325fda commit dbf95b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
18 changes: 12 additions & 6 deletions cpp/core/jni/JniCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,17 @@ class SparkAllocationListener final : public gluten::AllocationListener {
env->CallLongMethod(jListenerGlobalRef_, jReserveMethod_, size);
checkException(env);
}
std::lock_guard<std::mutex> lock(mutex_);
usedBytes_ += size;
peakBytes_ = std::max(usedBytes_, peakBytes_);
while (true) {
int64_t savedPeakBytes = peakBytes_;
if (usedBytes_ <= savedPeakBytes) {
break;
}
// usedBytes_ > savedPeakBytes, update peak
if (peakBytes_.compare_exchange_weak(savedPeakBytes, usedBytes_)) {
break;
}
}
}

int64_t currentBytes() override {
Expand All @@ -421,10 +429,8 @@ class SparkAllocationListener final : public gluten::AllocationListener {
jobject jListenerGlobalRef_;
const jmethodID jReserveMethod_;
const jmethodID jUnreserveMethod_;
int64_t usedBytes_{0L};
int64_t peakBytes_{0L};

mutable std::mutex mutex_;
std::atomic_int64_t usedBytes_{0L};
std::atomic_int64_t peakBytes_{0L};
};

class BacktraceAllocationListener final : public gluten::AllocationListener {
Expand Down
2 changes: 1 addition & 1 deletion cpp/core/memory/AllocationListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class BlockAllocationListener final : public AllocationListener {
}

private:
int64_t reserve(int64_t diff) {
inline int64_t reserve(int64_t diff) {
std::lock_guard<std::mutex> lock(mutex_);
usedBytes_ += diff;
int64_t newBlockCount;
Expand Down
12 changes: 10 additions & 2 deletions cpp/core/memory/MemoryAllocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,17 @@ int64_t ListenableMemoryAllocator::peakBytes() const {

void ListenableMemoryAllocator::updateUsage(int64_t size) {
listener_->allocationChanged(size);
std::lock_guard<std::mutex> lock(mutex_);
usedBytes_ += size;
peakBytes_ = std::max(peakBytes_, usedBytes_);
while (true) {
int64_t savedPeakBytes = peakBytes_;
if (usedBytes_ <= savedPeakBytes) {
break;
}
// usedBytes_ > savedPeakBytes, update peak
if (peakBytes_.compare_exchange_weak(savedPeakBytes, usedBytes_)) {
break;
}
}
}

bool StdMemoryAllocator::allocate(int64_t size, void** out) {
Expand Down
6 changes: 2 additions & 4 deletions cpp/core/memory/MemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ class ListenableMemoryAllocator final : public MemoryAllocator {
void updateUsage(int64_t size);
MemoryAllocator* const delegated_;
AllocationListener* const listener_;
int64_t usedBytes_{0L};
int64_t peakBytes_{0L};

mutable std::mutex mutex_;
std::atomic_int64_t usedBytes_{0L};
std::atomic_int64_t peakBytes_{0L};
};

class StdMemoryAllocator final : public MemoryAllocator {
Expand Down

0 comments on commit dbf95b4

Please sign in to comment.