@@ -121,7 +121,8 @@ class HprofHeap implements Heap {
121
121
private static final boolean DEBUG = false ;
122
122
123
123
private static final String SNAPSHOT_ID = "NBPHD" ;
124
- private static final int SNAPSHOT_VERSION = 2 ;
124
+ private static final int SNAPSHOT_VERSION = 3 ;
125
+ private static final String OS_PROP = "os.name" ;
125
126
126
127
//~ Instance fields ----------------------------------------------------------------------------------------------------------
127
128
@@ -347,6 +348,7 @@ void writeToFile() {
347
348
out = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (outFile ), 32768 ));
348
349
writeToStream (out );
349
350
out .close ();
351
+ cacheDirectory .setDirty (false );
350
352
} catch (IOException ex ) {
351
353
ex .printStackTrace (System .err );
352
354
}
@@ -357,6 +359,8 @@ void writeToStream(DataOutputStream out) throws IOException {
357
359
out .writeUTF (SNAPSHOT_ID );
358
360
out .writeInt (SNAPSHOT_VERSION );
359
361
out .writeUTF (heapDumpFile .getAbsolutePath ());
362
+ out .writeLong (dumpBuffer .getTime ());
363
+ out .writeUTF (System .getProperty (OS_PROP ));
360
364
nearestGCRoot .writeToStream (out );
361
365
allInstanceDumpBounds .writeToStream (out );
362
366
heapDumpSegment .writeToStream (out );
@@ -376,6 +380,9 @@ void writeToStream(DataOutputStream out) throws IOException {
376
380
}
377
381
378
382
HprofHeap (DataInputStream dis , CacheDirectory cacheDir ) throws IOException {
383
+ if (cacheDir .isDirty ()) {
384
+ throw new IOException ("Dirty cache " +cacheDir );
385
+ }
379
386
String id = dis .readUTF ();
380
387
if (!SNAPSHOT_ID .equals (id )) {
381
388
throw new IOException ("Invalid HPROF dump id " +id );
@@ -387,6 +394,14 @@ void writeToStream(DataOutputStream out) throws IOException {
387
394
heapDumpFile = cacheDir .getHeapFile (dis .readUTF ());
388
395
cacheDirectory = cacheDir ;
389
396
dumpBuffer = HprofByteBuffer .createHprofByteBuffer (heapDumpFile );
397
+ long time = dis .readLong ();
398
+ if (time != dumpBuffer .getTime ()) {
399
+ throw new IOException ("HPROF time mismatch. Cached " +time +" from heap dump " +dumpBuffer .getTime ());
400
+ }
401
+ String os = dis .readUTF ();
402
+ if (!os .equals (System .getProperty (OS_PROP ))) {
403
+ throw new IOException ("HPROF OS mismatch. Cached " +os +" current OS " +System .getProperty (OS_PROP ));
404
+ }
390
405
nearestGCRoot = new NearestGCRoot (this , dis );
391
406
allInstanceDumpBounds = new TagBounds (dis );
392
407
heapDumpSegment = new TagBounds (dis );
@@ -523,6 +538,7 @@ void computeInstances() {
523
538
}
524
539
525
540
HeapProgress .progressStart ();
541
+ cacheDirectory .setDirty (true );
526
542
ClassDumpSegment classDumpBounds = getClassDumpSegment ();
527
543
int idSize = dumpBuffer .getIDSize ();
528
544
long [] offset = new long [] { allInstanceDumpBounds .startOffset };
@@ -646,6 +662,7 @@ void computeReferences() {
646
662
Map classIdToClassMap = classDumpBounds .getClassIdToClassMap ();
647
663
648
664
computeInstances ();
665
+ cacheDirectory .setDirty (true );
649
666
for (long counter =0 ; offset [0 ] < allInstanceDumpBounds .endOffset ; counter ++) {
650
667
long start = offset [0 ];
651
668
int tag = readDumpTag (offset );
@@ -729,7 +746,9 @@ void computeRetainedSize() {
729
746
if (retainedSizeComputed ) {
730
747
return ;
731
748
}
732
- new TreeObject (this ,nearestGCRoot .getLeaves ()).computeTrees ();
749
+ LongBuffer leaves = nearestGCRoot .getLeaves ();
750
+ cacheDirectory .setDirty (true );
751
+ new TreeObject (this ,leaves ).computeTrees ();
733
752
domTree = new DominatorTree (this ,nearestGCRoot .getMultipleParents ());
734
753
domTree .computeDominators ();
735
754
long [] offset = new long [] { allInstanceDumpBounds .startOffset };
@@ -793,6 +812,7 @@ void computeRetainedSizeByClass() {
793
812
return ;
794
813
}
795
814
computeRetainedSize ();
815
+ cacheDirectory .setDirty (true );
796
816
long [] offset = new long [] { allInstanceDumpBounds .startOffset };
797
817
798
818
while (offset [0 ] < allInstanceDumpBounds .endOffset ) {
0 commit comments