Path: blob/master/test/hotspot/jtreg/gc/g1/TestShrinkAuxiliaryData.java
40942 views
/*1* Copyright (c) 2014, 2020, 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*/2223package gc.g1;2425import jdk.test.lib.Asserts;26import jdk.test.lib.Platform;27import jdk.test.lib.Utils;28import jdk.test.lib.process.ProcessTools;29import jdk.test.lib.process.OutputAnalyzer;30import jtreg.SkippedException;3132import java.io.IOException;33import java.lang.management.ManagementFactory;34import java.lang.management.MemoryUsage;35import java.text.DecimalFormat;36import java.text.DecimalFormatSymbols;37import java.util.ArrayList;38import java.util.Collections;39import java.util.LinkedList;40import java.util.List;41import java.util.Random;42import jdk.internal.misc.Unsafe; // for ADDRESS_SIZE43import sun.hotspot.WhiteBox;4445public class TestShrinkAuxiliaryData {46private static final Random RNG = Utils.getRandomInstance();4748private static final int REGION_SIZE = 1024 * 1024;4950private final static String[] initialOpts = new String[]{51"-XX:NewSize=16m",52"-XX:MinHeapFreeRatio=10",53"-XX:MaxHeapFreeRatio=11",54"-XX:+UseG1GC",55"-XX:G1HeapRegionSize=" + REGION_SIZE,56"-XX:-ExplicitGCInvokesConcurrent",57"-Xlog:gc=debug",58"-XX:+UnlockDiagnosticVMOptions",59"-XX:+WhiteBoxAPI",60"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",61"-Xbootclasspath/a:.",62};6364private final int hotCardTableSize;6566protected TestShrinkAuxiliaryData(int hotCardTableSize) {67this.hotCardTableSize = hotCardTableSize;68}6970protected void test() throws Exception {71ArrayList<String> vmOpts = new ArrayList<>();72Collections.addAll(vmOpts, initialOpts);7374int maxCacheSize = Math.max(0, Math.min(31, getMaxCacheSize()));75if (maxCacheSize < hotCardTableSize) {76throw new SkippedException(String.format(77"Skiping test for %d cache size due max cache size %d",78hotCardTableSize, maxCacheSize));79}8081printTestInfo(maxCacheSize);8283vmOpts.add("-XX:G1ConcRSLogCacheSize=" + hotCardTableSize);8485// for 32 bits ObjectAlignmentInBytes is not a option86if (Platform.is32bit()) {87ArrayList<String> vmOptsWithoutAlign = new ArrayList<>(vmOpts);88vmOptsWithoutAlign.add(ShrinkAuxiliaryDataTest.class.getName());89performTest(vmOptsWithoutAlign);90return;91}9293for (int alignment = 3; alignment <= 8; alignment++) {94ArrayList<String> vmOptsWithAlign = new ArrayList<>(vmOpts);95vmOptsWithAlign.add("-XX:ObjectAlignmentInBytes="96+ (int) Math.pow(2, alignment));97vmOptsWithAlign.add(ShrinkAuxiliaryDataTest.class.getName());9899performTest(vmOptsWithAlign);100}101}102103private void performTest(List<String> opts) throws Exception {104ProcessBuilder pb = ProcessTools.createTestJvm(opts);105106OutputAnalyzer output = new OutputAnalyzer(pb.start());107System.out.println(output.getStdout());108System.err.println(output.getStderr());109output.shouldHaveExitValue(0);110}111112private void printTestInfo(int maxCacheSize) {113114DecimalFormat grouped = new DecimalFormat("000,000");115DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();116formatSymbols.setGroupingSeparator(' ');117grouped.setDecimalFormatSymbols(formatSymbols);118119System.out.format(120"Test will use %s bytes of memory of %s available%n"121+ "Available memory is %s with %d bytes pointer size - can save %s pointers%n"122+ "Max cache size: 2^%d = %s elements%n",123grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),124grouped.format(Runtime.getRuntime().maxMemory()),125grouped.format(Runtime.getRuntime().maxMemory()126- ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),127Unsafe.ADDRESS_SIZE,128grouped.format((Runtime.getRuntime().freeMemory()129- ShrinkAuxiliaryDataTest.getMemoryUsedByTest())130/ Unsafe.ADDRESS_SIZE),131maxCacheSize,132grouped.format((int) Math.pow(2, maxCacheSize))133);134}135136/**137* Detects maximum possible size of G1ConcRSLogCacheSize available for138* current process based on maximum available process memory size139*140* @return power of two141*/142private static int getMaxCacheSize() {143long availableMemory = Runtime.getRuntime().freeMemory()144- ShrinkAuxiliaryDataTest.getMemoryUsedByTest() - 1l;145if (availableMemory <= 0) {146return 0;147}148149long availablePointersCount = availableMemory / Unsafe.ADDRESS_SIZE;150return (63 - (int) Long.numberOfLeadingZeros(availablePointersCount));151}152153static class ShrinkAuxiliaryDataTest {154155public static void main(String[] args) throws Exception {156157ShrinkAuxiliaryDataTest testCase = new ShrinkAuxiliaryDataTest();158159if (!testCase.checkEnvApplicability()) {160return;161}162163testCase.test();164}165166/**167* Checks is this environment suitable to run this test168* - memory is enough to decommit (page size is not big)169* - RSet cache size is not too big170*171* @return true if test could run, false if test should be skipped172*/173protected boolean checkEnvApplicability() {174175int pageSize = WhiteBox.getWhiteBox().getVMPageSize();176System.out.println( "Page size = " + pageSize177+ " region size = " + REGION_SIZE178+ " aux data ~= " + (REGION_SIZE * 3 / 100));179// If auxdata size will be less than page size it wouldn't decommit.180// Auxiliary data size is about ~3.6% of heap size.181if (pageSize >= REGION_SIZE * 3 / 100) {182System.out.format("Skipping test for too large page size = %d",183pageSize184);185return false;186}187188if (REGION_SIZE * REGIONS_TO_ALLOCATE > Runtime.getRuntime().maxMemory()) {189System.out.format("Skipping test for too low available memory. "190+ "Need %d, available %d",191REGION_SIZE * REGIONS_TO_ALLOCATE,192Runtime.getRuntime().maxMemory()193);194return false;195}196197return true;198}199200class GarbageObject {201202private final List<byte[]> payload = new ArrayList<>();203private final List<GarbageObject> ref = new LinkedList<>();204205public GarbageObject(int size) {206payload.add(new byte[size]);207}208209public void addRef(GarbageObject g) {210ref.add(g);211}212213public void mutate() {214if (!payload.isEmpty() && payload.get(0).length > 0) {215payload.get(0)[0] = (byte) (RNG.nextDouble() * Byte.MAX_VALUE);216}217}218}219220private final List<GarbageObject> garbage = new ArrayList<>();221222public void test() throws Exception {223224MemoryUsage muFull, muFree, muAuxDataFull, muAuxDataFree;225float auxFull, auxFree;226227allocate();228link();229mutate();230231muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();232long numUsedRegions = WhiteBox.getWhiteBox().g1NumMaxRegions()233- WhiteBox.getWhiteBox().g1NumFreeRegions();234muAuxDataFull = WhiteBox.getWhiteBox().g1AuxiliaryMemoryUsage();235auxFull = (float)muAuxDataFull.getUsed() / numUsedRegions;236237System.out.format("Full aux data ratio= %f, regions max= %d, used= %d\n",238auxFull, WhiteBox.getWhiteBox().g1NumMaxRegions(), numUsedRegions239);240241deallocate();242System.gc();243244if (WhiteBox.getWhiteBox().g1HasRegionsToUncommit()) {245System.out.println("Waiting for concurrent uncommit to complete");246do {247Thread.sleep(1000);248} while(WhiteBox.getWhiteBox().g1HasRegionsToUncommit());249System.out.println("Concurrent uncommit done");250}251252muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();253muAuxDataFree = WhiteBox.getWhiteBox().g1AuxiliaryMemoryUsage();254255numUsedRegions = WhiteBox.getWhiteBox().g1NumMaxRegions()256- WhiteBox.getWhiteBox().g1NumFreeRegions();257auxFree = (float)muAuxDataFree.getUsed() / numUsedRegions;258259System.out.format("Free aux data ratio= %f, regions max= %d, used= %d\n",260auxFree, WhiteBox.getWhiteBox().g1NumMaxRegions(), numUsedRegions261);262263Asserts.assertLessThanOrEqual(muFree.getCommitted(), muFull.getCommitted(),264String.format("heap decommit failed - full > free: %d > %d",265muFree.getCommitted(), muFull.getCommitted()266)267);268269System.out.format("State used committed\n");270System.out.format("Full aux data: %10d %10d\n", muAuxDataFull.getUsed(), muAuxDataFull.getCommitted());271System.out.format("Free aux data: %10d %10d\n", muAuxDataFree.getUsed(), muAuxDataFree.getCommitted());272273// if decommited check that aux data has same ratio274if (muFree.getCommitted() < muFull.getCommitted()) {275Asserts.assertLessThanOrEqual(auxFree, auxFull,276String.format("auxiliary data decommit failed - full > free: %f > %f",277auxFree, auxFull278)279);280}281}282283private void allocate() {284for (int r = 0; r < REGIONS_TO_ALLOCATE; r++) {285for (int i = 0; i < NUM_OBJECTS_PER_REGION; i++) {286GarbageObject g = new GarbageObject(REGION_SIZE287/ NUM_OBJECTS_PER_REGION);288garbage.add(g);289}290}291}292293/**294* Iterate through all allocated objects, and link to objects in another295* regions296*/297private void link() {298for (int ig = 0; ig < garbage.size(); ig++) {299int regionNumber = ig / NUM_OBJECTS_PER_REGION;300301for (int i = 0; i < NUM_LINKS; i++) {302int regionToLink;303do {304regionToLink = (int) (RNG.nextDouble() * REGIONS_TO_ALLOCATE);305} while (regionToLink == regionNumber);306307// get random garbage object from random region308garbage.get(ig).addRef(garbage.get(regionToLink309* NUM_OBJECTS_PER_REGION + (int) (RNG.nextDouble()310* NUM_OBJECTS_PER_REGION)));311}312}313}314315private void mutate() {316for (int ig = 0; ig < garbage.size(); ig++) {317garbage.get(ig).mutate();318}319}320321private void deallocate() {322garbage.clear();323System.gc();324}325326static long getMemoryUsedByTest() {327return REGIONS_TO_ALLOCATE * REGION_SIZE;328}329330private static final int REGIONS_TO_ALLOCATE = 100;331private static final int NUM_OBJECTS_PER_REGION = 10;332private static final int NUM_LINKS = 20; // how many links create for each object333}334}335336337