Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstat/utils/GarbageProducer.java
41159 views
/*1* Copyright (c) 2017, 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*/22package utils;2324import java.lang.management.ManagementFactory;25import java.lang.management.MemoryMXBean;26import java.lang.management.MemoryPoolMXBean;27import java.lang.management.MemoryUsage;28import java.util.ArrayList;29import java.util.List;3031/**32* This is an class used to allocate specified amount of metaspace and heap.33*/34public class GarbageProducer {3536// Uses fixed small objects to avoid Humongous objects allocation with G1 GC.37private static final int MEMORY_CHUNK = 2048;3839public static List<Object> allocatedMetaspace;40public static List<Object> allocatedMemory;4142private final MemoryMXBean memoryMXBean;43private final float targetMemoryUsagePercent;44private final long targetMemoryUsage;4546/**47* @param targetMemoryUsagePercent how many percent of metaspace and heap to48* allocate49*/50public GarbageProducer(float targetMemoryUsagePercent) {51memoryMXBean = ManagementFactory.getMemoryMXBean();52this.targetMemoryUsagePercent = targetMemoryUsagePercent;53targetMemoryUsage = (long) (memoryMXBean.getHeapMemoryUsage().getMax() * targetMemoryUsagePercent);54}5556/**57* Allocates heap and metaspace upon exit targetMemoryUsagePercent percents58* of heap and metaspace have been consumed.59*/60public void allocateMetaspaceAndHeap() {61// Metaspace should be filled before Java Heap to prevent unexpected OOME62// in the Java Heap while filling Metaspace63allocatedMetaspace = eatMetaspace(targetMemoryUsagePercent);64allocatedMemory = allocateGarbage(targetMemoryUsage);65}6667private List<Object> eatMetaspace(float targetUsage) {68List<Object> list = new ArrayList<>();69MemoryPoolMXBean metaspacePool = getMatchedMemoryPool(".*Metaspace.*");70float currentUsage;71GeneratedClassProducer gp = new GeneratedClassProducer();72do {73try {74list.add(gp.create(0));75} catch (OutOfMemoryError oome) {76list = null;77throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace.");78}79MemoryUsage memoryUsage = metaspacePool.getUsage();80currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax());81} while (currentUsage < targetUsage);82return list;83}8485private MemoryPoolMXBean getMatchedMemoryPool(String patternPoolName) {86return ManagementFactory.getMemoryPoolMXBeans().stream()87.filter(bean -> bean.getName().matches(patternPoolName))88.findFirst()89.orElseThrow(() -> new RuntimeException("Cannot find '" + patternPoolName + "' memory pool."));90}9192private List<Object> allocateGarbage(long targetMemoryUsage) {93List<Object> list = new ArrayList<>();94do {95try {96list.add(new byte[MEMORY_CHUNK]);97} catch (OutOfMemoryError e) {98list = null;99throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "'");100}101} while (memoryMXBean.getHeapMemoryUsage().getUsed() < targetMemoryUsage);102return list;103}104105/**106* Returns allocation rate for old gen based on appropriate MemoryPoolMXBean107* memory usage.108*109* @return allocation rate110*/111public float getOldGenAllocationRatio() {112MemoryPoolMXBean oldGenBean = getMatchedMemoryPool(".*Old.*|.*Tenured.*");113MemoryUsage usage = oldGenBean.getUsage();114System.out.format("Memory usage for %1s.\n", oldGenBean.getName());115System.out.format("Used: %1d\n", usage.getUsed());116System.out.format("Commited: %1d\n", usage.getCommitted());117System.out.format("Max: %1d\n", usage.getMax());118return ((float) usage.getUsed()) / usage.getCommitted();119}120}121122123