src/jdk.jcmd/share/classes/sun/tools/jstat/JStatLogger.java
author jdv
Wed, 18 Apr 2018 13:22:53 +0530
changeset 49996 39dc39093c5e
parent 47216 71c04702a3d5
permissions -rw-r--r--
6574555: PNGImageWriter incorrectly sets bKGD chunk Reviewed-by: prr, pnarayanan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5627
diff changeset
     2
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jstat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.jvmstat.monitor.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.regex.PatternSyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Class to sample and output various jvmstat statistics for a target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * a target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class JStatLogger {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private MonitoredVm monitoredVm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private volatile boolean active = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public JStatLogger(MonitoredVm monitoredVm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.monitoredVm = monitoredVm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * print the monitors that match the given monitor name pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public void printNames(String names, Comparator<Monitor> comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                           boolean showUnsupported, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                throws MonitorException, PatternSyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // get the set of all monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        List<Monitor> items = monitoredVm.findByPattern(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Collections.sort(items, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (Monitor m: items) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (!(m.isSupported() || showUnsupported)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            out.println(m.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * print name=value pairs for the given list of monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void printSnapShot(String names, Comparator<Monitor> comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                              boolean verbose, boolean showUnsupported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                              PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throws MonitorException, PatternSyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // get the set of all monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        List<Monitor> items = monitoredVm.findByPattern(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Collections.sort(items, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        printList(items, verbose, showUnsupported, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * print name=value pairs for the given list of monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void printList(List<Monitor> list, boolean verbose, boolean showUnsupported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                          PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // print out the name of each available counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (Monitor m: list ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (!(m.isSupported() || showUnsupported)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            StringBuilder buffer = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            buffer.append(m.getName()).append("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (m instanceof StringMonitor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                buffer.append("\"").append(m.getValue()).append("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                buffer.append(m.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                buffer.append(" ").append(m.getUnits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                buffer.append(" ").append(m.getVariability());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                buffer.append(" ").append(m.isSupported() ? "Supported"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                                          : "Unsupported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            out.println(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * method to for asynchronous termination of sampling loops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void stopLogging() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        active = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * print samples according to the given format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void logSamples(OutputFormatter formatter, int headerRate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                           int sampleInterval, int sampleCount, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        long iterationCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int printHeaderCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // if printHeader == 0, then only an initial column header is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int printHeader = headerRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (printHeader == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // print the column header once, disable future printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            out.println(formatter.getHeader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            printHeader = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        while (active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
          // check if it's time to print another column header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
          if (printHeader > 0 && --printHeaderCount <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
              printHeaderCount = printHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
              out.println(formatter.getHeader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          out.println(formatter.getRow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
          // check if we've hit the specified sample count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
          if (sampleCount > 0 && ++iterationCount >= sampleCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
          try { Thread.sleep(sampleInterval); } catch (Exception e) { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}