test/gc/metaspace/G1AddMetaspaceDependency.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5815
77a774ab3cf0
parent 0
f90c822e73f8
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, 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
aoqi@0 27 * @summary Checks that we don't get locking problems when adding metaspace dependencies with the G1 update buffer monitor
aoqi@0 28 * @run main/othervm -XX:+UseG1GC -XX:G1UpdateBufferSize=1 G1AddMetaspaceDependency
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 import java.io.InputStream;
aoqi@0 32
aoqi@0 33 public class G1AddMetaspaceDependency {
aoqi@0 34
aoqi@0 35 static byte[] getClassBytes(String name) {
aoqi@0 36 byte[] b = null;
aoqi@0 37 try (InputStream is = ClassLoader.getSystemResourceAsStream(name)) {
aoqi@0 38 byte[] tmp = new byte[is.available()];
aoqi@0 39 is.read(tmp);
aoqi@0 40 b = tmp;
aoqi@0 41 } finally {
aoqi@0 42 if (b == null) {
aoqi@0 43 throw new RuntimeException("Unable to load class file");
aoqi@0 44 }
aoqi@0 45 return b;
aoqi@0 46 }
aoqi@0 47 }
aoqi@0 48
aoqi@0 49 static final String a_name = G1AddMetaspaceDependency.class.getName() + "$A";
aoqi@0 50 static final String b_name = G1AddMetaspaceDependency.class.getName() + "$B";
aoqi@0 51
aoqi@0 52 public static void main(String... args) throws Exception {
aoqi@0 53 final byte[] a_bytes = getClassBytes(a_name + ".class");
aoqi@0 54 final byte[] b_bytes = getClassBytes(b_name + ".class");
aoqi@0 55
aoqi@0 56 for (int i = 0; i < 1000; i += 1) {
aoqi@0 57 runTest(a_bytes, b_bytes);
aoqi@0 58 }
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 static class Loader extends ClassLoader {
aoqi@0 62 private final String myClass;
aoqi@0 63 private final byte[] myBytes;
aoqi@0 64 private final String friendClass;
aoqi@0 65 private final ClassLoader friendLoader;
aoqi@0 66
aoqi@0 67 Loader(String myClass, byte[] myBytes,
aoqi@0 68 String friendClass, ClassLoader friendLoader) {
aoqi@0 69 this.myClass = myClass;
aoqi@0 70 this.myBytes = myBytes;
aoqi@0 71 this.friendClass = friendClass;
aoqi@0 72 this.friendLoader = friendLoader;
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 Loader(String myClass, byte[] myBytes) {
aoqi@0 76 this(myClass, myBytes, null, null);
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 @Override
aoqi@0 80 public Class<?> loadClass(String name) throws ClassNotFoundException {
aoqi@0 81 Class<?> c = findLoadedClass(name);
aoqi@0 82 if (c != null) {
aoqi@0 83 return c;
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 if (name.equals(friendClass)) {
aoqi@0 87 return friendLoader.loadClass(name);
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 if (name.equals(myClass)) {
aoqi@0 91 c = defineClass(name, myBytes, 0, myBytes.length);
aoqi@0 92 resolveClass(c);
aoqi@0 93 return c;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 return findSystemClass(name);
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 private static void runTest(final byte[] a_bytes, final byte[] b_bytes) throws Exception {
aoqi@0 102 Loader a_loader = new Loader(a_name, a_bytes);
aoqi@0 103 Loader b_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 104 Loader c_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 105 Loader d_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 106 Loader e_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 107 Loader f_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 108 Loader g_loader = new Loader(b_name, b_bytes, a_name, a_loader);
aoqi@0 109
aoqi@0 110 Class<?> c;
aoqi@0 111 c = b_loader.loadClass(b_name);
aoqi@0 112 c = c_loader.loadClass(b_name);
aoqi@0 113 c = d_loader.loadClass(b_name);
aoqi@0 114 c = e_loader.loadClass(b_name);
aoqi@0 115 c = f_loader.loadClass(b_name);
aoqi@0 116 c = g_loader.loadClass(b_name);
aoqi@0 117 }
aoqi@0 118 public class A {
aoqi@0 119 }
aoqi@0 120 class B extends A {
aoqi@0 121 }
aoqi@0 122 }

mercurial