author | lkorinth |
Wed, 13 Nov 2019 11:37:29 +0100 | |
changeset 59053 | ba6c248cae19 |
parent 52784 | 621efe32eb0b |
permissions | -rw-r--r-- |
17311 | 1 |
/* |
59053
ba6c248cae19
8232365: Implementation for JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
lkorinth
parents:
52784
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
17311 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
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 |
|
52784
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
30 |
|
17311 | 31 |
import java.io.File; |
52784
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
32 |
import java.io.FileInputStream; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
33 |
import java.io.IOException; |
17311 | 34 |
import java.net.MalformedURLException; |
35 |
import java.net.URL; |
|
36 |
import java.net.URLClassLoader; |
|
45825
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
37 |
import java.nio.file.Path; |
17311 | 38 |
import java.nio.file.Paths; |
39 |
import java.util.ArrayList; |
|
45825
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
40 |
import java.util.stream.Stream; |
17311 | 41 |
|
42 |
public class ClassUnloadCommon { |
|
43 |
public static class TestFailure extends RuntimeException { |
|
44 |
TestFailure(String msg) { |
|
45 |
super(msg); |
|
46 |
} |
|
47 |
} |
|
48 |
||
49 |
public static void failIf(boolean value, String msg) { |
|
50 |
if (value) throw new TestFailure("Test failed: " + msg); |
|
51 |
} |
|
52 |
||
53 |
private static volatile Object dummy = null; |
|
54 |
private static void allocateMemory(int kilobytes) { |
|
55 |
ArrayList<byte[]> l = new ArrayList<>(); |
|
56 |
dummy = l; |
|
57 |
for (int i = kilobytes; i > 0; i -= 1) { |
|
58 |
l.add(new byte[1024]); |
|
59 |
} |
|
60 |
l = null; |
|
61 |
dummy = null; |
|
62 |
} |
|
63 |
||
64 |
public static void triggerUnloading() { |
|
59053
ba6c248cae19
8232365: Implementation for JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
lkorinth
parents:
52784
diff
changeset
|
65 |
allocateMemory(16 * 1024); // force young collection |
17311 | 66 |
System.gc(); |
67 |
} |
|
68 |
||
45825
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
69 |
/** |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
70 |
* Creates a class loader that loads classes from {@code ${test.class.path}} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
71 |
* before delegating to the system class loader. |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
72 |
*/ |
17311 | 73 |
public static ClassLoader newClassLoader() { |
45825
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
74 |
String cp = System.getProperty("test.class.path", "."); |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
75 |
URL[] urls = Stream.of(cp.split(File.pathSeparator)) |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
76 |
.map(Paths::get) |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
77 |
.map(ClassUnloadCommon::toURL) |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
78 |
.toArray(URL[]::new); |
51747 | 79 |
return new URLClassLoader("ClassUnloadCommonClassLoader", urls, new ClassUnloadCommon().getClass().getClassLoader()) { |
45825
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
80 |
@Override |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
81 |
public Class<?> loadClass(String cn, boolean resolve) |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
82 |
throws ClassNotFoundException |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
83 |
{ |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
84 |
synchronized (getClassLoadingLock(cn)) { |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
85 |
Class<?> c = findLoadedClass(cn); |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
86 |
if (c == null) { |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
87 |
try { |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
88 |
c = findClass(cn); |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
89 |
} catch (ClassNotFoundException e) { |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
90 |
c = getParent().loadClass(cn); |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
91 |
} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
92 |
|
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
93 |
} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
94 |
if (resolve) { |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
95 |
resolveClass(c); |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
96 |
} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
97 |
return c; |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
98 |
} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
99 |
} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
100 |
}; |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
101 |
} |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
102 |
|
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
103 |
static URL toURL(Path path) { |
17311 | 104 |
try { |
45825
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
105 |
return path.toUri().toURL(); |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
106 |
} catch (MalformedURLException e) { |
4fa7bd62eb84
8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents:
35917
diff
changeset
|
107 |
throw new RuntimeException(e); |
17311 | 108 |
} |
109 |
} |
|
52784
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
110 |
|
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
111 |
// Get data for pre-compiled class file to load. |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
112 |
public static byte[] getClassData(String name) { |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
113 |
try { |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
114 |
String TempName = name.replaceAll("\\.", "/"); |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
115 |
String currentDir = System.getProperty("test.classes"); |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
116 |
String filename = currentDir + File.separator + TempName + ".class"; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
117 |
System.out.println("filename is " + filename); |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
118 |
FileInputStream fis = new FileInputStream(filename); |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
119 |
byte[] b = new byte[5000]; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
120 |
int cnt = fis.read(b, 0, 5000); |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
121 |
byte[] c = new byte[cnt]; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
122 |
for (int i=0; i<cnt; i++) c[i] = b[i]; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
123 |
return c; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
124 |
} catch (IOException e) { |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
125 |
return null; |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
126 |
} |
621efe32eb0b
8213209: [REDO] Allow Klass::_subklass and _next_sibling to have unloaded classes
eosterlund
parents:
52356
diff
changeset
|
127 |
} |
17311 | 128 |
} |