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