-
Notifications
You must be signed in to change notification settings - Fork 4
/
BigDropsQuery.java
389 lines (348 loc) · 14.6 KB
/
BigDropsQuery.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************************************
* Copyright (c) 2008, 2020 SAP AG and IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API and implementation
* Andrew Johnson/IBM Corporation - add icon
*******************************************************************************/
package org.eclipse.mat.inspections;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.internal.Messages;
import org.eclipse.mat.query.Bytes;
import org.eclipse.mat.query.Column;
import org.eclipse.mat.query.ContextProvider;
import org.eclipse.mat.query.IContextObject;
import org.eclipse.mat.query.IQuery;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.IResultTree;
import org.eclipse.mat.query.ResultMetaData;
import org.eclipse.mat.query.ResultMetaData.Builder;
import org.eclipse.mat.query.annotations.Argument;
import org.eclipse.mat.query.annotations.Argument.Advice;
import org.eclipse.mat.query.annotations.CommandName;
import org.eclipse.mat.query.annotations.HelpUrl;
import org.eclipse.mat.query.annotations.Icon;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.util.IProgressListener;
@CommandName("big_drops_in_dominator_tree")
@Icon("/META-INF/icons/big_drops.gif")
@HelpUrl("/org.eclipse.mat.ui.help/reference/findingmemoryleak.html#ref_findingmemoryleak__big_drops")
public class BigDropsQuery implements IQuery, IResultTree
{
private final static int ROOT_ID = -1;
private final static String ROOT_LABEL = Messages.BigDropsQuery_Root;
@Argument
public ISnapshot snapshot;
@Argument(advice = Advice.CLASS_NAME_PATTERN, isMandatory = false, flag = "skip")
public Pattern pattern = Pattern.compile("java.*|com\\.sun.\\.*"); //$NON-NLS-1$
// @Argument(isMandatory = false, flag = "t")
// @Help("Differences in the retained sizes of the parent and the children
// bigger than this threshold are considered big drops")
public int thresholdPercent = 1;
BigDropEntry rootEntry;
public IResult execute(IProgressListener listener) throws Exception
{
buildTree(thresholdPercent * snapshot.getSnapshotInfo().getUsedHeapSize() / 100, listener);
return this;
}
private void buildTree(long threshold, IProgressListener listener) throws SnapshotException
{
/*
* The algorithm implies that the children are sorted by their retained
* size descending. This is how the data is stored in the index
*/
StackEntry entry = new StackEntry(ROOT_ID, snapshot.getSnapshotInfo().getUsedHeapSize(), snapshot
.getImmediateDominatedIds(ROOT_ID), 0);
Stack<StackEntry> stack = new Stack<StackEntry>();
stack.push(entry);
rootEntry = new BigDropEntry(ROOT_ID, ROOT_LABEL, snapshot.getSnapshotInfo().getUsedHeapSize(),
entry.children.length, ROOT_ID, ROOT_LABEL, snapshot.getSnapshotInfo().getUsedHeapSize());
Stack<BigDropEntry> dropsStack = new Stack<BigDropEntry>();
dropsStack.push(rootEntry);
int iterations = 0;
while (stack.size() > 0)
{
/* check for cancellation */
iterations++;
if ((iterations & 0xfff) == 0)
{
if (listener.isCanceled()) { throw new IProgressListener.OperationCanceledException(); }
}
// peek at the arguments
entry = stack.peek();
if (entry.nextChild == 0) // first time we see the parent
{
if (entry.children.length == 0) // a leaf node
{
if (entry.parentSize > threshold)
{
// add to result
if (stack.size() > 1) // not a top-level
{
// construct the new entry
IObject obj = snapshot.getObject(entry.parentId);
IObject dominatorObject = getDominator(obj);
BigDropEntry newBigDrop = null;
if (dominatorObject == null) // add to the root
{
newBigDrop = new BigDropEntry(obj.getObjectId(), obj.getDisplayName(), obj
.getRetainedHeapSize(), entry.children.length, ROOT_ID, ROOT_LABEL,
rootEntry.objectRetainedSize.getValue());
}
else
{
newBigDrop = new BigDropEntry(obj.getObjectId(), obj.getDisplayName(), obj
.getRetainedHeapSize(), entry.children.length, dominatorObject
.getObjectId(), dominatorObject.getDisplayName(), dominatorObject
.getRetainedHeapSize());
}
BigDropEntry dropParent = dropsStack.peek();
dropParent.children.add(newBigDrop);
}
}
// the leaf node is processed
stack.pop();
continue;
}
else
// there are some children -> check for big drop
{
long childRetainedSize = snapshot.getRetainedHeapSize(entry.children[entry.nextChild]);
if (entry.parentSize - childRetainedSize > threshold)
{
// add to result
if (stack.size() > 1) // not a top-level
{
// construct the new entry
IObject obj = snapshot.getObject(entry.parentId);
IObject dominatorObject = getDominator(obj);
BigDropEntry newBigDrop = null;
if (dominatorObject == null)
{
newBigDrop = new BigDropEntry(obj.getObjectId(), obj.getDisplayName(), obj
.getRetainedHeapSize(), entry.children.length, ROOT_ID, ROOT_LABEL,
rootEntry.objectRetainedSize.getValue());
}
else
{
newBigDrop = new BigDropEntry(obj.getObjectId(), obj.getDisplayName(), obj
.getRetainedHeapSize(), entry.children.length, dominatorObject
.getObjectId(), dominatorObject.getDisplayName(), dominatorObject
.getRetainedHeapSize());
}
BigDropEntry dropParent = dropsStack.peek();
dropParent.children.add(newBigDrop);
dropsStack.push(newBigDrop);
}
}
else
// skip this object
{
stack.pop();
stack.push(new StackEntry(entry.children[entry.nextChild], childRetainedSize, snapshot
.getImmediateDominatedIds(entry.children[entry.nextChild]), 0));
continue;
}
}
}
/*
* at this point there should be printed parent with some children
*/
if (entry.nextChild < entry.children.length)
{
long childRetainedSize = snapshot.getRetainedHeapSize(entry.children[entry.nextChild]);
if (childRetainedSize > threshold)
{
// one of the children is big enough to get attention
stack.push(new StackEntry(entry.children[entry.nextChild], childRetainedSize, snapshot
.getImmediateDominatedIds(entry.children[entry.nextChild]), 0));
entry.nextChild++;
}
else
{
// pop this parent - it's processed
stack.pop();
if (entry.parentId == dropsStack.peek().objectId)
{
dropsStack.pop();
}
}
}
else
// pop this parent - it's processed
{
stack.pop();
if (entry.parentId == dropsStack.peek().objectId)
{
dropsStack.pop();
}
}
}
}
private static class StackEntry
{
int parentId;
int[] children;
int nextChild;
long parentSize;
public StackEntry(int parentId, long parentSize, int[] children, int nextChild)
{
this.parentId = parentId;
this.parentSize = parentSize;
this.children = children;
this.nextChild = nextChild;
}
}
/*
* returns the first dominator not matching the pattern or null (for the
* <ROOT>)
*/
private IObject getDominator(IObject object) throws SnapshotException
{
while (pattern.matcher(object.getTechnicalName()).matches())
{
int dominatorId = snapshot.getImmediateDominatorId(object.getObjectId());
if (dominatorId == ROOT_ID) { return null; }
object = snapshot.getObject(dominatorId);
}
return object;
}
// //////////////////////////////////////////////////////////////
//
// IResultTree implementation
//
// //////////////////////////////////////////////////////////////
public ResultMetaData getResultMetaData()
{
Builder b = new ResultMetaData.Builder();
for (ContextProvider prov : getContextProviders())
{
b.addContext(prov);
}
return b.build();
}
public Column[] getColumns()
{
return new Column[] { new Column(Messages.BigDropsQuery_Column_AccumulationPoint), //
new Column(Messages.BigDropsQuery_Column_AccPtSize, Bytes.class), //
new Column(Messages.BigDropsQuery_Column_NumChildren, Long.class), //
new Column(Messages.BigDropsQuery_Column_Dominator), //
new Column(Messages.BigDropsQuery_Column_DomRetainedSize, Bytes.class) };
}
public List<?> getChildren(Object parent)
{
return ((BigDropEntry) parent).children;
}
public Object getColumnValue(Object row, int columnIndex)
{
BigDropEntry element = (BigDropEntry) row;
switch (columnIndex)
{
case 0:
return element.objectLabel;
case 1:
return element.objectRetainedSize;
case 2:
return element.numberOfChildren;
case 3:
return element.dominatorLabel;
case 4:
return element.dominatorRetainedSize;
}
return null;
}
public List<?> getElements()
{
return rootEntry.children;
}
public boolean hasChildren(Object parent)
{
return ((BigDropEntry) parent).children.size() > 0;
}
public IContextObject getContext(final Object row)
{
return new IContextObject()
{
public int getObjectId()
{
return ((BigDropEntry) row).objectId;
}
};
}
public ContextProvider[] getContextProviders()
{
return new ContextProvider[] { new ContextProvider(Messages.BigDropsQuery_AccumulationPoint)
{
@Override
public IContextObject getContext(Object row)
{
return getAccumulationPoint(row);
}
}, new ContextProvider(Messages.BigDropsQuery_Dominator)
{
@Override
public IContextObject getContext(Object row)
{
return getDominator(row);
}
} };
}
IContextObject getAccumulationPoint(final Object row)
{
return new IContextObject()
{
public int getObjectId()
{
return ((BigDropEntry) row).objectId;
}
};
}
IContextObject getDominator(final Object row)
{
if (((BigDropEntry) row).dominatorId < 0)
return null;
return new IContextObject()
{
public int getObjectId()
{
return ((BigDropEntry) row).dominatorId;
}
};
}
// ////////////////////////////////////////////
//
// Some structure to hold the data
//
// //////////////////////////////////////////////
public static class BigDropEntry
{
int objectId;
String objectLabel;
Bytes objectRetainedSize;
int numberOfChildren;
int dominatorId;
String dominatorLabel;
Bytes dominatorRetainedSize;
List<BigDropEntry> children = new ArrayList<BigDropEntry>(1);
public BigDropEntry(int objectId, String objectLabel, long objectRetainedSize, int numberOfChildren,
int dominatorId, String dominatorLabel, long dominatorRetainedSize)
{
this.objectId = objectId;
this.objectLabel = objectLabel;
this.objectRetainedSize = new Bytes(objectRetainedSize);
this.numberOfChildren = numberOfChildren;
this.dominatorId = dominatorId;
this.dominatorLabel = dominatorLabel;
this.dominatorRetainedSize = new Bytes(dominatorRetainedSize);
}
}
}