23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/classLoaderDataGraph.hpp"
28 #include "classfile/javaClasses.inline.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/vmClasses.hpp"
31 #include "classfile/vmSymbols.hpp"
32 #include "gc/shared/gcLocker.hpp"
33 #include "gc/shared/gcVMOperations.hpp"
34 #include "gc/shared/workerThread.hpp"
35 #include "jfr/jfrEvents.hpp"
36 #include "jvm.h"
37 #include "memory/allocation.inline.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "memory/universe.hpp"
40 #include "oops/klass.inline.hpp"
41 #include "oops/objArrayKlass.hpp"
42 #include "oops/objArrayOop.inline.hpp"
43 #include "oops/oop.inline.hpp"
44 #include "oops/typeArrayOop.inline.hpp"
45 #include "runtime/frame.inline.hpp"
46 #include "runtime/handles.inline.hpp"
47 #include "runtime/javaCalls.hpp"
48 #include "runtime/javaThread.inline.hpp"
49 #include "runtime/jniHandles.hpp"
50 #include "runtime/os.hpp"
51 #include "runtime/reflectionUtils.hpp"
52 #include "runtime/threads.hpp"
53 #include "runtime/threadSMR.hpp"
54 #include "runtime/vframe.hpp"
55 #include "runtime/vmOperations.hpp"
56 #include "runtime/vmThread.hpp"
57 #include "services/heapDumper.hpp"
58 #include "services/heapDumperCompression.hpp"
59 #include "services/threadService.hpp"
60 #include "utilities/macros.hpp"
61 #include "utilities/ostream.hpp"
62
63 /*
64 * HPROF binary format - description copied from:
292 * 7: double array
293 * 8: byte array
294 * 9: short array
295 * 10: int array
296 * 11: long array
297 * [u1]* elements
298 *
299 * HPROF_CPU_SAMPLES a set of sample traces of running threads
300 *
301 * u4 total number of samples
302 * u4 # of traces
303 * [u4 # of samples
304 * u4]* stack trace serial number
305 *
306 * HPROF_CONTROL_SETTINGS the settings of on/off switches
307 *
308 * u4 0x00000001: alloc traces on/off
309 * 0x00000002: cpu sampling on/off
310 * u2 stack trace depth
311 *
312 *
313 * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
314 * be generated as a sequence of heap dump segments. This sequence is
315 * terminated by an end record. The additional tags allowed by format
316 * "JAVA PROFILE 1.0.2" are:
317 *
318 * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
319 *
320 * [heap dump sub-records]*
321 * The same sub-record types allowed by HPROF_HEAP_DUMP
322 *
323 * HPROF_HEAP_DUMP_END denotes the end of a heap dump
324 *
325 */
326
327
328 // HPROF tags
329
330 enum hprofTag : u1 {
331 // top-level records
332 HPROF_UTF8 = 0x01,
333 HPROF_LOAD_CLASS = 0x02,
334 HPROF_UNLOAD_CLASS = 0x03,
335 HPROF_FRAME = 0x04,
336 HPROF_TRACE = 0x05,
337 HPROF_ALLOC_SITES = 0x06,
338 HPROF_HEAP_SUMMARY = 0x07,
339 HPROF_START_THREAD = 0x0A,
340 HPROF_END_THREAD = 0x0B,
341 HPROF_HEAP_DUMP = 0x0C,
342 HPROF_CPU_SAMPLES = 0x0D,
343 HPROF_CONTROL_SETTINGS = 0x0E,
344
345 // 1.0.2 record types
346 HPROF_HEAP_DUMP_SEGMENT = 0x1C,
347 HPROF_HEAP_DUMP_END = 0x2C,
348
349 // field types
350 HPROF_ARRAY_OBJECT = 0x01,
351 HPROF_NORMAL_OBJECT = 0x02,
352 HPROF_BOOLEAN = 0x04,
353 HPROF_CHAR = 0x05,
354 HPROF_FLOAT = 0x06,
355 HPROF_DOUBLE = 0x07,
356 HPROF_BYTE = 0x08,
357 HPROF_SHORT = 0x09,
358 HPROF_INT = 0x0A,
359 HPROF_LONG = 0x0B,
360
361 // data-dump sub-records
362 HPROF_GC_ROOT_UNKNOWN = 0xFF,
363 HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
364 HPROF_GC_ROOT_JNI_LOCAL = 0x02,
365 HPROF_GC_ROOT_JAVA_FRAME = 0x03,
366 HPROF_GC_ROOT_NATIVE_STACK = 0x04,
367 HPROF_GC_ROOT_STICKY_CLASS = 0x05,
368 HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
369 HPROF_GC_ROOT_MONITOR_USED = 0x07,
370 HPROF_GC_ROOT_THREAD_OBJ = 0x08,
371 HPROF_GC_CLASS_DUMP = 0x20,
372 HPROF_GC_INSTANCE_DUMP = 0x21,
373 HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
374 HPROF_GC_PRIM_ARRAY_DUMP = 0x23
375 };
376
377 // Default stack trace ID (used for dummy HPROF_TRACE record)
378 enum {
379 STACK_TRACE_ID = 1,
380 INITIAL_CLASS_COUNT = 200
381 };
382
383 // Supports I/O operations for a dump
384 // Base class for dump and parallel dump
385 class AbstractDumpWriter : public StackObj {
386 protected:
387 enum {
388 io_buffer_max_size = 1*M,
389 io_buffer_max_waste = 10*K,
390 dump_segment_header_size = 9
391 };
392
393 char* _buffer; // internal buffer
394 size_t _size;
395 size_t _pos;
396
397 bool _in_dump_segment; // Are we currently in a dump segment?
398 bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
399 DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
400 DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
401
402 virtual void flush(bool force = false) = 0;
881 }
882 };
883
884 Monitor* ParDumpWriter::_lock = nullptr;
885
886 // Support class with a collection of functions used when dumping the heap
887
888 class DumperSupport : AllStatic {
889 public:
890
891 // write a header of the given type
892 static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
893
894 // returns hprof tag for the given type signature
895 static hprofTag sig2tag(Symbol* sig);
896 // returns hprof tag for the given basic type
897 static hprofTag type2tag(BasicType type);
898 // Returns the size of the data to write.
899 static u4 sig2size(Symbol* sig);
900
901 // returns the size of the instance of the given class
902 static u4 instance_size(Klass* k);
903
904 // dump a jfloat
905 static void dump_float(AbstractDumpWriter* writer, jfloat f);
906 // dump a jdouble
907 static void dump_double(AbstractDumpWriter* writer, jdouble d);
908 // dumps the raw value of the given field
909 static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);
910 // returns the size of the static fields; also counts the static fields
911 static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
912 // dumps static fields of the given class
913 static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
914 // dump the raw values of the instance fields of the given object
915 static void dump_instance_fields(AbstractDumpWriter* writer, oop o);
916 // get the count of the instance fields for a given class
917 static u2 get_instance_fields_count(InstanceKlass* ik);
918 // dumps the definition of the instance fields for a given class
919 static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);
920 // creates HPROF_GC_INSTANCE_DUMP record for the given object
921 static void dump_instance(AbstractDumpWriter* writer, oop o);
922 // creates HPROF_GC_CLASS_DUMP record for the given instance class
923 static void dump_instance_class(AbstractDumpWriter* writer, Klass* k);
924 // creates HPROF_GC_CLASS_DUMP record for a given array class
925 static void dump_array_class(AbstractDumpWriter* writer, Klass* k);
926
927 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
928 static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
929 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
930 static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
931 // create HPROF_FRAME record for the given method and bci
932 static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
933
934 // check if we need to truncate an array
935 static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
936
937 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
938 static void end_of_dump(AbstractDumpWriter* writer);
939
940 static oop mask_dormant_archived_object(oop o) {
941 if (o != nullptr && o->klass()->java_mirror() == nullptr) {
942 // Ignore this object since the corresponding java mirror is not loaded.
943 // Might be a dormant archive object.
944 return nullptr;
945 } else {
946 return o;
947 }
948 }
949 };
950
951 // write a header of the given type
952 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
953 writer->write_u1(tag);
954 writer->write_u4(0); // current ticks
955 writer->write_u4(len);
956 }
957
958 // returns hprof tag for the given type signature
959 hprofTag DumperSupport::sig2tag(Symbol* sig) {
960 switch (sig->char_at(0)) {
961 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
962 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
963 case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
964 case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
965 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
966 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
967 case JVM_SIGNATURE_INT : return HPROF_INT;
968 case JVM_SIGNATURE_LONG : return HPROF_LONG;
969 case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
970 case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
971 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
972 }
973 }
974
975 hprofTag DumperSupport::type2tag(BasicType type) {
976 switch (type) {
977 case T_BYTE : return HPROF_BYTE;
978 case T_CHAR : return HPROF_CHAR;
979 case T_FLOAT : return HPROF_FLOAT;
980 case T_DOUBLE : return HPROF_DOUBLE;
981 case T_INT : return HPROF_INT;
982 case T_LONG : return HPROF_LONG;
983 case T_SHORT : return HPROF_SHORT;
984 case T_BOOLEAN : return HPROF_BOOLEAN;
985 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
986 }
987 }
988
989 u4 DumperSupport::sig2size(Symbol* sig) {
990 switch (sig->char_at(0)) {
991 case JVM_SIGNATURE_CLASS:
992 case JVM_SIGNATURE_ARRAY: return sizeof(address);
993 case JVM_SIGNATURE_BOOLEAN:
994 case JVM_SIGNATURE_BYTE: return 1;
995 case JVM_SIGNATURE_SHORT:
996 case JVM_SIGNATURE_CHAR: return 2;
997 case JVM_SIGNATURE_INT:
998 case JVM_SIGNATURE_FLOAT: return 4;
999 case JVM_SIGNATURE_LONG:
1000 case JVM_SIGNATURE_DOUBLE: return 8;
1001 default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
1002 }
1003 }
1004
1005 template<typename T, typename F> T bit_cast(F from) { // replace with the real thing when we can use c++20
1006 T to;
1007 static_assert(sizeof(to) == sizeof(from), "must be of the same size");
1008 memcpy(&to, &from, sizeof(to));
1009 return to;
1010 }
1011
1012 // dump a jfloat
1013 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
1014 if (g_isnan(f)) {
1015 writer->write_u4(0x7fc00000); // collapsing NaNs
1016 } else {
1017 writer->write_u4(bit_cast<u4>(f));
1018 }
1019 }
1020
1021 // dump a jdouble
1022 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
1023 if (g_isnan(d)) {
1024 writer->write_u8(0x7ff80000ull << 32); // collapsing NaNs
1025 } else {
1026 writer->write_u8(bit_cast<u8>(d));
1027 }
1028 }
1029
1030 // dumps the raw value of the given field
1031 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {
1032 switch (type) {
1033 case JVM_SIGNATURE_CLASS :
1034 case JVM_SIGNATURE_ARRAY : {
1035 oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
1036 if (o != nullptr && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == nullptr) {
1037 ResourceMark rm;
1038 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1039 p2i(o), o->klass()->external_name(),
1040 p2i(obj), obj->klass()->external_name());
1041 }
1042 o = mask_dormant_archived_object(o);
1043 assert(oopDesc::is_oop_or_null(o), "Expected an oop or nullptr at " PTR_FORMAT, p2i(o));
1044 writer->write_objectID(o);
1045 break;
1046 }
1047 case JVM_SIGNATURE_BYTE : {
1048 jbyte b = obj->byte_field(offset);
1049 writer->write_u1(b);
1050 break;
1051 }
1052 case JVM_SIGNATURE_CHAR : {
1053 jchar c = obj->char_field(offset);
1074 writer->write_u4(i);
1075 break;
1076 }
1077 case JVM_SIGNATURE_LONG : {
1078 jlong l = obj->long_field(offset);
1079 writer->write_u8(l);
1080 break;
1081 }
1082 case JVM_SIGNATURE_BOOLEAN : {
1083 jboolean b = obj->bool_field(offset);
1084 writer->write_u1(b);
1085 break;
1086 }
1087 default : {
1088 ShouldNotReachHere();
1089 break;
1090 }
1091 }
1092 }
1093
1094 // returns the size of the instance of the given class
1095 u4 DumperSupport::instance_size(Klass* k) {
1096 InstanceKlass* ik = InstanceKlass::cast(k);
1097 u4 size = 0;
1098
1099 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1100 if (!fld.access_flags().is_static()) {
1101 size += sig2size(fld.signature());
1102 }
1103 }
1104 return size;
1105 }
1106
1107 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1108 field_count = 0;
1109 u4 size = 0;
1110
1111 for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1112 if (fldc.access_flags().is_static()) {
1113 field_count++;
1114 size += sig2size(fldc.signature());
1115 }
1116 }
1117
1118 // Add in resolved_references which is referenced by the cpCache
1119 // The resolved_references is an array per InstanceKlass holding the
1120 // strings and other oops resolved from the constant pool.
1121 oop resolved_references = ik->constants()->resolved_references_or_null();
1122 if (resolved_references != nullptr) {
1123 field_count++;
1124 size += sizeof(address);
1125
1126 // Add in the resolved_references of the used previous versions of the class
1127 // in the case of RedefineClasses
1128 InstanceKlass* prev = ik->previous_versions();
1129 while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1130 field_count++;
1131 size += sizeof(address);
1132 prev = prev->previous_versions();
1133 }
1134 }
1135
1136 // We write the value itself plus a name and a one byte type tag per field.
1137 return size + field_count * (sizeof(address) + 1);
1138 }
1139
1140 // dumps static fields of the given class
1141 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1142 InstanceKlass* ik = InstanceKlass::cast(k);
1143
1144 // dump the field descriptors and raw values
1145 for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1146 if (fld.access_flags().is_static()) {
1147 Symbol* sig = fld.signature();
1148
1149 writer->write_symbolID(fld.name()); // name
1150 writer->write_u1(sig2tag(sig)); // type
1151
1152 // value
1153 dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1154 }
1155 }
1156
1157 // Add resolved_references for each class that has them
1158 oop resolved_references = ik->constants()->resolved_references_or_null();
1159 if (resolved_references != nullptr) {
1160 writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1161 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1162 writer->write_objectID(resolved_references);
1163
1164 // Also write any previous versions
1165 InstanceKlass* prev = ik->previous_versions();
1166 while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1167 writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1168 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1169 writer->write_objectID(prev->constants()->resolved_references());
1170 prev = prev->previous_versions();
1171 }
1172 }
1173 }
1174
1175 // dump the raw values of the instance fields of the given object
1176 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o) {
1177 InstanceKlass* ik = InstanceKlass::cast(o->klass());
1178
1179 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1180 if (!fld.access_flags().is_static()) {
1181 Symbol* sig = fld.signature();
1182 dump_field_value(writer, sig->char_at(0), o, fld.offset());
1183 }
1184 }
1185 }
1186
1187 // dumps the definition of the instance fields for a given class
1188 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1189 u2 field_count = 0;
1190
1191 for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1192 if (!fldc.access_flags().is_static()) field_count++;
1193 }
1194
1195 return field_count;
1196 }
1197
1198 // dumps the definition of the instance fields for a given class
1199 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1200 InstanceKlass* ik = InstanceKlass::cast(k);
1201
1202 // dump the field descriptors
1203 for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1204 if (!fld.access_flags().is_static()) {
1205 Symbol* sig = fld.signature();
1206
1207 writer->write_symbolID(fld.name()); // name
1208 writer->write_u1(sig2tag(sig)); // type
1209 }
1210 }
1211 }
1212
1213 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1214 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o) {
1215 InstanceKlass* ik = InstanceKlass::cast(o->klass());
1216 u4 is = instance_size(ik);
1217 u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1218
1219 writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1220 writer->write_objectID(o);
1221 writer->write_u4(STACK_TRACE_ID);
1222
1223 // class ID
1224 writer->write_classID(ik);
1225
1226 // number of bytes that follow
1227 writer->write_u4(is);
1228
1229 // field values
1230 dump_instance_fields(writer, o);
1231
1232 writer->end_sub_record();
1233 }
1234
1235 // creates HPROF_GC_CLASS_DUMP record for the given instance class
1236 void DumperSupport::dump_instance_class(AbstractDumpWriter* writer, Klass* k) {
1237 InstanceKlass* ik = InstanceKlass::cast(k);
1238
1239 // We can safepoint and do a heap dump at a point where we have a Klass,
1240 // but no java mirror class has been setup for it. So we need to check
1241 // that the class is at least loaded, to avoid crash from a null mirror.
1242 if (!ik->is_loaded()) {
1243 return;
1244 }
1245
1246 u2 static_fields_count = 0;
1247 u4 static_size = get_static_fields_size(ik, static_fields_count);
1248 u2 instance_fields_count = get_instance_fields_count(ik);
1249 u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1250 u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size;
1255 writer->write_classID(ik);
1256 writer->write_u4(STACK_TRACE_ID);
1257
1258 // super class ID
1259 InstanceKlass* java_super = ik->java_super();
1260 if (java_super == nullptr) {
1261 writer->write_objectID(oop(nullptr));
1262 } else {
1263 writer->write_classID(java_super);
1264 }
1265
1266 writer->write_objectID(ik->class_loader());
1267 writer->write_objectID(ik->signers());
1268 writer->write_objectID(ik->protection_domain());
1269
1270 // reserved
1271 writer->write_objectID(oop(nullptr));
1272 writer->write_objectID(oop(nullptr));
1273
1274 // instance size
1275 writer->write_u4(DumperSupport::instance_size(ik));
1276
1277 // size of constant pool - ignored by HAT 1.1
1278 writer->write_u2(0);
1279
1280 // static fields
1281 writer->write_u2(static_fields_count);
1282 dump_static_fields(writer, ik);
1283
1284 // description of instance fields
1285 writer->write_u2(instance_fields_count);
1286 dump_instance_field_descriptors(writer, ik);
1287
1288 writer->end_sub_record();
1289 }
1290
1291 // creates HPROF_GC_CLASS_DUMP record for the given array class
1292 void DumperSupport::dump_array_class(AbstractDumpWriter* writer, Klass* k) {
1293 InstanceKlass* ik = nullptr; // bottom class for object arrays, null for primitive type arrays
1294 if (k->is_objArray_klass()) {
1295 Klass *bk = ObjArrayKlass::cast(k)->bottom_klass();
1309 assert(java_super != nullptr, "checking");
1310 writer->write_classID(java_super);
1311
1312 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->class_loader());
1313 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->signers());
1314 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->protection_domain());
1315
1316 writer->write_objectID(oop(nullptr)); // reserved
1317 writer->write_objectID(oop(nullptr));
1318 writer->write_u4(0); // instance size
1319 writer->write_u2(0); // constant pool
1320 writer->write_u2(0); // static fields
1321 writer->write_u2(0); // instance fields
1322
1323 writer->end_sub_record();
1324
1325 }
1326
1327 // Hprof uses an u4 as record length field,
1328 // which means we need to truncate arrays that are too long.
1329 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1330 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1331 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1332
1333 int length = array->length();
1334
1335 int type_size;
1336 if (type == T_OBJECT) {
1337 type_size = sizeof(address);
1338 } else {
1339 type_size = type2aelembytes(type);
1340 }
1341
1342 size_t length_in_bytes = (size_t)length * type_size;
1343 uint max_bytes = max_juint - header_size;
1344
1345 if (length_in_bytes > max_bytes) {
1346 length = max_bytes / type_size;
1347 length_in_bytes = (size_t)length * type_size;
1348
1349 warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1350 type2name_tab[type], array->length(), length);
1351 }
1352 return length;
1353 }
1354
1355 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1356 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1357 // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1358 short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1359 int length = calculate_array_max_length(writer, array, header_size);
1360 u4 size = header_size + length * sizeof(address);
1361
1362 writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1363 writer->write_objectID(array);
1364 writer->write_u4(STACK_TRACE_ID);
1365 writer->write_u4(length);
1366
1367 // array class ID
1368 writer->write_classID(array->klass());
1369
1370 // [id]* elements
1371 for (int index = 0; index < length; index++) {
1372 oop o = array->obj_at(index);
1373 if (o != nullptr && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == nullptr) {
1374 ResourceMark rm;
1375 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1376 p2i(o), o->klass()->external_name(),
1377 p2i(array), array->klass()->external_name());
1378 }
1379 o = mask_dormant_archived_object(o);
1380 writer->write_objectID(o);
1381 }
1382
1383 writer->end_sub_record();
1384 }
1385
1386 #define WRITE_ARRAY(Array, Type, Size, Length) \
1387 for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1388
1389 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1390 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1391 BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1392 // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1393 short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1394
1395 int length = calculate_array_max_length(writer, array, header_size);
1396 int type_size = type2aelembytes(type);
1397 u4 length_in_bytes = (u4)length * type_size;
1398 u4 size = header_size + length_in_bytes;
1399
1400 writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1401 writer->write_objectID(array);
1402 writer->write_u4(STACK_TRACE_ID);
1403 writer->write_u4(length);
1404 writer->write_u1(type2tag(type));
1405
1487 int bci) {
1488 int line_number;
1489 if (m->is_native()) {
1490 line_number = -3; // native frame
1491 } else {
1492 line_number = m->line_number_from_bci(bci);
1493 }
1494
1495 write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1496 writer->write_id(frame_serial_num); // frame serial number
1497 writer->write_symbolID(m->name()); // method's name
1498 writer->write_symbolID(m->signature()); // method's signature
1499
1500 assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1501 writer->write_symbolID(m->method_holder()->source_file_name()); // source file name
1502 writer->write_u4(class_serial_num); // class serial number
1503 writer->write_u4((u4) line_number); // line number
1504 }
1505
1506
1507 // Support class used to generate HPROF_UTF8 records from the entries in the
1508 // SymbolTable.
1509
1510 class SymbolTableDumper : public SymbolClosure {
1511 private:
1512 AbstractDumpWriter* _writer;
1513 AbstractDumpWriter* writer() const { return _writer; }
1514 public:
1515 SymbolTableDumper(AbstractDumpWriter* writer) { _writer = writer; }
1516 void do_symbol(Symbol** p);
1517 };
1518
1519 void SymbolTableDumper::do_symbol(Symbol** p) {
1520 ResourceMark rm;
1521 Symbol* sym = *p;
1522 int len = sym->utf8_length();
1523 if (len > 0) {
1524 char* s = sym->as_utf8();
1525 DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1526 writer()->write_symbolID(sym);
1709 }
1710
1711 if (DumperSupport::mask_dormant_archived_object(o) == nullptr) {
1712 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name());
1713 return;
1714 }
1715
1716 // If large object list exists and it is large object/array,
1717 // add oop into the list and skip scan. VM thread will process it later.
1718 if (_list != nullptr && is_large(o)) {
1719 _list->atomic_push(o);
1720 return;
1721 }
1722
1723 if (o->is_instance()) {
1724 // create a HPROF_GC_INSTANCE record for each object
1725 DumperSupport::dump_instance(writer(), o);
1726 } else if (o->is_objArray()) {
1727 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1728 DumperSupport::dump_object_array(writer(), objArrayOop(o));
1729 } else if (o->is_typeArray()) {
1730 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1731 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1732 }
1733 }
1734
1735 bool HeapObjectDumper::is_large(oop o) {
1736 size_t size = 0;
1737 if (o->is_instance()) {
1738 // Use o->size() * 8 as the upper limit of instance size to avoid iterating static fields
1739 size = o->size() * 8;
1740 } else if (o->is_objArray()) {
1741 objArrayOop array = objArrayOop(o);
1742 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1743 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1744 int length = array->length();
1745 int type_size = sizeof(address);
1746 size = (size_t)length * type_size;
1747 } else if (o->is_typeArray()) {
1748 typeArrayOop array = typeArrayOop(o);
1749 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1750 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1751 int length = array->length();
1752 int type_size = type2aelembytes(type);
1753 size = (size_t)length * type_size;
1754 }
1755 return size > HeapDumpLargeObjectList::LargeObjectSizeThreshold;
1756 }
1757
1758 // The dumper controller for parallel heap dump
1759 class DumperController : public CHeapObj<mtInternal> {
1760 private:
1761 bool _started;
1762 Monitor* _lock;
1763 uint _dumper_number;
1764 uint _complete_number;
1765
1766 public:
1799 MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1800 while (_complete_number != _dumper_number) {
1801 ml.wait();
1802 }
1803 _started = false;
1804 }
1805 };
1806
1807 // The VM operation that performs the heap dump
1808 class VM_HeapDumper : public VM_GC_Operation, public WorkerTask {
1809 private:
1810 static VM_HeapDumper* _global_dumper;
1811 static DumpWriter* _global_writer;
1812 DumpWriter* _local_writer;
1813 JavaThread* _oome_thread;
1814 Method* _oome_constructor;
1815 bool _gc_before_heap_dump;
1816 GrowableArray<Klass*>* _klass_map;
1817 ThreadStackTrace** _stack_traces;
1818 int _num_threads;
1819 // parallel heap dump support
1820 uint _num_dumper_threads;
1821 uint _num_writer_threads;
1822 DumperController* _dumper_controller;
1823 ParallelObjectIterator* _poi;
1824 HeapDumpLargeObjectList* _large_object_list;
1825
1826 // VMDumperType is for thread that dumps both heap and non-heap data.
1827 static const size_t VMDumperType = 0;
1828 static const size_t WriterType = 1;
1829 static const size_t DumperType = 2;
1830 // worker id of VMDumper thread.
1831 static const size_t VMDumperWorkerId = 0;
1832
1833 size_t get_worker_type(uint worker_id) {
1834 assert(_num_writer_threads >= 1, "Must be at least one writer");
1835 // worker id of VMDumper that dump heap and non-heap data
1836 if (worker_id == VMDumperWorkerId) {
1837 return VMDumperType;
1838 }
2223 writer()->writer_loop();
2224 return;
2225 }
2226 if (_num_dumper_threads > 1 && get_worker_type(worker_id) == DumperType) {
2227 _dumper_controller->wait_for_start_signal();
2228 }
2229 } else {
2230 // The worker 0 on all non-heap data dumping and part of heap iteration.
2231 // Write the file header - we always use 1.0.2
2232 const char* header = "JAVA PROFILE 1.0.2";
2233
2234 // header is few bytes long - no chance to overflow int
2235 writer()->write_raw(header, strlen(header) + 1); // NUL terminated
2236 writer()->write_u4(oopSize);
2237 // timestamp is current time in ms
2238 writer()->write_u8(os::javaTimeMillis());
2239 // HPROF_UTF8 records
2240 SymbolTableDumper sym_dumper(writer());
2241 SymbolTable::symbols_do(&sym_dumper);
2242
2243 // write HPROF_LOAD_CLASS records
2244 {
2245 LockedClassesDo locked_load_classes(&do_load_class);
2246 ClassLoaderDataGraph::classes_do(&locked_load_classes);
2247 }
2248
2249 // write HPROF_FRAME and HPROF_TRACE records
2250 // this must be called after _klass_map is built when iterating the classes above.
2251 dump_stack_traces();
2252
2253 // Writes HPROF_GC_CLASS_DUMP records
2254 {
2255 LockedClassesDo locked_dump_class(&do_class_dump);
2256 ClassLoaderDataGraph::classes_do(&locked_dump_class);
2257 }
2258
2259 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
2260 do_threads();
2261
2262 // HPROF_GC_ROOT_JNI_GLOBAL
2301 if (get_worker_type(worker_id) == VMDumperType) {
2302 _dumper_controller->wait_all_dumpers_complete();
2303 // clear internal buffer;
2304 pw.finish_dump_segment(true);
2305 // refresh the global_writer's buffer and position;
2306 writer()->refresh();
2307 } else {
2308 pw.finish_dump_segment(true);
2309 _dumper_controller->dumper_complete();
2310 return;
2311 }
2312 }
2313 }
2314
2315 assert(get_worker_type(worker_id) == VMDumperType, "Heap dumper must be VMDumper");
2316 // Use writer() rather than ParDumpWriter to avoid memory consumption.
2317 HeapObjectDumper obj_dumper(writer());
2318 dump_large_objects(&obj_dumper);
2319 // Writes the HPROF_HEAP_DUMP_END record.
2320 DumperSupport::end_of_dump(writer());
2321 // We are done with writing. Release the worker threads.
2322 writer()->deactivate();
2323 }
2324
2325 void VM_HeapDumper::dump_stack_traces() {
2326 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
2327 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
2328 writer()->write_u4((u4) STACK_TRACE_ID);
2329 writer()->write_u4(0); // thread number
2330 writer()->write_u4(0); // frame count
2331
2332 _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
2333 int frame_serial_num = 0;
2334 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
2335 oop threadObj = thread->threadObj();
2336 if (threadObj != nullptr && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
2337 // dump thread stack trace
2338 Thread* current_thread = Thread::current();
2339 ResourceMark rm(current_thread);
2340 HandleMark hm(current_thread);
2341
2342 ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
|
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/classLoaderDataGraph.hpp"
28 #include "classfile/javaClasses.inline.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/vmClasses.hpp"
31 #include "classfile/vmSymbols.hpp"
32 #include "gc/shared/gcLocker.hpp"
33 #include "gc/shared/gcVMOperations.hpp"
34 #include "gc/shared/workerThread.hpp"
35 #include "jfr/jfrEvents.hpp"
36 #include "jvm.h"
37 #include "memory/allocation.inline.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "memory/universe.hpp"
40 #include "oops/klass.inline.hpp"
41 #include "oops/objArrayKlass.hpp"
42 #include "oops/objArrayOop.inline.hpp"
43 #include "oops/flatArrayKlass.hpp"
44 #include "oops/flatArrayOop.inline.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "oops/typeArrayOop.inline.hpp"
47 #include "runtime/fieldDescriptor.inline.hpp"
48 #include "runtime/frame.inline.hpp"
49 #include "runtime/handles.inline.hpp"
50 #include "runtime/javaCalls.hpp"
51 #include "runtime/javaThread.inline.hpp"
52 #include "runtime/jniHandles.hpp"
53 #include "runtime/os.hpp"
54 #include "runtime/reflectionUtils.hpp"
55 #include "runtime/threads.hpp"
56 #include "runtime/threadSMR.hpp"
57 #include "runtime/vframe.hpp"
58 #include "runtime/vmOperations.hpp"
59 #include "runtime/vmThread.hpp"
60 #include "services/heapDumper.hpp"
61 #include "services/heapDumperCompression.hpp"
62 #include "services/threadService.hpp"
63 #include "utilities/macros.hpp"
64 #include "utilities/ostream.hpp"
65
66 /*
67 * HPROF binary format - description copied from:
295 * 7: double array
296 * 8: byte array
297 * 9: short array
298 * 10: int array
299 * 11: long array
300 * [u1]* elements
301 *
302 * HPROF_CPU_SAMPLES a set of sample traces of running threads
303 *
304 * u4 total number of samples
305 * u4 # of traces
306 * [u4 # of samples
307 * u4]* stack trace serial number
308 *
309 * HPROF_CONTROL_SETTINGS the settings of on/off switches
310 *
311 * u4 0x00000001: alloc traces on/off
312 * 0x00000002: cpu sampling on/off
313 * u2 stack trace depth
314 *
315 * HPROF_FLAT_ARRAYS list of flat arrays
316 *
317 * [flat array sub-records]*
318 *
319 * HPROF_FLAT_ARRAY flat array
320 *
321 * id array object ID (dumped as HPROF_GC_PRIM_ARRAY_DUMP)
322 * id element class ID (dumped by HPROF_GC_CLASS_DUMP)
323 *
324 * HPROF_INLINED_FIELDS decribes inlined fields
325 *
326 * [class with inlined fields sub-records]*
327 *
328 * HPROF_CLASS_WITH_INLINED_FIELDS
329 *
330 * id class ID (dumped as HPROF_GC_CLASS_DUMP)
331 *
332 * u2 number of instance inlined fields (not including super)
333 * [u2, inlined field index,
334 * u2, synthetic field count,
335 * id, original field name,
336 * id]* inlined field class ID (dumped by HPROF_GC_CLASS_DUMP)
337 *
338 * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
339 * be generated as a sequence of heap dump segments. This sequence is
340 * terminated by an end record. The additional tags allowed by format
341 * "JAVA PROFILE 1.0.2" are:
342 *
343 * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
344 *
345 * [heap dump sub-records]*
346 * The same sub-record types allowed by HPROF_HEAP_DUMP
347 *
348 * HPROF_HEAP_DUMP_END denotes the end of a heap dump
349 *
350 */
351
352
353 // HPROF tags
354
355 enum hprofTag : u1 {
356 // top-level records
357 HPROF_UTF8 = 0x01,
358 HPROF_LOAD_CLASS = 0x02,
359 HPROF_UNLOAD_CLASS = 0x03,
360 HPROF_FRAME = 0x04,
361 HPROF_TRACE = 0x05,
362 HPROF_ALLOC_SITES = 0x06,
363 HPROF_HEAP_SUMMARY = 0x07,
364 HPROF_START_THREAD = 0x0A,
365 HPROF_END_THREAD = 0x0B,
366 HPROF_HEAP_DUMP = 0x0C,
367 HPROF_CPU_SAMPLES = 0x0D,
368 HPROF_CONTROL_SETTINGS = 0x0E,
369
370 // 1.0.2 record types
371 HPROF_HEAP_DUMP_SEGMENT = 0x1C,
372 HPROF_HEAP_DUMP_END = 0x2C,
373
374 // inlined object support
375 HPROF_FLAT_ARRAYS = 0x12,
376 HPROF_INLINED_FIELDS = 0x13,
377 // inlined object subrecords
378 HPROF_FLAT_ARRAY = 0x01,
379 HPROF_CLASS_WITH_INLINED_FIELDS = 0x01,
380
381 // field types
382 HPROF_ARRAY_OBJECT = 0x01,
383 HPROF_NORMAL_OBJECT = 0x02,
384 HPROF_BOOLEAN = 0x04,
385 HPROF_CHAR = 0x05,
386 HPROF_FLOAT = 0x06,
387 HPROF_DOUBLE = 0x07,
388 HPROF_BYTE = 0x08,
389 HPROF_SHORT = 0x09,
390 HPROF_INT = 0x0A,
391 HPROF_LONG = 0x0B,
392
393 // data-dump sub-records
394 HPROF_GC_ROOT_UNKNOWN = 0xFF,
395 HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
396 HPROF_GC_ROOT_JNI_LOCAL = 0x02,
397 HPROF_GC_ROOT_JAVA_FRAME = 0x03,
398 HPROF_GC_ROOT_NATIVE_STACK = 0x04,
399 HPROF_GC_ROOT_STICKY_CLASS = 0x05,
400 HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
401 HPROF_GC_ROOT_MONITOR_USED = 0x07,
402 HPROF_GC_ROOT_THREAD_OBJ = 0x08,
403 HPROF_GC_CLASS_DUMP = 0x20,
404 HPROF_GC_INSTANCE_DUMP = 0x21,
405 HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
406 HPROF_GC_PRIM_ARRAY_DUMP = 0x23
407 };
408
409 // Default stack trace ID (used for dummy HPROF_TRACE record)
410 enum {
411 STACK_TRACE_ID = 1,
412 INITIAL_CLASS_COUNT = 200
413 };
414
415
416 class AbstractDumpWriter;
417
418 class InlinedObjects {
419
420 struct ClassInlinedFields {
421 const Klass *klass;
422 uintx base_index; // base index of the inlined field names (1st field has index base_index+1).
423 ClassInlinedFields(const Klass *klass = nullptr, uintx base_index = 0) : klass(klass), base_index(base_index) {}
424
425 // For GrowableArray::find_sorted().
426 static int compare(const ClassInlinedFields& a, const ClassInlinedFields& b) {
427 return a.klass - b.klass;
428 }
429 // For GrowableArray::sort().
430 static int compare(ClassInlinedFields* a, ClassInlinedFields* b) {
431 return compare(*a, *b);
432 }
433 };
434
435 uintx _min_string_id;
436 uintx _max_string_id;
437
438 GrowableArray<ClassInlinedFields> *_inlined_field_map;
439
440 // counters for classes with inlined fields and for the fields
441 int _classes_count;
442 int _inlined_fields_count;
443
444 static InlinedObjects *_instance;
445
446 static void inlined_field_names_callback(InlinedObjects* _this, const Klass *klass, uintx base_index, int count);
447
448 GrowableArray<oop> *_flat_arrays;
449
450 public:
451 InlinedObjects()
452 : _min_string_id(0), _max_string_id(0),
453 _inlined_field_map(nullptr),
454 _classes_count(0), _inlined_fields_count(0),
455 _flat_arrays(nullptr) {
456 }
457
458 static InlinedObjects* get_instance() {
459 return _instance;
460 }
461
462 void init();
463 void release();
464
465 void dump_inlined_field_names(AbstractDumpWriter *writer);
466
467 uintx get_base_index_for(Klass* k);
468 uintx get_next_string_id(uintx id);
469
470 void dump_classed_with_inlined_fields(AbstractDumpWriter* writer);
471
472 void add_flat_array(oop array);
473 void dump_flat_arrays(AbstractDumpWriter* writer);
474
475 };
476
477 InlinedObjects *InlinedObjects::_instance = nullptr;
478
479
480 // Supports I/O operations for a dump
481 // Base class for dump and parallel dump
482 class AbstractDumpWriter : public StackObj {
483 protected:
484 enum {
485 io_buffer_max_size = 1*M,
486 io_buffer_max_waste = 10*K,
487 dump_segment_header_size = 9
488 };
489
490 char* _buffer; // internal buffer
491 size_t _size;
492 size_t _pos;
493
494 bool _in_dump_segment; // Are we currently in a dump segment?
495 bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
496 DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
497 DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
498
499 virtual void flush(bool force = false) = 0;
978 }
979 };
980
981 Monitor* ParDumpWriter::_lock = nullptr;
982
983 // Support class with a collection of functions used when dumping the heap
984
985 class DumperSupport : AllStatic {
986 public:
987
988 // write a header of the given type
989 static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
990
991 // returns hprof tag for the given type signature
992 static hprofTag sig2tag(Symbol* sig);
993 // returns hprof tag for the given basic type
994 static hprofTag type2tag(BasicType type);
995 // Returns the size of the data to write.
996 static u4 sig2size(Symbol* sig);
997
998 // calculates the total size of the all fields of the given class.
999 static u4 instance_size(InstanceKlass* ik);
1000
1001 // dump a jfloat
1002 static void dump_float(AbstractDumpWriter* writer, jfloat f);
1003 // dump a jdouble
1004 static void dump_double(AbstractDumpWriter* writer, jdouble d);
1005 // dumps the raw value of the given field
1006 static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);
1007 // returns the size of the static fields; also counts the static fields
1008 static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
1009 // dumps static fields of the given class
1010 static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
1011 // dump the raw values of the instance fields of the given identity or inlined object;
1012 // for identity objects offset is 0 and 'klass' is o->klass(),
1013 // for inlined objects offset is the offset in the holder object, 'klass' is inlined object class
1014 static void dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, InstanceKlass* klass);
1015 // dump the raw values of the instance fields of the given inlined object;
1016 // dump_instance_fields wrapper for inlined objects
1017 static void dump_inlined_object_fields(AbstractDumpWriter* writer, oop o, int offset, InlineKlass* klass);
1018
1019 // get the count of the instance fields for a given class
1020 static u2 get_instance_fields_count(InstanceKlass* ik);
1021 // dumps the definition of the instance fields for a given class
1022 static void dump_instance_field_descriptors(AbstractDumpWriter* writer, InstanceKlass* k, uintx *inlined_fields_index = nullptr);
1023 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1024 static void dump_instance(AbstractDumpWriter* writer, oop o);
1025 // creates HPROF_GC_CLASS_DUMP record for the given instance class
1026 static void dump_instance_class(AbstractDumpWriter* writer, Klass* k);
1027 // creates HPROF_GC_CLASS_DUMP record for a given array class
1028 static void dump_array_class(AbstractDumpWriter* writer, Klass* k);
1029
1030 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1031 static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
1032 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given flat array
1033 static void dump_flat_array(AbstractDumpWriter* writer, flatArrayOop array);
1034 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1035 static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
1036 // create HPROF_FRAME record for the given method and bci
1037 static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
1038
1039 // check if we need to truncate an array
1040 static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
1041 // extended version to dump flat arrays as primitive arrays;
1042 // type_size specifies size of the inlined objects.
1043 static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, int type_size, short header_size);
1044
1045 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
1046 static void end_of_dump(AbstractDumpWriter* writer);
1047
1048 static oop mask_dormant_archived_object(oop o) {
1049 if (o != nullptr && o->klass()->java_mirror() == nullptr) {
1050 // Ignore this object since the corresponding java mirror is not loaded.
1051 // Might be a dormant archive object.
1052 return nullptr;
1053 } else {
1054 return o;
1055 }
1056 }
1057
1058 // helper methods for inlined fields.
1059 static bool is_inlined_field(const FieldStream& fld) {
1060 return fld.field_descriptor().is_flat();
1061 }
1062 static InlineKlass* get_inlined_field_klass(const FieldStream &fld) {
1063 assert(is_inlined_field(fld), "must be inlined field");
1064 InstanceKlass* holder_klass = fld.field_descriptor().field_holder();
1065 return InlineKlass::cast(holder_klass->get_inline_type_field_klass(fld.index()));
1066 }
1067 };
1068
1069 // write a header of the given type
1070 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
1071 writer->write_u1(tag);
1072 writer->write_u4(0); // current ticks
1073 writer->write_u4(len);
1074 }
1075
1076 // returns hprof tag for the given type signature
1077 hprofTag DumperSupport::sig2tag(Symbol* sig) {
1078 switch (sig->char_at(0)) {
1079 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
1080 case JVM_SIGNATURE_PRIMITIVE_OBJECT: return HPROF_NORMAL_OBJECT; // not inlined Q-object, i.e. identity object.
1081 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
1082 case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
1083 case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
1084 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
1085 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
1086 case JVM_SIGNATURE_INT : return HPROF_INT;
1087 case JVM_SIGNATURE_LONG : return HPROF_LONG;
1088 case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
1089 case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
1090 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1091 }
1092 }
1093
1094 hprofTag DumperSupport::type2tag(BasicType type) {
1095 switch (type) {
1096 case T_BYTE : return HPROF_BYTE;
1097 case T_CHAR : return HPROF_CHAR;
1098 case T_FLOAT : return HPROF_FLOAT;
1099 case T_DOUBLE : return HPROF_DOUBLE;
1100 case T_INT : return HPROF_INT;
1101 case T_LONG : return HPROF_LONG;
1102 case T_SHORT : return HPROF_SHORT;
1103 case T_BOOLEAN : return HPROF_BOOLEAN;
1104 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1105 }
1106 }
1107
1108 u4 DumperSupport::sig2size(Symbol* sig) {
1109 switch (sig->char_at(0)) {
1110 case JVM_SIGNATURE_CLASS:
1111 case JVM_SIGNATURE_PRIMITIVE_OBJECT:
1112 case JVM_SIGNATURE_ARRAY: return sizeof(address);
1113 case JVM_SIGNATURE_BOOLEAN:
1114 case JVM_SIGNATURE_BYTE: return 1;
1115 case JVM_SIGNATURE_SHORT:
1116 case JVM_SIGNATURE_CHAR: return 2;
1117 case JVM_SIGNATURE_INT:
1118 case JVM_SIGNATURE_FLOAT: return 4;
1119 case JVM_SIGNATURE_LONG:
1120 case JVM_SIGNATURE_DOUBLE: return 8;
1121 default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
1122 }
1123 }
1124
1125 template<typename T, typename F> T bit_cast(F from) { // replace with the real thing when we can use c++20
1126 T to;
1127 static_assert(sizeof(to) == sizeof(from), "must be of the same size");
1128 memcpy(&to, &from, sizeof(to));
1129 return to;
1130 }
1131
1132 // dump a jfloat
1133 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
1134 if (g_isnan(f)) {
1135 writer->write_u4(0x7fc00000); // collapsing NaNs
1136 } else {
1137 writer->write_u4(bit_cast<u4>(f));
1138 }
1139 }
1140
1141 // dump a jdouble
1142 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
1143 if (g_isnan(d)) {
1144 writer->write_u8(0x7ff80000ull << 32); // collapsing NaNs
1145 } else {
1146 writer->write_u8(bit_cast<u8>(d));
1147 }
1148 }
1149
1150
1151 // dumps the raw value of the given field
1152 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {
1153 switch (type) {
1154 case JVM_SIGNATURE_CLASS :
1155 case JVM_SIGNATURE_PRIMITIVE_OBJECT: // not inlined Q-object, i.e. identity object.
1156 case JVM_SIGNATURE_ARRAY : {
1157 oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
1158 if (o != nullptr && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == nullptr) {
1159 ResourceMark rm;
1160 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1161 p2i(o), o->klass()->external_name(),
1162 p2i(obj), obj->klass()->external_name());
1163 }
1164 o = mask_dormant_archived_object(o);
1165 assert(oopDesc::is_oop_or_null(o), "Expected an oop or nullptr at " PTR_FORMAT, p2i(o));
1166 writer->write_objectID(o);
1167 break;
1168 }
1169 case JVM_SIGNATURE_BYTE : {
1170 jbyte b = obj->byte_field(offset);
1171 writer->write_u1(b);
1172 break;
1173 }
1174 case JVM_SIGNATURE_CHAR : {
1175 jchar c = obj->char_field(offset);
1196 writer->write_u4(i);
1197 break;
1198 }
1199 case JVM_SIGNATURE_LONG : {
1200 jlong l = obj->long_field(offset);
1201 writer->write_u8(l);
1202 break;
1203 }
1204 case JVM_SIGNATURE_BOOLEAN : {
1205 jboolean b = obj->bool_field(offset);
1206 writer->write_u1(b);
1207 break;
1208 }
1209 default : {
1210 ShouldNotReachHere();
1211 break;
1212 }
1213 }
1214 }
1215
1216 // calculates the total size of the all fields of the given class.
1217 u4 DumperSupport::instance_size(InstanceKlass *ik) {
1218 u4 size = 0;
1219
1220 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1221 if (!fld.access_flags().is_static()) {
1222 if (is_inlined_field(fld)) {
1223 size += instance_size(get_inlined_field_klass(fld));
1224 } else {
1225 size += sig2size(fld.signature());
1226 }
1227 }
1228 }
1229 return size;
1230 }
1231
1232 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1233 field_count = 0;
1234 u4 size = 0;
1235
1236 for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1237 if (fldc.access_flags().is_static()) {
1238 assert(!is_inlined_field(fldc), "static fields cannot be inlined");
1239
1240 field_count++;
1241 size += sig2size(fldc.signature());
1242 }
1243 }
1244
1245 // Add in resolved_references which is referenced by the cpCache
1246 // The resolved_references is an array per InstanceKlass holding the
1247 // strings and other oops resolved from the constant pool.
1248 oop resolved_references = ik->constants()->resolved_references_or_null();
1249 if (resolved_references != nullptr) {
1250 field_count++;
1251 size += sizeof(address);
1252
1253 // Add in the resolved_references of the used previous versions of the class
1254 // in the case of RedefineClasses
1255 InstanceKlass* prev = ik->previous_versions();
1256 while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1257 field_count++;
1258 size += sizeof(address);
1259 prev = prev->previous_versions();
1260 }
1261 }
1262
1263 // We write the value itself plus a name and a one byte type tag per field.
1264 return size + field_count * (sizeof(address) + 1);
1265 }
1266
1267 // dumps static fields of the given class
1268 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1269 InstanceKlass* ik = InstanceKlass::cast(k);
1270
1271 // dump the field descriptors and raw values
1272 for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1273 if (fld.access_flags().is_static()) {
1274 assert(!is_inlined_field(fld), "static fields cannot be inlined");
1275
1276 Symbol* sig = fld.signature();
1277
1278 writer->write_symbolID(fld.name()); // name
1279 writer->write_u1(sig2tag(sig)); // type
1280
1281 // value
1282 dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1283 }
1284 }
1285
1286 // Add resolved_references for each class that has them
1287 oop resolved_references = ik->constants()->resolved_references_or_null();
1288 if (resolved_references != nullptr) {
1289 writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1290 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1291 writer->write_objectID(resolved_references);
1292
1293 // Also write any previous versions
1294 InstanceKlass* prev = ik->previous_versions();
1295 while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1296 writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1297 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1298 writer->write_objectID(prev->constants()->resolved_references());
1299 prev = prev->previous_versions();
1300 }
1301 }
1302 }
1303
1304 // dump the raw values of the instance fields of the given identity or inlined object;
1305 // for identity objects offset is 0 and 'klass' is o->klass(),
1306 // for inlined objects offset is the offset in the holder object, 'klass' is inlined object class.
1307 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, InstanceKlass *klass) {
1308 for (FieldStream fld(klass, false, false); !fld.eos(); fld.next()) {
1309 if (!fld.access_flags().is_static()) {
1310 if (is_inlined_field(fld)) {
1311 InlineKlass* field_klass = get_inlined_field_klass(fld);
1312 // the field is inlined, so all its fields are stored without headers.
1313 int fields_offset = offset + fld.offset() - field_klass->first_field_offset();
1314 dump_inlined_object_fields(writer, o, offset + fld.offset(), field_klass);
1315 } else {
1316 Symbol* sig = fld.signature();
1317 dump_field_value(writer, sig->char_at(0), o, offset + fld.offset());
1318 }
1319 }
1320 }
1321 }
1322
1323 void DumperSupport::dump_inlined_object_fields(AbstractDumpWriter* writer, oop o, int offset, InlineKlass* klass) {
1324 // the object is inlined, so all its fields are stored without headers.
1325 dump_instance_fields(writer, o, offset - klass->first_field_offset(), klass);
1326 }
1327
1328 // gets the count of the instance fields for a given class
1329 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1330 u2 field_count = 0;
1331
1332 for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1333 if (!fldc.access_flags().is_static()) {
1334 if (is_inlined_field(fldc)) {
1335 // add "synthetic" fields for inlined fields.
1336 field_count += get_instance_fields_count(get_inlined_field_klass(fldc));
1337 } else {
1338 field_count++;
1339 }
1340 }
1341 }
1342
1343 return field_count;
1344 }
1345
1346 // dumps the definition of the instance fields for a given class
1347 // inlined_fields_id is not-nullptr for inlined fields (to get synthetic field name IDs
1348 // by using InlinedObjects::get_next_string_id()).
1349 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, InstanceKlass* ik, uintx* inlined_fields_id) {
1350 // inlined_fields_id != nullptr means ik is a class of inlined field.
1351 // Inlined field id pointer for this class; lazyly initialized
1352 // if the class has inlined field(s) and the caller didn't provide inlined_fields_id.
1353 uintx *this_klass_inlined_fields_id = inlined_fields_id;
1354 uintx inlined_id = 0;
1355
1356 // dump the field descriptors
1357 for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1358 if (!fld.access_flags().is_static()) {
1359 if (is_inlined_field(fld)) {
1360 // dump "synthetic" fields for inlined fields.
1361 if (this_klass_inlined_fields_id == nullptr) {
1362 inlined_id = InlinedObjects::get_instance()->get_base_index_for(ik);
1363 this_klass_inlined_fields_id = &inlined_id;
1364 }
1365 dump_instance_field_descriptors(writer, get_inlined_field_klass(fld), this_klass_inlined_fields_id);
1366 } else {
1367 Symbol* sig = fld.signature();
1368 Symbol* name = nullptr;
1369 // Use inlined_fields_id provided by caller.
1370 if (inlined_fields_id != nullptr) {
1371 uintx name_id = InlinedObjects::get_instance()->get_next_string_id(*inlined_fields_id);
1372
1373 // name_id == 0 is returned on error. use original field signature.
1374 if (name_id != 0) {
1375 *inlined_fields_id = name_id;
1376 name = reinterpret_cast<Symbol*>(name_id);
1377 }
1378 }
1379 if (name == nullptr) {
1380 name = fld.name();
1381 }
1382
1383 writer->write_symbolID(name); // name
1384 writer->write_u1(sig2tag(sig)); // type
1385 }
1386 }
1387 }
1388 }
1389
1390 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1391 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o) {
1392 InstanceKlass* ik = InstanceKlass::cast(o->klass());
1393 u4 is = instance_size(ik);
1394 u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1395
1396 writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1397 writer->write_objectID(o);
1398 writer->write_u4(STACK_TRACE_ID);
1399
1400 // class ID
1401 writer->write_classID(ik);
1402
1403 // number of bytes that follow
1404 writer->write_u4(is);
1405
1406 // field values
1407 dump_instance_fields(writer, o, 0, ik);
1408
1409 writer->end_sub_record();
1410 }
1411
1412 // creates HPROF_GC_CLASS_DUMP record for the given instance class
1413 void DumperSupport::dump_instance_class(AbstractDumpWriter* writer, Klass* k) {
1414 InstanceKlass* ik = InstanceKlass::cast(k);
1415
1416 // We can safepoint and do a heap dump at a point where we have a Klass,
1417 // but no java mirror class has been setup for it. So we need to check
1418 // that the class is at least loaded, to avoid crash from a null mirror.
1419 if (!ik->is_loaded()) {
1420 return;
1421 }
1422
1423 u2 static_fields_count = 0;
1424 u4 static_size = get_static_fields_size(ik, static_fields_count);
1425 u2 instance_fields_count = get_instance_fields_count(ik);
1426 u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1427 u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size;
1432 writer->write_classID(ik);
1433 writer->write_u4(STACK_TRACE_ID);
1434
1435 // super class ID
1436 InstanceKlass* java_super = ik->java_super();
1437 if (java_super == nullptr) {
1438 writer->write_objectID(oop(nullptr));
1439 } else {
1440 writer->write_classID(java_super);
1441 }
1442
1443 writer->write_objectID(ik->class_loader());
1444 writer->write_objectID(ik->signers());
1445 writer->write_objectID(ik->protection_domain());
1446
1447 // reserved
1448 writer->write_objectID(oop(nullptr));
1449 writer->write_objectID(oop(nullptr));
1450
1451 // instance size
1452 writer->write_u4(HeapWordSize * ik->size_helper());
1453
1454 // size of constant pool - ignored by HAT 1.1
1455 writer->write_u2(0);
1456
1457 // static fields
1458 writer->write_u2(static_fields_count);
1459 dump_static_fields(writer, ik);
1460
1461 // description of instance fields
1462 writer->write_u2(instance_fields_count);
1463 dump_instance_field_descriptors(writer, ik);
1464
1465 writer->end_sub_record();
1466 }
1467
1468 // creates HPROF_GC_CLASS_DUMP record for the given array class
1469 void DumperSupport::dump_array_class(AbstractDumpWriter* writer, Klass* k) {
1470 InstanceKlass* ik = nullptr; // bottom class for object arrays, null for primitive type arrays
1471 if (k->is_objArray_klass()) {
1472 Klass *bk = ObjArrayKlass::cast(k)->bottom_klass();
1486 assert(java_super != nullptr, "checking");
1487 writer->write_classID(java_super);
1488
1489 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->class_loader());
1490 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->signers());
1491 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->protection_domain());
1492
1493 writer->write_objectID(oop(nullptr)); // reserved
1494 writer->write_objectID(oop(nullptr));
1495 writer->write_u4(0); // instance size
1496 writer->write_u2(0); // constant pool
1497 writer->write_u2(0); // static fields
1498 writer->write_u2(0); // instance fields
1499
1500 writer->end_sub_record();
1501
1502 }
1503
1504 // Hprof uses an u4 as record length field,
1505 // which means we need to truncate arrays that are too long.
1506 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, int type_size, short header_size) {
1507 int length = array->length();
1508
1509 size_t length_in_bytes = (size_t)length * type_size;
1510 uint max_bytes = max_juint - header_size;
1511
1512 if (length_in_bytes > max_bytes) {
1513 length = max_bytes / type_size;
1514 length_in_bytes = (size_t)length * type_size;
1515
1516 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1517 warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1518 type2name_tab[type], array->length(), length);
1519 }
1520 return length;
1521 }
1522
1523 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1524 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1525 assert((type >= T_BOOLEAN && type <= T_OBJECT) || type == T_PRIMITIVE_OBJECT, "invalid array element type");
1526 int type_size;
1527 if (type == T_OBJECT || type == T_PRIMITIVE_OBJECT) { // TODO: FIXME
1528 type_size = sizeof(address);
1529 } else {
1530 type_size = type2aelembytes(type);
1531 }
1532
1533 return calculate_array_max_length(writer, array, type_size, header_size);
1534 }
1535
1536 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1537 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1538 // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1539 short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1540 int length = calculate_array_max_length(writer, array, header_size);
1541 u4 size = header_size + length * sizeof(address);
1542
1543 writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1544 writer->write_objectID(array);
1545 writer->write_u4(STACK_TRACE_ID);
1546 writer->write_u4(length);
1547
1548 // array class ID
1549 writer->write_classID(array->klass());
1550
1551 // [id]* elements
1552 for (int index = 0; index < length; index++) {
1553 oop o = array->obj_at(index);
1554 if (o != nullptr && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == nullptr) {
1555 ResourceMark rm;
1556 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1557 p2i(o), o->klass()->external_name(),
1558 p2i(array), array->klass()->external_name());
1559 }
1560 o = mask_dormant_archived_object(o);
1561 writer->write_objectID(o);
1562 }
1563
1564 writer->end_sub_record();
1565 }
1566
1567 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given flat array
1568 void DumperSupport::dump_flat_array(AbstractDumpWriter* writer, flatArrayOop array) {
1569 FlatArrayKlass* array_klass = FlatArrayKlass::cast(array->klass());
1570 InlineKlass* element_klass = array_klass->element_klass();
1571 int element_size = instance_size(element_klass);
1572 /* id array object ID
1573 * u4 stack trace serial number
1574 * u4 number of elements
1575 * u1 element type
1576 */
1577 short header_size = 1 + sizeof(address) + 2 * 4 + 1;
1578
1579 // TODO: use T_SHORT/T_INT/T_LONG if needed to avoid truncation
1580 BasicType type = T_BYTE;
1581 int type_size = type2aelembytes(type);
1582 int length = calculate_array_max_length(writer, array, element_size, header_size);
1583 u4 length_in_bytes = (u4)(length * element_size);
1584 u4 size = header_size + length_in_bytes;
1585
1586 writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1587 writer->write_objectID(array);
1588 writer->write_u4(STACK_TRACE_ID);
1589 // TODO: round up array length for T_SHORT/T_INT/T_LONG
1590 writer->write_u4(length * element_size);
1591 writer->write_u1(type2tag(type));
1592
1593 for (int index = 0; index < length; index++) {
1594 // need offset in the holder to read inlined object. calculate it from flatArrayOop::value_at_addr()
1595 int offset = (int)((address)array->value_at_addr(index, array_klass->layout_helper())
1596 - cast_from_oop<address>(array));
1597 dump_inlined_object_fields(writer, array, offset, element_klass);
1598 }
1599
1600 // TODO: write padding bytes for T_SHORT/T_INT/T_LONG
1601
1602 InlinedObjects::get_instance()->add_flat_array(array);
1603
1604 writer->end_sub_record();
1605 }
1606
1607 #define WRITE_ARRAY(Array, Type, Size, Length) \
1608 for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1609
1610 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1611 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1612 BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1613 // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1614 short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1615
1616 int length = calculate_array_max_length(writer, array, header_size);
1617 int type_size = type2aelembytes(type);
1618 u4 length_in_bytes = (u4)length * type_size;
1619 u4 size = header_size + length_in_bytes;
1620
1621 writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1622 writer->write_objectID(array);
1623 writer->write_u4(STACK_TRACE_ID);
1624 writer->write_u4(length);
1625 writer->write_u1(type2tag(type));
1626
1708 int bci) {
1709 int line_number;
1710 if (m->is_native()) {
1711 line_number = -3; // native frame
1712 } else {
1713 line_number = m->line_number_from_bci(bci);
1714 }
1715
1716 write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1717 writer->write_id(frame_serial_num); // frame serial number
1718 writer->write_symbolID(m->name()); // method's name
1719 writer->write_symbolID(m->signature()); // method's signature
1720
1721 assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1722 writer->write_symbolID(m->method_holder()->source_file_name()); // source file name
1723 writer->write_u4(class_serial_num); // class serial number
1724 writer->write_u4((u4) line_number); // line number
1725 }
1726
1727
1728 class InlinedFieldNameDumper : public LockedClassesDo {
1729 public:
1730 typedef void (*Callback)(InlinedObjects *owner, const Klass *klass, uintx base_index, int count);
1731
1732 private:
1733 AbstractDumpWriter* _writer;
1734 InlinedObjects *_owner;
1735 Callback _callback;
1736 uintx _index;
1737
1738 void dump_inlined_field_names(GrowableArray<Symbol*>* super_names, Symbol* field_name, InlineKlass* klass) {
1739 super_names->push(field_name);
1740 for (FieldStream fld(klass, false, false); !fld.eos(); fld.next()) {
1741 if (!fld.access_flags().is_static()) {
1742 if (DumperSupport::is_inlined_field(fld)) {
1743 dump_inlined_field_names(super_names, fld.name(), DumperSupport::get_inlined_field_klass(fld));
1744 } else {
1745 // get next string ID.
1746 uintx next_index = _owner->get_next_string_id(_index);
1747 if (next_index == 0) {
1748 // something went wrong (overflow?)
1749 // stop generation; the rest of inlined objects will have original field names.
1750 return;
1751 }
1752 _index = next_index;
1753
1754 // Calculate length.
1755 int len = fld.name()->utf8_length();
1756 for (GrowableArrayIterator<Symbol*> it = super_names->begin(); it != super_names->end(); ++it) {
1757 len += (*it)->utf8_length() + 1; // +1 for ".".
1758 }
1759
1760 DumperSupport::write_header(_writer, HPROF_UTF8, oopSize + len);
1761 _writer->write_symbolID(reinterpret_cast<Symbol*>(_index));
1762 // Write the string value.
1763 // 1) super_names.
1764 for (GrowableArrayIterator<Symbol*> it = super_names->begin(); it != super_names->end(); ++it) {
1765 _writer->write_raw((*it)->bytes(), (*it)->utf8_length());
1766 _writer->write_u1('.');
1767 }
1768 // 2) field name.
1769 _writer->write_raw(fld.name()->bytes(), fld.name()->utf8_length());
1770 }
1771 }
1772 }
1773 super_names->pop();
1774 }
1775
1776 void dump_inlined_field_names(Symbol* field_name, InlineKlass* field_klass) {
1777 GrowableArray<Symbol*> super_names(4, mtServiceability);
1778 dump_inlined_field_names(&super_names, field_name, field_klass);
1779 }
1780
1781 public:
1782 InlinedFieldNameDumper(AbstractDumpWriter* writer, InlinedObjects* owner, Callback callback)
1783 : _writer(writer), _owner(owner), _callback(callback), _index(0) {
1784 }
1785
1786 void do_klass(Klass* k) {
1787 if (!k->is_instance_klass()) {
1788 return;
1789 }
1790 InstanceKlass* ik = InstanceKlass::cast(k);
1791 // if (ik->has_inline_type_fields()) {
1792 // return;
1793 // }
1794
1795 uintx base_index = _index;
1796 int count = 0;
1797
1798 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1799 if (!fld.access_flags().is_static()) {
1800 if (DumperSupport::is_inlined_field(fld)) {
1801 dump_inlined_field_names(fld.name(), DumperSupport::get_inlined_field_klass(fld));
1802 count++;
1803 }
1804 }
1805 }
1806
1807 if (count != 0) {
1808 _callback(_owner, k, base_index, count);
1809 }
1810 }
1811 };
1812
1813 class InlinedFieldsDumper : public LockedClassesDo {
1814 private:
1815 AbstractDumpWriter* _writer;
1816
1817 public:
1818 InlinedFieldsDumper(AbstractDumpWriter* writer) : _writer(writer) {}
1819
1820 void do_klass(Klass* k) {
1821 if (!k->is_instance_klass()) {
1822 return;
1823 }
1824 InstanceKlass* ik = InstanceKlass::cast(k);
1825 // if (ik->has_inline_type_fields()) {
1826 // return;
1827 // }
1828
1829 // We can be at a point where java mirror does not exist yet.
1830 // So we need to check that the class is at least loaded, to avoid crash from a null mirror.
1831 if (!ik->is_loaded()) {
1832 return;
1833 }
1834
1835 u2 inlined_count = 0;
1836 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1837 if (!fld.access_flags().is_static()) {
1838 if (DumperSupport::is_inlined_field(fld)) {
1839 inlined_count++;
1840 }
1841 }
1842 }
1843 if (inlined_count != 0) {
1844 _writer->write_u1(HPROF_CLASS_WITH_INLINED_FIELDS);
1845
1846 // class ID
1847 _writer->write_classID(ik);
1848 // number of inlined fields
1849 _writer->write_u2(inlined_count);
1850 u2 index = 0;
1851 for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1852 if (!fld.access_flags().is_static()) {
1853 if (DumperSupport::is_inlined_field(fld)) {
1854 // inlined field index
1855 _writer->write_u2(index);
1856 // synthetic field count
1857 u2 field_count = DumperSupport::get_instance_fields_count(DumperSupport::get_inlined_field_klass(fld));
1858 _writer->write_u2(field_count);
1859 // original field name
1860 _writer->write_symbolID(fld.name());
1861 // inlined field class ID
1862 _writer->write_classID(DumperSupport::get_inlined_field_klass(fld));
1863
1864 index += field_count;
1865 } else {
1866 index++;
1867 }
1868 }
1869 }
1870 }
1871 }
1872 };
1873
1874
1875 void InlinedObjects::init() {
1876 _instance = this;
1877
1878 struct Closure : public SymbolClosure {
1879 uintx _min_id = max_uintx;
1880 uintx _max_id = 0;
1881 Closure() : _min_id(max_uintx), _max_id(0) {}
1882
1883 void do_symbol(Symbol** p) {
1884 uintx val = reinterpret_cast<uintx>(*p);
1885 if (val < _min_id) {
1886 _min_id = val;
1887 }
1888 if (val > _max_id) {
1889 _max_id = val;
1890 }
1891 }
1892 } closure;
1893
1894 SymbolTable::symbols_do(&closure);
1895
1896 _min_string_id = closure._min_id;
1897 _max_string_id = closure._max_id;
1898 }
1899
1900 void InlinedObjects::release() {
1901 _instance = nullptr;
1902
1903 if (_inlined_field_map != nullptr) {
1904 delete _inlined_field_map;
1905 _inlined_field_map = nullptr;
1906 }
1907 if (_flat_arrays != nullptr) {
1908 delete _flat_arrays;
1909 _flat_arrays = nullptr;
1910 }
1911 }
1912
1913 void InlinedObjects::inlined_field_names_callback(InlinedObjects* _this, const Klass* klass, uintx base_index, int count) {
1914 if (_this->_inlined_field_map == nullptr) {
1915 _this->_inlined_field_map = new (mtServiceability) GrowableArray<ClassInlinedFields>(100, mtServiceability);
1916 }
1917 _this->_inlined_field_map->append(ClassInlinedFields(klass, base_index));
1918
1919 // counters for dumping classes with inlined fields
1920 _this->_classes_count++;
1921 _this->_inlined_fields_count += count;
1922 }
1923
1924 void InlinedObjects::dump_inlined_field_names(AbstractDumpWriter* writer) {
1925 InlinedFieldNameDumper nameDumper(writer, this, inlined_field_names_callback);
1926 ClassLoaderDataGraph::classes_do(&nameDumper);
1927
1928 if (_inlined_field_map != nullptr) {
1929 // prepare the map for get_base_index_for().
1930 _inlined_field_map->sort(ClassInlinedFields::compare);
1931 }
1932 }
1933
1934 uintx InlinedObjects::get_base_index_for(Klass* k) {
1935 if (_inlined_field_map != nullptr) {
1936 bool found = false;
1937 int idx = _inlined_field_map->find_sorted<ClassInlinedFields, ClassInlinedFields::compare>(ClassInlinedFields(k, 0), found);
1938 if (found) {
1939 return _inlined_field_map->at(idx).base_index;
1940 }
1941 }
1942
1943 // return max_uintx, so get_next_string_id returns 0.
1944 return max_uintx;
1945 }
1946
1947 uintx InlinedObjects::get_next_string_id(uintx id) {
1948 if (++id == _min_string_id) {
1949 return _max_string_id + 1;
1950 }
1951 return id;
1952 }
1953
1954 void InlinedObjects::dump_classed_with_inlined_fields(AbstractDumpWriter* writer) {
1955 if (_classes_count != 0) {
1956 // Record for each class contains tag(u1), class ID and count(u2)
1957 // for each inlined field index(u2), synthetic fields count(u2), original field name and class ID
1958 int size = _classes_count * (1 + sizeof(address) + 2)
1959 + _inlined_fields_count * (2 + 2 + sizeof(address) + sizeof(address));
1960 DumperSupport::write_header(writer, HPROF_INLINED_FIELDS, (u4)size);
1961
1962 InlinedFieldsDumper dumper(writer);
1963 ClassLoaderDataGraph::classes_do(&dumper);
1964 }
1965 }
1966
1967 void InlinedObjects::add_flat_array(oop array) {
1968 if (_flat_arrays == nullptr) {
1969 _flat_arrays = new (mtServiceability) GrowableArray<oop>(100, mtServiceability);
1970 }
1971 _flat_arrays->append(array);
1972 }
1973
1974 void InlinedObjects::dump_flat_arrays(AbstractDumpWriter* writer) {
1975 if (_flat_arrays != nullptr) {
1976 // For each flat array the record contains tag (u1), object ID and class ID.
1977 int size = _flat_arrays->length() * (1 + sizeof(address) + sizeof(address));
1978
1979 DumperSupport::write_header(writer, HPROF_FLAT_ARRAYS, (u4)size);
1980 for (GrowableArrayIterator<oop> it = _flat_arrays->begin(); it != _flat_arrays->end(); ++it) {
1981 flatArrayOop array = flatArrayOop(*it);
1982 FlatArrayKlass* array_klass = FlatArrayKlass::cast(array->klass());
1983 InlineKlass* element_klass = array_klass->element_klass();
1984 writer->write_u1(HPROF_FLAT_ARRAY);
1985 writer->write_objectID(array);
1986 writer->write_classID(element_klass);
1987 }
1988 }
1989 }
1990
1991
1992 // Support class used to generate HPROF_UTF8 records from the entries in the
1993 // SymbolTable.
1994
1995 class SymbolTableDumper : public SymbolClosure {
1996 private:
1997 AbstractDumpWriter* _writer;
1998 AbstractDumpWriter* writer() const { return _writer; }
1999 public:
2000 SymbolTableDumper(AbstractDumpWriter* writer) { _writer = writer; }
2001 void do_symbol(Symbol** p);
2002 };
2003
2004 void SymbolTableDumper::do_symbol(Symbol** p) {
2005 ResourceMark rm;
2006 Symbol* sym = *p;
2007 int len = sym->utf8_length();
2008 if (len > 0) {
2009 char* s = sym->as_utf8();
2010 DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
2011 writer()->write_symbolID(sym);
2194 }
2195
2196 if (DumperSupport::mask_dormant_archived_object(o) == nullptr) {
2197 log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name());
2198 return;
2199 }
2200
2201 // If large object list exists and it is large object/array,
2202 // add oop into the list and skip scan. VM thread will process it later.
2203 if (_list != nullptr && is_large(o)) {
2204 _list->atomic_push(o);
2205 return;
2206 }
2207
2208 if (o->is_instance()) {
2209 // create a HPROF_GC_INSTANCE record for each object
2210 DumperSupport::dump_instance(writer(), o);
2211 } else if (o->is_objArray()) {
2212 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
2213 DumperSupport::dump_object_array(writer(), objArrayOop(o));
2214 } else if (o->is_flatArray()) {
2215 DumperSupport::dump_flat_array(writer(), flatArrayOop(o));
2216 } else if (o->is_typeArray()) {
2217 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
2218 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
2219 }
2220 }
2221
2222 bool HeapObjectDumper::is_large(oop o) {
2223 size_t size = 0;
2224 if (o->is_instance()) {
2225 // Use o->size() * 8 as the upper limit of instance size to avoid iterating static fields
2226 size = o->size() * 8;
2227 } else if (o->is_objArray()) {
2228 objArrayOop array = objArrayOop(o);
2229 BasicType type = ArrayKlass::cast(array->klass())->element_type();
2230 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
2231 int length = array->length();
2232 int type_size = sizeof(address);
2233 size = (size_t)length * type_size;
2234 } else if (o->is_typeArray()) {
2235 flatArrayOop array = flatArrayOop(o);
2236 BasicType type = ArrayKlass::cast(array->klass())->element_type();
2237 assert(type == T_PRIMITIVE_OBJECT, "invalid array element type");
2238 int length = array->length();
2239 //TODO: FIXME
2240 //int type_size = type2aelembytes(type);
2241 //size = (size_t)length * type_size;
2242 } else if (o->is_typeArray()) {
2243 typeArrayOop array = typeArrayOop(o);
2244 BasicType type = ArrayKlass::cast(array->klass())->element_type();
2245 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
2246 int length = array->length();
2247 int type_size = type2aelembytes(type);
2248 size = (size_t)length * type_size;
2249 }
2250 return size > HeapDumpLargeObjectList::LargeObjectSizeThreshold;
2251 }
2252
2253 // The dumper controller for parallel heap dump
2254 class DumperController : public CHeapObj<mtInternal> {
2255 private:
2256 bool _started;
2257 Monitor* _lock;
2258 uint _dumper_number;
2259 uint _complete_number;
2260
2261 public:
2294 MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2295 while (_complete_number != _dumper_number) {
2296 ml.wait();
2297 }
2298 _started = false;
2299 }
2300 };
2301
2302 // The VM operation that performs the heap dump
2303 class VM_HeapDumper : public VM_GC_Operation, public WorkerTask {
2304 private:
2305 static VM_HeapDumper* _global_dumper;
2306 static DumpWriter* _global_writer;
2307 DumpWriter* _local_writer;
2308 JavaThread* _oome_thread;
2309 Method* _oome_constructor;
2310 bool _gc_before_heap_dump;
2311 GrowableArray<Klass*>* _klass_map;
2312 ThreadStackTrace** _stack_traces;
2313 int _num_threads;
2314
2315 // Inlined object support.
2316 InlinedObjects _inlined_objects;
2317 InlinedObjects* inlined_objects() { return &_inlined_objects; }
2318
2319 // parallel heap dump support
2320 uint _num_dumper_threads;
2321 uint _num_writer_threads;
2322 DumperController* _dumper_controller;
2323 ParallelObjectIterator* _poi;
2324 HeapDumpLargeObjectList* _large_object_list;
2325
2326 // VMDumperType is for thread that dumps both heap and non-heap data.
2327 static const size_t VMDumperType = 0;
2328 static const size_t WriterType = 1;
2329 static const size_t DumperType = 2;
2330 // worker id of VMDumper thread.
2331 static const size_t VMDumperWorkerId = 0;
2332
2333 size_t get_worker_type(uint worker_id) {
2334 assert(_num_writer_threads >= 1, "Must be at least one writer");
2335 // worker id of VMDumper that dump heap and non-heap data
2336 if (worker_id == VMDumperWorkerId) {
2337 return VMDumperType;
2338 }
2723 writer()->writer_loop();
2724 return;
2725 }
2726 if (_num_dumper_threads > 1 && get_worker_type(worker_id) == DumperType) {
2727 _dumper_controller->wait_for_start_signal();
2728 }
2729 } else {
2730 // The worker 0 on all non-heap data dumping and part of heap iteration.
2731 // Write the file header - we always use 1.0.2
2732 const char* header = "JAVA PROFILE 1.0.2";
2733
2734 // header is few bytes long - no chance to overflow int
2735 writer()->write_raw(header, strlen(header) + 1); // NUL terminated
2736 writer()->write_u4(oopSize);
2737 // timestamp is current time in ms
2738 writer()->write_u8(os::javaTimeMillis());
2739 // HPROF_UTF8 records
2740 SymbolTableDumper sym_dumper(writer());
2741 SymbolTable::symbols_do(&sym_dumper);
2742
2743 // HPROF_UTF8 records for inlined field names.
2744 inlined_objects()->init();
2745 inlined_objects()->dump_inlined_field_names(writer());
2746
2747 // HPROF_INLINED_FIELDS
2748 inlined_objects()->dump_classed_with_inlined_fields(writer());
2749
2750 // write HPROF_LOAD_CLASS records
2751 {
2752 LockedClassesDo locked_load_classes(&do_load_class);
2753 ClassLoaderDataGraph::classes_do(&locked_load_classes);
2754 }
2755
2756 // write HPROF_FRAME and HPROF_TRACE records
2757 // this must be called after _klass_map is built when iterating the classes above.
2758 dump_stack_traces();
2759
2760 // Writes HPROF_GC_CLASS_DUMP records
2761 {
2762 LockedClassesDo locked_dump_class(&do_class_dump);
2763 ClassLoaderDataGraph::classes_do(&locked_dump_class);
2764 }
2765
2766 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
2767 do_threads();
2768
2769 // HPROF_GC_ROOT_JNI_GLOBAL
2808 if (get_worker_type(worker_id) == VMDumperType) {
2809 _dumper_controller->wait_all_dumpers_complete();
2810 // clear internal buffer;
2811 pw.finish_dump_segment(true);
2812 // refresh the global_writer's buffer and position;
2813 writer()->refresh();
2814 } else {
2815 pw.finish_dump_segment(true);
2816 _dumper_controller->dumper_complete();
2817 return;
2818 }
2819 }
2820 }
2821
2822 assert(get_worker_type(worker_id) == VMDumperType, "Heap dumper must be VMDumper");
2823 // Use writer() rather than ParDumpWriter to avoid memory consumption.
2824 HeapObjectDumper obj_dumper(writer());
2825 dump_large_objects(&obj_dumper);
2826 // Writes the HPROF_HEAP_DUMP_END record.
2827 DumperSupport::end_of_dump(writer());
2828
2829 inlined_objects()->dump_flat_arrays(writer());
2830
2831 // We are done with writing. Release the worker threads.
2832 writer()->deactivate();
2833
2834 inlined_objects()->release();
2835 }
2836
2837 void VM_HeapDumper::dump_stack_traces() {
2838 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
2839 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
2840 writer()->write_u4((u4) STACK_TRACE_ID);
2841 writer()->write_u4(0); // thread number
2842 writer()->write_u4(0); // frame count
2843
2844 _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
2845 int frame_serial_num = 0;
2846 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
2847 oop threadObj = thread->threadObj();
2848 if (threadObj != nullptr && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
2849 // dump thread stack trace
2850 Thread* current_thread = Thread::current();
2851 ResourceMark rm(current_thread);
2852 HandleMark hm(current_thread);
2853
2854 ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
|