-
Notifications
You must be signed in to change notification settings - Fork 3
/
DTFJIndexBuilder.java
9724 lines (9261 loc) · 395 KB
/
DTFJIndexBuilder.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2009,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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.mat.dtfj;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayLong;
import org.eclipse.mat.collect.BitField;
import org.eclipse.mat.collect.HashMapIntLong;
import org.eclipse.mat.collect.HashMapIntObject;
import org.eclipse.mat.collect.HashMapLongObject;
import org.eclipse.mat.collect.IteratorInt;
import org.eclipse.mat.collect.IteratorLong;
import org.eclipse.mat.collect.SetInt;
import org.eclipse.mat.collect.SetLong;
import org.eclipse.mat.parser.IIndexBuilder;
import org.eclipse.mat.parser.IPreliminaryIndex;
import org.eclipse.mat.parser.index.IIndexReader;
import org.eclipse.mat.parser.index.IIndexReader.IOne2ManyIndex;
import org.eclipse.mat.parser.index.IIndexReader.IOne2SizeIndex;
import org.eclipse.mat.parser.index.IndexManager.Index;
import org.eclipse.mat.parser.index.IndexReader.SizeIndexReader;
import org.eclipse.mat.parser.index.IndexWriter;
import org.eclipse.mat.parser.index.IndexWriter.IntIndexStreamer;
import org.eclipse.mat.parser.index.IndexWriter.LongIndexStreamer;
import org.eclipse.mat.parser.index.IndexWriter.SizeIndexCollectorUncompressed;
import org.eclipse.mat.parser.model.ClassImpl;
import org.eclipse.mat.parser.model.XGCRootInfo;
import org.eclipse.mat.parser.model.XSnapshotInfo;
import org.eclipse.mat.snapshot.MultipleSnapshotsException;
import org.eclipse.mat.snapshot.UnreachableObjectsHistogram;
import org.eclipse.mat.snapshot.model.Field;
import org.eclipse.mat.snapshot.model.FieldDescriptor;
import org.eclipse.mat.snapshot.model.GCRootInfo;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IObject.Type;
import org.eclipse.mat.snapshot.model.ObjectReference;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.IProgressListener.Severity;
import org.eclipse.mat.util.MessageUtil;
import com.ibm.dtfj.image.CorruptData;
import com.ibm.dtfj.image.CorruptDataException;
import com.ibm.dtfj.image.DTFJException;
import com.ibm.dtfj.image.DataUnavailable;
import com.ibm.dtfj.image.Image;
import com.ibm.dtfj.image.ImageAddressSpace;
import com.ibm.dtfj.image.ImageFactory;
import com.ibm.dtfj.image.ImagePointer;
import com.ibm.dtfj.image.ImageProcess;
import com.ibm.dtfj.image.ImageSection;
import com.ibm.dtfj.image.ImageStackFrame;
import com.ibm.dtfj.image.ImageThread;
import com.ibm.dtfj.image.MemoryAccessException;
import com.ibm.dtfj.java.JavaClass;
import com.ibm.dtfj.java.JavaClassLoader;
import com.ibm.dtfj.java.JavaField;
import com.ibm.dtfj.java.JavaHeap;
import com.ibm.dtfj.java.JavaLocation;
import com.ibm.dtfj.java.JavaMethod;
import com.ibm.dtfj.java.JavaMonitor;
import com.ibm.dtfj.java.JavaObject;
import com.ibm.dtfj.java.JavaReference;
import com.ibm.dtfj.java.JavaRuntime;
import com.ibm.dtfj.java.JavaStackFrame;
import com.ibm.dtfj.java.JavaThread;
import com.ibm.dtfj.java.JavaVMOption;
import com.ibm.dtfj.runtime.ManagedRuntime;
/**
* Reads and parses a DTFJ dump, building indexes which are then used by MAT to create a snapshot.
* @author ajohnson
*/
public class DTFJIndexBuilder implements IIndexBuilder
{
/*
* Names used as pseudo-class names
* Not translatable
*/
static final String METHOD = "<method>"; //$NON-NLS-1$
private static final String METHOD_TYPE = "<method type>"; //$NON-NLS-1$
static final String STACK_FRAME = "<stack frame>"; //$NON-NLS-1$
private static final String NATIVE_MEMORY = "<native memory>"; //$NON-NLS-1$
private static final String NATIVE_MEMORY_TYPE = "<native memory type>"; //$NON-NLS-1$
/*
* Field names for pseudo classes.
* Not translatable
*/
static final String STACK_DEPTH = "stackDepth"; //$NON-NLS-1$
static final String FRAME_NUMBER = "frameNumber"; //$NON-NLS-1$
static final String LOCATION_ADDRESS = "locationAddress"; //$NON-NLS-1$
static final String COMPILATION_LEVEL = "compilationLevel"; //$NON-NLS-1$
static final String NATIVE = "native"; //$NON-NLS-1$
static final String LINE_NUMBER = "lineNumber"; //$NON-NLS-1$
private static final String DECLARING_CLASS = "declaringClass"; //$NON-NLS-1$
static final String METHOD_NAME = "methodName"; //$NON-NLS-1$
static final String FILE_NAME = "fileName"; //$NON-NLS-1$
/** Separator between the package/class name and the method name */
static final String METHOD_NAME_PREFIX = "."; //$NON-NLS-1$
/** Unique string only found in method names */
static final String METHOD_NAME_SIG = "("; //$NON-NLS-1$
private static final String PLUGIN_ID = InitDTFJ.getDefault().getBundle().getSymbolicName();
/** The key to store the runtime id out of the dump */
static final String RUNTIME_ID_KEY = "$runtimeId"; //$NON-NLS-1$
/** How many elements in an object array to examine at once */
private static final int ARRAY_PIECE_SIZE = 100000;
/** How many bytes to scan in a native stack frame when looking for GC roots */
private static final int NATIVE_STACK_FRAME_SIZE = 2048;
/** How many bytes to scan in a Java stack frame when looking for GC roots */
private static final int JAVA_STACK_FRAME_SIZE = 256;
/**
* How many bytes to scan in a huge Java stack section when looking for GC
* roots
*/
private static final long JAVA_STACK_SECTION_MAX_SIZE = 1024 * 1024L;
/**
* How many bytes to scan in a huge native stack section when looking for GC
* roots
*/
private static final long NATIVE_STACK_SECTION_MAX_SIZE = 1024 * 1024L;
/** Whether DTFJ has root support */
private static final boolean haveDTFJRoots = true;
/** Whether to use DTFJ Root support */
private static final boolean useDTFJRoots = true;
/**
* Whether DTFJ has references support - per instance as can switch on for
* dumps without field info
*/
private boolean haveDTFJRefs = false;
/**
* Whether to use DTFJ references support - per instance as can switch on
* for dumps without field info
*/
private boolean useDTFJRefs = false;
/**
* Whether to include thread roots (e.g. Java_Locals) as full roots, or just
* thread roots + refs from the thread
*/
private static final boolean useThreadRefsNotRoots = true;
/**
* Find roots - mark all unreferenced objects as roots so that not many
* objects are lost by the initial GC by MAT
*/
private static final boolean presumeRoots = false;
/** Whether to mark all class loaders (if presume roots is true) */
private static final boolean markAllLoaders = false;
/** Whether to mark system classes as heap roots */
private static final boolean useSystemClassRoots = true;
/** Whether to skip heap roots marked marked as weak/soft reference etc. */
private static final boolean skipWeakRoots = true;
/** Whether to fix bad DTFJ superclass */
private static final boolean fixBadSuperclass = false;
/** Whether DTFJ has a bad Method equals */
private static final boolean faultyMethodEquals = false;
private final String methodsAsClassesPref = Platform.getPreferencesService().getString(PLUGIN_ID,
PreferenceConstants.P_METHODS, "", null); //$NON-NLS-1$
private final boolean suppressClassNativeSizes = Platform.getPreferencesService().getBoolean(PLUGIN_ID,
PreferenceConstants.P_SUPPRESS_CLASS_NATIVE_SIZES, false, null);
/** Whether to represent all methods as pseudo-classes */
private final boolean getExtraInfo2 = PreferenceConstants.ALL_METHODS_AS_CLASSES.equals(methodsAsClassesPref);
/** Whether to represent only the stack frames as pseudo-objects */
private final boolean getExtraInfo3 = PreferenceConstants.FRAMES_ONLY.equals(methodsAsClassesPref);
/** Whether to represent stack frames and methods as objects and classes */
private final boolean getExtraInfo = getExtraInfo2 || getExtraInfo3
|| PreferenceConstants.RUNNING_METHODS_AS_CLASSES.equals(methodsAsClassesPref);
/** name of java.lang.Class */
private static final String JAVA_LANG_CLASS ="java/lang/Class"; //$NON-NLS-1$
/** name of java.lang.ClassLoader */
private static final String JAVA_LANG_CLASSLOADER ="java/lang/ClassLoader"; //$NON-NLS-1$
/**
* Whether to guess finalizable objects as those unreferenced objects with
* finalizers
*/
private static final boolean guessFinalizables = true;
/** whether to print out debug information and make errors more severe */
private static final boolean debugInfo = InitDTFJ.getDefault().isDebugging();
/** severity flag for internal - info means error worked around safely */
private static final IProgressListener.Severity Severity_INFO = debugInfo ? Severity.ERROR : Severity.INFO;
/** severity flag for internal - warning means possible problem */
private static final IProgressListener.Severity Severity_WARNING = debugInfo ? Severity.ERROR : Severity.WARNING;
/** How many times to print out a repeating error */
private static final int errorCount = debugInfo ? getDebugOption("org.eclipse.mat.dtfj/errorCount", 20) : 20; //$NON-NLS-1$
/** print out extra information to the console */
private static final boolean verbose = InitDTFJ.getDefault().isDebugging()
&& getDebugOption("org.eclipse.mat.dtfj/debug/verbose", false); //$NON-NLS-1$
/** How many objects before it is worth saving the index to disk */
private static final int INDEX_COUNT_FOR_TEMPFILE = 5000000;
/** The actual dump file */
private File dump;
/** The string prefix used to build the index files */
private String pfx;
/**
* The requested runtime id, or null. In the rare case of more than one Java
* runtime in a dump then this can be used to select another JVM.
*/
private String runtimeId;
/** All the key DTFJ data */
private RuntimeInfo dtfjInfo;
/** The outbound references index */
private IndexWriter.IntArray1NWriter outRefs;
/** The temporary object id to size index, for arrays and variable sized objects - used to build arrayToSize.
* Could instead be a {@link SizeIndexCollectorUncompressed} but that is bigger. */
private ObjectToSize indexToSize;
/** The array size in bytes index */
private IOne2SizeIndex arrayToSize;
/** The object/class id number to address index for accumulation of ids */
private IndexWriter.Identifier indexToAddress0;
/** The object/class id number to address index once all ids are found */
private IIndexReader.IOne2LongIndex indexToAddress;
/** The object id to class id index for accumulation and lookup */
private IndexWriter.IntIndexCollector objectToClass;
/** The object id to class id index once mapping is done */
private IIndexReader.IOne2OneIndex objectToClass1;
/** The class id to MAT class information index */
private HashMapIntObject<ClassImpl> idToClass;
/** The map of object ids to lists of associated GC roots for that object id */
private HashMapIntObject<List<XGCRootInfo>> gcRoot;
/** The map of thread object ids to GC roots owned by that thread */
private HashMapIntObject<HashMapIntObject<List<XGCRootInfo>>> threadRoots;
/** Flag used to not guess if GC roots finds finalizables */
private boolean foundFinalizableGCRoots = false;
/** number of times getModifiers succeeded */
private int modifiersFound;
/** address space pointer size */
private int addressSpacePointerSize;
/** compressed refs found */
private boolean compressedRefs;
/** Used to remember dummy addresses for classes without addresses */
private HashMap<JavaClass, Long> dummyClassAddress = new HashMap<JavaClass, Long>();
/** Used to remember dummy addresses for methods without addresses */
private HashMap<JavaMethod, Long> dummyMethodAddress = new HashMap<JavaMethod, Long>();
/** Used to remember dummy addresses for methods without addresses which have a faulty equals() method */
private IdentityHashMap<JavaMethod, Long> dummyMethodAddress2 = new IdentityHashMap<JavaMethod, Long>();
/**
* Used to remember method addresses in case two different methods attempt
* to use the same address
*/
private HashMapLongObject<JavaMethod> methodAddresses = new HashMapLongObject<JavaMethod>();
/** The next address to use for a class without an address */
private long nextClassAddress = 0x1000000080000000L;
/** Used to store the addresses of all the classes loaded by each class loader */
HashMapIntObject<ArrayLong> loaderClassCache;
/**
* Just used to check efficiency of pseudo roots - holds alternative set of
* roots.
*/
private HashMapIntObject<String> missedRoots;
/**
* Used to see how much memory has been freed by the initial garbage
* collection
*/
private HashMapIntObject<ClassImpl> idToClass2;
/**
* Used to see how much memory has been freed by the initial garbage
* collection
*/
private IndexWriter.IntIndexCollector objectToClass2;
/** Which class instances to possibly discard */
private Pattern discardPattern = Pattern.compile("char\\[\\]|java\\.lang\\.String"); //$NON-NLS-1$
/** How often to discard */
private double discardRatio = 0.0;
/** Select which group of objects are discarded */
private double discardOffset = 0.0;
/** Random number seed for choosing discards */
private long discardSeed = 1;
/** Random number generator to choose what to discard */
private Random rand = new Random(discardSeed);
/** The class id to MAT class for discarded objects information index */
private HashMapIntObject<ClassImpl> discardIdToClass;
/**
* Used for very corrupt dumps without a java/lang/Class
*/
private static final class DummyJavaClass implements JavaClass, CorruptData
{
private final String name;
public DummyJavaClass(String name)
{
this.name = name;
}
public JavaClassLoader getClassLoader() throws CorruptDataException
{
throw new CorruptDataException(this);
}
public JavaClass getComponentType() throws CorruptDataException
{
throw new CorruptDataException(this);
}
public Iterator<?> getConstantPoolReferences()
{
return Collections.EMPTY_LIST.iterator();
}
public Iterator<?> getDeclaredFields()
{
return Collections.EMPTY_LIST.iterator();
}
public Iterator<?> getDeclaredMethods()
{
return Collections.EMPTY_LIST.iterator();
}
public ImagePointer getID()
{
return null;
}
public Iterator<?> getInterfaces()
{
return Collections.EMPTY_LIST.iterator();
}
public int getModifiers() throws CorruptDataException
{
throw new CorruptDataException(this);
}
public String getName() throws CorruptDataException
{
return name;
}
public JavaObject getObject() throws CorruptDataException
{
return null;
}
public Iterator<?> getReferences()
{
return Collections.EMPTY_LIST.iterator();
}
public JavaClass getSuperclass() throws CorruptDataException
{
throw new CorruptDataException(this);
}
public boolean isArray() throws CorruptDataException
{
return false;
}
public ImagePointer getAddress()
{
return null;
}
public long getInstanceSize() throws DataUnavailable, CorruptDataException
{
throw new DataUnavailable();
}
public JavaObject getProtectionDomain() throws DataUnavailable, CorruptDataException
{
throw new DataUnavailable();
}
public boolean isPacked() throws DataUnavailable, CorruptDataException
{
throw new DataUnavailable();
}
public long getPackedDataSize() throws DataUnavailable, CorruptDataException
{
throw new DataUnavailable();
}
}
/**
* Use to see how much memory has been freed by the initial garbage
* collection
*/
private static class ObjectToSize
{
/** For small objects */
private byte objectToSize[];
/** For large objects */
private HashMapIntLong bigObjs = new HashMapIntLong();
private static final int SHIFT = 3;
private static final int MASK = 0xff;
ObjectToSize(int size)
{
objectToSize = new byte[size];
}
long get(int index)
{
if (bigObjs.containsKey(index))
{
long size = bigObjs.get(index);
return size;
}
else
{
return (objectToSize[index] & MASK) << SHIFT;
}
}
long getSize(int index)
{
return get(index);
}
/**
* Rely on most object sizes being small, and having the lower 3 bits
* zero. Some classes will have sizes with lower 3 bits not zero, but
* there are a limited number of these. It is better to use expand the
* range for ordinary objects.
*
* @param index
* @param size
*/
void set(int index, long size)
{
if ((size & ~(MASK << SHIFT)) == 0)
{
objectToSize[index] = (byte) (size >>> SHIFT);
}
else
{
bigObjs.put(index, size);
}
}
public IIndexReader.IOne2SizeIndex writeTo(File indexFile) throws IOException
{
final int size = objectToSize.length;
return new SizeIndexReader(new IntIndexStreamer().writeTo(indexFile, new IteratorInt()
{
int i;
public boolean hasNext()
{
return i < size;
}
public int next()
{
if (!hasNext())
throw new NoSuchElementException();
return SizeIndexCollectorUncompressed.compress(getSize(i++));
}
}));
}
}
private ObjectToSize objectToSize2;
/**
* Same as HashMapLongObject except that the first
* item put will be the first returned.
* Relies on key 0 being returned first from {@link HashMapLongObject} and {@link SetLong}.
*/
private static abstract class RefStore<E>
{
long first;
public abstract E put(long key, E value);
public abstract int size();
public abstract Iterator<HashMapLongObject.Entry<E>> entries();
public abstract long[] getAllKeys();
public abstract boolean containsKey(long k);
public abstract IteratorLong keys();
}
private static class RefMap<E> extends RefStore<E>
{
final HashMapLongObject<E> map;
/**
* Create a map
*/
public RefMap()
{
map = new HashMapLongObject<E>();
}
public E put(long key, E value)
{
if (size() == 0)
first = key;
return map.put(key ^ first, value);
}
public int size()
{
return map.size();
}
public Iterator<HashMapLongObject.Entry<E>> entries()
{
final Iterator<HashMapLongObject.Entry<E>> it = map.entries();
return new Iterator<HashMapLongObject.Entry<E>>() {
public boolean hasNext()
{
return it.hasNext();
}
public HashMapLongObject.Entry<E> next()
{
final HashMapLongObject.Entry<E> e = it.next();
return new HashMapLongObject.Entry<E>()
{
public long getKey()
{
return e.getKey() ^ first;
}
public E getValue()
{
return e.getValue();
}
};
}
public void remove()
{
it.remove();
}
};
}
public long[] getAllKeys()
{
long ret[] = map.getAllKeys();
for (int i = 0; i < ret.length; ++i)
{
ret[i] ^= first;
}
return ret;
}
public boolean containsKey(long k)
{
return map.containsKey(k ^ first);
}
public IteratorLong keys()
{
final IteratorLong it = map.keys();
return new IteratorLong()
{
public boolean hasNext()
{
return it.hasNext();
}
public long next()
{
return it.next() ^ first;
}
};
}
}
private static class RefSet<E> extends RefStore<E>
{
final SetLong set;
/**
* Create a map which just stores keys, not values
*/
public RefSet()
{
set = new SetLong();
}
/**
* Note: returns null if no value already set, or
* the supplied value if an existing value is set,
* but not the previous value.
*/
public E put(long key, E value)
{
if (size() == 0)
first = key;
return set.add(key ^ first) ? null : value;
}
public int size()
{
return set.size();
}
public Iterator<HashMapLongObject.Entry<E>> entries()
{
final IteratorLong it = set.iterator();
return new Iterator<HashMapLongObject.Entry<E>>()
{
public boolean hasNext()
{
return it.hasNext();
}
public HashMapLongObject.Entry<E> next()
{
final long e = it.next();
return new HashMapLongObject.Entry<E>()
{
public long getKey()
{
return e ^ first;
}
public E getValue()
{
throw new UnsupportedOperationException();
}
};
}
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
public long[] getAllKeys()
{
long ret[] = set.toArray();
for (int i = 0; i < ret.length; ++i)
{
ret[i] ^= first;
}
return ret;
}
public boolean containsKey(long k)
{
return set.contains(k ^ first);
}
public IteratorLong keys()
{
final IteratorLong it = set.iterator();
return new IteratorLong()
{
public boolean hasNext()
{
return it.hasNext();
}
public long next()
{
return it.next() ^ first;
}
};
}
}
/* Message counts to reduce duplicated messages */
private int msgNgetRefsMissing = errorCount;
private int msgNgetRefsExtra = errorCount;
private int msgNarrayRefsNPE = errorCount;
private int msgNgetRefsUnavailable = errorCount;
private int msgNgetRefsCorrupt = errorCount;
private int msgNbigSegs = errorCount;
private int msgNinvalidArray = errorCount;
private int msgNinvalidObj = errorCount;
private int msgNbrokenEquals = errorCount;
private int msgNbrokenInterfaceSuper = errorCount;
private int msgNmissingLoaderMsg = errorCount;
private int msgNcorruptCount = errorCount;
private int msgNrootsWarning = errorCount;
private int msgNguessFinalizable = errorCount;
private int msgNgetRefsAllMissing = errorCount;
private int msgNgetSuperclass = errorCount;
private int msgNnullThreadObject = errorCount;
private int msgNbadThreadInfo = errorCount;
private int msgNunexpectedModifiers = errorCount;
private int msgNcorruptSection = errorCount;
private int msgNclassForObject = errorCount;
private int msgNcomponentClass = errorCount;
private int msgNtypeForClassObject = errorCount;
private int msgNobjectSize = errorCount;
private int msgNoutboundReferences = errorCount;
private int msgNnoSuperClassForArray = errorCount;
private int msgNproblemReadingJavaStackFrame = errorCount;
private int msgNskipHeapObject = errorCount;
private int msgNthreadBlocking = errorCount;
private int msgNunexpectedSource = errorCount;
private int msgNcorruptCount1 = errorCount;
/*
* (non-Javadoc)
* @see org.eclipse.mat.parser.IIndexBuilder#cancel() Appears to be called
* first before anything happens
*/
public void cancel()
{
// Close DTFJ Image if possible
if (dtfjInfo != null)
DumpCache.releaseDump(dump, dtfjInfo, true);
if (outRefs != null)
{
outRefs.cancel();
outRefs = null;
}
if (arrayToSize != null)
{
try
{
arrayToSize.close();
}
catch (IOException e)
{}
arrayToSize.delete();
arrayToSize = null;
}
indexToAddress0 = null;
if (indexToAddress != null)
{
try
{
indexToAddress.close();
}
catch (IOException e)
{}
indexToAddress.delete();
indexToAddress = null;
}
objectToClass = null;
if (objectToClass1 != null)
{
try
{
objectToClass1.close();
}
catch (IOException e)
{}
objectToClass1.delete();
objectToClass1 = null;
}
idToClass = null;
if (objectToClass2 != null)
{
try
{
objectToClass2.close();
objectToClass2.delete();
}
catch (IOException e)
{}
objectToClass2 = null;
}
idToClass2 = null;
objectToSize2 = null;
}
/*
* (non-Javadoc)
* @see org.eclipse.mat.parser.IIndexBuilder#clean(int[],
* org.eclipse.mat.util.IProgressListener) Called after initial garbage
* collection to show new indices for objects and -1 for object which are
* garbage and have been deleted.
*/
public void clean(int[] purgedMapping, IProgressListener listener) throws IOException
{
if (purgedMapping == null)
{
listener.sendUserMessage(Severity.ERROR, Messages.DTFJIndexBuilder_NullPurgedMapping, null);
return;
}
listener.beginTask(Messages.DTFJIndexBuilder_PurgingDeadObjectsFromImage, purgedMapping.length / 10000);
int count = 0;
long memFree = 0;
for (int i = 0; i < purgedMapping.length; ++i)
{
if (i % 10000 == 0)
{
listener.worked(1);
if (listener.isCanceled()) { throw new IProgressListener.OperationCanceledException(); }
}
if (purgedMapping[i] == -1)
{
++count;
if (objectToSize2 != null)
{
long objSize = objectToSize2.getSize(i);
memFree += objSize;
if (verbose)
{
int type = objectToClass2.get(i);
if (debugInfo) debugPrint("Purging " + i + " size " + objSize + " type " + type + " " + idToClass2.get(type)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
if (missedRoots != null && missedRoots.containsKey(i))
{
if (debugInfo) debugPrint("Alternative roots would have found root " + i + " " + missedRoots.get(i)); //$NON-NLS-1$ //$NON-NLS-2$
}
}
else
{
// if (debugInfo) debugPrint("Remap "+i+"->"+purgedMapping[i]);
}
}
if (debugInfo)
{
listener.sendUserMessage(Severity.INFO, MessageFormat.format(Messages.DTFJIndexBuilder_PurgedIdentifiers,
count, memFree), null);
}
// Free memory
objectToSize2 = null;
missedRoots = null;
DumpCache.releaseDump(dump, dtfjInfo, false);
// Debug
listener.done();
}
/*
* (non-Javadoc)
* @seeorg.eclipse.mat.parser.IIndexBuilder#fill(org.eclipse.mat.parser.
* IPreliminaryIndex, org.eclipse.mat.util.IProgressListener) Fill the
* initial index with: mapping object index to address
*/
public void fill(IPreliminaryIndex index, IProgressListener listener) throws SnapshotException, IOException
{
if (debugInfo) debugPrint("Phase: Fill started"); //$NON-NLS-1$
long then1 = System.currentTimeMillis();
// This is 100% on the progress bar
final int workCount = 10000;
// How many objects to process before indicating to the progress bar
final int workObjectsStep = 10000;
listener.beginTask(MessageFormat.format(Messages.DTFJIndexBuilder_ProcessingImageFromFile, dump), workCount);
int workCountSoFar = 0;
XSnapshotInfo ifo = index.getSnapshotInfo();
// The dump may have changed, so reread it
DumpCache.clearCachedDump(dump);
Serializable dumpType = ifo.getProperty("$heapFormat"); //$NON-NLS-1$
dtfjInfo = DumpCache.getDump(dump, dumpType);
if (ifo.getProperty("discard_ratio") instanceof Integer) //$NON-NLS-1$
{
discardRatio = (Integer)ifo.getProperty("discard_ratio") / 100.0; //$NON-NLS-1$
if (ifo.getProperty("discard_offset") instanceof Integer) //$NON-NLS-1$
{
discardOffset = (Integer)ifo.getProperty("discard_offset") / 100.0; //$NON-NLS-1$
}
else
{
ifo.setProperty("discard_offset", (int)Math.round(discardOffset * 100)); //$NON-NLS-1$
}
if (ifo.getProperty("discard_seed") instanceof Integer) //$NON-NLS-1$
{
discardSeed = (Integer)ifo.getProperty("discard_seed"); //$NON-NLS-1$
}
else
{
ifo.setProperty("discard_seed", discardSeed); //$NON-NLS-1$
}
rand = new Random(discardSeed);
if (ifo.getProperty("discard_pattern") instanceof String) //$NON-NLS-1$
{
discardPattern = Pattern.compile((String)ifo.getProperty("discard_pattern")); //$NON-NLS-1$
}
else
{
ifo.setProperty("discard_pattern", discardPattern.toString()); //$NON-NLS-1$
}
}
long now1 = System.currentTimeMillis();
listener.sendUserMessage(Severity.INFO, MessageFormat.format(
Messages.DTFJIndexBuilder_TookmsToGetImageFromFile, (now1 - then1), dump, dumpType), null);
String reliabilityCheck = System.getProperty("reliabilityCheck"); //$NON-NLS-1$
if (reliabilityCheck == null)
{
reliabilityCheck = Platform.getPreferencesService().getString(PLUGIN_ID,
PreferenceConstants.P_RELIABILITY_CHECK, "", null); //$NON-NLS-1$
}
// Set a default preference value
if (!PreferenceConstants.RELIABILITY_FATAL.equals(reliabilityCheck)
&& !PreferenceConstants.RELIABILITY_WARNING.equals(reliabilityCheck)
&& !PreferenceConstants.RELIABILITY_SKIP.equals(reliabilityCheck))
{
reliabilityCheck = PreferenceConstants.RELIABILITY_FATAL;
}
if (!PreferenceConstants.RELIABILITY_SKIP.equals(reliabilityCheck))
{
DumpReliability reliability = checkDumpReliability(listener);
if (reliability == DumpReliability.DURING_GARBAGE_COLLECTION)
{
if (PreferenceConstants.RELIABILITY_FATAL.equals(reliabilityCheck))
{
throw new SnapshotException(MessageFormat.format(Messages.DTFJIndexBuilder_DuringGC));
}
else if (PreferenceConstants.RELIABILITY_WARNING.equals(reliabilityCheck))
{
listener.sendUserMessage(Severity.WARNING, Messages.DTFJIndexBuilder_DuringGC, null);
}
}
else if (reliability == DumpReliability.NON_EXCLUSIVE_ACCESS)
{
listener.sendUserMessage(Severity.WARNING, Messages.DTFJIndexBuilder_NonExclusive, null);
}
}
// Basic information
try
{
ifo.setCreationDate(new Date(dtfjInfo.getImage().getCreationTime()));
}
catch (DataUnavailable e)
{
listener.sendUserMessage(Severity.WARNING, Messages.DTFJIndexBuilder_NoDateInImage, e);
}
// Find the JVM
listener.worked(1);
workCountSoFar += 1;
listener.subTask(Messages.DTFJIndexBuilder_FindingJVM);
Serializable rtId = ifo.getProperty(RUNTIME_ID_KEY);
if (rtId instanceof String && ! rtId.equals("")) { //$NON-NLS-1$
runtimeId = (String) rtId;
}