Skip to content

Commit

Permalink
Merge pull request #17425 from SajinaKandy/codeCacheLowMem1
Browse files Browse the repository at this point in the history
Handle code cache alloction for low memory
  • Loading branch information
mpirvu committed May 26, 2023
2 parents 12286f5 + f7b6ca2 commit 1b94cba
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 29 deletions.
102 changes: 75 additions & 27 deletions runtime/compiler/control/J9Options.cpp
Expand Up @@ -58,7 +58,8 @@
#endif

#define SET_OPTION_BIT(x) TR::Options::setBit, offsetof(OMR::Options,_options[(x)&TR_OWM]), ((x)&~TR_OWM)

//Default code cache total max memory percentage set to 25% of physical RAM for low memory systems
#define CODECACHE_DEFAULT_MAXRAMPERCENTAGE 25
// For use with TPROF only, disable JVMPI hooks even if -Xrun is specified.
// The only hook that is required is J9HOOK_COMPILED_METHOD_LOAD.
//
Expand Down Expand Up @@ -264,6 +265,7 @@ int32_t J9::Options::_seriousCompFailureThreshold = 10; // above this threshold

bool J9::Options::_useCPUsToDetermineMaxNumberOfCompThreadsToActivate = false;
int32_t J9::Options::_numCodeCachesToCreateAtStartup = 0; // 0 means no change from default which is 1
bool J9::Options::_overrideCodecachetotal = false;

int32_t J9::Options::_dataCacheQuantumSize = 64;
int32_t J9::Options::_dataCacheMinQuanta = 2;
Expand Down Expand Up @@ -367,7 +369,8 @@ char * J9::Options::_externalOptionStrings[J9::ExternalOptions::TR_NumExternalOp
"-XX:-JITServerAOTCachePersistence", // = 64
"-XX:JITServerAOTCacheDir=", // = 65
"-XX:JITServerAOTCacheName=", // = 66
// TR_NumExternalOptions = 67
"-XX:codecachetotalMaxRAMPercentage=", // = 67
// TR_NumExternalOptions = 68
};

//************************************************************************
Expand Down Expand Up @@ -1432,6 +1435,8 @@ J9::Options::JITServerParseLocalSyncCompiles(J9VMInitArgs *vmArgsArray, J9JavaVM
}
#endif /* defined(J9VM_OPT_JITSERVER) */



void J9::Options::preProcessMmf(J9JavaVM *vm, J9JITConfig *jitConfig)
{
J9MemoryManagerFunctions * mmf = vm->memoryManagerFunctions;
Expand Down Expand Up @@ -1595,6 +1600,31 @@ void J9::Options::preProcessJniAccelerator(J9JavaVM *vm)
}
}

double getCodeCacheMaxPercentageOfAvailableMemory(J9JavaVM *vm)
{
PORT_ACCESS_FROM_JAVAVM(vm);
OMRPORT_ACCESS_FROM_J9PORT(PORTLIB);

double codeCacheTotalPercentage = CODECACHE_DEFAULT_MAXRAMPERCENTAGE;
char *xxccPercentOption = J9::Options::_externalOptionStrings[J9::ExternalOptions::XXcodecachetotalMaxRAMPercentage];
int32_t XXcodeCacheTotalPercentArg = FIND_ARG_IN_VMARGS(STARTSWITH_MATCH, xxccPercentOption, 0);
if (XXcodeCacheTotalPercentArg >= 0)
{
IDATA returnCode = GET_DOUBLE_VALUE(XXcodeCacheTotalPercentArg, xxccPercentOption, codeCacheTotalPercentage);
if (OPTION_OK == returnCode)
{
if (!(codeCacheTotalPercentage >= 1.0 && codeCacheTotalPercentage <= 100.0))
{
j9nls_printf(PORTLIB, J9NLS_WARNING, J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE, xxccPercentOption, codeCacheTotalPercentage, CODECACHE_DEFAULT_MAXRAMPERCENTAGE);
codeCacheTotalPercentage = CODECACHE_DEFAULT_MAXRAMPERCENTAGE;
}
}
else
j9nls_printf(PORTLIB, J9NLS_WARNING, J9NLS_JIT_OPTIONS_INCORRECT_MEMORY_SIZE, xxccPercentOption);
}
return codeCacheTotalPercentage;
}

