Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::MemPool Class Reference

#include <mem-pool.h>

Collaboration diagram for impala::MemPool:

Classes

struct  ChunkInfo
 

Public Member Functions

 MemPool (MemTracker *mem_tracker, int chunk_size=0)
 
 ~MemPool ()
 
uint8_t * Allocate (int size)
 
uint8_t * TryAllocate (int size)
 
void ReturnPartialAllocation (int byte_size)
 
void Clear ()
 Makes all allocated chunks available for re-use, but doesn't delete any chunks. More...
 
void FreeAll ()
 
void AcquireData (MemPool *src, bool keep_current)
 
bool Contains (uint8_t *ptr, int size)
 
std::string DebugString ()
 
int64_t total_allocated_bytes () const
 
int64_t peak_allocated_bytes () const
 
int64_t total_reserved_bytes () const
 
MemTrackermem_tracker ()
 
int64_t GetTotalChunkSizes () const
 Return sum of chunk_sizes_. More...
 
int GetOffset (uint8_t *data)
 
int GetCurrentOffset () const
 
uint8_t * GetDataPtr (int offset)
 
void GetChunkInfo (std::vector< std::pair< uint8_t *, int > > *chunk_info)
 Return (data ptr, allocated bytes) pairs for all chunks owned by this mempool. More...
 
std::string DebugPrint ()
 Print allocated bytes from all chunks. More...
 

Static Public Attributes

static const char * LLVM_CLASS_NAME = "class.impala::MemPool"
 

Private Member Functions

bool FindChunk (int min_size, bool check_limits)
 
bool CheckIntegrity (bool current_chunk_empty)
 
int GetOffsetHelper (uint8_t *data)
 
uint8_t * GetDataPtrHelper (int offset)
 
int GetFreeOffset () const
 Return offset to unoccpied space in current chunk. More...
 
template<bool CHECK_LIMIT_FIRST>
uint8_t * Allocate (int size)
 

Private Attributes

int current_chunk_idx_
 
int last_offset_conversion_chunk_idx_
 
int chunk_size_
 
int64_t total_allocated_bytes_
 sum of allocated_bytes_ More...
 
int64_t peak_allocated_bytes_
 Maximum number of bytes allocated from this pool at one time. More...
 
int64_t total_reserved_bytes_
 sum of all bytes allocated in chunks_ More...
 
std::vector< ChunkInfochunks_
 
MemTrackermem_tracker_
 

Static Private Attributes

static const int DEFAULT_INITIAL_CHUNK_SIZE = 4 * 1024
 

Friends

class MemPoolTest
 

Detailed Description

A MemPool maintains a list of memory chunks from which it allocates memory in response to Allocate() calls; Chunks stay around for the lifetime of the mempool or until they are passed on to another mempool. The caller registers a MemTracker with the pool; chunk allocations are counted against that tracker and all of its ancestors. If chunks get moved between pools during AcquireData() calls, the respective MemTrackers are updated accordingly. Chunks freed up in the d'tor are subtracted from the registered trackers. An Allocate() call will attempt to allocate memory from the chunk that was most recently added; if that chunk doesn't have enough memory to satisfy the allocation request, the free chunks are searched for one that is big enough otherwise a new chunk is added to the list. The current_chunk_idx_ always points to the last chunk with allocated memory. In order to keep allocation overhead low, chunk sizes double with each new one added, until they hit a maximum size. Example: MemPool* p = new MemPool(); for (int i = 0; i < 1024; ++i) { returns 8-byte aligned memory (effectively 24 bytes): .. = p->Allocate(17); } at this point, 17K have been handed out in response to Allocate() calls and 28K of chunks have been allocated (chunk sizes: 4K, 8K, 16K) We track total and peak allocated bytes. At this point they would be the same: 28k bytes. A call to Clear will return the allocated memory so total_allocate_bytes_ becomes 0 while peak_allocate_bytes_ remains at 28k. p->Clear(); the entire 1st chunk is returned: .. = p->Allocate(4 * 1024); 4K of the 2nd chunk are returned: .. = p->Allocate(4 * 1024); a new 20K chunk is created .. = p->Allocate(20 * 1024); MemPool* p2 = new MemPool(); the new mempool receives all chunks containing data from p p2->AcquireData(p, false); At this point p.total_allocated_bytes_ would be 0 while p.peak_allocated_bytes_ remains unchanged. The one remaining (empty) chunk is released: delete p;

