test/gc/metaspace/G1AddMetaspaceDependency.java

changeset 4903
ba42fd5e00e6
child 5815
77a774ab3cf0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/gc/metaspace/G1AddMetaspaceDependency.java	Wed Apr 10 13:27:35 2013 +0200
     1.3 @@ -0,0 +1,123 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test G1AddMetaspaceDependency
    1.29 + * @bug 8010196
    1.30 + * @summary Checks that we don't get locking problems when adding metaspace dependencies with the G1 update buffer monitor
    1.31 + * @run main/othervm -XX:+UseG1GC -XX:G1UpdateBufferSize=1 G1AddMetaspaceDependency
    1.32 + */
    1.33 +
    1.34 +import java.io.InputStream;
    1.35 +
    1.36 +public class G1AddMetaspaceDependency {
    1.37 +
    1.38 +  static byte[] getClassBytes(String name) {
    1.39 +    byte[] b = null;
    1.40 +    try (InputStream is = ClassLoader.getSystemResourceAsStream(name)) {
    1.41 +      byte[] tmp = new byte[is.available()];
    1.42 +      is.read(tmp);
    1.43 +      b = tmp;
    1.44 +    } finally {
    1.45 +      if (b == null) {
    1.46 +        throw new RuntimeException("Unable to load class file");
    1.47 +      }
    1.48 +      return b;
    1.49 +    }
    1.50 +  }
    1.51 +
    1.52 +  static final String a_name = G1AddMetaspaceDependency.class.getName() + "$A";
    1.53 +  static final String b_name = G1AddMetaspaceDependency.class.getName() + "$B";
    1.54 +
    1.55 +  public static void main(String... args) throws Exception {
    1.56 +    final byte[] a_bytes = getClassBytes(a_name + ".class");
    1.57 +    final byte[] b_bytes = getClassBytes(b_name + ".class");
    1.58 +
    1.59 +    for (int i = 0; i < 1000; i += 1) {
    1.60 +      runTest(a_bytes, b_bytes);
    1.61 +    }
    1.62 +  }
    1.63 +
    1.64 +  static class Loader extends ClassLoader {
    1.65 +    private final String myClass;
    1.66 +    private final byte[] myBytes;
    1.67 +    private final String friendClass;
    1.68 +    private final ClassLoader friendLoader;
    1.69 +
    1.70 +    Loader(String myClass, byte[] myBytes,
    1.71 +           String friendClass, ClassLoader friendLoader) {
    1.72 +      this.myClass = myClass;
    1.73 +      this.myBytes = myBytes;
    1.74 +      this.friendClass = friendClass;
    1.75 +      this.friendLoader = friendLoader;
    1.76 +    }
    1.77 +
    1.78 +    Loader(String myClass, byte[] myBytes) {
    1.79 +      this(myClass, myBytes, null, null);
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public Class<?> loadClass(String name) throws ClassNotFoundException {
    1.84 +      Class<?> c = findLoadedClass(name);
    1.85 +      if (c != null) {
    1.86 +        return c;
    1.87 +      }
    1.88 +
    1.89 +      if (name.equals(friendClass)) {
    1.90 +        return friendLoader.loadClass(name);
    1.91 +      }
    1.92 +
    1.93 +      if (name.equals(myClass)) {
    1.94 +        c = defineClass(name, myBytes, 0, myBytes.length);
    1.95 +        resolveClass(c);
    1.96 +        return c;
    1.97 +      }
    1.98 +
    1.99 +      return findSystemClass(name);
   1.100 +    }
   1.101 +
   1.102 +  }
   1.103 +
   1.104 +  private static void runTest(final byte[] a_bytes, final byte[] b_bytes) throws Exception {
   1.105 +    Loader a_loader = new Loader(a_name, a_bytes);
   1.106 +    Loader b_loader = new Loader(b_name, b_bytes, a_name, a_loader);
   1.107 +    Loader c_loader = new Loader(b_name, b_bytes, a_name, a_loader);
   1.108 +    Loader d_loader = new Loader(b_name, b_bytes, a_name, a_loader);
   1.109 +    Loader e_loader = new Loader(b_name, b_bytes, a_name, a_loader);
   1.110 +    Loader f_loader = new Loader(b_name, b_bytes, a_name, a_loader);
   1.111 +    Loader g_loader = new Loader(b_name, b_bytes, a_name, a_loader);
   1.112 +
   1.113 +    byte[] b = new byte[20 * 2 << 20];
   1.114 +    Class<?> c;
   1.115 +    c = b_loader.loadClass(b_name);
   1.116 +    c = c_loader.loadClass(b_name);
   1.117 +    c = d_loader.loadClass(b_name);
   1.118 +    c = e_loader.loadClass(b_name);
   1.119 +    c = f_loader.loadClass(b_name);
   1.120 +    c = g_loader.loadClass(b_name);
   1.121 +  }
   1.122 +  public class A {
   1.123 +  }
   1.124 +  class B extends A {
   1.125 +  }
   1.126 +}

mercurial