Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.ahat; |
| 18 | |
| 19 | import com.android.tools.perflib.heap.HprofParser; |
| 20 | import com.android.tools.perflib.heap.Snapshot; |
| 21 | import com.android.tools.perflib.heap.io.HprofBuffer; |
| 22 | import com.android.tools.perflib.heap.io.MemoryMappedFileBuffer; |
| 23 | import com.sun.net.httpserver.HttpServer; |
| 24 | import java.io.File; |
| 25 | import java.io.IOException; |
| 26 | import java.io.PrintStream; |
| 27 | import java.net.InetAddress; |
| 28 | import java.net.InetSocketAddress; |
| 29 | import java.util.concurrent.Executors; |
| 30 | |
| 31 | public class Main { |
| 32 | |
| 33 | public static void help(PrintStream out) { |
| 34 | out.println("java -jar ahat.jar [-p port] FILE"); |
| 35 | out.println(" Launch an http server for viewing " |
| 36 | + "the given Android heap-dump FILE."); |
| 37 | out.println(""); |
| 38 | out.println("Options:"); |
| 39 | out.println(" -p <port>"); |
| 40 | out.println(" Serve pages on the given port. Defaults to 7100."); |
| 41 | out.println(""); |
| 42 | } |
| 43 | |
| 44 | public static void main(String[] args) throws IOException { |
| 45 | int port = 7100; |
| 46 | for (String arg : args) { |
| 47 | if (arg.equals("--help")) { |
| 48 | help(System.out); |
| 49 | return; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | File hprof = null; |
| 54 | for (int i = 0; i < args.length; i++) { |
| 55 | if ("-p".equals(args[i]) && i + 1 < args.length) { |
| 56 | i++; |
| 57 | port = Integer.parseInt(args[i]); |
| 58 | } else { |
| 59 | if (hprof != null) { |
| 60 | System.err.println("multiple input files."); |
| 61 | help(System.err); |
| 62 | return; |
| 63 | } |
| 64 | hprof = new File(args[i]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (hprof == null) { |
| 69 | System.err.println("no input file."); |
| 70 | help(System.err); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | System.out.println("Reading hprof file..."); |
| 75 | HprofBuffer buffer = new MemoryMappedFileBuffer(hprof); |
| 76 | Snapshot snapshot = (new HprofParser(buffer)).parse(); |
| 77 | |
| 78 | System.out.println("Computing Dominators..."); |
| 79 | snapshot.computeDominators(); |
| 80 | |
| 81 | System.out.println("Processing snapshot for ahat..."); |
| 82 | AhatSnapshot ahat = new AhatSnapshot(snapshot); |
| 83 | |
| 84 | InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 85 | InetSocketAddress addr = new InetSocketAddress(loopback, port); |
| 86 | HttpServer server = HttpServer.create(addr, 0); |
| 87 | server.createContext("/", new OverviewHandler(ahat, hprof)); |
| 88 | server.createContext("/roots", new RootsHandler(ahat)); |
| 89 | server.createContext("/object", new ObjectHandler(ahat)); |
| 90 | server.createContext("/objects", new ObjectsHandler(ahat)); |
| 91 | server.createContext("/site", new SiteHandler(ahat)); |
| 92 | server.createContext("/bitmap", new BitmapHandler(ahat)); |
| 93 | server.createContext("/help", new StaticHandler("help.html", "text/html")); |
| 94 | server.createContext("/style.css", new StaticHandler("style.css", "text/css")); |
| 95 | server.setExecutor(Executors.newFixedThreadPool(1)); |
| 96 | System.out.println("Server started on localhost:" + port); |
| 97 | server.start(); |
| 98 | } |
| 99 | } |
| 100 | |