< prev index next >

test/lib/jdk/test/lib/hprof/parser/HprofReader.java

Print this page

 79     // Record types:
 80     //
 81     static final int HPROF_UTF8          = 0x01;
 82     static final int HPROF_LOAD_CLASS    = 0x02;
 83     static final int HPROF_UNLOAD_CLASS  = 0x03;
 84     static final int HPROF_FRAME         = 0x04;
 85     static final int HPROF_TRACE         = 0x05;
 86     static final int HPROF_ALLOC_SITES   = 0x06;
 87     static final int HPROF_HEAP_SUMMARY  = 0x07;
 88 
 89     static final int HPROF_START_THREAD  = 0x0a;
 90     static final int HPROF_END_THREAD    = 0x0b;
 91 
 92     static final int HPROF_HEAP_DUMP     = 0x0c;
 93 
 94     static final int HPROF_CPU_SAMPLES   = 0x0d;
 95     static final int HPROF_CONTROL_SETTINGS = 0x0e;
 96     static final int HPROF_LOCKSTATS_WAIT_TIME = 0x10;
 97     static final int HPROF_LOCKSTATS_HOLD_TIME = 0x11;
 98 






 99     static final int HPROF_GC_ROOT_UNKNOWN       = 0xff;
100     static final int HPROF_GC_ROOT_JNI_GLOBAL    = 0x01;
101     static final int HPROF_GC_ROOT_JNI_LOCAL     = 0x02;
102     static final int HPROF_GC_ROOT_JAVA_FRAME    = 0x03;
103     static final int HPROF_GC_ROOT_NATIVE_STACK  = 0x04;
104     static final int HPROF_GC_ROOT_STICKY_CLASS  = 0x05;
105     static final int HPROF_GC_ROOT_THREAD_BLOCK  = 0x06;
106     static final int HPROF_GC_ROOT_MONITOR_USED  = 0x07;
107     static final int HPROF_GC_ROOT_THREAD_OBJ    = 0x08;
108 
109     static final int HPROF_GC_CLASS_DUMP         = 0x20;
110     static final int HPROF_GC_INSTANCE_DUMP      = 0x21;
111     static final int HPROF_GC_OBJ_ARRAY_DUMP         = 0x22;
112     static final int HPROF_GC_PRIM_ARRAY_DUMP         = 0x23;
113 
114     static final int HPROF_HEAP_DUMP_SEGMENT     = 0x1c;
115     static final int HPROF_HEAP_DUMP_END         = 0x2c;
116 
117     private final static int T_CLASS = 2;
118 

