Skip to content

Commit

Permalink
disclaim_cold_cache: PR comments 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gita-omr committed May 21, 2024
1 parent 12ca113 commit d39b396
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 24 deletions.
20 changes: 14 additions & 6 deletions runtime/compiler/control/HookedByTheJit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4598,8 +4598,8 @@ void disclaimCodeCaches(uint64_t crtElapsedTime)
int numDisclaimed = TR::CodeCacheManager::instance()->disclaimAllCodeCaches();
size_t rssAfter = getRSS_Kb();
if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF, "t=%u JIT disclaimed %d Code Caches RSS before=%zu KB, RSS after=%zu KB, delta=%zu KB",
(uint32_t)crtElapsedTime, numDisclaimed, rssBefore, rssAfter, rssBefore - rssAfter);
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF, "t=%u JIT disclaimed %d Code Caches RSS before=%zu KB, RSS after=%zu KB, delta=%zu KB = %5.2f%%",
(uint32_t)crtElapsedTime, numDisclaimed, rssBefore, rssAfter, rssBefore - rssAfter, ((long)(rssAfter - rssBefore) * 100.0 / rssBefore));
}

void memoryDisclaimLogic(TR::CompilationInfo *compInfo, uint64_t crtElapsedTime, uint8_t jitState)
Expand Down Expand Up @@ -4637,11 +4637,13 @@ void memoryDisclaimLogic(TR::CompilationInfo *compInfo, uint64_t crtElapsedTime,
}
}

