Showing the changes to java/pkg/sun/tools/jstat/JStatLogger.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,159 @@ | ||
| 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 java.io.*; | |
| 30 | +import sun.jvmstat.monitor.*; | |
| 31 | +import sun.jvmstat.monitor.event.*; | |
| 32 | +import java.util.regex.PatternSyntaxException; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * Class to sample and output various jvmstat statistics for a target Java | |
| 36 | + * a target Java Virtual Machine. | |
| 37 | + * | |
| 38 | + * @author Brian Doherty | |
| 39 | + * @since 1.5 | |
| 40 | + */ | |
| 41 | +public class JStatLogger { | |
| 42 | + | |
| 43 | + private MonitoredVm monitoredVm; | |
| 44 | + private volatile boolean active = true; | |
| 45 | + | |
| 46 | + public JStatLogger(MonitoredVm monitoredVm) { | |
| 47 | + this.monitoredVm = monitoredVm; | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * print the monitors that match the given monitor name pattern string. | |
| 52 | + */ | |
| 53 | + public void printNames(String names, Comparator<Monitor> comparator, | |
| 54 | + boolean showUnsupported, PrintStream out) | |
| 55 | + throws MonitorException, PatternSyntaxException { | |
| 56 | + | |
| 57 | + // get the set of all monitors | |
| 58 | + List<Monitor> items = monitoredVm.findByPattern(names); | |
| 59 | + Collections.sort(items, comparator); | |
| 60 | + | |
| 61 | + for (Monitor m: items) { | |
| 62 | + if (!(m.isSupported() || showUnsupported)) { | |
| 63 | + continue; | |
| 64 | + } | |
| 65 | + out.println(m.getName()); | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * print name=value pairs for the given list of monitors. | |
| 71 | + */ | |
| 72 | + public void printSnapShot(String names, Comparator<Monitor> comparator, | |
| 73 | + boolean verbose, boolean showUnsupported, | |
| 74 | + PrintStream out) | |
| 75 | + throws MonitorException, PatternSyntaxException { | |
| 76 | + | |
| 77 | + // get the set of all monitors | |
| 78 | + List<Monitor> items = monitoredVm.findByPattern(names); | |
| 79 | + Collections.sort(items, comparator); | |
| 80 | + | |
| 81 | + printList(items, verbose, showUnsupported, out); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * print name=value pairs for the given list of monitors. | |
| 86 | + */ | |
| 87 | + public void printList(List<Monitor> list, boolean verbose, boolean showUnsupported, | |
| 88 | + PrintStream out) | |
| 89 | + throws MonitorException { | |
| 90 | + | |
| 91 | + // print out the name of each available counter | |
| 92 | + for (Monitor m: list ) { | |
| 93 | + | |
| 94 | + if (!(m.isSupported() || showUnsupported)) { | |
| 95 | + continue; | |
| 96 | + } | |
| 97 | + | |
| 98 | + StringBuilder buffer = new StringBuilder(); | |
| 99 | + buffer.append(m.getName()).append("="); | |
| 100 | + | |
| 101 | + if (m instanceof StringMonitor) { | |
| 102 | + buffer.append("\"").append(m.getValue()).append("\""); | |
| 103 | + } else { | |
| 104 | + buffer.append(m.getValue()); | |
| 105 | + } | |
| 106 | + | |
| 107 | + if (verbose) { | |
| 108 | + buffer.append(" ").append(m.getUnits()); | |
| 109 | + buffer.append(" ").append(m.getVariability()); | |
| 110 | + buffer.append(" ").append(m.isSupported() ? "Supported" | |
| 111 | + : "Unsupported"); | |
| 112 | + } | |
| 113 | + out.println(buffer); | |
| 114 | + } | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * method to for asynchronous termination of sampling loops | |
| 119 | + */ | |
| 120 | + public void stopLogging() { | |
| 121 | + active = false; | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * print samples according to the given format. | |
| 126 | + */ | |
| 127 | + public void logSamples(OutputFormatter formatter, int headerRate, | |
| 128 | + int sampleInterval, int sampleCount, PrintStream out) | |
| 129 | + throws MonitorException { | |
| 130 | + | |
| 131 | + long iterationCount = 0; | |
| 132 | + int printHeaderCount = 0; | |
| 133 | + | |
| 134 | + // if printHeader == 0, then only an initial column header is desired. | |
| 135 | + int printHeader = headerRate; | |
| 136 | + if (printHeader == 0) { | |
| 137 | + // print the column header once, disable future printing | |
| 138 | + out.println(formatter.getHeader()); | |
| 139 | + printHeader = -1; | |
| 140 | + } | |
| 141 | + | |
| 142 | + while (active) { | |
| 143 | + // check if it's time to print another column header | |
| 144 | + if (printHeader > 0 && --printHeaderCount <= 0) { | |
| 145 | + printHeaderCount = printHeader; | |
| 146 | + out.println(formatter.getHeader()); | |
| 147 | + } | |
| 148 | + | |
| 149 | + out.println(formatter.getRow()); | |
| 150 | + | |
| 151 | + // check if we've hit the specified sample count | |
| 152 | + if (sampleCount > 0 && ++iterationCount >= sampleCount) { | |
| 153 | + break; | |
| 154 | + } | |
| 155 | + | |
| 156 | + try { Thread.sleep(sampleInterval); } catch (Exception e) { }; | |
| 157 | + } | |
| 158 | + } | |
| 159 | +} | |