1 /* 2 * Copyright (c) 2018, 2019, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.gc.collection; 27 28 import static java.lang.System.gc; 29 import static java.lang.Thread.sleep; 30 import static java.util.Set.of; 31 import static java.util.stream.Collectors.joining; 32 import static java.util.stream.Collectors.toList; 33 import static java.util.stream.Collectors.toSet; 34 import static java.util.stream.IntStream.range; 35 import static jdk.jfr.event.gc.collection.Provoker.provokeMixedGC; 36 import static jdk.test.lib.Asserts.assertEquals; 37 import static jdk.test.lib.Asserts.assertTrue; 38 import static jdk.test.lib.jfr.Events.fromRecording; 39 import static sun.hotspot.WhiteBox.getWhiteBox; 40 41 import java.io.IOException; 42 import java.lang.ref.WeakReference; 43 import java.math.BigDecimal; 44 import java.util.ArrayList; 45 import java.util.Collection; 46 import java.util.List; 47 import java.util.Set; 48 49 import jdk.jfr.Recording; 50 import jdk.test.lib.Asserts; 51 import jdk.test.lib.jfr.EventNames; 52 import sun.hotspot.WhiteBox; 53 54 /** 55 * @test 56 * @key jfr 57 * @requires vm.hasJFR 58 * @requires vm.gc == "G1" | vm.gc == null 59 * @library /test/lib /test/jdk 60 * @build sun.hotspot.WhiteBox 61 * @run main ClassFileInstaller sun.hotspot.WhiteBox 62 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:MaxTenuringThreshold=1 -Xms20M -Xmx20M 63 * -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0 -XX:G1HeapRegionSize=1m 64 * -XX:+UseG1GC -XX:+UseStringDeduplication 65 * -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 66 * jdk.jfr.event.gc.collection.TestG1ParallelPhases 67 */ 68 69 public class TestG1ParallelPhases { 70 public static List<WeakReference<byte[]>> weakRefs; 71 72 public static void main(String[] args) throws IOException { 73 Recording recording = new Recording(); 74 recording.enable(EventNames.GCPhaseParallel); 75 recording.start(); 76 77 // create more weak garbage than can fit in this heap (-Xmx20m), will force collection of weak references 78 weakRefs = range(1, 100) 79 .mapToObj(n -> new WeakReference<>(new byte[1_000_000])) 80 .collect(toList()); // force evaluation of lazy stream (all weak refs must be created) 81 82 final var MEG = 1024 * 1024; 83 provokeMixedGC(1 * MEG); 84 recording.stop(); 85 86 Set<String> usedPhases = fromRecording(recording).stream() 87 .map(e -> e.getValue("name").toString()) 88 .collect(toSet()); 89 90 Set<String> allPhases = of( 91 "ExtRootScan", 92 "ThreadRoots", 93 "UniverseRoots", 94 "JNIRoots", 95 "ObjectSynchronizerRoots", 96 "ManagementRoots", 97 "SystemDictionaryRoots", 98 "CLDGRoots", 99 "JVMTIRoots", 100 "CMRefRoots", 101 "WaitForStrongRoots", 102 "MergeER", 103 "MergeHCC", 104 "MergeRS", 105 "MergeLB", 106 "ScanHR", 107 "CodeRoots", 108 "ObjCopy", 109 "Termination", 110 "StringDedupQueueFixup", 111 "StringDedupTableFixup", 112 "RedirtyCards", 113 "NonYoungFreeCSet", 114 "YoungFreeCSet" 115 ); 116 117 // Some GC phases may or may not occur depending on environment. Filter them out 118 // since we can not reliably guarantee that they occur (or not). 119 Set<String> optPhases = of( 120 "OptScanHR", 121 "OptMergeRS", 122 "OptCodeRoots", 123 "OptObjCopy" 124 ); 125 usedPhases.removeAll(optPhases); 126 127 assertTrue(usedPhases.equals(allPhases), "Compare events expected and received" 128 + ", Not found phases: " + allPhases.stream().filter(p -> !usedPhases.contains(p)).collect(joining(", ")) 129 + ", Not expected phases: " + usedPhases.stream().filter(p -> !allPhases.contains(p)).collect(joining(", "))); 130 } 131 } 132 133 /** 134 * Utility class to guarantee a mixed GC. The class allocates several arrays and 135 * promotes them to the oldgen. After that it tries to provoke mixed GC by 136 * allocating new objects. 137 */ 138 class Provoker { 139 private static void allocateOldObjects( 140 List<byte[]> liveOldObjects, 141 int g1HeapRegionSize, 142 int arraySize) { 143 144 var toUnreachable = new ArrayList<byte[]>(); 145 146 // Allocates buffer and promotes it to the old gen. Mix live and dead old objects. 147 // allocate about two regions of old memory. At least one full old region will guarantee 148 // mixed collection in the future 149 range(0, g1HeapRegionSize/arraySize).forEach(n -> { 150 liveOldObjects.add(new byte[arraySize]); 151 toUnreachable.add(new byte[arraySize]); 152 }); 153 154 // Do two young collections, MaxTenuringThreshold=1 will force promotion. 155 getWhiteBox().youngGC(); 156 getWhiteBox().youngGC(); 157 158 // Check it is promoted & keep alive 159 Asserts.assertTrue(getWhiteBox().isObjectInOldGen(liveOldObjects), "List of the objects is suppose to be in OldGen"); 160 Asserts.assertTrue(getWhiteBox().isObjectInOldGen(toUnreachable), "List of the objects is suppose to be in OldGen"); 161 } 162 163 private static void waitTillCMCFinished(int sleepTime) { 164 while (getWhiteBox().g1InConcurrentMark()) { 165 try {sleep(sleepTime);} catch (Exception e) {} 166 } 167 } 168 169 /** 170 * The necessary condition for guaranteed mixed GC is running in VM with the following flags: 171 * -XX:+UnlockExperimentalVMOptions -XX:MaxTenuringThreshold=1 -Xms{HEAP_SIZE}M 172 * -Xmx{HEAP_SIZE}M -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0 173 * -XX:G1HeapRegionSize={REGION_SIZE}m 174 * 175 * @param provokeSize The size to allocate to provoke the start of a mixed gc (half heap size?) 176 * @param g1HeapRegionSize The size of your regions in bytes 177 */ 178 public static void provokeMixedGC(int g1HeapRegionSize) { 179 final var arraySize = 20_000; 180 var liveOldObjects = new ArrayList<byte[]>(); 181 allocateOldObjects(liveOldObjects, g1HeapRegionSize, arraySize); 182 waitTillCMCFinished(10); 183 getWhiteBox().g1StartConcMarkCycle(); 184 waitTillCMCFinished(10); 185 getWhiteBox().youngGC(); 186 getWhiteBox().youngGC(); 187 188 // check that liveOldObjects still alive 189 assertTrue(getWhiteBox().isObjectInOldGen(liveOldObjects), "List of the objects is suppose to be in OldGen"); 190 } 191 }