1 /*
2  * Copyright (c) 2016, 2018, 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.objectGraphTest;
25 
26 import jdk.test.lib.process.OutputAnalyzer;
27 import sun.hotspot.WhiteBox;
28 
29 import java.io.File;
30 import java.io.IOException;
31 import java.lang.ref.Reference;
32 import java.lang.ref.ReferenceQueue;
33 import java.lang.ref.SoftReference;
34 import java.lang.ref.WeakReference;
35 import java.nio.file.Files;
36 import java.util.Map;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.HashSet;
40 import java.util.Set;
41 import java.util.function.BiConsumer;
42 import java.util.function.Consumer;
43 import java.util.function.Predicate;
44 import java.util.stream.Collectors;
45 
46 
47 /**
48  * @test TestObjectGraphAfterGC
49  * @summary Checks that objects' graph behave as expected after gc
50  * @requires vm.gc.G1
51  * @requires vm.opt.ExplicitGCInvokesConcurrent != true
52  * @library /test/lib /
53  * @modules java.management java.base/jdk.internal.misc
54  * @build sun.hotspot.WhiteBox
55  *
56  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
57  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
58  *
59  * @run main/othervm -Xms200M -Xmx200M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
60  * -XX:+UnlockExperimentalVMOptions -XX:MaxGCPauseMillis=30000 -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0
61  * -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectGraphAfterGC_MIXED_GC.gc.log -XX:MaxTenuringThreshold=1
62  * -XX:G1MixedGCCountTarget=1  -XX:G1OldCSetRegionThresholdPercent=100 -XX:SurvivorRatio=1 -XX:InitiatingHeapOccupancyPercent=0
63  * gc.g1.humongousObjects.objectGraphTest.TestObjectGraphAfterGC MIXED_GC
64  *
65  * @run main/othervm -Xms200M -Xmx200M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
66  * -XX:G1HeapRegionSize=1M -Xlog:gc*=debug:file=TestObjectGraphAfterGC_YOUNG_GC.gc.log
67  * gc.g1.humongousObjects.objectGraphTest.TestObjectGraphAfterGC YOUNG_GC
68  *
69  * @run main/othervm -Xms200M -Xmx200M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
70  * -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectGraphAfterGC_FULL_GC.gc.log
71  * gc.g1.humongousObjects.objectGraphTest.TestObjectGraphAfterGC FULL_GC
72  *
73  * @run main/othervm -Xms200M -Xmx200M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
74  * -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectGraphAfterGC_FULL_GC_MEMORY_PRESSURE.gc.log
75  * gc.g1.humongousObjects.objectGraphTest.TestObjectGraphAfterGC FULL_GC_MEMORY_PRESSURE
76  *
77  * @run main/othervm -Xms200M -Xmx200M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
78  * -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectGraphAfterGC_CMC.gc.log -XX:MaxTenuringThreshold=16
79  * gc.g1.humongousObjects.objectGraphTest.TestObjectGraphAfterGC CMC
80  *
81  * @run main/othervm -Xms200M -Xmx200M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
82  * -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectGraphAfterGC_CMC_NO_SURV_ROOTS.gc.log -XX:MaxTenuringThreshold=1
83  * gc.g1.humongousObjects.objectGraphTest.TestObjectGraphAfterGC CMC_NO_SURV_ROOTS
84  *
85  */
86 
87 /**
88  * Checks that objects' graph behave as expected after gc
89  * See README file for detailed info on test's logic
90  */
91 public class TestObjectGraphAfterGC {
92 
93     private static final int simpleAllocationSize = 1024;
94 
95     /**
96      * Entry point
97      *
98      * @param args - first argument - gc name
99      */
main(String[] args)100     public static void main(String[] args) {
101 
102         if (args.length < 1) {
103             throw new Error("Expected gc name wasn't provided as command line argument");
104         }
105 
106         GC gcType = GC.valueOf(args[0].toUpperCase());
107 
108         System.out.println("Testing " + gcType.name());
109 
110         TestcaseData.getPregeneratedTestcases().stream().forEach(testcase -> {
111             System.out.println("Testcase: " + testcase);
112 
113             try {
114                 TestObjectGraphAfterGC.doTesting(testcase, gcType.get(), gcType.getChecker(),
115                         gcType.getGcLogName(TestObjectGraphAfterGC.class.getSimpleName()), gcType.shouldContain(),
116                         gcType.shouldNotContain());
117             } catch (IOException e) {
118                 throw new Error("Problems trying to find or open " + TestObjectGraphAfterGC.class.getSimpleName()
119                         + ".gc.log", e);
120             }
121             System.out.println(" Passed");
122         });
123     }
124 
125     /**
126      * Implements testing with 3 methods - allocateObjectGraph, checkResults and checkGCLog
127      *
128      * @param testcaseData     testcase in the following notation:
129      *                         H - humongous node
130      *                         S - non-humongous node
131      *                         s - external soft reference
132      *                         w - external weak reference
133      *                         Hs->Sw - 1st node is humongous, externally soft referenced and strong references to
134      *                         non-humongous node 2 which is externally weak referenced
135      *                         H->1 - humongous node connects to the first node of chain
136      * @param doGC             method that initiates gc
137      * @param checker          consumer that checks node's state after gc and throws Error if it's wrong
138      * @param gcLogName        name of gc log
139      * @param shouldContain    list of tokens that should be contained in gc log
140      * @param shouldNotContain list of tokens that should not be contained in gc log
141      * @throws IOException if there are some issues with gc log
142      */
doTesting(String testcaseData, Runnable doGC, Consumer<ReferenceInfo<Object[]>> checker, String gcLogName, List<String> shouldContain, List<String> shouldNotContain)143     private static void doTesting(String testcaseData, Runnable doGC, Consumer<ReferenceInfo<Object[]>> checker,
144                                   String gcLogName, List<String> shouldContain, List<String> shouldNotContain)
145             throws IOException {
146         Set<ReferenceInfo<Object[]>> nodeData = allocateObjectGraph(testcaseData);
147         doGC.run();
148         checkResults(nodeData, checker);
149         checkGCLog(gcLogName, shouldContain, shouldNotContain);
150     }
151 
152     /**
153      * Allocates a number of objects of humongous and regular size and links then with strong references.
154      * How many objects to create, their size and links between them is encoded in the given parameters.
155      * As the result an object graph will be created.
156      * For the testing purpose for each created object (a graph node) an extra ReferenceInfo object will be created.
157      * The ReferenceInfo instances will contain either weak or soft reference to the graph node.
158      *
159      * @param testcaseData testcase in the
160      *                     <p>
161      *                     H - humongous node
162      *                     S - non-humongous node
163      *                     s - external soft reference
164      *                     w - external weak reference
165      *                     Hs->Sw - 1st node is humongous, externally soft referenced and strong references to
166      *                     non-humongous node 2 which is externally weak referenced
167      *                     H->1 - humongous node connects to the first node of chain
168      * @return set of ReferenceInfo objects containing weak/soft reference to the graph node and other data on how
169      * objects should behave after gc
170      */
allocateObjectGraph(String testcaseData)171     private static Set<ReferenceInfo<Object[]>> allocateObjectGraph(String testcaseData) {
172         Map<Object[], String> nodeIds = new HashMap<>();
173         Set<Object[]> humongousNodes = new HashSet<>();
174         Set<Object[]> externalSoftReferenced = new HashSet<>();
175         Set<Object[]> externalWeakReferenced = new HashSet<>();
176 
177         Map<Predicate<TestcaseData.FinalParsedNode>, BiConsumer<TestcaseData.FinalParsedNode, Object[][]>> visitors
178                 = new HashMap<>();
179 
180         visitors.put((parsedNode -> true),
181                 (parsedNode, objects) -> nodeIds.put(objects[Integer.valueOf(parsedNode.id)], parsedNode.id)
182         );
183 
184         visitors.put((parsedNode -> parsedNode.isHumongous),
185                 (parsedNode, objects) -> humongousNodes.add(objects[Integer.valueOf(parsedNode.id)])
186         );
187 
188         visitors.put(parsedNode -> parsedNode.getReferencesTypes().stream().
189                         anyMatch(referenceType -> referenceType == ObjectGraph.ReferenceType.SOFT),
190                 (parsedNode, objects) -> externalSoftReferenced.add(objects[Integer.valueOf(parsedNode.id)])
191         );
192 
193         visitors.put(parsedNode -> parsedNode.getReferencesTypes().stream().
194                         anyMatch(referenceType -> referenceType == ObjectGraph.ReferenceType.WEAK),
195                 (parsedNode, objects) -> externalWeakReferenced.add(objects[Integer.valueOf(parsedNode.id)])
196         );
197 
198         List<TestcaseData.FinalParsedNode> internalParsedNodes = TestcaseData.parse(testcaseData);
199 
200         Object[] root = ObjectGraph.generateObjectNodes(internalParsedNodes, visitors,
201                 WhiteBox.getWhiteBox().g1RegionSize(), simpleAllocationSize);
202 
203         ObjectGraph.propagateTransitiveProperty(humongousNodes, humongousNodes::add);
204         Set<Object[]> effectiveSoftReferenced = new HashSet<>();
205         ObjectGraph.propagateTransitiveProperty(externalSoftReferenced, effectiveSoftReferenced::add);
206 
207         // Create external references
208         ReferenceQueue<Object[]> referenceQueue = new ReferenceQueue<>();
209         Set<Reference<Object[]>> externalRefs = new HashSet<>();
210 
211         externalWeakReferenced.stream()
212                 .forEach(objects -> externalRefs.add(new WeakReference<>(objects, referenceQueue)));
213         externalSoftReferenced.stream()
214                 .forEach(objects -> externalRefs.add(new SoftReference<>(objects, referenceQueue)));
215 
216         return externalRefs.stream()
217                 .map(ref -> new ReferenceInfo<>(ref, testcaseData, nodeIds.get(ref.get()),
218                         effectiveSoftReferenced.contains(ref.get()), humongousNodes.contains(ref.get())))
219                 .collect(Collectors.toSet());
220 
221     }
222 
223     /**
224      * Checks that object' state after gc is as expected
225      *
226      * @param nodeData array with information about nodes
227      * @param checker  consumer that checks node's state after gc and throws Error if it's wrong
228      */
checkResults(Set<ReferenceInfo<Object[]>> nodeData, Consumer<ReferenceInfo<Object[]>> checker)229     private static void checkResults(Set<ReferenceInfo<Object[]>> nodeData, Consumer<ReferenceInfo<Object[]>> checker) {
230         nodeData.stream().forEach(checker::accept);
231     }
232 
233     /**
234      * Checks that gc log contains what we expected and does not contain what we didn't expect
235      *
236      * @param gcLogName        gc log name
237      * @param shouldContain    list of tokens that should be contained in gc log
238      * @param shouldNotContain list of tokens that should not be contained in gc log
239      * @throws IOException if there are some issues with gc log
240      */
checkGCLog(String gcLogName, List<String> shouldContain, List<String> shouldNotContain)241     private static void checkGCLog(String gcLogName, List<String> shouldContain, List<String> shouldNotContain)
242             throws IOException {
243 
244         if (gcLogName == null) {
245             return;
246         }
247         String gcLog = new String(Files.readAllBytes(new File(gcLogName).toPath()));
248 
249         OutputAnalyzer outputAnalyzer = new OutputAnalyzer(gcLog, "");
250 
251         shouldContain.stream().forEach(outputAnalyzer::shouldContain);
252         shouldNotContain.stream().forEach(outputAnalyzer::shouldNotContain);
253     }
254 
255 }
256