Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jconsole/SummaryTab.java
38918 views
/*1* Copyright (c) 2004, 2013, 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.tools.jconsole;2627import java.awt.*;28import java.io.*;29import java.lang.management.*;30import java.lang.reflect.*;31import java.text.*;32import java.util.*;33import java.util.concurrent.*;3435import javax.swing.*;363738import static sun.tools.jconsole.Formatter.*;39import static sun.tools.jconsole.Utilities.*;4041@SuppressWarnings("serial")42class SummaryTab extends Tab {43private static final String cpuUsageKey = "cpu";4445private static final String newDivider = "<tr><td colspan=4><font size =-1><hr>";46private static final String newTable = "<tr><td colspan=4 align=left><table cellpadding=1>";47private static final String newLeftTable = "<tr><td colspan=2 align=left><table cellpadding=1>";48private static final String newRightTable = "<td colspan=2 align=left><table cellpadding=1>";49private static final String endTable = "</table>";5051private static final int CPU_DECIMALS = 1;5253private CPUOverviewPanel overviewPanel;54private DateFormat headerDateTimeFormat;55private String pathSeparator = null;56HTMLPane info;5758private static class Result {59long upTime = -1L;60long processCpuTime = -1L;61long timeStamp;62int nCPUs;63String summary;64}6566public static String getTabName() {67return Messages.SUMMARY_TAB_TAB_NAME;68}6970public SummaryTab(VMPanel vmPanel) {71super(vmPanel, getTabName());7273setLayout(new BorderLayout());7475info = new HTMLPane();76setAccessibleName(info, getTabName());77add(new JScrollPane(info));7879headerDateTimeFormat =80Formatter.getDateTimeFormat(Messages.SUMMARY_TAB_HEADER_DATE_TIME_FORMAT);81}8283public SwingWorker<?, ?> newSwingWorker() {84return new SwingWorker<Result, Object>() {85public Result doInBackground() {86return formatSummary();87}888990protected void done() {91try {92Result result = get();93if (result != null) {94info.setText(result.summary);95if (overviewPanel != null &&96result.upTime > 0L &&97result.processCpuTime >= 0L) {9899overviewPanel.updateCPUInfo(result);100}101}102} catch (InterruptedException ex) {103} catch (ExecutionException ex) {104if (JConsole.isDebug()) {105ex.printStackTrace();106}107}108}109};110}111112StringBuilder buf;113114synchronized Result formatSummary() {115Result result = new Result();116ProxyClient proxyClient = vmPanel.getProxyClient();117if (proxyClient.isDead()) {118return null;119}120121buf = new StringBuilder();122append("<table cellpadding=1>");123124try {125RuntimeMXBean rmBean = proxyClient.getRuntimeMXBean();126CompilationMXBean cmpMBean = proxyClient.getCompilationMXBean();127ThreadMXBean tmBean = proxyClient.getThreadMXBean();128MemoryMXBean memoryBean = proxyClient.getMemoryMXBean();129ClassLoadingMXBean clMBean = proxyClient.getClassLoadingMXBean();130OperatingSystemMXBean osMBean = proxyClient.getOperatingSystemMXBean();131com.sun.management.OperatingSystemMXBean sunOSMBean =132proxyClient.getSunOperatingSystemMXBean();133134append("<tr><td colspan=4>");135append("<center><b>" + Messages.SUMMARY_TAB_TAB_NAME + "</b></center>");136String dateTime =137headerDateTimeFormat.format(System.currentTimeMillis());138append("<center>" + dateTime + "</center>");139140append(newDivider);141142{ // VM info143append(newLeftTable);144append(Messages.CONNECTION_NAME, vmPanel.getDisplayName());145append(Messages.VIRTUAL_MACHINE,146Resources.format(Messages.SUMMARY_TAB_VM_VERSION,147rmBean.getVmName(), rmBean.getVmVersion()));148append(Messages.VENDOR, rmBean.getVmVendor());149append(Messages.NAME, rmBean.getName());150append(endTable);151152append(newRightTable);153result.upTime = rmBean.getUptime();154append(Messages.UPTIME, formatTime(result.upTime));155if (sunOSMBean != null) {156result.processCpuTime = sunOSMBean.getProcessCpuTime();157append(Messages.PROCESS_CPU_TIME, formatNanoTime(result.processCpuTime));158}159160if (cmpMBean != null) {161append(Messages.JIT_COMPILER, cmpMBean.getName());162append(Messages.TOTAL_COMPILE_TIME,163cmpMBean.isCompilationTimeMonitoringSupported()164? formatTime(cmpMBean.getTotalCompilationTime())165: Messages.UNAVAILABLE);166} else {167append(Messages.JIT_COMPILER, Messages.UNAVAILABLE);168}169append(endTable);170}171172append(newDivider);173174{ // Threads and Classes175append(newLeftTable);176int tlCount = tmBean.getThreadCount();177int tdCount = tmBean.getDaemonThreadCount();178int tpCount = tmBean.getPeakThreadCount();179long ttCount = tmBean.getTotalStartedThreadCount();180String[] strings1 = formatLongs(tlCount, tpCount,181tdCount, ttCount);182append(Messages.LIVE_THREADS, strings1[0]);183append(Messages.PEAK, strings1[1]);184append(Messages.DAEMON_THREADS, strings1[2]);185append(Messages.TOTAL_THREADS_STARTED, strings1[3]);186append(endTable);187188append(newRightTable);189long clCount = clMBean.getLoadedClassCount();190long cuCount = clMBean.getUnloadedClassCount();191long ctCount = clMBean.getTotalLoadedClassCount();192String[] strings2 = formatLongs(clCount, cuCount, ctCount);193append(Messages.CURRENT_CLASSES_LOADED, strings2[0]);194append(Messages.TOTAL_CLASSES_LOADED, strings2[2]);195append(Messages.TOTAL_CLASSES_UNLOADED, strings2[1]);196append(null, "");197append(endTable);198}199200append(newDivider);201202{ // Memory203MemoryUsage u = memoryBean.getHeapMemoryUsage();204205append(newLeftTable);206String[] strings1 = formatKByteStrings(u.getUsed(), u.getMax());207append(Messages.CURRENT_HEAP_SIZE, strings1[0]);208append(Messages.MAXIMUM_HEAP_SIZE, strings1[1]);209append(endTable);210211append(newRightTable);212String[] strings2 = formatKByteStrings(u.getCommitted());213append(Messages.COMMITTED_MEMORY, strings2[0]);214append(Messages.SUMMARY_TAB_PENDING_FINALIZATION_LABEL,215Resources.format(Messages.SUMMARY_TAB_PENDING_FINALIZATION_VALUE,216memoryBean.getObjectPendingFinalizationCount()));217append(endTable);218219append(newTable);220Collection<GarbageCollectorMXBean> garbageCollectors =221proxyClient.getGarbageCollectorMXBeans();222for (GarbageCollectorMXBean garbageCollectorMBean : garbageCollectors) {223String gcName = garbageCollectorMBean.getName();224long gcCount = garbageCollectorMBean.getCollectionCount();225long gcTime = garbageCollectorMBean.getCollectionTime();226227append(Messages.GARBAGE_COLLECTOR,228Resources.format(Messages.GC_INFO, gcName, gcCount,229(gcTime >= 0) ? formatTime(gcTime)230: Messages.UNAVAILABLE),2314);232}233append(endTable);234}235236append(newDivider);237238{ // Operating System info239append(newLeftTable);240String osName = osMBean.getName();241String osVersion = osMBean.getVersion();242String osArch = osMBean.getArch();243result.nCPUs = osMBean.getAvailableProcessors();244append(Messages.OPERATING_SYSTEM, osName + " " + osVersion);245append(Messages.ARCHITECTURE, osArch);246append(Messages.NUMBER_OF_PROCESSORS, result.nCPUs+"");247248if (pathSeparator == null) {249// Must use separator of remote OS, not File.pathSeparator250// from this local VM. In the future, consider using251// RuntimeMXBean to get the remote system property.252pathSeparator = osName.startsWith("Windows ") ? ";" : ":";253}254255if (sunOSMBean != null) {256String[] kbStrings1 =257formatKByteStrings(sunOSMBean.getCommittedVirtualMemorySize());258259String[] kbStrings2 =260formatKByteStrings(sunOSMBean.getTotalPhysicalMemorySize(),261sunOSMBean.getFreePhysicalMemorySize(),262sunOSMBean.getTotalSwapSpaceSize(),263sunOSMBean.getFreeSwapSpaceSize());264265append(Messages.COMMITTED_VIRTUAL_MEMORY, kbStrings1[0]);266append(endTable);267268append(newRightTable);269append(Messages.TOTAL_PHYSICAL_MEMORY, kbStrings2[0]);270append(Messages.FREE_PHYSICAL_MEMORY, kbStrings2[1]);271append(Messages.TOTAL_SWAP_SPACE, kbStrings2[2]);272append(Messages.FREE_SWAP_SPACE, kbStrings2[3]);273}274275append(endTable);276}277278append(newDivider);279280{ // VM arguments and paths281append(newTable);282String args = "";283java.util.List<String> inputArguments = rmBean.getInputArguments();284for (String arg : inputArguments) {285args += arg + " ";286}287append(Messages.VM_ARGUMENTS, args, 4);288append(Messages.CLASS_PATH, rmBean.getClassPath(), 4);289append(Messages.LIBRARY_PATH, rmBean.getLibraryPath(), 4);290append(Messages.BOOT_CLASS_PATH,291rmBean.isBootClassPathSupported()292? rmBean.getBootClassPath()293: Messages.UNAVAILABLE,2944);295append(endTable);296}297} catch (IOException e) {298if (JConsole.isDebug()) {299e.printStackTrace();300}301proxyClient.markAsDead();302return null;303} catch (UndeclaredThrowableException e) {304if (JConsole.isDebug()) {305e.printStackTrace();306}307proxyClient.markAsDead();308return null;309}310311append("</table>");312313result.timeStamp = System.currentTimeMillis();314result.summary = buf.toString();315316return result;317}318319private synchronized void append(String str) {320buf.append(str);321}322323void append(String label, String value) {324append(newRow(label, value));325}326327private void append(String label, String value, int columnPerRow) {328if (columnPerRow == 4 && pathSeparator != null) {329value = value.replace(pathSeparator,330"<b></b>" + pathSeparator);331}332append(newRow(label, value, columnPerRow));333}334335OverviewPanel[] getOverviewPanels() {336if (overviewPanel == null) {337overviewPanel = new CPUOverviewPanel();338}339return new OverviewPanel[] { overviewPanel };340}341342private static class CPUOverviewPanel extends OverviewPanel {343private long prevUpTime, prevProcessCpuTime;344345CPUOverviewPanel() {346super(Messages.CPU_USAGE, cpuUsageKey, Messages.CPU_USAGE, Plotter.Unit.PERCENT);347getPlotter().setDecimals(CPU_DECIMALS);348}349350public void updateCPUInfo(Result result) {351if (prevUpTime > 0L && result.upTime > prevUpTime) {352// elapsedCpu is in ns and elapsedTime is in ms.353long elapsedCpu = result.processCpuTime - prevProcessCpuTime;354long elapsedTime = result.upTime - prevUpTime;355// cpuUsage could go higher than 100% because elapsedTime356// and elapsedCpu are not fetched simultaneously. Limit to357// 99% to avoid Plotter showing a scale from 0% to 200%.358float cpuUsage =359Math.min(99F,360elapsedCpu / (elapsedTime * 10000F * result.nCPUs));361362cpuUsage = Math.max(0F, cpuUsage);363364getPlotter().addValues(result.timeStamp,365Math.round(cpuUsage * Math.pow(10.0, CPU_DECIMALS)));366getInfoLabel().setText(Resources.format(Messages.CPU_USAGE_FORMAT,367String.format("%."+CPU_DECIMALS+"f", cpuUsage)));368}369this.prevUpTime = result.upTime;370this.prevProcessCpuTime = result.processCpuTime;371}372}373}374375376