Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/UnloadingTest.java
41159 views
/*1* Copyright (c) 2014, 2018, 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.unloading;2425import java.lang.Thread.UncaughtExceptionHandler;26import java.lang.management.*;27import java.util.Collection;28import java.util.List;29import java.util.Random;30import java.util.concurrent.atomic.AtomicLong;3132import gc.g1.unloading.check.Assertion;33import gc.g1.unloading.check.AssertionContainer;34import gc.g1.unloading.check.ClassAssertion;35import gc.g1.unloading.configuration.*;36import gc.g1.unloading.loading.*;37import nsk.share.gc.GCTestBase;38import nsk.share.test.ExecutionController;39import nsk.share.test.Stresser;40import nsk.share.test.Tests;4142import jtreg.SkippedException;4344/**45* This class contains main method. It's entry point for all configurations.46*47*/48public class UnloadingTest extends GCTestBase {4950private static String[] args;5152private TestConfiguration configuration;5354private AssertionContainer assertionContainer = new AssertionContainer();5556private Random random;5758private static final String classNamePrefix = "ClassAbc_";5960private static final long DELAY = 300;6162private static AtomicLong systemGcCallsCounter = new AtomicLong(0);6364public static void main(String[] args) {65UnloadingTest.args = args;66Tests.runTest(new UnloadingTest(), args);67}6869@Override70public void run() {71configuration = TestConfiguration.createTestConfiguration(args);7273checkIfG1Used();74checkFlags();7576ExecutionController stresser = new Stresser(args);77stresser.start(1);7879Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {80@Override81public void uncaughtException(Thread t, Throwable e) {82System.out.println("Throwable \"" + e + "\" in thread " + t.getName() + ", id=" + t.getId());83e.printStackTrace();84try {85checkGCCounters();86} catch (Throwable thr) {87thr.printStackTrace();88}89System.exit(2);90}91});9293random = new Random(runParams.getSeed());94ClassLoadingHelper classLoadingHelper = new ClassLoadingHelper(stresser, random.nextLong(), configuration);9596int classesCounter = 0;97while (stresser.continueExecution()) {98Collection<Assertion> assertions = null;99String className = classNamePrefix + (classesCounter++);100101try {102Thread.sleep(DELAY);103} catch (InterruptedException | IllegalArgumentException e) {104throw new RuntimeException("Something went wrong in ClassLoadingHelper", e);105}106107if (random.nextBoolean()) {108assertions = classLoadingHelper.loadClassThatGonnaDie(className);109} else {110assertions = classLoadingHelper.loadClassThatGonnaLive(className);111}112113System.gc();114long systemGCCalls = systemGcCallsCounter.incrementAndGet();115116assertionContainer.enqueue(assertions, systemGCCalls);117118check(assertionContainer.getElder(systemGCCalls - configuration.getNumberOfGCsBeforeCheck()));119120if (configuration.getNumberOfChecksLimit() >= 0 &&121ClassAssertion.getCounterOfCheckedAlive() >= configuration.getNumberOfChecksLimit() &&122ClassAssertion.getCounterOfCheckedUnloaded() >= configuration.getNumberOfChecksLimit()) {123System.out.println("Exiting because numberOfChecksLimit exceeded.");124stresser.finish();125break;126}127}128129System.out.println("ClassAssertion.getCounterOfCheckedAlive() = " + ClassAssertion.getCounterOfCheckedAlive());130System.out.println("ClassAssertion.getCounterOfCheckedUnloaded() = " + ClassAssertion.getCounterOfCheckedUnloaded());131checkGCCounters();132if (System.getProperty("FailTestIfNothingChecked") != null) {133if (ClassAssertion.getCounterOfCheckedAlive() == 0 || ClassAssertion.getCounterOfCheckedUnloaded() == 0) {134throw new RuntimeException("Test was useless. Smthng not checked: " + ClassAssertion.getCounterOfCheckedAlive() + " " +135ClassAssertion.getCounterOfCheckedUnloaded());136}137}138}139140private void check(Collection<Assertion> assertions) {141if (assertions.isEmpty()) {142return;143}144for (Assertion assertion : assertions) {145assertion.check();146assertion.cleanup();147}148}149150private static void checkGCCounters() {151// System.out.println("WhiteBox.getWhiteBox().g1GetTotalCollections() = \t" + WhiteBox.getWhiteBox().g1GetTotalCollections());152// System.out.println("WhiteBox.getWhiteBox().g1GetTotalFullCollections() = \t" + WhiteBox.getWhiteBox().g1GetTotalFullCollections());153GarbageCollectorMXBean oldGenBean = null;154for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {155System.out.println("bean.getName() = \t\"" + bean.getName() + "\", bean.getCollectionCount() = \t" + bean.getCollectionCount());156if (bean.getName().contains("Old")) {157oldGenBean = bean;158}159}160// if (WhiteBox.getWhiteBox().g1GetTotalFullCollections() != 0 || (oldGenBean != null && oldGenBean.getCollectionCount() != 0)) {161if (oldGenBean != null && oldGenBean.getCollectionCount() != 0) {162throw new RuntimeException("Full gc happened. Test was useless.");163}164}165166private void checkIfG1Used() {167for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {168if (!bean.getName().contains("G1")) {169throw new SkippedException("This test was created to cover G1 class unloading feature. It should be ran with -XX:+UseG1GC");170}171}172}173174private void checkFlags() {175RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();176List<String> arguments = runtimeMxBean.getInputArguments();177for (String argument : arguments) {178if (argument.contains("ExplicitGCInvokesConcurrent")) {179return;180}181}182throw new RuntimeException("This test supposed to be ran with -XX:+ExplicitGCInvokesConcurrent flag");183}184185}186187188