001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import java.util.*;
029: import sun.jvmstat.monitor.*;
030: import sun.jvmstat.monitor.event.*;
031: import sun.management.counter.Variability;
032: import sun.management.counter.Units;
033:
034: /**
035: * Application to output jvmstat statistics exported by a target Java
036: * Virtual Machine. The jstat tool gets its inspiration from the suite
037: * of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in
038: * various UNIX platforms.
039: *
040: * @author Brian Doherty
041: * @version 1.10, 05/09/07
042: * @since 1.5
043: */
044: public class Jstat {
045: private static Arguments arguments;
046:
047: public static void main(String[] args) {
048: try {
049: arguments = new Arguments(args);
050: } catch (IllegalArgumentException e) {
051: System.err.println(e.getMessage());
052: Arguments.printUsage(System.err);
053: System.exit(1);
054: }
055:
056: if (arguments.isHelp()) {
057: Arguments.printUsage(System.out);
058: System.exit(0);
059: }
060:
061: if (arguments.isOptions()) {
062: OptionLister ol = new OptionLister(arguments
063: .optionsSources());
064: ol.print(System.out);
065: System.exit(0);
066: }
067:
068: try {
069: if (arguments.isList()) {
070: logNames();
071: } else if (arguments.isSnap()) {
072: logSnapShot();
073: } else {
074: logSamples();
075: }
076: } catch (MonitorException e) {
077: if (e.getMessage() != null) {
078: System.err.println(e.getMessage());
079: } else {
080: Throwable cause = e.getCause();
081: if ((cause != null) && (cause.getMessage() != null)) {
082: System.err.println(cause.getMessage());
083: } else {
084: e.printStackTrace();
085: }
086: }
087: System.exit(1);
088: }
089: System.exit(0);
090: }
091:
092: static void logNames() throws MonitorException {
093: VmIdentifier vmId = arguments.vmId();
094: int interval = arguments.sampleInterval();
095: MonitoredHost monitoredHost = MonitoredHost
096: .getMonitoredHost(vmId);
097: MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId,
098: interval);
099: JStatLogger logger = new JStatLogger(monitoredVm);
100: logger.printNames(arguments.counterNames(), arguments
101: .comparator(), arguments.showUnsupported(), System.out);
102: monitoredHost.detach(monitoredVm);
103: }
104:
105: static void logSnapShot() throws MonitorException {
106: VmIdentifier vmId = arguments.vmId();
107: int interval = arguments.sampleInterval();
108: MonitoredHost monitoredHost = MonitoredHost
109: .getMonitoredHost(vmId);
110: MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId,
111: interval);
112: JStatLogger logger = new JStatLogger(monitoredVm);
113: logger.printSnapShot(arguments.counterNames(), arguments
114: .comparator(), arguments.isVerbose(), arguments
115: .showUnsupported(), System.out);
116: monitoredHost.detach(monitoredVm);
117: }
118:
119: static void logSamples() throws MonitorException {
120: final VmIdentifier vmId = arguments.vmId();
121: int interval = arguments.sampleInterval();
122: final MonitoredHost monitoredHost = MonitoredHost
123: .getMonitoredHost(vmId);
124: MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId,
125: interval);
126: final JStatLogger logger = new JStatLogger(monitoredVm);
127: OutputFormatter formatter = null;
128:
129: if (arguments.isSpecialOption()) {
130: OptionFormat format = arguments.optionFormat();
131: formatter = new OptionOutputFormatter(monitoredVm, format);
132: } else {
133: List<Monitor> logged = monitoredVm.findByPattern(arguments
134: .counterNames());
135: Collections.sort(logged, arguments.comparator());
136: List<Monitor> constants = new ArrayList<Monitor>();
137:
138: for (Iterator i = logged.iterator(); i.hasNext(); /* empty */) {
139: Monitor m = (Monitor) i.next();
140: if (!(m.isSupported() || arguments.showUnsupported())) {
141: i.remove();
142: continue;
143: }
144: if (m.getVariability() == Variability.CONSTANT) {
145: i.remove();
146: if (arguments.printConstants())
147: constants.add(m);
148: } else if ((m.getUnits() == Units.STRING)
149: && !arguments.printStrings()) {
150: i.remove();
151: }
152: }
153:
154: if (!constants.isEmpty()) {
155: logger.printList(constants, arguments.isVerbose(),
156: arguments.showUnsupported(), System.out);
157: if (!logged.isEmpty()) {
158: System.out.println();
159: }
160: }
161:
162: if (logged.isEmpty()) {
163: monitoredHost.detach(monitoredVm);
164: return;
165: }
166:
167: formatter = new RawOutputFormatter(logged, arguments
168: .printStrings());
169: }
170:
171: // handle user termination requests by stopping sampling loops
172: Runtime.getRuntime().addShutdownHook(new Thread() {
173: public void run() {
174: logger.stopLogging();
175: }
176: });
177:
178: // handle target termination events for targets other than ourself
179: HostListener terminator = new HostListener() {
180: public void vmStatusChanged(VmStatusChangeEvent ev) {
181: Integer lvmid = new Integer(vmId.getLocalVmId());
182: if (ev.getTerminated().contains(lvmid)) {
183: logger.stopLogging();
184: } else if (!ev.getActive().contains(lvmid)) {
185: logger.stopLogging();
186: }
187: }
188:
189: public void disconnected(HostEvent ev) {
190: if (monitoredHost == ev.getMonitoredHost()) {
191: logger.stopLogging();
192: }
193: }
194: };
195:
196: if (vmId.getLocalVmId() != 0) {
197: monitoredHost.addHostListener(terminator);
198: }
199:
200: logger.logSamples(formatter, arguments.headerRate(), arguments
201: .sampleInterval(), arguments.sampleCount(), System.out);
202:
203: // detach from host events and from the monitored target jvm
204: if (terminator != null) {
205: monitoredHost.removeHostListener(terminator);
206: }
207: monitoredHost.detach(monitoredVm);
208: }
209: }
|