1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package gc.g1.humongousObjects;
25 
26 import gc.testlibrary.Helpers;
27 import jdk.test.lib.Asserts;
28 import sun.hotspot.WhiteBox;
29 
30 import java.lang.ref.Reference;
31 import java.lang.ref.ReferenceQueue;
32 import java.lang.ref.SoftReference;
33 import java.lang.ref.WeakReference;
34 
35 /**
36  * @test TestObjectCollected
37  * @summary checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
38  * actual object behavior
39  * @requires vm.gc.G1
40  * @library /test/lib /
41  * @modules java.base/jdk.internal.misc
42  * @modules java.management
43  * @build sun.hotspot.WhiteBox
44  * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
45  *
46  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
47  *                   -XX:+WhiteBoxAPI -Xbootclasspath/a:. -Xms200m -Xmx200m -Xlog:gc
48  *                   -XX:InitiatingHeapOccupancyPercent=100 -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectCollected.gc.log
49  *                    gc.g1.humongousObjects.TestObjectCollected
50  */
51 
52 
53 /**
54  * Test checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
55  * actual object behavior.
56  * So if object was collected, reference.get() should return null and vice versa
57  * Since we check humongous objects after such an object is collected the region where it was allocated becomes free
58  * or/and change type to non-humongous. Two WhiteBox method were used - first returns if a region containing certain
59  * address is free and second - if a region containing certain address is humongous
60  */
61 
62 public class TestObjectCollected {
63     /**
64      * Provides methods to initiate GC of requested type
65      */
66     private enum GC {
67         YOUNG_CG {
68             @Override
provoke()69             public void provoke() {
70                 WHITE_BOX.youngGC();
71             }
72         },
73         FULL_GC {
74             @Override
provoke()75             public void provoke() {
76                 System.gc();
77             }
78         },
79         CMC {
80             @Override
provoke()81             public void provoke() {
82                 Helpers.waitTillCMCFinished(WHITE_BOX, 0);
83                 WHITE_BOX.g1StartConcMarkCycle();
84                 Helpers.waitTillCMCFinished(WHITE_BOX, 0);
85             }
86         },
87         FULL_GC_MEMORY_PRESSURE {
88             @Override
provoke()89             public void provoke() {
90                 WHITE_BOX.fullGC();
91             }
92         };
93         private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
94 
provoke()95         public abstract void provoke();
96     }
97 
98     /**
99      * Factory for weak and soft references.
100      * Allocates byte array of ALLOCATION_SIZE and returns weak/soft reference on it.
101      */
102     private enum REF_FACTORY {
103         WEAK {
104             @Override
create()105             public Reference<byte[]> create() {
106                 return new WeakReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
107             }
108         },
109         SOFT {
110             @Override
create()111             public Reference<byte[]> create() {
112                 return new SoftReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
113             }
114         };
115 
116         private static final ReferenceQueue<byte[]> referenceQueqe = new ReferenceQueue<>();
117         private static final int ALLOCATION_SIZE = WhiteBox.getWhiteBox().g1RegionSize() * 2 / 3;
118 
119         /**
120          * Factory method
121          *
122          * @return weak/soft reference on byte array of ALLOCATION_SIZE
123          */
create()124         public abstract Reference<byte[]> create();
125     }
126 
127 
128     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
129 
130     /**
131      * Does actual testing:
132      * Gets a reference
133      * Gets address of referenced object using WB method
134      * Calls gc of provided type
135      * Checks that object was/was not deleted using WB methods.
136      */
doTesting(GC gc, REF_FACTORY ref)137     public static void doTesting(GC gc, REF_FACTORY ref) {
138 
139         System.out.println(String.format("Testing %s reference behavior after %s", ref.name(), gc.name()));
140 
141         Reference<byte[]> reference = ref.create();
142         Asserts.assertNotNull(reference, "Test Bug: failed to allocate reference");
143         long adr = WHITE_BOX.getObjectAddress(reference.get());
144 
145         //Sanity checks
146         boolean isRefNulled = (reference.get() == null);
147         boolean isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
148         boolean isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
149 
150 
151         Asserts.assertEquals(isRefNulled, false,
152                 "We just allocated an object but reference.get() already returned null");
153 
154         Asserts.assertEquals(isRegionFree, false,
155                 "We just allocated an object but WB returns that allocation region is still considered free");
156 
157         Asserts.assertEquals(isRegionHumongous, true,
158                 "We just allocated a humongous object but WB returns that allocation region is not humongous");
159 
160         gc.provoke();
161 
162         isRefNulled = (reference.get() == null);
163         isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
164         isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
165 
166         boolean isObjCollected = isRegionFree || !isRegionHumongous;
167 
168         Asserts.assertEquals(isRefNulled, isObjCollected,
169                 String.format("There is an inconsistensy between reference and white box "
170                                 + "method behavior - one considers object referenced with "
171                                 + "%s type collected and another doesn't!\n"
172                                 + "\treference.get() returned %snull\n"
173                                 + "\tWhiteBox methods returned that object was%s collected",
174                         reference.getClass().getSimpleName(),
175                         (isRefNulled ? "" : "not "),
176                         (isObjCollected ? "" : " not")));
177 
178         System.out.println("Passed");
179     }
180 
181     /**
182      * Entry point
183      *
184      * @param args not used
185      */
main(String[] args)186     public static void main(String[] args) {
187         // Full gc - System.gc()
188         TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.WEAK);
189         TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.SOFT);
190 
191         // Full gc with memory pressure - WB.fullGC() emulates that no memory left
192         TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.WEAK);
193         TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.SOFT);
194 
195         // Young gc
196         TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.WEAK);
197         TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.SOFT);
198 
199         // Concurrent mark cycle
200         TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.WEAK);
201         TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.SOFT);
202     }
203 }
204