Skip to content

Commit

Permalink
Track the total usage of the code cache
Browse files Browse the repository at this point in the history
To support `-XX:+PrintCodeCache`(eclipse-openj9/openj9#8184), the high water
mark of the code cache usage needs to be tracked.
The usage is tracked when increasing/decreasing the code cache repository
take place and also the reuse of the free blocks in the code cache.

Also added information on the config size, the total free size,
and the used size for each code cache in `printOccupancyStats()`
which is used by OpenJ9 to show the code cache usage
when `TR_PrintCompMem` is set.

Related to eclipse-openj9/openj9#8184

Signed-off-by: Annabelle Huo <Annabelle.Huo@ibm.com>
  • Loading branch information
a7ehuo committed Jan 24, 2020
1 parent 754ff1a commit dbb88d1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
25 changes: 19 additions & 6 deletions compiler/runtime/OMRCodeCache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corp. and others
* Copyright (c) 2000, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -229,7 +229,7 @@ OMR::CodeCache::trimCodeMemoryAllocation(void *codeMemoryStart, size_t actualSiz

if (expectedHeapAlloc == _warmCodeAlloc)
{
_manager->increaseFreeSpaceInCodeCacheRepository(shrinkage);
_manager->decreaseCurrTotalUsedInBytes(shrinkage);
_warmCodeAlloc -= shrinkage;
cacheHeader->_size = actualSizeInBytes;
return true;
Expand Down Expand Up @@ -402,9 +402,8 @@ OMR::CodeCache::initialize(TR::CodeCacheManager *manager,

// Before returning, let's adjust the free space seen by VM.
// Usable space is between _warmCodeAlloc and _trampolineBase. Everything else is overhead
// Only relevant if code cache repository is used
size_t spaceLost = (_warmCodeAlloc - _segment->segmentBase()) + (_segment->segmentTop() - _trampolineBase);
_manager->decreaseFreeSpaceInCodeCacheRepository(spaceLost);
_manager->increaseCurrTotalUsedInBytes(spaceLost);

return true;
}
Expand Down Expand Up @@ -1065,6 +1064,8 @@ OMR::CodeCache::addFreeBlock2WithCallSite(uint8_t *start,

self()->updateMaxSizeOfFreeBlocks(link, link->_size);

_manager->decreaseCurrTotalUsedInBytes(size);

if (config.verboseReclamation())
{
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE,"--ccr-- addFreeBlock2WithCallSite CC=%p start=%p end=%p mergedBlock=%p link=%p link->_size=%u, _sizeOfLargestFreeWarmBlock=%d _sizeOfLargestFreeColdBlock=%d warmCodeAlloc=%p coldBlockAlloc=%p",
Expand Down Expand Up @@ -1215,6 +1216,8 @@ OMR::CodeCache::findFreeBlock(size_t size, bool isCold, bool isMethodHeaderNeede
{
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE,"--ccr- findFreeBlock: CodeCache=%p size=%u isCold=%d bestFitLink=%p bestFitLink->size=%u leftBlock=%p", this, size, isCold, bestFitLink, bestFitLink->_size, leftBlock);
}

_manager->increaseCurrTotalUsedInBytes(bestFitLink->_size);
}
// Because we call this method only after we made sure a free block exists
// this function can never return NULL
Expand Down Expand Up @@ -1288,6 +1291,7 @@ OMR::CodeCache::printOccupancyStats()
fprintf(stderr, "Code Cache @%p flags=0x%x almostFull=%d\n", this, _flags, _almostFull);
fprintf(stderr, " cold-warm hole size = %8" OMR_PRIuSIZE " bytes\n", self()->getFreeContiguousSpace());
fprintf(stderr, " warmCodeAlloc=%p coldCodeAlloc=%p\n", (void*)_warmCodeAlloc, (void*)_coldCodeAlloc);
size_t totalReclaimed=0;
if (_freeBlockList)
{
fprintf(stderr, " sizeOfLargestFreeColdBlock = %8" OMR_PRIuSIZE " bytes\n", _sizeOfLargestFreeColdBlock);
Expand All @@ -1297,7 +1301,10 @@ OMR::CodeCache::printOccupancyStats()
{
CacheCriticalSection resolveAndCreateTrampoline(self());
for (CodeCacheFreeCacheBlock *currLink = _freeBlockList; currLink; currLink = currLink->_next)
{
fprintf(stderr, " %" OMR_PRIuSIZE, currLink->_size);
totalReclaimed += currLink->_size;
}
}
fprintf(stderr, "\n");
}
Expand All @@ -1309,6 +1316,12 @@ OMR::CodeCache::printOccupancyStats()
(int32_t)(_trampolineReservationMark - _trampolineBase),
(int32_t)(_tempTrampolineNext - _tempTrampolineBase));
}

size_t totalConfigSizeInBytes = config.codeCacheKB() * 1024;
size_t totalFreeSizeInBytes = self()->getFreeContiguousSpace() + totalReclaimed;
fprintf(stderr, " config size = %8" OMR_PRIuSIZE " bytes\n", totalConfigSizeInBytes);
fprintf(stderr, " total free size = %8" OMR_PRIuSIZE " bytes\n", totalFreeSizeInBytes);
fprintf(stderr, " total used size = %8" OMR_PRIuSIZE " bytes\n", totalConfigSizeInBytes - totalFreeSizeInBytes);
}


Expand Down Expand Up @@ -1559,7 +1572,7 @@ OMR::CodeCache::allocateCodeMemory(size_t warmCodeSize,

// _warmCodeAlloc will change to its new value 'cacheHeapAlloc'
// Thus the free code cache space decreases by (cacheHeapAlloc-_warmCodeAlloc)
_manager->decreaseFreeSpaceInCodeCacheRepository(cacheHeapAlloc-_warmCodeAlloc);
_manager->increaseCurrTotalUsedInBytes(cacheHeapAlloc-_warmCodeAlloc);
_warmCodeAlloc = cacheHeapAlloc;
if (isMethodHeaderNeeded)
self()->writeMethodHeader(warmCodeAddress, warmSize, false);
Expand Down Expand Up @@ -1598,7 +1611,7 @@ OMR::CodeCache::allocateCodeMemory(size_t warmCodeSize,
}
return NULL;
}
_manager->decreaseFreeSpaceInCodeCacheRepository(_coldCodeAlloc - cacheHeapAlloc);
_manager->increaseCurrTotalUsedInBytes(_coldCodeAlloc - cacheHeapAlloc);
_coldCodeAlloc = cacheHeapAlloc;
coldCodeAddress = cacheHeapAlloc;
if (isMethodHeaderNeeded)
Expand Down
38 changes: 36 additions & 2 deletions compiler/runtime/OMRCodeCacheManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corp. and others
* Copyright (c) 2000, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -56,7 +56,9 @@ TR::CodeCacheSymbolContainer * OMR::CodeCacheManager::_symbolContainer = NULL;
OMR::CodeCacheManager::CodeCacheManager(TR::RawAllocator rawAllocator) :
_rawAllocator(rawAllocator),
_initialized(false),
_codeCacheFull(false)
_codeCacheFull(false),
_currTotalUsedInBytes(0),
_maxUsedInBytes(0)
{
}

Expand All @@ -78,6 +80,10 @@ OMR::CodeCacheManager::RepositoryMonitorCriticalSection::RepositoryMonitorCritic
{
}

OMR::CodeCacheManager::UsageMonitorCriticalSection::UsageMonitorCriticalSection(TR::CodeCacheManager *mgr)
: CriticalSection(mgr->_usageMonitor)
{
}

TR::CodeCache *
OMR::CodeCacheManager::initialize(
Expand Down Expand Up @@ -141,6 +147,9 @@ OMR::CodeCacheManager::initialize(
if (_codeCacheList._mutex == NULL)
return NULL;

if (!(_usageMonitor = TR::Monitor::create("CodeCacheUsageMonitor")))
return NULL;

#if defined(TR_HOST_POWER)
#define REACHEABLE_RANGE_KB (32*1024)
#else
Expand Down Expand Up @@ -958,6 +967,31 @@ OMR::CodeCacheManager::chooseCacheStartAddress(size_t repositorySize)
}


void
OMR::CodeCacheManager::increaseCurrTotalUsedInBytes(size_t size)
{
self()->decreaseFreeSpaceInCodeCacheRepository(size);

{
UsageMonitorCriticalSection updateCodeCacheUsage(self());
_currTotalUsedInBytes += size;
_maxUsedInBytes = (_maxUsedInBytes < _currTotalUsedInBytes) ? _currTotalUsedInBytes : _maxUsedInBytes;
}
}


void
OMR::CodeCacheManager::decreaseCurrTotalUsedInBytes(size_t size)
{
self()->increaseFreeSpaceInCodeCacheRepository(size);

{
UsageMonitorCriticalSection updateCodeCacheUsage(self());
_currTotalUsedInBytes = (size < _currTotalUsedInBytes) ? (_currTotalUsedInBytes - size) : 0;
}
}


void
OMR::CodeCacheManager::increaseFreeSpaceInCodeCacheRepository(size_t size)
{
Expand Down
16 changes: 15 additions & 1 deletion compiler/runtime/OMRCodeCacheManager.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corp. and others
* Copyright (c) 2000, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -128,6 +128,12 @@ class OMR_EXTENSIBLE CodeCacheManager
RepositoryMonitorCriticalSection(TR::CodeCacheManager *mgr);
};

class UsageMonitorCriticalSection : public CriticalSection
{
public:
UsageMonitorCriticalSection(TR::CodeCacheManager *mgr);
};

TR::CodeCacheConfig & codeCacheConfig() { return _config; }

/**
Expand Down Expand Up @@ -274,6 +280,11 @@ class OMR_EXTENSIBLE CodeCacheManager
*/
void freeCodeCacheSegment(TR::CodeCacheMemorySegment * memSegment) {}

void increaseCurrTotalUsedInBytes(size_t size);
void decreaseCurrTotalUsedInBytes(size_t size);
size_t getCurrTotalUsedInBytes() const { return _currTotalUsedInBytes; }
size_t getMaxUsedInBytes() const { return _maxUsedInBytes; }

protected:

TR::RawAllocator _rawAllocator;
Expand All @@ -291,6 +302,9 @@ class OMR_EXTENSIBLE CodeCacheManager
bool _lowCodeCacheSpaceThresholdReached; /*!< true if close to exhausting available code cache */
bool _codeCacheFull;

TR::Monitor *_usageMonitor;
size_t _currTotalUsedInBytes;
size_t _maxUsedInBytes;
#if (HOST_OS == OMR_LINUX)
public:
/**
Expand Down

0 comments on commit dbb88d1

Please sign in to comment.