Skip to content

Commit dcc00d3

Browse files
committed
Client Request Cached Methods List from JIT Server
Added an option that makes the client request a list of cached methods from the server on bootstrap, and set the count for those methods as scount to improve rampup Signed-off-by: Luke Li <[email protected]>
1 parent 445f858 commit dcc00d3

11 files changed

+331
-29
lines changed

runtime/compiler/control/HookedByTheJit.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
2121
*******************************************************************************/
2222

23+
#include <string>
24+
#include <unordered_set>
25+
2326
#include <algorithm>
2427
#include <limits.h>
2528
#ifdef LINUX
@@ -425,6 +428,42 @@ static void jitHookInitializeSendTarget(J9HookInterface * * hook, UDATA eventNum
425428
}
426429
}
427430

431+
#if defined(J9VM_OPT_JITSERVER)
432+
bool methodCachedAtServer = false;
433+
PersistentUnorderedSet<std::string> *serverAOTMethodSet =
434+
(PersistentUnorderedSet<std::string> *) jitConfig->serverAOTMethodSet;
435+
436+
if (serverAOTMethodSet != NULL)
437+
{
438+
// Construct a signature
439+
J9UTF8 *className;
440+
J9UTF8 *name;
441+
J9UTF8 *signature;
442+
getClassNameSignatureFromMethod(method, className, name, signature);
443+
// SigLen calculation as used in TRJ9VMBase::printTruncatedSignature
444+
int32_t sigLen = J9UTF8_LENGTH(className) + J9UTF8_LENGTH(name) +
445+
J9UTF8_LENGTH(signature) + 2;
446+
447+
char *sigC = new char[sizeof(char) * (sigLen + 1)];
448+
sigLen = sprintf(sigC, "%.*s.%.*s%.*s",
449+
J9UTF8_LENGTH(className), utf8Data(className),
450+
J9UTF8_LENGTH(name), utf8Data(name),
451+
J9UTF8_LENGTH(signature), utf8Data(signature));
452+
453+
// contains
454+
methodCachedAtServer =
455+
(serverAOTMethodSet->find(std::string(sigC)) == serverAOTMethodSet->end());
456+
457+
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
458+
if (methodCachedAtServer)
459+
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer,
460+
"Method %s was cached at the server",
461+
sigC);
462+
463+
delete[] sigC;
464+
}
465+
#endif // J9VM_OPT_JITSERVER
466+
428467
if (TR::Options::getAOTCmdLineOptions()->anOptionSetContainsACountValue())
429468
{
430469
TR::OptionSet * optionSet = findOptionSet(method, true);
@@ -475,6 +514,20 @@ static void jitHookInitializeSendTarget(J9HookInterface * * hook, UDATA eventNum
475514
TR::Options::getHighCodeCacheOccupancyBCount() :
476515
TR::Options::getHighCodeCacheOccupancyCount();
477516
}
517+
#if defined(J9VM_OPT_JITSERVER)
518+
else if (methodCachedAtServer) // Higher priority than SCC
519+
{
520+
int32_t scount = optionsAOT->getInitialSCount();
521+
if ((scount == TR_QUICKSTART_INITIAL_SCOUNT) || (scount == TR_INITIAL_SCOUNT))
522+
{
523+
// If scount is not user specified (coarse way due to info being lost
524+
// from options parsing)
525+
scount= std::min(getCount(romMethod, optionsJIT, optionsAOT),
526+
optionsAOT->getInitialSCount());
527+
}
528+
count = scount;
529+
}
530+
#endif // J9VM_OPT_JITSERVER
478531
else if (sccCounts)
479532
{
480533
// The default FE may not have TR_J9SharedCache object because the FE may have
@@ -4181,6 +4234,9 @@ void JitShutdown(J9JITConfig * jitConfig)
41814234

41824235
TR::CompilationInfo * compInfo = TR::CompilationInfo::get(jitConfig);
41834236

4237+
if (jitConfig->serverAOTMethodSet)
4238+
TR_Memory::jitPersistentFree((void *)jitConfig->serverAOTMethodSet);
4239+
41844240
#if defined(J9VM_OPT_CRIU_SUPPORT)
41854241
if (jitConfig->javaVM->internalVMFunctions->isCRaCorCRIUSupportEnabled(vmThread))
41864242
{

runtime/compiler/control/JITServerCompilationThread.cpp

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
2121
*******************************************************************************/
2222

23+
#include <string.h>
24+
2325
#include "control/JITServerCompilationThread.hpp"
2426

2527
#include "codegen/CodeGenerator.hpp"
@@ -202,9 +204,14 @@ outOfProcessCompilationEnd(TR_MethodToBeCompiled *entry, TR::Compilation *comp)
202204
CachedAOTMethod *freshMethodRecord = NULL;
203205
if (!methodRecord)
204206
{
205-
freshMethodRecord = CachedAOTMethod::create(compInfoPT->getDefiningClassChainRecord(), compInfoPT->getMethodIndex(),
206-
entry->_optimizationPlan->getOptLevel(), clientData->getAOTHeaderRecord(),
207-
comp->getSerializationRecords(), codeCacheHeader, codeSize, dataCacheHeader, dataSize);
207+
freshMethodRecord = CachedAOTMethod::create(compInfoPT->getDefiningClassChainRecord(),
208+
compInfoPT->getMethodIndex(),
209+
entry->_optimizationPlan->getOptLevel(),
210+
clientData->getAOTHeaderRecord(),
211+
comp->getSerializationRecords(),
212+
codeCacheHeader, codeSize,
213+
dataCacheHeader, dataSize,
214+
comp->signature());
208215
methodRecord = freshMethodRecord;
209216
}
210217

@@ -1101,6 +1108,58 @@ TR::CompilationInfoPerThreadRemote::processEntry(TR_MethodToBeCompiled &entry, J
11011108
stream->writeError(compilationLowPhysicalMemory, (uint64_t) computeServerMemoryState(getCompilationInfo()));
11021109
abortCompilation = true;
11031110
}
1111+
catch (const JITServer::StreamAotCacheMapRequest &e)
1112+
{
1113+
const std::string aotCacheName = e.getCacheName();
1114+
1115+
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
1116+
{
1117+
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer,
1118+
"compThreadID=%d handling request for AOT cache %s method list",
1119+
getCompThreadId(), aotCacheName.c_str());
1120+
}
1121+
1122+
auto aotCacheMap = compInfo->getJITServerAOTCacheMap();
1123+
bool pending = false;
1124+
auto aotCache = aotCacheMap->get(aotCacheName, 0, pending);
1125+
auto cachedAOTMethod = aotCache->getCachedMethodHead();
1126+
auto cachedMethodMonitor = aotCache->getCachedMethodMonitor();
1127+
1128+
std::vector<std::string> methodSignaturesV;
1129+
try
1130+
{
1131+
{
1132+
OMR::CriticalSection cs(cachedMethodMonitor);
1133+
1134+
for (;cachedAOTMethod != NULL;
1135+
cachedAOTMethod = cachedAOTMethod->getNextRecord())
1136+
{
1137+
const SerializedAOTMethod &serializedAOTMethod = cachedAOTMethod->data();
1138+
std::string sig = std::string(serializedAOTMethod.signature());
1139+
methodSignaturesV.push_back(sig);
1140+
}
1141+
}
1142+
}
1143+
catch (const std::bad_alloc &e)
1144+
{
1145+
if (TR::Options::isAnyVerboseOptionSet(TR_VerboseJITServer, TR_VerboseCompilationDispatch))
1146+
TR_VerboseLog::writeLineLocked(TR_Vlog_FAILURE,
1147+
"std::bad_alloc: %s",
1148+
e.what());
1149+
}
1150+
1151+
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
1152+
{
1153+
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer,
1154+
"Sending the list of AOT methods size %d",
1155+
methodSignaturesV.size());
1156+
}
1157+
1158+
stream->write(JITServer::MessageType::AOTCacheMap_reply, methodSignaturesV);
1159+
1160+
abortCompilation = true;
1161+
deleteStream = true;
1162+
}
11041163

11051164
// Acquire VM access
11061165
//

runtime/compiler/control/rossa.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <stdlib.h>
2525
#include <string.h>
2626

27+
#include <vector>
28+
#include <string>
29+
2730
#ifdef WINDOWS
2831
// Undefine the winsockapi because winsock2 defines it. Removes warnings.
2932
#if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
@@ -1917,6 +1920,82 @@ onLoadInternal(
19171920
return 0;
19181921
}
19191922

1923+
#if defined(J9VM_OPT_JITSERVER)
1924+
static int32_t J9THREAD_PROC fetchServerCachedAOTMethods(void * entryarg)
1925+
{
1926+
J9JITConfig *jitConfig = (J9JITConfig *) entryarg;
1927+
J9JavaVM *vm = jitConfig->javaVM;
1928+
TR::CompilationInfo * compInfo = TR::CompilationInfo::get(jitConfig);
1929+
TR::PersistentInfo *persistentInfo = compInfo->getPersistentInfo();
1930+
1931+
j9thread_t osThread = (j9thread_t) jitConfig->serverAOTQueryThread;
1932+
J9VMThread *vmThread = NULL;
1933+
1934+
int rc = vm->internalVMFunctions->internalAttachCurrentThread(vm, &vmThread, NULL,
1935+
J9_PRIVATE_FLAGS_DAEMON_THREAD | J9_PRIVATE_FLAGS_NO_OBJECT |
1936+
J9_PRIVATE_FLAGS_SYSTEM_THREAD | J9_PRIVATE_FLAGS_ATTACHED_THREAD,
1937+
osThread);
1938+
1939+
if (rc != JNI_OK)
1940+
{
1941+
return rc;
1942+
}
1943+
1944+
try
1945+
{
1946+
JITServer::ClientStream *client = new (PERSISTENT_NEW) JITServer::ClientStream(persistentInfo);
1947+
client->write(JITServer::MessageType::AOTCacheMap_request,
1948+
persistentInfo->getJITServerAOTCacheName());
1949+
1950+
client->read();
1951+
auto result = client->getRecvData<std::vector<std::string>>();
1952+
1953+
std::vector<std::string> cachedMethods = std::get<0>(result);
1954+
1955+
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
1956+
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "Received %d methods",
1957+
cachedMethods.size());
1958+
1959+
PersistentUnorderedSet<std::string> *serverAOTMethodSet =
1960+
new (PERSISTENT_NEW) PersistentUnorderedSet<std::string>(
1961+
PersistentUnorderedSet<std::string>::allocator_type
1962+
(TR::Compiler->persistentAllocator()));
1963+
1964+
for (const auto &methodSig : cachedMethods)
1965+
{
1966+
serverAOTMethodSet->insert(methodSig);
1967+
}
1968+
1969+
client->~ClientStream();
1970+
TR_Memory::jitPersistentFree(client);
1971+
1972+
jitConfig->serverAOTMethodSet = (void *) serverAOTMethodSet;
1973+
}
1974+
catch (const JITServer::StreamFailure &e)
1975+
{
1976+
if (TR::Options::isAnyVerboseOptionSet(TR_VerboseJITServer, TR_VerboseCompilationDispatch))
1977+
TR_VerboseLog::writeLineLocked(TR_Vlog_FAILURE,
1978+
"JITServer::StreamFailure: %s",
1979+
e.what());
1980+
1981+
JITServerHelpers::postStreamFailure(
1982+
OMRPORT_FROM_J9PORT(vm->portLibrary),
1983+
compInfo, e.retryConnectionImmediately(), true);
1984+
}
1985+
catch (const std::bad_alloc &e)
1986+
{
1987+
if (TR::Options::isAnyVerboseOptionSet(TR_VerboseJITServer, TR_VerboseCompilationDispatch))
1988+
TR_VerboseLog::writeLineLocked(TR_Vlog_FAILURE,
1989+
"std::bad_alloc: %s",
1990+
e.what());
1991+
}
1992+
1993+
vm->internalVMFunctions->DetachCurrentThread((JavaVM *) vm);
1994+
j9thread_exit(NULL);
1995+
1996+
return 0;
1997+
}
1998+
#endif // J9VM_OPT_JITSERVER
19201999

