Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/gc_glue_java/EnvironmentDelegate.hpp
5990 views
1
2
/*******************************************************************************
3
* Copyright (c) 2017, 2021 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* 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-exception
22
*******************************************************************************/
23
24
#ifndef ENVIRONMENTDELEGATE_HPP_
25
#define ENVIRONMENTDELEGATE_HPP_
26
27
#include "objectdescription.h"
28
29
#include "MarkJavaStats.hpp"
30
#include "ScavengerJavaStats.hpp"
31
#include "GCExtensionsBase.hpp"
32
#include "ObjectModel.hpp"
33
34
struct OMR_VMThread;
35
36
typedef struct GCmovedObjectHashCode {
37
uint32_t originalHashCode;
38
bool hasBeenMoved;
39
bool hasBeenHashed;
40
} GCmovedObjectHashCode;
41
42
class MM_EnvironmentBase;
43
class MM_OwnableSynchronizerObjectBuffer;
44
class MM_ReferenceObjectBuffer;
45
class MM_UnfinalizedObjectBuffer;
46
47
class GC_Environment
48
{
49
/* Data members */
50
private:
51
52
protected:
53
54
public:
55
MM_MarkJavaStats _markJavaStats;
56
#if defined(OMR_GC_MODRON_SCAVENGER)
57
MM_ScavengerJavaStats _scavengerJavaStats;
58
#endif /* OMR_GC_MODRON_SCAVENGER */
59
MM_ReferenceObjectBuffer *_referenceObjectBuffer; /**< The thread-specific buffer of recently discovered reference objects */
60
MM_UnfinalizedObjectBuffer *_unfinalizedObjectBuffer; /**< The thread-specific buffer of recently allocated unfinalized objects */
61
MM_OwnableSynchronizerObjectBuffer *_ownableSynchronizerObjectBuffer; /**< The thread-specific buffer of recently allocated ownable synchronizer objects */
62
63
struct GCmovedObjectHashCode movedObjectHashCodeCache; /**< Structure to aid on object movement and hashing */
64
65
/* Function members */
66
private:
67
protected:
68
public:
69
GC_Environment()
70
:_referenceObjectBuffer(NULL)
71
,_unfinalizedObjectBuffer(NULL)
72
,_ownableSynchronizerObjectBuffer(NULL)
73
{}
74
};
75
76
class MM_EnvironmentDelegate
77
{
78
/* Data members */
79
private:
80
MM_EnvironmentBase *_env;
81
MM_GCExtensionsBase *_extensions;
82
J9VMThread *_vmThread;
83
GC_Environment _gcEnv;
84
protected:
85
public:
86
87
/* Function members */
88
private:
89
protected:
90
public:
91
static OMR_VMThread *attachVMThread(OMR_VM *omrVM, const char *threadName, uintptr_t reason);
92
static void detachVMThread(OMR_VM *omrVM, OMR_VMThread *omrVMThread, uintptr_t reason);
93
94
/**
95
* Initialize the delegate's internal structures and values.
96
* @return true if initialization completed, false otherwise
97
*/
98
bool initialize(MM_EnvironmentBase *env);
99
100
/**
101
* Free any internal structures associated to the receiver.
102
*/
103
void tearDown();
104
105
GC_Environment * getGCEnvironment() { return &_gcEnv; }
106
107
void flushNonAllocationCaches();
108
109
void setGCMainThread(bool isMainThread);
110
111
/**
112
* This will be called for every allocated object. Note this is not necessarily done when the object is allocated. You are however
113
* guaranteed by the start of the next gc, you will be notified for all objects allocated since the last gc.
114
* hooktool is actually functionality better for this but is probably too heavy-weight for what we want for performant code.
115
*/
116
bool objectAllocationNotify(omrobjectptr_t omrObject) { return true; }
117
118
/**
119
* Acquire shared VM access. Threads must acquire VM access before accessing any OMR internal
120
* structures such as the heap. Requests for VM access will be blocked if any other thread is
121
* requesting or has obtained exclusive VM access until exclusive VM access is released.
122
*
123
* This implementation is not preemptive. Threads that have obtained shared VM access must
124
* check frequently whether any other thread is requesting exclusive VM access and release
125
* shared VM access as quickly as possible in that event.
126
*/
127
void acquireVMAccess();
128
129
/**
130
* Release shared VM access.
131
*/
132
void releaseVMAccess();
133
134
/**
135
* Returns true if a mutator threads entered native code without releasing VM access
136
*/
137
bool inNative();
138
139
/**
140
* Check whether another thread is requesting exclusive VM access. This method must be
141
* called frequently by all threads that are holding shared VM access if the VM access framework
142
* is not preemptive. If this method returns true, the calling thread should release shared
143
* VM access as quickly as possible and reacquire it if necessary.
144
*
145
* @return true if another thread is waiting to acquire exclusive VM access
146
*/
147
bool isExclusiveAccessRequestWaiting();
148
149
/**
150
* Acquire exclusive VM access. This method should only be called by the OMR runtime to
151
* perform stop-the-world operations such as garbage collection. Calling thread will be
152
* blocked until all other threads holding shared VM access have release VM access.
153
*/
154
void acquireExclusiveVMAccess();
155
156
/**
157
* Release exclusive VM access. If no other thread is waiting for exclusive VM access
158
* this method will notify all threads waiting for shared VM access to continue and
159
* acquire shared VM access.
160
*/
161
void releaseExclusiveVMAccess();
162
163
uintptr_t relinquishExclusiveVMAccess();
164
165
void assumeExclusiveVMAccess(uintptr_t exclusiveCount);
166
167
void releaseCriticalHeapAccess(uintptr_t *data);
168
169
void reacquireCriticalHeapAccess(uintptr_t data);
170
171
void forceOutOfLineVMAccess();
172
173
/**
174
* This method is responsible for remembering object information before object is moved. Differently than
175
* evacuation, we're sliding the object; therefore, we need to remember object's original information
176
* before object moves and could potentially grow
177
*
178
* @param[in] objectPtr points to the object that is about to be moved
179
* @see postObjectMoveForCompact(omrobjectptr_t)
180
*/
181
MMINLINE void
182
preObjectMoveForCompact(omrobjectptr_t objectPtr)
183
{
184
GC_ObjectModel *objectModel = &_extensions->objectModel;
185
bool hashed = objectModel->hasBeenHashed(objectPtr);
186
bool moved = objectModel->hasBeenMoved(objectPtr);
187
188
_gcEnv.movedObjectHashCodeCache.hasBeenHashed = hashed;
189
_gcEnv.movedObjectHashCodeCache.hasBeenMoved = moved;
190
191
if (hashed && !moved) {
192
/* calculate this BEFORE we (potentially) destroy the object */
193
_gcEnv.movedObjectHashCodeCache.originalHashCode = computeObjectAddressToHash((J9JavaVM *)_extensions->getOmrVM()->_language_vm, objectPtr);
194
}
195
}
196
197
/**
198
* This method may be called during heap compaction, after the object has been moved to a new location.
199
* The implementation may apply any information extracted and cached in the calling thread at this point.
200
* It also updates indexable dataAddr field through fixupDataAddr.
201
*
202
* @param[in] destinationObjectPtr points to the object that has just been moved (new location)
203
* @param[in] sourceObjectPtr points to the old object that has just been moved (old location)
204
* @see preObjectMoveForCompact(omrobjectptr_t)
205
*/
206
MMINLINE void
207
postObjectMoveForCompact(omrobjectptr_t destinationObjectPtr, omrobjectptr_t sourceObjectPtr)
208
{
209
GC_ObjectModel *objectModel = &_extensions->objectModel;
210
if (_gcEnv.movedObjectHashCodeCache.hasBeenHashed && !_gcEnv.movedObjectHashCodeCache.hasBeenMoved) {
211
*(uint32_t*)((uintptr_t)destinationObjectPtr + objectModel->getHashcodeOffset(destinationObjectPtr)) = _gcEnv.movedObjectHashCodeCache.originalHashCode;
212
objectModel->setObjectHasBeenMoved(destinationObjectPtr);
213
}
214
215
if (_extensions->objectModel.isIndexable(destinationObjectPtr)) {
216
/* Updates internal field of indexable objects. Every indexable object have an extra field
217
* that can be used to store any extra information about the indexable object. One use case is
218
* OpenJ9 where we use this field to point to array data. In this case it will always point to
219
* the address right after the header, in case of contiguous data it will point to the data
220
* itself, and in case of discontiguous arraylet it will point to the first arrayiod. How to
221
* updated dataAddr is up to the target language that must override fixupDataAddr */
222
_extensions->indexableObjectModel.fixupDataAddr(destinationObjectPtr);
223
224
if (_extensions->isVLHGC()) {
225
_extensions->indexableObjectModel.fixupInternalLeafPointersAfterCopy((J9IndexableObject *)destinationObjectPtr, (J9IndexableObject *)sourceObjectPtr);
226
}
227
}
228
}
229
230
#if defined (OMR_GC_THREAD_LOCAL_HEAP)
231
/**
232
* Disable inline TLH allocates by hiding the real heap top address from
233
* JIT/Interpreter in realHeapTop and setting HeapTop == heapALloc so TLH
234
* looks full.
235
*
236
*/
237
void disableInlineTLHAllocate();
238
239
/**
240
* Re-enable inline TLH allocate by restoring heapTop from realHeapTop
241
*/
242
void enableInlineTLHAllocate();
243
244
/**
245
* Determine if inline TLH allocate is enabled; its enabled if realheapTop is NULL.
246
* @return TRUE if inline TLH allocates currently enabled for this thread; FALSE otherwise
247
*/
248
bool isInlineTLHAllocateEnabled();
249
250
/**
251
* Set TLH Sampling Top by hiding the real heap top address from
252
* JIT/Interpreter in realHeapTop and setting HeapTop = (HeapAlloc + size) if size < (HeapTop - HeapAlloc)
253
* so out of line allocate would happen at TLH Sampling Top.
254
* If size >= (HeapTop - HeapAlloc) resetTLHSamplingTop()
255
*
256
* @param size the number of bytes to next sampling point
257
*/
258
void setTLHSamplingTop(uintptr_t size);
259
260
/**
261
* Restore heapTop from realHeapTop if realHeapTop != NULL
262
*/
263
void resetTLHSamplingTop();
264
265
/**
266
* Retrieve allocation size inside TLH Cache.
267
* @return (heapAlloc - heapBase)
268
*/
269
uintptr_t getAllocatedSizeInsideTLH();
270
271
#endif /* OMR_GC_THREAD_LOCAL_HEAP */
272
273
MM_EnvironmentDelegate()
274
275
276
: _env(NULL)
277
, _extensions(NULL)
278
, _vmThread(NULL)
279
{ }
280
};
281
282
#endif /* ENVIRONMENTDELEGATE_HPP_ */
283
284