8148397: Create new tests for IHOP

Reviewed-by: tschatzl, dfazunen
This commit is contained in:
Michail Chernov 2016-04-01 16:15:37 +03:00
parent 9be0552a61
commit c3469071aa
4 changed files with 787 additions and 0 deletions

View File

@ -0,0 +1,228 @@
/*
* Copyright (c) 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.
*/
/*
* @test TestIHOPErgo
* @bug 8148397
* @summary Test checks that behavior of Adaptive and Static IHOP at concurrent cycle initiation
* @requires vm.gc=="G1" | vm.gc=="null"
* @requires vm.opt.FlightRecorder != true
* @requires vm.opt.ExplicitGCInvokesConcurrent != true
* @library /testlibrary /test/lib /
* @modules java.management
* @build gc.g1.ihop.TestIHOPErgo
* gc.g1.ihop.lib.IhopUtils
* @run driver/timeout=480 gc.g1.ihop.TestIHOPErgo
*/
package gc.g1.ihop;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import jdk.test.lib.OutputAnalyzer;
import jdk.test.lib.ProcessTools;
import gc.g1.ihop.lib.IhopUtils;
/**
* The test starts the AppIHOP multiple times varying settings of MaxHeapSize.
* The test parses GC log from AppIHOP to check:
* - occupancy is not less than threshold for Adaptive and Static IHOP at
* concurrent cycle initiation
* - Adaptive IHOP prediction was started during AppIHOP executing
* - log contains ergonomic messages in log
*/
public class TestIHOPErgo {
// Common GC tune and logging options for test.
private final static String[] COMMON_OPTIONS = {
"-XX:+UnlockExperimentalVMOptions",
"-XX:G1MixedGCLiveThresholdPercent=100",
"-XX:G1HeapWastePercent=0",
"-XX:MaxGCPauseMillis=30000",
"-XX:G1MixedGCCountTarget=1",
"-XX:+UseG1GC",
"-XX:G1HeapRegionSize=1m",
"-XX:+G1UseAdaptiveIHOP",
"-Xlog:gc+ihop=debug,gc+ihop+ergo=debug,gc+ergo=debug",
"-XX:+AlwaysTenure",
"-XX:G1AdaptiveIHOPNumInitialSamples=1",
"-XX:InitiatingHeapOccupancyPercent=30"
};
public static void main(String[] args) throws Throwable {
// heap size MB, sleep time for allocator, true/false for adaptive/static
runTest(64, 0, false);
runTest(64, 100, false);
runTest(128, 100, false);
runTest(256, 50, false);
runTest(512, 30, false);
runTest(64, 50, true);
runTest(128, 200, true);
runTest(256, 100, true);
runTest(512, 50, true);
}
/**
* Runs AppIHOP in separate VM and checks GC log.
*
* @param heapSize heap size
* @param sleepTime sleep time between memory allocations.
* @param isIhopAdaptive true forAdaptive IHOP, false for Static
*
* @throws Throwable
*/
private static void runTest(int heapSize, int sleepTime, boolean isIhopAdaptive) throws Throwable {
System.out.println("IHOP test:");
System.out.println(" MaxHeapSize : " + heapSize);
List<String> options = new ArrayList<>();
Collections.addAll(options,
"-Dheap.size=" + heapSize,
"-Dsleep.time=" + sleepTime,
"-XX:MaxHeapSize=" + heapSize + "M",
"-XX:NewSize=" + heapSize / 8 + "M",
"-XX:MaxNewSize=" + heapSize / 8 + "M",
"-XX:InitialHeapSize=" + heapSize + "M",
"-XX:" + (isIhopAdaptive ? "+" : "-") + "G1UseAdaptiveIHOP"
);
Collections.addAll(options, COMMON_OPTIONS);
options.add(AppIHOP.class.getName());
OutputAnalyzer out = executeTest(options);
// Checks that log contains message which indicates that IHOP prediction is active
if (isIhopAdaptive) {
IhopUtils.checkAdaptiveIHOPWasActivated(out);
}
// Checks that log contains messages which indicates that VM initiates/checks heap occupancy
// and tries to start concurrent cycle.
IhopUtils.checkErgoMessagesExist(out);
// Checks threshold and occupancy values
IhopUtils.checkIhopLogValues(out);
}
private static OutputAnalyzer executeTest(List<String> options) throws Throwable, RuntimeException {
OutputAnalyzer out;
out = ProcessTools.executeTestJvm(options.toArray(new String[options.size()]));
if (out.getExitValue() != 0) {
System.out.println(out.getOutput());
throw new RuntimeException("AppIHOP failed with exit code" + out.getExitValue());
}
return out;
}
/**
* The AppIHOP fills 60% of heap and allocates and frees 30% of existing
* heap 'iterations' times to achieve IHOP activation. To be executed in
* separate VM. Expected properties:
* heap.size - heap size which is used to calculate amount of memory
* to be allocated and freed
* sleep.time - short pause between filling each MB
*/
public static class AppIHOP {
public final static LinkedList<Object> GARBAGE = new LinkedList<>();
private final int ITERATIONS = 10;
private final int OBJECT_SIZE = 100000;
// 60% of the heap will be filled before test cycles.
// 30% of the heap will be filled and freed during test cycle.
private final long HEAP_PREALLOC_PCT = 60;
private final long HEAP_ALLOC_PCT = 30;
private final long HEAP_SIZE;
// Amount of memory to be allocated before iterations start
private final long HEAP_PREALLOC_SIZE;
// Amount of memory to be allocated and freed during iterations
private final long HEAP_ALLOC_SIZE;
private final int SLEEP_TIME;
public static void main(String[] args) throws InterruptedException {
new AppIHOP().start();
}
AppIHOP() {
HEAP_SIZE = Integer.getInteger("heap.size") * 1024 * 1024;
SLEEP_TIME = Integer.getInteger("sleep.time");
HEAP_PREALLOC_SIZE = HEAP_SIZE * HEAP_PREALLOC_PCT / 100;
HEAP_ALLOC_SIZE = HEAP_SIZE * HEAP_ALLOC_PCT / 100;
}
public void start() throws InterruptedException {
fill(HEAP_PREALLOC_SIZE);
fillAndFree(HEAP_ALLOC_SIZE, ITERATIONS);
}
/**
* Fills allocationSize bytes of garbage.
*
* @param allocationSize amount of garbage
*/
private void fill(long allocationSize) {
long allocated = 0;
while (allocated < allocationSize) {
GARBAGE.addFirst(new byte[OBJECT_SIZE]);
allocated += OBJECT_SIZE;
}
}
/**
* Allocates allocationSize bytes of garbage. Performs a short pauses
* during allocation. Frees allocated garbage.
*
* @param allocationSize amount of garbage per iteration
* @param iterations iteration count
*
* @throws InterruptedException
*/
private void fillAndFree(long allocationSize, int iterations) throws InterruptedException {
for (int i = 0; i < iterations; ++i) {
System.out.println("Iteration:" + i);
long allocated = 0;
long counter = 0;
while (allocated < allocationSize) {
GARBAGE.addFirst(new byte[OBJECT_SIZE]);
allocated += OBJECT_SIZE;
counter += OBJECT_SIZE;
if (counter > 1024 * 1024) {
counter = 0;
if (SLEEP_TIME != 0) {
Thread.sleep(SLEEP_TIME);
}
}
}
long removed = 0;
while (removed < allocationSize) {
GARBAGE.removeLast();
removed += OBJECT_SIZE;
}
}
}
}
}