Definition at line 77 of file mem-pool.h.

Constructor & Destructor Documentation

MemPool::MemPool ( MemTracker mem_tracker,
int  chunk_size = 0 
)

Allocates mempool with fixed-size chunks of size 'chunk_size'. Chunk_size must be >= 0; 0 requests automatic doubling of chunk sizes, up to a limit. 'tracker' tracks the amount of memory allocated by this pool. Must not be NULL.

Definition at line 33 of file mem-pool.cc.

References chunk_size_.

MemPool::~MemPool ( )

Frees all chunks of memory and subtracts the total allocated bytes from the registered limits.

Definition at line 59 of file mem-pool.cc.

References chunks_, and impala::ImpaladMetrics::MEM_POOL_TOTAL_BYTES.

Member Function Documentation

void MemPool::AcquireData ( MemPool src,
bool  keep_current 
)
uint8_t* impala::MemPool::Allocate ( int  size)
inline

Allocates 8-byte aligned section of memory of 'size' bytes at the end of the the current chunk. Creates a new chunk if there aren't any chunks with enough capacity.

Definition at line 92 of file mem-pool.h.

Referenced by impala::FreePool::Allocate(), impala::DecompressorTest::CompressAndDecompress(), impala::DecompressorTest::CompressAndDecompressNoOutputAllocated(), impala::HdfsParquetScanner::ColumnReader< T >::ConvertSlot(), impala::HdfsTextScanner::CopyBoundaryField(), impala::HdfsParquetScanner::ColumnReader< T >::CopySlot(), impala::Tuple::Create(), impala::SimpleTupleStreamTest::CreateIntBatch(), impala::RowBatchListTest::CreateRowBatch(), impala::DataStreamTest::CreateRowBatch(), impala::SimpleTupleStreamTest::CreateStringBatch(), impala::OldHashTableTest::CreateTupleRow(), impala::HashTableTest::CreateTupleRow(), impala::TupleRow::DeepCopy(), impala::Tuple::DeepCopy(), impala::DataSourceScanNode::GetNext(), impala::StringBuffer::GrowBuffer(), impala::HashTable::GrowNodeArray(), impala::DataSourceScanNode::MaterializeNextRow(), impala::GzipCompressor::ProcessBlock(), impala::SnappyDecompressor::ProcessBlock(), impala::SnappyBlockCompressor::ProcessBlock(), impala::SnappyCompressor::ProcessBlock(), impala::SnappyBlockDecompressor::ProcessBlock(), impala::GzipDecompressor::ProcessBlockStreaming(), RandString(), impala::HdfsAvroScanner::ReadAvroChar(), impala::HdfsScanner::StartNewRowBatch(), impala::TEST(), impala::TextConverter::UnescapeString(), impala::RawValue::Write(), and impala::TextConverter::WriteSlot().

template<bool CHECK_LIMIT_FIRST>
uint8_t* impala::MemPool::Allocate ( int  size)
inlineprivate
bool MemPool::CheckIntegrity ( bool  current_chunk_empty)
private

Check integrity of the supporting data structures; always returns true but DCHECKs all invariants. If 'current_chunk_empty' is false, checks that the current chunk contains data.

Definition at line 265 of file mem-pool.cc.

References chunk_size_, chunks_, current_chunk_idx_, and total_allocated_bytes_.

Referenced by AcquireData(), impala::MemPoolTest::CheckIntegrity(), Clear(), and FindChunk().

void impala::MemPool::Clear ( )
inline

Makes all allocated chunks available for re-use, but doesn't delete any chunks.

Definition at line 118 of file mem-pool.h.

References CheckIntegrity(), chunks_, current_chunk_idx_, and total_allocated_bytes_.

