PlatformComponent.java 17.6 KB
Newer Older
1
/*
G
guangyu.zgy 已提交
2
 * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * 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 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24 25 26 27 28 29 30 31
 */

package java.lang.management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
32 33
import java.util.HashMap;
import java.util.Map;
34 35 36 37
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

38
import com.alibaba.management.ElasticHeapMXBean;
39 40
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.UnixOperatingSystemMXBean;
G
guangyu.zgy 已提交
41
import com.sun.management.VMOption;
42 43

import sun.management.ManagementFactoryHelper;
44
import sun.management.Util;
45

G
guangyu.zgy 已提交
46 47
import jdk.management.jfr.FlightRecorderMXBean;

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * This enum class defines the list of platform components
 * that provides monitoring and management support.
 * Each enum represents one MXBean interface. A MXBean
 * instance could implement one or more MXBean interfaces.
 *
 * For example, com.sun.management.GarbageCollectorMXBean
 * extends java.lang.management.GarbageCollectorMXBean
 * and there is one set of garbage collection MXBean instances,
 * each of which implements both c.s.m. and j.l.m. interfaces.
 * There are two separate enums GARBAGE_COLLECTOR
 * and SUN_GARBAGE_COLLECTOR so that ManagementFactory.getPlatformMXBeans(Class)
 * will return the list of MXBeans of the specified type.
 *
 * To add a new MXBean interface for the Java platform,
 * add a new enum constant and implement the MXBeanFetcher.
 */
enum PlatformComponent {

    /**
     * Class loading system of the Java virtual machine.
     */
    CLASS_LOADING(
        "java.lang.management.ClassLoadingMXBean",
        "java.lang", "ClassLoading", defaultKeyProperties(),
73
        true, // singleton
74 75 76 77 78 79 80 81 82 83 84 85
        new MXBeanFetcher<ClassLoadingMXBean>() {
            public List<ClassLoadingMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getClassLoadingMXBean());
            }
        }),

    /**
     * Compilation system of the Java virtual machine.
     */
    COMPILATION(
        "java.lang.management.CompilationMXBean",
        "java.lang", "Compilation", defaultKeyProperties(),
86
        true, // singleton
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        new MXBeanFetcher<CompilationMXBean>() {
            public List<CompilationMXBean> getMXBeans() {
                CompilationMXBean m = ManagementFactoryHelper.getCompilationMXBean();
                if (m == null) {
                   return Collections.emptyList();
                } else {
                   return Collections.singletonList(m);
                }
            }
        }),

    /**
     * Memory system of the Java virtual machine.
     */
    MEMORY(
        "java.lang.management.MemoryMXBean",
        "java.lang", "Memory", defaultKeyProperties(),
104
        true, // singleton
105 106 107 108 109 110 111 112 113 114 115 116
        new MXBeanFetcher<MemoryMXBean>() {
            public List<MemoryMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getMemoryMXBean());
            }
        }),

    /**
     * Garbage Collector in the Java virtual machine.
     */
    GARBAGE_COLLECTOR(
        "java.lang.management.GarbageCollectorMXBean",
        "java.lang", "GarbageCollector", keyProperties("name"),
117
        false, // zero or more instances
118 119 120 121 122 123 124 125 126 127 128 129 130
        new MXBeanFetcher<GarbageCollectorMXBean>() {
            public List<GarbageCollectorMXBean> getMXBeans() {
                return ManagementFactoryHelper.
                           getGarbageCollectorMXBeans();
            }
        }),

    /**
     * Memory manager in the Java virtual machine.
     */
    MEMORY_MANAGER(
        "java.lang.management.MemoryManagerMXBean",
        "java.lang", "MemoryManager", keyProperties("name"),
131
        false, // zero or more instances
132 133 134 135 136 137 138 139 140 141 142 143 144
        new MXBeanFetcher<MemoryManagerMXBean>() {
            public List<MemoryManagerMXBean> getMXBeans() {
                return ManagementFactoryHelper.getMemoryManagerMXBeans();
            }
        },
        GARBAGE_COLLECTOR),

    /**
     * Memory pool in the Java virtual machine.
     */
    MEMORY_POOL(
        "java.lang.management.MemoryPoolMXBean",
        "java.lang", "MemoryPool", keyProperties("name"),
145
        false, // zero or more instances
146 147 148 149 150 151 152 153 154 155 156 157
        new MXBeanFetcher<MemoryPoolMXBean>() {
            public List<MemoryPoolMXBean> getMXBeans() {
                return ManagementFactoryHelper.getMemoryPoolMXBeans();
            }
        }),

    /**
     * Operating system on which the Java virtual machine is running
     */
    OPERATING_SYSTEM(
        "java.lang.management.OperatingSystemMXBean",
        "java.lang", "OperatingSystem", defaultKeyProperties(),
158
        true, // singleton
159 160 161 162 163 164 165 166 167 168 169 170
        new MXBeanFetcher<OperatingSystemMXBean>() {
            public List<OperatingSystemMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getOperatingSystemMXBean());
            }
        }),

    /**
     * Runtime system of the Java virtual machine.
     */
    RUNTIME(
        "java.lang.management.RuntimeMXBean",
        "java.lang", "Runtime", defaultKeyProperties(),
171
        true, // singleton
172 173 174 175 176 177 178 179 180 181 182 183
        new MXBeanFetcher<RuntimeMXBean>() {
            public List<RuntimeMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getRuntimeMXBean());
            }
        }),

    /**
     * Threading system of the Java virtual machine.
     */
    THREADING(
        "java.lang.management.ThreadMXBean",
        "java.lang", "Threading", defaultKeyProperties(),
184
        true, // singleton
185 186 187 188 189 190 191 192 193 194 195
        new MXBeanFetcher<ThreadMXBean>() {
            public List<ThreadMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getThreadMXBean());
            }
        }),


    /**
     * Logging facility.
     */
    LOGGING(
196
        "java.lang.management.PlatformLoggingMXBean",
197
        "java.util.logging", "Logging", defaultKeyProperties(),
198
        true, // singleton
199 200
        new MXBeanFetcher<PlatformLoggingMXBean>() {
            public List<PlatformLoggingMXBean> getMXBeans() {
201 202 203 204 205 206
                PlatformLoggingMXBean m = ManagementFactoryHelper.getPlatformLoggingMXBean();
                if (m == null) {
                   return Collections.emptyList();
                } else {
                   return Collections.singletonList(m);
                }
207 208 209
            }
        }),

210 211 212 213
    /**
     * Buffer pools.
     */
    BUFFER_POOL(
214
        "java.lang.management.BufferPoolMXBean",
215
        "java.nio", "BufferPool", keyProperties("name"),
216
        false, // zero or more instances
217 218
        new MXBeanFetcher<BufferPoolMXBean>() {
            public List<BufferPoolMXBean> getMXBeans() {
219
                return ManagementFactoryHelper.getBufferPoolMXBeans();
220 221 222 223
            }
        }),


224 225 226 227 228 229 230 231
    // Sun Platform Extension

    /**
     * Sun extension garbage collector that performs collections in cycles.
     */
    SUN_GARBAGE_COLLECTOR(
        "com.sun.management.GarbageCollectorMXBean",
        "java.lang", "GarbageCollector", keyProperties("name"),
232
        false, // zero or more instances
233 234 235 236 237 238 239 240 241 242 243 244 245
        new MXBeanFetcher<com.sun.management.GarbageCollectorMXBean>() {
            public List<com.sun.management.GarbageCollectorMXBean> getMXBeans() {
                return getGcMXBeanList(com.sun.management.GarbageCollectorMXBean.class);
            }
        }),

    /**
     * Sun extension operating system on which the Java virtual machine
     * is running.
     */
    SUN_OPERATING_SYSTEM(
        "com.sun.management.OperatingSystemMXBean",
        "java.lang", "OperatingSystem", defaultKeyProperties(),
246
        true, // singleton
247 248 249 250 251 252 253 254 255 256 257 258
        new MXBeanFetcher<com.sun.management.OperatingSystemMXBean>() {
            public List<com.sun.management.OperatingSystemMXBean> getMXBeans() {
                return getOSMXBeanList(com.sun.management.OperatingSystemMXBean.class);
            }
        }),

    /**
     * Unix operating system.
     */
    SUN_UNIX_OPERATING_SYSTEM(
        "com.sun.management.UnixOperatingSystemMXBean",
        "java.lang", "OperatingSystem", defaultKeyProperties(),
259
        true, // singleton
260 261 262 263 264 265 266 267 268 269 270 271
        new MXBeanFetcher<UnixOperatingSystemMXBean>() {
            public List<UnixOperatingSystemMXBean> getMXBeans() {
                return getOSMXBeanList(com.sun.management.UnixOperatingSystemMXBean.class);
            }
        }),

    /**
     * Diagnostic support for the HotSpot Virtual Machine.
     */
    HOTSPOT_DIAGNOSTIC(
        "com.sun.management.HotSpotDiagnosticMXBean",
        "com.sun.management", "HotSpotDiagnostic", defaultKeyProperties(),
272
        true, // singleton
273 274 275 276
        new MXBeanFetcher<HotSpotDiagnosticMXBean>() {
            public List<HotSpotDiagnosticMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getDiagnosticMXBean());
            }
G
guangyu.zgy 已提交
277
        }),
278

G
guangyu.zgy 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    /**
     * Flight Recorder.
     */
    FLIGHT_RECORDER(
        "jdk.management.jfr.FlightRecorderMXBean",
        "jdk.management.jfr", "FlightRecorder", defaultKeyProperties(),
        true,
        new MXBeanFetcher<FlightRecorderMXBean>() {
            public List<FlightRecorderMXBean> getMXBeans() {
                HotSpotDiagnosticMXBean hsDiagMBean = ManagementFactoryHelper.getDiagnosticMXBean();
                VMOption opt = hsDiagMBean.getVMOption("EnableJFR");
                if (Boolean.valueOf(opt.getValue())) {
                    FlightRecorderMXBean m = ManagementFactoryHelper.getFlightRecorderMXBean();
                    if (m != null) {
                        return Collections.singletonList(m);
                    }
                }
                return Collections.emptyList();
            }
298 299 300 301 302 303 304 305 306 307 308 309 310
        }),

    /**
     * Elastic Heap.
     */
    ELASTIC_HEAP(
        "com.alibaba.management.ElasticHeapMXBean",
        "com.alibaba.management", "ElasticHeap", defaultKeyProperties(),
        true,
        new MXBeanFetcher<ElasticHeapMXBean>() {
            public List<ElasticHeapMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getElasticHeapMXBean());
            }
G
guangyu.zgy 已提交
311
        });
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

    /**
     * A task that returns the MXBeans for a component.
     */
    interface MXBeanFetcher<T extends PlatformManagedObject> {
        public List<T> getMXBeans();
    }

    /*
     * Returns a list of the GC MXBeans of the given type.
     */
    private static <T extends GarbageCollectorMXBean>
            List<T> getGcMXBeanList(Class<T> gcMXBeanIntf) {
        List<GarbageCollectorMXBean> list =
            ManagementFactoryHelper.getGarbageCollectorMXBeans();
327
        List<T> result = new ArrayList<>(list.size());
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        for (GarbageCollectorMXBean m : list) {
            if (gcMXBeanIntf.isInstance(m)) {
                result.add(gcMXBeanIntf.cast(m));
            }
        }
        return result;
    }

    /*
     * Returns the OS mxbean instance of the given type.
     */
    private static <T extends OperatingSystemMXBean>
            List<T> getOSMXBeanList(Class<T> osMXBeanIntf) {
        OperatingSystemMXBean m =
            ManagementFactoryHelper.getOperatingSystemMXBean();
        if (osMXBeanIntf.isInstance(m)) {
            return Collections.singletonList(osMXBeanIntf.cast(m));
        } else {
            return Collections.emptyList();
        }
    }

    private final String mxbeanInterfaceName;
    private final String domain;
    private final String type;
    private final Set<String> keyProperties;
354
    private final MXBeanFetcher<?> fetcher;
355
    private final PlatformComponent[] subComponents;
356
    private final boolean singleton;
357 358 359 360

    private PlatformComponent(String intfName,
                              String domain, String type,
                              Set<String> keyProperties,
361
                              boolean singleton,
362
                              MXBeanFetcher<?> fetcher,
363 364 365 366 367
                              PlatformComponent... subComponents) {
        this.mxbeanInterfaceName = intfName;
        this.domain = domain;
        this.type = type;
        this.keyProperties = keyProperties;
368
        this.singleton = singleton;
369 370 371 372 373 374 375 376 377 378 379 380 381
        this.fetcher = fetcher;
        this.subComponents = subComponents;
    }

    private static Set<String> defaultKeyProps;
    private static Set<String> defaultKeyProperties() {
        if (defaultKeyProps == null) {
            defaultKeyProps = Collections.singleton("type");
        }
        return defaultKeyProps;
    }

    private static Set<String> keyProperties(String... keyNames) {
382
        Set<String> set = new HashSet<>();
383 384 385 386 387 388 389
        set.add("type");
        for (String s : keyNames) {
            set.add(s);
        }
        return set;
    }

