Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/JStatLogger.java
41159 views
/*1* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstat;2627import java.util.*;28import java.io.*;29import sun.jvmstat.monitor.*;30import sun.jvmstat.monitor.event.*;31import java.util.regex.PatternSyntaxException;3233/**34* Class to sample and output various jvmstat statistics for a target Java35* a target Java Virtual Machine.36*37* @author Brian Doherty38* @since 1.539*/40public class JStatLogger {4142private MonitoredVm monitoredVm;43private volatile boolean active = true;4445public JStatLogger(MonitoredVm monitoredVm) {46this.monitoredVm = monitoredVm;47}4849/**50* print the monitors that match the given monitor name pattern string.51*/52public void printNames(String names, Comparator<Monitor> comparator,53boolean showUnsupported, PrintStream out)54throws MonitorException, PatternSyntaxException {5556// get the set of all monitors57List<Monitor> items = monitoredVm.findByPattern(names);58Collections.sort(items, comparator);5960for (Monitor m: items) {61if (!(m.isSupported() || showUnsupported)) {62continue;63}64out.println(m.getName());65}66}6768/**69* print name=value pairs for the given list of monitors.70*/71public void printSnapShot(String names, Comparator<Monitor> comparator,72boolean verbose, boolean showUnsupported,73PrintStream out)74throws MonitorException, PatternSyntaxException {7576// get the set of all monitors77List<Monitor> items = monitoredVm.findByPattern(names);78Collections.sort(items, comparator);7980printList(items, verbose, showUnsupported, out);81}8283/**84* print name=value pairs for the given list of monitors.85*/86public void printList(List<Monitor> list, boolean verbose, boolean showUnsupported,87PrintStream out)88throws MonitorException {8990// print out the name of each available counter91for (Monitor m: list ) {9293if (!(m.isSupported() || showUnsupported)) {94continue;95}9697StringBuilder buffer = new StringBuilder();98buffer.append(m.getName()).append("=");99100if (m instanceof StringMonitor) {101buffer.append("\"").append(m.getValue()).append("\"");102} else {103buffer.append(m.getValue());104}105106if (verbose) {107buffer.append(" ").append(m.getUnits());108buffer.append(" ").append(m.getVariability());109buffer.append(" ").append(m.isSupported() ? "Supported"110: "Unsupported");111}112out.println(buffer);113}114}115116/**117* method to for asynchronous termination of sampling loops118*/119public void stopLogging() {120active = false;121}122123/**124* print samples according to the given format.125*/126public void logSamples(OutputFormatter formatter, int headerRate,127int sampleInterval, int sampleCount, PrintStream out)128throws MonitorException {129130long iterationCount = 0;131int printHeaderCount = 0;132133// if printHeader == 0, then only an initial column header is desired.134int printHeader = headerRate;135if (printHeader == 0) {136// print the column header once, disable future printing137out.println(formatter.getHeader());138printHeader = -1;139}140141while (active) {142// check if it's time to print another column header143if (printHeader > 0 && --printHeaderCount <= 0) {144printHeaderCount = printHeader;145out.println(formatter.getHeader());146}147148out.println(formatter.getRow());149150// check if we've hit the specified sample count151if (sampleCount > 0 && ++iterationCount >= sampleCount) {152break;153}154155try { Thread.sleep(sampleInterval); } catch (Exception e) { };156}157}158}159160161