--- old/test/serviceability/tmtools/jstat/GcCauseTest02.java 2017-01-13 14:21:30.264899444 +0300 +++ new/test/serviceability/tmtools/jstat/GcCauseTest02.java 2017-01-13 14:21:30.136899443 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,7 +31,7 @@ * @modules java.base/jdk.internal.misc * @library /test/lib * @library ../share - * @run main/othervm -XX:+UsePerfData -XX:InitialHeapSize=128M -XX:MaxHeapSize=128M -XX:MaxMetaspaceSize=128M GcCauseTest02 + * @run main/othervm -XX:+UsePerfData -XX:NewSize=4m -XX:InitialHeapSize=128M -XX:MaxHeapSize=128M -XX:MaxMetaspaceSize=128M GcCauseTest02 */ import utils.*; @@ -41,6 +41,8 @@ public static void main(String[] args) throws Exception { + GarbageProducer gp = new GarbageProducer(targetMemoryUsagePercent); + // We will be running "jstat -gc" tool JstatGcCauseTool jstatGcTool = new JstatGcCauseTool(ProcessHandle.current().getPid()); @@ -48,10 +50,8 @@ JstatGcCauseResults measurement1 = jstatGcTool.measure(); measurement1.assertConsistency(); - GcProvoker gcProvoker = new GcProvoker(); - // Eat metaspace and heap then run the tool again and get the results asserting that they are reasonable - gcProvoker.allocateAvailableMetaspaceAndHeap(targetMemoryUsagePercent); + gp.allocateMetaspaceAndHeap(); // Collect garbage. Also update VM statistics System.gc(); JstatGcCauseResults measurement2 = jstatGcTool.measure(); --- old/test/serviceability/tmtools/jstat/GcTest02.java 2017-01-13 14:21:30.740899447 +0300 +++ new/test/serviceability/tmtools/jstat/GcTest02.java 2017-01-13 14:21:30.616899446 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,7 +32,7 @@ * @modules java.base/jdk.internal.misc * @library /test/lib * @library ../share - * @run main/othervm -XX:+UsePerfData -XX:InitialHeapSize=128M -XX:MaxHeapSize=128M -XX:MaxMetaspaceSize=128M GcTest02 + * @run main/othervm -XX:+UsePerfData -XX:NewSize=4m -XX:InitialHeapSize=128M -XX:MaxHeapSize=128M -XX:MaxMetaspaceSize=128M -Xlog:gc+heap=debug,gc=debug,gc+ergo=debug GcTest02 */ public class GcTest02 { @@ -41,6 +41,8 @@ public static void main(String[] args) throws Exception { + GarbageProducer gp = new GarbageProducer(targetMemoryUsagePercent); + // We will be running "jstat -gc" tool JstatGcTool jstatGcTool = new JstatGcTool(ProcessHandle.current().getPid()); @@ -48,10 +50,8 @@ JstatGcResults measurement1 = jstatGcTool.measure(); measurement1.assertConsistency(); - GcProvoker gcProvoker = new GcProvoker(); - // Eat metaspace and heap then run the tool again and get the results asserting that they are reasonable - gcProvoker.allocateAvailableMetaspaceAndHeap(targetMemoryUsagePercent); + gp.allocateMetaspaceAndHeap(); // Collect garbage. Also updates VM statistics System.gc(); JstatGcResults measurement2 = jstatGcTool.measure(); --- old/test/serviceability/tmtools/jstat/utils/GcProvoker.java 2017-01-13 14:21:31.172899450 +0300 +++ new/test/serviceability/tmtools/jstat/utils/GcProvoker.java 2017-01-13 14:21:31.044899449 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,9 +22,6 @@ */ package utils; -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryPoolMXBean; -import java.lang.management.MemoryUsage; import java.util.ArrayList; import java.util.List; @@ -37,10 +34,6 @@ // Uses fixed small objects to avoid Humongous objects allocation in G1 public static final int MEMORY_CHUNK = 2048; - public static final float ALLOCATION_TOLERANCE = 0.05f; - - public static List allocatedMetaspace; - public static List allocatedMemory; private final Runtime runtime; @@ -61,21 +54,6 @@ return list; } - private List allocateAvailableHeap(float targetUsage) { - // Calculates size of free memory after allocation with small tolerance. - long minFreeMemory = (long) ((1.0 - (targetUsage + ALLOCATION_TOLERANCE)) * runtime.maxMemory()); - List list = new ArrayList<>(); - do { - try { - list.add(new byte[MEMORY_CHUNK]); - } catch (OutOfMemoryError e) { - list = null; - throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "' while eating " + targetUsage + " of heap memory."); - } - } while (runtime.freeMemory() > minFreeMemory); - return list; - } - /** * This method provokes a GC */ @@ -93,65 +71,7 @@ } } - /** - * Allocates heap and metaspace upon exit not less than targetMemoryUsagePercent percents - * of heap and metaspace have been consumed. - * - * @param targetMemoryUsagePercent how many percent of heap and metaspace to - * allocate - */ - - public void allocateMetaspaceAndHeap(float targetMemoryUsagePercent) { - // Metaspace should be filled before Java Heap to prevent unexpected OOME - // in the Java Heap while filling Metaspace - allocatedMetaspace = eatMetaspace(targetMemoryUsagePercent); - allocatedMemory = allocateHeap(targetMemoryUsagePercent); - } - - /** - * Allocates heap and metaspace upon exit targetMemoryUsagePercent percents - * of heap and metaspace have been consumed. - * - * @param targetMemoryUsagePercent how many percent of heap and metaspace to - * allocate - */ - public void allocateAvailableMetaspaceAndHeap(float targetMemoryUsagePercent) { - // Metaspace should be filled before Java Heap to prevent unexpected OOME - // in the Java Heap while filling Metaspace - allocatedMetaspace = eatMetaspace(targetMemoryUsagePercent); - allocatedMemory = allocateAvailableHeap(targetMemoryUsagePercent); - } - - private List eatMetaspace(float targetUsage) { - List list = new ArrayList<>(); - final String metaspacePoolName = "Metaspace"; - MemoryPoolMXBean metaspacePool = null; - for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { - if (pool.getName().contains(metaspacePoolName)) { - metaspacePool = pool; - break; - } - } - if (metaspacePool == null) { - throw new RuntimeException("MXBean for Metaspace pool wasn't found"); - } - float currentUsage; - GeneratedClassProducer gp = new GeneratedClassProducer(); - do { - try { - list.add(gp.create(0)); - } catch (OutOfMemoryError oome) { - list = null; - throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace."); - } - MemoryUsage memoryUsage = metaspacePool.getUsage(); - currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax()); - } while (currentUsage < targetUsage); - return list; - } - public GcProvoker() { runtime = Runtime.getRuntime(); } - } --- /dev/null 2017-01-10 14:43:04.278213149 +0300 +++ new/test/serviceability/tmtools/jstat/utils/GarbageProducer.java 2017-01-13 14:21:31.468899452 +0300 @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package utils; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryPoolMXBean; +import java.lang.management.MemoryUsage; +import java.util.ArrayList; +import java.util.List; + +/** + * This is an class used to allocate specified amount of metaspace and heap. + * Object of this class should be created before any garbage collection cycles because GC can + * shrink heap and Runtime.maxMemory() might report unexpected value. + */ +public class GarbageProducer{ + + // Uses fixed small objects to avoid Humongous objects allocation with G1 GC. + public static final int MEMORY_CHUNK = 2048; + + public static List allocatedMetaspace; + public static List allocatedMemory; + + private final Runtime runtime; + private final float targetMemoryUsagePercent; + private final long garbageToAllocate; + + /** + * @param targetMemoryUsagePercent how many percent of metaspace and heap to allocate + */ + public GarbageProducer(float targetMemoryUsagePercent) { + this.targetMemoryUsagePercent = targetMemoryUsagePercent; + runtime = Runtime.getRuntime(); + // Calculate amount of garbage to occupate targetMemoryUsagePercent of heap + garbageToAllocate = runtime.freeMemory() + - (long) ((1.0 - targetMemoryUsagePercent) * runtime.maxMemory()); + } + + /** + * Allocates heap and metaspace upon exit targetMemoryUsagePercent percents + * of heap and metaspace have been consumed. + */ + public void allocateMetaspaceAndHeap() { + // Metaspace should be filled before Java Heap to prevent unexpected OOME + // in the Java Heap while filling Metaspace + allocatedMetaspace = eatMetaspace(targetMemoryUsagePercent); + allocatedMemory = allocateGarbage(garbageToAllocate); + } + + private List eatMetaspace(float targetUsage) { + List list = new ArrayList<>(); + final String metaspacePoolName = "Metaspace"; + MemoryPoolMXBean metaspacePool = null; + for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { + if (pool.getName().contains(metaspacePoolName)) { + metaspacePool = pool; + break; + } + } + if (metaspacePool == null) { + throw new RuntimeException("MXBean for Metaspace pool wasn't found"); + } + float currentUsage; + GeneratedClassProducer gp = new GeneratedClassProducer(); + do { + try { + list.add(gp.create(0)); + } catch (OutOfMemoryError oome) { + list = null; + throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace."); + } + MemoryUsage memoryUsage = metaspacePool.getUsage(); + currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax()); + } while (currentUsage < targetUsage); + return list; + } + + private List allocateGarbage(long garbageToAllocate) { + long allocated = 0l; + List list = new ArrayList<>(); + do { + try { + list.add(new byte[MEMORY_CHUNK]); + allocated += MEMORY_CHUNK; + } catch (OutOfMemoryError e) { + list = null; + throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "'. Allocated " + allocated + " bytes."); + } + } while (allocated < garbageToAllocate); + return list; + } +} --- old/test/serviceability/tmtools/jstat/utils/GcProvokerImpl.java 2017-01-13 14:21:32.004899455 +0300 +++ /dev/null 2017-01-10 14:43:04.278213149 +0300 @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package utils; - -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryPoolMXBean; -import java.lang.management.MemoryUsage; -import java.util.ArrayList; -import java.util.List; - -/** - * - * Utilities to provoke GC in various ways - */ -public class GcProvokerImpl implements GcProvoker { - - private static List eatenMetaspace; - private static List eatenMemory; - - static List eatHeapMemory(float targetUsage) { - long maxMemory = Runtime.getRuntime().maxMemory(); - // uses fixed small objects to avoid Humongous objects allocation in G1 - int memoryChunk = 2048; - List list = new ArrayList<>(); - long used = 0; - long target = (long) (maxMemory * targetUsage); - while (used < target) { - try { - list.add(new byte[memoryChunk]); - used += memoryChunk; - } catch (OutOfMemoryError e) { - list = null; - throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "' while eating " + targetUsage + " of heap memory."); - } - } - return list; - } - - @Override - public void provokeGc() { - for (int i = 0; i < 3; i++) { - long edenSize = Pools.getEdenCommittedSize(); - long heapSize = Pools.getHeapCommittedSize(); - float targetPercent = ((float) edenSize) / (heapSize); - if ((targetPercent < 0) || (targetPercent > 1.0)) { - throw new RuntimeException("Error in the percent calculation" + " (eden size: " + edenSize + ", heap size: " + heapSize + ", calculated eden percent: " + targetPercent + ")"); - } - eatHeapMemory(targetPercent); - eatHeapMemory(targetPercent); - System.gc(); - } - } - - @Override - public void eatMetaspaceAndHeap(float targetMemoryUsagePercent) { - // Metaspace should be filled before Java Heap to prevent unexpected OOME - // in the Java Heap while filling Metaspace - eatenMetaspace = eatMetaspace(targetMemoryUsagePercent); - eatenMemory = eatHeapMemory(targetMemoryUsagePercent); - } - - private static List eatMetaspace(float targetUsage) { - List list = new ArrayList<>(); - final String metaspacePoolName = "Metaspace"; - MemoryPoolMXBean metaspacePool = null; - for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { - if (pool.getName().contains(metaspacePoolName)) { - metaspacePool = pool; - break; - } - } - if (metaspacePool == null) { - throw new RuntimeException("MXBean for Metaspace pool wasn't found"); - } - float currentUsage; - GeneratedClassProducer gp = new GeneratedClassProducer(); - do { - try { - list.add(gp.create(0)); - } catch (OutOfMemoryError oome) { - list = null; - throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace."); - } - MemoryUsage memoryUsage = metaspacePool.getUsage(); - currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax()); - } while (currentUsage < targetUsage); - return list; - } - - public GcProvokerImpl() { - } - -}