-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathRealtimeAccessBarrier.cpp
964 lines (860 loc) · 36.8 KB
/
RealtimeAccessBarrier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* 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
*******************************************************************************/
#include "ArrayletObjectModel.hpp"
#include "Bits.hpp"
#include "EnvironmentRealtime.hpp"
#include "HeapRegionDescriptorRealtime.hpp"
#include "HeapRegionManager.hpp"
#include "JNICriticalRegion.hpp"
#include "RealtimeAccessBarrier.hpp"
#include "RealtimeGC.hpp"
#include "RealtimeMarkingScheme.hpp"
#if defined(J9VM_GC_REALTIME)
/**
* Static method for instantiating the access barrier.
*/
MM_RealtimeAccessBarrier *
MM_RealtimeAccessBarrier::newInstance(MM_EnvironmentBase *env)
{
MM_RealtimeAccessBarrier *barrier;
barrier = (MM_RealtimeAccessBarrier *)env->getForge()->allocate(sizeof(MM_RealtimeAccessBarrier), MM_AllocationCategory::FIXED, OMR_GET_CALLSITE());
if (barrier) {
new(barrier) MM_RealtimeAccessBarrier(env);
if (!barrier->initialize(env)) {
barrier->kill(env);
barrier = NULL;
}
}
return barrier;
}
bool
MM_RealtimeAccessBarrier::initialize(MM_EnvironmentBase *env)
{
if (!MM_ObjectAccessBarrier::initialize(env)) {
return false;
}
_realtimeGC = MM_GCExtensions::getExtensions(env)->realtimeGC;
_markingScheme = _realtimeGC->getMarkingScheme();
return true;
}
void
MM_RealtimeAccessBarrier::kill(MM_EnvironmentBase *env)
{
tearDown(env);
env->getForge()->free(this);
}
void
MM_RealtimeAccessBarrier::tearDown(MM_EnvironmentBase *env)
{
MM_ObjectAccessBarrier::tearDown(env);
}
/**
* Override of referenceGet. This barriered version does two things. (1) When the
* collector is tracing, it makes any gotten object "grey" to ensure that it is eventually
* traced. (2) When the collector is in the unmarkedImpliesCleared phase (after
* to-be-cleared soft and weak references have been identified and logically cleared), the
* get() operation returns NULL instead of the referent if the referent is unmarked.
*
* @param refObject the SoftReference or WeakReference object on which get() is being called.
* This barrier must not be called for PhantomReferences. The parameter must not be NULL.
*/
J9Object *
MM_RealtimeAccessBarrier::referenceGet(J9VMThread *vmThread, J9Object *refObject)
{
UDATA offset = J9VMJAVALANGREFREFERENCE_REFERENT_OFFSET(vmThread);
J9Object *referent = mixedObjectReadObject(vmThread, refObject, offset, false);
/* Do nothing exceptional for NULL or marked referents */
if (referent == NULL) {
goto done;
}
if (_markingScheme->isMarked(referent)) {
goto done;
}
/* Now we know referent isn't NULL and isn't marked */
if (_realtimeGC->getRealtimeDelegate()->_unmarkedImpliesCleared) {
/* In phase indicated by this flag, all unmarked references are logically cleared
* (will be physically cleared by the end of the gc).
*/
return NULL;
}
/* Throughout tracing, we must turn any gotten reference into a root, because the
* thread doing the getting may already have been scanned. However, since we are
* running on a mutator thread and not a gc thread we do this indirectly by putting
* the object in the barrier buffer.
*/
if (_realtimeGC->isBarrierEnabled()) {
MM_EnvironmentBase *env = MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread);
rememberObject(env, referent);
}
done:
/* We must return the external reference */
return referent;
}
void
MM_RealtimeAccessBarrier::referenceReprocess(J9VMThread *vmThread, J9Object *refObject)
{
referenceGet(vmThread, refObject);
}
/**
* Barrier called from within j9jni_deleteGlobalRef to maintain the "double barrier"
* invariant (see comment in preObjectStore). To maintain EXACTLY what we do for ordinary
* reference stores, we should trap globalref creation for unscanned threads (only) and
* globalref deletion for all threads. But, since creating a global ref can
* only open a leak if that reference is subsequently deleted, it is sufficient to trap
* deletions for all threads. Note that we do not attempt to guard against JNI threads
* passing objects by back channels without creating global refs (we could not do so even
* if we wished to). Such JNI code is already treacherously unsafe and would sooner or
* later crash with or without incremental thread scanning.
*
* @param reference the JNI global reference that is about to be deleted. Must not be NULL
* (same requirement as j9jni_deleteGlobalRef).
*/
void
MM_RealtimeAccessBarrier::jniDeleteGlobalReference(J9VMThread *vmThread, J9Object *reference)
{
MM_EnvironmentBase *env = MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread);
if (!_realtimeGC->isBarrierEnabled()) {
return;
}
deleteHeapReference(env, reference);
}
/**
* Take any required action when a string constant has been fetched from the table.
* If there is a yield point between the last marking work and when the string table
* is cleared, then a thread could potentially get a reference to a string constant
* that is about to be cleared. This method prevents that be adding the string to
* the remembered set.
*/
void
MM_RealtimeAccessBarrier::stringConstantEscaped(J9VMThread *vmThread, J9Object *stringConst)
{
MM_EnvironmentBase *env = MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread);
if (_realtimeGC->isBarrierEnabled()) {
rememberObject(env, stringConst);
}
}
/**
* Check that the two string constants should be considered truly "live".
* This is a bit of a hack to enable us to scan the string table incrementally.
* If we are still doing marking work, we treat a call to this method as meaning
* that one of the strings has been fetched from the string table. If we are finished
* marking work, but have yet to clear the string table, we treat any unmarked strings
* as cleared, by returning false from this function.
* NOTE: Because this function is called from the hash equals function, we can't
* actually tell which one of the strings is actually the one in the table already,
* so we have to assume they both are.
* @return true if they can be considered live, false otherwise.
*/
bool
MM_RealtimeAccessBarrier::checkStringConstantsLive(J9JavaVM *javaVM, j9object_t stringOne, j9object_t stringTwo)
{
if (_realtimeGC->isBarrierEnabled()) {
if (_realtimeGC->getRealtimeDelegate()->_unmarkedImpliesStringsCleared) {
/* If this flag is set, we will not scan the remembered set again, so we must
* treat any unmarked string constant as having been cleared.
*/
return (_realtimeGC->getMarkingScheme()->isMarked((J9Object *)stringOne) && _realtimeGC->getMarkingScheme()->isMarked((J9Object *)stringTwo));
} else {
J9VMThread* vmThread = javaVM->internalVMFunctions->currentVMThread(javaVM);
stringConstantEscaped(vmThread, (J9Object *)stringOne);
stringConstantEscaped(vmThread, (J9Object *)stringTwo);
}
}
return true;
}
/**
* Equivalent to checkStringConstantsLive but for a single string constant
*/
bool
MM_RealtimeAccessBarrier::checkStringConstantLive(J9JavaVM *javaVM, j9object_t string)
{
if (_realtimeGC->isBarrierEnabled()) {
if (_realtimeGC->getRealtimeDelegate()->_unmarkedImpliesStringsCleared) {
/* If this flag is set, we will not scan the remembered set again, so we must
* treat any unmarked string constant as having been cleared.
*/
return _realtimeGC->getMarkingScheme()->isMarked((J9Object *)string);
} else {
J9VMThread* vmThread = javaVM->internalVMFunctions->currentVMThread(javaVM);
stringConstantEscaped(vmThread, (J9Object *)string);
}
}
return true;
}
/**
* Take any required action when a heap reference is deleted. This method is called on
* all slots when finalizing a scoped object. It's also called from jniDeleteGlobalReference,
* but only when the collector is tracing (therefore we shouldn't check for isBarrierEnabled
* from this method).
*/
void
MM_RealtimeAccessBarrier::deleteHeapReference(MM_EnvironmentBase *env, J9Object *object)
{
rememberObject(env, object);
}
/**
* DEBUG method. Only called when MM_GCExtensions::debugWriteBarrier >= 1.
*/
void
MM_RealtimeAccessBarrier::validateWriteBarrier(J9VMThread *vmThread, J9Object *dstObject, fj9object_t *dstAddress, J9Object *srcObject)
{
J9JavaVM *javaVM = vmThread->javaVM;
PORT_ACCESS_FROM_JAVAVM(javaVM);
bool const compressed = J9VMTHREAD_COMPRESS_OBJECT_REFERENCES(vmThread);
switch(_extensions->objectModel.getScanType(dstObject)) {
case GC_ObjectModel::SCAN_MIXED_OBJECT_LINKED:
case GC_ObjectModel::SCAN_ATOMIC_MARKABLE_REFERENCE_OBJECT:
case GC_ObjectModel::SCAN_MIXED_OBJECT:
case GC_ObjectModel::SCAN_OWNABLESYNCHRONIZER_OBJECT:
case GC_ObjectModel::SCAN_CONTINUATION_OBJECT:
case GC_ObjectModel::SCAN_CLASS_OBJECT:
case GC_ObjectModel::SCAN_CLASSLOADER_OBJECT:
case GC_ObjectModel::SCAN_REFERENCE_MIXED_OBJECT:
{
intptr_t slotIndex = GC_SlotObject::subtractSlotAddresses(dstAddress, (fj9object_t*)dstObject, compressed);
if (slotIndex < 0) {
j9tty_printf(PORTLIB, "validateWriteBarrier: slotIndex is negative dstAddress %d and dstObject %d\n", dstAddress, dstObject);
}
UDATA dataSizeInSlots = MM_Bits::convertBytesToSlots(_extensions->objectModel.getSizeInBytesWithHeader(dstObject));
if ((UDATA)slotIndex >= dataSizeInSlots) {
j9tty_printf(PORTLIB, "validateWriteBarrier: slotIndex (%d) >= object size in slots (%d)", slotIndex, dataSizeInSlots);
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
j9tty_printf(PORTLIB, "\n");
}
/* Also consider validating that slot is a ptr slot */
break;
}
case GC_ObjectModel::SCAN_POINTER_ARRAY_OBJECT:
{
MM_HeapRegionManager *regionManager = MM_GCExtensions::getExtensions(javaVM)->getHeap()->getHeapRegionManager();
GC_ArrayletObjectModel::ArrayLayout layout = _extensions->indexableObjectModel.getArrayLayout((J9IndexableObject*)dstObject);
switch (layout) {
case GC_ArrayletObjectModel::InlineContiguous: {
UDATA** arrayletPtr = (UDATA**)(((J9IndexableObject*)dstObject) + 1);
UDATA* dataStart = *arrayletPtr;
UDATA* dataEnd = dataStart + _extensions->indexableObjectModel.getSizeInElements((J9IndexableObject*)dstObject);
if ((UDATA*)dstAddress < dataStart || (UDATA*)dstAddress >= dataEnd) {
j9tty_printf(PORTLIB, "validateWriteBarrier: IC: store to %p not in data section of array %p to %p", dstAddress, dataStart, dataEnd);
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
j9tty_printf(PORTLIB, "\n");
}
break;
}
case GC_ArrayletObjectModel::Discontiguous: {
MM_HeapRegionDescriptorRealtime *region = (MM_HeapRegionDescriptorRealtime *)regionManager->tableDescriptorForAddress(dstAddress);
if (!region->isArraylet()) {
j9tty_printf(PORTLIB, "validateWriteBarrier: D: dstAddress (%p) is not on an arraylet region", dstAddress);
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
j9tty_printf(PORTLIB, "\n");
}
else {
UDATA* arrayletParent = region->getArrayletParent(region->whichArraylet((UDATA*)dstAddress, javaVM->arrayletLeafLogSize));
if (arrayletParent != (UDATA*)dstObject) {
j9tty_printf(PORTLIB, "validateWriteBarrier: D: parent of arraylet (%p) is not destObject (%p)", arrayletParent, dstObject);
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
j9tty_printf(PORTLIB, "\n");
}
}
break;
}
case GC_ArrayletObjectModel::Hybrid: {
/* First check to see if it is in the last arraylet which is contiguous with the array spine. */
UDATA numberArraylets = _extensions->indexableObjectModel.numArraylets((J9IndexableObject*)dstObject);
UDATA** arrayletPtr = (UDATA**)(((J9IndexableObject*)dstObject)+1) + numberArraylets - 1;
UDATA* dataStart = *arrayletPtr;
UDATA spineSize = _extensions->indexableObjectModel.getSpineSize((J9IndexableObject*)dstObject);
UDATA* dataEnd = (UDATA*)(((U_8*)dstObject) + spineSize);
if ((UDATA*)dstAddress < dataStart || (UDATA*)dstAddress >= dataEnd) {
/* store was _not_ to last arraylet; attempt to validate that
* it was to one of the other arraylets of this array.
*/
MM_HeapRegionDescriptorRealtime *region = (MM_HeapRegionDescriptorRealtime *)regionManager->tableDescriptorForAddress(dstAddress);
if (!region->isArraylet()) {
j9tty_printf(PORTLIB, "validateWriteBarrier: H: dstAddress (%p) is not on an arraylet region", dstAddress);
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
}
else {
UDATA* arrayletParent = region->getArrayletParent(region->whichArraylet((UDATA*)dstAddress, javaVM->arrayletLeafLogSize));
if (arrayletParent != (UDATA*)dstObject) {
j9tty_printf(PORTLIB, "validateWriteBarrier: H: parent of arraylet (%p) is not destObject (%p)", arrayletParent, dstObject);
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
j9tty_printf(PORTLIB, "\n");
}
}
}
break;
}
default: {
j9tty_printf(PORTLIB, "validateWriteBarrier: unexpected arraylet type %d\n", layout);
assert(0);
}
};
break;
}
case GC_ObjectModel::SCAN_PRIMITIVE_ARRAY_OBJECT:
j9tty_printf(PORTLIB, "validateWriteBarrier: writeBarrier called on array of primitive\n");
j9tty_printf(PORTLIB, "value being overwritten is %d\n", GC_SlotObject::readSlot(dstAddress, compressed));
printClass(javaVM, J9GC_J9OBJECT_CLAZZ_VM(dstObject, javaVM));
j9tty_printf(PORTLIB, "\n");
break;
default:
Assert_MM_unreachable();
}
}
void
MM_RealtimeAccessBarrier::printClass(J9JavaVM *javaVM, J9Class* clazz)
{
J9ROMClass* romClass;
J9UTF8* utf;
PORT_ACCESS_FROM_JAVAVM(javaVM);
/* TODO: In Sov, if the class is char[], the string is printed instead of the class name */
romClass = clazz->romClass;
if(romClass->modifiers & J9AccClassArray) {
J9ArrayClass* arrayClass = (J9ArrayClass*) clazz;
UDATA arity = arrayClass->arity;
utf = J9ROMCLASS_CLASSNAME(arrayClass->leafComponentType->romClass);
j9tty_printf(PORTLIB, "%.*s", (UDATA)J9UTF8_LENGTH(utf), J9UTF8_DATA(utf));
while(arity--) {
j9tty_printf(PORTLIB, "[]");
}
} else {
utf = J9ROMCLASS_CLASSNAME(romClass);
j9tty_printf(PORTLIB, "%.*s", (UDATA)J9UTF8_LENGTH(utf), J9UTF8_DATA(utf));
}
}
/**
* Unmarked, heap reference, about to be deleted (or overwritten), while marking
* is in progress is to be remembered for later marking and scanning.
*/
void
MM_RealtimeAccessBarrier::rememberObject(MM_EnvironmentBase *env, J9Object *object)
{
if (_markingScheme->markObject(MM_EnvironmentRealtime::getEnvironment(env), object, true)) {
rememberObjectImpl(env, object);
}
}
/**
* Read an object from an internal VM slot (J9VMThread, J9JavaVM, named field of J9Class).
* This function is only concerned with moving the actual data. Do not re-implement
* unless the value is stored in a non-native format (e.g. compressed object pointers).
* See readObjectFromInternalVMSlot() for higher-level actions.
* In realtime, we must remember the object being read in case it's being read from an
* unmarked thread and stored on a marked threads' stack. If the unmarked thread terminates
* before being marked, we will miss the object since a stack push doesn't invoke the write
* barrier.
* @param srcAddress the address of the field to be read
* @param isVolatile non-zero if the field is volatile, zero otherwise
*/
mm_j9object_t
MM_RealtimeAccessBarrier::readObjectFromInternalVMSlotImpl(J9VMThread *vmThread, j9object_t *srcAddress, bool isVolatile)
{
mm_j9object_t object = *srcAddress;
if (NULL != vmThread) {
rememberObjectIfBarrierEnabled(vmThread, object);
}
return object;
}
/**
* Write an object to an internal VM slot (J9VMThread, J9JavaVM, named field of J9Class).
* In realtime, we must explicitly remember the value when being stored in case it's being
* read from an unmarked threads' stack and stored into a marked thread. If the unmarked
* thread terminates before being marked, we will miss the object since a stack pop doesn't
* invoke the barrier.
* @param destSlot the slot to be used
* @param value the value to be stored
*/
void
MM_RealtimeAccessBarrier::storeObjectToInternalVMSlot(J9VMThread *vmThread, J9Object** destSlot, J9Object *value)
{
if (preObjectStore(vmThread, destSlot, value, false)) {
rememberObjectIfBarrierEnabled(vmThread, value);
storeObjectToInternalVMSlotImpl(vmThread, destSlot, value, false);
postObjectStore(vmThread, destSlot, value, false);
}
}
/**
* Call rememberObject() if realtimeGC->isBarrierEnabled() returns true.
* @param object the object to remember
*/
void
MM_RealtimeAccessBarrier::rememberObjectIfBarrierEnabled(J9VMThread *vmThread, J9Object* object)
{
MM_EnvironmentRealtime* env = MM_EnvironmentRealtime::getEnvironment(vmThread->omrVMThread);
if (_realtimeGC->isBarrierEnabled()) {
rememberObject(env, object);
}
}
void*
MM_RealtimeAccessBarrier::jniGetPrimitiveArrayCritical(J9VMThread* vmThread, jarray array, jboolean *isCopy)
{
void *data = NULL;
J9JavaVM *javaVM = vmThread->javaVM;
J9InternalVMFunctions *functions = javaVM->internalVMFunctions;
J9IndexableObject *arrayObject = (J9IndexableObject*)J9_JNI_UNWRAP_REFERENCE(array);
bool shouldCopy = false;
if((javaVM->runtimeFlags & J9_RUNTIME_ALWAYS_COPY_JNI_CRITICAL) == J9_RUNTIME_ALWAYS_COPY_JNI_CRITICAL) {
shouldCopy = true;
} else if (!_extensions->indexableObjectModel.isInlineContiguousArraylet(arrayObject)) {
/* an array having discontiguous extents is another reason to force the critical section to be a copy */
shouldCopy = true;
}
if(shouldCopy) {
VM_VMAccess::inlineEnterVMFromJNI(vmThread);
GC_ArrayObjectModel* indexableObjectModel = &_extensions->indexableObjectModel;
I_32 sizeInElements = (I_32)indexableObjectModel->getSizeInElements(arrayObject);
UDATA sizeInBytes = indexableObjectModel->getDataSizeInBytes(arrayObject);
data = functions->jniArrayAllocateMemoryFromThread(vmThread, sizeInBytes);
if(NULL == data) {
functions->setNativeOutOfMemoryError(vmThread, 0, 0); // better error message here?
} else {
indexableObjectModel->memcpyFromArray(data, arrayObject, 0, sizeInElements);
if(NULL != isCopy) {
*isCopy = JNI_TRUE;
}
}
vmThread->jniCriticalCopyCount += 1;
VM_VMAccess::inlineExitVMToJNI(vmThread);
} else {
// acquire access and return a direct pointer
MM_JNICriticalRegion::enterCriticalRegion(vmThread, false);
data = (void *)_extensions->indexableObjectModel.getDataPointerForContiguous(arrayObject);
if(NULL != isCopy) {
*isCopy = JNI_FALSE;
}
}
return data;
}
void
MM_RealtimeAccessBarrier::jniReleasePrimitiveArrayCritical(J9VMThread* vmThread, jarray array, void * elems, jint mode)
{
J9JavaVM *javaVM = vmThread->javaVM;
J9InternalVMFunctions *functions = javaVM->internalVMFunctions;
J9IndexableObject *arrayObject = (J9IndexableObject*)J9_JNI_UNWRAP_REFERENCE(array);
bool shouldCopy = false;
if((javaVM->runtimeFlags & J9_RUNTIME_ALWAYS_COPY_JNI_CRITICAL) == J9_RUNTIME_ALWAYS_COPY_JNI_CRITICAL) {
shouldCopy = true;
} else if (!_extensions->indexableObjectModel.isInlineContiguousArraylet(arrayObject)) {
/* an array having discontiguous extents is another reason to force the critical section to be a copy */
shouldCopy = true;
}
if(shouldCopy) {
VM_VMAccess::inlineEnterVMFromJNI(vmThread);
if(JNI_ABORT != mode) {
GC_ArrayObjectModel* indexableObjectModel = &_extensions->indexableObjectModel;
I_32 sizeInElements = (I_32)indexableObjectModel->getSizeInElements(arrayObject);
_extensions->indexableObjectModel.memcpyToArray(arrayObject, 0, sizeInElements, elems);
}
// Commit means copy the data but do not free the buffer.
// All other modes free the buffer.
if(JNI_COMMIT != mode) {
functions->jniArrayFreeMemoryFromThread(vmThread, elems);
}
if(vmThread->jniCriticalCopyCount > 0) {
vmThread->jniCriticalCopyCount -= 1;
} else {
Assert_MM_invalidJNICall();
}
VM_VMAccess::inlineExitVMToJNI(vmThread);
} else {
/*
* Objects can not be moved if critical section is active
* This trace point will be generated if object has been moved or passed value of elems is corrupted
*/
void *data = (void *)_extensions->indexableObjectModel.getDataPointerForContiguous(arrayObject);
if(elems != data) {
Trc_MM_JNIReleasePrimitiveArrayCritical_invalid(vmThread, arrayObject, elems, data);
}
MM_JNICriticalRegion::exitCriticalRegion(vmThread, false);
}
}
const jchar*
MM_RealtimeAccessBarrier::jniGetStringCritical(J9VMThread* vmThread, jstring str, jboolean *isCopy)
{
jchar *data = NULL;
J9JavaVM *javaVM = vmThread->javaVM;
J9InternalVMFunctions *functions = javaVM->internalVMFunctions;
bool isCompressed = false;
bool shouldCopy = false;
bool hasVMAccess = false;
/* For now only copying is supported for arraylets */
VM_VMAccess::inlineEnterVMFromJNI(vmThread);
hasVMAccess = true;
shouldCopy = true;
if (shouldCopy) {
J9Object *stringObject = (J9Object*)J9_JNI_UNWRAP_REFERENCE(str);
J9IndexableObject *valueObject = (J9IndexableObject*)J9VMJAVALANGSTRING_VALUE(vmThread, stringObject);
jint length = J9VMJAVALANGSTRING_LENGTH(vmThread, stringObject);
UDATA sizeInBytes = length * sizeof(jchar);
if (IS_STRING_COMPRESSED(vmThread, stringObject)) {
isCompressed = true;
}
data = (jchar*)functions->jniArrayAllocateMemoryFromThread(vmThread, sizeInBytes);
if (NULL == data) {
functions->setNativeOutOfMemoryError(vmThread, 0, 0); // better error message here?
} else {
GC_ArrayObjectModel* indexableObjectModel = &_extensions->indexableObjectModel;
if (isCompressed) {
jint i;
for (i = 0; i < length; i++) {
data[i] = (jchar)(U_8)J9JAVAARRAYOFBYTE_LOAD(vmThread, (j9object_t)valueObject, i);
}
} else {
if (J9_ARE_ANY_BITS_SET(javaVM->runtimeFlags, J9_RUNTIME_STRING_BYTE_ARRAY)) {
// This API determines the stride based on the type of valueObject so in the [B case we must passin the length in bytes
indexableObjectModel->memcpyFromArray(data, valueObject, 0, (I_32)sizeInBytes);
} else {
indexableObjectModel->memcpyFromArray(data, valueObject, 0, length);
}
}
if (NULL != isCopy) {
*isCopy = JNI_TRUE;
}
}
vmThread->jniCriticalCopyCount += 1;
} else {
// acquire access and return a direct pointer
MM_JNICriticalRegion::enterCriticalRegion(vmThread, hasVMAccess);
J9Object *stringObject = (J9Object*)J9_JNI_UNWRAP_REFERENCE(str);
J9IndexableObject *valueObject = (J9IndexableObject*)J9VMJAVALANGSTRING_VALUE(vmThread, stringObject);
data = (jchar*)_extensions->indexableObjectModel.getDataPointerForContiguous(valueObject);
if (NULL != isCopy) {
*isCopy = JNI_FALSE;
}
}
if (hasVMAccess) {
VM_VMAccess::inlineExitVMToJNI(vmThread);
}
return data;
}
void
MM_RealtimeAccessBarrier::jniReleaseStringCritical(J9VMThread* vmThread, jstring str, const jchar* elems)
{
J9JavaVM *javaVM = vmThread->javaVM;
J9InternalVMFunctions *functions = javaVM->internalVMFunctions;
bool hasVMAccess = false;
bool shouldCopy = false;
/* For now only copying is supported for arraylets */
shouldCopy = true;
if (shouldCopy) {
// String data is not copied back
functions->jniArrayFreeMemoryFromThread(vmThread, (void*)elems);
if(vmThread->jniCriticalCopyCount > 0) {
vmThread->jniCriticalCopyCount -= 1;
} else {
Assert_MM_invalidJNICall();
}
} else {
// direct pointer, just drop access
MM_JNICriticalRegion::exitCriticalRegion(vmThread, hasVMAccess);
}
if (hasVMAccess) {
VM_VMAccess::inlineExitVMToJNI(vmThread);
}
}
#if defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
bool
MM_RealtimeAccessBarrier::checkClassLive(J9JavaVM *javaVM, J9Class *classPtr)
{
J9ClassLoader *classLoader = classPtr->classLoader;
bool result = false;
if ((0 == (classLoader->gcFlags & J9_GC_CLASS_LOADER_DEAD)) && (0 == (J9CLASS_FLAGS(classPtr) & J9AccClassDying))) {
/*
* this class has not been discovered dead yet
* so mark it if necessary to force it to be alive
*/
MM_GCExtensions *extensions = MM_GCExtensions::getExtensions(javaVM);
MM_RealtimeGC *realtimeGC = extensions->realtimeGC;
J9Object *classLoaderObject = classLoader->classLoaderObject;
if (NULL != classLoaderObject) {
if (realtimeGC->getRealtimeDelegate()->_unmarkedImpliesClasses) {
/*
* Mark is complete but GC cycle is still be in progress
* so we just can check is the correspondent class loader object marked
*/
result = realtimeGC->getMarkingScheme()->isMarked(classLoaderObject);
} else {
/*
* The return for this case is always true. If mark is active but not completed yet
* force this class to be marked to survive this GC
*/
J9VMThread* vmThread = javaVM->internalVMFunctions->currentVMThread(javaVM);
rememberObjectIfBarrierEnabled(vmThread, classLoaderObject);
result = true;
}
} else {
/* this class loader probably is in initialization process and class loader object has not been attached yet */
result = true;
}
}
return result;
}
#endif /* defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING) */
/**
* Unmarked, heap reference, about to be deleted (or overwritten), while marking
* is in progress is to be remembered for later marking and scanning.
* This method is called by MM_RealtimeAccessBarrier::rememberObject()
*/
void
MM_RealtimeAccessBarrier::rememberObjectImpl(MM_EnvironmentBase *env, J9Object* object)
{
J9VMThread *vmThread = (J9VMThread *)env->getLanguageVMThread();
MM_GCExtensions *extensions = MM_GCExtensions::getExtensions(vmThread->javaVM);
extensions->sATBBarrierRememberedSet->storeInFragment(env, &vmThread->sATBBarrierRememberedSetFragment, (UDATA *)object);
}
void
MM_RealtimeAccessBarrier::forcedToFinalizableObject(J9VMThread* vmThread, J9Object* object)
{
MM_EnvironmentBase* env = MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread);
if (isBarrierActive(env)) {
rememberObject(env, object);
}
}
/**
* @copydoc MM_ObjectAccessBarrier::preObjectStore()
*
* This is the implementation of the realtime write barrier.
*
* Realtime uses a snapshot-at-the-beginning algorithm, but with a fuzzy snapshot in the
* sense that threads are allowed to run during the root scan. This requires a "double
* barrier." The barrier is active from the start of root scanning through the end of
* tracing. For an unscanned thread performing a store, the new value is remembered by
* the collector. For any thread performing a store (whether scanned or not), the old
* value is remembered by the collector before being overwritten (thus this barrier must be
* positioned as a pre-store barrier). For the latter ("Yuasa barrier") aspect of the
* double barrier, only the first overwritten value needs to be remembered (remembering
* others is harmless but not needed), and so we omit synchronization on the reading of the
* old value.
*/
bool
MM_RealtimeAccessBarrier::preObjectStoreInternal(J9VMThread *vmThread, J9Object *destObject, fj9object_t *destAddress, J9Object *value, bool isVolatile)
{
MM_EnvironmentBase* env = MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread);
if (isBarrierActive(env)) {
if (NULL != destObject) {
if (isDoubleBarrierActiveOnThread(vmThread)) {
rememberObject(env, value);
}
J9Object *oldObject = NULL;
protectIfVolatileBefore(vmThread, isVolatile, true, false);
GC_SlotObject slotObject(vmThread->javaVM->omrVM, destAddress);
oldObject = slotObject.readReferenceFromSlot();
protectIfVolatileAfter(vmThread, isVolatile, true, false);
rememberObject(env, oldObject);
}
}
return true;
}
/**
* @copydoc MM_ObjectAccessBarrier::preObjectStore()
*
* This is the implementation of the realtime write barrier.
*
* Realtime uses a snapshot-at-the-beginning algorithm, but with a fuzzy snapshot in the
* sense that threads are allowed to run during the root scan. This requires a "double
* barrier." The barrier is active from the start of root scanning through the end of
* tracing. For an unscanned thread performing a store, the new value is remembered by
* the collector. For any thread performing a store (whether scanned or not), the old
* value is remembered by the collector before being overwritten (thus this barrier must be
* positioned as a pre-store barrier). For the latter ("Yuasa barrier") aspect of the
* double barrier, only the first overwritten value needs to be remembered (remembering
* others is harmless but not needed), and so we omit synchronization on the reading of the
* old value.
*/
bool
MM_RealtimeAccessBarrier::preObjectStoreInternal(J9VMThread *vmThread, J9Object **destAddress, J9Object *value, bool isVolatile)
{
MM_EnvironmentBase* env = MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread);
if (isBarrierActive(env)) {
if (isDoubleBarrierActiveOnThread(vmThread)) {
rememberObject(env, value);
}
J9Object* oldObject = NULL;
protectIfVolatileBefore(vmThread, isVolatile, true, false);
oldObject = *destAddress;
protectIfVolatileAfter(vmThread, isVolatile, true, false);
rememberObject(env, oldObject);
}
return true;
}
bool
MM_RealtimeAccessBarrier::preObjectStoreInternal(J9VMThread *vmThread, J9Object* destClass, J9Object **destAddress, J9Object *value, bool isVolatile)
{
/* the destClass argument is ignored, so just call the generic slot version */
return preObjectStoreInternal(vmThread, destAddress, value, isVolatile);
}
/**
* @copydoc MM_ObjectAccessBarrier::preObjectStore()
*
* Metronome uses a snapshot-at-the-beginning algorithm, but with a fuzzy snapshot in the
* sense that threads are allowed to run during the root scan. This requires a "double
* barrier." The barrier is active from the start of root scanning through the end of
* tracing. For an unscanned thread performing a store, the new value is remembered by
* the collector. For any thread performing a store (whether scanned or not), the old
* value is remembered by the collector before being overwritten (thus this barrier must be
* positioned as a pre-store barrier). For the latter ("Yuasa barrier") aspect of the
* double barrier, only the first overwritten value needs to be remembered (remembering
* others is harmless but not needed), and so we omit synchronization on the reading of the
* old value.
**/
bool
MM_RealtimeAccessBarrier::preObjectStore(J9VMThread *vmThread, J9Object *destObject, fj9object_t *destAddress, J9Object *value, bool isVolatile)
{
return preObjectStoreInternal(vmThread, destObject, destAddress, value, isVolatile);
}
/**
* @copydoc MM_MetronomeAccessBarrier::preObjectStore()
*
* Used for stores into classes
*/
bool
MM_RealtimeAccessBarrier::preObjectStore(J9VMThread *vmThread, J9Object *destClass, J9Object **destAddress, J9Object *value, bool isVolatile)
{
return preObjectStoreInternal(vmThread, destClass, destAddress, value, isVolatile);
}
/**
* @copydoc MM_MetronomeAccessBarrier::preObjectStore()
*
* Used for stores into internal structures
*/
bool
MM_RealtimeAccessBarrier::preObjectStore(J9VMThread *vmThread, J9Object **destAddress, J9Object *value, bool isVolatile)
{
return preObjectStoreInternal(vmThread, destAddress, value, isVolatile);
}
/**
* Enables the double barrier on the provided thread.
*/
void
MM_RealtimeAccessBarrier::setDoubleBarrierActiveOnThread(MM_EnvironmentBase* env)
{
MM_GCExtensions::getExtensions(env)->sATBBarrierRememberedSet->preserveLocalFragmentIndex(env, &(((J9VMThread *)env->getLanguageVMThread())->sATBBarrierRememberedSetFragment));
}
/**
* Disables the double barrier on the provided thread.
*/
void
MM_RealtimeAccessBarrier::setDoubleBarrierInactiveOnThread(MM_EnvironmentBase* env)
{
MM_GCExtensions::getExtensions(env)->sATBBarrierRememberedSet->restoreLocalFragmentIndex(env, &(((J9VMThread *)env->getLanguageVMThread())->sATBBarrierRememberedSetFragment));
}
void
MM_RealtimeAccessBarrier::initializeForNewThread(MM_EnvironmentBase* env)
{
MM_GCExtensions* extensions = MM_GCExtensions::getExtensions(env);
extensions->sATBBarrierRememberedSet->initializeFragment(env, &(((J9VMThread *)env->getLanguageVMThread())->sATBBarrierRememberedSetFragment));
if (isDoubleBarrierActive()) {
setDoubleBarrierActiveOnThread(env);
}
}
/* TODO: meter this scanning and include into utilization tracking */
void
MM_RealtimeAccessBarrier::scanContiguousArray(MM_EnvironmentRealtime *env, J9IndexableObject *objectPtr)
{
bool const compressed = env->compressObjectReferences();
J9JavaVM *vm = (J9JavaVM *)env->getLanguageVM();
#if defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
if(_realtimeGC->getRealtimeDelegate()->isDynamicClassUnloadingEnabled()) {
rememberObject(env, (J9Object *)objectPtr);
}
#endif /* J9VM_GC_DYNAMIC_CLASS_UNLOADING */
/* if NUA is enabled, separate path for contiguous arrays */
fj9object_t *scanPtr = (fj9object_t*) _extensions->indexableObjectModel.getDataPointerForContiguous(objectPtr);
fj9object_t *endScanPtr = GC_SlotObject::addToSlotAddress(scanPtr, _extensions->indexableObjectModel.getSizeInElements(objectPtr), compressed);
while(scanPtr < endScanPtr) {
/* since this is done from an external thread, we do not markObject, but rememberObject */
GC_SlotObject slotObject(vm->omrVM, scanPtr);
J9Object *field = slotObject.readReferenceFromSlot();
rememberObject(env, field);
scanPtr = GC_SlotObject::addToSlotAddress(scanPtr, 1, compressed);
}
/* this method assumes the array is large enough to set scan bit */
_markingScheme->setScanAtomic((J9Object *)objectPtr);
}
bool
MM_RealtimeAccessBarrier::markAndScanContiguousArray(MM_EnvironmentRealtime *env, J9IndexableObject *objectPtr)
{
UDATA arrayletSize = _extensions->indexableObjectModel.arrayletSize(objectPtr, /* arraylet index */ 0);
/* Sufficiently large to have a scan bit? */
if (arrayletSize < _extensions->minArraySizeToSetAsScanned) {
return false;
} else if (!_markingScheme->isScanned((J9Object *)objectPtr)) {
/* No, not scanned yet. We are going to mark it and scan right away */
_markingScheme->markAtomic((J9Object *)objectPtr);
/* The array might have been marked already (which means it will be scanned soon,
* or even being scanned at the moment). Regardless, we will proceed with scanning it */
scanContiguousArray(env, objectPtr);
}
return true;
}
/**
* Finds opportunities for doing the copy without executing Metronome WriteBarrier.
* @return ARRAY_COPY_SUCCESSFUL if copy was successful, ARRAY_COPY_NOT_DONE no copy is done
*/
I_32
MM_RealtimeAccessBarrier::backwardReferenceArrayCopyIndex(J9VMThread *vmThread, J9IndexableObject *srcObject, J9IndexableObject *destObject, I_32 srcIndex, I_32 destIndex, I_32 lengthInSlots)
{
MM_EnvironmentRealtime *env = MM_EnvironmentRealtime::getEnvironment(vmThread->omrVMThread);
/* a high level caller ensured destObject == srcObject */
if (_extensions->indexableObjectModel.isInlineContiguousArraylet(destObject)) {
if (isBarrierActive(env)) {
if (!markAndScanContiguousArray(env, destObject)) {
return ARRAY_COPY_NOT_DONE;
}
}
return doCopyContiguousBackward(vmThread, srcObject, destObject, srcIndex, destIndex, lengthInSlots);
}
return -2;
}
/**
* Finds opportunities for doing the copy without executing Metronome WriteBarrier.
* @return ARRAY_COPY_SUCCESSFUL if copy was successful, ARRAY_COPY_NOT_DONE no copy is done
*/
I_32
MM_RealtimeAccessBarrier::forwardReferenceArrayCopyIndex(J9VMThread *vmThread, J9IndexableObject *srcObject, J9IndexableObject *destObject, I_32 srcIndex, I_32 destIndex, I_32 lengthInSlots)
{
MM_EnvironmentRealtime *env = MM_EnvironmentRealtime::getEnvironment(vmThread->omrVMThread);
if (_extensions->indexableObjectModel.isInlineContiguousArraylet(destObject)
&& _extensions->indexableObjectModel.isInlineContiguousArraylet(srcObject)) {
if (isBarrierActive(env) ) {
if ((destObject != srcObject) && isDoubleBarrierActiveOnThread(vmThread)) {
return ARRAY_COPY_NOT_DONE;
} else {
if (markAndScanContiguousArray(env, destObject)) {
return doCopyContiguousForward(vmThread, srcObject, destObject, srcIndex, destIndex, lengthInSlots);
}
}
} else {
return doCopyContiguousForward(vmThread, srcObject, destObject, srcIndex, destIndex, lengthInSlots);
}
}
return -2;
}
void
MM_RealtimeAccessBarrier::preMountContinuation(J9VMThread *vmThread, j9object_t contObject)
{
MM_EnvironmentRealtime *env = MM_EnvironmentRealtime::getEnvironment(vmThread->omrVMThread);
if (isBarrierActive(env)) {
const bool beingMounted = true;
_realtimeGC->getRealtimeDelegate()->scanContinuationNativeSlots(env, contObject, beingMounted);
}
}
#endif /* J9VM_GC_REALTIME */