History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/tools/jstat/Jstat.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/tools/jstat/Jstat.java+199−0
@@ -0,0 +1,199 @@
1+/*
2+ * Copyright (c) 2004, 2010, Oracle and/or its affiliates. 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. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * 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+
32+/**
33+ * Application to output jvmstat statistics exported by a target Java
34+ * Virtual Machine. The jstat tool gets its inspiration from the suite
35+ * of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in
36+ * various UNIX platforms.
37+ *
38+ * @author Brian Doherty
39+ * @since 1.5
40+ */
41+public class Jstat {
42+ private static Arguments arguments;
43+
44+ public static void main(String[] args) {
45+ try {
46+ arguments = new Arguments(args);
47+ } catch (IllegalArgumentException e) {
48+ System.err.println(e.getMessage());
49+ Arguments.printUsage(System.err);
50+ System.exit(1);
51+ }
52+
53+ if (arguments.isHelp()) {
54+ Arguments.printUsage(System.out);
55+ System.exit(0);
56+ }
57+
58+ if (arguments.isOptions()) {
59+ OptionLister ol = new OptionLister(arguments.optionsSources());
60+ ol.print(System.out);
61+ System.exit(0);
62+ }
63+
64+ try {
65+ if (arguments.isList()) {
66+ logNames();
67+ } else if (arguments.isSnap()) {
68+ logSnapShot();
69+ } else {
70+ logSamples();
71+ }
72+ } catch (MonitorException e) {
73+ if (e.getMessage() != null) {
74+ System.err.println(e.getMessage());
75+ } else {
76+ Throwable cause = e.getCause();
77+ if ((cause != null) && (cause.getMessage() != null)) {
78+ System.err.println(cause.getMessage());
79+ } else {
80+ e.printStackTrace();
81+ }
82+ }
83+ System.exit(1);
84+ }
85+ System.exit(0);
86+ }
87+
88+ static void logNames() throws MonitorException {
89+ VmIdentifier vmId = arguments.vmId();
90+ int interval = arguments.sampleInterval();
91+ MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
92+ MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
93+ JStatLogger logger = new JStatLogger(monitoredVm);
94+ logger.printNames(arguments.counterNames(), arguments.comparator(),
95+ arguments.showUnsupported(), System.out);
96+ monitoredHost.detach(monitoredVm);
97+ }
98+
99+ static void logSnapShot() throws MonitorException {
100+ VmIdentifier vmId = arguments.vmId();
101+ int interval = arguments.sampleInterval();
102+ MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
103+ MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
104+ JStatLogger logger = new JStatLogger(monitoredVm);
105+ logger.printSnapShot(arguments.counterNames(), arguments.comparator(),
106+ arguments.isVerbose(), arguments.showUnsupported(),
107+ System.out);
108+ monitoredHost.detach(monitoredVm);
109+ }
110+
111+ static void logSamples() throws MonitorException {
112+ final VmIdentifier vmId = arguments.vmId();
113+ int interval = arguments.sampleInterval();
114+ final MonitoredHost monitoredHost =
115+ MonitoredHost.getMonitoredHost(vmId);
116+ MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
117+ final JStatLogger logger = new JStatLogger(monitoredVm);
118+ OutputFormatter formatter = null;
119+
120+ if (arguments.isSpecialOption()) {
121+ OptionFormat format = arguments.optionFormat();
122+ formatter = new OptionOutputFormatter(monitoredVm, format);
123+ } else {
124+ List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames());
125+ Collections.sort(logged, arguments.comparator());
126+ List<Monitor> constants = new ArrayList<Monitor>();
127+
128+ for (Iterator i = logged.iterator(); i.hasNext(); /* empty */) {
129+ Monitor m = (Monitor)i.next();
130+ if (!(m.isSupported() || arguments.showUnsupported())) {
131+ i.remove();
132+ continue;
133+ }
134+ if (m.getVariability() == Variability.CONSTANT) {
135+ i.remove();
136+ if (arguments.printConstants()) constants.add(m);
137+ } else if ((m.getUnits() == Units.STRING)
138+ && !arguments.printStrings()) {
139+ i.remove();
140+ }
141+ }
142+
143+ if (!constants.isEmpty()) {
144+ logger.printList(constants, arguments.isVerbose(),
145+ arguments.showUnsupported(), System.out);
146+ if (!logged.isEmpty()) {
147+ System.out.println();
148+ }
149+ }
150+
151+ if (logged.isEmpty()) {
152+ monitoredHost.detach(monitoredVm);
153+ return;
154+ }
155+
156+ formatter = new RawOutputFormatter(logged,
157+ arguments.printStrings());
158+ }
159+
160+ // handle user termination requests by stopping sampling loops
161+ Runtime.getRuntime().addShutdownHook(new Thread() {
162+ public void run() {
163+ logger.stopLogging();
164+ }
165+ });
166+
167+ // handle target termination events for targets other than ourself
168+ HostListener terminator = new HostListener() {
169+ public void vmStatusChanged(VmStatusChangeEvent ev) {
170+ Integer lvmid = new Integer(vmId.getLocalVmId());
171+ if (ev.getTerminated().contains(lvmid)) {
172+ logger.stopLogging();
173+ } else if (!ev.getActive().contains(lvmid)) {
174+ logger.stopLogging();
175+ }
176+ }
177+
178+ public void disconnected(HostEvent ev) {
179+ if (monitoredHost == ev.getMonitoredHost()) {
180+ logger.stopLogging();
181+ }
182+ }
183+ };
184+
185+ if (vmId.getLocalVmId() != 0) {
186+ monitoredHost.addHostListener(terminator);
187+ }
188+
189+ logger.logSamples(formatter, arguments.headerRate(),
190+ arguments.sampleInterval(), arguments.sampleCount(),
191+ System.out);
192+
193+ // detach from host events and from the monitored target jvm
194+ if (terminator != null) {
195+ monitoredHost.removeHostListener(terminator);
196+ }
197+ monitoredHost.detach(monitoredVm);
198+ }
199+}