author | dnsimon |
Mon, 25 Nov 2019 12:08:20 -0800 | |
changeset 59262 | 06970ab040d4 |
parent 58851 | f1e6442241ca |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
2 |
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. |
43972 | 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 |
package jdk.vm.ci.services; |
|
24 |
||
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
25 |
import java.io.ByteArrayInputStream; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
26 |
import java.io.ByteArrayOutputStream; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
27 |
import java.io.DataInputStream; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
28 |
import java.io.DataOutputStream; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
29 |
import java.io.IOException; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
30 |
import java.util.ArrayList; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
31 |
import java.util.Collections; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
32 |
import java.util.Formatter; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
33 |
import java.util.HashMap; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
34 |
import java.util.List; |
44836 | 35 |
import java.util.Map; |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
36 |
import java.util.ServiceLoader; |
52433
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
37 |
import java.util.Set; |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
38 |
|
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
39 |
import jdk.internal.misc.VM; |
43972 | 40 |
|
41 |
/** |
|
44836 | 42 |
* Provides utilities needed by JVMCI clients. |
43972 | 43 |
*/ |
44 |
public final class Services { |
|
45 |
||
50858 | 46 |
/** |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
47 |
* Guards code that should be run when building an JVMCI shared library but should be excluded |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
48 |
* from (being compiled into) the library. Such code must be directly guarded by an {@code if} |
50858 | 49 |
* statement on this field - the guard cannot be behind a method call. |
50 |
*/ |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
51 |
public static final boolean IS_BUILDING_NATIVE_IMAGE = Boolean.parseBoolean(VM.getSavedProperty("jdk.vm.ci.services.aot")); |
50858 | 52 |
|
53 |
/** |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
54 |
* Guards code that should only be run in a JVMCI shared library. Such code must be directly |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
55 |
* guarded by an {@code if} statement on this field - the guard cannot be behind a method call. |
50858 | 56 |
* |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
57 |
* The value of this field in a JVMCI shared library runtime must be {@code true}. |
50858 | 58 |
*/ |
59 |
public static final boolean IS_IN_NATIVE_IMAGE; |
|
60 |
static { |
|
61 |
/* |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
62 |
* Prevents javac from constant folding use of this field. It is set to true by the process |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
63 |
* that builds the shared library. |
50858 | 64 |
*/ |
65 |
IS_IN_NATIVE_IMAGE = false; |
|
66 |
} |
|
67 |
||
43972 | 68 |
private Services() { |
69 |
} |
|
70 |
||
58851 | 71 |
/** |
72 |
* In a native image, this field is initialized by {@link #initializeSavedProperties(byte[])}. |
|
73 |
*/ |
|
74 |
private static volatile Map<String, String> savedProperties; |
|
75 |
||
76 |
static final boolean JVMCI_ENABLED = Boolean.parseBoolean(VM.getSavedProperties().get("jdk.internal.vm.ci.enabled")); |
|
44836 | 77 |
|
78 |
/** |
|
79 |
* Checks that JVMCI is enabled in the VM and throws an error if it isn't. |
|
80 |
*/ |
|
81 |
static void checkJVMCIEnabled() { |
|
82 |
if (!JVMCI_ENABLED) { |
|
83 |
throw new Error("The EnableJVMCI VM option must be true (i.e., -XX:+EnableJVMCI) to use JVMCI"); |
|
43972 | 84 |
} |
85 |
} |
|
86 |
||
87 |
/** |
|
44836 | 88 |
* Gets an unmodifiable copy of the system properties saved when {@link System} is initialized. |
43972 | 89 |
*/ |
44836 | 90 |
public static Map<String, String> getSavedProperties() { |
91 |
checkJVMCIEnabled(); |
|
58851 | 92 |
if (IS_IN_NATIVE_IMAGE) { |
93 |
if (savedProperties == null) { |
|
94 |
throw new InternalError("Saved properties not initialized"); |
|
95 |
} |
|
96 |
} else { |
|
97 |
if (savedProperties == null) { |
|
98 |
synchronized (Services.class) { |
|
99 |
if (savedProperties == null) { |
|
100 |
SecurityManager sm = System.getSecurityManager(); |
|
101 |
if (sm != null) { |
|
102 |
sm.checkPermission(new JVMCIPermission()); |
|
103 |
} |
|
104 |
savedProperties = VM.getSavedProperties(); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
43972 | 108 |
} |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
109 |
return savedProperties; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
110 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
111 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
112 |
/** |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
113 |
* Helper method equivalent to {@link #getSavedProperties()}{@code .getOrDefault(name, def)}. |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
114 |
*/ |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
115 |
public static String getSavedProperty(String name, String def) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
116 |
return Services.getSavedProperties().getOrDefault(name, def); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
117 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
118 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
119 |
/** |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
120 |
* Helper method equivalent to {@link #getSavedProperties()}{@code .get(name)}. |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
121 |
*/ |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
122 |
public static String getSavedProperty(String name) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
123 |
return Services.getSavedProperties().get(name); |
43972 | 124 |
} |
125 |
||
126 |
/** |
|
44836 | 127 |
* Causes the JVMCI subsystem to be initialized if it isn't already initialized. |
43972 | 128 |
*/ |
44836 | 129 |
public static void initializeJVMCI() { |
130 |
checkJVMCIEnabled(); |
|
131 |
try { |
|
132 |
Class.forName("jdk.vm.ci.runtime.JVMCI"); |
|
133 |
} catch (ClassNotFoundException e) { |
|
134 |
throw new InternalError(e); |
|
43972 | 135 |
} |
136 |
} |
|
52433
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
137 |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
138 |
private static final Map<Class<?>, List<?>> servicesCache = IS_BUILDING_NATIVE_IMAGE ? new HashMap<>() : null; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
139 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
140 |
@SuppressWarnings("unchecked") |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
141 |
private static <S> Iterable<S> load0(Class<S> service) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
142 |
if (IS_IN_NATIVE_IMAGE || IS_BUILDING_NATIVE_IMAGE) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
143 |
List<?> list = servicesCache.get(service); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
144 |
if (list != null) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
145 |
return (Iterable<S>) list; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
146 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
147 |
if (IS_IN_NATIVE_IMAGE) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
148 |
throw new InternalError(String.format("No %s providers found when building native image", service.getName())); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
149 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
150 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
151 |
|
59262
06970ab040d4
8234681: Remove UseJVMCIClassLoader logic from JVMCI code
dnsimon
parents:
58851
diff
changeset
|
152 |
Iterable<S> providers = ServiceLoader.load(service, ClassLoader.getSystemClassLoader()); |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
153 |
if (IS_BUILDING_NATIVE_IMAGE) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
154 |
synchronized (servicesCache) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
155 |
ArrayList<S> providersList = new ArrayList<>(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
156 |
for (S provider : providers) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
157 |
providersList.add(provider); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
158 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
159 |
servicesCache.put(service, providersList); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
160 |
providers = providersList; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
161 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
162 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
163 |
return providers; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
164 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
165 |
|
52433
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
166 |
/** |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
167 |
* Opens all JVMCI packages to {@code otherModule}. |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
168 |
*/ |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
169 |
static void openJVMCITo(Module otherModule) { |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
170 |
Module jvmci = Services.class.getModule(); |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
171 |
if (jvmci != otherModule) { |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
172 |
Set<String> packages = jvmci.getPackages(); |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
173 |
for (String pkg : packages) { |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
174 |
boolean opened = jvmci.isOpen(pkg, otherModule); |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
175 |
if (!opened) { |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
176 |
jvmci.addOpens(pkg, otherModule); |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
177 |
} |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
178 |
} |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
179 |
} |
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
50858
diff
changeset
|
180 |
} |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
181 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
182 |
/** |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
183 |
* Gets an {@link Iterable} of the JVMCI providers available for a given service. |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
184 |
* |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
185 |
* @throws SecurityException if a security manager is present and it denies <tt> |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
186 |
* {@link RuntimePermission}("jvmci")</tt> |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
187 |
*/ |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
188 |
public static <S> Iterable<S> load(Class<S> service) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
189 |
SecurityManager sm = System.getSecurityManager(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
190 |
if (sm != null) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
191 |
sm.checkPermission(new JVMCIPermission()); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
192 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
193 |
return load0(service); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
194 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
195 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
196 |
/** |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
197 |
* Gets the JVMCI provider for a given service for which at most one provider must be available. |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
198 |
* |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
199 |
* @param service the service whose provider is being requested |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
200 |
* @param required specifies if an {@link InternalError} should be thrown if no provider of |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
201 |
* {@code service} is available |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
202 |
* @throws SecurityException if a security manager is present and it denies <tt> |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
203 |
* {@link RuntimePermission}("jvmci")</tt> |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
204 |
*/ |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
205 |
public static <S> S loadSingle(Class<S> service, boolean required) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
206 |
SecurityManager sm = System.getSecurityManager(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
207 |
if (sm != null) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
208 |
sm.checkPermission(new JVMCIPermission()); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
209 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
210 |
Iterable<S> providers = load0(service); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
211 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
212 |
S singleProvider = null; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
213 |
for (S provider : providers) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
214 |
if (singleProvider != null) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
215 |
throw new InternalError(String.format("Multiple %s providers found: %s, %s", service.getName(), singleProvider.getClass().getName(), provider.getClass().getName())); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
216 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
217 |
singleProvider = provider; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
218 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
219 |
if (singleProvider == null && required) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
220 |
String javaHome = Services.getSavedProperty("java.home"); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
221 |
String vmName = Services.getSavedProperty("java.vm.name"); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
222 |
Formatter errorMessage = new Formatter(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
223 |
errorMessage.format("The VM does not expose required service %s.%n", service.getName()); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
224 |
errorMessage.format("Currently used Java home directory is %s.%n", javaHome); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
225 |
errorMessage.format("Currently used VM configuration is: %s", vmName); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
226 |
throw new UnsupportedOperationException(errorMessage.toString()); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
227 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
228 |
return singleProvider; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
229 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
230 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
231 |
/** |
54732 | 232 |
* A Java {@code char} has a maximal UTF8 length of 3. |
233 |
*/ |
|
234 |
private static final int MAX_UNICODE_IN_UTF8_LENGTH = 3; |
|
235 |
||
236 |
/** |
|
237 |
* {@link DataOutputStream#writeUTF(String)} only supports values whose UTF8 encoding length is |
|
238 |
* less than 65535. |
|
239 |
*/ |
|
240 |
private static final int MAX_UTF8_PROPERTY_STRING_LENGTH = 65535 / MAX_UNICODE_IN_UTF8_LENGTH; |
|
241 |
||
242 |
/** |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
243 |
* Serializes the {@linkplain #getSavedProperties() saved system properties} to a byte array for |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
244 |
* the purpose of {@linkplain #initializeSavedProperties(byte[]) initializing} the initial |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
245 |
* properties in the JVMCI shared library. |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
246 |
*/ |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
247 |
@VMEntryPoint |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
248 |
private static byte[] serializeSavedProperties() throws IOException { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
249 |
if (IS_IN_NATIVE_IMAGE) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
250 |
throw new InternalError("Can only serialize saved properties in HotSpot runtime"); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
251 |
} |
54732 | 252 |
return serializeProperties(Services.getSavedProperties()); |
253 |
} |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
254 |
|
54732 | 255 |
private static byte[] serializeProperties(Map<String, String> props) throws IOException { |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
256 |
// Compute size of output on the assumption that |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
257 |
// all system properties have ASCII names and values |
54732 | 258 |
int estimate = 4 + 4; |
259 |
int nonUtf8Props = 0; |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
260 |
for (Map.Entry<String, String> e : props.entrySet()) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
261 |
String name = e.getKey(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
262 |
String value = e.getValue(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
263 |
estimate += (2 + (name.length())) + (2 + (value.length())); |
54732 | 264 |
if (name.length() > MAX_UTF8_PROPERTY_STRING_LENGTH || value.length() > MAX_UTF8_PROPERTY_STRING_LENGTH) { |
265 |
nonUtf8Props++; |
|
266 |
} |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
267 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
268 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
269 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(estimate); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
270 |
DataOutputStream out = new DataOutputStream(baos); |
54732 | 271 |
out.writeInt(props.size() - nonUtf8Props); |
272 |
out.writeInt(nonUtf8Props); |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
273 |
for (Map.Entry<String, String> e : props.entrySet()) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
274 |
String name = e.getKey(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
275 |
String value = e.getValue(); |
54732 | 276 |
if (name.length() <= MAX_UTF8_PROPERTY_STRING_LENGTH && value.length() <= MAX_UTF8_PROPERTY_STRING_LENGTH) { |
277 |
out.writeUTF(name); |
|
278 |
out.writeUTF(value); |
|
279 |
} |
|
280 |
} |
|
281 |
if (nonUtf8Props != 0) { |
|
282 |
for (Map.Entry<String, String> e : props.entrySet()) { |
|
283 |
String name = e.getKey(); |
|
284 |
String value = e.getValue(); |
|
285 |
if (name.length() > MAX_UTF8_PROPERTY_STRING_LENGTH || value.length() > MAX_UTF8_PROPERTY_STRING_LENGTH) { |
|
286 |
byte[] utf8Name = name.getBytes("UTF-8"); |
|
287 |
byte[] utf8Value = value.getBytes("UTF-8"); |
|
288 |
out.writeInt(utf8Name.length); |
|
289 |
out.write(utf8Name); |
|
290 |
out.writeInt(utf8Value.length); |
|
291 |
out.write(utf8Value); |
|
292 |
} |
|
293 |
} |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
294 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
295 |
return baos.toByteArray(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
296 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
297 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
298 |
/** |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
299 |
* Initialized the {@linkplain #getSavedProperties() saved system properties} in the JVMCI |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
300 |
* shared library from the {@linkplain #serializeSavedProperties() serialized saved properties} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
301 |
* in the HotSpot runtime. |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
302 |
*/ |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
303 |
@VMEntryPoint |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
304 |
private static void initializeSavedProperties(byte[] serializedProperties) throws IOException { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
305 |
if (!IS_IN_NATIVE_IMAGE) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
306 |
throw new InternalError("Can only initialize saved properties in JVMCI shared library runtime"); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
307 |
} |
54732 | 308 |
savedProperties = Collections.unmodifiableMap(deserializeProperties(serializedProperties)); |
309 |
} |
|
310 |
||
311 |
private static Map<String, String> deserializeProperties(byte[] serializedProperties) throws IOException { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
312 |
DataInputStream in = new DataInputStream(new ByteArrayInputStream(serializedProperties)); |
54732 | 313 |
int utf8Props = in.readInt(); |
314 |
int nonUtf8Props = in.readInt(); |
|
315 |
Map<String, String> props = new HashMap<>(utf8Props + nonUtf8Props); |
|
316 |
int index = 0; |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
317 |
while (in.available() != 0) { |
54732 | 318 |
if (index < utf8Props) { |
319 |
String name = in.readUTF(); |
|
320 |
String value = in.readUTF(); |
|
321 |
props.put(name, value); |
|
322 |
} else { |
|
323 |
int nameLen = in.readInt(); |
|
324 |
byte[] nameBytes = new byte[nameLen]; |
|
325 |
in.read(nameBytes); |
|
326 |
int valueLen = in.readInt(); |
|
327 |
byte[] valueBytes = new byte[valueLen]; |
|
328 |
in.read(valueBytes); |
|
329 |
String name = new String(nameBytes, "UTF-8"); |
|
330 |
String value = new String(valueBytes, "UTF-8"); |
|
331 |
props.put(name, value); |
|
332 |
} |
|
333 |
index++; |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
334 |
} |
54732 | 335 |
return props; |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
336 |
} |
43972 | 337 |
} |