-
Notifications
You must be signed in to change notification settings - Fork 42
/
ADIConfig.java
139 lines (111 loc) · 3.66 KB
/
ADIConfig.java
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
131
132
133
134
135
136
137
138
139
package com.adi;
import java.util.Arrays;
import static com.adi.JVMTIConstant.JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
import static com.adi.JVMTIConstant.JVMTI_EVENT_CLASS_PREPARE;
import static com.adi.JVMTIConstant.JVMTI_EVENT_EXCEPTION_CATCH;
import static com.adi.JVMTIConstant.JVMTI_EVENT_MONITOR_CONTENDED_ENTER;
import static com.adi.JVMTIConstant.JVMTI_EVENT_MONITOR_CONTENDED_ENTERED;
import static com.adi.JVMTIConstant.JVMTI_EVENT_OBJECT_FREE;
import static com.adi.JVMTIConstant.JVMTI_EVENT_THREAD_END;
import static com.adi.JVMTIConstant.JVMTI_EVENT_THREAD_START;
import static com.adi.JVMTIConstant.JVMTI_EVENT_VM_OBJECT_ALLOC;
public class ADIConfig {
/**
* ADI 监控的事件类型
*/
public enum Type {
/**
* 监控 Java 对象内存的分配和释放
*/
OBJECT_ALLOC_AND_FREE(JVMTI_EVENT_VM_OBJECT_ALLOC, JVMTI_EVENT_OBJECT_FREE),
/**
* 监控多线程锁竞争
*/
THREAD_MONITOR_CONTEND(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED),
/**
* 只监控对象加载事件,调试用
*
* @deprecated
*/
_CLASS_LOAD(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK),
/**
* 监控线程数量、Class 载入等事件
*/
BRIEF_INFORMATION(JVMTI_EVENT_CLASS_PREPARE, JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END,
JVMTI_EVENT_EXCEPTION_CATCH),
;
private int[] events;
/**
* 给指定 Type 增加新的监听事件。
* <br><b>这是后门方法,使用前一定要充分了解自己在做什么!</b></br>
*
* @param event
*/
public void addEvent(int event) {
int newLength = events.length + 1;
events = Arrays.copyOf(events, newLength);
events[newLength - 1] = event;
}
Type(int... events) {
this.events = events;
}
}
private float sampleIntervalMs;
private int stackDepth;
private int[] events;
public float getSampleIntervalMs() {
return sampleIntervalMs;
}
public int[] getEvents() {
return events;
}
public int getStackDepth() {
return stackDepth;
}
private ADIConfig() {
}
public static class Builder {
private float sampleIntervalMs = 0.8F;
private int[] events;
private int stackDepth = 10;
/**
* 设置事件监控的采样,单位毫秒,默认值是 0.8 毫秒。
* <br><b>支持事件类型:<br></b>OBJECT_ALLOC_AND_FREE
*
* @param ms
* @return
*/
public Builder setSampleInterval(float ms) {
sampleIntervalMs = ms;
return this;
}
/**
* 设置 ADI 监控的事件类型,参考 {@link Type}
*
* @param type
* @return
*/
public Builder setEventType(Type type) {
this.events = type.events;
return this;
}
/**
* 设置获取调用栈的深度,默认 10。
* <br><b>支持事件类型:<br></b>
* OBJECT_ALLOC_AND_FREE、THREAD_MONITOR_CONTEND
*
* @param stackDepth
*/
public Builder setStackDepth(int stackDepth) {
this.stackDepth = stackDepth;
return this;
}
public ADIConfig build() {
ADIConfig config = new ADIConfig();
config.sampleIntervalMs = this.sampleIntervalMs;
config.events = this.events;
config.stackDepth = this.stackDepth;
return config;
}
}
}