Path: blob/master/src/java.management/share/classes/sun/management/VMManagementImpl.java
41152 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import jdk.internal.perf.Perf;28import sun.management.counter.*;29import sun.management.counter.perf.*;30import java.nio.ByteBuffer;31import java.io.IOException;32import java.net.InetAddress;33import java.net.UnknownHostException;34import java.util.List;35import java.util.Arrays;36import java.util.Collections;37import java.security.AccessController;38import java.security.PrivilegedAction;3940/**41* Implementation of VMManagement interface that accesses the management42* attributes and operations locally within the same Java virtual43* machine.44*/45class VMManagementImpl implements VMManagement {4647private static String version;4849private static boolean compTimeMonitoringSupport;50private static boolean threadContentionMonitoringSupport;51private static boolean currentThreadCpuTimeSupport;52private static boolean otherThreadCpuTimeSupport;53private static boolean objectMonitorUsageSupport;54private static boolean synchronizerUsageSupport;55private static boolean threadAllocatedMemorySupport;56private static boolean gcNotificationSupport;57private static boolean remoteDiagnosticCommandsSupport;585960static {61version = getVersion0();62if (version == null) {63throw new AssertionError("Invalid Management Version");64}65initOptionalSupportFields();66}67private native static String getVersion0();68private native static void initOptionalSupportFields();6970// Optional supports71public boolean isCompilationTimeMonitoringSupported() {72return compTimeMonitoringSupport;73}7475public boolean isThreadContentionMonitoringSupported() {76return threadContentionMonitoringSupport;77}7879public boolean isCurrentThreadCpuTimeSupported() {80return currentThreadCpuTimeSupport;81}8283public boolean isOtherThreadCpuTimeSupported() {84return otherThreadCpuTimeSupport;85}8687public boolean isBootClassPathSupported() {88return false;89}9091public boolean isObjectMonitorUsageSupported() {92return objectMonitorUsageSupport;93}9495public boolean isSynchronizerUsageSupported() {96return synchronizerUsageSupport;97}9899public boolean isThreadAllocatedMemorySupported() {100return threadAllocatedMemorySupport;101}102103public boolean isGcNotificationSupported() {104boolean isSupported = true;105try {106Class.forName("com.sun.management.GarbageCollectorMXBean");107} catch (ClassNotFoundException x) {108isSupported = false;109}110return isSupported;111}112113public boolean isRemoteDiagnosticCommandsSupported() {114return remoteDiagnosticCommandsSupport;115}116117public native boolean isThreadContentionMonitoringEnabled();118public native boolean isThreadCpuTimeEnabled();119public native boolean isThreadAllocatedMemoryEnabled();120121// Class Loading Subsystem122public int getLoadedClassCount() {123long count = getTotalClassCount() - getUnloadedClassCount();124return (int) count;125}126public native long getTotalClassCount();127public native long getUnloadedClassCount();128129public native boolean getVerboseClass();130131// Memory Subsystem132public native boolean getVerboseGC();133134// Runtime Subsystem135public String getManagementVersion() {136return version;137}138139public String getVmId() {140int pid = getProcessId();141String hostname = "localhost";142try {143hostname = InetAddress.getLocalHost().getHostName();144} catch (UnknownHostException e) {145// ignore146}147148return pid + "@" + hostname;149}150private native int getProcessId();151152public String getVmName() {153return System.getProperty("java.vm.name");154}155156public String getVmVendor() {157return System.getProperty("java.vm.vendor");158}159public String getVmVersion() {160return System.getProperty("java.vm.version");161}162public String getVmSpecName() {163return System.getProperty("java.vm.specification.name");164}165public String getVmSpecVendor() {166return System.getProperty("java.vm.specification.vendor");167}168public String getVmSpecVersion() {169return System.getProperty("java.vm.specification.version");170}171public String getClassPath() {172return System.getProperty("java.class.path");173}174public String getLibraryPath() {175return System.getProperty("java.library.path");176}177178public String getBootClassPath( ) {179throw new UnsupportedOperationException(180"Boot class path mechanism is not supported");181}182183public long getUptime() {184return getUptime0();185}186187private List<String> vmArgs = null;188public synchronized List<String> getVmArguments() {189if (vmArgs == null) {190String[] args = getVmArguments0();191List<String> l = ((args != null && args.length != 0) ? Arrays.asList(args) :192Collections.<String>emptyList());193vmArgs = Collections.unmodifiableList(l);194}195return vmArgs;196}197public native String[] getVmArguments0();198199public native long getStartupTime();200private native long getUptime0();201public native int getAvailableProcessors();202203// Compilation Subsystem204public String getCompilerName() {205@SuppressWarnings("removal")206String name = AccessController.doPrivileged(207new PrivilegedAction<String>() {208public String run() {209return System.getProperty("sun.management.compiler");210}211});212return name;213}214public native long getTotalCompileTime();215216// Thread Subsystem217public native long getTotalThreadCount();218public native int getLiveThreadCount();219public native int getPeakThreadCount();220public native int getDaemonThreadCount();221222// Operating System223public String getOsName() {224return System.getProperty("os.name");225}226public String getOsArch() {227return System.getProperty("os.arch");228}229public String getOsVersion() {230return System.getProperty("os.version");231}232233// Hotspot-specific runtime support234public native long getSafepointCount();235public native long getTotalSafepointTime();236public native long getSafepointSyncTime();237public native long getTotalApplicationNonStoppedTime();238239public native long getLoadedClassSize();240public native long getUnloadedClassSize();241public native long getClassLoadingTime();242public native long getMethodDataSize();243public native long getInitializedClassCount();244public native long getClassInitializationTime();245public native long getClassVerificationTime();246247// Performance Counter Support248private PerfInstrumentation perfInstr = null;249private boolean noPerfData = false;250251private synchronized PerfInstrumentation getPerfInstrumentation() {252if (noPerfData || perfInstr != null) {253return perfInstr;254}255256// construct PerfInstrumentation object257@SuppressWarnings("removal")258Perf perf = AccessController.doPrivileged(new Perf.GetPerfAction());259try {260ByteBuffer bb = perf.attach(0, "r");261if (bb.capacity() == 0) {262noPerfData = true;263return null;264}265perfInstr = new PerfInstrumentation(bb);266} catch (IllegalArgumentException e) {267// If the shared memory doesn't exist e.g. if -XX:-UsePerfData268// was set269noPerfData = true;270} catch (IOException e) {271throw new AssertionError(e);272}273return perfInstr;274}275276public List<Counter> getInternalCounters(String pattern) {277PerfInstrumentation perf = getPerfInstrumentation();278if (perf != null) {279return perf.findByPattern(pattern);280} else {281return Collections.emptyList();282}283}284}285286287