Skip to content

Commit 5f125c0

Browse files
authored
calculate yql memory bytes allocated (#9065)
1 parent 3821c07 commit 5f125c0

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

ydb/library/yql/public/udf/arrow/memory_pool.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,46 @@ class TYqlMemoryPool : public arrow::MemoryPool {
1212
arrow::Status Allocate(int64_t size, uint8_t** out) final {
1313
Y_ENSURE(size >= 0 && out);
1414
*out = (uint8_t*)UdfArrowAllocate(size);
15+
UpdateAllocatedBytes(size);
1516
return arrow::Status::OK();
1617
}
17-
18+
1819
arrow::Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) final {
1920
Y_ENSURE(old_size >= 0 && new_size >= 0 && ptr);
2021
*ptr = (uint8_t*)UdfArrowReallocate(*ptr, old_size, new_size);
22+
UpdateAllocatedBytes(new_size - old_size);
2123
return arrow::Status::OK();
2224
}
2325

2426
void Free(uint8_t* buffer, int64_t size) final {
2527
Y_ENSURE(size >= 0);
2628
UdfArrowFree(buffer, size);
29+
UpdateAllocatedBytes(-size);
30+
}
31+
32+
int64_t max_memory() const final {
33+
return max_memory_.load();
2734
}
2835

29-
virtual int64_t bytes_allocated() const final {
30-
return 0;
36+
int64_t bytes_allocated() const final {
37+
return bytes_allocated_.load();
38+
}
39+
40+
inline void UpdateAllocatedBytes(int64_t diff) {
41+
// inspired by arrow/memory_pool.h impl.
42+
int64_t allocated = bytes_allocated_.fetch_add(diff) + diff;
43+
if (diff > 0 && allocated > max_memory_) {
44+
max_memory_ = allocated;
45+
}
3146
}
3247

3348
virtual std::string backend_name() const final {
3449
return "yql";
3550
}
51+
52+
private:
53+
std::atomic<int64_t> bytes_allocated_{0};
54+
std::atomic<int64_t> max_memory_{0};
3655
};
3756

3857
arrow::MemoryPool* GetYqlMemoryPool() {

0 commit comments

Comments
 (0)