void J9::Options::preProcessCodeCacheIncreaseTotalSize(J9JavaVM *vm, J9JITConfig *jitConfig)
{
PORT_ACCESS_FROM_JAVAVM(vm);
Expand All @@ -1605,6 +1635,24 @@ void J9::Options::preProcessCodeCacheIncreaseTotalSize(J9JavaVM *vm, J9JITConfig
if (!codecachetotalAlreadyParsed) // avoid processing twice for AOT and JIT and produce duplicate messages
{
codecachetotalAlreadyParsed = true;

UDATA ccTotalSize = jitConfig->codeCacheTotalKB;
#if !defined(J9ZTPF)
// The z/TPF OS reserves code cache memory differently
bool incomplete;
uint64_t freePhysicalMemoryB = getCompilationInfo(jitConfig)->computeAndCacheFreePhysicalMemory(incomplete);
if (freePhysicalMemoryB != OMRPORT_MEMINFO_NOT_AVAILABLE && !incomplete)
{
// If the available memory is less than the default code cache total value
// then use only the user specified percentage(default 25%) of the free memory as code cache total
uint64_t proposedCodeCacheTotalKB = ((uint64_t)(((double)freePhysicalMemoryB / 100.0) * getCodeCacheMaxPercentageOfAvailableMemory(vm))) >> 10;
if (proposedCodeCacheTotalKB < jitConfig->codeCacheTotalKB)
{
ccTotalSize = static_cast<UDATA>(proposedCodeCacheTotalKB);
_overrideCodecachetotal = true;
}
}
#endif
char *xccOption = J9::Options::_externalOptionStrings[J9::ExternalOptions::Xcodecachetotal];
char *xxccOption = J9::Options::_externalOptionStrings[J9::ExternalOptions::XXcodecachetotal];
int32_t codeCacheTotalArgIndex = FIND_ARG_IN_VMARGS(EXACT_MEMORY_MATCH, xccOption, 0);
Expand All @@ -1624,42 +1672,42 @@ void J9::Options::preProcessCodeCacheIncreaseTotalSize(J9JavaVM *vm, J9JITConfig
argIndex = codeCacheTotalArgIndex;
ccTotalOption = xccOption;
}
UDATA ccTotalSize;
IDATA returnCode = GET_MEMORY_VALUE(argIndex, ccTotalOption, ccTotalSize);
if (OPTION_OK == returnCode)
{
ccTotalSize >>= 10; // convert to KB

// Impose a minimum value of 2 MB
if (ccTotalSize < 2048)
ccTotalSize = 2048;

// Restriction: total size must be a multiple of the size of one code cache
UDATA fragmentSize = ccTotalSize % jitConfig->codeCacheKB;
if (fragmentSize > 0) // TODO: do we want a message here?
ccTotalSize += jitConfig->codeCacheKB - fragmentSize; // round-up

// Proportionally increase the data cache as well
// Use 'double' to avoid truncation/overflow
UDATA dcTotalSize = (double)ccTotalSize / (double)(jitConfig->codeCacheTotalKB) *
(double)(jitConfig->dataCacheTotalKB);

// Round up to a multiple of the data cache size
fragmentSize = dcTotalSize % jitConfig->dataCacheKB;
if (fragmentSize > 0)
dcTotalSize += jitConfig->dataCacheKB - fragmentSize;
// Now write the values in jitConfig
jitConfig->codeCacheTotalKB = ccTotalSize;
// Make sure that the new value for dataCacheTotal doesn't shrink the default
if (dcTotalSize > jitConfig->dataCacheTotalKB)
jitConfig->dataCacheTotalKB = dcTotalSize;
_overrideCodecachetotal = false; // User specified value takes precedence over defaults.
}
else // Error with the option
{
// TODO: do we want a message here?
j9nls_printf(PORTLIB, J9NLS_WARNING, J9NLS_JIT_OPTIONS_INCORRECT_MEMORY_SIZE, ccTotalOption);
}
}

// Impose a minimum value of 2 MB
if (ccTotalSize < 2048)
ccTotalSize = 2048;

// Restriction: total size must be a multiple of the size of one code cache
UDATA fragmentSize = ccTotalSize % jitConfig->codeCacheKB;
if (fragmentSize > 0) // TODO: do we want a message here?
ccTotalSize -= fragmentSize; // round-down

// Proportionally increase the data cache as well
// Use 'double' to avoid truncation/overflow
UDATA dcTotalSize = (double)ccTotalSize / (double)(jitConfig->codeCacheTotalKB) *
(double)(jitConfig->dataCacheTotalKB);

// Round up to a multiple of the data cache size
fragmentSize = dcTotalSize % jitConfig->dataCacheKB;
if (fragmentSize > 0)
dcTotalSize += jitConfig->dataCacheKB - fragmentSize;
// Now write the values in jitConfig
jitConfig->codeCacheTotalKB = ccTotalSize;
// Make sure that the new value for dataCacheTotal doesn't shrink the default
if (dcTotalSize > jitConfig->dataCacheTotalKB)
jitConfig->dataCacheTotalKB = dcTotalSize;
}
}

