View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.utils;
20  
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  import java.io.PrintStream;
25  import java.lang.management.ManagementFactory;
26  import java.nio.charset.Charset;
27  import java.util.Date;
28  
29  /**
30   * Helper to run jmap and print the output
31   */
32  public class JMap {
33    /** The command to run */
34    public static final String CMD = "jmap ";
35    /** Arguments to pass in to command */
36    public static final String ARGS = " -histo ";
37    /** This option will print out onlu live objects */
38    private static String LIVE_HISTO_OPTION = " -histo:live ";
39  
40    /** Do not construct */
41    protected JMap() { }
42  
43    /**
44     * Get the process ID of the current running process
45     *
46     * @return Integer process ID
47     */
48    public static int getProcessId() {
49      String processId = ManagementFactory.getRuntimeMXBean().getName();
50      if (processId.contains("@")) {
51        processId = processId.substring(0, processId.indexOf("@"));
52      }
53      return Integer.parseInt(processId);
54    }
55  
56    /**
57     * Run jmap, print numLines of output from it to stderr.
58     *
59     * @param numLines Number of lines to print
60     * @param liveObjectsOnly Should we only print non GC-able objects?
61     */
62    public static void heapHistogramDump(int numLines,
63                                         boolean liveObjectsOnly) {
64      heapHistogramDump(numLines, liveObjectsOnly, System.err);
65    }
66  
67    /**
68     * Run jmap, print numLines of output from it to stderr.
69     *
70     * @param numLines Number of lines to print
71     */
72    public static void heapHistogramDump(int numLines) {
73      heapHistogramDump(numLines, System.err);
74    }
75  
76    /**
77     * Run jmap, print numLines of output from it to stream passed in.
78     *
79     * @param numLines Number of lines to print
80     * @param printStream Stream to print to
81     */
82    public static void heapHistogramDump(int numLines, PrintStream printStream) {
83      heapHistogramDump(numLines, false, printStream);
84    }
85  
86    /**
87     * Run jmap, print numLines of output from it to stream passed in.
88     *
89     * @param numLines Number of lines to print
90     * @param liveObjectsOnly Should we only print non GC-able objects?
91     * @param printStream Stream to print to
92     */
93    private static void heapHistogramDump(int numLines,
94                                          boolean liveObjectsOnly,
95                                          PrintStream printStream) {
96      try {
97        String args = liveObjectsOnly ? LIVE_HISTO_OPTION : ARGS;
98        Process p = Runtime.getRuntime().exec(CMD + args + getProcessId());
99        BufferedReader in = new BufferedReader(
100           new InputStreamReader(p.getInputStream(), Charset.defaultCharset()));
101       printStream.println("JMap " +
102           (liveObjectsOnly ? "histo:live" : "histo") +
103           " dump at " + new Date());
104       String line = in.readLine();
105       for (int i = 0; i < numLines && line != null; ++i) {
106         printStream.println("--\t" + line);
107         line = in.readLine();
108       }
109       in.close();
110     } catch (IOException e) {
111       e.printStackTrace();
112     }
113   }
114 }