View File

@ -0,0 +1,199 @@
/*
* Copyright (c) 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.
*/
/*
* @test TestIHOPStatic
* @bug 8148397
* @summary Test checks concurrent cycle initiation which depends on IHOP value.
* @requires vm.gc=="G1" | vm.gc=="null"
* @requires vm.opt.FlightRecorder != true
* @requires vm.opt.ExplicitGCInvokesConcurrent != true
* @library /testlibrary /
* @modules java.management
* @build gc.g1.ihop.TestIHOPStatic
* gc.g1.ihop.lib.IhopUtils
* @run driver/timeout=240 gc.g1.ihop.TestIHOPStatic
*/
package gc.g1.ihop;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import jdk.test.lib.OutputAnalyzer;
import jdk.test.lib.ProcessTools;
import jdk.test.lib.Utils;
import gc.g1.ihop.lib.IhopUtils;
/**
* The test starts the AppIHOP multiple times varying setting of MaxHeapSize,
* IHOP and amount of memory to allocate. Then the test parses the GC log from
* the app to check that Concurrent Mark Cycle was initiated only if needed
* and at the right moment, defined by IHOP setting.
*/
public class TestIHOPStatic {
final static long YOUNG_SIZE = 8 * 1024 * 1024;
private final static String[] COMMON_OPTIONS = {
"-XX:+UseG1GC",
"-XX:G1HeapRegionSize=1m",
"-XX:-G1UseAdaptiveIHOP",
"-XX:NewSize=" + YOUNG_SIZE,
"-XX:MaxNewSize=" + YOUNG_SIZE,
"-Xlog:gc+ihop+ergo=debug,gc*=debug"
};
public static void main(String[] args) throws Throwable {
// Test case:
// IHOP value, heap occupancy, heap size, expectation of message
// Test cases for occupancy is greater than IHOP
runTest(30, 35, 64, true);
runTest(50, 55, 256, true);
runTest(60, 65, 64, true);
runTest(70, 75, 512, true);
// Test cases for big difference between occupancy and IHOP
runTest(30, 50, 256, true);
runTest(30, 70, 512, true);
runTest(50, 70, 256, true);
// Test cases for occupancy is less than IHOP
runTest(30, 25, 64, false);
runTest(50, 45, 256, false);
runTest(70, 65, 64, false);
runTest(70, 65, 512, false);
// Test cases for big difference between occupancy and IHOP
runTest(50, 30, 300, false);
runTest(70, 50, 160, false);
// Cases for 0 and 100 IHOP.
runTest(0, 50, 256, true);
runTest(0, 95, 512, true);
runTest(100, 20, 64, false);
runTest(100, 100, 512, false);
}
/**
* Runs the test case.
*
* @param ihop IHOP value
* @param pctToFill heap percentage to be filled
* @param heapSize heap size for test
* @param expectInitiationMessage
* true - concurrent cycle initiation message is expected
* false - message is not expected
*
* @throws Throwable
*/
private static void runTest(int ihop, long pctToFill, long heapSize, boolean expectInitiationMessage) throws Throwable {
System.out.println("");
System.out.println("IHOP test:");
System.out.println(" InitiatingHeapOccupancyPercent : " + ihop);
System.out.println(" Part of heap to fill (percentage) : " + pctToFill);
System.out.println(" MaxHeapSize : " + heapSize);
System.out.println(" Expect for concurrent cycle initiation message : " + expectInitiationMessage);
List<String> options = new ArrayList<>();
Collections.addAll(options, Utils.getTestJavaOpts());
Collections.addAll(options,
"-XX:InitiatingHeapOccupancyPercent=" + ihop,
"-Dmemory.fill=" + (heapSize * 1024 * 1024 * pctToFill / 100),
"-XX:MaxHeapSize=" + heapSize + "M",
"-XX:InitialHeapSize=" + heapSize + "M"
);
Collections.addAll(options, COMMON_OPTIONS);
options.add(AppIHOP.class.getName());
OutputAnalyzer out = ProcessTools.executeTestJvm(options.toArray(new String[options.size()]));
if (out.getExitValue() != 0) {
System.out.println(out.getOutput());
throw new RuntimeException("IhopTest failed with exit code " + out.getExitValue());
}
checkResult(out, expectInitiationMessage);
}
/**
* Checks execution results to ensure that concurrent cycle was initiated or
* was not.
*
* @param out
* @param expectInitiationMessage true - test expects for concurrent cycle initiation.
* false - test does not expect for concurrent cycle initiation
*/
private static void checkResult(OutputAnalyzer out, boolean expectInitiationMessage) {
// Find expected messages
List<String> logItems = IhopUtils.getErgoInitiationMessages(out);
// Concurrent cycle was not initiated but was expected.
if (logItems.isEmpty() && expectInitiationMessage) {
System.out.println(out.getOutput());
throw new RuntimeException("Concurrent cycle was not initiated.");
}
IhopUtils.checkIhopLogValues(out);
}
static class AppIHOP {
/**
* Simple class which fills part of memory and initiates GC.
* To be executed in separate VM.
* Expect next VM properties to be set:
* memory.fill - amount of garbage to be created.
*/
private static final long MEMORY_TO_FILL = Integer.getInteger("memory.fill");
private final static int CHUNK_SIZE = 10000;
public final static ArrayList<Object> STORAGE = new ArrayList<>();
public static void main(String[] args) throws InterruptedException {
// Calculate part of heap to be filled to achieve expected occupancy.
System.out.println("Mem to fill:" + MEMORY_TO_FILL);
if (MEMORY_TO_FILL <= 0) {
throw new RuntimeException("Wrong memory size: " + MEMORY_TO_FILL);
}
try {
createGarbage(MEMORY_TO_FILL);
} catch (OutOfMemoryError oome) {
return;
}
// Concurrent cycle initiation should start at end of Young GC cycle.
// Will fill entire young gen with garbage to guarantee that Young GC was initiated.
try {
createGarbage(TestIHOPStatic.YOUNG_SIZE);
} catch (OutOfMemoryError oome) {
}
}
private static void createGarbage(long memToFill) {
for (long i = 0; i < memToFill / CHUNK_SIZE; i++) {
STORAGE.add(new byte[CHUNK_SIZE]);
}
}
}
}

