Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/metaspace/G1AddMetaspaceDependency.java
32284 views
/*1* Copyright (c) 2013, 2014, 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*/2223/*24* @test G1AddMetaspaceDependency25* @bug 801019626* @requires vm.gc=="G1" | vm.gc=="null"27* @summary Checks that we don't get locking problems when adding metaspace dependencies with the G1 update buffer monitor28* @run main/othervm -XX:+UseG1GC -XX:G1UpdateBufferSize=1 G1AddMetaspaceDependency29*/3031import java.io.InputStream;3233public class G1AddMetaspaceDependency {3435static byte[] getClassBytes(String name) {36byte[] b = null;37try (InputStream is = ClassLoader.getSystemResourceAsStream(name)) {38byte[] tmp = new byte[is.available()];39is.read(tmp);40b = tmp;41} finally {42if (b == null) {43throw new RuntimeException("Unable to load class file");44}45return b;46}47}4849static final String a_name = G1AddMetaspaceDependency.class.getName() + "$A";50static final String b_name = G1AddMetaspaceDependency.class.getName() + "$B";5152public static void main(String... args) throws Exception {53final byte[] a_bytes = getClassBytes(a_name + ".class");54final byte[] b_bytes = getClassBytes(b_name + ".class");5556for (int i = 0; i < 1000; i += 1) {57runTest(a_bytes, b_bytes);58}59}6061static class Loader extends ClassLoader {62private final String myClass;63private final byte[] myBytes;64private final String friendClass;65private final ClassLoader friendLoader;6667Loader(String myClass, byte[] myBytes,68String friendClass, ClassLoader friendLoader) {69this.myClass = myClass;70this.myBytes = myBytes;71this.friendClass = friendClass;72this.friendLoader = friendLoader;73}7475Loader(String myClass, byte[] myBytes) {76this(myClass, myBytes, null, null);77}7879@Override80public Class<?> loadClass(String name) throws ClassNotFoundException {81Class<?> c = findLoadedClass(name);82if (c != null) {83return c;84}8586if (name.equals(friendClass)) {87return friendLoader.loadClass(name);88}8990if (name.equals(myClass)) {91c = defineClass(name, myBytes, 0, myBytes.length);92resolveClass(c);93return c;94}9596return findSystemClass(name);97}9899}100101private static void runTest(final byte[] a_bytes, final byte[] b_bytes) throws Exception {102Loader a_loader = new Loader(a_name, a_bytes);103Loader b_loader = new Loader(b_name, b_bytes, a_name, a_loader);104Loader c_loader = new Loader(b_name, b_bytes, a_name, a_loader);105Loader d_loader = new Loader(b_name, b_bytes, a_name, a_loader);106Loader e_loader = new Loader(b_name, b_bytes, a_name, a_loader);107Loader f_loader = new Loader(b_name, b_bytes, a_name, a_loader);108Loader g_loader = new Loader(b_name, b_bytes, a_name, a_loader);109110Class<?> c;111c = b_loader.loadClass(b_name);112c = c_loader.loadClass(b_name);113c = d_loader.loadClass(b_name);114c = e_loader.loadClass(b_name);115c = f_loader.loadClass(b_name);116c = g_loader.loadClass(b_name);117}118public class A {119}120class B extends A {121}122}123124125