Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JMap |
|
| 2.0;2 |
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 | /** Arguments to pass in to command */ | |
34 | public static final String ARGS = " -histo "; | |
35 | /** This option will print out onlu live objects */ | |
36 | 0 | private static String LIVE_HISTO_OPTION = " -histo:live "; |
37 | ||
38 | /** Do not construct */ | |
39 | 0 | protected JMap() { } |
40 | ||
41 | /** | |
42 | * Get the process ID of the current running process | |
43 | * | |
44 | * @return Integer process ID | |
45 | */ | |
46 | public static int getProcessId() { | |
47 | 0 | String processId = ManagementFactory.getRuntimeMXBean().getName(); |
48 | 0 | if (processId.contains("@")) { |
49 | 0 | processId = processId.substring(0, processId.indexOf("@")); |
50 | } | |
51 | 0 | return Integer.parseInt(processId); |
52 | } | |
53 | ||
54 | /** | |
55 | * Run jmap, print numLines of output from it to stderr. | |
56 | * | |
57 | * @param numLines Number of lines to print | |
58 | * @param liveObjectsOnly Should we only print non GC-able objects? | |
59 | * @param jmapPath Path to jmap binary | |
60 | */ | |
61 | public static void heapHistogramDump(int numLines, | |
62 | boolean liveObjectsOnly, | |
63 | String jmapPath) { | |
64 | 0 | heapHistogramDump(numLines, liveObjectsOnly, System.err, jmapPath); |
65 | 0 | } |
66 | ||
67 | /** | |
68 | * Run jmap, print numLines of output from it to stderr. | |
69 | * | |
70 | * @param numLines Number of lines to print | |
71 | * @param jmapPath Path to jmap binary | |
72 | */ | |
73 | public static void heapHistogramDump(int numLines, String jmapPath) { | |
74 | 0 | heapHistogramDump(numLines, System.err, jmapPath); |
75 | 0 | } |
76 | ||
77 | /** | |
78 | * Run jmap, print numLines of output from it to stream passed in. | |
79 | * | |
80 | * @param numLines Number of lines to print | |
81 | * @param printStream Stream to print to | |
82 | * @param jmapPath Path to jmap binary | |
83 | */ | |
84 | public static void heapHistogramDump(int numLines, PrintStream printStream, | |
85 | String jmapPath) { | |
86 | 0 | heapHistogramDump(numLines, false, printStream, jmapPath); |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * Run jmap, print numLines of output from it to stream passed in. | |
91 | * | |
92 | * @param numLines Number of lines to print | |
93 | * @param liveObjectsOnly Should we only print non GC-able objects? | |
94 | * @param printStream Stream to print to | |
95 | * @param jmapPath Path to jmap binary | |
96 | */ | |
97 | private static void heapHistogramDump(int numLines, | |
98 | boolean liveObjectsOnly, | |
99 | PrintStream printStream, | |
100 | String jmapPath) { | |
101 | try { | |
102 | 0 | String args = liveObjectsOnly ? LIVE_HISTO_OPTION : ARGS; |
103 | 0 | Process p = Runtime.getRuntime().exec(jmapPath + args + getProcessId()); |
104 | 0 | BufferedReader in = new BufferedReader( |
105 | 0 | new InputStreamReader(p.getInputStream(), Charset.defaultCharset())); |
106 | 0 | printStream.println("JMap " + |
107 | (liveObjectsOnly ? "histo:live" : "histo") + | |
108 | " dump at " + new Date()); | |
109 | 0 | String line = in.readLine(); |
110 | 0 | for (int i = 0; i < numLines && line != null; ++i) { |
111 | 0 | printStream.println("--\t" + line); |
112 | 0 | line = in.readLine(); |
113 | } | |
114 | 0 | in.close(); |
115 | 0 | } catch (IOException e) { |
116 | 0 | e.printStackTrace(); |
117 | 0 | } |
118 | 0 | } |
119 | } |