jdk/src/share/classes/sun/jvmstat/monitor/MonitoredVmUtil.java
author herrick
Wed, 09 Feb 2011 09:19:33 -0500
changeset 8197 e45f21c2a40b
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
permissions -rw-r--r--
7016724: Remove sun.jkernel.* classes in JDK 7 Summary: Remove sun.jkernel.* classes in JDK 7 Reviewed-by: ohair, alanb, mchung
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, 2005, 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.jvmstat.monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Utility class proving concenience methods for extracting various
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * information from an MonitoredVm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class MonitoredVmUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * Private constructor - prevent instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private MonitoredVmUtil() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Return the Java Virtual Machine Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @return String - contains the version of the target JVM or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *                  the string "Unknown" if the version cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *                  determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static String vmVersion(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        StringMonitor ver =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
               (StringMonitor)vm.findByName("java.property.java.vm.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return (ver == null) ? "Unknown" : ver.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Return the command line for the target Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @return String - contains the command line of the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *                  application or the the string "Unknown" if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *                  command line cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static String commandLine(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        StringMonitor cmd = (StringMonitor)vm.findByName("sun.rt.javaCommand");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (cmd == null) ? "Unknown" : cmd.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Return the arguments to the main class for the target Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Returns the arguments to the main class. If the arguments can't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * found, the string "Unknown" is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return String - contains the arguments to the main class for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *                  target Java application or the the string "Unknown"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *                  if the command line cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static String mainArgs(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String commandLine = commandLine(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int firstSpace = commandLine.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (firstSpace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return commandLine.substring(firstSpace + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else if (commandLine.compareTo("Unknown") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Return the main class for the target Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns the main class or the name of the jar file if the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * was started with the <em>-jar</em> option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param fullPath include the full path to Jar file, where applicable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return String - contains the main class of the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *                  application or the the string "Unknown" if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *                  command line cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static String mainClass(MonitoredVm vm, boolean fullPath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                         throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String commandLine = commandLine(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String arg0 = commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int firstSpace = commandLine.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (firstSpace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            arg0 = commandLine.substring(0, firstSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!fullPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             * can't use File.separator() here because the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             * for the target jvm may be different than the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
             * for the monitoring jvm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int lastFileSeparator = arg0.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (lastFileSeparator > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                 return arg0.substring(lastFileSeparator + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            lastFileSeparator = arg0.lastIndexOf('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (lastFileSeparator > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                 return arg0.substring(lastFileSeparator + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            int lastPackageSeparator = arg0.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (lastPackageSeparator > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                 return arg0.substring(lastPackageSeparator + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return arg0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Return the JVM arguments for the target Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return String - contains the arguments passed to the JVM for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *                  target Java application or the the string "Unknown"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *                  if the command line cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static String jvmArgs(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        StringMonitor jvmArgs = (StringMonitor)vm.findByName("java.rt.vmArgs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return (jvmArgs == null) ? "Unknown" : jvmArgs.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Return the JVM flags for the target Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return String - contains the flags passed to the JVM for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *                  target Java application or the the string "Unknown"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *                  if the command line cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static String jvmFlags(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        StringMonitor jvmFlags =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
               (StringMonitor)vm.findByName("java.rt.vmFlags");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (jvmFlags == null) ? "Unknown" : jvmFlags.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // Index of the sun.rt.jvmCapabilities counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static int IS_ATTACHABLE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static int IS_KERNEL_VM  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns true if the VM supports attach-on-demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param vm the target MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public static boolean isAttachable(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        StringMonitor jvmCapabilities =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
               (StringMonitor)vm.findByName("sun.rt.jvmCapabilities");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (jvmCapabilities == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
             return jvmCapabilities.stringValue().charAt(IS_ATTACHABLE) == '1';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}