-
Notifications
You must be signed in to change notification settings - Fork 392
/
MemorySubSpace.cpp
2015 lines (1742 loc) · 64.2 KB
/
MemorySubSpace.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 1991, 2021 IBM Corp. and others
*
* 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] http://openjdk.java.net/legal/assembly-exception.html
*
* 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
*******************************************************************************/
#include "MemorySubSpace.hpp"
#include <string.h>
#include "omrlinkedlist.h"
#include "j9nongenerated.h"
#include "omrport.h"
#include "AllocateDescription.hpp"
#include "EnvironmentBase.hpp"
#include "Forge.hpp"
#include "GCCode.hpp"
#include "GCExtensionsBase.hpp"
#include "GlobalCollector.hpp"
#include "Heap.hpp"
#include "HeapRegionDescriptor.hpp"
#include "HeapResizeStats.hpp"
#include "Math.hpp"
#include "MemoryPool.hpp"
#include "MemorySpace.hpp"
#include "ModronAssertions.h"
#include "PercolateStats.hpp"
#include "PhysicalSubArena.hpp"
/* OMRTODO temporary workaround to allow both ut_j9mm.h and ut_omrmm.h to be included.
* Dependency on ut_j9mm.h should be removed in the future.
*/
#undef UT_MODULE_LOADED
#undef UT_MODULE_UNLOADED
#include "ut_omrmm.h"
class MM_EnvironmentBase;
extern "C" {
void
memorySubSpaceAsyncCallbackHandler(OMR_VMThread *omrVMThread)
{
/* TODO: MultiMemorySpace - This is really once for every collector in the system */
MM_EnvironmentBase *env = MM_EnvironmentBase::getEnvironment(omrVMThread);
if(!env->isThreadScanned()) {
MM_MemorySpace *defaultMemorySpace = env->getExtensions()->heap->getDefaultMemorySpace();
MM_MemorySubSpace *memorySubSpaceList = defaultMemorySpace->getMemorySubSpaceList();
while(memorySubSpaceList) {
memorySubSpaceList->getCollector()->scanThread(env);
memorySubSpaceList = memorySubSpaceList->getNext();
}
}
}
} /* extern "C" */
void
MM_MemorySubSpace::reportSystemGCStart(MM_EnvironmentBase* env, uint32_t gcCode)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
Trc_OMRMM_SystemGCStart(env->getOmrVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0));
Trc_MM_SystemGCStart(env->getLanguageVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0));
uint64_t exclusiveAccessTimeMicros = omrtime_hires_delta(0, env->getExclusiveAccessTime(), OMRPORT_TIME_DELTA_IN_MICROSECONDS);
uint64_t meanExclusiveAccessIdleTimeMicros = omrtime_hires_delta(0, env->getMeanExclusiveAccessIdleTime(), OMRPORT_TIME_DELTA_IN_MICROSECONDS);
Trc_MM_ExclusiveAccess(env->getLanguageVMThread(),
(uint32_t)(exclusiveAccessTimeMicros / 1000),
(uint32_t)(exclusiveAccessTimeMicros % 1000),
(uint32_t)(meanExclusiveAccessIdleTimeMicros / 1000),
(uint32_t)(meanExclusiveAccessIdleTimeMicros % 1000),
env->getExclusiveAccessHaltedThreads(),
env->getLastExclusiveAccessResponder(),
env->exclusiveAccessBeatenByOtherThread());
if (J9_EVENT_IS_HOOKED(_extensions->privateHookInterface, J9HOOK_MM_PRIVATE_SYSTEM_GC_START)) {
MM_CommonGCStartData commonData;
_extensions->heap->initializeCommonGCStartData(env, &commonData);
ALWAYS_TRIGGER_J9HOOK_MM_PRIVATE_SYSTEM_GC_START(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_SYSTEM_GC_START,
gcCode,
&commonData);
}
}
void
MM_MemorySubSpace::reportSystemGCEnd(MM_EnvironmentBase* env)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
Trc_OMRMM_SystemGCEnd(env->getOmrVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0));
Trc_MM_SystemGCEnd(env->getLanguageVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0));
if (J9_EVENT_IS_HOOKED(_extensions->privateHookInterface, J9HOOK_MM_PRIVATE_SYSTEM_GC_END)) {
MM_CommonGCEndData commonData;
_extensions->heap->initializeCommonGCEndData(env, &commonData);
ALWAYS_TRIGGER_J9HOOK_MM_PRIVATE_SYSTEM_GC_END(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_SYSTEM_GC_END,
env->getExclusiveAccessTime(),
&commonData);
}
}
/**
* Report the start of a heap expansion event through hooks.
*/
void
MM_MemorySubSpace::reportHeapResizeAttempt(MM_EnvironmentBase* env, uintptr_t amount, uintptr_t type)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
MM_HeapResizeStats *resizeStats = _extensions->heap->getResizeStats();
uint64_t resizeTime = (type == HEAP_EXPAND)
? resizeStats->getLastExpandTime()
: resizeStats->getLastContractTime();
uint32_t gcTimeRatio = 0;
if (HEAP_EXPAND == type) {
gcTimeRatio = resizeStats->getRatioExpandPercentage();
} else if (HEAP_CONTRACT == type) {
gcTimeRatio = resizeStats->getRatioContractPercentage();
}
uintptr_t reason = 0;
if (HEAP_EXPAND == type) {
reason = (uintptr_t)resizeStats->getLastExpandReason();
} else if (HEAP_CONTRACT == type) {
reason = (uintptr_t)resizeStats->getLastContractReason();
} else if (HEAP_LOA_EXPAND == type) {
reason = (uintptr_t)resizeStats->getLastLoaResizeReason();
Assert_MM_true(reason <= LOA_EXPAND_LAST_RESIZE_REASON);
} else if (HEAP_LOA_CONTRACT == type) {
reason = (uintptr_t)resizeStats->getLastLoaResizeReason();
Assert_MM_true(reason > LOA_EXPAND_LAST_RESIZE_REASON);
}
TRIGGER_J9HOOK_MM_PRIVATE_HEAP_RESIZE(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_HEAP_RESIZE,
type,
getTypeFlags(),
gcTimeRatio,
amount,
getActiveMemorySize(),
omrtime_hires_delta(0, resizeTime, OMRPORT_TIME_DELTA_IN_MICROSECONDS),
reason);
}
void
MM_MemorySubSpace::reportPercolateCollect(MM_EnvironmentBase* env)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
TRIGGER_J9HOOK_MM_PRIVATE_PERCOLATE_COLLECT(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_PERCOLATE_COLLECT,
(uintptr_t)_extensions->heap->getPercolateStats()->getLastPercolateReason());
}
/**
* Return the number of memory pools associated to the receiver.
* @note In the general case, 0 is returned.
* @return memory pool count
*/
uintptr_t
MM_MemorySubSpace::getMemoryPoolCount()
{
Assert_MM_unreachable();
return 0;
}
/**
* Event reporting
*/
void
MM_MemorySubSpace::reportAllocationFailureStart(MM_EnvironmentBase* env, MM_AllocateDescription* allocDescription)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
generateAllocationFailureStats(env, allocDescription);
env->allocationFailureStartReportIfRequired(allocDescription, getTypeFlags());
Trc_MM_AllocationFailureCycleStart(env->getLanguageVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0),
allocDescription->getBytesRequested());
Trc_OMRMM_AllocationFailureCycleStart(env->getOmrVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0),
allocDescription->getBytesRequested());
uint64_t exclusiveAccessTimeMicros = omrtime_hires_delta(0, env->getExclusiveAccessTime(), OMRPORT_TIME_DELTA_IN_MICROSECONDS);
uint64_t meanExclusiveAccessIdleTimeMicros = omrtime_hires_delta(0, env->getMeanExclusiveAccessIdleTime(), OMRPORT_TIME_DELTA_IN_MICROSECONDS);
Trc_MM_ExclusiveAccess(env->getLanguageVMThread(),
(uint32_t)(exclusiveAccessTimeMicros / 1000),
(uint32_t)(exclusiveAccessTimeMicros % 1000),
(uint32_t)(meanExclusiveAccessIdleTimeMicros / 1000),
(uint32_t)(meanExclusiveAccessIdleTimeMicros % 1000),
env->getExclusiveAccessHaltedThreads(),
env->getLastExclusiveAccessResponder(),
env->exclusiveAccessBeatenByOtherThread());
if (J9_EVENT_IS_HOOKED(_extensions->privateHookInterface, J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_START)) {
MM_CommonGCStartData commonData;
_extensions->heap->initializeCommonGCStartData(env, &commonData);
ALWAYS_TRIGGER_J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_START(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_START,
allocDescription->getBytesRequested(),
&commonData,
getTypeFlags());
}
}
void
MM_MemorySubSpace::reportAllocationFailureEnd(MM_EnvironmentBase* env)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
Trc_MM_AllocationFailureCycleEnd(env->getLanguageVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0));
Trc_OMRMM_AllocationFailureCycleEnd(env->getOmrVMThread(),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_NEW),
_extensions->heap->getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD),
_extensions->heap->getActiveMemorySize(MEMORY_TYPE_OLD),
(_extensions->largeObjectArea ? _extensions->heap->getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD) : 0),
(_extensions->largeObjectArea ? _extensions->heap->getActiveLOAMemorySize(MEMORY_TYPE_OLD) : 0));
if (J9_EVENT_IS_HOOKED(_extensions->privateHookInterface, J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_END)) {
MM_CommonGCEndData commonData;
_extensions->heap->initializeCommonGCEndData(env, &commonData);
ALWAYS_TRIGGER_J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_END(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_END,
env->getExclusiveAccessTime(),
getTypeFlags(),
&commonData);
}
}
void
MM_MemorySubSpace::reportAcquiredExclusiveToSatisfyAllocate(MM_EnvironmentBase* env, MM_AllocateDescription* allocateDescription)
{
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
Trc_MM_AcquiredExclusiveToSatisfyAllocation(env->getLanguageVMThread(), allocateDescription->getBytesRequested(), getTypeFlags());
TRIGGER_J9HOOK_MM_PRIVATE_ACQUIRED_EXCLUSIVE_TO_SATISFY_ALLOCATION(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_ALLOCATION_FAILURE_CYCLE_START,
allocateDescription->getBytesRequested(),
getTypeFlags());
}
void
MM_MemorySubSpace::registerMemorySubSpace(MM_MemorySubSpace* memorySubSpace)
{
memorySubSpace->setParent(this);
if (_children) {
_children->setPrevious(memorySubSpace);
}
memorySubSpace->setNext(_children);
memorySubSpace->setPrevious(NULL);
_children = memorySubSpace;
}
void
MM_MemorySubSpace::unregisterMemorySubSpace(MM_MemorySubSpace* memorySubSpace)
{
MM_MemorySubSpace* previous, *next;
previous = memorySubSpace->getPrevious();
next = memorySubSpace->getNext();
if (previous) {
previous->setNext(next);
} else {
_children = next;
}
if (next) {
next->setPrevious(previous);
}
}
/**
* Set the owning memory space and cascade down the hierarchy.
*/
void
MM_MemorySubSpace::setMemorySpace(MM_MemorySpace* memorySpace)
{
if (NULL != _physicalSubArena) {
_physicalSubArena->setParent(memorySpace->getPhysicalArena());
}
if (NULL != _children) {
_children->setMemorySpace(memorySpace);
}
if (NULL != _next) {
_next->setMemorySpace(memorySpace);
}
_memorySpace = memorySpace;
}
MM_MemorySubSpace *
MM_MemorySubSpace::getTopLevelMemorySubSpace(uintptr_t typeFlags)
{
Assert_MM_true(typeFlags == (getTypeFlags() & typeFlags));
MM_MemorySubSpace *topLevelSubSpace = this;
while (topLevelSubSpace->getParent() && (typeFlags == (topLevelSubSpace->getParent()->getTypeFlags() & typeFlags))) {
topLevelSubSpace = topLevelSubSpace->getParent();
}
return topLevelSubSpace;
}
/**
* If the subSpace is active return true, else return false.
* @note All subSpaces, with the exception of the semi-space, are active by default.
*/
bool
MM_MemorySubSpace::isActive()
{
return true;
}
/**
* If the child subSpace is active return true, else return false.
* @note All subSpaces, with the exception of the semi-space, are active by default.
*/
bool
MM_MemorySubSpace::isChildActive(MM_MemorySubSpace* memorySubSpace)
{
return true;
}
/**
* Initialize a memorysubspace
*/
bool
MM_MemorySubSpace::initialize(MM_EnvironmentBase* env)
{
if (!_lock.initialize(env, &env->getExtensions()->lnrlOptions, "MM_MemorySubSpace:_lock")) {
return false;
}
/* Attach to the correct parent */
/* TODO: this code to go */
if (_parent) {
_parent->registerMemorySubSpace(this);
} else if (_memorySpace) {
_memorySpace->registerMemorySubSpace(this);
}
/* If the receiver uses the global collector, find and associate to it */
if (_usesGlobalCollector) {
_collector = _extensions->getGlobalCollector();
}
if (NULL != _physicalSubArena) {
_physicalSubArena->setSubSpace(this);
}
return true;
}
/**
* Free all internal structures and resource of the receiver, and then free the receiver itself.
*/
void
MM_MemorySubSpace::kill(MM_EnvironmentBase* env)
{
tearDown(env);
env->getForge()->free(this);
}
void
MM_MemorySubSpace::tearDown(MM_EnvironmentBase* env)
{
MM_MemorySubSpace* child;
/* Kill the subarena */
if (NULL != _physicalSubArena) {
_physicalSubArena->kill(env);
_physicalSubArena = NULL;
}
/* Kill the collector (if applicable) */
if ((NULL != _collector) && !_usesGlobalCollector) {
_collector->kill(env);
_collector = NULL;
}
/* Kill all children */
child = _children;
while (NULL != child) {
MM_MemorySubSpace* next;
next = child->getNext();
child->kill(env);
child = next;
}
_children = NULL;
/* Remove the receiver from its owners list */
if (NULL != _parent) {
_parent->unregisterMemorySubSpace(this);
} else if (NULL != _memorySpace) {
_memorySpace->unregisterMemorySubSpace(this);
}
_lock.tearDown();
}
bool
MM_MemorySubSpace::inflate(MM_EnvironmentBase* env)
{
bool result;
MM_MemorySubSpace* child;
if (_physicalSubArena && !_physicalSubArena->inflate(env)) {
return false;
}
result = true;
child = _children;
while (result && child) {
result = child->inflate(env);
child = child->getNext();
}
return result;
}
/**
* Return the number of active memory pools associated to the receiver.
* @note In the general case, 0 is returned.
* @return memory pool count
*/
uintptr_t
MM_MemorySubSpace::getActiveMemoryPoolCount()
{
Assert_MM_unreachable();
return 0;
}
/**
* Return the memory pool associated to the receiver.
* @note In the general case, NULL is returned.
* @return MM_MemoryPool
*/
MM_MemoryPool*
MM_MemorySubSpace::getMemoryPool()
{
return NULL;
}
/**
* Return the memory pool associated with a given storage location.
* @param Address of stoarge location
* @return MM_MemoryPool
*/
MM_MemoryPool*
MM_MemorySubSpace::getMemoryPool(void* addr)
{
return getMemoryPool();
}
/**
* Return the memory pool associated with a given allocation size
* @param Size of allocation request
* @return MM_MemoryPool
*/
MM_MemoryPool*
MM_MemorySubSpace::getMemoryPool(uintptr_t size)
{
return getMemoryPool();
}
/**
* Return the memory pool associated with a specified range of storage locations.
*
* @param addrBase Low address in specified range
* @param addrTop High address in specified range
* @param highAddr If range spans end of memory pool set to address of first byte
* which does not belong in returned pool.
* @return MM_MemoryPool for storage location addrBase
*/
MM_MemoryPool*
MM_MemorySubSpace::getMemoryPool(MM_EnvironmentBase* env, void* addrBase, void* addrTop, void*& highAddr)
{
highAddr = NULL;
return getMemoryPool();
}
MM_LargeObjectAllocateStats*
MM_MemorySubSpace::getLargeObjectAllocateStats()
{
if (NULL == _children) {
return getMemoryPool()->getLargeObjectAllocateStats();
}
return NULL;
}
/**
* Generate the allocationfailurestats for the given allocDescription
* @param allocDescription the allocDescription to store the stats of
*/
void
MM_MemorySubSpace::generateAllocationFailureStats(MM_EnvironmentBase* env, MM_AllocateDescription* allocDescription)
{
_allocationFailureStats.subSpaceType = getTypeFlags();
_allocationFailureStats.allocationFailureSize = allocDescription->getBytesRequested();
_allocationFailureStats.allocationFailureCount += 1;
}
/**
* Get the sum of all free memory currently available for allocation in the subspace and its children.
* This call will return an accurate count of the current size of all free memory. It will not
* consider defered work that may be done to increase current free memory stores.
* @see getApproximateFreeMemorySize()
* @return the total free memory currently available for allocation.
*/
uintptr_t
MM_MemorySubSpace::getActualFreeMemorySize()
{
uintptr_t freeMemory;
freeMemory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
freeMemory += child->getActualFreeMemorySize();
child = child->getNext();
}
return freeMemory;
}
/**
* Get the approximate sum of all free memory available for allocation in the subspace and its children.
* This call will return an estimated count of the current size of all free memory. Although this
* estimate may be accurate, it will consider potential defered work that may be done to increase current
* free memory stores.
* @see getActualFreeMemorySize()
* @return the approximate total free memory available for allocation.
*/
uintptr_t
MM_MemorySubSpace::getApproximateFreeMemorySize()
{
uintptr_t freeMemory;
freeMemory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
freeMemory += child->getApproximateFreeMemorySize();
child = child->getNext();
}
return freeMemory;
}
uintptr_t
MM_MemorySubSpace::getActiveMemorySize()
{
return getActiveMemorySize(MEMORY_TYPE_OLD | MEMORY_TYPE_NEW);
}
uintptr_t
MM_MemorySubSpace::getActiveMemorySize(uintptr_t includeMemoryType)
{
uintptr_t memory;
memory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
memory += child->getActiveMemorySize(includeMemoryType);
child = child->getNext();
}
return memory;
}
uintptr_t
MM_MemorySubSpace::getActiveLOAMemorySize(uintptr_t includeMemoryType)
{
uintptr_t memory;
memory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
memory += child->getActiveLOAMemorySize(includeMemoryType);
child = child->getNext();
}
return memory;
}
uintptr_t
MM_MemorySubSpace::getActiveSurvivorMemorySize(uintptr_t includeMemoryType)
{
uintptr_t memory;
memory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
memory += child->getActiveSurvivorMemorySize(includeMemoryType);
child = child->getNext();
}
return memory;
}
/**
* Get the sum of all free memory currently available for allocation in the subspace and its children.
* This call will return an accurate count of the current size of all free memory. It will not
* consider defered work that may be done to increase current free memory stores.
* @see getApproximateActiveFreeMemorySize()
* @return the total free memory currently available for allocation.
*/
uintptr_t
MM_MemorySubSpace::getActualActiveFreeMemorySize()
{
return getActualActiveFreeMemorySize(MEMORY_TYPE_OLD | MEMORY_TYPE_NEW);
}
/**
* Get the sum of all free memory currently available for allocation in the subspace and its children of the specified type.
* This call will return an accurate count of the current size of all free memory of the specified type. It will not
* consider defered work that may be done to increase current free memory stores.
*
* @see getApproximateActiveFreeMemorySize(uintptr_t)
* @param includeMemoryType memory subspace types to consider in the calculation.
* @return the total free memory currently available for allocation from subspaces of the specified type.
*/
uintptr_t
MM_MemorySubSpace::getActualActiveFreeMemorySize(uintptr_t includeMemoryType)
{
uintptr_t freeMemory;
freeMemory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
freeMemory += child->getActualActiveFreeMemorySize(includeMemoryType);
child = child->getNext();
}
return freeMemory;
}
/**
* Get the sum of all free LOA memory currently available for allocation in the subspace and its children.
* This call will return an estimated count of the current size of all free memory. Although this
* estimate may be accurate, it will consider potential defered work that may be done to increase current
* free memory stores.
*
* @return the total free LOA memory currently available for allocation
*/
uintptr_t
MM_MemorySubSpace::getApproximateActiveFreeLOAMemorySize()
{
return getApproximateActiveFreeLOAMemorySize(MEMORY_TYPE_OLD);
}
uintptr_t
MM_MemorySubSpace::getApproximateActiveFreeSurvivorMemorySize()
{
return getApproximateActiveFreeSurvivorMemorySize(MEMORY_TYPE_NEW);
}
/**
* Get the sum of all free LOA memory currently available for allocation in the subspace and its children of the specified type.
* This call will return an estimated count of the current size of all free memory. Although this
* estimate may be accurate, it will consider potential defered work that may be done to increase current
* free memory stores.
*
* @param includeMemoryType memory subspace types to consider in the calculation.
* @return the total free memory currently available for allocation from subspaces of the specified type.
*/
uintptr_t
MM_MemorySubSpace::getApproximateActiveFreeLOAMemorySize(uintptr_t includeMemoryType)
{
uintptr_t freeMemory;
freeMemory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
freeMemory += child->getApproximateActiveFreeLOAMemorySize(includeMemoryType);
child = child->getNext();
}
return freeMemory;
}
uintptr_t
MM_MemorySubSpace::getApproximateActiveFreeSurvivorMemorySize(uintptr_t includeMemoryType)
{
uintptr_t freeMemory;
freeMemory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
freeMemory += child->getApproximateActiveFreeSurvivorMemorySize(includeMemoryType);
child = child->getNext();
}
return freeMemory;
}
/**
* Get the approximate sum of all free memory available for allocation in the subspace and its children.
* This call will return an estimated count of the current size of all free memory. Although this
* estimate may be accurate, it will consider potential defered work that may be done to increase current
* free memory stores.
*
* @see getActualActiveFreeMemorySize()
* @return the approximate total free memory available for allocation.
*/
uintptr_t
MM_MemorySubSpace::getApproximateActiveFreeMemorySize()
{
return getApproximateActiveFreeMemorySize(MEMORY_TYPE_OLD | MEMORY_TYPE_NEW);
}
/**
* Get the approximate sum of all free memory available for allocation in subspace and its children of the specified type.
* This call will return an estimated count of the current size of all free memory of the specified type. Although this
* estimate may be accurate, it will consider defered work that may be done to increase current free memory stores.
*
* @see getActualActiveFreeMemorySize(uintptr_t)
* @param includeMemoryType memory subspace types to consider in the calculation.
* @return the total free memory currently available for allocation from subspaces of the specified type.
*/
uintptr_t
MM_MemorySubSpace::getApproximateActiveFreeMemorySize(uintptr_t includeMemoryType)
{
uintptr_t freeMemory;
freeMemory = 0;
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
freeMemory += child->getApproximateActiveFreeMemorySize(includeMemoryType);
child = child->getNext();
}
return freeMemory;
}
/**
* Ask all memory spaces/pools if a complete rebuild of the freelist is requried
*/
bool
MM_MemorySubSpace::completeFreelistRebuildRequired(MM_EnvironmentBase* env)
{
MM_MemorySubSpace* child = _children;
bool rebuildRequired = false;
while (!rebuildRequired && child) {
rebuildRequired = child->completeFreelistRebuildRequired(env);
child = child->getNext();
}
return rebuildRequired;
}
void
MM_MemorySubSpace::mergeHeapStats(MM_HeapStats* heapStats)
{
mergeHeapStats(heapStats, (MEMORY_TYPE_OLD | MEMORY_TYPE_NEW));
}
void
MM_MemorySubSpace::mergeHeapStats(MM_HeapStats* heapStats, uintptr_t includeMemoryType)
{
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
child->mergeHeapStats(heapStats, includeMemoryType);
child = child->getNext();
}
}
void
MM_MemorySubSpace::resetHeapStatistics(bool globalCollect)
{
/* Call on children */
MM_MemorySubSpace* child;
child = _children;
while (child) {
child->resetHeapStatistics(globalCollect);
child = child->getNext();
}
}
/**
* Return the allocation failure stats for this subSpace.
*/
MM_AllocationFailureStats*
MM_MemorySubSpace::getAllocationFailureStats()
{
return &_allocationFailureStats;
}
void
MM_MemorySubSpace::systemGarbageCollect(MM_EnvironmentBase* env, uint32_t gcCode)
{
if (_parent) {
_parent->systemGarbageCollect(env, gcCode);
return;
}
/* do not launch system gc in -Xgcpolicy:nogc */
if (_collector && _usesGlobalCollector && !_collector->isDisabled(env)) {
/* TODO: This is bogus for multiple memory spaces - should ask the space, not the heap */
_extensions->heap->getResizeStats()->setFreeBytesAtSystemGCStart(getApproximateActiveFreeMemorySize());
env->acquireExclusiveVMAccessForGC(_collector);
reportSystemGCStart(env, gcCode);
/* system GCs are accounted into "user" time in GC/total time ratio calculation */
_collector->garbageCollect(env, this, NULL, gcCode, NULL, NULL, NULL);
reportSystemGCEnd(env);
env->releaseExclusiveVMAccessForGC();
#if defined(OMR_GC_IDLE_HEAP_MANAGER)
if ((J9MMCONSTANT_EXPLICIT_GC_IDLE_GC == gcCode) && (_extensions->gcOnIdle)) {
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);
uint64_t startTime = omrtime_hires_clock();
uintptr_t releasedBytes = _extensions->heap->getDefaultMemorySpace()->releaseFreeMemoryPages(env);
uint64_t endTime = omrtime_hires_clock();
TRIGGER_J9HOOK_MM_PRIVATE_HEAP_RESIZE(
_extensions->privateHookInterface,
env->getOmrVMThread(),
omrtime_hires_clock(),
J9HOOK_MM_PRIVATE_HEAP_RESIZE,
HEAP_RELEASE_FREE_PAGES,
getTypeFlags(),
/* GC Time Ratio not applicable for "release free heap pages" */
0,
releasedBytes,
getActiveMemorySize(),
omrtime_hires_delta(startTime, endTime, OMRPORT_TIME_DELTA_IN_MICROSECONDS),
/* reason enum variable not applicable/used, so passing univeral value 1 = not found*/
1
);
}
#endif
}
}
bool
MM_MemorySubSpace::percolateGarbageCollect(MM_EnvironmentBase* env, MM_AllocateDescription* allocDescription, uint32_t gcCode)
{
Trc_MM_MemorySubSpace_percolateGarbageCollect_Entry(env->getLanguageVMThread());
if (_parent) {
bool result = _parent->garbageCollect(env, allocDescription, gcCode);
Trc_MM_MemorySubSpace_percolateGarbageCollect_Exit1(env->getLanguageVMThread(), result ? "true" : "false");
Trc_OMRMM_MemorySubSpace_percolateGarbageCollect_Exit1(env->getOmrVMThread(), result ? "true" : "false");
return result;
}
Trc_MM_MemorySubSpace_percolateGarbageCollect_Exit2(env->getLanguageVMThread());
Trc_OMRMM_MemorySubSpace_percolateGarbageCollect_Exit2(env->getOmrVMThread());
return false; /* No gc done */
}
bool
MM_MemorySubSpace::garbageCollect(MM_EnvironmentBase* env, MM_AllocateDescription* allocDescription, uint32_t gcCode)
{
Trc_MM_MemorySubSpace_garbageCollect_Entry(env->getLanguageVMThread());
/* If MMS has a collector call it.. */
if (_collector) {
if (_collector->forceKickoff(env, this, allocDescription, gcCode)) {
Trc_MM_MemorySubSpace_garbageCollect_Exit4(env->getLanguageVMThread());
return false; /* forced kickoff activated */
}
if (MM_GCCode(gcCode).isPercolateGC()) {
/* MM_EnvironmentBase::acquireExclusiveVMAccessForGC has a mechanism to ensure
* that only one GC is triggered by a multiple mutators racing to acquire exclusive VM access for GC.
* That mechanism works well, if original requesting Collectors are same.
* However in percolate case, original Collector (for example Scavenge) the requested exclusive access and the actual performing collectors
* (in same example, Global) are same.
* If original requesting Collectors are different, but the actual performing collectors ended up not being same,