J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.jstat; |
| 27 | |
| 28 | import java.util.*; |
| 29 | import sun.jvmstat.monitor.*; |
| 30 | import sun.jvmstat.monitor.event.*; |
| 31 | import sun.management.counter.Variability; |
| 32 | import sun.management.counter.Units; |
| 33 | |
| 34 | /** |
| 35 | * Application to output jvmstat statistics exported by a target Java |
| 36 | * Virtual Machine. The jstat tool gets its inspiration from the suite |
| 37 | * of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in |
| 38 | * various UNIX platforms. |
| 39 | * |
| 40 | * @author Brian Doherty |
| 41 | * @since 1.5 |
| 42 | */ |
| 43 | public class Jstat { |
| 44 | private static Arguments arguments; |
| 45 | |
| 46 | public static void main(String[] args) { |
| 47 | try { |
| 48 | arguments = new Arguments(args); |
| 49 | } catch (IllegalArgumentException e) { |
| 50 | System.err.println(e.getMessage()); |
| 51 | Arguments.printUsage(System.err); |
| 52 | System.exit(1); |
| 53 | } |
| 54 | |
| 55 | if (arguments.isHelp()) { |
| 56 | Arguments.printUsage(System.out); |
| 57 | System.exit(0); |
| 58 | } |
| 59 | |
| 60 | if (arguments.isOptions()) { |
| 61 | OptionLister ol = new OptionLister(arguments.optionsSources()); |
| 62 | ol.print(System.out); |
| 63 | System.exit(0); |
| 64 | } |
| 65 | |
| 66 | try { |
| 67 | if (arguments.isList()) { |
| 68 | logNames(); |
| 69 | } else if (arguments.isSnap()) { |
| 70 | logSnapShot(); |
| 71 | } else { |
| 72 | logSamples(); |
| 73 | } |
| 74 | } catch (MonitorException e) { |
| 75 | if (e.getMessage() != null) { |
| 76 | System.err.println(e.getMessage()); |
| 77 | } else { |
| 78 | Throwable cause = e.getCause(); |
| 79 | if ((cause != null) && (cause.getMessage() != null)) { |
| 80 | System.err.println(cause.getMessage()); |
| 81 | } else { |
| 82 | e.printStackTrace(); |
| 83 | } |
| 84 | } |
| 85 | System.exit(1); |
| 86 | } |
| 87 | System.exit(0); |
| 88 | } |
| 89 | |
| 90 | static void logNames() throws MonitorException { |
| 91 | VmIdentifier vmId = arguments.vmId(); |
| 92 | int interval = arguments.sampleInterval(); |
| 93 | MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId); |
| 94 | MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval); |
| 95 | JStatLogger logger = new JStatLogger(monitoredVm); |
| 96 | logger.printNames(arguments.counterNames(), arguments.comparator(), |
| 97 | arguments.showUnsupported(), System.out); |
| 98 | monitoredHost.detach(monitoredVm); |
| 99 | } |
| 100 | |
| 101 | static void logSnapShot() throws MonitorException { |
| 102 | VmIdentifier vmId = arguments.vmId(); |
| 103 | int interval = arguments.sampleInterval(); |
| 104 | MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId); |
| 105 | MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval); |
| 106 | JStatLogger logger = new JStatLogger(monitoredVm); |
| 107 | logger.printSnapShot(arguments.counterNames(), arguments.comparator(), |
| 108 | arguments.isVerbose(), arguments.showUnsupported(), |
| 109 | System.out); |
| 110 | monitoredHost.detach(monitoredVm); |
| 111 | } |
| 112 | |
| 113 | static void logSamples() throws MonitorException { |
| 114 | final VmIdentifier vmId = arguments.vmId(); |
| 115 | int interval = arguments.sampleInterval(); |
| 116 | final MonitoredHost monitoredHost = |
| 117 | MonitoredHost.getMonitoredHost(vmId); |
| 118 | MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval); |
| 119 | final JStatLogger logger = new JStatLogger(monitoredVm); |
| 120 | OutputFormatter formatter = null; |
| 121 | |
| 122 | if (arguments.isSpecialOption()) { |
| 123 | OptionFormat format = arguments.optionFormat(); |
| 124 | formatter = new OptionOutputFormatter(monitoredVm, format); |
| 125 | } else { |
| 126 | List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames()); |
| 127 | Collections.sort(logged, arguments.comparator()); |
| 128 | List<Monitor> constants = new ArrayList<Monitor>(); |
| 129 | |
| 130 | for (Iterator i = logged.iterator(); i.hasNext(); /* empty */) { |
| 131 | Monitor m = (Monitor)i.next(); |
| 132 | if (!(m.isSupported() || arguments.showUnsupported())) { |
| 133 | i.remove(); |
| 134 | continue; |
| 135 | } |
| 136 | if (m.getVariability() == Variability.CONSTANT) { |
| 137 | i.remove(); |
| 138 | if (arguments.printConstants()) constants.add(m); |
| 139 | } else if ((m.getUnits() == Units.STRING) |
| 140 | && !arguments.printStrings()) { |
| 141 | i.remove(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!constants.isEmpty()) { |
| 146 | logger.printList(constants, arguments.isVerbose(), |
| 147 | arguments.showUnsupported(), System.out); |
| 148 | if (!logged.isEmpty()) { |
| 149 | System.out.println(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (logged.isEmpty()) { |
| 154 | monitoredHost.detach(monitoredVm); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | formatter = new RawOutputFormatter(logged, |
| 159 | arguments.printStrings()); |
| 160 | } |
| 161 | |
| 162 | // handle user termination requests by stopping sampling loops |
| 163 | Runtime.getRuntime().addShutdownHook(new Thread() { |
| 164 | public void run() { |
| 165 | logger.stopLogging(); |
| 166 | } |
| 167 | }); |
| 168 | |
| 169 | // handle target termination events for targets other than ourself |
| 170 | HostListener terminator = new HostListener() { |
| 171 | public void vmStatusChanged(VmStatusChangeEvent ev) { |
| 172 | Integer lvmid = new Integer(vmId.getLocalVmId()); |
| 173 | if (ev.getTerminated().contains(lvmid)) { |
| 174 | logger.stopLogging(); |
| 175 | } else if (!ev.getActive().contains(lvmid)) { |
| 176 | logger.stopLogging(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | public void disconnected(HostEvent ev) { |
| 181 | if (monitoredHost == ev.getMonitoredHost()) { |
| 182 | logger.stopLogging(); |
| 183 | } |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | if (vmId.getLocalVmId() != 0) { |
| 188 | monitoredHost.addHostListener(terminator); |
| 189 | } |
| 190 | |
| 191 | logger.logSamples(formatter, arguments.headerRate(), |
| 192 | arguments.sampleInterval(), arguments.sampleCount(), |
| 193 | System.out); |
| 194 | |
| 195 | // detach from host events and from the monitored target jvm |
| 196 | if (terminator != null) { |
| 197 | monitoredHost.removeHostListener(terminator); |
| 198 | } |
| 199 | monitoredHost.detach(monitoredVm); |
| 200 | } |
| 201 | } |