jdk/src/share/classes/com/sun/tools/hat/Main.java
author ohair
Wed, 30 Apr 2008 17:34:41 -0700
changeset 468 642c8c0be52e
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6695553: Cleanup GPLv2+SPL legal notices in hat sources Summary: Just correcting the legal notices on the HAT sources. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    29
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package com.sun.tools.hat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.hat.internal.model.Snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.hat.internal.model.ReachableExcludesImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.hat.internal.server.QueryListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
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 class Main {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static String VERSION_STRING = "jhat version 2.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void usage(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if ( message != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.err.println("ERROR: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.err.println("Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.err.println("\t-J<flag>          Pass <flag> directly to the runtime system. For");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.err.println("\t\t\t  example, -J-mx512m to use a maximum heap size of 512MB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.err.println("\t-stack false:     Turn off tracking object allocation call stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        System.err.println("\t-refs false:      Turn off tracking of references to objects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.err.println("\t-port <port>:     Set the port for the HTTP server.  Defaults to 7000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        System.err.println("\t-exclude <file>:  Specify a file that lists data members that should");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.err.println("\t\t\t  be excluded from the reachableFrom query.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.err.println("\t-baseline <file>: Specify a baseline object dump.  Objects in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.err.println("\t\t\t  both heap dumps with the same ID and same class will");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.err.println("\t\t\t  be marked as not being \"new\".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.err.println("\t-debug <int>:     Set debug level.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.err.println("\t\t\t    0:  No debug output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.err.println("\t\t\t    1:  Debug hprof file parsing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.err.println("\t\t\t    2:  Debug hprof file parsing, no server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.err.println("\t-version          Report version number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.err.println("\t-h|-help          Print this help and exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.err.println("\t<file>            The file to read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.err.println("For a dump file that contains multiple heap dumps,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.err.println("you may specify which dump in the file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.err.println("by appending \"#<number>\" to the file name, i.e. \"foo.hprof#3\".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.err.println("All boolean options default to \"true\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Convert s to a boolean.  If it's invalid, abort the program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static boolean booleanValue(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if ("true".equalsIgnoreCase(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else if ("false".equalsIgnoreCase(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            usage("Boolean value must be true or false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return false;       // Never happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (args.length < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            usage("No arguments supplied");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        boolean parseonly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int portNumber = 7000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        boolean callStack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        boolean calculateRefs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String baselineDump = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        String excludeFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int debugLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 0; ; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (i > (args.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                usage("Option parsing error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if ("-version".equals(args[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.out.print(VERSION_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                System.out.println(" (java version " + System.getProperty("java.version") + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if ("-h".equals(args[i]) || "-help".equals(args[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                usage(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (i == (args.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            String key = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String value = args[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if ("-stack".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                callStack = booleanValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else if ("-refs".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                calculateRefs = booleanValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else if ("-port".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                portNumber = Integer.parseInt(value, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } else if ("-exclude".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                excludeFileName = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } else if ("-baseline".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                baselineDump = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else if ("-debug".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                debugLevel = Integer.parseInt(value, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } else if ("-parseonly".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // Undocumented option. To be used for testing purpose only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                parseonly = booleanValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String fileName = args[args.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Snapshot model = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        File excludeFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (excludeFileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            excludeFile = new File(excludeFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (!excludeFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                System.out.println("Exclude file " + excludeFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                    + " does not exist.  Aborting.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.out.println("Reading from " + fileName + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            model = com.sun.tools.hat.internal.parser.Reader.readFile(fileName, callStack, debugLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } catch (RuntimeException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println("Snapshot read, resolving...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        model.resolve(calculateRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        System.out.println("Snapshot resolved.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (excludeFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            model.setReachableExcludes(new ReachableExcludesImpl(excludeFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (baselineDump != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.out.println("Reading baseline snapshot...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            Snapshot baseline = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                baseline = com.sun.tools.hat.internal.parser.Reader.readFile(baselineDump, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                                      debugLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } catch (RuntimeException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            baseline.resolve(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            System.out.println("Discovering new objects...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            model.markNewRelativeTo(baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            baseline = null;    // Guard against conservative GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if ( debugLevel == 2 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            System.out.println("No server, -debug 2 was used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (parseonly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // do not start web server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            System.out.println("-parseonly is true, exiting..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        QueryListener listener = new QueryListener(portNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        listener.setModel(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Thread t = new Thread(listener, "Query Listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        t.setPriority(Thread.NORM_PRIORITY+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        System.out.println("Started HTTP server on port " + portNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        System.out.println("Server is ready.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}