hotspot/test/runtime/testlibrary/ClassUnloadCommon.java
author mockner
Wed, 03 Feb 2016 11:40:30 -0500
changeset 35917 463d67f86eaa
parent 17311 6ba3841cb7f9
child 45825 4fa7bd62eb84
permissions -rw-r--r--
8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging. Summary: TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData have been reimplemented using Unified logging. Reviewed-by: iklam, coleenp, dholmes, jiangli, hseigel, rprotacio Contributed-by: max.ockner@oracle.com, ioi.lam@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     1
/*
35917
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     4
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     8
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    14
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    18
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    21
 * questions.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    22
 */
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    23
35917
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    24
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    25
/*
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    26
 * To use ClassUnloadCommon from a sub-process, see hotspot/test/runtime/logging/ClassLoadUnloadTest.java
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    27
 * for an example.
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    28
 */
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    29
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    30
import java.io.File;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    31
import java.net.MalformedURLException;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    32
import java.net.URL;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    33
import java.net.URLClassLoader;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    34
import java.nio.file.Paths;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    35
import java.util.ArrayList;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    36
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    37
public class ClassUnloadCommon {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    38
    public static class TestFailure extends RuntimeException {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    39
        TestFailure(String msg) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    40
            super(msg);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    41
        }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    42
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    43
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    44
    public static void failIf(boolean value, String msg) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    45
        if (value) throw new TestFailure("Test failed: " + msg);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    46
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    47
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    48
    private static volatile Object dummy = null;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    49
    private static void allocateMemory(int kilobytes) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    50
        ArrayList<byte[]> l = new ArrayList<>();
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    51
        dummy = l;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    52
        for (int i = kilobytes; i > 0; i -= 1) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    53
            l.add(new byte[1024]);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    54
        }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    55
        l = null;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    56
        dummy = null;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    57
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    58
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    59
    public static void triggerUnloading() {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    60
        allocateMemory(16 * 1024); // yg size is 8m with cms, force young collection
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    61
        System.gc();
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    62
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    63
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    64
    public static ClassLoader newClassLoader() {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    65
        try {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    66
            return new URLClassLoader(new URL[] {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    67
                Paths.get(System.getProperty("test.classes",".") + File.separatorChar + "classes").toUri().toURL(),
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    68
            }, null);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    69
        } catch (MalformedURLException e){
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    70
            throw new RuntimeException("Unexpected URL conversion failure", e);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    71
        }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    72
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    73
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    74
}