1 /* 2 * Copyright (c) 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 /* 25 * @test TestOldGenCollectionUsage.java 26 * @bug 8195115 27 * @summary G1 Old Gen's CollectionUsage.used is zero after mixed GC which is incorrect 28 * @key gc 29 * @requires vm.gc.G1 30 * @requires vm.opt.MaxGCPauseMillis == "null" 31 * @library /test/lib 32 * @modules java.base/jdk.internal.misc 33 * @modules java.management 34 * @build sun.hotspot.WhiteBox 35 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 36 * @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -verbose:gc -XX:SurvivorRatio=1 -Xmx12m -Xms12m -XX:MaxTenuringThreshold=1 -XX:InitiatingHeapOccupancyPercent=100 -XX:-G1UseAdaptiveIHOP -XX:G1MixedGCCountTarget=4 -XX:MaxGCPauseMillis=30000 -XX:G1HeapRegionSize=1m -XX:G1HeapWastePercent=0 -XX:G1MixedGCLiveThresholdPercent=100 TestOldGenCollectionUsage 37 */ 38 39 import jdk.test.lib.Asserts; 40 import sun.hotspot.WhiteBox; 41 42 import java.util.ArrayList; 43 import java.util.List; 44 import java.util.Collections; 45 46 import java.lang.management.*; 47 48 // 8195115 says that for the "G1 Old Gen" MemoryPool, CollectionUsage.used 49 // is zero for G1 after a mixed collection, which is incorrect. 50 51 public class TestOldGenCollectionUsage { 52 53 private String poolName = "G1 Old Gen"; 54 private String collectorName = "G1 Young Generation"; 55 56 public static void main(String [] args) throws Exception { 57 TestOldGenCollectionUsage t = new TestOldGenCollectionUsage(); 58 t.run(); 59 } 60 61 public TestOldGenCollectionUsage() { 62 System.out.println("Monitor G1 Old Gen pool with G1 Young Generation collector."); 63 } 64 65 public void run() { 66 // Find memory pool and collector 67 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 68 MemoryPoolMXBean pool = null; 69 boolean foundPool = false; 70 for (int i = 0; i < pools.size(); i++) { 71 pool = pools.get(i); 72 String name = pool.getName(); 73 if (name.contains(poolName)) { 74 System.out.println("Found pool: " + name); 75 foundPool = true; 76 break; 77 } 78 } 79 if (!foundPool) { 80 throw new RuntimeException(poolName + " not found, test with -XX:+UseG1GC"); 81 } 82 83 List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans(); 84 GarbageCollectorMXBean collector = null; 85 boolean foundCollector = false; 86 for (int i = 0; i < collectors.size(); i++) { 87 collector = collectors.get(i); 88 String name = collector.getName(); 89 if (name.contains(collectorName)) { 90 System.out.println("Found collector: " + name); 91 foundCollector = true; 92 break; 93 } 94 } 95 if (!foundCollector) { 96 throw new RuntimeException(collectorName + " not found, test with -XX:+UseG1GC"); 97 } 98 99 MixedGCProvoker gcProvoker = new MixedGCProvoker(); 100 gcProvoker.allocateOldObjects(); 101 102 // Verify no non-zero result was stored 103 long usage = pool.getCollectionUsage().getUsed(); 104 System.out.println(poolName + ": usage after GC = " + usage); 105 if (usage > 0) { 106 throw new RuntimeException("Premature mixed collections(s)"); 107 } 108 109 // Verify that collections were done 110 long collectionCount = collector.getCollectionCount(); 111 System.out.println(collectorName + ": collection count = " 112 + collectionCount); 113 long collectionTime = collector.getCollectionTime(); 114 System.out.println(collectorName + ": collection time = " 115 + collectionTime); 116 if (collectionCount <= 0) { 117 throw new RuntimeException("Collection count <= 0"); 118 } 119 if (collectionTime <= 0) { 120 throw new RuntimeException("Collector has not run"); 121 } 122 123 gcProvoker.provokeMixedGC(); 124 125 usage = pool.getCollectionUsage().getUsed(); 126 System.out.println(poolName + ": usage after GC = " + usage); 127 if (usage <= 0) { 128 throw new RuntimeException(poolName + " found with zero usage"); 129 } 130 131 long newCollectionCount = collector.getCollectionCount(); 132 System.out.println(collectorName + ": collection count = " 133 + newCollectionCount); 134 long newCollectionTime = collector.getCollectionTime(); 135 System.out.println(collectorName + ": collection time = " 136 + newCollectionTime); 137 if (newCollectionCount <= collectionCount) { 138 throw new RuntimeException("No new collection"); 139 } 140 if (newCollectionTime <= collectionTime) { 141 throw new RuntimeException("Collector has not run some more"); 142 } 143 144 System.out.println("Test passed."); 145 } 146 147 /** 148 * Utility class to guarantee a mixed GC. The class allocates several arrays and 149 * promotes them to the oldgen. After that it tries to provoke mixed GC by 150 * allocating new objects. 151 * 152 * The necessary condition for guaranteed mixed GC is running MixedGCProvoker is 153 * running in VM with the following flags: -XX:MaxTenuringThreshold=1 -Xms12M 154 * -Xmx12M -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0 155 * -XX:G1HeapRegionSize=1m 156 */ 157 public class MixedGCProvoker { 158 private final WhiteBox WB = WhiteBox.getWhiteBox(); 159 private final List<byte[]> liveOldObjects = new ArrayList<>(); 160 private final List<byte[]> newObjects = new ArrayList<>(); 161 162 public static final int ALLOCATION_SIZE = 20000; 163 public static final int ALLOCATION_COUNT = 15; 164 165 public void allocateOldObjects() { 166 List<byte[]> deadOldObjects = new ArrayList<>(); 167 // Allocates buffer and promotes it to the old gen. Mix live and dead old 168 // objects 169 for (int i = 0; i < ALLOCATION_COUNT; ++i) { 170 liveOldObjects.add(new byte[ALLOCATION_SIZE * 5]); 171 deadOldObjects.add(new byte[ALLOCATION_SIZE * 5]); 172 } 173 174 // Do two young collections, MaxTenuringThreshold=1 will force promotion. 175 // G1HeapRegionSize=1m guarantees that old gen regions will be filled. 176 WB.youngGC(); 177 WB.youngGC(); 178 // Check it is promoted & keep alive 179 Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects), 180 "List of the objects is suppose to be in OldGen"); 181 Asserts.assertTrue(WB.isObjectInOldGen(deadOldObjects), 182 "List of the objects is suppose to be in OldGen"); 183 } 184 185 /** 186 * Waits until Concurent Mark Cycle finishes 187 * @param wb Whitebox instance 188 * @param sleepTime sleep time 189 */ 190 private void waitTillCMCFinished(int sleepTime) { 191 while (WB.g1InConcurrentMark()) { 192 if (sleepTime > -1) { 193 try { 194 Thread.sleep(sleepTime); 195 } catch (InterruptedException e) { 196 System.out.println("Got InterruptedException while waiting for ConcMarkCycle to finish"); 197 } 198 } 199 } 200 } 201 202 public void provokeMixedGC() { 203 waitTillCMCFinished(0); 204 WB.g1StartConcMarkCycle(); 205 waitTillCMCFinished(0); 206 WB.youngGC(); 207 208 System.out.println("Allocating new objects to provoke mixed GC"); 209 // Provoke a mixed collection. G1MixedGCLiveThresholdPercent=100 210 // guarantees that full old gen regions will be included. 211 for (int i = 0; i < (ALLOCATION_COUNT * 20); i++) { 212 newObjects.add(new byte[ALLOCATION_SIZE]); 213 } 214 // check that liveOldObjects still alive 215 Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects), 216 "List of the objects is suppose to be in OldGen"); 217 } 218 219 } 220 221 }