hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 1 489c9b5090e2
child 15809 e2516b8c8f35
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 2004, 2007, 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() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
        return "-heap|-heap:format=b|-histo|-permstat|-finalizerinfo";
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");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
        System.out.println("    -permstat\tto print permanent generation statistics");
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;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    public static final int MODE_PERMSTAT = 2;
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        case MODE_PERMSTAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
            tool = new PermStat();
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")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
                mode = MODE_PERMSTAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
            } else if (modeFlag.equals("-finalizerinfo")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
                mode = MODE_FINALIZERINFO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
                int index = modeFlag.indexOf("-heap:format=");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
                if (index != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                    String format = modeFlag.substring(1 + modeFlag.indexOf('='));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                    if (format.equals("b")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
                        mode = MODE_HEAP_GRAPH_HPROF_BIN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                    } else if (format.equals("x")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                        mode = MODE_HEAP_GRAPH_GXL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                        System.err.println("unknown heap format:" + format);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
                        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
                    copyArgs = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
            if (copyArgs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                String[] newArgs = new String[args.length - 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
                for (int i = 0; i < newArgs.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
                    newArgs[i] = args[i + 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
                args = newArgs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
        JMap jmap = new JMap(mode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
        jmap.start(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        jmap.stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    public boolean writeHeapHprofBin(String fileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
            HeapGraphWriter hgw = new HeapHprofBinWriter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
            hgw.write(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
            System.out.println("heap written to " + fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            System.err.println(exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    public boolean writeHeapHprofBin() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
        return writeHeapHprofBin("heap.bin");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    private boolean writeHeapGXL(String fileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
            HeapGraphWriter hgw = new HeapGXLWriter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
            hgw.write(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
            System.out.println("heap written to " + fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
            System.err.println(exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    public boolean writeHeapGXL() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
        return writeHeapGXL("heap.xml");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    private int mode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
}