jdk/src/share/classes/sun/tools/jstat/Jstat.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 5627 e636ac7a63a4
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 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
import sun.management.counter.Variability;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.management.counter.Units;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Application to output jvmstat statistics exported by a target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Virtual Machine. The jstat tool gets its inspiration from the suite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * of 'stat' tools, such as vmstat, iostat, mpstat, etc., available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * various UNIX platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class Jstat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static Arguments arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            arguments = new Arguments(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            Arguments.printUsage(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (arguments.isHelp()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            Arguments.printUsage(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (arguments.isOptions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            OptionLister ol = new OptionLister(arguments.optionsSources());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            ol.print(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (arguments.isList()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                logNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            } else if (arguments.isSnap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                logSnapShot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                logSamples();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (MonitorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if ((cause != null) && (cause.getMessage() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    System.err.println(cause.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.exit(0);
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 logNames() 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.printNames(arguments.counterNames(), arguments.comparator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                          arguments.showUnsupported(), System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void logSnapShot() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        VmIdentifier vmId = arguments.vmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int interval = arguments.sampleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        JStatLogger logger = new JStatLogger(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        logger.printSnapShot(arguments.counterNames(), arguments.comparator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                             arguments.isVerbose(), arguments.showUnsupported(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                             System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void logSamples() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        final VmIdentifier vmId = arguments.vmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int interval = arguments.sampleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        final MonitoredHost monitoredHost =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                MonitoredHost.getMonitoredHost(vmId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        final JStatLogger logger = new JStatLogger(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        OutputFormatter formatter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (arguments.isSpecialOption()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            OptionFormat format = arguments.optionFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            formatter = new OptionOutputFormatter(monitoredVm, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            List<Monitor> logged = monitoredVm.findByPattern(arguments.counterNames());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Collections.sort(logged, arguments.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            List<Monitor> constants = new ArrayList<Monitor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            for (Iterator i = logged.iterator(); i.hasNext(); /* empty */) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                Monitor m = (Monitor)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (!(m.isSupported() || arguments.showUnsupported())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (m.getVariability() == Variability.CONSTANT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    if (arguments.printConstants()) constants.add(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                } else if ((m.getUnits() == Units.STRING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        && !arguments.printStrings()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (!constants.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                logger.printList(constants, arguments.isVerbose(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                 arguments.showUnsupported(), System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (!logged.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (logged.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            formatter = new RawOutputFormatter(logged,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                               arguments.printStrings());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // handle user termination requests by stopping sampling loops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Runtime.getRuntime().addShutdownHook(new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            public void run() {
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
        // handle target termination events for targets other than ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        HostListener terminator = new HostListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            public void vmStatusChanged(VmStatusChangeEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                Integer lvmid = new Integer(vmId.getLocalVmId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (ev.getTerminated().contains(lvmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else if (!ev.getActive().contains(lvmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            public void disconnected(HostEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (monitoredHost == ev.getMonitoredHost()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    logger.stopLogging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (vmId.getLocalVmId() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            monitoredHost.addHostListener(terminator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        logger.logSamples(formatter, arguments.headerRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                          arguments.sampleInterval(), arguments.sampleCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                          System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // detach from host events and from the monitored target jvm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (terminator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            monitoredHost.removeHostListener(terminator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        monitoredHost.detach(monitoredVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}