225                     byte[] chars = new byte[(int)length - identifierSize];
226                     in.readFully(chars);
227                     names.put(id, new String(chars));
228                     break;
229                 }
230                 case HPROF_LOAD_CLASS: {
231                     int serialNo = in.readInt();        // Not used
232                     long classID = readID();
233                     int stackTraceSerialNo = in.readInt();
234                     long classNameID = readID();
235                     Long classIdI = classID;
236                     String nm = getNameFromID(classNameID).replace('/', '.');
237                     classNameFromObjectID.put(classIdI, nm);
238                     if (classNameFromSerialNo != null) {
239                         classNameFromSerialNo.put(serialNo, nm);
240                     }
241                     break;
242                 }
243 
244                 case HPROF_HEAP_DUMP: {
245                     if (dumpsToSkip <= 0) {
246                         try {
247                             readHeapDump(length, currPos);
248                         } catch (EOFException exp) {
249                             handleEOF(exp, snapshot);
250                         }
251                         if (debugLevel > 0) {
252                             System.out.println("    Finished processing instances in heap dump.");
253                         }
254                         return snapshot;
255                     } else {
256                         dumpsToSkip--;
257                         skipBytes(length);
258                     }
259                     break;
260                 }
261 
262                 case HPROF_HEAP_DUMP_END: {
263                     if (version >= VERSION_JDK6) {
264                         if (dumpsToSkip <= 0) {
265                             skipBytes(length);  // should be no-op
266                             return snapshot;
267                         } else {
268                             // skip this dump (of the end record for a sequence of dump segments)
269                             dumpsToSkip--;
270                         }
271                     } else {
272                         // HPROF_HEAP_DUMP_END only recognized in >= 1.0.2
273                         warn("Ignoring unrecognized record type " + type);
274                     }
275                     skipBytes(length);  // should be no-op
276                     break;
277                 }
278 
279                 case HPROF_HEAP_DUMP_SEGMENT: {
280                     if (version >= VERSION_JDK6) {
281                         if (dumpsToSkip <= 0) {
282                             try {
283                                 // read the dump segment
284                                 readHeapDump(length, currPos);
285                             } catch (EOFException exp) {
286                                 handleEOF(exp, snapshot);
287                             }
288                         } else {
289                             // all segments comprising the heap dump will be skipped
290                             skipBytes(length);
291                         }
292                     } else {
293                         // HPROF_HEAP_DUMP_SEGMENT only recognized in >= 1.0.2
294                         warn("Ignoring unrecognized record type " + type);
295                         skipBytes(length);
296                     }
297                     break;
298                 }
299 
300                 case HPROF_FRAME: {
301                     if (stackFrames == null) {

335                         }
336                         stackTraces.put(serialNo,
337                                         new StackTrace(frames));
338                     }
339                     break;
340                 }
341                 case HPROF_UNLOAD_CLASS:
342                 case HPROF_ALLOC_SITES:
343                 case HPROF_START_THREAD:
344                 case HPROF_END_THREAD:
345                 case HPROF_HEAP_SUMMARY:
346                 case HPROF_CPU_SAMPLES:
347                 case HPROF_CONTROL_SETTINGS:
348                 case HPROF_LOCKSTATS_WAIT_TIME:
349                 case HPROF_LOCKSTATS_HOLD_TIME:
350                 {
351                     // Ignore these record types
352                     skipBytes(length);
353                     break;
354                 }









355                 default: {
356                     skipBytes(length);
357                     warn("Ignoring unrecognized record type " + type);
358                 }
359             }
360         }
361 
362         return snapshot;
363     }
364 
365     public String printStackTraces() {
366         StringBuffer output = new StringBuffer();
367         for (Map.Entry<Integer, StackTrace> entry : stackTraces.entrySet()) {
368             StackFrame[] frames = entry.getValue().getFrames();
369             output.append("SerialNo " + entry.getKey() + "\n");
370             for (int i = 0; i < frames.length; i++) {
371                 output.append("  " + frames[i].getClassName() + "." + frames[i].getMethodName()
372                         + frames[i].getMethodSignature() + " (" + frames[i].getSourceFileName()
373                         + ":" + frames[i].getLineNumber() + ")" + "\n");
374             }

835                 }
836                 case T_INT: {
837                     primitiveSignature = (byte) 'I';
838                     elSize = 4;
839                     break;
840                 }
841                 case T_LONG: {
842                     primitiveSignature = (byte) 'J';
843                     elSize = 8;
844                     break;
845                 }
846             }
847             if (version >= VERSION_JDK12BETA4 && primitiveSignature == 0x00) {
848                 throw new IOException("Unrecognized typecode:  "
849                                         + elementClassID);
850             }
851         }
852         if (primitiveSignature != 0x00) {
853             long size = elSize * (long)num;
854             bytesRead += size;
855             JavaValueArray va = new JavaValueArray(primitiveSignature, start);
856             skipBytes(size);
857             snapshot.addHeapObject(id, va);
858             snapshot.setSiteTrace(va, stackTrace);
859         } else {
860             long sz = (long)num * identifierSize;
861             bytesRead += sz;
862             JavaObjectArray arr = new JavaObjectArray(elementClassID, start);
863             skipBytes(sz);
864             snapshot.addHeapObject(id, arr);
865             snapshot.setSiteTrace(arr, stackTrace);
866         }
867         return bytesRead;
868     }
869 
870     private byte signatureFromTypeId(byte typeId) throws IOException {
871         switch (typeId) {
872             case T_CLASS: {
873                 return (byte) 'L';
874             }
875             case T_BOOLEAN: {

885                 return (byte) 'D';
886             }
887             case T_BYTE: {
888                 return (byte) 'B';
889             }
890             case T_SHORT: {
891                 return (byte) 'S';
892             }
893             case T_INT: {
894                 return (byte) 'I';
895             }
896             case T_LONG: {
897                 return (byte) 'J';
898             }
899             default: {
900                 throw new IOException("Invalid type id of " + typeId);
901             }
902         }
903     }
904 




















































