src/jdk.jcmd/share/classes/sun/tools/jstat/Arguments.java
author vlivanov
Fri, 26 May 2017 18:39:27 +0300
changeset 48557 2e867226b914
parent 47216 71c04702a3d5
child 48543 7067fe4e054e
permissions -rw-r--r--
8174962: Better interface invocations Reviewed-by: jrose, coleenp, ahgross, acorn, iignatyev
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28099
44dfaff3cc59 6364329: jstat displays "invalid argument count" with usage
egahlin
parents: 25859
diff changeset
     2
 * Copyright (c) 2004, 2014, 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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.jvmstat.monitor.Monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.jvmstat.monitor.VmIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Class for processing command line arguments and providing method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * level access to arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class Arguments {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final boolean debug = Boolean.getBoolean("jstat.debug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final boolean showUnsupported =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            Boolean.getBoolean("jstat.showUnsupported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final String JVMSTAT_USERDIR = ".jvmstat";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final String OPTIONS_FILENAME = "jstat_options";
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
    50
    private static final String UNSUPPORTED_OPTIONS_FILENAME = "jstat_unsupported_options";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String ALL_NAMES = "\\w*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Comparator<Monitor> comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int headerRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean help;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean constants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean constantsOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean strings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean snap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private String specialOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private String names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private OptionFormat optionFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int count = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int interval = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String vmIdString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private VmIdentifier vmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static void printUsage(PrintStream ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ps.println("Usage: jstat -help|-options");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ps.println("       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ps.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ps.println("Definitions:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        ps.println("  <option>      An option reported by the -options option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ps.println("  <vmid>        Virtual Machine Identifier. A vmid takes the following form:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ps.println("                     <lvmid>[@<hostname>[:<port>]]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ps.println("                Where <lvmid> is the local vm identifier for the target");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ps.println("                Java virtual machine, typically a process id; <hostname> is");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ps.println("                the name of the host running the target Java virtual machine;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ps.println("                and <port> is the port number for the rmiregistry on the");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ps.println("                target host. See the jvmstat documentation for a more complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ps.println("                description of the Virtual Machine Identifier.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ps.println("  <lines>       Number of samples between header lines.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        ps.println("  <interval>    Sampling interval. The following forms are allowed:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ps.println("                    <n>[\"ms\"|\"s\"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        ps.println("                Where <n> is an integer and the suffix specifies the units as ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ps.println("                milliseconds(\"ms\") or seconds(\"s\"). The default units are \"ms\".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ps.println("  <count>       Number of samples to take before terminating.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ps.println("  -J<flag>      Pass <flag> directly to the runtime system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // undocumented options:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        //   -list [<vmid>]  - list counter names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        //   -snap <vmid>    - snapshot counter values as name=value pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        //   -name <pattern> - output counters matching given pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        //   -a              - sort in ascending order (default)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        //   -d              - sort in descending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        //   -v              - verbose output  (-snap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        //   -constants      - output constants with -name output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        //   -strings        - output strings with -name output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static int toMillis(String s) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        String[] unitStrings = { "ms", "s" }; // ordered from most specific to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                              // least specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String unitString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String valueString = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int i = 0; i < unitStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int index = s.indexOf(unitStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                unitString = s.substring(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                valueString = s.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int value = Integer.parseInt(valueString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (unitString == null || unitString.compareTo("ms") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if (unitString.compareTo("s") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return value * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        "Unknow time unit: " + unitString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    "Could not convert interval: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public Arguments(String[] args) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int argc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
28099
44dfaff3cc59 6364329: jstat displays "invalid argument count" with usage
egahlin
parents: 25859
diff changeset
   144
        if (args.length == 0) {
44dfaff3cc59 6364329: jstat displays "invalid argument count" with usage
egahlin
parents: 25859
diff changeset
   145
            help = true;
44dfaff3cc59 6364329: jstat displays "invalid argument count" with usage
egahlin
parents: 25859
diff changeset
   146
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ((args[0].compareTo("-?") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                || (args[0].compareTo("-help") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            help = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else if (args[0].compareTo("-options") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            options = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else if (args[0].compareTo("-list") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            list = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (args.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
              throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // list can take one arg - a vmid - fall through for arg processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            argc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        for ( ; (argc < args.length) && (args[argc].startsWith("-")); argc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            String arg = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (arg.compareTo("-a") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                comparator = new AscendingMonitorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else if (arg.compareTo("-d") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                comparator =  new DescendingMonitorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } else if (arg.compareTo("-t") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                timestamp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else if (arg.compareTo("-v") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } else if ((arg.compareTo("-constants") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                       || (arg.compareTo("-c") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                constants = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else if ((arg.compareTo("-strings") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                       || (arg.compareTo("-s") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                strings = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else if (arg.startsWith("-h")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                if (arg.compareTo("-h") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    value = arg.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    argc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    if (argc >= args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                "-h requires an integer argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    value = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    headerRate = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    headerRate = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (headerRate < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            "illegal -h argument: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } else if (arg.startsWith("-name")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (arg.startsWith("-name=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    names = arg.substring(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    argc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if (argc >= args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                "option argument expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    names = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                 * there are scenarios here: special jstat_options file option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                 * or the rare case of a negative lvmid. The negative lvmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                 * can occur in some operating environments (such as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                 * 95/98/ME), so we provide for this case here by checking if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                 * the argument has any numerical characters. This assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                 * there are no special jstat_options that contain numerical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                 * characters in their name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                // extract the lvmid part of possible lvmid@host.domain:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                String lvmidStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                int at_index = args[argc].indexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (at_index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    lvmidStr = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    lvmidStr = args[argc].substring(0, at_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                // try to parse the lvmid part as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    int vmid = Integer.parseInt(lvmidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    // it parsed, assume a negative lvmid and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    // it didn't parse. check for the -snap or jstat_options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    // file options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    if ((argc == 0) && (args[argc].compareTo("-snap") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        snap = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    } else if (argc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        specialOption = args[argc].substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                "illegal argument: " + args[argc]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // prevent 'jstat <pid>' from being accepted as a valid argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (!(specialOption != null || list || snap || names != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new IllegalArgumentException("-<option> required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        switch (args.length - argc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (snap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                count = Integer.parseInt(args[args.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new IllegalArgumentException("illegal count value: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                                   + args[args.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            interval = toMillis(args[args.length-2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            vmIdString = args[args.length-3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (snap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            interval = toMillis(args[args.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            vmIdString = args[args.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            vmIdString = args[args.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (!list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // set count and interval to their default values if not set above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (count == -1 && interval == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // default is for a single sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            interval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // validate arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (comparator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            comparator = new AscendingMonitorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // allow ',' characters to separate names, convert to '|' chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        names = (names == null) ? ALL_NAMES : names.replace(',', '|');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // verify that the given pattern parses without errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            Pattern pattern = Pattern.compile(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } catch (PatternSyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new IllegalArgumentException("Bad name pattern: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                               + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // verify that the special option is valid and get it's formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (specialOption != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            OptionFinder finder = new OptionFinder(optionsSources());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            optionFormat = finder.getOptionFormat(specialOption, timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (optionFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                throw new IllegalArgumentException("Unknown option: -"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                                   + specialOption);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // verify that the vm identifier is valied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            vmId = new VmIdentifier(vmIdString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            IllegalArgumentException iae = new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    "Malformed VM Identifier: " + vmIdString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            iae.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public Comparator<Monitor> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public boolean isHelp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return help;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public boolean isList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public boolean isSnap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return snap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean isOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public boolean isVerbose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public boolean printConstants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return constants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public boolean isConstantsOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return constantsOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public boolean printStrings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return strings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public boolean showUnsupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return showUnsupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public int headerRate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return headerRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public String counterNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public VmIdentifier vmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return vmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public String vmIdString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return vmIdString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public int sampleInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public int sampleCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public boolean isTimestamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public boolean isSpecialOption() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return specialOption != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public String specialOption() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return specialOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public OptionFormat optionFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return optionFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   416
    public List<URL> optionsSources() {
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   417
        List<URL> sources = new ArrayList<URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        String filename = OPTIONS_FILENAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            String userHome = System.getProperty("user.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            String userDir = userHome + "/" + JVMSTAT_USERDIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            File home = new File(userDir + "/" + filename);
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   426
            sources.add(home.toURI().toURL());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new IllegalArgumentException("Internal Error: Bad URL: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                               + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   435
        URL u = this.getClass().getResource("resources/" + filename);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   436
        assert u != null;
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   437
        sources.add(u);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   438
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   439
        if (showUnsupported) {
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   440
            u = this.getClass().getResource("resources/" +  UNSUPPORTED_OPTIONS_FILENAME);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   441
            assert u != null;
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   442
            sources.add(u);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   443
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return sources;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}