Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/Jstat.java
41159 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) {72e.printStackTrace();73System.exit(1);74}75System.exit(0);76}7778static void logNames() throws MonitorException {79VmIdentifier vmId = arguments.vmId();80int interval = arguments.sampleInterval();81MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);82MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);83JStatLogger logger = new JStatLogger(monitoredVm);84logger.printNames(arguments.counterNames(), arguments.comparator(),85arguments.showUnsupported(), System.out);86monitoredHost.detach(monitoredVm);87}8889static void logSnapShot() throws MonitorException {90VmIdentifier vmId = arguments.vmId();91int interval = arguments.sampleInterval();92MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);93MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);94JStatLogger logger = new JStatLogger(monitoredVm);95logger.printSnapShot(arguments.counterNames(), arguments.comparator(),96arguments.isVerbose(), arguments.showUnsupported(),97System.out);98monitoredHost.detach(monitoredVm);99}100101static void logSamples() throws MonitorException {102final VmIdentifier vmId = arguments.vmId();103int interval = arguments.sampleInterval();104final MonitoredHost monitoredHost =105MonitoredHost.getMonitoredHost(vmId);106MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);107final JStatLogger logger = new JStatLogger(monitoredVm);108OutputFormatter formatter = null;109110if (arguments.isSpecialOption()) {111OptionFormat format = arguments.optionFormat();112formatter = new OptionOutputFormatter(monitoredVm, format);113} else {114List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames());115Collections.sort(logged, arguments.comparator());116List<Monitor> constants = new ArrayList<Monitor>();117118for (Iterator<Monitor> i = logged.iterator(); i.hasNext(); /* empty */) {119Monitor m = i.next();120if (!(m.isSupported() || arguments.showUnsupported())) {121i.remove();122continue;123}124if (m.getVariability() == Variability.CONSTANT) {125i.remove();126if (arguments.printConstants()) constants.add(m);127} else if ((m.getUnits() == Units.STRING)128&& !arguments.printStrings()) {129i.remove();130}131}132133if (!constants.isEmpty()) {134logger.printList(constants, arguments.isVerbose(),135arguments.showUnsupported(), System.out);136if (!logged.isEmpty()) {137System.out.println();138}139}140141if (logged.isEmpty()) {142monitoredHost.detach(monitoredVm);143return;144}145146formatter = new RawOutputFormatter(logged,147arguments.printStrings());148}149150// handle user termination requests by stopping sampling loops151Runtime.getRuntime().addShutdownHook(new Thread() {152public void run() {153logger.stopLogging();154}155});156157// handle target termination events for targets other than ourself158HostListener terminator = new HostListener() {159public void vmStatusChanged(VmStatusChangeEvent ev) {160Integer lvmid = vmId.getLocalVmId();161if (ev.getTerminated().contains(lvmid)) {162logger.stopLogging();163} else if (!ev.getActive().contains(lvmid)) {164logger.stopLogging();165}166}167168public void disconnected(HostEvent ev) {169if (monitoredHost == ev.getMonitoredHost()) {170logger.stopLogging();171}172}173};174175if (vmId.getLocalVmId() != 0) {176monitoredHost.addHostListener(terminator);177}178179logger.logSamples(formatter, arguments.headerRate(),180arguments.sampleInterval(), arguments.sampleCount(),181System.out);182183// detach from host events and from the monitored target jvm184if (terminator != null) {185monitoredHost.removeHostListener(terminator);186}187monitoredHost.detach(monitoredVm);188}189}190191192