-
Notifications
You must be signed in to change notification settings - Fork 3
/
SnapshotImpl.java
2479 lines (2175 loc) · 88.3 KB
/
SnapshotImpl.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) 2008, 2023 SAP AG, IBM Corporation and others.
* 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 - validation of indices
*******************************************************************************/
package org.eclipse.mat.parser.internal;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayInt;
import org.eclipse.mat.collect.ArrayIntBig;
import org.eclipse.mat.collect.BitField;
import org.eclipse.mat.collect.HashMapIntObject;
import org.eclipse.mat.collect.IteratorInt;
import org.eclipse.mat.collect.SetInt;
import org.eclipse.mat.parser.IObjectReader;
import org.eclipse.mat.parser.index.IIndexReader;
import org.eclipse.mat.parser.index.IIndexReader.IOne2OneIndex;
import org.eclipse.mat.parser.index.IIndexReader.IOne2SizeIndex;
import org.eclipse.mat.parser.index.IndexManager;
import org.eclipse.mat.parser.index.IndexManager.Index;
import org.eclipse.mat.parser.internal.snapshot.HistogramBuilder;
import org.eclipse.mat.parser.internal.snapshot.MultiplePathsFromGCRootsComputerImpl;
import org.eclipse.mat.parser.internal.snapshot.ObjectCache;
import org.eclipse.mat.parser.internal.snapshot.ObjectMarker;
import org.eclipse.mat.parser.internal.snapshot.PathsFromGCRootsTreeBuilder;
import org.eclipse.mat.parser.internal.snapshot.RetainedSizeCache;
import org.eclipse.mat.parser.internal.util.IntStack;
import org.eclipse.mat.parser.internal.util.ParserRegistry;
import org.eclipse.mat.parser.internal.util.ParserRegistry.Parser;
import org.eclipse.mat.parser.model.AbstractObjectImpl;
import org.eclipse.mat.parser.model.ClassImpl;
import org.eclipse.mat.parser.model.ClassLoaderImpl;
import org.eclipse.mat.parser.model.InstanceImpl;
import org.eclipse.mat.parser.model.ObjectArrayImpl;
import org.eclipse.mat.parser.model.PrimitiveArrayImpl;
import org.eclipse.mat.parser.model.XClassHistogramRecord;
import org.eclipse.mat.parser.model.XGCRootInfo;
import org.eclipse.mat.parser.model.XSnapshotInfo;
import org.eclipse.mat.snapshot.DominatorsSummary;
import org.eclipse.mat.snapshot.DominatorsSummary.ClassDominatorRecord;
import org.eclipse.mat.snapshot.ExcludedReferencesDescriptor;
import org.eclipse.mat.snapshot.Histogram;
import org.eclipse.mat.snapshot.IMultiplePathsFromGCRootsComputer;
import org.eclipse.mat.snapshot.IPathsFromGCRootsComputer;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.PathsFromGCRootsTree;
import org.eclipse.mat.snapshot.UnreachableObjectsHistogram;
import org.eclipse.mat.snapshot.model.GCRootInfo;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IPrimitiveArray;
import org.eclipse.mat.snapshot.model.IThreadStack;
import org.eclipse.mat.snapshot.model.NamedReference;
import org.eclipse.mat.snapshot.model.ObjectReference;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.IProgressListener.OperationCanceledException;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.mat.util.VoidProgressListener;
/**
* The implementation of the {@link ISnapshot} representing the entire heap dump.
*/
public final class SnapshotImpl implements ISnapshot
{
// //////////////////////////////////////////////////////////////
// factory methods
// //////////////////////////////////////////////////////////////
private static final String VERSION = "MAT_01";//$NON-NLS-1$
/**
* Read the snapshot from an already indexed dump.
* @param file the dump file
* @param prefix the prefix of the path to find the index files
* @param listener to report progress
* @return the {@link ISnapshot}
* @throws SnapshotException problem reading index files
* @throws IOException file I/O problem
*/
@SuppressWarnings("unchecked")
public static SnapshotImpl readFromFile(File file, String prefix, IProgressListener listener)
throws SnapshotException, IOException
{
listener.beginTask(Messages.SnapshotImpl_ReopeningParsedHeapDumpFile, 9);
File indexFile = new File(prefix + "index"); //$NON-NLS-1$
try (FileInputStream fis = new FileInputStream(indexFile);
/**
* Classes deserialized:
* org.eclipse.mat.parser.model.XSnapshotInfo
* org.eclipse.mat.parser.model.AbstractObjectImpl
* org.eclipse.mat.parser.model.XGCRootInfo
* org.eclipse.mat.parser.model.ClassImpl
* [Lorg.eclipse.mat.parser.model.XGCRootInfo
* org.eclipse.mat.parser.model.XGCRootInfo
*
* org.eclipse.mat.snapshot.SnapshotInfo
* org.eclipse.mat.snapshot.UnreachableObjectsHistogram
*
* org.eclipse.mat.snapshot.model.GCRootInfo
* org.eclipse.mat.snapshot.model.FieldDescriptor
* org.eclipse.mat.snapshot.model.Field
* [Lorg.eclipse.mat.snapshot.model.FieldDescriptor
* [Lorg.eclipse.mat.snapshot.model.Field
* org.eclipse.mat.snapshot.model.ObjectReference
*
* org.eclipse.mat.collect.HashMapIntObject
* org.eclipse.mat.collect.BitField
*
* java.util.ArrayList:
* java.util.Date
* java.util.HashMap
*
* java.lang.Boolean
* java.lang.Long
* java.lang.Number
* java.lang.Byte
* java.lang.Short
* java.lang.Character
* java.lang.Double
* java.lang.Float
* [I
*
*/
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(fis)) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
// similar to system property jdk.serialFilter
String match="java.lang.*;java.util.*;org.eclipse.mat.parser.model.*;org.eclipse.mat.snapshot.*;org.eclipse.mat.snapshot.model.*;org.eclipse.mat.collect.*;!*"; //$NON-NLS-1$
String nm = desc.getName();
if (!nm.startsWith("[")) //$NON-NLS-1$
{
for (String pt : match.split(";")) //$NON-NLS-1$
{
boolean not = pt.startsWith("!"); //$NON-NLS-1$
if (not)
pt = pt.substring(1);
boolean m;
if (pt.endsWith(".**")) //$NON-NLS-1$
m = nm.startsWith(pt.substring(0, pt.length() - 2));
else if (pt.endsWith(".*")) //$NON-NLS-1$
m = nm.startsWith(pt.substring(0, pt.length() - 1))
&& !nm.substring(pt.length() - 1).contains("."); //$NON-NLS-1$
else if (pt.endsWith("*")) //$NON-NLS-1$
m = nm.startsWith(pt.substring(0, pt.length() - 1));
else
m = nm.equals(pt);
if (not && m)
throw new InvalidClassException(nm, match);
if (m)
break;
}
}
return super.resolveClass(desc);
}
};)
{
listener.worked(1);
String version = in.readUTF();
if (!VERSION.equals(version))
throw new IOException(MessageUtil.format(Messages.SnapshotImpl_Error_UnknownVersion, version));
String objectReaderUniqueIdentifier = in.readUTF();
Parser parser = ParserPlugin.getDefault().getParserRegistry().lookupParser(objectReaderUniqueIdentifier);
if (parser == null)
throw new IOException(Messages.SnapshotImpl_Error_ParserNotFound + objectReaderUniqueIdentifier);
listener.worked(1);
IObjectReader heapObjectReader = parser.create(IObjectReader.class, ParserRegistry.OBJECT_READER);
if (heapObjectReader == null)
throw new SnapshotException(MessageUtil.format(Messages.SnapshotFactoryImpl_Error_OpeningHeapDump, file));
XSnapshotInfo snapshotInfo = (XSnapshotInfo) in.readObject();
snapshotInfo.setProperty("$heapFormat", parser.getId()); //$NON-NLS-1$
HashMapIntObject<ClassImpl> classCache = (HashMapIntObject<ClassImpl>) in.readObject();
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
HashMapIntObject<XGCRootInfo[]> roots = (HashMapIntObject<XGCRootInfo[]>) in.readObject();
HashMapIntObject<HashMapIntObject<XGCRootInfo[]>> rootsPerThread = (HashMapIntObject<HashMapIntObject<XGCRootInfo[]>>) in
.readObject();
listener.worked(1);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
HashMapIntObject<String> loaderLabels = (HashMapIntObject<String>) in.readObject();
BitField arrayObjects = (BitField) in.readObject();
listener.worked(3);
snapshotInfo.setPrefix(prefix);
// Allow a dump to be opened via the index file
if (file.equals(indexFile))
{
// Previous location of the heap dump
file = new File(snapshotInfo.getPath());
if (!file.exists())
{
// Perhaps files were moved, so try in same directory as index
file = new File(indexFile.getParentFile(), file.getName());
snapshotInfo.setPath(file.getAbsolutePath());
}
}
else
{
snapshotInfo.setPath(file.getAbsolutePath());
}
IndexManager indexManager = new IndexManager();
boolean done = false;
try
{
indexManager.init(prefix);
SnapshotImpl ret = new SnapshotImpl(snapshotInfo, heapObjectReader, classCache, roots, rootsPerThread, loaderLabels,
arrayObjects, indexManager);
listener.worked(3);
done = true;
return ret;
}
finally
{
if (!done)
{
// Close files on error to allow delete
indexManager.close();
}
}
}
catch (ClassNotFoundException e)
{
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
throw ioe;
}
catch (ClassCastException e)
{
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
throw ioe;
}
finally
{
listener.done();
}
}
/**
* Create the snapshot after a fresh parse.
* @param snapshotInfo the basic data about the snapshot
* @param objectReaderUniqueIdentifier the type of the snapshot from the parser
* @param heapObjectReader how to read objects from the file
* @param classCache all the classes
* @param roots all the GC roots
* @param rootsPerThread the GC roots per thread
* @param arrayObjects which objects are arrays
* @param indexManager all the indexes
* @param listener to report progress
* @return the new {@link ISnapshot}
* @throws IOException a file I/O problem
* @throws SnapshotException another problem
*/
public static SnapshotImpl create(XSnapshotInfo snapshotInfo, //
String objectReaderUniqueIdentifier, //
IObjectReader heapObjectReader, //
HashMapIntObject<ClassImpl> classCache, //
HashMapIntObject<XGCRootInfo[]> roots, //
HashMapIntObject<HashMapIntObject<XGCRootInfo[]>> rootsPerThread, //
BitField arrayObjects, //
IndexManager indexManager, //
IProgressListener listener) throws IOException, SnapshotException
{
SnapshotImpl answer = new SnapshotImpl(snapshotInfo, heapObjectReader, classCache, roots, rootsPerThread, null,
arrayObjects, indexManager);
answer.calculateLoaderLabels();
try (
FileOutputStream fos = new FileOutputStream(snapshotInfo.getPrefix() + "index");//$NON-NLS-1$
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fos));)
{
out.writeUTF(VERSION);
out.writeUTF(objectReaderUniqueIdentifier);
out.writeObject(answer.snapshotInfo);
out.writeObject(answer.classCache);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
out.writeObject(answer.roots);
out.writeObject(answer.rootsPerThread);
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
out.writeObject(answer.loaderLabels);
out.writeObject(answer.arrayObjects);
}
catch (IOException e)
{
// Throw a snapshot exception so that
// we don't retry with a different parser.
throw new SnapshotException(e);
}
return answer;
}
// //////////////////////////////////////////////////////////////
// member variables
// //////////////////////////////////////////////////////////////
// serialized data
private XSnapshotInfo snapshotInfo;
private HashMapIntObject<ClassImpl> classCache;
private HashMapIntObject<XGCRootInfo[]> roots;
private HashMapIntObject<HashMapIntObject<XGCRootInfo[]>> rootsPerThread;
private HashMapIntObject<String> loaderLabels;
private BitField arrayObjects;
// stored in separate files
private IndexManager indexManager;
// runtime data
private IObjectReader heapObjectReader;
private boolean dominatorTreeCalculated;
private Map<String, List<IClass>> classCacheByName;
private ObjectCache<IObject> objectCache;
private boolean parsedThreads = false;
HashMapIntObject<IThreadStack> threadId2stack;
// //////////////////////////////////////////////////////////////
// constructor
// //////////////////////////////////////////////////////////////
private SnapshotImpl(XSnapshotInfo snapshotInfo, //
IObjectReader heapObjectReader, //
HashMapIntObject<ClassImpl> classCache, //
HashMapIntObject<XGCRootInfo[]> roots, //
HashMapIntObject<HashMapIntObject<XGCRootInfo[]>> rootsPerThread, //
HashMapIntObject<String> loaderLabels, //
BitField arrayObjects, //
IndexManager indexManager) throws SnapshotException, IOException
{
this.snapshotInfo = snapshotInfo;
this.heapObjectReader = heapObjectReader;
this.classCache = classCache;
this.roots = roots;
this.rootsPerThread = rootsPerThread;
this.loaderLabels = loaderLabels;
this.arrayObjects = arrayObjects;
this.indexManager = indexManager;
// initialize data
if (indexManager.i2sv2 == null)
{
// If the class+classloader retained size cache doesn't exist then create it
indexManager.setReader(Index.I2RETAINED, new RetainedSizeCache(snapshotInfo));
}
this.classCacheByName = new HashMap<String, List<IClass>>(this.classCache.size());
for (Iterator<ClassImpl> iter = this.classCache.values(); iter.hasNext();)
{
ClassImpl clasz = iter.next();
clasz.setSnapshot(this);
List<IClass> list = classCacheByName.get(clasz.getName());
if (list == null)
classCacheByName.put(clasz.getName(), list = new ArrayList<IClass>());
list.add(clasz);
}
this.dominatorTreeCalculated = indexManager.dominated() != null && indexManager.o2retained() != null
&& indexManager.dominator() != null;
this.objectCache = new HeapObjectCache(this, 1000);
this.heapObjectReader.open(this);
Object unreach = snapshotInfo.getProperty(UnreachableObjectsHistogram.class.getName());
if (unreach instanceof UnreachableObjectsHistogram)
((UnreachableObjectsHistogram)unreach).setSnapshot(this);
}
private void calculateLoaderLabels() throws SnapshotException
{
loaderLabels = new HashMapIntObject<String>();
long usedHeapSize = 0;
int systemClassLoaderId = indexManager.o2address().reverse(0);
Object[] classes = classCache.getAllValues();
for (int i = 0; i < classes.length; i++)
{
ClassImpl clasz = (ClassImpl) classes[i];
usedHeapSize += clasz.getTotalSize();
int classLoaderId = clasz.getClassLoaderId();
String label = loaderLabels.get(classLoaderId);
if (label != null)
continue;
if (classLoaderId == systemClassLoaderId)
{
label = "<system class loader>";//$NON-NLS-1$
}
else
{
IObject classLoader = getObject(classLoaderId);
label = classLoader.getClassSpecificName();
if (label == null)
label = ClassLoaderImpl.NO_LABEL;
}
loaderLabels.put(classLoaderId, label);
}
// now, let's go through all instances of all sub classes to attach
// labels
Collection<IClass> loaderClasses = getClassesByName(IClass.JAVA_LANG_CLASSLOADER, true);
if (loaderClasses != null)
{
for (IClass clazz : loaderClasses)
{
for (int classLoaderId : clazz.getObjectIds())
{
String label = loaderLabels.get(classLoaderId);
if (label != null)
continue;
if (classLoaderId == systemClassLoaderId)
{
label = "<system class loader>";//$NON-NLS-1$
}
else
{
IObject classLoader = getObject(classLoaderId);
label = classLoader.getClassSpecificName();
if (label == null)
label = ClassLoaderImpl.NO_LABEL;
}
loaderLabels.put(classLoaderId, label);
}
}
}
snapshotInfo.setUsedHeapSize(usedHeapSize);
// numberOfObjects was previously calculated by summing getNumberOfObjects() for
// each class. Sometimes there was a mismatch. See bug 294311
snapshotInfo.setNumberOfObjects(indexManager.idx.size());
snapshotInfo.setNumberOfClassLoaders(loaderLabels.size());
snapshotInfo.setNumberOfGCRoots(roots.size());
snapshotInfo.setNumberOfClasses(classCache.size());
// important: refresh object cache. To calculate the loader labels, the
// class loader instances are loaded. however, the object cache uses the
// loader label to determine the implementation class. hence, cached
// instances from creating the labels have the wrong implementation
// class. hence, the refresh.
objectCache.clear();
}
// //////////////////////////////////////////////////////////////
// interface implementation
// //////////////////////////////////////////////////////////////
@Override
public XSnapshotInfo getSnapshotInfo()
{
return snapshotInfo;
}
@Override
public int[] getGCRoots() throws SnapshotException
{
return roots.getAllKeys();
// return Arrays.asList((GCRootInfo[]) roots.getAllValues(new
// GCRootInfo[roots.size()]));
}
@Override
public Collection<IClass> getClasses() throws SnapshotException
{
return Arrays.asList(classCache.getAllValues(new IClass[classCache.size()]));
}
@Override
public Collection<IClass> getClassesByName(String name, boolean includeSubClasses) throws SnapshotException
{
List<IClass> list = this.classCacheByName.get(name);
if (list == null)
return null;
if (!includeSubClasses)
return Collections.unmodifiableCollection(list);
// use set to filter out duplicate subclasses
Set<IClass> answer = new HashSet<IClass>();
answer.addAll(list);
for (IClass clazz : list)
answer.addAll(clazz.getAllSubclasses());
return answer;
}
@Override
public Collection<IClass> getClassesByName(Pattern namePattern, boolean includeSubClasses) throws SnapshotException
{
Set<IClass> result = new HashSet<IClass>();
Object[] classes = classCache.getAllValues();
for (int i = 0; i < classes.length; i++)
{
IClass clazz = (IClass) classes[i];
if (namePattern.matcher(clazz.getName()).matches())
{
result.add(clazz);
if (includeSubClasses)
{
result.addAll(clazz.getAllSubclasses());
}
}
}
return result;
}
@Override
public Histogram getHistogram(IProgressListener listener) throws SnapshotException
{
if (listener == null)
listener = new VoidProgressListener();
HistogramBuilder histogramBuilder = new HistogramBuilder(Messages.SnapshotImpl_Histogram);
Object[] classes = classCache.getAllValues();
for (int i = 0; i < classes.length; i++)
{
histogramBuilder.put(new XClassHistogramRecord((ClassImpl) classes[i]));
}
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
return histogramBuilder.toHistogram(this, true);
}
@Override
public Histogram getHistogram(int[] objectIds, IProgressListener progressMonitor) throws SnapshotException
{
if (progressMonitor == null)
progressMonitor = new VoidProgressListener();
HistogramBuilder histogramBuilder = new HistogramBuilder(Messages.SnapshotImpl_Histogram);
progressMonitor.beginTask(Messages.SnapshotImpl_BuildingHistogram, objectIds.length >>> 8);
// Arrays.sort(objectIds);
// int[] classIds = indexManager.o2class().getAll(objectIds);
IOne2OneIndex o2class = indexManager.o2class();
int classId;
for (int ii = 0; ii < objectIds.length; ii++)
{
classId = o2class.get(objectIds[ii]);
histogramBuilder.add(classId, objectIds[ii], getHeapSize(objectIds[ii]));
if ((ii & 0xff) == 0)
{
if (progressMonitor.isCanceled())
return null;
progressMonitor.worked(1);
}
}
progressMonitor.done();
return histogramBuilder.toHistogram(this, false);
}
@Override
public int[] getInboundRefererIds(int objectId) throws SnapshotException
{
// Add a useful error message
int nobjs = indexManager.inbound().size();
if (objectId >= nobjs || objectId < 0)
{
throw new SnapshotException(MessageUtil.format(Messages.SnapshotImpl_Error_ObjectNotFound, objectId));
}
return indexManager.inbound().get(objectId);
}
@Override
public int[] getOutboundReferentIds(int objectId) throws SnapshotException
{
// Add a useful error message
int nobjs = indexManager.outbound().size();
if (objectId >= nobjs || objectId < 0)
{
throw new SnapshotException(MessageUtil.format(Messages.SnapshotImpl_Error_ObjectNotFound, objectId));
}
return indexManager.outbound().get(objectId);
}
@Override
public int[] getInboundRefererIds(int[] objectIds, IProgressListener progressMonitor) throws SnapshotException
{
if (progressMonitor == null)
progressMonitor = new VoidProgressListener();
IIndexReader.IOne2ManyIndex inbound = indexManager.inbound();
SetInt result = new SetInt();
progressMonitor.beginTask(Messages.SnapshotImpl_ReadingInboundReferrers, objectIds.length / 100);
// Add a useful error message
int nobjs = indexManager.inbound().size();
for (int ii = 0; ii < objectIds.length; ii++)
{
int objectId = objectIds[ii];
if (objectId >= nobjs || objectId < 0)
{
throw new SnapshotException(MessageUtil.format(Messages.SnapshotImpl_Error_ObjectNotFound, objectId));
}
int[] referees = inbound.get(objectId);
for (int refereeId : referees)
result.add(refereeId);
if (ii % 100 == 0)
{
if (progressMonitor.isCanceled())
return null;
progressMonitor.worked(1);
}
}
int[] endResult = result.toArray();
// It used to be sorted before (TreeSet<Integer>) but I don't
// remember if this is needed
// Arrays.sort(endResult);
progressMonitor.done();
return endResult;
}
@Override
public int[] getOutboundReferentIds(int[] objectIds, IProgressListener progressMonitor) throws SnapshotException
{
if (progressMonitor == null)
progressMonitor = new VoidProgressListener();
IIndexReader.IOne2ManyIndex outbound = indexManager.outbound();
SetInt result = new SetInt();
progressMonitor.beginTask(Messages.SnapshotImpl_ReadingOutboundReferrers, objectIds.length / 100);
// Add a useful error message
int nobjs = indexManager.outbound().size();
for (int ii = 0; ii < objectIds.length; ii++)
{
int objectId = objectIds[ii];
if (objectId >= nobjs || objectId < 0)
{
throw new SnapshotException(MessageUtil.format(Messages.SnapshotImpl_Error_ObjectNotFound, objectId));
}
int[] referees = outbound.get(objectId);
for (int refereeId : referees)
result.add(refereeId);
if (ii % 100 == 0)
{
if (progressMonitor.isCanceled())
return null;
progressMonitor.worked(1);
}
}
int[] endResult = result.toArray();
progressMonitor.done();
return endResult;
}
@Override
public IPathsFromGCRootsComputer getPathsFromGCRoots(int objectId, Map<IClass, Set<String>> excludeList)
throws SnapshotException
{
return new PathsFromGCRootsComputerImpl(objectId, excludeList);
}
@Override
public IMultiplePathsFromGCRootsComputer getMultiplePathsFromGCRoots(int[] objectIds,
Map<IClass, Set<String>> excludeList) throws SnapshotException
{
return new MultiplePathsFromGCRootsComputerImpl(objectIds, excludeList, this);
}
int[] getRetainedSetSingleThreaded(int[] objectIds, IProgressListener progressMonitor) throws SnapshotException
{
/* for empty initial set - return immediately an empty retained set */
if (objectIds.length == 0) { return new int[0]; }
/*
* take the retained set of a single object out of the dominator tree -
* it's faster
*/
if (objectIds.length == 1) { return getSingleObjectRetainedSet(objectIds[0]); }
int numberOfObjects = snapshotInfo.getNumberOfObjects();
if (progressMonitor == null)
progressMonitor = new VoidProgressListener();
/* a bit field to mark all reached objects */
boolean[] reachable = new boolean[numberOfObjects];
/*
* Initially mark all the objects whose retained set is to be calculated
* Thus the dfs will not go through this objects, and all objects
* retained from them will stay unmarked (the bits will be clear)
*/
for (int objId : objectIds)
{
reachable[objId] = true;
}
/*
* The dfs() will start from the GC roots, follow the outbound
* references, and mark all unmarked objects. The retained set will
* contain the unmarked objects
*/
ObjectMarker marker = new ObjectMarker(roots.getAllKeys(), reachable, indexManager.outbound(),
IndexManager.Index.OUTBOUND.getFile(getSnapshotInfo().getPrefix()).length(),
progressMonitor);
int numReached;
try
{
numReached = marker.markSingleThreaded();
}
catch (OperationCanceledException e)
{
// $JL-EXC$
return null;
}
// int numReached = dfs(reachable);
int[] retained = new int[numberOfObjects - numReached];
/*
* Unmark also the initial objects, as we want them to be included in
* the retained set
*/
for (int objId : objectIds)
{
reachable[objId] = false;
}
/* Put each unmarked bit into the retained set */
int j = 0;
for (int i = 0; i < numberOfObjects; i++)
{
if (!reachable[i])
{
retained[j++] = i;
}
}
return retained;
}
private int[] getRetainedSetMultiThreaded(int[] objectIds, int availableProcessors,
IProgressListener progressMonitor) throws SnapshotException
{
/* for empty initial set - return immediately an empty retained set */
if (objectIds.length == 0) { return new int[0]; }
/*
* take the retained set of a single object out of the dominator tree -
* it's faster
*/
if (objectIds.length == 1) { return getSingleObjectRetainedSet(objectIds[0]); }
int numberOfObjects = snapshotInfo.getNumberOfObjects();
if (progressMonitor == null)
progressMonitor = new VoidProgressListener();
/* a boolean[] to mark all reached objects */
boolean[] reachable = new boolean[numberOfObjects];
/*
* Initially mark all the objects whose retained set is to be calculated
* Thus the dfs will not go through this objects, and all objects
* retained from them will stay unmarked (the bits will be clear)
*/
for (int objId : objectIds)
{
reachable[objId] = true;
}
/*
* Mark all the GC roots, and keep them in a stack. The worker threads
* are going to pop() one by one the gc roots and do the marking from
* them
*/
int[] gcRoots = roots.getAllKeys();
ObjectMarker marker = new ObjectMarker(gcRoots, reachable, indexManager.outbound(),
IndexManager.Index.OUTBOUND.getFile(getSnapshotInfo().getPrefix()).length(),
progressMonitor);
try
{
marker.markMultiThreaded(availableProcessors);
}
catch (InterruptedException e)
{
throw new SnapshotException(e);
}
/*
* Unmark also the initial objects, as we want them to be included in
* the retained set
*/
for (int objId : objectIds)
{
reachable[objId] = false;
}
// Clear to make space
objectIds = null;
/*
* build the result in an IntArray - the exact number of marked is not
* known
*/
ArrayIntBig retained = new ArrayIntBig();
/* Put each unmarked object into the retained set */
for (int i = 0; i < numberOfObjects; i++)
{
if (!reachable[i])
{
retained.add(i);
}
}
// Clear to make space
reachable = null;
return retained.toArray();
}
@Override
public int[] getRetainedSet(int[] objectIds, IProgressListener progressMonitor) throws SnapshotException
{
int availableProcessors = Runtime.getRuntime().availableProcessors();
if (availableProcessors > 1)
{
return getRetainedSetMultiThreaded(objectIds, availableProcessors, progressMonitor);
}
else
{
return getRetainedSetSingleThreaded(objectIds, progressMonitor);
}
}
@Override
public int[] getRetainedSet(int[] objectIds, String[] fieldNames, IProgressListener listener)
throws SnapshotException
{
if (objectIds.length == 0) { return new int[0]; }
int numberOfObjects = indexManager.o2address().size();
if (listener == null)
listener = new VoidProgressListener();
BitField initialSet = new BitField(numberOfObjects);
for (int objId : objectIds)
initialSet.set(objId);
if (listener.isCanceled())
return null;
BitField reachable = new BitField(numberOfObjects);
int markedObjects = dfs2(reachable, initialSet, fieldNames);
int[] retained = new int[numberOfObjects - markedObjects];
int j = 0;
for (int i = 0; i < numberOfObjects; i++)
{
if (!reachable.get(i))
{
retained[j++] = i;
}
}
return retained;
}
@Override
public int[] getRetainedSet(int[] objectIds, ExcludedReferencesDescriptor[] excludedReferences,
IProgressListener progressMonitor) throws SnapshotException
{
if (progressMonitor == null)
progressMonitor = new VoidProgressListener();
/*
* first pass - mark starting from the GC roots, avoiding
* excludedReferences, until initial are reached. The non-marked objects
* will be a common retained set from the excluded and initial objects
*/
boolean[] firstPass = new boolean[getSnapshotInfo().getNumberOfObjects()];
// mark all initial
for (int objId : objectIds)
{
firstPass[objId] = true;
}
ObjectMarker marker = new ObjectMarker(getGCRoots(), firstPass, getIndexManager().outbound,
IndexManager.Index.OUTBOUND.getFile(getSnapshotInfo().getPrefix()).length(),
progressMonitor);
marker.markSingleThreaded(excludedReferences, this);
// un-mark initial - they have to go into the retained set
for (int objId : objectIds)
{
firstPass[objId] = false;
}
/*
* Second pass - from the non-marked objects mark the ones starting from
* the initial set (objectIds)
*/
boolean[] secondPass = new boolean[firstPass.length];
System.arraycopy(firstPass, 0, secondPass, 0, firstPass.length);
ObjectMarker secondMarker = new ObjectMarker(objectIds, secondPass, getIndexManager().outbound,
progressMonitor);
secondMarker.markSingleThreaded();
// Clear to make space
objectIds = null;
/*
* Have to merge the results of the two markings here
*/
int numObjects = getSnapshotInfo().getNumberOfObjects();
ArrayIntBig retainedSet = new ArrayIntBig();
for (int i = 0; i < numObjects; i++)
{
if (!firstPass[i] && secondPass[i])
{
retainedSet.add(i);
}
}
// Clear to make space
firstPass = null;
secondPass = null;
return retainedSet.toArray();
}
@Override
public long getMinRetainedSize(int[] objectIds, IProgressListener progressMonitor)
throws UnsupportedOperationException, SnapshotException
{