View File

@ -0,0 +1,144 @@
/*
* Copyright (c) 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 gc.g1.ihop.lib;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.test.lib.OutputAnalyzer;
/**
* Utility class to extract IHOP related information from the GC log.
* The class provides a number of static method to be used from tests.
*/
public class IhopUtils {
// Examples of GC log for IHOP:
// [0.402s][debug][gc,ergo,ihop] GC(9) Do not request concurrent cycle initiation (still doing mixed collections) occupancy: 66060288B allocation request: 0B threshold: 59230757B (88.26) source: end of GC
// [0.466s][debug][gc,ergo,ihop] GC(18) Request concurrent cycle initiation (occupancy higher than threshold) occupancy: 52428800B allocation request: 0B threshold: 0B (0.00) source: end of GC
/**
* Patterns are used for extracting occupancy and threshold from GC log.
*/
private final static Pattern OCCUPANCY = Pattern.compile("occupancy: (\\d+)B");
private final static Pattern THRESHOLD = Pattern.compile("threshold: (\\d+)B");
/**
* Messages related to concurrent cycle initiation.
*/
private final static String CYCLE_INITIATION_MESSAGE = "Request concurrent cycle initiation (occupancy higher than threshold)";
private final static String CYCLE_INITIATION_MESSAGE_FALSE = "Do not request concurrent cycle initiation (still doing mixed collections)";
private final static String ADAPTIVE_IHOP_PREDICTION_ACTIVE_MESSAGE = "prediction active: true";
/**
* Finds strings which contains patterns for finding.
*
* @param outputAnalyzer List of string for IHOP messages extraction
* @param stringsToFind Strings which is checked for matching with OutputAnalyzer content
* @return List of strings which were matched.
*/
private static List<String> findInLog(OutputAnalyzer outputAnalyzer, String... stringsToFind) {
return outputAnalyzer.asLines().stream()
.filter(string -> {
return Stream.of(stringsToFind)
.filter(find -> string.contains(find))
.findAny()
.isPresent();
})
.collect(Collectors.toList());
}
/**
* Checks that memory occupancy is greater or equal to the threshold.
* This methods searches for occupancy and threshold in the GC log corresponding Conc Mark Cycle initiation
* and compare their values.If no CMC initiation happens, does nothing.
* @param outputAnalyzer OutputAnalyzer which contains GC log to be checked
* @throw RuntimeException If check fails
*/
public static void checkIhopLogValues(OutputAnalyzer outputAnalyzer) {
// Concurrent cycle was initiated but was not expected.
// Checks occupancy should be greater than threshold.
List<String> logItems = IhopUtils.getErgoMessages(outputAnalyzer);
logItems.stream()
.forEach(item -> {
long occupancy = IhopUtils.getLongByPattern(item, IhopUtils.OCCUPANCY);
long threshold = IhopUtils.getLongByPattern(item, IhopUtils.THRESHOLD);
if (occupancy < threshold) {
System.out.println(outputAnalyzer.getOutput());
throw new RuntimeException("Concurrent cycle initiation is unexpected. Occupancy (" + occupancy + ") is less then threshold (" + threshold + ")");
}
System.out.printf("Concurrent cycle was initiated with occupancy = %d and threshold = %d%n", occupancy, threshold);
});
}
private static Long getLongByPattern(String line, Pattern pattern) {
Matcher number = pattern.matcher(line);
if (number.find()) {
return Long.parseLong(number.group(1));
}
System.out.println(line);
throw new RuntimeException("Cannot find Long in string.");
}
/**
* Finds concurrent cycle initiation messages.
* @param outputAnalyzer OutputAnalyzer
* @return List with messages which were found.
*/
public static List<String> getErgoInitiationMessages(OutputAnalyzer outputAnalyzer) {
return IhopUtils.findInLog(outputAnalyzer, CYCLE_INITIATION_MESSAGE);
}
/**
* Gets IHOP ergo messages from GC log.
* @param outputAnalyzer
* @return List with found messages
*/
private static List<String> getErgoMessages(OutputAnalyzer outputAnalyzer) {
return IhopUtils.findInLog(outputAnalyzer, CYCLE_INITIATION_MESSAGE, CYCLE_INITIATION_MESSAGE_FALSE);
}
/**
* Checks that GC log contains expected ergonomic messages
* @param outputAnalyzer OutputAnalyer with GC log for checking
* @throws RuntimeException If no IHOP ergo messages were not found
*/
public static void checkErgoMessagesExist(OutputAnalyzer outputAnalyzer) {
String output = outputAnalyzer.getOutput();
if (!(output.contains(CYCLE_INITIATION_MESSAGE) | output.contains(CYCLE_INITIATION_MESSAGE_FALSE))) {
throw new RuntimeException("Cannot find expected IHOP ergonomics messages");
}
}
/**
* Checks that adaptive IHOP was activated
* @param outputAnalyzer OutputAnalyer with GC log for checking
* @throws RuntimeException If IHOP message was not found.
*/
public static void checkAdaptiveIHOPWasActivated(OutputAnalyzer outputAnalyzer) {
outputAnalyzer.shouldContain(ADAPTIVE_IHOP_PREDICTION_ACTIVE_MESSAGE);
}
}

