~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/management/ManagementFactoryHelper.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
 
 
26
package sun.management;
 
27
 
 
28
import java.lang.management.*;
 
29
 
 
30
import javax.management.InstanceAlreadyExistsException;
 
31
import javax.management.InstanceNotFoundException;
 
32
import javax.management.MBeanServer;
 
33
import javax.management.MBeanRegistrationException;
 
34
import javax.management.NotCompliantMBeanException;
 
35
import javax.management.ObjectName;
 
36
import javax.management.RuntimeOperationsException;
 
37
import java.security.AccessController;
 
38
import java.security.PrivilegedActionException;
 
39
import java.security.PrivilegedExceptionAction;
 
40
import sun.security.action.LoadLibraryAction;
 
41
 
 
42
import sun.util.logging.LoggingSupport;
 
43
 
 
44
import java.util.ArrayList;
 
45
import java.util.Collections;
 
46
import java.util.List;
 
47
import com.sun.management.OSMBeanFactory;
 
48
 
 
49
import static java.lang.management.ManagementFactory.*;
 
50
 
 
51
/**
 
52
 * ManagementFactoryHelper provides static factory methods to create
 
53
 * instances of the management interface.
 
54
 */
 
55
public class ManagementFactoryHelper {
 
56
    private ManagementFactoryHelper() {};
 
57
 
 
58
    private static VMManagement jvm;
 
59
 
 
60
    private static ClassLoadingImpl    classMBean = null;
 
61
    private static MemoryImpl          memoryMBean = null;
 
62
    private static ThreadImpl          threadMBean = null;
 
63
    private static RuntimeImpl         runtimeMBean = null;
 
64
    private static CompilationImpl     compileMBean = null;
 
65
    private static OperatingSystemImpl osMBean = null;
 
66
 
 
67
    public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
 
68
        if (classMBean == null) {
 
69
            classMBean = new ClassLoadingImpl(jvm);
 
70
        }
 
71
        return classMBean;
 
72
    }
 
73
 
 
74
    public static synchronized MemoryMXBean getMemoryMXBean() {
 
75
        if (memoryMBean == null) {
 
76
            memoryMBean = new MemoryImpl(jvm);
 
77
        }
 
78
        return memoryMBean;
 
79
    }
 
80
 
 
81
    public static synchronized ThreadMXBean getThreadMXBean() {
 
82
        if (threadMBean == null) {
 
83
            threadMBean = new ThreadImpl(jvm);
 
84
        }
 
85
        return threadMBean;
 
86
    }
 
87
 
 
88
    public static synchronized RuntimeMXBean getRuntimeMXBean() {
 
89
        if (runtimeMBean == null) {
 
90
            runtimeMBean = new RuntimeImpl(jvm);
 
91
        }
 
92
        return runtimeMBean;
 
93
    }
 
94
 
 
95
    public static synchronized CompilationMXBean getCompilationMXBean() {
 
96
        if (compileMBean == null && jvm.getCompilerName() != null) {
 
97
            compileMBean = new CompilationImpl(jvm);
 
98
        }
 
99
        return compileMBean;
 
100
    }
 
101
 
 
102
    public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
 
103
        if (osMBean == null) {
 
104
            osMBean = (OperatingSystemImpl)
 
105
                          OSMBeanFactory.getOperatingSystemMXBean(jvm);
 
106
        }
 
107
        return osMBean;
 
108
    }
 
109
 
 
110
    public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
 
111
        MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();
 
112
        List<MemoryPoolMXBean> list = new ArrayList<MemoryPoolMXBean>(pools.length);
 
113
        for (MemoryPoolMXBean p : pools) {
 
114
            list.add(p);
 
115
        }
 
116
        return list;
 
117
    }
 
118
 
 
119
    public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
 
120
        MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 
121
        List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length);
 
122
        for (MemoryManagerMXBean m : mgrs) {
 
123
            result.add(m);
 
124
        }
 
125
        return result;
 
126
    }
 
127
 
 
128
    public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 
129
        MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 
130
        List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length);
 
131
        for (MemoryManagerMXBean m : mgrs) {
 
132
            if (GarbageCollectorMXBean.class.isInstance(m)) {
 
133
                 result.add(GarbageCollectorMXBean.class.cast(m));
 
134
            }
 
135
        }
 
136
        return result;
 
137
    }
 
138
 
 
139
    public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
 
140
        if (LoggingSupport.isAvailable()) {
 
141
            return PlatformLoggingImpl.instance;
 
142
        } else {
 
143
            return null;
 
144
        }
 
145
    }
 
146
 
 
147
    // The logging MXBean object is an instance of
 
148
    // PlatformLoggingMXBean and java.util.logging.LoggingMXBean
 
149
    // but it can't directly implement two MXBean interfaces
 
150
    // as a compliant MXBean implements exactly one MXBean interface,
 
151
    // or if it implements one interface that is a subinterface of
 
152
    // all the others; otherwise, it is a non-compliant MXBean
 
153
    // and MBeanServer will throw NotCompliantMBeanException.
 
154
    // See the Definition of an MXBean section in javax.management.MXBean spec.
 
155
    //
 
156
    // To create a compliant logging MXBean, define a LoggingMXBean interface
 
157
    // that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
 
