Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jstat/Jstat.java
38918 views
/*1* Copyright (c) 2004, 2010, 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.jstat;2627import java.util.*;28import sun.jvmstat.monitor.*;29import sun.jvmstat.monitor.event.*;3031/**32* Application to output jvmstat statistics exported by a target Java33* Virtual Machine. The jstat tool gets its inspiration from the suite34* of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in35* various UNIX platforms.36*37* @author Brian Doherty38* @since 1.539*/40public class Jstat {41private static Arguments arguments;4243public static void main(String[] args) {44try {45arguments = new Arguments(args);46} catch (IllegalArgumentException e) {47System.err.println(e.getMessage());48Arguments.printUsage(System.err);49System.exit(1);50}5152if (arguments.isHelp()) {53Arguments.printUsage(System.out);54System.exit(0);55}5657if (arguments.isOptions()) {58OptionLister ol = new OptionLister(arguments.optionsSources());59ol.print(System.out);60System.exit(0);61}6263try {64if (arguments.isList()) {65logNames();66} else if (arguments.isSnap()) {67logSnapShot();68} else {69logSamples();70}71} catch (MonitorException e) {72if (e.getMessage() != null) {73System.err.println(e.getMessage());74} else {75Throwable cause = e.getCause();76if ((cause != null) && (cause.getMessage() != null)) {77System.err.println(cause.getMessage());78} else {79e.printStackTrace();80}81}82System.exit(1);83}84System.exit(0);85}8687static void logNames() throws MonitorException {88VmIdentifier vmId = arguments.vmId();89int interval = arguments.sampleInterval();90MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);91MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);92JStatLogger logger = new JStatLogger(monitoredVm);93logger.printNames(arguments.counterNames(), arguments.comparator(),94arguments.showUnsupported(), System.out);95monitoredHost.detach(monitoredVm);96}9798static void logSnapShot() throws MonitorException {99VmIdentifier vmId = arguments.vmId();100int interval = arguments.sampleInterval();101MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);102MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);103JStatLogger logger = new JStatLogger(monitoredVm);104logger.printSnapShot(arguments.counterNames(), arguments.comparator(),105arguments.isVerbose(), arguments.showUnsupported(),106System.out);107monitoredHost.detach(monitoredVm);108}109110static void logSamples() throws MonitorException {111final VmIdentifier vmId = arguments.vmId();112int interval = arguments.sampleInterval();113final MonitoredHost monitoredHost =114MonitoredHost.getMonitoredHost(vmId);115MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);116final JStatLogger logger = new JStatLogger(monitoredVm);117OutputFormatter formatter = null;118119if (arguments.isSpecialOption()) {120OptionFormat format = arguments.optionFormat();121formatter = new OptionOutputFormatter(monitoredVm, format);122} else {123List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames());124Collections.sort(logged, arguments.comparator());125List<Monitor> constants = new ArrayList<Monitor>();126127for (Iterator i = logged.iterator(); i.hasNext(); /* empty */) {128Monitor m = (Monitor)i.next();129if (!(m.isSupported() || arguments.showUnsupported())) {130i.remove();131continue;132}133if (m.getVariability() == Variability.CONSTANT) {134i.remove();135if (arguments.printConstants()) constants.add(m);136} else if ((m.getUnits() == Units.STRING)137&& !arguments.printStrings()) {138i.remove();139}140}141142if (!constants.isEmpty()) {143logger.printList(constants, arguments.isVerbose(),144arguments.showUnsupported(), System.out);145if (!logged.isEmpty()) {146System.out.println();147}148}149150if (logged.isEmpty()) {151monitoredHost.detach(monitoredVm);152return;153}154155formatter = new RawOutputFormatter(logged,156arguments.printStrings());157}158159// handle user termination requests by stopping sampling loops160Runtime.getRuntime().addShutdownHook(new Thread() {161public void run() {162logger.stopLogging();163}164});165166// handle target termination events for targets other than ourself167HostListener terminator = new HostListener() {168public void vmStatusChanged(VmStatusChangeEvent ev) {169Integer lvmid = new Integer(vmId.getLocalVmId());170if (ev.getTerminated().contains(lvmid)) {171logger.stopLogging();172} else if (!ev.getActive().contains(lvmid)) {173logger.stopLogging();174}175}176177public void disconnected(HostEvent ev) {178if (monitoredHost == ev.getMonitoredHost()) {179logger.stopLogging();180}181}182};183184if (vmId.getLocalVmId() != 0) {185monitoredHost.addHostListener(terminator);186}187188logger.logSamples(formatter, arguments.headerRate(),189arguments.sampleInterval(), arguments.sampleCount(),190System.out);191192// detach from host events and from the monitored target jvm193if (terminator != null) {194monitoredHost.removeHostListener(terminator);195}196monitoredHost.detach(monitoredVm);197}198}199200201