Path: blob/master/runtime/compiler/control/JITServerCompilationThread.hpp
6000 views
/*******************************************************************************1* Copyright (c) 2018, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122#ifndef JITSERVER_COMPILATION_THREAD_H23#define JITSERVER_COMPILATION_THREAD_H2425#include "control/CompilationThread.hpp"26#include "env/j9methodServer.hpp"27#include "runtime/JITClientSession.hpp"2829class TR_IPBytecodeHashTableEntry;3031using IPTableHeapEntry = UnorderedMap<uint32_t, TR_IPBytecodeHashTableEntry*>;32using IPTableHeap_t = UnorderedMap<J9Method *, IPTableHeapEntry *>;33using ResolvedMirrorMethodsPersistIP_t = Vector<TR_ResolvedJ9Method *>;34using ClassOfStatic_t = UnorderedMap<std::pair<TR_OpaqueClassBlock *, int32_t>, TR_OpaqueClassBlock *>;35using FieldOrStaticAttrTable_t = UnorderedMap<std::pair<TR_OpaqueClassBlock *, int32_t>, TR_J9MethodFieldAttributes>;3637void outOfProcessCompilationEnd(TR_MethodToBeCompiled *entry, TR::Compilation *comp);3839namespace TR40{41// Objects of this type are instantiated at JITServer42class CompilationInfoPerThreadRemote : public TR::CompilationInfoPerThread43{44public:45friend class TR::CompilationInfo;46CompilationInfoPerThreadRemote(TR::CompilationInfo &compInfo, J9JITConfig *jitConfig, int32_t id, bool isDiagnosticThread);4748virtual void processEntry(TR_MethodToBeCompiled &entry, J9::J9SegmentProvider &scratchSegmentProvider) override;49TR_PersistentMethodInfo *getRecompilationMethodInfo() const { return _recompilationMethodInfo; }5051uint32_t getSeqNo() const { return _seqNo; }; // For ordering requests at the server52void setSeqNo(uint32_t seqNo) { _seqNo = seqNo; }53uint32_t getExpectedSeqNo() const { return _expectedSeqNo; }54void setExpectedSeqNo(uint32_t seqNo) { _expectedSeqNo = seqNo; }5556void notifyAndDetachWaitingRequests(ClientSessionData *clientSession);57void waitForMyTurn(ClientSessionData *clientSession, TR_MethodToBeCompiled &entry); // Return false if timeout58bool getWaitToBeNotified() const { return _waitToBeNotified; }59void setWaitToBeNotified(bool b) { _waitToBeNotified = b; }60static int32_t getNumClearedCaches() { return _numClearedCaches; }61void incNumClearedCaches() { _numClearedCaches++; }6263void copyClientOptions(const std::string &clientOptStr, TR_PersistentMemory *persistentMemory)64{65size_t clientOptSize = clientOptStr.size();66_clientOptionsSize = clientOptSize;67_clientOptions = new (persistentMemory->_persistentAllocator.get()) char[clientOptSize];68memcpy(_clientOptions, clientOptStr.data(), clientOptSize);69}7071void deleteClientOptions(TR_PersistentMemory *persistentMemory)72{73if (_clientOptions)74{75persistentMemory->freePersistentMemory(_clientOptions);76_clientOptions = NULL;77_clientOptionsSize = 0;78}79}8081char *getClientOptions() { return _clientOptions; }82size_t getClientOptionsSize() { return _clientOptionsSize; }8384bool cacheIProfilerInfo(TR_OpaqueMethodBlock *method, uint32_t byteCodeIndex, TR_IPBytecodeHashTableEntry *entry);85TR_IPBytecodeHashTableEntry *getCachedIProfilerInfo(TR_OpaqueMethodBlock *method, uint32_t byteCodeIndex, bool *methodInfoPresent);8687void cacheResolvedMethod(TR_ResolvedMethodKey key, TR_OpaqueMethodBlock *method, uint32_t vTableSlot, const TR_ResolvedJ9JITServerMethodInfo &methodInfo, int32_t ttlForUnresolved = 2);88bool getCachedResolvedMethod(TR_ResolvedMethodKey key, TR_ResolvedJ9JITServerMethod *owningMethod, TR_ResolvedMethod **resolvedMethod, bool *unresolvedInCP = NULL);89TR_ResolvedMethodKey getResolvedMethodKey(TR_ResolvedMethodType type, TR_OpaqueClassBlock *ramClass, int32_t cpIndex, TR_OpaqueClassBlock *classObject = NULL);9091void cacheResolvedMirrorMethodsPersistIPInfo(TR_ResolvedJ9Method *resolvedMethod);92ResolvedMirrorMethodsPersistIP_t *getCachedResolvedMirrorMethodsPersistIPInfo() const { return _resolvedMirrorMethodsPersistIPInfo; }9394void cacheNullClassOfStatic(TR_OpaqueClassBlock *ramClass, int32_t cpIndex);95bool getCachedNullClassOfStatic(TR_OpaqueClassBlock *ramClass, int32_t cpIndex);9697void cacheFieldOrStaticAttributes(TR_OpaqueClassBlock *ramClass, int32_t cpIndex, const TR_J9MethodFieldAttributes &attrs, bool isStatic);98bool getCachedFieldOrStaticAttributes(TR_OpaqueClassBlock *ramClass, int32_t cpIndex, TR_J9MethodFieldAttributes &attrs, bool isStatic);99100void cacheIsUnresolvedStr(TR_OpaqueClassBlock *ramClass, int32_t cpIndex, const TR_IsUnresolvedString &stringAttrs);101bool getCachedIsUnresolvedStr(TR_OpaqueClassBlock *ramClass, int32_t cpIndex, TR_IsUnresolvedString &stringAttrs);102103void clearPerCompilationCaches();104void deleteClientSessionData(uint64_t clientId, TR::CompilationInfo* compInfo, J9VMThread* compThread);105virtual void freeAllResources() override;106107void incrementClassUnloadReadMutexDepth() { _classUnloadReadMutexDepth++; }108void decrementClassUnloadReadMutexDepth() { _classUnloadReadMutexDepth--; }109int32_t getClassUnloadReadMutexDepth() { return _classUnloadReadMutexDepth; }110111bool isAOTCacheStore() const { return _aotCacheStore; }112uint32_t getMethodIndex() const { return _methodIndex; }113const AOTCacheClassChainRecord *getDefiningClassChainRecord() { return _definingClassChainRecord; }114115private:116/* Template method for allocating a cache of type T on the heap.117* Cache pointer must be NULL.118*/119template <typename T>120bool initializePerCompilationCache(T* &cache)121{122// Initialize map123TR_ASSERT(!cache, "Cache already initialized");124TR_Memory *trMemory = getCompilation()->trMemory();125cache = new (trMemory->trHeapMemory()) T(typename T::allocator_type(trMemory->heapMemoryRegion()));126return cache != NULL;127}128129/* Template method for storing key-value pairs (of types K and V respectively)130* to a heap-allocated unordered map.131* If a map is NULL, will allocate it.132*/133template <typename K, typename V>134void cacheToPerCompilationMap(UnorderedMap<K, V>* &map, const K &key, const V &value)135{136if (!map)137initializePerCompilationCache(map);138map->insert({ key, value });139}140141/* Template method for retrieving values from heap-allocated unordered map.142* If the map is NULL or value is not found, returns false.143* Otherwise, sets value to retrieved value and returns true144*/145template <typename K, typename V>146bool getCachedValueFromPerCompilationMap(UnorderedMap<K, V>* &map, const K &key, V &value)147{148if (!map)149return false;150auto it = map->find(key);151if (it != map->end())152{153// Found entry at a given key154value = it->second;155return true;156}157return false;158}159160/* Template method for clearing a heap-allocated cache.161* Simply sets pointer to cache to NULL.162*/163template <typename T>164void clearPerCompilationCache(T* &cache)165{166// Since cache was heap-allocated,167// memory will be released automatically at the end of the compilation168cache = NULL;169}170171bool serveCachedAOTMethod(TR_MethodToBeCompiled &entry, J9Method *method, J9Class *definingClass,172TR_OptimizationPlan *optPlan, ClientSessionData *clientData,173J9::J9SegmentProvider &scratchSegmentProvider);174175TR_PersistentMethodInfo *_recompilationMethodInfo;176uint32_t _seqNo;177uint32_t _expectedSeqNo; // this request is allowed to go if _expectedSeqNo is processed178bool _waitToBeNotified; // accessed with clientSession->_sequencingMonitor in hand179char *_clientOptions;180size_t _clientOptionsSize;181IPTableHeap_t *_methodIPDataPerComp;182TR_ResolvedMethodInfoCache *_resolvedMethodInfoMap;183ResolvedMirrorMethodsPersistIP_t *_resolvedMirrorMethodsPersistIPInfo; // list of mirrors of resolved methods for persisting IProfiler info184ClassOfStatic_t *_classOfStaticMap;185FieldOrStaticAttrTable_t *_fieldAttributesCache;186FieldOrStaticAttrTable_t *_staticAttributesCache;187UnorderedMap<std::pair<TR_OpaqueClassBlock *, int32_t>, TR_IsUnresolvedString> *_isUnresolvedStrCache;188int32_t _classUnloadReadMutexDepth;189bool _aotCacheStore; // True if the result of this compilation will be stored in AOT cache190uint32_t _methodIndex; // Index of the method being compiled in the array of methods of its defining class191const AOTCacheClassChainRecord *_definingClassChainRecord; // Used to store the result of the compilation in AOT cache192193static int32_t _numClearedCaches; // number of instances JITServer was forced to clear its internal per-client caches194195}; // class CompilationInfoPerThreadRemote196} // namespace TR197198#endif // defined(JITSERVER_COMPILATION_THREAD_H)199200201