Expand Down
5 changes: 3 additions & 2 deletions runtime/compiler/control/J9Options.hpp
Expand Up @@ -120,7 +120,8 @@ enum ExternalOptions
XXminusJITServerAOTCachePersistenceOption = 64,
XXJITServerAOTCacheDirOption = 65,
XXJITServerAOTCacheNameOption = 66,
TR_NumExternalOptions = 67
XXcodecachetotalMaxRAMPercentage = 67,
TR_NumExternalOptions = 68
};

class OMR_EXTENSIBLE Options : public OMR::OptionsConnector
Expand Down Expand Up @@ -287,7 +288,7 @@ class OMR_EXTENSIBLE Options : public OMR::OptionsConnector

static int32_t _numCodeCachesToCreateAtStartup;
static int32_t getNumCodeCachesToCreateAtStartup() { return _numCodeCachesToCreateAtStartup; }

static bool _overrideCodecachetotal;
static int32_t _dataCacheQuantumSize;
static int32_t _dataCacheMinQuanta;
static int32_t getDataCacheQuantumSize() { return _dataCacheQuantumSize; }
Expand Down
1 change: 1 addition & 0 deletions runtime/compiler/control/OptionsPostRestore.cpp
Expand Up @@ -168,6 +168,7 @@ J9::OptionsPostRestore::iterateOverExternalOptions()
case J9::ExternalOptions::XXplusJITServerAOTCachePersistenceOption:
case J9::ExternalOptions::XXminusJITServerAOTCachePersistenceOption:
case J9::ExternalOptions::XXJITServerAOTCacheDirOption:
case J9::ExternalOptions::XXcodecachetotalMaxRAMPercentage:
{
// do nothing, consume them to prevent errors
FIND_AND_CONSUME_RESTORE_ARG(OPTIONAL_LIST_MATCH, optString, 0);
Expand Down
10 changes: 10 additions & 0 deletions runtime/compiler/control/rossa.cpp
Expand Up @@ -1374,6 +1374,16 @@ onLoadInternal(

TR::CodeCacheConfig &codeCacheConfig = codeCacheManager->codeCacheConfig();

if (TR::Options::_overrideCodecachetotal && TR::Options::isAnyVerboseOptionSet(TR_VerbosePerformance,TR_VerboseCodeCache))
{
// Code cache total value defaults were overridden due to low memory available
bool incomplete;
uint64_t freePhysicalMemoryB = getCompilationInfo(jitConfig)->computeFreePhysicalMemory(incomplete);
if (freePhysicalMemoryB != OMRPORT_MEMINFO_NOT_AVAILABLE)
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE, "Available freePhysicalMemory=%llu MB, allocating code cache total size=%lu MB", freePhysicalMemoryB >> 20, jitConfig->codeCacheTotalKB >> 10);
else
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE, "Allocating code cache total=%llu MB due to low available memory", jitConfig->codeCacheTotalKB >> 10);
}
// Do not allow code caches smaller than 128KB
if (jitConfig->codeCacheKB < 128)
jitConfig->codeCacheKB = 128;
Expand Down
9 changes: 9 additions & 0 deletions runtime/nls/jitm/j9jit.nls
Expand Up @@ -421,3 +421,12 @@ J9NLS_RELOCATABLE_CODE_CH_TABLE_MISMATCH.system_action=The compiler will not gen
J9NLS_RELOCATABLE_CODE_CH_TABLE_MISMATCH.user_response=Destroy and recreate the AOT code in the shared class cache for the current platform using the -Xshareclasses:reset option.
J9NLS_RELOCATABLE_CODE_CH_TABLE_MISMATCH.link=dita:///diag/appendixes/cmdline/cmdline_x.dita#cmdline_x/xshareclasses-reset
# END NON-TRANSLATABLE

J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE=Value '%1$s%2$f' must be between 1.0 and 100.0; using default percentage '%3$d' instead.
# START NON-TRANSLATABLE
J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE.explanation=The specified option is not within the permitted range.
J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE.system_action=The default percentage value will be used instead.
J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE.user_response=Correct the option.
J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE.sample_input_1=-XX:codecachetotalMaxRAMPercentage=
J9NLS_JIT_OPTIONS_PERCENT_OUT_OF_RANGE.sample_input_2=25
# END NON-TRANSLATABLE

0 comments on commit 1b94cba

Please sign in to comment.