158
    interface LoggingMXBean
 
159
        extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
 
160
    }
 
161
 
 
162
    static class PlatformLoggingImpl implements LoggingMXBean
 
163
    {
 
164
        final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
 
165
        final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
 
166
 
 
167
        private volatile ObjectName objname;  // created lazily
 
168
        @Override
 
169
        public ObjectName getObjectName() {
 
170
            ObjectName result = objname;
 
171
            if (result == null) {
 
172
                synchronized (this) {
 
173
                    result = objname;
 
174
                    if (result == null) {
 
175
                        result = Util.newObjectName(LOGGING_MXBEAN_NAME);
 
176
                        objname = result;
 
177
                    }
 
178
                }
 
179
            }
 
180
            return result;
 
181
        }
 
182
 
 
183
        @Override
 
184
        public java.util.List<String> getLoggerNames() {
 
185
            return LoggingSupport.getLoggerNames();
 
186
        }
 
187
 
 
188
        @Override
 
189
        public String getLoggerLevel(String loggerName) {
 
190
            return LoggingSupport.getLoggerLevel(loggerName);
 
191
        }
 
192
 
 
193
        @Override
 
194
        public void setLoggerLevel(String loggerName, String levelName) {
 
195
            LoggingSupport.setLoggerLevel(loggerName, levelName);
 
196
        }
 
197
 
 
198
        @Override
 
199
        public String getParentLoggerName(String loggerName) {
 
200
            return LoggingSupport.getParentLoggerName(loggerName);
 
201
        }
 
202
    }
 
203
 
 
204
    private static List<BufferPoolMXBean> bufferPools = null;
 
205
    public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
 
206
        if (bufferPools == null) {
 
207
            bufferPools = new ArrayList<>(2);
 
208
            bufferPools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()
 
209
                .getDirectBufferPool()));
 
210
            bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
 
211
                .getMappedBufferPool()));
 
212
        }
 
213
        return bufferPools;
 
214
    }
 
215
 
 
216
    private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
 
217
 
 
218
    /**
 
219
     * Creates management interface for the given buffer pool.
 
220
     */
 
221
    private static BufferPoolMXBean
 
222
        createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)
 
223
    {
 
224
        return new BufferPoolMXBean() {
 
225
            private volatile ObjectName objname;  // created lazily
 
226
            @Override
 
227
            public ObjectName getObjectName() {
 
228
                ObjectName result = objname;
 
229
                if (result == null) {
 
230
                    synchronized (this) {
 
231
                        result = objname;
 
232
                        if (result == null) {
 
233
                            result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME +
 
234
                                ",name=" + pool.getName());
 
235
                            objname = result;
 
236
                        }
 
237
                    }
 
238
                }
 
239
                return result;
 
240
            }
 
241
            @Override
 
242
            public String getName() {
 
243
                return pool.getName();
 
244
            }
 
245
            @Override
 
246
            public long getCount() {
 
247
                return pool.getCount();
 
248
            }
 
249
            @Override
 
250
            public long getTotalCapacity() {
 
251
                return pool.getTotalCapacity();
 
252
            }
 
253
            @Override
 
254
            public long getMemoryUsed() {
 
255
                return pool.getMemoryUsed();
 
256
            }
 
257
        };
 
258
    }
 
259
 
 
260
    /**
 
261
     * Registers a given MBean if not registered in the MBeanServer;
 
262
     * otherwise, just return.
 
263
     */
 
264
    private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
 
265
        try {
 
266
            final ObjectName objName = Util.newObjectName(mbeanName);
 
267
 
 
268
            // inner class requires these fields to be final
 
269
            final MBeanServer mbs0 = mbs;
 
270
            final Object mbean0 = mbean;
 
271
            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 
272
                public Void run() throws MBeanRegistrationException,
 
273
                                         NotCompliantMBeanException {
 
274
                    try {
 
275
                        mbs0.registerMBean(mbean0, objName);
 
276
                        return null;
 
277
                    } catch (InstanceAlreadyExistsException e) {
 
278
                        // if an instance with the object name exists in
 
279
                        // the MBeanServer ignore the exception
 
280
                    }
 
281
                    return null;
 
282
                }
 
283
            });
 
284
        } catch (PrivilegedActionException e) {
 
285
            throw Util.newException(e.getException());
 
286
        }
 
287
    }
 
288
 
 
289
    static void registerInternalMBeans(MBeanServer mbs) {
 
290
    }
 
291
 
 
292
    static void unregisterInternalMBeans(MBeanServer mbs) {
 
293
    }
 
294
 
 
295
    static {
 
296
        jvm = new VMManagementImpl();
 
297
    }
 
298
 
 
299
    public static boolean isThreadSuspended(int state) {
 
300
        return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
 
301
    }
 
302
 
 
303
    public static boolean isThreadRunningNative(int state) {
 
304
        return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);
 
305
    }
 
306
 
 
307
    public static Thread.State toThreadState(int state) {
 
308
        // suspended and native bits may be set in state
 
309
        int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;
 
310
        return sun.misc.VM.toThreadState(threadStatus);
 
311
    }
 
312
 
 
313
    // These values are defined in jmm.h
 
314
    private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
 
315
    private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
 
316
    private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
 
317
 
 
318
}