// Use logic similar to Data caches above for now
if (TR::CodeCacheManager::instance()->isDisclaimEnabled())
{
int64_t lateSCCDisclaimTime = compInfo->getPersistentInfo()->getLateSCCDisclaimTime();

// Ensure we don't do it too often
if (crtElapsedTime > lastCodeCacheDisclaimTime + TR::Options::_minTimeBetweenMemoryDisclaims)
if (lateSCCDisclaimTime == 0 &&
crtElapsedTime > lastCodeCacheDisclaimTime + TR::Options::_minTimeBetweenMemoryDisclaims)
{
// Disclaim if at least one code cache has been allocated since the last disclaim
// or if there was a large time interval since the last disclaim
Expand Down Expand Up @@ -5342,17 +5344,23 @@ static void jitStateLogic(J9JITConfig * jitConfig, TR::CompilationInfo * compInf
}
#endif

if (lateDisclaimNeeded)
if (lateDisclaimNeeded &&
TR::CodeCacheManager::instance()->isDisclaimEnabled())
{
CpuUtilization *cpuUtil = compInfo->getCpuUtil();
cpuUtil->updateCpuUtil(jitConfig);
if (cpuUtil->getVmTotalCpuTime() >= persistentInfo->getLateSCCDisclaimTime())

int64_t lateSCCDisclaimTime = persistentInfo->getLateSCCDisclaimTime();

if (lateSCCDisclaimTime != 0 &&
cpuUtil->getVmTotalCpuTime() >= lateSCCDisclaimTime)
{
javaVM->internalVMFunctions->jvmPhaseChange(javaVM, J9VM_PHASE_LATE_SCC_DISCLAIM);
lateDisclaimNeeded = false;
if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
{
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF, "t=%u JIT issuing late SCC disclaim", (uint32_t)crtElapsedTime);
disclaimCodeCaches(crtElapsedTime);
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions runtime/compiler/control/J9Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2905,8 +2905,17 @@ J9::Options::fePostProcessJIT(void * base)
TR::Options::disableMemoryDisclaimIfNeeded(jitConfig);
}

if (TR::Compiler->target.isLinux() &&
self()->getOption(TR_EnableCodeCacheDisclaiming))
const char *ccOption = J9::Options::_externalOptionStrings[J9::ExternalOptions::Xcodecache];
J9JavaVM *vm = javaVM; // needed by FIND_ARG_IN_VMARGS macro
int32_t argIndex = FIND_ARG_IN_VMARGS(EXACT_MEMORY_MATCH, ccOption, 0);

if (argIndex >= 0)
{
if (jitConfig->codeCacheKB < 4*1024*1024)
self()->setOption(TR_EnableCodeCacheDisclaiming, false);
}
else if (TR::Compiler->target.isLinux() &&
self()->getOption(TR_EnableCodeCacheDisclaiming))
{
jitConfig->codeCacheKB *= 2;
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/compiler/control/OptionsPostRestore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ J9::OptionsPostRestore::OptionsPostRestore(J9VMThread *vmThread, J9JITConfig *ji
= J9::Options::_xrsSync
|| options->getOption(TR_NoResumableTrapHandler)
|| options->getOption(TR_DisableTraps);

_enableCodeCacheDisclaimingPreCheckpoint = options->getOption(TR_EnableCodeCacheDisclaiming);
}

void
Expand Down Expand Up @@ -804,6 +806,13 @@ J9::OptionsPostRestore::postProcessInternalCompilerOptions()
}
}


if (!_enableCodeCacheDisclaimingPreCheckpoint &&
TR::Options::getCmdLineOptions()->getOption(TR_EnableCodeCacheDisclaiming))
{
TR::Options::getCmdLineOptions()->setOption(TR_EnableCodeCacheDisclaiming, false);
}

if (!TR::Options::getCmdLineOptions()->getOption(TR_DisableDataCacheDisclaiming) ||
!TR::Options::getCmdLineOptions()->getOption(TR_DisableIProfilerDataDisclaiming) ||
TR::Options::getCmdLineOptions()->getOption(TR_EnableCodeCacheDisclaiming))
Expand Down
1 change: 1 addition & 0 deletions runtime/compiler/control/OptionsPostRestore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class OptionsPostRestore
bool _asyncCompilationPreCheckpoint;
bool _disableTrapsPreCheckpoint;
bool _disableAOTPostRestore;
bool _enableCodeCacheDisclaimingPreCheckpoint;

int32_t _argIndexXjit;
int32_t _argIndexXjitcolon;
Expand Down
3 changes: 2 additions & 1 deletion runtime/compiler/env/J2IThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ TR_MHJ2IThunk::allocate(
else
#endif /* defined(J9VM_OPT_JITSERVER) */
{
result = (TR_MHJ2IThunk*)cg->allocateCodeMemory(totalSize, true, false);
bool disclaim = TR::Options::getCmdLineOptions()->getOption(TR_EnableCodeCacheDisclaiming);
result = (TR_MHJ2IThunk*)cg->allocateCodeMemory(totalSize, !disclaim, false);
}
omrthread_jit_write_protect_disable();
result->_codeSize = codeSize;
Expand Down
29 changes: 18 additions & 11 deletions runtime/compiler/runtime/J9CodeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "j9.h"
#include "j9protos.h"
#include "j9thread.h"
Expand Down Expand Up @@ -147,24 +148,29 @@ J9::CodeCache::initialize(TR::CodeCacheManager *manager,
if (!self()->OMR::CodeCache::initialize(manager, codeCacheSegment, allocatedCodeCacheSizeInBytes))
return false;

self()->setInitialAllocationPointers();

#ifdef LINUX
if (manager->isDisclaimEnabled())
{
uint8_t *middle = _warmCodeAlloc + (_coldCodeAllocBase - _warmCodeAlloc) / 2;
size_t round = DISCLAIM_PAGE_SIZE - 1;

middle = (uint8_t *)(((size_t)(middle + round)) & ~round);

TR_ASSERT_FATAL(_coldCodeAlloc > middle, "A code cache can't be smaller than a page");

size_t coldCacheSize = _coldCodeAlloc - middle;
coldCacheSize = (coldCacheSize + round) & ~round;

if (madvise(middle, coldCacheSize, MADV_NOHUGEPAGE) != 0)
{
const char *error = strerror(errno);
if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
TR_VerboseLog::writeLine(TR_Vlog_INFO, "Failed to set MADV_NOHUGEPAGE for code cache");
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO, "Failed to set MADV_NOHUGEPAGE for code cache: %s: %p %zu", error, middle, coldCacheSize);
}
else if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
{
TR_VerboseLog::writeLine(TR_Vlog_INFO, "In code cache %p small pages start from %p\n", this, middle);
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO, "In code cache %p small pages start from %p\n", this, middle);
}

// If the memory segment is backed by a file, disable read-ahead
Expand All @@ -174,15 +180,12 @@ J9::CodeCache::initialize(TR::CodeCacheManager *manager,
if (madvise(middle, coldCacheSize, MADV_RANDOM) != 0)
{
if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
TR_VerboseLog::writeLine(TR_Vlog_INFO, "Failed to set MADV_RANDOM for cold code cache");
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO, "Failed to set MADV_RANDOM for cold code cache");
}
}
}
#endif // ifdef LINUX


self()->setInitialAllocationPointers();

_manager->reportCodeLoadEvents();

return true;
Expand Down Expand Up @@ -823,14 +826,18 @@ J9::CodeCache::disclaim(TR::CodeCacheManager *manager, bool canDisclaimOnSwap)
size_t pageSize = DISCLAIM_PAGE_SIZE;
size_t round = pageSize - 1;
disclaim_start = (uint8_t *)(((size_t)(disclaim_start + round)) & ~round);
size_t disclaim_size = pageSize * ((_coldCodeAllocBase - disclaim_start)/pageSize);

if (_coldCodeAllocBase <= disclaim_start)
return 0;

size_t disclaim_size = (_coldCodeAllocBase - disclaim_start + round) & ~round;

if (trace)
{
size_t warm_size = _warmCodeAlloc - _segment->segmentBase() + sizeof(this);
size_t cold_size = _coldCodeAllocBase - _coldCodeAlloc;

TR_VerboseLog::writeLine(TR_Vlog_PERF, "Disclaiming cold code cache %p : coldStart=%p coldBase=%p warm_size=%zuB cold_size=%zuB cold_size/(cold_size + warm_size)=%5.2f%%\n",
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF, "Will disclaim cold code cache %p : coldStart=%p coldBase=%p warm_size=%zuB cold_size=%zuB cold_size/(cold_size + warm_size)=%5.2f%%\n",
this, _coldCodeAlloc, _coldCodeAllocBase,
warm_size, cold_size, cold_size * 100.0/(cold_size + warm_size));
}
Expand All @@ -840,13 +847,13 @@ J9::CodeCache::disclaim(TR::CodeCacheManager *manager, bool canDisclaimOnSwap)
if (ret != 0)
{
if (trace)
TR_VerboseLog::writeLine(TR_Vlog_PERF, "WARNING: Failed to use madvise to disclaim memory for code cache");
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF, "WARNING: Failed to use madvise to disclaim memory for code cache");

if (ret == EINVAL)
{
manager->setDisclaimEnabled(false); // Don't try to disclaim again, since support seems to be missing
if (trace)
TR_VerboseLog::writeLine(TR_Vlog_PERF, "WARNING: Disabling data cache disclaiming from now on");
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF, "WARNING: Disabling data cache disclaiming from now on");
}
}
else
Expand Down
9 changes: 5 additions & 4 deletions runtime/compiler/runtime/J9CodeCacheManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,18 @@ J9::CodeCacheManager::allocateCodeCacheSegment(size_t segmentSize,
}

if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
TR_VerboseLog::writeLine(TR_Vlog_INFO, "Allocated new code cache segment %p starting at address %p",
codeCacheSegment,
codeCacheSegment->heapBase);
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO, "Allocated new code cache segment %p starting at address %p %d Kb",
codeCacheSegment,
codeCacheSegment->heapBase,
_jitConfig->codeCacheKB);
}
else
{
// TODO: we should generate a trace point
mcc_printf("TR::CodeCache::allocate : codeCacheSegment is NULL, %p\n",codeCacheSegment);

if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
TR_VerboseLog::writeLine(TR_Vlog_INFO, "Failed to allocate new code cache segment of %d Kb", _jitConfig->codeCacheKB);
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO, "Failed to allocate new code cache segment of %d Kb", _jitConfig->codeCacheKB);

return 0;
}
Expand Down

0 comments on commit d39b396

Please sign in to comment.