-
Notifications
You must be signed in to change notification settings - Fork 3
/
CustomizedRetainedSetQuery.java
187 lines (168 loc) · 7 KB
/
CustomizedRetainedSetQuery.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
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* IBM Corporation - charset
*******************************************************************************/
package org.eclipse.mat.inspections;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayIntBig;
import org.eclipse.mat.internal.Messages;
import org.eclipse.mat.query.IQuery;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.annotations.Argument;
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.ExcludedReferencesDescriptor;
import org.eclipse.mat.snapshot.Histogram;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.query.IHeapObjectArgument;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.MessageUtil;
@CommandName("customized_retained_set")
@Icon("/META-INF/icons/show_retained_set.gif")
@HelpUrl("/org.eclipse.mat.ui.help/concepts/shallowretainedheap.html")
public class CustomizedRetainedSetQuery implements IQuery
{
@Argument
public ISnapshot snapshot;
@Argument(flag = Argument.UNFLAGGED)
public IHeapObjectArgument objects;
@Argument(isMandatory = false, flag = "x")
public String[] excludedReferences;
@Argument(isMandatory = false, flag = "xfile")
public File excludedReferencesListFile;
public IResult execute(IProgressListener listener) throws Exception
{
int[] retainedSet;
if (excludedReferences == null && excludedReferencesListFile == null)
{
// normal retained set
retainedSet = snapshot.getRetainedSet(objects.getIds(listener), listener);
}
else
{
// retained set with excluded refs
// read the file (if any)
String[] fromFile = getLinesFromFile();
if (fromFile != null && fromFile.length > 0)
{
if (excludedReferences != null)
{
// merge from file and manually entered entries
String[] tmp = new String[fromFile.length + excludedReferences.length];
System.arraycopy(fromFile, 0, tmp, 0, fromFile.length);
System.arraycopy(excludedReferences, 0, tmp, fromFile.length, excludedReferences.length);
excludedReferences = tmp;
}
else
{
excludedReferences = fromFile;
}
}
ExcludedReferencesDescriptor[] excludedRefDescriptors = getExcludedReferenceDescriptors(excludedReferences);
retainedSet = snapshot.getRetainedSet(objects.getIds(listener), excludedRefDescriptors, listener);
}
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
Histogram histogram = snapshot.getHistogram(retainedSet, listener);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
histogram.setLabel(MessageUtil.format(Messages.CustomizedRetainedSetQuery_RetainedBy, objects.getLabel()));
return histogram;
}
private ExcludedReferencesDescriptor[] getExcludedReferenceDescriptors(String[] excludedRefs)
throws SnapshotException
{
ExcludedReferencesDescriptor[] result = new ExcludedReferencesDescriptor[excludedRefs.length];
int i = 0;
for (String s : excludedRefs)
{
// fields are separated by ":"
StringTokenizer tokenizer = new StringTokenizer(s, ":"); //$NON-NLS-1$
String objectsDescription = tokenizer.nextToken();
ArrayIntBig objectIds = new ArrayIntBig();
if (objectsDescription.startsWith("0x")) //$NON-NLS-1$
{
long objAddress = Long.parseLong(objectsDescription.substring(2), 16);
objectIds.add(snapshot.mapAddressToId(objAddress));
}
else
{
Collection<IClass> classes = snapshot.getClassesByName(objectsDescription, true);
if (classes != null)
for (IClass clazz : classes)
{
objectIds.addAll(clazz.getObjectIds());
}
}
Set<String> fields = null;
if (tokenizer.hasMoreTokens())
{
fields = new HashSet<String>();
StringTokenizer fieldTokenizer = new StringTokenizer(tokenizer.nextToken(), ","); //$NON-NLS-1$
while (fieldTokenizer.hasMoreTokens())
{
fields.add(fieldTokenizer.nextToken());
}
}
ExcludedReferencesDescriptor desc = new ExcludedReferencesDescriptor(objectIds.toArray(), fields);
result[i++] = desc;
}
return result;
}
private String[] getLinesFromFile() throws IOException
{
if (excludedReferencesListFile == null)
return null;
List<String> result = new ArrayList<String>();
Charset cs;
try
{
String encoding = ResourcesPlugin.getEncoding();
if (encoding != null)
cs = Charset.forName(encoding);
else
cs = StandardCharsets.UTF_8;
}
catch (UnsupportedCharsetException e)
{
cs = StandardCharsets.UTF_8;
}
try (FileInputStream fis = new FileInputStream(excludedReferencesListFile);
InputStreamReader isr = new InputStreamReader(fis, cs);
BufferedReader in = new BufferedReader(isr))
{
String line = null;
while ((line = in.readLine()) != null)
{
result.add(line);
}
}
return result.toArray(new String[result.size()]);
}
}