hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java
author ehelin
Wed, 20 Feb 2013 16:41:23 +0100
changeset 15809 e2516b8c8f35
parent 5547 f4b087cbb361
child 17118 cea2adff853d
permissions -rw-r--r--
8008536: Add HotSpot support for printing class loader statistics for JMap Reviewed-by: sla, brutisso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
     2
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.tools;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
public class JMap extends Tool {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
    public JMap(int m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
        mode = m;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    public JMap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
        this(MODE_PMAP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    protected boolean needsJavaPrefix() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    public String getName() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
        return "jmap";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    protected String getCommandFlags() {
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    48
        return "-heap|-heap:format=b|-histo|-clstats|-finalizerinfo";
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    protected void printFlagsUsage() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
        System.out.println("    <no option>\tto print same info as Solaris pmap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
        System.out.println("    -heap\tto print java heap summary");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
        System.out.println("    -heap:format=b\tto dump java heap in hprof binary format");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
        System.out.println("    -histo\tto print histogram of java object heap");
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    56
        System.out.println("    -clstats\tto print class loader statistics");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        System.out.println("    -finalizerinfo\tto print information on objects awaiting finalization");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
        super.printFlagsUsage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    public static final int MODE_HEAP_SUMMARY = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    public static final int MODE_HISTOGRAM = 1;
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    63
    public static final int MODE_CLSTATS = 2;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    public static final int MODE_PMAP = 3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    public static final int MODE_HEAP_GRAPH_GXL = 5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    public static final int MODE_FINALIZERINFO = 6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
        Tool tool = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
        switch (mode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
        case MODE_HEAP_SUMMARY:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
            tool = new HeapSummary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
        case MODE_HISTOGRAM:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
            tool = new ObjectHistogram();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    81
        case MODE_CLSTATS:
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    82
            tool = new ClassLoaderStats();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        case MODE_PMAP:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
            tool = new PMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
        case MODE_HEAP_GRAPH_HPROF_BIN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
            writeHeapHprofBin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
        case MODE_HEAP_GRAPH_GXL:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
            writeHeapGXL();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        case MODE_FINALIZERINFO:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
            tool = new FinalizerInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
            usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
       tool.setAgent(getAgent());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
       tool.setDebugeeType(getDebugeeType());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
       tool.run();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    public static void main(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        int mode = MODE_PMAP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
        if (args.length > 1 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
            String modeFlag = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
            boolean copyArgs = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
            if (modeFlag.equals("-heap")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
                mode = MODE_HEAP_SUMMARY;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
            } else if (modeFlag.equals("-histo")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                mode = MODE_HISTOGRAM;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
            } else if (modeFlag.equals("-permstat")) {
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
   121
                mode = MODE_CLSTATS;
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
   122
            } else if (modeFlag.equals("-clstats")) {
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
   123
                mode = MODE_CLSTATS;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
            } else if (modeFlag.equals("-finalizerinfo")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
                mode = MODE_FINALIZERINFO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                int index = modeFlag.indexOf("-heap:format=");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                if (index != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
                    String format = modeFlag.substring(1 + modeFlag.indexOf('='));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                    if (format.equals("b")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                        mode = MODE_HEAP_GRAPH_HPROF_BIN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                    } else if (format.equals("x")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                        mode = MODE_HEAP_GRAPH_GXL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                        System.err.println("unknown heap format:" + format);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                    copyArgs = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
            if (copyArgs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
                String[] newArgs = new String[args.length - 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
                for (int i = 0; i < newArgs.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
                    newArgs[i] = args[i + 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
                args = newArgs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        JMap jmap = new JMap(mode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        jmap.start(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        jmap.stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    public boolean writeHeapHprofBin(String fileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
            HeapGraphWriter hgw = new HeapHprofBinWriter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
            hgw.write(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
            System.out.println("heap written to " + fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
            System.err.println(exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    public boolean writeHeapHprofBin() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        return writeHeapHprofBin("heap.bin");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    private boolean writeHeapGXL(String fileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
            HeapGraphWriter hgw = new HeapGXLWriter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
            hgw.write(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
            System.out.println("heap written to " + fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
            System.err.println(exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    public boolean writeHeapGXL() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        return writeHeapGXL("heap.xml");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    private int mode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
}