hotspot/test/runtime/Metaspace/FragmentMetaspaceSimple.java
author alanb
Thu, 01 Dec 2016 15:54:39 +0000
changeset 42309 21f9f3fbc302
parent 31628 61d58c8bd374
child 46825 524c33aefd91
permissions -rw-r--r--
8170599: compiler/** tests using ToolProvider.getSystemClassLoader failing Reviewed-by: hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     1
/*
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     4
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     8
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    14
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    18
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    21
 * questions.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    22
 */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    23
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    24
/**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    25
 * @test
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    26
 * @library classes
31628
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    27
 * @build test.Empty
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    28
 * @run main/othervm/timeout=200 FragmentMetaspaceSimple
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    29
 */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    30
31628
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    31
import java.io.DataInputStream;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    32
import java.io.File;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    33
import java.io.FileInputStream;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    34
import java.io.IOException;
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    35
import java.util.ArrayList;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    36
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    37
/**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    38
 * Test that tries to fragment the native memory used by class loaders.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    39
 * Keeps every other class loader alive in order to fragment the memory space
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    40
 * used to store classes and meta data. Since the memory is probably allocated in
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    41
 * chunks per class loader this will cause a lot of fragmentation if not handled
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    42
 * properly since every other chunk will be unused.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    43
 */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    44
public class FragmentMetaspaceSimple {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    45
    public static void main(String... args) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    46
        runSimple(Long.valueOf(System.getProperty("time", "80000")));
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    47
        System.gc();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    48
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    49
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    50
    private static void runSimple(long time) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    51
        long startTime = System.currentTimeMillis();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    52
        ArrayList<ClassLoader> cls = new ArrayList<>();
31628
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    53
        char sep = File.separatorChar;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    54
        String fileName = "classes" + sep + "test" + sep + "Empty.class";
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    55
        File file = new File(System.getProperty("test.classes",".") + sep + fileName);
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    56
        byte buff[] = read(file);
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    57
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    58
        int i = 0;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    59
        for (i = 0; System.currentTimeMillis() < startTime + time; ++i) {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    60
            ClassLoader ldr = new MyClassLoader(buff);
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    61
            if (i % 1000 == 0) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    62
                cls.clear();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    63
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    64
            // only keep every other class loader alive
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    65
            if (i % 2 == 1) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    66
                cls.add(ldr);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    67
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    68
            Class<?> c = null;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    69
            try {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    70
                c = ldr.loadClass("test.Empty");
31628
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    71
                c.getClass().getClassLoader(); // make sure we have a valid class.
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    72
            } catch (ClassNotFoundException ex) {
31628
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    73
                System.out.println("i=" + i + ", len" + buff.length);
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    74
                throw new RuntimeException(ex);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    75
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    76
            c = null;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    77
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    78
        cls = null;
31628
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    79
        System.out.println("Finished " + i + " iterations in " +
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    80
                           (System.currentTimeMillis() - startTime) + " ms");
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    81
    }
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    82
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    83
    private static byte[] read(File file) {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    84
        byte buff[] = new byte[(int)(file.length())];
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    85
        try {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    86
            DataInputStream din = new DataInputStream(new FileInputStream(file));
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    87
            din.readFully(buff);
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    88
            din.close();
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    89
        } catch (IOException ex) {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    90
            throw new RuntimeException(ex);
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    91
        }
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    92
        return buff;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    93
    }
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    94
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    95
    static class MyClassLoader extends ClassLoader {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    96
        byte buff[];
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    97
        MyClassLoader(byte buff[]) {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    98
            this.buff = buff;
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
    99
        }
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   100
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   101
        public Class<?> loadClass() throws ClassNotFoundException {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   102
            String name = "test.Empty";
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   103
            try {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   104
                return defineClass(name, buff, 0, buff.length);
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   105
            } catch (Throwable e) {
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   106
                throw new ClassNotFoundException(name, e);
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   107
            }
61d58c8bd374 8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty
iklam
parents: 17827
diff changeset
   108
        }
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   109
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   110
}