49815
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
|
49815
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
49815
|
25 |
package org.graalvm.compiler.hotspot.management;
|
|
26 |
|
49873
|
27 |
import java.lang.management.ManagementFactory;
|
|
28 |
import java.util.ArrayList;
|
|
29 |
|
|
30 |
import javax.management.InstanceAlreadyExistsException;
|
|
31 |
import javax.management.MBeanRegistrationException;
|
|
32 |
import javax.management.MBeanServer;
|
|
33 |
import javax.management.MBeanServerFactory;
|
|
34 |
import javax.management.MalformedObjectNameException;
|
|
35 |
import javax.management.NotCompliantMBeanException;
|
|
36 |
import javax.management.ObjectName;
|
|
37 |
|
|
38 |
import org.graalvm.compiler.debug.TTY;
|
58533
|
39 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
49873
|
40 |
import org.graalvm.compiler.hotspot.HotSpotGraalManagementRegistration;
|
|
41 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntime;
|
|
42 |
import org.graalvm.compiler.serviceprovider.ServiceProvider;
|
|
43 |
|
49815
|
44 |
/**
|
49873
|
45 |
* Dynamically registers an MBean with the {@link ManagementFactory#getPlatformMBeanServer()}.
|
|
46 |
*
|
|
47 |
* Polling for an active platform MBean server is done by calling
|
|
48 |
* {@link MBeanServerFactory#findMBeanServer(String)} with an argument value of {@code null}. Once
|
|
49 |
* this returns an non-empty list, {@link ManagementFactory#getPlatformMBeanServer()} can be called
|
|
50 |
* to obtain a reference to the platform MBean server instance.
|
49815
|
51 |
*/
|
49873
|
52 |
@ServiceProvider(HotSpotGraalManagementRegistration.class)
|
|
53 |
public final class HotSpotGraalManagement implements HotSpotGraalManagementRegistration {
|
|
54 |
|
|
55 |
private HotSpotGraalRuntimeMBean bean;
|
|
56 |
private volatile boolean needsRegistration = true;
|
|
57 |
HotSpotGraalManagement nextDeferred;
|
|
58 |
|
|
59 |
@Override
|
58533
|
60 |
public void initialize(HotSpotGraalRuntime runtime, GraalHotSpotVMConfig config) {
|
49873
|
61 |
if (bean == null) {
|
|
62 |
if (runtime.getManagement() != this) {
|
|
63 |
throw new IllegalArgumentException("Cannot initialize a second management object for runtime " + runtime.getName());
|
|
64 |
}
|
|
65 |
try {
|
|
66 |
String name = runtime.getName().replace(':', '_');
|
|
67 |
ObjectName objectName = new ObjectName("org.graalvm.compiler.hotspot:type=" + name);
|
|
68 |
bean = new HotSpotGraalRuntimeMBean(objectName, runtime);
|
|
69 |
registration.add(this);
|
|
70 |
} catch (MalformedObjectNameException err) {
|
|
71 |
err.printStackTrace(TTY.out);
|
|
72 |
}
|
|
73 |
} else if (bean.getRuntime() != runtime) {
|
|
74 |
throw new IllegalArgumentException("Cannot change the runtime a management interface is associated with");
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
static final class RegistrationThread extends Thread {
|
|
79 |
|
|
80 |
private MBeanServer platformMBeanServer;
|
|
81 |
private HotSpotGraalManagement deferred;
|
|
82 |
|
|
83 |
RegistrationThread() {
|
|
84 |
super("HotSpotGraalManagement Bean Registration");
|
|
85 |
this.setPriority(Thread.MIN_PRIORITY);
|
|
86 |
this.setDaemon(true);
|
|
87 |
this.start();
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Poll for active MBean server every 2 seconds.
|
|
92 |
*/
|
|
93 |
private static final int POLL_INTERVAL_MS = 2000;
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Adds a {@link HotSpotGraalManagement} to register with an active MBean server when one
|
|
97 |
* becomes available.
|
|
98 |
*/
|
|
99 |
synchronized void add(HotSpotGraalManagement e) {
|
|
100 |
if (deferred != null) {
|
|
101 |
e.nextDeferred = deferred;
|
|
102 |
}
|
|
103 |
deferred = e;
|
|
104 |
|
|
105 |
// Notify the registration thread that there is now
|
|
106 |
// a deferred registration to process
|
|
107 |
notify();
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Processes and clears any deferred registrations.
|
|
112 |
*/
|
|
113 |
private void process() {
|
|
114 |
for (HotSpotGraalManagement m = deferred; m != null; m = m.nextDeferred) {
|
|
115 |
HotSpotGraalRuntimeMBean bean = m.bean;
|
|
116 |
if (m.needsRegistration && bean != null) {
|
|
117 |
try {
|
|
118 |
platformMBeanServer.registerMBean(bean, bean.getObjectName());
|
|
119 |
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
|
|
120 |
e.printStackTrace(TTY.out);
|
|
121 |
// Registration failed - don't try again
|
|
122 |
m.bean = null;
|
|
123 |
}
|
|
124 |
m.needsRegistration = false;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
deferred = null;
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
public void run() {
|
|
132 |
while (true) {
|
|
133 |
try {
|
|
134 |
synchronized (this) {
|
|
135 |
// Wait until there are deferred registrations to process
|
|
136 |
while (deferred == null) {
|
|
137 |
wait();
|
|
138 |
}
|
|
139 |
}
|
|
140 |
poll();
|
|
141 |
Thread.sleep(POLL_INTERVAL_MS);
|
|
142 |
} catch (InterruptedException e) {
|
|
143 |
// Be verbose about unexpected interruption and then continue
|
|
144 |
e.printStackTrace(TTY.out);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Checks for active MBean server and if available, processes deferred registrations.
|
|
151 |
*/
|
|
152 |
synchronized void poll() {
|
|
153 |
if (platformMBeanServer == null) {
|
52578
|
154 |
try {
|
|
155 |
ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
|
|
156 |
if (!servers.isEmpty()) {
|
|
157 |
platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
|
|
158 |
process();
|
|
159 |
}
|
57537
|
160 |
} catch (SecurityException | UnsatisfiedLinkError | NoClassDefFoundError | UnsupportedOperationException e) {
|
52578
|
161 |
// Without permission to find or create the MBeanServer,
|
|
162 |
// we cannot process any Graal mbeans.
|
57537
|
163 |
// Various other errors can occur in the ManagementFactory (JDK-8076557)
|
52578
|
164 |
deferred = null;
|
49873
|
165 |
}
|
|
166 |
} else {
|
|
167 |
process();
|
|
168 |
}
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
private static final RegistrationThread registration = new RegistrationThread();
|
|
173 |
|
|
174 |
@Override
|
|
175 |
public ObjectName poll(boolean sync) {
|
|
176 |
if (sync) {
|
|
177 |
registration.poll();
|
|
178 |
}
|
|
179 |
if (bean == null || needsRegistration) {
|
|
180 |
// initialize() has not been called, it failed or registration failed
|
|
181 |
return null;
|
|
182 |
}
|
|
183 |
return bean.getObjectName();
|
|
184 |
}
|
49815
|
185 |
}
|