1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.utils;
2021import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.InputStreamReader;
24import java.io.PrintStream;
25import java.lang.management.ManagementFactory;
26import java.nio.charset.Charset;
27import java.util.Date;
2829/**30 * Helper to run jmap and print the output31 */32publicclassJMap {
33/** Arguments to pass in to command */34publicstaticfinal String ARGS = " -histo ";
35/** This option will print out onlu live objects */36privatestatic String LIVE_HISTO_OPTION = " -histo:live ";
3738/** Do not construct */39protectedJMap() { }
4041/**42 * Get the process ID of the current running process43 *44 * @return Integer process ID45 */46publicstaticint getProcessId() {
47 String processId = ManagementFactory.getRuntimeMXBean().getName();
48if (processId.contains("@")) {
49 processId = processId.substring(0, processId.indexOf("@"));
50 }
51return Integer.parseInt(processId);
52 }
5354/**55 * Run jmap, print numLines of output from it to stderr.56 *57 * @param numLines Number of lines to print58 * @param liveObjectsOnly Should we only print non GC-able objects?59 * @param jmapPath Path to jmap binary60 */61publicstaticvoid heapHistogramDump(int numLines,
62boolean liveObjectsOnly,
63 String jmapPath) {
64 heapHistogramDump(numLines, liveObjectsOnly, System.err, jmapPath);
65 }
6667/**68 * Run jmap, print numLines of output from it to stderr.69 *70 * @param numLines Number of lines to print71 * @param jmapPath Path to jmap binary72 */73publicstaticvoid heapHistogramDump(int numLines, String jmapPath) {
74 heapHistogramDump(numLines, System.err, jmapPath);
75 }
7677/**78 * Run jmap, print numLines of output from it to stream passed in.79 *80 * @param numLines Number of lines to print81 * @param printStream Stream to print to82 * @param jmapPath Path to jmap binary83 */84publicstaticvoid heapHistogramDump(int numLines, PrintStream printStream,
85 String jmapPath) {
86 heapHistogramDump(numLines, false, printStream, jmapPath);
87 }
8889/**90 * Run jmap, print numLines of output from it to stream passed in.91 *92 * @param numLines Number of lines to print93 * @param liveObjectsOnly Should we only print non GC-able objects?94 * @param printStream Stream to print to95 * @param jmapPath Path to jmap binary96 */97privatestaticvoid heapHistogramDump(int numLines,
98boolean liveObjectsOnly,
99 PrintStream printStream,
100 String jmapPath) {
101try {
102 String args = liveObjectsOnly ? LIVE_HISTO_OPTION : ARGS;
103 Process p = Runtime.getRuntime().exec(jmapPath + args + getProcessId());
104 BufferedReader in = new BufferedReader(
105new InputStreamReader(p.getInputStream(), Charset.defaultCharset()));
106 printStream.println("JMap " +
107 (liveObjectsOnly ? "histo:live" : "histo") +
108" dump at " + new Date());
109 String line = in.readLine();
110for (int i = 0; i < numLines && line != null; ++i) {
111 printStream.println("--\t" + line);
112 line = in.readLine();
113 }
114 in.close();
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 }
119 }