Path: blob/master/runtime/gc_realtime/RealtimeGC.hpp
5986 views
/*******************************************************************************1* Copyright (c) 1991, 2020 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/**23* @file24* @ingroup GC_Metronome25*/2627#if !defined(REALTIMEGC_HPP_)28#define REALTIMEGC_HPP_2930#include "CycleState.hpp"31#include "GCExtensionsBase.hpp"32#include "GlobalCollector.hpp"33#include "HeapRegionList.hpp"34#include "IncrementalOverflow.hpp"35#include "MemorySubSpace.hpp"36#include "MemoryPoolSegregated.hpp"37#include "MetronomeDelegate.hpp"38#include "omrmodroncore.h"39#include "OMRVMInterface.hpp"40#include "Scheduler.hpp"41#include "WorkPacketsRealtime.hpp"4243class MM_ParallelDispatcher;44class MM_EnvironmentBase;45class MM_MemorySubSpaceMetronome;46class MM_RealtimeMarkingScheme;47class MM_SweepSchemeRealtime;48class MM_FreeHeapRegionList;4950/**51* @todo Provide class documentation52* @ingroup GC_Metronome53*/54class MM_RealtimeGC : public MM_GlobalCollector55{56/*57* Data members58*/59protected:60OMR_VM *_vm;61MM_GCExtensionsBase *_extensions;6263private:64intptr_t _currentGCThreadPriority;65bool _previousCycleBelowTrigger;66bool _sweepingArraylets;6768uintptr_t _gcPhase; /**< What gc phase are we currently in? */6970MM_CycleState _cycleState; /**< Embedded cycle state to be used as the main cycle state for GC activity */7172bool _moreTracingRequired; /**< Is used to decide if there needs to be another pass of the tracing loop. */7374protected:75MM_RealtimeMarkingScheme *_markingScheme; /**< The marking scheme used to mark objects. */76MM_SweepSchemeRealtime *_sweepScheme; /**< The sweep scheme used to sweep objects for MemoryPoolSegregated. */7778public:79MM_MemoryPoolSegregated *_memoryPool;80MM_MemorySubSpaceMetronome *_memorySubSpace;81bool _allowGrowth;82MM_OSInterface *_osInterface;83MM_Scheduler *_sched;8485bool _fixHeapForWalk;86float _avgPercentFreeHeapAfterCollect;87MM_WorkPacketsRealtime *_workPackets;8889bool _stopTracing;90MM_MetronomeDelegate _realtimeDelegate;9192/*93* Function members94*/95private:96protected:97virtual void internalPreCollect(MM_EnvironmentBase *env, MM_MemorySubSpace *subSpace, MM_AllocateDescription *allocDescription, U_32 gcCode);98virtual void internalPostCollect(MM_EnvironmentBase *env, MM_MemorySubSpace *subSpace);99virtual bool internalGarbageCollect(MM_EnvironmentBase *env, MM_MemorySubSpace *subSpace, MM_AllocateDescription *allocDescription);100101void reportMarkStart(MM_EnvironmentBase *env);102void reportMarkEnd(MM_EnvironmentBase *env);103void reportSweepStart(MM_EnvironmentBase *env);104void reportSweepEnd(MM_EnvironmentBase *env);105void reportGCStart(MM_EnvironmentBase *env);106void reportGCEnd(MM_EnvironmentBase *env);107108public:109void mainSetupForGC(MM_EnvironmentBase *env);110void mainCleanupAfterGC(MM_EnvironmentBase *env);111void doAuxiliaryGCWork(MM_EnvironmentBase *env); /* Wake up finalizer thread. Discard segments freed by class unloading. */112void incrementalCollect(MM_EnvironmentRealtime *env);113114virtual void *createSweepPoolState(MM_EnvironmentBase *env, MM_MemoryPool *memoryPool)115{116/*117* This function does nothing and is designed to have implementation of abstract in GlobalCollector,118* but must return non-NULL value to pass initialization - so, return "this"119*/120return this;121}122123virtual void deleteSweepPoolState(MM_EnvironmentBase *env, void *sweepPoolState)124{125/*126* This function does nothing and is designed to have implementation of abstract in GlobalCollector127*/128}129130void traceAll(MM_EnvironmentBase *env);131132void clearGCStats();133void mergeGCStats(MM_EnvironmentBase *env);134135void reportSyncGCStart(MM_EnvironmentBase *env, GCReason reason, uintptr_t reasonParameter);136void reportSyncGCEnd(MM_EnvironmentBase *env);137void reportGCCycleStart(MM_EnvironmentBase *env);138void reportGCCycleEnd(MM_EnvironmentBase *env);139void reportGCCycleFinalIncrementEnding(MM_EnvironmentBase *env);140141void flushCachesForGC(MM_EnvironmentBase *env) {142/* we do not want to flush caches for each increment, unless there is a reason to walk the heap143* (for instance, fillFromOverflow requires heap be walkable) */144if (_workPackets->getIncrementalOverflowHandler()->isOverflowThisGCCycle()) {145GC_OMRVMInterface::flushCachesForGC(env);146}147}148149/**150* helper function that determines if non-deterministic sweep is enabled151*/152bool shouldPerformNonDeterministicSweep()153{154return (_extensions->nonDeterministicSweep && !isCollectorSweepingArraylets());155}156157virtual uintptr_t getVMStateID() { return OMRVMSTATE_GC_COLLECTOR_METRONOME; };158159static MM_RealtimeGC *newInstance(MM_EnvironmentBase *env);160virtual void kill(MM_EnvironmentBase *env);161bool initialize(MM_EnvironmentBase *env);162void tearDown(MM_EnvironmentBase *env);163uintptr_t verbose(MM_EnvironmentBase *env);164uintptr_t gcCount() { return _extensions->globalGCStats.gcCount; }165166MMINLINE bool isBarrierEnabled() { return (_gcPhase == GC_PHASE_ROOT || _gcPhase == GC_PHASE_TRACE || _gcPhase == GC_PHASE_CONCURRENT_TRACE); }167168MMINLINE bool isCollectorIdle() { return (_gcPhase == GC_PHASE_IDLE); }169MMINLINE bool isCollectorRootMarking() { return (_gcPhase == GC_PHASE_ROOT); }170MMINLINE bool isCollectorTracing() { return (_gcPhase == GC_PHASE_TRACE); }171MMINLINE bool isCollectorUnloadingClassLoaders() { return (_gcPhase == GC_PHASE_UNLOADING_CLASS_LOADERS); }172MMINLINE bool isCollectorSweeping() { return (_gcPhase == GC_PHASE_SWEEP); }173MMINLINE bool isCollectorConcurrentTracing() { return (_gcPhase == GC_PHASE_CONCURRENT_TRACE); }174MMINLINE bool isCollectorConcurrentSweeping() { return (_gcPhase == GC_PHASE_CONCURRENT_SWEEP); }175MMINLINE bool isCollectorSweepingArraylets() { return _sweepingArraylets; }176MMINLINE bool isFixHeapForWalk() { return _fixHeapForWalk; }177178MMINLINE void setCollectorIdle() { _gcPhase = GC_PHASE_IDLE; _sched->_gcPhaseSet |= GC_PHASE_IDLE; }179MMINLINE void setCollectorRootMarking() { _gcPhase = GC_PHASE_ROOT; _sched->_gcPhaseSet |= GC_PHASE_ROOT; }180MMINLINE void setCollectorTracing() { _gcPhase = GC_PHASE_TRACE; _sched->_gcPhaseSet |= GC_PHASE_TRACE; }181MMINLINE void setCollectorUnloadingClassLoaders() { _gcPhase = GC_PHASE_UNLOADING_CLASS_LOADERS; _sched->_gcPhaseSet |= GC_PHASE_UNLOADING_CLASS_LOADERS; }182MMINLINE void setCollectorSweeping() { _gcPhase = GC_PHASE_SWEEP; _sched->_gcPhaseSet |= GC_PHASE_SWEEP; }183MMINLINE void setCollectorConcurrentTracing() { _gcPhase = GC_PHASE_CONCURRENT_TRACE; _sched->_gcPhaseSet |= GC_PHASE_CONCURRENT_TRACE; }184MMINLINE void setCollectorConcurrentSweeping() { _gcPhase = GC_PHASE_CONCURRENT_SWEEP; _sched->_gcPhaseSet |= GC_PHASE_CONCURRENT_SWEEP; }185MMINLINE void setCollectorSweepingArraylets(bool sweepingArraylets) { _sweepingArraylets = sweepingArraylets; }186MMINLINE void setFixHeapForWalk(bool fixHeapForWalk) { _fixHeapForWalk = fixHeapForWalk; }187MMINLINE bool isPreviousCycleBelowTrigger() { return _previousCycleBelowTrigger; }188MMINLINE void setPreviousCycleBelowTrigger(bool previousCycleBelowTrigger) { _previousCycleBelowTrigger = previousCycleBelowTrigger; }189190virtual void setupForGC(MM_EnvironmentBase *env);191virtual bool collectorStartup(MM_GCExtensionsBase* extensions);192virtual void collectorShutdown(MM_GCExtensionsBase *extensions);193void setMemoryPool(MM_MemoryPoolSegregated *memoryPool) { _memoryPool = memoryPool; }194void setMemorySubSpace(MM_MemorySubSpaceMetronome *memorySubSpace) { _memorySubSpace = memorySubSpace; }195196virtual bool heapAddRange(MM_EnvironmentBase *env, MM_MemorySubSpace *subspace, uintptr_t size, void *lowAddress, void *highAddress);197virtual bool heapRemoveRange(198MM_EnvironmentBase *env, MM_MemorySubSpace *subspace, uintptr_t size, void *lowAddress, void *highAddress,199void *lowValidAddress, void *highValidAddress);200201void workerSetupForGC(MM_EnvironmentBase *env);202void allThreadsAllocateUnmarked(MM_EnvironmentBase *env);203/**204* Helper function that flushes cached per-context full regions to the global region pool.205* Currently called before debug function showPages for calculating region pool stats.206*/207void flushCachedFullRegions(MM_EnvironmentBase *env);208virtual void setGCThreadPriority(OMR_VMThread *vmThread, uintptr_t priority);209/* Create the access barrier */210MM_WorkPacketsRealtime* allocateWorkPackets(MM_EnvironmentBase *env);211/* Create an EventTypeSpaceVersion object for TuningFork tracing */212void completeMarking(MM_EnvironmentRealtime* env);213virtual bool condYield(MM_EnvironmentBase *env, U_64 timeSlackNanoSec);214virtual bool shouldYield(MM_EnvironmentBase *env);215virtual void yield(MM_EnvironmentBase *env);216void enableWriteBarrier(MM_EnvironmentBase* env);217void disableWriteBarrier(MM_EnvironmentBase* env);218void enableDoubleBarrier(MM_EnvironmentBase* env);219void disableDoubleBarrierOnThread(MM_EnvironmentBase* env, OMR_VMThread *vmThread);220void disableDoubleBarrier(MM_EnvironmentBase* env);221222void flushRememberedSet(MM_EnvironmentRealtime *env);223224MM_RealtimeMarkingScheme *getMarkingScheme() { return _markingScheme; }225MM_SweepSchemeRealtime *getSweepScheme() { return _sweepScheme; }226MM_MetronomeDelegate *getRealtimeDelegate() { return &_realtimeDelegate; }227228virtual bool isMarked(void *objectPtr);229230MM_RealtimeGC(MM_EnvironmentBase *env)231: MM_GlobalCollector()232, _vm(env->getOmrVM())233, _extensions(env->getExtensions())234, _currentGCThreadPriority(-1)235, _previousCycleBelowTrigger(true)236, _sweepingArraylets(false)237, _cycleState()238, _moreTracingRequired(false)239, _markingScheme(NULL)240, _sweepScheme(NULL)241, _memoryPool(NULL)242, _memorySubSpace(NULL)243, _osInterface(NULL)244, _sched(NULL)245, _fixHeapForWalk(false)246, _avgPercentFreeHeapAfterCollect(0)247, _workPackets(NULL)248, _stopTracing(false)249, _realtimeDelegate(env)250{251_typeId = __FUNCTION__;252_realtimeDelegate._realtimeGC = this;253}254255/*256* Friends257*/258friend class MM_MemoryPoolSegregated;259friend class MM_Scheduler;260friend class MM_MetronomeDelegate;261};262263#endif /* REALTIMEGC_HPP_ */264265266