-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathagent.cpp
More file actions
279 lines (239 loc) · 8.9 KB
/
agent.cpp
File metadata and controls
279 lines (239 loc) · 8.9 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
#include <jvmti.h>
#include <queue>
#include <iostream>
#include <unordered_map>
#include <cstring>
#include "log.h"
#include "global_data.h"
#include "utils.h"
#include "roots/paths_to_closest_gc_roots.h"
#include "reachability/objects_of_class_in_heap.h"
#include "sizes/shallow_size_by_classes.h"
#include "sizes/retained_size_and_held_objects.h"
#include "sizes/retained_size_by_classes.h"
#include "sizes/retained_size_by_objects.h"
#include "sizes/retained_size_by_classloaders.h"
#include "allocation_sampling.h"
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
static GlobalAgentData *gdata = nullptr;
static bool canSampleAllocations = false;
extern void handleOptions(const char *);
static void setRequiredCapabilities(jvmtiEnv *jvmti, jvmtiCapabilities &effective) {
jvmtiCapabilities potential;
std::memset(&potential, 0, sizeof(jvmtiCapabilities));
std::memset(&effective, 0, sizeof(jvmtiCapabilities));
jvmti->GetPotentialCapabilities(&potential);
if (potential.can_tag_objects) {
effective.can_tag_objects = 1;
}
if (potential.can_generate_object_free_events) {
effective.can_generate_object_free_events = 1;
}
if (potential.can_generate_sampled_object_alloc_events) {
canSampleAllocations = true;
effective.can_generate_sampled_object_alloc_events = 1;
}
if (potential.can_suspend) {
effective.can_suspend = 1;
}
}
static jboolean setAllocationSamplingMode(jvmtiEventMode mode) {
if (canSampleAllocations) {
jvmtiError error = gdata->jvmti->SetEventNotificationMode(mode, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL);
return (jboolean) error == JVMTI_ERROR_NONE;
}
return (jboolean) 0;
}
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
handleOptions(options);
debug("on agent load");
jvmtiEnv *jvmti = nullptr;
jint result = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_0);
if (result != JNI_OK || jvmti == nullptr) {
std::cerr << "ERROR: Unable to access JVMTI!" << std::endl;
return result;
}
debug("set capabilities");
jvmtiCapabilities capabilities;
setRequiredCapabilities(jvmti, capabilities);
jvmtiError error = jvmti->AddCapabilities(&capabilities);
if (error != JVMTI_ERROR_NONE) {
handleError(jvmti, error, "Could not set capabilities");
return JNI_ERR;
}
if (canSampleAllocations) {
error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL);
if (error != JVMTI_ERROR_NONE) {
handleError(jvmti, error, "Could not set allocation sampling notification mode");
return JNI_ERR;
}
}
debug("set callbacks");
jvmtiEventCallbacks callbacks;
std::memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
callbacks.SampledObjectAlloc = SampledObjectAlloc;
jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));
gdata = new GlobalAgentData();
gdata->jvmti = jvmti;
debug("initializing done");
return JNI_OK;
}
JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *vm, char *options, void *reserved) {
delete gdata;
return Agent_OnLoad(vm, options, reserved);
}
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
delete gdata;
Agent_OnLoad(vm, nullptr, reserved);
return JNI_VERSION_1_6;
}
JNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm) {
debug("on agent unload");
delete gdata;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_canEstimateObjectSize(
JNIEnv *env,
jobject thisObject) {
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_canEstimateObjectsSizes(
JNIEnv *env,
jobject thisObject) {
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_canFindPathsToClosestGcRoots(
JNIEnv *env,
jobject thisObject) {
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_canGetRetainedSizeByClasses(
JNIEnv *env,
jobject thisObject) {
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_canGetRetainedSizeByClassLoaders(
JNIEnv *env,
jobject thisObject) {
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_canGetShallowSizeByClasses(
JNIEnv *env,
jobject thisObject) {
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_isLoadedImpl(
JNIEnv *env,
jclass thisClass) {
return gdata != nullptr;
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_estimateRetainedSize(
JNIEnv *env,
jobject thisObject,
jobjectArray objects) {
return RetainedSizeByObjectsAction(env, gdata->jvmti, thisObject).run(objects);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_size(
JNIEnv *env,
jobject thisObject,
jobject object) {
return RetainedSizeAndHeldObjectsAction(env, gdata->jvmti, thisObject).run(object);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_findPathsToClosestGcRoots(
JNIEnv *env,
jobject thisObject,
jobject object,
jint pathsNumber,
jint objectsNumber) {
return PathsToClosestGcRootsAction(env, gdata->jvmti, thisObject).run(object, pathsNumber, objectsNumber);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_getShallowSizeByClasses(
JNIEnv *env,
jobject thisObject,
jobjectArray classesArray) {
return ShallowSizeByClassesAction(env, gdata->jvmti, thisObject).run(classesArray);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_getRetainedSizeByClasses(
JNIEnv *env,
jobject thisObject,
jobjectArray classesArray) {
return RetainedSizeByClassesAction(env, gdata->jvmti, thisObject).run(classesArray);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_getRetainedSizeByClassLoaders(
JNIEnv *env,
jobject thisObject,
jobjectArray classLoadersArray) {
return RetainedSizeByClassLoadersAction(env, gdata->jvmti, thisObject).run(classLoadersArray);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_getShallowAndRetainedSizeByClasses(
JNIEnv *env,
jobject thisObject,
jobjectArray classesArray) {
return RetainedAndShallowSizeByClassesAction(env, gdata->jvmti, thisObject).run(classesArray);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_getFirstReachableObject(
JNIEnv *env,
jobject thisObject,
jobject startObject,
jobjectArray suspectClass) {
return GetFirstReachableObjectOfClassAction(env, gdata->jvmti, thisObject).run(startObject, suspectClass);
}
extern "C"
JNIEXPORT jobjectArray JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_getAllReachableObjects(
JNIEnv *env,
jobject thisObject,
jobject startObject,
jobject suspectClass) {
return GetAllReachableObjectsOfClassAction(env, gdata->jvmti, thisObject).run(startObject, suspectClass);
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_setHeapSamplingInterval(
JNIEnv *env,
jclass thisClass,
jlong interval) {
if (!canSampleAllocations || JVMTI_ERROR_NONE != gdata->jvmti->SetHeapSamplingInterval(interval)) {
return (jboolean) 0;
}
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_initArrayOfListeners(
JNIEnv *env,
jclass thisClass,
jobject array) {
if (!canSampleAllocations) {
return (jboolean) 0;
}
arrayOfListeners.init(env, array);
return (jboolean) 1;
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_enableAllocationSampling(
JNIEnv *env,
jclass thisClass) {
return setAllocationSamplingMode(JVMTI_ENABLE);
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_intellij_memory_agent_IdeaNativeAgentProxy_disableAllocationSampling(
JNIEnv *env,
jclass thisClass) {
return setAllocationSamplingMode(JVMTI_DISABLE);
}
#pragma clang diagnostic pop