Referenced by impala::TEST().

bool MemPool::Contains ( uint8_t *  ptr,
int  size 
)

Diagnostic to check if memory is allocated from this mempool. Inputs: ptr: start of memory block. size: size of memory block. Returns true if memory block is in one of the chunks in this mempool.

Definition at line 223 of file mem-pool.cc.

References impala::MemPool::ChunkInfo::allocated_bytes, chunks_, and impala::MemPool::ChunkInfo::data.

string MemPool::DebugPrint ( )

Print allocated bytes from all chunks.

Definition at line 332 of file mem-pool.cc.

References impala::MemPool::ChunkInfo::allocated_bytes, chunks_, and impala::MemPool::ChunkInfo::data.

string MemPool::DebugString ( )

Definition at line 238 of file mem-pool.cc.

References chunks_, current_chunk_idx_, GetTotalChunkSizes(), and total_allocated_bytes_.

bool MemPool::FindChunk ( int  min_size,
bool  check_limits 
)
private

Find or allocated a chunk with at least min_size spare capacity and update current_chunk_idx_. Also updates chunks_, chunk_sizes_ and allocated_bytes_ if a new chunk needs to be created. If check_limits is true, this call can fail (returns false) if adding a new chunk exceeds the mem limits.

Definition at line 92 of file mem-pool.cc.

References impala::MemPool::ChunkInfo::allocated_bytes, CheckIntegrity(), chunk_size_, chunks_, impala::MemTracker::Consume(), impala::MemPool::ChunkInfo::cumulative_allocated_bytes, current_chunk_idx_, DEFAULT_INITIAL_CHUNK_SIZE, mem_tracker_, total_reserved_bytes_, and impala::MemTracker::TryConsume().

Referenced by Allocate().

void MemPool::GetChunkInfo ( std::vector< std::pair< uint8_t *, int > > *  chunk_info)

Return (data ptr, allocated bytes) pairs for all chunks owned by this mempool.

Definition at line 325 of file mem-pool.cc.

References chunks_.

int impala::MemPool::GetCurrentOffset ( ) const
inline

Return logical offset of memory returned by next call to Allocate() into allocated data.

Definition at line 163 of file mem-pool.h.

References total_allocated_bytes_.

Referenced by impala::Tuple::DeepCopy(), and impala::TEST().

uint8_t * impala::MemPool::GetDataPtr ( int  offset)
inline

Given a logical offset into the allocated data (allowed values: 0 - total_allocated_bytes() - 1), return a pointer to that offset.

Definition at line 294 of file mem-pool.h.

References impala::MemPool::ChunkInfo::allocated_bytes, chunks_, impala::MemPool::ChunkInfo::cumulative_allocated_bytes, impala::MemPool::ChunkInfo::data, GetDataPtrHelper(), and last_offset_conversion_chunk_idx_.

Referenced by impala::TEST().

int impala::MemPool::GetFreeOffset ( ) const
inlineprivate

Return offset to unoccpied space in current chunk.

Definition at line 250 of file mem-pool.h.

References chunks_, and current_chunk_idx_.

Referenced by AcquireData().

int impala::MemPool::GetOffset ( uint8_t *  data)
inline

Return logical offset of data ptr into allocated data (interval [0, total_allocated_bytes()) ). Returns -1 if 'data' doesn't belong to this mempool.

Definition at line 283 of file mem-pool.h.

References impala::MemPool::ChunkInfo::allocated_bytes, chunks_, impala::MemPool::ChunkInfo::cumulative_allocated_bytes, impala::MemPool::ChunkInfo::data, GetOffsetHelper(), and last_offset_conversion_chunk_idx_.

Referenced by impala::TEST().

int64_t MemPool::GetTotalChunkSizes ( ) const

Return sum of chunk_sizes_.

Definition at line 257 of file mem-pool.cc.

References chunks_.

Referenced by DebugString(), and impala::TEST().

MemTracker* impala::MemPool::mem_tracker ( )
inline
int64_t impala::MemPool::peak_allocated_bytes ( ) const
inline

