jdk/src/jdk.jcmd/share/classes/sun/tools/jstat/Arguments.java
author vinnie
Mon, 06 Oct 2014 16:44:57 +0100
changeset 26958 cb5cbde6e7a4
parent 25859 3317bb8137f4
child 28099 44dfaff3cc59
permissions -rw-r--r--
8059627: Enable PKCS11 tests on Mac Reviewed-by: mullan
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: 5778
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.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (args.length < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if ((args[0].compareTo("-?") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                || (args[0].compareTo("-help") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            help = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else if (args[0].compareTo("-options") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            options = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } else if (args[0].compareTo("-list") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            list = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (args.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
              throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // list can take one arg - a vmid - fall through for arg processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            argc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        for ( ; (argc < args.length) && (args[argc].startsWith("-")); argc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            String arg = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (arg.compareTo("-a") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                comparator = new AscendingMonitorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else if (arg.compareTo("-d") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                comparator =  new DescendingMonitorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else if (arg.compareTo("-t") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                timestamp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else if (arg.compareTo("-v") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if ((arg.compareTo("-constants") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                       || (arg.compareTo("-c") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                constants = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else if ((arg.compareTo("-strings") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                       || (arg.compareTo("-s") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                strings = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else if (arg.startsWith("-h")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (arg.compareTo("-h") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    value = arg.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    argc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    if (argc >= args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                "-h requires an integer argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    value = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    headerRate = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    headerRate = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (headerRate < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            "illegal -h argument: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else if (arg.startsWith("-name")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (arg.startsWith("-name=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    names = arg.substring(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    argc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    if (argc >= args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                "option argument expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    names = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                 * there are scenarios here: special jstat_options file option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                 * or the rare case of a negative lvmid. The negative lvmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                 * can occur in some operating environments (such as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                 * 95/98/ME), so we provide for this case here by checking if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                 * the argument has any numerical characters. This assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                 * there are no special jstat_options that contain numerical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                 * characters in their name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                // extract the lvmid part of possible lvmid@host.domain:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                String lvmidStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                int at_index = args[argc].indexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (at_index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    lvmidStr = args[argc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    lvmidStr = args[argc].substring(0, at_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                // try to parse the lvmid part as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    int vmid = Integer.parseInt(lvmidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    // it parsed, assume a negative lvmid and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    // it didn't parse. check for the -snap or jstat_options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    // file options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if ((argc == 0) && (args[argc].compareTo("-snap") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        snap = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    } else if (argc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        specialOption = args[argc].substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                "illegal argument: " + args[argc]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    }
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
        // prevent 'jstat <pid>' from being accepted as a valid argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (!(specialOption != null || list || snap || names != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new IllegalArgumentException("-<option> required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        switch (args.length - argc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (snap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                count = Integer.parseInt(args[args.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                throw new IllegalArgumentException("illegal count value: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                                   + args[args.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            interval = toMillis(args[args.length-2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            vmIdString = args[args.length-3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (snap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            interval = toMillis(args[args.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            vmIdString = args[args.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            vmIdString = args[args.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (!list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw new IllegalArgumentException("invalid argument count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // set count and interval to their default values if not set above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (count == -1 && interval == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // default is for a single sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            interval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // validate arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (comparator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            comparator = new AscendingMonitorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // allow ',' characters to separate names, convert to '|' chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        names = (names == null) ? ALL_NAMES : names.replace(',', '|');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // verify that the given pattern parses without errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            Pattern pattern = Pattern.compile(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } catch (PatternSyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new IllegalArgumentException("Bad name pattern: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                               + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // verify that the special option is valid and get it's formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (specialOption != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            OptionFinder finder = new OptionFinder(optionsSources());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            optionFormat = finder.getOptionFormat(specialOption, timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (optionFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                throw new IllegalArgumentException("Unknown option: -"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                   + specialOption);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // verify that the vm identifier is valied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            vmId = new VmIdentifier(vmIdString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            IllegalArgumentException iae = new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    "Malformed VM Identifier: " + vmIdString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            iae.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public Comparator<Monitor> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public boolean isHelp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return help;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public boolean isList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public boolean isSnap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return snap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public boolean isOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public boolean isVerbose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public boolean printConstants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return constants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public boolean isConstantsOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return constantsOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public boolean printStrings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return strings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public boolean showUnsupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return showUnsupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public int headerRate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return headerRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public String counterNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public VmIdentifier vmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return vmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public String vmIdString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return vmIdString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public int sampleInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public int sampleCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public boolean isTimestamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public boolean isSpecialOption() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return specialOption != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public String specialOption() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return specialOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public OptionFormat optionFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return optionFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   415
    public List<URL> optionsSources() {
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   416
        List<URL> sources = new ArrayList<URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        String filename = OPTIONS_FILENAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            String userHome = System.getProperty("user.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            String userDir = userHome + "/" + JVMSTAT_USERDIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            File home = new File(userDir + "/" + filename);
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   425
            sources.add(home.toURI().toURL());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new IllegalArgumentException("Internal Error: Bad URL: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                               + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
5778
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   434
        URL u = this.getClass().getResource("resources/" + filename);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   435
        assert u != null;
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   436
        sources.add(u);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   437
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   438
        if (showUnsupported) {
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   439
            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
   440
            assert u != null;
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   441
            sources.add(u);
ca3811dc046d 6959965: jstat: Add new -classload option to print class loading statistics
mchung
parents: 5506
diff changeset
   442
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return sources;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}