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