-
Notifications
You must be signed in to change notification settings - Fork 3
/
LeakHunterQuery2.java
184 lines (168 loc) · 8.08 KB
/
LeakHunterQuery2.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
/*******************************************************************************
* Copyright (c) 2020,2023 IBM Corporation.
* 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:
* Andrew Johnson (IBM Corporation) - initial API and implementation
*******************************************************************************/
package org.eclipse.mat.inspections;
import java.io.File;
import java.text.Format;
import java.util.Arrays;
import java.util.regex.Pattern;
import org.eclipse.mat.query.BytesFormat;
import org.eclipse.mat.query.IResult;
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.query.registry.Converters;
import org.eclipse.mat.query.results.CompositeResult;
import org.eclipse.mat.report.Params;
import org.eclipse.mat.report.QuerySpec;
import org.eclipse.mat.report.SectionSpec;
import org.eclipse.mat.snapshot.Histogram;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.query.SnapshotQuery;
import org.eclipse.mat.util.IProgressListener;
/**
* Looks for leaks based on a delta in retained sizes of
* the dominator tree from two snapshots.
*/
@CommandName("leakhunter2")
@Icon("/META-INF/icons/leak.gif")
@HelpUrl("/org.eclipse.mat.ui.help/tasks/runningleaksuspectreport.html#task_runningleaksuspectreport__compare")
public class LeakHunterQuery2 extends LeakHunterQuery
{
@Argument(advice = Advice.SECONDARY_SNAPSHOT)
public ISnapshot baseline;
@Argument(isMandatory = false)
public String options = "-prefix"; //$NON-NLS-1$
@Argument(isMandatory = false)
public Pattern mask = Pattern.compile("\\s@ 0x[0-9a-f]+" //$NON-NLS-1$
+ "|^(\\[[0-9]+\\], ){0,100}\\[[0-9]+\\](,\\.\\.\\.)?$" //$NON-NLS-1$
+ "|(?<=\\p{javaJavaIdentifierPart}\\[)\\d+(?=\\])" //$NON-NLS-1$
);
@Argument(isMandatory = false, flag = "x")
public String[] extraReferences = new String[] {
"java.util.HashMap$Node:key", //$NON-NLS-1$
"java.util.Hashtable$Entry:key", //$NON-NLS-1$
"java.util.WeakHashMap$Entry:referent", //$NON-NLS-1$
"java.util.concurrent.ConcurrentHashMap$Node:key" //$NON-NLS-1$
};
@Argument(isMandatory = false, flag = "xfile")
public File extraReferencesListFile;
public IResult execute(IProgressListener listener) throws Exception
{
// Get a signed bytes formatter
Histogram dummy = snapshot.getHistogram(new int[0], listener);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
dummy = dummy.diffWithBaseline(dummy);
Format f = dummy.getColumns()[2].getFormatter();
if (f instanceof BytesFormat)
bytesFormatter = (BytesFormat)f;
IResult res = super.execute(listener);
if (res instanceof SectionSpec)
{
// Add in saved dominator tree
QuerySpec spec = new QuerySpec(savedResult.getName());
spec.setResult(savedResult.getResult());
spec.setCommand(savedcmd);
StringBuilder opts = new StringBuilder("-mode DIFF_RATIO_TO_FIRST"); //$NON-NLS-1$
if (options != null)
opts.append(' ').append(options);
if (mask != null)
opts.append(" -mask ").append(Converters.convertAndEscape(Pattern.class, mask)); //$NON-NLS-1$
if (extraReferences != null)
{
opts.append(" -x "); //$NON-NLS-1$
for (String e : extraReferences)
opts.append(" ").append(Converters.convertAndEscape(String.class, e)); //$NON-NLS-1$
}
if (extraReferencesListFile != null)
{
opts.append(" -xfile ").append(Converters.convertAndEscape(File.class, extraReferencesListFile)); //$NON-NLS-1$
}
spec.setCommand("simplecomparison -query \"dominator_tree -groupby NONE\" -retained PRECISE -baseline " + //$NON-NLS-1$
Converters.convertAndEscape(String.class, baseline.getSnapshotInfo().getPath())
+ " -defaultoptions \"\"" //$NON-NLS-1$
+ " -options " + Converters.convertAndEscape(String.class, opts.toString()) //$NON-NLS-1$
);
System.out.println(spec.getCommand());
spec.set(Params.Html.COLLAPSED, Boolean.TRUE.toString());
spec.set(Params.Rendering.SORT_COLUMN, "#5"); //$NON-NLS-1$
spec.set(Params.Rendering.HIDE_COLUMN, "#7,#8,#9"); //$NON-NLS-1$
((SectionSpec) res).add(spec);
}
return res;
}
CompositeResult.Entry savedResult;
String savedcmd;
FindLeaksQuery.SuspectsResultTable callFindLeaks(IProgressListener listener) throws Exception
{
String querycmd = "find_leaks2"; //$NON-NLS-1$
StringBuilder cmd = new StringBuilder(querycmd);
SnapshotQuery query = SnapshotQuery.parse(querycmd, snapshot)
.setArgument("threshold_percent", threshold_percent) //$NON-NLS-1$
.setArgument("max_paths", max_paths) //$NON-NLS-1$
.setArgument("excludes", excludes) //$NON-NLS-1$
.setArgument("baseline", baseline); //$NON-NLS-1$
if (options != null)
query.setArgument("options", options); //$NON-NLS-1$
cmd.append(" -threshold_percent ").append(threshold_percent); //$NON-NLS-1$
cmd.append(" -max_paths ").append(max_paths); //$NON-NLS-1$
addExcludes(cmd);
cmd.append(" -baseline ").append(Converters.convertAndEscape(String.class, baseline.getSnapshotInfo().getPath())); //$NON-NLS-1$
if (mask != null)
{
query.setArgument("mask", mask); //$NON-NLS-1$
cmd.append(" ").append("-mask ").append(Converters.convertAndEscape(Pattern.class, mask)); //$NON-NLS-1$ //$NON-NLS-2$
}
if (extraReferences != null)
{
query.setArgument("extraReferences", Arrays.asList(extraReferences)); //$NON-NLS-1$
cmd.append(" ").append("-x "); //$NON-NLS-1$ //$NON-NLS-2$
for (String e : extraReferences)
cmd.append(" ").append(Converters.convertAndEscape(String.class, e)); //$NON-NLS-1$
}
if (extraReferencesListFile != null)
{
query.setArgument("extraReferencesListFile", extraReferencesListFile); //$NON-NLS-1$
cmd.append(" -xfile ").append(Converters.convertAndEscape(File.class, extraReferencesListFile)); //$NON-NLS-1$
}
savedcmd = cmd.toString();
IResult ret = query.execute(listener);
if (ret instanceof CompositeResult)
{
CompositeResult cr = (CompositeResult)ret;
// The delta dominator tree, save for later.
savedResult = cr.getResultEntries().get(0);
// The leaks, pass back to LinkHunterQuery.
return (FindLeaksQuery.SuspectsResultTable)cr.getResultEntries().get(1).getResult();
}
else
{
return (FindLeaksQuery.SuspectsResultTable)ret;
}
}
static String escape(String s)
{
/*
* abc\def -> abc\def
* abc"def -> abc\"def
* abc\\def -> abc\\\def
* abc\"def -> abc\\\"def
*/
s = s.replaceAll("\\\\(?=\\\\)|\\\\(?=\")|\"", "\\\\$0"); //$NON-NLS-1$ //$NON-NLS-2$
if (s.indexOf(' ') >= 0)
return "\"" + s + "\""; //$NON-NLS-1$ //$NON-NLS-2$
return s;
}
}