author | ihse |
Wed, 23 Oct 2013 13:05:58 +0200 | |
changeset 20654 | 2c17cbccf2ef |
parent 20388 | 2cf7b26682dc |
child 43406 | 6e7a0f24778b |
permissions | -rw-r--r-- |
20388
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
1 |
/* |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
4 |
* |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
8 |
* |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
13 |
* accompanied this code). |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
14 |
* |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
18 |
* |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
21 |
* questions. |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
22 |
*/ |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
23 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
24 |
import java.lang.management.ManagementFactory; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
25 |
import java.lang.management.RuntimeMXBean; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
26 |
import java.lang.reflect.Field; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
27 |
import java.lang.reflect.Method; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
28 |
import java.util.ArrayList; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
29 |
import java.util.List; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
30 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
31 |
import sun.management.VMManagement; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
32 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
33 |
public class JMapHProfLargeHeapProc { |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
34 |
private static final List<byte[]> heapGarbage = new ArrayList<>(); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
35 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
36 |
public static void main(String[] args) throws Exception { |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
37 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
38 |
buildLargeHeap(args); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
39 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
40 |
// Print our pid on stdout |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
41 |
System.out.println("PID[" + getProcessId() + "]"); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
42 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
43 |
// Wait for input before termination |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
44 |
System.in.read(); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
45 |
} |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
46 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
47 |
private static void buildLargeHeap(String[] args) { |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
48 |
for (long i = 0; i < Integer.parseInt(args[0]); i++) { |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
49 |
heapGarbage.add(new byte[1024]); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
50 |
} |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
51 |
} |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
52 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
53 |
public static int getProcessId() throws Exception { |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
54 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
55 |
// Get the current process id using a reflection hack |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
56 |
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
57 |
Field jvm = runtime.getClass().getDeclaredField("jvm"); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
58 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
59 |
jvm.setAccessible(true); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
60 |
VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
61 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
62 |
Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId"); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
63 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
64 |
pid_method.setAccessible(true); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
65 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
66 |
int pid = (Integer) pid_method.invoke(mgmt); |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
67 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
68 |
return pid; |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
69 |
} |
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
70 |
|
2cf7b26682dc
6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents:
diff
changeset
|
71 |
} |