View File

@ -0,0 +1,216 @@
/*
* Copyright (c) 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.
*/
/*
* @test TestStressIHOPMultiThread
* @bug 8148397
* @key stress
* @summary Stress test for IHOP
* @requires vm.gc=="G1" | vm.gc=="null"
* @run main/othervm/timeout=200 -Xmx128m -XX:G1HeapWastePercent=0 -XX:G1MixedGCCountTarget=1
* -XX:+UseG1GC -XX:G1HeapRegionSize=1m -XX:+G1UseAdaptiveIHOP
* -Xlog:gc+ihop=debug,gc+ihop+ergo=debug,gc+ergo=debug:TestStressIHOPMultiThread1.log
* -Dtimeout=2 -DheapUsageMinBound=30 -DheapUsageMaxBound=80
* -Dthreads=2 TestStressIHOPMultiThread
* @run main/othervm/timeout=200 -Xmx256m -XX:G1HeapWastePercent=0 -XX:G1MixedGCCountTarget=1
* -XX:+UseG1GC -XX:G1HeapRegionSize=2m -XX:+G1UseAdaptiveIHOP
* -Xlog:gc+ihop=debug,gc+ihop+ergo=debug,gc+ergo=debug:TestStressIHOPMultiThread2.log
* -Dtimeout=2 -DheapUsageMinBound=60 -DheapUsageMaxBound=90
* -Dthreads=3 TestStressIHOPMultiThread
* @run main/othervm/timeout=200 -Xmx256m -XX:G1HeapWastePercent=0 -XX:G1MixedGCCountTarget=1
* -XX:+UseG1GC -XX:G1HeapRegionSize=4m -XX:-G1UseAdaptiveIHOP
* -Xlog:gc+ihop=debug,gc+ihop+ergo=debug,gc+ergo=debug:TestStressIHOPMultiThread3.log
* -Dtimeout=2 -DheapUsageMinBound=40 -DheapUsageMaxBound=90
* -Dthreads=5 TestStressIHOPMultiThread
* @run main/othervm/timeout=200 -Xmx128m -XX:G1HeapWastePercent=0 -XX:G1MixedGCCountTarget=1
* -XX:+UseG1GC -XX:G1HeapRegionSize=8m -XX:+G1UseAdaptiveIHOP
* -Xlog:gc+ihop=debug,gc+ihop+ergo=debug,gc+ergo=debug:TestStressIHOPMultiThread4.log
* -Dtimeout=2 -DheapUsageMinBound=20 -DheapUsageMaxBound=90
* -Dthreads=10 TestStressIHOPMultiThread
* @run main/othervm/timeout=200 -Xmx512m -XX:G1HeapWastePercent=0 -XX:G1MixedGCCountTarget=1
* -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:+G1UseAdaptiveIHOP
* -Xlog:gc+ihop=debug,gc+ihop+ergo=debug,gc+ergo=debug:TestStressIHOPMultiThread5.log
* -Dtimeout=2 -DheapUsageMinBound=20 -DheapUsageMaxBound=90
* -Dthreads=17 TestStressIHOPMultiThread
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Stress test for Adaptive IHOP. Starts a number of threads that fill and free
* specified amount of memory. Tests work with enabled IHOP logging.
*
*/
public class TestStressIHOPMultiThread {
public final static List<Object> GARBAGE = new LinkedList<>();
private final long HEAP_SIZE;
// Amount of memory to be allocated before iterations start
private final long HEAP_PREALLOC_SIZE;
// Amount of memory to be allocated and freed during iterations
private final long HEAP_ALLOC_SIZE;
private final int CHUNK_SIZE = 100000;
private final int TIMEOUT;
private final int THREADS;
private final int HEAP_LOW_BOUND;
private final int HEAP_HIGH_BOUND;
private volatile boolean running = true;
private final List<AllocationThread> threads;
public static void main(String[] args) throws InterruptedException {
new TestStressIHOPMultiThread().start();
}
TestStressIHOPMultiThread() {
TIMEOUT = Integer.getInteger("timeout") * 60;
THREADS = Integer.getInteger("threads");
HEAP_LOW_BOUND = Integer.getInteger("heapUsageMinBound");
HEAP_HIGH_BOUND = Integer.getInteger("heapUsageMaxBound");
HEAP_SIZE = Runtime.getRuntime().maxMemory();
HEAP_PREALLOC_SIZE = HEAP_SIZE * HEAP_LOW_BOUND / 100;
HEAP_ALLOC_SIZE = HEAP_SIZE * (HEAP_HIGH_BOUND - HEAP_LOW_BOUND) / 100;
threads = new ArrayList<>(THREADS);
}
public void start() throws InterruptedException {
fill();
createThreads();
waitForStress();
stressDone();
waitForFinish();
}
/**
* Fills HEAP_PREALLOC_SIZE bytes of garbage.
*/
private void fill() {
long allocated = 0;
while (allocated < HEAP_PREALLOC_SIZE) {
GARBAGE.add(new byte[CHUNK_SIZE]);
allocated += CHUNK_SIZE;
}
}
/**
* Creates a number of threads which will fill and free amount of memory.
*/
private void createThreads() {
for (int i = 0; i < THREADS; ++i) {
System.out.println("Create thread " + i);
AllocationThread thread =new TestStressIHOPMultiThread.AllocationThread(i, HEAP_ALLOC_SIZE / THREADS);
// Put reference to thread garbage into common garbage for avoiding possible optimization.
GARBAGE.add(thread.getList());
threads.add(thread);
}
threads.forEach(t -> t.start());
}
/**
* Wait each thread for finishing
*/
private void waitForFinish() {
threads.forEach(thread -> {
thread.silentJoin();
});
}
private boolean isRunning() {
return running;
}
private void stressDone() {
running = false;
}
private void waitForStress() throws InterruptedException {
Thread.sleep(TIMEOUT * 1000);
}
private class AllocationThread extends Thread {
private final List<Object> garbage;
private final long amountOfGarbage;
private final int threadId;
public AllocationThread(int id, long amount) {
super("Thread " + id);
threadId = id;
amountOfGarbage = amount;
garbage = new LinkedList<>();
}
/**
* Returns list of garbage.
* @return List with thread garbage.
*/
public List<Object> getList(){
return garbage;
}
@Override
public void run() {
System.out.println("Start the thread " + threadId);
while (TestStressIHOPMultiThread.this.isRunning()) {
allocate(amountOfGarbage);
free();
}
}
private void silentJoin() {
System.out.println("Join the thread " + threadId);
try {
join();
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
/**
* Allocates thread local garbage
*/
private void allocate(long amount) {
long allocated = 0;
while (allocated < amount && TestStressIHOPMultiThread.this.isRunning()) {
garbage.add(new byte[CHUNK_SIZE]);
allocated += CHUNK_SIZE;
}
}
/**
* Frees thread local garbage
*/
private void free() {
garbage.clear();
}
}
}