Definition at line 149 of file mem-pool.h.

References peak_allocated_bytes_.

Referenced by impala::TEST().

void impala::MemPool::ReturnPartialAllocation ( int  byte_size)
inline

Returns 'byte_size' to the current chunk back to the mem pool. This can only be used to return either all or part of the previous allocation returned by Allocate().

Definition at line 107 of file mem-pool.h.

References impala::MemPool::ChunkInfo::allocated_bytes, chunks_, current_chunk_idx_, impala::MemPool::ChunkInfo::owns_data, and total_allocated_bytes_.

Referenced by impala::TEST().

int64_t impala::MemPool::total_allocated_bytes ( ) const
inline

Definition at line 148 of file mem-pool.h.

References total_allocated_bytes_.

Referenced by impala::RowBatch::AtCapacity(), and impala::TEST().

int64_t impala::MemPool::total_reserved_bytes ( ) const
inline

Definition at line 150 of file mem-pool.h.

References total_reserved_bytes_.

uint8_t* impala::MemPool::TryAllocate ( int  size)
inline

Same as Allocate() except the mem limit is checked before the allocation and this call will fail (returns NULL) if it does. The caller must handle the NULL case. This should be used for allocations where the size can be very big to bound the amount by which we exceed mem limits.

Definition at line 100 of file mem-pool.h.

Friends And Related Function Documentation

friend class MemPoolTest
friend

Definition at line 180 of file mem-pool.h.

Member Data Documentation

int impala::MemPool::chunk_size_
private

Definition at line 217 of file mem-pool.h.

Referenced by CheckIntegrity(), FindChunk(), and MemPool().

int impala::MemPool::current_chunk_idx_
private

chunk from which we served the last Allocate() call; always points to the last chunk that contains allocated data; chunks 0..current_chunk_idx_ are guaranteed to contain data (chunks_[i].allocated_bytes > 0 for i: 0..current_chunk_idx_); -1 if no chunks present

Definition at line 211 of file mem-pool.h.

Referenced by AcquireData(), Allocate(), CheckIntegrity(), Clear(), DebugString(), FindChunk(), FreeAll(), GetFreeOffset(), and ReturnPartialAllocation().

const int MemPool::DEFAULT_INITIAL_CHUNK_SIZE = 4 * 1024
staticprivate

Definition at line 181 of file mem-pool.h.

Referenced by FindChunk().

int impala::MemPool::last_offset_conversion_chunk_idx_
private

chunk where last offset conversion (GetOffset() or GetDataPtr()) took place; -1 if those functions have never been called

Definition at line 215 of file mem-pool.h.

Referenced by FreeAll(), GetDataPtr(), GetDataPtrHelper(), GetOffset(), and GetOffsetHelper().

const char * MemPool::LLVM_CLASS_NAME = "class.impala::MemPool"
static

TODO: make a macro for doing this For C++/IR interop, we need to be able to look up types by name.

Definition at line 177 of file mem-pool.h.

Referenced by impala::HdfsAvroScanner::CodegenMaterializeTuple(), and impala::HdfsScanner::CodegenWriteCompleteTuple().

MemTracker* impala::MemPool::mem_tracker_
private

The current and peak memory footprint of this pool. This is different from total allocated_bytes_ since it includes bytes in chunks that are not used.

Definition at line 232 of file mem-pool.h.

Referenced by AcquireData(), FindChunk(), FreeAll(), and mem_tracker().

int64_t impala::MemPool::peak_allocated_bytes_
private

Maximum number of bytes allocated from this pool at one time.

Definition at line 223 of file mem-pool.h.

Referenced by AcquireData(), Allocate(), and peak_allocated_bytes().

int64_t impala::MemPool::total_allocated_bytes_
private
int64_t impala::MemPool::total_reserved_bytes_
private

sum of all bytes allocated in chunks_

Definition at line 226 of file mem-pool.h.

Referenced by AcquireData(), FindChunk(), FreeAll(), and total_reserved_bytes().


The documentation for this class was generated from the following files: