PlatformComponent.java 17.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2012, 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.TenantContainerMXBean;
39
import com.alibaba.management.ElasticHeapMXBean;
Y
yunyao.zxl 已提交
40
import com.alibaba.management.WispCounterMXBean;
41 42 43 44
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.UnixOperatingSystemMXBean;

import sun.management.ManagementFactoryHelper;
45
import sun.management.Util;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

/**
 * 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(),
72
        true, // singleton
73 74 75 76 77 78 79 80 81 82 83 84
        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(),
85
        true, // singleton
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        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(),
103
        true, // singleton
104 105 106 107 108 109 110 111 112 113 114 115
        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"),
116
        false, // zero or more instances
117 118 119 120 121 122 123 124 125 126 127 128 129
        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"),
130
        false, // zero or more instances
131 132 133 134 135 136 137 138 139 140 141 142 143
        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"),
144
        false, // zero or more instances
145 146 147 148 149 150 151 152 153 154 155 156
        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(),
157
        true, // singleton
158 159 160 161 162 163 164 165 166 167 168 169
        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(),
170
        true, // singleton
171 172 173 174 175 176 177 178 179 180 181 182
        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(),
183
        true, // singleton
184 185 186 187 188 189 190 191 192 193 194
        new MXBeanFetcher<ThreadMXBean>() {
            public List<ThreadMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getThreadMXBean());
            }
        }),


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

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


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

    /**
     * Sun extension garbage collector that performs collections in cycles.
     */
    SUN_GARBAGE_COLLECTOR(
        "com.sun.management.GarbageCollectorMXBean",
        "java.lang", "GarbageCollector", keyProperties("name"),
231
        false, // zero or more instances
232 233 234 235 236 237 238 239 240 241 242 243 244
        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(),
245
        true, // singleton
246 247 248 249 250 251 252 253 254 255 256 257
        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(),
258
        true, // singleton
259 260 261 262 263 264 265 266 267 268 269 270
        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(),
271
        true, // singleton
272 273 274 275
        new MXBeanFetcher<HotSpotDiagnosticMXBean>() {
            public List<HotSpotDiagnosticMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getDiagnosticMXBean());
            }
G
guangyu.zgy 已提交
276
        }),
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    /**
     * Tenant Container.
     */
    TENANT_CONTAINER(
        "com.alibaba.management.TenantContainerMXBean",
        "com.alibaba.management", "TenantContainer", defaultKeyProperties(),
        true,
        new MXBeanFetcher<TenantContainerMXBean>() {
            public List<TenantContainerMXBean> getMXBeans() {
                TenantContainerMXBean m = ManagementFactoryHelper.getTenantContainerMXBean();
                if (null == m) {
                    return Collections.emptyList();
                } else {
                    return Collections.singletonList(m);
                }
            }
        }),

296 297 298 299 300 301 302 303 304 305 306
    /**
     * 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());
            }
Y
yunyao.zxl 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319
        }),

    /**
     * Wisp Counter.
     */
    WISP_COUNTER(
        "com.alibaba.management.WispCounterMXBean",
                "com.alibaba.management", "WispCounter", defaultKeyProperties(),
        true, // singleton
        new MXBeanFetcher<WispCounterMXBean>() {
            public List<WispCounterMXBean> getMXBeans() {
                return Collections.singletonList(ManagementFactoryHelper.getWispCounterMXBean());
            }
G
guangyu.zgy 已提交
320
        });
321

322

323 324 325 326 327 328 329 330 331 332 333 334 335 336
    /**
     * 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();
337
        List<T> result = new ArrayList<>(list.size());
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        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;
364
    private final MXBeanFetcher<?> fetcher;
365
    private final PlatformComponent[] subComponents;
366
    private final boolean singleton;
367 368 369 370

    private PlatformComponent(String intfName,
                              String domain, String type,
                              Set<String> keyProperties,
371
                              boolean singleton,
372
                              MXBeanFetcher<?> fetcher,
373 374 375 376 377
                              PlatformComponent... subComponents) {
        this.mxbeanInterfaceName = intfName;
        this.domain = domain;
        this.type = type;
        this.keyProperties = keyProperties;
378
        this.singleton = singleton;
379 380 381 382 383 384 385 386 387 388 389 390 391
        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) {
392
        Set<String> set = new HashSet<>();
393 394 395 396 397 398 399
        set.add("type");
        for (String s : keyNames) {
            set.add(s);
        }
        return set;
    }

400 401 402 403
    boolean isSingleton() {
        return singleton;
    }

404 405 406 407 408 409 410 411 412
    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>)
413 414
                       Class.forName(mxbeanInterfaceName, false,
                                     PlatformManagedObject.class.getClassLoader());
415 416 417 418 419 420 421 422 423
        } catch (ClassNotFoundException x) {
            throw new AssertionError(x);
        }
    }

    @SuppressWarnings("unchecked")
    <T extends PlatformManagedObject>
        List<T> getMXBeans(Class<T> mxbeanInterface)
    {
424
        return (List<T>) fetcher.getMXBeans();
425 426
    }

427 428
    <T extends PlatformManagedObject> T getSingletonMXBean(Class<T> mxbeanInterface)
    {
429
        if (!singleton)
430 431 432
            throw new IllegalArgumentException(mxbeanInterfaceName +
                " can have zero or more than one instances");

433
        List<T> list = getMXBeans(mxbeanInterface);
434 435 436 437
        assert list.size() == 1;
        return list.isEmpty() ? null : list.get(0);
    }

438
    <T extends PlatformManagedObject>
439 440 441
            T getSingletonMXBean(MBeanServerConnection mbs, Class<T> mxbeanInterface)
        throws java.io.IOException
    {
442
        if (!singleton)
443 444 445 446 447 448 449 450 451 452 453 454 455
            throw new IllegalArgumentException(mxbeanInterfaceName +
                " can have zero or more than one instances");

        // 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)
456 457
        throws java.io.IOException
    {
458
        List<T> result = new ArrayList<>();
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
        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 += ",*";
        }
477
        ObjectName on = Util.newObjectName(domainAndType);
478 479 480 481 482 483 484
        Set<ObjectName> set =  mbs.queryNames(on, null);
        for (PlatformComponent pc : subComponents) {
            set.addAll(pc.getObjectNames(mbs));
        }
        return set;
    }

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    // 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);
509
        if (pc != null && pc.getMXBeanInterface() == mxbeanInterface)
510 511 512 513
            return pc;
        return null;
    }

514 515
    private static final long serialVersionUID = 6992337162326171013L;
}