test/gc/metaspace/G1AddMetaspaceDependency.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 7994
04ff2f6cd0eb
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

aoqi@0 1 /*
jwilhelm@7785 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test G1AddMetaspaceDependency
aoqi@0 26 * @bug 8010196
jwilhelm@7785 27 * @requires vm.gc=="G1" | vm.gc=="null"
aoqi@0 28 * @summary Checks that we don't get locking problems when adding metaspace dependencies with the G1 update buffer monitor
aoqi@0 29 * @run main/othervm -XX:+UseG1GC -XX:G1UpdateBufferSize=1 G1AddMetaspaceDependency
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.io.InputStream;
aoqi@0 33
aoqi@0 34 public class G1AddMetaspaceDependency {
aoqi@0 35
aoqi@0 36 static byte[] getClassBytes(String name) {
aoqi@0 37 byte[] b = null;
aoqi@0 38 try (InputStream is = ClassLoader.getSystemResourceAsStream(name)) {
aoqi@0 39 byte[] tmp = new byte[is.available()];
aoqi@0 40 is.read(tmp);
aoqi@0 41 b = tmp;
aoqi@0 42 } finally {
aoqi@0 43 if (b == null) {
aoqi@0 44 throw new RuntimeException("Unable to load class file");
aoqi@0 45 }
aoqi@0 46 return b;
aoqi@0 47 }
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 static final String a_name = G1AddMetaspaceDependency.class.getName() + "$A";
aoqi@0 51 static final String b_name = G1AddMetaspaceDependency.class.getName() + "$B";
aoqi@0 52
aoqi@0 53 public static void main(String... args) throws Exception {
aoqi@0 54 final byte[] a_bytes = getClassBytes(a_name + ".class");
aoqi@0 55 final byte[] b_bytes = getClassBytes(b_name + ".class");
aoqi@0 56
aoqi@0 57 for (int i = 0; i < 1000; i += 1) {
aoqi@0 58 runTest(a_bytes, b_bytes);
aoqi@0 59 }
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 static class Loader extends ClassLoader {
aoqi@0 63 private final String myClass;
aoqi@0 64 private final byte[] myBytes;
aoqi@0 65 private final String friendClass;
aoqi@0 66 private final ClassLoader friendLoader;
aoqi@0 67
aoqi@0 68 Loader(String myClass, byte[] myBytes,
aoqi@0 69 String friendClass, ClassLoader friendLoader) {
aoqi@0 70 this.myClass = myClass;
aoqi@0 71 this.myBytes = myBytes;
aoqi@0 72 this.friendClass = friendClass;
aoqi@0 73 this.friendLoader = friendLoader;
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 Loader(String myClass, byte[] myBytes) {
aoqi@0 77 this(myClass, myBytes, null, null);
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 @Override
aoqi@0 81 public Class<?> loadClass(String name) throws ClassNotFoundException {
aoqi@0 82 Class<?> c = findLoadedClass(name);
aoqi@0 83 if (c != null) {
aoqi@0 84 return c;
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 if (name.equals(friendClass)) {
aoqi@0 88 return friendLoader.loadClass(name);
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 if (name.equals(myClass)) {
aoqi@0 92 c = defineClass(name, myBytes, 0, myBytes.length);
aoqi@0 93 resolveClass(c);
aoqi@0 94 return c;
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 return findSystemClass(name);
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 private static void runTest(final byte[] a_bytes, final byte[] b_bytes) throws Exception {
aoqi@0 103 Loader a_loader = new Loader(a_name, a_bytes);
aoqi@0 104 Loader b_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 105 Loader c_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 106 Loader d_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 107 Loader e_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 108 Loader f_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 109 Loader g_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 110
aoqi@0 111 Class<?> c;
aoqi@0 112 c = b_loader.loadClass(b_name);
aoqi@0 113 c = c_loader.loadClass(b_name);
aoqi@0 114 c = d_loader.loadClass(b_name);
aoqi@0 115 c = e_loader.loadClass(b_name);
aoqi@0 116 c = f_loader.loadClass(b_name);
aoqi@0 117 c = g_loader.loadClass(b_name);
aoqi@0 118 }
aoqi@0 119 public class A {
aoqi@0 120 }
aoqi@0 121 class B extends A {
aoqi@0 122 }
aoqi@0 123 }

mercurial