905     private void handleEOF(EOFException exp, Snapshot snapshot) {
906         if (debugLevel > 0) {
907             exp.printStackTrace();
908         }
909         warn("Unexpected EOF. Will miss information...");
910         // we have EOF, we have to tolerate missing references
911         snapshot.setUnresolvedObjectsOK(true);
912     }
913 
914     private void warn(String msg) {
915         System.out.println("WARNING: " + msg);
916     }
917 
918 }

 79     // Record types:
 80     //
 81     static final int HPROF_UTF8          = 0x01;
 82     static final int HPROF_LOAD_CLASS    = 0x02;
 83     static final int HPROF_UNLOAD_CLASS  = 0x03;
 84     static final int HPROF_FRAME         = 0x04;
 85     static final int HPROF_TRACE         = 0x05;
 86     static final int HPROF_ALLOC_SITES   = 0x06;
 87     static final int HPROF_HEAP_SUMMARY  = 0x07;
 88 
 89     static final int HPROF_START_THREAD  = 0x0a;
 90     static final int HPROF_END_THREAD    = 0x0b;
 91 
 92     static final int HPROF_HEAP_DUMP     = 0x0c;
 93 
 94     static final int HPROF_CPU_SAMPLES   = 0x0d;
 95     static final int HPROF_CONTROL_SETTINGS = 0x0e;
 96     static final int HPROF_LOCKSTATS_WAIT_TIME = 0x10;
 97     static final int HPROF_LOCKSTATS_HOLD_TIME = 0x11;
 98 
 99     static final int HPROF_FLAT_ARRAYS          = 0x12;
100     static final int HPROF_INLINED_FIELDS       = 0x13;
101 
102     static final int HPROF_FLAT_ARRAY           = 0x01;
103     static final int HPROF_CLASS_WITH_INLINED_FIELDS = 0x01;
104 
105     static final int HPROF_GC_ROOT_UNKNOWN       = 0xff;
106     static final int HPROF_GC_ROOT_JNI_GLOBAL    = 0x01;
107     static final int HPROF_GC_ROOT_JNI_LOCAL     = 0x02;
108     static final int HPROF_GC_ROOT_JAVA_FRAME    = 0x03;
109     static final int HPROF_GC_ROOT_NATIVE_STACK  = 0x04;
110     static final int HPROF_GC_ROOT_STICKY_CLASS  = 0x05;
111     static final int HPROF_GC_ROOT_THREAD_BLOCK  = 0x06;
112     static final int HPROF_GC_ROOT_MONITOR_USED  = 0x07;
113     static final int HPROF_GC_ROOT_THREAD_OBJ    = 0x08;
114 
115     static final int HPROF_GC_CLASS_DUMP         = 0x20;
116     static final int HPROF_GC_INSTANCE_DUMP      = 0x21;
117     static final int HPROF_GC_OBJ_ARRAY_DUMP         = 0x22;
118     static final int HPROF_GC_PRIM_ARRAY_DUMP         = 0x23;
119 
120     static final int HPROF_HEAP_DUMP_SEGMENT     = 0x1c;
121     static final int HPROF_HEAP_DUMP_END         = 0x2c;
122 
123     private final static int T_CLASS = 2;
124 

