jdk/src/share/classes/sun/tools/jstat/Jstat.java
author sjiang
Thu, 12 Jun 2014 10:32:19 +0200
changeset 24871 224e298c3978
parent 23718 0056bc7134f2
child 25522 10d789df41bb
permissions -rw-r--r--
8044865: Fix raw and unchecked lint warnings in management-related code Reviewed-by: darcy, sla, egahlin, dfuchs
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 sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Application to output jvmstat statistics exported by a target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Virtual Machine. The jstat tool gets its inspiration from the suite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * various UNIX platforms.
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 Jstat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static Arguments arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            arguments = new Arguments(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            Arguments.printUsage(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (arguments.isHelp()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            Arguments.printUsage(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (arguments.isOptions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            OptionLister ol = new OptionLister(arguments.optionsSources());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            ol.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (arguments.isList()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                logNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } else if (arguments.isSnap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                logSnapShot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                logSamples();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (MonitorException e) {
23718
0056bc7134f2 8028354: jstat does not provide enough information when fail to read data from remote JVM
sla
parents: 7668
diff changeset
    73
            e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static void logNames() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        VmIdentifier vmId = arguments.vmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int interval = arguments.sampleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        JStatLogger logger = new JStatLogger(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        logger.printNames(arguments.counterNames(), arguments.comparator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                          arguments.showUnsupported(), System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static void logSnapShot() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        VmIdentifier vmId = arguments.vmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int interval = arguments.sampleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        JStatLogger logger = new JStatLogger(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        logger.printSnapShot(arguments.counterNames(), arguments.comparator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                             arguments.isVerbose(), arguments.showUnsupported(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                             System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static void logSamples() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        final VmIdentifier vmId = arguments.vmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int interval = arguments.sampleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        final MonitoredHost monitoredHost =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                MonitoredHost.getMonitoredHost(vmId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        final JStatLogger logger = new JStatLogger(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        OutputFormatter formatter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (arguments.isSpecialOption()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            OptionFormat format = arguments.optionFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            formatter = new OptionOutputFormatter(monitoredVm, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            Collections.sort(logged, arguments.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            List<Monitor> constants = new ArrayList<Monitor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 23718
diff changeset
   119
            for (Iterator<Monitor> i = logged.iterator(); i.hasNext(); /* empty */) {
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 23718
diff changeset
   120
                Monitor m = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (!(m.isSupported() || arguments.showUnsupported())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (m.getVariability() == Variability.CONSTANT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if (arguments.printConstants()) constants.add(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } else if ((m.getUnits() == Units.STRING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        && !arguments.printStrings()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (!constants.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                logger.printList(constants, arguments.isVerbose(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                 arguments.showUnsupported(), System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (!logged.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (logged.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            formatter = new RawOutputFormatter(logged,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                               arguments.printStrings());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // handle user termination requests by stopping sampling loops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Runtime.getRuntime().addShutdownHook(new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // handle target termination events for targets other than ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        HostListener terminator = new HostListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            public void vmStatusChanged(VmStatusChangeEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                Integer lvmid = new Integer(vmId.getLocalVmId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (ev.getTerminated().contains(lvmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                } else if (!ev.getActive().contains(lvmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            public void disconnected(HostEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (monitoredHost == ev.getMonitoredHost()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (vmId.getLocalVmId() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            monitoredHost.addHostListener(terminator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        logger.logSamples(formatter, arguments.headerRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                          arguments.sampleInterval(), arguments.sampleCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                          System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // detach from host events and from the monitored target jvm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (terminator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            monitoredHost.removeHostListener(terminator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}