19212000
extern "C" int32_t
19222001
aboutToBootstrap(J9JavaVM * javaVM, J9JITConfig * jitConfig)
@@ -2184,6 +2263,43 @@ aboutToBootstrap(J9JavaVM * javaVM, J9JITConfig * jitConfig)
21842263
UT_MODULE_LOADED(J9_UTINTERFACE_FROM_VM(javaVM));
21852264
Trc_JIT_VMInitStages_Event1(curThread);
21862265
Trc_JIT_portableSharedCache_enabled_or_disabled(curThread, J9_ARE_ANY_BITS_SET(javaVM->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_ENABLE_PORTABLE_SHARED_CACHE) ? 1 : 0);
2266+
2267+
#if defined(J9VM_OPT_JITSERVER)
2268+
jitConfig->serverAOTMethodSet = NULL;
2269+
if (TR::Options::getCmdLineOptions()->getOption(TR_RequestJITServerCachedMethods))
2270+
{
2271+
// Ask the server for its cached methods
2272+
if (compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::CLIENT)
2273+
{
2274+
if (JITServerHelpers::isServerAvailable())
2275+
{
2276+
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
2277+
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer,
2278+
"Creating a thread to ask the server for its cached methods");
2279+
2280+
IDATA result = javaVM->internalVMFunctions->createThreadWithCategory(
2281+
(omrthread_t *) &(jitConfig->serverAOTQueryThread),
2282+
javaVM->defaultOSStackSize,
2283+
J9THREAD_PRIORITY_NORMAL,
2284+
0,
2285+
&fetchServerCachedAOTMethods,
2286+
(void *) jitConfig,
2287+
J9THREAD_CATEGORY_SYSTEM_JIT_THREAD
2288+
);
2289+
2290+
if (result != J9THREAD_SUCCESS)
2291+
{
2292+
jitConfig->serverAOTMethodSet = NULL;
2293+
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
2294+
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer,
2295+
"Query thread not created");
2296+
}
2297+
}
2298+
}
2299+
}
2300+
#endif // J9VM_OPT_JITSERVER
2301+
2302+
21872303
return 0;
21882304
}
21892305

