blob: 254b4c4c12aa1a7a12a8612bae4de60b3c425067 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07002 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
J. Duke319a3b92007-12-01 00:00:00 +00003 * 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'Hairfe008ae2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
J. Duke319a3b92007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
J. Duke319a3b92007-12-01 00:00:00 +000010 *
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'Hairfe008ae2010-05-25 15:58:33 -070021 * 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. Duke319a3b92007-12-01 00:00:00 +000024 */
25
26package sun.tools.jstat;
27
28import java.util.*;
29import java.io.*;
30import sun.jvmstat.monitor.*;
31import sun.jvmstat.monitor.event.*;
32import sun.management.counter.Units;
33import sun.management.counter.Variability;
34import 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 */
43public 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}