25 using namespace impala;
34 : current_chunk_idx_(-1),
35 last_offset_conversion_chunk_idx_(-1),
37 chunk_size_(chunk_size == 0
39 : ((chunk_size + 7) / 8) * 8),
40 total_allocated_bytes_(0),
41 peak_allocated_bytes_(0),
42 total_reserved_bytes_(0),
43 mem_tracker_(mem_tracker) {
45 DCHECK(mem_tracker != NULL);
50 data(reinterpret_cast<uint8_t*>(malloc(size))),
52 cumulative_allocated_bytes(0),
60 int64_t total_bytes_released = 0;
61 for (
size_t i = 0; i <
chunks_.size(); ++i) {
62 if (!
chunks_[i].owns_data)
continue;
63 total_bytes_released +=
chunks_[i].size;
67 DCHECK(
chunks_.empty()) <<
"Must call FreeAll() or AcquireData() for this pool";
74 int64_t total_bytes_released = 0;
75 for (
size_t i = 0; i <
chunks_.size(); ++i) {
76 if (!
chunks_[i].owns_data)
continue;
77 total_bytes_released +=
chunks_[i].size;
106 current_chunk_idx_ = first_free_idx;
115 if (chunk_size == 0) {
123 chunk_size = ::max(min_size, chunk_size);
125 if (FLAGS_disable_mem_pools) chunk_size = min_size;
140 if (first_free_idx == static_cast<int>(
chunks_.size())) {
163 int num_acquired_chunks;
173 if (num_acquired_chunks <= 0) {
174 if (!keep_current) src->
FreeAll();
178 vector<ChunkInfo>::iterator end_chunk = src->
chunks_.begin() + num_acquired_chunks;
179 int64_t total_transfered_bytes = 0;
180 for (vector<ChunkInfo>::iterator i = src->
chunks_.begin(); i != end_chunk; ++i) {
181 total_transfered_bytes += i->size;
197 DCHECK(src->
chunks_.size() == 1 || src->
chunks_[1].allocated_bytes == 0);
199 src->
chunks_[0].cumulative_allocated_bytes = 0;
209 int start_idx =
chunks_.size() - num_acquired_chunks;
210 int cumulative_bytes = (start_idx == 0
212 :
chunks_[start_idx - 1].cumulative_allocated_bytes
213 +
chunks_[start_idx - 1].allocated_bytes);
215 chunks_[i].cumulative_allocated_bytes = cumulative_bytes;
216 cumulative_bytes +=
chunks_[i].allocated_bytes;
219 if (!keep_current) src->
FreeAll();
224 for (
int i = 0; i <
chunks_.size(); ++i) {
228 DCHECK_LE(reinterpret_cast<size_t>(ptr + size),
241 out <<
"MemPool(#chunks=" <<
chunks_.size() <<
" [";
242 for (
int i = 0; i <
chunks_.size(); ++i) {
243 sprintf(str,
"0x%lx=", reinterpret_cast<size_t>(
chunks_[i].data));
244 out << (i > 0 ?
" " :
"")
247 <<
"/" <<
chunks_[i].cumulative_allocated_bytes
248 <<
"/" <<
chunks_[i].allocated_bytes;
259 for (
int i = 0; i <
chunks_.size(); ++i) {
267 if (FLAGS_disable_mem_pools)
return true;
271 int64_t total_allocated = 0;
272 for (
int i = 0; i <
chunks_.size(); ++i) {
275 DCHECK_GT(
chunks_[i].allocated_bytes, 0);
277 if (current_chunk_empty) {
278 DCHECK_EQ(
chunks_[i].allocated_bytes, 0);
280 DCHECK_GT(
chunks_[i].allocated_bytes, 0);
283 DCHECK_EQ(
chunks_[i].allocated_bytes, 0);
286 DCHECK_EQ(
chunks_[i-1].cumulative_allocated_bytes +
chunks_[i-1].allocated_bytes,
287 chunks_[i].cumulative_allocated_bytes);
290 total_allocated +=
chunks_[i].allocated_bytes;
297 if (
chunks_.empty())
return -1;
300 for (
int i = 0; i <
chunks_.size(); ++i) {
313 for (
int i = 0; i <
chunks_.size(); ++i) {
327 for (vector<ChunkInfo>::iterator info =
chunks_.begin(); info !=
chunks_.end(); ++info) {
328 chunk_info->push_back(make_pair(info->data, info->allocated_bytes));
335 for (
int i = 0; i <
chunks_.size(); ++i) {
340 sprintf(str,
"%x ", info.
data[j]);
int last_offset_conversion_chunk_idx_
bool CheckIntegrity(bool current_chunk_empty)
int GetFreeOffset() const
Return offset to unoccpied space in current chunk.
MemTracker * mem_tracker_
bool Contains(uint8_t *ptr, int size)
int allocated_bytes
bytes allocated via Allocate() in this chunk
bool FindChunk(int min_size, bool check_limits)
bool TryConsume(int64_t bytes)
std::vector< ChunkInfo > chunks_
std::string DebugPrint()
Print allocated bytes from all chunks.
static IntGauge * MEM_POOL_TOTAL_BYTES
int64_t GetTotalChunkSizes() const
Return sum of chunk_sizes_.
void AcquireData(MemPool *src, bool keep_current)
static const int DEFAULT_INITIAL_CHUNK_SIZE
uint8_t * GetDataPtrHelper(int offset)
int64_t total_allocated_bytes_
sum of allocated_bytes_
int cumulative_allocated_bytes
void GetChunkInfo(std::vector< std::pair< uint8_t *, int > > *chunk_info)
Return (data ptr, allocated bytes) pairs for all chunks owned by this mempool.
int64_t peak_allocated_bytes_
Maximum number of bytes allocated from this pool at one time.
DECLARE_bool(disable_mem_pools)
This class is thread-safe.
static const char * LLVM_CLASS_NAME
void Release(int64_t bytes)
Decreases consumption of this tracker and its ancestors by 'bytes'.
MemPool(MemTracker *mem_tracker, int chunk_size=0)
int64_t total_reserved_bytes_
sum of all bytes allocated in chunks_
void Consume(int64_t bytes)
Increases consumption of this tracker and its ancestors by 'bytes'.
uint8_t offset[7 *64-sizeof(uint64_t)]
std::string DebugString()
int GetOffsetHelper(uint8_t *data)