-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintSystemDictionary.java
More file actions
130 lines (102 loc) · 4.56 KB
/
Copy pathPrintSystemDictionary.java
File metadata and controls
130 lines (102 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import sun.jvm.hotspot.debugger.Address;
import sun.jvm.hotspot.memory.Dictionary;
import sun.jvm.hotspot.memory.DictionaryEntry;
import sun.jvm.hotspot.memory.SystemDictionary;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
import sun.jvm.hotspot.utilities.BasicHashtableEntry;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class PrintSystemDictionary {
public static void main(String args[]) {
try {
System.out.println("Hanyu King " + args[0]);
new PrintSystemDictionaryTask().doStart(args);
System.out.println("Hanyu King end " + args[0]);
} catch (Throwable e) {
for(StackTraceElement stackTraceElement : e.getStackTrace()) {
System.out.println(stackTraceElement.toString());
}
System.out.println(e.toString());
System.out.println("cause: " + e.getCause());
}
}
}
class PrintSystemDictionaryTask extends Tool {
public PrintSystemDictionaryTask() {
System.out.println("new PrintSystemDictionaryTask ");
}
class SystemDictionaryPrinter extends Dictionary {
public SystemDictionaryPrinter(Address addr) {
super(addr);
}
void print() {
long[] bucketSize = new long[tableSize()];
System.out.println("Hanyu King tableSize " + tableSize());
Set<String> classLoaders = new HashSet<String>();
Map<String, Long> classTotalMap = new ConcurrentHashMap<String, Long>();
for(int i = 0; i < tableSize(); i++) {
BasicHashtableEntry currHashtableEntry = bucket(i);
if(currHashtableEntry == null) {
continue;
}
DictionaryEntry dictionaryEntry = (DictionaryEntry) currHashtableEntry;
Klass klass = dictionaryEntry.klass();
//System.out.println(klass.getName().asString() + "->" + getClassLoaderOopFrom((InstanceKlass) klass));
classLoaders.add(getClassLoaderOopFrom((InstanceKlass) klass));
while (currHashtableEntry != null) {
bucketSize[i]++;
String className = ((DictionaryEntry) currHashtableEntry).klass().getName().asString();
Long loadedClassTotal;
if((loadedClassTotal = classTotalMap.get(className)) != null) {
classTotalMap.put(className, loadedClassTotal + 1);
} else {
classTotalMap.put(className, 1L);
}
currHashtableEntry = currHashtableEntry.next();
}
}
for(Map.Entry<String, Long> entry : classTotalMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
List<Map.Entry<String,Long>> list = new ArrayList<Map.Entry<String,Long>>(classTotalMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
@Override
public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
return (int) (o1.getValue() - o2.getValue());
}
});
for(Map.Entry<String,Long> mapping : list){
System.out.println(mapping.getKey()+" : "+mapping.getValue());
}
// for(int i = 0; i < bucketSize.length; i++) {
// System.out.println(bucketSize[i]);
// }
// System.out.println(classLoaders);
}
}
private static String getClassLoaderOopFrom(InstanceKlass klass) {
Oop loader = klass.getClassLoader();
return loader != null
? getClassNameFrom((InstanceKlass) loader.getKlass()) + " @ " + loader.getHandle()
: "<bootstrap>";
}
private static String getClassNameFrom(InstanceKlass klass) {
return klass != null ? klass.getName().asString().replace('/', '.') : null;
}
void doStart(String[] args) {
System.out.println("doStart ");
PrintSystemDictionaryTask pst = new PrintSystemDictionaryTask();
pst.start(args);
System.out.println("start.... ");
pst.stop();
System.out.println("stop.... ");
}
@Override
public void run() {
SystemDictionary systemDictionary = VM.getVM().getSystemDictionary();
Dictionary dictionary = systemDictionary.dictionary();
new SystemDictionaryPrinter(dictionary.getAddress()).print();
}
}