390 391 392 393
    boolean isSingleton() {
        return singleton;
    }

394 395 396 397 398 399 400 401 402
    String getMXBeanInterfaceName() {
        return mxbeanInterfaceName;
    }

    @SuppressWarnings("unchecked")
    Class<? extends PlatformManagedObject> getMXBeanInterface() {
        try {
            // Lazy loading the MXBean interface only when it is needed
            return (Class<? extends PlatformManagedObject>)
403 404
                       Class.forName(mxbeanInterfaceName, false,
                                     PlatformManagedObject.class.getClassLoader());
405 406 407 408 409 410 411 412 413
        } catch (ClassNotFoundException x) {
            throw new AssertionError(x);
        }
    }

    @SuppressWarnings("unchecked")
    <T extends PlatformManagedObject>
        List<T> getMXBeans(Class<T> mxbeanInterface)
    {
414
        return (List<T>) fetcher.getMXBeans();
415 416
    }

417 418
    <T extends PlatformManagedObject> T getSingletonMXBean(Class<T> mxbeanInterface)
    {
G
guangyu.zgy 已提交
419
        if (!singleton) {
420 421
            throw new IllegalArgumentException(mxbeanInterfaceName +
                " can have zero or more than one instances");
G
guangyu.zgy 已提交
422
        }
423

424
        List<T> list = getMXBeans(mxbeanInterface);
425 426 427 428
        assert list.size() == 1;
        return list.isEmpty() ? null : list.get(0);
    }

429
    <T extends PlatformManagedObject>
430 431 432
            T getSingletonMXBean(MBeanServerConnection mbs, Class<T> mxbeanInterface)
        throws java.io.IOException
    {
G
guangyu.zgy 已提交
433
        if (!singleton) {
434 435
            throw new IllegalArgumentException(mxbeanInterfaceName +
                " can have zero or more than one instances");
G
guangyu.zgy 已提交
436
        }
437 438 439 440 441 442 443 444 445 446 447

        // ObjectName of a singleton MXBean contains only domain and type
        assert keyProperties.size() == 1;
        String on = domain + ":type=" + type;
        return ManagementFactory.newPlatformMXBeanProxy(mbs,
                                                        on,
                                                        mxbeanInterface);
    }

    <T extends PlatformManagedObject>
            List<T> getMXBeans(MBeanServerConnection mbs, Class<T> mxbeanInterface)
448 449
        throws java.io.IOException
    {
450
        List<T> result = new ArrayList<>();
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        for (ObjectName on : getObjectNames(mbs)) {
            result.add(ManagementFactory.
                newPlatformMXBeanProxy(mbs,
                                       on.getCanonicalName(),
                                       mxbeanInterface)
            );
        }
        return result;
    }

    private Set<ObjectName> getObjectNames(MBeanServerConnection mbs)
        throws java.io.IOException
    {
        String domainAndType = domain + ":type=" + type;
        if (keyProperties.size() > 1) {
            // if there are more than 1 key properties (i.e. other than "type")
            domainAndType += ",*";
        }
469
        ObjectName on = Util.newObjectName(domainAndType);
470 471 472 473 474 475 476
        Set<ObjectName> set =  mbs.queryNames(on, null);
        for (PlatformComponent pc : subComponents) {
            set.addAll(pc.getObjectNames(mbs));
        }
        return set;
    }

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    // a map from MXBean interface name to PlatformComponent
    private static Map<String, PlatformComponent> enumMap;
    private static synchronized void ensureInitialized() {
        if (enumMap == null) {
            enumMap = new HashMap<>();
            for (PlatformComponent pc: PlatformComponent.values()) {
                // Use String as the key rather than Class<?> to avoid
                // causing unnecessary class loading of management interface
                enumMap.put(pc.getMXBeanInterfaceName(), pc);
            }
        }
    }

    static boolean isPlatformMXBean(String cn) {
        ensureInitialized();
        return enumMap.containsKey(cn);
    }

    static <T extends PlatformManagedObject>
        PlatformComponent getPlatformComponent(Class<T> mxbeanInterface)
    {
        ensureInitialized();
        String cn = mxbeanInterface.getName();
        PlatformComponent pc = enumMap.get(cn);
G
guangyu.zgy 已提交
501
        if (pc != null && pc.getMXBeanInterface() == mxbeanInterface) {
502
            return pc;
G
guangyu.zgy 已提交
503
        }
504 505 506
        return null;
    }

507 508
    private static final long serialVersionUID = 6992337162326171013L;
}