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 java.io.*;
030: import sun.jvmstat.monitor.*;
031: import sun.jvmstat.monitor.event.*;
032: import sun.management.counter.Units;
033: import sun.management.counter.Variability;
034: import java.util.regex.PatternSyntaxException;
035:
036: /**
037: * Class to sample and output various jvmstat statistics for a target Java
038: * a target Java Virtual Machine.
039: *
040: * @author Brian Doherty
041: * @version 1.9, 05/09/07
042: * @since 1.5
043: */
044: public class JStatLogger {
045:
046: private MonitoredVm monitoredVm;
047: private volatile boolean active = true;
048:
049: public JStatLogger(MonitoredVm monitoredVm) {
050: this .monitoredVm = monitoredVm;
051: }
052:
053: /**
054: * print the monitors that match the given monitor name pattern string.
055: */
056: public void printNames(String names,
057: Comparator<Monitor> comparator, boolean showUnsupported,
058: PrintStream out) throws MonitorException,
059: PatternSyntaxException {
060:
061: // get the set of all monitors
062: List<Monitor> items = monitoredVm.findByPattern(names);
063: Collections.sort(items, comparator);
064:
065: for (Monitor m : items) {
066: if (!(m.isSupported() || showUnsupported)) {
067: continue;
068: }
069: out.println(m.getName());
070: }
071: }
072:
073: /**
074: * print name=value pairs for the given list of monitors.
075: */
076: public void printSnapShot(String names,
077: Comparator<Monitor> comparator, boolean verbose,
078: boolean showUnsupported, PrintStream out)
079: throws MonitorException, PatternSyntaxException {
080:
081: // get the set of all monitors
082: List<Monitor> items = monitoredVm.findByPattern(names);
083: Collections.sort(items, comparator);
084:
085: printList(items, verbose, showUnsupported, out);
086: }
087:
088: /**
089: * print name=value pairs for the given list of monitors.
090: */
091: public void printList(List<Monitor> list, boolean verbose,
092: boolean showUnsupported, PrintStream out)
093: throws MonitorException {
094:
095: // print out the name of each available counter
096: for (Monitor m : list) {
097:
098: if (!(m.isSupported() || showUnsupported)) {
099: continue;
100: }
101:
102: StringBuilder buffer = new StringBuilder();
103: buffer.append(m.getName()).append("=");
104:
105: if (m instanceof StringMonitor) {
106: buffer.append("\"").append(m.getValue()).append("\"");
107: } else {
108: buffer.append(m.getValue());
109: }
110:
111: if (verbose) {
112: buffer.append(" ").append(m.getUnits());
113: buffer.append(" ").append(m.getVariability());
114: buffer.append(" ").append(
115: m.isSupported() ? "Supported" : "Unsupported");
116: }
117: out.println(buffer);
118: }
119: }
120:
121: /**
122: * method to for asynchronous termination of sampling loops
123: */
124: public void stopLogging() {
125: active = false;
126: }
127:
128: /**
129: * print samples according to the given format.
130: */
131: public void logSamples(OutputFormatter formatter, int headerRate,
132: int sampleInterval, int sampleCount, PrintStream out)
133: throws MonitorException {
134:
135: long iterationCount = 0;
136: int printHeaderCount = 0;
137:
138: // if printHeader == 0, then only an initial column header is desired.
139: int printHeader = headerRate;
140: if (printHeader == 0) {
141: // print the column header once, disable future printing
142: out.println(formatter.getHeader());
143: printHeader = -1;
144: }
145:
146: while (active) {
147: // check if it's time to print another column header
148: if (printHeader > 0 && --printHeaderCount <= 0) {
149: printHeaderCount = printHeader;
150: out.println(formatter.getHeader());
151: }
152:
153: out.println(formatter.getRow());
154:
155: // check if we've hit the specified sample count
156: if (sampleCount > 0 && ++iterationCount >= sampleCount) {
157: break;
158: }
159:
160: try {
161: Thread.sleep(sampleInterval);
162: } catch (Exception e) {
163: }
164: ;
165: }
166: }
167: }
|