runtime/compiler/net/MessageTypes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ const char *messageNames[] =
257257
"KnownObjectTable_getReferenceField",
258258
"KnownObjectTable_getKnownObjectTableDumpInfo",
259259
"AOTCache_getROMClassBatch",
260+
"AOTCacheMap_request",
261+
"AOTCacheMap_reply"
260262
};
261263

262264
static_assert(sizeof(messageNames) / sizeof(messageNames[0]) == MessageType_MAXTYPE,

runtime/compiler/net/MessageTypes.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ enum MessageType : uint16_t
284284

285285
AOTCache_getROMClassBatch,
286286

287+
AOTCacheMap_request,
288+
AOTCacheMap_reply,
289+
287290
MessageType_MAXTYPE
288291
};
289292
extern const char *messageNames[];

runtime/compiler/net/ServerStream.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ class ServerStream : public CommunicationStream
179179
{
180180
return getArgsRaw<T...>(_cMsg);
181181
}
182+
case MessageType::AOTCacheMap_request:
183+
{
184+
std::string cacheName = std::get<0>(getArgsRaw<std::string>(_cMsg));
185+
throw StreamAotCacheMapRequest(cacheName);
186+
}
182187
default:
183188
{
184189
throw StreamMessageTypeMismatch(MessageType::compilationRequest, _cMsg.type());

runtime/compiler/net/StreamExceptions.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ class StreamClientSessionTerminate: public virtual std::exception
8181
uint64_t _clientId;
8282
};
8383

84+
85+
class StreamAotCacheMapRequest: public virtual std::exception
86+
{
87+
public:
88+
StreamAotCacheMapRequest(std::string cacheName) : _cacheName(cacheName)
89+
{
90+
}
91+
virtual const char* what() const throw()
92+
{
93+
return "Requesting AOT cache content";
94+
}
95+
const std::string& getCacheName() const
96+
{
97+
return _cacheName;
98+
}
99+
private:
100+
const std::string _cacheName;
101+
};
102+
84103
class StreamOOO : public virtual std::exception
85104
{
86105
public:

0 commit comments

Comments
 (0)