2
|
1 |
/*
|
|
2 |
* Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.misc;
|
|
27 |
|
|
28 |
import java.util.Properties;
|
|
29 |
import java.util.HashMap;
|
|
30 |
import java.util.Map;
|
|
31 |
import java.util.Set;
|
|
32 |
|
|
33 |
public class VM {
|
|
34 |
|
|
35 |
/* The following methods used to be native methods that instruct
|
|
36 |
* the VM to selectively suspend certain threads in low-memory
|
|
37 |
* situations. They are inherently dangerous and not implementable
|
|
38 |
* on native threads. We removed them in JDK 1.2. The skeletons
|
|
39 |
* remain so that existing applications that use these methods
|
|
40 |
* will still work.
|
|
41 |
*/
|
|
42 |
private static boolean suspended = false;
|
|
43 |
|
|
44 |
/** @deprecated */
|
|
45 |
@Deprecated
|
|
46 |
public static boolean threadsSuspended() {
|
|
47 |
return suspended;
|
|
48 |
}
|
|
49 |
|
|
50 |
public static boolean allowThreadSuspension(ThreadGroup g, boolean b) {
|
|
51 |
return g.allowThreadSuspension(b);
|
|
52 |
}
|
|
53 |
|
|
54 |
/** @deprecated */
|
|
55 |
@Deprecated
|
|
56 |
public static boolean suspendThreads() {
|
|
57 |
suspended = true;
|
|
58 |
return true;
|
|
59 |
}
|
|
60 |
|
|
61 |
// Causes any suspended threadgroups to be resumed.
|
|
62 |
/** @deprecated */
|
|
63 |
@Deprecated
|
|
64 |
public static void unsuspendThreads() {
|
|
65 |
suspended = false;
|
|
66 |
}
|
|
67 |
|
|
68 |
// Causes threadgroups no longer marked suspendable to be resumed.
|
|
69 |
/** @deprecated */
|
|
70 |
@Deprecated
|
|
71 |
public static void unsuspendSomeThreads() {
|
|
72 |
}
|
|
73 |
|
|
74 |
/* Deprecated fields and methods -- Memory advice not supported in 1.2 */
|
|
75 |
|
|
76 |
/** @deprecated */
|
|
77 |
@Deprecated
|
|
78 |
public static final int STATE_GREEN = 1;
|
|
79 |
|
|
80 |
/** @deprecated */
|
|
81 |
@Deprecated
|
|
82 |
public static final int STATE_YELLOW = 2;
|
|
83 |
|
|
84 |
/** @deprecated */
|
|
85 |
@Deprecated
|
|
86 |
public static final int STATE_RED = 3;
|
|
87 |
|
|
88 |
/** @deprecated */
|
|
89 |
@Deprecated
|
|
90 |
public static final int getState() {
|
|
91 |
return STATE_GREEN;
|
|
92 |
}
|
|
93 |
|
|
94 |
/** @deprecated */
|
|
95 |
@Deprecated
|
|
96 |
public static void registerVMNotification(VMNotification n) { }
|
|
97 |
|
|
98 |
/** @deprecated */
|
|
99 |
@Deprecated
|
|
100 |
public static void asChange(int as_old, int as_new) { }
|
|
101 |
|
|
102 |
/** @deprecated */
|
|
103 |
@Deprecated
|
|
104 |
public static void asChange_otherthread(int as_old, int as_new) { }
|
|
105 |
|
|
106 |
/*
|
|
107 |
* Not supported in 1.2 because these will have to be exported as
|
|
108 |
* JVM functions, and we are not sure we want do that. Leaving
|
|
109 |
* here so it can be easily resurrected -- just remove the //
|
|
110 |
* comments.
|
|
111 |
*/
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Resume Java profiling. All profiling data is added to any
|
|
115 |
* earlier profiling, unless <code>resetJavaProfiler</code> is
|
|
116 |
* called in between. If profiling was not started from the
|
|
117 |
* command line, <code>resumeJavaProfiler</code> will start it.
|
|
118 |
* <p>
|
|
119 |
*
|
|
120 |
* NOTE: Profiling must be enabled from the command line for a
|
|
121 |
* java.prof report to be automatically generated on exit; if not,
|
|
122 |
* writeJavaProfilerReport must be invoked to write a report.
|
|
123 |
*
|
|
124 |
* @see resetJavaProfiler
|
|
125 |
* @see writeJavaProfilerReport
|
|
126 |
*/
|
|
127 |
|
|
128 |
// public native static void resumeJavaProfiler();
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Suspend Java profiling.
|
|
132 |
*/
|
|
133 |
// public native static void suspendJavaProfiler();
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Initialize Java profiling. Any accumulated profiling
|
|
137 |
* information is discarded.
|
|
138 |
*/
|
|
139 |
// public native static void resetJavaProfiler();
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Write the current profiling contents to the file "java.prof".
|
|
143 |
* If the file already exists, it will be overwritten.
|
|
144 |
*/
|
|
145 |
// public native static void writeJavaProfilerReport();
|
|
146 |
|
|
147 |
|
|
148 |
private static volatile boolean booted = false;
|
|
149 |
|
|
150 |
// Invoked by by System.initializeSystemClass just before returning.
|
|
151 |
// Subsystems that are invoked during initialization can check this
|
|
152 |
// property in order to avoid doing things that should wait until the
|
|
153 |
// application class loader has been set up.
|
|
154 |
//
|
|
155 |
public static void booted() {
|
|
156 |
booted = true;
|
|
157 |
}
|
|
158 |
|
|
159 |
public static boolean isBooted() {
|
|
160 |
return booted;
|
|
161 |
}
|
|
162 |
|
|
163 |
// A user-settable upper limit on the maximum amount of allocatable direct
|
|
164 |
// buffer memory. This value may be changed during VM initialization if
|
|
165 |
// "java" is launched with "-XX:MaxDirectMemorySize=<size>".
|
|
166 |
//
|
|
167 |
// The initial value of this field is arbitrary; during JRE initialization
|
|
168 |
// it will be reset to the value specified on the command line, if any,
|
|
169 |
// otherwise to Runtime.getRuntime.maxDirectMemory().
|
|
170 |
//
|
|
171 |
private static long directMemory = 64 * 1024 * 1024;
|
|
172 |
|
|
173 |
// If this method is invoked during VM initialization, it initializes the
|
|
174 |
// maximum amount of allocatable direct buffer memory (in bytes) from the
|
|
175 |
// system property sun.nio.MaxDirectMemorySize. The system property will
|
|
176 |
// be removed when it is accessed.
|
|
177 |
//
|
|
178 |
// If this method is invoked after the VM is booted, it returns the
|
|
179 |
// maximum amount of allocatable direct buffer memory.
|
|
180 |
//
|
|
181 |
public static long maxDirectMemory() {
|
|
182 |
if (booted)
|
|
183 |
return directMemory;
|
|
184 |
|
|
185 |
Properties p = System.getProperties();
|
|
186 |
String s = (String)p.remove("sun.nio.MaxDirectMemorySize");
|
|
187 |
System.setProperties(p);
|
|
188 |
|
|
189 |
if (s != null) {
|
|
190 |
if (s.equals("-1")) {
|
|
191 |
// -XX:MaxDirectMemorySize not given, take default
|
|
192 |
directMemory = Runtime.getRuntime().maxMemory();
|
|
193 |
} else {
|
|
194 |
long l = Long.parseLong(s);
|
|
195 |
if (l > -1)
|
|
196 |
directMemory = l;
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
return directMemory;
|
|
201 |
}
|
|
202 |
|
|
203 |
// A user-settable boolean to determine whether ClassLoader.loadClass should
|
|
204 |
// accept array syntax. This value may be changed during VM initialization
|
|
205 |
// via the system property "sun.lang.ClassLoader.allowArraySyntax".
|
|
206 |
//
|
|
207 |
// The default for 1.5 is "true", array syntax is allowed. In 1.6, the
|
|
208 |
// default will be "false". The presence of this system property to
|
|
209 |
// control array syntax allows applications the ability to preview this new
|
|
210 |
// behaviour.
|
|
211 |
//
|
|
212 |
private static boolean defaultAllowArraySyntax = false;
|
|
213 |
private static boolean allowArraySyntax = defaultAllowArraySyntax;
|
|
214 |
|
|
215 |
// If this method is invoked during VM initialization, it initializes the
|
|
216 |
// allowArraySyntax boolean based on the value of the system property
|
|
217 |
// "sun.lang.ClassLoader.allowArraySyntax". If the system property is not
|
|
218 |
// provided, the default for 1.5 is "true". In 1.6, the default will be
|
|
219 |
// "false". If the system property is provided, then the value of
|
|
220 |
// allowArraySyntax will be equal to "true" if Boolean.parseBoolean()
|
|
221 |
// returns "true". Otherwise, the field will be set to "false".
|
|
222 |
//
|
|
223 |
// If this method is invoked after the VM is booted, it returns the
|
|
224 |
// allowArraySyntax boolean set during initialization.
|
|
225 |
//
|
|
226 |
public static boolean allowArraySyntax() {
|
|
227 |
if (!booted) {
|
|
228 |
String s
|
|
229 |
= System.getProperty("sun.lang.ClassLoader.allowArraySyntax");
|
|
230 |
allowArraySyntax = (s == null
|
|
231 |
? defaultAllowArraySyntax
|
|
232 |
: Boolean.parseBoolean(s));
|
|
233 |
}
|
|
234 |
return allowArraySyntax;
|
|
235 |
}
|
|
236 |
|
|
237 |
// Initialize any miscellenous operating system settings that need to be
|
|
238 |
// set for the class libraries.
|
|
239 |
//
|
|
240 |
public static void initializeOSEnvironment() {
|
|
241 |
if (!booted) {
|
|
242 |
OSEnvironment.initialize();
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
/* Current count of objects pending for finalization */
|
|
247 |
private static volatile int finalRefCount = 0;
|
|
248 |
|
|
249 |
/* Peak count of objects pending for finalization */
|
|
250 |
private static volatile int peakFinalRefCount = 0;
|
|
251 |
|
|
252 |
/*
|
|
253 |
* Gets the number of objects pending for finalization.
|
|
254 |
*
|
|
255 |
* @return the number of objects pending for finalization.
|
|
256 |
*/
|
|
257 |
public static int getFinalRefCount() {
|
|
258 |
return finalRefCount;
|
|
259 |
}
|
|
260 |
|
|
261 |
/*
|
|
262 |
* Gets the peak number of objects pending for finalization.
|
|
263 |
*
|
|
264 |
* @return the peak number of objects pending for finalization.
|
|
265 |
*/
|
|
266 |
public static int getPeakFinalRefCount() {
|
|
267 |
return peakFinalRefCount;
|
|
268 |
}
|
|
269 |
|
|
270 |
/*
|
|
271 |
* Add <tt>n</tt> to the objects pending for finalization count.
|
|
272 |
*
|
|
273 |
* @param n an integer value to be added to the objects pending
|
|
274 |
* for finalization count
|
|
275 |
*/
|
|
276 |
public static void addFinalRefCount(int n) {
|
|
277 |
// The caller must hold lock to synchronize the update.
|
|
278 |
|
|
279 |
finalRefCount += n;
|
|
280 |
if (finalRefCount > peakFinalRefCount) {
|
|
281 |
peakFinalRefCount = finalRefCount;
|
|
282 |
}
|
|
283 |
}
|
|
284 |
|
|
285 |
|
|
286 |
public static Thread.State toThreadState(int threadStatus) {
|
|
287 |
// Initialize the threadStateMap
|
|
288 |
initThreadStateMap();
|
|
289 |
|
|
290 |
Thread.State s = threadStateMap.get(threadStatus);
|
|
291 |
if (s == null) {
|
|
292 |
// default to RUNNABLE if the threadStatus value is unknown
|
|
293 |
s = Thread.State.RUNNABLE;
|
|
294 |
}
|
|
295 |
return s;
|
|
296 |
}
|
|
297 |
|
|
298 |
// a map of threadStatus values to the corresponding Thread.State
|
|
299 |
private static Map<Integer, Thread.State> threadStateMap = null;
|
|
300 |
private static Map<Integer, String> threadStateNames = null;
|
|
301 |
|
|
302 |
private synchronized static void initThreadStateMap() {
|
|
303 |
if (threadStateMap != null) {
|
|
304 |
return;
|
|
305 |
}
|
|
306 |
|
|
307 |
final Thread.State[] ts = Thread.State.values();
|
|
308 |
|
|
309 |
final int[][] vmThreadStateValues = new int[ts.length][];
|
|
310 |
final String[][] vmThreadStateNames = new String[ts.length][];
|
|
311 |
getThreadStateValues(vmThreadStateValues, vmThreadStateNames);
|
|
312 |
|
|
313 |
threadStateMap = new HashMap<Integer, Thread.State>();
|
|
314 |
threadStateNames = new HashMap<Integer, String>();
|
|
315 |
for (int i = 0; i < ts.length; i++) {
|
|
316 |
String state = ts[i].name();
|
|
317 |
int[] values = null;
|
|
318 |
String[] names = null;
|
|
319 |
for (int j = 0; j < ts.length; j++) {
|
|
320 |
if (vmThreadStateNames[j][0].startsWith(state)) {
|
|
321 |
values = vmThreadStateValues[j];
|
|
322 |
names = vmThreadStateNames[j];
|
|
323 |
}
|
|
324 |
}
|
|
325 |
if (values == null) {
|
|
326 |
throw new InternalError("No VM thread state mapped to " +
|
|
327 |
state);
|
|
328 |
}
|
|
329 |
if (values.length != names.length) {
|
|
330 |
throw new InternalError("VM thread state values and names " +
|
|
331 |
" mapped to " + state + ": length not matched" );
|
|
332 |
}
|
|
333 |
for (int k = 0; k < values.length; k++) {
|
|
334 |
threadStateMap.put(values[k], ts[i]);
|
|
335 |
threadStateNames.put(values[k], names[k]);
|
|
336 |
}
|
|
337 |
}
|
|
338 |
}
|
|
339 |
// Fill in vmThreadStateValues with int arrays, each of which contains
|
|
340 |
// the threadStatus values mapping to the Thread.State enum constant.
|
|
341 |
// Fill in vmThreadStateNames with String arrays, each of which contains
|
|
342 |
// the name of each threadStatus value of the format:
|
|
343 |
// <Thread.State.name()>[.<Substate name>]
|
|
344 |
// e.g. WAITING.OBJECT_WAIT
|
|
345 |
//
|
|
346 |
private native static void getThreadStateValues(int[][] vmThreadStateValues,
|
|
347 |
String[][] vmThreadStateNames);
|
|
348 |
|
|
349 |
static {
|
|
350 |
initialize();
|
|
351 |
}
|
|
352 |
private native static void initialize();
|
|
353 |
}
|