From 62dcdb0afa301364dacfb5709ee110650ae65106 Mon Sep 17 00:00:00 2001 From: Rok Date: Thu, 17 May 2018 09:26:30 +0200 Subject: [PATCH 1/9] ARROW-530: C++/Python: Provide subpools for better memory allocation tracking --- cpp/src/arrow/memory_pool.cc | 41 ++++++++++++++++++++++++++++ cpp/src/arrow/memory_pool.h | 21 ++++++++++++++ python/pyarrow/__init__.py | 2 +- python/pyarrow/includes/libarrow.pxd | 4 +++ python/pyarrow/memory.pxi | 14 ++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/memory_pool.cc b/cpp/src/arrow/memory_pool.cc index dedab7ea7291..0945052c4ba0 100644 --- a/cpp/src/arrow/memory_pool.cc +++ b/cpp/src/arrow/memory_pool.cc @@ -201,4 +201,45 @@ int64_t LoggingMemoryPool::max_memory() const { std::cout << "max_memory: " << mem << std::endl; return mem; } + +ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool), proxy_bytes_allocated_(0) {} + +Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) { + proxy_bytes_allocated_ += size; + Status s = pool_->Allocate(size, out); + std::cout << "Allocate: size = " << size << std::endl; + return s; +} + +Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) { + Status s = pool_->Reallocate(old_size, new_size, ptr); + proxy_bytes_allocated_ += new_size - old_size; + std::cout << "Reallocate: old_size = " << old_size << " - new_size = " << new_size + << " - proxy_allocated - " << proxy_bytes_allocated_ << std::endl; + return s; +} + +void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) { + pool_->Free(buffer, size); + proxy_bytes_allocated_ -= size; + std::cout << "Free: size = " << size << std::endl; +} + +int64_t ProxyMemoryPool::bytes_allocated() const { + int64_t nb_bytes = pool_->bytes_allocated(); + std::cout << "bytes_allocated: " << nb_bytes << std::endl; + return nb_bytes; +} + +int64_t ProxyMemoryPool::proxy_bytes_allocated() const { + int64_t nb_bytes = proxy_bytes_allocated_; + std::cout << "bytes_allocated: " << nb_bytes << std::endl; + return nb_bytes; +} + +int64_t ProxyMemoryPool::max_memory() const { + int64_t mem = pool_->max_memory(); + std::cout << "max_memory: " << mem << std::endl; + return mem; +} } // namespace arrow diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index 348343b54e91..52146c7b27b7 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -86,6 +86,27 @@ class ARROW_EXPORT LoggingMemoryPool : public MemoryPool { MemoryPool* pool_; }; +class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { + public: + explicit ProxyMemoryPool (MemoryPool* pool); + ~ProxyMemoryPool () override = default; + + Status Allocate(int64_t size, uint8_t** out) override; + Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) override; + + void Free(uint8_t* buffer, int64_t size) override; + + int64_t bytes_allocated() const override; + + int64_t max_memory() const override; + + int64_t proxy_bytes_allocated_; + int64_t proxy_bytes_allocated() const; + + private: + MemoryPool* pool_; +}; + ARROW_EXPORT MemoryPool* default_memory_pool(); #ifdef ARROW_NO_DEFAULT_MEMORY_POOL diff --git a/python/pyarrow/__init__.py b/python/pyarrow/__init__.py index 09f907c8a8a9..e4caf446777b 100644 --- a/python/pyarrow/__init__.py +++ b/python/pyarrow/__init__.py @@ -90,7 +90,7 @@ def parse_version(root): from pyarrow.lib import (Buffer, ResizableBuffer, foreign_buffer, py_buffer, compress, decompress, allocate_buffer) -from pyarrow.lib import (MemoryPool, total_allocated_bytes, +from pyarrow.lib import (MemoryPool, ProxyMemoryPool, total_allocated_bytes, set_memory_pool, default_memory_pool, log_memory_allocations) diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index 9a0b4687e124..e96bb489b0ae 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -192,6 +192,10 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: cdef cppclass CLoggingMemoryPool" arrow::LoggingMemoryPool"(CMemoryPool): CLoggingMemoryPool(CMemoryPool*) + cdef cppclass CProxyMemoryPool" arrow::ProxyMemoryPool"(CMemoryPool): + CProxyMemoryPool(CMemoryPool*) + int64_t proxy_bytes_allocated() + cdef cppclass CBuffer" arrow::Buffer": CBuffer(const uint8_t* data, int64_t size) const uint8_t* data() diff --git a/python/pyarrow/memory.pxi b/python/pyarrow/memory.pxi index 3d2601f89c8f..92eeecc248e2 100644 --- a/python/pyarrow/memory.pxi +++ b/python/pyarrow/memory.pxi @@ -44,6 +44,18 @@ cdef class LoggingMemoryPool(MemoryPool): self.init(self.logging_pool.get()) +cdef class ProxyMemoryPool(MemoryPool): + cdef: + unique_ptr[CProxyMemoryPool] proxy_pool + + def __cinit__(self, MemoryPool pool): + self.proxy_pool.reset(new CProxyMemoryPool(pool.pool)) + self.init(self.proxy_pool.get()) + + def proxy_bytes_allocated(self): + return self.proxy_pool.get().proxy_bytes_allocated() + + def default_memory_pool(): cdef: MemoryPool pool = MemoryPool() @@ -58,6 +70,8 @@ def set_memory_pool(MemoryPool pool): cdef MemoryPool _default_memory_pool = default_memory_pool() cdef LoggingMemoryPool _logging_memory_pool = ( LoggingMemoryPool(_default_memory_pool)) +cdef ProxyMemoryPool _proxy_memory_pool = ( + ProxyMemoryPool(_default_memory_pool)) def log_memory_allocations(enable=True): From 7c4b769230e838bf0a12815f4610975f40a1d3fe Mon Sep 17 00:00:00 2001 From: Rok Date: Thu, 17 May 2018 23:43:25 +0200 Subject: [PATCH 2/9] adding c++ unit tests --- cpp/src/arrow/memory_pool-test.cc | 21 +++++++++++++++++++++ cpp/src/arrow/memory_pool.cc | 10 +--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/cpp/src/arrow/memory_pool-test.cc b/cpp/src/arrow/memory_pool-test.cc index c5e3ef295a7f..f0f0af644068 100644 --- a/cpp/src/arrow/memory_pool-test.cc +++ b/cpp/src/arrow/memory_pool-test.cc @@ -91,4 +91,25 @@ TEST(LoggingMemoryPool, Logging) { ASSERT_EQ(200, pool->max_memory()); } + +TEST(ProxyMemoryPool, ProxyMemoryPool) { + MemoryPool* pool = default_memory_pool(); + + ProxyMemoryPool pp(pool); + + uint8_t* data; + ASSERT_OK(pool->Allocate(100, &data)); + + uint8_t* data2; + ASSERT_OK(pp->Allocate(300, &data2)); + + ASSERT_EQ(100, pool->bytes_allocated()); + ASSERT_EQ(300, pp->proxy_bytes_allocated()); + + pool->Free(data, 100); + pp->Free(data2, 300); + + ASSERT_EQ(0, pool->bytes_allocated()); + ASSERT_EQ(0, pp->proxy_bytes_allocated()); +} } // namespace arrow diff --git a/cpp/src/arrow/memory_pool.cc b/cpp/src/arrow/memory_pool.cc index 0945052c4ba0..d6ecfecf75e9 100644 --- a/cpp/src/arrow/memory_pool.cc +++ b/cpp/src/arrow/memory_pool.cc @@ -207,39 +207,31 @@ ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool), proxy_bytes_al Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) { proxy_bytes_allocated_ += size; Status s = pool_->Allocate(size, out); - std::cout << "Allocate: size = " << size << std::endl; return s; } Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) { Status s = pool_->Reallocate(old_size, new_size, ptr); proxy_bytes_allocated_ += new_size - old_size; - std::cout << "Reallocate: old_size = " << old_size << " - new_size = " << new_size - << " - proxy_allocated - " << proxy_bytes_allocated_ << std::endl; return s; } void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) { pool_->Free(buffer, size); proxy_bytes_allocated_ -= size; - std::cout << "Free: size = " << size << std::endl; } int64_t ProxyMemoryPool::bytes_allocated() const { int64_t nb_bytes = pool_->bytes_allocated(); - std::cout << "bytes_allocated: " << nb_bytes << std::endl; return nb_bytes; } int64_t ProxyMemoryPool::proxy_bytes_allocated() const { - int64_t nb_bytes = proxy_bytes_allocated_; - std::cout << "bytes_allocated: " << nb_bytes << std::endl; - return nb_bytes; + return proxy_bytes_allocated_; } int64_t ProxyMemoryPool::max_memory() const { int64_t mem = pool_->max_memory(); - std::cout << "max_memory: " << mem << std::endl; return mem; } } // namespace arrow From f90b8970eae04a78b614404c9e288b7e2bab6981 Mon Sep 17 00:00:00 2001 From: Rok Date: Sat, 19 May 2018 22:09:54 +0200 Subject: [PATCH 3/9] C++ unit tests for ARROW-530 passing --- cpp/src/arrow/memory_pool-test.cc | 14 +++++++------- cpp/src/arrow/memory_pool.cc | 9 +++++---- cpp/src/arrow/memory_pool.h | 4 ++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cpp/src/arrow/memory_pool-test.cc b/cpp/src/arrow/memory_pool-test.cc index f0f0af644068..74cf870209d4 100644 --- a/cpp/src/arrow/memory_pool-test.cc +++ b/cpp/src/arrow/memory_pool-test.cc @@ -92,24 +92,24 @@ TEST(LoggingMemoryPool, Logging) { ASSERT_EQ(200, pool->max_memory()); } -TEST(ProxyMemoryPool, ProxyMemoryPool) { +TEST(ProxyMemoryPool, Logging) { MemoryPool* pool = default_memory_pool(); - ProxyMemoryPool pp(pool); + ProxyMemoryPool pp = ProxyMemoryPool(pool); uint8_t* data; ASSERT_OK(pool->Allocate(100, &data)); uint8_t* data2; - ASSERT_OK(pp->Allocate(300, &data2)); + ASSERT_OK(pp.Allocate(300, &data2)); - ASSERT_EQ(100, pool->bytes_allocated()); - ASSERT_EQ(300, pp->proxy_bytes_allocated()); + ASSERT_EQ(400, pool->bytes_allocated()); + ASSERT_EQ(300, pp.proxy_bytes_allocated()); pool->Free(data, 100); - pp->Free(data2, 300); + pp.Free(data2, 300); ASSERT_EQ(0, pool->bytes_allocated()); - ASSERT_EQ(0, pp->proxy_bytes_allocated()); + ASSERT_EQ(0, pp.proxy_bytes_allocated()); } } // namespace arrow diff --git a/cpp/src/arrow/memory_pool.cc b/cpp/src/arrow/memory_pool.cc index d6ecfecf75e9..9e77406b2d4c 100644 --- a/cpp/src/arrow/memory_pool.cc +++ b/cpp/src/arrow/memory_pool.cc @@ -202,11 +202,11 @@ int64_t LoggingMemoryPool::max_memory() const { return mem; } -ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool), proxy_bytes_allocated_(0) {} +ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool) {} Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) { - proxy_bytes_allocated_ += size; Status s = pool_->Allocate(size, out); + proxy_bytes_allocated_ += size; return s; } @@ -226,8 +226,9 @@ int64_t ProxyMemoryPool::bytes_allocated() const { return nb_bytes; } -int64_t ProxyMemoryPool::proxy_bytes_allocated() const { - return proxy_bytes_allocated_; +int64_t ProxyMemoryPool::proxy_bytes_allocated() { + int64_t nb_bytes = proxy_bytes_allocated_; + return nb_bytes; } int64_t ProxyMemoryPool::max_memory() const { diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index 52146c7b27b7..0ef7d2f7e5af 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -100,11 +100,11 @@ class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { int64_t max_memory() const override; - int64_t proxy_bytes_allocated_; - int64_t proxy_bytes_allocated() const; + int64_t proxy_bytes_allocated(); private: MemoryPool* pool_; + int64_t proxy_bytes_allocated_ = 0; }; ARROW_EXPORT MemoryPool* default_memory_pool(); From 17a5c9b6cf8915288010f8380e1c6f861f5f6dfc Mon Sep 17 00:00:00 2001 From: Rok Date: Sun, 20 May 2018 07:20:00 +0200 Subject: [PATCH 4/9] ARROW-530 - C++ linter fix --- cpp/src/arrow/memory_pool.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index 0ef7d2f7e5af..0d29bbfd03a3 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -88,8 +88,8 @@ class ARROW_EXPORT LoggingMemoryPool : public MemoryPool { class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { public: - explicit ProxyMemoryPool (MemoryPool* pool); - ~ProxyMemoryPool () override = default; + explicit ProxyMemoryPool(MemoryPool* pool); + ~ProxyMemoryPool() override = default; Status Allocate(int64_t size, uint8_t** out) override; Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) override; From 0645e50023c50bea83ceebf2d1d06bb2bb792bb6 Mon Sep 17 00:00:00 2001 From: Rok Date: Wed, 23 May 2018 01:23:07 +0200 Subject: [PATCH 5/9] refactoring ProxyMemoryPool interface --- cpp/src/arrow/memory_pool-test.cc | 4 ++-- cpp/src/arrow/memory_pool.cc | 13 ++++--------- cpp/src/arrow/memory_pool.h | 4 +--- python/pyarrow/includes/libarrow.pxd | 2 +- python/pyarrow/memory.pxi | 3 --- 5 files changed, 8 insertions(+), 18 deletions(-) diff --git a/cpp/src/arrow/memory_pool-test.cc b/cpp/src/arrow/memory_pool-test.cc index 74cf870209d4..18c035f38418 100644 --- a/cpp/src/arrow/memory_pool-test.cc +++ b/cpp/src/arrow/memory_pool-test.cc @@ -104,12 +104,12 @@ TEST(ProxyMemoryPool, Logging) { ASSERT_OK(pp.Allocate(300, &data2)); ASSERT_EQ(400, pool->bytes_allocated()); - ASSERT_EQ(300, pp.proxy_bytes_allocated()); + ASSERT_EQ(300, pp.bytes_allocated()); pool->Free(data, 100); pp.Free(data2, 300); ASSERT_EQ(0, pool->bytes_allocated()); - ASSERT_EQ(0, pp.proxy_bytes_allocated()); + ASSERT_EQ(0, pp.bytes_allocated()); } } // namespace arrow diff --git a/cpp/src/arrow/memory_pool.cc b/cpp/src/arrow/memory_pool.cc index 9e77406b2d4c..afa520e51070 100644 --- a/cpp/src/arrow/memory_pool.cc +++ b/cpp/src/arrow/memory_pool.cc @@ -206,28 +206,23 @@ ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool) {} Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) { Status s = pool_->Allocate(size, out); - proxy_bytes_allocated_ += size; + bytes_allocated_ += size; return s; } Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) { Status s = pool_->Reallocate(old_size, new_size, ptr); - proxy_bytes_allocated_ += new_size - old_size; + bytes_allocated_ += new_size - old_size; return s; } void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) { pool_->Free(buffer, size); - proxy_bytes_allocated_ -= size; + bytes_allocated_ -= size; } int64_t ProxyMemoryPool::bytes_allocated() const { - int64_t nb_bytes = pool_->bytes_allocated(); - return nb_bytes; -} - -int64_t ProxyMemoryPool::proxy_bytes_allocated() { - int64_t nb_bytes = proxy_bytes_allocated_; + int64_t nb_bytes = bytes_allocated_; return nb_bytes; } diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index 0d29bbfd03a3..baf3239420c4 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -100,11 +100,9 @@ class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { int64_t max_memory() const override; - int64_t proxy_bytes_allocated(); - private: MemoryPool* pool_; - int64_t proxy_bytes_allocated_ = 0; + int64_t bytes_allocated_ = 0; }; ARROW_EXPORT MemoryPool* default_memory_pool(); diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index e96bb489b0ae..38ff619dbe14 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -194,7 +194,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: cdef cppclass CProxyMemoryPool" arrow::ProxyMemoryPool"(CMemoryPool): CProxyMemoryPool(CMemoryPool*) - int64_t proxy_bytes_allocated() + int64_t bytes_allocated() cdef cppclass CBuffer" arrow::Buffer": CBuffer(const uint8_t* data, int64_t size) diff --git a/python/pyarrow/memory.pxi b/python/pyarrow/memory.pxi index 92eeecc248e2..5b9983f103e9 100644 --- a/python/pyarrow/memory.pxi +++ b/python/pyarrow/memory.pxi @@ -52,9 +52,6 @@ cdef class ProxyMemoryPool(MemoryPool): self.proxy_pool.reset(new CProxyMemoryPool(pool.pool)) self.init(self.proxy_pool.get()) - def proxy_bytes_allocated(self): - return self.proxy_pool.get().proxy_bytes_allocated() - def default_memory_pool(): cdef: From 195a1367c5fc7dc62b0c6397eeb0ecb9b0f369cc Mon Sep 17 00:00:00 2001 From: Rok Date: Sat, 2 Jun 2018 21:58:38 +0200 Subject: [PATCH 6/9] implementing feedback --- cpp/src/arrow/memory_pool-test.cc | 10 ++++----- cpp/src/arrow/memory_pool.cc | 31 +++++++++++++++++----------- cpp/src/arrow/memory_pool.h | 10 +++++++-- python/pyarrow/includes/libarrow.pxd | 1 - python/pyarrow/memory.pxi | 2 -- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/cpp/src/arrow/memory_pool-test.cc b/cpp/src/arrow/memory_pool-test.cc index 18c035f38418..ca703693f4b0 100644 --- a/cpp/src/arrow/memory_pool-test.cc +++ b/cpp/src/arrow/memory_pool-test.cc @@ -95,21 +95,21 @@ TEST(LoggingMemoryPool, Logging) { TEST(ProxyMemoryPool, Logging) { MemoryPool* pool = default_memory_pool(); - ProxyMemoryPool pp = ProxyMemoryPool(pool); + ProxyMemoryPool* pp = new ProxyMemoryPool(pool); uint8_t* data; ASSERT_OK(pool->Allocate(100, &data)); uint8_t* data2; - ASSERT_OK(pp.Allocate(300, &data2)); + ASSERT_OK(pp->Allocate(300, &data2)); ASSERT_EQ(400, pool->bytes_allocated()); - ASSERT_EQ(300, pp.bytes_allocated()); + ASSERT_EQ(300, pp->bytes_allocated()); pool->Free(data, 100); - pp.Free(data2, 300); + pp->Free(data2, 300); ASSERT_EQ(0, pool->bytes_allocated()); - ASSERT_EQ(0, pp.bytes_allocated()); + ASSERT_EQ(0, pp->bytes_allocated()); } } // namespace arrow diff --git a/cpp/src/arrow/memory_pool.cc b/cpp/src/arrow/memory_pool.cc index afa520e51070..34bd600e83f4 100644 --- a/cpp/src/arrow/memory_pool.cc +++ b/cpp/src/arrow/memory_pool.cc @@ -205,15 +205,27 @@ int64_t LoggingMemoryPool::max_memory() const { ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool) {} Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) { - Status s = pool_->Allocate(size, out); + RETURN_NOT_OK(pool_->Allocate(size, out)); bytes_allocated_ += size; - return s; + { + std::lock_guard guard(lock_); + if (bytes_allocated_ > max_memory_) { + max_memory_ = bytes_allocated_.load(); + } + } + return Status::OK(); } Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) { - Status s = pool_->Reallocate(old_size, new_size, ptr); + RETURN_NOT_OK(pool_->Reallocate(old_size, new_size, ptr)); bytes_allocated_ += new_size - old_size; - return s; + { + std::lock_guard guard(lock_); + if (bytes_allocated_ > max_memory_) { + max_memory_ = bytes_allocated_.load(); + } + } + return Status::OK(); } void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) { @@ -221,13 +233,8 @@ void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) { bytes_allocated_ -= size; } -int64_t ProxyMemoryPool::bytes_allocated() const { - int64_t nb_bytes = bytes_allocated_; - return nb_bytes; -} +int64_t ProxyMemoryPool::bytes_allocated() const { return bytes_allocated_.load(); } + +int64_t ProxyMemoryPool::max_memory() const { return max_memory_.load(); } -int64_t ProxyMemoryPool::max_memory() const { - int64_t mem = pool_->max_memory(); - return mem; -} } // namespace arrow diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index baf3239420c4..703808a6f449 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -19,6 +19,7 @@ #define ARROW_MEMORY_POOL_H #include +#include #include #include "arrow/util/visibility.h" @@ -86,10 +87,13 @@ class ARROW_EXPORT LoggingMemoryPool : public MemoryPool { MemoryPool* pool_; }; +/// Derived class for memory allocation. +/// +/// Tracks the number of bytes and maximum mmeory allocated through its direct +/// calls. Actual allocation is delegated to MemoryPool class. class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { public: explicit ProxyMemoryPool(MemoryPool* pool); - ~ProxyMemoryPool() override = default; Status Allocate(int64_t size, uint8_t** out) override; Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) override; @@ -101,8 +105,10 @@ class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { int64_t max_memory() const override; private: + mutable std::mutex lock_; MemoryPool* pool_; - int64_t bytes_allocated_ = 0; + std::atomic bytes_allocated_{0}; + std::atomic max_memory_{0}; }; ARROW_EXPORT MemoryPool* default_memory_pool(); diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index 38ff619dbe14..660c119bda56 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -194,7 +194,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: cdef cppclass CProxyMemoryPool" arrow::ProxyMemoryPool"(CMemoryPool): CProxyMemoryPool(CMemoryPool*) - int64_t bytes_allocated() cdef cppclass CBuffer" arrow::Buffer": CBuffer(const uint8_t* data, int64_t size) diff --git a/python/pyarrow/memory.pxi b/python/pyarrow/memory.pxi index 5b9983f103e9..a2a043f0ff45 100644 --- a/python/pyarrow/memory.pxi +++ b/python/pyarrow/memory.pxi @@ -67,8 +67,6 @@ def set_memory_pool(MemoryPool pool): cdef MemoryPool _default_memory_pool = default_memory_pool() cdef LoggingMemoryPool _logging_memory_pool = ( LoggingMemoryPool(_default_memory_pool)) -cdef ProxyMemoryPool _proxy_memory_pool = ( - ProxyMemoryPool(_default_memory_pool)) def log_memory_allocations(enable=True): From 5bebf97dbe5b88ee752a155e27dff167e9e25b5a Mon Sep 17 00:00:00 2001 From: Rok Date: Sun, 3 Jun 2018 12:57:56 +0200 Subject: [PATCH 7/9] adding docstring --- cpp/src/arrow/memory_pool.h | 2 +- python/pyarrow/memory.pxi | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index 703808a6f449..7112549590b6 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -89,7 +89,7 @@ class ARROW_EXPORT LoggingMemoryPool : public MemoryPool { /// Derived class for memory allocation. /// -/// Tracks the number of bytes and maximum mmeory allocated through its direct +/// Tracks the number of bytes and maximum memory allocated through its direct /// calls. Actual allocation is delegated to MemoryPool class. class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { public: diff --git a/python/pyarrow/memory.pxi b/python/pyarrow/memory.pxi index a2a043f0ff45..1a461ef20c72 100644 --- a/python/pyarrow/memory.pxi +++ b/python/pyarrow/memory.pxi @@ -45,6 +45,10 @@ cdef class LoggingMemoryPool(MemoryPool): cdef class ProxyMemoryPool(MemoryPool): + """ + Derived MemoryPool class that tracks the number of bytes and + maximum memory allocated through its direct calls. + """ cdef: unique_ptr[CProxyMemoryPool] proxy_pool From 96e58ee62790676893e26e9b4f22ee2e41b15398 Mon Sep 17 00:00:00 2001 From: Rok Date: Tue, 5 Jun 2018 00:37:09 +0200 Subject: [PATCH 8/9] fixing linting issue --- cpp/src/arrow/memory_pool.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/arrow/memory_pool.h b/cpp/src/arrow/memory_pool.h index 7112549590b6..de588965d9eb 100644 --- a/cpp/src/arrow/memory_pool.h +++ b/cpp/src/arrow/memory_pool.h @@ -19,8 +19,8 @@ #define ARROW_MEMORY_POOL_H #include -#include #include +#include #include "arrow/util/visibility.h" From eabd8a4fe73ac007487d5f3657be7392bd89c715 Mon Sep 17 00:00:00 2001 From: Rok Date: Wed, 6 Jun 2018 11:59:46 +0200 Subject: [PATCH 9/9] changing ProxyMemoryPool instantiation --- cpp/src/arrow/memory_pool-test.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/memory_pool-test.cc b/cpp/src/arrow/memory_pool-test.cc index ca703693f4b0..8915708ea231 100644 --- a/cpp/src/arrow/memory_pool-test.cc +++ b/cpp/src/arrow/memory_pool-test.cc @@ -95,21 +95,21 @@ TEST(LoggingMemoryPool, Logging) { TEST(ProxyMemoryPool, Logging) { MemoryPool* pool = default_memory_pool(); - ProxyMemoryPool* pp = new ProxyMemoryPool(pool); + ProxyMemoryPool pp(pool); uint8_t* data; ASSERT_OK(pool->Allocate(100, &data)); uint8_t* data2; - ASSERT_OK(pp->Allocate(300, &data2)); + ASSERT_OK(pp.Allocate(300, &data2)); ASSERT_EQ(400, pool->bytes_allocated()); - ASSERT_EQ(300, pp->bytes_allocated()); + ASSERT_EQ(300, pp.bytes_allocated()); pool->Free(data, 100); - pp->Free(data2, 300); + pp.Free(data2, 300); ASSERT_EQ(0, pool->bytes_allocated()); - ASSERT_EQ(0, pp->bytes_allocated()); + ASSERT_EQ(0, pp.bytes_allocated()); } } // namespace arrow