Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/g1/mixedgc/TestOldGenCollectionUsage.java
32285 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestOldGenCollectionUsage.java25* @bug 819511526* @summary G1 Old Gen's CollectionUsage.used is zero after mixed GC which is incorrect27* @key gc28* @requires vm.gc=="G1" | vm.gc=="null"29* @library /testlibrary /testlibrary/whitebox30* @build ClassFileInstaller com.oracle.java.testlibrary.* sun.hotspot.WhiteBox TestOldGenCollectionUsage31* @run main ClassFileInstaller sun.hotspot.WhiteBox32* sun.hotspot.WhiteBox$WhiteBoxPermission33* @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions34* -XX:+WhiteBoxAPI -verbose:gc -XX:SurvivorRatio=1 -Xmx14m -Xms14m -XX:MaxTenuringThreshold=135* -XX:InitiatingHeapOccupancyPercent=100 -XX:G1MixedGCCountTarget=4 -XX:MaxGCPauseMillis=3000036* -XX:G1HeapRegionSize=1m -XX:G1HeapWastePercent=0 -XX:G1MixedGCLiveThresholdPercent=10037* TestOldGenCollectionUsage38*/3940import com.oracle.java.testlibrary.Asserts;41import sun.hotspot.WhiteBox;4243import java.util.ArrayList;44import java.util.List;45import java.util.Collections;4647import java.lang.management.*;4849// 8195115 says that for the "G1 Old Gen" MemoryPool, CollectionUsage.used50// is zero for G1 after a mixed collection, which is incorrect.5152public class TestOldGenCollectionUsage {5354private String poolName = "G1 Old Gen";55private String collectorName = "G1 Young Generation";5657public static void main(String [] args) throws Exception {58TestOldGenCollectionUsage t = new TestOldGenCollectionUsage();59t.run();60}6162public TestOldGenCollectionUsage() {63System.out.println("Monitor G1 Old Gen pool with G1 Young Generation collector.");64}6566public void run() {67// Find memory pool and collector68List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();69MemoryPoolMXBean pool = null;70boolean foundPool = false;71for (int i = 0; i < pools.size(); i++) {72pool = pools.get(i);73String name = pool.getName();74if (name.contains(poolName)) {75System.out.println("Found pool: " + name);76foundPool = true;77break;78}79}80if (!foundPool) {81throw new RuntimeException(poolName + " not found, test with -XX:+UseG1GC");82}8384List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();85GarbageCollectorMXBean collector = null;86boolean foundCollector = false;87for (int i = 0; i < collectors.size(); i++) {88collector = collectors.get(i);89String name = collector.getName();90if (name.contains(collectorName)) {91System.out.println("Found collector: " + name);92foundCollector = true;93break;94}95}96if (!foundCollector) {97throw new RuntimeException(collectorName + " not found, test with -XX:+UseG1GC");98}99100MixedGCProvoker gcProvoker = new MixedGCProvoker();101gcProvoker.allocateOldObjects();102103// Verify no non-zero result was stored104long usage = pool.getCollectionUsage().getUsed();105System.out.println(poolName + ": usage after GC = " + usage);106if (usage > 0) {107throw new RuntimeException("Premature mixed collections(s)");108}109110// Verify that collections were done111long collectionCount = collector.getCollectionCount();112System.out.println(collectorName + ": collection count = "113+ collectionCount);114long collectionTime = collector.getCollectionTime();115System.out.println(collectorName + ": collection time = "116+ collectionTime);117if (collectionCount <= 0) {118throw new RuntimeException("Collection count <= 0");119}120if (collectionTime <= 0) {121throw new RuntimeException("Collector has not run");122}123124gcProvoker.provokeMixedGC();125126usage = pool.getCollectionUsage().getUsed();127System.out.println(poolName + ": usage after GC = " + usage);128if (usage <= 0) {129throw new RuntimeException(poolName + " found with zero usage");130}131132long newCollectionCount = collector.getCollectionCount();133System.out.println(collectorName + ": collection count = "134+ newCollectionCount);135long newCollectionTime = collector.getCollectionTime();136System.out.println(collectorName + ": collection time = "137+ newCollectionTime);138if (newCollectionCount <= collectionCount) {139throw new RuntimeException("No new collection");140}141if (newCollectionTime <= collectionTime) {142throw new RuntimeException("Collector has not run some more");143}144145System.out.println("Test passed.");146}147148/**149* Utility class to guarantee a mixed GC. The class allocates several arrays and150* promotes them to the oldgen. After that it tries to provoke mixed GC by151* allocating new objects.152*153* The necessary condition for guaranteed mixed GC is running MixedGCProvoker is154* running in VM with the following flags: -XX:MaxTenuringThreshold=1 -Xms12M155* -Xmx12M -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0156* -XX:G1HeapRegionSize=1m157*/158public class MixedGCProvoker {159private final WhiteBox WB = WhiteBox.getWhiteBox();160private final List<byte[]> liveOldObjects = new ArrayList<>();161private final List<byte[]> newObjects = new ArrayList<>();162163public static final int ALLOCATION_SIZE = 20000;164public static final int ALLOCATION_COUNT = 15;165166public void allocateOldObjects() {167List<byte[]> deadOldObjects = new ArrayList<>();168// Allocates buffer and promotes it to the old gen. Mix live and dead old169// objects170for (int i = 0; i < ALLOCATION_COUNT; ++i) {171liveOldObjects.add(new byte[ALLOCATION_SIZE * 5]);172deadOldObjects.add(new byte[ALLOCATION_SIZE * 5]);173}174175// Do two young collections, MaxTenuringThreshold=1 will force promotion.176// G1HeapRegionSize=1m guarantees that old gen regions will be filled.177WB.youngGC();178WB.youngGC();179// Check it is promoted & keep alive180Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects),181"List of the objects is suppose to be in OldGen");182Asserts.assertTrue(WB.isObjectInOldGen(deadOldObjects),183"List of the objects is suppose to be in OldGen");184}185186/**187* Waits until Concurent Mark Cycle finishes188* @param wb Whitebox instance189* @param sleepTime sleep time190*/191private void waitTillCMCFinished(int sleepTime) {192while (WB.g1InConcurrentMark()) {193if (sleepTime > -1) {194try {195Thread.sleep(sleepTime);196} catch (InterruptedException e) {197System.out.println("Got InterruptedException while waiting for ConcMarkCycle to finish");198}199}200}201}202203public void provokeMixedGC() {204waitTillCMCFinished(0);205WB.g1StartConcMarkCycle();206waitTillCMCFinished(0);207WB.youngGC();208209System.out.println("Allocating new objects to provoke mixed GC");210// Provoke a mixed collection. G1MixedGCLiveThresholdPercent=100211// guarantees that full old gen regions will be included.212for (int i = 0; i < (ALLOCATION_COUNT * 20); i++) {213try {214newObjects.add(new byte[ALLOCATION_SIZE]);215} catch (OutOfMemoryError e) {216newObjects.clear();217WB.youngGC();218WB.youngGC();219System.out.println("OutOfMemoryError is reported, stop allocating new objects");220break;221}222}223// check that liveOldObjects still alive224Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects),225"List of the objects is suppose to be in OldGen");226}227228}229230}231232233