Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/tools/hat/Main.java
38920 views
/*1* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* The Original Code is HAT. The Initial Developer of the27* Original Code is Bill Foote, with contributions from others28* at JavaSoft/Sun.29*/3031package com.sun.tools.hat;3233import java.io.IOException;34import java.io.File;3536import com.sun.tools.hat.internal.model.Snapshot;37import com.sun.tools.hat.internal.model.ReachableExcludesImpl;38import com.sun.tools.hat.internal.server.QueryListener;3940/**41*42* @author Bill Foote43*/444546public class Main {4748private static String VERSION_STRING = "jhat version 2.0";4950private static void usage(String message) {51if ( message != null ) {52System.err.println("ERROR: " + message);53}54System.err.println("Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>");55System.err.println();56System.err.println("\t-J<flag> Pass <flag> directly to the runtime system. For");57System.err.println("\t\t\t example, -J-mx512m to use a maximum heap size of 512MB");58System.err.println("\t-stack false: Turn off tracking object allocation call stack.");59System.err.println("\t-refs false: Turn off tracking of references to objects");60System.err.println("\t-port <port>: Set the port for the HTTP server. Defaults to 7000");61System.err.println("\t-exclude <file>: Specify a file that lists data members that should");62System.err.println("\t\t\t be excluded from the reachableFrom query.");63System.err.println("\t-baseline <file>: Specify a baseline object dump. Objects in");64System.err.println("\t\t\t both heap dumps with the same ID and same class will");65System.err.println("\t\t\t be marked as not being \"new\".");66System.err.println("\t-debug <int>: Set debug level.");67System.err.println("\t\t\t 0: No debug output");68System.err.println("\t\t\t 1: Debug hprof file parsing");69System.err.println("\t\t\t 2: Debug hprof file parsing, no server");70System.err.println("\t-version Report version number");71System.err.println("\t-h|-help Print this help and exit");72System.err.println("\t<file> The file to read");73System.err.println();74System.err.println("For a dump file that contains multiple heap dumps,");75System.err.println("you may specify which dump in the file");76System.err.println("by appending \"#<number>\" to the file name, i.e. \"foo.hprof#3\".");77System.err.println();78System.err.println("All boolean options default to \"true\"");79System.exit(1);80}8182//83// Convert s to a boolean. If it's invalid, abort the program.84//85private static boolean booleanValue(String s) {86if ("true".equalsIgnoreCase(s)) {87return true;88} else if ("false".equalsIgnoreCase(s)) {89return false;90} else {91usage("Boolean value must be true or false");92return false; // Never happens93}94}9596public static void main(String[] args) {97if (args.length < 1) {98usage("No arguments supplied");99}100101boolean parseonly = false;102int portNumber = 7000;103boolean callStack = true;104boolean calculateRefs = true;105String baselineDump = null;106String excludeFileName = null;107int debugLevel = 0;108for (int i = 0; ; i += 2) {109if (i > (args.length - 1)) {110usage("Option parsing error");111}112if ("-version".equals(args[i])) {113System.out.print(VERSION_STRING);114System.out.println(" (java version " + System.getProperty("java.version") + ")");115System.exit(0);116}117118if ("-h".equals(args[i]) || "-help".equals(args[i])) {119usage(null);120}121122if (i == (args.length - 1)) {123break;124}125String key = args[i];126String value = args[i+1];127if ("-stack".equals(key)) {128callStack = booleanValue(value);129} else if ("-refs".equals(key)) {130calculateRefs = booleanValue(value);131} else if ("-port".equals(key)) {132portNumber = Integer.parseInt(value, 10);133} else if ("-exclude".equals(key)) {134excludeFileName = value;135} else if ("-baseline".equals(key)) {136baselineDump = value;137} else if ("-debug".equals(key)) {138debugLevel = Integer.parseInt(value, 10);139} else if ("-parseonly".equals(key)) {140// Undocumented option. To be used for testing purpose only141parseonly = booleanValue(value);142}143}144String fileName = args[args.length - 1];145Snapshot model = null;146File excludeFile = null;147if (excludeFileName != null) {148excludeFile = new File(excludeFileName);149if (!excludeFile.exists()) {150System.out.println("Exclude file " + excludeFile151+ " does not exist. Aborting.");152System.exit(1);153}154}155156System.out.println("Reading from " + fileName + "...");157try {158model = com.sun.tools.hat.internal.parser.Reader.readFile(fileName, callStack, debugLevel);159} catch (IOException ex) {160ex.printStackTrace();161System.exit(1);162} catch (RuntimeException ex) {163ex.printStackTrace();164System.exit(1);165}166System.out.println("Snapshot read, resolving...");167model.resolve(calculateRefs);168System.out.println("Snapshot resolved.");169170if (excludeFile != null) {171model.setReachableExcludes(new ReachableExcludesImpl(excludeFile));172}173174if (baselineDump != null) {175System.out.println("Reading baseline snapshot...");176Snapshot baseline = null;177try {178baseline = com.sun.tools.hat.internal.parser.Reader.readFile(baselineDump, false,179debugLevel);180} catch (IOException ex) {181ex.printStackTrace();182System.exit(1);183} catch (RuntimeException ex) {184ex.printStackTrace();185System.exit(1);186}187baseline.resolve(false);188System.out.println("Discovering new objects...");189model.markNewRelativeTo(baseline);190baseline = null; // Guard against conservative GC191}192if ( debugLevel == 2 ) {193System.out.println("No server, -debug 2 was used.");194System.exit(0);195}196197if (parseonly) {198// do not start web server.199System.out.println("-parseonly is true, exiting..");200System.exit(0);201}202203QueryListener listener = new QueryListener(portNumber);204listener.setModel(model);205Thread t = new Thread(listener, "Query Listener");206t.setPriority(Thread.NORM_PRIORITY+1);207t.start();208System.out.println("Started HTTP server on port " + portNumber);209System.out.println("Server is ready.");210}211}212213214