0N/A/*
3261N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.jstat;
0N/A
0N/Aimport java.util.*;
0N/Aimport sun.jvmstat.monitor.*;
0N/Aimport sun.jvmstat.monitor.event.*;
0N/A
0N/A/**
0N/A * Application to output jvmstat statistics exported by a target Java
0N/A * Virtual Machine. The jstat tool gets its inspiration from the suite
0N/A * of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in
0N/A * various UNIX platforms.
0N/A *
0N/A * @author Brian Doherty
0N/A * @since 1.5
0N/A */
0N/Apublic class Jstat {
0N/A private static Arguments arguments;
0N/A
0N/A public static void main(String[] args) {
0N/A try {
0N/A arguments = new Arguments(args);
0N/A } catch (IllegalArgumentException e) {
0N/A System.err.println(e.getMessage());
0N/A Arguments.printUsage(System.err);
0N/A System.exit(1);
0N/A }
0N/A
0N/A if (arguments.isHelp()) {
0N/A Arguments.printUsage(System.out);
0N/A System.exit(0);
0N/A }
0N/A
0N/A if (arguments.isOptions()) {
0N/A OptionLister ol = new OptionLister(arguments.optionsSources());
0N/A ol.print(System.out);
0N/A System.exit(0);
0N/A }
0N/A
0N/A try {
0N/A if (arguments.isList()) {
0N/A logNames();
0N/A } else if (arguments.isSnap()) {
0N/A logSnapShot();
0N/A } else {
0N/A logSamples();
0N/A }
0N/A } catch (MonitorException e) {
0N/A if (e.getMessage() != null) {
0N/A System.err.println(e.getMessage());
0N/A } else {
0N/A Throwable cause = e.getCause();
0N/A if ((cause != null) && (cause.getMessage() != null)) {
0N/A System.err.println(cause.getMessage());
0N/A } else {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A System.exit(1);
0N/A }
0N/A System.exit(0);
0N/A }
0N/A
0N/A static void logNames() throws MonitorException {
0N/A VmIdentifier vmId = arguments.vmId();
0N/A int interval = arguments.sampleInterval();
0N/A MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
0N/A MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
0N/A JStatLogger logger = new JStatLogger(monitoredVm);
0N/A logger.printNames(arguments.counterNames(), arguments.comparator(),
0N/A arguments.showUnsupported(), System.out);
0N/A monitoredHost.detach(monitoredVm);
0N/A }
0N/A
0N/A static void logSnapShot() throws MonitorException {
0N/A VmIdentifier vmId = arguments.vmId();
0N/A int interval = arguments.sampleInterval();
0N/A MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
0N/A MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
0N/A JStatLogger logger = new JStatLogger(monitoredVm);
0N/A logger.printSnapShot(arguments.counterNames(), arguments.comparator(),
0N/A arguments.isVerbose(), arguments.showUnsupported(),
0N/A System.out);
0N/A monitoredHost.detach(monitoredVm);
0N/A }
0N/A
0N/A static void logSamples() throws MonitorException {
0N/A final VmIdentifier vmId = arguments.vmId();
0N/A int interval = arguments.sampleInterval();
0N/A final MonitoredHost monitoredHost =
0N/A MonitoredHost.getMonitoredHost(vmId);
0N/A MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
0N/A final JStatLogger logger = new JStatLogger(monitoredVm);
0N/A OutputFormatter formatter = null;
0N/A
0N/A if (arguments.isSpecialOption()) {
0N/A OptionFormat format = arguments.optionFormat();
0N/A formatter = new OptionOutputFormatter(monitoredVm, format);
0N/A } else {
0N/A List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames());
0N/A Collections.sort(logged, arguments.comparator());
0N/A List<Monitor> constants = new ArrayList<Monitor>();
0N/A
0N/A for (Iterator i = logged.iterator(); i.hasNext(); /* empty */) {
0N/A Monitor m = (Monitor)i.next();
0N/A if (!(m.isSupported() || arguments.showUnsupported())) {
0N/A i.remove();
0N/A continue;
0N/A }
0N/A if (m.getVariability() == Variability.CONSTANT) {
0N/A i.remove();
0N/A if (arguments.printConstants()) constants.add(m);
0N/A } else if ((m.getUnits() == Units.STRING)
0N/A && !arguments.printStrings()) {
0N/A i.remove();
0N/A }
0N/A }
0N/A
0N/A if (!constants.isEmpty()) {
0N/A logger.printList(constants, arguments.isVerbose(),
0N/A arguments.showUnsupported(), System.out);
0N/A if (!logged.isEmpty()) {
0N/A System.out.println();
0N/A }
0N/A }
0N/A
0N/A if (logged.isEmpty()) {
0N/A monitoredHost.detach(monitoredVm);
0N/A return;
0N/A }
0N/A
0N/A formatter = new RawOutputFormatter(logged,
0N/A arguments.printStrings());
0N/A }
0N/A
0N/A // handle user termination requests by stopping sampling loops
0N/A Runtime.getRuntime().addShutdownHook(new Thread() {
0N/A public void run() {
0N/A logger.stopLogging();
0N/A }
0N/A });
0N/A
0N/A // handle target termination events for targets other than ourself
0N/A HostListener terminator = new HostListener() {
0N/A public void vmStatusChanged(VmStatusChangeEvent ev) {
0N/A Integer lvmid = new Integer(vmId.getLocalVmId());
0N/A if (ev.getTerminated().contains(lvmid)) {
0N/A logger.stopLogging();
0N/A } else if (!ev.getActive().contains(lvmid)) {
0N/A logger.stopLogging();
0N/A }
0N/A }
0N/A
0N/A public void disconnected(HostEvent ev) {
0N/A if (monitoredHost == ev.getMonitoredHost()) {
0N/A logger.stopLogging();
0N/A }
0N/A }
0N/A };
0N/A
0N/A if (vmId.getLocalVmId() != 0) {
0N/A monitoredHost.addHostListener(terminator);
0N/A }
0N/A
0N/A logger.logSamples(formatter, arguments.headerRate(),
0N/A arguments.sampleInterval(), arguments.sampleCount(),
0N/A System.out);
0N/A
0N/A // detach from host events and from the monitored target jvm
0N/A if (terminator != null) {
0N/A monitoredHost.removeHostListener(terminator);
0N/A }
0N/A monitoredHost.detach(monitoredVm);
0N/A }
0N/A}