jdk/src/share/classes/sun/management/HotSpotDiagnostic.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 11365 05d995976571
child 11849 b6c37f4753fd
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2011, 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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1226
diff changeset
    29
import java.util.ArrayList;
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
    30
import java.util.Arrays;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
    31
import java.util.Collections;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1226
diff changeset
    32
import java.util.List;
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    33
import javax.management.ObjectName;
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    34
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
    35
import com.sun.management.DiagnosticCommandInfo;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
    36
import com.sun.management.DiagnosticCommandArgumentInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.management.HotSpotDiagnosticMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.management.VMOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Implementation of the diagnostic MBean for Hotspot VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public HotSpotDiagnostic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public native void dumpHeap(String outputFile, boolean live) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public List<VMOption> getDiagnosticOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        List<Flag> allFlags = Flag.getAllFlags();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11365
diff changeset
    51
        List<VMOption> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        for (Flag flag : allFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            if (flag.isWriteable() && flag.isExternal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                result.add(flag.getVMOption());
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 result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public VMOption getVMOption(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new NullPointerException("name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Flag f = Flag.getFlag(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new IllegalArgumentException("VM option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                name + "\" does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return f.getVMOption();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void setVMOption(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new NullPointerException("name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new NullPointerException("value cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    81
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Flag flag = Flag.getFlag(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (flag == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new IllegalArgumentException("VM option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                name + "\" does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (!flag.isWriteable()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new IllegalArgumentException("VM Option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                name + "\" is not writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // Check the type of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Object v = flag.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (v instanceof Long) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                long l = Long.parseLong(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                Flag.setLongValue(name, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                IllegalArgumentException iae =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    new IllegalArgumentException("Invalid value:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        " VM Option \"" + name + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        " expects numeric value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                iae.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                throw iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else if (v instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (!value.equalsIgnoreCase("true") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                !value.equalsIgnoreCase("false")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                throw new IllegalArgumentException("Invalid value:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    " VM Option \"" + name + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    " expects \"true\" or \"false\".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            Flag.setBooleanValue(name, Boolean.parseBoolean(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else if (v instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            Flag.setStringValue(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new IllegalArgumentException("VM Option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                name + "\" is of an unsupported type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                v.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   122
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   123
    public List<String> getDiagnosticCommands() {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   124
        String[] commands = getDiagnosticCommands0();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   125
        return commands == null ? Collections.<String>emptyList() :
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   126
            Arrays.asList(commands);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   127
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   128
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   129
    public DiagnosticCommandInfo getDiagnosticCommandInfo(String command) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   130
        String[] array = new String[] { command };
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   131
        return getDiagnosticCommandInfo0(array)[0];
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   132
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   133
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   134
    public List<DiagnosticCommandInfo> getDiagnosticCommandInfo() {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   135
        String[] commands = getDiagnosticCommands0();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   136
        return Arrays.asList(getDiagnosticCommandInfo0(commands));
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   137
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   138
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   139
    public List<DiagnosticCommandInfo> getDiagnosticCommandInfo(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   140
        List<String> commands) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   141
        return Arrays.asList(getDiagnosticCommandInfo0(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   142
            commands.toArray(new String[commands.size()])));
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   143
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   144
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   145
    public String execute(String command) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   146
        Util.checkControlAccess();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   147
        return executeDiagnosticCommand0(command);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   148
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   149
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   150
    public String execute(String cmd, String... arguments) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   151
        if(cmd == null) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   152
            throw new NullPointerException("Missing command name");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   153
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   154
        StringBuilder sb = new StringBuilder();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   155
        sb.append(cmd);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   156
        sb.append(" ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   157
        for(String arg : arguments) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   158
            sb.append(arg);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   159
            sb.append(" ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   160
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   161
        return execute(sb.toString());
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   162
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   163
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   164
    public ObjectName getObjectName() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1226
diff changeset
   165
        return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   166
    }
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   167
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   168
    private native String[] getDiagnosticCommands0();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   169
    private native DiagnosticCommandInfo[] getDiagnosticCommandInfo0(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   170
        String[] commands) throws IllegalArgumentException;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   171
    private native String executeDiagnosticCommand0(String command)
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents: 5506
diff changeset
   172
        throws IllegalArgumentException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}