-
Notifications
You must be signed in to change notification settings - Fork 3
/
HprofIndexBuilder.java
212 lines (178 loc) · 7.5 KB
/
HprofIndexBuilder.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
/*******************************************************************************
* Copyright (c) 2008, 2023 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 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 - multiple heap dumps
*******************************************************************************/
package org.eclipse.mat.hprof;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.IteratorLong;
import org.eclipse.mat.hprof.extension.IParsingEnhancer;
import org.eclipse.mat.hprof.ui.HprofPreferences;
import org.eclipse.mat.parser.IIndexBuilder;
import org.eclipse.mat.parser.IPreliminaryIndex;
import org.eclipse.mat.parser.index.IIndexReader.IOne2LongIndex;
import org.eclipse.mat.parser.index.IndexWriter;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.mat.util.SimpleMonitor;
public class HprofIndexBuilder implements IIndexBuilder
{
private File file;
private String prefix;
private IOne2LongIndex id2position;
private List<IParsingEnhancer> enhancers;
public void init(File file, String prefix)
{
this.file = file;
this.prefix = prefix;
this.enhancers = new ArrayList<IParsingEnhancer>();
for (EnhancerRegistry.Enhancer enhancer : EnhancerRegistry.instance().delegates())
{
IParsingEnhancer parsingEnhancer = enhancer.parser();
if (parsingEnhancer != null)
this.enhancers.add(parsingEnhancer);
}
}
public void fill(IPreliminaryIndex preliminary, IProgressListener listener) throws SnapshotException, IOException
{
HprofPreferences.HprofStrictness strictnessPreference = HprofPreferences.getCurrentStrictness();
SimpleMonitor monitor = new SimpleMonitor(MessageUtil.format(Messages.HprofIndexBuilder_Parsing,
new Object[] { file.getAbsolutePath() }), listener, new int[] { 500, 1500 });
IHprofParserHandler handler = new HprofParserHandlerImpl();
handler.beforePass1(preliminary.getSnapshotInfo());
long estimatedLength = CompressedRandomAccessFile.estimatedLength(file);
int pass1Work = (int) (CompressedRandomAccessFile.estimateWork(file) / 1000);
SimpleMonitor.Listener mon = (SimpleMonitor.Listener) monitor.nextMonitor();
mon.beginTask(MessageUtil.format(Messages.HprofIndexBuilder_Scanning, new Object[] { file.getAbsolutePath() }),
pass1Work);
Pass1Parser pass1 = new Pass1Parser(handler, mon, strictnessPreference);
Serializable id = preliminary.getSnapshotInfo().getProperty("$runtimeId"); //$NON-NLS-1$
String dumpNrToRead;
if (id instanceof String)
{
dumpNrToRead = (String)id;
}
else
{
dumpNrToRead = pass1.determineDumpNumber();
}
pass1.read(file, prefix, dumpNrToRead, estimatedLength);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
mon.done();
handler.beforePass2(listener);
long streamLength = pass1.streamLength();
mon = (SimpleMonitor.Listener) monitor.nextMonitor();
mon.beginTask(MessageUtil.format(Messages.HprofIndexBuilder_ExtractingObjects,
new Object[] { file.getAbsolutePath() }), (int) (streamLength / 1000));
/*
* Estimate whether parallel processing of object arrays will cause an
* OutOfMemoryError.
*/
long biggestArrays = pass1.biggestArrays();
// Just a guess from experimentation with a dumps with 100x[200000] and 2x[20000000] arrays
long memestimate = biggestArrays * 24;
Runtime runtime = Runtime.getRuntime();
// This free memory calculation is very approximate - we do a GC to get a better estimate
long maxFree = runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory();
if (!(maxFree > memestimate))
{
runtime.gc();
maxFree = runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory();
}
boolean parallel = maxFree > memestimate;
Pass2Parser pass2 = new Pass2Parser(handler, mon, strictnessPreference, streamLength, parallel);
pass2.stackFrameAlign = pass1.stackFrameAlign;
pass2.stackFrameBase = pass1.stackFrameBase;
pass2.read(file, prefix, dumpNrToRead);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
mon.done();
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
for (IParsingEnhancer enhancer : enhancers)
enhancer.onParsingCompleted(handler.getSnapshotInfo());
id2position = handler.fillIn(preliminary, listener);
}
public void clean(final int[] purgedMapping, IProgressListener listener) throws IOException
{
// //////////////////////////////////////////////////////////////
// object 2 hprof position
// //////////////////////////////////////////////////////////////
File indexFile = new File(prefix + "o2hprof.index"); //$NON-NLS-1$
listener.subTask(MessageUtil.format(Messages.HprofIndexBuilder_Writing,
new Object[] { indexFile.getAbsolutePath() }));
IOne2LongIndex newIndex = new IndexWriter.LongIndexStreamer().writeTo(indexFile, new IndexIterator(id2position,
purgedMapping));
try
{
newIndex.close();
}
catch (IOException ignore)
{}
try
{
id2position.close();
}
catch (IOException ignore)
{}
id2position.delete();
id2position = null;
}
public void cancel()
{
if (id2position != null)
{
try
{
id2position.close();
}
catch (IOException ignore)
{
// $JL-EXC$
}
id2position.delete();
}
}
private static final class IndexIterator implements IteratorLong
{
private final IOne2LongIndex id2position;
private final int[] purgedMapping;
private int nextIndex = -1;
private IndexIterator(IOne2LongIndex id2position, int[] purgedMapping)
{
this.id2position = id2position;
this.purgedMapping = purgedMapping;
findNext();
}
public boolean hasNext()
{
return nextIndex < purgedMapping.length;
}
public long next()
{
long answer = id2position.get(nextIndex);
findNext();
return answer;
}
protected void findNext()
{
nextIndex++;
while (nextIndex < purgedMapping.length && purgedMapping[nextIndex] < 0)
nextIndex++;
}
}
}