231                     byte[] chars = new byte[(int)length - identifierSize];
232                     in.readFully(chars);
233                     names.put(id, new String(chars));
234                     break;
235                 }
236                 case HPROF_LOAD_CLASS: {
237                     int serialNo = in.readInt();        // Not used
238                     long classID = readID();
239                     int stackTraceSerialNo = in.readInt();
240                     long classNameID = readID();
241                     Long classIdI = classID;
242                     String nm = getNameFromID(classNameID).replace('/', '.');
243                     classNameFromObjectID.put(classIdI, nm);
244                     if (classNameFromSerialNo != null) {
245                         classNameFromSerialNo.put(serialNo, nm);
246                     }
247                     break;
248                 }
249 
250                 case HPROF_HEAP_DUMP: {
251                     if (dumpsToSkip == 0) {
252                         try {
253                             readHeapDump(length, currPos);
254                         } catch (EOFException exp) {
255                             handleEOF(exp, snapshot);
256                         }
257                         if (debugLevel > 0) {
258                             System.out.println("    Finished processing instances in heap dump.");
259                         }

260                     } else {
261                         dumpsToSkip--;
262                         skipBytes(length);
263                     }
264                     break;
265                 }
266 
267                 case HPROF_HEAP_DUMP_END: {
268                     if (version >= VERSION_JDK6) {
269                         if (dumpsToSkip == 0) {
270                             // update dumpsToSkip to skip other dumps
271                             dumpsToSkip--;
272                         } else {
273                             // skip this dump (of the end record for a sequence of dump segments)
274                             dumpsToSkip--;
275                         }
276                     } else {
277                         // HPROF_HEAP_DUMP_END only recognized in >= 1.0.2
278                         warn("Ignoring unrecognized record type " + type);
279                     }
280                     skipBytes(length);  // should be no-op
281                     break;
282                 }
283 
284                 case HPROF_HEAP_DUMP_SEGMENT: {
285                     if (version >= VERSION_JDK6) {
286                         if (dumpsToSkip == 0) {
287                             try {
288                                 // read the dump segment
289                                 readHeapDump(length, currPos);
290                             } catch (EOFException exp) {
291                                 handleEOF(exp, snapshot);
292                             }
293                         } else {
294                             // all segments comprising the heap dump will be skipped
295                             skipBytes(length);
296                         }
297                     } else {
298                         // HPROF_HEAP_DUMP_SEGMENT only recognized in >= 1.0.2
299                         warn("Ignoring unrecognized record type " + type);
300                         skipBytes(length);
301                     }
302                     break;
303                 }
304 
305                 case HPROF_FRAME: {
306                     if (stackFrames == null) {

340                         }
341                         stackTraces.put(serialNo,
342                                         new StackTrace(frames));
343                     }
344                     break;
345                 }
346                 case HPROF_UNLOAD_CLASS:
347                 case HPROF_ALLOC_SITES:
348                 case HPROF_START_THREAD:
349                 case HPROF_END_THREAD:
350                 case HPROF_HEAP_SUMMARY:
351                 case HPROF_CPU_SAMPLES:
352                 case HPROF_CONTROL_SETTINGS:
353                 case HPROF_LOCKSTATS_WAIT_TIME:
354                 case HPROF_LOCKSTATS_HOLD_TIME:
355                 {
356                     // Ignore these record types
357                     skipBytes(length);
358                     break;
359                 }
360                 case HPROF_FLAT_ARRAYS: {
361                     readFlatArrays(length);
362                     break;
363                 }
364                 case HPROF_INLINED_FIELDS: {
365                     readInlinedFields(length);
366                     break;
367                 }
368 
369                 default: {
370                     skipBytes(length);
371                     warn("Ignoring unrecognized record type " + type);
372                 }
373             }
374         }
375 
376         return snapshot;
377     }
378 
379     public String printStackTraces() {
380         StringBuffer output = new StringBuffer();
381         for (Map.Entry<Integer, StackTrace> entry : stackTraces.entrySet()) {
382             StackFrame[] frames = entry.getValue().getFrames();
383             output.append("SerialNo " + entry.getKey() + "\n");
384             for (int i = 0; i < frames.length; i++) {
385                 output.append("  " + frames[i].getClassName() + "." + frames[i].getMethodName()
386                         + frames[i].getMethodSignature() + " (" + frames[i].getSourceFileName()
387                         + ":" + frames[i].getLineNumber() + ")" + "\n");
388             }

849                 }
850                 case T_INT: {
851                     primitiveSignature = (byte) 'I';
852                     elSize = 4;
853                     break;
854                 }
855                 case T_LONG: {
856                     primitiveSignature = (byte) 'J';
857                     elSize = 8;
858                     break;
859                 }
860             }
861             if (version >= VERSION_JDK12BETA4 && primitiveSignature == 0x00) {
862                 throw new IOException("Unrecognized typecode:  "
863                                         + elementClassID);
864             }
865         }
866         if (primitiveSignature != 0x00) {
867             long size = elSize * (long)num;
868             bytesRead += size;
869             JavaValueArray va = new JavaValueArray(id, primitiveSignature, start);
870             skipBytes(size);
871             snapshot.addHeapObject(id, va);
872             snapshot.setSiteTrace(va, stackTrace);
873         } else {
874             long sz = (long)num * identifierSize;
875             bytesRead += sz;
876             JavaObjectArray arr = new JavaObjectArray(elementClassID, start);
877             skipBytes(sz);
878             snapshot.addHeapObject(id, arr);
879             snapshot.setSiteTrace(arr, stackTrace);
880         }
881         return bytesRead;
882     }
883 
884     private byte signatureFromTypeId(byte typeId) throws IOException {
885         switch (typeId) {
886             case T_CLASS: {
887                 return (byte) 'L';
888             }
889             case T_BOOLEAN: {

899                 return (byte) 'D';
900             }
901             case T_BYTE: {
902                 return (byte) 'B';
903             }
904             case T_SHORT: {
905                 return (byte) 'S';
906             }
907             case T_INT: {
908                 return (byte) 'I';
909             }
910             case T_LONG: {
911                 return (byte) 'J';
912             }
913             default: {
914                 throw new IOException("Invalid type id of " + typeId);
915             }
916         }
917     }
918 
919     private void readFlatArrays(long length) throws IOException {
920         while (length > 0) {
921             byte tag = in.readByte();
922             length--;
923             switch (tag) {
924                 case HPROF_FLAT_ARRAY: {
925                     long objId = readID();
926                     length -= identifierSize;
927                     long elementClassId = readID();
928                     length -= identifierSize;
929                     snapshot.addFlatArray(objId, elementClassId);
930                     break;
931                 }
932                 default: {
933                     throw new IOException("Invalid tag " + tag);
934                 }
935             }
936         }
937     }
938 
939     private void readInlinedFields(long length) throws IOException {
940         while (length > 0) {
941             byte tag = in.readByte();
942             length--;
943             switch (tag) {
944                 case HPROF_CLASS_WITH_INLINED_FIELDS: {
945                     long classID = readID();
946                     length -= identifierSize;
947                     int fieldNum = in.readUnsignedShort();
948                     length -= 2;
949                     Snapshot.ClassInlinedFields[] fields = new Snapshot.ClassInlinedFields[fieldNum];
950                     for (int i = 0; i < fieldNum; i++) {
951                         int fieldIndex = in.readUnsignedShort();
952                         length -= 2;
953                         int synthFieldCount = in.readUnsignedShort();
954                         length -= 2;
955                         String fieldName = getNameFromID(readID());
956                         length -= identifierSize;
957                         long fieldClassId = readID();
958                         length -= identifierSize;
959                         fields[i] = new Snapshot.ClassInlinedFields(fieldIndex, synthFieldCount, fieldName, fieldClassId);
960                     }
961                     snapshot.addClassInlinedFields(classID, fields);
962                     break;
963                 }
964                 default: {
965                     throw new IOException("Invalid tag " + tag);
966                 }
967             }
968         }
969     }
970 
971     private void handleEOF(EOFException exp, Snapshot snapshot) {
972         if (debugLevel > 0) {
973             exp.printStackTrace();
974         }
975         warn("Unexpected EOF. Will miss information...");
976         // we have EOF, we have to tolerate missing references
977         snapshot.setUnresolvedObjectsOK(true);
978     }
979 
980     private void warn(String msg) {
981         System.out.println("WARNING: " + msg);
982     }
983 
984 }
< prev index next >