Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/SysDictTest.java
41161 views
/*1* Copyright (c) 2010, 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 nsk.sysdict.share;2425import java.io.File;26import java.net.MalformedURLException;27import java.net.URL;28import java.net.URLClassLoader;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.List;32import nsk.share.ClassUnloader;33import nsk.share.TestFailure;34import nsk.share.gc.ThreadedGCTest;35import nsk.share.gc.gp.GarbageUtils;36import nsk.share.test.ExecutionController;37import nsk.share.test.LocalRandom;3839/**40* This is the base class for btree & chain tests. It is a standard GCThreaded Test.41*/42public abstract class SysDictTest extends ThreadedGCTest {4344static String PACKAGE_PREFIX = "nsk.sysdict.share.";45// Should we additionally use ClassUnloader.unload to stress GC46private boolean isHeapStressed = false;47// Should we use one JarLoader or a lot of them48private boolean useSingleLoader = true;49// Should we use fats.jar with little amount large classes or not50boolean useFats = false;51URL[] jars;5253protected void parseArgs(String args[]) {54for (int i = 0; i < args.length; i++) {55if (args[i].equals("-stressHeap")) {56isHeapStressed = true;57}58if (args[i].equals("-useFatClass")) {59useFats = true;60}61if (args[i].equals("-useSingleLoader")) {62this.useSingleLoader = false;63}64// jar path is in useal classpath format65if (args[i].equals("-jarpath")) {66String[] files = args[i + 1].split(File.pathSeparator);67jars = new URL[files.length];68for (int j = 0; j < files.length; j++) {69try {70jars[j] = new File(files[j]).toURI().toURL();71} catch (MalformedURLException e) {72throw new TestFailure(e);73}74}75}76}77}7879// each time we create a new classloader80protected ClassLoader createJarLoader() {81return new URLClassLoader(jars);82}8384// The btree and chain tests differs by loaders and classes85// let define them in subclasses86abstract ClassLoader[] createLoaders();8788abstract String[] getClassNames();8990ClassLoader[] createClassLoadersInternal() {91if (!useSingleLoader) {92return createLoaders();93} else {94ClassLoader[] single = new ClassLoader[1];95single[0] = createJarLoader();96return single;97}98}99volatile ClassLoader[] currentClassLoaders;100101class Worker implements Runnable {102103private ClassLoader loader;104private String[] names;105private ExecutionController stresser;106int index;107public String tmp;108109public Worker(int number, String[] classnames) {110111this.index = number;112this.names = new String[classnames.length];113List<String> listNames = new ArrayList<String>(classnames.length);114listNames.addAll(Arrays.asList(classnames));115for (int i = 0; i < classnames.length; i++) {116int idx1 = LocalRandom.nextInt(listNames.size());117this.names[i] = listNames.remove(idx1);118}119}120121@Override122public void run() {123if (stresser == null) {124stresser = getExecutionController();125}126127// only first thread update all classloaders128// do not care about synchronization129if (index == 0) {130try {131currentClassLoaders = createClassLoadersInternal();132} catch (OutOfMemoryError oome) {133// skip iterations until all loaders will be unloaded134Thread.yield();135return;136}137}138for (int i = 0; i < names.length; i++) {139try {140String name = names[i];141if (!stresser.continueExecution()) {142return;143}144// just check if loader was updated145loader = currentClassLoaders[index];146Class clz = Class.forName(name, true, loader);147// set name into public variable just to be sure148// that class is loaded149tmp = clz.getName();150} catch (ClassNotFoundException cnfe) {151throw new TestFailure(cnfe);152} catch (OutOfMemoryError oome) {153// just ignore154// we do not check memory leaks in PermGen in this tests155} catch (StackOverflowError soe) {156// just ignore, chains could be too large157// StackOverflowError could be in some sparcs158}159}160if (isHeapStressed) {161GarbageUtils.eatMemory(stresser, 50, 1024, 0);162}163}164}165166@Override167protected Runnable createRunnable(int i) {168currentClassLoaders = createClassLoadersInternal();169return new Worker(i % currentClassLoaders.length, getClassNames());170}171}172173174