From f4ff1430f0d6ae7dd5a6be0bd665678b30a63aca Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Fri, 3 Dec 2010 22:16:16 -0700 Subject: first iteration of memory pool code --- indra/llcommon/llmemory.cpp | 955 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 954 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index a502d1a7eb..ca06589611 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -26,8 +26,9 @@ #include "linden_common.h" +#include "llthread.h" #if defined(LL_WINDOWS) -# include +//# include # include #elif defined(LL_DARWIN) # include @@ -38,11 +39,19 @@ #endif #include "llmemory.h" +#include "llsys.h" + //---------------------------------------------------------------------------- //static char* LLMemory::reserveMem = 0; +U32 LLMemory::sAvailPhysicalMemInKB = U32_MAX ; +U32 LLMemory::sMaxPhysicalMemInKB = 0; +U32 LLMemory::sAllocatedMemInKB = 0; +U32 LLMemory::sAllocatedPageSizeInKB = 0 ; +U32 LLMemory::sMaxHeapSizeInKB = U32_MAX ; +BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE; //static void LLMemory::initClass() @@ -67,6 +76,131 @@ void LLMemory::freeReserve() reserveMem = NULL; } +//static +void LLMemory::initMaxHeapSizeGB(F32 max_heap_size_gb, BOOL prevent_heap_failure) +{ + sMaxHeapSizeInKB = (U32)(max_heap_size_gb * 1024 * 1024) ; + sEnableMemoryFailurePrevention = prevent_heap_failure ; +} + +//static +void LLMemory::updateMemoryInfo() +{ +#if LL_WINDOWS + HANDLE self = GetCurrentProcess(); + PROCESS_MEMORY_COUNTERS counters; + + if (!GetProcessMemoryInfo(self, &counters, sizeof(counters))) + { + llwarns << "GetProcessMemoryInfo failed" << llendl; + return ; + } + + sAllocatedMemInKB = (U32)(counters.WorkingSetSize / 1024) ; + sAllocatedPageSizeInKB = (U32)(counters.PagefileUsage / 1024) ; + sMaxPhysicalMemInKB = llmin(LLMemoryInfo::getAvailableMemoryKB() + sAllocatedMemInKB, sMaxHeapSizeInKB); + + if(sMaxPhysicalMemInKB > sAllocatedMemInKB) + { + sAvailPhysicalMemInKB = sMaxPhysicalMemInKB - sAllocatedMemInKB ; + } + else + { + sAvailPhysicalMemInKB = 0 ; + } +#else + //not valid for other systems for now. + sAllocatedMemInKB = (U32)(LLMemory::getCurrentRSS() / 1024) ; + sMaxPhysicalMemInKB = U32_MAX ; + sAvailPhysicalMemInKB = U32_MAX ; +#endif + + return ; +} + +// +//this function is to test if there is enough space with the size in the virtual address space. +//it does not do any real allocation +//if success, it returns the address where the memory chunk can fit in; +//otherwise it returns NULL. +// +//static +void* LLMemory::tryToAlloc(void* address, U32 size) +{ +#if LL_WINDOWS + address = VirtualAlloc(address, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS) ; + if(address) + { + if(!VirtualFree(address, 0, MEM_RELEASE)) + { + llerrs << "error happens when free some memory reservation." << llendl ; + } + } +#else +#endif + + return address ; +} + +//static +void LLMemory::logMemoryInfo(BOOL update) +{ + if(update) + { + updateMemoryInfo() ; + } + + llinfos << "Current allocated physical memory(KB): " << sAllocatedMemInKB << llendl ; + llinfos << "Current allocated page size (KB): " << sAllocatedPageSizeInKB << llendl ; + llinfos << "Current availabe physical memory(KB): " << sAvailPhysicalMemInKB << llendl ; + llinfos << "Current max usable memory(KB): " << sMaxPhysicalMemInKB << llendl ; +} + +//return 0: everything is normal; +//return 1: the memory pool is low, but not in danger; +//return -1: the memory pool is in danger, is about to crash. +//static +S32 LLMemory::isMemoryPoolLow() +{ + static const U32 LOW_MEMEOY_POOL_THRESHOLD_KB = 64 * 1024 ; //64 MB for emergency use + + if(!sEnableMemoryFailurePrevention) + { + return 0 ; //no memory failure prevention. + } + + if(sAvailPhysicalMemInKB < (LOW_MEMEOY_POOL_THRESHOLD_KB >> 2)) //out of physical memory + { + return -1 ; + } + + if(sAllocatedPageSizeInKB + (LOW_MEMEOY_POOL_THRESHOLD_KB >> 2) > sMaxHeapSizeInKB) //out of virtual address space. + { + return -1 ; + } + + return (S32)(sAvailPhysicalMemInKB < LOW_MEMEOY_POOL_THRESHOLD_KB || + sAllocatedPageSizeInKB + LOW_MEMEOY_POOL_THRESHOLD_KB > sMaxHeapSizeInKB) ; +} + +//static +U32 LLMemory::getAvailableMemKB() +{ + return sAvailPhysicalMemInKB ; +} + +//static +U32 LLMemory::getMaxMemKB() +{ + return sMaxPhysicalMemInKB ; +} + +//static +U32 LLMemory::getAllocatedMemKB() +{ + return sAllocatedMemInKB ; +} + void* ll_allocate (size_t size) { if (size == 0) @@ -221,3 +355,822 @@ U64 LLMemory::getCurrentRSS() } #endif + +//------------------------------------------------------------- +//class LLPrivateMemoryPool::LLMemoryBlock +//------------------------------------------------------------- +// +//each memory block could fit for two page sizes: 0.75 * mSlotSize, which starts from the beginning of the memory chunk and grow towards the end of the +//the block; another is mSlotSize, which starts from the end of the block and grows towards the beginning of the block. +// +LLPrivateMemoryPool::LLMemoryBlock::LLMemoryBlock() +{ + //empty +} + +LLPrivateMemoryPool::LLMemoryBlock::~LLMemoryBlock() +{ + //empty +} + +void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 slot_size) +{ + mBuffer = buffer ; + mBufferSize = buffer_size ; + mSlotSize = slot_size ; + mTotalSlots = buffer_size / mSlotSize ; + llassert_always(mTotalSlots < 256) ; //max number is 256 + mAllocatedSlots = 0 ; + + //mark free bits + S32 usage_bit_len = (mTotalSlots + 31) / 32 ; + mDummySize = usage_bit_len - 1 ; + if(mDummySize > 0) //extra space to store mUsageBits + { + mTotalSlots -= (mDummySize * sizeof(mUsageBits) + mSlotSize - 1) / mSlotSize ; + usage_bit_len = (mTotalSlots + 31) / 32 ; + mDummySize = usage_bit_len - 1 ; + + if(mDummySize > 0) + { + mUsageBits = 0 ; + for(S32 i = 0 ; i < mDummySize ; i++) + { + *((U32*)mBuffer + i) = 0 ; + } + if(mTotalSlots & 31) + { + *((U32*)mBuffer + mDummySize - 1) = (0xffffffff << (mTotalSlots & 31)) ; + } + } + } + + if(mDummySize < 1) + { + mUsageBits = 0 ; + if(mTotalSlots & 31) + { + mUsageBits = (0xffffffff << (mTotalSlots & 31)) ; + } + } + + mSelf = NULL ; + mNext = NULL ; +} + +void LLPrivateMemoryPool::LLMemoryBlock::setBuffer(char* buffer, U32 buffer_size) +{ + mBuffer = buffer ; + mBufferSize = buffer_size ; + mTotalSlots = 0 ; //set the block is free. +} + +char* LLPrivateMemoryPool::LLMemoryBlock::allocate() +{ + llassert_always(mAllocatedSlots < mTotalSlots) ; + + //find a free slot + U32* bits = NULL ; + U32 k = 0 ; + if(mUsageBits != 0xffffffff) + { + bits = &mUsageBits ; + } + else if(mDummySize > 0)//go to extra space + { + for(S32 i = 0 ; i < mDummySize; i++) + { + if(*((U32*)mBuffer + i) != 0xffffffff) + { + bits = (U32*)mBuffer + i ; + k = i + 1 ; + break ; + } + } + } + S32 idx = 0 ; + U32 tmp = *bits ; + for(; tmp & 1 ; tmp >>= 1, idx++) ; + + //set the slot reserved + if(!idx) + { + *bits |= 1 ; + } + else + { + *bits |= (1 << idx) ; + } + + mAllocatedSlots++ ; + + return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; +} + +void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr) +{ + U32 idx = ((char*) addr - mBuffer - mDummySize * sizeof(U32)) / mSlotSize ; + + U32* bits = &mUsageBits ; + if(idx > 32) + { + bits = (U32*)mBuffer + (idx - 32) / 32 ; + } + if(idx & 31) + { + *bits &= ~(1 << (idx & 31)) ; + } + else + { + *bits &= ~1 ; + } + + mAllocatedSlots-- ; +} + +//------------------------------------------------------------------- +//class LLMemoryChunk +//-------------------------------------------------------------------- +LLPrivateMemoryPool::LLMemoryChunk::LLMemoryChunk() +{ + //empty +} + +LLPrivateMemoryPool::LLMemoryChunk::~LLMemoryChunk() +{ + //empty +} + +void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 min_slot_size, U32 max_slot_size, U32 min_block_size, U32 max_block_size) +{ + mBuffer = buffer ; + mBufferSize = buffer_size ; + + mMetaBuffer = mBuffer + sizeof(LLMemoryChunk) ; + + mMinBlockSize = min_block_size; + mMaxBlockSize = max_block_size; + mMinSlotSize = min_slot_size; + mBlockLevels = max_block_size / min_block_size ; + mPartitionLevels = mMaxBlockSize / mMinBlockSize + 1 ; + + S32 max_num_blocks = (buffer_size - sizeof(LLMemoryChunk) - mBlockLevels * sizeof(LLMemoryBlock*) - mPartitionLevels * sizeof(LLMemoryBlock*)) / + (mMinBlockSize + sizeof(LLMemoryBlock)) ; + //meta data space + mBlocks = (LLMemoryBlock*)mMetaBuffer ; + mAvailBlockList = (LLMemoryBlock**)((char*)mBlocks + sizeof(LLMemoryBlock) * max_num_blocks) ; + mFreeSpaceList = (LLMemoryBlock**)((char*)mAvailBlockList + sizeof(LLMemoryBlock*) * mBlockLevels) ; + + //data buffer + mDataBuffer = (char*)mFreeSpaceList + sizeof(LLMemoryBlock*) * mPartitionLevels ; + + //init + for(U32 i = 0 ; i < mBlockLevels; i++) + { + mAvailBlockList[i] = NULL ; + } + for(U32 i = 0 ; i < mPartitionLevels ; i++) + { + mFreeSpaceList[i] = NULL ; + } + + mBlocks[0].setBuffer(mDataBuffer, buffer_size - (mDataBuffer - mBuffer)) ; + addToFreeSpace(&mBlocks[0]) ; + + mKey = (U32)mBuffer ; + mNext = NULL ; + mPrev = NULL ; +} + +//static +U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_page_size) +{ + return 2048 + + sizeof(LLMemoryBlock) * (data_buffer_size / min_page_size) ; +} + +char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) +{ + char* p = NULL ; + U32 blk_idx = size / mMinSlotSize ; + if(mMinSlotSize * blk_idx < size) + { + blk_idx++ ; + } + + //check if there is free block available + if(mAvailBlockList[blk_idx]) + { + LLMemoryBlock* blk = mAvailBlockList[blk_idx] ; + p = blk->allocate() ; + + if(blk->isFull()) + { + //removeFromFreelist + popAvailBlockList(blk_idx) ; + } + } + + //ask for a new block + if(!p) + { + LLMemoryBlock* blk = addBlock(blk_idx) ; + if(blk) + { + p = blk->allocate() ; + + if(blk->isFull()) + { + //removeFromFreelist + popAvailBlockList(blk_idx) ; + } + } + } + + //ask for space from higher level blocks + if(!p) + { + for(S32 i = blk_idx + 1 ; i < mBlockLevels; i++) + { + if(mAvailBlockList[i]) + { + LLMemoryBlock* blk = mAvailBlockList[i] ; + p = blk->allocate() ; + + if(blk->isFull()) + { + //removeFromFreelist + popAvailBlockList(i) ; + } + break ; + } + } + } + + return p ; +} + +void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr) +{ + LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + (((char*)addr - mDataBuffer) / mMinBlockSize) * sizeof(LLMemoryBlock)) ; + blk = blk->mSelf ; + + bool was_full = blk->isFull() ; + blk->free(addr) ; + + if(blk->empty()) + { + removeBlock(blk) ; + } + else if(was_full) + { + addToAvailBlockList(blk) ; + } +} + +LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock(U32 blk_idx) +{ + U32 slot_size = mMinSlotSize * (blk_idx + 1) ; + U32 preferred_block_size = llmax(mMinBlockSize, slot_size * 32) ; + preferred_block_size = llmin(preferred_block_size, mMaxBlockSize) ; + + U32 idx = preferred_block_size / mMinBlockSize ; + preferred_block_size = idx * mMinBlockSize ; //round to integer times of mMinBlockSize. + + LLMemoryBlock* blk = NULL ; + + if(mFreeSpaceList[idx])//if there is free slot for blk_idx + { + blk = createNewBlock(&mFreeSpaceList[idx], preferred_block_size, slot_size, blk_idx) ; + } + else if(mFreeSpaceList[mPartitionLevels - 1]) //search free pool + { + blk = createNewBlock(&mFreeSpaceList[mPartitionLevels - 1], preferred_block_size, slot_size, blk_idx) ; + } + else //search for other non-preferred but enough space slot. + { + for(U32 i = idx - 1 ; i >= 0 ; i--) //search the small slots first + { + if(mFreeSpaceList[i]) + { + //create a NEW BLOCK THERE. + if(mFreeSpaceList[i]->getBufferSize() >= slot_size) //at least there is space for one slot. + { + blk = createNewBlock(&mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ; + } + break ; + } + } + + if(!blk) + { + for(U16 i = idx + 1 ; i < mPartitionLevels - 1; i++) //search the large slots + { + if(mFreeSpaceList[i]) + { + //create a NEW BLOCK THERE. + blk = createNewBlock(&mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ; + break ; + } + } + } + } + + return blk ; +} + +LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNewBlock(LLMemoryBlock** cur_idxp, U32 buffer_size, U32 slot_size, U32 blk_idx) +{ + LLMemoryBlock* blk = *cur_idxp ; + + buffer_size = llmin(buffer_size, blk->getBufferSize()) ; + U32 new_free_blk_size = blk->getBufferSize() - buffer_size ; + if(new_free_blk_size < mMinBlockSize) //can not partition the memory into size smaller than mMinBlockSize + { + buffer_size += new_free_blk_size ; + new_free_blk_size = 0 ; + } + blk->init(blk->getBuffer(), buffer_size, slot_size) ; + + if(new_free_blk_size > 0) //cur_idx still has free space + { + LLMemoryBlock* next_blk = blk + (buffer_size / mMinBlockSize) ; + next_blk->setBuffer(blk->getBuffer() + buffer_size, new_free_blk_size) ; + + if(new_free_blk_size > mMaxBlockSize) //stays in the free pool + { + next_blk->mPrev = NULL ; + next_blk->mNext = blk->mNext ; + if(next_blk->mNext) + { + next_blk->mNext->mPrev = next_blk ; + } + *cur_idxp = next_blk ; + } + else + { + *cur_idxp = blk->mNext ; //move to the next slot + (*cur_idxp)->mPrev = NULL ; + + addToFreeSpace(next_blk) ; + } + } + else //move to the next block + { + *cur_idxp = blk->mNext ; + (*cur_idxp)->mPrev = NULL ; + } + + //insert to the available block list... + blk->mNext = NULL ; + blk->mPrev = NULL ; + blk->mSelf = blk ; + mAvailBlockList[blk_idx] = blk ; + + //mark the address map + U32 end = (buffer_size / mMinBlockSize) ; + for(U32 i = 1 ; i < end ; i++) + { + (blk + i)->mSelf = blk ; + } + + return blk ; +} + +void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk) +{ + //remove from the available block list + if(blk->mPrev) + { + blk->mPrev->mNext = blk->mNext ; + } + if(blk->mNext) + { + blk->mNext->mPrev = blk->mPrev ; + } + + //mark it free + blk->setBuffer(blk->getBuffer(), blk->getBufferSize()) ; + + //merge blk with neighbors if possible + if(blk->getBuffer() > mDataBuffer) //has the left neighbor + { + if((blk - 1)->mSelf->isFree()) + { + removeFromFreeSpace((blk - 1)->mSelf); + (blk - 1)->mSelf->setBuffer((blk-1)->mSelf->getBuffer(), (blk-1)->mSelf->getBufferSize() + blk->getBufferSize()) ; + blk = (blk - 1)->mSelf ; + } + } + if(blk->getBuffer() + blk->getBufferSize() < mBuffer + mBufferSize) //has the right neighbor + { + U32 d = blk->getBufferSize() / mMinBlockSize ; + if((blk + d)->isFree()) + { + removeFromFreeSpace(blk + d) ; + blk->setBuffer(blk->getBuffer(), blk->getBufferSize() + (blk + d)->getBufferSize()) ; + } + } + + addToFreeSpace(blk) ; + + return ; +} + +//the top block in the list is full, pop it out of the list +void LLPrivateMemoryPool::LLMemoryChunk::popAvailBlockList(U32 blk_idx) +{ + if(mAvailBlockList[blk_idx]) + { + LLMemoryBlock* next = mAvailBlockList[blk_idx]->mNext ; + next->mPrev = NULL ; + mAvailBlockList[blk_idx]->mNext = NULL ; + mAvailBlockList[blk_idx] = next ; + } +} + +void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) +{ + U16 free_idx = blk->getBufferSize() / mMinBlockSize ; + (blk + free_idx)->mSelf = blk ; //mark the end pointing back to the head. + free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ; + + blk->mNext = mFreeSpaceList[free_idx] ; + if(mFreeSpaceList[free_idx]) + { + mFreeSpaceList[free_idx]->mPrev = blk ; + } + mFreeSpaceList[free_idx] = blk ; + blk->mPrev = NULL ; + blk->mSelf = blk ; + + return ; +} + +void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk) +{ + U16 free_idx = blk->getBufferSize() / mMinBlockSize ; + free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ; + + if(mFreeSpaceList[free_idx] == blk) + { + mFreeSpaceList[free_idx] = blk->mNext ; + } + if(blk->mPrev) + { + blk->mPrev->mNext = blk->mNext ; + } + if(blk->mNext) + { + blk->mNext->mPrev = blk->mPrev ; + } + + return ; +} + +void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk) +{ + U32 blk_idx = blk->getSlotSize() / mMinSlotSize ; + + blk->mNext = mAvailBlockList[blk_idx] ; + if(blk->mNext) + { + blk->mNext->mPrev = blk ; + } + blk->mPrev = NULL ; + + return ; +} + +//------------------------------------------------------------------- +//class LLPrivateMemoryPool +//-------------------------------------------------------------------- +LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : + mMutexp(NULL), + mMaxPoolSize(max_size), + mReservedPoolSize(0) +{ + if(threaded) + { + mMutexp = new LLMutex(NULL) ; + } + + for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) + { + mChunkList[i] = NULL ; + } + + mChunkVectorCapacity = 128 ; + mChunks.resize(mChunkVectorCapacity) ; //at most 128 chunks + mNumOfChunks = 0 ; +} + +LLPrivateMemoryPool::~LLPrivateMemoryPool() +{ + destroyPool(); + delete mMutexp ; +} + +char* LLPrivateMemoryPool::allocate(U32 size) +{ + const static U32 MAX_BLOCK_SIZE = 4 * 1024 * 1024 ; //4MB + + //if the asked size larger than MAX_BLOCK_SIZE, fetch from heap directly, the pool does not manage it + if(size >= MAX_BLOCK_SIZE) + { + return new char[size] ; + } + + char* p = NULL ; + + //find the appropriate chunk + S32 chunk_idx = getChunkIndex(size) ; + + lock() ; + + LLMemoryChunk* chunk = mChunkList[chunk_idx]; + while(chunk) + { + if(p = chunk->allocate(size)) + { + break ; + } + chunk = chunk->mNext ; + } + + //fetch new memory chunk + if(!p) + { + chunk = addChunk(chunk_idx) ; + p = chunk->allocate(size) ; + } + + unlock() ; + + return p ; +} + +void LLPrivateMemoryPool::free(void* addr) +{ + lock() ; + + LLMemoryChunk* chunk = mChunks[findChunk((char*)addr)] ; + if(!chunk) + { + delete[] (char*)addr ; //release from heap + } + else + { + chunk->free(addr) ; + + if(chunk->empty()) + { + removeChunk(chunk) ; + } + } + + unlock() ; +} + +void LLPrivateMemoryPool::dump() +{ +} + +void LLPrivateMemoryPool::lock() +{ + if(mMutexp) + { + mMutexp->lock() ; + } +} + +void LLPrivateMemoryPool::unlock() +{ + if(mMutexp) + { + mMutexp->unlock() ; + } +} + +S32 LLPrivateMemoryPool::getChunkIndex(U32 size) +{ + if(size < 2048) + { + return 0 ; + } + else if(size < (512 << 10)) + { + return 1 ; + } + else + { + return 2 ; + } +} + +//destroy the entire pool +void LLPrivateMemoryPool::destroyPool() +{ + for(U16 i = 0 ; i < mNumOfChunks ; i++) + { + delete[] mChunks[i]->getBuffer() ; + } + mNumOfChunks = 0 ; + for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) + { + mChunkList[i] = NULL ; + } +} + +LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_index) +{ + static const U32 MIN_BLOCK_SIZES[SUPER_ALLOCATION] = {2 << 10, 32 << 10, 64 << 10} ; + static const U32 MAX_BLOCK_SIZES[SUPER_ALLOCATION] = {64 << 10, 1 << 20, 4 << 20} ; + static const U32 MIN_SLOT_SIZES[SUPER_ALLOCATION] = {8, 2 << 10, 512 << 10}; + static const U32 MAX_SLOT_SIZES[SUPER_ALLOCATION] = {(2 << 10) - 8, (512 - 2) << 10, 4 << 20}; + + U32 preferred_size ; + U32 overhead ; + if(chunk_index < LARGE_ALLOCATION) + { + preferred_size = (4 << 20) ; //4MB + overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; + } + else + { + preferred_size = (16 << 20) ; //16MB + overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; + } + char* buffer = new(std::nothrow) char[preferred_size + overhead] ; + if(!buffer) + { + return NULL ; + } + + LLMemoryChunk* chunk = new (buffer) LLMemoryChunk() ; + chunk->init(buffer, preferred_size + overhead, MIN_SLOT_SIZES[chunk_index], + MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ; + + //add to the head of the linked list + chunk->mNext = mChunkList[chunk_index] ; + if(mChunkList[chunk_index]) + { + mChunkList[chunk_index]->mPrev = chunk ; + } + chunk->mPrev = NULL ; + mChunkList[chunk_index] = chunk ; + + //insert into the array + llassert_always(mNumOfChunks + 1 < mChunkVectorCapacity) ; + if(!mNumOfChunks) + { + mChunks[0] = chunk ; + } + else + { + U16 k ; + if(mChunks[0]->getBuffer() > chunk->getBuffer()) + { + k = 0 ; + } + else + { + k = findChunk(chunk->getBuffer()) + 1 ; + } + for(U16 i = mNumOfChunks ; i > k ; i++) + { + mChunks[i] = mChunks[i-1] ; + } + mChunks[k] = chunk ; + } + mNumOfChunks++; + + return chunk ; +} + +void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk) +{ + //remove from the linked list + if(chunk->mPrev) + { + chunk->mPrev->mNext = chunk->mNext ; + } + if(chunk->mNext) + { + chunk->mNext->mPrev = chunk->mPrev ; + } + + //remove from the array + U16 k = findChunk(chunk->getBuffer()) ; + mNumOfChunks--; + for(U16 i = k ; i < mNumOfChunks ; i++) + { + mChunks[i] = mChunks[i+1] ; + } + + //release memory + delete[] chunk->getBuffer() ; +} + +U16 LLPrivateMemoryPool::findChunk(const char* addr) +{ + llassert_always(mNumOfChunks > 0) ; + + U16 s = 0, e = mNumOfChunks; + U16 k = (s + e) / 2 ; + while(s < e) + { + if(mChunks[k]->mKey > (U32)addr) + { + e = k ; + } + else if(k < mNumOfChunks - 1 && mChunks[k+1]->mKey < (U32)addr) + { + s = k ; + } + else + { + break ; + } + + k = (s + e) / 2 ; + } + + return k ; +} + +//-------------------------------------------------------------------- +//class LLPrivateMemoryPoolTester +LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::sInstance = NULL ; +LLPrivateMemoryPool* LLPrivateMemoryPoolTester::sPool = NULL ; +LLPrivateMemoryPoolTester::LLPrivateMemoryPoolTester() +{ +} + +LLPrivateMemoryPoolTester::~LLPrivateMemoryPoolTester() +{ +} + +//static +LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::getInstance() +{ + if(!sInstance) + { + sInstance = new LLPrivateMemoryPoolTester() ; + } + return sInstance ; +} + +//static +void LLPrivateMemoryPoolTester::destroy() +{ + if(sInstance) + { + delete sInstance ; + sInstance = NULL ; + } + + if(sPool) + { + delete sPool ; + sPool = NULL ; + } +} + +void LLPrivateMemoryPoolTester::run() +{ + const U32 max_pool_size = 16 << 20 ; + const bool threaded = false ; + if(!sPool) + { + sPool = new LLPrivateMemoryPool(max_pool_size, threaded) ; + } + + //run the test + correctnessTest() ; + reliabilityTest() ; + performanceTest() ; + fragmentationtest() ; +} + +void LLPrivateMemoryPoolTester::correctnessTest() +{ + //try many different sized allocation, fill the memory fully to see if allocation is right. + +} + +void LLPrivateMemoryPoolTester::reliabilityTest() +void LLPrivateMemoryPoolTester::performanceTest() +void LLPrivateMemoryPoolTester::fragmentationtest() + +void* LLPrivateMemoryPoolTester::operator new(size_t size) +{ + return (void*)sPool->allocate(size) ; +} + +void LLPrivateMemoryPoolTester::operator delete(void* addr) +{ + sPool->free(addr) ; +} + +//-------------------------------------------------------------------- -- cgit v1.2.3 From 43f4429363e63484f35663c10ca993d0d812e855 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 8 Dec 2010 20:50:39 -0700 Subject: test code and some code change --- indra/llcommon/llmemory.cpp | 251 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 212 insertions(+), 39 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index ca06589611..a659e84309 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -40,7 +40,7 @@ #include "llmemory.h" #include "llsys.h" - +#include "llframetimer.h" //---------------------------------------------------------------------------- @@ -505,6 +505,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 { mBuffer = buffer ; mBufferSize = buffer_size ; + mAlloatedSize = 0 ; mMetaBuffer = mBuffer + sizeof(LLMemoryChunk) ; @@ -552,18 +553,16 @@ U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) { char* p = NULL ; - U32 blk_idx = size / mMinSlotSize ; - if(mMinSlotSize * blk_idx < size) - { - blk_idx++ ; - } + U32 blk_idx = getBlockLevel(size); + + LLMemoryBlock* blk = NULL ; //check if there is free block available if(mAvailBlockList[blk_idx]) { - LLMemoryBlock* blk = mAvailBlockList[blk_idx] ; + blk = mAvailBlockList[blk_idx] ; p = blk->allocate() ; - + if(blk->isFull()) { //removeFromFreelist @@ -574,7 +573,7 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) //ask for a new block if(!p) { - LLMemoryBlock* blk = addBlock(blk_idx) ; + blk = addBlock(blk_idx) ; if(blk) { p = blk->allocate() ; @@ -594,7 +593,7 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) { if(mAvailBlockList[i]) { - LLMemoryBlock* blk = mAvailBlockList[i] ; + blk = mAvailBlockList[i] ; p = blk->allocate() ; if(blk->isFull()) @@ -607,16 +606,23 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) } } + if(p && blk) + { + mAlloatedSize += blk->getSlotSize() ; + } return p ; } void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr) { - LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + (((char*)addr - mDataBuffer) / mMinBlockSize) * sizeof(LLMemoryBlock)) ; + U32 blk_idx = ((U32)addr - (U32)mDataBuffer) / mMinBlockSize ; + if(blk_idx > 0) blk_idx-- ; + LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ; blk = blk->mSelf ; bool was_full = blk->isFull() ; blk->free(addr) ; + mAlloatedSize -= blk->getSlotSize() ; if(blk->empty()) { @@ -628,13 +634,18 @@ void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr) } } +bool LLPrivateMemoryPool::LLMemoryChunk::empty() +{ + return !mAlloatedSize ; +} + LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock(U32 blk_idx) { U32 slot_size = mMinSlotSize * (blk_idx + 1) ; U32 preferred_block_size = llmax(mMinBlockSize, slot_size * 32) ; preferred_block_size = llmin(preferred_block_size, mMaxBlockSize) ; - U32 idx = preferred_block_size / mMinBlockSize ; + U32 idx = preferred_block_size / mMinBlockSize - 1; preferred_block_size = idx * mMinBlockSize ; //round to integer times of mMinBlockSize. LLMemoryBlock* blk = NULL ; @@ -710,7 +721,10 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNe else { *cur_idxp = blk->mNext ; //move to the next slot - (*cur_idxp)->mPrev = NULL ; + if(*cur_idxp) + { + (*cur_idxp)->mPrev = NULL ; + } addToFreeSpace(next_blk) ; } @@ -718,7 +732,10 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNe else //move to the next block { *cur_idxp = blk->mNext ; - (*cur_idxp)->mPrev = NULL ; + if(*cur_idxp) + { + (*cur_idxp)->mPrev = NULL ; + } } //insert to the available block list... @@ -791,7 +808,9 @@ void LLPrivateMemoryPool::LLMemoryChunk::popAvailBlockList(U32 blk_idx) void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) { - U16 free_idx = blk->getBufferSize() / mMinBlockSize ; + U16 free_idx = blk->getBufferSize() / mMinBlockSize; + if(free_idx > 0) free_idx--; + (blk + free_idx)->mSelf = blk ; //mark the end pointing back to the head. free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ; @@ -809,7 +828,8 @@ void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk) { - U16 free_idx = blk->getBufferSize() / mMinBlockSize ; + U16 free_idx = blk->getBufferSize() / mMinBlockSize; + if(free_idx > 0) free_idx-- ; free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ; if(mFreeSpaceList[free_idx] == blk) @@ -830,7 +850,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk) void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk) { - U32 blk_idx = blk->getSlotSize() / mMinSlotSize ; + U32 blk_idx = getBlockLevel(blk->getSlotSize()); blk->mNext = mAvailBlockList[blk_idx] ; if(blk->mNext) @@ -842,6 +862,16 @@ void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk) return ; } +U32 LLPrivateMemoryPool::LLMemoryChunk::getBlockLevel(U32 size) +{ + return (size + mMinSlotSize - 1) / mMinSlotSize - 1 ; +} + +U32 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size) +{ + return (size + mMinBlockSize - 1) / mMinBlockSize - 1 ; +} + //------------------------------------------------------------------- //class LLPrivateMemoryPool //-------------------------------------------------------------------- @@ -875,6 +905,11 @@ char* LLPrivateMemoryPool::allocate(U32 size) { const static U32 MAX_BLOCK_SIZE = 4 * 1024 * 1024 ; //4MB + if(!size) + { + return NULL ; + } + //if the asked size larger than MAX_BLOCK_SIZE, fetch from heap directly, the pool does not manage it if(size >= MAX_BLOCK_SIZE) { @@ -902,7 +937,10 @@ char* LLPrivateMemoryPool::allocate(U32 size) if(!p) { chunk = addChunk(chunk_idx) ; - p = chunk->allocate(size) ; + if(chunk) + { + p = chunk->allocate(size) ; + } } unlock() ; @@ -912,6 +950,11 @@ char* LLPrivateMemoryPool::allocate(U32 size) void LLPrivateMemoryPool::free(void* addr) { + if(!addr) + { + return ; + } + lock() ; LLMemoryChunk* chunk = mChunks[findChunk((char*)addr)] ; @@ -1116,7 +1159,7 @@ LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::getInstance() { if(!sInstance) { - sInstance = new LLPrivateMemoryPoolTester() ; + sInstance = ::new LLPrivateMemoryPoolTester() ; } return sInstance ; } @@ -1126,51 +1169,181 @@ void LLPrivateMemoryPoolTester::destroy() { if(sInstance) { - delete sInstance ; + ::delete sInstance ; sInstance = NULL ; } if(sPool) { - delete sPool ; + ::delete sPool ; sPool = NULL ; } } -void LLPrivateMemoryPoolTester::run() +void LLPrivateMemoryPoolTester::run(bool threaded) { const U32 max_pool_size = 16 << 20 ; - const bool threaded = false ; - if(!sPool) + + if(sPool) { - sPool = new LLPrivateMemoryPool(max_pool_size, threaded) ; + ::delete sPool ; } + sPool = ::new LLPrivateMemoryPool(max_pool_size, threaded) ; //run the test correctnessTest() ; - reliabilityTest() ; performanceTest() ; fragmentationtest() ; + + //release pool. + ::delete sPool ; + sPool = NULL ; +} + +void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 times, + bool random_deletion, bool output_statistics) +{ + U32 levels = (max_size - min_size) / stride + 1 ; + char*** p ; + U32 i, j ; + + //allocate space for p ; + if(!(p = ::new char**[times]) || !(*p = ::new char*[times * levels])) + { + llerrs << "memory initialization for p failed" << llendl ; + } + + //init + for(i = 0 ; i < times; i++) + { + p[i] = *p + i * levels ; + for(j = 0 ; j < levels; j++) + { + p[i][j] = NULL ; + } + } + + //allocation + U32 size ; + for(i = 0 ; i < times ; i++) + { + for(j = 0 ; j < levels; j++) + { + size = min_size + j * stride ; + p[i][j] = sPool->allocate(size) ; + p[i][j][size - 1] = '\0' ; //access the last element to verify the success of the allocation. + + //randomly release memory + if(random_deletion) + { + S32 k = rand() % levels ; + sPool->free(p[i][k]) ; + p[i][k] = NULL ; + } + } + } + + //output pool allocation statistics + if(output_statistics) + { + } + + //release all memory allocations + for(i = 0 ; i < times; i++) + { + for(j = 0 ; j < levels; j++) + { + sPool->free(p[i][j]) ; + p[i][j] = NULL ; + } + } + + ::delete[] *p ; + ::delete[] p ; } void LLPrivateMemoryPoolTester::correctnessTest() { - //try many different sized allocation, fill the memory fully to see if allocation is right. + //try many different sized allocation, and all kinds of edge cases, access the allocated memory + //to see if allocation is right. + + //edge case + char* p = sPool->allocate(0) ; + sPool->free(p) ; + + //small sized + // [8 bytes, 2KB), each asks for 256 allocations and deallocations + test(8, 2040, 8, 256, true, true) ; + + //medium sized + //[2KB, 512KB), each asks for 16 allocations and deallocations + test(2048, 512 * 1024 - 2048, 2048, 16, true, true) ; + //large sized + //[512KB, 4MB], each asks for 8 allocations and deallocations + test(512 * 1024, 4 * 1024 * 1024, 64 * 1024, 8, true, true) ; } -void LLPrivateMemoryPoolTester::reliabilityTest() void LLPrivateMemoryPoolTester::performanceTest() +{ + U32 test_size[3] = {768, 3* 1024, 3* 1024 * 1024}; + + S32 i ; + LLFrameTimer timer ; + + //do 1024 various-sized allocations / deallocations, compare the performance with the normal ones. + + //small sized + { + timer.reset() ; + char* p[1024] = {NULL} ; + for(i = 0 ; i < 1024; i++) + { + p[i] = sPool->allocate(test_size[0]) ; + if(!p[i]) + { + llerrs << "allocation failed" << llendl ; + } + } + + for(i = 0 ; i < 1024; i++) + { + sPool->free(p[i]) ; + p[i] = NULL ; + } + llinfos << "time spent on 1024 small allocations: %f " << timer.getElapsedTimeF32() << llendl ; + + timer.reset() ; + + //using the standard allocator/de-allocator: + for(i = 0 ; i < 1024; i++) + { + p[i] = ::new char[test_size[0]] ; + if(!p[i]) + { + llerrs << "allocation failed" << llendl ; + } + } + + for(i = 0 ; i < 1024; i++) + { + ::delete[] p[i] ; + p[i] = NULL ; + } + llinfos << "time spent on 1024 small allocations: %f using standard allocator/de-allocator." << timer.getElapsedTimeF32() << llendl ; + + timer.reset() ; + } + //medium sized + + //large sized +} + void LLPrivateMemoryPoolTester::fragmentationtest() +{ + //for internal fragmentation statistics: + //every time when asking for a new chunk during correctness test, and performance test, + //print out the chunk usage statistices. +} -void* LLPrivateMemoryPoolTester::operator new(size_t size) -{ - return (void*)sPool->allocate(size) ; -} - -void LLPrivateMemoryPoolTester::operator delete(void* addr) -{ - sPool->free(addr) ; -} - -//-------------------------------------------------------------------- +//-------------------------------------------------------------------- -- cgit v1.2.3 From 5654abd50d834c3a7d0efb5dde393ff34f09be17 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 4 Jan 2011 13:14:36 -0700 Subject: a wroking version with a lot of debugging code (to be removed). --- indra/llcommon/llmemory.cpp | 770 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 599 insertions(+), 171 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index a659e84309..00ef09d7a2 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -356,6 +356,22 @@ U64 LLMemory::getCurrentRSS() #endif +//------------------------------------------------------------- +//minimum block sizes (page size) for small allocation, medium allocation, large allocation +const U32 MIN_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {2 << 10, 4 << 10, 16 << 10} ; // + +//maximum block sizes for small allocation, medium allocation, large allocation +const U32 MAX_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {64 << 10, 1 << 20, 4 << 20} ; + +//minimum slot sizes for small allocation, medium allocation, large allocation +const U32 MIN_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {8, 2 << 10, 512 << 10}; + +//maximum slot sizes for small allocation, medium allocation, large allocation +const U32 MAX_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {(2 << 10) - 8, (512 - 2) << 10, 4 << 20}; + +//size of a block with multiple slots can not exceed CUT_OFF_SIZE +const U32 CUT_OFF_SIZE = (64 << 10) ; //64 KB + //------------------------------------------------------------- //class LLPrivateMemoryPool::LLMemoryBlock //------------------------------------------------------------- @@ -375,6 +391,8 @@ LLPrivateMemoryPool::LLMemoryBlock::~LLMemoryBlock() void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 slot_size) { + llassert_always(buffer_size >= slot_size) ; + mBuffer = buffer ; mBufferSize = buffer_size ; mSlotSize = slot_size ; @@ -414,14 +432,20 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 } } - mSelf = NULL ; + mSelf = this ; mNext = NULL ; + mPrev = NULL ; + + llassert_always(mTotalSlots > 0) ; } void LLPrivateMemoryPool::LLMemoryBlock::setBuffer(char* buffer, U32 buffer_size) { + llassert_always(buffer_size <= (16 << 20)) ; + mBuffer = buffer ; mBufferSize = buffer_size ; + mSelf = NULL ; mTotalSlots = 0 ; //set the block is free. } @@ -455,27 +479,47 @@ char* LLPrivateMemoryPool::LLMemoryBlock::allocate() //set the slot reserved if(!idx) { + llassert_always(!(*bits & 1)); *bits |= 1 ; } else { + llassert_always(!(*bits & (1 << idx))) ; *bits |= (1 << idx) ; } mAllocatedSlots++ ; + + //return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; - return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; + char* p = mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; + llassert_always(mBuffer != p || !mDummySize) ; + llassert_always(*(U32*)p == 0 && *((U32*)p + 1) == 0) ; + + return p ; } +U32 col = 0, row = 0 ; void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr) { - U32 idx = ((char*) addr - mBuffer - mDummySize * sizeof(U32)) / mSlotSize ; + llassert_always((U32)addr >= (U32)mBuffer + mDummySize * sizeof(U32) && + (U32)addr < (U32)mBuffer + mBufferSize) ; + + U32 idx = ((U32)addr - (U32)mBuffer - mDummySize * sizeof(U32)) / mSlotSize ; + + llassert_always(idx < mTotalSlots) ; + llassert_always(addr == mBuffer + mDummySize * sizeof(U32) + idx * mSlotSize) ; + llassert_always(*(U32*)addr == col && *((U32*)addr + 1) == row) ; + + *(U32*)addr = 0 ; + *((U32*)addr + 1) = 0 ; U32* bits = &mUsageBits ; - if(idx > 32) + if(idx >= 32) { bits = (U32*)mBuffer + (idx - 32) / 32 ; } + if(idx & 31) { *bits &= ~(1 << (idx & 31)) ; @@ -488,6 +532,15 @@ void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr) mAllocatedSlots-- ; } +//for debug use +void LLPrivateMemoryPool::LLMemoryBlock::resetBitMap() +{ + for(S32 i = 0 ; i < mDummySize ; i++) + { + *((U32*)mBuffer + i) = 0 ; + } + mUsageBits = 0 ; +} //------------------------------------------------------------------- //class LLMemoryChunk //-------------------------------------------------------------------- @@ -510,10 +563,10 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 mMetaBuffer = mBuffer + sizeof(LLMemoryChunk) ; mMinBlockSize = min_block_size; - mMaxBlockSize = max_block_size; mMinSlotSize = min_slot_size; - mBlockLevels = max_block_size / min_block_size ; - mPartitionLevels = mMaxBlockSize / mMinBlockSize + 1 ; + mMaxSlotSize = max_slot_size ; + mBlockLevels = mMaxSlotSize / mMinSlotSize ; + mPartitionLevels = max_block_size / mMinBlockSize + 1 ; S32 max_num_blocks = (buffer_size - sizeof(LLMemoryChunk) - mBlockLevels * sizeof(LLMemoryBlock*) - mPartitionLevels * sizeof(LLMemoryBlock*)) / (mMinBlockSize + sizeof(LLMemoryBlock)) ; @@ -535,10 +588,20 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 mFreeSpaceList[i] = NULL ; } + mBlocks[0].mPrev = NULL ; + mBlocks[0].mNext = NULL ; mBlocks[0].setBuffer(mDataBuffer, buffer_size - (mDataBuffer - mBuffer)) ; + + //debug + U32 end = (mBlocks[0].getBufferSize() / mMinBlockSize) ; + for(U32 i = 1 ; i < end ; i++) + { + mBlocks[i].mSelf = NULL ; + } + addToFreeSpace(&mBlocks[0]) ; - mKey = (U32)mBuffer ; + mHashNext = NULL ; mNext = NULL ; mPrev = NULL ; } @@ -546,8 +609,14 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 //static U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_page_size) { - return 2048 + - sizeof(LLMemoryBlock) * (data_buffer_size / min_page_size) ; + if(data_buffer_size / min_page_size < 64) //large allocations + { + return 4096 ; //4KB + } + else + { + return 0 ; //do not reserve extra overhead if for small allocations + } } char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) @@ -565,7 +634,6 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) if(blk->isFull()) { - //removeFromFreelist popAvailBlockList(blk_idx) ; } } @@ -580,7 +648,6 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) if(blk->isFull()) { - //removeFromFreelist popAvailBlockList(blk_idx) ; } } @@ -598,7 +665,6 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) if(blk->isFull()) { - //removeFromFreelist popAvailBlockList(i) ; } break ; @@ -606,8 +672,18 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) } } + llassert_always(!p || blk) ; + if(p && blk) { + if(blk->getTotalSlots() == 1) + { + llassert_always(blk->getBuffer() == (char*)p) ; + } + U32 blk_idx = getPageIndex((U32)p) ; + LLMemoryBlock* b = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ; + llassert_always(blk == b || b->mSelf == blk) ; + mAlloatedSize += blk->getSlotSize() ; } return p ; @@ -615,23 +691,34 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr) { - U32 blk_idx = ((U32)addr - (U32)mDataBuffer) / mMinBlockSize ; - if(blk_idx > 0) blk_idx-- ; + U32 blk_idx = getPageIndex((U32)addr) ; LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ; + llassert_always(blk->mSelf) ; blk = blk->mSelf ; + llassert_always(addr >= blk->getBuffer() && addr < blk->getBuffer() + blk->getBufferSize()) ; + if(blk->getTotalSlots() == 1) + { + llassert_always(blk->getBuffer() == (char*)addr) ; + } + bool was_full = blk->isFull() ; blk->free(addr) ; mAlloatedSize -= blk->getSlotSize() ; if(blk->empty()) { + blk->resetBitMap() ; //debug use removeBlock(blk) ; + + dump(); } else if(was_full) { addToAvailBlockList(blk) ; - } + + dump(); + } } bool LLPrivateMemoryPool::LLMemoryChunk::empty() @@ -639,35 +726,170 @@ bool LLPrivateMemoryPool::LLMemoryChunk::empty() return !mAlloatedSize ; } +bool LLPrivateMemoryPool::LLMemoryChunk::containsAddress(const char* addr) const +{ + return (U32)mBuffer <= (U32)addr && (U32)mBuffer + mBufferSize > (U32)addr ; +} + +void LLPrivateMemoryPool::LLMemoryChunk::dump() +{ + //sanity check + std::vector< LLMemoryBlock* > blk_list ; + for(std::set::iterator iter = mActiveBlockList.begin() ; iter != mActiveBlockList.end(); ++iter) + { + blk_list.push_back(*iter) ; + } + //for(S32 i = 0 ; i < mBlockLevels ; i++) + //{ + // LLMemoryBlock* blk = mAvailBlockList[i] ; + // while(blk) + // { + // blk_list.push_back(blk) ; + // blk = blk->mNext ; + // } + //} + for(S32 i = 0 ; i < mPartitionLevels ; i++) + { + LLMemoryBlock* blk = mFreeSpaceList[i] ; + while(blk) + { + blk_list.push_back(blk) ; + blk = blk->mNext ; + } + } + + std::sort(blk_list.begin(), blk_list.end(), LLMemoryBlock::CompareAddress()); + + U32 total_size = blk_list[0]->getBufferSize() ; + for(U32 i = 1 ; i < blk_list.size(); i++) + { + total_size += blk_list[i]->getBufferSize() ; + if((U32)blk_list[i]->getBuffer() < (U32)blk_list[i-1]->getBuffer() + blk_list[i-1]->getBufferSize()) + { + llerrs << "buffer corrupted." << llendl ; + } + } + + llassert_always(total_size + mMinBlockSize >= mBufferSize - ((U32)mDataBuffer - (U32)mBuffer)) ; + + U32 blk_num = (mBufferSize - (mDataBuffer - mBuffer)) / mMinBlockSize ; + for(U32 i = 0 ; i < blk_num ; ) + { + LLMemoryBlock* blk = &mBlocks[i] ; + if(blk->mSelf) + { + U32 end = blk->getBufferSize() / mMinBlockSize ; + for(U32 j = 0 ; j < end ; j++) + { + llassert_always(blk->mSelf == blk || !blk->mSelf) ; + } + i += end ; + } + else + { + llerrs << "gap happens" << llendl ; + } + } +#if 0 + llinfos << "---------------------------" << llendl ; + llinfos << "Chunk buffer: " << (U32)getBuffer() << " size: " << getBufferSize() << llendl ; + + llinfos << "available blocks ... " << llendl ; + for(S32 i = 0 ; i < mBlockLevels ; i++) + { + LLMemoryBlock* blk = mAvailBlockList[i] ; + while(blk) + { + llinfos << "blk buffer " << (U32)blk->getBuffer() << " size: " << blk->getBufferSize() << llendl ; + blk = blk->mNext ; + } + } + + llinfos << "free blocks ... " << llendl ; + for(S32 i = 0 ; i < mPartitionLevels ; i++) + { + LLMemoryBlock* blk = mFreeSpaceList[i] ; + while(blk) + { + llinfos << "blk buffer " << (U32)blk->getBuffer() << " size: " << blk->getBufferSize() << llendl ; + blk = blk->mNext ; + } + } +#endif +} + +U32 LLPrivateMemoryPool::LLMemoryChunk::calcBlockSize(U32 slot_size) +{ + // + //Note: we try to make a block to have 32 slots if the size is not over 32 pages + //32 is the number of bits of an integer in a 32-bit system + // + + U32 block_size; + U32 cut_off_size = llmin(CUT_OFF_SIZE, (U32)(mMinBlockSize << 5)) ; + + if((slot_size << 5) <= mMinBlockSize)//for small allocations, return one page + { + block_size = mMinBlockSize ; + } + else if(slot_size >= cut_off_size)//for large allocations, return one-slot block + { + block_size = (slot_size / mMinBlockSize) * mMinBlockSize ; + if(block_size < slot_size) + { + block_size += mMinBlockSize ; + } + } + else //medium allocations + { + if((slot_size << 5) >= cut_off_size) + { + block_size = cut_off_size ; + } + else + { + block_size = ((slot_size << 5) / mMinBlockSize) * mMinBlockSize ; + } + } + + llassert_always(block_size >= slot_size) ; + + return block_size ; +} + LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock(U32 blk_idx) { U32 slot_size = mMinSlotSize * (blk_idx + 1) ; - U32 preferred_block_size = llmax(mMinBlockSize, slot_size * 32) ; - preferred_block_size = llmin(preferred_block_size, mMaxBlockSize) ; - - U32 idx = preferred_block_size / mMinBlockSize - 1; - preferred_block_size = idx * mMinBlockSize ; //round to integer times of mMinBlockSize. + U32 preferred_block_size = calcBlockSize(slot_size) ; + + U16 idx = getPageLevel(preferred_block_size); + llassert_always(idx < mPartitionLevels - 1) ; + llassert_always(preferred_block_size == (idx + 1) * mMinBlockSize) ; //round to integer times of mMinBlockSize. LLMemoryBlock* blk = NULL ; if(mFreeSpaceList[idx])//if there is free slot for blk_idx { - blk = createNewBlock(&mFreeSpaceList[idx], preferred_block_size, slot_size, blk_idx) ; + blk = createNewBlock(mFreeSpaceList[idx], preferred_block_size, slot_size, blk_idx) ; } else if(mFreeSpaceList[mPartitionLevels - 1]) //search free pool { - blk = createNewBlock(&mFreeSpaceList[mPartitionLevels - 1], preferred_block_size, slot_size, blk_idx) ; + blk = createNewBlock(mFreeSpaceList[mPartitionLevels - 1], preferred_block_size, slot_size, blk_idx) ; } else //search for other non-preferred but enough space slot. { - for(U32 i = idx - 1 ; i >= 0 ; i--) //search the small slots first + for(S32 i = (S32)idx - 1 ; i >= 0 ; i--) //search the small slots first { if(mFreeSpaceList[i]) { + U32 new_preferred_block_size = mFreeSpaceList[i]->getBufferSize(); + new_preferred_block_size = (new_preferred_block_size / mMinBlockSize) * mMinBlockSize ; //round to integer times of mMinBlockSize. + //create a NEW BLOCK THERE. - if(mFreeSpaceList[i]->getBufferSize() >= slot_size) //at least there is space for one slot. + if(new_preferred_block_size >= slot_size) //at least there is space for one slot. { - blk = createNewBlock(&mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ; + + blk = createNewBlock(mFreeSpaceList[i], new_preferred_block_size, slot_size, blk_idx) ; } break ; } @@ -680,70 +902,72 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock if(mFreeSpaceList[i]) { //create a NEW BLOCK THERE. - blk = createNewBlock(&mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ; + blk = createNewBlock(mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ; break ; } } } } + dump() ; + return blk ; } -LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNewBlock(LLMemoryBlock** cur_idxp, U32 buffer_size, U32 slot_size, U32 blk_idx) +char* _prev = NULL ; +LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNewBlock(LLMemoryBlock* blk, U32 buffer_size, U32 slot_size, U32 blk_idx) { - LLMemoryBlock* blk = *cur_idxp ; - - buffer_size = llmin(buffer_size, blk->getBufferSize()) ; - U32 new_free_blk_size = blk->getBufferSize() - buffer_size ; - if(new_free_blk_size < mMinBlockSize) //can not partition the memory into size smaller than mMinBlockSize + llassert_always(blk->getBufferSize() >= buffer_size) ; + + //debug { - buffer_size += new_free_blk_size ; - new_free_blk_size = 0 ; + { + U32 blk_idx = getPageIndex((U32)blk->getBuffer()) ; + llassert_always(blk == (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock))) ; + } + U32 end = (blk->getBufferSize() / mMinBlockSize) ; + llassert_always(blk->mSelf == blk && blk->isFree()) ; + llassert_always((blk + end - 1)->mSelf == blk) ; + for(U32 i = 1 ; i < end - 1; i++) + { + llassert_always(!(blk + i)->mSelf) ; + } } - blk->init(blk->getBuffer(), buffer_size, slot_size) ; - - if(new_free_blk_size > 0) //cur_idx still has free space + + //unlink from the free space + removeFromFreeSpace(blk) ; + + //check the rest space + U32 new_free_blk_size = blk->getBufferSize() - buffer_size ; + if(new_free_blk_size < mMinBlockSize) //can not partition the memory into size smaller than mMinBlockSize + { + new_free_blk_size = 0 ; //discard the last small extra space. + } + + //add the rest space back to the free list + if(new_free_blk_size > 0) //blk still has free space { LLMemoryBlock* next_blk = blk + (buffer_size / mMinBlockSize) ; next_blk->setBuffer(blk->getBuffer() + buffer_size, new_free_blk_size) ; - - if(new_free_blk_size > mMaxBlockSize) //stays in the free pool - { - next_blk->mPrev = NULL ; - next_blk->mNext = blk->mNext ; - if(next_blk->mNext) - { - next_blk->mNext->mPrev = next_blk ; - } - *cur_idxp = next_blk ; - } - else - { - *cur_idxp = blk->mNext ; //move to the next slot - if(*cur_idxp) - { - (*cur_idxp)->mPrev = NULL ; - } - addToFreeSpace(next_blk) ; - } - } - else //move to the next block - { - *cur_idxp = blk->mNext ; - if(*cur_idxp) { - (*cur_idxp)->mPrev = NULL ; + U32 blk_idx = getPageIndex((U32)next_blk->getBuffer()) ; + llassert_always(next_blk == (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock))) ; } + llassert_always(buffer_size == (buffer_size / mMinBlockSize) * mMinBlockSize) ; + llassert_always(((U32)next_blk->getBuffer() - (U32)mDataBuffer) == ((U32)next_blk->getBuffer() - (U32)mDataBuffer) / mMinBlockSize * mMinBlockSize) ; + addToFreeSpace(next_blk) ; } + blk->init(blk->getBuffer(), buffer_size, slot_size) ; //insert to the available block list... - blk->mNext = NULL ; - blk->mPrev = NULL ; - blk->mSelf = blk ; + llassert_always(!mAvailBlockList[blk_idx]) ; mAvailBlockList[blk_idx] = blk ; + llassert_always(blk->getTotalSlots() > 0) ; + llassert_always(mAvailBlockList[blk_idx]->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; + llassert_always(buffer_size == (buffer_size / mMinBlockSize) * mMinBlockSize) ; + //mark the address map U32 end = (buffer_size / mMinBlockSize) ; for(U32 i = 1 ; i < end ; i++) @@ -751,6 +975,12 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNe (blk + i)->mSelf = blk ; } + llassert_always(blk->getBuffer() != _prev) ; + + llassert_always(mActiveBlockList.find(blk) == mActiveBlockList.end()) ; + + mActiveBlockList.insert(blk) ; + return blk ; } @@ -765,29 +995,54 @@ void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk) { blk->mNext->mPrev = blk->mPrev ; } + U32 blk_idx = getBlockLevel(blk->getSlotSize()); + if(mAvailBlockList[blk_idx] == blk) + { + mAvailBlockList[blk_idx] = blk->mNext ; + } + + blk->mNext = NULL ; + blk->mPrev = NULL ; + + std::set::iterator iter = mActiveBlockList.find(blk) ; + llassert_always(iter != mActiveBlockList.end()) ; + mActiveBlockList.erase(iter) ; //mark it free blk->setBuffer(blk->getBuffer(), blk->getBufferSize()) ; + //debug + U32 end = (blk->getBufferSize() / mMinBlockSize) ; + for(U32 i = 1 ; i < end ; i++) + { + llassert_always((blk + i)->mSelf == blk) ; + (blk + i)->mSelf = NULL ; + } +#if 0 //merge blk with neighbors if possible if(blk->getBuffer() > mDataBuffer) //has the left neighbor { if((blk - 1)->mSelf->isFree()) { + LLMemoryBlock* left_blk = (blk - 1)->mSelf ; removeFromFreeSpace((blk - 1)->mSelf); - (blk - 1)->mSelf->setBuffer((blk-1)->mSelf->getBuffer(), (blk-1)->mSelf->getBufferSize() + blk->getBufferSize()) ; - blk = (blk - 1)->mSelf ; + left_blk->setBuffer(left_blk->getBuffer(), left_blk->getBufferSize() + blk->getBufferSize()) ; + blk = left_blk ; } } - if(blk->getBuffer() + blk->getBufferSize() < mBuffer + mBufferSize) //has the right neighbor + if(blk->getBuffer() + blk->getBufferSize() <= mBuffer + mBufferSize - mMinBlockSize) //has the right neighbor { U32 d = blk->getBufferSize() / mMinBlockSize ; if((blk + d)->isFree()) { + LLMemoryBlock* right_blk = blk + d ; removeFromFreeSpace(blk + d) ; - blk->setBuffer(blk->getBuffer(), blk->getBufferSize() + (blk + d)->getBufferSize()) ; + blk->setBuffer(blk->getBuffer(), blk->getBufferSize() + right_blk->getBufferSize()) ; } } +#endif + llassert_always(blk->getBuffer() != _prev) ; + llassert_always(mActiveBlockList.find(blk) == mActiveBlockList.end()) ; addToFreeSpace(blk) ; @@ -800,16 +1055,29 @@ void LLPrivateMemoryPool::LLMemoryChunk::popAvailBlockList(U32 blk_idx) if(mAvailBlockList[blk_idx]) { LLMemoryBlock* next = mAvailBlockList[blk_idx]->mNext ; - next->mPrev = NULL ; + if(next) + { + next->mPrev = NULL ; + } + mAvailBlockList[blk_idx]->mPrev = NULL ; mAvailBlockList[blk_idx]->mNext = NULL ; mAvailBlockList[blk_idx] = next ; + if(next) + { + llassert_always(mAvailBlockList[blk_idx]->getTotalSlots() > 0) ; + llassert_always(mAvailBlockList[blk_idx]->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; + } + + dump() ; } } void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) { - U16 free_idx = blk->getBufferSize() / mMinBlockSize; - if(free_idx > 0) free_idx--; + llassert_always(!blk->mPrev) ; + llassert_always(!blk->mNext) ; + + U16 free_idx = blk->getBufferSize() / mMinBlockSize - 1; (blk + free_idx)->mSelf = blk ; //mark the end pointing back to the head. free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ; @@ -828,8 +1096,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk) { - U16 free_idx = blk->getBufferSize() / mMinBlockSize; - if(free_idx > 0) free_idx-- ; + U16 free_idx = blk->getBufferSize() / mMinBlockSize - 1; free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ; if(mFreeSpaceList[free_idx] == blk) @@ -844,37 +1111,70 @@ void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk) { blk->mNext->mPrev = blk->mPrev ; } - + blk->mNext = NULL ; + blk->mPrev = NULL ; + blk->mSelf = NULL ; + return ; } void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk) { + llassert_always(!blk->mPrev) ; + llassert_always(!blk->mNext) ; + U32 blk_idx = getBlockLevel(blk->getSlotSize()); + llassert_always(blk->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; + blk->mNext = mAvailBlockList[blk_idx] ; if(blk->mNext) { blk->mNext->mPrev = blk ; } blk->mPrev = NULL ; - + mAvailBlockList[blk_idx] = blk ; + + llassert_always(mAvailBlockList[blk_idx]->getTotalSlots() > 0) ; + llassert_always(mAvailBlockList[blk_idx]->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; + return ; } +U32 LLPrivateMemoryPool::LLMemoryChunk::getPageIndex(U32 addr) +{ + return (addr - (U32)mDataBuffer) / mMinBlockSize ; +} + +//for mAvailBlockList U32 LLPrivateMemoryPool::LLMemoryChunk::getBlockLevel(U32 size) { + llassert(size >= mMinSlotSize && size <= mMaxSlotSize) ; + + //start from 0 return (size + mMinSlotSize - 1) / mMinSlotSize - 1 ; } -U32 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size) +//for mFreeSpaceList +U16 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size) { - return (size + mMinBlockSize - 1) / mMinBlockSize - 1 ; + llassert_always(size >= mMinBlockSize); + llassert_always(!(size % mMinBlockSize)) ; + + //start from 0 + U16 level = size / mMinBlockSize - 1 ; + if(level >= mPartitionLevels) + { + level = mPartitionLevels - 1 ; + } + return level ; } //------------------------------------------------------------------- //class LLPrivateMemoryPool //-------------------------------------------------------------------- +const U32 CHUNK_SIZE = 4 << 20 ; //4 MB +const U32 HASH_FACTOR = 255 ; LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : mMutexp(NULL), mMaxPoolSize(max_size), @@ -890,8 +1190,12 @@ LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : mChunkList[i] = NULL ; } - mChunkVectorCapacity = 128 ; - mChunks.resize(mChunkVectorCapacity) ; //at most 128 chunks + mChunkHashList.resize(HASH_FACTOR + 1) ; + for(U32 i = 0 ; i <= HASH_FACTOR ; i++) + { + mChunkHashList[i] = NULL ; + } + mNumOfChunks = 0 ; } @@ -903,15 +1207,13 @@ LLPrivateMemoryPool::~LLPrivateMemoryPool() char* LLPrivateMemoryPool::allocate(U32 size) { - const static U32 MAX_BLOCK_SIZE = 4 * 1024 * 1024 ; //4MB - if(!size) { return NULL ; } //if the asked size larger than MAX_BLOCK_SIZE, fetch from heap directly, the pool does not manage it - if(size >= MAX_BLOCK_SIZE) + if(size >= CHUNK_SIZE) { return new char[size] ; } @@ -936,6 +1238,19 @@ char* LLPrivateMemoryPool::allocate(U32 size) //fetch new memory chunk if(!p) { + if(mReservedPoolSize + CHUNK_SIZE > mMaxPoolSize) + { + chunk = mChunkList[chunk_idx]; + while(chunk) + { + if(p = chunk->allocate(size)) + { + break ; + } + chunk = chunk->mNext ; + } + } + chunk = addChunk(chunk_idx) ; if(chunk) { @@ -957,28 +1272,92 @@ void LLPrivateMemoryPool::free(void* addr) lock() ; - LLMemoryChunk* chunk = mChunks[findChunk((char*)addr)] ; + U16 key ; + LLMemoryChunk* chunk =findChunk((char*)addr, key) ; + if(!chunk) { delete[] (char*)addr ; //release from heap } else { + llassert_always((U32)addr >= (U32)chunk->getBuffer() && (U32)addr < (U32)chunk->getBuffer() + chunk->getBufferSize()) ; + chunk->free(addr) ; if(chunk->empty()) { - removeChunk(chunk) ; + removeChunk(chunk, key) ; } } unlock() ; } +LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::findChunk(const char* addr, U16& key) +{ + key = findHashKey(addr) ; + + //check the hash value "key" + LLMemoryChunk* chunk = mChunkHashList[key] ; + while(chunk && !chunk->containsAddress(addr)) + { + chunk = chunk->mHashNext ; + } + + if(!chunk && key > 0) //check the "key - 1" + { + chunk = mChunkHashList[key - 1] ; + while(chunk && !chunk->containsAddress(addr)) + { + chunk = chunk->mHashNext ; + } + + if(chunk) + { + key-- ; + } + } + + if(!chunk && key < HASH_FACTOR) //check the "key + 1" + { + chunk = mChunkHashList[key + 1] ; + while(chunk && !chunk->containsAddress(addr)) + { + chunk = chunk->mHashNext ; + } + + if(chunk) + { + key++ ; + } + } + + return chunk ; +} + void LLPrivateMemoryPool::dump() { } +U32 LLPrivateMemoryPool::getTotalAllocatedSize() +{ + U32 total_allocated = 0 ; + + LLMemoryChunk* chunk ; + for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) + { + chunk = mChunkList[i]; + while(chunk) + { + total_allocated += chunk->getAllocatedSize() ; + chunk = chunk->mNext ; + } + } + + return total_allocated ; +} + void LLPrivateMemoryPool::lock() { if(mMutexp) @@ -997,58 +1376,72 @@ void LLPrivateMemoryPool::unlock() S32 LLPrivateMemoryPool::getChunkIndex(U32 size) { - if(size < 2048) - { - return 0 ; - } - else if(size < (512 << 10)) - { - return 1 ; - } - else - { - return 2 ; - } + S32 i ; + for(i = 0 ; size > MAX_SLOT_SIZES[i]; i++); + + llassert_always(i < SUPER_ALLOCATION); + + return i ; } //destroy the entire pool void LLPrivateMemoryPool::destroyPool() { - for(U16 i = 0 ; i < mNumOfChunks ; i++) + lock() ; + for(U32 i = 0 ; i <= HASH_FACTOR; i++) { - delete[] mChunks[i]->getBuffer() ; + while(mChunkHashList[i]) + { + removeChunk(mChunkHashList[i], i) ; + } } - mNumOfChunks = 0 ; + llassert_always(mNumOfChunks == 0) ; + llassert_always(mReservedPoolSize == 0) ; + for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) { mChunkList[i] = NULL ; } + + unlock() ; } -LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_index) +void LLPrivateMemoryPool::checkSize(U32 asked_size) { - static const U32 MIN_BLOCK_SIZES[SUPER_ALLOCATION] = {2 << 10, 32 << 10, 64 << 10} ; - static const U32 MAX_BLOCK_SIZES[SUPER_ALLOCATION] = {64 << 10, 1 << 20, 4 << 20} ; - static const U32 MIN_SLOT_SIZES[SUPER_ALLOCATION] = {8, 2 << 10, 512 << 10}; - static const U32 MAX_SLOT_SIZES[SUPER_ALLOCATION] = {(2 << 10) - 8, (512 - 2) << 10, 4 << 20}; + if(mReservedPoolSize + asked_size > mMaxPoolSize) + { + llinfos << "Max pool size: " << mMaxPoolSize << llendl ; + llinfos << "Total reserved size: " << mReservedPoolSize + asked_size << llendl ; + llinfos << "Total_allocated Size: " << getTotalAllocatedSize() << llendl ; + + llerrs << "The pool is overflowing..." << llendl ; + } +} +LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_index) +{ U32 preferred_size ; U32 overhead ; if(chunk_index < LARGE_ALLOCATION) { - preferred_size = (4 << 20) ; //4MB + preferred_size = CHUNK_SIZE ; //4MB overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; } else { - preferred_size = (16 << 20) ; //16MB + preferred_size = 4 * CHUNK_SIZE ; //16MB overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; } + + checkSize(preferred_size + overhead) ; + mReservedPoolSize += preferred_size + overhead ; + char* buffer = new(std::nothrow) char[preferred_size + overhead] ; if(!buffer) { return NULL ; } + memset(buffer, 0, preferred_size + overhead) ; LLMemoryChunk* chunk = new (buffer) LLMemoryChunk() ; chunk->init(buffer, preferred_size + overhead, MIN_SLOT_SIZES[chunk_index], @@ -1063,37 +1456,35 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_inde chunk->mPrev = NULL ; mChunkList[chunk_index] = chunk ; - //insert into the array - llassert_always(mNumOfChunks + 1 < mChunkVectorCapacity) ; - if(!mNumOfChunks) - { - mChunks[0] = chunk ; - } - else - { - U16 k ; - if(mChunks[0]->getBuffer() > chunk->getBuffer()) - { - k = 0 ; - } - else - { - k = findChunk(chunk->getBuffer()) + 1 ; - } - for(U16 i = mNumOfChunks ; i > k ; i++) - { - mChunks[i] = mChunks[i-1] ; - } - mChunks[k] = chunk ; - } + //insert into the hash table + U16 key = findHashKey(chunk->getBuffer()) ; + chunk->mHashNext = mChunkHashList[key] ; + mChunkHashList[key] = chunk ; + mNumOfChunks++; return chunk ; } -void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk) +char*** _p = NULL ; +U32 _times; +U32 _levels; +void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk, U16 key) { + if(!chunk) + { + return ; + } + //remove from the linked list + for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) + { + if(mChunkList[i] == chunk) + { + mChunkList[i] = chunk->mNext ; + } + } + if(chunk->mPrev) { chunk->mPrev->mNext = chunk->mNext ; @@ -1103,43 +1494,47 @@ void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk) chunk->mNext->mPrev = chunk->mPrev ; } - //remove from the array - U16 k = findChunk(chunk->getBuffer()) ; - mNumOfChunks--; - for(U16 i = k ; i < mNumOfChunks ; i++) + //remove from the hash table + if(mChunkHashList[key] == chunk) { - mChunks[i] = mChunks[i+1] ; + mChunkHashList[key] = chunk->mHashNext ; } - - //release memory - delete[] chunk->getBuffer() ; -} - -U16 LLPrivateMemoryPool::findChunk(const char* addr) -{ - llassert_always(mNumOfChunks > 0) ; - - U16 s = 0, e = mNumOfChunks; - U16 k = (s + e) / 2 ; - while(s < e) + else { - if(mChunks[k]->mKey > (U32)addr) + LLMemoryChunk* prev = mChunkHashList[key] ; + while(prev->mHashNext && prev->mHashNext != chunk) { - e = k ; + prev = prev->mHashNext ; } - else if(k < mNumOfChunks - 1 && mChunks[k+1]->mKey < (U32)addr) - { - s = k ; - } - else + llassert_always(prev->mHashNext == chunk) ; + + prev->mHashNext = chunk->mHashNext ; + } + mNumOfChunks--; + mReservedPoolSize -= chunk->getBufferSize() ; + + //debug check + if(_p) + { + for(U32 i = 0 ; i < _times; i++) { - break ; + for(U32 j = 0 ; j < _levels ;j++) + { + if( i == col && j == row) + { + continue ; + } + llassert_always(!_p[i][j] || !chunk->containsAddress(_p[i][j])) ; + } } - - k = (s + e) / 2 ; } + //release memory + delete[] chunk->getBuffer() ; +} - return k ; +U16 LLPrivateMemoryPool::findHashKey(const char* addr) +{ + return (((U32)addr) / CHUNK_SIZE) % HASH_FACTOR ; } //-------------------------------------------------------------------- @@ -1182,7 +1577,7 @@ void LLPrivateMemoryPoolTester::destroy() void LLPrivateMemoryPoolTester::run(bool threaded) { - const U32 max_pool_size = 16 << 20 ; + const U32 max_pool_size = 1024 << 20 ; if(sPool) { @@ -1192,8 +1587,8 @@ void LLPrivateMemoryPoolTester::run(bool threaded) //run the test correctnessTest() ; - performanceTest() ; - fragmentationtest() ; + //performanceTest() ; + //fragmentationtest() ; //release pool. ::delete sPool ; @@ -1206,6 +1601,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 U32 levels = (max_size - min_size) / stride + 1 ; char*** p ; U32 i, j ; + U32 total_allocated_size = 0 ; //allocate space for p ; if(!(p = ::new char**[times]) || !(*p = ::new char*[times * levels])) @@ -1223,6 +1619,10 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 } } + _p = p ; + _times = times; + _levels = levels ; + //allocation U32 size ; for(i = 0 ; i < times ; i++) @@ -1230,15 +1630,33 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 for(j = 0 ; j < levels; j++) { size = min_size + j * stride ; - p[i][j] = sPool->allocate(size) ; - p[i][j][size - 1] = '\0' ; //access the last element to verify the success of the allocation. + _prev = p[i][j] = sPool->allocate(size) ; + + total_allocated_size+= size ; + + *(U32*)p[i][j] = i ; + *((U32*)p[i][j] + 1) = j ; + //p[i][j][size - 1] = '\0' ; //access the last element to verify the success of the allocation. //randomly release memory if(random_deletion) { S32 k = rand() % levels ; - sPool->free(p[i][k]) ; - p[i][k] = NULL ; + + col = i ; + row = k ; + + if(p[i][k]) + { + if(_prev == p[i][k]) + { + _prev = NULL ; + } + llassert_always(*(U32*)p[i][k] == i && *((U32*)p[i][k] + 1) == k) ; + sPool->free(p[i][k]) ; + total_allocated_size -= min_size + k * stride ; + p[i][k] = NULL ; + } } } } @@ -1248,18 +1666,28 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 { } + _prev = NULL ; //release all memory allocations for(i = 0 ; i < times; i++) { for(j = 0 ; j < levels; j++) { - sPool->free(p[i][j]) ; - p[i][j] = NULL ; + col = i ; + row = j ; + + if(p[i][j]) + { + llassert_always(*(U32*)p[i][j] == i && *((U32*)p[i][j] + 1) == j) ; + sPool->free(p[i][j]) ; + total_allocated_size -= min_size + j * stride ; + p[i][j] = NULL ; + } } } ::delete[] *p ; ::delete[] p ; + _p = NULL ; } void LLPrivateMemoryPoolTester::correctnessTest() @@ -1281,7 +1709,7 @@ void LLPrivateMemoryPoolTester::correctnessTest() //large sized //[512KB, 4MB], each asks for 8 allocations and deallocations - test(512 * 1024, 4 * 1024 * 1024, 64 * 1024, 8, true, true) ; + test(512 * 1024, 4 * 1024 * 1024, 64 * 1024, 6, true, true) ; } void LLPrivateMemoryPoolTester::performanceTest() -- cgit v1.2.3 From f4a8027feb2bbeafe7b0cfb3b05fd27f3cf243d3 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 6 Jan 2011 12:36:44 -0700 Subject: removed some debug code, redesigned the hash function, fixed bugs --- indra/llcommon/llmemory.cpp | 605 ++++++++++++++++++++++---------------------- 1 file changed, 309 insertions(+), 296 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 00ef09d7a2..f9a2770691 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -356,7 +356,8 @@ U64 LLMemory::getCurrentRSS() #endif -//------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------- //minimum block sizes (page size) for small allocation, medium allocation, large allocation const U32 MIN_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {2 << 10, 4 << 10, 16 << 10} ; // @@ -389,6 +390,7 @@ LLPrivateMemoryPool::LLMemoryBlock::~LLMemoryBlock() //empty } +//create and initialize a memory block void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 slot_size) { llassert_always(buffer_size >= slot_size) ; @@ -397,17 +399,20 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 mBufferSize = buffer_size ; mSlotSize = slot_size ; mTotalSlots = buffer_size / mSlotSize ; + llassert_always(mTotalSlots < 256) ; //max number is 256 + mAllocatedSlots = 0 ; + //init the bit map. //mark free bits S32 usage_bit_len = (mTotalSlots + 31) / 32 ; - mDummySize = usage_bit_len - 1 ; - if(mDummySize > 0) //extra space to store mUsageBits + mDummySize = usage_bit_len - 1 ; //if the mTotalSlots more than 32, needs extra space for bit map + if(mDummySize > 0) //reserve extra space from mBuffer to store bitmap if needed. { mTotalSlots -= (mDummySize * sizeof(mUsageBits) + mSlotSize - 1) / mSlotSize ; usage_bit_len = (mTotalSlots + 31) / 32 ; - mDummySize = usage_bit_len - 1 ; + mDummySize = usage_bit_len - 1 ;//number of 32bits reserved from mBuffer for bitmap if(mDummySize > 0) { @@ -423,7 +428,7 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 } } - if(mDummySize < 1) + if(mDummySize < 1)//no extra bitmap space reserved { mUsageBits = 0 ; if(mTotalSlots & 31) @@ -439,16 +444,16 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 llassert_always(mTotalSlots > 0) ; } +//mark this block to be free with the memory [mBuffer, mBuffer + mBufferSize). void LLPrivateMemoryPool::LLMemoryBlock::setBuffer(char* buffer, U32 buffer_size) { - llassert_always(buffer_size <= (16 << 20)) ; - mBuffer = buffer ; mBufferSize = buffer_size ; mSelf = NULL ; mTotalSlots = 0 ; //set the block is free. } +//reserve a slot char* LLPrivateMemoryPool::LLMemoryBlock::allocate() { llassert_always(mAllocatedSlots < mTotalSlots) ; @@ -479,47 +484,31 @@ char* LLPrivateMemoryPool::LLMemoryBlock::allocate() //set the slot reserved if(!idx) { - llassert_always(!(*bits & 1)); *bits |= 1 ; } else { - llassert_always(!(*bits & (1 << idx))) ; *bits |= (1 << idx) ; } mAllocatedSlots++ ; - //return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; - - char* p = mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; - llassert_always(mBuffer != p || !mDummySize) ; - llassert_always(*(U32*)p == 0 && *((U32*)p + 1) == 0) ; - - return p ; + return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; } -U32 col = 0, row = 0 ; +//free a slot void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr) { - llassert_always((U32)addr >= (U32)mBuffer + mDummySize * sizeof(U32) && - (U32)addr < (U32)mBuffer + mBufferSize) ; - + //bit index U32 idx = ((U32)addr - (U32)mBuffer - mDummySize * sizeof(U32)) / mSlotSize ; - llassert_always(idx < mTotalSlots) ; - llassert_always(addr == mBuffer + mDummySize * sizeof(U32) + idx * mSlotSize) ; - llassert_always(*(U32*)addr == col && *((U32*)addr + 1) == row) ; - - *(U32*)addr = 0 ; - *((U32*)addr + 1) = 0 ; - U32* bits = &mUsageBits ; if(idx >= 32) { bits = (U32*)mBuffer + (idx - 32) / 32 ; } + //reset the bit if(idx & 31) { *bits &= ~(1 << (idx & 31)) ; @@ -532,7 +521,7 @@ void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr) mAllocatedSlots-- ; } -//for debug use +//for debug use: reset the entire bitmap. void LLPrivateMemoryPool::LLMemoryBlock::resetBitMap() { for(S32 i = 0 ; i < mDummySize ; i++) @@ -554,6 +543,7 @@ LLPrivateMemoryPool::LLMemoryChunk::~LLMemoryChunk() //empty } +//create and init a memory chunk void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 min_slot_size, U32 max_slot_size, U32 min_block_size, U32 max_block_size) { mBuffer = buffer ; @@ -562,7 +552,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 mMetaBuffer = mBuffer + sizeof(LLMemoryChunk) ; - mMinBlockSize = min_block_size; + mMinBlockSize = min_block_size; //page size mMinSlotSize = min_slot_size; mMaxSlotSize = max_slot_size ; mBlockLevels = mMaxSlotSize / mMinSlotSize ; @@ -571,11 +561,11 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 S32 max_num_blocks = (buffer_size - sizeof(LLMemoryChunk) - mBlockLevels * sizeof(LLMemoryBlock*) - mPartitionLevels * sizeof(LLMemoryBlock*)) / (mMinBlockSize + sizeof(LLMemoryBlock)) ; //meta data space - mBlocks = (LLMemoryBlock*)mMetaBuffer ; + mBlocks = (LLMemoryBlock*)mMetaBuffer ; //space reserved for all memory blocks. mAvailBlockList = (LLMemoryBlock**)((char*)mBlocks + sizeof(LLMemoryBlock) * max_num_blocks) ; mFreeSpaceList = (LLMemoryBlock**)((char*)mAvailBlockList + sizeof(LLMemoryBlock*) * mBlockLevels) ; - //data buffer + //data buffer, which can be used for allocation mDataBuffer = (char*)mFreeSpaceList + sizeof(LLMemoryBlock*) * mPartitionLevels ; //init @@ -588,17 +578,10 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 mFreeSpaceList[i] = NULL ; } + //assign the entire chunk to the first block mBlocks[0].mPrev = NULL ; mBlocks[0].mNext = NULL ; mBlocks[0].setBuffer(mDataBuffer, buffer_size - (mDataBuffer - mBuffer)) ; - - //debug - U32 end = (mBlocks[0].getBufferSize() / mMinBlockSize) ; - for(U32 i = 1 ; i < end ; i++) - { - mBlocks[i].mSelf = NULL ; - } - addToFreeSpace(&mBlocks[0]) ; mHashNext = NULL ; @@ -609,6 +592,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 //static U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_page_size) { + //for large allocations, reserve some extra memory for meta data to avoid wasting much if(data_buffer_size / min_page_size < 64) //large allocations { return 4096 ; //4KB @@ -621,6 +605,15 @@ U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) { + if(mMinSlotSize > size) + { + size = mMinSlotSize ; + } + if(mAlloatedSize + size > mBufferSize - (mDataBuffer - mBuffer)) + { + return NULL ; //no enough space in this chunk. + } + char* p = NULL ; U32 blk_idx = getBlockLevel(size); @@ -653,7 +646,7 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) } } - //ask for space from higher level blocks + //ask for space from larger blocks if(!p) { for(S32 i = blk_idx + 1 ; i < mBlockLevels; i++) @@ -672,18 +665,8 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) } } - llassert_always(!p || blk) ; - if(p && blk) - { - if(blk->getTotalSlots() == 1) - { - llassert_always(blk->getBuffer() == (char*)p) ; - } - U32 blk_idx = getPageIndex((U32)p) ; - LLMemoryBlock* b = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ; - llassert_always(blk == b || b->mSelf == blk) ; - + { mAlloatedSize += blk->getSlotSize() ; } return p ; @@ -693,31 +676,19 @@ void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr) { U32 blk_idx = getPageIndex((U32)addr) ; LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ; - llassert_always(blk->mSelf) ; blk = blk->mSelf ; - llassert_always(addr >= blk->getBuffer() && addr < blk->getBuffer() + blk->getBufferSize()) ; - if(blk->getTotalSlots() == 1) - { - llassert_always(blk->getBuffer() == (char*)addr) ; - } - bool was_full = blk->isFull() ; blk->free(addr) ; mAlloatedSize -= blk->getSlotSize() ; if(blk->empty()) { - blk->resetBitMap() ; //debug use removeBlock(blk) ; - - dump(); } else if(was_full) { addToAvailBlockList(blk) ; - - dump(); } } @@ -731,14 +702,11 @@ bool LLPrivateMemoryPool::LLMemoryChunk::containsAddress(const char* addr) const return (U32)mBuffer <= (U32)addr && (U32)mBuffer + mBufferSize > (U32)addr ; } +//debug use void LLPrivateMemoryPool::LLMemoryChunk::dump() { +#if 0 //sanity check - std::vector< LLMemoryBlock* > blk_list ; - for(std::set::iterator iter = mActiveBlockList.begin() ; iter != mActiveBlockList.end(); ++iter) - { - blk_list.push_back(*iter) ; - } //for(S32 i = 0 ; i < mBlockLevels ; i++) //{ // LLMemoryBlock* blk = mAvailBlockList[i] ; @@ -790,6 +758,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::dump() llerrs << "gap happens" << llendl ; } } +#endif #if 0 llinfos << "---------------------------" << llendl ; llinfos << "Chunk buffer: " << (U32)getBuffer() << " size: " << getBufferSize() << llendl ; @@ -818,6 +787,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::dump() #endif } +//compute the size for a block, the size is round to integer times of mMinBlockSize. U32 LLPrivateMemoryPool::LLMemoryChunk::calcBlockSize(U32 slot_size) { // @@ -857,15 +827,12 @@ U32 LLPrivateMemoryPool::LLMemoryChunk::calcBlockSize(U32 slot_size) return block_size ; } +//create a new block in the chunk LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock(U32 blk_idx) { U32 slot_size = mMinSlotSize * (blk_idx + 1) ; - U32 preferred_block_size = calcBlockSize(slot_size) ; - + U32 preferred_block_size = calcBlockSize(slot_size) ; U16 idx = getPageLevel(preferred_block_size); - llassert_always(idx < mPartitionLevels - 1) ; - llassert_always(preferred_block_size == (idx + 1) * mMinBlockSize) ; //round to integer times of mMinBlockSize. - LLMemoryBlock* blk = NULL ; if(mFreeSpaceList[idx])//if there is free slot for blk_idx @@ -878,7 +845,12 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock } else //search for other non-preferred but enough space slot. { - for(S32 i = (S32)idx - 1 ; i >= 0 ; i--) //search the small slots first + S32 min_idx = 0 ; + if(slot_size > mMinBlockSize) + { + min_idx = getPageLevel(slot_size) ; + } + for(S32 i = (S32)idx - 1 ; i >= min_idx ; i--) //search the small slots first { if(mFreeSpaceList[i]) { @@ -909,31 +881,12 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock } } - dump() ; - return blk ; } -char* _prev = NULL ; +//create a new block at the designed location LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNewBlock(LLMemoryBlock* blk, U32 buffer_size, U32 slot_size, U32 blk_idx) { - llassert_always(blk->getBufferSize() >= buffer_size) ; - - //debug - { - { - U32 blk_idx = getPageIndex((U32)blk->getBuffer()) ; - llassert_always(blk == (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock))) ; - } - U32 end = (blk->getBufferSize() / mMinBlockSize) ; - llassert_always(blk->mSelf == blk && blk->isFree()) ; - llassert_always((blk + end - 1)->mSelf == blk) ; - for(U32 i = 1 ; i < end - 1; i++) - { - llassert_always(!(blk + i)->mSelf) ; - } - } - //unlink from the free space removeFromFreeSpace(blk) ; @@ -949,41 +902,24 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNe { LLMemoryBlock* next_blk = blk + (buffer_size / mMinBlockSize) ; next_blk->setBuffer(blk->getBuffer() + buffer_size, new_free_blk_size) ; - - { - U32 blk_idx = getPageIndex((U32)next_blk->getBuffer()) ; - llassert_always(next_blk == (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock))) ; - } - llassert_always(buffer_size == (buffer_size / mMinBlockSize) * mMinBlockSize) ; - llassert_always(((U32)next_blk->getBuffer() - (U32)mDataBuffer) == ((U32)next_blk->getBuffer() - (U32)mDataBuffer) / mMinBlockSize * mMinBlockSize) ; addToFreeSpace(next_blk) ; } blk->init(blk->getBuffer(), buffer_size, slot_size) ; //insert to the available block list... - llassert_always(!mAvailBlockList[blk_idx]) ; mAvailBlockList[blk_idx] = blk ; - llassert_always(blk->getTotalSlots() > 0) ; - llassert_always(mAvailBlockList[blk_idx]->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; - llassert_always(buffer_size == (buffer_size / mMinBlockSize) * mMinBlockSize) ; - - //mark the address map + //mark the address map: all blocks covered by this block space pointing back to this block. U32 end = (buffer_size / mMinBlockSize) ; for(U32 i = 1 ; i < end ; i++) { (blk + i)->mSelf = blk ; } - llassert_always(blk->getBuffer() != _prev) ; - - llassert_always(mActiveBlockList.find(blk) == mActiveBlockList.end()) ; - - mActiveBlockList.insert(blk) ; - return blk ; } +//delete a block, release the block to the free pool. void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk) { //remove from the available block list @@ -1003,22 +939,11 @@ void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk) blk->mNext = NULL ; blk->mPrev = NULL ; - - std::set::iterator iter = mActiveBlockList.find(blk) ; - llassert_always(iter != mActiveBlockList.end()) ; - mActiveBlockList.erase(iter) ; //mark it free blk->setBuffer(blk->getBuffer(), blk->getBufferSize()) ; - //debug - U32 end = (blk->getBufferSize() / mMinBlockSize) ; - for(U32 i = 1 ; i < end ; i++) - { - llassert_always((blk + i)->mSelf == blk) ; - (blk + i)->mSelf = NULL ; - } -#if 0 +#if 1 //merge blk with neighbors if possible if(blk->getBuffer() > mDataBuffer) //has the left neighbor { @@ -1041,9 +966,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk) } } #endif - llassert_always(blk->getBuffer() != _prev) ; - llassert_always(mActiveBlockList.find(blk) == mActiveBlockList.end()) ; - + addToFreeSpace(blk) ; return ; @@ -1062,16 +985,10 @@ void LLPrivateMemoryPool::LLMemoryChunk::popAvailBlockList(U32 blk_idx) mAvailBlockList[blk_idx]->mPrev = NULL ; mAvailBlockList[blk_idx]->mNext = NULL ; mAvailBlockList[blk_idx] = next ; - if(next) - { - llassert_always(mAvailBlockList[blk_idx]->getTotalSlots() > 0) ; - llassert_always(mAvailBlockList[blk_idx]->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; - } - - dump() ; } } +//add the block back to the free pool void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) { llassert_always(!blk->mPrev) ; @@ -1094,6 +1011,7 @@ void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk) return ; } +//remove the space from the free pool void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk) { U16 free_idx = blk->getBufferSize() / mMinBlockSize - 1; @@ -1125,8 +1043,6 @@ void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk) U32 blk_idx = getBlockLevel(blk->getSlotSize()); - llassert_always(blk->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; - blk->mNext = mAvailBlockList[blk_idx] ; if(blk->mNext) { @@ -1135,9 +1051,6 @@ void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk) blk->mPrev = NULL ; mAvailBlockList[blk_idx] = blk ; - llassert_always(mAvailBlockList[blk_idx]->getTotalSlots() > 0) ; - llassert_always(mAvailBlockList[blk_idx]->getSlotSize() == (blk_idx + 1) * mMinSlotSize) ; - return ; } @@ -1158,9 +1071,6 @@ U32 LLPrivateMemoryPool::LLMemoryChunk::getBlockLevel(U32 size) //for mFreeSpaceList U16 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size) { - llassert_always(size >= mMinBlockSize); - llassert_always(!(size % mMinBlockSize)) ; - //start from 0 U16 level = size / mMinBlockSize - 1 ; if(level >= mPartitionLevels) @@ -1174,11 +1084,12 @@ U16 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size) //class LLPrivateMemoryPool //-------------------------------------------------------------------- const U32 CHUNK_SIZE = 4 << 20 ; //4 MB -const U32 HASH_FACTOR = 255 ; +const U32 LARGE_CHUNK_SIZE = 4 * CHUNK_SIZE ; //16 MB LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : mMutexp(NULL), mMaxPoolSize(max_size), - mReservedPoolSize(0) + mReservedPoolSize(0), + mHashFactor(1) { if(threaded) { @@ -1188,13 +1099,7 @@ LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) { mChunkList[i] = NULL ; - } - - mChunkHashList.resize(HASH_FACTOR + 1) ; - for(U32 i = 0 ; i <= HASH_FACTOR ; i++) - { - mChunkHashList[i] = NULL ; - } + } mNumOfChunks = 0 ; } @@ -1272,70 +1177,25 @@ void LLPrivateMemoryPool::free(void* addr) lock() ; - U16 key ; - LLMemoryChunk* chunk =findChunk((char*)addr, key) ; + LLMemoryChunk* chunk = findChunk((char*)addr) ; if(!chunk) { - delete[] (char*)addr ; //release from heap + delete[] addr ; //release from heap } else { - llassert_always((U32)addr >= (U32)chunk->getBuffer() && (U32)addr < (U32)chunk->getBuffer() + chunk->getBufferSize()) ; - chunk->free(addr) ; if(chunk->empty()) { - removeChunk(chunk, key) ; + removeChunk(chunk) ; } } unlock() ; } -LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::findChunk(const char* addr, U16& key) -{ - key = findHashKey(addr) ; - - //check the hash value "key" - LLMemoryChunk* chunk = mChunkHashList[key] ; - while(chunk && !chunk->containsAddress(addr)) - { - chunk = chunk->mHashNext ; - } - - if(!chunk && key > 0) //check the "key - 1" - { - chunk = mChunkHashList[key - 1] ; - while(chunk && !chunk->containsAddress(addr)) - { - chunk = chunk->mHashNext ; - } - - if(chunk) - { - key-- ; - } - } - - if(!chunk && key < HASH_FACTOR) //check the "key + 1" - { - chunk = mChunkHashList[key + 1] ; - while(chunk && !chunk->containsAddress(addr)) - { - chunk = chunk->mHashNext ; - } - - if(chunk) - { - key++ ; - } - } - - return chunk ; -} - void LLPrivateMemoryPool::dump() { } @@ -1388,11 +1248,11 @@ S32 LLPrivateMemoryPool::getChunkIndex(U32 size) void LLPrivateMemoryPool::destroyPool() { lock() ; - for(U32 i = 0 ; i <= HASH_FACTOR; i++) + for(U32 i = 0 ; i < mHashFactor; i++) { while(mChunkHashList[i]) { - removeChunk(mChunkHashList[i], i) ; + removeChunk(mChunkHashList[i]) ; } } llassert_always(mNumOfChunks == 0) ; @@ -1429,7 +1289,7 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_inde } else { - preferred_size = 4 * CHUNK_SIZE ; //16MB + preferred_size = LARGE_CHUNK_SIZE ; //16MB overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; } @@ -1457,19 +1317,14 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_inde mChunkList[chunk_index] = chunk ; //insert into the hash table - U16 key = findHashKey(chunk->getBuffer()) ; - chunk->mHashNext = mChunkHashList[key] ; - mChunkHashList[key] = chunk ; + addToHashTable(chunk) ; mNumOfChunks++; return chunk ; } -char*** _p = NULL ; -U32 _times; -U32 _levels; -void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk, U16 key) +void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk) { if(!chunk) { @@ -1495,46 +1350,210 @@ void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk, U16 key) } //remove from the hash table - if(mChunkHashList[key] == chunk) + removeFromHashTable(chunk) ; + + mNumOfChunks--; + mReservedPoolSize -= chunk->getBufferSize() ; + + //release memory + delete[] chunk->getBuffer() ; +} + +U16 LLPrivateMemoryPool::findHashKey(const char* addr) +{ + return (((U32)addr) / CHUNK_SIZE) % mHashFactor ; +} + +LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::findChunk(const char* addr) +{ + U16 key = findHashKey(addr) ; + if(mChunkHashList.size() <= key) { - mChunkHashList[key] = chunk->mHashNext ; + return NULL ; } - else + + //check the hash value "key" + LLMemoryChunk* chunk = mChunkHashList[key] ; + while(chunk && !chunk->containsAddress(addr)) + { + chunk = chunk->mHashNext ; + } + + return chunk ; +} + +void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) +{ + static const U16 HASH_FACTORS[] = {41, 83, 193, 317, 419, 523, 0xFFFF}; + + U16 i ; + if(mChunkHashList.empty()) + { + mHashFactor = HASH_FACTORS[0] ; + rehash() ; + } + + U16 start_key = findHashKey(chunk->getBuffer()) ; + U16 end_key = findHashKey(chunk->getBuffer() + chunk->getBufferSize() - 1) ; + bool need_rehash = false ; + + if(mChunkHashList[start_key]) { - LLMemoryChunk* prev = mChunkHashList[key] ; - while(prev->mHashNext && prev->mHashNext != chunk) + if(mChunkHashList[start_key] == chunk) { - prev = prev->mHashNext ; + return; //already inserted. } - llassert_always(prev->mHashNext == chunk) ; + + need_rehash = mChunkHashList[start_key]->mHashNext != NULL ; + if(!need_rehash) + { + llassert_always(!chunk->mHashNext) ; - prev->mHashNext = chunk->mHashNext ; + chunk->mHashNext = mChunkHashList[start_key] ; + mChunkHashList[start_key] = chunk ; + } } - mNumOfChunks--; - mReservedPoolSize -= chunk->getBufferSize() ; - - //debug check - if(_p) + else + { + mChunkHashList[start_key] = chunk ; + } + + if(!need_rehash) + { + if(mChunkHashList[end_key]) + { + llassert_always(mChunkHashList[end_key] != chunk) + + need_rehash = mChunkHashList[end_key]->mHashNext != NULL ; + if(!need_rehash) + { + mChunkHashList[end_key]->mHashNext = chunk ; + } + } + else + { + mChunkHashList[end_key] = chunk ; + } + } + + if(!need_rehash) { - for(U32 i = 0 ; i < _times; i++) + if(end_key < start_key) { - for(U32 j = 0 ; j < _levels ;j++) + for(U16 i = start_key + 1 ; i < mHashFactor; i++) { - if( i == col && j == row) + if(mChunkHashList[i]) { - continue ; + llassert_always(mChunkHashList[i] != chunk) ; + need_rehash = true ; + break ; + } + else + { + mChunkHashList[i] = chunk ; + } + } + + if(!need_rehash) + { + for(U16 i = 0 ; i < end_key; i++) + { + if(mChunkHashList[i]) + { + llassert_always(mChunkHashList[i] != chunk) ; + need_rehash = true ; + break ; + } + else + { + mChunkHashList[i] = chunk ; + } + } + } + } + else + { + for(i = start_key + 1; i < end_key; i++) + { + if(mChunkHashList[i]) + { + llassert_always(mChunkHashList[i] != chunk) ; + need_rehash = true ; + break ; + } + else + { + mChunkHashList[i] = chunk ; } - llassert_always(!_p[i][j] || !chunk->containsAddress(_p[i][j])) ; } } } - //release memory - delete[] chunk->getBuffer() ; + + if(need_rehash) + { + i = 0 ; + while(HASH_FACTORS[i] <= mHashFactor) i++; + + mHashFactor = HASH_FACTORS[i] ; + llassert_always(mHashFactor != 0xFFFF) ;//stop point of the recursive calls + + rehash() ; + } } -U16 LLPrivateMemoryPool::findHashKey(const char* addr) +void LLPrivateMemoryPool::removeFromHashTable(LLMemoryChunk* chunk) +{ + U16 start_key = findHashKey(chunk->getBuffer()) ; + U16 end_key = findHashKey(chunk->getBuffer() + chunk->getBufferSize() - 1) ; + + mChunkHashList[start_key] = chunk->mHashNext ; + chunk->mHashNext = NULL ; + + if(mChunkHashList[end_key] != chunk) + { + mChunkHashList[end_key]->mHashNext = NULL ; + } + else + { + mChunkHashList[end_key] = NULL ; + } + + if(end_key < start_key) + { + for(U16 i = start_key + 1 ; i < mHashFactor; i++) + { + mChunkHashList[i] = NULL ; + } + for(U16 i = 0 ; i < end_key; i++) + { + mChunkHashList[i] = NULL ; + } + } + else + { + for(U16 i = start_key + 1 ; i < end_key; i++) + { + mChunkHashList[i] = NULL ; + } + } +} + +void LLPrivateMemoryPool::rehash() { - return (((U32)addr) / CHUNK_SIZE) % HASH_FACTOR ; + mChunkHashList.clear() ; + mChunkHashList.resize(mHashFactor, NULL) ; + + LLMemoryChunk* chunk ; + for(U16 i = 0 ; i < SUPER_ALLOCATION ; i++) + { + chunk = mChunkList[i] ; + while(chunk) + { + chunk->mHashNext = NULL ; + addToHashTable(chunk) ; + chunk = chunk->mNext ; + } + } } //-------------------------------------------------------------------- @@ -1587,7 +1606,7 @@ void LLPrivateMemoryPoolTester::run(bool threaded) //run the test correctnessTest() ; - //performanceTest() ; + performanceTest() ; //fragmentationtest() ; //release pool. @@ -1619,10 +1638,6 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 } } - _p = p ; - _times = times; - _levels = levels ; - //allocation U32 size ; for(i = 0 ; i < times ; i++) @@ -1630,7 +1645,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 for(j = 0 ; j < levels; j++) { size = min_size + j * stride ; - _prev = p[i][j] = sPool->allocate(size) ; + p[i][j] = sPool->allocate(size) ; total_allocated_size+= size ; @@ -1643,15 +1658,8 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 { S32 k = rand() % levels ; - col = i ; - row = k ; - if(p[i][k]) { - if(_prev == p[i][k]) - { - _prev = NULL ; - } llassert_always(*(U32*)p[i][k] == i && *((U32*)p[i][k] + 1) == k) ; sPool->free(p[i][k]) ; total_allocated_size -= min_size + k * stride ; @@ -1666,15 +1674,11 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 { } - _prev = NULL ; //release all memory allocations for(i = 0 ; i < times; i++) { for(j = 0 ; j < levels; j++) { - col = i ; - row = j ; - if(p[i][j]) { llassert_always(*(U32*)p[i][j] == i && *((U32*)p[i][j] + 1) == j) ; @@ -1687,7 +1691,57 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 ::delete[] *p ; ::delete[] p ; - _p = NULL ; +} + +void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times) +{ + LLTimer timer ; + + llinfos << " -**********************- " << llendl ; + llinfos << "test size: " << size << " test times: " << times << llendl ; + + timer.reset() ; + char** p = new char*[times] ; + + //using the customized memory pool + //allocation + for(U32 i = 0 ; i < times; i++) + { + p[i] = sPool->allocate(size) ; + if(!p[i]) + { + llerrs << "allocation failed" << llendl ; + } + } + //de-allocation + for(U32 i = 0 ; i < times; i++) + { + sPool->free(p[i]) ; + p[i] = NULL ; + } + llinfos << "time spent using customized memory pool: " << timer.getElapsedTimeF32() << llendl ; + + timer.reset() ; + + //using the standard allocator/de-allocator: + //allocation + for(U32 i = 0 ; i < times; i++) + { + p[i] = ::new char[size] ; + if(!p[i]) + { + llerrs << "allocation failed" << llendl ; + } + } + //de-allocation + for(U32 i = 0 ; i < times; i++) + { + ::delete[] p[i] ; + p[i] = NULL ; + } + llinfos << "time spent using standard allocator/de-allocator: " << timer.getElapsedTimeF32() << llendl ; + + delete[] p; } void LLPrivateMemoryPoolTester::correctnessTest() @@ -1715,56 +1769,15 @@ void LLPrivateMemoryPoolTester::correctnessTest() void LLPrivateMemoryPoolTester::performanceTest() { U32 test_size[3] = {768, 3* 1024, 3* 1024 * 1024}; - - S32 i ; - LLFrameTimer timer ; - - //do 1024 various-sized allocations / deallocations, compare the performance with the normal ones. - + //small sized - { - timer.reset() ; - char* p[1024] = {NULL} ; - for(i = 0 ; i < 1024; i++) - { - p[i] = sPool->allocate(test_size[0]) ; - if(!p[i]) - { - llerrs << "allocation failed" << llendl ; - } - } - - for(i = 0 ; i < 1024; i++) - { - sPool->free(p[i]) ; - p[i] = NULL ; - } - llinfos << "time spent on 1024 small allocations: %f " << timer.getElapsedTimeF32() << llendl ; - - timer.reset() ; - - //using the standard allocator/de-allocator: - for(i = 0 ; i < 1024; i++) - { - p[i] = ::new char[test_size[0]] ; - if(!p[i]) - { - llerrs << "allocation failed" << llendl ; - } - } - - for(i = 0 ; i < 1024; i++) - { - ::delete[] p[i] ; - p[i] = NULL ; - } - llinfos << "time spent on 1024 small allocations: %f using standard allocator/de-allocator." << timer.getElapsedTimeF32() << llendl ; - - timer.reset() ; - } + testAndTime(test_size[0], 8) ; + //medium sized + testAndTime(test_size[1], 8) ; //large sized + testAndTime(test_size[2], 8) ; } void LLPrivateMemoryPoolTester::fragmentationtest() -- cgit v1.2.3 From a3759a7815f7ba55b825bc76f30a1e333e01f295 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 6 Jan 2011 16:17:38 -0700 Subject: add the class LLPrivateMemoryPoolManager --- indra/llcommon/llmemory.cpp | 157 +++++++++++++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 52 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index f9a2770691..f1285841b3 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1404,14 +1404,10 @@ void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) return; //already inserted. } - need_rehash = mChunkHashList[start_key]->mHashNext != NULL ; - if(!need_rehash) - { - llassert_always(!chunk->mHashNext) ; + llassert_always(!chunk->mHashNext) ; - chunk->mHashNext = mChunkHashList[start_key] ; - mChunkHashList[start_key] = chunk ; - } + chunk->mHashNext = mChunkHashList[start_key] ; + mChunkHashList[start_key] = chunk ; } else { @@ -1440,52 +1436,15 @@ void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) { if(end_key < start_key) { - for(U16 i = start_key + 1 ; i < mHashFactor; i++) - { - if(mChunkHashList[i]) - { - llassert_always(mChunkHashList[i] != chunk) ; - need_rehash = true ; - break ; - } - else - { - mChunkHashList[i] = chunk ; - } - } - + need_rehash = fillHashTable(start_key + 1, mHashFactor, chunk) ; if(!need_rehash) { - for(U16 i = 0 ; i < end_key; i++) - { - if(mChunkHashList[i]) - { - llassert_always(mChunkHashList[i] != chunk) ; - need_rehash = true ; - break ; - } - else - { - mChunkHashList[i] = chunk ; - } - } + need_rehash = fillHashTable(0, end_key, chunk) ; } } else { - for(i = start_key + 1; i < end_key; i++) - { - if(mChunkHashList[i]) - { - llassert_always(mChunkHashList[i] != chunk) ; - need_rehash = true ; - break ; - } - else - { - mChunkHashList[i] = chunk ; - } - } + need_rehash = fillHashTable(start_key + 1, end_key, chunk) ; } } @@ -1495,7 +1454,7 @@ void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) while(HASH_FACTORS[i] <= mHashFactor) i++; mHashFactor = HASH_FACTORS[i] ; - llassert_always(mHashFactor != 0xFFFF) ;//stop point of the recursive calls + llassert_always(mHashFactor != 0xFFFF) ;//stop point to prevent endlessly recursive calls rehash() ; } @@ -1540,6 +1499,8 @@ void LLPrivateMemoryPool::removeFromHashTable(LLMemoryChunk* chunk) void LLPrivateMemoryPool::rehash() { + llinfos << "new hash factor: " << mHashFactor << llendl ; + mChunkHashList.clear() ; mChunkHashList.resize(mHashFactor, NULL) ; @@ -1556,8 +1517,100 @@ void LLPrivateMemoryPool::rehash() } } +bool LLPrivateMemoryPool::fillHashTable(U16 start, U16 end, LLMemoryChunk* chunk) +{ + for(U16 i = start; i < end; i++) + { + if(mChunkHashList[i]) //the slot is occupied. + { + llassert_always(mChunkHashList[i] != chunk) ; + return true ; + } + else + { + mChunkHashList[i] = chunk ; + } + } + + return false ; +} + +//-------------------------------------------------------------------- +//class LLPrivateMemoryPoolManager +//-------------------------------------------------------------------- +LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::sInstance = NULL ; + +LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager() +{ +} + +LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() +{ + //all private pools should be released by their owners before reaching here. + llassert_always(mPoolList.empty()) ; + +#if 0 + if(!mPoolList.empty()) + { + for(std::set::iterator iter = mPoolList.begin(); iter != mPoolList.end(); ++iter) + { + delete *iter; + } + mPoolList.clear() ; + } +#endif +} + +//static +LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::getInstance() +{ + if(!sInstance) + { + sInstance = new LLPrivateMemoryPoolManager() ; + } + return sInstance ; +} + +//static +void LLPrivateMemoryPoolManager::destroyClass() +{ + if(sInstance) + { + delete sInstance ; + sInstance = NULL ; + } +} + +LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(U32 max_size, bool threaded) +{ + LLPrivateMemoryPool* pool = new LLPrivateMemoryPool(max_size, threaded) ; + mPoolList.insert(pool) ; + + return pool ; +} + +void LLPrivateMemoryPoolManager::deletePool(LLPrivateMemoryPool* pool) +{ + mPoolList.erase(pool) ; + delete pool; +} + +//debug +void LLPrivateMemoryPoolManager::updateStatistics() +{ + mTotalReservedSize = 0 ; + mTotalAllocatedSize = 0 ; + + for(std::set::iterator iter = mPoolList.begin(); iter != mPoolList.end(); ++iter) + { + mTotalReservedSize += (*iter)->getTotalReservedSize() ; + mTotalAllocatedSize += (*iter)->getTotalAllocatedSize() ; + } +} + //-------------------------------------------------------------------- //class LLPrivateMemoryPoolTester +//-------------------------------------------------------------------- LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::sInstance = NULL ; LLPrivateMemoryPool* LLPrivateMemoryPoolTester::sPool = NULL ; LLPrivateMemoryPoolTester::LLPrivateMemoryPoolTester() @@ -1589,7 +1642,7 @@ void LLPrivateMemoryPoolTester::destroy() if(sPool) { - ::delete sPool ; + LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ; sPool = NULL ; } } @@ -1600,9 +1653,9 @@ void LLPrivateMemoryPoolTester::run(bool threaded) if(sPool) { - ::delete sPool ; + LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ; } - sPool = ::new LLPrivateMemoryPool(max_pool_size, threaded) ; + sPool = LLPrivateMemoryPoolManager::getInstance()->newPool(max_pool_size, threaded) ; //run the test correctnessTest() ; @@ -1610,7 +1663,7 @@ void LLPrivateMemoryPoolTester::run(bool threaded) //fragmentationtest() ; //release pool. - ::delete sPool ; + LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ; sPool = NULL ; } -- cgit v1.2.3 From 9434f0c2a0e46c580a2aacc04cc1b58876bd3bd8 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 6 Jan 2011 16:48:00 -0700 Subject: fix an exit crash. --- indra/llcommon/llmemory.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index f1285841b3..1f40f5e17a 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1248,13 +1248,18 @@ S32 LLPrivateMemoryPool::getChunkIndex(U32 size) void LLPrivateMemoryPool::destroyPool() { lock() ; - for(U32 i = 0 ; i < mHashFactor; i++) + if(mNumOfChunks > 0) { - while(mChunkHashList[i]) + for(U32 i = 0 ; i < mHashFactor; i++) { - removeChunk(mChunkHashList[i]) ; + while(mChunkHashList[i]) + { + removeChunk(mChunkHashList[i]) ; + } } } + mChunkHashList.clear() ; + mHashFactor = 1 ; llassert_always(mNumOfChunks == 0) ; llassert_always(mReservedPoolSize == 0) ; -- cgit v1.2.3 From 7daa3d1ca10199468946feef0ce8eb67489deee0 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Fri, 7 Jan 2011 14:57:35 -0700 Subject: fixed a hash bug, enlarged the overhead for large allocations, and add new chunk to the tail of the linked list so new allocations go to oldest chunks first. --- indra/llcommon/llmemory.cpp | 46 +++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 1f40f5e17a..543f17baf4 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -393,8 +393,6 @@ LLPrivateMemoryPool::LLMemoryBlock::~LLMemoryBlock() //create and initialize a memory block void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 slot_size) { - llassert_always(buffer_size >= slot_size) ; - mBuffer = buffer ; mBufferSize = buffer_size ; mSlotSize = slot_size ; @@ -590,12 +588,18 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 } //static -U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_page_size) +U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_slot_size, + U32 max_slot_size, U32 min_block_size, U32 max_block_size) { //for large allocations, reserve some extra memory for meta data to avoid wasting much - if(data_buffer_size / min_page_size < 64) //large allocations + if(data_buffer_size / min_slot_size < 64) //large allocations { - return 4096 ; //4KB + U32 overhead = sizeof(LLMemoryChunk) + (data_buffer_size / min_block_size) * sizeof(LLMemoryBlock) + + sizeof(LLMemoryBlock*) * (max_slot_size / min_slot_size) + sizeof(LLMemoryBlock*) * (max_block_size / min_block_size + 1) ; + + //round to integer times of min_block_size + overhead = ((overhead + min_block_size - 1) / min_block_size) * min_block_size ; + return overhead ; } else { @@ -1290,12 +1294,14 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_inde if(chunk_index < LARGE_ALLOCATION) { preferred_size = CHUNK_SIZE ; //4MB - overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; + overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_SLOT_SIZES[chunk_index], + MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ; } else { preferred_size = LARGE_CHUNK_SIZE ; //16MB - overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ; + overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_SLOT_SIZES[chunk_index], + MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ; } checkSize(preferred_size + overhead) ; @@ -1306,20 +1312,28 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_inde { return NULL ; } - memset(buffer, 0, preferred_size + overhead) ; - + LLMemoryChunk* chunk = new (buffer) LLMemoryChunk() ; chunk->init(buffer, preferred_size + overhead, MIN_SLOT_SIZES[chunk_index], MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ; - //add to the head of the linked list - chunk->mNext = mChunkList[chunk_index] ; - if(mChunkList[chunk_index]) + //add to the tail of the linked list { - mChunkList[chunk_index]->mPrev = chunk ; + if(!mChunkList[chunk_index]) + { + mChunkList[chunk_index] = chunk ; + } + else + { + LLMemoryChunk* cur = mChunkList[chunk_index] ; + while(cur->mNext) + { + cur = cur->mNext ; + } + cur->mNext = chunk ; + chunk->mPrev = cur ; + } } - chunk->mPrev = NULL ; - mChunkList[chunk_index] = chunk ; //insert into the hash table addToHashTable(chunk) ; @@ -1425,7 +1439,7 @@ void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) { llassert_always(mChunkHashList[end_key] != chunk) - need_rehash = mChunkHashList[end_key]->mHashNext != NULL ; + need_rehash = mChunkHashList[end_key]->mHashNext != NULL || mChunkHashList[end_key] == chunk->mHashNext; if(!need_rehash) { mChunkHashList[end_key]->mHashNext = chunk ; -- cgit v1.2.3 From bcb5b209d1813681202524362dd186c8b0982357 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 12 Jan 2011 07:51:14 -0800 Subject: trivial: fix some mac compiling errors --- indra/llcommon/llmemory.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 543f17baf4..875ff9971c 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -398,7 +398,7 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 mSlotSize = slot_size ; mTotalSlots = buffer_size / mSlotSize ; - llassert_always(mTotalSlots < 256) ; //max number is 256 + llassert_always(buffer_size / mSlotSize < 256) ; //max number is 256 mAllocatedSlots = 0 ; @@ -1137,7 +1137,7 @@ char* LLPrivateMemoryPool::allocate(U32 size) LLMemoryChunk* chunk = mChunkList[chunk_idx]; while(chunk) { - if(p = chunk->allocate(size)) + if((p = chunk->allocate(size))) { break ; } @@ -1152,7 +1152,7 @@ char* LLPrivateMemoryPool::allocate(U32 size) chunk = mChunkList[chunk_idx]; while(chunk) { - if(p = chunk->allocate(size)) + if((p = chunk->allocate(size))) { break ; } @@ -1185,7 +1185,7 @@ void LLPrivateMemoryPool::free(void* addr) if(!chunk) { - delete[] addr ; //release from heap + delete[] (char*)addr ; //release from heap } else { -- cgit v1.2.3 From 1f0cc074823b299ed77b3d6e90e4e8f4ea60415f Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 23 Feb 2011 12:41:33 -0700 Subject: fix an assert error --- indra/llcommon/llmemory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index a931cd62ba..08fc1ddfe5 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -572,7 +572,7 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 mSlotSize = slot_size ; mTotalSlots = buffer_size / mSlotSize ; - llassert_always(buffer_size / mSlotSize < 256) ; //max number is 256 + llassert_always(mTotalSlots <= 256) ; //max number is 256 mAllocatedSlots = 0 ; -- cgit v1.2.3 From d593f5c1d391d7406015e488d59c2b00beb7d8be Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 23 Feb 2011 14:35:53 -0700 Subject: fix a merge error --- indra/llcommon/llmemory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 08fc1ddfe5..1414ac7b9e 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1271,7 +1271,7 @@ LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : { if(threaded) { - mMutexp = new LLMutex(NULL) ; + mMutexp = new LLMutex ; } for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) -- cgit v1.2.3 From 108980f68c184341e83454bbd5e72a5803b33092 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 23 Feb 2011 17:53:08 -0700 Subject: add types to LLPrivateMemoryPool --- indra/llcommon/llmemory.cpp | 70 +++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 24 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 1414ac7b9e..062640f546 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1263,13 +1263,16 @@ U16 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size) //-------------------------------------------------------------------- const U32 CHUNK_SIZE = 4 << 20 ; //4 MB const U32 LARGE_CHUNK_SIZE = 4 * CHUNK_SIZE ; //16 MB -LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) : - mMutexp(NULL), - mMaxPoolSize(max_size), +LLPrivateMemoryPool::LLPrivateMemoryPool(S32 type) : + mMutexp(NULL), mReservedPoolSize(0), - mHashFactor(1) + mHashFactor(1), + mType(type) { - if(threaded) + const U32 MAX_POOL_SIZE = 256 * 1024 * 1024 ; //256 MB + + mMaxPoolSize = MAX_POOL_SIZE ; + if(type == STATIC_THREADED || type == VOLATILE_THREADED) { mMutexp = new LLMutex ; } @@ -1735,22 +1738,35 @@ LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::sInstance = NULL ; LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager() { + mPoolList.resize(LLPrivateMemoryPool::MAX_TYPES) ; + + for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++) + { + mPoolList[i] = NULL ; + } } LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() { +#if 0 //all private pools should be released by their owners before reaching here. - llassert_always(mPoolList.empty()) ; + for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++) + { + llassert_always(!mPoolList[i]) ; + } + mPoolList.clear() ; -#if 0 - if(!mPoolList.empty()) +#else + //forcefully release all memory + for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++) { - for(std::set::iterator iter = mPoolList.begin(); iter != mPoolList.end(); ++iter) + if(mPoolList[i]) { - delete *iter; + delete mPoolList[i] ; + mPoolList[i] = NULL ; } - mPoolList.clear() ; } + mPoolList.clear() ; #endif } @@ -1774,18 +1790,23 @@ void LLPrivateMemoryPoolManager::destroyClass() } } -LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(U32 max_size, bool threaded) +LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(S32 type) { - LLPrivateMemoryPool* pool = new LLPrivateMemoryPool(max_size, threaded) ; - mPoolList.insert(pool) ; + if(!mPoolList[type]) + { + mPoolList[type] = new LLPrivateMemoryPool(type) ; + } - return pool ; + return mPoolList[type] ; } void LLPrivateMemoryPoolManager::deletePool(LLPrivateMemoryPool* pool) { - mPoolList.erase(pool) ; - delete pool; + if(pool->isEmpty()) + { + mPoolList[pool->getType()] = NULL ; + delete pool; + } } //debug @@ -1794,10 +1815,13 @@ void LLPrivateMemoryPoolManager::updateStatistics() mTotalReservedSize = 0 ; mTotalAllocatedSize = 0 ; - for(std::set::iterator iter = mPoolList.begin(); iter != mPoolList.end(); ++iter) + for(U32 i = 0; i < mPoolList.size(); i++) { - mTotalReservedSize += (*iter)->getTotalReservedSize() ; - mTotalAllocatedSize += (*iter)->getTotalAllocatedSize() ; + if(mPoolList[i]) + { + mTotalReservedSize += mPoolList[i]->getTotalReservedSize() ; + mTotalAllocatedSize += mPoolList[i]->getTotalAllocatedSize() ; + } } } @@ -1840,15 +1864,13 @@ void LLPrivateMemoryPoolTester::destroy() } } -void LLPrivateMemoryPoolTester::run(bool threaded) +void LLPrivateMemoryPoolTester::run(S32 type) { - const U32 max_pool_size = 1024 << 20 ; - if(sPool) { LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ; } - sPool = LLPrivateMemoryPoolManager::getInstance()->newPool(max_pool_size, threaded) ; + sPool = LLPrivateMemoryPoolManager::getInstance()->newPool(type) ; //run the test correctnessTest() ; -- cgit v1.2.3 From fc106df53085f549acdbb2f8149ca75e400532fa Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 24 Feb 2011 19:47:55 -0700 Subject: fix the compiling error: "free" is defined and in use globally. --- indra/llcommon/llmemory.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 062640f546..49e2cd9ac4 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -572,7 +572,7 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 mSlotSize = slot_size ; mTotalSlots = buffer_size / mSlotSize ; - llassert_always(mTotalSlots <= 256) ; //max number is 256 + llassert_always(buffer_size / mSlotSize <= 256) ; //max number is 256 mAllocatedSlots = 0 ; @@ -669,7 +669,7 @@ char* LLPrivateMemoryPool::LLMemoryBlock::allocate() } //free a slot -void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr) +void LLPrivateMemoryPool::LLMemoryBlock::freeMem(void* addr) { //bit index U32 idx = ((U32)addr - (U32)mBuffer - mDummySize * sizeof(U32)) / mSlotSize ; @@ -850,14 +850,14 @@ char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size) return p ; } -void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr) +void LLPrivateMemoryPool::LLMemoryChunk::freeMem(void* addr) { U32 blk_idx = getPageIndex((U32)addr) ; LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ; blk = blk->mSelf ; bool was_full = blk->isFull() ; - blk->free(addr) ; + blk->freeMem(addr) ; mAlloatedSize -= blk->getSlotSize() ; if(blk->empty()) @@ -1349,7 +1349,7 @@ char* LLPrivateMemoryPool::allocate(U32 size) return p ; } -void LLPrivateMemoryPool::free(void* addr) +void LLPrivateMemoryPool::freeMem(void* addr) { if(!addr) { @@ -1366,7 +1366,7 @@ void LLPrivateMemoryPool::free(void* addr) } else { - chunk->free(addr) ; + chunk->freeMem(addr) ; if(chunk->empty()) { @@ -1929,7 +1929,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 if(p[i][k]) { llassert_always(*(U32*)p[i][k] == i && *((U32*)p[i][k] + 1) == k) ; - sPool->free(p[i][k]) ; + sPool->freeMem(p[i][k]) ; total_allocated_size -= min_size + k * stride ; p[i][k] = NULL ; } @@ -1950,7 +1950,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 if(p[i][j]) { llassert_always(*(U32*)p[i][j] == i && *((U32*)p[i][j] + 1) == j) ; - sPool->free(p[i][j]) ; + sPool->freeMem(p[i][j]) ; total_allocated_size -= min_size + j * stride ; p[i][j] = NULL ; } @@ -1984,7 +1984,7 @@ void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times) //de-allocation for(U32 i = 0 ; i < times; i++) { - sPool->free(p[i]) ; + sPool->freeMem(p[i]) ; p[i] = NULL ; } llinfos << "time spent using customized memory pool: " << timer.getElapsedTimeF32() << llendl ; @@ -2019,7 +2019,7 @@ void LLPrivateMemoryPoolTester::correctnessTest() //edge case char* p = sPool->allocate(0) ; - sPool->free(p) ; + sPool->freeMem(p) ; //small sized // [8 bytes, 2KB), each asks for 256 allocations and deallocations -- cgit v1.2.3 From a40ee94cd6768445fdaeffd18bf4392e1398d42b Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Mon, 4 Apr 2011 10:00:29 -0600 Subject: fix the bug for mac and linux of continuously adjusting memory. --- indra/llcommon/llmemory.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 49e2cd9ac4..62db7e3385 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -140,10 +140,10 @@ void* LLMemory::tryToAlloc(void* address, U32 size) llerrs << "error happens when free some memory reservation." << llendl ; } } -#else -#endif - return address ; +#else + return 0x01 ; //skip checking +#endif } //static -- cgit v1.2.3 From 17854c4e8702febaa8fe4adfbc678f9abaaa52c7 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Mon, 4 Apr 2011 14:40:07 -0600 Subject: fix an issue on mac and linux, also fix an assertion. --- indra/llcommon/llmemory.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 62db7e3385..dfc00b5e0a 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -142,7 +142,7 @@ void* LLMemory::tryToAlloc(void* address, U32 size) } return address ; #else - return 0x01 ; //skip checking + return (void*)0x01 ; //skip checking #endif } @@ -1079,6 +1079,8 @@ LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNe if(new_free_blk_size > 0) //blk still has free space { LLMemoryBlock* next_blk = blk + (buffer_size / mMinBlockSize) ; + next_blk->mPrev = NULL ; + next_blk->mNext = NULL ; next_blk->setBuffer(blk->getBuffer() + buffer_size, new_free_blk_size) ; addToFreeSpace(next_blk) ; } -- cgit v1.2.3 From b594d3b04d3095f15750436910debdd5a602a872 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 10 May 2011 21:02:20 -0600 Subject: add debug mode to track the memory allocation/deallocation. --- indra/llcommon/llmemory.cpp | 117 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 107 insertions(+), 10 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index dfc00b5e0a..8f65107e47 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -57,6 +57,10 @@ U32 LLMemory::sAllocatedPageSizeInKB = 0 ; U32 LLMemory::sMaxHeapSizeInKB = U32_MAX ; BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE; +#if __DEBUG_PRIVATE_MEM__ +LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sMemAllocationTracker; +#endif + //static void LLMemory::initClass() { @@ -1431,8 +1435,14 @@ S32 LLPrivateMemoryPool::getChunkIndex(U32 size) void LLPrivateMemoryPool::destroyPool() { lock() ; - if(mNumOfChunks > 0) + +#if 0 + if(mNumOfChunks > 0) { + //Warn: + //should crash here because there is memory leaking if reach here. + // + for(U32 i = 0 ; i < mHashFactor; i++) { while(mChunkHashList[i]) @@ -1441,11 +1451,19 @@ void LLPrivateMemoryPool::destroyPool() } } } - mChunkHashList.clear() ; - mHashFactor = 1 ; + llassert_always(mNumOfChunks == 0) ; llassert_always(mReservedPoolSize == 0) ; +#endif + + if(mNumOfChunks > 0) + { + llwarns << "There is some memory not freed when destroy the memory pool!" << llendl ; + } + mNumOfChunks = 0 ; + mChunkHashList.clear() ; + mHashFactor = 1 ; for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++) { mChunkList[i] = NULL ; @@ -1750,6 +1768,21 @@ LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager() LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() { + +#if __DEBUG_PRIVATE_MEM__ + if(!sMemAllocationTracker.empty()) + { + llwarns << "there is potential memory leaking here. The list of not freed memory blocks are from: " <second << llendl ; + } + sMemAllocationTracker.clear() ; + } +#endif + #if 0 //all private pools should be released by their owners before reaching here. for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++) @@ -1827,6 +1860,70 @@ void LLPrivateMemoryPoolManager::updateStatistics() } } +#if __DEBUG_PRIVATE_MEM__ +//static +char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size, const char* function, const int line) +{ + char* p ; + + if(!poolp) + { + p = new char[size] ; + } + else + { + p = poolp->allocate(size) ; + } + + if(p) + { + char num[16] ; + sprintf(num, " line: %d ", line) ; + std::string str(function) ; + str += num; + + sMemAllocationTracker[p] = str ; + } + + return p ; +} +#else +//static +char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size) +{ + if(!poolp) + { + return new char[size] ; + } + else + { + return poolp->allocate(size) ; + } +} +#endif + +//static +void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr) +{ + if(!addr) + { + return ; + } + +#if __DEBUG_PRIVATE_MEM__ + sMemAllocationTracker.erase((char*)addr) ; +#endif + + if(poolp) + { + poolp->freeMem(addr) ; + } + else + { + delete[] addr ; + } +} + //-------------------------------------------------------------------- //class LLPrivateMemoryPoolTester //-------------------------------------------------------------------- @@ -1915,7 +2012,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 for(j = 0 ; j < levels; j++) { size = min_size + j * stride ; - p[i][j] = sPool->allocate(size) ; + p[i][j] = ALLOCATE_MEM(sPool, size) ; total_allocated_size+= size ; @@ -1931,7 +2028,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 if(p[i][k]) { llassert_always(*(U32*)p[i][k] == i && *((U32*)p[i][k] + 1) == k) ; - sPool->freeMem(p[i][k]) ; + FREE_MEM(sPool, p[i][k]) ; total_allocated_size -= min_size + k * stride ; p[i][k] = NULL ; } @@ -1952,7 +2049,7 @@ void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 if(p[i][j]) { llassert_always(*(U32*)p[i][j] == i && *((U32*)p[i][j] + 1) == j) ; - sPool->freeMem(p[i][j]) ; + FREE_MEM(sPool, p[i][j]) ; total_allocated_size -= min_size + j * stride ; p[i][j] = NULL ; } @@ -1977,7 +2074,7 @@ void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times) //allocation for(U32 i = 0 ; i < times; i++) { - p[i] = sPool->allocate(size) ; + p[i] = ALLOCATE_MEM(sPool, size) ; if(!p[i]) { llerrs << "allocation failed" << llendl ; @@ -1986,7 +2083,7 @@ void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times) //de-allocation for(U32 i = 0 ; i < times; i++) { - sPool->freeMem(p[i]) ; + FREE_MEM(sPool, p[i]) ; p[i] = NULL ; } llinfos << "time spent using customized memory pool: " << timer.getElapsedTimeF32() << llendl ; @@ -2020,8 +2117,8 @@ void LLPrivateMemoryPoolTester::correctnessTest() //to see if allocation is right. //edge case - char* p = sPool->allocate(0) ; - sPool->freeMem(p) ; + char* p = ALLOCATE_MEM(sPool, 0) ; + FREE_MEM(sPool, p) ; //small sized // [8 bytes, 2KB), each asks for 256 allocations and deallocations -- cgit v1.2.3 From 39f033a013278206fa6a93273965bd11066ee492 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 10 May 2011 21:13:20 -0600 Subject: fix a linux compiling error. --- indra/llcommon/llmemory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 8f65107e47..2d190fe660 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1920,7 +1920,7 @@ void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr } else { - delete[] addr ; + delete[] (char*)addr ; } } -- cgit v1.2.3 From d696977c705929b944359f4a5bbb7501645f62bc Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 11 May 2011 14:23:15 -0600 Subject: fix a crash --- indra/llcommon/llmemory.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 2d190fe660..4143f630c8 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1629,6 +1629,10 @@ void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) { mChunkHashList[start_key] = chunk ; } + if(start_key == end_key) + { + return ; //done + } if(!need_rehash) { -- cgit v1.2.3 From d31e6735370711088f01cff448aa22f71c4c10c4 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 11 May 2011 14:41:23 -0600 Subject: fix a crash --- indra/llcommon/llmemory.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 4143f630c8..629e76f341 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -1687,6 +1687,10 @@ void LLPrivateMemoryPool::removeFromHashTable(LLMemoryChunk* chunk) mChunkHashList[start_key] = chunk->mHashNext ; chunk->mHashNext = NULL ; + if(start_key == end_key) + { + return ; //done + } if(mChunkHashList[end_key] != chunk) { -- cgit v1.2.3 From 76eca5d0bce3e303f6d77b0d16f114320830ac6a Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 19 Jul 2011 23:17:55 -0600 Subject: fix for memory alignment to 16 bytes. --- indra/llcommon/llmemory.cpp | 98 ++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 37 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index ed28974163..6e804a94b0 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -62,7 +62,7 @@ LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sM #endif #ifndef _USE_PRIVATE_MEM_POOL_ -#define _USE_PRIVATE_MEM_POOL_ 0 +#define _USE_PRIVATE_MEM_POOL_ 1 #endif //static @@ -535,6 +535,9 @@ const char* LLMemTracker::getNextLine() //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- +//minimum slot size and minimal slot size interval +const U32 ATOMIC_MEM_SLOT = 16 ; //bytes + //minimum block sizes (page size) for small allocation, medium allocation, large allocation const U32 MIN_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {2 << 10, 4 << 10, 16 << 10} ; // @@ -542,14 +545,30 @@ const U32 MIN_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {2 << 10, 4 < const U32 MAX_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {64 << 10, 1 << 20, 4 << 20} ; //minimum slot sizes for small allocation, medium allocation, large allocation -const U32 MIN_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {8, 2 << 10, 512 << 10}; +const U32 MIN_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {ATOMIC_MEM_SLOT, 2 << 10, 512 << 10}; //maximum slot sizes for small allocation, medium allocation, large allocation -const U32 MAX_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {(2 << 10) - 8, (512 - 2) << 10, 4 << 20}; +const U32 MAX_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {(2 << 10) - ATOMIC_MEM_SLOT, (512 - 2) << 10, 4 << 20}; //size of a block with multiple slots can not exceed CUT_OFF_SIZE const U32 CUT_OFF_SIZE = (64 << 10) ; //64 KB +//max number of slots in a block +const U32 MAX_NUM_SLOTS_IN_A_BLOCK = llmin(MIN_BLOCK_SIZES[0] / ATOMIC_MEM_SLOT, ATOMIC_MEM_SLOT * 8) ; + +//------------------------------------------------------------- +//align val to be integer times of ATOMIC_MEM_SLOT +U32 align(U32 val) +{ + U32 aligned = (val / ATOMIC_MEM_SLOT) * ATOMIC_MEM_SLOT ; + if(aligned < val) + { + aligned += ATOMIC_MEM_SLOT ; + } + + return aligned ; +} + //------------------------------------------------------------- //class LLPrivateMemoryPool::LLMemoryBlock //------------------------------------------------------------- @@ -575,35 +594,36 @@ void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 mSlotSize = slot_size ; mTotalSlots = buffer_size / mSlotSize ; - llassert_always(buffer_size / mSlotSize <= 256) ; //max number is 256 + llassert_always(buffer_size / mSlotSize <= MAX_NUM_SLOTS_IN_A_BLOCK) ; //max number is 128 mAllocatedSlots = 0 ; + mDummySize = 0 ; //init the bit map. - //mark free bits - S32 usage_bit_len = (mTotalSlots + 31) / 32 ; - mDummySize = usage_bit_len - 1 ; //if the mTotalSlots more than 32, needs extra space for bit map - if(mDummySize > 0) //reserve extra space from mBuffer to store bitmap if needed. + //mark free bits + if(mTotalSlots > 32) //reserve extra space from mBuffer to store bitmap if needed. { - mTotalSlots -= (mDummySize * sizeof(mUsageBits) + mSlotSize - 1) / mSlotSize ; - usage_bit_len = (mTotalSlots + 31) / 32 ; - mDummySize = usage_bit_len - 1 ;//number of 32bits reserved from mBuffer for bitmap + mDummySize = ATOMIC_MEM_SLOT ; + mTotalSlots -= (mDummySize + mSlotSize - 1) / mSlotSize ; + mUsageBits = 0 ; - if(mDummySize > 0) + S32 usage_bit_len = (mTotalSlots + 31) / 32 ; + + for(S32 i = 0 ; i < usage_bit_len - 1 ; i++) { - mUsageBits = 0 ; - for(S32 i = 0 ; i < mDummySize ; i++) - { - *((U32*)mBuffer + i) = 0 ; - } - if(mTotalSlots & 31) - { - *((U32*)mBuffer + mDummySize - 1) = (0xffffffff << (mTotalSlots & 31)) ; - } + *((U32*)mBuffer + i) = 0 ; } - } - - if(mDummySize < 1)//no extra bitmap space reserved + for(S32 i = usage_bit_len - 1 ; i < mDummySize / sizeof(U32) ; i++) + { + *((U32*)mBuffer + i) = 0xffffffff ; + } + + if(mTotalSlots & 31) + { + *((U32*)mBuffer + usage_bit_len - 2) = (0xffffffff << (mTotalSlots & 31)) ; + } + } + else//no extra bitmap space reserved { mUsageBits = 0 ; if(mTotalSlots & 31) @@ -642,7 +662,7 @@ char* LLPrivateMemoryPool::LLMemoryBlock::allocate() } else if(mDummySize > 0)//go to extra space { - for(S32 i = 0 ; i < mDummySize; i++) + for(S32 i = 0 ; i < mDummySize / sizeof(U32); i++) { if(*((U32*)mBuffer + i) != 0xffffffff) { @@ -668,14 +688,14 @@ char* LLPrivateMemoryPool::LLMemoryBlock::allocate() mAllocatedSlots++ ; - return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ; + return mBuffer + mDummySize + (k * 32 + idx) * mSlotSize ; } //free a slot void LLPrivateMemoryPool::LLMemoryBlock::freeMem(void* addr) { //bit index - U32 idx = ((U32)addr - (U32)mBuffer - mDummySize * sizeof(U32)) / mSlotSize ; + U32 idx = ((U32)addr - (U32)mBuffer - mDummySize) / mSlotSize ; U32* bits = &mUsageBits ; if(idx >= 32) @@ -699,7 +719,7 @@ void LLPrivateMemoryPool::LLMemoryBlock::freeMem(void* addr) //for debug use: reset the entire bitmap. void LLPrivateMemoryPool::LLMemoryBlock::resetBitMap() { - for(S32 i = 0 ; i < mDummySize ; i++) + for(S32 i = 0 ; i < mDummySize / sizeof(U32) ; i++) { *((U32*)mBuffer + i) = 0 ; } @@ -742,7 +762,10 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 //data buffer, which can be used for allocation mDataBuffer = (char*)mFreeSpaceList + sizeof(LLMemoryBlock*) * mPartitionLevels ; - + + //alignmnet + mDataBuffer = mBuffer + align(mDataBuffer - mBuffer) ; + //init for(U32 i = 0 ; i < mBlockLevels; i++) { @@ -1306,7 +1329,7 @@ char* LLPrivateMemoryPool::allocate(U32 size) //if the asked size larger than MAX_BLOCK_SIZE, fetch from heap directly, the pool does not manage it if(size >= CHUNK_SIZE) { - return new char[size] ; + return (char*)malloc(size) ; } char* p = NULL ; @@ -1367,7 +1390,7 @@ void LLPrivateMemoryPool::freeMem(void* addr) if(!chunk) { - delete[] (char*)addr ; //release from heap + free(addr) ; //release from heap } else { @@ -1503,7 +1526,7 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_inde checkSize(preferred_size + overhead) ; mReservedPoolSize += preferred_size + overhead ; - char* buffer = new(std::nothrow) char[preferred_size + overhead] ; + char* buffer = (char*)malloc(preferred_size + overhead) ; if(!buffer) { return NULL ; @@ -1571,7 +1594,7 @@ void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk) mReservedPoolSize -= chunk->getBufferSize() ; //release memory - delete[] chunk->getBuffer() ; + free(chunk->getBuffer()) ; } U16 LLPrivateMemoryPool::findHashKey(const char* addr) @@ -1875,7 +1898,7 @@ char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size, if(!poolp) { - p = new char[size] ; + p = (char*)malloc(size) ; } else { @@ -1901,7 +1924,7 @@ char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size) #if _USE_PRIVATE_MEM_POOL_ if(!poolp) { - return new char[size] ; + return (char*)malloc(size) ; } else { @@ -1932,7 +1955,7 @@ void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr } else { - delete[] (char*)addr ; + free(addr) ; } #else free(addr) ; @@ -1942,6 +1965,7 @@ void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr //-------------------------------------------------------------------- //class LLPrivateMemoryPoolTester //-------------------------------------------------------------------- +#if 0 LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::sInstance = NULL ; LLPrivateMemoryPool* LLPrivateMemoryPoolTester::sPool = NULL ; LLPrivateMemoryPoolTester::LLPrivateMemoryPoolTester() @@ -2168,5 +2192,5 @@ void LLPrivateMemoryPoolTester::fragmentationtest() //every time when asking for a new chunk during correctness test, and performance test, //print out the chunk usage statistices. } - +#endif //-------------------------------------------------------------------- -- cgit v1.2.3 From 5ee90d78488690e49a8195ce2a08034f73b637ee Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 20 Jul 2011 10:24:44 -0600 Subject: fix a merge error --- indra/llcommon/llmemory.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 6e804a94b0..eb55bdae84 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -110,7 +110,10 @@ void LLMemory::updateMemoryInfo() sAllocatedMemInKB = (U32)(counters.WorkingSetSize / 1024) ; sAllocatedPageSizeInKB = (U32)(counters.PagefileUsage / 1024) ; - sMaxPhysicalMemInKB = llmin(LLMemoryInfo::getAvailableMemoryKB() + sAllocatedMemInKB, sMaxHeapSizeInKB); + + U32 avail_phys, avail_virtual; + LLMemoryInfo::getAvailableMemoryKB(avail_phys, avail_virtual) ; + sMaxPhysicalMemInKB = llmin(avail_phys + sAllocatedMemInKB, sMaxHeapSizeInKB); if(sMaxPhysicalMemInKB > sAllocatedMemInKB) { -- cgit v1.2.3 From 48d949150cd445ce1e801a7a8ee67597a965f14b Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 20 Jul 2011 16:05:19 -0600 Subject: add a debug setting "MemoryPrivatePoolEnabled" to turn on/off private memory pool. --- indra/llcommon/llmemory.cpp | 51 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index eb55bdae84..0d36009fc4 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -61,10 +61,6 @@ BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE; LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sMemAllocationTracker; #endif -#ifndef _USE_PRIVATE_MEM_POOL_ -#define _USE_PRIVATE_MEM_POOL_ 1 -#endif - //static void LLMemory::initClass() { @@ -1386,7 +1382,7 @@ void LLPrivateMemoryPool::freeMem(void* addr) { return ; } - + lock() ; LLMemoryChunk* chunk = findChunk((char*)addr) ; @@ -1789,7 +1785,7 @@ bool LLPrivateMemoryPool::fillHashTable(U16 start, U16 end, LLMemoryChunk* chunk //-------------------------------------------------------------------- LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::sInstance = NULL ; -LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager() +LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager(BOOL enabled) { mPoolList.resize(LLPrivateMemoryPool::MAX_TYPES) ; @@ -1797,6 +1793,8 @@ LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager() { mPoolList[i] = NULL ; } + + mPrivatePoolEnabled = enabled ; } LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() @@ -1838,13 +1836,21 @@ LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager() #endif } +//static +void LLPrivateMemoryPoolManager::initClass(BOOL enabled) +{ + llassert_always(!sInstance) ; + + sInstance = new LLPrivateMemoryPoolManager(enabled) ; +} + //static LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::getInstance() { - if(!sInstance) - { - sInstance = new LLPrivateMemoryPoolManager() ; - } + //if(!sInstance) + //{ + // sInstance = new LLPrivateMemoryPoolManager(FALSE) ; + //} return sInstance ; } @@ -1860,6 +1866,11 @@ void LLPrivateMemoryPoolManager::destroyClass() LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(S32 type) { + if(!mPrivatePoolEnabled) + { + return NULL ; + } + if(!mPoolList[type]) { mPoolList[type] = new LLPrivateMemoryPool(type) ; @@ -1870,7 +1881,7 @@ LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(S32 type) void LLPrivateMemoryPoolManager::deletePool(LLPrivateMemoryPool* pool) { - if(pool->isEmpty()) + if(pool && pool->isEmpty()) { mPoolList[pool->getType()] = NULL ; delete pool; @@ -1907,7 +1918,7 @@ char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size, { p = poolp->allocate(size) ; } - + if(p) { char num[16] ; @@ -1924,18 +1935,14 @@ char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size, //static char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size) { -#if _USE_PRIVATE_MEM_POOL_ - if(!poolp) + if(poolp) { - return (char*)malloc(size) ; + return poolp->allocate(size) ; } else { - return poolp->allocate(size) ; + return (char*)malloc(size) ; } -#else - return (char*)malloc(size) ; -#endif } #endif @@ -1951,7 +1958,6 @@ void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr sMemAllocationTracker.erase((char*)addr) ; #endif -#if _USE_PRIVATE_MEM_POOL_ if(poolp) { poolp->freeMem(addr) ; @@ -1959,10 +1965,7 @@ void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr else { free(addr) ; - } -#else - free(addr) ; -#endif + } } //-------------------------------------------------------------------- -- cgit v1.2.3 From ba2ae6bc9583420a07245b4a7af2a779fb02b6c9 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Fri, 2 Sep 2011 11:17:03 -0600 Subject: re-write the hash table code to eliminate potential flaws and simplify the implementation. --- indra/llcommon/llmemory.cpp | 153 ++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 82 deletions(-) (limited to 'indra/llcommon/llmemory.cpp') diff --git a/indra/llcommon/llmemory.cpp b/indra/llcommon/llmemory.cpp index 0d36009fc4..8c02ad8290 100644 --- a/indra/llcommon/llmemory.cpp +++ b/indra/llcommon/llmemory.cpp @@ -781,7 +781,6 @@ void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 mBlocks[0].setBuffer(mDataBuffer, buffer_size - (mDataBuffer - mBuffer)) ; addToFreeSpace(&mBlocks[0]) ; - mHashNext = NULL ; mNext = NULL ; mPrev = NULL ; } @@ -1457,26 +1456,6 @@ void LLPrivateMemoryPool::destroyPool() { lock() ; -#if 0 - if(mNumOfChunks > 0) - { - //Warn: - //should crash here because there is memory leaking if reach here. - // - - for(U32 i = 0 ; i < mHashFactor; i++) - { - while(mChunkHashList[i]) - { - removeChunk(mChunkHashList[i]) ; - } - } - } - - llassert_always(mNumOfChunks == 0) ; - llassert_always(mReservedPoolSize == 0) ; -#endif - if(mNumOfChunks > 0) { llwarns << "There is some memory not freed when destroy the memory pool!" << llendl ; @@ -1609,14 +1588,7 @@ LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::findChunk(const char* a return NULL ; } - //check the hash value "key" - LLMemoryChunk* chunk = mChunkHashList[key] ; - while(chunk && !chunk->containsAddress(addr)) - { - chunk = chunk->mHashNext ; - } - - return chunk ; + return mChunkHashList[key].findChunk(addr) ; } void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) @@ -1634,43 +1606,20 @@ void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk) U16 end_key = findHashKey(chunk->getBuffer() + chunk->getBufferSize() - 1) ; bool need_rehash = false ; - if(mChunkHashList[start_key]) + if(mChunkHashList[start_key].hasElement(chunk)) { - if(mChunkHashList[start_key] == chunk) - { - return; //already inserted. - } - - llassert_always(!chunk->mHashNext) ; - - chunk->mHashNext = mChunkHashList[start_key] ; - mChunkHashList[start_key] = chunk ; + return; //already inserted. } - else - { - mChunkHashList[start_key] = chunk ; - } - if(start_key == end_key) + need_rehash = mChunkHashList[start_key].add(chunk) ; + + if(start_key == end_key && !need_rehash) { return ; //done } if(!need_rehash) { - if(mChunkHashList[end_key]) - { - llassert_always(mChunkHashList[end_key] != chunk) - - need_rehash = mChunkHashList[end_key]->mHashNext != NULL || mChunkHashList[end_key] == chunk->mHashNext; - if(!need_rehash) - { - mChunkHashList[end_key]->mHashNext = chunk ; - } - } - else - { - mChunkHashList[end_key] = chunk ; - } + need_rehash = mChunkHashList[end_key].add(chunk) ; } if(!need_rehash) @@ -1706,38 +1655,30 @@ void LLPrivateMemoryPool::removeFromHashTable(LLMemoryChunk* chunk) U16 start_key = findHashKey(chunk->getBuffer()) ; U16 end_key = findHashKey(chunk->getBuffer() + chunk->getBufferSize() - 1) ; - mChunkHashList[start_key] = chunk->mHashNext ; - chunk->mHashNext = NULL ; + mChunkHashList[start_key].remove(chunk) ; if(start_key == end_key) { return ; //done } - if(mChunkHashList[end_key] != chunk) - { - mChunkHashList[end_key]->mHashNext = NULL ; - } - else - { - mChunkHashList[end_key] = NULL ; - } - + mChunkHashList[end_key].remove(chunk) ; + if(end_key < start_key) { for(U16 i = start_key + 1 ; i < mHashFactor; i++) { - mChunkHashList[i] = NULL ; + mChunkHashList[i].remove(chunk) ; } for(U16 i = 0 ; i < end_key; i++) { - mChunkHashList[i] = NULL ; + mChunkHashList[i].remove(chunk) ; } } else { for(U16 i = start_key + 1 ; i < end_key; i++) { - mChunkHashList[i] = NULL ; + mChunkHashList[i].remove(chunk) ; } } } @@ -1747,7 +1688,7 @@ void LLPrivateMemoryPool::rehash() llinfos << "new hash factor: " << mHashFactor << llendl ; mChunkHashList.clear() ; - mChunkHashList.resize(mHashFactor, NULL) ; + mChunkHashList.resize(mHashFactor) ; LLMemoryChunk* chunk ; for(U16 i = 0 ; i < SUPER_ALLOCATION ; i++) @@ -1755,7 +1696,6 @@ void LLPrivateMemoryPool::rehash() chunk = mChunkList[i] ; while(chunk) { - chunk->mHashNext = NULL ; addToHashTable(chunk) ; chunk = chunk->mNext ; } @@ -1766,20 +1706,69 @@ bool LLPrivateMemoryPool::fillHashTable(U16 start, U16 end, LLMemoryChunk* chunk { for(U16 i = start; i < end; i++) { - if(mChunkHashList[i]) //the slot is occupied. - { - llassert_always(mChunkHashList[i] != chunk) ; + if(mChunkHashList[i].add(chunk)) + { return true ; - } - else - { - mChunkHashList[i] = chunk ; - } + } + } + + return false ; +} + +//-------------------------------------------------------------------- +// class LLChunkHashElement +//-------------------------------------------------------------------- +LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::LLChunkHashElement::findChunk(const char* addr) +{ + if(mFirst && mFirst->containsAddress(addr)) + { + return mFirst ; + } + else if(mSecond && mSecond->containsAddress(addr)) + { + return mSecond ; + } + + return NULL ; +} + +//return false if successfully inserted to the hash slot. +bool LLPrivateMemoryPool::LLChunkHashElement::add(LLPrivateMemoryPool::LLMemoryChunk* chunk) +{ + llassert_always(!hasElement(chunk)) ; + + if(!mFirst) + { + mFirst = chunk ; + } + else if(!mSecond) + { + mSecond = chunk ; + } + else + { + return true ; //failed } return false ; } +void LLPrivateMemoryPool::LLChunkHashElement::remove(LLPrivateMemoryPool::LLMemoryChunk* chunk) +{ + if(mFirst == chunk) + { + mFirst = NULL ; + } + else if(mSecond ==chunk) + { + mSecond = NULL ; + } + else + { + llerrs << "This slot does not contain this chunk!" << llendl ; + } +} + //-------------------------------------------------------------------- //class LLPrivateMemoryPoolManager //-------------------------------------------------------------------- -- cgit v1.2.3